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

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(int argc, char *argv[])
{   
    FILE *f; //file pointer

    printf("Real UID = %d\n", getuid());
    printf("Effective UID = %d\n", geteuid());
    
    //read test
    if ((f = fopen(argv[1], "r")) == NULL) {
      fprintf(stderr, "%s is not readable\n", argv[1]);
    } else {
      fprintf(stderr, "%s is readable\n", argv[1]); fclose(f);
    }

    //write test
    if ((f = fopen(argv[1], "w")) == NULL) {
      fprintf(stderr, "%s is not writable\n", argv[1]);
    } else {
      fprintf(stderr, "%s is writable\n", argv[1]); fclose(f);
    }

    //lowering the access rights to the caller
    if (setuid(getuid())) {  
      fprintf(stderr, "Could not reset setuid\n"); return 1;
    }
        
    printf("Real UID = %d\n", getuid());
    printf("Effective UID = %d\n", geteuid());      

    //read test
    if ((f = fopen(argv[1], "r")) == NULL) {
      fprintf(stderr, "%s is not readable\n", argv[1]);
    } else {
      fprintf(stderr, "%s is readable\n", argv[1]); fclose(f);
    }

    //write test
    if ((f = fopen(argv[1], "w")) == NULL) {
      fprintf(stderr, "%s is not writable\n", argv[1]);
    } else {
      fprintf(stderr, "%s is writable\n", argv[1]); fclose(f);
    }

    return 0;
}