Running one Python program from another as a different user

D

dwelch91

Greetings-
This is on Linux... I have a daemon running as root and I want to
execute another Python program as another user (a regular user). I have
the name of the user and can use the 'pwd' and 'grp' modules to get
that user's user and group ids. What I don't understand is how to then
go about launching that new program. I had considered having the
launched program switch itself back to the target user (somehow), but
the launched program is graphical in nature (PyQt), and I am afraid of
X11 locking out the display to user root (many distros seem to ship
with server access for user root turned off). That might prevent the
launched program from even starting?

Any ideas? My Google searching was not successful in figuring this
out...

Thanks,

Don
 
J

Jeff Schwab

Greetings-
This is on Linux... I have a daemon running as root and I want to
execute another Python program as another user (a regular user). I have
the name of the user and can use the 'pwd' and 'grp' modules to get
that user's user and group ids. What I don't understand is how to then
go about launching that new program. I had considered having the
launched program switch itself back to the target user (somehow), but
the launched program is graphical in nature (PyQt), and I am afraid of
X11 locking out the display to user root (many distros seem to ship
with server access for user root turned off). That might prevent the
launched program from even starting?

Any ideas? My Google searching was not successful in figuring this
out...

from subprocess import *

def run_as(username):
pipe = Popen(["su", username], stdin=PIPE, stdout=PIPE)
(out, err) = pipe.communicate("whoami")
print out,


if __name__ == "__main__":
import sys
for user in sys.argv[1:]:
run_as(user)
 
D

dwelch91

Thanks, that looks very promising...
Is there a solution for pre-Python v2.4? I have to have code that works
on 2.x, 0<=x<=4. Do I just use the os.popen instead?

-Don
 
P

Paul Rubin

This is on Linux... I have a daemon running as root and I want to
execute another Python program as another user (a regular user). I have
the name of the user and can use the 'pwd' and 'grp' modules to get
that user's user and group ids. What I don't understand is how to then
go about launching that new program.

Use os.fork to start a new process and have the new process do
os.setuid to the new user. Then os.execl (or whatever) to run the
target program.
 
M

Mike Meyer

Greetings-
This is on Linux... I have a daemon running as root and I want to
execute another Python program as another user (a regular user). I have
the name of the user and can use the 'pwd' and 'grp' modules to get
that user's user and group ids. What I don't understand is how to then
go about launching that new program. I had considered having the
launched program switch itself back to the target user (somehow), but
the launched program is graphical in nature (PyQt), and I am afraid of
X11 locking out the display to user root (many distros seem to ship
with server access for user root turned off). That might prevent the
launched program from even starting?

Any ideas? My Google searching was not successful in figuring this
out...

Well, Jeff already pointed out running su. You might also check the
os.setuid docs, and the setuid man page.

<mike
 
J

Jeff Schwab

Thanks, that looks very promising...
Is there a solution for pre-Python v2.4? I have to have code that works
on 2.x, 0<=x<=4. Do I just use the os.popen instead?

import os

def run_as(username):
pipe = os.popen("su %s" % username, 'w')
pipe.write("whoami")


if __name__ == "__main__":
import sys
for user in sys.argv[1:]:
run_as(user)
 
A

Alessandro Bottoni

Jeff said:
Thanks, that looks very promising...
Is there a solution for pre-Python v2.4? I have to have code that works
on 2.x, 0<=x<=4. Do I just use the os.popen instead?

import os

def run_as(username):
pipe = os.popen("su %s" % username, 'w')
pipe.write("whoami")


if __name__ == "__main__":
import sys
for user in sys.argv[1:]:
run_as(user)

Jeff, may I suggest you to write down a new recipe for the ASPN Python
Cookbook based on these code snippets? This is exactly the kind of thing
the people is happy to find at ASPN.

CU
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top