Ezmlm/GpgEzmlm.pm untested

filter: Statementstmt 0.0% 0 / 371 Branchbran 0.0% 0 / 142 Conditioncond 0.0% 0 / 48 MC/DCmcdc 0.0% 0 / 27 Subroutinesub 0.0% 0 / 27 Podpod 0.0% 0 / 19 totaltotal 0.0% 0 / 634 CC 70 SCAR 44.7
CC
70
Cov
0%
_cleanup_dotqmail_files
210.0
update_special
156.0
convert_to_encrypted
132.0
CRAP 87.1
1 # ===========================================================================
2 # GpgEzmlm.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::GpgEzmlm;
27  
28 use strict;
29 use warnings;
30 use diagnostics;
31 use vars qw($GPG_EZMLM_BASE $GPG_BIN $VERSION @ISA @EXPORT @EXPORT_OK);
32 use File::Copy;
33 use Carp;
34  
35 use Mail::Ezmlm;
36  
37 # this package inherits object methods from Mail::Ezmlm
38 @ISA = qw(Mail::Ezmlm);
39  
40 $VERSION = '0.1';
41  
42 require 5.005;
43  
44 =head1 NAME
45  
46 Mail::Ezmlm::GpgEzmlm - Object Methods for encrypted Ezmlm Mailing Lists
47  
48 =head1 SYNOPSIS
49  
50 use Mail::Ezmlm::GpgEzmlm;
51 $list = new Mail::Ezmlm::GpgEzmlm(DIRNAME);
52  
53 The rest is a bit complicated for a Synopsis, see the description.
54  
55 =head1 DESCRIPTION
56  
57 Mail::Ezmlm::GpgEzmlm is a Perl module that is designed to provide an object
58 interface to encrypted mailing lists based upon gpg-ezmlm.
59 See the gpg-ezmlm web page (http://www.synacklabs.net/projects/crypt-ml/) for
60 details about this software.
61  
62 The Mail::Ezmlm::GpgEzmlm class is inherited from the Mail::Ezmlm class.
63  
64 =cut
65  
66 # == Begin site dependant variables ==
67 $GPG_EZMLM_BASE = '/usr/bin'; # Autoinserted by Makefile.PL
68 $GPG_BIN = '/usr/bin/gpg'; # Autoinserted by Makefile.PL
69  
70 # == clean up the path for taint checking ==
71 local $ENV{PATH};
72 # the following lines were taken from "man perlrun"
73 $ENV{PATH} = $GPG_EZMLM_BASE;
74 $ENV{SHELL} = '/bin/sh' if exists $ENV{SHELL};
75 delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
76  
77  
78 # check, if gpg-ezmlm is installed
79 unless (-x "$GPG_EZMLM_BASE/gpg-ezmlm-manage.pl") {
80 die("Warning: gpg-ezmlm does not seem to be installed - "
81 . "executable '$GPG_EZMLM_BASE/gpg-ezmlm-manage.pl' not found!");
82 }
83  
84  
85 # == Initialiser - Returns a reference to the object ==
86  
87 =head2 Setting up a new Mail::Ezmlm::GpgEzmlm object:
88  
89 use Mail::Ezmlm::GpgEzmlm;
90 $list = new Mail::Ezmlm::GpgEzmlm('/home/user/lists/moolist');
91  
92 new() returns undefined if an error occoured.
93  
94 Use this function to access an existing encrypted mailing list.
95  
96 =cut
97  
98 sub new {
99 my ($class, $list_dir) = @_;
100 # call the previous initialization function
101 my $self = $class->SUPER::new($list_dir);
102 bless $self, ref $class || $class || 'Mail::Ezmlm::GpgEzmlm';
103 # define the available (supported) options for gpg-ezmlm ==
104 @{$self->{SUPPORTED_OPTIONS}} = (
105 "GnuPG",
106 "KeyDir",
107 "RequireSub",
108 "RequireSigs",
109 "NoKeyNoCrypt",
110 "SignMessages",
111 "EncryptToAll",
112 "VerifiedKeyReq",
113 "AllowKeySubmission");
114 # check if the mailing is encrypted
115 if (_is_encrypted($list_dir)) {
116 return $self;
117 } else {
118 return undef;
119 }
120 }
121  
122 # == convert an existing list to gpg-ezmlm ==
123  
124 =head2 Converting a plaintext mailing list to an encrypted list:
125  
126 You need to have a normal list before you can convert it into an encrypted list.
127 You can create plaintext mailing list with Mail::Ezmlm.
128  
129 $encrypted_list->Mail::Ezmlm::GpgEzmlm->convert_to_encrypted('/lists/foo');
130  
131 Use this function to convert a plaintext list into an encrypted mailing list.
132 The function returns a Mail::Ezmlm::GpgEzmlm object if it was successful.
133 Otherwise it returns undef.
134  
135 =cut
136  
137 sub convert_to_encrypted {
138 my $class = shift;
139 my $list_dir = shift;
140 my ($backup_dir);
141  
142 # untaint "list_dir"
143 $list_dir =~ m/^([\w\d\_\-\.\@ \/]+)$/;
144 if (defined($1)) {
145 $list_dir = $1;
146 } else {
147 warn "[GpgEzmlm] list directory contains invalid characters!";
148 return undef;
149 }
150  
151 # the backup directory will contain the old config file and the dotqmails
152 $backup_dir = _get_config_backup_dir($list_dir);
153 if ((! -e $backup_dir) && (!mkdir($backup_dir))) {
154 warn "[GpgEzmlm] failed to create gpg-ezmlm conversion backup dir ("
155 . "$backup_dir): $!";
156 return undef;
157 }
158  
159 # check the input
160 unless (defined($list_dir)) {
161 warn '[GpgEzmlm] must define directory in convert_to_encrypted()';
162 return undef;
163 }
164  
165 # does the list directory exist?
166 unless (-d $list_dir) {
167 warn '[GpgEzmlm] directory does not exist: ' . $list_dir;
168 return undef;
169 }
170  
171 # the list should currently _not_ be encrypted
172 if (_is_encrypted($list_dir)) {
173 warn '[GpgEzmlm] list is already encrypted: ' . $list_dir;
174 return undef;
175 }
176  
177  
178 # here starts the real conversion - the code is based on
179 # "gpg-ezmlm-convert.pl" - see http://www.synacklabs.net/projects/crypt-ml/
180  
181 # update the dotqmail files
182 return undef unless (_cleanup_dotqmail_files($list_dir, $backup_dir));
183  
184 # create the new config file, if it did not exist before
185 unless (-e "$backup_dir/config.gpg-ezmlm") {
186 if (open(CONFIG_NEW, ">$backup_dir/config.gpg-ezmlm")) {
187 # just create the empty file (default)
188 close CONFIG_NEW;
189 } else {
190 warn "[GpgEzmlm] failed to create new config file ("
191 . "$backup_dir/config.gpg-ezmlm): $!";
192 return undef;
193 }
194 }
195  
196 return undef unless (&_enable_encryption_config_file($list_dir));
197  
198 # create the (empty) gnupg keyring directory - this enables the keyring
199 # management interface. Don't create it, if it already exists.
200 if ((!-e "$list_dir/.gnupg") && (!mkdir("$list_dir/.gnupg", 0700))) {
201 warn "[GpgEzmlm] failed to create the gnupg keyring directory: $!";
202 return undef;
203 }
204  
205 my $result = $class->new($list_dir);
206 return $result;
207 }
208  
209 # == convert an encrypted list back to plaintext ==
210  
211 =head2 Converting an encryted mailing list to a plaintext list:
212  
213 $list->convert_to_plaintext();
214  
215 This function returns undef in case of errors. Otherwise the Mail::Ezmlm
216 object of the plaintext mailing list is returned.
217  
218 =cut
219  
220 sub convert_to_plaintext {
221 my $self = shift;
222 my ($dot_loc, $list_dir, $dot_prefix, $backup_dir);
223  
224 $list_dir = $self->thislist();
225 # untaint the input
226 $list_dir =~ m/^([\w\d\_\-\.\/\@]+)$/;
227 unless (defined($1)) {
228 # sanitize directory name (it must be safe to put the warn message)
229 $list_dir =~ s/\W/_/g;
230 warn "[GpgEzmlm] the list directory contains invalid characters: '"
231 . $list_dir . "' (special characters are escaped)";
232 return undef;
233 }
234 $list_dir = $1;
235  
236 # check if a directory was given
237 unless (defined($list_dir)) {
238 $self->_seterror(-1, 'must define directory in convert_to_plaintext()');
239 return undef;
240 }
241 # the list directory must exist
242 unless (-d $list_dir) {
243 $self->_seterror(-1, 'directory does not exist: ' . $list_dir);
244 return undef;
245 }
246 # check if the current object is still encrypted
247 unless (_is_encrypted($list_dir)) {
248 $self->_seterror(-1, 'list is not encrypted: ' . $list_dir);
249 return undef;
250 }
251  
252 # retrieve location of dotqmail-files
253 $dot_loc = _get_dotqmail_location($list_dir);
254  
255 # untaint "dot_loc"
256 $dot_loc =~ m/^([\w\d\_\-\.\@ \/]+)$/;
257 if (defined($1)) {
258 $dot_loc = $1;
259 } else {
260 $dot_loc =~ s/\W/_/g;
261 warn "[GpgEzmlm] directory name of dotqmail files contains invalid "
262 . "characters: $dot_loc (special characters are escaped)";
263 return undef;
264 }
265  
266 # the backup directory should contain the old config file (if it existed)
267 # and the original dotqmail files
268 $backup_dir = _get_config_backup_dir($self->thislist());
269 unless (-r $backup_dir) {
270 warn "[GpgEzmlm] failed to revert conversion - the backup directory "
271 . "is missing: $backup_dir";
272 return undef;
273 }
274  
275 # the "dot_prefix" is the basename of the main dotqmail file
276 # (e.g. '.qmail-list-foo')
277 $dot_loc =~ m/\/([^\/]+)$/;
278 if (defined($1)) {
279 $dot_prefix = $1;
280 } else {
281 warn '[GpgEzmlm] invalid location of dotqmail file: ' . $dot_loc;
282 return undef;
283 }
284  
285 # the "dotqmail" location must be valid
286 unless (defined($dot_loc) && ($dot_loc ne '') && (-e $dot_loc)) {
287 $self->_seterror(-1, 'dotqmail files not found: ' . $dot_loc);
288 return undef;
289 }
290  
291 # start reverting the gpg-ezmlm conversion:
292 # - restore old dotqmail files
293 # - restore old config file (if it existed before)
294  
295 # restore original config file (if it exists)
296 &_enable_plaintext_config_file($list_dir);
297  
298 # replace the dotqmail files with the ones from the backup
299 unless ((File::Copy::copy("$backup_dir/$dot_prefix", "$dot_loc"))
300 && (File::Copy::copy("$backup_dir/$dot_prefix-default",
301 "$dot_loc-default",))) {
302 warn "[GpgEzmlm] failed to restore dotqmail files: $!";
303 return undef;
304 }
305  
306 $self = Mail::Ezmlm->new($list_dir);
307 return $self;
308 }
309  
310 # == Update the "normal" settings of the current list ==
311  
312 =head2 Updating the common configuration settings of the current list:
313  
314 $list->update("moUx");
315  
316 =cut
317  
318 # update the "normal" (=not related to encryption) settings of the list
319 sub update {
320 my $self = shift;
321 my $options = shift;
322  
323 my ($result);
324  
325
326 # restore the ususal ezmlm-idx config file (for v0.4xx)
327 &_enable_plaintext_config_file($self->thislist());
328 # let ezmlm-make do the setup
329 $result = $self->SUPER::update($options);
330 # restore the gpg-ezmlm config file
331 &_enable_encryption_config_file($self->thislist());
332 # "repair" the dotqmail files (use "gpg-ezmlm-send" instead of "ezmlm-send")
333 &_cleanup_dotqmail_files($self->thislist());
334  
335 # return the result of the ezmlm-make run
336 return $result;
337 }
338  
339 # == Update the encryption settings of the current list ==
340  
341 =head2 Updating the configuration of the current list:
342  
343 $list->update_special({ 'allowKeySubmission' => 1 });
344  
345 =cut
346  
347 # update the encryption specific settings
348 sub update_special {
349 my ($self, %switches) = @_;
350 my (%ok_switches, $one_key, @delete_switches);
351  
352 # check for important files: 'config'
353 unless (_is_encrypted($self->thislist())) {
354 $self->_seterror(-1, "Update failed: '" . $self->thislist()
355 . "' does not appear to be a valid list");
356 return undef;
357 }
358  
359 @delete_switches = ();
360 # check if all supplied settings are supported
361 # btw we change the case (upper/lower) of the setting to the default one
362 foreach $one_key (keys %switches) {
363 my $ok_key;
364 foreach $ok_key (@{$self->{SUPPORTED_OPTIONS}}) {
365 # check the key case-insensitively
366 if ($ok_key =~ /^$one_key$/i) {
367 $ok_switches{$ok_key} = $switches{$one_key};
368 push @delete_switches, $one_key;
369 }
370 }
371 }
372 # remove all keys, that were accepted above
373 # we could not do it before, since this could cause issues with the current
374 # "foreach" looping through the hash
375 foreach $one_key (@delete_switches) {
376 delete $switches{$one_key};
377 }
378  
379 # %switches should be empty now
380 if (%switches) {
381 foreach $one_key (keys %switches) {
382 warn "[GpgEzmlm] unsupported setting: $one_key";
383 }
384 }
385  
386 my $errorstring;
387 my $config_file_old = $self->thislist() . "/config";
388 my $config_file_new = $self->thislist() . "/config.new";
389 my $gnupg_setting_found = (0==1);
390 if (open(CONFIG_OLD, "<$config_file_old")) {
391 if (open(CONFIG_NEW, ">$config_file_new")) {
392 my ($in_line, $one_opt, $one_val, $new_setting);
393 while (<CONFIG_OLD>) {
394 $in_line = $_;
395 $gnupg_setting_found = (0==0) if ($in_line =~ m/^\s*GnuPG\s+/i);
396 if (%ok_switches) {
397 my $found = 0;
398 while (($one_opt, $one_val) = each(%ok_switches)) {
399 # is this the right line (maybe commented out)?
400 if ($in_line =~ m/^#?\s*$one_opt\s+/i) {
401 print CONFIG_NEW _get_config_line($one_opt, $one_val);
402 delete $ok_switches{$one_opt};
403 $found = 1;
404 }
405 }
406 print CONFIG_NEW $in_line if ($found == 0);
407 } else {
408 # just print the remaining config file if no other settings are left
409 print CONFIG_NEW $in_line;
410 }
411 }
412 # write the remaining settings to the end of the file
413 while (($one_opt, $one_val) = each(%ok_switches)) {
414 print CONFIG_NEW _get_config_line($one_opt, $one_val);
415 }
416 # always set the default value for the "gpg" setting explicitely,
417 # if it was not overriden - otherwise gpg-ezmlm breaks on most
418 # systems (its default location is /usr/local/bin/gpg)
419 unless ($gnupg_setting_found) {
420 print CONFIG_NEW _get_config_line("GnuPG", $GPG_BIN);
421 }
422 } else {
423 $errorstring = "failed to write to temporary config file: $config_file_new";
424 $self->_seterror(-1, $errorstring);
425 warn "[GpgEzmlm] $errorstring";
426 close CONFIG_OLD;
427 return (1==0);
428 }
429 close CONFIG_NEW;
430 } else {
431 $errorstring = "failed to read the config file: $config_file_old";
432 $self->_seterror(-1, $errorstring);
433 warn "[GpgEzmlm] $errorstring";
434 return (1==0);
435 }
436 close CONFIG_OLD;
437 unless (rename($config_file_new, $config_file_old)) {
438 $errorstring = "failed to move new config file ($config_file_new) "
439 . "to original config file ($config_file_old)";
440 $self->_seterror(-1, $errorstring);
441 warn "[GpgEzmlm] $errorstring";
442 return (1==0);
443 }
444 $self->_seterror(undef);
445 return (0==0);
446 }
447  
448  
449 # return the configuration file string for a key/value combination
450 sub _get_config_line {
451 my $key = shift;
452 my $value = shift;
453  
454 my $result = "$key ";
455 if (($key eq "GnuPG") || ($key eq "keyDir")) {
456 # these are the only settings with string values
457 # escape special characters
458 $value =~ s/[^\w\.\/\-]/_/g;
459 $result .= $value;
460 } else {
461 $result .= ($value)? "yes" : "no";
462 }
463 $result .= "\n";
464 return $result;
465 }
466  
467 # == Get a list of options for the current list ==
468  
469 =head2 Getting the current configuration of the current list:
470  
471 $list->getconfig;
472  
473 getconfig() returns a hash including all available settings
474 (undefined settings are returned with their default value).
475  
476 =cut
477  
478 # call the original 'getconfig' function after restoring the "normal" config
479 # file (necessary only for ezmlm-idx < 0.4x)
480 sub getconfig {
481 my $self = shift;
482  
483 my ($result);
484  
485 &_enable_plaintext_config_file($self->thislist());
486 $result = $self->SUPER::getconfig();
487 &_enable_encryption_config_file($self->thislist());
488  
489 return $result;
490 }
491  
492 # retrieve the specific configuration of the list
493 sub getconfig_special {
494 my ($self) = @_;
495 my (%options, $list_dir);
496  
497 # continue with retrieving the encryption configuration
498  
499 # define defaults
500 $options{KeyDir} = '';
501 $options{SignMessages} = 1;
502 $options{NoKeyNoCrypt} = 0;
503 $options{AllowKeySubmission} = 1;
504 $options{EncryptToAll} = 0;
505 $options{VerifiedKeyReq} = 0;
506 $options{RequireSub} = 0;
507 $options{RequireSigs} = 0;
508  
509  
510 # Read the config file
511 $list_dir = $self->thislist();
512 if (open(CONFIG, "<$list_dir/config")) {
513 # 'config' contains the authorative information
514 while(<CONFIG>) {
515 if (/^(\w+)\s(.*)$/) {
516 my $optname = $1;
517 my $optvalue = $2;
518 my $one_opt;
519 foreach $one_opt (@{$self->{SUPPORTED_OPTIONS}}) {
520 if ($one_opt =~ m/^$optname$/i) {
521 if ($optvalue =~ /^yes$/i) {
522 $options{$one_opt} = 1;
523 } else {
524 $options{$one_opt} = 0;
525 }
526 }
527 }
528 }
529 }
530 close CONFIG;
531 } else {
532 $self->_seterror(-1, 'unable to read configuration file in getconfig()');
533 return undef;
534 }
535  
536 $self->_seterror(undef);
537 return %options;
538 }
539  
540  
541 # ********** internal functions ****************
542  
543 # return the location of the dotqmail files
544 sub _get_dotqmail_location {
545 my $list_dir = shift;
546 my ($plain_list, $dot_loc);
547  
548 $plain_list = Mail::Ezmlm->new($list_dir);
549 if ($plain_list) {
550 if (-r "$list_dir/dot") {
551 $dot_loc = $plain_list->getpart("dot");
552 chomp($dot_loc);
553 } elsif (-r "$list_dir/config") {
554 # the "config" file was used before ezmlm-idx v5
555 $dot_loc = $1 if ($plain_list->getpart("config") =~ /^T:(.*)$/m);
556 } else {
557 warn '[GpgEzmlm] list configuration file not found: ' . $list_dir;
558 $dot_loc = undef;
559 }
560 } else {
561 # return undef for invalid list directories
562 $dot_loc = undef;
563 }
564 return $dot_loc;
565 }
566  
567  
568 # return true if the given directory contains a gpg-ezmlm mailing list
569 sub _is_encrypted {
570 my $list_dir = shift;
571 my ($result, $plain_list);
572
573 # by default we assume, that the list is not encrypted
574 $result = 0;
575  
576 if (-e "$list_dir/lock") {
577 # it is a valid ezmlm-idx mailing list
578 $plain_list = Mail::Ezmlm->new($list_dir);
579 if ($plain_list) {
580 if (-e "$list_dir/config") {
581 my $content = $plain_list->getpart("config");
582 $content = '' unless defined($content);
583 # return false if we encounter the usual ezmlm-idx-v0.4-header
584 if ($content =~ /^F:/m) {
585 # this is a plaintext ezmlm-idx v0.4 mailing list
586 # this is a valid case - no warning necessary
587 } else {
588 # this is a gpg-ezmlm mailing list
589 $result = 1;
590 }
591 } else {
592 # gpg-ezmlm needs a "config" file - thus the list seems to be plain
593 # this is a valid case - no warning necessary
594 }
595 } else {
596 # failed to create a plaintext mailing list object
597 warn "[GpgEzmlm] failed to create Mail::Ezmlm object for: "
598 . $list_dir;
599 }
600 } else {
601 warn "[GpgEzmlm] Directory does not appear to contain a valid list: "
602 . $list_dir;
603 }
604  
605 return $result;
606 }
607  
608  
609 # what is done:
610 # - copy current dotqmail files to the backup directory
611 # - replace "ezmlm-send" and "ezmlm-manage" with the gpg-ezmlm replacements
612 # (in the real dotqmail files)
613 # This function should be called:
614 # 1) as part of the plaintext->encryption conversion of a list
615 # 2) after calling ezmlm-make for an encrypted list (since the dotqmail files
616 # are overwritten by ezmlm-make)
617 sub _cleanup_dotqmail_files {
618 my $list_dir = shift;
619 my ($backup_dir, $dot_loc, $dot_prefix);
620  
621 # where should we store the current dotqmail files?
622 $backup_dir = _get_config_backup_dir($list_dir);
623  
624 # retrieve location of dotqmail-files
625 $dot_loc = _get_dotqmail_location($list_dir);
626  
627 # untaint "dot_loc"
628 $dot_loc =~ m/^([\w\d\_\-\.\@ \/]+)$/;
629 if (defined($1)) {
630 $dot_loc = $1;
631 } else {
632 $dot_loc =~ s/\W/_/g;
633 warn "[GpgEzmlm] directory name of dotqmail files contains invalid "
634 . "characters: $dot_loc (escaped special characters)";
635 return undef;
636 }
637  
638 # the "dot_prefix" is the basename of the main dotqmail file
639 # (e.g. '.qmail-list-foo')
640 $dot_loc =~ m/\/([^\/]+)$/;
641 if (defined($1)) {
642 $dot_prefix = $1;
643 } else {
644 warn '[GpgEzmlm] invalid location of dotqmail file: ' . $dot_loc;
645 return undef;
646 }
647  
648 # check if the base dotqmail file exists
649 unless (defined($dot_loc) && ($dot_loc ne '') && (-e $dot_loc)) {
650 warn '[GpgEzmlm] dotqmail files not found: ' . $dot_loc;
651 return undef;
652 }
653  
654 # move the base dotqmail file
655 if (open(DOT_NEW, ">$backup_dir/$dot_prefix.new")) {
656 if (open(DOT_ORIG, "<$dot_loc")) {
657 my $line_found = (0==1);
658 while (<DOT_ORIG>) {
659 my $line = $_;
660 if ($line =~ /ezmlm-send\s+(\S+)/) {
661 print DOT_NEW "\|$GPG_EZMLM_BASE/gpg-ezmlm-send.pl $1\n";
662 $line_found = (0==0);
663 } else {
664 print DOT_NEW $line;
665 }
666 }
667 close DOT_ORIG;
668 # move the original file to the backup and the new file back
669 if ($line_found) {
670 unless ((rename($dot_loc, "$backup_dir/$dot_prefix"))
671 && (rename("$backup_dir/$dot_prefix.new", $dot_loc))) {
672 warn "[GpgEzmlm] failed to move base dotqmail file: $!";
673 return undef;
674 }
675 } else {
676 warn "[GpgEzmlm] Warning: I expected a pristine base "
677 . "dotqmail file: $dot_loc";
678 }
679 } else {
680 warn "[GpgEzmlm] failed to open base dotqmail file: $dot_loc";
681 return undef;
682 }
683 close DOT_NEW;
684 } else {
685 warn "[GpgEzmlm] failed to create new base dotqmail file: "
686 . "$backup_dir/$dot_prefix.new";
687 return undef;
688 }
689  
690 # move the "-default" dotqmail file
691 if (open(DEFAULT_NEW, ">$backup_dir/$dot_prefix-default.new")) {
692 if (open(DEFAULT_ORIG, "<$dot_loc-default")) {
693 my $line_found = (0==1);
694 while (<DEFAULT_ORIG>) {
695 my $line = $_;
696 if ($line =~ /ezmlm-manage\s+(\S+)/) {
697 print DEFAULT_NEW "\|$GPG_EZMLM_BASE/gpg-ezmlm-manage.pl $1\n";
698 $line_found = (0==0);
699 } else {
700 print DEFAULT_NEW $line;
701 }
702 }
703 close DEFAULT_ORIG;
704 # move the original file to the backup and the new file back
705 if ($line_found) {
706 unless ((rename("$dot_loc-default",
707 "$backup_dir/$dot_prefix-default"))
708 && (rename("$backup_dir/$dot_prefix-default.new",
709 "$dot_loc-default"))) {
710 warn "[GpgEzmlm] failed to move default dotqmail file: $!";
711 return undef;
712 }
713 } else {
714 warn "[GpgEzmlm] Warning: I expected a pristine default "
715 . "dotqmail file: $dot_loc-default";
716 }
717 } else {
718 warn "[GpgEzmlm] failed to open default dotqmail file: "
719 . "$dot_loc-default";
720 return undef;
721 }
722 close DEFAULT_NEW;
723 } else {
724 warn "[GpgEzmlm] failed to create new default dotqmail file: "
725 . "$backup_dir/$dot_prefix-default.new";
726 return undef;
727 }
728  
729 return (0==0);
730 }
731  
732  
733 # activate the config file for encryption (gpg-ezmlm)
734 sub _enable_encryption_config_file {
735 my $list_dir = shift;
736 my ($backup_dir);
737  
738 $backup_dir = _get_config_backup_dir($list_dir);
739  
740 # check, if the current config file is for gpg-ezmlm or for ezmlm-idx
741 if (_is_encrypted($list_dir)) {
742 warn "[GpgEzmlm] I expected a pristine ezmlm-idx config file: "
743 . "$list_dir/config";
744 return undef;
745 }
746  
747 # store the current original config file
748 if ((-e "$list_dir/config") && (!File::Copy::copy("$list_dir/config",
749 "$backup_dir/config.original"))) {
750 warn "[GpgEzmlm] failed to save the current ezmlm-idx config file ('"
751 . "$list_dir/config') to '$backup_dir/config.original': $!";
752 return undef;
753 }
754  
755 # copy the encryption config file to the list directory
756 unless (File::Copy::copy("$backup_dir/config.gpg-ezmlm",
757 "$list_dir/config")) {
758 warn "[GpgEzmlm] failed to enable the gpg-ezmlm config file (from '"
759 . "$backup_dir/config.gpg-ezmlm' to '$list_dir/config'): $!";
760 return undef;
761 }
762  
763 return (0==0);
764 }
765  
766  
767 # activate the config file for plain ezmlm-idx lists
768 sub _enable_plaintext_config_file {
769 my $list_dir = shift;
770 my ($backup_dir);
771  
772 $backup_dir = _get_config_backup_dir($list_dir);
773  
774 # check, if the current config file is for gpg-ezmlm or for ezmlm-idx
775 unless (_is_encrypted($list_dir)) {
776 warn "[GpgEzmlm] I expected a config file for gpg-ezmlm: "
777 . "$list_dir/config";
778 return undef;
779 }
780  
781 # store the current gpg-ezmlm config file
782 unless (File::Copy::copy("$list_dir/config",
783 "$backup_dir/config.gpg-ezmlm")) {
784 warn "[GpgEzmlm] failed to save the current gpg-ezmlm config file ('"
785 . "$list_dir/config') to '$backup_dir/config.gpg-ezmlm': $!";
786 return undef;
787 }
788  
789 # copy the ezmlm-idx config file to the list directory - or remove the
790 # currently active gpg-ezmlm config file
791 if (-e "$backup_dir/config.original") {
792 unless (File::Copy::copy("$backup_dir/config.original",
793 "$list_dir/config")) {
794 warn "[GpgEzmlm] failed to enable the originnal config file (from '"
795 . "$backup_dir/config.original' to '$list_dir/config': $!";
796 return undef;
797 }
798 } else {
799 unless (unlink("$list_dir/config")) {
800 warn "[GpgEzmlm] failed to remove the gpg-ezmlm config file ("
801 . "$list_dir/config): $!";
802 return undef;
803 }
804 }
805  
806 return (0==0);
807 }
808  
809  
810 # where should the dotqmail files and the config file be stored?
811 sub _get_config_backup_dir {
812 my $list_dir = shift;
813 return $list_dir . '/.gpg-ezmlm.backup';
814 }
815  
816  
817 # == check version of gpg-ezmlm ==
818 sub check_gpg_ezmlm_version {
819 my $ret_value = system("'$GPG_EZMLM_BASE/gpg-ezmlm-convert.pl' --version &>/dev/null");
820 # for now we do not need a specific version of gpg-ezmlm - it just has to
821 # know the "--version" argument (available since gpg-ezmlm 0.3.4)
822 return ($ret_value == 0);
823 }
824  
825 # == check if gpg-ezmlm is installed ==
826 sub is_available {
827 # the existence of the gpg-ezmlm script is sufficient for now
828 return -e "$GPG_EZMLM_BASE/gpg-ezmlm-convert.pl";
829 }
830  
831 ############ some internal functions ##############
832  
833 # == return an error message if appropriate ==
834 sub errmsg {
835 my ($self) = @_;
836 return $self->{'ERRMSG'};
837 }
838  
839 sub errno {
840 my ($self) = @_;
841 return $self->{'ERRNO'};
842 }
843  
844  
845 # == Internal function to set the error to return ==
846 sub _seterror {
847 my ($self, $no, $mesg) = @_;
848  
849 if (defined($no) && $no) {
850 if ($no < 0) {
851 $self->{'ERRNO'} = -1;
852 $self->{'ERRMSG'} = $mesg || 'An undefined error occoured';
853 } else {
854 $self->{'ERRNO'} = $no / 256;
855 $self->{'ERRMSG'} = $! || $mesg || 'An undefined error occoured in a system() call';
856 }
857 } else {
858 $self->{'ERRNO'} = 0;
859 $self->{'ERRMSG'} = undef;
860 }
861 return 1;
862 }
863  
864 1;
865  
866 =head1 AUTHOR
867  
868 Lars Kruse <devel@sumpfralle.de>
869  
870 =head1 BUGS
871  
872 There are no known bugs.
873  
874 Please report bugs to the author or use the bug tracking system at
875 https://systemausfall.org/trac/ezmlm-web.
876  
877 =head1 SEE ALSO
878  
879 ezmlm(5), ezmlm-make(2), ezmlm-sub(1),
880 ezmlm-unsub(1), ezmlm-list(1), ezmlm-issub(1)
881  
882 https://systemausfall.org/toolforge/ezmlm-web/
883 http://www.synacklabs.net/projects/crypt-ml/
884 http://www.ezmlm.org/
885 http://www.qmail.org/
886  
887 =cut