Perl System Calls When Running as Another User Using Sudo

Perl system calls when running as another user using sudo

I think you probably need to add the -i option ("simulate initial login") to sudo:

sudo -i -u perluser /usr/bin/perl /data/perlscripts/scripta.pl

That will ensure that .profile or .login or whatnot is run properly, and therefore that $PATH is set up properly and so on. It will really be, in almost all respects, as if perluser were actually logging in and running /usr/bin/perl /data/perlscripts/scripta.pl at the shell.

perl: Launch process with system(), then check if its running under a specific display number

I solved it after many attempts.

Did a piped open

$pidofcall = open(HANDLE, "process2|");

Then I did whatever I did need to do, and I made the server send a signal to me if it loses connection with process2. If I did need to bang out, I simply did a "goto killprocess;" Then I simply had:

killprocess:
kill(9,$pidofcall);
close(HANDLE);
$mw->destroy;
system("logout");

Call a perl script from another perl script with specific user authentication

If your program is running as root, you can change the real ($<) and effective ($>) UIDs of the process at will. For your purpose, changing EUID is enough:

use v5.12;

say "euid: $>";
system('whoami');
# switch user
$> = 1000;
say "euid: $>";
system('whoami');
# switch back
$>=$<;
say "euid: $>";
system('whoami');

Running that script as root:

$ sudo perl uidtest
euid: 0
root
euid: 1000
ben
euid: 0
root

How can I check for root when a user runs a perl script

If $> (aka $EFFECTIVE_USER_ID if you use English) is non-zero, then the user is not root.

how can I run a perl script which needs root?

Usually you would tell sudo that the webserver user can execute the script without need to authenticate with a password. You'll have to add a line like the following in /etc/sudoers:

www-data ALL=(ALL:ALL) NOPASSWD: /root/scripts/script.pl

This would let the www-data user execute script.pl via sudo without having to enter a password.

Then execute the script from PHP using

exec('sudo /root/scripts/script.pl');


Related Topics



Leave a reply



Submit