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