xref: /openbsd-src/bin/chmod/chmod.c (revision a28daedfc357b214be5c701aa8ba8adb29a7f1c2)
1 /*	$OpenBSD: chmod.c,v 1.24 2009/01/21 16:05:39 sobrado Exp $	*/
2 /*	$NetBSD: chmod.c,v 1.12 1995/03/21 09:02:09 cgd Exp $	*/
3 
4 /*
5  * Copyright (c) 1989, 1993, 1994
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #ifndef lint
34 static const char copyright[] =
35 "@(#) Copyright (c) 1989, 1993, 1994\n\
36 	The Regents of the University of California.  All rights reserved.\n";
37 #endif /* not lint */
38 
39 #ifndef lint
40 #if 0
41 static char sccsid[] = "@(#)chmod.c	8.8 (Berkeley) 4/1/94";
42 #else
43 static const char rcsid[] = "$OpenBSD: chmod.c,v 1.24 2009/01/21 16:05:39 sobrado Exp $";
44 #endif
45 #endif /* not lint */
46 
47 #include <sys/param.h>
48 #include <sys/stat.h>
49 
50 #include <err.h>
51 #include <errno.h>
52 #include <fts.h>
53 #include <grp.h>
54 #include <limits.h>
55 #include <locale.h>
56 #include <pwd.h>
57 #include <stdio.h>
58 #include <stdlib.h>
59 #include <string.h>
60 #include <unistd.h>
61 
62 int ischflags, ischown, ischgrp, ischmod;
63 extern char *__progname;
64 
65 gid_t a_gid(const char *);
66 uid_t a_uid(const char *, int);
67 __dead void usage(void);
68 
69 int
70 main(int argc, char *argv[])
71 {
72 	FTS *ftsp;
73 	FTSENT *p;
74 	void *set;
75 	long val;
76 	int oct;
77 	mode_t omode;
78 	int Hflag, Lflag, Rflag, ch, fflag, fts_options, hflag, rval;
79 	uid_t uid;
80 	gid_t gid;
81 	u_int32_t fclear, fset;
82 	char *ep, *mode, *cp, *flags;
83 #ifdef lint
84 	set = NULL;
85 	oct = omode = 0;
86 #endif
87 
88 	setlocale(LC_ALL, "");
89 
90 	ischown = __progname[2] == 'o';
91 	ischgrp = __progname[2] == 'g';
92 	ischmod = __progname[2] == 'm';
93 	ischflags = __progname[2] == 'f';
94 
95 	uid = (uid_t)-1;
96 	gid = (gid_t)-1;
97 	Hflag = Lflag = Rflag = fflag = hflag = 0;
98 	while ((ch = getopt(argc, argv, "HLPRXfghorstuwx")) != -1)
99 		switch (ch) {
100 		case 'H':
101 			Hflag = 1;
102 			Lflag = 0;
103 			break;
104 		case 'L':
105 			Lflag = 1;
106 			Hflag = 0;
107 			break;
108 		case 'P':
109 			Hflag = Lflag = 0;
110 			break;
111 		case 'R':
112 			Rflag = 1;
113 			break;
114 		case 'f':		/* XXX: undocumented. */
115 			fflag = 1;
116 			break;
117 		case 'h':
118 			/*
119 			 * In System V (and probably POSIX.2) the -h option
120 			 * causes chmod to change the mode of the symbolic
121 			 * link.  4.4BSD's symbolic links don't have modes,
122 			 * so it's an undocumented noop.  Do syntax checking,
123 			 * though.
124 			 */
125 			hflag = 1;
126 			break;
127 		/*
128 		 * XXX
129 		 * "-[rwx]" are valid mode commands.  If they are the entire
130 		 * argument, getopt has moved past them, so decrement optind.
131 		 * Regardless, we're done argument processing.
132 		 */
133 		case 'g': case 'o': case 'r': case 's':
134 		case 't': case 'u': case 'w': case 'X': case 'x':
135 			if (!ischmod)
136 				usage();
137 			if (argv[optind - 1][0] == '-' &&
138 			    argv[optind - 1][1] == ch &&
139 			    argv[optind - 1][2] == '\0')
140 				--optind;
141 			goto done;
142 		default:
143 			usage();
144 		}
145 done:
146 	argv += optind;
147 	argc -= optind;
148 
149 	if (argc < 2)
150 		usage();
151 
152 	fts_options = FTS_PHYSICAL;
153 	if (Rflag) {
154 		if (hflag)
155 			errx(1,
156 		"the -R and -h options may not be specified together.");
157 		if (Hflag)
158 			fts_options |= FTS_COMFOLLOW;
159 		if (Lflag) {
160 			fts_options &= ~FTS_PHYSICAL;
161 			fts_options |= FTS_LOGICAL;
162 		}
163 	}
164 
165 	if (ischflags) {
166 		flags = *argv;
167 		if (*flags >= '0' && *flags <= '7') {
168 			errno = 0;
169 			val = strtoul(flags, &ep, 8);
170 			if (val > UINT_MAX)
171 				errno = ERANGE;
172 			if (errno)
173 				err(1, "invalid flags: %s", flags);
174 			if (*ep)
175 				errx(1, "invalid flags: %s", flags);
176 			fset = val;
177 			oct = 1;
178 		} else {
179 			if (strtofflags(&flags, &fset, &fclear))
180 				errx(1, "invalid flag: %s", flags);
181 			fclear = ~fclear;
182 			oct = 0;
183 		}
184 	} else if (ischmod) {
185 		mode = *argv;
186 		if (*mode >= '0' && *mode <= '7') {
187 			errno = 0;
188 			val = strtol(mode, &ep, 8);
189 			if (val > INT_MAX || val < 0)
190 				errno = ERANGE;
191 			if (errno)
192 				err(1, "invalid file mode: %s", mode);
193 			if (*ep)
194 				errx(1, "invalid file mode: %s", mode);
195 			omode = val;
196 			oct = 1;
197 		} else {
198 			if ((set = setmode(mode)) == NULL)
199 				errx(1, "invalid file mode: %s", mode);
200 			oct = 0;
201 		}
202 	} else if (ischown) {
203 		/* Both UID and GID are given. */
204 		if ((cp = strchr(*argv, ':')) != NULL) {
205 			*cp++ = '\0';
206 			gid = a_gid(cp);
207 		}
208 #ifdef SUPPORT_DOT
209 		/* UID and GID are separated by a dot and UID exists. */
210 		else if ((cp = strchr(*argv, '.')) != NULL &&
211 		    (uid = a_uid(*argv, 1)) == (uid_t)-1) {
212 			*cp++ = '\0';
213 			gid = a_gid(cp);
214 		}
215 #endif
216 		if (uid == (uid_t)-1)
217 			uid = a_uid(*argv, 0);
218 	} else
219 		gid = a_gid(*argv);
220 
221 	if ((ftsp = fts_open(++argv, fts_options, 0)) == NULL)
222 		err(1, NULL);
223 	for (rval = 0; (p = fts_read(ftsp)) != NULL;) {
224 		switch (p->fts_info) {
225 		case FTS_D:
226 			if (!Rflag)
227 				fts_set(ftsp, p, FTS_SKIP);
228 			if (ischmod)
229 				break;
230 			else
231 				continue;
232 		case FTS_DNR:			/* Warn, chmod, continue. */
233 			warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
234 			rval = 1;
235 			break;
236 		case FTS_DP:			/* Already changed at FTS_D. */
237 			if (ischmod)
238 				continue;
239 			else
240 				break;
241 		case FTS_ERR:			/* Warn, continue. */
242 		case FTS_NS:
243 			warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
244 			rval = 1;
245 			continue;
246 		case FTS_SL:			/* Ignore. */
247 		case FTS_SLNONE:
248 			/*
249 			 * The only symlinks that end up here are ones that
250 			 * don't point to anything and ones that we found
251 			 * doing a physical walk.
252 			 */
253 			if (ischflags || ischmod || !hflag)
254 				continue;
255 			break;
256 		default:
257 			break;
258 		}
259 		if (ischflags) {
260 			if (oct) {
261 				if (!chflags(p->fts_accpath, fset))
262 					continue;
263 			} else {
264 				p->fts_statp->st_flags |= fset;
265 				p->fts_statp->st_flags &= fclear;
266 				if (!chflags(p->fts_accpath, p->fts_statp->st_flags))
267 					continue;
268 			}
269 			warn("%s", p->fts_path);
270 			rval = 1;
271 		} else if (ischmod && chmod(p->fts_accpath, oct ? omode :
272 		    getmode(set, p->fts_statp->st_mode)) && !fflag) {
273 			warn("%s", p->fts_path);
274 			rval = 1;
275 		} else if (!ischmod && !ischflags &&
276 		    (hflag ? lchown(p->fts_accpath, uid, gid) :
277 		    chown(p->fts_accpath, uid, gid)) && !fflag) {
278 			warn("%s", p->fts_path);
279 			rval = 1;
280 		}
281 	}
282 	if (errno)
283 		err(1, "fts_read");
284 	exit(rval);
285 }
286 
287 /*
288  * Given a UID or user name in a string, return the UID.  If an empty string
289  * was given, returns -1.  If silent is 0, exits on invalid user names/UIDs;
290  * otherwise, returns -1.
291  */
292 uid_t
293 a_uid(const char *s, int silent)
294 {
295 	struct passwd *pw;
296 	const char *errstr;
297 	uid_t uid;
298 
299 	if (*s == '\0')			/* Argument was "[:.]gid". */
300 		return ((uid_t)-1);
301 
302 	/* User name was given. */
303 	if ((pw = getpwnam(s)) != NULL)
304 		return (pw->pw_uid);
305 
306 	/* UID was given. */
307 	uid = (uid_t)strtonum(s, 0, UID_MAX, &errstr);
308 	if (errstr) {
309 		if (silent)
310 			return ((uid_t)-1);
311 		else
312 			errx(1, "user is %s: %s", errstr, s);
313 	}
314 
315 	return (uid);
316 }
317 
318 /*
319  * Given a GID or group name in a string, return the GID.  If an empty string
320  * was given, returns -1.  Exits on invalid user names/UIDs.
321  */
322 gid_t
323 a_gid(const char *s)
324 {
325 	struct group *gr;
326 	const char *errstr;
327 	gid_t gid;
328 
329 	if (*s == '\0')			/* Argument was "uid[:.]". */
330 		return ((gid_t)-1);
331 
332 	/* Group name was given. */
333 	if ((gr = getgrnam(s)) != NULL)
334 		return (gr->gr_gid);
335 
336 	/* GID was given. */
337 	gid = (gid_t)strtonum(s, 0, GID_MAX, &errstr);
338 	if (errstr)
339 		errx(1, "group is %s: %s", errstr, s);
340 
341 	return (gid);
342 }
343 
344 void
345 usage(void)
346 {
347 	if (ischmod || ischflags)
348 		fprintf(stderr,
349 		    "usage: %s [-R [-H | -L | -P]] %s file ...\n",
350 		    __progname, ischmod ? "mode" : "flags");
351 	else
352 		fprintf(stderr,
353 		    "usage: %s [-fh] [-R [-H | -L | -P]] %s file ...\n",
354 		    __progname, ischown ? "owner[:group]" : "group");
355 	if (ischown)
356 		fprintf(stderr,
357 		    "       %s [-fh] [-R [-H | -L | -P]] :group file ...\n",
358 		    __progname);
359 	exit(1);
360 }
361