aboutsummaryrefslogtreecommitdiff
path: root/chmod_chown_chgrp.c
diff options
context:
space:
mode:
Diffstat (limited to 'chmod_chown_chgrp.c')
-rw-r--r--chmod_chown_chgrp.c120
1 files changed, 8 insertions, 112 deletions
diff --git a/chmod_chown_chgrp.c b/chmod_chown_chgrp.c
index 37a52281c..5c2adab78 100644
--- a/chmod_chown_chgrp.c
+++ b/chmod_chown_chgrp.c
@@ -29,7 +29,7 @@ static int uid=-1;
29static int gid=0; 29static int gid=0;
30static int whichApp; 30static int whichApp;
31static char* invocationName=NULL; 31static char* invocationName=NULL;
32static mode_t mode=7777; 32static mode_t mode=0644;
33 33
34 34
35#define CHGRP_APP 1 35#define CHGRP_APP 1
@@ -59,6 +59,7 @@ static int fileAction(const char *fileName)
59 if (chown(fileName, ((whichApp==CHOWN_APP)? uid: statBuf.st_uid), gid) < 0) 59 if (chown(fileName, ((whichApp==CHOWN_APP)? uid: statBuf.st_uid), gid) < 0)
60 return( TRUE); 60 return( TRUE);
61 case CHMOD_APP: 61 case CHMOD_APP:
62 fprintf(stderr, "%s, %d\n", fileName, mode);
62 if (chmod(fileName, mode)) 63 if (chmod(fileName, mode))
63 return( TRUE); 64 return( TRUE);
64 } 65 }
@@ -67,118 +68,13 @@ static int fileAction(const char *fileName)
67 return( FALSE); 68 return( FALSE);
68} 69}
69 70
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) 71int chmod_chown_chgrp_main(int argc, char **argv)
174{ 72{
175 struct group *grp; 73 struct group *grp;
176 struct passwd *pwd; 74 struct passwd *pwd;
177 int recursiveFlag=FALSE; 75 int recursiveFlag=FALSE;
178 char *groupName; 76 char *groupName;
179 mode_t andWithMode = S_ISVTX|S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO; 77 mode_t andWithMode= S_ISVTX|S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO;
180 mode_t orWithMode = 0;
181
182 78
183 whichApp = (strcmp(*argv, "chown")==0)? CHOWN_APP : (strcmp(*argv, "chmod")==0)? CHMOD_APP : CHGRP_APP; 79 whichApp = (strcmp(*argv, "chown")==0)? CHOWN_APP : (strcmp(*argv, "chmod")==0)? CHMOD_APP : CHGRP_APP;
184 80
@@ -207,12 +103,12 @@ int chmod_chown_chgrp_main(int argc, char **argv)
207 103
208 if ( whichApp == CHMOD_APP ) { 104 if ( whichApp == CHMOD_APP ) {
209 /* Find the specified modes */ 105 /* Find the specified modes */
210 if ( parse_mode(*argv, &orWithMode, &andWithMode, 0) == FALSE ) { 106 if ( parse_mode(*argv, &mode) == FALSE ) {
211 fprintf(stderr, "%s: Unknown mode: %s\n", invocationName, *argv); 107 fprintf(stderr, "%s: Unknown mode: %s\n", invocationName, *argv);
212 exit( FALSE); 108 exit( FALSE);
213 } 109 }
214 mode &= andWithMode; 110 //mode &= andWithMode;
215 mode |= orWithMode; 111 fprintf(stderr, "mode %d\n", mode);
216 } else { 112 } else {
217 113
218 /* Find the selected group */ 114 /* Find the selected group */
@@ -245,8 +141,8 @@ int chmod_chown_chgrp_main(int argc, char **argv)
245 exit( FALSE); 141 exit( FALSE);
246 } 142 }
247 while (argc-- > 1) { 143 while (argc-- > 1) {
248 argv++; 144 if (recursiveAction( *(++argv), recursiveFlag, TRUE, fileAction, fileAction)==FALSE)
249 recursiveAction( *argv, recursiveFlag, TRUE, fileAction, fileAction); 145 exit( FALSE);
250 } 146 }
251 exit(TRUE); 147 exit(TRUE);
252} 148}