0

I'm in a bit of trouble and need to run a linux command as sudo using php. Is this possible using php's exec()? I've tried but cannot enter the sudo password and then execute another command on the same exec() call.

edit: i cannot access the server remotely via ssh due to own stupidity. That's why i have to run commands thru the web server.

3 Answers 3

2

You can grant some privileges for some command to a given user (such as the user that runs your webserver...) with visudo, or have to setsuid the program you want to run, but I strongly disadvise you to do this.

Can't you use a more secure way, for example writting data in a database, and build a daemon robot that often looks in the database, do achieve the job, and this daemon could be granted as root?

Sign up to request clarification or add additional context in comments.

Comments

1

You should set up your sudoers not to require a password (NOPASSWD:) for the user that your PHP is running as. Be sure to completely lock down the commands that that user can run via sudo!

2 Comments

vi is painful, but with nano, you can severely screw up the sudoers file ... locked myself out of an ec2 instance once, because I couldn't sudo :P
@PeteHerbertPenito: You can use nano with visudo. Simply add the line Defaults editor=/usr/bin/nano or the like to your sudoers. Then, in future, always use visudo! It does syntax checking for you.
0

Use proc_open which allows you to create a custom pipe to feed data (the user password) when prompted. See the comments at the link for ways to create a custom password pipe.

Update, since you're not able to read.

Snippet from comment by snowleopard at amused dot NOSPAMPLEASE dot com dot au at 05-Jun-2008 02:46 at the previously twice mentioned link. You just need to apply this to your own situation.

    // Set up the descriptors
    $Descriptors = array(
        0 => array("pipe", "r"),
        1 => array("pipe", "w"),
        2 => array("pipe", "w"),
        3 => array("pipe", "r") // This is the pipe we can feed the password into
    );

    // Build the command line and start the process
    $CommandLine = $GPGPath . ' --homedir ' . $HomeDir . ' --quiet --batch --local-user "' . $Identity . '" --passphrase-fd 3 --decrypt -';
    $ProcessHandle = proc_open( $CommandLine, $Descriptors, $Pipes);

    if(is_resource($ProcessHandle)) {
        // Push passphrase to custom pipe
        fwrite($Pipes[3], $PassPhrase);
        fclose($Pipes[3]);

2 Comments

Sorry but I can't figure out how to use proc_open. Ideas?
wtf is your problem? i'm new to php and don't know how to do that kind of stuff. Jerk.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.