Function popen() doesn't work for apache user?

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
calcop
Forum Newbie
Posts: 20
Joined: Fri Mar 04, 2005 2:13 pm

Function popen() doesn't work for apache user?

Post 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!
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Re: Function popen() doesn't work for apache user?

Post by Kieran Huggins »

Try shell_exec() instead

incidentally, popen() must be closed with pclose() - not fclose().
calcop
Forum Newbie
Posts: 20
Joined: Fri Mar 04, 2005 2:13 pm

Re: Function popen() doesn't work for apache user?

Post by calcop »

Thanks for catching that! :) Still doesn't keep $info from being populated. Any ideas?
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Re: Function popen() doesn't work for apache user?

Post by Kieran Huggins »

shell_exec didn't work?

check the permissions on "finger"
calcop
Forum Newbie
Posts: 20
Joined: Fri Mar 04, 2005 2:13 pm

Re: Function popen() doesn't work for apache user?

Post 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.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Re: Function popen() doesn't work for apache user?

Post 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
calcop
Forum Newbie
Posts: 20
Joined: Fri Mar 04, 2005 2:13 pm

Re: Function popen() doesn't work for apache user?

Post 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.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Re: Function popen() doesn't work for apache user?

Post by Kieran Huggins »

I thought you needed bidirectional access? no?

if not, use shell_exec.
calcop
Forum Newbie
Posts: 20
Joined: Fri Mar 04, 2005 2:13 pm

Re: Function popen() doesn't work for apache user?

Post by calcop »

No, just one-way. Getting kind of frustrating why it won't work from apache! hehe
calcop
Forum Newbie
Posts: 20
Joined: Fri Mar 04, 2005 2:13 pm

Re: Function popen() doesn't work for apache user?

Post by calcop »

Strange. It doesn't look like it is popen. I tried to grab the results from exec() and the same. Nothing.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Re: Function popen() doesn't work for apache user?

Post by Kieran Huggins »

hmmm... only thing I can suggest is checking the error logs for a clue as to why it won't work
calcop
Forum Newbie
Posts: 20
Joined: Fri Mar 04, 2005 2:13 pm

Re: Function popen() doesn't work for apache user?

Post by calcop »

Safe mode would prevent from executing anything. However, it is off.
calcop
Forum Newbie
Posts: 20
Joined: Fri Mar 04, 2005 2:13 pm

Re: Function popen() doesn't work for apache user?

Post by calcop »

Found the problem. I thought I configured SELinux to let apache to do so, but apparently I didn't.
calcop
Forum Newbie
Posts: 20
Joined: Fri Mar 04, 2005 2:13 pm

Re: Function popen() doesn't work for apache user?

Post 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
Post Reply