xref: /illumos-gate/usr/src/ucbcmd/chown/chown.c (revision edd4c526)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28*edd4c526SToomas Soome /*	  All Rights Reserved	*/
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
327c478bd9Sstevel@tonic-gate  * The Regents of the University of California
337c478bd9Sstevel@tonic-gate  * All Rights Reserved
347c478bd9Sstevel@tonic-gate  *
357c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
367c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
377c478bd9Sstevel@tonic-gate  * contributors.
387c478bd9Sstevel@tonic-gate  */
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate /*
417c478bd9Sstevel@tonic-gate  * chown [-fR] uid[.gid] file ...
427c478bd9Sstevel@tonic-gate  */
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate #include <stdio.h>
457c478bd9Sstevel@tonic-gate #include <ctype.h>
467c478bd9Sstevel@tonic-gate #include <sys/types.h>
477c478bd9Sstevel@tonic-gate #include <sys/stat.h>
487c478bd9Sstevel@tonic-gate #include <pwd.h>
497c478bd9Sstevel@tonic-gate #include <dirent.h>
507c478bd9Sstevel@tonic-gate #include <grp.h>
517c478bd9Sstevel@tonic-gate #include <errno.h>
52*edd4c526SToomas Soome #include <unistd.h>
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate struct	passwd *pwd;
557c478bd9Sstevel@tonic-gate struct	passwd *getpwnam();
567c478bd9Sstevel@tonic-gate struct	stat stbuf;
577c478bd9Sstevel@tonic-gate uid_t	uid;
587c478bd9Sstevel@tonic-gate int	status;
597c478bd9Sstevel@tonic-gate int	fflag;
607c478bd9Sstevel@tonic-gate int	rflag;
617c478bd9Sstevel@tonic-gate 
62cc6c5292Schin void fatal(int, char *, char *);
63cc6c5292Schin 
64cc6c5292Schin int
main(int argc,char * argv[])65cc6c5292Schin main(int argc, char *argv[])
667c478bd9Sstevel@tonic-gate {
67cc6c5292Schin 	int c;
687c478bd9Sstevel@tonic-gate 	gid_t gid;
69cc6c5292Schin 	char *cp, *group;
70cc6c5292Schin 	char optchar[2];
717c478bd9Sstevel@tonic-gate 	struct group *grp;
727c478bd9Sstevel@tonic-gate 	extern char *strchr();
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate 	argc--, argv++;
757c478bd9Sstevel@tonic-gate 	while (argc > 0 && argv[0][0] == '-') {
767c478bd9Sstevel@tonic-gate 		for (cp = &argv[0][1]; *cp; cp++)
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate 		switch (*cp) {
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate 		case 'f':
817c478bd9Sstevel@tonic-gate 			fflag++;
827c478bd9Sstevel@tonic-gate 			break;
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate 		case 'R':
857c478bd9Sstevel@tonic-gate 			rflag++;
867c478bd9Sstevel@tonic-gate 			break;
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate 		default:
89cc6c5292Schin 			optchar[0] = *cp;
90cc6c5292Schin 			optchar[1] = '\0';
91cc6c5292Schin 			fatal(255, "unknown option: %s", optchar);
927c478bd9Sstevel@tonic-gate 		}
937c478bd9Sstevel@tonic-gate 		argv++, argc--;
947c478bd9Sstevel@tonic-gate 	}
957c478bd9Sstevel@tonic-gate 	if (argc < 2) {
967c478bd9Sstevel@tonic-gate 		fprintf(stderr, "usage: chown [-fR] owner[.group] file ...\n");
977c478bd9Sstevel@tonic-gate 		exit(-1);
987c478bd9Sstevel@tonic-gate 	}
997c478bd9Sstevel@tonic-gate 	gid = -1;
1007c478bd9Sstevel@tonic-gate 	group = strchr(argv[0], '.');
1017c478bd9Sstevel@tonic-gate 	if (group != NULL) {
1027c478bd9Sstevel@tonic-gate 		*group++ = '\0';
1037c478bd9Sstevel@tonic-gate 		if (!isnumber(group)) {
1047c478bd9Sstevel@tonic-gate 			if ((grp = getgrnam(group)) == NULL)
1057c478bd9Sstevel@tonic-gate 				fatal(255, "unknown group: %s", group);
1067c478bd9Sstevel@tonic-gate 			gid = grp -> gr_gid;
1077c478bd9Sstevel@tonic-gate 			(void) endgrent();
1087c478bd9Sstevel@tonic-gate 		} else if (*group != '\0') {
1097c478bd9Sstevel@tonic-gate 			errno = 0;
1107c478bd9Sstevel@tonic-gate 			gid = (gid_t)strtol(group, NULL, 10);
1117c478bd9Sstevel@tonic-gate 			if (errno != 0) {
1127c478bd9Sstevel@tonic-gate 				if (errno == ERANGE) {
1137c478bd9Sstevel@tonic-gate 					fatal(2,
1147c478bd9Sstevel@tonic-gate 					    "group id too large: %s", group);
1157c478bd9Sstevel@tonic-gate 				} else {
1167c478bd9Sstevel@tonic-gate 					fatal(2, "group id invalid: %s", group);
1177c478bd9Sstevel@tonic-gate 				}
1187c478bd9Sstevel@tonic-gate 			}
1197c478bd9Sstevel@tonic-gate 		}
1207c478bd9Sstevel@tonic-gate 	}
1217c478bd9Sstevel@tonic-gate 	if (!isnumber(argv[0])) {
1227c478bd9Sstevel@tonic-gate 		if ((pwd = getpwnam(argv[0])) == NULL)
1237c478bd9Sstevel@tonic-gate 			fatal(255, "unknown user id: %s", argv[0]);
1247c478bd9Sstevel@tonic-gate 		uid = pwd->pw_uid;
1257c478bd9Sstevel@tonic-gate 	} else {
1267c478bd9Sstevel@tonic-gate 		errno = 0;
1277c478bd9Sstevel@tonic-gate 		uid = (uid_t)strtol(argv[0], NULL, 10);
1287c478bd9Sstevel@tonic-gate 		if (errno != 0) {
1297c478bd9Sstevel@tonic-gate 			if (errno == ERANGE) {
1307c478bd9Sstevel@tonic-gate 				fatal(2, "user id too large: %s", argv[0]);
1317c478bd9Sstevel@tonic-gate 			} else {
1327c478bd9Sstevel@tonic-gate 				fatal(2, "user id invalid: %s", argv[0]);
1337c478bd9Sstevel@tonic-gate 			}
1347c478bd9Sstevel@tonic-gate 		}
1357c478bd9Sstevel@tonic-gate 	}
1367c478bd9Sstevel@tonic-gate 	for (c = 1; c < argc; c++) {
1377c478bd9Sstevel@tonic-gate 		/* do stat for directory arguments */
1387c478bd9Sstevel@tonic-gate 		if (lstat(argv[c], &stbuf) < 0) {
1397c478bd9Sstevel@tonic-gate 			status += Perror(argv[c]);
1407c478bd9Sstevel@tonic-gate 			continue;
1417c478bd9Sstevel@tonic-gate 		}
1427c478bd9Sstevel@tonic-gate 		if (rflag && ((stbuf.st_mode&S_IFMT) == S_IFDIR)) {
1437c478bd9Sstevel@tonic-gate 			status += chownr(argv[c], uid, gid);
1447c478bd9Sstevel@tonic-gate 			continue;
1457c478bd9Sstevel@tonic-gate 		}
1467c478bd9Sstevel@tonic-gate 		if (lchown(argv[c], uid, gid)) {
1477c478bd9Sstevel@tonic-gate 			status += Perror(argv[c]);
1487c478bd9Sstevel@tonic-gate 			continue;
1497c478bd9Sstevel@tonic-gate 		}
1507c478bd9Sstevel@tonic-gate 	}
151cc6c5292Schin 	return (status);
1527c478bd9Sstevel@tonic-gate }
1537c478bd9Sstevel@tonic-gate 
154cc6c5292Schin int
isnumber(char * s)155cc6c5292Schin isnumber(char *s)
1567c478bd9Sstevel@tonic-gate {
157cc6c5292Schin 	int c;
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate 	while (c = *s++)
1607c478bd9Sstevel@tonic-gate 		if (!isdigit(c))
1617c478bd9Sstevel@tonic-gate 			return (0);
1627c478bd9Sstevel@tonic-gate 	return (1);
1637c478bd9Sstevel@tonic-gate }
1647c478bd9Sstevel@tonic-gate 
165cc6c5292Schin int
chownr(char * dir,uid_t uid,gid_t gid)166cc6c5292Schin chownr(char *dir, uid_t uid, gid_t gid)
1677c478bd9Sstevel@tonic-gate {
168cc6c5292Schin 	DIR *dirp;
169cc6c5292Schin 	struct dirent *dp;
1707c478bd9Sstevel@tonic-gate 	struct stat st;
1717c478bd9Sstevel@tonic-gate 	char savedir[1024];
1727c478bd9Sstevel@tonic-gate 	int ecode;
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate 	if (getcwd(savedir, 1024) == NULL)
1757c478bd9Sstevel@tonic-gate 		fatal(255, "%s", savedir);
1767c478bd9Sstevel@tonic-gate 	/*
1777c478bd9Sstevel@tonic-gate 	 * Change what we are given before doing it's contents.
1787c478bd9Sstevel@tonic-gate 	 */
1797c478bd9Sstevel@tonic-gate 	if (chown(dir, uid, gid) < 0 && Perror(dir))
1807c478bd9Sstevel@tonic-gate 		return (1);
1817c478bd9Sstevel@tonic-gate 	if (chdir(dir) < 0) {
1827c478bd9Sstevel@tonic-gate 		Perror(dir);
1837c478bd9Sstevel@tonic-gate 		return (1);
1847c478bd9Sstevel@tonic-gate 	}
1857c478bd9Sstevel@tonic-gate 	if ((dirp = opendir(".")) == NULL) {
1867c478bd9Sstevel@tonic-gate 		Perror(dir);
1877c478bd9Sstevel@tonic-gate 		return (1);
1887c478bd9Sstevel@tonic-gate 	}
1897c478bd9Sstevel@tonic-gate 	dp = readdir(dirp);
1907c478bd9Sstevel@tonic-gate 	dp = readdir(dirp); /* read "." and ".." */
1917c478bd9Sstevel@tonic-gate 	ecode = 0;
1927c478bd9Sstevel@tonic-gate 	for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp)) {
1937c478bd9Sstevel@tonic-gate 		if (lstat(dp->d_name, &st) < 0) {
1947c478bd9Sstevel@tonic-gate 			ecode = Perror(dp->d_name);
1957c478bd9Sstevel@tonic-gate 			if (ecode)
1967c478bd9Sstevel@tonic-gate 				break;
1977c478bd9Sstevel@tonic-gate 			continue;
1987c478bd9Sstevel@tonic-gate 		}
1997c478bd9Sstevel@tonic-gate 		if ((st.st_mode&S_IFMT) == S_IFDIR) {
2007c478bd9Sstevel@tonic-gate 			ecode = chownr(dp->d_name, uid, gid);
2017c478bd9Sstevel@tonic-gate 			if (ecode)
2027c478bd9Sstevel@tonic-gate 				break;
2037c478bd9Sstevel@tonic-gate 			continue;
2047c478bd9Sstevel@tonic-gate 		}
2057c478bd9Sstevel@tonic-gate 		if (lchown(dp->d_name, uid, gid) < 0 &&
2067c478bd9Sstevel@tonic-gate 		    (ecode = Perror(dp->d_name)))
2077c478bd9Sstevel@tonic-gate 			break;
2087c478bd9Sstevel@tonic-gate 	}
2097c478bd9Sstevel@tonic-gate 	closedir(dirp);
2107c478bd9Sstevel@tonic-gate 	if (chdir(savedir) < 0)
2117c478bd9Sstevel@tonic-gate 		fatal(255, "can't change back to %s", savedir);
2127c478bd9Sstevel@tonic-gate 	return (ecode);
2137c478bd9Sstevel@tonic-gate }
2147c478bd9Sstevel@tonic-gate 
215cc6c5292Schin int
error(char * fmt,char * a)216cc6c5292Schin error(char *fmt, char *a)
2177c478bd9Sstevel@tonic-gate {
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate 	if (!fflag) {
2207c478bd9Sstevel@tonic-gate 		fprintf(stderr, "chown: ");
2217c478bd9Sstevel@tonic-gate 		fprintf(stderr, fmt, a);
2227c478bd9Sstevel@tonic-gate 		putc('\n', stderr);
2237c478bd9Sstevel@tonic-gate 	}
2247c478bd9Sstevel@tonic-gate 	return (!fflag);
2257c478bd9Sstevel@tonic-gate }
2267c478bd9Sstevel@tonic-gate 
227cc6c5292Schin void
fatal(int status,char * fmt,char * a)228cc6c5292Schin fatal(int status, char *fmt, char *a)
2297c478bd9Sstevel@tonic-gate {
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 	fflag = 0;
2327c478bd9Sstevel@tonic-gate 	(void) error(fmt, a);
2337c478bd9Sstevel@tonic-gate 	exit(status);
2347c478bd9Sstevel@tonic-gate }
2357c478bd9Sstevel@tonic-gate 
236cc6c5292Schin int
Perror(char * s)237cc6c5292Schin Perror(char *s)
2387c478bd9Sstevel@tonic-gate {
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate 	if (!fflag) {
2417c478bd9Sstevel@tonic-gate 		fprintf(stderr, "chown: ");
2427c478bd9Sstevel@tonic-gate 		perror(s);
2437c478bd9Sstevel@tonic-gate 	}
2447c478bd9Sstevel@tonic-gate 	return (!fflag);
2457c478bd9Sstevel@tonic-gate }
246