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