aboutsummaryrefslogtreecommitdiff
path: root/chown.c
diff options
context:
space:
mode:
Diffstat (limited to 'chown.c')
-rw-r--r--chown.c191
1 files changed, 160 insertions, 31 deletions
diff --git a/chown.c b/chown.c
index 8d6cffa14..76f3333e3 100644
--- a/chown.c
+++ b/chown.c
@@ -1,5 +1,5 @@
1/* 1/*
2 * Mini chown/chgrp implementation for busybox 2 * Mini chown/chmod/chgrp implementation for busybox
3 * 3 *
4 * Copyright (C) 1998 by Erik Andersen <andersee@debian.org> 4 * Copyright (C) 1998 by Erik Andersen <andersee@debian.org>
5 * 5 *
@@ -27,33 +27,150 @@
27 27
28static int uid=-1; 28static int uid=-1;
29static int gid=0; 29static int gid=0;
30static int chownApp; 30static int whichApp;
31static char* invocationName=NULL; 31static char* invocationName=NULL;
32static mode_t mode=7777;
32 33
33 34
35#define CHGRP_APP 1
36#define CHOWN_APP 2
37#define CHMOD_APP 3
38
34static const char chgrp_usage[] = "[OPTION]... GROUP FILE...\n" 39static const char chgrp_usage[] = "[OPTION]... GROUP FILE...\n"
35 "Change the group membership of each FILE to GROUP.\n" 40 "Change the group membership of each FILE to GROUP.\n"
36 "\n\tOptions:\n" "\t-R\tchange files and directories recursively\n"; 41 "\n\tOptions:\n" "\t-R\tchange files and directories recursively\n";
37static const char chown_usage[] = "[OPTION]... OWNER[.[GROUP] FILE...\n" 42static const char chown_usage[] = "[OPTION]... OWNER[.[GROUP] FILE...\n"
38 "Change the owner and/or group of each FILE to OWNER and/or GROUP.\n" 43 "Change the owner and/or group of each FILE to OWNER and/or GROUP.\n"
39 "\n\tOptions:\n" "\t-R\tchange files and directories recursively\n"; 44 "\n\tOptions:\n" "\t-R\tchange files and directories recursively\n";
45static 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";
40 49
41 50
42 51
43static int fileAction(const char *fileName) 52static int fileAction(const char *fileName)
44{ 53{
45 struct stat statBuf; 54 struct stat statBuf;
46 if ((stat(fileName, &statBuf) < 0) || 55 if (stat(fileName, &statBuf) < 0) {
47 (chown(fileName, 56 switch (whichApp) {
48 ((chownApp==TRUE)? uid: statBuf.st_uid), 57 case CHGRP_APP:
49 gid) < 0)) { 58 case CHOWN_APP:
50 perror(fileName); 59 if (chown(fileName, ((whichApp==CHOWN_APP)? uid: statBuf.st_uid), gid) < 0)
51 return( FALSE); 60 return( TRUE);
61 case CHMOD_APP:
62 if (chmod(fileName, mode))
63 return( TRUE);
64 }
52 } 65 }
53 return( TRUE); 66 perror(fileName);
67 return( FALSE);
54} 68}
55 69
56int chown_main(int argc, char **argv) 70/* [ugoa]{+|-|=}[rwxstl] */
71int parse_mode( const char* s, mode_t* or, mode_t* and, int* group_execute)
72{
73 mode_t mode = 0;
74 mode_t groups = S_ISVTX;
75 char type;
76 char c;
77
78 do {
79 for ( ; ; ) {
80 switch ( c = *s++ ) {
81 case '\0':
82 return (FALSE);
83 case 'u':
84 groups |= S_ISUID|S_IRWXU;
85 continue;
86 case 'g':
87 groups |= S_ISGID|S_IRWXG;
88 continue;
89 case 'o':
90 groups |= S_IRWXO;
91 continue;
92 case 'a':
93 groups |= S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO;
94 continue;
95 case '+':
96 case '=':
97 case '-':
98 type = c;
99 if ( groups == S_ISVTX ) /* The default is "all" */
100 groups |= S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO;
101 break;
102 default:
103 if ( c >= '0' && c <= '7' && mode == 0 && groups == S_ISVTX ) {
104 *and = 0;
105 *or = strtol(--s, 0, 010);
106 return (TRUE);
107 }
108 else
109 return (FALSE);
110 }
111 break;
112 }
113
114 while ( (c = *s++) != '\0' ) {
115 switch ( c ) {
116 case ',':
117 break;
118 case 'r':
119 mode |= S_IRUSR|S_IRGRP|S_IROTH;
120 continue;
121 case 'w':
122 mode |= S_IWUSR|S_IWGRP|S_IWOTH;
123 continue;
124 case 'x':
125 mode |= S_IXUSR|S_IXGRP|S_IXOTH;
126 continue;
127 case 's':
128 if ( group_execute != 0 && (groups & S_IRWXG) ) {
129 if ( *group_execute < 0 )
130 return (FALSE);
131 if ( type != '-' ) {
132 mode |= S_IXGRP;
133 *group_execute = 1;
134 }
135 }
136 mode |= S_ISUID|S_ISGID;
137 continue;
138 case 'l':
139 if ( *group_execute > 0 )
140 return (FALSE);
141 if ( type != '-' ) {
142 *and &= ~S_IXGRP;
143 *group_execute = -1;
144 }
145 mode |= S_ISGID;
146 groups |= S_ISGID;
147 continue;
148 case 't':
149 mode |= S_ISVTX;
150 continue;
151 default:
152 return (FALSE);
153 }
154 break;
155 }
156 switch ( type ) {
157 case '=':
158 *and &= ~(groups);
159 /* fall through */
160 case '+':
161 *or |= mode & groups;
162 break;
163 case '-':
164 *and &= ~(mode & groups);
165 *or &= *and;
166 break;
167 }
168 } while ( c == ',' );
169 return (TRUE);
170}
171
172
173int chmod_chown_chgrp_main(int argc, char **argv)
57{ 174{
58 struct group *grp; 175 struct group *grp;
59 struct passwd *pwd; 176 struct passwd *pwd;
@@ -61,11 +178,11 @@ int chown_main(int argc, char **argv)
61 char *groupName; 178 char *groupName;
62 179
63 180
64 chownApp = (strcmp(*argv, "chown")==0)? TRUE : FALSE; 181 whichApp = (strcmp(*argv, "chown")==0)? CHOWN_APP : (strcmp(*argv, "chmod")==0)? CHMOD_APP : CHGRP_APP;
65 182
66 if (argc < 2) { 183 if (argc < 2) {
67 fprintf(stderr, "Usage: %s %s", *argv, 184 fprintf(stderr, "Usage: %s %s", *argv,
68 (chownApp==TRUE)? chown_usage : chgrp_usage); 185 (whichApp==TRUE)? chown_usage : chgrp_usage);
69 exit( FALSE); 186 exit( FALSE);
70 } 187 }
71 invocationName=*argv; 188 invocationName=*argv;
@@ -86,29 +203,40 @@ int chown_main(int argc, char **argv)
86 argv++; 203 argv++;
87 } 204 }
88 205
89 /* Find the selected group */ 206 if ( whichApp == CHMOD_APP ) {
90 groupName = strchr(*argv, '.'); 207 /* Find the specified modes */
91 if ( chownApp==TRUE && groupName ) 208 if ( parse_mode(*argv, &orWithMode, &andWithMode, 0) == FALSE ) {
92 *groupName++ = '\0'; 209 fprintf(stderr, "%s: Unknown mode: %s\n", invocationName, *argv);
93 else 210 exit( FALSE);
94 groupName = *argv; 211 }
95 grp = getgrnam(groupName); 212 mode &= andWithMode;
96 if (grp == NULL) { 213 mode |= orWithMode;
97 fprintf(stderr, "%s: Unknown group name: %s\n", invocationName, groupName); 214 } else {
98 exit( FALSE);
99 }
100 gid = grp->gr_gid;
101 215
102 /* Find the selected user (if appropriate) */ 216 /* Find the selected group */
103 if (chownApp==TRUE) { 217 groupName = strchr(*argv, '.');
104 pwd = getpwnam(*argv); 218 if ( whichApp==TRUE && groupName )
105 if (pwd == NULL) { 219 *groupName++ = '\0';
106 fprintf(stderr, "%s: Unknown user name: %s\n", invocationName, *argv); 220 else
221 groupName = *argv;
222 grp = getgrnam(groupName);
223 if (grp == NULL) {
224 fprintf(stderr, "%s: Unknown group name: %s\n", invocationName, groupName);
107 exit( FALSE); 225 exit( FALSE);
108 } 226 }
109 uid = pwd->pw_uid; 227 gid = grp->gr_gid;
110 }
111 228
229 /* Find the selected user (if appropriate) */
230 if (whichApp==TRUE) {
231 pwd = getpwnam(*argv);
232 if (pwd == NULL) {
233 fprintf(stderr, "%s: Unknown user name: %s\n", invocationName, *argv);
234 exit( FALSE);
235 }
236 uid = pwd->pw_uid;
237 }
238 }
239
112 /* Ok, ready to do the deed now */ 240 /* Ok, ready to do the deed now */
113 if (argc <= 1) { 241 if (argc <= 1) {
114 fprintf(stderr, "%s: too few arguments", invocationName); 242 fprintf(stderr, "%s: too few arguments", invocationName);
@@ -120,3 +248,4 @@ int chown_main(int argc, char **argv)
120 } 248 }
121 exit(TRUE); 249 exit(TRUE);
122} 250}
251