1 #include <stdio.h> |
1 #include <stdio.h> |
2 #include <stdlib.h> |
2 #include <stdlib.h> |
3 #include <unistd.h> |
3 #include <unistd.h> |
|
4 #include <errno.h> |
4 |
5 |
5 int main(int argc, char *argv[]) |
6 int main(int argc, char *argv[]) |
6 { |
7 { |
7 FILE *f; //file pointer |
8 FILE *f; //file pointer |
8 |
9 |
9 printf("Real UID = %d\n", getuid()); |
10 printf("Real UID = %d\n", getuid()); |
10 printf("Effective UID = %d\n", geteuid()); |
11 printf("Effective UID = %d\n", geteuid()); |
11 |
12 |
12 //read test |
13 //read test (error 13 is permission denied) |
13 if ((f = fopen(argv[1], "r")) == NULL) { |
14 if ((f = fopen(argv[1], "r")) == NULL) { |
14 fprintf(stderr, "%s is not readable\n", argv[1]); |
15 fprintf(stderr, "%s is not readable, errno = %d\n", argv[1], errno); |
15 } else { |
16 } else { |
16 fprintf(stderr, "%s is readable\n", argv[1]); fclose(f); |
17 fprintf(stderr, "%s is readable\n", argv[1]); fclose(f); |
17 } |
18 } |
18 |
19 |
19 //write test |
20 //write test |
20 if ((f = fopen(argv[1], "w")) == NULL) { |
21 if ((f = fopen(argv[1], "r+")) == NULL) { |
21 fprintf(stderr, "%s is not writable\n", argv[1]); |
22 fprintf(stderr, "%s is not writable, errno = %d\n", argv[1], errno); |
22 } else { |
23 } else { |
23 fprintf(stderr, "%s is writable\n", argv[1]); fclose(f); |
24 fprintf(stderr, "%s is writable\n", argv[1]); fclose(f); |
24 } |
25 } |
25 |
26 |
26 //lowering the access rights to the caller |
27 //lowering the access rights to the caller |
27 if (setuid(getuid())) { |
28 if (setuid(getuid())) { |
28 fprintf(stderr, "Could not reset setuid\n"); return 1; |
29 fprintf(stderr, "could not reset setuid, errno = %d\n", errno); return 1; |
29 } |
30 } |
30 |
31 |
31 printf("Real UID = %d\n", getuid()); |
32 printf("Real UID = %d\n", getuid()); |
32 printf("Effective UID = %d\n", geteuid()); |
33 printf("Effective UID = %d\n", geteuid()); |
33 |
34 |
34 //read test |
35 //read test |
35 if ((f = fopen(argv[1], "r")) == NULL) { |
36 if ((f = fopen(argv[1], "r")) == NULL) { |
36 fprintf(stderr, "%s is not readable\n", argv[1]); |
37 fprintf(stderr, "%s is not readable, errno = %d\n", argv[1], errno); |
37 } else { |
38 } else { |
38 fprintf(stderr, "%s is readable\n", argv[1]); fclose(f); |
39 fprintf(stderr, "%s is readable\n", argv[1]); fclose(f); |
39 } |
40 } |
40 |
41 |
41 //write test |
42 //write test |
42 if ((f = fopen(argv[1], "w")) == NULL) { |
43 if ((f = fopen(argv[1], "w")) == NULL) { |
43 fprintf(stderr, "%s is not writable\n", argv[1]); |
44 fprintf(stderr, "%s is not writable, errno = %d\n", argv[1], errno); |
44 } else { |
45 } else { |
45 fprintf(stderr, "%s is writable\n", argv[1]); fclose(f); |
46 fprintf(stderr, "%s is writable\n", argv[1]); fclose(f); |
46 } |
47 } |
47 |
48 |
48 return 0; |
49 return 0; |