11553Srgrimes/* 21553Srgrimes * Copyright (c) 1988, 1993, 1994 31553Srgrimes * The Regents of the University of California. All rights reserved. 41553Srgrimes * 51553Srgrimes * Redistribution and use in source and binary forms, with or without 61553Srgrimes * modification, are permitted provided that the following conditions 71553Srgrimes * are met: 81553Srgrimes * 1. Redistributions of source code must retain the above copyright 91553Srgrimes * notice, this list of conditions and the following disclaimer. 101553Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 111553Srgrimes * notice, this list of conditions and the following disclaimer in the 121553Srgrimes * documentation and/or other materials provided with the distribution. 131553Srgrimes * 4. Neither the name of the University nor the names of its contributors 141553Srgrimes * may be used to endorse or promote products derived from this software 151553Srgrimes * without specific prior written permission. 161553Srgrimes * 171553Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 181553Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 191553Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 201553Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 211553Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 221553Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 231553Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 241553Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 251553Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 261553Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 271553Srgrimes * SUCH DAMAGE. 281553Srgrimes */ 291553Srgrimes 30114601Sobrien#if 0 311553Srgrimes#ifndef lint 3228643Sstevestatic const char copyright[] = 331553Srgrimes"@(#) Copyright (c) 1988, 1993, 1994\n\ 341553Srgrimes The Regents of the University of California. All rights reserved.\n"; 351553Srgrimes#endif /* not lint */ 361553Srgrimes 371553Srgrimes#ifndef lint 381553Srgrimesstatic char sccsid[] = "@(#)chown.c 8.8 (Berkeley) 4/4/94"; 39114601Sobrien#endif /* not lint */ 4028643Ssteve#endif 41119856Scharnier 4299141Sjmallett#include <sys/cdefs.h> 4399141Sjmallett__FBSDID("$FreeBSD$"); 4499141Sjmallett 451553Srgrimes#include <sys/param.h> 461553Srgrimes#include <sys/stat.h> 471553Srgrimes 481553Srgrimes#include <err.h> 491553Srgrimes#include <errno.h> 50283875Ssmh#include <fcntl.h> 511553Srgrimes#include <fts.h> 521553Srgrimes#include <grp.h> 53108443Sobrien#include <libgen.h> 541553Srgrimes#include <pwd.h> 55114005Sjohan#include <stdint.h> 561553Srgrimes#include <stdio.h> 571553Srgrimes#include <stdlib.h> 581553Srgrimes#include <string.h> 591553Srgrimes#include <unistd.h> 601553Srgrimes 61227251Sedstatic void a_gid(const char *); 62227251Sedstatic void a_uid(const char *); 63227251Sedstatic void chownerr(const char *); 64227251Sedstatic uid_t id(const char *, const char *); 65227251Sedstatic void usage(void); 661553Srgrimes 67227251Sedstatic uid_t uid; 68227251Sedstatic gid_t gid; 69227251Sedstatic int ischown; 70227251Sedstatic const char *gname; 711553Srgrimes 721553Srgrimesint 7399141Sjmallettmain(int argc, char **argv) 741553Srgrimes{ 751553Srgrimes FTS *ftsp; 761553Srgrimes FTSENT *p; 77204165Sgavin int Hflag, Lflag, Rflag, fflag, hflag, vflag, xflag; 7883410Sru int ch, fts_options, rval; 791553Srgrimes char *cp; 808857Srgrimes 81108443Sobrien ischown = (strcmp(basename(argv[0]), "chown") == 0); 828857Srgrimes 83204165Sgavin Hflag = Lflag = Rflag = fflag = hflag = vflag = xflag = 0; 84204165Sgavin while ((ch = getopt(argc, argv, "HLPRfhvx")) != -1) 851553Srgrimes switch (ch) { 861553Srgrimes case 'H': 871553Srgrimes Hflag = 1; 8883410Sru Lflag = 0; 891553Srgrimes break; 901553Srgrimes case 'L': 911553Srgrimes Lflag = 1; 9283410Sru Hflag = 0; 931553Srgrimes break; 941553Srgrimes case 'P': 951553Srgrimes Hflag = Lflag = 0; 961553Srgrimes break; 971553Srgrimes case 'R': 981553Srgrimes Rflag = 1; 991553Srgrimes break; 1001553Srgrimes case 'f': 1011553Srgrimes fflag = 1; 1021553Srgrimes break; 1031553Srgrimes case 'h': 1041553Srgrimes hflag = 1; 1051553Srgrimes break; 10657830Sobrien case 'v': 107114005Sjohan vflag++; 10857830Sobrien break; 109204165Sgavin case 'x': 110204165Sgavin xflag = 1; 111204165Sgavin break; 1121553Srgrimes case '?': 1131553Srgrimes default: 1141553Srgrimes usage(); 1151553Srgrimes } 1161553Srgrimes argv += optind; 1171553Srgrimes argc -= optind; 1181553Srgrimes 1191553Srgrimes if (argc < 2) 1201553Srgrimes usage(); 1211553Srgrimes 1221553Srgrimes if (Rflag) { 12383410Sru if (hflag && (Hflag || Lflag)) 12483410Sru errx(1, "the -R%c and -h options may not be " 12583410Sru "specified together", Hflag ? 'H' : 'L'); 126283875Ssmh if (Lflag) { 127283875Ssmh fts_options = FTS_LOGICAL; 128283875Ssmh } else { 129283875Ssmh fts_options = FTS_PHYSICAL; 130283875Ssmh 131283875Ssmh if (Hflag) { 132283875Ssmh fts_options |= FTS_COMFOLLOW; 133283875Ssmh } 1341553Srgrimes } 135283875Ssmh } else if (hflag) { 136283875Ssmh fts_options = FTS_PHYSICAL; 137283875Ssmh } else { 138283875Ssmh fts_options = FTS_LOGICAL; 139283875Ssmh } 140283875Ssmh 141204165Sgavin if (xflag) 142204165Sgavin fts_options |= FTS_XDEV; 1431553Srgrimes 14483410Sru uid = (uid_t)-1; 14583410Sru gid = (gid_t)-1; 1461553Srgrimes if (ischown) { 14729656Swosch if ((cp = strchr(*argv, ':')) != NULL) { 1481553Srgrimes *cp++ = '\0'; 1491553Srgrimes a_gid(cp); 15029656Swosch } 15129656Swosch#ifdef SUPPORT_DOT 15229656Swosch else if ((cp = strchr(*argv, '.')) != NULL) { 153100252Sdwmalone warnx("separation of user and group with a period is deprecated"); 1541553Srgrimes *cp++ = '\0'; 1551553Srgrimes a_gid(cp); 1568857Srgrimes } 15729656Swosch#endif 1581553Srgrimes a_uid(*argv); 1598857Srgrimes } else 1601553Srgrimes a_gid(*argv); 1611553Srgrimes 1621553Srgrimes if ((ftsp = fts_open(++argv, fts_options, 0)) == NULL) 1631553Srgrimes err(1, NULL); 1641553Srgrimes 1651553Srgrimes for (rval = 0; (p = fts_read(ftsp)) != NULL;) { 166283875Ssmh int atflag; 167283875Ssmh 168283875Ssmh if ((fts_options & FTS_LOGICAL) || 169283875Ssmh ((fts_options & FTS_COMFOLLOW) && 170283875Ssmh p->fts_level == FTS_ROOTLEVEL)) 171283875Ssmh atflag = 0; 172283875Ssmh else 173283875Ssmh atflag = AT_SYMLINK_NOFOLLOW; 174283875Ssmh 1751553Srgrimes switch (p->fts_info) { 17683410Sru case FTS_D: /* Change it at FTS_DP. */ 17717597Sadam if (!Rflag) 17817597Sadam fts_set(ftsp, p, FTS_SKIP); 17917597Sadam continue; 18083410Sru case FTS_DNR: /* Warn, chown. */ 1811553Srgrimes warnx("%s: %s", p->fts_path, strerror(p->fts_errno)); 1821553Srgrimes rval = 1; 1831553Srgrimes break; 1841553Srgrimes case FTS_ERR: /* Warn, continue. */ 1851553Srgrimes case FTS_NS: 1861553Srgrimes warnx("%s: %s", p->fts_path, strerror(p->fts_errno)); 1871553Srgrimes rval = 1; 1881553Srgrimes continue; 1891553Srgrimes default: 1901553Srgrimes break; 1911553Srgrimes } 19283410Sru if ((uid == (uid_t)-1 || uid == p->fts_statp->st_uid) && 19383410Sru (gid == (gid_t)-1 || gid == p->fts_statp->st_gid)) 19464014Speter continue; 195283875Ssmh if (fchownat(AT_FDCWD, p->fts_accpath, uid, gid, atflag) 196283875Ssmh == -1 && !fflag) { 197283875Ssmh chownerr(p->fts_path); 198283875Ssmh rval = 1; 199283875Ssmh } else if (vflag) { 200283875Ssmh printf("%s", p->fts_path); 201283875Ssmh if (vflag > 1) { 202283875Ssmh if (ischown) { 203283875Ssmh printf(": %ju:%ju -> %ju:%ju", 204283875Ssmh (uintmax_t) 205283875Ssmh p->fts_statp->st_uid, 206283875Ssmh (uintmax_t) 207283875Ssmh p->fts_statp->st_gid, 208283875Ssmh (uid == (uid_t)-1) ? 209283875Ssmh (uintmax_t) 210283875Ssmh p->fts_statp->st_uid : 211283875Ssmh (uintmax_t)uid, 212283875Ssmh (gid == (gid_t)-1) ? 213283875Ssmh (uintmax_t) 214283875Ssmh p->fts_statp->st_gid : 215283875Ssmh (uintmax_t)gid); 216283875Ssmh } else { 217283875Ssmh printf(": %ju -> %ju", 218283875Ssmh (uintmax_t) 219283875Ssmh p->fts_statp->st_gid, 220283875Ssmh (gid == (gid_t)-1) ? 221283875Ssmh (uintmax_t) 222283875Ssmh p->fts_statp->st_gid : 223283875Ssmh (uintmax_t)gid); 224114005Sjohan } 225114005Sjohan } 226283875Ssmh printf("\n"); 2271553Srgrimes } 2281553Srgrimes } 2291553Srgrimes if (errno) 2301553Srgrimes err(1, "fts_read"); 2311553Srgrimes exit(rval); 2321553Srgrimes} 2331553Srgrimes 234227251Sedstatic void 23599141Sjmalletta_gid(const char *s) 2361553Srgrimes{ 2371553Srgrimes struct group *gr; 2381553Srgrimes 2391553Srgrimes if (*s == '\0') /* Argument was "uid[:.]". */ 2401553Srgrimes return; 2411553Srgrimes gname = s; 24283410Sru gid = ((gr = getgrnam(s)) != NULL) ? gr->gr_gid : id(s, "group"); 2431553Srgrimes} 2441553Srgrimes 245227251Sedstatic void 24699141Sjmalletta_uid(const char *s) 2471553Srgrimes{ 2481553Srgrimes struct passwd *pw; 2491553Srgrimes 2501553Srgrimes if (*s == '\0') /* Argument was "[:.]gid". */ 2511553Srgrimes return; 25283410Sru uid = ((pw = getpwnam(s)) != NULL) ? pw->pw_uid : id(s, "user"); 2531553Srgrimes} 2541553Srgrimes 255227251Sedstatic uid_t 25699141Sjmallettid(const char *name, const char *type) 2571553Srgrimes{ 258119856Scharnier uid_t val; 2591553Srgrimes char *ep; 2601553Srgrimes 2611553Srgrimes /* 2621553Srgrimes * XXX 2631553Srgrimes * We know that uid_t's and gid_t's are unsigned longs. 2641553Srgrimes */ 2651553Srgrimes errno = 0; 2661553Srgrimes val = strtoul(name, &ep, 10); 267208792Strasz if (errno || *ep != '\0') 2681553Srgrimes errx(1, "%s: illegal %s name", name, type); 2691553Srgrimes return (val); 2701553Srgrimes} 2711553Srgrimes 272227251Sedstatic void 27399141Sjmallettchownerr(const char *file) 2741553Srgrimes{ 27583410Sru static uid_t euid = -1; 27683410Sru static int ngroups = -1; 277194494Sbrooks static long ngroups_max; 278194494Sbrooks gid_t *groups; 2791553Srgrimes 2801553Srgrimes /* Check for chown without being root. */ 28197732Stjr if (errno != EPERM || (uid != (uid_t)-1 && 28297732Stjr euid == (uid_t)-1 && (euid = geteuid()) != 0)) { 28397732Stjr warn("%s", file); 28497732Stjr return; 28597732Stjr } 2861553Srgrimes 2871553Srgrimes /* Check group membership; kernel just returns EPERM. */ 28883410Sru if (gid != (gid_t)-1 && ngroups == -1 && 28983410Sru euid == (uid_t)-1 && (euid = geteuid()) != 0) { 290194494Sbrooks ngroups_max = sysconf(_SC_NGROUPS_MAX) + 1; 291194494Sbrooks if ((groups = malloc(sizeof(gid_t) * ngroups_max)) == NULL) 292194494Sbrooks err(1, "malloc"); 293194494Sbrooks ngroups = getgroups(ngroups_max, groups); 2941553Srgrimes while (--ngroups >= 0 && gid != groups[ngroups]); 295208815Strasz free(groups); 29697732Stjr if (ngroups < 0) { 29797732Stjr warnx("you are not a member of group %s", gname); 29897732Stjr return; 29997732Stjr } 3001553Srgrimes } 30128643Ssteve warn("%s", file); 3021553Srgrimes} 3031553Srgrimes 304227251Sedstatic void 30599141Sjmallettusage(void) 3061553Srgrimes{ 30783410Sru 30883410Sru if (ischown) 30983410Sru (void)fprintf(stderr, "%s\n%s\n", 310204165Sgavin "usage: chown [-fhvx] [-R [-H | -L | -P]] owner[:group]" 31183410Sru " file ...", 312204165Sgavin " chown [-fhvx] [-R [-H | -L | -P]] :group file ..."); 31383410Sru else 31483410Sru (void)fprintf(stderr, "%s\n", 315204165Sgavin "usage: chgrp [-fhvx] [-R [-H | -L | -P]] group file ..."); 3161553Srgrimes exit(1); 3171553Srgrimes} 318