2 * Copyright (c) 1988, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 static const char copyright[] =
33 "@(#) Copyright (c) 1988, 1993, 1994\n\
34 The Regents of the University of California. All rights reserved.\n";
38 static char sccsid[] = "@(#)chown.c 8.8 (Berkeley) 4/4/94";
42 #include <sys/cdefs.h>
43 __FBSDID("$FreeBSD$");
45 #include <sys/param.h>
62 static void a_gid(const char *);
63 static void a_uid(const char *);
64 static void chownerr(const char *);
65 static uid_t id(const char *, const char *);
66 static void usage(void);
67 static void print_info(const FTSENT *, int);
72 static const char *gname;
73 static volatile sig_atomic_t siginfo;
76 siginfo_handler(int sig __unused)
83 main(int argc, char **argv)
87 int Hflag, Lflag, Rflag, fflag, hflag, vflag, xflag;
88 int ch, fts_options, rval;
91 ischown = (strcmp(basename(argv[0]), "chown") == 0);
93 Hflag = Lflag = Rflag = fflag = hflag = vflag = xflag = 0;
94 while ((ch = getopt(argc, argv, "HLPRfhvx")) != -1)
132 (void)signal(SIGINFO, siginfo_handler);
135 if (hflag && (Hflag || Lflag))
136 errx(1, "the -R%c and -h options may not be "
137 "specified together", Hflag ? 'H' : 'L');
139 fts_options = FTS_LOGICAL;
141 fts_options = FTS_PHYSICAL;
144 fts_options |= FTS_COMFOLLOW;
148 fts_options = FTS_PHYSICAL;
150 fts_options = FTS_LOGICAL;
154 fts_options |= FTS_XDEV;
159 if ((cp = strchr(*argv, ':')) != NULL) {
164 else if ((cp = strchr(*argv, '.')) != NULL) {
165 warnx("separation of user and group with a period is deprecated");
174 if ((ftsp = fts_open(++argv, fts_options, NULL)) == NULL)
177 for (rval = 0; (p = fts_read(ftsp)) != NULL;) {
180 if ((fts_options & FTS_LOGICAL) ||
181 ((fts_options & FTS_COMFOLLOW) &&
182 p->fts_level == FTS_ROOTLEVEL))
185 atflag = AT_SYMLINK_NOFOLLOW;
187 switch (p->fts_info) {
188 case FTS_D: /* Change it at FTS_DP. */
190 fts_set(ftsp, p, FTS_SKIP);
192 case FTS_DNR: /* Warn, chown. */
193 warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
196 case FTS_ERR: /* Warn, continue. */
198 warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
208 if ((uid == (uid_t)-1 || uid == p->fts_statp->st_uid) &&
209 (gid == (gid_t)-1 || gid == p->fts_statp->st_gid))
211 if (fchownat(AT_FDCWD, p->fts_accpath, uid, gid, atflag)
213 chownerr(p->fts_path);
216 print_info(p, vflag);
228 if (*s == '\0') /* Argument was "uid[:.]". */
231 gid = ((gr = getgrnam(s)) != NULL) ? gr->gr_gid : id(s, "group");
239 if (*s == '\0') /* Argument was "[:.]gid". */
241 uid = ((pw = getpwnam(s)) != NULL) ? pw->pw_uid : id(s, "user");
245 id(const char *name, const char *type)
252 * We know that uid_t's and gid_t's are unsigned longs.
255 val = strtoul(name, &ep, 10);
256 if (errno || *ep != '\0')
257 errx(1, "%s: illegal %s name", name, type);
262 chownerr(const char *file)
264 static uid_t euid = -1;
265 static int ngroups = -1;
266 static long ngroups_max;
269 /* Check for chown without being root. */
270 if (errno != EPERM || (uid != (uid_t)-1 &&
271 euid == (uid_t)-1 && (euid = geteuid()) != 0)) {
276 /* Check group membership; kernel just returns EPERM. */
277 if (gid != (gid_t)-1 && ngroups == -1 &&
278 euid == (uid_t)-1 && (euid = geteuid()) != 0) {
279 ngroups_max = sysconf(_SC_NGROUPS_MAX) + 1;
280 if ((groups = malloc(sizeof(gid_t) * ngroups_max)) == NULL)
282 ngroups = getgroups(ngroups_max, groups);
283 while (--ngroups >= 0 && gid != groups[ngroups]);
286 warnx("you are not a member of group %s", gname);
298 (void)fprintf(stderr, "%s\n%s\n",
299 "usage: chown [-fhvx] [-R [-H | -L | -P]] owner[:group]"
301 " chown [-fhvx] [-R [-H | -L | -P]] :group file ...");
303 (void)fprintf(stderr, "%s\n",
304 "usage: chgrp [-fhvx] [-R [-H | -L | -P]] group file ...");
309 print_info(const FTSENT *p, int vflag)
312 printf("%s", p->fts_path);
315 printf(": %ju:%ju -> %ju:%ju",
316 (uintmax_t)p->fts_statp->st_uid,
317 (uintmax_t)p->fts_statp->st_gid,
319 (uintmax_t)p->fts_statp->st_uid : (uintmax_t)uid,
321 (uintmax_t)p->fts_statp->st_gid : (uintmax_t)gid);
323 printf(": %ju -> %ju", (uintmax_t)p->fts_statp->st_gid,
325 (uintmax_t)p->fts_statp->st_gid : (uintmax_t)gid);