xref: /freebsd/usr.sbin/chown/chown.c (revision 390e8cc2974df1888369c06339ef8e0e92b312b6)
1 /*
2  * Copyright (c) 1988, 1993, 1994
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #if 0
35 #ifndef lint
36 static const char copyright[] =
37 "@(#) Copyright (c) 1988, 1993, 1994\n\
38 	The Regents of the University of California.  All rights reserved.\n";
39 #endif /* not lint */
40 
41 #ifndef lint
42 static char sccsid[] = "@(#)chown.c	8.8 (Berkeley) 4/4/94";
43 #endif /* not lint */
44 #endif
45 #include <sys/cdefs.h>
46 __FBSDID("$FreeBSD$");
47 
48 #include <sys/param.h>
49 #include <sys/stat.h>
50 
51 #include <err.h>
52 #include <errno.h>
53 #include <fts.h>
54 #include <grp.h>
55 #include <libgen.h>
56 #include <pwd.h>
57 #include <stdint.h>
58 #include <stdio.h>
59 #include <stdlib.h>
60 #include <string.h>
61 #include <unistd.h>
62 
63 void	a_gid(const char *);
64 void	a_uid(const char *);
65 void	chownerr(const char *);
66 u_long	id(const char *, const char *);
67 void	usage(void);
68 
69 uid_t uid;
70 gid_t gid;
71 int ischown;
72 const char *gname;
73 
74 int
75 main(int argc, char **argv)
76 {
77 	FTS *ftsp;
78 	FTSENT *p;
79 	int Hflag, Lflag, Rflag, fflag, hflag, vflag;
80 	int ch, fts_options, rval;
81 	char *cp;
82 
83 	ischown = (strcmp(basename(argv[0]), "chown") == 0);
84 
85 	Hflag = Lflag = Rflag = fflag = hflag = vflag = 0;
86 	while ((ch = getopt(argc, argv, "HLPRfhv")) != -1)
87 		switch (ch) {
88 		case 'H':
89 			Hflag = 1;
90 			Lflag = 0;
91 			break;
92 		case 'L':
93 			Lflag = 1;
94 			Hflag = 0;
95 			break;
96 		case 'P':
97 			Hflag = Lflag = 0;
98 			break;
99 		case 'R':
100 			Rflag = 1;
101 			break;
102 		case 'f':
103 			fflag = 1;
104 			break;
105 		case 'h':
106 			hflag = 1;
107 			break;
108 		case 'v':
109 			vflag++;
110 			break;
111 		case '?':
112 		default:
113 			usage();
114 		}
115 	argv += optind;
116 	argc -= optind;
117 
118 	if (argc < 2)
119 		usage();
120 
121 	if (Rflag) {
122 		fts_options = FTS_PHYSICAL;
123 		if (hflag && (Hflag || Lflag))
124 			errx(1, "the -R%c and -h options may not be "
125 			    "specified together", Hflag ? 'H' : 'L');
126 		if (Hflag)
127 			fts_options |= FTS_COMFOLLOW;
128 		else if (Lflag) {
129 			fts_options &= ~FTS_PHYSICAL;
130 			fts_options |= FTS_LOGICAL;
131 		}
132 	} else
133 		fts_options = hflag ? FTS_PHYSICAL : FTS_LOGICAL;
134 
135 	uid = (uid_t)-1;
136 	gid = (gid_t)-1;
137 	if (ischown) {
138 		if ((cp = strchr(*argv, ':')) != NULL) {
139 			*cp++ = '\0';
140 			a_gid(cp);
141 		}
142 #ifdef SUPPORT_DOT
143 		else if ((cp = strchr(*argv, '.')) != NULL) {
144 			warnx("separation of user and group with a period is deprecated");
145 			*cp++ = '\0';
146 			a_gid(cp);
147 		}
148 #endif
149 		a_uid(*argv);
150 	} else
151 		a_gid(*argv);
152 
153 	if ((ftsp = fts_open(++argv, fts_options, 0)) == NULL)
154 		err(1, NULL);
155 
156 	for (rval = 0; (p = fts_read(ftsp)) != NULL;) {
157 		switch (p->fts_info) {
158 		case FTS_D:			/* Change it at FTS_DP. */
159 			if (!Rflag)
160 				fts_set(ftsp, p, FTS_SKIP);
161 			continue;
162 		case FTS_DNR:			/* Warn, chown. */
163 			warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
164 			rval = 1;
165 			break;
166 		case FTS_ERR:			/* Warn, continue. */
167 		case FTS_NS:
168 			warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
169 			rval = 1;
170 			continue;
171 		case FTS_SL:
172 		case FTS_SLNONE:
173 			/*
174 			 * The only symlinks that end up here are ones that
175 			 * don't point to anything and ones that we found
176 			 * doing a physical walk.
177 			 */
178 			if (hflag)
179 				break;
180 			else
181 				continue;
182 		default:
183 			break;
184 		}
185 		if ((uid == (uid_t)-1 || uid == p->fts_statp->st_uid) &&
186 		    (gid == (gid_t)-1 || gid == p->fts_statp->st_gid))
187 			continue;
188 		if ((hflag ? lchown : chown)(p->fts_accpath, uid, gid) == -1) {
189 			if (!fflag) {
190 				chownerr(p->fts_path);
191 				rval = 1;
192 			}
193 		} else {
194 			if (vflag) {
195 				printf("%s", p->fts_path);
196 				if (vflag > 1) {
197 					if (ischown) {
198 						printf(": %ju:%ju -> %ju:%ju",
199 						    (uintmax_t)
200 						    p->fts_statp->st_uid,
201 						    (uintmax_t)
202 						    p->fts_statp->st_gid,
203 						    (uid == (uid_t)-1) ?
204 						    (uintmax_t)
205 						    p->fts_statp->st_uid :
206 						    (uintmax_t)uid,
207 						    (gid == (gid_t)-1) ?
208 						    (uintmax_t)
209 						    p->fts_statp->st_gid :
210 						    (uintmax_t)gid);
211 					} else {
212 						printf(": %ju -> %ju",
213 						    (uintmax_t)
214 						    p->fts_statp->st_gid,
215 						    (gid == (gid_t)-1) ?
216 						    (uintmax_t)
217 						    p->fts_statp->st_gid :
218 						    (uintmax_t)gid);
219 					}
220 				}
221 				printf("\n");
222 			}
223 		}
224 	}
225 	if (errno)
226 		err(1, "fts_read");
227 	exit(rval);
228 }
229 
230 void
231 a_gid(const char *s)
232 {
233 	struct group *gr;
234 
235 	if (*s == '\0')			/* Argument was "uid[:.]". */
236 		return;
237 	gname = s;
238 	gid = ((gr = getgrnam(s)) != NULL) ? gr->gr_gid : id(s, "group");
239 }
240 
241 void
242 a_uid(const char *s)
243 {
244 	struct passwd *pw;
245 
246 	if (*s == '\0')			/* Argument was "[:.]gid". */
247 		return;
248 	uid = ((pw = getpwnam(s)) != NULL) ? pw->pw_uid : id(s, "user");
249 }
250 
251 u_long
252 id(const char *name, const char *type)
253 {
254 	u_long val;
255 	char *ep;
256 
257 	/*
258 	 * XXX
259 	 * We know that uid_t's and gid_t's are unsigned longs.
260 	 */
261 	errno = 0;
262 	val = strtoul(name, &ep, 10);
263 	if (errno)
264 		err(1, "%s", name);
265 	if (*ep != '\0')
266 		errx(1, "%s: illegal %s name", name, type);
267 	return (val);
268 }
269 
270 void
271 chownerr(const char *file)
272 {
273 	static uid_t euid = -1;
274 	static int ngroups = -1;
275 	gid_t groups[NGROUPS_MAX];
276 
277 	/* Check for chown without being root. */
278 	if (errno != EPERM || (uid != (uid_t)-1 &&
279 	    euid == (uid_t)-1 && (euid = geteuid()) != 0)) {
280 		warn("%s", file);
281 		return;
282 	}
283 
284 	/* Check group membership; kernel just returns EPERM. */
285 	if (gid != (gid_t)-1 && ngroups == -1 &&
286 	    euid == (uid_t)-1 && (euid = geteuid()) != 0) {
287 		ngroups = getgroups(NGROUPS_MAX, groups);
288 		while (--ngroups >= 0 && gid != groups[ngroups]);
289 		if (ngroups < 0) {
290 			warnx("you are not a member of group %s", gname);
291 			return;
292 		}
293 	}
294 	warn("%s", file);
295 }
296 
297 void
298 usage(void)
299 {
300 
301 	if (ischown)
302 		(void)fprintf(stderr, "%s\n%s\n",
303 		    "usage: chown [-fhv] [-R [-H | -L | -P]] owner[:group]"
304 		    " file ...",
305 		    "       chown [-fhv] [-R [-H | -L | -P]] :group file ...");
306 	else
307 		(void)fprintf(stderr, "%s\n",
308 		    "usage: chgrp [-fhv] [-R [-H | -L | -P]] group file ...");
309 	exit(1);
310 }
311