2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1988, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 static const char copyright[] =
35 "@(#) Copyright (c) 1988, 1993, 1994\n\
36 The Regents of the University of California. All rights reserved.\n";
40 static char sccsid[] = "@(#)chown.c 8.8 (Berkeley) 4/4/94";
44 #include <sys/cdefs.h>
45 __FBSDID("$FreeBSD$");
47 #include <sys/param.h>
64 static void a_gid(const char *);
65 static void a_uid(const char *);
66 static void chownerr(const char *);
67 static uid_t id(const char *, const char *);
68 static void usage(void);
69 static void print_info(const FTSENT *, int);
74 static const char *gname;
75 static volatile sig_atomic_t siginfo;
78 siginfo_handler(int sig __unused)
85 main(int argc, char **argv)
89 int Hflag, Lflag, Rflag, fflag, hflag, vflag, xflag;
90 int ch, fts_options, rval;
93 ischown = (strcmp(basename(argv[0]), "chown") == 0);
95 Hflag = Lflag = Rflag = fflag = hflag = vflag = xflag = 0;
96 while ((ch = getopt(argc, argv, "HLPRfhvx")) != -1)
134 (void)signal(SIGINFO, siginfo_handler);
137 if (hflag && (Hflag || Lflag))
138 errx(1, "the -R%c and -h options may not be "
139 "specified together", Hflag ? 'H' : 'L');
141 fts_options = FTS_LOGICAL;
143 fts_options = FTS_PHYSICAL;
146 fts_options |= FTS_COMFOLLOW;
150 fts_options = FTS_PHYSICAL;
152 fts_options = FTS_LOGICAL;
156 fts_options |= FTS_XDEV;
161 if ((cp = strchr(*argv, ':')) != NULL) {
166 else if ((cp = strchr(*argv, '.')) != NULL) {
167 warnx("separation of user and group with a period is deprecated");
176 if ((ftsp = fts_open(++argv, fts_options, NULL)) == NULL)
179 for (rval = 0; (p = fts_read(ftsp)) != NULL;) {
182 if ((fts_options & FTS_LOGICAL) ||
183 ((fts_options & FTS_COMFOLLOW) &&
184 p->fts_level == FTS_ROOTLEVEL))
187 atflag = AT_SYMLINK_NOFOLLOW;
189 switch (p->fts_info) {
190 case FTS_D: /* Change it at FTS_DP. */
192 fts_set(ftsp, p, FTS_SKIP);
194 case FTS_DNR: /* Warn, chown. */
195 warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
198 case FTS_ERR: /* Warn, continue. */
200 warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
210 if ((uid == (uid_t)-1 || uid == p->fts_statp->st_uid) &&
211 (gid == (gid_t)-1 || gid == p->fts_statp->st_gid))
213 if (fchownat(AT_FDCWD, p->fts_accpath, uid, gid, atflag)
215 chownerr(p->fts_path);
218 print_info(p, vflag);
230 if (*s == '\0') /* Argument was "uid[:.]". */
233 gid = ((gr = getgrnam(s)) != NULL) ? gr->gr_gid : id(s, "group");
241 if (*s == '\0') /* Argument was "[:.]gid". */
243 uid = ((pw = getpwnam(s)) != NULL) ? pw->pw_uid : id(s, "user");
247 id(const char *name, const char *type)
254 * We know that uid_t's and gid_t's are unsigned longs.
257 val = strtoul(name, &ep, 10);
258 if (errno || *ep != '\0')
259 errx(1, "%s: illegal %s name", name, type);
264 chownerr(const char *file)
266 static uid_t euid = -1;
267 static int ngroups = -1;
268 static long ngroups_max;
271 /* Check for chown without being root. */
272 if (errno != EPERM || (uid != (uid_t)-1 &&
273 euid == (uid_t)-1 && (euid = geteuid()) != 0)) {
278 /* Check group membership; kernel just returns EPERM. */
279 if (gid != (gid_t)-1 && ngroups == -1 &&
280 euid == (uid_t)-1 && (euid = geteuid()) != 0) {
281 ngroups_max = sysconf(_SC_NGROUPS_MAX) + 1;
282 if ((groups = malloc(sizeof(gid_t) * ngroups_max)) == NULL)
284 ngroups = getgroups(ngroups_max, groups);
285 while (--ngroups >= 0 && gid != groups[ngroups]);
288 warnx("you are not a member of group %s", gname);
300 (void)fprintf(stderr, "%s\n%s\n",
301 "usage: chown [-fhvx] [-R [-H | -L | -P]] owner[:group]"
303 " chown [-fhvx] [-R [-H | -L | -P]] :group file ...");
305 (void)fprintf(stderr, "%s\n",
306 "usage: chgrp [-fhvx] [-R [-H | -L | -P]] group file ...");
311 print_info(const FTSENT *p, int vflag)
314 printf("%s", p->fts_path);
317 printf(": %ju:%ju -> %ju:%ju",
318 (uintmax_t)p->fts_statp->st_uid,
319 (uintmax_t)p->fts_statp->st_gid,
321 (uintmax_t)p->fts_statp->st_uid : (uintmax_t)uid,
323 (uintmax_t)p->fts_statp->st_gid : (uintmax_t)gid);
325 printf(": %ju -> %ju", (uintmax_t)p->fts_statp->st_gid,
327 (uintmax_t)p->fts_statp->st_gid : (uintmax_t)gid);