【问题标题】:linux access system call not working as expectedlinux访问系统调用未按预期工作
【发布时间】:2014-11-02 15:07:06
【问题描述】:

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

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

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

【问题讨论】:

  • 一个相关问题 - stackoverflow.com/questions/1223600/…。但是,解决方案没有说明如何获取文件权限。我需要能够在尝试之前检测操作是否可行。
  • 如果 setuid 设置为登录用户,则无法重置原始 uid。看起来没有办法做到这一点:(

标签: linux java-native-interface file-permissions setfsuid


【解决方案1】:

找到了解决问题的简单方法。不知道它有多完整。它不考虑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;
    }
}

【讨论】:

    猜你喜欢
    • 2015-03-15
    • 1970-01-01
    • 2018-11-29
    • 1970-01-01
    • 1970-01-01
    • 2018-01-17
    • 1970-01-01
    • 1970-01-01
    • 2014-08-01
    相关资源
    最近更新 更多