xref: /freebsd-src/bin/chmod/chmod.c (revision 0b8224d1cc9dc6c9778ba04a75b2c8d47e5d7481)
19ddb49cbSWarner Losh /*-
28a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni  *
44b88c807SRodney W. Grimes  * Copyright (c) 1989, 1993, 1994
54b88c807SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
64b88c807SRodney W. Grimes  *
74b88c807SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
84b88c807SRodney W. Grimes  * modification, are permitted provided that the following conditions
94b88c807SRodney W. Grimes  * are met:
104b88c807SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
114b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
124b88c807SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
134b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
144b88c807SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
15fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
164b88c807SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
174b88c807SRodney W. Grimes  *    without specific prior written permission.
184b88c807SRodney W. Grimes  *
194b88c807SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
204b88c807SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
214b88c807SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
224b88c807SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
234b88c807SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
244b88c807SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
254b88c807SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
264b88c807SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
274b88c807SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
284b88c807SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
294b88c807SRodney W. Grimes  * SUCH DAMAGE.
304b88c807SRodney W. Grimes  */
314b88c807SRodney W. Grimes 
32c5cb1e51SEdward Tomasz Napierala #include <sys/param.h>
334b88c807SRodney W. Grimes #include <sys/stat.h>
344b88c807SRodney W. Grimes 
354b88c807SRodney W. Grimes #include <err.h>
364b88c807SRodney W. Grimes #include <errno.h>
37ad34caceSSteven Hartland #include <fcntl.h>
384b88c807SRodney W. Grimes #include <fts.h>
3950c1f897SBruce Evans #include <limits.h>
40a758566cSConrad Meyer #include <signal.h>
414b88c807SRodney W. Grimes #include <stdio.h>
424b88c807SRodney W. Grimes #include <stdlib.h>
434b88c807SRodney W. Grimes #include <string.h>
444b88c807SRodney W. Grimes #include <unistd.h>
454b88c807SRodney W. Grimes 
46a758566cSConrad Meyer static volatile sig_atomic_t siginfo;
47a758566cSConrad Meyer 
48*e9746806SAlfonso Gregory static void usage(void) __dead2;
49c5cb1e51SEdward Tomasz Napierala static int may_have_nfs4acl(const FTSENT *ent, int hflag);
504b88c807SRodney W. Grimes 
51a758566cSConrad Meyer static void
siginfo_handler(int sig __unused)52a758566cSConrad Meyer siginfo_handler(int sig __unused)
53a758566cSConrad Meyer {
54a758566cSConrad Meyer 
55a758566cSConrad Meyer 	siginfo = 1;
56a758566cSConrad Meyer }
57a758566cSConrad Meyer 
584b88c807SRodney W. Grimes int
main(int argc,char * argv[])595dce647cSWarner Losh main(int argc, char *argv[])
604b88c807SRodney W. Grimes {
614b88c807SRodney W. Grimes 	FTS *ftsp;
624b88c807SRodney W. Grimes 	FTSENT *p;
634b88c807SRodney W. Grimes 	mode_t *set;
64ad34caceSSteven Hartland 	int Hflag, Lflag, Rflag, ch, fflag, fts_options, hflag, rval;
655bb83b98SDavid E. O'Brien 	int vflag;
66ad6a34deSAnton Berezin 	char *mode;
67ad6a34deSAnton Berezin 	mode_t newmode;
684b88c807SRodney W. Grimes 
690fd510b7SJoerg Wunsch 	set = NULL;
7071bea4f7SMark Murray 	Hflag = Lflag = Rflag = fflag = hflag = vflag = 0;
71abb1dad1SRuslan Ermilov 	while ((ch = getopt(argc, argv, "HLPRXfghorstuvwx")) != -1)
724b88c807SRodney W. Grimes 		switch (ch) {
734b88c807SRodney W. Grimes 		case 'H':
744b88c807SRodney W. Grimes 			Hflag = 1;
7571bea4f7SMark Murray 			Lflag = 0;
764b88c807SRodney W. Grimes 			break;
774b88c807SRodney W. Grimes 		case 'L':
784b88c807SRodney W. Grimes 			Lflag = 1;
7971bea4f7SMark Murray 			Hflag = 0;
804b88c807SRodney W. Grimes 			break;
814b88c807SRodney W. Grimes 		case 'P':
824b88c807SRodney W. Grimes 			Hflag = Lflag = 0;
834b88c807SRodney W. Grimes 			break;
844b88c807SRodney W. Grimes 		case 'R':
854b88c807SRodney W. Grimes 			Rflag = 1;
864b88c807SRodney W. Grimes 			break;
877007d55cSChris Costello 		case 'f':
884b88c807SRodney W. Grimes 			fflag = 1;
894b88c807SRodney W. Grimes 			break;
904b88c807SRodney W. Grimes 		case 'h':
914b88c807SRodney W. Grimes 			/*
9266d39fc6SSevan Janiyan 			 * In System V the -h option causes chmod to change
9366d39fc6SSevan Janiyan 			 * the mode of the symbolic link. 4.4BSD's symbolic
9466d39fc6SSevan Janiyan 			 * links didn't have modes, so it was an undocumented
9566d39fc6SSevan Janiyan 			 * noop.  In FreeBSD 3.0, lchmod(2) is introduced and
9666d39fc6SSevan Janiyan 			 * this option does real work.
974b88c807SRodney W. Grimes 			 */
984b88c807SRodney W. Grimes 			hflag = 1;
994b88c807SRodney W. Grimes 			break;
1004b88c807SRodney W. Grimes 		/*
1014b88c807SRodney W. Grimes 		 * XXX
1024b88c807SRodney W. Grimes 		 * "-[rwx]" are valid mode commands.  If they are the entire
1034b88c807SRodney W. Grimes 		 * argument, getopt has moved past them, so decrement optind.
1044b88c807SRodney W. Grimes 		 * Regardless, we're done argument processing.
1054b88c807SRodney W. Grimes 		 */
1064b88c807SRodney W. Grimes 		case 'g': case 'o': case 'r': case 's':
1074b88c807SRodney W. Grimes 		case 't': case 'u': case 'w': case 'X': case 'x':
1084b88c807SRodney W. Grimes 			if (argv[optind - 1][0] == '-' &&
1094b88c807SRodney W. Grimes 			    argv[optind - 1][1] == ch &&
1104b88c807SRodney W. Grimes 			    argv[optind - 1][2] == '\0')
1114b88c807SRodney W. Grimes 				--optind;
1124b88c807SRodney W. Grimes 			goto done;
1135bb83b98SDavid E. O'Brien 		case 'v':
11430d90946SDavid E. O'Brien 			vflag++;
1155bb83b98SDavid E. O'Brien 			break;
1164b88c807SRodney W. Grimes 		case '?':
1174b88c807SRodney W. Grimes 		default:
1184b88c807SRodney W. Grimes 			usage();
1194b88c807SRodney W. Grimes 		}
1204b88c807SRodney W. Grimes done:	argv += optind;
1214b88c807SRodney W. Grimes 	argc -= optind;
1224b88c807SRodney W. Grimes 
1234b88c807SRodney W. Grimes 	if (argc < 2)
1244b88c807SRodney W. Grimes 		usage();
1254b88c807SRodney W. Grimes 
126a758566cSConrad Meyer 	(void)signal(SIGINFO, siginfo_handler);
127a758566cSConrad Meyer 
1284b88c807SRodney W. Grimes 	if (Rflag) {
1294b88c807SRodney W. Grimes 		if (hflag)
130ad34caceSSteven Hartland 			errx(1, "the -R and -h options may not be "
131ad34caceSSteven Hartland 			    "specified together.");
1324b88c807SRodney W. Grimes 		if (Lflag) {
133ad34caceSSteven Hartland 			fts_options = FTS_LOGICAL;
134ad34caceSSteven Hartland 		} else {
135ad34caceSSteven Hartland 			fts_options = FTS_PHYSICAL;
136ad34caceSSteven Hartland 
137ad34caceSSteven Hartland 			if (Hflag) {
138ad34caceSSteven Hartland 				fts_options |= FTS_COMFOLLOW;
1394b88c807SRodney W. Grimes 			}
140ad34caceSSteven Hartland 		}
141ad34caceSSteven Hartland 	} else if (hflag) {
142ad34caceSSteven Hartland 		fts_options = FTS_PHYSICAL;
143ad34caceSSteven Hartland 	} else {
144ad34caceSSteven Hartland 		fts_options = FTS_LOGICAL;
145ad34caceSSteven Hartland 	}
1464b88c807SRodney W. Grimes 
1474b88c807SRodney W. Grimes 	mode = *argv;
1484b88c807SRodney W. Grimes 	if ((set = setmode(mode)) == NULL)
1494b88c807SRodney W. Grimes 		errx(1, "invalid file mode: %s", mode);
1504b88c807SRodney W. Grimes 
1514b88c807SRodney W. Grimes 	if ((ftsp = fts_open(++argv, fts_options, 0)) == NULL)
1523951e2abSMatthew Dillon 		err(1, "fts_open");
1532dfa4b66SBryan Drewery 	for (rval = 0; errno = 0, (p = fts_read(ftsp)) != NULL;) {
154ad34caceSSteven Hartland 		int atflag;
155ad34caceSSteven Hartland 
156ad34caceSSteven Hartland 		if ((fts_options & FTS_LOGICAL) ||
157ad34caceSSteven Hartland 		    ((fts_options & FTS_COMFOLLOW) &&
158ad34caceSSteven Hartland 		    p->fts_level == FTS_ROOTLEVEL))
159ad34caceSSteven Hartland 			atflag = 0;
160ad34caceSSteven Hartland 		else
161ad34caceSSteven Hartland 			atflag = AT_SYMLINK_NOFOLLOW;
162ad34caceSSteven Hartland 
1634b88c807SRodney W. Grimes 		switch (p->fts_info) {
164db1a93d4SSergey Kandaurov 		case FTS_D:
165f3ad46f4SAdam David 			if (!Rflag)
1664b88c807SRodney W. Grimes 				fts_set(ftsp, p, FTS_SKIP);
167db1a93d4SSergey Kandaurov 			break;
168ad34caceSSteven Hartland 		case FTS_DNR:			/* Warn, chmod. */
1694b88c807SRodney W. Grimes 			warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
1704b88c807SRodney W. Grimes 			rval = 1;
1714b88c807SRodney W. Grimes 			break;
172db1a93d4SSergey Kandaurov 		case FTS_DP:			/* Already changed at FTS_D. */
173db1a93d4SSergey Kandaurov 			continue;
1744b88c807SRodney W. Grimes 		case FTS_ERR:			/* Warn, continue. */
1754b88c807SRodney W. Grimes 		case FTS_NS:
1764b88c807SRodney W. Grimes 			warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
1774b88c807SRodney W. Grimes 			rval = 1;
1784b88c807SRodney W. Grimes 			continue;
1794b88c807SRodney W. Grimes 		default:
1804b88c807SRodney W. Grimes 			break;
1814b88c807SRodney W. Grimes 		}
182ad6a34deSAnton Berezin 		newmode = getmode(set, p->fts_statp->st_mode);
183deda5987SEdward Tomasz Napierala 		/*
184deda5987SEdward Tomasz Napierala 		 * With NFSv4 ACLs, it is possible that applying a mode
185deda5987SEdward Tomasz Napierala 		 * identical to the one computed from an ACL will change
186deda5987SEdward Tomasz Napierala 		 * that ACL.
187deda5987SEdward Tomasz Napierala 		 */
188c5cb1e51SEdward Tomasz Napierala 		if (may_have_nfs4acl(p, hflag) == 0 &&
189deda5987SEdward Tomasz Napierala 		    (newmode & ALLPERMS) == (p->fts_statp->st_mode & ALLPERMS))
1901510e9e7SPeter Wemm 				continue;
191ad34caceSSteven Hartland 		if (fchmodat(AT_FDCWD, p->fts_accpath, newmode, atflag) == -1
192ad34caceSSteven Hartland 		    && !fflag) {
193daad7b8cSKris Kennaway 			warn("%s", p->fts_path);
1944b88c807SRodney W. Grimes 			rval = 1;
195a758566cSConrad Meyer 		} else if (vflag || siginfo) {
196887cb17fSRuslan Ermilov 			(void)printf("%s", p->fts_path);
19730d90946SDavid E. O'Brien 
198a758566cSConrad Meyer 			if (vflag > 1 || siginfo) {
19930d90946SDavid E. O'Brien 				char m1[12], m2[12];
20030d90946SDavid E. O'Brien 
20130d90946SDavid E. O'Brien 				strmode(p->fts_statp->st_mode, m1);
20230d90946SDavid E. O'Brien 				strmode((p->fts_statp->st_mode &
20330d90946SDavid E. O'Brien 				    S_IFMT) | newmode, m2);
20430d90946SDavid E. O'Brien 				(void)printf(": 0%o [%s] -> 0%o [%s]",
20530d90946SDavid E. O'Brien 				    p->fts_statp->st_mode, m1,
20630d90946SDavid E. O'Brien 				    (p->fts_statp->st_mode & S_IFMT) |
20730d90946SDavid E. O'Brien 				    newmode, m2);
20830d90946SDavid E. O'Brien 			}
20930d90946SDavid E. O'Brien 			(void)printf("\n");
210a758566cSConrad Meyer 			siginfo = 0;
21130d90946SDavid E. O'Brien 		}
2124b88c807SRodney W. Grimes 	}
2134b88c807SRodney W. Grimes 	if (errno)
2144b88c807SRodney W. Grimes 		err(1, "fts_read");
2154b88c807SRodney W. Grimes 	exit(rval);
2164b88c807SRodney W. Grimes }
2174b88c807SRodney W. Grimes 
218431586a8SXin LI static void
usage(void)2195dce647cSWarner Losh usage(void)
2204b88c807SRodney W. Grimes {
2214b88c807SRodney W. Grimes 	(void)fprintf(stderr,
222abb1dad1SRuslan Ermilov 	    "usage: chmod [-fhv] [-R [-H | -L | -P]] mode file ...\n");
2234b88c807SRodney W. Grimes 	exit(1);
2244b88c807SRodney W. Grimes }
225deda5987SEdward Tomasz Napierala 
226deda5987SEdward Tomasz Napierala static int
may_have_nfs4acl(const FTSENT * ent,int hflag)227c5cb1e51SEdward Tomasz Napierala may_have_nfs4acl(const FTSENT *ent, int hflag)
228deda5987SEdward Tomasz Napierala {
229deda5987SEdward Tomasz Napierala 	int ret;
230c5cb1e51SEdward Tomasz Napierala 	static dev_t previous_dev = NODEV;
231deda5987SEdward Tomasz Napierala 	static int supports_acls = -1;
232deda5987SEdward Tomasz Napierala 
233deda5987SEdward Tomasz Napierala 	if (previous_dev != ent->fts_statp->st_dev) {
234deda5987SEdward Tomasz Napierala 		previous_dev = ent->fts_statp->st_dev;
235deda5987SEdward Tomasz Napierala 		supports_acls = 0;
236deda5987SEdward Tomasz Napierala 
237c5cb1e51SEdward Tomasz Napierala 		if (hflag)
238c5cb1e51SEdward Tomasz Napierala 			ret = lpathconf(ent->fts_accpath, _PC_ACL_NFS4);
239c5cb1e51SEdward Tomasz Napierala 		else
240deda5987SEdward Tomasz Napierala 			ret = pathconf(ent->fts_accpath, _PC_ACL_NFS4);
241deda5987SEdward Tomasz Napierala 		if (ret > 0)
242deda5987SEdward Tomasz Napierala 			supports_acls = 1;
243deda5987SEdward Tomasz Napierala 		else if (ret < 0 && errno != EINVAL)
244deda5987SEdward Tomasz Napierala 			warn("%s", ent->fts_path);
245deda5987SEdward Tomasz Napierala 	}
246deda5987SEdward Tomasz Napierala 
247deda5987SEdward Tomasz Napierala 	return (supports_acls);
248deda5987SEdward Tomasz Napierala }
249