xref: /freebsd/usr.sbin/chown/chown.c (revision 817420dc8eac7df799c78f5309b75092b7f7cd40)
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
35 static 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
42 static char sccsid[] = "@(#)chown.c	8.8 (Berkeley) 4/4/94";
43 #else
44 static const char rcsid[] =
45   "$FreeBSD$";
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 
64 void	a_gid __P((char *));
65 void	a_uid __P((char *));
66 void	chownerr __P((char *));
67 u_long	id __P((char *, char *));
68 void	usage __P((void));
69 
70 uid_t uid;
71 gid_t gid;
72 int Rflag, ischown, fflag, hflag, vflag;
73 char *gname, *myname;
74 
75 int
76 main(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 = vflag = 0;
89 	while ((ch = getopt(argc, argv, "HLPRfhv")) != -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 'v':
113 			vflag = 1;
114 			break;
115 		case '?':
116 		default:
117 			usage();
118 		}
119 	argv += optind;
120 	argc -= optind;
121 
122 	if (argc < 2)
123 		usage();
124 
125 	fts_options = FTS_PHYSICAL;
126 	if (Rflag) {
127 		if (hflag && (Lflag || Hflag))
128 			errx(1, "the -R and -h options may not be specified together");
129 		if (Hflag)
130 			fts_options |= FTS_COMFOLLOW;
131 		if (Lflag) {
132 			fts_options &= ~FTS_PHYSICAL;
133 			fts_options |= FTS_LOGICAL;
134 		}
135 	}
136 
137 	uid = gid = -1;
138 	if (ischown) {
139 		if ((cp = strchr(*argv, ':')) != NULL) {
140 			*cp++ = '\0';
141 			a_gid(cp);
142 		}
143 #ifdef SUPPORT_DOT
144 		else if ((cp = strchr(*argv, '.')) != NULL) {
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, continue. */
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:			/* Ignore. */
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 == -1 || uid == p->fts_statp->st_uid) &&
186 		    (gid == -1 || gid == p->fts_statp->st_gid))
187 			continue;
188 		if (hflag) {
189 			if (lchown(p->fts_accpath, uid, gid) && !fflag) {
190 				chownerr(p->fts_path);
191 				rval = 1;
192 			} else {
193 			    	if (vflag)
194 					(void)printf("%s\n", p->fts_accpath);
195 			}
196 		} else {
197 			if (chown(p->fts_accpath, uid, gid) && !fflag) {
198 				chownerr(p->fts_path);
199 				rval = 1;
200 			} else {
201 			    	if (vflag)
202 					(void)printf("%s\n", p->fts_accpath);
203 			}
204 		}
205 	}
206 	if (errno)
207 		err(1, "fts_read");
208 	exit(rval);
209 }
210 
211 void
212 a_gid(s)
213 	char *s;
214 {
215 	struct group *gr;
216 
217 	if (*s == '\0')			/* Argument was "uid[:.]". */
218 		return;
219 	gname = s;
220 	gid = ((gr = getgrnam(s)) == NULL) ? id(s, "group") : gr->gr_gid;
221 }
222 
223 void
224 a_uid(s)
225 	char *s;
226 {
227 	struct passwd *pw;
228 
229 	if (*s == '\0')			/* Argument was "[:.]gid". */
230 		return;
231 	uid = ((pw = getpwnam(s)) == NULL) ? id(s, "user") : pw->pw_uid;
232 }
233 
234 u_long
235 id(name, type)
236 	char *name, *type;
237 {
238 	u_long val;
239 	char *ep;
240 
241 	/*
242 	 * XXX
243 	 * We know that uid_t's and gid_t's are unsigned longs.
244 	 */
245 	errno = 0;
246 	val = strtoul(name, &ep, 10);
247 	if (errno)
248 		err(1, "%s", name);
249 	if (*ep != '\0')
250 		errx(1, "%s: illegal %s name", name, type);
251 	return (val);
252 }
253 
254 void
255 chownerr(file)
256 	char *file;
257 {
258 	static int euid = -1, ngroups = -1;
259 	gid_t groups[NGROUPS];
260 
261 	/* Check for chown without being root. */
262 	if (errno != EPERM ||
263 	    (uid != -1 && euid == -1 && (euid = geteuid()) != 0))
264 		err(1, "%s", file);
265 
266 	/* Check group membership; kernel just returns EPERM. */
267 	if (gid != -1 && ngroups == -1 &&
268 	    euid == -1 && (euid = geteuid()) != 0) {
269 		ngroups = getgroups(NGROUPS, groups);
270 		while (--ngroups >= 0 && gid != groups[ngroups]);
271 		if (ngroups < 0)
272 			errx(1, "you are not a member of group %s", gname);
273 	}
274 	warn("%s", file);
275 }
276 
277 void
278 usage()
279 {
280 	(void)fprintf(stderr, "%s\n%s\n%s\n",
281 	    "usage: chown [-R [-H | -L | -P]] [-f] [-h] [-v] owner[:group] file ...",
282 	    "       chown [-R [-H | -L | -P]] [-f] [-h] [-v] :group file ...",
283 	    "       chgrp [-R [-H | -L | -P]] [-f] [-h] [-v] group file ...");
284 	exit(1);
285 }
286