LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How to execute system calls in php via browser? (https://www.linuxquestions.org/questions/programming-9/how-to-execute-system-calls-in-php-via-browser-4175468803/)

HIDS2013 07-07-2013 04:19 PM

How to execute system calls in php via browser?
 
I have a C program that makes a system call (centOS 6.0) to encrypt a file, my code is:

#include <stdlib.h>
int main () {
system ("gpg -c --batch --passphrase mypass file.txt");
return 0;
}
The executable object is called encrypt_file
When I run ./encrypt_file directly through CLI it runs perfectly I obtain my file.txt.gpg, but when I try to execute it via browser when I get no response. Code in php: shell_exec("./encrypt_file");

I already changed permissions to apache user by issuing the following:
chown apache.apache /var/www/html/
And added the next line in /etc/sudoers:
apache ALL=(ALL) NOPASSWD:ALL

NOTE: The only command I have issues is gpg, I can make a system call with any other command that I needed to use, I can even run python scripts, and other C programs that doesn't contain anything related to gpg.

Where can I change the PATH variable that is in use with the apache webserver???

I hope a fast reply! I need to use a lot this encrypt_file!

j-ray 07-08-2013 04:05 AM

1. You better give the shell_exec the full path to the file that is to be executed.

2. The apache web user is probably not "apache" but instead "wwwrun" or "nobody", depending on apache config. (regarding the sudoers)

I think you better do not allow the web user to execute everything on your box as sudo without password, if I don't get you wrong...

Hope this helps,
j

HIDS2013 07-08-2013 10:34 AM

I already added the full PATH, still no response.
But checking the error_log in /var/log/httpd/error_log I saw this line:

gpg: Fatal: can't create directory `/var/www/.gnupg': Permission denied

It's a permission problem obviously but I'm not sure where...

Thanks!

j-ray 07-08-2013 11:27 AM

Can't you manually create this directory as root and grant write access to it to the web user? /var/www/is probably the basic document root and writable for root only...

sudo mkdir /var/www/.gnupg

And if the user needs writing access to it you may
chown wwwrun:users /var/www/.gnupg

HIDS2013 07-08-2013 12:53 PM

Thanks! Problem solved...

I found a solution at this site -> http://gnupg.10057.n7.nabble.com/Exi...pt-td7342.html

I added that --homedir with the PATH that I found in the error.log of apache, and it works perfectly!

I will try your solution that seems more elegant! Thanks a lot for your help!


All times are GMT -5. The time now is 11:43 AM.