473,387 Members | 1,529 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

switch back and forth from root to a user

Hi there,

I looked around and couldn't find an answer to this question (and I'm now wondering is there is one :-)

I have a piece of code in which I need to switch from root to a particular user, run the command as this user and would like to return to root. I don't want to use, if possible at all, "su".

Here is the snippet code that is run as root initially:
Expand|Select|Wrap|Line Numbers
  1. import os
  2. try:
  3.   if os.name == 'posix':
  4.     import pwd
  5.     pw  = pwd.getpwnam(user)
  6.     uid = pw.pw_uid
  7.     os.setuid(uid)
  8. except:
  9.     return 1 # Error
  10. finally:
  11.   # Put back root user...
  12.   os.setuid(0)
As you can imagine, you can't set back to root (setuid(0)) - running as 'user' at this point.

Thanks in advance for any help.

D.
Aug 31 '10 #1
4 15602
Oops! Correcting indentation:

Expand|Select|Wrap|Line Numbers
  1. import os
  2. try:
  3.   if os.name == 'posix':
  4.     import pwd
  5.  
  6.     pw = pwd.getpwnam(user)
  7.     uid = pw.pw_uid
  8.     os.setuid(uid)
  9. else:
  10.     # Do something useful here
  11. except:
  12.   return 1 # Error
  13. finally:
  14.   # Put back root user...
  15.   os.setuid(0)
  16.  
Aug 31 '10 #2
dwblas
626 Expert 512MB
Hopefully, no OS will let you do that. su and sudo are there to limit escalation of privileges, and are the only way that you can do this AFAIK.
Aug 31 '10 #3
May be I wasn't clear enough. The script runs as root all the time. I just need to run the few steps as a given user and get back to root. Theses steps involve populating a cron tab. I'm looking for the best way of doing that. An idea that I was thinking of was to do this in a separate thread, then this approach isn't Posix compliant and I'm not sure if the whole process doesn't get the same UID (the script runs under Solaris).

Any other ideas?
Sep 1 '10 #4
dwblas
626 Expert 512MB
Theses steps involve populating a cron tab
It is as simple as changing the permissions of cron to the user's (it never is). I tried multiprocessing (on Linux) and it appears to work. I am not a fan of setuid so know very little about it.
Expand|Select|Wrap|Line Numbers
  1. import os
  2. import pwd
  3. import time
  4. from multiprocessing import Process
  5.  
  6. class TestClass():
  7.  
  8.     def test_f(self, user):
  9.        try:
  10.            if os.name == 'posix':
  11.  
  12.                pw = pwd.getpwnam(user)
  13.                uid = pw.pw_uid
  14.                os.setuid(uid)
  15.                print "setuid successful", pw
  16.  
  17.                ##--- create file as test of permissions
  18.                fp = open("./test_user", "w")
  19.                fp.write("test user\n")
  20.                fp.close()
  21.        except:
  22.            return 1 # Error
  23.  
  24. if __name__ == '__main__':
  25.     CT=TestClass()
  26.     p = Process(target=CT.test_f, args=('user name',))
  27.     p.start()
  28.  
  29.     ## sleep for 5 seconds to simulate processing and terminate
  30.     time.sleep(5.0)
  31.     p.terminate()
  32.  
  33.     ##--- create file as test of permissions
  34.     fp = open("./test_root", "w")
  35.     fp.write("test root\n")
  36.     fp.close() 
Sep 1 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: * | last post by:
Is there a way that I can run the system() command as a root user. Need to restart network services on a server via php.
2
by: Matthew | last post by:
Hello! What is the best NS/IE compliant way to accomplish this using the least amount of javascript code? <div id="block1"> Group 1 Code </div> <div id="block2"> Group 2 Code </div>
5
by: MLH | last post by:
I'm supposed to set a password for the MySQL root user. The output of mysql_install_db instructed me to run the following commands... /usr/bin/mysqladmin -u root -h appserver password mynwewpasswd...
1
by: Angus Comber | last post by:
I have set a password for the mysql root user so when I do this: shell> perl -MCPAN -e shell cpan> install DBI cpan> install DBD::mysql install DBD::mysql fails Is there a way I can run...
1
by: Challenge | last post by:
Hi, I'm testing automatic reroute on two instances on the same server. The client can successfully connect to instance 2 after I stop instance 1. However I don't know how to let client back to...
1
by: menoquindici | last post by:
We have a situation here where one of my co-workers removed several privileges from the 'root' user, namely: INSERT UPDATE DELETE FILE
8
Frinavale
by: Frinavale | last post by:
Hi there, I'm developing a website that has a print feature. I was working on developing a CSS that would properly format the web-pages to be more printer friendly and never quite finished the...
0
by: ozredwine | last post by:
Hello Smart guys, I made a mistake which makes me feel like a stupid user. I am new to mysql and I need your help to create a root id on my server. I tried to do what most of the posts are...
3
by: tinnews | last post by:
Does python install fairly easily for a non-root user? I have an ssh login account onto a Linux system that currently provides Python 2.4.3 and I'd really like to use some of the improvements in...
2
by: ivytony | last post by:
First of all, I'm not sure if I've set up root password for MySQL server or not. I was following this tutorial http://www.howtoforge.com/centos-5.1-server-lamp-email-dns-ftp-ispconfig-p4 to install...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.