gpt4 book ai didi

linux 访问系统调用未按预期工作

转载 作者:太空宇宙 更新时间:2023-11-04 03:49:56 24 4
gpt4 key购买 nike

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

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

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

最佳答案

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

关于linux 访问系统调用未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26682067/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com