progs/read.c
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Sat, 27 Dec 2014 04:10:36 +0000
changeset 360 eb2004430215
child 364 e4a879d442d5
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>
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     4
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     5
int main(int argc, char *argv[])
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     6
{   
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     7
    FILE *f; //file pointer
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     8
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     9
    printf("Real UID = %d\n", getuid());
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    10
    printf("Effective UID = %d\n", geteuid());
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    11
    
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    12
    //read test
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    13
    if ((f = fopen(argv[1], "r")) == NULL) {
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    14
      fprintf(stderr, "%s is not readable\n", argv[1]);
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    15
    } else {
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    16
      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
    17
    }
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
    //write test
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    20
    if ((f = fopen(argv[1], "w")) == NULL) {
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    21
      fprintf(stderr, "%s is not writable\n", argv[1]);
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    22
    } else {
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    23
      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
    24
    }
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
    //lowering the access rights to the caller
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    27
    if (setuid(getuid())) {  
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    28
      fprintf(stderr, "Could not reset setuid\n"); return 1;
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    29
    }
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
    printf("Real UID = %d\n", getuid());
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    32
    printf("Effective UID = %d\n", geteuid());      
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
    //read test
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    35
    if ((f = fopen(argv[1], "r")) == NULL) {
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    36
      fprintf(stderr, "%s is not readable\n", argv[1]);
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    37
    } else {
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    38
      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
    39
    }
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
    //write test
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    42
    if ((f = fopen(argv[1], "w")) == NULL) {
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    43
      fprintf(stderr, "%s is not writable\n", argv[1]);
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    44
    } else {
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    45
      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
    46
    }
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
    return 0;
eb2004430215 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    49
}