xref: /titanic_50/usr/src/ucbcmd/chown/chown.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate /*
31*7c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
32*7c478bd9Sstevel@tonic-gate  * The Regents of the University of California
33*7c478bd9Sstevel@tonic-gate  * All Rights Reserved
34*7c478bd9Sstevel@tonic-gate  *
35*7c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
36*7c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
37*7c478bd9Sstevel@tonic-gate  * contributors.
38*7c478bd9Sstevel@tonic-gate  */
39*7c478bd9Sstevel@tonic-gate 
40*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
41*7c478bd9Sstevel@tonic-gate 
42*7c478bd9Sstevel@tonic-gate /*
43*7c478bd9Sstevel@tonic-gate  * chown [-fR] uid[.gid] file ...
44*7c478bd9Sstevel@tonic-gate  */
45*7c478bd9Sstevel@tonic-gate 
46*7c478bd9Sstevel@tonic-gate #include <stdio.h>
47*7c478bd9Sstevel@tonic-gate #include <ctype.h>
48*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
49*7c478bd9Sstevel@tonic-gate #include <sys/stat.h>
50*7c478bd9Sstevel@tonic-gate #include <pwd.h>
51*7c478bd9Sstevel@tonic-gate #include <dirent.h>
52*7c478bd9Sstevel@tonic-gate #include <grp.h>
53*7c478bd9Sstevel@tonic-gate #include <errno.h>
54*7c478bd9Sstevel@tonic-gate 
55*7c478bd9Sstevel@tonic-gate struct	passwd *pwd;
56*7c478bd9Sstevel@tonic-gate struct	passwd *getpwnam();
57*7c478bd9Sstevel@tonic-gate struct	stat stbuf;
58*7c478bd9Sstevel@tonic-gate uid_t	uid;
59*7c478bd9Sstevel@tonic-gate int	status;
60*7c478bd9Sstevel@tonic-gate int	fflag;
61*7c478bd9Sstevel@tonic-gate int	rflag;
62*7c478bd9Sstevel@tonic-gate 
63*7c478bd9Sstevel@tonic-gate main(argc, argv)
64*7c478bd9Sstevel@tonic-gate 	char *argv[];
65*7c478bd9Sstevel@tonic-gate {
66*7c478bd9Sstevel@tonic-gate 	register int c;
67*7c478bd9Sstevel@tonic-gate 	gid_t gid;
68*7c478bd9Sstevel@tonic-gate 	register char *cp, *group;
69*7c478bd9Sstevel@tonic-gate 	struct group *grp;
70*7c478bd9Sstevel@tonic-gate 	extern char *strchr();
71*7c478bd9Sstevel@tonic-gate 
72*7c478bd9Sstevel@tonic-gate 	argc--, argv++;
73*7c478bd9Sstevel@tonic-gate 	while (argc > 0 && argv[0][0] == '-') {
74*7c478bd9Sstevel@tonic-gate 		for (cp = &argv[0][1]; *cp; cp++)
75*7c478bd9Sstevel@tonic-gate 
76*7c478bd9Sstevel@tonic-gate 		switch (*cp) {
77*7c478bd9Sstevel@tonic-gate 
78*7c478bd9Sstevel@tonic-gate 		case 'f':
79*7c478bd9Sstevel@tonic-gate 			fflag++;
80*7c478bd9Sstevel@tonic-gate 			break;
81*7c478bd9Sstevel@tonic-gate 
82*7c478bd9Sstevel@tonic-gate 		case 'R':
83*7c478bd9Sstevel@tonic-gate 			rflag++;
84*7c478bd9Sstevel@tonic-gate 			break;
85*7c478bd9Sstevel@tonic-gate 
86*7c478bd9Sstevel@tonic-gate 		default:
87*7c478bd9Sstevel@tonic-gate 			fatal(255, "unknown option: %c", *cp);
88*7c478bd9Sstevel@tonic-gate 		}
89*7c478bd9Sstevel@tonic-gate 		argv++, argc--;
90*7c478bd9Sstevel@tonic-gate 	}
91*7c478bd9Sstevel@tonic-gate 	if (argc < 2) {
92*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, "usage: chown [-fR] owner[.group] file ...\n");
93*7c478bd9Sstevel@tonic-gate 		exit(-1);
94*7c478bd9Sstevel@tonic-gate 	}
95*7c478bd9Sstevel@tonic-gate 	gid = -1;
96*7c478bd9Sstevel@tonic-gate 	group = strchr(argv[0], '.');
97*7c478bd9Sstevel@tonic-gate 	if (group != NULL) {
98*7c478bd9Sstevel@tonic-gate 		*group++ = '\0';
99*7c478bd9Sstevel@tonic-gate 		if (!isnumber(group)) {
100*7c478bd9Sstevel@tonic-gate 			if ((grp = getgrnam(group)) == NULL)
101*7c478bd9Sstevel@tonic-gate 				fatal(255, "unknown group: %s", group);
102*7c478bd9Sstevel@tonic-gate 			gid = grp -> gr_gid;
103*7c478bd9Sstevel@tonic-gate 			(void) endgrent();
104*7c478bd9Sstevel@tonic-gate 		} else if (*group != '\0') {
105*7c478bd9Sstevel@tonic-gate 			errno = 0;
106*7c478bd9Sstevel@tonic-gate 			gid = (gid_t)strtol(group, NULL, 10);
107*7c478bd9Sstevel@tonic-gate 			if (errno != 0) {
108*7c478bd9Sstevel@tonic-gate 				if (errno == ERANGE) {
109*7c478bd9Sstevel@tonic-gate 					fatal(2,
110*7c478bd9Sstevel@tonic-gate 					    "group id too large: %s", group);
111*7c478bd9Sstevel@tonic-gate 				} else {
112*7c478bd9Sstevel@tonic-gate 					fatal(2, "group id invalid: %s", group);
113*7c478bd9Sstevel@tonic-gate 				}
114*7c478bd9Sstevel@tonic-gate 			}
115*7c478bd9Sstevel@tonic-gate 		}
116*7c478bd9Sstevel@tonic-gate 	}
117*7c478bd9Sstevel@tonic-gate 	if (!isnumber(argv[0])) {
118*7c478bd9Sstevel@tonic-gate 		if ((pwd = getpwnam(argv[0])) == NULL)
119*7c478bd9Sstevel@tonic-gate 			fatal(255, "unknown user id: %s", argv[0]);
120*7c478bd9Sstevel@tonic-gate 		uid = pwd->pw_uid;
121*7c478bd9Sstevel@tonic-gate 	} else {
122*7c478bd9Sstevel@tonic-gate 		errno = 0;
123*7c478bd9Sstevel@tonic-gate 		uid = (uid_t)strtol(argv[0], NULL, 10);
124*7c478bd9Sstevel@tonic-gate 		if (errno != 0) {
125*7c478bd9Sstevel@tonic-gate 			if (errno == ERANGE) {
126*7c478bd9Sstevel@tonic-gate 				fatal(2, "user id too large: %s", argv[0]);
127*7c478bd9Sstevel@tonic-gate 			} else {
128*7c478bd9Sstevel@tonic-gate 				fatal(2, "user id invalid: %s", argv[0]);
129*7c478bd9Sstevel@tonic-gate 			}
130*7c478bd9Sstevel@tonic-gate 		}
131*7c478bd9Sstevel@tonic-gate 	}
132*7c478bd9Sstevel@tonic-gate 	for (c = 1; c < argc; c++) {
133*7c478bd9Sstevel@tonic-gate 		/* do stat for directory arguments */
134*7c478bd9Sstevel@tonic-gate 		if (lstat(argv[c], &stbuf) < 0) {
135*7c478bd9Sstevel@tonic-gate 			status += Perror(argv[c]);
136*7c478bd9Sstevel@tonic-gate 			continue;
137*7c478bd9Sstevel@tonic-gate 		}
138*7c478bd9Sstevel@tonic-gate 		if (rflag && ((stbuf.st_mode&S_IFMT) == S_IFDIR)) {
139*7c478bd9Sstevel@tonic-gate 			status += chownr(argv[c], uid, gid);
140*7c478bd9Sstevel@tonic-gate 			continue;
141*7c478bd9Sstevel@tonic-gate 		}
142*7c478bd9Sstevel@tonic-gate 		if (lchown(argv[c], uid, gid)) {
143*7c478bd9Sstevel@tonic-gate 			status += Perror(argv[c]);
144*7c478bd9Sstevel@tonic-gate 			continue;
145*7c478bd9Sstevel@tonic-gate 		}
146*7c478bd9Sstevel@tonic-gate 	}
147*7c478bd9Sstevel@tonic-gate 	exit(status);
148*7c478bd9Sstevel@tonic-gate }
149*7c478bd9Sstevel@tonic-gate 
150*7c478bd9Sstevel@tonic-gate isnumber(s)
151*7c478bd9Sstevel@tonic-gate 	char *s;
152*7c478bd9Sstevel@tonic-gate {
153*7c478bd9Sstevel@tonic-gate 	register c;
154*7c478bd9Sstevel@tonic-gate 
155*7c478bd9Sstevel@tonic-gate 	while (c = *s++)
156*7c478bd9Sstevel@tonic-gate 		if (!isdigit(c))
157*7c478bd9Sstevel@tonic-gate 			return (0);
158*7c478bd9Sstevel@tonic-gate 	return (1);
159*7c478bd9Sstevel@tonic-gate }
160*7c478bd9Sstevel@tonic-gate 
161*7c478bd9Sstevel@tonic-gate chownr(dir, uid, gid)
162*7c478bd9Sstevel@tonic-gate 	char *dir;
163*7c478bd9Sstevel@tonic-gate 	uid_t uid;
164*7c478bd9Sstevel@tonic-gate 	gid_t gid;
165*7c478bd9Sstevel@tonic-gate {
166*7c478bd9Sstevel@tonic-gate 	register DIR *dirp;
167*7c478bd9Sstevel@tonic-gate 	register struct dirent *dp;
168*7c478bd9Sstevel@tonic-gate 	struct stat st;
169*7c478bd9Sstevel@tonic-gate 	char savedir[1024];
170*7c478bd9Sstevel@tonic-gate 	int ecode;
171*7c478bd9Sstevel@tonic-gate 
172*7c478bd9Sstevel@tonic-gate 	if (getcwd(savedir, 1024) == NULL)
173*7c478bd9Sstevel@tonic-gate 		fatal(255, "%s", savedir);
174*7c478bd9Sstevel@tonic-gate 	/*
175*7c478bd9Sstevel@tonic-gate 	 * Change what we are given before doing it's contents.
176*7c478bd9Sstevel@tonic-gate 	 */
177*7c478bd9Sstevel@tonic-gate 	if (chown(dir, uid, gid) < 0 && Perror(dir))
178*7c478bd9Sstevel@tonic-gate 		return (1);
179*7c478bd9Sstevel@tonic-gate 	if (chdir(dir) < 0) {
180*7c478bd9Sstevel@tonic-gate 		Perror(dir);
181*7c478bd9Sstevel@tonic-gate 		return (1);
182*7c478bd9Sstevel@tonic-gate 	}
183*7c478bd9Sstevel@tonic-gate 	if ((dirp = opendir(".")) == NULL) {
184*7c478bd9Sstevel@tonic-gate 		Perror(dir);
185*7c478bd9Sstevel@tonic-gate 		return (1);
186*7c478bd9Sstevel@tonic-gate 	}
187*7c478bd9Sstevel@tonic-gate 	dp = readdir(dirp);
188*7c478bd9Sstevel@tonic-gate 	dp = readdir(dirp); /* read "." and ".." */
189*7c478bd9Sstevel@tonic-gate 	ecode = 0;
190*7c478bd9Sstevel@tonic-gate 	for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp)) {
191*7c478bd9Sstevel@tonic-gate 		if (lstat(dp->d_name, &st) < 0) {
192*7c478bd9Sstevel@tonic-gate 			ecode = Perror(dp->d_name);
193*7c478bd9Sstevel@tonic-gate 			if (ecode)
194*7c478bd9Sstevel@tonic-gate 				break;
195*7c478bd9Sstevel@tonic-gate 			continue;
196*7c478bd9Sstevel@tonic-gate 		}
197*7c478bd9Sstevel@tonic-gate 		if ((st.st_mode&S_IFMT) == S_IFDIR) {
198*7c478bd9Sstevel@tonic-gate 			ecode = chownr(dp->d_name, uid, gid);
199*7c478bd9Sstevel@tonic-gate 			if (ecode)
200*7c478bd9Sstevel@tonic-gate 				break;
201*7c478bd9Sstevel@tonic-gate 			continue;
202*7c478bd9Sstevel@tonic-gate 		}
203*7c478bd9Sstevel@tonic-gate 		if (lchown(dp->d_name, uid, gid) < 0 &&
204*7c478bd9Sstevel@tonic-gate 		    (ecode = Perror(dp->d_name)))
205*7c478bd9Sstevel@tonic-gate 			break;
206*7c478bd9Sstevel@tonic-gate 	}
207*7c478bd9Sstevel@tonic-gate 	closedir(dirp);
208*7c478bd9Sstevel@tonic-gate 	if (chdir(savedir) < 0)
209*7c478bd9Sstevel@tonic-gate 		fatal(255, "can't change back to %s", savedir);
210*7c478bd9Sstevel@tonic-gate 	return (ecode);
211*7c478bd9Sstevel@tonic-gate }
212*7c478bd9Sstevel@tonic-gate 
213*7c478bd9Sstevel@tonic-gate error(fmt, a)
214*7c478bd9Sstevel@tonic-gate 	char *fmt, *a;
215*7c478bd9Sstevel@tonic-gate {
216*7c478bd9Sstevel@tonic-gate 
217*7c478bd9Sstevel@tonic-gate 	if (!fflag) {
218*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, "chown: ");
219*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, fmt, a);
220*7c478bd9Sstevel@tonic-gate 		putc('\n', stderr);
221*7c478bd9Sstevel@tonic-gate 	}
222*7c478bd9Sstevel@tonic-gate 	return (!fflag);
223*7c478bd9Sstevel@tonic-gate }
224*7c478bd9Sstevel@tonic-gate 
225*7c478bd9Sstevel@tonic-gate fatal(status, fmt, a)
226*7c478bd9Sstevel@tonic-gate 	int status;
227*7c478bd9Sstevel@tonic-gate 	char *fmt, *a;
228*7c478bd9Sstevel@tonic-gate {
229*7c478bd9Sstevel@tonic-gate 
230*7c478bd9Sstevel@tonic-gate 	fflag = 0;
231*7c478bd9Sstevel@tonic-gate 	(void) error(fmt, a);
232*7c478bd9Sstevel@tonic-gate 	exit(status);
233*7c478bd9Sstevel@tonic-gate }
234*7c478bd9Sstevel@tonic-gate 
235*7c478bd9Sstevel@tonic-gate Perror(s)
236*7c478bd9Sstevel@tonic-gate 	char *s;
237*7c478bd9Sstevel@tonic-gate {
238*7c478bd9Sstevel@tonic-gate 
239*7c478bd9Sstevel@tonic-gate 	if (!fflag) {
240*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, "chown: ");
241*7c478bd9Sstevel@tonic-gate 		perror(s);
242*7c478bd9Sstevel@tonic-gate 	}
243*7c478bd9Sstevel@tonic-gate 	return (!fflag);
244*7c478bd9Sstevel@tonic-gate }
245