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