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: releng/10.2/bin/chmod/chmod.c 283875 2015-06-01 09:04:57Z smh $"); 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 /* 941556Srgrimes * In System V (and probably POSIX.2) the -h option 951556Srgrimes * causes chmod to change the mode of the symbolic 9677342Sru * link. 4.4BSD's symbolic links didn't have modes, 9777342Sru * so it was an undocumented noop. In FreeBSD 3.0, 9877342Sru * lchmod(2) is introduced and this option does real 9977342Sru * work. 1001556Srgrimes */ 1011556Srgrimes hflag = 1; 1021556Srgrimes break; 1031556Srgrimes /* 1041556Srgrimes * XXX 1051556Srgrimes * "-[rwx]" are valid mode commands. If they are the entire 1061556Srgrimes * argument, getopt has moved past them, so decrement optind. 1071556Srgrimes * Regardless, we're done argument processing. 1081556Srgrimes */ 1091556Srgrimes case 'g': case 'o': case 'r': case 's': 1101556Srgrimes case 't': case 'u': case 'w': case 'X': case 'x': 1111556Srgrimes if (argv[optind - 1][0] == '-' && 1121556Srgrimes argv[optind - 1][1] == ch && 1131556Srgrimes argv[optind - 1][2] == '\0') 1141556Srgrimes --optind; 1151556Srgrimes goto done; 11653780Sobrien case 'v': 117101297Sobrien vflag++; 11853780Sobrien break; 1191556Srgrimes case '?': 1201556Srgrimes default: 1211556Srgrimes usage(); 1221556Srgrimes } 1231556Srgrimesdone: argv += optind; 1241556Srgrimes argc -= optind; 1251556Srgrimes 1261556Srgrimes if (argc < 2) 1271556Srgrimes usage(); 1281556Srgrimes 1291556Srgrimes if (Rflag) { 1301556Srgrimes if (hflag) 131283875Ssmh errx(1, "the -R and -h options may not be " 132283875Ssmh "specified together."); 1331556Srgrimes if (Lflag) { 134283875Ssmh fts_options = FTS_LOGICAL; 135283875Ssmh } else { 136283875Ssmh fts_options = FTS_PHYSICAL; 137283875Ssmh 138283875Ssmh if (Hflag) { 139283875Ssmh fts_options |= FTS_COMFOLLOW; 140283875Ssmh } 1411556Srgrimes } 142283875Ssmh } else if (hflag) { 143283875Ssmh fts_options = FTS_PHYSICAL; 144283875Ssmh } else { 145283875Ssmh fts_options = FTS_LOGICAL; 146283875Ssmh } 1471556Srgrimes 1481556Srgrimes mode = *argv; 149121794Stobez if ((set = setmode(mode)) == NULL) 150121794Stobez errx(1, "invalid file mode: %s", mode); 1511556Srgrimes 1521556Srgrimes if ((ftsp = fts_open(++argv, fts_options, 0)) == NULL) 15399743Sdillon err(1, "fts_open"); 1541556Srgrimes for (rval = 0; (p = fts_read(ftsp)) != NULL;) { 155283875Ssmh int atflag; 156283875Ssmh 157283875Ssmh if ((fts_options & FTS_LOGICAL) || 158283875Ssmh ((fts_options & FTS_COMFOLLOW) && 159283875Ssmh p->fts_level == FTS_ROOTLEVEL)) 160283875Ssmh atflag = 0; 161283875Ssmh else 162283875Ssmh atflag = AT_SYMLINK_NOFOLLOW; 163283875Ssmh 1641556Srgrimes switch (p->fts_info) { 16517496Sadam case FTS_D: /* Change it at FTS_DP. */ 16617496Sadam if (!Rflag) 16717496Sadam fts_set(ftsp, p, FTS_SKIP); 16817496Sadam continue; 169283875Ssmh case FTS_DNR: /* Warn, chmod. */ 1701556Srgrimes warnx("%s: %s", p->fts_path, strerror(p->fts_errno)); 1711556Srgrimes rval = 1; 1721556Srgrimes break; 1731556Srgrimes case FTS_ERR: /* Warn, continue. */ 1741556Srgrimes case FTS_NS: 1751556Srgrimes warnx("%s: %s", p->fts_path, strerror(p->fts_errno)); 1761556Srgrimes rval = 1; 1771556Srgrimes continue; 1781556Srgrimes default: 1791556Srgrimes break; 1801556Srgrimes } 181121794Stobez newmode = getmode(set, p->fts_statp->st_mode); 182195243Strasz /* 183195243Strasz * With NFSv4 ACLs, it is possible that applying a mode 184195243Strasz * identical to the one computed from an ACL will change 185195243Strasz * that ACL. 186195243Strasz */ 187196711Strasz if (may_have_nfs4acl(p, hflag) == 0 && 188195243Strasz (newmode & ALLPERMS) == (p->fts_statp->st_mode & ALLPERMS)) 189195243Strasz continue; 190283875Ssmh if (fchmodat(AT_FDCWD, p->fts_accpath, newmode, atflag) == -1 191283875Ssmh && !fflag) { 192283875Ssmh warn("%s", p->fts_path); 193283875Ssmh rval = 1; 194283875Ssmh } else if (vflag) { 195283875Ssmh (void)printf("%s", p->fts_path); 196101297Sobrien 197283875Ssmh if (vflag > 1) { 198283875Ssmh char m1[12], m2[12]; 199101297Sobrien 200283875Ssmh strmode(p->fts_statp->st_mode, m1); 201283875Ssmh strmode((p->fts_statp->st_mode & 202283875Ssmh S_IFMT) | newmode, m2); 203283875Ssmh (void)printf(": 0%o [%s] -> 0%o [%s]", 204283875Ssmh p->fts_statp->st_mode, m1, 205283875Ssmh (p->fts_statp->st_mode & S_IFMT) | 206283875Ssmh newmode, m2); 207101297Sobrien } 208283875Ssmh (void)printf("\n"); 2091556Srgrimes } 2101556Srgrimes } 2111556Srgrimes if (errno) 2121556Srgrimes err(1, "fts_read"); 2131556Srgrimes exit(rval); 2141556Srgrimes} 2151556Srgrimes 216194795Sdelphijstatic void 21790107Simpusage(void) 2181556Srgrimes{ 2191556Srgrimes (void)fprintf(stderr, 22077342Sru "usage: chmod [-fhv] [-R [-H | -L | -P]] mode file ...\n"); 2211556Srgrimes exit(1); 2221556Srgrimes} 223195243Strasz 224195243Straszstatic int 225196711Straszmay_have_nfs4acl(const FTSENT *ent, int hflag) 226195243Strasz{ 227195243Strasz int ret; 228196711Strasz static dev_t previous_dev = NODEV; 229195243Strasz static int supports_acls = -1; 230195243Strasz 231195243Strasz if (previous_dev != ent->fts_statp->st_dev) { 232195243Strasz previous_dev = ent->fts_statp->st_dev; 233195243Strasz supports_acls = 0; 234195243Strasz 235196711Strasz if (hflag) 236196711Strasz ret = lpathconf(ent->fts_accpath, _PC_ACL_NFS4); 237196711Strasz else 238196711Strasz ret = pathconf(ent->fts_accpath, _PC_ACL_NFS4); 239195243Strasz if (ret > 0) 240195243Strasz supports_acls = 1; 241195243Strasz else if (ret < 0 && errno != EINVAL) 242195243Strasz warn("%s", ent->fts_path); 243195243Strasz } 244195243Strasz 245195243Strasz return (supports_acls); 246195243Strasz} 247