chown.c revision 35622
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#else
44static const char rcsid[] =
45	"$Id: chown.c,v 1.10 1998/03/09 08:54:31 jkh Exp $";
46#endif
47#endif /* not lint */
48
49#include <sys/param.h>
50#include <sys/stat.h>
51
52#include <ctype.h>
53#include <dirent.h>
54#include <err.h>
55#include <errno.h>
56#include <fts.h>
57#include <grp.h>
58#include <pwd.h>
59#include <stdio.h>
60#include <stdlib.h>
61#include <string.h>
62#include <unistd.h>
63
64void	a_gid __P((char *));
65void	a_uid __P((char *));
66void	chownerr __P((char *));
67u_long	id __P((char *, char *));
68void	usage __P((void));
69
70uid_t uid;
71gid_t gid;
72int Rflag, ischown, fflag, hflag;
73char *gname, *myname;
74
75int
76main(argc, argv)
77	int argc;
78	char *argv[];
79{
80	FTS *ftsp;
81	FTSENT *p;
82	int Hflag, Lflag, Pflag, ch, fts_options, hflag, rval;
83	char *cp;
84
85	myname = (cp = rindex(*argv, '/')) ? cp + 1 : *argv;
86	ischown = myname[2] == 'o';
87
88	Hflag = Lflag = Pflag = hflag = 0;
89	while ((ch = getopt(argc, argv, "HLPRfh")) != -1)
90		switch (ch) {
91		case 'H':
92			Hflag = 1;
93			Lflag = Pflag = 0;
94			break;
95		case 'L':
96			Lflag = 1;
97			Hflag = Pflag = 0;
98			break;
99		case 'P':
100			Pflag = 1;
101			Hflag = Lflag = 0;
102			break;
103		case 'R':
104			Rflag = 1;
105			break;
106		case 'f':
107			fflag = 1;
108			break;
109		case 'h':
110			hflag = 1;
111			break;
112		case '?':
113		default:
114			usage();
115		}
116	argv += optind;
117	argc -= optind;
118
119	if (argc < 2)
120		usage();
121
122	fts_options = FTS_PHYSICAL;
123	if (Rflag) {
124		if (hflag && (Lflag || Hflag))
125			errx(1, "the -R and -h options may not be specified together");
126		if (Hflag)
127			fts_options |= FTS_COMFOLLOW;
128		if (Lflag) {
129			fts_options &= ~FTS_PHYSICAL;
130			fts_options |= FTS_LOGICAL;
131		}
132	}
133
134	uid = gid = -1;
135	if (ischown) {
136		if ((cp = strchr(*argv, ':')) != NULL) {
137			*cp++ = '\0';
138			a_gid(cp);
139		}
140#ifdef SUPPORT_DOT
141		else if ((cp = strchr(*argv, '.')) != NULL) {
142			*cp++ = '\0';
143			a_gid(cp);
144		}
145#endif
146		a_uid(*argv);
147	} else
148		a_gid(*argv);
149
150	if ((ftsp = fts_open(++argv, fts_options, 0)) == NULL)
151		err(1, NULL);
152
153	for (rval = 0; (p = fts_read(ftsp)) != NULL;) {
154		switch (p->fts_info) {
155		case FTS_D: 			/* Change it at FTS_DP. */
156			if (!Rflag)
157				fts_set(ftsp, p, FTS_SKIP);
158			continue;
159		case FTS_DNR:			/* Warn, chown, continue. */
160			warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
161			rval = 1;
162			break;
163		case FTS_ERR:			/* Warn, continue. */
164		case FTS_NS:
165			warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
166			rval = 1;
167			continue;
168		case FTS_SLNONE:
169			/*
170			 * The only symlinks that end up here are ones that
171			 * don't point to anything and ones that we found
172			 * doing a physical walk.
173			 */
174			if (hflag)
175				break;
176			else
177				continue;
178		default:
179			break;
180		}
181		if (hflag) {
182			if (lchown(p->fts_accpath, uid, gid) && !fflag) {
183				chownerr(p->fts_path);
184				rval = 1;
185			}
186		} else {
187			if (chown(p->fts_accpath, uid, gid) && !fflag) {
188				chownerr(p->fts_path);
189				rval = 1;
190			}
191		}
192	}
193	if (errno)
194		err(1, "fts_read");
195	exit(rval);
196}
197
198void
199a_gid(s)
200	char *s;
201{
202	struct group *gr;
203
204	if (*s == '\0')			/* Argument was "uid[:.]". */
205		return;
206	gname = s;
207	gid = ((gr = getgrnam(s)) == NULL) ? id(s, "group") : gr->gr_gid;
208}
209
210void
211a_uid(s)
212	char *s;
213{
214	struct passwd *pw;
215
216	if (*s == '\0')			/* Argument was "[:.]gid". */
217		return;
218	uid = ((pw = getpwnam(s)) == NULL) ? id(s, "user") : pw->pw_uid;
219}
220
221u_long
222id(name, type)
223	char *name, *type;
224{
225	u_long val;
226	char *ep;
227
228	/*
229	 * XXX
230	 * We know that uid_t's and gid_t's are unsigned longs.
231	 */
232	errno = 0;
233	val = strtoul(name, &ep, 10);
234	if (errno)
235		err(1, "%s", name);
236	if (*ep != '\0')
237		errx(1, "%s: illegal %s name", name, type);
238	return (val);
239}
240
241void
242chownerr(file)
243	char *file;
244{
245	static int euid = -1, ngroups = -1;
246	gid_t groups[NGROUPS];
247
248	/* Check for chown without being root. */
249	if (errno != EPERM ||
250	    (uid != -1 && euid == -1 && (euid = geteuid()) != 0))
251		err(1, "%s", file);
252
253	/* Check group membership; kernel just returns EPERM. */
254	if (gid != -1 && ngroups == -1 &&
255	    euid == -1 && (euid = geteuid()) != 0) {
256		ngroups = getgroups(NGROUPS, groups);
257		while (--ngroups >= 0 && gid != groups[ngroups]);
258		if (ngroups < 0)
259			errx(1, "you are not a member of group %s", gname);
260	}
261	warn("%s", file);
262}
263
264void
265usage()
266{
267	(void)fprintf(stderr, "%s\n%s\n%s\n",
268	    "usage: chown [-R [-H | -L | -P]] [-f] [-h] owner[:group] file ...",
269	    "       chown [-R [-H | -L | -P]] [-f] [-h] :group file ...",
270	    "       chgrp [-R [-H | -L | -P]] [-f] [-h] group file ...");
271	exit(1);
272}
273