chown.c revision 97732
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 341553Srgrimes#ifndef lint 3528643Sstevestatic const char copyright[] = 361553Srgrimes"@(#) Copyright (c) 1988, 1993, 1994\n\ 371553Srgrimes The Regents of the University of California. All rights reserved.\n"; 381553Srgrimes#endif /* not lint */ 391553Srgrimes 401553Srgrimes#ifndef lint 4128643Ssteve#if 0 421553Srgrimesstatic char sccsid[] = "@(#)chown.c 8.8 (Berkeley) 4/4/94"; 4328643Ssteve#else 4428643Sstevestatic const char rcsid[] = 4550479Speter "$FreeBSD: head/usr.sbin/chown/chown.c 97732 2002-06-02 09:32:39Z tjr $"; 4628643Ssteve#endif 471553Srgrimes#endif /* not lint */ 481553Srgrimes 491553Srgrimes#include <sys/param.h> 501553Srgrimes#include <sys/stat.h> 511553Srgrimes 521553Srgrimes#include <err.h> 531553Srgrimes#include <errno.h> 541553Srgrimes#include <fts.h> 551553Srgrimes#include <grp.h> 561553Srgrimes#include <pwd.h> 571553Srgrimes#include <stdio.h> 581553Srgrimes#include <stdlib.h> 591553Srgrimes#include <string.h> 601553Srgrimes#include <unistd.h> 611553Srgrimes 6283410Sruvoid a_gid __P((const char *)); 6383410Sruvoid a_uid __P((const char *)); 6483410Sruvoid chownerr __P((const char *)); 6583410Sruu_long id __P((const char *, const char *)); 661553Srgrimesvoid usage __P((void)); 6787893Sluigiint main __P((int argc, char *argv[])); 681553Srgrimes 691553Srgrimesuid_t uid; 701553Srgrimesgid_t gid; 7183410Sruint ischown; 7283410Sruconst char *gname; 731553Srgrimes 741553Srgrimesint 751553Srgrimesmain(argc, argv) 761553Srgrimes int argc; 771553Srgrimes char *argv[]; 781553Srgrimes{ 791553Srgrimes FTS *ftsp; 801553Srgrimes FTSENT *p; 8183410Sru int Hflag, Lflag, Rflag, fflag, hflag, vflag; 8283410Sru int ch, fts_options, rval; 831553Srgrimes char *cp; 848857Srgrimes 8583410Sru cp = strrchr(argv[0], '/'); 8683410Sru cp = (cp != NULL) ? cp + 1 : argv[0]; 8783410Sru ischown = (strcmp(cp, "chown") == 0); 888857Srgrimes 8983410Sru Hflag = Lflag = Rflag = fflag = hflag = vflag = 0; 9057830Sobrien while ((ch = getopt(argc, argv, "HLPRfhv")) != -1) 911553Srgrimes switch (ch) { 921553Srgrimes case 'H': 931553Srgrimes Hflag = 1; 9483410Sru Lflag = 0; 951553Srgrimes break; 961553Srgrimes case 'L': 971553Srgrimes Lflag = 1; 9883410Sru Hflag = 0; 991553Srgrimes break; 1001553Srgrimes case 'P': 1011553Srgrimes Hflag = Lflag = 0; 1021553Srgrimes break; 1031553Srgrimes case 'R': 1041553Srgrimes Rflag = 1; 1051553Srgrimes break; 1061553Srgrimes case 'f': 1071553Srgrimes fflag = 1; 1081553Srgrimes break; 1091553Srgrimes case 'h': 1101553Srgrimes hflag = 1; 1111553Srgrimes break; 11257830Sobrien case 'v': 11357830Sobrien vflag = 1; 11457830Sobrien break; 1151553Srgrimes case '?': 1161553Srgrimes default: 1171553Srgrimes usage(); 1181553Srgrimes } 1191553Srgrimes argv += optind; 1201553Srgrimes argc -= optind; 1211553Srgrimes 1221553Srgrimes if (argc < 2) 1231553Srgrimes usage(); 1241553Srgrimes 1251553Srgrimes if (Rflag) { 12677333Sru fts_options = FTS_PHYSICAL; 12783410Sru if (hflag && (Hflag || Lflag)) 12883410Sru errx(1, "the -R%c and -h options may not be " 12983410Sru "specified together", Hflag ? 'H' : 'L'); 1301553Srgrimes if (Hflag) 1311553Srgrimes fts_options |= FTS_COMFOLLOW; 13283410Sru else if (Lflag) { 1331553Srgrimes fts_options &= ~FTS_PHYSICAL; 1341553Srgrimes fts_options |= FTS_LOGICAL; 1351553Srgrimes } 13677333Sru } else 13777522Sru fts_options = hflag ? FTS_PHYSICAL : FTS_LOGICAL; 1381553Srgrimes 13983410Sru uid = (uid_t)-1; 14083410Sru gid = (gid_t)-1; 1411553Srgrimes if (ischown) { 14229656Swosch if ((cp = strchr(*argv, ':')) != NULL) { 1431553Srgrimes *cp++ = '\0'; 1441553Srgrimes a_gid(cp); 14529656Swosch } 14629656Swosch#ifdef SUPPORT_DOT 14729656Swosch else if ((cp = strchr(*argv, '.')) != NULL) { 1481553Srgrimes *cp++ = '\0'; 1491553Srgrimes a_gid(cp); 1508857Srgrimes } 15129656Swosch#endif 1521553Srgrimes a_uid(*argv); 1538857Srgrimes } else 1541553Srgrimes a_gid(*argv); 1551553Srgrimes 1561553Srgrimes if ((ftsp = fts_open(++argv, fts_options, 0)) == NULL) 1571553Srgrimes err(1, NULL); 1581553Srgrimes 1591553Srgrimes for (rval = 0; (p = fts_read(ftsp)) != NULL;) { 1601553Srgrimes switch (p->fts_info) { 16183410Sru case FTS_D: /* Change it at FTS_DP. */ 16217597Sadam if (!Rflag) 16317597Sadam fts_set(ftsp, p, FTS_SKIP); 16417597Sadam continue; 16583410Sru case FTS_DNR: /* Warn, chown. */ 1661553Srgrimes warnx("%s: %s", p->fts_path, strerror(p->fts_errno)); 1671553Srgrimes rval = 1; 1681553Srgrimes break; 1691553Srgrimes case FTS_ERR: /* Warn, continue. */ 1701553Srgrimes case FTS_NS: 1711553Srgrimes warnx("%s: %s", p->fts_path, strerror(p->fts_errno)); 1721553Srgrimes rval = 1; 1731553Srgrimes continue; 17483410Sru case FTS_SL: 1751553Srgrimes case FTS_SLNONE: 1761553Srgrimes /* 1771553Srgrimes * The only symlinks that end up here are ones that 1781553Srgrimes * don't point to anything and ones that we found 1791553Srgrimes * doing a physical walk. 1801553Srgrimes */ 18124446Speter if (hflag) 18224446Speter break; 18324446Speter else 18424446Speter continue; 1851553Srgrimes default: 1861553Srgrimes break; 1871553Srgrimes } 18883410Sru if ((uid == (uid_t)-1 || uid == p->fts_statp->st_uid) && 18983410Sru (gid == (gid_t)-1 || gid == p->fts_statp->st_gid)) 19064014Speter continue; 19183410Sru if ((hflag ? lchown : chown)(p->fts_accpath, uid, gid) == -1) { 19283410Sru if (!fflag) { 19324446Speter chownerr(p->fts_path); 19424446Speter rval = 1; 19524446Speter } 19624446Speter } else { 19783410Sru if (vflag) 19883410Sru printf("%s\n", p->fts_path); 1991553Srgrimes } 2001553Srgrimes } 2011553Srgrimes if (errno) 2021553Srgrimes err(1, "fts_read"); 2031553Srgrimes exit(rval); 2041553Srgrimes} 2051553Srgrimes 2061553Srgrimesvoid 2071553Srgrimesa_gid(s) 20883410Sru const char *s; 2091553Srgrimes{ 2101553Srgrimes struct group *gr; 2111553Srgrimes 2121553Srgrimes if (*s == '\0') /* Argument was "uid[:.]". */ 2131553Srgrimes return; 2141553Srgrimes gname = s; 21583410Sru gid = ((gr = getgrnam(s)) != NULL) ? gr->gr_gid : id(s, "group"); 2161553Srgrimes} 2171553Srgrimes 2181553Srgrimesvoid 2191553Srgrimesa_uid(s) 22083410Sru const char *s; 2211553Srgrimes{ 2221553Srgrimes struct passwd *pw; 2231553Srgrimes 2241553Srgrimes if (*s == '\0') /* Argument was "[:.]gid". */ 2251553Srgrimes return; 22683410Sru uid = ((pw = getpwnam(s)) != NULL) ? pw->pw_uid : id(s, "user"); 2271553Srgrimes} 2281553Srgrimes 2291553Srgrimesu_long 2301553Srgrimesid(name, type) 23183410Sru const char *name, *type; 2321553Srgrimes{ 2331553Srgrimes u_long val; 2341553Srgrimes char *ep; 2351553Srgrimes 2361553Srgrimes /* 2371553Srgrimes * XXX 2381553Srgrimes * We know that uid_t's and gid_t's are unsigned longs. 2391553Srgrimes */ 2401553Srgrimes errno = 0; 2411553Srgrimes val = strtoul(name, &ep, 10); 2421553Srgrimes if (errno) 2431553Srgrimes err(1, "%s", name); 2441553Srgrimes if (*ep != '\0') 2451553Srgrimes errx(1, "%s: illegal %s name", name, type); 2461553Srgrimes return (val); 2471553Srgrimes} 2481553Srgrimes 2491553Srgrimesvoid 2501553Srgrimeschownerr(file) 25183410Sru const char *file; 2521553Srgrimes{ 25383410Sru static uid_t euid = -1; 25483410Sru static int ngroups = -1; 25583410Sru gid_t groups[NGROUPS_MAX]; 2561553Srgrimes 2571553Srgrimes /* Check for chown without being root. */ 25897732Stjr if (errno != EPERM || (uid != (uid_t)-1 && 25997732Stjr euid == (uid_t)-1 && (euid = geteuid()) != 0)) { 26097732Stjr warn("%s", file); 26197732Stjr return; 26297732Stjr } 2631553Srgrimes 2641553Srgrimes /* Check group membership; kernel just returns EPERM. */ 26583410Sru if (gid != (gid_t)-1 && ngroups == -1 && 26683410Sru euid == (uid_t)-1 && (euid = geteuid()) != 0) { 26783410Sru ngroups = getgroups(NGROUPS_MAX, groups); 2681553Srgrimes while (--ngroups >= 0 && gid != groups[ngroups]); 26997732Stjr if (ngroups < 0) { 27097732Stjr warnx("you are not a member of group %s", gname); 27197732Stjr return; 27297732Stjr } 2731553Srgrimes } 27428643Ssteve warn("%s", file); 2751553Srgrimes} 2761553Srgrimes 2771553Srgrimesvoid 2781553Srgrimesusage() 2791553Srgrimes{ 28083410Sru 28183410Sru if (ischown) 28283410Sru (void)fprintf(stderr, "%s\n%s\n", 28383410Sru "usage: chown [-fhv] [-R [-H | -L | -P]] owner[:group]" 28483410Sru " file ...", 28583410Sru " chown [-fhv] [-R [-H | -L | -P]] :group file ..."); 28683410Sru else 28783410Sru (void)fprintf(stderr, "%s\n", 28883410Sru "usage: chgrp [-fhv] [-R [-H | -L | -P]] group file ..."); 2891553Srgrimes exit(1); 2901553Srgrimes} 291