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