473,671 Members | 2,331 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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($p rocess)) {
while (!feof($pipes[1])) {
echo fgets($pipes[1], 1024);
}
fclose($pipes[1]);

$return_value = proc_close($pro cess);

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 7848
On Thu, 16 Dec 2004 11:21:42 -0600, "Bob Garbados" <bo*********@ya hoo.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...

$descriptorspe c = 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",
$descriptorspe c, $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($p rocess)) {
while (!feof($pipes[1])) {
echo fgets($pipes[1], 1024);
}
fclose($pipes[1]);

$return_value = proc_close($pro cess);

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.andyhsoftwa re.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.c om...
On Thu, 16 Dec 2004 11:21:42 -0600, "Bob Garbados" <bo*********@ya hoo.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...

$descriptorspe c = 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",
$descriptorspe c, $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($p rocess)) {
while (!feof($pipes[1])) {
echo fgets($pipes[1], 1024);
}
fclose($pipes[1]);

$return_value = proc_close($pro cess);

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.andyhsoftwa re.co.uk/space> Space: disk usage analysis tool



Jul 17 '05 #3
On Fri, 17 Dec 2004 09:27:26 -0600, "nntp.charter.n et" <bo*********@ya hoo.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",
$descriptorspe c, $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.andyhsoftwa re.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
5045
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, sometimes not. From mysql: mysql> show databases; +-----------+ | Database |
2
13613
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 * from Employee;" <% Dim objRS, sqlStmt set objRS = Server.CreateObject("ADODB.Recordset") Dim conn Set conn = Server.CreateObject("ADODB.Connection")
7
8655
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= 'select * from %s where cusid like %s ' Cursor.execute(sql,(name,recID))
9
9433
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
12410
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 provided by Informix. This is the problem: the execution of the translated SQL leaves the rows in the temp table correctly but raises error SQL0480N. It's very simple to try it: ------------
2
7621
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 Gcnndoh.Execute "AppendDataEntrySummaryLevel2" causes a Run-time error '-2147217900 (80040e14). If I execute the query from the query editor it's working ok!! No messages!!!
2
3330
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 clearer explanation... Page1 exists in WebApp1. In the code-behind of Page2 in WebApp2, I have put Server.Execute("Path to Page1 in WebApp1"). When I call Server.Execute(String) using "http://localhost/WebApp1/Page1.aspx", everything works ok...
8
18784
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 fetchall does not seem to make any difference, since it's the execute that uses memory. Breaking the query down to build lots of small tables doesn't help, since execute doesn't give its memory back, after reading enough small tables execute...
1
1974
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', '02') When I use direct string formating it works: a=('01','02')
9
4341
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). The browser then makes a new request to the server to redirect itself to abcd.asp after which the user gets redirected to abcd.asp. But in case of Server.Execute (or Server.Transfer), when the server
0
8473
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8390
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8911
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8819
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7428
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6222
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5692
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
2048
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1806
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.