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?
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.
In Unix-like systems (including Linux and Mac OS X), the executable that runs with elevated permissions should do this:
- 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.
- Use
setreuid(2)
andsetregid(2)
to set your user ID and group ID back to an unprivileged user. - Use one of the
exec(3)
functions to execute the unprivileged executable. - 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 withchown root the-program; chmod +s the-program
.
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.
For Windows, it's similar but slightly different:
- Assume you're running as root and open the file for reading using
CreateFile
. Do not use default security attributes -- create aSECURITY_ATTRIBUTES
structure withbInheritHandle
set toTRUE
so that the handle will be inherited by child processes. If opening the file failed, print an error message and exit. - 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. - Embed a manifest in this executable with
requireAdministrator
set totrue
. 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.
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 iostream
s (ok, Unix has fdopen(3)
, but there's no equivalent on Windows as far as I'm aware).
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.
What you're looking for is called privilege escalation, and it very much depends on the platform you're running on. In general, what your program would have to do is run a portion as the superuser. On unix systems, for instance, you might be able to use sudo
to read the contents of the file.
But as mentioned, this really depends on what system you're running on.
I would split the program in two.
- Handles opening the file and accessing the contents. It can assume it's started with the privileges it needs.
- Everything else that doesn't require special privileges.
Put a config entry which describes how to exec
or subprocess
the command that requires extra privileges. ie.
access_special_file: sudo access_special_file
or
access_special_file: runas /user:AccountWithPrivs access_special_file
This offloads some of the system specifics for privilege escalation to the system shell where there may be more convenient ways of gaining the permissions you need.
On linux it's a cynch, as @ViktorKerkez showed. This is how I streamed my WiFi passwords files (readable only by root/sudo):
import subprocess
import sys
# substitute your Windoze/DOS/PowerlessShell command here:
cat_wifi_pws = 'sudo cat /etc/NetworkManager/system-connections/*'
process = subprocess.Popen(cat_wifi_pws, stdout=subprocess.PIPE, shell=True)
# read one line at a time, as it becomes available
for line in iter(process.stdout.readline, ''):
sys.stdout.write(line)
Of course this will prompt you for your sudo password. And you can use gksudo if you're on a system that has it and you prefer dialog boxes. As a bonus, if you have a decent timeout_default
in /etc/sudoers
, and you've recently run sudo in the same shell where you launched the python interpreter, you won't have to enter a password at all.
精彩评论