exec problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
vimadeva
Forum Newbie
Posts: 8
Joined: Tue May 01, 2007 3:51 pm

exec problem

Post by vimadeva »

Hi all! I'm new in linux and php. My problem is that i want to create a file (dhcpd.conf) and then copy it to /etc/conf.d.
The problem is that i can't use exec(cp /var/www/html/dhcpd.conf /etc/dhcp.conf) or exec(sudo -u root cp /var/www/html/dhcpd.conf /etc/dhcp.conf).
I already added this line to /etc/ sudoers: ALL ALL=(ALL) NOPASSWD ALL hoping that that would help, but the problem continues.
If I run exec("whoami") in a php file, it shows apache, but exec("sudo -u root whoami") dies.
I would appreciate some help, i need this for today :D.
Sorry for the bad english.

Please i running out of ideas.

Summary: Hot to mate exec(sudo.... ) works.
I dont care about security, I only wants it to work.
User avatar
arturm
Forum Commoner
Posts: 86
Joined: Fri Apr 13, 2007 8:29 am
Location: NY
Contact:

Post by arturm »

you cannot run

Code: Select all

exec("sudo -u root whoami")
because apache user (usually www-data or nobody) can't login as a root
and you can't copy your files because you don't have permission to the directory you are trying to copy to.
in linux console you can change permissions

Code: Select all

chown nobody /etc/dhcp.conf
but I wouldn't advice that since all apache users will have access to that file.
vimadeva
Forum Newbie
Posts: 8
Joined: Tue May 01, 2007 3:51 pm

Post by vimadeva »

arturm wrote:you cannot run

Code: Select all

exec("sudo -u root whoami")
because apache user (usually www-data or nobody) can't login as a root
and you can't copy your files because you don't have permission to the directory you are trying to copy to.
in linux console you can change permissions

Code: Select all

chown nobody /etc/dhcpd.conf
but I wouldn't advice that since all apache users will have access to that file.
Thank you arturm but I think I can, because in a gentoo installation i could do that. What about run

Code: Select all

exec("sudo whoami")
It doesn't works, also

Code: Select all

exec("cp /var/www/html/some.txt /var/www/html/other.txt");
doesn't work, also I "need" to restart a sevice like

Code: Select all

service httpd restart
I will appreciate any thought.
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

Have you tried simply using copy()?
User avatar
arturm
Forum Commoner
Posts: 86
Joined: Fri Apr 13, 2007 8:29 am
Location: NY
Contact:

Post by arturm »

try to put

Code: Select all

error_reporting(E_ALL);
at the top of your php page.
At least we will see what error do you have.
vimadeva
Forum Newbie
Posts: 8
Joined: Tue May 01, 2007 3:51 pm

Post by vimadeva »

arturm wrote:try to put

Code: Select all

error_reporting(E_ALL);
at the top of your php page.
At least we will see what error do you have.
Well, i putted that command at the start of the line but nothing is printed :S. Does it means that there were no erros in the execution of exec('sudo whoami'); ?

I already hace trien with system() and shell_exec().

And now i need to execute something like this: shell_exec(sudo service dhcpd restart);
Last edited by vimadeva on Wed May 02, 2007 12:14 pm, edited 1 time in total.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

aaronhall wrote:Have you tried simply using copy()?
Agreed. Have you?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Re: exec problem

Post by volka »

vimadeva wrote:I already added this line to /etc/ sudoers: ALL ALL=(ALL) NOPASSWD ALL
by hand or did you use a tool like visudo for that?
vimadeva
Forum Newbie
Posts: 8
Joined: Tue May 01, 2007 3:51 pm

Post by vimadeva »

Jcart wrote:
aaronhall wrote:Have you tried simply using copy()?
Agreed. Have you?
Sorry. I already have, and it works. But it was only an example, I really need to exec commands from php.
vimadeva
Forum Newbie
Posts: 8
Joined: Tue May 01, 2007 3:51 pm

Re: exec problem

Post by vimadeva »

volka wrote:
vimadeva wrote:I already added this line to /etc/ sudoers: ALL ALL=(ALL) NOPASSWD ALL
by hand or did you use a tool like visudo for that?
I did both...
Is there a problem??
User avatar
arturm
Forum Commoner
Posts: 86
Joined: Fri Apr 13, 2007 8:29 am
Location: NY
Contact:

Post by arturm »

Ok and what about this syntax:

Code: Select all

echo `sudo -u root whoami`;
User avatar
arturm
Forum Commoner
Posts: 86
Joined: Fri Apr 13, 2007 8:29 am
Location: NY
Contact:

Post by arturm »

Error is not on the PHP side but on the Linux side.
I just tested it and I know how to debug the problem.
You have to run the script in a console using binary php

on my machine it is

Code: Select all

$ php5-cgi test.php
where test.php is a file with exec function and php5-cgi is a binary version of php.
It can be named as simply as php or php5 depends on the system you are using.
If you don't have it you have to compile it.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

I've never seen ALL ALL=(ALL) before.
Would the meaning be "everybody can do everything"?
I doubt that's possible with sudo.
vimadeva
Forum Newbie
Posts: 8
Joined: Tue May 01, 2007 3:51 pm

Problem solved

Post by vimadeva »

Hi all! I already solved the problem, I'll just post it:

In the sudoers file, you have to comment the line that says

Code: Select all

Defaults  reuiretty
That was the problem!!! since php isn't running from a tty, and also you have to add the line

Code: Select all

nobody ALL=(ALL) NOPASSWD: ALL
to give all the privileges to de server! I hope this is usefull for you, it was for me :P

Sorry for my english... I think that you understand :D
Post Reply