xref: /onnv-gate/usr/src/ucbcmd/install.d/install.c (revision 669:3cf64fd04122)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
230Sstevel@tonic-gate  * Copyright 1996 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
280Sstevel@tonic-gate /*	  All Rights Reserved  	*/
290Sstevel@tonic-gate 
300Sstevel@tonic-gate /*
310Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
320Sstevel@tonic-gate  * The Regents of the University of California
330Sstevel@tonic-gate  * All Rights Reserved
340Sstevel@tonic-gate  *
350Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
360Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
370Sstevel@tonic-gate  * contributors.
380Sstevel@tonic-gate  */
390Sstevel@tonic-gate 
400Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
410Sstevel@tonic-gate 
420Sstevel@tonic-gate #include <sys/param.h>
430Sstevel@tonic-gate #include <sys/types.h>
440Sstevel@tonic-gate #include <sys/stat.h>
450Sstevel@tonic-gate #include <sys/file.h>
460Sstevel@tonic-gate #include <fcntl.h>
470Sstevel@tonic-gate #include <grp.h>
480Sstevel@tonic-gate #include <pwd.h>
490Sstevel@tonic-gate #include <stdio.h>
500Sstevel@tonic-gate #include <ctype.h>
510Sstevel@tonic-gate #include <errno.h>
520Sstevel@tonic-gate #include <locale.h>
530Sstevel@tonic-gate 
540Sstevel@tonic-gate #define	DEF_GROUP	"staff"		/* default group */
550Sstevel@tonic-gate #define	DEF_OWNER	"root"		/* default owner */
560Sstevel@tonic-gate #define	DEF_MODE	0755		/* default mode */
570Sstevel@tonic-gate 
580Sstevel@tonic-gate char *group = DEF_GROUP;
590Sstevel@tonic-gate char *owner = DEF_OWNER;
600Sstevel@tonic-gate int mode    = DEF_MODE;
610Sstevel@tonic-gate int sflag = 0;
620Sstevel@tonic-gate struct passwd *pp;
630Sstevel@tonic-gate struct group *gp;
640Sstevel@tonic-gate extern int errno;
650Sstevel@tonic-gate int copy();
660Sstevel@tonic-gate void usage();
670Sstevel@tonic-gate 
68*669Schin int
main(int argc,char ** argv)69*669Schin main(int argc, char **argv)
700Sstevel@tonic-gate {
710Sstevel@tonic-gate 	extern char	*optarg;
720Sstevel@tonic-gate 	extern int	optind;
730Sstevel@tonic-gate 	struct stat	stb;
740Sstevel@tonic-gate 	char	*dirname;
750Sstevel@tonic-gate 	int	ch;
760Sstevel@tonic-gate 	int	i;
770Sstevel@tonic-gate 	int	rc;
780Sstevel@tonic-gate 	int	dflag = 0;
790Sstevel@tonic-gate 	int	gflag = 0;
800Sstevel@tonic-gate 	int	oflag = 0;
810Sstevel@tonic-gate 	int	mflag = 0;
820Sstevel@tonic-gate 
830Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
840Sstevel@tonic-gate 
850Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
860Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST"
870Sstevel@tonic-gate #endif
880Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
890Sstevel@tonic-gate 
900Sstevel@tonic-gate 	while ((ch = getopt(argc, argv, "dcg:o:m:s")) != EOF)
910Sstevel@tonic-gate 		switch((char)ch) {
920Sstevel@tonic-gate 		case 'c':
930Sstevel@tonic-gate 			break;	/* always do "copy" */
940Sstevel@tonic-gate 		case 'd':
950Sstevel@tonic-gate 			dflag++;
960Sstevel@tonic-gate 			break;
970Sstevel@tonic-gate 		case 'g':
980Sstevel@tonic-gate 			gflag++;
990Sstevel@tonic-gate 			group = optarg;
1000Sstevel@tonic-gate 			break;
1010Sstevel@tonic-gate 		case 'm':
1020Sstevel@tonic-gate 			mflag++;
1030Sstevel@tonic-gate 			mode = atoo(optarg);
1040Sstevel@tonic-gate 			break;
1050Sstevel@tonic-gate 		case 'o':
1060Sstevel@tonic-gate 			oflag++;
1070Sstevel@tonic-gate 			owner = optarg;
1080Sstevel@tonic-gate 			break;
1090Sstevel@tonic-gate 		case 's':
1100Sstevel@tonic-gate 			sflag++;
1110Sstevel@tonic-gate 			break;
1120Sstevel@tonic-gate 		case '?':
1130Sstevel@tonic-gate 		default:
1140Sstevel@tonic-gate 			usage();
1150Sstevel@tonic-gate 		}
1160Sstevel@tonic-gate 	argc -= optind;
1170Sstevel@tonic-gate 	argv += optind;
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate 	/* get group and owner id's */
1200Sstevel@tonic-gate 	if (!(gp = getgrnam(group))) {
1210Sstevel@tonic-gate 		fprintf(stderr, gettext("install: unknown group %s.\n"), group);
1220Sstevel@tonic-gate 		exit(1);
1230Sstevel@tonic-gate 	}
1240Sstevel@tonic-gate 	if (!(pp = getpwnam(owner))) {
1250Sstevel@tonic-gate 		fprintf(stderr, gettext("install: unknown user %s.\n"), owner);
1260Sstevel@tonic-gate 		exit(1);
1270Sstevel@tonic-gate 	}
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate 	if (dflag) {		/* install a directory */
1300Sstevel@tonic-gate 		int exists = 0;
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate 		if (argc != 1)
1330Sstevel@tonic-gate 			usage();
1340Sstevel@tonic-gate 		dirname = argv[0];
1350Sstevel@tonic-gate 		if (mkdirp(dirname, 0777) < 0) {
1360Sstevel@tonic-gate 			exists = errno == EEXIST;
1370Sstevel@tonic-gate 			if (!exists) {
1380Sstevel@tonic-gate 				fprintf(stderr, gettext("install: mkdir: %s: %s\n"), dirname, strerror(errno));
1390Sstevel@tonic-gate 				exit(1);
1400Sstevel@tonic-gate 			}
1410Sstevel@tonic-gate 		}
1420Sstevel@tonic-gate 		if (stat(dirname, &stb) < 0) {
1430Sstevel@tonic-gate 			fprintf(stderr, gettext("install: stat: %s: %s\n"), dirname, strerror(errno));
1440Sstevel@tonic-gate 			exit(1);
1450Sstevel@tonic-gate 		}
1460Sstevel@tonic-gate 		if ((stb.st_mode&S_IFMT) != S_IFDIR) {
1470Sstevel@tonic-gate 			fprintf(stderr, gettext("install: %s is not a directory\n"), dirname);
1480Sstevel@tonic-gate 		}
1490Sstevel@tonic-gate 		/* make sure directory setgid bit is inherited */
1500Sstevel@tonic-gate 		mode = (mode & ~S_ISGID) | (stb.st_mode & S_ISGID);
1510Sstevel@tonic-gate 		if (mflag && chmod(dirname, mode)) {
1520Sstevel@tonic-gate 			fprintf(stderr, gettext("install: chmod: %s: %s\n"), dirname, strerror(errno));
1530Sstevel@tonic-gate 			if (!exists)
1540Sstevel@tonic-gate 				(void) unlink(dirname);
1550Sstevel@tonic-gate 			exit(1) ;
1560Sstevel@tonic-gate 		}
1570Sstevel@tonic-gate 		if (oflag && chown(dirname, pp->pw_uid, -1) && errno != EPERM) {
1580Sstevel@tonic-gate 			fprintf(stderr, gettext("install: chown: %s: %s\n"), dirname, strerror(errno));
1590Sstevel@tonic-gate 			if (!exists)
1600Sstevel@tonic-gate 				(void) unlink(dirname);
1610Sstevel@tonic-gate 			exit(1) ;
1620Sstevel@tonic-gate 		}
1630Sstevel@tonic-gate 		if (gflag && chown(dirname, -1, gp->gr_gid) && errno != EPERM) {
1640Sstevel@tonic-gate 			fprintf(stderr, gettext("install: chgrp: %s: %s\n"), dirname, strerror(errno));
1650Sstevel@tonic-gate 			if (!exists)
1660Sstevel@tonic-gate 				(void) unlink(dirname);
1670Sstevel@tonic-gate 			exit(1) ;
1680Sstevel@tonic-gate 		}
1690Sstevel@tonic-gate 		exit(0);
1700Sstevel@tonic-gate 	}
1710Sstevel@tonic-gate 
1720Sstevel@tonic-gate 	if (argc < 2)
1730Sstevel@tonic-gate 		usage();
1740Sstevel@tonic-gate 
1750Sstevel@tonic-gate         if (argc > 2) {		/* last arg must be a directory */
1760Sstevel@tonic-gate                 if (stat(argv[argc-1], &stb) < 0)
1770Sstevel@tonic-gate                         usage();
1780Sstevel@tonic-gate                 if ((stb.st_mode&S_IFMT) != S_IFDIR)
1790Sstevel@tonic-gate                         usage();
1800Sstevel@tonic-gate         }
1810Sstevel@tonic-gate         rc = 0;
1820Sstevel@tonic-gate         for (i = 0; i < argc-1; i++)
1830Sstevel@tonic-gate                 rc |= install(argv[i], argv[argc-1]);
184*669Schin         return (rc);
1850Sstevel@tonic-gate }
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate int
install(from,to)1880Sstevel@tonic-gate install(from, to)
1890Sstevel@tonic-gate 	char *from, *to;
1900Sstevel@tonic-gate {
1910Sstevel@tonic-gate 	int to_fd;
1920Sstevel@tonic-gate 	int devnull;
1930Sstevel@tonic-gate 	int status = 0;
1940Sstevel@tonic-gate 	char *path;
1950Sstevel@tonic-gate 	struct stat from_sb, to_sb;
1960Sstevel@tonic-gate 	static char pbuf[MAXPATHLEN];
1970Sstevel@tonic-gate 	char buf[MAXPATHLEN + 10];
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate 	/* check source */
2000Sstevel@tonic-gate 	if (stat(from, &from_sb)) {
2010Sstevel@tonic-gate 		fprintf(stderr, gettext("install: %s: %s\n"), from, strerror(errno));
2020Sstevel@tonic-gate 		return (1);
2030Sstevel@tonic-gate 	}
2040Sstevel@tonic-gate 	/* special case for removing files */
2050Sstevel@tonic-gate 	devnull = !strcmp(from, "/dev/null");
2060Sstevel@tonic-gate 	if (!devnull && !((from_sb.st_mode&S_IFMT) == S_IFREG)) {
2070Sstevel@tonic-gate 		fprintf(stderr, gettext("install: %s isn't a regular file.\n"), from);
2080Sstevel@tonic-gate 		return (1);
2090Sstevel@tonic-gate 	}
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate 	/* build target path, find out if target is same as source */
2120Sstevel@tonic-gate 	if (!stat(path = to, &to_sb)) {
2130Sstevel@tonic-gate 		if ((to_sb.st_mode&S_IFMT) == S_IFDIR) {
2140Sstevel@tonic-gate 			char *C, *strrchr();
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate 			(void) sprintf(path = pbuf, "%s/%s", to, (C = strrchr(from, '/')) ? ++C : from);
2170Sstevel@tonic-gate 			if (stat(path, &to_sb))
2180Sstevel@tonic-gate 				goto nocompare;
2190Sstevel@tonic-gate 		}
2200Sstevel@tonic-gate 		if ((to_sb.st_mode&S_IFMT) != S_IFREG) {
2210Sstevel@tonic-gate 			fprintf(stderr, gettext("install: %s isn't a regular file.\n"), path);
2220Sstevel@tonic-gate 			return (1);
2230Sstevel@tonic-gate 		}
2240Sstevel@tonic-gate 		if (to_sb.st_dev == from_sb.st_dev && to_sb.st_ino == from_sb.st_ino) {
2250Sstevel@tonic-gate 			fprintf(stderr, gettext("install: %s and %s are the same file.\n"), from, path);
2260Sstevel@tonic-gate 			return (1);
2270Sstevel@tonic-gate 		}
2280Sstevel@tonic-gate 		/* unlink now... avoid ETXTBSY errors later */
2290Sstevel@tonic-gate 		(void) unlink(path);
2300Sstevel@tonic-gate 	}
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate nocompare:
2330Sstevel@tonic-gate 	/* open target, set mode, owner, group */
2340Sstevel@tonic-gate 	if ((to_fd = open(path, O_CREAT|O_WRONLY|O_TRUNC, 0)) < 0) {
2350Sstevel@tonic-gate 		fprintf(stderr, gettext("install: %s: %s\n"), path, strerror(errno));
2360Sstevel@tonic-gate 		return (1);
2370Sstevel@tonic-gate 	}
2380Sstevel@tonic-gate 	if (fchmod(to_fd, mode)) {
2390Sstevel@tonic-gate 		fprintf(stderr, gettext("install: chmod: %s: %s\n"), path, strerror(errno));
2400Sstevel@tonic-gate 		status = 1;
2410Sstevel@tonic-gate 		close(to_fd);
2420Sstevel@tonic-gate 		goto inst_done;
2430Sstevel@tonic-gate 	}
2440Sstevel@tonic-gate 	if (!devnull) {
2450Sstevel@tonic-gate 		status = copy(from, to_fd, path);  /* copy */
2460Sstevel@tonic-gate 		close(to_fd);
2470Sstevel@tonic-gate 	}
2480Sstevel@tonic-gate 	if (sflag) {
2490Sstevel@tonic-gate 		sprintf(buf, "strip %s", path);
2500Sstevel@tonic-gate 		system(buf);
2510Sstevel@tonic-gate 	}
2520Sstevel@tonic-gate 	if (chown(path, pp->pw_uid, gp->gr_gid) && errno != EPERM) {
2530Sstevel@tonic-gate 		fprintf(stderr, gettext("install: chown: %s: %s\n"), path, strerror(errno));
2540Sstevel@tonic-gate 		status = 1;
2550Sstevel@tonic-gate 	}
2560Sstevel@tonic-gate 
2570Sstevel@tonic-gate inst_done:
2580Sstevel@tonic-gate 	if (status)
2590Sstevel@tonic-gate 		(void) unlink(path);
2600Sstevel@tonic-gate 	return (status);
2610Sstevel@tonic-gate }
2620Sstevel@tonic-gate 
2630Sstevel@tonic-gate /*
2640Sstevel@tonic-gate  * copy --
2650Sstevel@tonic-gate  *	copy from one file to another
2660Sstevel@tonic-gate  */
2670Sstevel@tonic-gate int
copy(from_name,to_fd,to_name)2680Sstevel@tonic-gate copy(from_name, to_fd, to_name)
2690Sstevel@tonic-gate 	int to_fd;
2700Sstevel@tonic-gate 	char *from_name, *to_name;
2710Sstevel@tonic-gate {
2720Sstevel@tonic-gate 	int n, from_fd;
2730Sstevel@tonic-gate 	int status = 0;
2740Sstevel@tonic-gate 	char buf[MAXBSIZE];
2750Sstevel@tonic-gate 
2760Sstevel@tonic-gate 	if ((from_fd = open(from_name, O_RDONLY, 0)) < 0) {
2770Sstevel@tonic-gate 		fprintf(stderr, gettext("install: open: %s: %s\n"), from_name, strerror(errno));
2780Sstevel@tonic-gate 		return (1);
2790Sstevel@tonic-gate 	}
2800Sstevel@tonic-gate 	while ((n = read(from_fd, buf, sizeof(buf))) > 0)
2810Sstevel@tonic-gate 		if (write(to_fd, buf, n) != n) {
2820Sstevel@tonic-gate 			fprintf(stderr, gettext("install: write: %s: %s\n"), to_name, strerror(errno));
2830Sstevel@tonic-gate 		status = 1;
2840Sstevel@tonic-gate 		goto copy_done;
2850Sstevel@tonic-gate 		}
2860Sstevel@tonic-gate 	if (n == -1) {
2870Sstevel@tonic-gate 		fprintf(stderr, gettext("install: read: %s: %s\n"), from_name, strerror(errno));
2880Sstevel@tonic-gate 		status = 1;
2890Sstevel@tonic-gate 		goto copy_done;
2900Sstevel@tonic-gate 	}
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate copy_done:
2930Sstevel@tonic-gate 	(void) close(from_fd);
2940Sstevel@tonic-gate 	return (status);
2950Sstevel@tonic-gate }
2960Sstevel@tonic-gate 
2970Sstevel@tonic-gate /*
2980Sstevel@tonic-gate  * atoo --
2990Sstevel@tonic-gate  *      octal string to int
3000Sstevel@tonic-gate  */
3010Sstevel@tonic-gate int
atoo(str)3020Sstevel@tonic-gate atoo(str)
303*669Schin         char   *str;
3040Sstevel@tonic-gate {
305*669Schin         int    val;
3060Sstevel@tonic-gate 
3070Sstevel@tonic-gate         for (val = 0; isdigit(*str); ++str)
3080Sstevel@tonic-gate                 val = val * 8 + *str - '0';
3090Sstevel@tonic-gate         return(val);
3100Sstevel@tonic-gate }
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate 
3130Sstevel@tonic-gate /*
3140Sstevel@tonic-gate  * usage --
3150Sstevel@tonic-gate  *	print a usage message and die
3160Sstevel@tonic-gate  */
3170Sstevel@tonic-gate void
usage()3180Sstevel@tonic-gate usage()
3190Sstevel@tonic-gate {
3200Sstevel@tonic-gate 	fputs(gettext("usage: install [-cs] [-g group] [-m mode] [-o owner] file ...  destination\n"), stderr);
3210Sstevel@tonic-gate 	fputs(gettext("       install  -d   [-g group] [-m mode] [-o owner] dir\n"), stderr);
3220Sstevel@tonic-gate 	exit(1);
3230Sstevel@tonic-gate }
3240Sstevel@tonic-gate 
3250Sstevel@tonic-gate /*
3260Sstevel@tonic-gate  * mkdirp --
3270Sstevel@tonic-gate  *	make a directory and parents if needed
3280Sstevel@tonic-gate  */
3290Sstevel@tonic-gate int
mkdirp(dir,mode)3300Sstevel@tonic-gate mkdirp(dir, mode)
3310Sstevel@tonic-gate 	char *dir;
3320Sstevel@tonic-gate 	int mode;
3330Sstevel@tonic-gate {
3340Sstevel@tonic-gate 	int err;
3350Sstevel@tonic-gate 	char *slash;
3360Sstevel@tonic-gate 	char *strrchr();
3370Sstevel@tonic-gate 	extern int errno;
3380Sstevel@tonic-gate 
3390Sstevel@tonic-gate 	if (mkdir(dir, mode) == 0)
3400Sstevel@tonic-gate 		return (0);
3410Sstevel@tonic-gate 	if (errno != ENOENT)
3420Sstevel@tonic-gate 		return (-1);
3430Sstevel@tonic-gate 	slash = strrchr(dir, '/');
3440Sstevel@tonic-gate 	if (slash == NULL)
3450Sstevel@tonic-gate 		return (-1);
3460Sstevel@tonic-gate 	*slash = '\0';
3470Sstevel@tonic-gate 	err = mkdirp(dir, 0777);
3480Sstevel@tonic-gate 	*slash = '/';
3490Sstevel@tonic-gate 	if (err)
3500Sstevel@tonic-gate 		return (err);
3510Sstevel@tonic-gate 	return mkdir(dir, mode);
3520Sstevel@tonic-gate }
353