xref: /onnv-gate/usr/src/ucbcmd/chown/chown.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 2003 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 /*
430Sstevel@tonic-gate  * chown [-fR] uid[.gid] file ...
440Sstevel@tonic-gate  */
450Sstevel@tonic-gate 
460Sstevel@tonic-gate #include <stdio.h>
470Sstevel@tonic-gate #include <ctype.h>
480Sstevel@tonic-gate #include <sys/types.h>
490Sstevel@tonic-gate #include <sys/stat.h>
500Sstevel@tonic-gate #include <pwd.h>
510Sstevel@tonic-gate #include <dirent.h>
520Sstevel@tonic-gate #include <grp.h>
530Sstevel@tonic-gate #include <errno.h>
540Sstevel@tonic-gate 
550Sstevel@tonic-gate struct	passwd *pwd;
560Sstevel@tonic-gate struct	passwd *getpwnam();
570Sstevel@tonic-gate struct	stat stbuf;
580Sstevel@tonic-gate uid_t	uid;
590Sstevel@tonic-gate int	status;
600Sstevel@tonic-gate int	fflag;
610Sstevel@tonic-gate int	rflag;
620Sstevel@tonic-gate 
63*669Schin void fatal(int, char *, char *);
64*669Schin 
65*669Schin int
main(int argc,char * argv[])66*669Schin main(int argc, char *argv[])
670Sstevel@tonic-gate {
68*669Schin 	int c;
690Sstevel@tonic-gate 	gid_t gid;
70*669Schin 	char *cp, *group;
71*669Schin 	char optchar[2];
720Sstevel@tonic-gate 	struct group *grp;
730Sstevel@tonic-gate 	extern char *strchr();
740Sstevel@tonic-gate 
750Sstevel@tonic-gate 	argc--, argv++;
760Sstevel@tonic-gate 	while (argc > 0 && argv[0][0] == '-') {
770Sstevel@tonic-gate 		for (cp = &argv[0][1]; *cp; cp++)
780Sstevel@tonic-gate 
790Sstevel@tonic-gate 		switch (*cp) {
800Sstevel@tonic-gate 
810Sstevel@tonic-gate 		case 'f':
820Sstevel@tonic-gate 			fflag++;
830Sstevel@tonic-gate 			break;
840Sstevel@tonic-gate 
850Sstevel@tonic-gate 		case 'R':
860Sstevel@tonic-gate 			rflag++;
870Sstevel@tonic-gate 			break;
880Sstevel@tonic-gate 
890Sstevel@tonic-gate 		default:
90*669Schin 			optchar[0] = *cp;
91*669Schin 			optchar[1] = '\0';
92*669Schin 			fatal(255, "unknown option: %s", optchar);
930Sstevel@tonic-gate 		}
940Sstevel@tonic-gate 		argv++, argc--;
950Sstevel@tonic-gate 	}
960Sstevel@tonic-gate 	if (argc < 2) {
970Sstevel@tonic-gate 		fprintf(stderr, "usage: chown [-fR] owner[.group] file ...\n");
980Sstevel@tonic-gate 		exit(-1);
990Sstevel@tonic-gate 	}
1000Sstevel@tonic-gate 	gid = -1;
1010Sstevel@tonic-gate 	group = strchr(argv[0], '.');
1020Sstevel@tonic-gate 	if (group != NULL) {
1030Sstevel@tonic-gate 		*group++ = '\0';
1040Sstevel@tonic-gate 		if (!isnumber(group)) {
1050Sstevel@tonic-gate 			if ((grp = getgrnam(group)) == NULL)
1060Sstevel@tonic-gate 				fatal(255, "unknown group: %s", group);
1070Sstevel@tonic-gate 			gid = grp -> gr_gid;
1080Sstevel@tonic-gate 			(void) endgrent();
1090Sstevel@tonic-gate 		} else if (*group != '\0') {
1100Sstevel@tonic-gate 			errno = 0;
1110Sstevel@tonic-gate 			gid = (gid_t)strtol(group, NULL, 10);
1120Sstevel@tonic-gate 			if (errno != 0) {
1130Sstevel@tonic-gate 				if (errno == ERANGE) {
1140Sstevel@tonic-gate 					fatal(2,
1150Sstevel@tonic-gate 					    "group id too large: %s", group);
1160Sstevel@tonic-gate 				} else {
1170Sstevel@tonic-gate 					fatal(2, "group id invalid: %s", group);
1180Sstevel@tonic-gate 				}
1190Sstevel@tonic-gate 			}
1200Sstevel@tonic-gate 		}
1210Sstevel@tonic-gate 	}
1220Sstevel@tonic-gate 	if (!isnumber(argv[0])) {
1230Sstevel@tonic-gate 		if ((pwd = getpwnam(argv[0])) == NULL)
1240Sstevel@tonic-gate 			fatal(255, "unknown user id: %s", argv[0]);
1250Sstevel@tonic-gate 		uid = pwd->pw_uid;
1260Sstevel@tonic-gate 	} else {
1270Sstevel@tonic-gate 		errno = 0;
1280Sstevel@tonic-gate 		uid = (uid_t)strtol(argv[0], NULL, 10);
1290Sstevel@tonic-gate 		if (errno != 0) {
1300Sstevel@tonic-gate 			if (errno == ERANGE) {
1310Sstevel@tonic-gate 				fatal(2, "user id too large: %s", argv[0]);
1320Sstevel@tonic-gate 			} else {
1330Sstevel@tonic-gate 				fatal(2, "user id invalid: %s", argv[0]);
1340Sstevel@tonic-gate 			}
1350Sstevel@tonic-gate 		}
1360Sstevel@tonic-gate 	}
1370Sstevel@tonic-gate 	for (c = 1; c < argc; c++) {
1380Sstevel@tonic-gate 		/* do stat for directory arguments */
1390Sstevel@tonic-gate 		if (lstat(argv[c], &stbuf) < 0) {
1400Sstevel@tonic-gate 			status += Perror(argv[c]);
1410Sstevel@tonic-gate 			continue;
1420Sstevel@tonic-gate 		}
1430Sstevel@tonic-gate 		if (rflag && ((stbuf.st_mode&S_IFMT) == S_IFDIR)) {
1440Sstevel@tonic-gate 			status += chownr(argv[c], uid, gid);
1450Sstevel@tonic-gate 			continue;
1460Sstevel@tonic-gate 		}
1470Sstevel@tonic-gate 		if (lchown(argv[c], uid, gid)) {
1480Sstevel@tonic-gate 			status += Perror(argv[c]);
1490Sstevel@tonic-gate 			continue;
1500Sstevel@tonic-gate 		}
1510Sstevel@tonic-gate 	}
152*669Schin 	return (status);
1530Sstevel@tonic-gate }
1540Sstevel@tonic-gate 
155*669Schin int
isnumber(char * s)156*669Schin isnumber(char *s)
1570Sstevel@tonic-gate {
158*669Schin 	int c;
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate 	while (c = *s++)
1610Sstevel@tonic-gate 		if (!isdigit(c))
1620Sstevel@tonic-gate 			return (0);
1630Sstevel@tonic-gate 	return (1);
1640Sstevel@tonic-gate }
1650Sstevel@tonic-gate 
166*669Schin int
chownr(char * dir,uid_t uid,gid_t gid)167*669Schin chownr(char *dir, uid_t uid, gid_t gid)
1680Sstevel@tonic-gate {
169*669Schin 	DIR *dirp;
170*669Schin 	struct dirent *dp;
1710Sstevel@tonic-gate 	struct stat st;
1720Sstevel@tonic-gate 	char savedir[1024];
1730Sstevel@tonic-gate 	int ecode;
1740Sstevel@tonic-gate 
1750Sstevel@tonic-gate 	if (getcwd(savedir, 1024) == NULL)
1760Sstevel@tonic-gate 		fatal(255, "%s", savedir);
1770Sstevel@tonic-gate 	/*
1780Sstevel@tonic-gate 	 * Change what we are given before doing it's contents.
1790Sstevel@tonic-gate 	 */
1800Sstevel@tonic-gate 	if (chown(dir, uid, gid) < 0 && Perror(dir))
1810Sstevel@tonic-gate 		return (1);
1820Sstevel@tonic-gate 	if (chdir(dir) < 0) {
1830Sstevel@tonic-gate 		Perror(dir);
1840Sstevel@tonic-gate 		return (1);
1850Sstevel@tonic-gate 	}
1860Sstevel@tonic-gate 	if ((dirp = opendir(".")) == NULL) {
1870Sstevel@tonic-gate 		Perror(dir);
1880Sstevel@tonic-gate 		return (1);
1890Sstevel@tonic-gate 	}
1900Sstevel@tonic-gate 	dp = readdir(dirp);
1910Sstevel@tonic-gate 	dp = readdir(dirp); /* read "." and ".." */
1920Sstevel@tonic-gate 	ecode = 0;
1930Sstevel@tonic-gate 	for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp)) {
1940Sstevel@tonic-gate 		if (lstat(dp->d_name, &st) < 0) {
1950Sstevel@tonic-gate 			ecode = Perror(dp->d_name);
1960Sstevel@tonic-gate 			if (ecode)
1970Sstevel@tonic-gate 				break;
1980Sstevel@tonic-gate 			continue;
1990Sstevel@tonic-gate 		}
2000Sstevel@tonic-gate 		if ((st.st_mode&S_IFMT) == S_IFDIR) {
2010Sstevel@tonic-gate 			ecode = chownr(dp->d_name, uid, gid);
2020Sstevel@tonic-gate 			if (ecode)
2030Sstevel@tonic-gate 				break;
2040Sstevel@tonic-gate 			continue;
2050Sstevel@tonic-gate 		}
2060Sstevel@tonic-gate 		if (lchown(dp->d_name, uid, gid) < 0 &&
2070Sstevel@tonic-gate 		    (ecode = Perror(dp->d_name)))
2080Sstevel@tonic-gate 			break;
2090Sstevel@tonic-gate 	}
2100Sstevel@tonic-gate 	closedir(dirp);
2110Sstevel@tonic-gate 	if (chdir(savedir) < 0)
2120Sstevel@tonic-gate 		fatal(255, "can't change back to %s", savedir);
2130Sstevel@tonic-gate 	return (ecode);
2140Sstevel@tonic-gate }
2150Sstevel@tonic-gate 
216*669Schin int
error(char * fmt,char * a)217*669Schin error(char *fmt, char *a)
2180Sstevel@tonic-gate {
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate 	if (!fflag) {
2210Sstevel@tonic-gate 		fprintf(stderr, "chown: ");
2220Sstevel@tonic-gate 		fprintf(stderr, fmt, a);
2230Sstevel@tonic-gate 		putc('\n', stderr);
2240Sstevel@tonic-gate 	}
2250Sstevel@tonic-gate 	return (!fflag);
2260Sstevel@tonic-gate }
2270Sstevel@tonic-gate 
228*669Schin void
fatal(int status,char * fmt,char * a)229*669Schin fatal(int status, char *fmt, char *a)
2300Sstevel@tonic-gate {
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate 	fflag = 0;
2330Sstevel@tonic-gate 	(void) error(fmt, a);
2340Sstevel@tonic-gate 	exit(status);
2350Sstevel@tonic-gate }
2360Sstevel@tonic-gate 
237*669Schin int
Perror(char * s)238*669Schin Perror(char *s)
2390Sstevel@tonic-gate {
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate 	if (!fflag) {
2420Sstevel@tonic-gate 		fprintf(stderr, "chown: ");
2430Sstevel@tonic-gate 		perror(s);
2440Sstevel@tonic-gate 	}
2450Sstevel@tonic-gate 	return (!fflag);
2460Sstevel@tonic-gate }
247