Reputation: 4567
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?
Upvotes: 5
Views: 13754
Reputation: 19259
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.
Upvotes: 2
Reputation: 8774
I would split the program in two.
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.
Upvotes: 3
Reputation: 400304
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:
setreuid(2)
and setregid(2)
to set your user ID and group ID back to an unprivileged user.exec(3)
functions to execute the unprivileged executable.sudo
, then make it owned by root and make it a set-user-ID executable with chown 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:
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.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.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.
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.
Upvotes: 4
Reputation: 15063
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.
Upvotes: 4