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