0

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?

Sandeep Chatterjee
  • 3,220
  • 9
  • 31
  • 47
user3911119
  • 273
  • 3
  • 14
  • A related question - http://stackoverflow.com/questions/1223600/change-uid-gid-only-of-one-thread-in-linux. However, solution does not tell how to get the file permissions. I need to be able to detect if an operation is possible before it is attempted. – user3911119 Nov 01 '14 at 04:22
  • if setuid is set to the logged in user, unable to reset back the original uid. Looks like there is no way to do it :( – user3911119 Nov 01 '14 at 19:27

1 Answers1

0

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;
    }
}
user3911119
  • 273
  • 3
  • 14