#!/usr/bin/perl -w #---------------------------------------------------------------------------- # Easy and simple interface to GnuPG # # Copyright (c) 20021,2003 Baltasar Cevc, Walter Werther # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # DISCLAIMER: THIS SOFTWARE AND DOCUMENTATION IS PROVIDED "AS IS," AND # COPYRIGHT HOLDERS MAKE NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO, WARRANTIES OF MERCHANTABILITY # OR FITNESS FOR ANY PARTICULAR PURPOSE OR THAT THE USE OF THE SOFTWARE # OR DOCUMENTATION WILL NOT INFRINGE ANY THIRD PARTY PATENTS, COPYRIGHTS, # TRADEMARKS OR OTHER RIGHTS. # IF YOU USE THIS SOFTWARE, YOU DO SO AT YOUR OWN RISK. # # See this internet site for more details: http://technik.juz-kirchheim.de/ # # Creation: outsourced from useradm 13.08.03/bc # Last Update: 12.08.03 bc # Version: 0. 1. 2 # ---------------------------------------------------------------------------- use IO::File; use IO::Handle; use IO::Wrap; use GnuPG::Interface; package GnuPG::easygnupg; ################################################## # MODULE FUNCTIONS ################################################## sub check_passphrase { my ($homedir, $id, $passphrase) = @_; my ($gnupg); # elwomis::check_var('serviceconfig-dir', \$servicehash{'serviceconfig-dir'}, 'dir, exist, critical'); # my $gnupg_homedir = $servicehash{'serviceconfig-dir'}.'.gnupg'; # settting up the situation $gnupg = GnuPG::Interface->new(); $gnupg->options->hash_init( armor => 1, homedir => $homedir ); $gnupg->passphrase ($passphrase); return $gnupg->test_default_key_passphrase(); }; sub change_passphrase { my ($homedir, $id) = @_; system ('gpg', '--homedir', $homedir, '--edit-key', $id, 'passwd', 'save'); }; sub encrypt { my ($homedir, $filename, $string, $recipient) = @_; #my $senderpassword #my $sender = $recipient; my ($gnupg, $input, $output, $pid, $handles,$temp); # settting up the situation $gnupg = GnuPG::Interface->new(); $gnupg->options->hash_init( armor => 1, homedir => $homedir ); # Note you can set the recipients even if you aren't encrypting! $gnupg->options->push_recipients( $recipient ); $gnupg->options->meta_interactive( 0 ); #$gnupg->options->meta_signing_key_id ($sender); #$gnupg->passphrase ($senderpassword); # how we create some handles to interact with GnuPG $input = IO::Handle->new(); $output = IO::Handle->new(); $handles = GnuPG::Handles->new( stdin => $input, stdout => $output); # encrypt and sign #$pid = $gnupg->sign_and_encrypt( handles => $handles ); $pid = $gnupg->encrypt( handles => $handles ); # Now we write to the input of GnuPG print $input $string; close $input; open ($temp,">$filename"); while (<$output>) { print $temp $_; } close ($temp); waitpid $pid, 0; }; sub decrypt { my ($homedir, $filename, $password) = @_; my ($gnupg, $input, $line, $output, $handles, $pid,$temp,$stderr,$status,$logger,$passphrase); my $string = ''; # settting up the situation $gnupg = GnuPG::Interface->new(); $gnupg->options->hash_init( armor => 1, homedir => $homedir ); # GnuPG erstellen $gnupg->passphrase($password); # how we create some handles to interact with GnuPG $input = IO::Handle->new(); $output = IO::Handle->new(); $stderr = IO::Handle->new(); $status = IO::Handle->new(); $logger = IO::Handle->new(); $handles = GnuPG::Handles->new( stdin => $input, stdout => $output, stderr => $stderr, status => $status, logger => $logger); # Now we'll go about encrypting with the options already set $pid = $gnupg->decrypt(command_args => [ '--no-tty' ], handles => $handles ); open ($temp,$filename); while (<$temp>) { print $input $_; } close $input; close $temp; while ($line = <$output>) { $string .=$line } close ($output); waitpid $pid, 0; close ($stderr); close ($status); return '' unless ($string); return $string; }; ################################################## # POD DOCUMENTATION ################################################## =head1 NAME C - a very easy to use, simplicistic interface to GnuPG =head1 SYNOPSIS C C C C =head1 DESCRIPTION These functions have been written for use in a user administration script, where we needed to store some passwords in a more or less safe matter. As we didn't know much about the GnuPG-Interface, we read us trough quite a lot of examples and documentation and got these functions. They should provide a very easy to use interface to the main uses of GnuPG: encrypting and decrypting texts. For all actions, you must also specify the GnuPG homedir (not to be confused with a user's home directory - the GnuPG homedir is the path where GnuPG stores its keyrings, trustdbs etc.), which makes it possible to have totally separate keyrings for use with this module. If you have questions, feel free to contact the autors C! =head1 CHANGES None after splitting this module out of our user administration scripts. =head1 BUGS None known. If you find one, please notify the authors by mail C =head1 AUTHORS (C) 2002,2003 Walter Werther and Baltasar Cevc =head1 COPYRIGHT This program is copyrighted by its authors Walter Werther and Baltasar Cevc. It is free software - use annd distribute under the terms of the GPL (GNU Public License, Version 2, or at your choice any later version). =cut 1;