LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices

Reply
 
Search this Thread
Old 07-11-2009, 05:24 PM   #1
tibberous
Member
 
Registered: Oct 2004
Posts: 56

Rep: Reputation: 15
Need to run a command, as root, from PHP


I am trying to make a web interface to run chmod commands as root. PHP is run as the apache user.

I thought I could create a shell script as root, set it as 555, and run it from apache - but it doesn't do anything. No errors, no output - nothing at all.

Any idea how I can do this? I am trying not to use sudo, because then they'll be a plain-text version of my server password sitting on the server.

Thanks
 
Old 07-12-2009, 05:20 AM   #2
j-ray
Senior Member
 
Registered: Jan 2002
Location: germany
Distribution: ubuntu
Posts: 1,426

Rep: Reputation: 100Reputation: 100
even if the script is owned by root it is still executed by the apache user and still the apache user does not have the permission to change permissions of files that don't belong to him or are set to 0777 anyway...Could you create a group, "chown" the files to the group and make apache user member of that group? sth like that?
 
Old 07-12-2009, 05:36 AM   #3
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,310
Blog Entries: 54

Rep: Reputation: 2860Reputation: 2860Reputation: 2860Reputation: 2860Reputation: 2860Reputation: 2860Reputation: 2860Reputation: 2860Reputation: 2860Reputation: 2860Reputation: 2860
I wonder if there really is a need for running such an UI in the first place.
Maybe you're trying to find a workaround for things that should be solved differently.
Care to comment on the need to run chmod commands in the first place?


Quote:
Originally Posted by tibberous View Post
I am trying to make a web interface to run chmod commands as root. PHP is run as the apache user.
If you're not a seasoned PHP programmer I'd suggest you look at other commonly used PHP-based UI's and see how they have solved things.


Quote:
Originally Posted by tibberous View Post
I thought I could create a shell script as root, set it as 555, and run it from apache - but it doesn't do anything. No errors, no output - nothing at all.
Then interspersing your lines with echo statements and tailing the webservers access and error log might help.


Quote:
Originally Posted by tibberous View Post
I am trying not to use sudo, because then they'll be a plain-text version of my server password sitting on the server.
See 'man sudoers', look for "NOPASSWD"?
 
Old 07-12-2009, 04:24 PM   #4
tibberous
Member
 
Registered: Oct 2004
Posts: 56

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by unSpawn View Post
See 'man sudoers', look for "NOPASSWD"?
Can I use the sudoers file to only let the apache one run command, as root, without a password?

I made a program that does the chmoding:

PHP Code:
#include <stdlib.h>
#include <stdio.h>

int main(int argschar *argv[]){
    if(
strlen(argv[1])){
        
char command[500];
        
        
command[0] = (char)NULL;
        
strcat(command"chmod 666 -R /var/www/vhosts/");
        
strcat(commandargv[1]);
        
strcat(command"/httpdocs/*");
        
        
printf(command"\n");
        
system(command);
    }
    
    return 
0;

So, you pass it the name of the site and it fixes the permissions (/fix.bin site.com)

Do I add something like this in the sudoers file?:

apache ALL=NOPASSWD: /fix.bin

I tried it, and then put this in my php script, and it still doesn't work:

echo `sudo -u root -n /test.bin`;

I'm not sure if the -n flag is right or not - I am trying to switch off the password prompt.
 
Old 07-12-2009, 05:47 PM   #5
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,310
Blog Entries: 54

Rep: Reputation: 2860Reputation: 2860Reputation: 2860Reputation: 2860Reputation: 2860Reputation: 2860Reputation: 2860Reputation: 2860Reputation: 2860Reputation: 2860Reputation: 2860
Quote:
Originally Posted by tibberous View Post
PHP Code:
strcat(command"chmod 666 -R /var/www/vhosts/"); 
...and that's why I asked if there really was a need for running an UI in the first place. There clearly is no need: you're trying to be creative the wrong way. DAC rights problems must be solved in the standard way. Unless, in your infinite wisdom, you decide that making directores inaccessable to all or having a world-writable file called "vhosts/" (?!) is somehow standards compliant, please cut the chase and explain the real problem you're having.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to run PHP and Apache as root? nyheat Linux - Server 3 03-16-2008 11:55 PM
why lftp command run failed when user isn't root, but ok when logining as root steven_yu Linux - Software 0 06-06-2007 08:36 PM
write a PHP code to run some command as ROOT ??? tritong Programming 13 02-22-2006 10:48 PM
help getting pptp-php-gtk.php to run as root mrtwice Linux - Software 0 11-21-2003 12:49 PM
Is their a way to run a command as root..or another user.. in php BaerRS Programming 2 04-25-2002 02:38 PM


All times are GMT -5. The time now is 07:39 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration