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