xref: /freebsd/usr.sbin/chown/chown.c (revision 5e3190f700637fcfc1a52daeaa4a031fdd2557c7)
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 #include <sys/param.h>
46 #include <sys/stat.h>
47 
48 #include <err.h>
49 #include <errno.h>
50 #include <fcntl.h>
51 #include <fts.h>
52 #include <grp.h>
53 #include <libgen.h>
54 #include <pwd.h>
55 #include <signal.h>
56 #include <stddef.h>
57 #include <stdint.h>
58 #include <stdio.h>
59 #include <stdlib.h>
60 #include <string.h>
61 #include <unistd.h>
62 
63 static void	a_gid(const char *);
64 static void	a_uid(const char *);
65 static void	chownerr(const char *);
66 static uid_t	id(const char *, const char *);
67 static void	usage(void);
68 static void	print_info(const FTSENT *, int);
69 
70 static uid_t uid;
71 static gid_t gid;
72 static int ischown;
73 static const char *gname;
74 static volatile sig_atomic_t siginfo;
75 
76 static void
77 siginfo_handler(int sig __unused)
78 {
79 
80 	siginfo = 1;
81 }
82 
83 int
84 main(int argc, char **argv)
85 {
86 	FTS *ftsp;
87 	FTSENT *p;
88 	int Hflag, Lflag, Rflag, fflag, hflag, vflag, xflag;
89 	int ch, fts_options, rval;
90 	char *cp;
91 
92 	ischown = (strcmp(basename(argv[0]), "chown") == 0);
93 
94 	Hflag = Lflag = Rflag = fflag = hflag = vflag = xflag = 0;
95 	while ((ch = getopt(argc, argv, "HLPRfhvx")) != -1)
96 		switch (ch) {
97 		case 'H':
98 			Hflag = 1;
99 			Lflag = 0;
100 			break;
101 		case 'L':
102 			Lflag = 1;
103 			Hflag = 0;
104 			break;
105 		case 'P':
106 			Hflag = Lflag = 0;
107 			break;
108 		case 'R':
109 			Rflag = 1;
110 			break;
111 		case 'f':
112 			fflag = 1;
113 			break;
114 		case 'h':
115 			hflag = 1;
116 			break;
117 		case 'v':
118 			vflag++;
119 			break;
120 		case 'x':
121 			xflag = 1;
122 			break;
123 		case '?':
124 		default:
125 			usage();
126 		}
127 	argv += optind;
128 	argc -= optind;
129 
130 	if (argc < 2)
131 		usage();
132 
133 	(void)signal(SIGINFO, siginfo_handler);
134 
135 	if (Rflag) {
136 		if (hflag && (Hflag || Lflag))
137 			errx(1, "the -R%c and -h options may not be "
138 			    "specified together", Hflag ? 'H' : 'L');
139 		if (Lflag) {
140 			fts_options = FTS_LOGICAL;
141 		} else {
142 			fts_options = FTS_PHYSICAL;
143 
144 			if (Hflag) {
145 				fts_options |= FTS_COMFOLLOW;
146 			}
147 		}
148 	} else if (hflag) {
149 		fts_options = FTS_PHYSICAL;
150 	} else {
151 		fts_options = FTS_LOGICAL;
152 	}
153 
154 	if (xflag)
155 		fts_options |= FTS_XDEV;
156 
157 	uid = (uid_t)-1;
158 	gid = (gid_t)-1;
159 	if (ischown) {
160 		if ((cp = strchr(*argv, ':')) != NULL) {
161 			*cp++ = '\0';
162 			a_gid(cp);
163 		}
164 #ifdef SUPPORT_DOT
165 		else if ((cp = strchr(*argv, '.')) != NULL) {
166 			warnx("separation of user and group with a period is deprecated");
167 			*cp++ = '\0';
168 			a_gid(cp);
169 		}
170 #endif
171 		a_uid(*argv);
172 	} else
173 		a_gid(*argv);
174 
175 	if ((ftsp = fts_open(++argv, fts_options, NULL)) == NULL)
176 		err(1, NULL);
177 
178 	for (rval = 0; errno = 0, (p = fts_read(ftsp)) != NULL;) {
179 		int atflag;
180 
181 		if ((fts_options & FTS_LOGICAL) ||
182 		    ((fts_options & FTS_COMFOLLOW) &&
183 		    p->fts_level == FTS_ROOTLEVEL))
184 			atflag = 0;
185 		else
186 			atflag = AT_SYMLINK_NOFOLLOW;
187 
188 		switch (p->fts_info) {
189 		case FTS_D:			/* Change it at FTS_DP. */
190 			if (!Rflag)
191 				fts_set(ftsp, p, FTS_SKIP);
192 			continue;
193 		case FTS_DNR:			/* Warn, chown. */
194 			warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
195 			rval = 1;
196 			break;
197 		case FTS_ERR:			/* Warn, continue. */
198 		case FTS_NS:
199 			warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
200 			rval = 1;
201 			continue;
202 		default:
203 			break;
204 		}
205 		if (siginfo) {
206 			print_info(p, 2);
207 			siginfo = 0;
208 		}
209 		if ((uid == (uid_t)-1 || uid == p->fts_statp->st_uid) &&
210 		    (gid == (gid_t)-1 || gid == p->fts_statp->st_gid))
211 			continue;
212 		if (fchownat(AT_FDCWD, p->fts_accpath, uid, gid, atflag)
213 		    == -1 && !fflag) {
214 			chownerr(p->fts_path);
215 			rval = 1;
216 		} else if (vflag)
217 			print_info(p, vflag);
218 	}
219 	if (errno)
220 		err(1, "fts_read");
221 	exit(rval);
222 }
223 
224 static void
225 a_gid(const char *s)
226 {
227 	struct group *gr;
228 
229 	if (*s == '\0')			/* Argument was "uid[:.]". */
230 		return;
231 	gname = s;
232 	gid = ((gr = getgrnam(s)) != NULL) ? gr->gr_gid : id(s, "group");
233 }
234 
235 static void
236 a_uid(const char *s)
237 {
238 	struct passwd *pw;
239 
240 	if (*s == '\0')			/* Argument was "[:.]gid". */
241 		return;
242 	uid = ((pw = getpwnam(s)) != NULL) ? pw->pw_uid : id(s, "user");
243 }
244 
245 static uid_t
246 id(const char *name, const char *type)
247 {
248 	unsigned long val;
249 	char *ep;
250 
251 	errno = 0;
252 	val = strtoul(name, &ep, 10);
253 	_Static_assert(UID_MAX >= GID_MAX, "UID MAX less than GID MAX");
254 	if (errno || *ep != '\0' || val > UID_MAX)
255 		errx(1, "%s: illegal %s name", name, type);
256 	return (val);
257 }
258 
259 static void
260 chownerr(const char *file)
261 {
262 	static uid_t euid = -1;
263 	static int ngroups = -1;
264 	static long ngroups_max;
265 	gid_t *groups;
266 
267 	/* Check for chown without being root. */
268 	if (errno != EPERM || (uid != (uid_t)-1 &&
269 	    euid == (uid_t)-1 && (euid = geteuid()) != 0)) {
270 		warn("%s", file);
271 		return;
272 	}
273 
274 	/* Check group membership; kernel just returns EPERM. */
275 	if (gid != (gid_t)-1 && ngroups == -1 &&
276 	    euid == (uid_t)-1 && (euid = geteuid()) != 0) {
277 		ngroups_max = sysconf(_SC_NGROUPS_MAX) + 1;
278 		if ((groups = malloc(sizeof(gid_t) * ngroups_max)) == NULL)
279 			err(1, "malloc");
280 		ngroups = getgroups(ngroups_max, groups);
281 		while (--ngroups >= 0 && gid != groups[ngroups]);
282 		free(groups);
283 		if (ngroups < 0) {
284 			warnx("you are not a member of group %s", gname);
285 			return;
286 		}
287 	}
288 	warn("%s", file);
289 }
290 
291 static void
292 usage(void)
293 {
294 
295 	if (ischown)
296 		(void)fprintf(stderr, "%s\n%s\n",
297 		    "usage: chown [-fhvx] [-R [-H | -L | -P]] owner[:group]"
298 		    " file ...",
299 		    "       chown [-fhvx] [-R [-H | -L | -P]] :group file ...");
300 	else
301 		(void)fprintf(stderr, "%s\n",
302 		    "usage: chgrp [-fhvx] [-R [-H | -L | -P]] group file ...");
303 	exit(1);
304 }
305 
306 static void
307 print_info(const FTSENT *p, int vflag)
308 {
309 
310 	printf("%s", p->fts_path);
311 	if (vflag > 1) {
312 		if (ischown) {
313 			printf(": %ju:%ju -> %ju:%ju",
314 			    (uintmax_t)p->fts_statp->st_uid,
315 			    (uintmax_t)p->fts_statp->st_gid,
316 			    (uid == (uid_t)-1) ?
317 			    (uintmax_t)p->fts_statp->st_uid : (uintmax_t)uid,
318 			    (gid == (gid_t)-1) ?
319 			    (uintmax_t)p->fts_statp->st_gid : (uintmax_t)gid);
320 		} else {
321 			printf(": %ju -> %ju", (uintmax_t)p->fts_statp->st_gid,
322 			    (gid == (gid_t)-1) ?
323 			    (uintmax_t)p->fts_statp->st_gid : (uintmax_t)gid);
324 		}
325 	}
326 	printf("\n");
327 }
328