--- a/progs/read.c Sat Dec 27 11:58:07 2014 +0000
+++ b/progs/read.c Tue Dec 30 01:22:25 2014 +0000
@@ -1,6 +1,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
+#include <errno.h>
int main(int argc, char *argv[])
{
@@ -9,23 +10,23 @@
printf("Real UID = %d\n", getuid());
printf("Effective UID = %d\n", geteuid());
- //read test
+ //read test (error 13 is permission denied)
if ((f = fopen(argv[1], "r")) == NULL) {
- fprintf(stderr, "%s is not readable\n", argv[1]);
+ fprintf(stderr, "%s is not readable, errno = %d\n", argv[1], errno);
} else {
fprintf(stderr, "%s is readable\n", argv[1]); fclose(f);
}
//write test
- if ((f = fopen(argv[1], "w")) == NULL) {
- fprintf(stderr, "%s is not writable\n", argv[1]);
+ if ((f = fopen(argv[1], "r+")) == NULL) {
+ fprintf(stderr, "%s is not writable, errno = %d\n", argv[1], errno);
} else {
fprintf(stderr, "%s is writable\n", argv[1]); fclose(f);
}
//lowering the access rights to the caller
if (setuid(getuid())) {
- fprintf(stderr, "Could not reset setuid\n"); return 1;
+ fprintf(stderr, "could not reset setuid, errno = %d\n", errno); return 1;
}
printf("Real UID = %d\n", getuid());
@@ -33,14 +34,14 @@
//read test
if ((f = fopen(argv[1], "r")) == NULL) {
- fprintf(stderr, "%s is not readable\n", argv[1]);
+ fprintf(stderr, "%s is not readable, errno = %d\n", argv[1], errno);
} else {
fprintf(stderr, "%s is readable\n", argv[1]); fclose(f);
}
//write test
if ((f = fopen(argv[1], "w")) == NULL) {
- fprintf(stderr, "%s is not writable\n", argv[1]);
+ fprintf(stderr, "%s is not writable, errno = %d\n", argv[1], errno);
} else {
fprintf(stderr, "%s is writable\n", argv[1]); fclose(f);
}