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