xinstall.c revision 1.31 1 /* $NetBSD: xinstall.c,v 1.31 1999/01/25 01:42:57 hubertf Exp $ */
2
3 /*
4 * Copyright (c) 1987, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #include <sys/cdefs.h>
37 #ifndef lint
38 __COPYRIGHT("@(#) Copyright (c) 1987, 1993\n\
39 The Regents of the University of California. All rights reserved.\n");
40 #endif /* not lint */
41
42 #ifndef lint
43 #if 0
44 static char sccsid[] = "@(#)xinstall.c 8.1 (Berkeley) 7/21/93";
45 #else
46 __RCSID("$NetBSD: xinstall.c,v 1.31 1999/01/25 01:42:57 hubertf Exp $");
47 #endif
48 #endif /* not lint */
49
50 #include <sys/param.h>
51 #include <sys/wait.h>
52 #include <sys/mman.h>
53 #include <sys/stat.h>
54
55 #include <ctype.h>
56 #include <errno.h>
57 #include <fcntl.h>
58 #include <grp.h>
59 #include <paths.h>
60 #include <pwd.h>
61 #include <stdio.h>
62 #include <stdlib.h>
63 #include <string.h>
64 #include <unistd.h>
65 #include <err.h>
66
67 #include "pathnames.h"
68 #include "stat_flags.h"
69
70 #define STRIP_ARGS_MAX 32
71 #define BACKUP_SUFFIX ".old"
72
73 struct passwd *pp;
74 struct group *gp;
75 int dobackup=0, docopy=0, dodir=0, dostrip=0, dolink=0, dopreserve=0;
76 int mode = S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
77 char pathbuf[MAXPATHLEN];
78 uid_t uid;
79 gid_t gid;
80 char *stripArgs=NULL;
81 char *suffix=BACKUP_SUFFIX;
82
83 #define LN_ABSOLUTE 0x01
84 #define LN_RELATIVE 0x02
85 #define LN_HARD 0x04
86 #define LN_SYMBOLIC 0x08
87 #define LN_MIXED 0x10
88
89 #define DIRECTORY 0x01 /* Tell install it's a directory. */
90 #define SETFLAGS 0x02 /* Tell install to set flags. */
91
92 void copy __P((int, char *, int, char *, off_t));
93 void makelink __P((char *, char *));
94 void install __P((char *, char *, u_long, u_int));
95 void install_dir __P((char *));
96 void strip __P((char *));
97 void usage __P((void));
98 int main __P((int, char *[]));
99
100 int
101 main(argc, argv)
102 int argc;
103 char *argv[];
104 {
105 struct stat from_sb, to_sb;
106 mode_t *set;
107 u_long fset;
108 u_int iflags;
109 int ch, no_target;
110 char *p;
111 char *flags = NULL, *to_name, *group = NULL, *owner = NULL;
112
113 iflags = 0;
114 while ((ch = getopt(argc, argv, "cbB:df:g:l:m:o:psS:")) != -1)
115 switch((char)ch) {
116 case 'B':
117 suffix = optarg;
118 /* fall through; -B implies -b */
119 case 'b':
120 dobackup = 1;
121 break;
122 case 'c':
123 docopy = 1;
124 break;
125 case 'd':
126 dodir = 1;
127 break;
128 case 'f':
129 flags = optarg;
130 if (string_to_flags(&flags, &fset, NULL))
131 errx(1, "%s: invalid flag", flags);
132 iflags |= SETFLAGS;
133 break;
134 case 'g':
135 group = optarg;
136 break;
137 case 'l':
138 for (p = optarg; *p; p++)
139 switch (*p) {
140 case 's':
141 dolink &= ~(LN_HARD|LN_MIXED);
142 dolink |= LN_SYMBOLIC;
143 break;
144 case 'h':
145 dolink &= ~(LN_SYMBOLIC|LN_MIXED);
146 dolink |= LN_HARD;
147 break;
148 case 'm':
149 dolink &= ~(LN_SYMBOLIC|LN_HARD);
150 dolink |= LN_MIXED;
151 break;
152 case 'a':
153 dolink &= ~LN_RELATIVE;
154 dolink |= LN_ABSOLUTE;
155 break;
156 case 'r':
157 dolink &= ~LN_ABSOLUTE;
158 dolink |= LN_RELATIVE;
159 break;
160 default:
161 errx(1, "%c: invalid link type", *p);
162 break;
163 }
164 break;
165 case 'm':
166 if (!(set = setmode(optarg)))
167 errx(1, "%s: invalid file mode", optarg);
168 mode = getmode(set, 0);
169 break;
170 case 'o':
171 owner = optarg;
172 break;
173 case 'p':
174 dopreserve = 1;
175 break;
176 case 'S':
177 stripArgs = (char*)malloc(sizeof(char)*(strlen(optarg)+1));
178 strcpy(stripArgs,optarg);
179 /* fall through; -S implies -s */
180 case 's':
181 dostrip = 1;
182 break;
183 case '?':
184 default:
185 usage();
186 }
187 argc -= optind;
188 argv += optind;
189
190 /* strip and link options make no sense when creating directories */
191 if ((dostrip || dolink) && dodir)
192 usage();
193
194 /* strip and flags make no sense with links */
195 if ((dostrip || flags) && dolink)
196 usage();
197
198 /* must have at least two arguments, except when creating directories */
199 if (argc < 2 && !dodir)
200 usage();
201
202 /* get group and owner id's */
203 if (group && !(gp = getgrnam(group)) && !isdigit((unsigned char)*group))
204 errx(1, "unknown group %s", group);
205 gid = (group) ? ((gp) ? gp->gr_gid : atoi(group)) : -1;
206 if (owner && !(pp = getpwnam(owner)) && !isdigit((unsigned char)*owner))
207 errx(1, "unknown user %s", owner);
208 uid = (owner) ? ((pp) ? pp->pw_uid : atoi(owner)) : -1;
209
210 if (dodir) {
211 for (; *argv != NULL; ++argv)
212 install_dir(*argv);
213 exit (0);
214 }
215
216 no_target = stat(to_name = argv[argc - 1], &to_sb);
217 if (!no_target && S_ISDIR(to_sb.st_mode)) {
218 for (; *argv != to_name; ++argv)
219 install(*argv, to_name, fset, iflags | DIRECTORY);
220 exit(0);
221 }
222
223 /* can't do file1 file2 directory/file */
224 if (argc != 2)
225 usage();
226
227 if (!no_target) {
228 if (stat(*argv, &from_sb))
229 err(1, "%s", *argv);
230 if (!S_ISREG(to_sb.st_mode))
231 errx(1, "%s: %s", to_name, strerror(EFTYPE));
232 if (!dolink && to_sb.st_dev == from_sb.st_dev &&
233 to_sb.st_ino == from_sb.st_ino)
234 errx(1, "%s and %s are the same file", *argv, to_name);
235 /*
236 * Unlink now... avoid ETXTBSY errors later. Try and turn
237 * off the append/immutable bits -- if we fail, go ahead,
238 * it might work.
239 */
240 #define NOCHANGEBITS (UF_IMMUTABLE | UF_APPEND | SF_IMMUTABLE | SF_APPEND)
241 if (to_sb.st_flags & NOCHANGEBITS)
242 (void)chflags(to_name,
243 to_sb.st_flags & ~(NOCHANGEBITS));
244 if (dobackup) {
245 char backup[FILENAME_MAX];
246 (void)snprintf(backup, FILENAME_MAX, "%s%s", to_name, suffix);
247 (void)rename(to_name, backup);
248 } else
249 (void)unlink(to_name);
250 }
251 install(*argv, to_name, fset, iflags);
252 exit(0);
253 }
254
255 /*
256 * makelink --
257 * make a link from source to destination
258 */
259 void
260 makelink(from_name, to_name)
261 char *from_name;
262 char *to_name;
263 {
264 char src[MAXPATHLEN], dst[MAXPATHLEN], lnk[MAXPATHLEN];
265
266 /* Try hard links first */
267 if (dolink & (LN_HARD|LN_MIXED)) {
268 if (link(from_name, to_name) == -1) {
269 if ((dolink & LN_HARD) || errno != EXDEV)
270 err(1, "link %s -> %s", from_name, to_name);
271 }
272 else
273 return;
274 }
275
276 /* Symbolic links */
277 if (dolink & LN_ABSOLUTE) {
278 /* Convert source path to absolute */
279 if (realpath(from_name, src) == NULL)
280 err(1, "%s", src);
281 if (symlink(src, to_name) == -1)
282 err(1, "symlink %s -> %s", src, to_name);
283 return;
284 }
285
286 if (dolink & LN_RELATIVE) {
287 char *s, *d;
288
289 /* Resolve pathnames */
290 if (realpath(from_name, src) == NULL)
291 err(1, "%s", src);
292 if (realpath(to_name, dst) == NULL)
293 err(1, "%s", dst);
294
295 /* trim common path components */
296 for (s = src, d = dst; *s == *d; s++, d++)
297 continue;
298 while (*s != '/')
299 s--, d--;
300
301 /* count the number of directories we need to backtrack */
302 for (++d, lnk[0] = '\0'; *d; d++)
303 if (*d == '/')
304 (void) strcat(lnk, "../");
305
306 (void) strcat(lnk, ++s);
307
308 if (symlink(lnk, dst) == -1)
309 err(1, "symlink %s -> %s", lnk, dst);
310 return;
311 }
312
313 /*
314 * If absolute or relative was not specified,
315 * try the names the user provided
316 */
317 if (symlink(from_name, to_name) == -1)
318 err(1, "symlink %s -> %s", from_name, to_name);
319
320 }
321
322 /*
323 * install --
324 * build a path name and install the file
325 */
326 void
327 install(from_name, to_name, fset, flags)
328 char *from_name, *to_name;
329 u_long fset;
330 u_int flags;
331 {
332 struct stat from_sb, to_sb;
333 int devnull, from_fd, to_fd, serrno;
334 char *p;
335
336 /* If try to install NULL file to a directory, fails. */
337 if (flags & DIRECTORY || strcmp(from_name, _PATH_DEVNULL)) {
338 if (stat(from_name, &from_sb))
339 err(1, "%s", from_name);
340 if (!S_ISREG(from_sb.st_mode))
341 errx(1, "%s: %s", from_name, strerror(EFTYPE));
342 /* Build the target path. */
343 if (flags & DIRECTORY) {
344 (void)snprintf(pathbuf, sizeof(pathbuf), "%s/%s",
345 to_name,
346 (p = strrchr(from_name, '/')) ? ++p : from_name);
347 to_name = pathbuf;
348 }
349 devnull = 0;
350 } else {
351 from_sb.st_flags = 0; /* XXX */
352 devnull = 1;
353 }
354
355 /*
356 * Unlink now... avoid ETXTBSY errors later. Try and turn
357 * off the append/immutable bits -- if we fail, go ahead,
358 * it might work.
359 */
360 if (stat(to_name, &to_sb) == 0 &&
361 to_sb.st_flags & (NOCHANGEBITS))
362 (void)chflags(to_name, to_sb.st_flags & ~(NOCHANGEBITS));
363 if (dobackup) {
364 char backup[FILENAME_MAX];
365 (void)snprintf(backup, FILENAME_MAX, "%s%s", to_name, suffix);
366 (void)rename(to_name, backup);
367 } else
368 (void)unlink(to_name);
369
370 if (dolink) {
371 makelink(from_name, to_name);
372 return;
373 }
374
375 /* Create target. */
376 if ((to_fd = open(to_name,
377 O_CREAT | O_WRONLY | O_TRUNC, S_IRUSR | S_IWUSR)) < 0)
378 err(1, "%s", to_name);
379 if (!devnull) {
380 if ((from_fd = open(from_name, O_RDONLY, 0)) < 0) {
381 (void)unlink(to_name);
382 err(1, "%s", from_name);
383 }
384 copy(from_fd, from_name, to_fd, to_name, from_sb.st_size);
385 (void)close(from_fd);
386 }
387
388 if (dostrip) {
389 strip(to_name);
390
391 /*
392 * Re-open our fd on the target, in case we used a strip
393 * that does not work in-place -- like gnu binutils strip.
394 */
395 close(to_fd);
396 if ((to_fd = open(to_name, O_RDONLY, S_IRUSR | S_IWUSR)) < 0)
397 err(1, "stripping %s", to_name);
398 }
399
400 /*
401 * Set owner, group, mode for target; do the chown first,
402 * chown may lose the setuid bits.
403 */
404 if ((gid != -1 || uid != -1) && fchown(to_fd, uid, gid)) {
405 serrno = errno;
406 (void)unlink(to_name);
407 errx(1, "%s: chown/chgrp: %s", to_name, strerror(serrno));
408 }
409 if (fchmod(to_fd, mode)) {
410 serrno = errno;
411 (void)unlink(to_name);
412 errx(1, "%s: chmod: %s", to_name, strerror(serrno));
413 }
414
415 /*
416 * If provided a set of flags, set them, otherwise, preserve the
417 * flags, except for the dump flag.
418 */
419 if (fchflags(to_fd,
420 flags & SETFLAGS ? fset : from_sb.st_flags & ~UF_NODUMP)) {
421 if (errno != EOPNOTSUPP || (from_sb.st_flags & ~UF_NODUMP) != 0)
422 warn("%s: chflags", to_name);
423 }
424
425 /*
426 * Preserve the date of the source file.
427 */
428 if (dopreserve) {
429 struct timeval tv[2];
430
431 TIMESPEC_TO_TIMEVAL(&tv[0], &from_sb.st_atimespec);
432 TIMESPEC_TO_TIMEVAL(&tv[1], &from_sb.st_mtimespec);
433 if (futimes(to_fd, tv) == -1)
434 warn("%s: futimes", to_name);
435 }
436
437 (void)close(to_fd);
438 if (!docopy && !devnull && unlink(from_name))
439 err(1, "%s", from_name);
440 }
441
442 /*
443 * copy --
444 * copy from one file to another
445 */
446 void
447 copy(from_fd, from_name, to_fd, to_name, size)
448 int from_fd, to_fd;
449 char *from_name, *to_name;
450 off_t size;
451 {
452 int nr, nw;
453 int serrno;
454 char *p;
455 char buf[MAXBSIZE];
456
457 /*
458 * There's no reason to do anything other than close the file
459 * now if it's empty, so let's not bother.
460 */
461 if (size > 0) {
462 /*
463 * Mmap and write if less than 8M (the limit is so we don't totally
464 * trash memory on big files). This is really a minor hack, but it
465 * wins some CPU back.
466 */
467 if (size <= 8 * 1048576) {
468 if ((p = mmap(NULL, (size_t)size, PROT_READ,
469 MAP_FILE|MAP_SHARED, from_fd, (off_t)0)) == (char *)-1)
470 err(1, "%s", from_name);
471 if (write(to_fd, p, size) != size)
472 err(1, "%s", to_name);
473 } else {
474 while ((nr = read(from_fd, buf, sizeof(buf))) > 0)
475 if ((nw = write(to_fd, buf, nr)) != nr) {
476 serrno = errno;
477 (void)unlink(to_name);
478 errx(1, "%s: %s",
479 to_name, strerror(nw > 0 ? EIO : serrno));
480 }
481 if (nr != 0) {
482 serrno = errno;
483 (void)unlink(to_name);
484 errx(1, "%s: %s", from_name, strerror(serrno));
485 }
486 }
487 }
488 }
489
490 /*
491 * strip --
492 * use strip(1) to strip the target file
493 */
494 void
495 strip(to_name)
496 char *to_name;
497 {
498 int serrno, status;
499 char *stripprog;
500
501 switch (vfork()) {
502 case -1:
503 serrno = errno;
504 (void)unlink(to_name);
505 errx(1, "vfork: %s", strerror(serrno));
506 case 0:
507 stripprog = getenv("STRIP");
508 if (stripprog == NULL)
509 stripprog = _PATH_STRIP;
510
511 if (stripArgs) {
512 /* build up a command line and let /bin/sh parse the arguments */
513 char* cmd = (char*)malloc(sizeof(char)*
514 (3+strlen(stripprog)+
515 strlen(stripArgs)+
516 strlen(to_name)));
517
518 sprintf(cmd, "%s %s %s", stripprog, stripArgs, to_name);
519
520 execl(_PATH_BSHELL, "sh", "-c", cmd, NULL);
521 } else
522 execl(stripprog, "strip", to_name, NULL);
523
524 warn("%s", stripprog);
525 _exit(1);
526 default:
527 if (wait(&status) == -1 || status)
528 (void)unlink(to_name);
529 }
530 }
531
532 /*
533 * install_dir --
534 * build directory heirarchy
535 */
536 void
537 install_dir(path)
538 char *path;
539 {
540 char *p;
541 struct stat sb;
542 int ch;
543
544 for (p = path;; ++p)
545 if (!*p || (p != path && *p == '/')) {
546 ch = *p;
547 *p = '\0';
548 if (stat(path, &sb)) {
549 if (errno != ENOENT || mkdir(path, 0777) < 0) {
550 err(1, "%s", path);
551 /* NOTREACHED */
552 }
553 }
554 if (!(*p = ch))
555 break;
556 }
557
558 if (((gid != -1 || uid != -1) && chown(path, uid, gid)) ||
559 chmod(path, mode)) {
560 warn("%s", path);
561 }
562 }
563
564 /*
565 * usage --
566 * print a usage message and die
567 */
568 void
569 usage()
570 {
571 (void)fprintf(stderr, "\
572 usage: install [-bcps] [-B suffix] [-f flags] [-m mode] [-o owner] [-g group] [-l linkflags] [-S stripflags] file1 file2\n\
573 install [-bcps] [-B suffix] [-f flags] [-m mode] [-o owner] [-g group] [-l linkflags] [-S stripflags] file1 ... fileN directory\n\
574 install -pd [-m mode] [-o owner] [-g group] directory ...\n");
575 exit(1);
576 }
577