Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.25 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/stat.h>
  4.  
  5. struct Task
  6. {
  7.     unsigned uid;
  8.     int gid_count;
  9.     unsigned *gids;
  10. };
  11.  
  12. static int isuser(const struct stat *stb, const struct Task *task) {
  13.     if (stb->st_uid == task->uid) {
  14.         return 1;
  15.     }
  16.     return 0;
  17. }
  18.  
  19. static int isgrp (const struct stat *stb, const struct Task *task) {
  20.     for (int i = 0; i < task->gid_count; i++) {
  21.         if (task->gids[i] == stb->st_gid) {
  22.             return 1;
  23.         }
  24.     }
  25.     return 0;
  26. }
  27.  
  28. enum masks
  29. {
  30.     USER = 0700,
  31.     GROUP = 0070,
  32.     OTHER = 0007,
  33.     U_OFFSET = 6,
  34.     G_OFFSET = 3
  35. };
  36.  
  37. enum { permissions = 3 };
  38.  
  39. int myaccess(const struct stat *stb, const struct Task *task, int access) {
  40.     if (task->uid == 0) {
  41.         return 1;
  42.     }
  43.     int mode;
  44.     if (isuser(stb, task)) {
  45.         mode = stb->st_mode & USER;
  46.         mode >>= U_OFFSET;
  47.         if (access != mode) {
  48.             return 0;
  49.         }
  50.     } else if (isgrp(stb, task)) {
  51.         mode = stb->st_mode & GROUP;
  52.         mode >>= G_OFFSET;
  53.         if (access != mode) {
  54.             return 0;
  55.         }
  56.     } else {
  57.         mode = stb->st_mode & OTHER;
  58.         if (access != mode) {
  59.             return 0;
  60.         }
  61.     }
  62.     return 1;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement