0

我正在尝试根据我的 java Web 应用程序中的登录用户执行文件操作。为此,我使用 JNI 原生实现将 fs uid & fs gid 设置为登录用户的 uid 和 gid。现在,仅当登录用户具有权限时才允许文件操作。

我还想检索登录用户是否具有文件的读/写/执行权限。尝试使用 access、faccessat 系统调用,但它们似乎没有使用 fs uid。

如何获取已登录用户的文件权限?

4

1 回答 1

0

找到了解决问题的简单方法。不知道它有多完整。它不考虑acls。

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;
    }
}
于 2014-11-02T07:07:06.063 回答