473,385 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

execute gpg from php

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
3 7824
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Tim Williams | last post by:
I'm trying to write a simple python program to access a MySQL database. I'm having a problem with using MySQLdb to get the results of a SQL command in a cursor. Sometimes the cursor.execute works,...
2
by: Matt | last post by:
I want to exexute stored procedure in ASP, but it has error "Microsoft VBScript compilation (0x800A0401) Expected end of statement" on line (1). The stored procedure "sp_emp" contain "select *...
7
by: William Gill | last post by:
I have been trying to pass parameters as indicated in the api. when I use: sql= 'select * from %s where cusid = %s ' % name,recID) Cursor.execute(sql) it works fine, but when I try : sql=...
9
by: PyPK | last post by:
Hi if I have a function called tmp=0 def execute(): tmp = tmp+1 return tmp also I have def func1(): execute() ....
5
by: Gustavo Randich | last post by:
Hello, I'm writing an automatic SQL parser and translator from Informix to DB2. Now I'm faced with one of the most difficult things to translate, the "foreach execute procedure" functionality...
2
by: Norman Fritag | last post by:
Hi there The below code executes some queries. As newbie I was wondering weather you are better of using connection execute or command execute to execute queries? I am asking as...
2
by: Dune | last post by:
I'm trying to execute an aspx page by calling Server.Execute. The aspx page I'm trying to execute is in a different web app from the aspx page containing the Server.Execute statement. A slightly...
8
by: johnlichtenstein | last post by:
I am using cx_Oracle and MySQLdb to pull a lot of data from some tables and I find that the cursor.execute method uses a lot of memory that never gets garbage collected. Using fetchmany instead of...
1
by: gglegrp112 | last post by:
Is it possible to send an array as a parameter for an execute method in dbapi2 module? I'm using adodbapi and try to perfrom the following SQL query: select * from item where storeid in ('01',...
9
by: RN1 | last post by:
When a server encounters the line Response.Redirect("abcd.asp") in a ASP script, the server tells the browser that it has to be redirected to another page (which is abcd.asp, in this case)....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.