sfakeroot

manipulate files faking root privileges
git clone git://git.vx21.xyz/sfakeroot
Log | Files | Refs | README | LICENSE

chmod.c (1199B)


      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 #include <string.h>
      4 #include <sys/stat.h>
      5 #include <unistd.h>
      6 #include <fcntl.h>
      7 #include <errno.h>
      8 
      9 int main(void)
     10 {
     11     char *template = "/tmp/sfakeroot.test.XXXXXX";
     12     char path[strlen(template) + 1];
     13     struct stat st_before;
     14     struct stat st_after;
     15     int fd;
     16 
     17     strcpy(path, template);
     18     printf("test making path %s\n", mktemp(path));
     19 
     20     if ((fd = creat(path, 777)) == -1) {
     21         fprintf(stderr, "creat '%s' failed: %s\n", path, strerror(errno));
     22         return 1;
     23     }
     24     close(fd);
     25 
     26     if (stat(path, &st_before) == -1)
     27     {
     28         fprintf(stderr, "stat failed\n");
     29         return 1;
     30     }
     31     if (st_before.st_uid != 0 || st_before.st_gid != 0) {
     32         fprintf(stderr, "uid/gid not 0\n");
     33         return 1;
     34     }
     35 
     36     if (chmod(path, 0444) == -1) {
     37         fprintf(stderr, "chown '%s' failed: %s\n", path, strerror(errno));
     38         return 1;
     39     }
     40 
     41     if (stat(path, &st_after) == -1)
     42     {
     43         fprintf(stderr, "stat failed\n");
     44         return 1;
     45     }
     46     if ((st_after.st_mode & ~S_IFMT) != 0444) {
     47         fprintf(stderr, "mode not 444, it is %o\n", st_after.st_mode & ~S_IFMT);
     48         return 1;
     49     }
     50 
     51     return 0;
     52 }
     53