Added sfdisk. Ststic-ified a bunch of stuff.
[oweals/busybox.git] / chmod_chown_chgrp.c
1 /*
2  * Mini chown/chmod/chgrp implementation for busybox
3  *
4  * Copyright (C) 1998 by Erik Andersen <andersee@debian.org>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  */
21
22 #include <stdio.h>
23 #include <grp.h>
24 #include <pwd.h>
25 #include "internal.h"
26
27
28 static int uid=-1;
29 static int gid=0;
30 static int whichApp;
31 static char* invocationName=NULL;
32 static mode_t mode=0644;
33
34
35 #define CHGRP_APP   1
36 #define CHOWN_APP   2
37 #define CHMOD_APP   3
38
39 static const char chgrp_usage[] = "[OPTION]... GROUP FILE...\n"
40     "Change the group membership of each FILE to GROUP.\n"
41     "\n\tOptions:\n" "\t-R\tchange files and directories recursively\n";
42 static const char chown_usage[] = "[OPTION]...  OWNER[.[GROUP] FILE...\n"
43     "Change the owner and/or group of each FILE to OWNER and/or GROUP.\n"
44     "\n\tOptions:\n" "\t-R\tchange files and directories recursively\n";
45 static const char chmod_usage[] = "[-R] MODE[,MODE]... FILE...\n"
46 "Each MODE is one or more of the letters ugoa, one of the symbols +-= and\n"
47 "one or more of the letters rwxst.\n\n"
48  "\t-R\tchange files and directories recursively.\n";
49
50
51
52 static int fileAction(const char *fileName, struct stat* statbuf)
53 {
54     switch (whichApp) {
55         case CHGRP_APP:
56         case CHOWN_APP:
57             if (chown(fileName, ((whichApp==CHOWN_APP)? uid: statbuf->st_uid), gid) < 0)
58                 return( TRUE);
59         case CHMOD_APP:
60             fprintf(stderr, "%s, %d\n", fileName, mode);
61             if (chmod(fileName, mode))
62                 return( TRUE);
63     }
64     perror(fileName);
65     return( FALSE);
66 }
67
68 int chmod_chown_chgrp_main(int argc, char **argv)
69 {
70     struct group *grp;
71     struct passwd *pwd;
72     int recursiveFlag=FALSE;
73     char *groupName;
74
75     whichApp = (strcmp(*argv, "chown")==0)? CHOWN_APP : (strcmp(*argv, "chmod")==0)? CHMOD_APP : CHGRP_APP; 
76
77     if (argc < 2) {
78         fprintf(stderr, "Usage: %s %s", *argv, 
79                 (whichApp==TRUE)? chown_usage : chgrp_usage);
80         exit( FALSE);
81     }
82     invocationName=*argv;
83     argc--;
84     argv++;
85
86     /* Parse options */
87     while (**argv == '-') {
88         while (*++(*argv)) switch (**argv) {
89             case 'R':
90                 recursiveFlag = TRUE;
91                 break;
92             default:
93                 fprintf(stderr, "Unknown option: %c\n", **argv);
94                 exit( FALSE);
95         }
96         argc--;
97         argv++;
98     }
99     
100     if ( whichApp == CHMOD_APP ) {
101         /* Find the specified modes */
102         mode &= S_ISVTX|S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO;
103         if ( parse_mode(*argv, &mode) == FALSE ) {
104             fprintf(stderr, "%s: Unknown mode: %s\n", invocationName, *argv);
105             exit( FALSE);
106         }
107         //mode &= andWithMode;
108         fprintf(stderr, "mode %d\n", mode);
109     } else {
110
111         /* Find the selected group */
112         groupName = strchr(*argv, '.');
113         if ( whichApp==TRUE && groupName )
114             *groupName++ = '\0';
115         else
116             groupName = *argv;
117         grp = getgrnam(groupName);
118         if (grp == NULL) {
119             fprintf(stderr, "%s: Unknown group name: %s\n", invocationName, groupName);
120             exit( FALSE);
121         }
122         gid = grp->gr_gid;
123
124         /* Find the selected user (if appropriate)  */
125         if (whichApp==TRUE) {
126             pwd = getpwnam(*argv);
127             if (pwd == NULL) {
128                 fprintf(stderr, "%s: Unknown user name: %s\n", invocationName, *argv);
129                 exit( FALSE);
130             }
131             uid = pwd->pw_uid;
132         }
133     }
134     
135     /* Ok, ready to do the deed now */
136     if (argc <= 1) {
137         fprintf(stderr, "%s: too few arguments", invocationName);
138         exit( FALSE);
139     }
140     while (argc-- > 1) {
141         if (recursiveAction( *(++argv), recursiveFlag, TRUE, FALSE, fileAction, fileAction)==FALSE)
142             exit( FALSE);
143     }
144     exit(TRUE);
145 }
146
147
148
149
150
151
152
153
154
155
156
157
158 #ifdef fooo
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174 #include "internal.h"
175 #include <pwd.h>
176 #include <grp.h>
177 #include <string.h>
178 #include <stdio.h>
179
180 int my_getid(const char *filename, const char *name, uid_t *id) 
181 {
182         FILE *stream;
183         uid_t rid;
184         char *rname, *start, *end, buf[128];
185
186         stream=fopen(filename,"r");
187
188         while (fgets (buf, 128, stream) != NULL) {
189                 if (buf[0] == '#')
190                         continue;
191
192                 start = buf;
193                 end = strchr (start, ':');
194                 if (end == NULL)
195                         continue;
196                 *end = '\0';
197                 rname = start;
198
199                 start = end + 1;
200                 end = strchr (start, ':');
201                 if (end == NULL)
202                         continue;
203
204                 start = end + 1;
205                 rid = (uid_t) strtol (start, &end, 10);
206                 if (end == start)
207                         continue;
208
209                 if (name) {
210                         if (0 == strcmp(rname, name)) {
211                                 *id=rid;
212                                 return 0;
213                         }
214                 } else {
215                         if ( *id == rid )
216                                 return 0;
217                 }
218         }
219         fclose(stream);
220         return -1;
221 }
222
223 int 
224 my_getpwuid(uid_t *uid) 
225 {
226         return my_getid("/etc/passwd", NULL, uid);
227 }
228
229 int 
230 my_getpwnam(char *name, uid_t *uid) 
231 {
232         return my_getid("/etc/passwd", name, uid);
233 }
234
235 int 
236 my_getgrgid(gid_t *gid) 
237 {
238         return my_getid("/etc/group", NULL, gid);
239 }
240
241 int 
242 my_getgrnam(char *name, gid_t *gid) 
243 {
244         return my_getid("/etc/group", name, gid);
245 }
246
247 const char      chown_usage[] = "chown [-R] user-name file [file ...]\n"
248 "\n\tThe group list is kept in the file /etc/groups.\n\n"
249 "\t-R:\tRecursively change the mode of all files and directories\n"
250 "\t\tunder the argument directory.";
251
252 int
253 parse_user_name(const char * s, struct FileInfo * i)
254 {
255         char *                          dot = strchr(s, '.');
256         char * end = NULL;
257         uid_t id = 0;
258
259         if (! dot )
260                 dot = strchr(s, ':');
261
262         if ( dot )
263                 *dot = '\0';
264
265         if ( my_getpwnam(s,&id) == -1 ) {
266                 id = strtol(s,&end,10);
267                 if ((*end != '\0') || ( my_getpwuid(&id) == -1 )) {
268                         fprintf(stderr, "%s: no such user.\n", s);
269                         return 1;
270                 }
271         }
272         i->userID = id;
273
274         if ( dot ) {
275                 if ( my_getgrnam(++dot,&id) == -1 ) {
276                         id = strtol(dot,&end,10);
277                         if ((*end != '\0') || ( my_getgrgid(&id) == -1 )) {
278                                 fprintf(stderr, "%s: no such group.\n", dot);
279                                 return 1;
280                         }
281                 }
282                 i->groupID = id;
283                 i->changeGroupID = 1;
284         }
285         return 0;
286 }
287
288 extern int
289 chown_main(struct FileInfo * i, int argc, char * * argv)
290 {
291         int                                     status;
292
293         while ( argc >= 3 && strcmp("-R", argv[1]) == 0 ) {
294                 i->recursive = 1;
295                 argc--;
296                 argv++;
297         }
298
299         if ( (status = parse_user_name(argv[1], i)) != 0 )
300                 return status;
301
302         argv++;
303         argc--;
304
305         i->changeUserID = 1;
306         i->complainInPostProcess = 1;
307
308         return monadic_main(i, argc, argv);
309 }
310
311
312
313
314 #endif