1 | |
---|
2 | /* |
---|
3 | * COPYRIGHT (c) 1989-2011. |
---|
4 | * On-Line Applications Research Corporation (OAR). |
---|
5 | * |
---|
6 | * The license and distribution terms for this file may be |
---|
7 | * found in the file LICENSE in this distribution or at |
---|
8 | * http://www.rtems.com/license/LICENSE. |
---|
9 | * |
---|
10 | * $Id$ |
---|
11 | */ |
---|
12 | |
---|
13 | #ifdef HAVE_CONFIG_H |
---|
14 | #include "config.h" |
---|
15 | #endif |
---|
16 | |
---|
17 | #include <sys/stat.h> |
---|
18 | #include <fcntl.h> |
---|
19 | #include <errno.h> |
---|
20 | #include <string.h> |
---|
21 | #include <stdio.h> |
---|
22 | #include <stdlib.h> |
---|
23 | #include <stdint.h> |
---|
24 | #include <dirent.h> |
---|
25 | #include <unistd.h> |
---|
26 | |
---|
27 | #include "fstest.h" |
---|
28 | #include "pmacros.h" |
---|
29 | |
---|
30 | /* |
---|
31 | * Test the umask |
---|
32 | */ |
---|
33 | void umask_test01(void ) |
---|
34 | { |
---|
35 | |
---|
36 | mode_t previous_cmask; |
---|
37 | mode_t tmp_mode; |
---|
38 | mode_t file_mode; |
---|
39 | struct stat statbuf; |
---|
40 | int status = 0; |
---|
41 | int fd; |
---|
42 | |
---|
43 | char* file01="file01"; |
---|
44 | char* file02="file02"; |
---|
45 | char* directory01="dir01"; |
---|
46 | |
---|
47 | const char* wd=__func__; |
---|
48 | |
---|
49 | mode_t mode=S_IRWXU|S_IRWXG|S_IRWXO ; |
---|
50 | |
---|
51 | |
---|
52 | |
---|
53 | /* |
---|
54 | *Create a new directory and change the current directory to this |
---|
55 | */ |
---|
56 | status=mkdir(wd,mode); |
---|
57 | rtems_test_assert(status==0); |
---|
58 | status=chdir(wd); |
---|
59 | rtems_test_assert(status==0); |
---|
60 | |
---|
61 | /* |
---|
62 | * |
---|
63 | * Call open creat and mkdir to create new files and directory |
---|
64 | */ |
---|
65 | fd=open(file01,O_CREAT|O_RDWR,mode); |
---|
66 | status=close(fd); |
---|
67 | rtems_test_assert(status==0); |
---|
68 | |
---|
69 | fd=creat(file02,mode); |
---|
70 | status=close(fd); |
---|
71 | rtems_test_assert(status==0); |
---|
72 | |
---|
73 | status=mkdir(directory01,mode); |
---|
74 | rtems_test_assert(status==0); |
---|
75 | |
---|
76 | |
---|
77 | /* |
---|
78 | *Get the previous cmask and set it to a new one |
---|
79 | */ |
---|
80 | previous_cmask = umask (0321); |
---|
81 | printf("The previous cmask is %03o\n",(unsigned int)previous_cmask); |
---|
82 | file_mode= mode & ~previous_cmask; |
---|
83 | |
---|
84 | status = stat (file01, &statbuf); |
---|
85 | rtems_test_assert (status == 0); |
---|
86 | tmp_mode = (statbuf.st_mode) & ALLPERMS; |
---|
87 | printf("The file mode of %s is %03o\n",file01,(unsigned int)tmp_mode); |
---|
88 | rtems_test_assert(tmp_mode==file_mode); |
---|
89 | |
---|
90 | |
---|
91 | status = stat (file02, &statbuf); |
---|
92 | rtems_test_assert (status == 0); |
---|
93 | tmp_mode = (statbuf.st_mode) & ALLPERMS; |
---|
94 | printf("The file mode of %s is %03o\n",file02,(unsigned int)tmp_mode); |
---|
95 | rtems_test_assert(tmp_mode==file_mode); |
---|
96 | |
---|
97 | status = stat (directory01, &statbuf); |
---|
98 | rtems_test_assert (status == 0); |
---|
99 | tmp_mode = (statbuf.st_mode) & ALLPERMS; |
---|
100 | printf("The file mode of %s is %03o\n",directory01,(unsigned int)tmp_mode); |
---|
101 | rtems_test_assert(tmp_mode==file_mode); |
---|
102 | |
---|
103 | /* |
---|
104 | *Remove them and recreate them with the same mode |
---|
105 | */ |
---|
106 | |
---|
107 | status=unlink(file01); |
---|
108 | rtems_test_assert(status==0); |
---|
109 | fd=open(file01,O_CREAT|O_RDWR,mode); |
---|
110 | status=close(fd); |
---|
111 | rtems_test_assert(status==0); |
---|
112 | |
---|
113 | status=unlink(file02); |
---|
114 | rtems_test_assert(status==0); |
---|
115 | fd=creat(file02,mode); |
---|
116 | status=close(fd); |
---|
117 | rtems_test_assert(status==0); |
---|
118 | |
---|
119 | status=rmdir(directory01); |
---|
120 | rtems_test_assert(status==0); |
---|
121 | status=mkdir(directory01,mode); |
---|
122 | rtems_test_assert(status==0); |
---|
123 | |
---|
124 | /* |
---|
125 | *Check the file mode |
---|
126 | */ |
---|
127 | |
---|
128 | previous_cmask = umask (00); |
---|
129 | printf("The previous cmask is %03o\n",(unsigned int)previous_cmask); |
---|
130 | file_mode= mode & ~previous_cmask; |
---|
131 | |
---|
132 | status = stat (file01, &statbuf); |
---|
133 | rtems_test_assert (status == 0); |
---|
134 | tmp_mode = (statbuf.st_mode) & ALLPERMS; |
---|
135 | printf("The file mode of %s is %03o\n",file01,(unsigned int)tmp_mode); |
---|
136 | rtems_test_assert(tmp_mode==file_mode); |
---|
137 | |
---|
138 | |
---|
139 | status = stat (file02, &statbuf); |
---|
140 | rtems_test_assert (status == 0); |
---|
141 | tmp_mode = (statbuf.st_mode) & ALLPERMS; |
---|
142 | printf("The file mode of %s is %03o\n",file02,(unsigned int)tmp_mode); |
---|
143 | rtems_test_assert(tmp_mode==file_mode); |
---|
144 | |
---|
145 | status = stat (directory01, &statbuf); |
---|
146 | rtems_test_assert (status == 0); |
---|
147 | tmp_mode = (statbuf.st_mode) & ALLPERMS; |
---|
148 | printf("The file mode of %s is %03o\n",directory01,(unsigned int)tmp_mode); |
---|
149 | rtems_test_assert(tmp_mode==file_mode); |
---|
150 | |
---|
151 | /* |
---|
152 | * Go back to parent directory |
---|
153 | */ |
---|
154 | status=chdir(".."); |
---|
155 | rtems_test_assert(status==0); |
---|
156 | |
---|
157 | } |
---|
158 | /* |
---|
159 | * Check the file mode in file and directory |
---|
160 | */ |
---|
161 | void test_premission01(void ) |
---|
162 | { |
---|
163 | mode_t tmp_mode; |
---|
164 | struct stat statbuf; |
---|
165 | int status = 0; |
---|
166 | int fd; |
---|
167 | |
---|
168 | char* file01="file01"; |
---|
169 | char* file02="file02"; |
---|
170 | char* directory01="dir01"; |
---|
171 | |
---|
172 | char path[20]; |
---|
173 | char* test_data="Test Data"; |
---|
174 | char* data_buf; |
---|
175 | size_t len=strlen(test_data); |
---|
176 | |
---|
177 | int n; |
---|
178 | DIR *dp; |
---|
179 | |
---|
180 | const char* wd=__func__; |
---|
181 | |
---|
182 | mode_t mode=S_IRWXU|S_IRWXG|S_IRWXO ; |
---|
183 | uid_t user_id =65534; |
---|
184 | gid_t group_id =65534; |
---|
185 | |
---|
186 | uid_t another_user_id =65533; |
---|
187 | gid_t another_group_id =65533; |
---|
188 | |
---|
189 | |
---|
190 | /* |
---|
191 | *Create a new directory and change the current directory to this |
---|
192 | */ |
---|
193 | umask(00); |
---|
194 | status=mkdir(wd,mode); |
---|
195 | rtems_test_assert(status==0); |
---|
196 | status=chdir(wd); |
---|
197 | rtems_test_assert(status==0); |
---|
198 | |
---|
199 | status=setgid(group_id); |
---|
200 | rtems_test_assert(status==0); |
---|
201 | status=seteuid(user_id); |
---|
202 | rtems_test_assert(status==0); |
---|
203 | status=seteuid(user_id); |
---|
204 | rtems_test_assert(status==0); |
---|
205 | |
---|
206 | |
---|
207 | /* |
---|
208 | *Create a file with mode 0777 |
---|
209 | */ |
---|
210 | fd=creat(file01,mode); |
---|
211 | status=close(fd); |
---|
212 | rtems_test_assert(status==0); |
---|
213 | /* |
---|
214 | *Create a file with mode 0240 |
---|
215 | */ |
---|
216 | |
---|
217 | fd=creat(file02,0240); |
---|
218 | status=close(fd); |
---|
219 | rtems_test_assert(status==0); |
---|
220 | |
---|
221 | |
---|
222 | /* |
---|
223 | *Check the file mode uid and gid |
---|
224 | */ |
---|
225 | status = stat (file01, &statbuf); |
---|
226 | rtems_test_assert (status == 0); |
---|
227 | tmp_mode = (statbuf.st_mode) & ALLPERMS; |
---|
228 | printf("The file mode of %s is %03o\n",file01,(unsigned int)tmp_mode); |
---|
229 | rtems_test_assert(tmp_mode==mode); |
---|
230 | rtems_test_assert(statbuf.st_uid==user_id); |
---|
231 | rtems_test_assert(statbuf.st_gid==group_id); |
---|
232 | |
---|
233 | status = stat (file02, &statbuf); |
---|
234 | rtems_test_assert (status == 0); |
---|
235 | tmp_mode = (statbuf.st_mode) & ALLPERMS; |
---|
236 | printf("The file mode of %s is %03o\n",file02,(unsigned int)tmp_mode); |
---|
237 | rtems_test_assert(tmp_mode==0240); |
---|
238 | rtems_test_assert(statbuf.st_uid==user_id); |
---|
239 | rtems_test_assert(statbuf.st_gid==group_id); |
---|
240 | |
---|
241 | /* |
---|
242 | * Create directory and a file in it for tese |
---|
243 | */ |
---|
244 | |
---|
245 | status=mkdir(directory01,0777); |
---|
246 | rtems_test_assert(status==0); |
---|
247 | sprintf(path,"%s/%s",directory01,file01); |
---|
248 | fd=creat(path,0777); |
---|
249 | |
---|
250 | status=chmod(directory01,0340); |
---|
251 | rtems_test_assert (status == 0); |
---|
252 | status = stat (directory01, &statbuf); |
---|
253 | rtems_test_assert (status == 0); |
---|
254 | tmp_mode = (statbuf.st_mode) & ALLPERMS; |
---|
255 | printf("The file mode of %s is %03o\n",directory01,(unsigned int)tmp_mode); |
---|
256 | rtems_test_assert(tmp_mode==0340); |
---|
257 | rtems_test_assert(statbuf.st_uid==user_id); |
---|
258 | rtems_test_assert(statbuf.st_gid==group_id); |
---|
259 | |
---|
260 | /* |
---|
261 | * Check the file with open and write |
---|
262 | */ |
---|
263 | |
---|
264 | /* |
---|
265 | * Test write |
---|
266 | */ |
---|
267 | fd=open(file01,O_WRONLY); |
---|
268 | n=write(fd,test_data,len); |
---|
269 | rtems_test_assert(n==len); |
---|
270 | status=close(fd); |
---|
271 | rtems_test_assert(status==0); |
---|
272 | |
---|
273 | fd=open(file02,O_WRONLY); |
---|
274 | n=write(fd,test_data,len); |
---|
275 | rtems_test_assert(n==len); |
---|
276 | status=close(fd); |
---|
277 | rtems_test_assert(status==0); |
---|
278 | |
---|
279 | /* |
---|
280 | * Test read |
---|
281 | */ |
---|
282 | data_buf=(char*)malloc(len+1); |
---|
283 | fd=open(file01,O_RDWR); |
---|
284 | rtems_test_assert(fd!=-1); |
---|
285 | n=read(fd,data_buf,len); |
---|
286 | rtems_test_assert(n==len); |
---|
287 | status=close(fd); |
---|
288 | rtems_test_assert(status==0); |
---|
289 | |
---|
290 | EXPECT_ERROR(EACCES,open,file02,O_RDONLY); |
---|
291 | EXPECT_ERROR(EACCES,open,file02,O_RDWR); |
---|
292 | |
---|
293 | /* |
---|
294 | * Test read directory |
---|
295 | */ |
---|
296 | dp= opendir(directory01); |
---|
297 | rtems_test_assert(dp==NULL); |
---|
298 | rtems_test_assert(errno==EACCES); |
---|
299 | |
---|
300 | /* |
---|
301 | * Test write directory |
---|
302 | */ |
---|
303 | status = lstat (path, &statbuf); |
---|
304 | rtems_test_assert (status == 0); |
---|
305 | |
---|
306 | status=unlink(path); |
---|
307 | rtems_test_assert(status==0); |
---|
308 | |
---|
309 | |
---|
310 | /* |
---|
311 | * Change euid and check |
---|
312 | */ |
---|
313 | puts("Change euid and check"); |
---|
314 | status=seteuid(0); |
---|
315 | rtems_test_assert(status==0); |
---|
316 | |
---|
317 | status=seteuid(another_user_id); |
---|
318 | rtems_test_assert(status==0); |
---|
319 | |
---|
320 | fd=open(file01,O_WRONLY); |
---|
321 | n=write(fd,test_data,len); |
---|
322 | rtems_test_assert(n==len); |
---|
323 | status=close(fd); |
---|
324 | rtems_test_assert(status==0); |
---|
325 | |
---|
326 | EXPECT_ERROR(EACCES,open,file02,O_WRONLY); |
---|
327 | EXPECT_ERROR(EACCES,open,file02,O_RDWR); |
---|
328 | |
---|
329 | /* |
---|
330 | * Test read directory |
---|
331 | */ |
---|
332 | dp= opendir(directory01); |
---|
333 | rtems_test_assert(dp!=NULL); |
---|
334 | status=closedir(dp); |
---|
335 | rtems_test_assert(status==0); |
---|
336 | |
---|
337 | /* |
---|
338 | * Test write directory |
---|
339 | */ |
---|
340 | EXPECT_ERROR(EACCES,creat,path,mode); |
---|
341 | EXPECT_ERROR(EACCES,rename,path,"test"); |
---|
342 | EXPECT_ERROR(EACCES,truncate,path,0); |
---|
343 | EXPECT_ERROR(EACCES,link,path,"test"); |
---|
344 | EXPECT_ERROR(EACCES,unlink,path); |
---|
345 | /* |
---|
346 | * Change egid and check |
---|
347 | */ |
---|
348 | puts("Change egid and check"); |
---|
349 | status=seteuid(0); |
---|
350 | rtems_test_assert(status==0); |
---|
351 | |
---|
352 | status=setgid(another_group_id); |
---|
353 | rtems_test_assert(status==0); |
---|
354 | |
---|
355 | status=seteuid(another_user_id); |
---|
356 | rtems_test_assert(status==0); |
---|
357 | |
---|
358 | EXPECT_ERROR(EACCES,open,file02,O_WRONLY); |
---|
359 | EXPECT_ERROR(EACCES,open,file02,O_RDONLY); |
---|
360 | EXPECT_ERROR(EACCES,open,file02,O_RDWR); |
---|
361 | |
---|
362 | /* |
---|
363 | * Test read directory |
---|
364 | */ |
---|
365 | dp= opendir(directory01); |
---|
366 | rtems_test_assert(dp==NULL); |
---|
367 | rtems_test_assert(errno==EACCES); |
---|
368 | |
---|
369 | /* |
---|
370 | * Test write directory |
---|
371 | */ |
---|
372 | EXPECT_ERROR(EACCES,creat,path,mode); |
---|
373 | |
---|
374 | /* |
---|
375 | * Go back to parent directory |
---|
376 | */ |
---|
377 | status=seteuid(0); |
---|
378 | rtems_test_assert(status==0); |
---|
379 | status=setgid(0); |
---|
380 | rtems_test_assert(status==0); |
---|
381 | free(data_buf); |
---|
382 | |
---|
383 | status=chdir(".."); |
---|
384 | rtems_test_assert(status==0); |
---|
385 | } |
---|
386 | |
---|
387 | /* |
---|
388 | * Test chown and chmod |
---|
389 | */ |
---|
390 | void test_premission02(void ) |
---|
391 | { |
---|
392 | struct stat statbuf; |
---|
393 | int status = 0; |
---|
394 | int fd; |
---|
395 | |
---|
396 | char* file01="file01"; |
---|
397 | char* directory01="dir01"; |
---|
398 | |
---|
399 | mode_t tmp_mode; |
---|
400 | mode_t file_mode=0321; |
---|
401 | |
---|
402 | const char* wd=__func__; |
---|
403 | |
---|
404 | mode_t mode=S_IRWXU|S_IRWXG|S_IRWXO ; |
---|
405 | uid_t user_id =65534; |
---|
406 | gid_t group_id =65534; |
---|
407 | |
---|
408 | /* |
---|
409 | *Create a new directory and change the current directory to this |
---|
410 | */ |
---|
411 | status=mkdir(wd,mode); |
---|
412 | rtems_test_assert(status==0); |
---|
413 | status=chdir(wd); |
---|
414 | rtems_test_assert(status==0); |
---|
415 | |
---|
416 | umask(00); |
---|
417 | |
---|
418 | fd=creat(file01,mode); |
---|
419 | status=close(fd); |
---|
420 | rtems_test_assert(status==0); |
---|
421 | status=stat(file01,&statbuf); |
---|
422 | tmp_mode = (statbuf.st_mode) & ALLPERMS; |
---|
423 | rtems_test_assert(tmp_mode==mode); |
---|
424 | |
---|
425 | /* |
---|
426 | *Change the file mode uid and gid |
---|
427 | */ |
---|
428 | |
---|
429 | status=chmod(file01,file_mode); |
---|
430 | rtems_test_assert(status==0); |
---|
431 | status=chown(file01,user_id,group_id); |
---|
432 | rtems_test_assert(status==0); |
---|
433 | status=stat(file01,&statbuf); |
---|
434 | tmp_mode = (statbuf.st_mode) & ALLPERMS; |
---|
435 | rtems_test_assert(tmp_mode==file_mode); |
---|
436 | rtems_test_assert(user_id==statbuf.st_uid); |
---|
437 | rtems_test_assert(group_id==statbuf.st_gid); |
---|
438 | |
---|
439 | status=mkdir(directory01,mode); |
---|
440 | rtems_test_assert(status==0); |
---|
441 | status=stat(directory01,&statbuf); |
---|
442 | tmp_mode = (statbuf.st_mode) & ALLPERMS; |
---|
443 | printf("The directory file mode is %0o\n",(unsigned int)tmp_mode); |
---|
444 | rtems_test_assert(tmp_mode==mode); |
---|
445 | |
---|
446 | /* |
---|
447 | *Change the directory file mode |
---|
448 | */ |
---|
449 | status=chmod(directory01,file_mode); |
---|
450 | rtems_test_assert(status==0); |
---|
451 | status=stat(directory01,&statbuf); |
---|
452 | tmp_mode = (statbuf.st_mode) & ALLPERMS; |
---|
453 | rtems_test_assert(tmp_mode==file_mode); |
---|
454 | printf("The directory file mode is %0o\n",(unsigned int)tmp_mode); |
---|
455 | |
---|
456 | /* |
---|
457 | * Go back to parent directory |
---|
458 | */ |
---|
459 | status=chdir(".."); |
---|
460 | rtems_test_assert(status==0); |
---|
461 | } |
---|
462 | void root_test(void ) |
---|
463 | { |
---|
464 | int fd; |
---|
465 | int sc; |
---|
466 | |
---|
467 | fd =creat("test",0000); |
---|
468 | sc=close(fd); |
---|
469 | rtems_test_assert(sc==0); |
---|
470 | |
---|
471 | fd=open("test",O_RDONLY); |
---|
472 | rtems_test_assert(fd!=-1); |
---|
473 | } |
---|
474 | |
---|
475 | void test(void ) |
---|
476 | { |
---|
477 | puts( "\n\n*** PERMISSION TEST ***" ); |
---|
478 | umask_test01(); |
---|
479 | test_premission01(); |
---|
480 | test_premission02(); |
---|
481 | root_test(); |
---|
482 | puts( "*** END OF PERMISSION TEST ***" ); |
---|
483 | } |
---|