chown.c revision 114601
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 * 3. All advertising materials mentioning features or use of this software 141553Srgrimes * must display the following acknowledgement: 151553Srgrimes * This product includes software developed by the University of 161553Srgrimes * California, Berkeley and its contributors. 171553Srgrimes * 4. Neither the name of the University nor the names of its contributors 181553Srgrimes * may be used to endorse or promote products derived from this software 191553Srgrimes * without specific prior written permission. 201553Srgrimes * 211553Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 221553Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 231553Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 241553Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 251553Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 261553Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 271553Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 281553Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 291553Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 301553Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 311553Srgrimes * SUCH DAMAGE. 321553Srgrimes */ 331553Srgrimes 34114601Sobrien#if 0 351553Srgrimes#ifndef lint 3628643Sstevestatic const char copyright[] = 371553Srgrimes"@(#) Copyright (c) 1988, 1993, 1994\n\ 381553Srgrimes The Regents of the University of California. All rights reserved.\n"; 391553Srgrimes#endif /* not lint */ 401553Srgrimes 411553Srgrimes#ifndef lint 421553Srgrimesstatic char sccsid[] = "@(#)chown.c 8.8 (Berkeley) 4/4/94"; 43114601Sobrien#endif /* not lint */ 4428643Ssteve#endif 4599141Sjmallett#include <sys/cdefs.h> 4699141Sjmallett__FBSDID("$FreeBSD: head/usr.sbin/chown/chown.c 114601 2003-05-03 21:06:42Z obrien $"); 4799141Sjmallett 481553Srgrimes#include <sys/param.h> 491553Srgrimes#include <sys/stat.h> 501553Srgrimes 511553Srgrimes#include <err.h> 521553Srgrimes#include <errno.h> 531553Srgrimes#include <fts.h> 541553Srgrimes#include <grp.h> 55108443Sobrien#include <libgen.h> 561553Srgrimes#include <pwd.h> 57114005Sjohan#include <stdint.h> 581553Srgrimes#include <stdio.h> 591553Srgrimes#include <stdlib.h> 601553Srgrimes#include <string.h> 611553Srgrimes#include <unistd.h> 621553Srgrimes 6399141Sjmallettvoid a_gid(const char *); 6499141Sjmallettvoid a_uid(const char *); 6599141Sjmallettvoid chownerr(const char *); 6699141Sjmallettu_long id(const char *, const char *); 6799141Sjmallettvoid usage(void); 681553Srgrimes 691553Srgrimesuid_t uid; 701553Srgrimesgid_t gid; 7183410Sruint ischown; 7283410Sruconst char *gname; 731553Srgrimes 741553Srgrimesint 7599141Sjmallettmain(int argc, char **argv) 761553Srgrimes{ 771553Srgrimes FTS *ftsp; 781553Srgrimes FTSENT *p; 7983410Sru int Hflag, Lflag, Rflag, fflag, hflag, vflag; 8083410Sru int ch, fts_options, rval; 811553Srgrimes char *cp; 828857Srgrimes 83108443Sobrien ischown = (strcmp(basename(argv[0]), "chown") == 0); 848857Srgrimes 8583410Sru Hflag = Lflag = Rflag = fflag = hflag = vflag = 0; 8657830Sobrien while ((ch = getopt(argc, argv, "HLPRfhv")) != -1) 871553Srgrimes switch (ch) { 881553Srgrimes case 'H': 891553Srgrimes Hflag = 1; 9083410Sru Lflag = 0; 911553Srgrimes break; 921553Srgrimes case 'L': 931553Srgrimes Lflag = 1; 9483410Sru Hflag = 0; 951553Srgrimes break; 961553Srgrimes case 'P': 971553Srgrimes Hflag = Lflag = 0; 981553Srgrimes break; 991553Srgrimes case 'R': 1001553Srgrimes Rflag = 1; 1011553Srgrimes break; 1021553Srgrimes case 'f': 1031553Srgrimes fflag = 1; 1041553Srgrimes break; 1051553Srgrimes case 'h': 1061553Srgrimes hflag = 1; 1071553Srgrimes break; 10857830Sobrien case 'v': 109114005Sjohan vflag++; 11057830Sobrien break; 1111553Srgrimes case '?': 1121553Srgrimes default: 1131553Srgrimes usage(); 1141553Srgrimes } 1151553Srgrimes argv += optind; 1161553Srgrimes argc -= optind; 1171553Srgrimes 1181553Srgrimes if (argc < 2) 1191553Srgrimes usage(); 1201553Srgrimes 1211553Srgrimes if (Rflag) { 12277333Sru fts_options = FTS_PHYSICAL; 12383410Sru if (hflag && (Hflag || Lflag)) 12483410Sru errx(1, "the -R%c and -h options may not be " 12583410Sru "specified together", Hflag ? 'H' : 'L'); 1261553Srgrimes if (Hflag) 1271553Srgrimes fts_options |= FTS_COMFOLLOW; 12883410Sru else if (Lflag) { 1291553Srgrimes fts_options &= ~FTS_PHYSICAL; 1301553Srgrimes fts_options |= FTS_LOGICAL; 1311553Srgrimes } 13277333Sru } else 13377522Sru fts_options = hflag ? FTS_PHYSICAL : FTS_LOGICAL; 1341553Srgrimes 13583410Sru uid = (uid_t)-1; 13683410Sru gid = (gid_t)-1; 1371553Srgrimes if (ischown) { 13829656Swosch if ((cp = strchr(*argv, ':')) != NULL) { 1391553Srgrimes *cp++ = '\0'; 1401553Srgrimes a_gid(cp); 14129656Swosch } 14229656Swosch#ifdef SUPPORT_DOT 14329656Swosch else if ((cp = strchr(*argv, '.')) != NULL) { 144100252Sdwmalone warnx("separation of user and group with a period is deprecated"); 1451553Srgrimes *cp++ = '\0'; 1461553Srgrimes a_gid(cp); 1478857Srgrimes } 14829656Swosch#endif 1491553Srgrimes a_uid(*argv); 1508857Srgrimes } else 1511553Srgrimes a_gid(*argv); 1521553Srgrimes 1531553Srgrimes if ((ftsp = fts_open(++argv, fts_options, 0)) == NULL) 1541553Srgrimes err(1, NULL); 1551553Srgrimes 1561553Srgrimes for (rval = 0; (p = fts_read(ftsp)) != NULL;) { 1571553Srgrimes switch (p->fts_info) { 15883410Sru case FTS_D: /* Change it at FTS_DP. */ 15917597Sadam if (!Rflag) 16017597Sadam fts_set(ftsp, p, FTS_SKIP); 16117597Sadam continue; 16283410Sru case FTS_DNR: /* Warn, chown. */ 1631553Srgrimes warnx("%s: %s", p->fts_path, strerror(p->fts_errno)); 1641553Srgrimes rval = 1; 1651553Srgrimes break; 1661553Srgrimes case FTS_ERR: /* Warn, continue. */ 1671553Srgrimes case FTS_NS: 1681553Srgrimes warnx("%s: %s", p->fts_path, strerror(p->fts_errno)); 1691553Srgrimes rval = 1; 1701553Srgrimes continue; 17183410Sru case FTS_SL: 1721553Srgrimes case FTS_SLNONE: 1731553Srgrimes /* 1741553Srgrimes * The only symlinks that end up here are ones that 1751553Srgrimes * don't point to anything and ones that we found 1761553Srgrimes * doing a physical walk. 1771553Srgrimes */ 17824446Speter if (hflag) 17924446Speter break; 18024446Speter else 18124446Speter continue; 1821553Srgrimes default: 1831553Srgrimes break; 1841553Srgrimes } 18583410Sru if ((uid == (uid_t)-1 || uid == p->fts_statp->st_uid) && 18683410Sru (gid == (gid_t)-1 || gid == p->fts_statp->st_gid)) 18764014Speter continue; 18883410Sru if ((hflag ? lchown : chown)(p->fts_accpath, uid, gid) == -1) { 18983410Sru if (!fflag) { 19024446Speter chownerr(p->fts_path); 19124446Speter rval = 1; 19224446Speter } 19324446Speter } else { 194114005Sjohan if (vflag) { 195114005Sjohan printf("%s", p->fts_path); 196114005Sjohan if (vflag > 1) { 197114005Sjohan if (ischown) { 198114005Sjohan printf(": %ju:%ju -> %ju:%ju", 199114005Sjohan (uintmax_t) 200114005Sjohan p->fts_statp->st_uid, 201114005Sjohan (uintmax_t) 202114005Sjohan p->fts_statp->st_gid, 203114005Sjohan (uid == (uid_t)-1) ? 204114005Sjohan (uintmax_t) 205114005Sjohan p->fts_statp->st_uid : 206114005Sjohan (uintmax_t)uid, 207114005Sjohan (gid == (gid_t)-1) ? 208114005Sjohan (uintmax_t) 209114005Sjohan p->fts_statp->st_gid : 210114005Sjohan (uintmax_t)gid); 211114005Sjohan } else { 212114005Sjohan printf(": %ju -> %ju", 213114005Sjohan (uintmax_t) 214114005Sjohan p->fts_statp->st_gid, 215114005Sjohan (gid == (gid_t)-1) ? 216114005Sjohan (uintmax_t) 217114005Sjohan p->fts_statp->st_gid : 218114005Sjohan (uintmax_t)gid); 219114005Sjohan } 220114005Sjohan } 221114005Sjohan printf("\n"); 222114005Sjohan } 2231553Srgrimes } 2241553Srgrimes } 2251553Srgrimes if (errno) 2261553Srgrimes err(1, "fts_read"); 2271553Srgrimes exit(rval); 2281553Srgrimes} 2291553Srgrimes 2301553Srgrimesvoid 23199141Sjmalletta_gid(const char *s) 2321553Srgrimes{ 2331553Srgrimes struct group *gr; 2341553Srgrimes 2351553Srgrimes if (*s == '\0') /* Argument was "uid[:.]". */ 2361553Srgrimes return; 2371553Srgrimes gname = s; 23883410Sru gid = ((gr = getgrnam(s)) != NULL) ? gr->gr_gid : id(s, "group"); 2391553Srgrimes} 2401553Srgrimes 2411553Srgrimesvoid 24299141Sjmalletta_uid(const char *s) 2431553Srgrimes{ 2441553Srgrimes struct passwd *pw; 2451553Srgrimes 2461553Srgrimes if (*s == '\0') /* Argument was "[:.]gid". */ 2471553Srgrimes return; 24883410Sru uid = ((pw = getpwnam(s)) != NULL) ? pw->pw_uid : id(s, "user"); 2491553Srgrimes} 2501553Srgrimes 2511553Srgrimesu_long 25299141Sjmallettid(const char *name, const char *type) 2531553Srgrimes{ 2541553Srgrimes u_long val; 2551553Srgrimes char *ep; 2561553Srgrimes 2571553Srgrimes /* 2581553Srgrimes * XXX 2591553Srgrimes * We know that uid_t's and gid_t's are unsigned longs. 2601553Srgrimes */ 2611553Srgrimes errno = 0; 2621553Srgrimes val = strtoul(name, &ep, 10); 2631553Srgrimes if (errno) 2641553Srgrimes err(1, "%s", name); 2651553Srgrimes if (*ep != '\0') 2661553Srgrimes errx(1, "%s: illegal %s name", name, type); 2671553Srgrimes return (val); 2681553Srgrimes} 2691553Srgrimes 2701553Srgrimesvoid 27199141Sjmallettchownerr(const char *file) 2721553Srgrimes{ 27383410Sru static uid_t euid = -1; 27483410Sru static int ngroups = -1; 27583410Sru gid_t groups[NGROUPS_MAX]; 2761553Srgrimes 2771553Srgrimes /* Check for chown without being root. */ 27897732Stjr if (errno != EPERM || (uid != (uid_t)-1 && 27997732Stjr euid == (uid_t)-1 && (euid = geteuid()) != 0)) { 28097732Stjr warn("%s", file); 28197732Stjr return; 28297732Stjr } 2831553Srgrimes 2841553Srgrimes /* Check group membership; kernel just returns EPERM. */ 28583410Sru if (gid != (gid_t)-1 && ngroups == -1 && 28683410Sru euid == (uid_t)-1 && (euid = geteuid()) != 0) { 28783410Sru ngroups = getgroups(NGROUPS_MAX, groups); 2881553Srgrimes while (--ngroups >= 0 && gid != groups[ngroups]); 28997732Stjr if (ngroups < 0) { 29097732Stjr warnx("you are not a member of group %s", gname); 29197732Stjr return; 29297732Stjr } 2931553Srgrimes } 29428643Ssteve warn("%s", file); 2951553Srgrimes} 2961553Srgrimes 2971553Srgrimesvoid 29899141Sjmallettusage(void) 2991553Srgrimes{ 30083410Sru 30183410Sru if (ischown) 30283410Sru (void)fprintf(stderr, "%s\n%s\n", 30383410Sru "usage: chown [-fhv] [-R [-H | -L | -P]] owner[:group]" 30483410Sru " file ...", 30583410Sru " chown [-fhv] [-R [-H | -L | -P]] :group file ..."); 30683410Sru else 30783410Sru (void)fprintf(stderr, "%s\n", 30883410Sru "usage: chgrp [-fhv] [-R [-H | -L | -P]] group file ..."); 3091553Srgrimes exit(1); 3101553Srgrimes} 311