chown.c revision 77522
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 77522 2001-05-31 11:47:20Z ru $";
4628643Ssteve#endif
471553Srgrimes#endif /* not lint */
481553Srgrimes
491553Srgrimes#include <sys/param.h>
501553Srgrimes#include <sys/stat.h>
511553Srgrimes
521553Srgrimes#include <ctype.h>
531553Srgrimes#include <dirent.h>
541553Srgrimes#include <err.h>
551553Srgrimes#include <errno.h>
561553Srgrimes#include <fts.h>
571553Srgrimes#include <grp.h>
581553Srgrimes#include <pwd.h>
591553Srgrimes#include <stdio.h>
601553Srgrimes#include <stdlib.h>
611553Srgrimes#include <string.h>
621553Srgrimes#include <unistd.h>
631553Srgrimes
641553Srgrimesvoid	a_gid __P((char *));
651553Srgrimesvoid	a_uid __P((char *));
661553Srgrimesvoid	chownerr __P((char *));
671553Srgrimesu_long	id __P((char *, char *));
681553Srgrimesvoid	usage __P((void));
691553Srgrimes
701553Srgrimesuid_t uid;
711553Srgrimesgid_t gid;
7253780Sobrienint Rflag, ischown, fflag, hflag, vflag;
731553Srgrimeschar *gname, *myname;
741553Srgrimes
751553Srgrimesint
761553Srgrimesmain(argc, argv)
771553Srgrimes	int argc;
781553Srgrimes	char *argv[];
791553Srgrimes{
801553Srgrimes	FTS *ftsp;
811553Srgrimes	FTSENT *p;
821553Srgrimes	int Hflag, Lflag, Pflag, ch, fts_options, hflag, rval;
831553Srgrimes	char *cp;
848857Srgrimes
851553Srgrimes	myname = (cp = rindex(*argv, '/')) ? cp + 1 : *argv;
861553Srgrimes	ischown = myname[2] == 'o';
878857Srgrimes
8864014Speter	Hflag = Lflag = Pflag = hflag = vflag = 0;
8957830Sobrien	while ((ch = getopt(argc, argv, "HLPRfhv")) != -1)
901553Srgrimes		switch (ch) {
911553Srgrimes		case 'H':
921553Srgrimes			Hflag = 1;
931553Srgrimes			Lflag = Pflag = 0;
941553Srgrimes			break;
951553Srgrimes		case 'L':
961553Srgrimes			Lflag = 1;
971553Srgrimes			Hflag = Pflag = 0;
981553Srgrimes			break;
991553Srgrimes		case 'P':
1001553Srgrimes			Pflag = 1;
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;
12734399Sjkh		if (hflag && (Lflag || Hflag))
12829105Scharnier			errx(1, "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		}
13577333Sru	} else
13677522Sru		fts_options = hflag ? FTS_PHYSICAL : FTS_LOGICAL;
1371553Srgrimes
1381553Srgrimes	uid = gid = -1;
1391553Srgrimes	if (ischown) {
14029656Swosch		if ((cp = strchr(*argv, ':')) != NULL) {
1411553Srgrimes			*cp++ = '\0';
1421553Srgrimes			a_gid(cp);
14329656Swosch		}
14429656Swosch#ifdef SUPPORT_DOT
14529656Swosch		else if ((cp = strchr(*argv, '.')) != NULL) {
1461553Srgrimes			*cp++ = '\0';
1471553Srgrimes			a_gid(cp);
1488857Srgrimes		}
14929656Swosch#endif
1501553Srgrimes		a_uid(*argv);
1518857Srgrimes	} else
1521553Srgrimes		a_gid(*argv);
1531553Srgrimes
1541553Srgrimes	if ((ftsp = fts_open(++argv, fts_options, 0)) == NULL)
1551553Srgrimes		err(1, NULL);
1561553Srgrimes
1571553Srgrimes	for (rval = 0; (p = fts_read(ftsp)) != NULL;) {
1581553Srgrimes		switch (p->fts_info) {
15917597Sadam		case FTS_D: 			/* Change it at FTS_DP. */
16017597Sadam			if (!Rflag)
16117597Sadam				fts_set(ftsp, p, FTS_SKIP);
16217597Sadam			continue;
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;
17235632Sbde		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			 */
17924446Speter			if (hflag)
18024446Speter				break;
18124446Speter			else
18224446Speter				continue;
1831553Srgrimes		default:
1841553Srgrimes			break;
1851553Srgrimes		}
18664014Speter		if ((uid == -1 || uid == p->fts_statp->st_uid) &&
18764014Speter		    (gid == -1 || gid == p->fts_statp->st_gid))
18864014Speter			continue;
18924446Speter		if (hflag) {
19024446Speter			if (lchown(p->fts_accpath, uid, gid) && !fflag) {
19124446Speter				chownerr(p->fts_path);
19224446Speter				rval = 1;
19353780Sobrien			} else {
19453780Sobrien			    	if (vflag)
19553780Sobrien					(void)printf("%s\n", p->fts_accpath);
19624446Speter			}
19724446Speter		} else {
19824446Speter			if (chown(p->fts_accpath, uid, gid) && !fflag) {
19924446Speter				chownerr(p->fts_path);
20024446Speter				rval = 1;
20153780Sobrien			} else {
20253780Sobrien			    	if (vflag)
20353780Sobrien					(void)printf("%s\n", p->fts_accpath);
20424446Speter			}
2051553Srgrimes		}
2061553Srgrimes	}
2071553Srgrimes	if (errno)
2081553Srgrimes		err(1, "fts_read");
2091553Srgrimes	exit(rval);
2101553Srgrimes}
2111553Srgrimes
2121553Srgrimesvoid
2131553Srgrimesa_gid(s)
2141553Srgrimes	char *s;
2151553Srgrimes{
2161553Srgrimes	struct group *gr;
2171553Srgrimes
2181553Srgrimes	if (*s == '\0')			/* Argument was "uid[:.]". */
2191553Srgrimes		return;
2201553Srgrimes	gname = s;
2211553Srgrimes	gid = ((gr = getgrnam(s)) == NULL) ? id(s, "group") : gr->gr_gid;
2221553Srgrimes}
2231553Srgrimes
2241553Srgrimesvoid
2251553Srgrimesa_uid(s)
2261553Srgrimes	char *s;
2271553Srgrimes{
2281553Srgrimes	struct passwd *pw;
2291553Srgrimes
2301553Srgrimes	if (*s == '\0')			/* Argument was "[:.]gid". */
2311553Srgrimes		return;
2321553Srgrimes	uid = ((pw = getpwnam(s)) == NULL) ? id(s, "user") : pw->pw_uid;
2331553Srgrimes}
2341553Srgrimes
2351553Srgrimesu_long
2361553Srgrimesid(name, type)
2371553Srgrimes	char *name, *type;
2381553Srgrimes{
2391553Srgrimes	u_long val;
2401553Srgrimes	char *ep;
2411553Srgrimes
2421553Srgrimes	/*
2431553Srgrimes	 * XXX
2441553Srgrimes	 * We know that uid_t's and gid_t's are unsigned longs.
2451553Srgrimes	 */
2461553Srgrimes	errno = 0;
2471553Srgrimes	val = strtoul(name, &ep, 10);
2481553Srgrimes	if (errno)
2491553Srgrimes		err(1, "%s", name);
2501553Srgrimes	if (*ep != '\0')
2511553Srgrimes		errx(1, "%s: illegal %s name", name, type);
2521553Srgrimes	return (val);
2531553Srgrimes}
2541553Srgrimes
2551553Srgrimesvoid
2561553Srgrimeschownerr(file)
2571553Srgrimes	char *file;
2581553Srgrimes{
2591553Srgrimes	static int euid = -1, ngroups = -1;
2603032Sdg	gid_t groups[NGROUPS];
2611553Srgrimes
2621553Srgrimes	/* Check for chown without being root. */
2631553Srgrimes	if (errno != EPERM ||
26428643Ssteve	    (uid != -1 && euid == -1 && (euid = geteuid()) != 0))
2651553Srgrimes		err(1, "%s", file);
2661553Srgrimes
2671553Srgrimes	/* Check group membership; kernel just returns EPERM. */
26828643Ssteve	if (gid != -1 && ngroups == -1 &&
26928643Ssteve	    euid == -1 && (euid = geteuid()) != 0) {
2701553Srgrimes		ngroups = getgroups(NGROUPS, groups);
2711553Srgrimes		while (--ngroups >= 0 && gid != groups[ngroups]);
27228643Ssteve		if (ngroups < 0)
2731553Srgrimes			errx(1, "you are not a member of group %s", gname);
2741553Srgrimes	}
27528643Ssteve	warn("%s", file);
2761553Srgrimes}
2771553Srgrimes
2781553Srgrimesvoid
2791553Srgrimesusage()
2801553Srgrimes{
28129105Scharnier	(void)fprintf(stderr, "%s\n%s\n%s\n",
28253780Sobrien	    "usage: chown [-R [-H | -L | -P]] [-f] [-h] [-v] owner[:group] file ...",
28353780Sobrien	    "       chown [-R [-H | -L | -P]] [-f] [-h] [-v] :group file ...",
28453780Sobrien	    "       chgrp [-R [-H | -L | -P]] [-f] [-h] [-v] group file ...");
2851553Srgrimes	exit(1);
2861553Srgrimes}
287