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