简体   繁体   English

如何检查用户是否具有执行权限?

[英]How can I check to see if user has execute permissions?

I wish to know how to check if the "user" (other than who is running this program) has execute permissions on a file ? 我想知道如何检查“用户”(运行此程序的用户除外)是否对文件具有执行权限? [C api] [C api]

I have looked at "access" it gives information with respect to the caller. 我查看了“访问”,它提供了与调用者有关的信息。

I am looking for something like :- 我正在寻找类似的东西: -

"<cmd> <user_name> <file_name>"

here I am trying to get if <user_name> has execute permissions for <file_name> ? 如果<user_name>具有<file_name>执行权限,我试图获取?

I am looking C api ? 我在寻找C api?

Possible solution :- I am using the following algo to get this information 可能的解决方案: - 我使用以下算法来获取此信息

boolean_t
is_user_has_execute_permissions(char *run_as_user)
{
        /* Check world execute permission */
        if ((cmd_stat.st_mode & S_IXOTH) == S_IXOTH) {
                return (TRUE);
        }

        /* group id for run_as_user */
        getpwnam_r(run_as_user, &pw, buf, passwd_len);

        /* Check group execute permission */
        if ((cmd_stat.st_mode & S_IXGRP) == S_IXGRP) {
                if (pw->pw_gid == cmd_stat.st_gid)
                        return (TRUE);
        }

        return (FALSE);
}

Did anyone see any error in this one ? 有没有人看到这个错误?

You need the call stat(2), which returns the permission bits, for the owner of the file, the owner group, and the others. 您需要调用stat(2),它返回文件所有者,所有者组和其他人的权限位。 Than you have to find out the id of the user you're interested in and ids its groups: see getpwent(3) and getgrouplist(3). 您必须找出您感兴趣的用户的ID并且需要其组:请参阅getpwent(3)和getgrouplist(3)。 The first which match, will give the resulting permissions. 匹配的第一个将给出结果权限。

From the command line, you can use an Awk program easily enough. 从命令行,您可以轻松地使用Awk程序。 Something like 就像是

ls -l filename | awk -F '{ if (substring($1,3,1) == "x" exit(0); exit(1)}'

will set the return code to 0 if it's found, 1 if it's not. 如果找到则将返回代码设置为0,如果不是则将其设置为1。

From a C program, you want to use fstat . 从C程序,您想要使用fstat Basically you open the file 基本上你打开文件

int fd = fopen("filename", "r");

then get the file stat block with fstat 然后使用fstat获取文件stat块

fstat(fd, &bufr)

and look at bufr.st_mode . 并查看bufr.st_mode

Here's a description of fstat . 这是fstat的描述。

Update 更新

I'll note crankily that when the OP originally posted, it wasn't clear the C API was what was desired. 我会注意到,当OP最初发布时,不清楚C API是否是所期望的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何检查C ++代码中的文件写入权限? - how can i check file write permissions in C++ code? 如何在没有在C中的Linux上打开文件的情况下检查我是否有权打开文件? - How can I check if I have permissions to open a file without opening it on Linux in C? Unix Shell:如何检查用户输入以查看它是否是有效的Unix命令? - Unix shell: how do I check user input to see if it's a valid unix command? 如何使用设置向导(即设置组标识)漏洞执行权限受限的文件? - How can I use the set-guid (i.e., set group identification) vulnerability to execute a file with limited permissions? C:如何检查是否已写入 memory 地址? - C: How can I check if a memory address has been written? 在 C++ 中创建一个只有用户可以看到和编辑的权限的文件 - Making a file in C++ with permissions that only the user can see the file and edit it 如何分配具有执行权限的内存? - How to allocate a memory with execute permissions? 如何获得有效的文件权限? - How can I get effective file permissions? 如何从 Linux kernel 空间模块执行/调用用户空间定义的 function? - How can I Execute/Call a user-space defined function from Linux kernel space module? 我可以检查待处理的信号并立即执行它们 - Can I check for pending signals and execute them immediately
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM