tags:

views:

897

answers:

3

I have a shell_exec() command that accesses a directory above my document root so I need to use sudo "as root" to make it happen. (I understand the security issues and am putitng in measures to address it).

The issue is when I run the shell_exec() I get a "sudo: must be setuid root" error in my apache error_log file.

I thought the solution was to chmod 4750 the bash script that is called by my sheel_exec() but that does not do the job.

What exactly is "sudo: must be setuid root" trying to tell me and how might I resolve it?

A: 

Did you check the permissions for your script?

Who is owning the script?

Does the web user has the rights to sudo?

Roberto Aloi
Here are the permissions for the files in question:<br /> <br />PHP Script run from browser<br />-rw-r--r-- 1 drd drd 339 Feb 9 11:09 test.php<br /> <br />BASH Script called from shell_exec()<br />-rwsr-x--- 1 root nobody 209 Feb 9 09:01 test.bash*<br /> <br />and here is the shell_exec() command<br />shell_exec('sudo -u root -S /home/drd/public_html/app/shell/test.bash < /home/drd/public_html/app/shell/temp-pswd.txt');<br /> <br />re: your question "Does the web user has the rig...?" are you referring to sudoers? My preference is not to alter the sudoers file but to use root.
Dr. DOT
how do you get line breaks in comments in StackOverflow (using FF 3.6)?
Dr. DOT
-rw-r--r-- 1 drd drd 339 Feb 9 11:09 test.php
Dr. DOT
-rwsr-x--- 1 root nobody 209 Feb 9 09:01 test.bash*
Dr. DOT
shell_exec('sudo -u root -S /home/drd/public_html/app/shell/test.bash < /home/drd/public_html/app/shell/temp-pswd.txt');
Dr. DOT
+1  A: 

Is the sudo executable itself setuid root? You may need to

chown root: /usr/bin/sudo
chmod u+s /usr/bin/sudo
eswald
---x--x--x 2 root root 159752 Feb 13 2009 sudo*
Dr. DOT
Yes, that could definitely be your problem. You might need `chmod a+r /usr/bin/sudo` in addition to the above, but the `u+s` line is definitely required.
eswald
I want to avoid having to make server-side tweaks in order to get the script to run. The reason is that what I am building is intended to be rolled out to other servers through a 1-time install process. So I don't want to have to request server-side tweaks in order to get the script to work. If that has to be the case, then I can just as easily have a sys admin load the file as a pre-requisite to the install process being run. Bottom-line, trying to create something that is independent of the server config and is self-dependent.
Dr. DOT
(continued from previous comment) So, out-of-the box if shell_exec() cannot run as root, then I would really like to know that so I don;t continue to spin my wheels or have hope that it can. I have seen this post (http://www.php.net/manual/en/function.shell-exec.php#68685) but I cannot get it to work on my server.Thanks
Dr. DOT
`sudo` cannot operate without its setuid bit sed. If you expect *anything* on that server to sudo, then you need to repair it.
eswald
+1  A: 

Alternatively, skip sudo altogether. If your script is owned by root and has its own setuid bit set, then you don't need to use sudo to get root privileges. In fact, it can be more secure that way; you guarantee that your web user can only use that script, without having to edit sudoers. To do so, remove sudo from your shell_exec() line:

<?php
    shell_exec('/path/to/your/command');
?>
eswald
I have set the script to be owned by root. It seems that because the script is evoked or called by the browser, then "nobody" actually is the user running the script regardless of who linux thinks owns the file.
Dr. DOT
eswald -- does this work for you on your server?
Dr. DOT
I haven't yet encountered a situation where it has been necessary; in particular, accessing parent directories shouldn't need root privileges. Which server (and version) are you using?
eswald