I am trying to do file operations depending on logged-in user in my java web application. For this, I have used JNI native implementation to set the fs uid & fs gid to the logged-in user's uid and gid. Now, file operations are allowed only if the logged-in user has permissions.
I also want to retrieve whether the logged-in user has read/write/execute permissions for a file. Tried to use the access, faccessat system calls but they do not seem to be using the fs uid.
How do I get the file permissions for a logged-in user?
Found a simple way of solving the problem. Not sure how complete it is. It does not take acls into account.
struct passwd *pw = getpwnam(userName);
if (pw == NULL) {
return NULL;
}
jint fill[3];//rwx - 1 indicates success, 0 indicates failure
if(pw->pw_uid == 0) {
fill[0] = fill[1] = fill[2] = 1;
} else {
struct stat info;
stat(path, &info);
int mode = info.st_mode;
if(pw->pw_uid == info.st_uid) {
fill[0] = mode & S_IRUSR ? 1 : 0; /* 3 bits for user */
fill[1] = mode & S_IWUSR ? 1 : 0;
fill[2] = mode & S_IXUSR ? 1 : 0;
} else if(pw->pw_gid == info.st_gid) {
fill[0] = mode & S_IRGRP ? 1 : 0; /* 3 bits for group */
fill[1] = mode & S_IWGRP ? 1 : 0;
fill[2] = mode & S_IXGRP ? 1 : 0;
} else {
fill[0] = mode & S_IROTH ? 1 : 0; /* 3 bits for group */
fill[1] = mode & S_IWOTH ? 1 : 0;
fill[2] = mode & S_IXOTH ? 1 : 0;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.