file_cmds-272.250.1.tar.gz
[apple/file_cmds.git] / chown / chown.c
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
34 #include <sys/cdefs.h>
35 #ifndef lint
36 __used static const char copyright[] =
37 "@(#) Copyright (c) 1988, 1993, 1994\n\
38 The Regents of the University of California. All rights reserved.\n";
39 #endif /* not lint */
40
41 #ifndef lint
42 #if 0
43 static char sccsid[] = "@(#)chown.c 8.8 (Berkeley) 4/4/94";
44 #endif
45 #endif /* not lint */
46
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
50 #include <sys/param.h>
51 #include <sys/stat.h>
52
53 #include <err.h>
54 #include <errno.h>
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>
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__ */
69
70 void a_gid(const char *);
71 void a_uid(const char *);
72 void chownerr(const char *);
73 static uid_t id(const char *, const char *);
74 void usage(void);
75
76 uid_t uid;
77 gid_t gid;
78 int ischown;
79 const char *gname;
80
81 int
82 main(int argc, char **argv)
83 {
84 FTS *ftsp;
85 FTSENT *p;
86 int Hflag, Lflag, Pflag, Rflag, fflag, hflag, vflag;
87 int ch, fts_options, rval;
88 char *cp;
89 int unix2003_compat = 0;
90 int symlink_found = 0;
91
92 if (argc < 1)
93 usage();
94 cp = strrchr(argv[0], '/');
95 cp = (cp != NULL) ? cp + 1 : argv[0];
96 ischown = (strcmp(cp, "chown") == 0);
97
98 Hflag = Lflag = Pflag = Rflag = fflag = hflag = vflag = 0;
99 while ((ch = getopt(argc, argv, "HLPRfhv")) != -1)
100 switch (ch) {
101 case 'H':
102 Hflag = 1;
103 Lflag = Pflag = 0;
104 break;
105 case 'L':
106 Lflag = 1;
107 Hflag = Pflag = 0;
108 break;
109 case 'P':
110 Pflag = 1;
111 Hflag = Lflag = 0;
112 break;
113 case 'R':
114 Rflag = 1;
115 break;
116 case 'f':
117 fflag = 1;
118 break;
119 case 'h':
120 hflag = 1;
121 break;
122 case 'v':
123 vflag = 1;
124 break;
125 case '?':
126 default:
127 usage();
128 }
129 argv += optind;
130 argc -= optind;
131
132 if (argc < 2)
133 usage();
134 if (!Rflag && (Hflag || Lflag || Pflag))
135 warnx("options -H, -L, -P only useful with -R");
136
137 if (Rflag) {
138 fts_options = FTS_PHYSICAL;
139 if (hflag && (Hflag || Lflag))
140 errx(1, "the -R%c and -h options may not be "
141 "specified together", Hflag ? 'H' : 'L');
142 if (Hflag)
143 fts_options |= FTS_COMFOLLOW;
144 else if (Lflag) {
145 fts_options &= ~FTS_PHYSICAL;
146 fts_options |= FTS_LOGICAL;
147 }
148 } else
149 fts_options = hflag ? FTS_PHYSICAL : FTS_LOGICAL;
150
151 uid = (uid_t)-1;
152 gid = (gid_t)-1;
153 if (ischown) {
154 unix2003_compat = COMPAT_MODE("bin/chown", "Unix2003");
155 if ((cp = strchr(*argv, ':')) != NULL) {
156 *cp++ = '\0';
157 a_gid(cp);
158 }
159 #ifdef SUPPORT_DOT
160 else if ((cp = strchr(*argv, '.')) != NULL) {
161 warnx("separation of user and group with a period is deprecated");
162 *cp++ = '\0';
163 a_gid(cp);
164 }
165 #endif
166 a_uid(*argv);
167 } else {
168 unix2003_compat = COMPAT_MODE("bin/chgrp", "Unix2003");
169 a_gid(*argv);
170 }
171
172 if ((ftsp = fts_open(++argv, fts_options, 0)) == NULL)
173 err(1, NULL);
174
175 for (rval = 0; (p = fts_read(ftsp)) != NULL;) {
176 symlink_found = 0;
177 switch (p->fts_info) {
178 case FTS_D: /* Change it at FTS_DP. */
179 if (!Rflag)
180 fts_set(ftsp, p, FTS_SKIP);
181 continue;
182 case FTS_DNR: /* Warn, chown. */
183 warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
184 rval = 1;
185 break;
186 case FTS_ERR: /* Warn, continue. */
187 case FTS_NS:
188 warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
189 rval = 1;
190 continue;
191 case FTS_SL:
192 case FTS_SLNONE:
193 /*
194 * The only symlinks that end up here are ones that
195 * don't point to anything and ones that we found
196 * doing a physical walk.
197 */
198 if (hflag)
199 break;
200 else {
201 symlink_found = 1;
202 if (unix2003_compat) {
203 if (Hflag || Lflag) { /* -H or -L was specified */
204 if (p->fts_errno) {
205 warnx("%s: %s", p->fts_name, strerror(p->fts_errno));
206 rval = 1;
207 continue;
208 }
209 }
210 break; /* Otherwise symlinks keep going */
211 }
212 continue;
213 }
214 default:
215 break;
216 }
217 if (unix2003_compat) {
218 /* Can only avoid updating times if both uid and gid are -1 */
219 if ((uid == (uid_t)-1) && (gid == (gid_t)-1))
220 continue;
221 } else {
222 if ((uid == (uid_t)-1 || uid == p->fts_statp->st_uid) &&
223 (gid == (gid_t)-1 || gid == p->fts_statp->st_gid))
224 continue;
225 }
226 if (((hflag || symlink_found) ? lchown : chown)(p->fts_accpath, uid, gid) == -1) {
227 if (!fflag) {
228 chownerr(p->fts_path);
229 rval = 1;
230 }
231 } else {
232 if (vflag)
233 printf("%s\n", p->fts_path);
234 }
235 }
236 if (errno)
237 err(1, "fts_read");
238 exit(rval);
239 }
240
241 void
242 a_gid(const char *s)
243 {
244 struct group *gr;
245
246 if (*s == '\0') /* Argument was "uid[:.]". */
247 return;
248 gname = s;
249 gid = ((gr = getgrnam(s)) != NULL) ? gr->gr_gid : id(s, "group");
250 }
251
252 void
253 a_uid(const char *s)
254 {
255 struct passwd *pw;
256
257 if (*s == '\0') /* Argument was "[:.]gid". */
258 return;
259 uid = ((pw = getpwnam(s)) != NULL) ? pw->pw_uid : id(s, "user");
260 }
261
262 static uid_t
263 id(const char *name, const char *type)
264 {
265 unsigned long val;
266 char *ep;
267
268 errno = 0;
269 val = strtoul(name, &ep, 10);
270 if (errno || *ep != '\0' || val > UID_MAX)
271 errx(1, "%s: illegal %s name", name, type);
272 return (uid_t)val;
273 }
274
275 void
276 chownerr(const char *file)
277 {
278 static uid_t euid = -1;
279 static int ngroups = -1;
280 gid_t groups[NGROUPS_MAX];
281
282 /* Check for chown without being root. */
283 if (errno != EPERM || (uid != (uid_t)-1 &&
284 euid == (uid_t)-1 && (euid = geteuid()) != 0)) {
285 warn("%s", file);
286 return;
287 }
288
289 /* Check group membership; kernel just returns EPERM. */
290 if (gid != (gid_t)-1 && ngroups == -1 &&
291 euid == (uid_t)-1 && (euid = geteuid()) != 0) {
292 ngroups = getgroups(NGROUPS_MAX, groups);
293 while (--ngroups >= 0 && gid != groups[ngroups]);
294 if (ngroups < 0) {
295 warnx("you are not a member of group %s", gname);
296 return;
297 }
298 }
299 warn("%s", file);
300 }
301
302 void
303 usage(void)
304 {
305
306 if (ischown)
307 (void)fprintf(stderr, "%s\n%s\n",
308 "usage: chown [-fhv] [-R [-H | -L | -P]] owner[:group]"
309 " file ...",
310 " chown [-fhv] [-R [-H | -L | -P]] :group file ...");
311 else
312 (void)fprintf(stderr, "%s\n",
313 "usage: chgrp [-fhv] [-R [-H | -L | -P]] group file ...");
314 exit(1);
315 }