LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-12-2005, 01:16 AM   #1
rblampain
Senior Member
 
Registered: Aug 2004
Location: Western Australia
Distribution: Debian 11
Posts: 1,288

Rep: Reputation: 52
PHP GnuPG encryption/decryption


I found on the web a snippet of PHP code that supposedly does what I need: encrypt a small file received through a CGI routine, however I cannot get it to work probably due to my very basic knowledge of PHP, here is the code:

enc.php
<?php
$gpg='/usr/bin/gpg';
$nam='myname@internode.on.net';
$ifile='file2encode.txt';
echo shell_exec("$gpg -e -r $nam $ifile");
?>

Typing "http://192.168.1.13/cgi-bin/enc.php in the browser gives
"exec format error on enc.php"
in /var/log/httpd/error_log

The following code entered at the command line works:
$ gpg -e -r myname@internode.on.net file2encode.txt

Can you help with this?

Thank you.
 
Old 08-12-2005, 03:12 AM   #2
Thinking
Member
 
Registered: Oct 2003
Posts: 249

Rep: Reputation: 30
well i'm not sure what the problem is

i tried your code using commandline php and it worked
so try this one

<?php
$gpg='/usr/bin/gpg';
$nam='myname@internode.on.net';
$ifile='file2encode.txt';
$retval=`$gpg -e -r $nam $ifile`;
echo $retval;
?>

or

<?php
$gpg='/usr/bin/gpg';
$nam='myname@internode.on.net';
$ifile='file2encode.txt';
system("$gpg -e -r $nam $ifile",$retval);
echo $retval;
?>

but i think both won't work
because i think you have a failure configuration of the apache
do you have php configured?
i think apache (or whatever you use for http server) just does not know what to do with a php file
 
Old 08-12-2005, 06:49 AM   #3
rblampain
Senior Member
 
Registered: Aug 2004
Location: Western Australia
Distribution: Debian 11
Posts: 1,288

Original Poster
Rep: Reputation: 52
I have Apache (httpd) configured and working. I have a number of CGI routines in /var/www/cgi-bin that Apache has no problem to find and execute (I just checked it)

But if the script works on your machine, the bug is not in the script.
You were right, your suggestions didn't work.

Thank you for the hint.
I misread your question: no I didn't configure PHP.

Last edited by rblampain; 08-12-2005 at 06:59 AM.
 
Old 08-12-2005, 07:05 AM   #4
rblampain
Senior Member
 
Registered: Aug 2004
Location: Western Australia
Distribution: Debian 11
Posts: 1,288

Original Poster
Rep: Reputation: 52
Checking this directory (/var/www/cgi-bin), I found there is no PHP script in it, could it have something to do with PHP?
 
Old 08-12-2005, 09:19 AM   #5
Thinking
Member
 
Registered: Oct 2003
Posts: 249

Rep: Reputation: 30
ok, let's figure it out ;-)

1. google told me that "exec format error on ..." most time means, that the program can't be executed
think about just copy a .exe from windows to a linux distri, make a chmod, and try to execute the .exe file
i didn't try it, but you should get something like "exec format error on" because linux (the operating system itself) doesn't understand the .exe format

http://minitutorials.com/apache/php12.shtml
http://www.thesitewizard.com/archive/php4install.shtml (for windows, but interesting too)
http://www.webmasterstop.com/48-3.html (windows too)
http://docs.php.net/en/install.unix.apache.html

2. you said you have many cgi routines in /var/www/cgi-bin but no PHP script!
i wrote a cgi prog in C for windows and linux and as far as i know a CGI program is nothing else than a simple executable file which uses environment variables and printf to get apache information and print the response which is shown to the user

i'm not sure, but i never had a php script in cgi-bin!
because a php script is something different! you need php to interpret the script

i'm thinking about 2 problems:
1. i think php itself isn't correctly configured in your apache or the php script itself has to be in a different directory than cgi-bin
(check, apache php configuration and read through some php apache manuals)
2. i don't know if it will work, but heres a workaround
change the script to:

#!/usr/bin/php
<?php
$gpg='/usr/bin/gpg';
$nam='myname@internode.on.net';
$ifile='file2encode.txt';
echo shell_exec("$gpg -e -r $nam $ifile");
?>


the only difference is the first line (#!/usr/bin/php)
you have to change #!/usr/bin/php to the right path to the php executable! (but in most cases it should work)
then make chmod for your script (the script is in cgi-bin already!!) to make it executable

in the command line try
/var/www/cgi-bin/enc.php

if this works!
you can try to use it in the browser (just like you did already)

it's just a workaround
 
Old 08-12-2005, 10:10 AM   #6
rblampain
Senior Member
 
Registered: Aug 2004
Location: Western Australia
Distribution: Debian 11
Posts: 1,288

Original Poster
Rep: Reputation: 52
We're progressing!
I changed the line as you suggested: #!/usr/bin/php
Accessing enc.php from the command line gives:
usage: gpg [options] --encrypt [filename]
Content-type: text/html
X-Powered-By: PHP/4.3.4

And accessing through the browser gives:
Security Alert! The PHP CGI cannot be accessed directly.
This PHP CGI binary was compiled with force-cgi-redirect enabled. This means that a page will only be served up if the REDIRECT_STATUS CGI variable is set, e.g. via an Apache Action directive.
For more information as to why this behaviour exists, see the manual page for CGI security.
For more information about changing this behaviour or re-enabling this webserver, consult the installation file that came with this distribution, or visit the manual page.

I'll have a good look at the manuals but I think I'll probably need to open a thread about HTTPD config.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
right-click encryption/decryption for Gnome? DJOtaku Linux - General 1 09-23-2005 11:40 AM
Hardware real time encryption/decryption in Linux... Akonbobot Linux - Security 2 11-24-2004 01:33 AM
tar encryption (gnupg problems) wedgeworth Linux - Security 1 07-01-2004 04:08 PM
passwd decryption in php questioner Programming 5 02-15-2004 10:35 PM
Mandrake 9.0 Wireless Works without encryption.. does not with encryption topcat Linux - Wireless Networking 3 05-04-2003 08:47 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:23 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration