Page 1 of 1
Function popen() doesn't work for apache user?
Posted: Sat Jun 21, 2008 3:32 am
by calcop
Hello everyone! I have a quick question. I am having problems with the popen() function. I am using it to evaluate a command (obviously). Anyway, it works just fine if I run the process from command line, but when I try to use it on my webpage (which is executed by user 'apache') it does not work. The user 'apache' has all the proper permissions of executing the command I am trying to use. I have even tried the 'date' command, just to make sure. And still, nothing is being read in the stream.
Have there been any problems with apache accessing a popen stream? Or, am I just missing something?
Here is my code:
Code: Select all
$handle = popen("finger $user", "r");
$info = fread($handle, 80);
fclose($handle);
echo $info;
Again, running from command line it works. From the apache web server, nothing appears to be in the $info variable.
I would appreciate any help.
Thanks!
Re: Function popen() doesn't work for apache user?
Posted: Sat Jun 21, 2008 3:43 am
by Kieran Huggins
Try
shell_exec() instead
incidentally, popen() must be closed with pclose() - not fclose().
Re: Function popen() doesn't work for apache user?
Posted: Sat Jun 21, 2008 3:48 am
by calcop
Thanks for catching that!

Still doesn't keep $info from being populated. Any ideas?
Re: Function popen() doesn't work for apache user?
Posted: Sat Jun 21, 2008 3:55 am
by Kieran Huggins
shell_exec didn't work?
check the permissions on "finger"
Re: Function popen() doesn't work for apache user?
Posted: Sat Jun 21, 2008 4:00 am
by calcop
Of corse, anyone can execute 'finger.' This is just an example. I need popen because the script ultimately needs to interact with a program (to edit system users, etc). I'm setting up a web based user manager for a system.
Re: Function popen() doesn't work for apache user?
Posted: Sat Jun 21, 2008 4:04 am
by Kieran Huggins
Note: If you're looking for bi-directional support (two-way), use proc_open().
Check the user comments on that page for proper form:
http://ca.php.net/manual/en/function.proc-open.php
Re: Function popen() doesn't work for apache user?
Posted: Sat Jun 21, 2008 4:08 am
by calcop
Useful, but I unidirectional works fine for me. I can use popen and get what i need (reading) in three lines of code. It works just fine (from command-line), it just doesn't work when apache runs the script. I am trying to find out how to get it to work from the web server.
Re: Function popen() doesn't work for apache user?
Posted: Sat Jun 21, 2008 4:12 am
by Kieran Huggins
I thought you needed bidirectional access? no?
if not, use shell_exec.
Re: Function popen() doesn't work for apache user?
Posted: Sat Jun 21, 2008 4:14 am
by calcop
No, just one-way. Getting kind of frustrating why it won't work from apache! hehe
Re: Function popen() doesn't work for apache user?
Posted: Sat Jun 21, 2008 4:19 am
by calcop
Strange. It doesn't look like it is popen. I tried to grab the results from exec() and the same. Nothing.
Re: Function popen() doesn't work for apache user?
Posted: Sat Jun 21, 2008 4:26 am
by Kieran Huggins
hmmm... only thing I can suggest is checking the error logs for a clue as to why it won't work
Re: Function popen() doesn't work for apache user?
Posted: Sat Jun 21, 2008 4:30 am
by calcop
Safe mode would prevent from executing anything. However, it is off.
Re: Function popen() doesn't work for apache user?
Posted: Sat Jun 21, 2008 4:43 am
by calcop
Found the problem. I thought I configured SELinux to let apache to do so, but apparently I didn't.
Re: Function popen() doesn't work for apache user?
Posted: Sat Jun 21, 2008 5:10 am
by calcop
It is unsafe not to run with SELinux, so this is how to fix it:
"/usr/sbin/setsebool httpd_ssi_exec true"
This is to use any external execution functions.
- matt