iamresearcher Posted October 1, 2011 Share Posted October 1, 2011 I have this code below: shell_exec("/usr/bin/sudo /usr/sbin/usermod -p $userpwd $username"); Its purpose is to modify the password of the username using php. Please dont criticize why I do what I do. I would like to ask your assistance if that script is correct on its syntax like if I use " or should I use '. I have tried every combination to make it work, but it is not working till now. I also tried: shell_exec("/usr/bin/sudo /usr/sbin/usermod -p". $userpwd . " " . $username); Still not working to update the password of the user. Quote Link to comment https://forums.phpfreaks.com/topic/248214-shell_exec-on-php/ Share on other sites More sharing options...
trq Posted October 1, 2011 Share Posted October 1, 2011 the syntax is fine. does apache have permission to use sudo? Quote Link to comment https://forums.phpfreaks.com/topic/248214-shell_exec-on-php/#findComment-1274574 Share on other sites More sharing options...
iamresearcher Posted October 1, 2011 Author Share Posted October 1, 2011 What syntax is fine? I dont know if apache allows sudo? can you help me find out if apache allows sudo. Quote Link to comment https://forums.phpfreaks.com/topic/248214-shell_exec-on-php/#findComment-1274575 Share on other sites More sharing options...
trq Posted October 1, 2011 Share Posted October 1, 2011 The first syntax is fine. You will need to take a look at your /etc/sudoers file. Use sudoedit to open it though. Quote Link to comment https://forums.phpfreaks.com/topic/248214-shell_exec-on-php/#findComment-1274577 Share on other sites More sharing options...
iamresearcher Posted October 1, 2011 Author Share Posted October 1, 2011 really my first sytax is fine? was it right to use " instead of ' ? and the position of my quotes? on my sudoers file, is root ALL=<ALL> ALL Quote Link to comment https://forums.phpfreaks.com/topic/248214-shell_exec-on-php/#findComment-1274578 Share on other sites More sharing options...
trq Posted October 1, 2011 Share Posted October 1, 2011 Both of the syntaxes you posted are fine. That entry within your sudoers file simply means that root can execute all commands. You need to make sure your Apache user (whatever that is) is allowed to execute sudo. This also assumes you are trying to execute this line of php from a page requested via Apache? Quote Link to comment https://forums.phpfreaks.com/topic/248214-shell_exec-on-php/#findComment-1274579 Share on other sites More sharing options...
iamresearcher Posted October 1, 2011 Author Share Posted October 1, 2011 I only have one user and that is root. do I have to place NOPASSWD:ALL LIKE root ALL=<ALL> NOPASSWD:ALL Yes I am using php with apache. but I dont get it why I dont have any result from my script. Quote Link to comment https://forums.phpfreaks.com/topic/248214-shell_exec-on-php/#findComment-1274581 Share on other sites More sharing options...
trq Posted October 1, 2011 Share Posted October 1, 2011 I only have one user and that is root No you don't. Linux systems have multiple users even if you haven't setup any. There are system users which various pieces of software run as. Apache runs as one of these users. You can find out which user apache is running as by looking for the User and Group directives within your configuration. Assuming your main configuration file is /etc/apache/httpd.conf, this command should show you which user Apache is running as. grep User /etc/apache/httpd.conf I say should because not all configs are setup the same. You will need to investigate this further yourself, or let us know which distribution you are using. Quote Link to comment https://forums.phpfreaks.com/topic/248214-shell_exec-on-php/#findComment-1274594 Share on other sites More sharing options...
iamresearcher Posted October 1, 2011 Author Share Posted October 1, 2011 using grep User /etc/httpd/conf/httpd.conf, the result is below: [root@s1 ~]# grep User /etc/httpd/conf/httpd.conf # AssignUserID: Takes two parameters, uid and gid (or really, user name and # User/Group: The name (or #number) of the user/group to run httpd as. # . On SCO (ODT 3) use "User nouser" and "Group nogroup". User apache # UserDir: The name of the directory that is appended onto a user's home # UserDir is disabled by default since it can confirm the presence UserDir disabled # directory, remove the "UserDir disabled" line above, and uncomment #UserDir public_html # Control access to UserDir directories. The following is an example LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined LogFormat "%{User-agent}i" agent #LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio [root@s1 ~]# I am using centos 5 Quote Link to comment https://forums.phpfreaks.com/topic/248214-shell_exec-on-php/#findComment-1274651 Share on other sites More sharing options...
trq Posted October 1, 2011 Share Posted October 1, 2011 As you can tell by the output of that command Apache runs as a user called apache. this user will need to be granted permission to use sudo without password. Be aware that this also opens massive security holes. You should lockdown the sudo access to only the specific commands you wish to execute. Quote Link to comment https://forums.phpfreaks.com/topic/248214-shell_exec-on-php/#findComment-1274652 Share on other sites More sharing options...
iamresearcher Posted October 2, 2011 Author Share Posted October 2, 2011 what I have now on my sudoers which I only modified was: root ALL=(ALL) ALL apache ALL=NOPASSWD: /usr/bin/sudo, /usr/sbin/useradd, /usr/sbin/usermod, /usr/sbin/userdel that is all. I restart httpd but still shell_exec does not perform. Is the syntax below correct? shell_exec('/usr/bin/sudo /usr/sbin/userdel -r ' . $username); Quote Link to comment https://forums.phpfreaks.com/topic/248214-shell_exec-on-php/#findComment-1274836 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.