xinstall.c revision 1.35 1 /* $NetBSD: xinstall.c,v 1.35 1999/03/29 17:01:49 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.35 1999/03/29 17:01:49 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 dorename = 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:")) != -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 '?':
204 default:
205 usage();
206 }
207 argc -= optind;
208 argv += optind;
209
210 /* strip and link options make no sense when creating directories */
211 if ((dostrip || dolink) && dodir)
212 usage();
213
214 /* strip and flags make no sense with links */
215 if ((dostrip || flags) && dolink)
216 usage();
217
218 /* must have at least two arguments, except when creating directories */
219 if (argc < 2 && !dodir)
220 usage();
221
222 /* get group and owner id's */
223 if (group && !(gp = getgrnam(group)) && !isdigit((unsigned char)*group))
224 errx(1, "unknown group %s", group);
225 gid = (group) ? ((gp) ? gp->gr_gid : atoi(group)) : -1;
226 if (owner && !(pp = getpwnam(owner)) && !isdigit((unsigned char)*owner))
227 errx(1, "unknown user %s", owner);
228 uid = (owner) ? ((pp) ? pp->pw_uid : atoi(owner)) : -1;
229
230 if (dodir) {
231 for (; *argv != NULL; ++argv)
232 install_dir(*argv);
233 exit (0);
234 }
235
236 no_target = stat(to_name = argv[argc - 1], &to_sb);
237 if (!no_target && S_ISDIR(to_sb.st_mode)) {
238 for (; *argv != to_name; ++argv)
239 install(*argv, to_name, fset, iflags | DIRECTORY);
240 exit(0);
241 }
242
243 /* can't do file1 file2 directory/file */
244 if (argc != 2)
245 usage();
246
247 if (!no_target) {
248 if (stat(*argv, &from_sb))
249 err(1, "%s", *argv);
250 if (!S_ISREG(to_sb.st_mode))
251 errx(1, "%s: %s", to_name, strerror(EFTYPE));
252 if (!dolink && to_sb.st_dev == from_sb.st_dev &&
253 to_sb.st_ino == from_sb.st_ino)
254 errx(1, "%s and %s are the same file", *argv, to_name);
255 /*
256 * Unlink now... avoid ETXTBSY errors later. Try and turn
257 * off the append/immutable bits -- if we fail, go ahead,
258 * it might work.
259 */
260 #define NOCHANGEBITS (UF_IMMUTABLE | UF_APPEND | SF_IMMUTABLE | SF_APPEND)
261 if (to_sb.st_flags & NOCHANGEBITS)
262 (void)chflags(to_name,
263 to_sb.st_flags & ~(NOCHANGEBITS));
264 if (dobackup)
265 backup(to_name);
266 else if (!dorename)
267 (void)unlink(to_name);
268 }
269 install(*argv, to_name, fset, iflags);
270 exit(0);
271 }
272
273 /*
274 * makelink --
275 * make a link from source to destination
276 */
277 void
278 makelink(from_name, to_name)
279 char *from_name;
280 char *to_name;
281 {
282 char src[MAXPATHLEN], dst[MAXPATHLEN], lnk[MAXPATHLEN];
283
284 /* Try hard links first */
285 if (dolink & (LN_HARD|LN_MIXED)) {
286 if (link(from_name, to_name) == -1) {
287 if ((dolink & LN_HARD) || errno != EXDEV)
288 err(1, "link %s -> %s", from_name, to_name);
289 }
290 else
291 return;
292 }
293
294 /* Symbolic links */
295 if (dolink & LN_ABSOLUTE) {
296 /* Convert source path to absolute */
297 if (realpath(from_name, src) == NULL)
298 err(1, "%s", src);
299 if (symlink(src, to_name) == -1)
300 err(1, "symlink %s -> %s", src, to_name);
301 return;
302 }
303
304 if (dolink & LN_RELATIVE) {
305 char *s, *d;
306
307 /* Resolve pathnames */
308 if (realpath(from_name, src) == NULL)
309 err(1, "%s", src);
310 if (realpath(to_name, dst) == NULL)
311 err(1, "%s", dst);
312
313 /* trim common path components */
314 for (s = src, d = dst; *s == *d; s++, d++)
315 continue;
316 while (*s != '/')
317 s--, d--;
318
319 /* count the number of directories we need to backtrack */
320 for (++d, lnk[0] = '\0'; *d; d++)
321 if (*d == '/')
322 (void) strcat(lnk, "../");
323
324 (void) strcat(lnk, ++s);
325
326 if (symlink(lnk, dst) == -1)
327 err(1, "symlink %s -> %s", lnk, dst);
328 return;
329 }
330
331 /*
332 * If absolute or relative was not specified,
333 * try the names the user provided
334 */
335 if (symlink(from_name, to_name) == -1)
336 err(1, "symlink %s -> %s", from_name, to_name);
337
338 }
339
340 /*
341 * install --
342 * build a path name and install the file
343 */
344 void
345 install(from_name, to_name, fset, flags)
346 char *from_name, *to_name;
347 u_long fset;
348 u_int flags;
349 {
350 struct stat from_sb, to_sb;
351 int devnull, from_fd, to_fd, serrno;
352 char *p, tmpl[MAXPATHLEN], *oto_name;
353
354 /* If try to install NULL file to a directory, fails. */
355 if (flags & DIRECTORY || strcmp(from_name, _PATH_DEVNULL)) {
356 if (stat(from_name, &from_sb))
357 err(1, "%s", from_name);
358 if (!S_ISREG(from_sb.st_mode))
359 errx(1, "%s: %s", from_name, strerror(EFTYPE));
360 /* Build the target path. */
361 if (flags & DIRECTORY) {
362 (void)snprintf(pathbuf, sizeof(pathbuf), "%s/%s",
363 to_name,
364 (p = strrchr(from_name, '/')) ? ++p : from_name);
365 to_name = pathbuf;
366 }
367 devnull = 0;
368 } else {
369 from_sb.st_flags = 0; /* XXX */
370 devnull = 1;
371 }
372
373 /*
374 * Unlink now... avoid ETXTBSY errors later. Try and turn
375 * off the append/immutable bits -- if we fail, go ahead,
376 * it might work.
377 */
378 if (stat(to_name, &to_sb) == 0 &&
379 to_sb.st_flags & (NOCHANGEBITS))
380 (void)chflags(to_name, to_sb.st_flags & ~(NOCHANGEBITS));
381 if (dorename) {
382 char *ptr, c, *dir;
383
384 if ((ptr = strrchr(to_name, '/')) != NULL) {
385 c = *++ptr;
386 *ptr = '\0';
387 dir = to_name;
388 } else {
389 dir = tmpl;
390 *dir = '\0';
391 }
392 (void)snprintf(tmpl, sizeof(tmpl), "%sinst.XXXXXX", dir);
393 if (ptr)
394 *ptr = c;
395 oto_name = to_name;
396 to_name = tmpl;
397
398 } else {
399 if (dobackup)
400 backup(to_name);
401 else
402 (void)unlink(to_name);
403 }
404
405 if (dolink) {
406 makelink(from_name, to_name);
407 return;
408 }
409
410 /* Create target. */
411 if (dorename) {
412 if ((to_fd = mkstemp(to_name)) == -1)
413 err(1, "%s", to_name);
414 } else {
415 if ((to_fd = open(to_name,
416 O_CREAT | O_WRONLY | O_TRUNC, S_IRUSR | S_IWUSR)) < 0)
417 err(1, "%s", to_name);
418 }
419 if (!devnull) {
420 if ((from_fd = open(from_name, O_RDONLY, 0)) < 0) {
421 (void)unlink(to_name);
422 err(1, "%s", from_name);
423 }
424 copy(from_fd, from_name, to_fd, to_name, from_sb.st_size);
425 (void)close(from_fd);
426 }
427
428 if (dostrip) {
429 strip(to_name);
430
431 /*
432 * Re-open our fd on the target, in case we used a strip
433 * that does not work in-place -- like gnu binutils strip.
434 */
435 close(to_fd);
436 if ((to_fd = open(to_name, O_RDONLY, S_IRUSR | S_IWUSR)) < 0)
437 err(1, "stripping %s", to_name);
438 }
439
440 /*
441 * Set owner, group, mode for target; do the chown first,
442 * chown may lose the setuid bits.
443 */
444 if ((gid != -1 || uid != -1) && fchown(to_fd, uid, gid)) {
445 serrno = errno;
446 (void)unlink(to_name);
447 errx(1, "%s: chown/chgrp: %s", to_name, strerror(serrno));
448 }
449 if (fchmod(to_fd, mode)) {
450 serrno = errno;
451 (void)unlink(to_name);
452 errx(1, "%s: chmod: %s", to_name, strerror(serrno));
453 }
454
455 /*
456 * If provided a set of flags, set them, otherwise, preserve the
457 * flags, except for the dump flag.
458 */
459 if (fchflags(to_fd,
460 flags & SETFLAGS ? fset : from_sb.st_flags & ~UF_NODUMP)) {
461 if (errno != EOPNOTSUPP || (from_sb.st_flags & ~UF_NODUMP) != 0)
462 warn("%s: chflags", to_name);
463 }
464
465 /*
466 * Preserve the date of the source file.
467 */
468 if (dopreserve) {
469 struct timeval tv[2];
470
471 TIMESPEC_TO_TIMEVAL(&tv[0], &from_sb.st_atimespec);
472 TIMESPEC_TO_TIMEVAL(&tv[1], &from_sb.st_mtimespec);
473 if (futimes(to_fd, tv) == -1)
474 warn("%s: futimes", to_name);
475 }
476
477 (void)close(to_fd);
478
479 if (dorename)
480 if (rename(to_name, oto_name) == -1)
481 err(1, "%s: rename", to_name);
482
483 if (!docopy && !devnull && unlink(from_name))
484 err(1, "%s", from_name);
485 }
486
487 /*
488 * copy --
489 * copy from one file to another
490 */
491 void
492 copy(from_fd, from_name, to_fd, to_name, size)
493 int from_fd, to_fd;
494 char *from_name, *to_name;
495 off_t size;
496 {
497 int nr, nw;
498 int serrno;
499 char *p;
500 char buf[MAXBSIZE];
501
502 /*
503 * There's no reason to do anything other than close the file
504 * now if it's empty, so let's not bother.
505 */
506 if (size > 0) {
507 /*
508 * Mmap and write if less than 8M (the limit is so we don't totally
509 * trash memory on big files). This is really a minor hack, but it
510 * wins some CPU back.
511 */
512 if (size <= 8 * 1048576) {
513 if ((p = mmap(NULL, (size_t)size, PROT_READ,
514 MAP_FILE|MAP_SHARED, from_fd, (off_t)0)) == (char *)-1)
515 err(1, "%s", from_name);
516 if (write(to_fd, p, size) != size)
517 err(1, "%s", to_name);
518 } else {
519 while ((nr = read(from_fd, buf, sizeof(buf))) > 0)
520 if ((nw = write(to_fd, buf, nr)) != nr) {
521 serrno = errno;
522 (void)unlink(to_name);
523 errx(1, "%s: %s",
524 to_name, strerror(nw > 0 ? EIO : serrno));
525 }
526 if (nr != 0) {
527 serrno = errno;
528 (void)unlink(to_name);
529 errx(1, "%s: %s", from_name, strerror(serrno));
530 }
531 }
532 }
533 }
534
535 /*
536 * strip --
537 * use strip(1) to strip the target file
538 */
539 void
540 strip(to_name)
541 char *to_name;
542 {
543 int serrno, status;
544 char *stripprog;
545
546 switch (vfork()) {
547 case -1:
548 serrno = errno;
549 (void)unlink(to_name);
550 errx(1, "vfork: %s", strerror(serrno));
551 case 0:
552 stripprog = getenv("STRIP");
553 if (stripprog == NULL)
554 stripprog = _PATH_STRIP;
555
556 if (stripArgs) {
557 /* build up a command line and let /bin/sh parse the arguments */
558 char* cmd = (char*)malloc(sizeof(char)*
559 (3+strlen(stripprog)+
560 strlen(stripArgs)+
561 strlen(to_name)));
562
563 sprintf(cmd, "%s %s %s", stripprog, stripArgs, to_name);
564
565 execl(_PATH_BSHELL, "sh", "-c", cmd, NULL);
566 } else
567 execl(stripprog, "strip", to_name, NULL);
568
569 warn("%s", stripprog);
570 _exit(1);
571 default:
572 if (wait(&status) == -1 || status)
573 (void)unlink(to_name);
574 }
575 }
576
577 /*
578 * backup file "to_name" to to_name.suffix
579 * if suffix contains a "%", it's taken as a printf(3) pattern
580 * used for a numbered backup.
581 */
582 void
583 backup(to_name)
584 const char *to_name;
585 {
586 char backup[FILENAME_MAX];
587
588 if (numberedbackup) {
589 /* Do numbered backup */
590 int cnt;
591 char suffix_expanded[FILENAME_MAX];
592
593 cnt=0;
594 do {
595 (void)snprintf(suffix_expanded, FILENAME_MAX, suffix, cnt);
596 (void)snprintf(backup, FILENAME_MAX, "%s%s",
597 to_name, suffix_expanded);
598 cnt++;
599 } while (access(backup, F_OK)==0);
600 } else {
601 /* Do simple backup */
602 (void)snprintf(backup, FILENAME_MAX, "%s%s", to_name, suffix);
603 }
604
605 (void)rename(to_name, backup);
606 }
607
608 /*
609 * install_dir --
610 * build directory heirarchy
611 */
612 void
613 install_dir(path)
614 char *path;
615 {
616 char *p;
617 struct stat sb;
618 int ch;
619
620 for (p = path;; ++p)
621 if (!*p || (p != path && *p == '/')) {
622 ch = *p;
623 *p = '\0';
624 if (stat(path, &sb)) {
625 if (errno != ENOENT || mkdir(path, 0777) < 0) {
626 err(1, "%s", path);
627 /* NOTREACHED */
628 }
629 }
630 if (!(*p = ch))
631 break;
632 }
633
634 if (((gid != -1 || uid != -1) && chown(path, uid, gid)) ||
635 chmod(path, mode)) {
636 warn("%s", path);
637 }
638 }
639
640 /*
641 * usage --
642 * print a usage message and die
643 */
644 void
645 usage()
646 {
647 (void)fprintf(stderr, "\
648 usage: install [-bcps] [-B suffix] [-f flags] [-m mode] [-o owner] [-g group] [-l linkflags] [-S stripflags] file1 file2\n\
649 install [-bcps] [-B suffix] [-f flags] [-m mode] [-o owner] [-g group] [-l linkflags] [-S stripflags] file1 ... fileN directory\n\
650 install -pd [-m mode] [-o owner] [-g group] directory ...\n");
651 exit(1);
652 }
653