progs/read.c
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Tue, 30 Dec 2014 01:22:25 +0000
changeset 364 e4a879d442d5
parent 360 eb2004430215
child 365 942205605c30
permissions -rw-r--r--
updated
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 <stdlib.h>
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     3
#include <unistd.h>
364
e4a879d442d5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
     4
#include <errno.h>
360
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     5
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     6
int main(int argc, char *argv[])
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     7
{   
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     8
    FILE *f; //file pointer
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     9
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    10
    printf("Real UID = %d\n", getuid());
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    11
    printf("Effective UID = %d\n", geteuid());
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    12
    
364
e4a879d442d5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
    13
    //read test (error 13 is permission denied)
360
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    14
    if ((f = fopen(argv[1], "r")) == NULL) {
364
e4a879d442d5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
    15
      fprintf(stderr, "%s is not readable, errno = %d\n", argv[1], errno);
360
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    16
    } else {
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    17
      fprintf(stderr, "%s is readable\n", argv[1]); fclose(f);
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    18
    }
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    19
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    20
    //write test
364
e4a879d442d5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
    21
    if ((f = fopen(argv[1], "r+")) == NULL) {
e4a879d442d5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
    22
      fprintf(stderr, "%s is not writable, errno = %d\n", argv[1], errno);
360
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    23
    } else {
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    24
      fprintf(stderr, "%s is writable\n", argv[1]); fclose(f);
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    25
    }
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    26
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    27
    //lowering the access rights to the caller
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    28
    if (setuid(getuid())) {  
364
e4a879d442d5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
    29
      fprintf(stderr, "could not reset setuid, errno = %d\n", errno); return 1;
360
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    30
    }
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    31
        
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    32
    printf("Real UID = %d\n", getuid());
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    33
    printf("Effective UID = %d\n", geteuid());      
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    34
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    35
    //read test
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    36
    if ((f = fopen(argv[1], "r")) == NULL) {
364
e4a879d442d5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
    37
      fprintf(stderr, "%s is not readable, errno = %d\n", argv[1], errno);
360
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    38
    } else {
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    39
      fprintf(stderr, "%s is readable\n", argv[1]); fclose(f);
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    40
    }
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    41
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    42
    //write test
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    43
    if ((f = fopen(argv[1], "w")) == NULL) {
364
e4a879d442d5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
    44
      fprintf(stderr, "%s is not writable, errno = %d\n", argv[1], errno);
360
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    45
    } else {
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    46
      fprintf(stderr, "%s is writable\n", argv[1]); fclose(f);
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    47
    }
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    48
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    49
    return 0;
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    50
}