简体   繁体   English

如何在python中检查另一个用户的文件权限?

[英]How do I check another user's file permissions in python?

In python I can check whether my own user can access a file or directory using os.access .在 python 中,我可以检查我自己的用户是否可以使用os.access访问文件或目录。 However, is it possible check whether another user can do so?但是,是否可以检查其他用户是否可以这样做?

One possibility is to check the user's uid and groups against the permissions on the file:一种可能性是根据文件的权限检查用户的 uid 和组:

import os, grp, pwd, stat
def user_access(uid, path, perms=os.R_OK | os.W_OK):
    stats = os.stat(path)
    # other users
    if stats.st_mode & perms == perms:
        return True
    # owner
    if stats.st_uid == uid and (stats.st_mode >> 6) & perms == perms:
        return True
    # group
    user = pwd.getpwuid(uid).pw_name
    if (stats.st_mode >> 3) & perms == perms and user in grp.getgrgid(stats.st_gid).gr_mem:
         return True
    return False
        

However, I guess this is specific to POSIX systems and I could imagine there would be edge cases it fails on.但是,我想这是特定于 POSIX 系统的,我可以想象它会失败的边缘情况。 In fact, the libc manual on access specifically warns against this solution:事实上,关于accesslibc 手册特别警告这种解决方案:

There is another way you could check this access, which is ... to examine the file mode bits and mimic the system's own access computation.还有另一种方法可以检查此访问,即……检查文件模式位并模拟系统自己的访问计算。 This method is undesirable because many systems have additional access control features;这种方法是不可取的,因为许多系统具有额外的访问控制功能; your program cannot portably mimic them, and you would not want to try to keep track of the diverse features that different systems have.您的程序无法可移植地模仿它们,并且您不想尝试跟踪不同系统具有的不同功能。 Using access is simple and automatically does whatever is appropriate for the system you are using.使用访问权限很简单,并且会自动执行适合您正在使用的系统的任何操作。

Is there a more concise and/or robust solution for checking access of other users?是否有更简洁和/或更强大的解决方案来检查其他用户的访问权限?

Edit 2020-12-09: Fixed bugs with the bit twiddling.编辑 2020-12-09:修复了一些小问题。

I came up with a bad answer.我想出了一个糟糕的答案。 Basically, you use os.setuid to switch users, then use os.access on the file.基本上,您使用os.setuid切换用户,然后在文件上使用os.access

import multiprocessing as mp
def user_access_mp(uid, path, perms=os.R_OK | os.W_OK):
    def disguise():
        os.setuid(uid)
        allowed = os.access(path, perms)
        exit(int(allowed))

    p = mp.Process(target=disguise)
    p.start()
    p.join()
    assert p.exitcode in (0, 1)  # other values indicate unexpected signals
    return bool(p.exitcode)

Unfortunately this (1) has to be done as root, (2) requires forking, since there's no way to setuid back to the original user, and (3) is slow.不幸的是,这 (1) 必须以 root 身份完成,(2) 需要分叉,因为无法将 setuid 返回给原始用户,并且 (3) 很慢。

Furthermore, it doesn't even work consistently on the system I care about, a RHEL server using LDAP.此外,它甚至无法在我关心的系统(使用 LDAP 的 RHEL 服务器)上始终如一地工作。 In this case I get 'False' for some files that I belong to the group of.在这种情况下,对于我属于该组的某些文件,我得到“假”。 Most likely this is because the system uses LDAP for groups, so some groups don't appear in a user's group list.这很可能是因为系统将 LDAP 用于组,因此某些组不会出现在用户的组列表中。 It might be possible to bypass this using the C API (as done to fix grp.getgrall() ), but I haven't tested.可能可以使用 C API 绕过它(就像修复grp.getgrall() 那样),但我还没有测试过。 Anyways the requirement to run as root severely limits this solution.无论如何,以 root 身份运行的要求严重限制了这个解决方案。

I've never really tried this before, but let me know if it works:我以前从未真正尝试过,但请告诉我它是否有效:

import os
import stat

def isgroupreadable(filepath):
  st = os.stat(filepath)
  return bool(st.st_mode & stat.S_IRGRP)

If you wanted to print this, you could add at the end:如果你想打印这个,你可以在最后添加:

print(isgroupreadable(filepath))打印(isgroup可读(文件路径))

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM