chown.c revision 1553
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 351553Srgrimesstatic 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 411553Srgrimesstatic char sccsid[] = "@(#)chown.c 8.8 (Berkeley) 4/4/94"; 421553Srgrimes#endif /* not lint */ 431553Srgrimes 441553Srgrimes#include <sys/param.h> 451553Srgrimes#include <sys/stat.h> 461553Srgrimes 471553Srgrimes#include <ctype.h> 481553Srgrimes#include <dirent.h> 491553Srgrimes#include <err.h> 501553Srgrimes#include <errno.h> 511553Srgrimes#include <fts.h> 521553Srgrimes#include <grp.h> 531553Srgrimes#include <pwd.h> 541553Srgrimes#include <stdio.h> 551553Srgrimes#include <stdlib.h> 561553Srgrimes#include <string.h> 571553Srgrimes#include <unistd.h> 581553Srgrimes 591553Srgrimesvoid a_gid __P((char *)); 601553Srgrimesvoid a_uid __P((char *)); 611553Srgrimesvoid chownerr __P((char *)); 621553Srgrimesu_long id __P((char *, char *)); 631553Srgrimesvoid usage __P((void)); 641553Srgrimes 651553Srgrimesuid_t uid; 661553Srgrimesgid_t gid; 671553Srgrimesint Rflag, ischown, fflag; 681553Srgrimeschar *gname, *myname; 691553Srgrimes 701553Srgrimesint 711553Srgrimesmain(argc, argv) 721553Srgrimes int argc; 731553Srgrimes char *argv[]; 741553Srgrimes{ 751553Srgrimes FTS *ftsp; 761553Srgrimes FTSENT *p; 771553Srgrimes int Hflag, Lflag, Pflag, ch, fts_options, hflag, rval; 781553Srgrimes char *cp; 791553Srgrimes 801553Srgrimes myname = (cp = rindex(*argv, '/')) ? cp + 1 : *argv; 811553Srgrimes ischown = myname[2] == 'o'; 821553Srgrimes 831553Srgrimes Hflag = Lflag = Pflag = hflag = 0; 841553Srgrimes while ((ch = getopt(argc, argv, "HLPRfh")) != EOF) 851553Srgrimes switch (ch) { 861553Srgrimes case 'H': 871553Srgrimes Hflag = 1; 881553Srgrimes Lflag = Pflag = 0; 891553Srgrimes break; 901553Srgrimes case 'L': 911553Srgrimes Lflag = 1; 921553Srgrimes Hflag = Pflag = 0; 931553Srgrimes break; 941553Srgrimes case 'P': 951553Srgrimes Pflag = 1; 961553Srgrimes Hflag = Lflag = 0; 971553Srgrimes break; 981553Srgrimes case 'R': 991553Srgrimes Rflag = 1; 1001553Srgrimes break; 1011553Srgrimes case 'f': 1021553Srgrimes fflag = 1; 1031553Srgrimes break; 1041553Srgrimes case 'h': 1051553Srgrimes /* 1061553Srgrimes * In System V (and probably POSIX.2) the -h option 1071553Srgrimes * causes chown/chgrp to change the owner/group of 1081553Srgrimes * the symbolic link. 4.4BSD's symbolic links don't 1091553Srgrimes * have owners/groups, so it's an undocumented noop. 1101553Srgrimes * Do syntax checking, though. 1111553Srgrimes */ 1121553Srgrimes hflag = 1; 1131553Srgrimes break; 1141553Srgrimes case '?': 1151553Srgrimes default: 1161553Srgrimes usage(); 1171553Srgrimes } 1181553Srgrimes argv += optind; 1191553Srgrimes argc -= optind; 1201553Srgrimes 1211553Srgrimes if (argc < 2) 1221553Srgrimes usage(); 1231553Srgrimes 1241553Srgrimes fts_options = FTS_PHYSICAL; 1251553Srgrimes if (Rflag) { 1261553Srgrimes if (hflag) 1271553Srgrimes errx(1, 1281553Srgrimes "the -R and -h options may not be specified together."); 1291553Srgrimes if (Hflag) 1301553Srgrimes fts_options |= FTS_COMFOLLOW; 1311553Srgrimes if (Lflag) { 1321553Srgrimes fts_options &= ~FTS_PHYSICAL; 1331553Srgrimes fts_options |= FTS_LOGICAL; 1341553Srgrimes } 1351553Srgrimes } 1361553Srgrimes 1371553Srgrimes uid = gid = -1; 1381553Srgrimes if (ischown) { 1391553Srgrimes#ifdef SUPPORT_DOT 1401553Srgrimes if ((cp = strchr(*argv, '.')) != NULL) { 1411553Srgrimes *cp++ = '\0'; 1421553Srgrimes a_gid(cp); 1431553Srgrimes } else 1441553Srgrimes#endif 1451553Srgrimes if ((cp = strchr(*argv, ':')) != NULL) { 1461553Srgrimes *cp++ = '\0'; 1471553Srgrimes a_gid(cp); 1481553Srgrimes } 1491553Srgrimes a_uid(*argv); 1501553Srgrimes } 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) { 1581553Srgrimes case FTS_D: 1591553Srgrimes if (Rflag) /* Change it at FTS_DP. */ 1601553Srgrimes continue; 1611553Srgrimes fts_set(ftsp, p, FTS_SKIP); 1621553Srgrimes break; 1631553Srgrimes case FTS_DNR: /* Warn, chown, continue. */ 1641553Srgrimes warnx("%s: %s", p->fts_path, strerror(p->fts_errno)); 1651553Srgrimes rval = 1; 1661553Srgrimes break; 1671553Srgrimes case FTS_ERR: /* Warn, continue. */ 1681553Srgrimes case FTS_NS: 1691553Srgrimes warnx("%s: %s", p->fts_path, strerror(p->fts_errno)); 1701553Srgrimes rval = 1; 1711553Srgrimes continue; 1721553Srgrimes case FTS_SL: /* Ignore. */ 1731553Srgrimes case FTS_SLNONE: 1741553Srgrimes /* 1751553Srgrimes * The only symlinks that end up here are ones that 1761553Srgrimes * don't point to anything and ones that we found 1771553Srgrimes * doing a physical walk. 1781553Srgrimes */ 1791553Srgrimes continue; 1801553Srgrimes default: 1811553Srgrimes break; 1821553Srgrimes } 1831553Srgrimes if (chown(p->fts_accpath, uid, gid) && !fflag) { 1841553Srgrimes chownerr(p->fts_path); 1851553Srgrimes rval = 1; 1861553Srgrimes } 1871553Srgrimes } 1881553Srgrimes if (errno) 1891553Srgrimes err(1, "fts_read"); 1901553Srgrimes exit(rval); 1911553Srgrimes} 1921553Srgrimes 1931553Srgrimesvoid 1941553Srgrimesa_gid(s) 1951553Srgrimes char *s; 1961553Srgrimes{ 1971553Srgrimes struct group *gr; 1981553Srgrimes 1991553Srgrimes if (*s == '\0') /* Argument was "uid[:.]". */ 2001553Srgrimes return; 2011553Srgrimes gname = s; 2021553Srgrimes gid = ((gr = getgrnam(s)) == NULL) ? id(s, "group") : gr->gr_gid; 2031553Srgrimes} 2041553Srgrimes 2051553Srgrimesvoid 2061553Srgrimesa_uid(s) 2071553Srgrimes char *s; 2081553Srgrimes{ 2091553Srgrimes struct passwd *pw; 2101553Srgrimes 2111553Srgrimes if (*s == '\0') /* Argument was "[:.]gid". */ 2121553Srgrimes return; 2131553Srgrimes uid = ((pw = getpwnam(s)) == NULL) ? id(s, "user") : pw->pw_uid; 2141553Srgrimes} 2151553Srgrimes 2161553Srgrimesu_long 2171553Srgrimesid(name, type) 2181553Srgrimes char *name, *type; 2191553Srgrimes{ 2201553Srgrimes u_long val; 2211553Srgrimes char *ep; 2221553Srgrimes 2231553Srgrimes /* 2241553Srgrimes * XXX 2251553Srgrimes * We know that uid_t's and gid_t's are unsigned longs. 2261553Srgrimes */ 2271553Srgrimes errno = 0; 2281553Srgrimes val = strtoul(name, &ep, 10); 2291553Srgrimes if (errno) 2301553Srgrimes err(1, "%s", name); 2311553Srgrimes if (*ep != '\0') 2321553Srgrimes errx(1, "%s: illegal %s name", name, type); 2331553Srgrimes return (val); 2341553Srgrimes} 2351553Srgrimes 2361553Srgrimesvoid 2371553Srgrimeschownerr(file) 2381553Srgrimes char *file; 2391553Srgrimes{ 2401553Srgrimes static int euid = -1, ngroups = -1; 2411553Srgrimes int groups[NGROUPS]; 2421553Srgrimes 2431553Srgrimes /* Check for chown without being root. */ 2441553Srgrimes if (errno != EPERM || 2451553Srgrimes uid != -1 && euid == -1 && (euid = geteuid()) != 0) { 2461553Srgrimes if (fflag) 2471553Srgrimes exit(0); 2481553Srgrimes err(1, "%s", file); 2491553Srgrimes } 2501553Srgrimes 2511553Srgrimes /* Check group membership; kernel just returns EPERM. */ 2521553Srgrimes if (gid != -1 && ngroups == -1) { 2531553Srgrimes ngroups = getgroups(NGROUPS, groups); 2541553Srgrimes while (--ngroups >= 0 && gid != groups[ngroups]); 2551553Srgrimes if (ngroups < 0) { 2561553Srgrimes if (fflag) 2571553Srgrimes exit(0); 2581553Srgrimes errx(1, "you are not a member of group %s", gname); 2591553Srgrimes } 2601553Srgrimes } 2611553Srgrimes 2621553Srgrimes if (!fflag) 2631553Srgrimes warn("%s", file); 2641553Srgrimes} 2651553Srgrimes 2661553Srgrimesvoid 2671553Srgrimesusage() 2681553Srgrimes{ 2691553Srgrimes (void)fprintf(stderr, 2701553Srgrimes "usage: %s [-R [-H | -L | -P]] [-f] %s file ...\n", 2711553Srgrimes myname, ischown ? "[owner][:group]" : "group"); 2721553Srgrimes exit(1); 2731553Srgrimes} 274