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
32 #include <sys/param.h>
50 static void a_gid(const char *);
51 static void a_uid(const char *);
52 static void chownerr(const char *);
53 static uid_t id(const char *, const char *);
54 static void usage(void);
55 static void print_info(const FTSENT *, int);
60 static const char *gname;
61 static volatile sig_atomic_t siginfo;
64 siginfo_handler(int sig __unused)
71 main(int argc, char **argv)
75 int Hflag, Lflag, Rflag, fflag, hflag, vflag, xflag;
76 int ch, fts_options, rval;
79 ischown = (strcmp(basename(argv[0]), "chown") == 0);
81 Hflag = Lflag = Rflag = fflag = hflag = vflag = xflag = 0;
82 while ((ch = getopt(argc, argv, "HLPRfhvx")) != -1)
120 (void)signal(SIGINFO, siginfo_handler);
123 if (hflag && (Hflag || Lflag))
124 errx(1, "the -R%c and -h options may not be "
125 "specified together", Hflag ? 'H' : 'L');
127 fts_options = FTS_LOGICAL;
129 fts_options = FTS_PHYSICAL;
132 fts_options |= FTS_COMFOLLOW;
136 fts_options = FTS_PHYSICAL;
138 fts_options = FTS_LOGICAL;
142 fts_options |= FTS_XDEV;
147 if ((cp = strchr(*argv, ':')) != NULL) {
152 else if ((cp = strchr(*argv, '.')) != NULL) {
153 warnx("separation of user and group with a period is deprecated");
162 if ((ftsp = fts_open(++argv, fts_options, NULL)) == NULL)
165 for (rval = 0; errno = 0, (p = fts_read(ftsp)) != NULL;) {
168 if ((fts_options & FTS_LOGICAL) ||
169 ((fts_options & FTS_COMFOLLOW) &&
170 p->fts_level == FTS_ROOTLEVEL))
173 atflag = AT_SYMLINK_NOFOLLOW;
175 switch (p->fts_info) {
176 case FTS_D: /* Change it at FTS_DP. */
178 fts_set(ftsp, p, FTS_SKIP);
180 case FTS_DNR: /* Warn, chown. */
181 warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
184 case FTS_ERR: /* Warn, continue. */
186 warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
196 if ((uid == (uid_t)-1 || uid == p->fts_statp->st_uid) &&
197 (gid == (gid_t)-1 || gid == p->fts_statp->st_gid))
199 if (fchownat(AT_FDCWD, p->fts_accpath, uid, gid, atflag)
201 chownerr(p->fts_path);
204 print_info(p, vflag);
216 if (*s == '\0') /* Argument was "uid[:.]". */
219 gid = ((gr = getgrnam(s)) != NULL) ? gr->gr_gid : id(s, "group");
227 if (*s == '\0') /* Argument was "[:.]gid". */
229 uid = ((pw = getpwnam(s)) != NULL) ? pw->pw_uid : id(s, "user");
233 id(const char *name, const char *type)
239 val = strtoul(name, &ep, 10);
240 _Static_assert(UID_MAX >= GID_MAX, "UID MAX less than GID MAX");
241 if (errno || *ep != '\0' || val > UID_MAX)
242 errx(1, "%s: illegal %s name", name, type);
247 chownerr(const char *file)
249 static uid_t euid = -1;
250 static int ngroups = -1;
251 static long ngroups_max;
254 /* Check for chown without being root. */
255 if (errno != EPERM || (uid != (uid_t)-1 &&
256 euid == (uid_t)-1 && (euid = geteuid()) != 0)) {
261 /* Check group membership; kernel just returns EPERM. */
262 if (gid != (gid_t)-1 && ngroups == -1 &&
263 euid == (uid_t)-1 && (euid = geteuid()) != 0) {
264 ngroups_max = sysconf(_SC_NGROUPS_MAX) + 1;
265 if ((groups = malloc(sizeof(gid_t) * ngroups_max)) == NULL)
267 ngroups = getgroups(ngroups_max, groups);
268 while (--ngroups >= 0 && gid != groups[ngroups]);
271 warnx("you are not a member of group %s", gname);
283 (void)fprintf(stderr, "%s\n%s\n",
284 "usage: chown [-fhvx] [-R [-H | -L | -P]] owner[:group]"
286 " chown [-fhvx] [-R [-H | -L | -P]] :group file ...");
288 (void)fprintf(stderr, "%s\n",
289 "usage: chgrp [-fhvx] [-R [-H | -L | -P]] group file ...");
294 print_info(const FTSENT *p, int vflag)
297 printf("%s", p->fts_path);
300 printf(": %ju:%ju -> %ju:%ju",
301 (uintmax_t)p->fts_statp->st_uid,
302 (uintmax_t)p->fts_statp->st_gid,
304 (uintmax_t)p->fts_statp->st_uid : (uintmax_t)uid,
306 (uintmax_t)p->fts_statp->st_gid : (uintmax_t)gid);
308 printf(": %ju -> %ju", (uintmax_t)p->fts_statp->st_gid,
310 (uintmax_t)p->fts_statp->st_gid : (uintmax_t)gid);