360
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
1 |
#include <stdio.h>
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
2 |
#include <unistd.h>
|
364
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
3 |
#include <errno.h>
|
360
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
4 |
|
365
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
5 |
FILE *f; //file pointer
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
6 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
7 |
//tests return errno = 13 for permission denied
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
8 |
void read_test(char *name)
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
9 |
{
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
10 |
if ((f = fopen(name, "r")) == NULL) {
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
11 |
printf("%s is not readable, errno = %d\n", name, errno);
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
12 |
} else {
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
13 |
printf("%s is readable\n", name); fclose(f);
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
14 |
}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
15 |
}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
16 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
17 |
void write_test(char *name)
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
18 |
{
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
19 |
if ((f = fopen(name, "r+")) == NULL) {
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
20 |
printf("%s is not writable, errno = %d\n", name, errno);
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
21 |
} else {
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
22 |
printf("%s is writable\n", name); fclose(f);
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
23 |
}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
24 |
}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
25 |
|
360
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
26 |
int main(int argc, char *argv[])
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
27 |
{
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
28 |
printf("Real UID = %d\n", getuid());
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
29 |
printf("Effective UID = %d\n", geteuid());
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
30 |
|
365
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
31 |
read_test(argv[1]);
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
32 |
write_test(argv[1]);
|
360
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
33 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
34 |
//lowering the access rights to the caller
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
35 |
if (setuid(getuid())) {
|
365
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
36 |
printf("could not reset setuid, errno = %d\n", errno);
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
37 |
return 1;
|
360
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
38 |
}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
39 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
40 |
printf("Real UID = %d\n", getuid());
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
41 |
printf("Effective UID = %d\n", geteuid());
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
42 |
|
365
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
43 |
read_test(argv[1]);
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
44 |
write_test(argv[1]);
|
360
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
45 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
46 |
return 0;
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
47 |
}
|