By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,730 Members | 1,480 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,730 IT Pros & Developers. It's quick & easy.

Running one Python program from another as a different user

P: n/a
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

Aug 12 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
dw******@gmail.com wrote:
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)
Aug 12 '05 #2

P: n/a
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

Aug 12 '05 #3

P: n/a
"dw******@gmail.com" <dw******@gmail.com> writes:
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.
Aug 13 '05 #4

P: n/a
"dw******@gmail.com" <dw******@gmail.com> writes:
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
--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Aug 13 '05 #5

P: n/a
dw******@gmail.com wrote:
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)
Aug 13 '05 #6

P: n/a
Jeff Schwab wrote:
dw******@gmail.com wrote:
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
-----------------------------------
Alessandro Bottoni
Aug 13 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.