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