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?
Asked
Active
Viewed 298 times
2

Kusalananda
- 333,661

slashmais
- 551
1 Answers
1
Thanks to roaima's comment I came up with the following:
//snip...
#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);
...
//snip
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.

slashmais
- 551
-
-
1http://man7.org/linux/man-pages/man2/access.2.html
access()
use real UID and GID, which isn't the case when you try to open or execute a file. http://man7.org/linux/man-pages/man2/chmod.2.html sayschmod()
don't use realUID – 炸鱼薯条德里克 Aug 03 '19 at 12:01 -
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 03 '19 at 09:48man 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. – Chris Davies Aug 03 '19 at 09:55man access
gave me the solution, thx. CallingbcanR=(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 03 '19 at 10:21