Ezmlm/GpgKeyRing.pm untested

filter: Statementstmt 0.0% 0 / 131 Branchbran 0.0% 0 / 46 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 12 totaltotal 0.0% 0 / 213 CC 17 SCAR 24.7
CC
17
Cov
0%
_get_fingerprint
30.0
_get_keys
20.0
set_location
12.0
CRAP 11.9
1 # ===========================================================================
2 # Gpg.pm
3 # $Id$
4 #
5 # Object methods for gpg-ezmlm mailing lists
6 #
7 # Copyright (C) 2006, Lars Kruse, All Rights Reserved.
8 # Please send bug reports and comments to devel@sumpfralle.de
9 #
10 # This program is free software; you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 2 of the License, or
13 # (at your option) any later version.
14 #
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
19 #
20 # You should have received a copy of the GNU General Public License
21 # along with this program; if not, write to the Free Software
22 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 #
24 # ==========================================================================
25  
26 package Mail::Ezmlm::GpgKeyRing;
27  
28 use strict;
29 use vars qw($GPG_BIN $VERSION @ISA @EXPORT @EXPORT_OK);
30 use Carp;
31 use Crypt::GPG;
32  
33 $VERSION = '0.1';
34  
35 require 5.005;
36  
37 =head1 NAME
38  
39 Mail::Ezmlm::GpgKeyRing - Object Methods for gnupg keyring management
40  
41 =head1 SYNOPSIS
42  
43 use Mail::Ezmlm::GpgKeyRing;
44 $keyring = new Mail::Ezmlm::GpgKeyRing(DIRNAME);
45  
46 The rest is a bit complicated for a Synopsis, see the description.
47  
48 =head1 DESCRIPTION
49  
50 Mail::Ezmlm::GpgKeyRing is a Perl module that is designed to provide an object
51 interface to GnuPG keyrings for encrypted mailing lists.
52  
53 =cut
54  
55 # == Begin site dependant variables ==
56 $GPG_BIN = '/usr/bin/gpg'; # Autoinserted by Makefile.PL
57  
58  
59 # == check the gpg path ==
60 $GPG_BIN = '/usr/local/bin/gpg'
61 unless (-x "$GPG_BIN");
62 $GPG_BIN = '/usr/bin/gpg'
63 unless (-x "$GPG_BIN");
64 $GPG_BIN = '/bin/gpg'
65 unless (-x "$GPG_BIN");
66 $GPG_BIN = '/usr/local/bin/gpg2'
67 unless (-x "$GPG_BIN");
68 $GPG_BIN = '/usr/bin/gpg2'
69 unless (-x "$GPG_BIN");
70 $GPG_BIN = '/bin/gpg2'
71 unless (-x "$GPG_BIN");
72  
73 # == clean up the path ==
74 local $ENV{'PATH'} = "/bin";
75  
76 # check, if gpg is installed
77 unless (-x "$GPG_BIN") {
78 die("Warning: gnupg does not seem to be installed - none of the "
79 . "executables 'gpg' or 'gpg2' were found at the usual locations!");
80 }
81  
82  
83 # == Initialiser - Returns a reference to the object ==
84  
85 =head2 Setting up a new Mail::Ezmlm::GpgKeyRing object:
86  
87 use Mail::Ezmlm::GpgKeyRing;
88 $keyring = new Mail::Ezmlm::GpgKeyRing('/home/user/lists/foolist/.gnupg');
89  
90 new() returns the new instance for success, undefined if there was a problem.
91  
92 =cut
93  
94 sub new {
95 my($class, $keyring_dir) = @_;
96 my $self = {};
97 bless $self, ref $class || $class || 'Mail::Ezmlm::GpgKeyRing';
98 if ($self->set_location($keyring_dir)) {
99 return $self;
100 } else {
101 return undef;
102 }
103 }
104  
105  
106 # == Return the directory of the gnupg keyring ==
107  
108 =head2 Determining the location of the configured keyring.
109  
110 $whichkeyring = $keyring->get_location();
111 print $keyring->get_location();
112  
113 =cut
114  
115 sub get_location {
116 my($self) = shift;
117 return $self->{'KEYRING_DIR'};
118 }
119  
120  
121 # == Set the current keyring directory ==
122  
123 =head2 Changing which keyring the Mail::Ezmlm::GpgKeyRing object points at:
124
125 $keyring->set_location('/home/user/lists/foolist/.gnupg');
126  
127 =cut
128  
129 sub set_location {
130 my($self, $keyring_dir) = @_;
131 if (-e "$keyring_dir") {
132 if (-x "$keyring_dir") {
133 # at least it is a directory - so it looks ok
134 $self->{'KEYRING_DIR'} = $keyring_dir;
135 } else {
136 # it seems to be a file or something else - we complain
137 warn "GPG keyring location must be a directory: $keyring_dir";
138 $self->{'KEYRING_DIR'} = undef;
139 }
140 } else {
141 # probably the keyring directory does not exist, yet
142 # a warning should not be necessary
143 $self->{'KEYRING_DIR'} = $keyring_dir;
144 }
145 return $self->{'KEYRING_DIR'}
146 }
147  
148  
149 # == export a key ==
150  
151 =head2 Export a key:
152  
153 You may export public keys of the keyring.
154  
155 The key can be identified by its id or other (unique) patterns (like the
156 gnupg program).
157  
158 $keyring->export_key($key_id);
159 $keyring->export_key($email_address);
160  
161 The return value is a string containing the ascii armored key data.
162  
163 =cut
164  
165 sub export_key {
166 my ($self, $keyid) = @_;
167 my ($gpg, $gpgoption, $gpgcommand, $output);
168  
169 # return immediately - this avoids creating an empty keyring unintentionally
170 return () unless (-e $self->{'KEYRING_DIR'});
171 $gpg = $self->_get_gpg_object();
172 $gpgoption = "--armor --export $keyid";
173 $gpgcommand = $gpg->gpgbin() . " " . $gpg->gpgopts() . " $gpgoption";
174 $output = `$gpgcommand 2>/dev/null`;
175 if ($output) {
176 return $output;
177 } else {
178 return undef;
179 }
180 }
181  
182  
183 # == import a new key ==
184  
185 =head2 Import a key:
186  
187 You can import public or secret keys into the keyring.
188  
189 The key should be ascii armored.
190  
191 $keyring->import_key($ascii_armored_key_data);
192  
193 =cut
194  
195 sub import_key {
196 my ($self, $key) = @_;
197 my $gpg = $self->_get_gpg_object();
198 if ($gpg->addkey($key)) {
199 return (0==0);
200 } else {
201 return (1==0);
202 }
203 }
204  
205  
206 # == delete a key ==
207  
208 =head2 Delete a key:
209  
210 Remove a public key (and the matching secret key if it exists) from the keyring.
211  
212 The argument is the id of the key or any other unique pattern.
213  
214 $keyring->delete_key($keyid);
215  
216 =cut
217  
218 sub delete_key {
219 my ($self, $keyid) = @_;
220 my $gpg = $self->_get_gpg_object();
221 my $fprint = $self->_get_fingerprint($keyid);
222 return (1==0) unless (defined($fprint));
223 my $gpgoption = "--delete-secret-and-public-key $fprint";
224 my $gpgcommand = $gpg->gpgbin() . " " . $gpg->gpgopts() . " $gpgoption";
225 if (system($gpgcommand)) {
226 return (1==0);
227 } else {
228 return (0==0);
229 }
230 }
231  
232  
233 # == generate new private key ==
234  
235 =head2 Generate a new key:
236  
237 $keyring->generate_key($name, $comment, $email_address, $keysize, $expire);
238  
239 Refer to the documentation of gnupg for the format of the arguments.
240  
241 =cut
242  
243 sub generate_private_key {
244 my ($self, $name, $comment, $email, $keysize, $expire) = @_;
245 my $gpg = $self->_get_gpg_object();
246 my $gpgoption = "--gen-key";
247 my $gpgcommand = $gpg->gpgbin() . " " . $gpg->gpgopts() . " $gpgoption";
248 my $pid = open(INPUT, "| $gpgcommand");
249 print INPUT "Key-Type: DSA\n";
250 print INPUT "Key-Length: 1024\n";
251 print INPUT "Subkey-Type: ELG-E\n";
252 print INPUT "Subkey-Length: $keysize\n";
253 print INPUT "Name-Real: $name\n";
254 print INPUT "Name-Comment: $comment\n" if ($comment);
255 print INPUT "Name-Email: $email\n";
256 print INPUT "Expire-Date: $expire\n";
257 return close INPUT;
258 }
259  
260  
261 # == get_public_keys ==
262  
263 =head2 Getting public keys:
264  
265 Return an array of key hashes each containing the following elements:
266  
267 =over
268  
269 =item *
270 name
271  
272 =item *
273 email
274  
275 =item *
276 id
277  
278 =item *
279 expires
280  
281 =back
282  
283 $keyring->get_public_keys();
284 $keyring->get_secret_keys();
285  
286 =cut
287  
288 sub get_public_keys {
289 my ($self) = @_;
290 my @keys = $self->_get_keys("pub");
291 return @keys;
292 }
293  
294  
295 # == get_private_keys ==
296 # see above for POD (get_public_keys)
297 sub get_secret_keys {
298 my ($self) = @_;
299 my @keys = $self->_get_keys("sec");
300 return @keys;
301 }
302  
303  
304 ############ some internal functions ##############
305  
306 # == internal function for creating a gpg object ==
307 sub _get_gpg_object() {
308 my ($self) = @_;
309 my $gpg = new Crypt::GPG();
310 my $dirname = $self->get_location();
311 # replace whitespace characters in the keyring directory name
312 $dirname =~ s/(\s)/\\$1/g;
313 $gpg->gpgbin($GPG_BIN);
314 $gpg->gpgopts("--lock-multiple --no-tty --no-secmem-warning --batch --quiet --homedir $dirname");
315 return $gpg;
316 }
317  
318  
319 # == internal function to list keys ==
320 sub _get_keys() {
321 # type can be "pub" or "sec"
322 my ($self, $keyType) = @_;
323 my ($gpg, $flag, $gpgoption, @keys, $key);
324  
325 # return immediately - this avoids creating an empty keyring unintentionally
326 return () unless (-r $self->{'KEYRING_DIR'});
327 $gpg = $self->_get_gpg_object();
328 if ($keyType eq "pub") {
329 $flag = "pub";
330 $gpgoption = "--list-keys";
331 } elsif ($keyType eq "sec") {
332 $flag = "sec";
333 $gpgoption = "--list-secret-keys";
334 } else {
335 warn "wrong keyType: $keyType";
336 return undef;
337 }
338 my $gpgcommand = $gpg->gpgbin() . " " . $gpg->gpgopts() . " --with-colons $gpgoption";
339 my @read_keys = grep /^$flag/, `$gpgcommand`;
340 foreach $key (@read_keys) {
341 my ($type, $trust, $size, $algorithm, $id, $created,
342 $expires, $u2, $ownertrust, $uid) = split ":", $key;
343 # stupid way of "decoding" utf8 (at least it works for ":")
344 $uid =~ s/\\x3a/:/g;
345 $uid =~ /^(.*) <([^<]*)>/;
346 my $name = $1;
347 my $email = $2;
348 push @keys, {name => $name, email => $email, id => $id, expires => $expires};
349 }
350 return @keys;
351 }
352  
353  
354 # == internal function to retrieve the fingerprint of a key ==
355 sub _get_fingerprint()
356 {
357 my ($self, $key_id) = @_;
358 my $gpg = $self->_get_gpg_object();
359 $key_id =~ /^([0-9A-Z]*)$/;
360 $key_id = $1;
361 return undef unless ($key_id);
362 my $gpgoption = "--fingerprint $key_id";
363  
364 my $gpgcommand = $gpg->gpgbin() . " " . $gpg->gpgopts() . " --with-colons $gpgoption";
365
366 my @fingerprints = grep /^fpr:/, `$gpgcommand`;
367 if (@fingerprints > 1) {
368 warn "[Mail::Ezmlm::GpgKeyRing] more than one key matched ($key_id)!";
369 return undef;
370 }
371 return undef if (@fingerprints < 1);
372 my $fpr = $fingerprints[0];
373 $fpr =~ /^fpr:*([0-9A-Z]*):*$/;
374 $fpr = $1;
375 return undef unless $1;
376 return $1;
377 }
378  
379  
380 =head1 AUTHOR
381  
382 Lars Kruse <devel@sumpfralle.de>
383  
384 =head1 BUGS
385  
386 There are no known bugs.
387  
388 Please report bugs to the author or use the bug tracking system at
389 https://systemausfall.org/trac/ezmlm-web.
390  
391 =head1 SEE ALSO
392  
393 gnupg(7), gpg(1), gpg2(1), Crypt::GPG(3pm)
394  
395 https://systemausfall.org/toolforge/ezmlm-web/
396 http://www.ezmlm.org/
397  
398 =cut
399