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