1139969Simp/*-
21556Srgrimes * Copyright (c) 1989, 1993, 1994
31556Srgrimes *	The Regents of the University of California.  All rights reserved.
41556Srgrimes *
51556Srgrimes * Redistribution and use in source and binary forms, with or without
61556Srgrimes * modification, are permitted provided that the following conditions
71556Srgrimes * are met:
81556Srgrimes * 1. Redistributions of source code must retain the above copyright
91556Srgrimes *    notice, this list of conditions and the following disclaimer.
101556Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111556Srgrimes *    notice, this list of conditions and the following disclaimer in the
121556Srgrimes *    documentation and/or other materials provided with the distribution.
131556Srgrimes * 4. Neither the name of the University nor the names of its contributors
141556Srgrimes *    may be used to endorse or promote products derived from this software
151556Srgrimes *    without specific prior written permission.
161556Srgrimes *
171556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
181556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
191556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
201556Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
211556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
221556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
231556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
241556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
251556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
261556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
271556Srgrimes * SUCH DAMAGE.
281556Srgrimes */
291556Srgrimes
30114433Sobrien#if 0
311556Srgrimes#ifndef lint
3220411Sstevestatic char const copyright[] =
331556Srgrimes"@(#) Copyright (c) 1989, 1993, 1994\n\
341556Srgrimes	The Regents of the University of California.  All rights reserved.\n";
351556Srgrimes#endif /* not lint */
361556Srgrimes
371556Srgrimes#ifndef lint
3836002Scharnierstatic char sccsid[] = "@(#)chmod.c	8.8 (Berkeley) 4/1/94";
39114433Sobrien#endif /* not lint */
4035773Scharnier#endif
4199109Sobrien#include <sys/cdefs.h>
4299109Sobrien__FBSDID("$FreeBSD: stable/10/bin/chmod/chmod.c 306976 2016-10-10 16:07:23Z sevan $");
431556Srgrimes
44196711Strasz#include <sys/param.h>
451556Srgrimes#include <sys/stat.h>
461556Srgrimes
471556Srgrimes#include <err.h>
481556Srgrimes#include <errno.h>
49283875Ssmh#include <fcntl.h>
501556Srgrimes#include <fts.h>
516170Sbde#include <limits.h>
521556Srgrimes#include <stdio.h>
531556Srgrimes#include <stdlib.h>
541556Srgrimes#include <string.h>
551556Srgrimes#include <unistd.h>
561556Srgrimes
57194795Sdelphijstatic void usage(void);
58196711Straszstatic int may_have_nfs4acl(const FTSENT *ent, int hflag);
591556Srgrimes
601556Srgrimesint
6190107Simpmain(int argc, char *argv[])
621556Srgrimes{
631556Srgrimes	FTS *ftsp;
641556Srgrimes	FTSENT *p;
651556Srgrimes	mode_t *set;
66283875Ssmh	int Hflag, Lflag, Rflag, ch, fflag, fts_options, hflag, rval;
6753780Sobrien	int vflag;
68121794Stobez	char *mode;
69121794Stobez	mode_t newmode;
701556Srgrimes
717165Sjoerg	set = NULL;
7291078Smarkm	Hflag = Lflag = Rflag = fflag = hflag = vflag = 0;
7377342Sru	while ((ch = getopt(argc, argv, "HLPRXfghorstuvwx")) != -1)
741556Srgrimes		switch (ch) {
751556Srgrimes		case 'H':
761556Srgrimes			Hflag = 1;
7791078Smarkm			Lflag = 0;
781556Srgrimes			break;
791556Srgrimes		case 'L':
801556Srgrimes			Lflag = 1;
8191078Smarkm			Hflag = 0;
821556Srgrimes			break;
831556Srgrimes		case 'P':
841556Srgrimes			Hflag = Lflag = 0;
851556Srgrimes			break;
861556Srgrimes		case 'R':
871556Srgrimes			Rflag = 1;
881556Srgrimes			break;
8949544Schris		case 'f':
901556Srgrimes			fflag = 1;
911556Srgrimes			break;
921556Srgrimes		case 'h':
931556Srgrimes			/*
94306976Ssevan			 * In System V the -h option causes chmod to change
95306976Ssevan			 * the mode of the symbolic link. 4.4BSD's symbolic
96306976Ssevan			 * links didn't have modes, so it was an undocumented
97306976Ssevan			 * noop.  In FreeBSD 3.0, lchmod(2) is introduced and
98306976Ssevan			 * this option does real work.
991556Srgrimes			 */
1001556Srgrimes			hflag = 1;
1011556Srgrimes			break;
1021556Srgrimes		/*
1031556Srgrimes		 * XXX
1041556Srgrimes		 * "-[rwx]" are valid mode commands.  If they are the entire
1051556Srgrimes		 * argument, getopt has moved past them, so decrement optind.
1061556Srgrimes		 * Regardless, we're done argument processing.
1071556Srgrimes		 */
1081556Srgrimes		case 'g': case 'o': case 'r': case 's':
1091556Srgrimes		case 't': case 'u': case 'w': case 'X': case 'x':
1101556Srgrimes			if (argv[optind - 1][0] == '-' &&
1111556Srgrimes			    argv[optind - 1][1] == ch &&
1121556Srgrimes			    argv[optind - 1][2] == '\0')
1131556Srgrimes				--optind;
1141556Srgrimes			goto done;
11553780Sobrien		case 'v':
116101297Sobrien			vflag++;
11753780Sobrien			break;
1181556Srgrimes		case '?':
1191556Srgrimes		default:
1201556Srgrimes			usage();
1211556Srgrimes		}
1221556Srgrimesdone:	argv += optind;
1231556Srgrimes	argc -= optind;
1241556Srgrimes
1251556Srgrimes	if (argc < 2)
1261556Srgrimes		usage();
1271556Srgrimes
1281556Srgrimes	if (Rflag) {
1291556Srgrimes		if (hflag)
130283875Ssmh			errx(1, "the -R and -h options may not be "
131283875Ssmh			    "specified together.");
1321556Srgrimes		if (Lflag) {
133283875Ssmh			fts_options = FTS_LOGICAL;
134283875Ssmh		} else {
135283875Ssmh			fts_options = FTS_PHYSICAL;
136283875Ssmh
137283875Ssmh			if (Hflag) {
138283875Ssmh				fts_options |= FTS_COMFOLLOW;
139283875Ssmh			}
1401556Srgrimes		}
141283875Ssmh	} else if (hflag) {
142283875Ssmh		fts_options = FTS_PHYSICAL;
143283875Ssmh	} else {
144283875Ssmh		fts_options = FTS_LOGICAL;
145283875Ssmh	}
1461556Srgrimes
1471556Srgrimes	mode = *argv;
148121794Stobez	if ((set = setmode(mode)) == NULL)
149121794Stobez		errx(1, "invalid file mode: %s", mode);
1501556Srgrimes
1511556Srgrimes	if ((ftsp = fts_open(++argv, fts_options, 0)) == NULL)
15299743Sdillon		err(1, "fts_open");
1531556Srgrimes	for (rval = 0; (p = fts_read(ftsp)) != NULL;) {
154283875Ssmh		int atflag;
155283875Ssmh
156283875Ssmh		if ((fts_options & FTS_LOGICAL) ||
157283875Ssmh		    ((fts_options & FTS_COMFOLLOW) &&
158283875Ssmh		    p->fts_level == FTS_ROOTLEVEL))
159283875Ssmh			atflag = 0;
160283875Ssmh		else
161283875Ssmh			atflag = AT_SYMLINK_NOFOLLOW;
162283875Ssmh
1631556Srgrimes		switch (p->fts_info) {
16417496Sadam		case FTS_D:			/* Change it at FTS_DP. */
16517496Sadam			if (!Rflag)
16617496Sadam				fts_set(ftsp, p, FTS_SKIP);
16717496Sadam			continue;
168283875Ssmh		case FTS_DNR:			/* Warn, chmod. */
1691556Srgrimes			warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
1701556Srgrimes			rval = 1;
1711556Srgrimes			break;
1721556Srgrimes		case FTS_ERR:			/* Warn, continue. */
1731556Srgrimes		case FTS_NS:
1741556Srgrimes			warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
1751556Srgrimes			rval = 1;
1761556Srgrimes			continue;
1771556Srgrimes		default:
1781556Srgrimes			break;
1791556Srgrimes		}
180121794Stobez		newmode = getmode(set, p->fts_statp->st_mode);
181195243Strasz		/*
182195243Strasz		 * With NFSv4 ACLs, it is possible that applying a mode
183195243Strasz		 * identical to the one computed from an ACL will change
184195243Strasz		 * that ACL.
185195243Strasz		 */
186196711Strasz		if (may_have_nfs4acl(p, hflag) == 0 &&
187195243Strasz		    (newmode & ALLPERMS) == (p->fts_statp->st_mode & ALLPERMS))
188195243Strasz				continue;
189283875Ssmh		if (fchmodat(AT_FDCWD, p->fts_accpath, newmode, atflag) == -1
190283875Ssmh		    && !fflag) {
191283875Ssmh			warn("%s", p->fts_path);
192283875Ssmh			rval = 1;
193283875Ssmh		} else if (vflag) {
194283875Ssmh			(void)printf("%s", p->fts_path);
195101297Sobrien
196283875Ssmh			if (vflag > 1) {
197283875Ssmh				char m1[12], m2[12];
198101297Sobrien
199283875Ssmh				strmode(p->fts_statp->st_mode, m1);
200283875Ssmh				strmode((p->fts_statp->st_mode &
201283875Ssmh				    S_IFMT) | newmode, m2);
202283875Ssmh				(void)printf(": 0%o [%s] -> 0%o [%s]",
203283875Ssmh				    p->fts_statp->st_mode, m1,
204283875Ssmh				    (p->fts_statp->st_mode & S_IFMT) |
205283875Ssmh				    newmode, m2);
206101297Sobrien			}
207283875Ssmh			(void)printf("\n");
2081556Srgrimes		}
2091556Srgrimes	}
2101556Srgrimes	if (errno)
2111556Srgrimes		err(1, "fts_read");
2121556Srgrimes	exit(rval);
2131556Srgrimes}
2141556Srgrimes
215194795Sdelphijstatic void
21690107Simpusage(void)
2171556Srgrimes{
2181556Srgrimes	(void)fprintf(stderr,
21977342Sru	    "usage: chmod [-fhv] [-R [-H | -L | -P]] mode file ...\n");
2201556Srgrimes	exit(1);
2211556Srgrimes}
222195243Strasz
223195243Straszstatic int
224196711Straszmay_have_nfs4acl(const FTSENT *ent, int hflag)
225195243Strasz{
226195243Strasz	int ret;
227196711Strasz	static dev_t previous_dev = NODEV;
228195243Strasz	static int supports_acls = -1;
229195243Strasz
230195243Strasz	if (previous_dev != ent->fts_statp->st_dev) {
231195243Strasz		previous_dev = ent->fts_statp->st_dev;
232195243Strasz		supports_acls = 0;
233195243Strasz
234196711Strasz		if (hflag)
235196711Strasz			ret = lpathconf(ent->fts_accpath, _PC_ACL_NFS4);
236196711Strasz		else
237196711Strasz			ret = pathconf(ent->fts_accpath, _PC_ACL_NFS4);
238195243Strasz		if (ret > 0)
239195243Strasz			supports_acls = 1;
240195243Strasz		else if (ret < 0 && errno != EINVAL)
241195243Strasz			warn("%s", ent->fts_path);
242195243Strasz	}
243195243Strasz
244195243Strasz	return (supports_acls);
245195243Strasz}
246