473,397 Members | 2,068 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,397 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 7829
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: 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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.