xref: /freebsd/usr.sbin/chown/chown.c (revision 0b8224d1cc9dc6c9778ba04a75b2c8d47e5d7481)
18a16b7a1SPedro F. Giffuni /*-
28a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni  *
4dea673e9SRodney W. Grimes  * Copyright (c) 1988, 1993, 1994
5dea673e9SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
6dea673e9SRodney W. Grimes  *
7dea673e9SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
8dea673e9SRodney W. Grimes  * modification, are permitted provided that the following conditions
9dea673e9SRodney W. Grimes  * are met:
10dea673e9SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
11dea673e9SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
12dea673e9SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
13dea673e9SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
14dea673e9SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
15fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
16dea673e9SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
17dea673e9SRodney W. Grimes  *    without specific prior written permission.
18dea673e9SRodney W. Grimes  *
19dea673e9SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20dea673e9SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21dea673e9SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22dea673e9SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23dea673e9SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24dea673e9SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25dea673e9SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26dea673e9SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27dea673e9SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28dea673e9SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29dea673e9SRodney W. Grimes  * SUCH DAMAGE.
30dea673e9SRodney W. Grimes  */
31dea673e9SRodney W. Grimes 
32dea673e9SRodney W. Grimes #include <sys/param.h>
33dea673e9SRodney W. Grimes #include <sys/stat.h>
34dea673e9SRodney W. Grimes 
35dea673e9SRodney W. Grimes #include <err.h>
36dea673e9SRodney W. Grimes #include <errno.h>
37ad34caceSSteven Hartland #include <fcntl.h>
38dea673e9SRodney W. Grimes #include <fts.h>
39dea673e9SRodney W. Grimes #include <grp.h>
40987d3799SDavid E. O'Brien #include <libgen.h>
41dea673e9SRodney W. Grimes #include <pwd.h>
42bb577bb6SConrad Meyer #include <signal.h>
43d51abd5fSBryan Drewery #include <stddef.h>
44ae29e300SJohan Karlsson #include <stdint.h>
45dea673e9SRodney W. Grimes #include <stdio.h>
46dea673e9SRodney W. Grimes #include <stdlib.h>
47dea673e9SRodney W. Grimes #include <string.h>
48dea673e9SRodney W. Grimes #include <unistd.h>
49dea673e9SRodney W. Grimes 
50d35fcc68SEd Schouten static void	a_gid(const char *);
51d35fcc68SEd Schouten static void	a_uid(const char *);
52d35fcc68SEd Schouten static void	chownerr(const char *);
53d35fcc68SEd Schouten static uid_t	id(const char *, const char *);
54d35fcc68SEd Schouten static void	usage(void);
55bb577bb6SConrad Meyer static void	print_info(const FTSENT *, int);
56dea673e9SRodney W. Grimes 
57d35fcc68SEd Schouten static uid_t uid;
58d35fcc68SEd Schouten static gid_t gid;
59d35fcc68SEd Schouten static int ischown;
60d35fcc68SEd Schouten static const char *gname;
61bb577bb6SConrad Meyer static volatile sig_atomic_t siginfo;
62bb577bb6SConrad Meyer 
63bb577bb6SConrad Meyer static void
siginfo_handler(int sig __unused)64bb577bb6SConrad Meyer siginfo_handler(int sig __unused)
65bb577bb6SConrad Meyer {
66bb577bb6SConrad Meyer 
67bb577bb6SConrad Meyer 	siginfo = 1;
68bb577bb6SConrad Meyer }
69dea673e9SRodney W. Grimes 
70dea673e9SRodney W. Grimes int
main(int argc,char ** argv)717aed2b41SJuli Mallett main(int argc, char **argv)
72dea673e9SRodney W. Grimes {
73dea673e9SRodney W. Grimes 	FTS *ftsp;
74dea673e9SRodney W. Grimes 	FTSENT *p;
751901b124SGavin Atkinson 	int Hflag, Lflag, Rflag, fflag, hflag, vflag, xflag;
768f9dacc2SRuslan Ermilov 	int ch, fts_options, rval;
77dea673e9SRodney W. Grimes 	char *cp;
78dea673e9SRodney W. Grimes 
79987d3799SDavid E. O'Brien 	ischown = (strcmp(basename(argv[0]), "chown") == 0);
80dea673e9SRodney W. Grimes 
811901b124SGavin Atkinson 	Hflag = Lflag = Rflag = fflag = hflag = vflag = xflag = 0;
821901b124SGavin Atkinson 	while ((ch = getopt(argc, argv, "HLPRfhvx")) != -1)
83dea673e9SRodney W. Grimes 		switch (ch) {
84dea673e9SRodney W. Grimes 		case 'H':
85dea673e9SRodney W. Grimes 			Hflag = 1;
868f9dacc2SRuslan Ermilov 			Lflag = 0;
87dea673e9SRodney W. Grimes 			break;
88dea673e9SRodney W. Grimes 		case 'L':
89dea673e9SRodney W. Grimes 			Lflag = 1;
908f9dacc2SRuslan Ermilov 			Hflag = 0;
91dea673e9SRodney W. Grimes 			break;
92dea673e9SRodney W. Grimes 		case 'P':
93dea673e9SRodney W. Grimes 			Hflag = Lflag = 0;
94dea673e9SRodney W. Grimes 			break;
95dea673e9SRodney W. Grimes 		case 'R':
96dea673e9SRodney W. Grimes 			Rflag = 1;
97dea673e9SRodney W. Grimes 			break;
98dea673e9SRodney W. Grimes 		case 'f':
99dea673e9SRodney W. Grimes 			fflag = 1;
100dea673e9SRodney W. Grimes 			break;
101dea673e9SRodney W. Grimes 		case 'h':
102dea673e9SRodney W. Grimes 			hflag = 1;
103dea673e9SRodney W. Grimes 			break;
1040be12df0SDavid E. O'Brien 		case 'v':
105ae29e300SJohan Karlsson 			vflag++;
1060be12df0SDavid E. O'Brien 			break;
1071901b124SGavin Atkinson 		case 'x':
1081901b124SGavin Atkinson 			xflag = 1;
1091901b124SGavin Atkinson 			break;
110dea673e9SRodney W. Grimes 		case '?':
111dea673e9SRodney W. Grimes 		default:
112dea673e9SRodney W. Grimes 			usage();
113dea673e9SRodney W. Grimes 		}
114dea673e9SRodney W. Grimes 	argv += optind;
115dea673e9SRodney W. Grimes 	argc -= optind;
116dea673e9SRodney W. Grimes 
117dea673e9SRodney W. Grimes 	if (argc < 2)
118dea673e9SRodney W. Grimes 		usage();
119dea673e9SRodney W. Grimes 
120bb577bb6SConrad Meyer 	(void)signal(SIGINFO, siginfo_handler);
121bb577bb6SConrad Meyer 
122dea673e9SRodney W. Grimes 	if (Rflag) {
1238f9dacc2SRuslan Ermilov 		if (hflag && (Hflag || Lflag))
1248f9dacc2SRuslan Ermilov 			errx(1, "the -R%c and -h options may not be "
1258f9dacc2SRuslan Ermilov 			    "specified together", Hflag ? 'H' : 'L');
126ad34caceSSteven Hartland 		if (Lflag) {
127ad34caceSSteven Hartland 			fts_options = FTS_LOGICAL;
128ad34caceSSteven Hartland 		} else {
129ad34caceSSteven Hartland 			fts_options = FTS_PHYSICAL;
130ad34caceSSteven Hartland 
131ad34caceSSteven Hartland 			if (Hflag) {
132dea673e9SRodney W. Grimes 				fts_options |= FTS_COMFOLLOW;
133dea673e9SRodney W. Grimes 			}
134ad34caceSSteven Hartland 		}
135ad34caceSSteven Hartland 	} else if (hflag) {
136ad34caceSSteven Hartland 		fts_options = FTS_PHYSICAL;
137ad34caceSSteven Hartland 	} else {
138ad34caceSSteven Hartland 		fts_options = FTS_LOGICAL;
139ad34caceSSteven Hartland 	}
140ad34caceSSteven Hartland 
1411901b124SGavin Atkinson 	if (xflag)
1421901b124SGavin Atkinson 		fts_options |= FTS_XDEV;
143dea673e9SRodney W. Grimes 
1448f9dacc2SRuslan Ermilov 	uid = (uid_t)-1;
1458f9dacc2SRuslan Ermilov 	gid = (gid_t)-1;
146dea673e9SRodney W. Grimes 	if (ischown) {
147dea673e9SRodney W. Grimes 		if ((cp = strchr(*argv, ':')) != NULL) {
148dea673e9SRodney W. Grimes 			*cp++ = '\0';
149dea673e9SRodney W. Grimes 			a_gid(cp);
150dea673e9SRodney W. Grimes 		}
151d95fc683SWolfram Schneider #ifdef SUPPORT_DOT
152d95fc683SWolfram Schneider 		else if ((cp = strchr(*argv, '.')) != NULL) {
153999feccfSDavid Malone 			warnx("separation of user and group with a period is deprecated");
154d95fc683SWolfram Schneider 			*cp++ = '\0';
155d95fc683SWolfram Schneider 			a_gid(cp);
156d95fc683SWolfram Schneider 		}
157d95fc683SWolfram Schneider #endif
158dea673e9SRodney W. Grimes 		a_uid(*argv);
159dea673e9SRodney W. Grimes 	} else
160dea673e9SRodney W. Grimes 		a_gid(*argv);
161dea673e9SRodney W. Grimes 
162cb4fe258SEd Maste 	if ((ftsp = fts_open(++argv, fts_options, NULL)) == NULL)
163dea673e9SRodney W. Grimes 		err(1, NULL);
164dea673e9SRodney W. Grimes 
165*2dfa4b66SBryan Drewery 	for (rval = 0; errno = 0, (p = fts_read(ftsp)) != NULL;) {
166ad34caceSSteven Hartland 		int atflag;
167ad34caceSSteven Hartland 
168ad34caceSSteven Hartland 		if ((fts_options & FTS_LOGICAL) ||
169ad34caceSSteven Hartland 		    ((fts_options & FTS_COMFOLLOW) &&
170ad34caceSSteven Hartland 		    p->fts_level == FTS_ROOTLEVEL))
171ad34caceSSteven Hartland 			atflag = 0;
172ad34caceSSteven Hartland 		else
173ad34caceSSteven Hartland 			atflag = AT_SYMLINK_NOFOLLOW;
174ad34caceSSteven Hartland 
175dea673e9SRodney W. Grimes 		switch (p->fts_info) {
1767ba57172SAdam David 		case FTS_D:			/* Change it at FTS_DP. */
1777ba57172SAdam David 			if (!Rflag)
178dea673e9SRodney W. Grimes 				fts_set(ftsp, p, FTS_SKIP);
1797ba57172SAdam David 			continue;
1808f9dacc2SRuslan Ermilov 		case FTS_DNR:			/* Warn, chown. */
181dea673e9SRodney W. Grimes 			warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
182dea673e9SRodney W. Grimes 			rval = 1;
183dea673e9SRodney W. Grimes 			break;
184dea673e9SRodney W. Grimes 		case FTS_ERR:			/* Warn, continue. */
185dea673e9SRodney W. Grimes 		case FTS_NS:
186dea673e9SRodney W. Grimes 			warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
187dea673e9SRodney W. Grimes 			rval = 1;
188dea673e9SRodney W. Grimes 			continue;
189dea673e9SRodney W. Grimes 		default:
190dea673e9SRodney W. Grimes 			break;
191dea673e9SRodney W. Grimes 		}
192bb577bb6SConrad Meyer 		if (siginfo) {
193bb577bb6SConrad Meyer 			print_info(p, 2);
194bb577bb6SConrad Meyer 			siginfo = 0;
195bb577bb6SConrad Meyer 		}
1968f9dacc2SRuslan Ermilov 		if ((uid == (uid_t)-1 || uid == p->fts_statp->st_uid) &&
1978f9dacc2SRuslan Ermilov 		    (gid == (gid_t)-1 || gid == p->fts_statp->st_gid))
198cc8660c7SPeter Wemm 			continue;
199ad34caceSSteven Hartland 		if (fchownat(AT_FDCWD, p->fts_accpath, uid, gid, atflag)
200ad34caceSSteven Hartland 		    == -1 && !fflag) {
2012e08522bSPeter Wemm 			chownerr(p->fts_path);
2022e08522bSPeter Wemm 			rval = 1;
203bb577bb6SConrad Meyer 		} else if (vflag)
204bb577bb6SConrad Meyer 			print_info(p, vflag);
205dea673e9SRodney W. Grimes 	}
206dea673e9SRodney W. Grimes 	if (errno)
207dea673e9SRodney W. Grimes 		err(1, "fts_read");
208dea673e9SRodney W. Grimes 	exit(rval);
209dea673e9SRodney W. Grimes }
210dea673e9SRodney W. Grimes 
211d35fcc68SEd Schouten static void
a_gid(const char * s)2127aed2b41SJuli Mallett a_gid(const char *s)
213dea673e9SRodney W. Grimes {
214dea673e9SRodney W. Grimes 	struct group *gr;
215dea673e9SRodney W. Grimes 
216dea673e9SRodney W. Grimes 	if (*s == '\0')			/* Argument was "uid[:.]". */
217dea673e9SRodney W. Grimes 		return;
218dea673e9SRodney W. Grimes 	gname = s;
2198f9dacc2SRuslan Ermilov 	gid = ((gr = getgrnam(s)) != NULL) ? gr->gr_gid : id(s, "group");
220dea673e9SRodney W. Grimes }
221dea673e9SRodney W. Grimes 
222d35fcc68SEd Schouten static void
a_uid(const char * s)2237aed2b41SJuli Mallett a_uid(const char *s)
224dea673e9SRodney W. Grimes {
225dea673e9SRodney W. Grimes 	struct passwd *pw;
226dea673e9SRodney W. Grimes 
227dea673e9SRodney W. Grimes 	if (*s == '\0')			/* Argument was "[:.]gid". */
228dea673e9SRodney W. Grimes 		return;
2298f9dacc2SRuslan Ermilov 	uid = ((pw = getpwnam(s)) != NULL) ? pw->pw_uid : id(s, "user");
230dea673e9SRodney W. Grimes }
231dea673e9SRodney W. Grimes 
232d35fcc68SEd Schouten static uid_t
id(const char * name,const char * type)2337aed2b41SJuli Mallett id(const char *name, const char *type)
234dea673e9SRodney W. Grimes {
235d51abd5fSBryan Drewery 	unsigned long val;
236dea673e9SRodney W. Grimes 	char *ep;
237dea673e9SRodney W. Grimes 
238dea673e9SRodney W. Grimes 	errno = 0;
239dea673e9SRodney W. Grimes 	val = strtoul(name, &ep, 10);
240d51abd5fSBryan Drewery 	_Static_assert(UID_MAX >= GID_MAX, "UID MAX less than GID MAX");
241d51abd5fSBryan Drewery 	if (errno || *ep != '\0' || val > UID_MAX)
242dea673e9SRodney W. Grimes 		errx(1, "%s: illegal %s name", name, type);
243dea673e9SRodney W. Grimes 	return (val);
244dea673e9SRodney W. Grimes }
245dea673e9SRodney W. Grimes 
246d35fcc68SEd Schouten static void
chownerr(const char * file)2477aed2b41SJuli Mallett chownerr(const char *file)
248dea673e9SRodney W. Grimes {
2498f9dacc2SRuslan Ermilov 	static uid_t euid = -1;
2508f9dacc2SRuslan Ermilov 	static int ngroups = -1;
25154404cfbSBrooks Davis 	static long ngroups_max;
25254404cfbSBrooks Davis 	gid_t *groups;
253dea673e9SRodney W. Grimes 
254dea673e9SRodney W. Grimes 	/* Check for chown without being root. */
255dcafd7c9STim J. Robbins 	if (errno != EPERM || (uid != (uid_t)-1 &&
256dcafd7c9STim J. Robbins 	    euid == (uid_t)-1 && (euid = geteuid()) != 0)) {
257dcafd7c9STim J. Robbins 		warn("%s", file);
258dcafd7c9STim J. Robbins 		return;
259dcafd7c9STim J. Robbins 	}
260dea673e9SRodney W. Grimes 
261dea673e9SRodney W. Grimes 	/* Check group membership; kernel just returns EPERM. */
2628f9dacc2SRuslan Ermilov 	if (gid != (gid_t)-1 && ngroups == -1 &&
2638f9dacc2SRuslan Ermilov 	    euid == (uid_t)-1 && (euid = geteuid()) != 0) {
26454404cfbSBrooks Davis 		ngroups_max = sysconf(_SC_NGROUPS_MAX) + 1;
26554404cfbSBrooks Davis 		if ((groups = malloc(sizeof(gid_t) * ngroups_max)) == NULL)
26654404cfbSBrooks Davis 			err(1, "malloc");
26754404cfbSBrooks Davis 		ngroups = getgroups(ngroups_max, groups);
268dea673e9SRodney W. Grimes 		while (--ngroups >= 0 && gid != groups[ngroups]);
2693e6ce93eSEdward Tomasz Napierala 		free(groups);
270dcafd7c9STim J. Robbins 		if (ngroups < 0) {
271dcafd7c9STim J. Robbins 			warnx("you are not a member of group %s", gname);
272dcafd7c9STim J. Robbins 			return;
273dcafd7c9STim J. Robbins 		}
274dea673e9SRodney W. Grimes 	}
275dea673e9SRodney W. Grimes 	warn("%s", file);
276dea673e9SRodney W. Grimes }
277dea673e9SRodney W. Grimes 
278d35fcc68SEd Schouten static void
usage(void)2797aed2b41SJuli Mallett usage(void)
280dea673e9SRodney W. Grimes {
2818f9dacc2SRuslan Ermilov 
2828f9dacc2SRuslan Ermilov 	if (ischown)
2838f9dacc2SRuslan Ermilov 		(void)fprintf(stderr, "%s\n%s\n",
2841901b124SGavin Atkinson 		    "usage: chown [-fhvx] [-R [-H | -L | -P]] owner[:group]"
2858f9dacc2SRuslan Ermilov 		    " file ...",
2861901b124SGavin Atkinson 		    "       chown [-fhvx] [-R [-H | -L | -P]] :group file ...");
2878f9dacc2SRuslan Ermilov 	else
2888f9dacc2SRuslan Ermilov 		(void)fprintf(stderr, "%s\n",
2891901b124SGavin Atkinson 		    "usage: chgrp [-fhvx] [-R [-H | -L | -P]] group file ...");
290dea673e9SRodney W. Grimes 	exit(1);
291dea673e9SRodney W. Grimes }
292bb577bb6SConrad Meyer 
293bb577bb6SConrad Meyer static void
print_info(const FTSENT * p,int vflag)294bb577bb6SConrad Meyer print_info(const FTSENT *p, int vflag)
295bb577bb6SConrad Meyer {
296bb577bb6SConrad Meyer 
297bb577bb6SConrad Meyer 	printf("%s", p->fts_path);
298bb577bb6SConrad Meyer 	if (vflag > 1) {
299bb577bb6SConrad Meyer 		if (ischown) {
300bb577bb6SConrad Meyer 			printf(": %ju:%ju -> %ju:%ju",
301bb577bb6SConrad Meyer 			    (uintmax_t)p->fts_statp->st_uid,
302bb577bb6SConrad Meyer 			    (uintmax_t)p->fts_statp->st_gid,
303bb577bb6SConrad Meyer 			    (uid == (uid_t)-1) ?
304bb577bb6SConrad Meyer 			    (uintmax_t)p->fts_statp->st_uid : (uintmax_t)uid,
305bb577bb6SConrad Meyer 			    (gid == (gid_t)-1) ?
306bb577bb6SConrad Meyer 			    (uintmax_t)p->fts_statp->st_gid : (uintmax_t)gid);
307bb577bb6SConrad Meyer 		} else {
308bb577bb6SConrad Meyer 			printf(": %ju -> %ju", (uintmax_t)p->fts_statp->st_gid,
309bb577bb6SConrad Meyer 			    (gid == (gid_t)-1) ?
310bb577bb6SConrad Meyer 			    (uintmax_t)p->fts_statp->st_gid : (uintmax_t)gid);
311bb577bb6SConrad Meyer 		}
312bb577bb6SConrad Meyer 	}
313bb577bb6SConrad Meyer 	printf("\n");
314bb577bb6SConrad Meyer }
315