Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Running a perl script with root, changing to another user and then back to root?

by morthed (Novice)
on Feb 15, 2018 at 14:13 UTC ( [id://1209220]=perlquestion: print w/replies, xml ) Need Help??

morthed has asked for the wisdom of the Perl Monks concerning the following question:

I've tried to search a lot around the web for this question, to no avail. <\p>

Currently I'm building a perl script that needs to do a specific job for each line in some database. The database contains several arguements including a "user" field. The script runs as root, and in each line iteration in the db, it will need to switch to the user provided and operate as the user, finishing the round and exiting back to root and then again, running as a different user. <\p>

So far I've tried: <\p>

using POSIX::setuid($User) in order to switch to the user within the same script, meaning:<\p><code>while (my $line = <$DB>) .. some code .. POSIX::setuid($User); #switch to user ... do stuff POSIX::setuid(0); #return to root <\code>

But this solution didn't work so well, after changing once it was stuck in this user through all iterations of the loop, and couldn't return to root, which is obviously logical because it doesn't have the correct permissions.<\p>

Since this didn't work, I thought to myself maybe I should split the job into 2 scripts. One script will run as root, whereas the other script will be called to by root in the first script and then use POSIX::setuid. I thought, that maybe because it's a subproc\shell it'll just return itself back to root once the job is done on the second script, but it doesn't work as well... I'm kinda out of ideas, anything you guys think'll help?<\p>

ANOTHER THING: I know this is probably incorrect, so that's why I ask this around... please keep mean comments off or stuff like "you don't know what you're doing", if I did I wouldn't have asked... I basically look for a safe way to change the eid\uid of the SECOND SCRIPT only and keep root as the user for the main one... is there a way for that? Thanks! <\p>

  • Comment on Running a perl script with root, changing to another user and then back to root?

Replies are listed 'Best First'.
Re: Running a perl script with root, changing to another user and then back to root?
by shmem (Chancellor) on Feb 15, 2018 at 14:46 UTC

    You could just fork and setuid in the child. Then you communicate with the the child via some IPC, e.g. a set of pipes.

    perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'
Re: Running a perl script with root, changing to another user and then back to root?
by salva (Canon) on Feb 15, 2018 at 15:38 UTC
    Maybe setting the EUID (Effective User Id) is enough for your purposes: check $> documentation in perlvar.

    Update: Though, note that you can do that only when the code running with the EUID changed is controlled by you and safe. Otherwise, it could be made to change the EUID again in order to execute arbitrary code as root.

Re: Running a perl script with root, changing to another user and then back to root?
by thanos1983 (Parson) on Feb 15, 2018 at 14:34 UTC

    Hello morthed,

    Welcome to the Monastery. Well a quick and dirty solution could be to ssh as root do what ever you want exit and continue.

    Well ssh might not be the best option what about the Sudo module? You can write a small script to be executed by root. Sample of code:

    In the sample.pl script:

    Hope this helps, BR.

    Seeking for Perl wisdom...on the process of learning...not there...yet!
      Could you please elaborate on that? ssh in the sub script, or ssh in the main script? Right now the main script *runs* as root since the start but after switching is stuck or either using it's privileges to check stuff.. but I need to use another user's priv. to check (that's the entire job :) )

        Hello again morthed,

        You can ssh from the main script as root then in apply the commands that you want, close the session and continue as normal user.

        Check also the updated proposed solution that I just added.

        Hope this helps, BR.

        Seeking for Perl wisdom...on the process of learning...not there...yet!
Re: Running a perl script with root, changing to another user and then back to root?
by haukex (Archbishop) on Feb 18, 2018 at 11:44 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1209220]
Approved by marto
Front-paged by haukex
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (7)
As of 2024-04-23 09:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found