Annotation of src/usr.bin/xinstall/xinstall.c, Revision 1.62
1.62 ! jasper 1: /* $OpenBSD: xinstall.c,v 1.61 2015/07/18 15:42:37 jasper Exp $ */
1.2 deraadt 2: /* $NetBSD: xinstall.c,v 1.9 1995/12/20 10:25:17 jonathan Exp $ */
1.1 deraadt 3:
4: /*
5: * Copyright (c) 1987, 1993
6: * The Regents of the University of California. All rights reserved.
7: *
8: * Redistribution and use in source and binary forms, with or without
9: * modification, are permitted provided that the following conditions
10: * are met:
11: * 1. Redistributions of source code must retain the above copyright
12: * notice, this list of conditions and the following disclaimer.
13: * 2. Redistributions in binary form must reproduce the above copyright
14: * notice, this list of conditions and the following disclaimer in the
15: * documentation and/or other materials provided with the distribution.
1.34 millert 16: * 3. Neither the name of the University nor the names of its contributors
1.1 deraadt 17: * may be used to endorse or promote products derived from this software
18: * without specific prior written permission.
19: *
20: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30: * SUCH DAMAGE.
31: */
32:
1.58 deraadt 33: #include <sys/param.h> /* MAXBSIZE */
1.1 deraadt 34: #include <sys/wait.h>
35: #include <sys/mman.h>
36: #include <sys/stat.h>
37:
38: #include <ctype.h>
1.4 millert 39: #include <err.h>
1.1 deraadt 40: #include <errno.h>
41: #include <fcntl.h>
42: #include <grp.h>
43: #include <paths.h>
44: #include <pwd.h>
45: #include <stdio.h>
46: #include <stdlib.h>
47: #include <string.h>
48: #include <unistd.h>
1.58 deraadt 49: #include <limits.h>
1.4 millert 50: #include <sysexits.h>
51: #include <utime.h>
1.60 jasper 52: #include <libgen.h>
1.1 deraadt 53:
54: #include "pathnames.h"
55:
1.58 deraadt 56: #define MINIMUM(a, b) (((a) < (b)) ? (a) : (b))
57:
1.19 millert 58: #define DIRECTORY 0x01 /* Tell install it's a directory. */
59: #define SETFLAGS 0x02 /* Tell install to set flags. */
60: #define NOCHANGEBITS (UF_IMMUTABLE | UF_APPEND | SF_IMMUTABLE | SF_APPEND)
61: #define BACKUP_SUFFIX ".old"
62:
1.1 deraadt 63: struct passwd *pp;
64: struct group *gp;
1.60 jasper 65: int dobackup, docompare, dodest, dodir, dopreserve, dostrip, safecopy;
1.1 deraadt 66: int mode = S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
1.58 deraadt 67: char pathbuf[PATH_MAX], tempfile[PATH_MAX];
1.19 millert 68: char *suffix = BACKUP_SUFFIX;
1.1 deraadt 69: uid_t uid;
70: gid_t gid;
71:
1.31 millert 72: void copy(int, char *, int, char *, off_t, int);
1.59 guenther 73: int compare(int, const char *, off_t, int, const char *, off_t);
1.31 millert 74: void install(char *, char *, u_long, u_int);
1.62 ! jasper 75: void install_dir(char *, int);
1.31 millert 76: void strip(char *);
77: void usage(void);
78: int create_newfile(char *, struct stat *);
79: int create_tempfile(char *, char *, size_t);
80: int file_write(int, char *, size_t, int *, int *, int);
1.33 tedu 81: void file_flush(int, int);
1.1 deraadt 82:
83: int
1.35 deraadt 84: main(int argc, char *argv[])
1.1 deraadt 85: {
86: struct stat from_sb, to_sb;
1.41 otto 87: void *set;
1.23 mickey 88: u_int32_t fset;
1.1 deraadt 89: u_int iflags;
90: int ch, no_target;
91: char *flags, *to_name, *group = NULL, *owner = NULL;
92:
93: iflags = 0;
1.60 jasper 94: while ((ch = getopt(argc, argv, "B:bCcDdf:g:m:o:pSs")) != -1)
1.56 okan 95: switch(ch) {
1.4 millert 96: case 'C':
97: docompare = 1;
98: break;
1.19 millert 99: case 'B':
100: suffix = optarg;
101: /* fall through; -B implies -b */
102: case 'b':
103: dobackup = 1;
104: break;
1.1 deraadt 105: case 'c':
1.4 millert 106: /* For backwards compatibility. */
1.1 deraadt 107: break;
108: case 'f':
109: flags = optarg;
1.23 mickey 110: if (strtofflags(&flags, &fset, NULL))
1.4 millert 111: errx(EX_USAGE, "%s: invalid flag", flags);
1.1 deraadt 112: iflags |= SETFLAGS;
113: break;
114: case 'g':
115: group = optarg;
116: break;
117: case 'm':
118: if (!(set = setmode(optarg)))
1.4 millert 119: errx(EX_USAGE, "%s: invalid file mode", optarg);
1.1 deraadt 120: mode = getmode(set, 0);
1.16 deraadt 121: free(set);
1.1 deraadt 122: break;
123: case 'o':
124: owner = optarg;
125: break;
1.4 millert 126: case 'p':
127: docompare = dopreserve = 1;
128: break;
129: case 'S':
130: safecopy = 1;
131: break;
1.1 deraadt 132: case 's':
133: dostrip = 1;
134: break;
1.60 jasper 135: case 'D':
136: dodest = 1;
137: break;
1.1 deraadt 138: case 'd':
139: dodir = 1;
140: break;
141: case '?':
142: default:
143: usage();
144: }
145: argc -= optind;
146: argv += optind;
147:
1.4 millert 148: /* some options make no sense when creating directories */
149: if ((safecopy || docompare || dostrip) && dodir)
1.1 deraadt 150: usage();
151:
152: /* must have at least two arguments, except when creating directories */
153: if (argc < 2 && !dodir)
154: usage();
155:
1.4 millert 156: /* need to make a temp copy so we can compare stripped version */
157: if (docompare && dostrip)
158: safecopy = 1;
159:
1.1 deraadt 160: /* get group and owner id's */
1.55 deraadt 161: if (group && !(gp = getgrnam(group)) && !isdigit((unsigned char)*group))
1.4 millert 162: errx(EX_NOUSER, "unknown group %s", group);
163: gid = (group) ? ((gp) ? gp->gr_gid : (gid_t)strtoul(group, NULL, 10)) : (gid_t)-1;
1.55 deraadt 164: if (owner && !(pp = getpwnam(owner)) && !isdigit((unsigned char)*owner))
1.4 millert 165: errx(EX_NOUSER, "unknown user %s", owner);
166: uid = (owner) ? ((pp) ? pp->pw_uid : (uid_t)strtoul(owner, NULL, 10)) : (uid_t)-1;
1.1 deraadt 167:
168: if (dodir) {
169: for (; *argv != NULL; ++argv)
1.62 ! jasper 170: install_dir(*argv, mode);
1.4 millert 171: exit(EX_OK);
1.1 deraadt 172: /* NOTREACHED */
173: }
174:
1.60 jasper 175: if (dodest) {
176: char *dest = dirname(argv[argc - 1]);
177: if (dest == NULL)
178: errx(EX_OSERR, "cannot determine dirname");
1.62 ! jasper 179: /*
! 180: * When -D is passed, do not chmod the directory with the mode set for
! 181: * the target file. If more restrictive permissions are required then
! 182: * '-d -m' ought to be used instead.
! 183: */
! 184: install_dir(dest, 0755);
1.60 jasper 185: }
186:
1.1 deraadt 187: no_target = stat(to_name = argv[argc - 1], &to_sb);
188: if (!no_target && S_ISDIR(to_sb.st_mode)) {
189: for (; *argv != to_name; ++argv)
190: install(*argv, to_name, fset, iflags | DIRECTORY);
1.4 millert 191: exit(EX_OK);
192: /* NOTREACHED */
1.1 deraadt 193: }
194:
195: /* can't do file1 file2 directory/file */
196: if (argc != 2)
1.32 millert 197: errx(EX_OSERR, "Target: %s", argv[argc-1]);
1.1 deraadt 198:
199: if (!no_target) {
200: if (stat(*argv, &from_sb))
1.4 millert 201: err(EX_OSERR, "%s", *argv);
1.1 deraadt 202: if (!S_ISREG(to_sb.st_mode))
1.57 guenther 203: errc(EX_OSERR, EFTYPE, "%s", to_name);
1.1 deraadt 204: if (to_sb.st_dev == from_sb.st_dev &&
205: to_sb.st_ino == from_sb.st_ino)
1.4 millert 206: errx(EX_USAGE, "%s and %s are the same file", *argv, to_name);
1.1 deraadt 207: }
208: install(*argv, to_name, fset, iflags);
1.4 millert 209: exit(EX_OK);
210: /* NOTREACHED */
1.1 deraadt 211: }
212:
213: /*
214: * install --
215: * build a path name and install the file
216: */
217: void
1.35 deraadt 218: install(char *from_name, char *to_name, u_long fset, u_int flags)
1.1 deraadt 219: {
220: struct stat from_sb, to_sb;
1.59 guenther 221: struct timespec ts[2];
1.4 millert 222: int devnull, from_fd, to_fd, serrno, files_match = 0;
1.1 deraadt 223: char *p;
224:
1.4 millert 225: (void)memset((void *)&from_sb, 0, sizeof(from_sb));
226: (void)memset((void *)&to_sb, 0, sizeof(to_sb));
227:
1.1 deraadt 228: /* If try to install NULL file to a directory, fails. */
229: if (flags & DIRECTORY || strcmp(from_name, _PATH_DEVNULL)) {
230: if (stat(from_name, &from_sb))
1.4 millert 231: err(EX_OSERR, "%s", from_name);
1.1 deraadt 232: if (!S_ISREG(from_sb.st_mode))
1.57 guenther 233: errc(EX_OSERR, EFTYPE, "%s", from_name);
1.1 deraadt 234: /* Build the target path. */
235: if (flags & DIRECTORY) {
236: (void)snprintf(pathbuf, sizeof(pathbuf), "%s/%s",
237: to_name,
1.11 millert 238: (p = strrchr(from_name, '/')) ? ++p : from_name);
1.1 deraadt 239: to_name = pathbuf;
240: }
241: devnull = 0;
242: } else {
243: devnull = 1;
244: }
245:
1.4 millert 246: if (stat(to_name, &to_sb) == 0) {
247: /* Only compare against regular files. */
248: if (docompare && !S_ISREG(to_sb.st_mode)) {
249: docompare = 0;
1.57 guenther 250: warnc(EFTYPE, "%s", to_name);
1.4 millert 251: }
252: } else if (docompare) {
253: /* File does not exist so silently ignore compare flag. */
254: docompare = 0;
255: }
256:
1.52 millert 257: if (!devnull) {
258: if ((from_fd = open(from_name, O_RDONLY, 0)) < 0)
259: err(EX_OSERR, "%s", from_name);
260: }
261:
1.4 millert 262: if (safecopy) {
263: to_fd = create_tempfile(to_name, tempfile, sizeof(tempfile));
264: if (to_fd < 0)
265: err(EX_OSERR, "%s", tempfile);
266: } else if (docompare && !dostrip) {
267: if ((to_fd = open(to_name, O_RDONLY, 0)) < 0)
268: err(EX_OSERR, "%s", to_name);
269: } else {
270: if ((to_fd = create_newfile(to_name, &to_sb)) < 0)
271: err(EX_OSERR, "%s", to_name);
272: }
273:
1.1 deraadt 274: if (!devnull) {
1.4 millert 275: if (docompare && !safecopy) {
276: files_match = !(compare(from_fd, from_name,
1.59 guenther 277: from_sb.st_size, to_fd,
278: to_name, to_sb.st_size));
1.4 millert 279:
280: /* Truncate "to" file for copy unless we match */
281: if (!files_match) {
282: (void)close(to_fd);
283: if ((to_fd = create_newfile(to_name, &to_sb)) < 0)
284: err(EX_OSERR, "%s", to_name);
285: }
1.1 deraadt 286: }
1.4 millert 287: if (!files_match)
288: copy(from_fd, from_name, to_fd,
1.17 millert 289: safecopy ? tempfile : to_name, from_sb.st_size,
290: ((off_t)from_sb.st_blocks * S_BLKSIZE < from_sb.st_size));
1.1 deraadt 291: }
1.2 deraadt 292:
293: if (dostrip) {
1.4 millert 294: strip(safecopy ? tempfile : to_name);
1.2 deraadt 295:
296: /*
297: * Re-open our fd on the target, in case we used a strip
298: * that does not work in-place -- like gnu binutils strip.
299: */
300: close(to_fd);
1.4 millert 301: if ((to_fd = open(safecopy ? tempfile : to_name, O_RDONLY,
302: 0)) < 0)
303: err(EX_OSERR, "stripping %s", to_name);
304: }
305:
306: /*
307: * Compare the (possibly stripped) temp file to the target.
308: */
309: if (safecopy && docompare) {
310: int temp_fd = to_fd;
311: struct stat temp_sb;
312:
313: /* Re-open to_fd using the real target name. */
314: if ((to_fd = open(to_name, O_RDONLY, 0)) < 0)
315: err(EX_OSERR, "%s", to_name);
316:
317: if (fstat(temp_fd, &temp_sb)) {
318: serrno = errno;
319: (void)unlink(tempfile);
1.57 guenther 320: errc(EX_OSERR, serrno, "%s", tempfile);
1.4 millert 321: }
322:
1.59 guenther 323: if (compare(temp_fd, tempfile, temp_sb.st_size, to_fd,
324: to_name, to_sb.st_size) == 0) {
1.4 millert 325: /*
326: * If target has more than one link we need to
327: * replace it in order to snap the extra links.
328: * Need to preserve target file times, though.
329: */
330: if (to_sb.st_nlink != 1) {
1.59 guenther 331: ts[0] = to_sb.st_atim;
332: ts[1] = to_sb.st_mtim;
333: futimens(temp_fd, ts);
1.4 millert 334: } else {
335: files_match = 1;
336: (void)unlink(tempfile);
337: }
338: }
1.48 phessler 339: (void)close(to_fd);
340: to_fd = temp_fd;
1.4 millert 341: }
342:
343: /*
1.30 millert 344: * Preserve the timestamp of the source file if necessary.
1.4 millert 345: */
346: if (dopreserve && !files_match) {
1.59 guenther 347: ts[0] = from_sb.st_atim;
348: ts[1] = from_sb.st_mtim;
349: futimens(to_fd, ts);
1.2 deraadt 350: }
351:
1.1 deraadt 352: /*
353: * Set owner, group, mode for target; do the chown first,
354: * chown may lose the setuid bits.
355: */
1.48 phessler 356: if ((gid != (gid_t)-1 || uid != (uid_t)-1) &&
357: fchown(to_fd, uid, gid)) {
1.1 deraadt 358: serrno = errno;
1.48 phessler 359: (void)unlink(safecopy ? tempfile : to_name);
1.61 jasper 360: errx(EX_OSERR, "%s: chown/chgrp: %s",
1.48 phessler 361: safecopy ? tempfile : to_name, strerror(serrno));
1.1 deraadt 362: }
363: if (fchmod(to_fd, mode)) {
364: serrno = errno;
1.48 phessler 365: (void)unlink(safecopy ? tempfile : to_name);
366: errx(EX_OSERR, "%s: chmod: %s", safecopy ? tempfile : to_name,
367: strerror(serrno));
1.1 deraadt 368: }
369:
370: /*
371: * If provided a set of flags, set them, otherwise, preserve the
372: * flags, except for the dump flag.
373: */
374: if (fchflags(to_fd,
375: flags & SETFLAGS ? fset : from_sb.st_flags & ~UF_NODUMP)) {
1.12 millert 376: if (errno != EOPNOTSUPP || (from_sb.st_flags & ~UF_NODUMP) != 0)
1.61 jasper 377: warnx("%s: chflags: %s",
1.48 phessler 378: safecopy ? tempfile :to_name, strerror(errno));
1.1 deraadt 379: }
380:
381: (void)close(to_fd);
1.18 millert 382: if (!devnull)
383: (void)close(from_fd);
1.48 phessler 384:
385: /*
386: * Move the new file into place if doing a safe copy
387: * and the files are different (or just not compared).
388: */
389: if (safecopy && !files_match) {
390: /* Try to turn off the immutable bits. */
391: if (to_sb.st_flags & (NOCHANGEBITS))
392: (void)chflags(to_name, to_sb.st_flags & ~(NOCHANGEBITS));
393: if (dobackup) {
1.58 deraadt 394: char backup[PATH_MAX];
395: (void)snprintf(backup, PATH_MAX, "%s%s", to_name,
1.48 phessler 396: suffix);
397: /* It is ok for the target file not to exist. */
398: if (rename(to_name, backup) < 0 && errno != ENOENT) {
399: serrno = errno;
400: unlink(tempfile);
401: errx(EX_OSERR, "rename: %s to %s: %s", to_name,
402: backup, strerror(serrno));
403: }
404: }
405: if (rename(tempfile, to_name) < 0 ) {
406: serrno = errno;
407: unlink(tempfile);
408: errx(EX_OSERR, "rename: %s to %s: %s", tempfile,
409: to_name, strerror(serrno));
410: }
411: }
1.1 deraadt 412: }
413:
414: /*
415: * copy --
416: * copy from one file to another
417: */
418: void
1.35 deraadt 419: copy(int from_fd, char *from_name, int to_fd, char *to_name, off_t size,
420: int sparse)
1.1 deraadt 421: {
1.29 mpech 422: ssize_t nr, nw;
1.1 deraadt 423: int serrno;
424: char *p, buf[MAXBSIZE];
425:
1.51 millert 426: if (size == 0)
427: return;
428:
1.7 millert 429: /* Rewind file descriptors. */
430: if (lseek(from_fd, (off_t)0, SEEK_SET) == (off_t)-1)
431: err(EX_OSERR, "lseek: %s", from_name);
432: if (lseek(to_fd, (off_t)0, SEEK_SET) == (off_t)-1)
433: err(EX_OSERR, "lseek: %s", to_name);
434:
1.1 deraadt 435: /*
436: * Mmap and write if less than 8M (the limit is so we don't totally
437: * trash memory on big files. This is really a minor hack, but it
1.17 millert 438: * wins some CPU back. Sparse files need special treatment.
1.1 deraadt 439: */
1.51 millert 440: if (!sparse && size <= 8 * 1048576) {
1.50 espie 441: size_t siz;
1.17 millert 442:
1.19 millert 443: if ((p = mmap(NULL, (size_t)size, PROT_READ, MAP_PRIVATE,
1.38 grange 444: from_fd, (off_t)0)) == MAP_FAILED) {
1.6 millert 445: serrno = errno;
446: (void)unlink(to_name);
1.57 guenther 447: errc(EX_OSERR, serrno, "%s", from_name);
1.6 millert 448: }
1.50 espie 449: madvise(p, size, MADV_SEQUENTIAL);
1.3 deraadt 450: siz = (size_t)size;
1.6 millert 451: if ((nw = write(to_fd, p, siz)) != siz) {
452: serrno = errno;
453: (void)unlink(to_name);
454: errx(EX_OSERR, "%s: %s",
455: to_name, strerror(nw > 0 ? EIO : serrno));
456: }
1.4 millert 457: (void) munmap(p, (size_t)size);
1.1 deraadt 458: } else {
1.17 millert 459: int sz, rem, isem = 1;
460: struct stat sb;
461:
462: /*
463: * Pass the blocksize of the file being written to the write
464: * routine. if the size is zero, use the default S_BLKSIZE.
465: */
466: if (fstat(to_fd, &sb) != 0 || sb.st_blksize == 0)
467: sz = S_BLKSIZE;
468: else
469: sz = sb.st_blksize;
1.20 millert 470: rem = sz;
1.17 millert 471:
472: while ((nr = read(from_fd, buf, sizeof(buf))) > 0) {
473: if (sparse)
474: nw = file_write(to_fd, buf, nr, &rem, &isem, sz);
475: else
476: nw = write(to_fd, buf, nr);
477: if (nw != nr) {
1.1 deraadt 478: serrno = errno;
479: (void)unlink(to_name);
1.4 millert 480: errx(EX_OSERR, "%s: %s",
1.1 deraadt 481: to_name, strerror(nw > 0 ? EIO : serrno));
482: }
1.17 millert 483: }
1.33 tedu 484: if (sparse)
485: file_flush(to_fd, isem);
1.1 deraadt 486: if (nr != 0) {
487: serrno = errno;
488: (void)unlink(to_name);
1.57 guenther 489: errc(EX_OSERR, serrno, "%s", from_name);
1.1 deraadt 490: }
491: }
492: }
493:
494: /*
1.4 millert 495: * compare --
496: * compare two files; non-zero means files differ
497: */
498: int
1.59 guenther 499: compare(int from_fd, const char *from_name, off_t from_len, int to_fd,
500: const char *to_name, off_t to_len)
1.4 millert 501: {
502: caddr_t p1, p2;
1.59 guenther 503: size_t length;
504: off_t from_off, to_off, remainder;
1.4 millert 505: int dfound;
506:
1.50 espie 507: if (from_len == 0 && from_len == to_len)
508: return (0);
509:
1.4 millert 510: if (from_len != to_len)
1.50 espie 511: return (1);
1.4 millert 512:
513: /*
514: * Compare the two files being careful not to mmap
515: * more than 8M at a time.
516: */
1.21 millert 517: from_off = to_off = (off_t)0;
1.4 millert 518: remainder = from_len;
519: do {
1.58 deraadt 520: length = MINIMUM(remainder, 8 * 1048576);
1.4 millert 521: remainder -= length;
522:
1.22 mickey 523: if ((p1 = mmap(NULL, length, PROT_READ, MAP_PRIVATE,
1.38 grange 524: from_fd, from_off)) == MAP_FAILED)
1.4 millert 525: err(EX_OSERR, "%s", from_name);
1.22 mickey 526: if ((p2 = mmap(NULL, length, PROT_READ, MAP_PRIVATE,
1.38 grange 527: to_fd, to_off)) == MAP_FAILED)
1.4 millert 528: err(EX_OSERR, "%s", to_name);
1.39 mickey 529: if (length) {
530: madvise(p1, length, MADV_SEQUENTIAL);
531: madvise(p2, length, MADV_SEQUENTIAL);
532: }
1.4 millert 533:
534: dfound = memcmp(p1, p2, length);
535:
536: (void) munmap(p1, length);
537: (void) munmap(p2, length);
1.21 millert 538:
539: from_off += length;
540: to_off += length;
1.4 millert 541:
1.7 millert 542: } while (!dfound && remainder > 0);
1.4 millert 543:
544: return(dfound);
545: }
546:
547: /*
1.1 deraadt 548: * strip --
549: * use strip(1) to strip the target file
550: */
551: void
1.35 deraadt 552: strip(char *to_name)
1.1 deraadt 553: {
554: int serrno, status;
1.26 millert 555: char * volatile path_strip;
1.18 millert 556:
1.13 millert 557: if (issetugid() || (path_strip = getenv("STRIP")) == NULL)
558: path_strip = _PATH_STRIP;
1.1 deraadt 559:
560: switch (vfork()) {
561: case -1:
562: serrno = errno;
563: (void)unlink(to_name);
1.57 guenther 564: errc(EX_TEMPFAIL, serrno, "forks");
1.1 deraadt 565: case 0:
1.44 moritz 566: execl(path_strip, "strip", "--", to_name, (char *)NULL);
1.13 millert 567: warn("%s", path_strip);
1.9 deraadt 568: _exit(EX_OSERR);
1.1 deraadt 569: default:
1.14 millert 570: if (wait(&status) == -1 || !WIFEXITED(status))
1.1 deraadt 571: (void)unlink(to_name);
572: }
573: }
574:
575: /*
576: * install_dir --
1.42 jsg 577: * build directory hierarchy
1.1 deraadt 578: */
579: void
1.62 ! jasper 580: install_dir(char *path, int mode)
1.1 deraadt 581: {
1.29 mpech 582: char *p;
1.8 imp 583: struct stat sb;
584: int ch;
585:
586: for (p = path;; ++p)
587: if (!*p || (p != path && *p == '/')) {
588: ch = *p;
589: *p = '\0';
1.54 naddy 590: if (mkdir(path, 0777)) {
591: int mkdir_errno = errno;
592: if (stat(path, &sb)) {
593: /* Not there; use mkdir()s errno */
1.57 guenther 594: errc(EX_OSERR, mkdir_errno, "%s",
595: path);
1.54 naddy 596: /* NOTREACHED */
597: }
598: if (!S_ISDIR(sb.st_mode)) {
599: /* Is there, but isn't a directory */
1.57 guenther 600: errc(EX_OSERR, ENOTDIR, "%s", path);
1.1 deraadt 601: /* NOTREACHED */
1.8 imp 602: }
603: }
604: if (!(*p = ch))
1.1 deraadt 605: break;
1.8 imp 606: }
1.1 deraadt 607:
1.4 millert 608: if (((gid != (gid_t)-1 || uid != (uid_t)-1) && chown(path, uid, gid)) ||
1.8 imp 609: chmod(path, mode)) {
610: warn("%s", path);
611: }
1.1 deraadt 612: }
613:
614: /*
615: * usage --
616: * print a usage message and die
617: */
618: void
1.35 deraadt 619: usage(void)
1.1 deraadt 620: {
621: (void)fprintf(stderr, "\
1.60 jasper 622: usage: install [-bCcDdpSs] [-B suffix] [-f flags] [-g group] [-m mode] [-o owner]\n source ... target ...\n");
1.4 millert 623: exit(EX_USAGE);
624: /* NOTREACHED */
625: }
626:
627: /*
628: * create_tempfile --
629: * create a temporary file based on path and open it
630: */
631: int
1.35 deraadt 632: create_tempfile(char *path, char *temp, size_t tsize)
1.4 millert 633: {
634: char *p;
635:
1.43 millert 636: strlcpy(temp, path, tsize);
637: if ((p = strrchr(temp, '/')) != NULL)
1.4 millert 638: p++;
639: else
640: p = temp;
1.43 millert 641: *p = '\0';
642: strlcat(p, "INS@XXXXXXXXXX", tsize);
1.4 millert 643:
644: return(mkstemp(temp));
645: }
646:
647: /*
648: * create_newfile --
1.30 millert 649: * create a new file, overwriting an existing one if necessary
1.4 millert 650: */
651: int
1.35 deraadt 652: create_newfile(char *path, struct stat *sbp)
1.4 millert 653: {
1.58 deraadt 654: char backup[PATH_MAX];
1.19 millert 655:
1.4 millert 656: /*
657: * Unlink now... avoid ETXTBSY errors later. Try and turn
658: * off the append/immutable bits -- if we fail, go ahead,
659: * it might work.
660: */
661: if (sbp->st_flags & (NOCHANGEBITS))
662: (void)chflags(path, sbp->st_flags & ~(NOCHANGEBITS));
663:
1.19 millert 664: if (dobackup) {
1.58 deraadt 665: (void)snprintf(backup, PATH_MAX, "%s%s", path, suffix);
1.28 heko 666: /* It is ok for the target file not to exist. */
667: if (rename(path, backup) < 0 && errno != ENOENT)
668: err(EX_OSERR, "rename: %s to %s (errno %d)", path, backup, errno);
1.53 miod 669: } else {
670: if (unlink(path) < 0 && errno != ENOENT)
671: err(EX_OSERR, "%s", path);
672: }
1.4 millert 673:
1.52 millert 674: return(open(path, O_CREAT | O_RDWR | O_EXCL, S_IRUSR | S_IWUSR));
1.17 millert 675: }
676:
677: /*
678: * file_write()
679: * Write/copy a file (during copy or archive extract). This routine knows
680: * how to copy files with lseek holes in it. (Which are read as file
681: * blocks containing all 0's but do not have any file blocks associated
682: * with the data). Typical examples of these are files created by dbm
683: * variants (.pag files). While the file size of these files are huge, the
684: * actual storage is quite small (the files are sparse). The problem is
685: * the holes read as all zeros so are probably stored on the archive that
686: * way (there is no way to determine if the file block is really a hole,
687: * we only know that a file block of all zero's can be a hole).
688: * At this writing, no major archive format knows how to archive files
689: * with holes. However, on extraction (or during copy, -rw) we have to
690: * deal with these files. Without detecting the holes, the files can
691: * consume a lot of file space if just written to disk. This replacement
692: * for write when passed the basic allocation size of a file system block,
693: * uses lseek whenever it detects the input data is all 0 within that
694: * file block. In more detail, the strategy is as follows:
695: * While the input is all zero keep doing an lseek. Keep track of when we
1.45 krw 696: * pass over file block boundaries. Only write when we hit a non zero
1.17 millert 697: * input. once we have written a file block, we continue to write it to
698: * the end (we stop looking at the input). When we reach the start of the
699: * next file block, start checking for zero blocks again. Working on file
1.45 krw 700: * block boundaries significantly reduces the overhead when copying files
1.17 millert 701: * that are NOT very sparse. This overhead (when compared to a write) is
702: * almost below the measurement resolution on many systems. Without it,
703: * files with holes cannot be safely copied. It does has a side effect as
704: * it can put holes into files that did not have them before, but that is
705: * not a problem since the file contents are unchanged (in fact it saves
706: * file space). (Except on paging files for diskless clients. But since we
707: * cannot determine one of those file from here, we ignore them). If this
708: * ever ends up on a system where CTG files are supported and the holes
709: * are not desired, just do a conditional test in those routines that
710: * call file_write() and have it call write() instead. BEFORE CLOSING THE
711: * FILE, make sure to call file_flush() when the last write finishes with
712: * an empty block. A lot of file systems will not create an lseek hole at
713: * the end. In this case we drop a single 0 at the end to force the
714: * trailing 0's in the file.
715: * ---Parameters---
716: * rem: how many bytes left in this file system block
717: * isempt: have we written to the file block yet (is it empty)
718: * sz: basic file block allocation size
719: * cnt: number of bytes on this write
720: * str: buffer to write
721: * Return:
722: * number of bytes written, -1 on write (or lseek) error.
723: */
724:
725: int
1.35 deraadt 726: file_write(int fd, char *str, size_t cnt, int *rem, int *isempt, int sz)
1.17 millert 727: {
1.29 mpech 728: char *pt;
729: char *end;
730: size_t wcnt;
731: char *st = str;
1.17 millert 732:
733: /*
734: * while we have data to process
735: */
736: while (cnt) {
737: if (!*rem) {
738: /*
739: * We are now at the start of file system block again
740: * (or what we think one is...). start looking for
741: * empty blocks again
742: */
743: *isempt = 1;
744: *rem = sz;
745: }
746:
747: /*
748: * only examine up to the end of the current file block or
749: * remaining characters to write, whatever is smaller
750: */
1.58 deraadt 751: wcnt = MINIMUM(cnt, *rem);
1.17 millert 752: cnt -= wcnt;
753: *rem -= wcnt;
754: if (*isempt) {
755: /*
756: * have not written to this block yet, so we keep
757: * looking for zero's
758: */
759: pt = st;
760: end = st + wcnt;
761:
762: /*
763: * look for a zero filled buffer
764: */
765: while ((pt < end) && (*pt == '\0'))
766: ++pt;
767:
768: if (pt == end) {
769: /*
770: * skip, buf is empty so far
771: */
772: if (lseek(fd, (off_t)wcnt, SEEK_CUR) < 0) {
773: warn("lseek");
774: return(-1);
775: }
776: st = pt;
777: continue;
778: }
779: /*
780: * drat, the buf is not zero filled
781: */
782: *isempt = 0;
783: }
784:
785: /*
786: * have non-zero data in this file system block, have to write
787: */
788: if (write(fd, st, wcnt) != wcnt) {
789: warn("write");
790: return(-1);
791: }
792: st += wcnt;
793: }
794: return(st - str);
1.33 tedu 795: }
796:
797: /*
798: * file_flush()
799: * when the last file block in a file is zero, many file systems will not
800: * let us create a hole at the end. To get the last block with zeros, we
801: * write the last BYTE with a zero (back up one byte and write a zero).
802: */
803: void
804: file_flush(int fd, int isempt)
805: {
806: static char blnk[] = "\0";
807:
808: /*
809: * silly test, but make sure we are only called when the last block is
810: * filled with all zeros.
811: */
812: if (!isempt)
813: return;
814:
815: /*
816: * move back one byte and write a zero
817: */
818: if (lseek(fd, (off_t)-1, SEEK_CUR) < 0) {
819: warn("Failed seek on file");
820: return;
821: }
822:
823: if (write(fd, blnk, 1) < 0)
824: warn("Failed write to file");
825: return;
1.1 deraadt 826: }