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