xinstall.c revision 1.41 1 /* $NetBSD: xinstall.c,v 1.41 2000/07/27 03:57:50 cgd 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.41 2000/07/27 03:57:50 cgd 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 dorename = 0, unpriv = 0;
77 static int numberedbackup = 0;
78 int mode = S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
79 char pathbuf[MAXPATHLEN];
80 uid_t uid;
81 gid_t gid;
82 char *stripArgs=NULL;
83 char *suffix=BACKUP_SUFFIX;
84
85 #define LN_ABSOLUTE 0x01
86 #define LN_RELATIVE 0x02
87 #define LN_HARD 0x04
88 #define LN_SYMBOLIC 0x08
89 #define LN_MIXED 0x10
90
91 #define DIRECTORY 0x01 /* Tell install it's a directory. */
92 #define SETFLAGS 0x02 /* Tell install to set flags. */
93
94 void copy __P((int, char *, int, char *, off_t));
95 void makelink __P((char *, char *));
96 void install __P((char *, char *, u_long, u_int));
97 void install_dir __P((char *));
98 void strip __P((char *));
99 void backup __P((const char *));
100 void usage __P((void));
101 int main __P((int, char *[]));
102
103 int
104 main(argc, argv)
105 int argc;
106 char *argv[];
107 {
108 struct stat from_sb, to_sb;
109 mode_t *set;
110 u_long fset;
111 u_int iflags;
112 int ch, no_target;
113 char *p;
114 char *flags = NULL, *to_name, *group = NULL, *owner = NULL;
115
116 iflags = 0;
117 while ((ch = getopt(argc, argv, "cbB:df:g:l:m:o:prsS:U")) != -1)
118 switch((char)ch) {
119 case 'B':
120 suffix = optarg;
121 numberedbackup = 0;
122 {
123 /* Check if given suffix really generates
124 different suffixes - catch e.g. ".%" */
125 char suffix_expanded0[FILENAME_MAX],
126 suffix_expanded1[FILENAME_MAX];
127 (void)snprintf(suffix_expanded0, FILENAME_MAX,
128 suffix, 0);
129 (void)snprintf(suffix_expanded1, FILENAME_MAX,
130 suffix, 1);
131 if (strcmp(suffix_expanded0, suffix_expanded1)
132 != 0)
133 numberedbackup = 1;
134 }
135 /* fall through; -B implies -b */
136 case 'b':
137 dobackup = 1;
138 break;
139 case 'c':
140 docopy = 1;
141 break;
142 case 'd':
143 dodir = 1;
144 break;
145 case 'f':
146 flags = optarg;
147 if (string_to_flags(&flags, &fset, NULL))
148 errx(1, "%s: invalid flag", flags);
149 iflags |= SETFLAGS;
150 break;
151 case 'g':
152 group = optarg;
153 break;
154 case 'l':
155 for (p = optarg; *p; p++)
156 switch (*p) {
157 case 's':
158 dolink &= ~(LN_HARD|LN_MIXED);
159 dolink |= LN_SYMBOLIC;
160 break;
161 case 'h':
162 dolink &= ~(LN_SYMBOLIC|LN_MIXED);
163 dolink |= LN_HARD;
164 break;
165 case 'm':
166 dolink &= ~(LN_SYMBOLIC|LN_HARD);
167 dolink |= LN_MIXED;
168 break;
169 case 'a':
170 dolink &= ~LN_RELATIVE;
171 dolink |= LN_ABSOLUTE;
172 break;
173 case 'r':
174 dolink &= ~LN_ABSOLUTE;
175 dolink |= LN_RELATIVE;
176 break;
177 default:
178 errx(1, "%c: invalid link type", *p);
179 break;
180 }
181 break;
182 case 'm':
183 if (!(set = setmode(optarg)))
184 errx(1, "%s: invalid file mode", optarg);
185 mode = getmode(set, 0);
186 break;
187 case 'o':
188 owner = optarg;
189 break;
190 case 'p':
191 dopreserve = 1;
192 break;
193 case 'r':
194 dorename = 1;
195 break;
196 case 'S':
197 stripArgs = (char*)malloc(sizeof(char)*(strlen(optarg)+1));
198 strcpy(stripArgs,optarg);
199 /* fall through; -S implies -s */
200 case 's':
201 dostrip = 1;
202 break;
203 case 'U':
204 unpriv = 1;
205 break;
206 case '?':
207 default:
208 usage();
209 }
210 argc -= optind;
211 argv += optind;
212
213 /* strip and link options make no sense when creating directories */
214 if ((dostrip || dolink) && dodir)
215 usage();
216
217 /* strip and flags make no sense with links */
218 if ((dostrip || flags) && dolink)
219 usage();
220
221 /* must have at least two arguments, except when creating directories */
222 if (argc < 2 && !dodir)
223 usage();
224
225 /* get group and owner id's */
226 if (unpriv)
227 uid = gid = -1;
228 else {
229 if (group && !(gp = getgrnam(group)) &&
230 !isdigit((unsigned char)*group))
231 errx(1, "unknown group %s", group);
232 gid = (group) ? ((gp) ? gp->gr_gid : atoi(group)) : -1;
233 if (owner && !(pp = getpwnam(owner)) &&
234 !isdigit((unsigned char)*owner))
235 errx(1, "unknown user %s", owner);
236 uid = (owner) ? ((pp) ? pp->pw_uid : atoi(owner)) : -1;
237 }
238
239 if (dodir) {
240 for (; *argv != NULL; ++argv)
241 install_dir(*argv);
242 exit (0);
243 }
244
245 no_target = stat(to_name = argv[argc - 1], &to_sb);
246 if (!no_target && S_ISDIR(to_sb.st_mode)) {
247 for (; *argv != to_name; ++argv)
248 install(*argv, to_name, fset, iflags | DIRECTORY);
249 exit(0);
250 }
251
252 /* can't do file1 file2 directory/file */
253 if (argc != 2)
254 usage();
255
256 if (!no_target) {
257 if (stat(*argv, &from_sb))
258 err(1, "%s", *argv);
259 if (!S_ISREG(to_sb.st_mode))
260 errx(1, "%s: not a regular file", to_name);
261 if (!dolink && to_sb.st_dev == from_sb.st_dev &&
262 to_sb.st_ino == from_sb.st_ino)
263 errx(1, "%s and %s are the same file", *argv, to_name);
264 /*
265 * Unlink now... avoid ETXTBSY errors later. Try and turn
266 * off the append/immutable bits -- if we fail, go ahead,
267 * it might work.
268 */
269 #define NOCHANGEBITS (UF_IMMUTABLE | UF_APPEND | SF_IMMUTABLE | SF_APPEND)
270 if (to_sb.st_flags & NOCHANGEBITS)
271 (void)chflags(to_name,
272 to_sb.st_flags & ~(NOCHANGEBITS));
273 if (dobackup)
274 backup(to_name);
275 else if (!dorename)
276 (void)unlink(to_name);
277 }
278 install(*argv, to_name, fset, iflags);
279 exit(0);
280 }
281
282 /*
283 * makelink --
284 * make a link from source to destination
285 */
286 void
287 makelink(from_name, to_name)
288 char *from_name;
289 char *to_name;
290 {
291 char src[MAXPATHLEN], dst[MAXPATHLEN], lnk[MAXPATHLEN];
292
293 /* Try hard links first */
294 if (dolink & (LN_HARD|LN_MIXED)) {
295 if (link(from_name, to_name) == -1) {
296 if ((dolink & LN_HARD) || errno != EXDEV)
297 err(1, "link %s -> %s", from_name, to_name);
298 }
299 else
300 return;
301 }
302
303 /* Symbolic links */
304 if (dolink & LN_ABSOLUTE) {
305 /* Convert source path to absolute */
306 if (realpath(from_name, src) == NULL)
307 err(1, "%s", src);
308 if (symlink(src, to_name) == -1)
309 err(1, "symlink %s -> %s", src, to_name);
310 return;
311 }
312
313 if (dolink & LN_RELATIVE) {
314 char *s, *d;
315
316 /* Resolve pathnames */
317 if (realpath(from_name, src) == NULL)
318 err(1, "%s", src);
319 if (realpath(to_name, dst) == NULL)
320 err(1, "%s", dst);
321
322 /* trim common path components */
323 for (s = src, d = dst; *s == *d; s++, d++)
324 continue;
325 while (*s != '/')
326 s--, d--;
327
328 /* count the number of directories we need to backtrack */
329 for (++d, lnk[0] = '\0'; *d; d++)
330 if (*d == '/')
331 (void) strcat(lnk, "../");
332
333 (void) strcat(lnk, ++s);
334
335 if (symlink(lnk, dst) == -1)
336 err(1, "symlink %s -> %s", lnk, dst);
337 return;
338 }
339
340 /*
341 * If absolute or relative was not specified,
342 * try the names the user provided
343 */
344 if (symlink(from_name, to_name) == -1)
345 err(1, "symlink %s -> %s", from_name, to_name);
346
347 }
348
349 /*
350 * install --
351 * build a path name and install the file
352 */
353 void
354 install(from_name, to_name, fset, flags)
355 char *from_name, *to_name;
356 u_long fset;
357 u_int flags;
358 {
359 struct stat from_sb, to_sb;
360 int devnull, from_fd, to_fd, serrno;
361 char *p, tmpl[MAXPATHLEN], *oto_name;
362
363 /* If try to install NULL file to a directory, fails. */
364 if (flags & DIRECTORY || strcmp(from_name, _PATH_DEVNULL)) {
365 if (stat(from_name, &from_sb))
366 err(1, "%s", from_name);
367 if (!S_ISREG(from_sb.st_mode))
368 errx(1, "%s: not a regular file", to_name);
369 /* Build the target path. */
370 if (flags & DIRECTORY) {
371 (void)snprintf(pathbuf, sizeof(pathbuf), "%s/%s",
372 to_name,
373 (p = strrchr(from_name, '/')) ? ++p : from_name);
374 to_name = pathbuf;
375 }
376 devnull = 0;
377 } else {
378 from_sb.st_flags = 0; /* XXX */
379 devnull = 1;
380 }
381
382 /*
383 * Unlink now... avoid ETXTBSY errors later. Try and turn
384 * off the append/immutable bits -- if we fail, go ahead,
385 * it might work.
386 */
387 if (stat(to_name, &to_sb) == 0 &&
388 to_sb.st_flags & (NOCHANGEBITS))
389 (void)chflags(to_name, to_sb.st_flags & ~(NOCHANGEBITS));
390 if (dorename) {
391 char *ptr, c, *dir;
392
393 if ((ptr = strrchr(to_name, '/')) != NULL) {
394 c = *++ptr;
395 *ptr = '\0';
396 dir = to_name;
397 } else {
398 c = '\0'; /* pacify gcc */
399 dir = tmpl;
400 *dir = '\0';
401 }
402 (void)snprintf(tmpl, sizeof(tmpl), "%sinst.XXXXXX", dir);
403 if (ptr)
404 *ptr = c;
405 oto_name = to_name;
406 to_name = tmpl;
407
408 } else {
409 oto_name = NULL; /* pacify gcc */
410 if (dobackup)
411 backup(to_name);
412 else
413 (void)unlink(to_name);
414 }
415
416 if (dolink) {
417 makelink(from_name, to_name);
418 return;
419 }
420
421 /* Create target. */
422 if (dorename) {
423 if ((to_fd = mkstemp(to_name)) == -1)
424 err(1, "%s", to_name);
425 } else {
426 if ((to_fd = open(to_name,
427 O_CREAT | O_WRONLY | O_TRUNC, S_IRUSR | S_IWUSR)) < 0)
428 err(1, "%s", to_name);
429 }
430 if (!devnull) {
431 if ((from_fd = open(from_name, O_RDONLY, 0)) < 0) {
432 (void)unlink(to_name);
433 err(1, "%s", from_name);
434 }
435 copy(from_fd, from_name, to_fd, to_name, from_sb.st_size);
436 (void)close(from_fd);
437 }
438
439 if (dostrip) {
440 strip(to_name);
441
442 /*
443 * Re-open our fd on the target, in case we used a strip
444 * that does not work in-place -- like gnu binutils strip.
445 */
446 close(to_fd);
447 if ((to_fd = open(to_name, O_RDONLY, S_IRUSR | S_IWUSR)) < 0)
448 err(1, "stripping %s", to_name);
449 }
450
451 /*
452 * Set owner, group, mode for target; do the chown first,
453 * chown may lose the setuid bits.
454 */
455 if ((gid != -1 || uid != -1) && fchown(to_fd, uid, gid)) {
456 serrno = errno;
457 (void)unlink(to_name);
458 errx(1, "%s: chown/chgrp: %s", to_name, strerror(serrno));
459 }
460 if (fchmod(to_fd, mode)) {
461 serrno = errno;
462 (void)unlink(to_name);
463 errx(1, "%s: chmod: %s", to_name, strerror(serrno));
464 }
465
466 /*
467 * If provided a set of flags, set them, otherwise, preserve the
468 * flags, except for the dump flag.
469 */
470 if (fchflags(to_fd,
471 flags & SETFLAGS ? fset : from_sb.st_flags & ~UF_NODUMP)) {
472 if (errno != EOPNOTSUPP || (from_sb.st_flags & ~UF_NODUMP) != 0)
473 warn("%s: chflags", to_name);
474 }
475
476 /*
477 * Preserve the date of the source file.
478 */
479 if (dopreserve) {
480 struct timeval tv[2];
481
482 #ifdef BSD4_4
483 TIMESPEC_TO_TIMEVAL(&tv[0], &from_sb.st_atimespec);
484 TIMESPEC_TO_TIMEVAL(&tv[1], &from_sb.st_mtimespec);
485 #else
486 tv[0].tv_sec = from_sb.st_atime;
487 tv[0].tv_usec = 0;
488 tv[1].tv_sec = from_sb.st_mtime;
489 tv[1].tv_usec = 0;
490 #endif
491 if (futimes(to_fd, tv) == -1)
492 warn("%s: futimes", to_name);
493 }
494
495 (void)close(to_fd);
496
497 if (dorename)
498 if (rename(to_name, oto_name) == -1)
499 err(1, "%s: rename", to_name);
500
501 if (!docopy && !devnull && unlink(from_name))
502 err(1, "%s", from_name);
503 }
504
505 /*
506 * copy --
507 * copy from one file to another
508 */
509 void
510 copy(from_fd, from_name, to_fd, to_name, size)
511 int from_fd, to_fd;
512 char *from_name, *to_name;
513 off_t size;
514 {
515 ssize_t nr, nw;
516 int serrno;
517 char *p;
518 char buf[MAXBSIZE];
519
520 /*
521 * There's no reason to do anything other than close the file
522 * now if it's empty, so let's not bother.
523 */
524 if (size > 0) {
525 /*
526 * Mmap and write if less than 8M (the limit is so we don't totally
527 * trash memory on big files). This is really a minor hack, but it
528 * wins some CPU back.
529 */
530 if (size <= 8 * 1048576) {
531 if ((p = mmap(NULL, (size_t)size, PROT_READ,
532 MAP_FILE|MAP_SHARED, from_fd, (off_t)0)) == MAP_FAILED) {
533 serrno = errno;
534 (void)unlink(to_name);
535 errx(1, "%s: %s", from_name, strerror(serrno));
536 }
537 if (madvise(p, (size_t)size, MADV_SEQUENTIAL) == -1
538 && errno != EOPNOTSUPP)
539 warnx("madvise: %s", strerror(errno));
540
541 if (write(to_fd, p, size) != size) {
542 serrno = errno;
543 (void)unlink(to_name);
544 errx(1, "%s: %s",
545 to_name, strerror(serrno));
546 }
547 } else {
548 while ((nr = read(from_fd, buf, sizeof(buf))) > 0) {
549 if ((nw = write(to_fd, buf, nr)) != nr) {
550 serrno = errno;
551 (void)unlink(to_name);
552 errx(1, "%s: %s",
553 to_name, strerror(nw > 0 ? EIO : serrno));
554 }
555 }
556 if (nr != 0) {
557 serrno = errno;
558 (void)unlink(to_name);
559 errx(1, "%s: %s", from_name, strerror(serrno));
560 }
561 }
562 }
563 }
564
565 /*
566 * strip --
567 * use strip(1) to strip the target file
568 */
569 void
570 strip(to_name)
571 char *to_name;
572 {
573 int serrno, status;
574 char *stripprog;
575
576 switch (vfork()) {
577 case -1:
578 serrno = errno;
579 (void)unlink(to_name);
580 errx(1, "vfork: %s", strerror(serrno));
581 case 0:
582 stripprog = getenv("STRIP");
583 if (stripprog == NULL)
584 stripprog = _PATH_STRIP;
585
586 if (stripArgs) {
587 /* build up a command line and let /bin/sh parse the arguments */
588 char* cmd = (char*)malloc(sizeof(char)*
589 (3+strlen(stripprog)+
590 strlen(stripArgs)+
591 strlen(to_name)));
592
593 sprintf(cmd, "%s %s %s", stripprog, stripArgs, to_name);
594
595 execl(_PATH_BSHELL, "sh", "-c", cmd, NULL);
596 } else
597 execlp(stripprog, "strip", to_name, NULL);
598
599 warn("%s", stripprog);
600 _exit(1);
601 default:
602 if (wait(&status) == -1 || status)
603 (void)unlink(to_name);
604 }
605 }
606
607 /*
608 * backup file "to_name" to to_name.suffix
609 * if suffix contains a "%", it's taken as a printf(3) pattern
610 * used for a numbered backup.
611 */
612 void
613 backup(to_name)
614 const char *to_name;
615 {
616 char backup[FILENAME_MAX];
617
618 if (numberedbackup) {
619 /* Do numbered backup */
620 int cnt;
621 char suffix_expanded[FILENAME_MAX];
622
623 cnt=0;
624 do {
625 (void)snprintf(suffix_expanded, FILENAME_MAX, suffix, cnt);
626 (void)snprintf(backup, FILENAME_MAX, "%s%s",
627 to_name, suffix_expanded);
628 cnt++;
629 } while (access(backup, F_OK)==0);
630 } else {
631 /* Do simple backup */
632 (void)snprintf(backup, FILENAME_MAX, "%s%s", to_name, suffix);
633 }
634
635 (void)rename(to_name, backup);
636 }
637
638 /*
639 * install_dir --
640 * build directory heirarchy
641 */
642 void
643 install_dir(path)
644 char *path;
645 {
646 char *p;
647 struct stat sb;
648 int ch;
649
650 for (p = path;; ++p)
651 if (!*p || (p != path && *p == '/')) {
652 ch = *p;
653 *p = '\0';
654 if (stat(path, &sb)) {
655 if (errno != ENOENT || mkdir(path, 0777) < 0) {
656 err(1, "%s", path);
657 /* NOTREACHED */
658 }
659 }
660 if (!(*p = ch))
661 break;
662 }
663
664 if (((gid != -1 || uid != -1) && chown(path, uid, gid)) ||
665 chmod(path, mode)) {
666 warn("%s", path);
667 }
668 }
669
670 /*
671 * usage --
672 * print a usage message and die
673 */
674 void
675 usage()
676 {
677 (void)fprintf(stderr, "\
678 usage: install [-Ubcps] [-B suffix] [-f flags] [-m mode] [-o owner] [-g group] [-l linkflags] [-S stripflags] file1 file2\n\
679 install [-Ubcps] [-B suffix] [-f flags] [-m mode] [-o owner] [-g group] [-l linkflags] [-S stripflags] file1 ... fileN directory\n\
680 install [-Up] -d [-m mode] [-o owner] [-g group] directory ...\n");
681 exit(1);
682 }
683