progs/read.c
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Thu, 05 Nov 2015 02:11:13 +0000
changeset 422 abe178b3197e
parent 365 942205605c30
permissions -rw-r--r--
finished ZKP protocols
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
360
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     1
#include <stdio.h>
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     2
#include <unistd.h>
364
e4a879d442d5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
     3
#include <errno.h>
360
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     4
365
942205605c30 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 364
diff changeset
     5
FILE *f; //file pointer
942205605c30 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 364
diff changeset
     6
942205605c30 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 364
diff changeset
     7
//tests return errno = 13 for permission denied
942205605c30 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 364
diff changeset
     8
void read_test(char *name) 
942205605c30 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 364
diff changeset
     9
{
942205605c30 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 364
diff changeset
    10
    if ((f = fopen(name, "r")) == NULL) {
942205605c30 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 364
diff changeset
    11
      printf("%s is not readable, errno = %d\n", name, errno);
942205605c30 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 364
diff changeset
    12
    } else {
942205605c30 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 364
diff changeset
    13
      printf("%s is readable\n", name); fclose(f);
942205605c30 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 364
diff changeset
    14
    }
942205605c30 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 364
diff changeset
    15
}
942205605c30 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 364
diff changeset
    16
942205605c30 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 364
diff changeset
    17
void write_test(char *name) 
942205605c30 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 364
diff changeset
    18
{
942205605c30 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 364
diff changeset
    19
    if ((f = fopen(name, "r+")) == NULL) {
942205605c30 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 364
diff changeset
    20
      printf("%s is not writable, errno = %d\n", name, errno);
942205605c30 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 364
diff changeset
    21
    } else {
942205605c30 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 364
diff changeset
    22
      printf("%s is writable\n", name); fclose(f);
942205605c30 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 364
diff changeset
    23
    }
942205605c30 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 364
diff changeset
    24
}
942205605c30 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 364
diff changeset
    25
360
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    26
int main(int argc, char *argv[])
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    27
{   
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    28
    printf("Real UID = %d\n", getuid());
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    29
    printf("Effective UID = %d\n", geteuid());
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    30
    
365
942205605c30 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 364
diff changeset
    31
    read_test(argv[1]);
942205605c30 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 364
diff changeset
    32
    write_test(argv[1]);
360
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    33
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    34
    //lowering the access rights to the caller
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    35
    if (setuid(getuid())) {  
365
942205605c30 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 364
diff changeset
    36
      printf("could not reset setuid, errno = %d\n", errno); 
942205605c30 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 364
diff changeset
    37
      return 1;
360
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    38
    }
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    39
        
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    40
    printf("Real UID = %d\n", getuid());
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    41
    printf("Effective UID = %d\n", geteuid());      
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    42
365
942205605c30 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 364
diff changeset
    43
    read_test(argv[1]);
942205605c30 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 364
diff changeset
    44
    write_test(argv[1]);
360
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    45
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    46
    return 0;
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    47
}