Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <sys/stat.h>
- struct Task
- {
- unsigned uid;
- int gid_count;
- unsigned *gids;
- };
- static _Bool find_gid(unsigned gid, const unsigned *gids, int gid_count)
- {
- for (int i = 0; i < gid_count; i++) {
- if (gid == gids[i]) {
- return 1;
- }
- }
- return 0;
- }
- static _Bool check_mode(int access, int mode)
- {
- enum { OK = 0b111 };
- return ((~access | mode) & OK) == OK;
- }
- int myaccess(const struct stat *stb, const struct Task *task, int access)
- {
- enum {
- MASK = 0b111,
- U_SHIFT = 6,
- G_SHIFT = 3
- };
- if (!task->uid || (stb->st_uid == task->uid && check_mode(access, stb->st_mode >> U_SHIFT))) {
- return 1;
- }
- if (find_gid(stb->st_gid, task->gids, task->gid_count) &&
- check_mode(access, (stb->st_mode >> G_SHIFT) & MASK)) {
- return 1;
- }
- if (check_mode(access, stb->st_mode & MASK)) {
- return 1;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement