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