xref: /freebsd/bin/chmod/chmod.c (revision 5dae51da3da0cc94d17bd67b308fad304ebec7e0)
1 /*-
2  * Copyright (c) 1989, 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  * 4. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #if 0
31 #ifndef lint
32 static char const copyright[] =
33 "@(#) Copyright (c) 1989, 1993, 1994\n\
34 	The Regents of the University of California.  All rights reserved.\n";
35 #endif /* not lint */
36 
37 #ifndef lint
38 static char sccsid[] = "@(#)chmod.c	8.8 (Berkeley) 4/1/94";
39 #endif /* not lint */
40 #endif
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
43 
44 #include <sys/param.h>
45 #include <sys/stat.h>
46 
47 #include <err.h>
48 #include <errno.h>
49 #include <fcntl.h>
50 #include <fts.h>
51 #include <limits.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <unistd.h>
56 
57 static void usage(void);
58 static int may_have_nfs4acl(const FTSENT *ent, int hflag);
59 
60 int
61 main(int argc, char *argv[])
62 {
63 	FTS *ftsp;
64 	FTSENT *p;
65 	mode_t *set;
66 	int Hflag, Lflag, Rflag, ch, fflag, fts_options, hflag, rval;
67 	int vflag;
68 	char *mode;
69 	mode_t newmode;
70 
71 	set = NULL;
72 	Hflag = Lflag = Rflag = fflag = hflag = vflag = 0;
73 	while ((ch = getopt(argc, argv, "HLPRXfghorstuvwx")) != -1)
74 		switch (ch) {
75 		case 'H':
76 			Hflag = 1;
77 			Lflag = 0;
78 			break;
79 		case 'L':
80 			Lflag = 1;
81 			Hflag = 0;
82 			break;
83 		case 'P':
84 			Hflag = Lflag = 0;
85 			break;
86 		case 'R':
87 			Rflag = 1;
88 			break;
89 		case 'f':
90 			fflag = 1;
91 			break;
92 		case 'h':
93 			/*
94 			 * In System V the -h option causes chmod to change
95 			 * the mode of the symbolic link. 4.4BSD's symbolic
96 			 * links didn't have modes, so it was an undocumented
97 			 * noop.  In FreeBSD 3.0, lchmod(2) is introduced and
98 			 * this option does real work.
99 			 */
100 			hflag = 1;
101 			break;
102 		/*
103 		 * XXX
104 		 * "-[rwx]" are valid mode commands.  If they are the entire
105 		 * argument, getopt has moved past them, so decrement optind.
106 		 * Regardless, we're done argument processing.
107 		 */
108 		case 'g': case 'o': case 'r': case 's':
109 		case 't': case 'u': case 'w': case 'X': case 'x':
110 			if (argv[optind - 1][0] == '-' &&
111 			    argv[optind - 1][1] == ch &&
112 			    argv[optind - 1][2] == '\0')
113 				--optind;
114 			goto done;
115 		case 'v':
116 			vflag++;
117 			break;
118 		case '?':
119 		default:
120 			usage();
121 		}
122 done:	argv += optind;
123 	argc -= optind;
124 
125 	if (argc < 2)
126 		usage();
127 
128 	if (Rflag) {
129 		if (hflag)
130 			errx(1, "the -R and -h options may not be "
131 			    "specified together.");
132 		if (Lflag) {
133 			fts_options = FTS_LOGICAL;
134 		} else {
135 			fts_options = FTS_PHYSICAL;
136 
137 			if (Hflag) {
138 				fts_options |= FTS_COMFOLLOW;
139 			}
140 		}
141 	} else if (hflag) {
142 		fts_options = FTS_PHYSICAL;
143 	} else {
144 		fts_options = FTS_LOGICAL;
145 	}
146 
147 	mode = *argv;
148 	if ((set = setmode(mode)) == NULL)
149 		errx(1, "invalid file mode: %s", mode);
150 
151 	if ((ftsp = fts_open(++argv, fts_options, 0)) == NULL)
152 		err(1, "fts_open");
153 	for (rval = 0; (p = fts_read(ftsp)) != NULL;) {
154 		int atflag;
155 
156 		if ((fts_options & FTS_LOGICAL) ||
157 		    ((fts_options & FTS_COMFOLLOW) &&
158 		    p->fts_level == FTS_ROOTLEVEL))
159 			atflag = 0;
160 		else
161 			atflag = AT_SYMLINK_NOFOLLOW;
162 
163 		switch (p->fts_info) {
164 		case FTS_D:
165 			if (!Rflag)
166 				fts_set(ftsp, p, FTS_SKIP);
167 			break;
168 		case FTS_DNR:			/* Warn, chmod. */
169 			warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
170 			rval = 1;
171 			break;
172 		case FTS_DP:			/* Already changed at FTS_D. */
173 			continue;
174 		case FTS_ERR:			/* Warn, continue. */
175 		case FTS_NS:
176 			warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
177 			rval = 1;
178 			continue;
179 		default:
180 			break;
181 		}
182 		newmode = getmode(set, p->fts_statp->st_mode);
183 		/*
184 		 * With NFSv4 ACLs, it is possible that applying a mode
185 		 * identical to the one computed from an ACL will change
186 		 * that ACL.
187 		 */
188 		if (may_have_nfs4acl(p, hflag) == 0 &&
189 		    (newmode & ALLPERMS) == (p->fts_statp->st_mode & ALLPERMS))
190 				continue;
191 		if (fchmodat(AT_FDCWD, p->fts_accpath, newmode, atflag) == -1
192 		    && !fflag) {
193 			warn("%s", p->fts_path);
194 			rval = 1;
195 		} else if (vflag) {
196 			(void)printf("%s", p->fts_path);
197 
198 			if (vflag > 1) {
199 				char m1[12], m2[12];
200 
201 				strmode(p->fts_statp->st_mode, m1);
202 				strmode((p->fts_statp->st_mode &
203 				    S_IFMT) | newmode, m2);
204 				(void)printf(": 0%o [%s] -> 0%o [%s]",
205 				    p->fts_statp->st_mode, m1,
206 				    (p->fts_statp->st_mode & S_IFMT) |
207 				    newmode, m2);
208 			}
209 			(void)printf("\n");
210 		}
211 	}
212 	if (errno)
213 		err(1, "fts_read");
214 	exit(rval);
215 }
216 
217 static void
218 usage(void)
219 {
220 	(void)fprintf(stderr,
221 	    "usage: chmod [-fhv] [-R [-H | -L | -P]] mode file ...\n");
222 	exit(1);
223 }
224 
225 static int
226 may_have_nfs4acl(const FTSENT *ent, int hflag)
227 {
228 	int ret;
229 	static dev_t previous_dev = NODEV;
230 	static int supports_acls = -1;
231 
232 	if (previous_dev != ent->fts_statp->st_dev) {
233 		previous_dev = ent->fts_statp->st_dev;
234 		supports_acls = 0;
235 
236 		if (hflag)
237 			ret = lpathconf(ent->fts_accpath, _PC_ACL_NFS4);
238 		else
239 			ret = pathconf(ent->fts_accpath, _PC_ACL_NFS4);
240 		if (ret > 0)
241 			supports_acls = 1;
242 		else if (ret < 0 && errno != EINVAL)
243 			warn("%s", ent->fts_path);
244 	}
245 
246 	return (supports_acls);
247 }
248