chown.c revision 114601
10SN/A/*
22509Sihse * Copyright (c) 1988, 1993, 1994
30SN/A *	The Regents of the University of California.  All rights reserved.
40SN/A *
50SN/A * Redistribution and use in source and binary forms, with or without
60SN/A * modification, are permitted provided that the following conditions
7180SN/A * are met:
80SN/A * 1. Redistributions of source code must retain the above copyright
9180SN/A *    notice, this list of conditions and the following disclaimer.
100SN/A * 2. Redistributions in binary form must reproduce the above copyright
110SN/A *    notice, this list of conditions and the following disclaimer in the
120SN/A *    documentation and/or other materials provided with the distribution.
130SN/A * 3. All advertising materials mentioning features or use of this software
140SN/A *    must display the following acknowledgement:
150SN/A *	This product includes software developed by the University of
160SN/A *	California, Berkeley and its contributors.
170SN/A * 4. Neither the name of the University nor the names of its contributors
180SN/A *    may be used to endorse or promote products derived from this software
190SN/A *    without specific prior written permission.
200SN/A *
21180SN/A * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22180SN/A * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23180SN/A * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
240SN/A * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
250SN/A * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261410Sihse * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271410Sihse * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281410Sihse * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291410Sihse * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301410Sihse * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311410Sihse * SUCH DAMAGE.
32910SN/A */
33910SN/A
341410Sihse#if 0
355SN/A#ifndef lint
36910SN/Astatic const char copyright[] =
371410Sihse"@(#) Copyright (c) 1988, 1993, 1994\n\
38910SN/A	The Regents of the University of California.  All rights reserved.\n";
39338SN/A#endif /* not lint */
401426Sihse
411410Sihse#ifndef lint
421426Sihsestatic char sccsid[] = "@(#)chown.c	8.8 (Berkeley) 4/4/94";
431410Sihse#endif /* not lint */
441410Sihse#endif
451426Sihse#include <sys/cdefs.h>
461426Sihse__FBSDID("$FreeBSD: head/usr.sbin/chown/chown.c 114601 2003-05-03 21:06:42Z obrien $");
47311SN/A
481426Sihse#include <sys/param.h>
491426Sihse#include <sys/stat.h>
501426Sihse
511410Sihse#include <err.h>
521426Sihse#include <errno.h>
531651Sihse#include <fts.h>
542509Sihse#include <grp.h>
551120SN/A#include <libgen.h>
561410Sihse#include <pwd.h>
571410Sihse#include <stdint.h>
581410Sihse#include <stdio.h>
591426Sihse#include <stdlib.h>
601426Sihse#include <string.h>
611426Sihse#include <unistd.h>
621426Sihse
631426Sihsevoid	a_gid(const char *);
641426Sihsevoid	a_uid(const char *);
651426Sihsevoid	chownerr(const char *);
661426Sihseu_long	id(const char *, const char *);
671426Sihsevoid	usage(void);
681426Sihse
692475Sihseuid_t uid;
701426Sihsegid_t gid;
711426Sihseint ischown;
721426Sihseconst char *gname;
731426Sihse
741426Sihseint
751426Sihsemain(int argc, char **argv)
761426Sihse{
771426Sihse	FTS *ftsp;
781426Sihse	FTSENT *p;
791426Sihse	int Hflag, Lflag, Rflag, fflag, hflag, vflag;
801426Sihse	int ch, fts_options, rval;
811426Sihse	char *cp;
821426Sihse
831426Sihse	ischown = (strcmp(basename(argv[0]), "chown") == 0);
841426Sihse
851426Sihse	Hflag = Lflag = Rflag = fflag = hflag = vflag = 0;
861426Sihse	while ((ch = getopt(argc, argv, "HLPRfhv")) != -1)
871426Sihse		switch (ch) {
881426Sihse		case 'H':
891426Sihse			Hflag = 1;
901426Sihse			Lflag = 0;
911426Sihse			break;
921426Sihse		case 'L':
931426Sihse			Lflag = 1;
941426Sihse			Hflag = 0;
951426Sihse			break;
961426Sihse		case 'P':
971426Sihse			Hflag = Lflag = 0;
981426Sihse			break;
991426Sihse		case 'R':
1001426Sihse			Rflag = 1;
1011426Sihse			break;
1021426Sihse		case 'f':
1031426Sihse			fflag = 1;
1041426Sihse			break;
1051426Sihse		case 'h':
1061426Sihse			hflag = 1;
1071426Sihse			break;
1081426Sihse		case 'v':
1091426Sihse			vflag++;
1101426Sihse			break;
1111426Sihse		case '?':
1121426Sihse		default:
1131426Sihse			usage();
1141426Sihse		}
1151426Sihse	argv += optind;
1161426Sihse	argc -= optind;
1171426Sihse
1181426Sihse	if (argc < 2)
1191426Sihse		usage();
1201426Sihse
1211426Sihse	if (Rflag) {
1221426Sihse		fts_options = FTS_PHYSICAL;
1231426Sihse		if (hflag && (Hflag || Lflag))
1241410Sihse			errx(1, "the -R%c and -h options may not be "
1251120SN/A			    "specified together", Hflag ? 'H' : 'L');
1261426Sihse		if (Hflag)
1271426Sihse			fts_options |= FTS_COMFOLLOW;
1281426Sihse		else if (Lflag) {
1291426Sihse			fts_options &= ~FTS_PHYSICAL;
1301426Sihse			fts_options |= FTS_LOGICAL;
1311426Sihse		}
1321426Sihse	} else
1331410Sihse		fts_options = hflag ? FTS_PHYSICAL : FTS_LOGICAL;
1341426Sihse
1351426Sihse	uid = (uid_t)-1;
1361426Sihse	gid = (gid_t)-1;
13727SN/A	if (ischown) {
1381410Sihse		if ((cp = strchr(*argv, ':')) != NULL) {
1391410Sihse			*cp++ = '\0';
1401701Sihse			a_gid(cp);
1411701Sihse		}
1421701Sihse#ifdef SUPPORT_DOT
1431410Sihse		else if ((cp = strchr(*argv, '.')) != NULL) {
1441410Sihse			warnx("separation of user and group with a period is deprecated");
1451410Sihse			*cp++ = '\0';
1461410Sihse			a_gid(cp);
1471410Sihse		}
1481410Sihse#endif
1491410Sihse		a_uid(*argv);
1501426Sihse	} else
1511426Sihse		a_gid(*argv);
1521410Sihse
1531410Sihse	if ((ftsp = fts_open(++argv, fts_options, 0)) == NULL)
1541410Sihse		err(1, NULL);
1551156SN/A
1561120SN/A	for (rval = 0; (p = fts_read(ftsp)) != NULL;) {
1571936Sihse		switch (p->fts_info) {
1581936Sihse		case FTS_D:			/* Change it at FTS_DP. */
1591936Sihse			if (!Rflag)
1602360Sihse				fts_set(ftsp, p, FTS_SKIP);
1611426Sihse			continue;
1621426Sihse		case FTS_DNR:			/* Warn, chown. */
1631426Sihse			warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
1641410Sihse			rval = 1;
16527SN/A			break;
1661426Sihse		case FTS_ERR:			/* Warn, continue. */
1671426Sihse		case FTS_NS:
1681426Sihse			warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
1691426Sihse			rval = 1;
1701426Sihse			continue;
1711426Sihse		case FTS_SL:
1721651Sihse		case FTS_SLNONE:
1731651Sihse			/*
1741915Sihse			 * The only symlinks that end up here are ones that
1751651Sihse			 * don't point to anything and ones that we found
1761651Sihse			 * doing a physical walk.
1771651Sihse			 */
1781651Sihse			if (hflag)
1791426Sihse				break;
1801426Sihse			else
1811426Sihse				continue;
1821426Sihse		default:
1831651Sihse			break;
1841426Sihse		}
1851410Sihse		if ((uid == (uid_t)-1 || uid == p->fts_statp->st_uid) &&
1861410Sihse		    (gid == (gid_t)-1 || gid == p->fts_statp->st_gid))
1871410Sihse			continue;
1881651Sihse		if ((hflag ? lchown : chown)(p->fts_accpath, uid, gid) == -1) {
1891410Sihse			if (!fflag) {
1901426Sihse				chownerr(p->fts_path);
1911651Sihse				rval = 1;
1921651Sihse			}
1931651Sihse		} else {
1941651Sihse			if (vflag) {
1951651Sihse				printf("%s", p->fts_path);
1961651Sihse				if (vflag > 1) {
1971651Sihse					if (ischown) {
1981651Sihse						printf(": %ju:%ju -> %ju:%ju",
1991651Sihse						    (uintmax_t)
2001651Sihse						    p->fts_statp->st_uid,
2011651Sihse						    (uintmax_t)
2021651Sihse						    p->fts_statp->st_gid,
2031651Sihse						    (uid == (uid_t)-1) ?
2041651Sihse						    (uintmax_t)
2051410Sihse						    p->fts_statp->st_uid :
2061410Sihse						    (uintmax_t)uid,
2071410Sihse						    (gid == (gid_t)-1) ?
2081410Sihse						    (uintmax_t)
2090SN/A						    p->fts_statp->st_gid :
2101426Sihse						    (uintmax_t)gid);
2111410Sihse					} else {
2121410Sihse						printf(": %ju -> %ju",
2131410Sihse						    (uintmax_t)
2141410Sihse						    p->fts_statp->st_gid,
2151410Sihse						    (gid == (gid_t)-1) ?
2161410Sihse						    (uintmax_t)
2171410Sihse						    p->fts_statp->st_gid :
2181410Sihse						    (uintmax_t)gid);
2191410Sihse					}
2201426Sihse				}
2211426Sihse				printf("\n");
2221426Sihse			}
2231426Sihse		}
2241426Sihse	}
2251426Sihse	if (errno)
2261426Sihse		err(1, "fts_read");
2271426Sihse	exit(rval);
2281651Sihse}
2291651Sihse
2301651Sihsevoid
2311410Sihsea_gid(const char *s)
2321862Serikj{
2331862Serikj	struct group *gr;
2341410Sihse
2351410Sihse	if (*s == '\0')			/* Argument was "uid[:.]". */
2361410Sihse		return;
2371410Sihse	gname = s;
2381410Sihse	gid = ((gr = getgrnam(s)) != NULL) ? gr->gr_gid : id(s, "group");
2391410Sihse}
2401426Sihse
2411426Sihsevoid
2421426Sihsea_uid(const char *s)
2431426Sihse{
2441426Sihse	struct passwd *pw;
2451426Sihse
2461426Sihse	if (*s == '\0')			/* Argument was "[:.]gid". */
2471426Sihse		return;
2481426Sihse	uid = ((pw = getpwnam(s)) != NULL) ? pw->pw_uid : id(s, "user");
2491426Sihse}
2501426Sihse
2511426Sihseu_long
2521426Sihseid(const char *name, const char *type)
2531327SN/A{
2542509Sihse	u_long val;
2552509Sihse	char *ep;
2562509Sihse
2572509Sihse	/*
2582509Sihse	 * XXX
2591628Sihse	 * We know that uid_t's and gid_t's are unsigned longs.
2601628Sihse	 */
2611628Sihse	errno = 0;
2621410Sihse	val = strtoul(name, &ep, 10);
2631410Sihse	if (errno)
2641410Sihse		err(1, "%s", name);
2651410Sihse	if (*ep != '\0')
2661410Sihse		errx(1, "%s: illegal %s name", name, type);
2671410Sihse	return (val);
2681410Sihse}
2691410Sihse
2701410Sihsevoid
2711426Sihsechownerr(const char *file)
2721426Sihse{
2731426Sihse	static uid_t euid = -1;
2741410Sihse	static int ngroups = -1;
2751651Sihse	gid_t groups[NGROUPS_MAX];
2762034Serikj
2772034Serikj	/* Check for chown without being root. */
2782034Serikj	if (errno != EPERM || (uid != (uid_t)-1 &&
2792034Serikj	    euid == (uid_t)-1 && (euid = geteuid()) != 0)) {
2802034Serikj		warn("%s", file);
2811426Sihse		return;
2822034Serikj	}
2831410Sihse
2841410Sihse	/* Check group membership; kernel just returns EPERM. */
2851410Sihse	if (gid != (gid_t)-1 && ngroups == -1 &&
2862360Sihse	    euid == (uid_t)-1 && (euid = geteuid()) != 0) {
2871410Sihse		ngroups = getgroups(NGROUPS_MAX, groups);
2880SN/A		while (--ngroups >= 0 && gid != groups[ngroups]);
2891426Sihse		if (ngroups < 0) {
2901426Sihse			warnx("you are not a member of group %s", gname);
2911426Sihse			return;
2921623Sihse		}
2931862Serikj	}
2941426Sihse	warn("%s", file);
2951426Sihse}
2961862Serikj
2971426Sihsevoid
2981426Sihseusage(void)
2991426Sihse{
3001426Sihse
3011426Sihse	if (ischown)
3021426Sihse		(void)fprintf(stderr, "%s\n%s\n",
3031426Sihse		    "usage: chown [-fhv] [-R [-H | -L | -P]] owner[:group]"
3041426Sihse		    " file ...",
3051862Serikj		    "       chown [-fhv] [-R [-H | -L | -P]] :group file ...");
3061623Sihse	else
3071862Serikj		(void)fprintf(stderr, "%s\n",
3081862Serikj		    "usage: chgrp [-fhv] [-R [-H | -L | -P]] group file ...");
3091862Serikj	exit(1);
3101862Serikj}
3111623Sihse