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