]> git.saurik.com Git - apple/file_cmds.git/blame - chown/chown.c
file_cmds-321.100.10.0.1.tar.gz
[apple/file_cmds.git] / chown / chown.c
CommitLineData
44a7a5ab
A
1/*
2 * Copyright (c) 1988, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
00337e45 34#include <sys/cdefs.h>
44a7a5ab 35#ifndef lint
00337e45 36__used static const char copyright[] =
6c780a1f
A
37"@(#) Copyright (c) 1988, 1993, 1994\n\
38 The Regents of the University of California. All rights reserved.\n";
44a7a5ab
A
39#endif /* not lint */
40
41#ifndef lint
42#if 0
43static char sccsid[] = "@(#)chown.c 8.8 (Berkeley) 4/4/94";
44a7a5ab
A
44#endif
45#endif /* not lint */
46
6c780a1f
A
47#include <sys/cdefs.h>
48__RCSID("$FreeBSD: src/usr.sbin/chown/chown.c,v 1.24 2002/07/17 16:22:24 dwmalone Exp $");
49
44a7a5ab
A
50#include <sys/param.h>
51#include <sys/stat.h>
52
44a7a5ab
A
53#include <err.h>
54#include <errno.h>
44a7a5ab
A
55#include <fts.h>
56#include <grp.h>
57#include <pwd.h>
58#include <stdio.h>
59#include <stdlib.h>
60#include <string.h>
61#include <unistd.h>
864a4b6e
A
62#include <sys/time.h>
63
64#ifdef __APPLE__
65#include <get_compat.h>
66#else
67#define COMPAT_MODE(a,b) (1)
68#endif /* __APPLE__ */
44a7a5ab 69
6c780a1f
A
70void a_gid(const char *);
71void a_uid(const char *);
72void chownerr(const char *);
4d0bb651 73static uid_t id(const char *, const char *);
6c780a1f 74void usage(void);
44a7a5ab
A
75
76uid_t uid;
77gid_t gid;
6c780a1f 78int ischown;
48fce603
A
79#ifdef __APPLE__
80int isnumeric = 0;
81#endif
6c780a1f 82const char *gname;
44a7a5ab
A
83
84int
6c780a1f 85main(int argc, char **argv)
44a7a5ab
A
86{
87 FTS *ftsp;
88 FTSENT *p;
40bf83fe 89 int Hflag, Lflag, Pflag, Rflag, fflag, hflag, vflag;
6c780a1f 90 int ch, fts_options, rval;
44a7a5ab 91 char *cp;
864a4b6e
A
92 int unix2003_compat = 0;
93 int symlink_found = 0;
44a7a5ab 94
864a4b6e
A
95 if (argc < 1)
96 usage();
6c780a1f
A
97 cp = strrchr(argv[0], '/');
98 cp = (cp != NULL) ? cp + 1 : argv[0];
99 ischown = (strcmp(cp, "chown") == 0);
100
40bf83fe 101 Hflag = Lflag = Pflag = Rflag = fflag = hflag = vflag = 0;
48fce603
A
102#ifdef __APPLE__
103 while ((ch = getopt(argc, argv, "HLPRfhnv")) != -1)
104#else
6c780a1f 105 while ((ch = getopt(argc, argv, "HLPRfhv")) != -1)
48fce603 106#endif
44a7a5ab
A
107 switch (ch) {
108 case 'H':
109 Hflag = 1;
40bf83fe 110 Lflag = Pflag = 0;
44a7a5ab
A
111 break;
112 case 'L':
113 Lflag = 1;
40bf83fe 114 Hflag = Pflag = 0;
44a7a5ab
A
115 break;
116 case 'P':
40bf83fe 117 Pflag = 1;
44a7a5ab
A
118 Hflag = Lflag = 0;
119 break;
120 case 'R':
121 Rflag = 1;
122 break;
123 case 'f':
124 fflag = 1;
125 break;
126 case 'h':
44a7a5ab 127 hflag = 1;
f383e97b 128 break;
48fce603
A
129#ifdef __APPLE__
130 case 'n':
131 isnumeric = 1;
132 break;
133#endif
6c780a1f
A
134 case 'v':
135 vflag = 1;
136 break;
44a7a5ab
A
137 case '?':
138 default:
139 usage();
140 }
141 argv += optind;
142 argc -= optind;
143
144 if (argc < 2)
145 usage();
40bf83fe
A
146 if (!Rflag && (Hflag || Lflag || Pflag))
147 warnx("options -H, -L, -P only useful with -R");
44a7a5ab 148
44a7a5ab 149 if (Rflag) {
6c780a1f
A
150 fts_options = FTS_PHYSICAL;
151 if (hflag && (Hflag || Lflag))
152 errx(1, "the -R%c and -h options may not be "
153 "specified together", Hflag ? 'H' : 'L');
44a7a5ab
A
154 if (Hflag)
155 fts_options |= FTS_COMFOLLOW;
6c780a1f 156 else if (Lflag) {
44a7a5ab
A
157 fts_options &= ~FTS_PHYSICAL;
158 fts_options |= FTS_LOGICAL;
159 }
6c780a1f
A
160 } else
161 fts_options = hflag ? FTS_PHYSICAL : FTS_LOGICAL;
44a7a5ab 162
6c780a1f
A
163 uid = (uid_t)-1;
164 gid = (gid_t)-1;
44a7a5ab 165 if (ischown) {
864a4b6e 166 unix2003_compat = COMPAT_MODE("bin/chown", "Unix2003");
440bd198 167 if ((cp = strchr(*argv, ':')) != NULL) {
44a7a5ab
A
168 *cp++ = '\0';
169 a_gid(cp);
440bd198
A
170 }
171#ifdef SUPPORT_DOT
172 else if ((cp = strchr(*argv, '.')) != NULL) {
6c780a1f 173 warnx("separation of user and group with a period is deprecated");
44a7a5ab
A
174 *cp++ = '\0';
175 a_gid(cp);
440bd198
A
176 }
177#endif
44a7a5ab 178 a_uid(*argv);
864a4b6e
A
179 } else {
180 unix2003_compat = COMPAT_MODE("bin/chgrp", "Unix2003");
44a7a5ab 181 a_gid(*argv);
864a4b6e 182 }
44a7a5ab
A
183
184 if ((ftsp = fts_open(++argv, fts_options, 0)) == NULL)
6c780a1f 185 err(1, NULL);
44a7a5ab
A
186
187 for (rval = 0; (p = fts_read(ftsp)) != NULL;) {
864a4b6e 188 symlink_found = 0;
44a7a5ab 189 switch (p->fts_info) {
6c780a1f
A
190 case FTS_D: /* Change it at FTS_DP. */
191 if (!Rflag)
44a7a5ab
A
192 fts_set(ftsp, p, FTS_SKIP);
193 continue;
6c780a1f 194 case FTS_DNR: /* Warn, chown. */
44a7a5ab
A
195 warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
196 rval = 1;
197 break;
198 case FTS_ERR: /* Warn, continue. */
199 case FTS_NS:
200 warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
201 rval = 1;
202 continue;
6c780a1f 203 case FTS_SL:
44a7a5ab
A
204 case FTS_SLNONE:
205 /*
206 * The only symlinks that end up here are ones that
207 * don't point to anything and ones that we found
208 * doing a physical walk.
209 */
6c780a1f
A
210 if (hflag)
211 break;
864a4b6e
A
212 else {
213 symlink_found = 1;
214 if (unix2003_compat) {
215 if (Hflag || Lflag) { /* -H or -L was specified */
216 if (p->fts_errno) {
217 warnx("%s: %s", p->fts_name, strerror(p->fts_errno));
218 rval = 1;
219 continue;
220 }
221 }
222 break; /* Otherwise symlinks keep going */
223 }
6c780a1f 224 continue;
864a4b6e 225 }
44a7a5ab
A
226 default:
227 break;
228 }
864a4b6e
A
229 if (unix2003_compat) {
230 /* Can only avoid updating times if both uid and gid are -1 */
231 if ((uid == (uid_t)-1) && (gid == (gid_t)-1))
232 continue;
233 } else {
234 if ((uid == (uid_t)-1 || uid == p->fts_statp->st_uid) &&
235 (gid == (gid_t)-1 || gid == p->fts_statp->st_gid))
236 continue;
237 }
238 if (((hflag || symlink_found) ? lchown : chown)(p->fts_accpath, uid, gid) == -1) {
6c780a1f
A
239 if (!fflag) {
240 chownerr(p->fts_path);
241 rval = 1;
242 }
243 } else {
244 if (vflag)
245 printf("%s\n", p->fts_path);
44a7a5ab
A
246 }
247 }
248 if (errno)
249 err(1, "fts_read");
250 exit(rval);
251}
252
253void
6c780a1f 254a_gid(const char *s)
44a7a5ab
A
255{
256 struct group *gr;
257
258 if (*s == '\0') /* Argument was "uid[:.]". */
259 return;
260 gname = s;
48fce603
A
261#ifdef __APPLE__
262 gid = (!isnumeric && ((gr = getgrnam(s)) != NULL)) ? gr->gr_gid : id(s, "group");
263#else
6c780a1f 264 gid = ((gr = getgrnam(s)) != NULL) ? gr->gr_gid : id(s, "group");
48fce603 265#endif
44a7a5ab
A
266}
267
268void
6c780a1f 269a_uid(const char *s)
44a7a5ab
A
270{
271 struct passwd *pw;
272
273 if (*s == '\0') /* Argument was "[:.]gid". */
274 return;
48fce603
A
275#ifdef __APPLE__
276 uid = (!isnumeric && ((pw = getpwnam(s)) != NULL)) ? pw->pw_uid : id(s, "user");
277#else
6c780a1f 278 uid = ((pw = getpwnam(s)) != NULL) ? pw->pw_uid : id(s, "user");
48fce603 279#endif
44a7a5ab
A
280}
281
4d0bb651 282static uid_t
6c780a1f 283id(const char *name, const char *type)
44a7a5ab 284{
4d0bb651 285 unsigned long val;
44a7a5ab
A
286 char *ep;
287
44a7a5ab
A
288 errno = 0;
289 val = strtoul(name, &ep, 10);
4d0bb651 290 if (errno || *ep != '\0' || val > UID_MAX)
6c780a1f 291 errx(1, "%s: illegal %s name", name, type);
4d0bb651 292 return (uid_t)val;
44a7a5ab
A
293}
294
295void
6c780a1f 296chownerr(const char *file)
44a7a5ab 297{
6c780a1f
A
298 static uid_t euid = -1;
299 static int ngroups = -1;
300 gid_t groups[NGROUPS_MAX];
301
302 /* Check for chown without being root. */
303 if (errno != EPERM || (uid != (uid_t)-1 &&
304 euid == (uid_t)-1 && (euid = geteuid()) != 0)) {
305 warn("%s", file);
306 return;
307 }
308
309 /* Check group membership; kernel just returns EPERM. */
310 if (gid != (gid_t)-1 && ngroups == -1 &&
311 euid == (uid_t)-1 && (euid = geteuid()) != 0) {
312 ngroups = getgroups(NGROUPS_MAX, groups);
313 while (--ngroups >= 0 && gid != groups[ngroups]);
314 if (ngroups < 0) {
315 warnx("you are not a member of group %s", gname);
316 return;
317 }
318 }
319 warn("%s", file);
320}
321
322void
323usage(void)
324{
325
326 if (ischown)
327 (void)fprintf(stderr, "%s\n%s\n",
48fce603
A
328#ifdef __APPLE__
329 "usage: chown [-fhnv] [-R [-H | -L | -P]] owner[:group]"
330 " file ...",
331 " chown [-fhnv] [-R [-H | -L | -P]] :group file ...");
332#else
6c780a1f
A
333 "usage: chown [-fhv] [-R [-H | -L | -P]] owner[:group]"
334 " file ...",
335 " chown [-fhv] [-R [-H | -L | -P]] :group file ...");
48fce603 336#endif
6c780a1f
A
337 else
338 (void)fprintf(stderr, "%s\n",
48fce603
A
339#ifdef __APPLE__
340 "usage: chgrp [-fhnv] [-R [-H | -L | -P]] group file ...");
341#else
6c780a1f 342 "usage: chgrp [-fhv] [-R [-H | -L | -P]] group file ...");
48fce603 343#endif
44a7a5ab
A
344 exit(1);
345}