Advertisement
Guest User

Untitled

a guest
Oct 15th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.97 KB | None | 0 0
  1. #include <sys/stat.h>
  2.  
  3.  
  4. struct Task
  5. {
  6.     unsigned uid;
  7.     int gid_count;
  8.     unsigned *gids;
  9. };
  10.  
  11.  
  12. static _Bool find_gid(unsigned gid, const unsigned *gids, int gid_count)
  13. {
  14.     for (int i = 0; i < gid_count; i++) {
  15.         if (gid == gids[i]) {
  16.             return 1;
  17.         }
  18.     }
  19.     return 0;
  20. }
  21.  
  22.  
  23. static _Bool check_mode(int access, int mode)
  24. {
  25.     enum { OK = 0b111 };
  26.     return ((~access | mode) & OK) == OK;
  27. }
  28.  
  29.  
  30. int myaccess(const struct stat *stb, const struct Task *task, int access)
  31. {
  32.     enum {
  33.         MASK = 0b111,
  34.         U_SHIFT = 6,
  35.         G_SHIFT = 3
  36.     };
  37.     if (!task->uid || (stb->st_uid == task->uid && check_mode(access, stb->st_mode >> U_SHIFT))) {
  38.         return 1;
  39.     }
  40.     if (find_gid(stb->st_gid, task->gids, task->gid_count) &&
  41.         check_mode(access, (stb->st_mode >> G_SHIFT) & MASK)) {
  42.         return 1;
  43.     }
  44.     if (check_mode(access, stb->st_mode & MASK)) {
  45.         return 1;
  46.     }
  47.     return 0;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement