I can get the real and effective UIDs of the current user, so, given some file or directory, how can I use these UIDs to check the rights, rwx, that this specific user has for accessing/modifying the file or directory?

  • So, for a given file or directory, return the permissions that applies to a specific user given their UID and/or group memberships? – Kusalananda Aug 3 '19 at 9:41
  • @Kusalananda: yes, I currently use stat to check owner against uid,s only; but groups also apply, and users with root-access. I am hoping for some fast method to check all the possibilities.. (ps thx for edits) – slashmais Aug 3 '19 at 9:48
  • 1
    See man 1 access for a starting point, but IIRC there are caveats when using it with effective UID/GID values. I can't test right now so not offering a proper answer. – roaima Aug 3 '19 at 9:55
  • Another thought. Can you simply use exception handling at the point you need to make a change to the file or directory? – roaima Aug 3 '19 at 10:10
  • @roaima: your reference to man access gave me the solution, thx. Calling bcanR=(faccessat(0, fullpath, R_OK, AT_EACCESS)==0); (same for W_OK & X_OK) solves my need (I think, unless there's a gotcha:) – slashmais Aug 3 '19 at 10:21

Thanks to roaima's comment I came up with the following:

#include <fcntl.h>
#include <unistd.h>
    bool bcanR, bcanW, bcanX;
    //'fullpath' is to file or directory..
    bcanR=(faccessat(0, fullpath, R_OK, AT_EACCESS)==0);
    bcanW=(faccessat(0, fullpath, W_OK, AT_EACCESS)==0);
    bcanX=(faccessat(0, fullpath, X_OK, AT_EACCESS)==0);

which answers my need since it uses effective UID to check permissions.

Also based on this answer need to keep in mind that changing the permissions (using chmod) can only be done by the owner or root.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.