在python中以超级用户身份打开文件 [英] Open a file as superuser in python

查看:222
本文介绍了在python中以超级用户身份打开文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须打开一个系统文件并从中读取.该文件通常只能由root用户(超级用户)读取.我有一种向用户询问超级用户密码的方法.我想使用此凭据打开文件并从中读取文件,而无需让我的整个程序作为超级用户进程运行.有没有办法以多平台的方式实现这一目标?

I have to open a system file and read from it. This file is usually only readable by root (the super user). I have a way to ask the user for the superuser password. I would like to use this credentials to open the file and read from it without having my entire program running as a superuser process. Is there a way to achieve this in a multiplatform way?

推荐答案

由于特权在类似Unix的系统和Windows上完全不同,因此您将需要特定于平台的代码.无论如何,您都需要将程序分成两个单独的程序,其中一个以提升的权限运行,而另一个以标准/减少的权限运行.

Since privileges work completely differently on Unix-like systems and Windows, you're going to need to have platform-specific code. In any case, you'll need to break up your program into two separate programs, one of which runs with elevated permissions and the other of which runs with standard/reduced permissions.

在类似Unix的系统(包括Linux和Mac OS X)中,以提升的权限运行的可执行文件应执行以下操作:

In Unix-like systems (including Linux and Mac OS X), the executable that runs with elevated permissions should do this:

  1. 假设您以root用户身份运行并打开文件以进行读取.由于您提到文件很大,因此您实际上并没有读取整个文件,只需保留一个打开的文件描述符.如果打开失败,则打印错误消息并退出.
  2. 使用 setreuid(2) exec(3) 函数之一执行无特权的可执行文件.
  3. li>
  4. 如果要创建它,以便无需使用sudo即可运行该程序,请使其成为root用户并使用chown root the-program; chmod +s the-program将其设置为set-user-ID可执行文件.
  1. Assume you're running as root and open the file for reading. Since you mentioned that the file is very large, you don't actually read the whole file in, you just keep an open file descriptor. If opening it fails, print an error message and exit.
  2. Use setreuid(2) and setregid(2) to set your user ID and group ID back to an unprivileged user.
  3. Use one of the exec(3) functions to execute the unprivileged executable.
  4. If you want to make it so that you can run this program without using sudo, then make it owned by root and make it a set-user-ID executable with chown root the-program; chmod +s the-program.

该非特权程序现在将以正常权限运行,但是启动时,它将具有一个打开的文件描述符(文件描述符#3),可用于从您的特殊文件中读取.

The unprivileged program will now be run with normal permissions, but when it starts up, it will have an open file descriptor (file descriptor #3) that can be used to read from your special file.

对于Windows,它相似但略有不同:

For Windows, it's similar but slightly different:

  1. 假定您以root用户身份运行,并使用 CreateFile .不要使用默认的安全属性-在bInheritHandle设置为TRUE的情况下创建一个SECURITY_ATTRIBUTES结构,以便该句柄将被子进程继承.如果打开文件失败,请打印一条错误消息并退出.
  2. 使用 CreateProcess 启动您的子进程.在命令行中传递上方的句柄(例如打印为数值);您还可以使用共享内存区域,但这比这个问题的价值还大.
  3. 在此可执行文件中嵌入清单,且requireAdministrator设置为true.完成此操作后,在运行程序时,会出现一个UAC提示,询问您是否要允许程序进行更改.
  1. Assume you're running as root and open the file for reading using CreateFile. Do not use default security attributes -- create a SECURITY_ATTRIBUTES structure with bInheritHandle set to TRUE so that the handle will be inherited by child processes. If opening the file failed, print an error message and exit.
  2. Use CreateProcess to launch your child process. Pass in the handle above on the command line (e.g. printed as a numerical value); you could also use a shared memory region, but that's more trouble than it's worth for this problem.
  3. Embed a manifest in this executable with requireAdministrator set to true. After you do this, when you run the program, you'll get a UAC prompt asking you if you want to allow the program to makes changes.

然后,子进程通过解析命令行来抓取继承的句柄,然后可以根据需要读取数据.

The child process then does grabs the inherited handle by parsing the command line, and it can then read in the data as it pleases.

这种方法的一个问题是,当您继承一个句柄时,您必须使用低级系统调用(在Unix上为read(2),在Windows上为ReadFile)来读取它-您不能使用更高级别的功能,例如C的fread(3)或C ++的iostream s(好的,Unix具有fdopen(3),但是据我所知,在Windows上没有等效的功能).

One problem with this approach is that when you inherit a handle, you have to use the low-level system calls (read(2) on Unix, ReadFile on Windows) to read from it -- you can't use higher-level functions like C's fread(3) or C++'s iostreams (ok, Unix has fdopen(3), but there's no equivalent on Windows as far as I'm aware).

我敢肯定,您现在已经注意到,以上所有内容都在C中.在Unix中,由于 Pywin32 ,但坚持使用C可能会更容易.

As I'm sure you've noticed by now, everything above has been in C. In Unix, this translates pretty straightforwardly into Python, since the os module has lots of goodies like setreuid, exec*, and fdopen. On Windows, you might be able to do some of this stuff with the ctypes module and/or Pywin32, but it's probably easier to stick with C.

这篇关于在python中以超级用户身份打开文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆