progs/read.c
changeset 360 eb2004430215
child 364 e4a879d442d5
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/progs/read.c	Sat Dec 27 04:10:36 2014 +0000
@@ -0,0 +1,49 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+int main(int argc, char *argv[])
+{   
+    FILE *f; //file pointer
+
+    printf("Real UID = %d\n", getuid());
+    printf("Effective UID = %d\n", geteuid());
+    
+    //read test
+    if ((f = fopen(argv[1], "r")) == NULL) {
+      fprintf(stderr, "%s is not readable\n", argv[1]);
+    } 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]);
+    } 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;
+    }
+        
+    printf("Real UID = %d\n", getuid());
+    printf("Effective UID = %d\n", geteuid());      
+
+    //read test
+    if ((f = fopen(argv[1], "r")) == NULL) {
+      fprintf(stderr, "%s is not readable\n", argv[1]);
+    } 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]);
+    } else {
+      fprintf(stderr, "%s is writable\n", argv[1]); fclose(f);
+    }
+
+    return 0;
+}