Forum Moderators: coopster & phranque

Message Too Old, No Replies

Making system calls from perl

How to run command with su

         

runner

3:26 pm on Jun 30, 2006 (gmt 0)

10+ Year Member



I wrote a perl script to manage some log files via a web interface. One part of the script will secure copy (scp) a source file to a destination file on a different host.

I'm doing the scp like this:

my @args = ('/usr/bin/scp', '-p', $source, $destination);
my $scpresult = system(@args);

This works great and actually returns the correct return code so I can determine if the operation was successful or not.

Here is the problem: For reasons beyond my control, I have to run the perl script as root. However, the scp command needs to be run as a different user.

So, my perl script has to effectively change the system call from:

scp -p source destination

to

su - newuser -c scp -p source destination

I can't figure out how to get all the args right so the system function works. Adding the "su - newuser" piece breaks the system call from perl.

I've tried:

my @args = ('su', '-', 'newuser', '-c', 'scp', '-p', $source, $destination);

and I've tried

my @args = ('su - newuser -c scp -p $source $destination');

But both fail. Is there some way I can run my perl script as root and have that one system call be executed as a different user?

DrDoc

3:39 pm on Jun 30, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Look into suid ... You can run the Perl script itself as root. Once it runs as root, you can toggle between user nobody and root.

runner

10:37 pm on Jun 30, 2006 (gmt 0)

10+ Year Member



Someone at work showed me the error of my ways... I needed quotes around the entire scp command and those extra quotes needed to be escaped as follows

my @args = ("su - newuser -c \"scp -p $source $destination\"");

Works just fine now.