user1071840
user1071840

Reputation: 3592

How to accurately fetch Unix permissions a user has on a given fs object?

This is the logic I have now:

  int getInodePermissions(final int uid, final int[] gids, final fsObject inode)
  {
     final int inodeOwner = getOwner(inode);
     final int inodeGroup = getGroup(inode);
     final int inodeMode = getMode(inode);

     int perm;
     if (uid == inodeOwner) {
         perm = (inodeMode >> 6) & 7;
     } else if (isUserInGroup(inodeGroup, gids)) {
        perm = (inodeMode >> 3) & 7;
     } else {
        perm = inodeMode & 7;
     }
     return perm;
  }

But I just realized that if we have e.g. x bit as owner and w bit as group, we should you get both x and w. I tried looking up some tutorials but they explain individual access not combinations of different groups. Could some either direct me to such a description or list all cases so I can make sure I'm not missing any.

Upvotes: 1

Views: 32

Answers (1)

wholerabbit
wholerabbit

Reputation: 11536

if we have e.g. x bit as owner and w bit as group, we should you get both x and w

Nope! If you are the owner, you get the owner permissions, not the group permissions, even if you are also in the group. For example:

> touch example.file

Presuming example.file did not already exist, if you have a normal umask, the file's permissions will be 664, with your eponymous group as the group. Now:

> sudo chmod 077 example.file
> echo whatever >> example.file
bash: example.file: Permission denied

Even though you are in the group and the group has rwx permission, you are denied because you are the owner, and the owner has no permissions.

So, what you already have is correct!

Upvotes: 2

Related Questions