By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,903 Members | 1,856 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,903 IT Pros & Developers. It's quick & easy.

execute gpg from php

P: n/a
I'm pretty new to php\linux and I'm trying to encrypt a file using gpg from
a php page on a linux box and it's not working. The following code works
for executing 'ls', but not gpg...

$descriptorspec = array(
0 => array("pipe", "r"), // stdin is a pipe that the child will read
from
1 => array("pipe", "w"), // stdout is a pipe that the child will write
to
2 => array("file", "/tmp/error-output.txt", "a") // stderr is a file to
write to
);

//LINE BELOW DOESN'T WORK
//$process = proc_open(gpg --output encmail.txt --recipient
so*****@domain.com --always-trust --armor --yes --encrypt mail.txt",
$descriptorspec, $pipes);

//LINE BELOW WORKS
$process = proc_open("ls", $descriptorspec, $pipes);

if (is_resource($process)) {
while (!feof($pipes[1])) {
echo fgets($pipes[1], 1024);
}
fclose($pipes[1]);

$return_value = proc_close($process);

echo "command returned $return_value\n";
}

When I try to execute gpg, the value returned is 2, but that means nothing
to me. Am I doing something wrong from the php side of things or am I
overlooking something on the server?

Thanks.
Jul 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On Thu, 16 Dec 2004 11:21:42 -0600, "Bob Garbados" <bo*********@yahoo.com>
wrote:
I'm pretty new to php\linux and I'm trying to encrypt a file using gpg from
a php page on a linux box and it's not working. The following code works
for executing 'ls', but not gpg...

$descriptorspec = array(
0 => array("pipe", "r"), // stdin is a pipe that the child will read
from
1 => array("pipe", "w"), // stdout is a pipe that the child will write
to
2 => array("file", "/tmp/error-output.txt", "a") // stderr is a file to
write to
);

//LINE BELOW DOESN'T WORK
//$process = proc_open(gpg --output encmail.txt --recipient
so*****@domain.com --always-trust --armor --yes --encrypt mail.txt",
$descriptorspec, $pipes);
This would give a parse error when uncommented. What code did you really try?
//LINE BELOW WORKS
$process = proc_open("ls", $descriptorspec, $pipes);

if (is_resource($process)) {
while (!feof($pipes[1])) {
echo fgets($pipes[1], 1024);
}
fclose($pipes[1]);

$return_value = proc_close($process);

echo "command returned $return_value\n";
}

When I try to execute gpg, the value returned is 2, but that means nothing
to me. Am I doing something wrong from the php side of things or am I
overlooking something on the server?


Is there anything in /tmp/error-output.txt ? Do you have permissions to write
to there in the first place?

If it didn't get that far, then it'll be the underlying exec* call that
generated an error. You've said you're on Linux, so looking up error codes in
Linux shows that 2 is:

#define ENOENT 2 /* No such file or directory */

Is gpg on the PATH environment variable as seen by the webserver? Try
specifying a complete path to the executable.

Is gpg executable by the user the webserver runs as?

--
Andy Hassall / <an**@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Jul 17 '05 #2

P: n/a
Thanks for the response.

Here's the line that doesn't work, it was just missing a " in front of the
gpg command...
$process = proc_open("gpg --output encmail.txt --recipient
so*****@domain.com --always-trust --armor --yes --encrypt mail.txt",
$descriptorspec, $pipes);

I temporarily gave the Apache user full rights, I can ssh to the server, su
to apache, and execute the gpg command above in the proc_open call without a
hitch; so gpg is on the PATH variable and it's executable by the apache
user.

Here's the error message in /tmp/error-output.txt... gpg: fatal:
/root/.gnupg: can't create directory: Permission denied.

I don't get it. If I can run the gpg command successfullty from the command
line as the apache user, why would the apache user not be able to run the
gpg command from a php page?

Bob

"Andy Hassall" <an**@andyh.co.uk> wrote in message
news:53********************************@4ax.com...
On Thu, 16 Dec 2004 11:21:42 -0600, "Bob Garbados" <bo*********@yahoo.com>
wrote:
I'm pretty new to php\linux and I'm trying to encrypt a file using gpg froma php page on a linux box and it's not working. The following code works
for executing 'ls', but not gpg...

$descriptorspec = array(
0 => array("pipe", "r"), // stdin is a pipe that the child will read
from
1 => array("pipe", "w"), // stdout is a pipe that the child will writeto
2 => array("file", "/tmp/error-output.txt", "a") // stderr is a file towrite to
);

//LINE BELOW DOESN'T WORK
//$process = proc_open(gpg --output encmail.txt --recipient
so*****@domain.com --always-trust --armor --yes --encrypt mail.txt",
$descriptorspec, $pipes);
This would give a parse error when uncommented. What code did you really

try?
//LINE BELOW WORKS
$process = proc_open("ls", $descriptorspec, $pipes);

if (is_resource($process)) {
while (!feof($pipes[1])) {
echo fgets($pipes[1], 1024);
}
fclose($pipes[1]);

$return_value = proc_close($process);

echo "command returned $return_value\n";
}

When I try to execute gpg, the value returned is 2, but that means nothingto me. Am I doing something wrong from the php side of things or am I
overlooking something on the server?
Is there anything in /tmp/error-output.txt ? Do you have permissions to

write to there in the first place?

If it didn't get that far, then it'll be the underlying exec* call that
generated an error. You've said you're on Linux, so looking up error codes in Linux shows that 2 is:

#define ENOENT 2 /* No such file or directory */

Is gpg on the PATH environment variable as seen by the webserver? Try
specifying a complete path to the executable.

Is gpg executable by the user the webserver runs as?

--
Andy Hassall / <an**@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool



Jul 17 '05 #3

P: n/a
On Fri, 17 Dec 2004 09:27:26 -0600, "nntp.charter.net" <bo*********@yahoo.com>
wrote:
Here's the line that doesn't work, it was just missing a " in front of the
gpg command...
$process = proc_open("gpg --output encmail.txt --recipient
so*****@domain.com --always-trust --armor --yes --encrypt mail.txt",
$descriptorspec, $pipes);

I temporarily gave the Apache user full rights, I can ssh to the server, su
to apache, and execute the gpg command above in the proc_open call without a
hitch; so gpg is on the PATH variable and it's executable by the apache
user.

Here's the error message in /tmp/error-output.txt... gpg: fatal:
/root/.gnupg: can't create directory: Permission denied.
The webserver is unlikely to ever be able to write to root's home directory -
at least you'd hope so!
I don't get it. If I can run the gpg command successfullty from the command
line as the apache user, why would the apache user not be able to run the
gpg command from a php page?


Apache initially starts as root (so it can bind to port 80), but the child
process that ends up serving requests gets setuid down to a less privileged
user. It may well be that there are remnants of root's environment variables
inherited into the child process's environment, enough that gpg thinks it
should be creating directories in /root.

Whereas, when you log in as the apache user, all you get is the apache user's
environment. So the environment would point to the user's own home directory,
which would be writable.

man gpg says:

<<
OPTIONS
Long options can be put in an options file (default
"~/.gnupg/gpg.conf")

Sounds like it's seeing $HOME=/root then. Apache on my test server has HOME=/
- which also wouldn't work well here.

Later it has:

<<
--homedir directory
Set the name of the home directory to directory If this option
is not used it defaults to
"~/.gnupg". It does not make sense to use this in a options
file. This also overrides the
environment variable "GNUPGHOME".


Try using that to point to a more appropriate directory. I've not used gpg
personally, so there may be more issues.

--
Andy Hassall / <an**@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.