10 March 2006

Apache SSL and gnupg and PERL

What a day. I was having a lot of problem with invoking gnupg from a PERL script running under Apache SSL.

I was getting the error message
gpg: fatal: ~/.gnupg: can't create directory: No such file or directory

Everything looks fine. The PERL script was running under the correct ID and the .gnupg directory and the files in it had the correct permissions (700) so what was wrong.

I found a few hits on the web but the solution looked a bit unsafe (give gnupg world access!). So I started to experiment with gpg --homedir /home/yyy/xxx and even when the xxx directory was only accessible by the owner, gpg created the files in it OK. So, I set gpg --homedir /home/yyy/.gnupg and, surprise surprise, it worked.

I did not like hard coding the homedir in the PERL script as it could fail in the future should the files get moved. So I used the following code to make it general purpose:


# for some reason gpg does not work unless you specify the home directory explicitly!
my @pwArray = getpwuid($>); # get info from password file for this effective ID
# password entry [7] is the home directory
my $gpgOptions = "--homedir $pwArray[7]/.gnupg --batch --yes -a etc
my $encrypted = `echo "$toEncrypt" gpg $gpgOptions 2>&1`;


I hope this helps others.

1 comment:

Rufus Cole said...

There's not too much out there when you're troubleshooting for Apache SSL out there - you have to look deep for the exact issue you're looking for, but this might help others. Thanks a lot.