From adbbfe6f675fbbb3ca661e25970352fcfd7d1d3f Mon Sep 17 00:00:00 2001 From: Matt Simerson Date: Mon, 7 May 2012 03:35:58 -0400 Subject: [PATCH] auth_vpopmail: refactored, added tests, logging added more logging standard log prefixes tests run a pretest to make sure tests have a chance to succeed --- plugins/auth/auth_vpopmail | 20 +++++++++++--------- t/plugin_tests/auth/auth_vpopmail | 20 +++++++++++++------- 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/plugins/auth/auth_vpopmail b/plugins/auth/auth_vpopmail index 99d9a1a..ab05698 100644 --- a/plugins/auth/auth_vpopmail +++ b/plugins/auth/auth_vpopmail @@ -45,11 +45,13 @@ use warnings; use Qpsmtpd::Constants; use Digest::HMAC_MD5 qw(hmac_md5_hex); -use vpopmail; +#use vpopmail; # we eval this in $test_vpopmail sub register { my ($self, $qp) = @_; + return (DECLINED) if ! $self->test_vpopmail_module(); + $self->register_hook("auth-plain", "auth_vpopmail" ); $self->register_hook("auth-login", "auth_vpopmail" ); $self->register_hook("auth-cram-md5", "auth_vpopmail"); @@ -62,9 +64,6 @@ sub auth_vpopmail { $self->log(LOGINFO, "Authenticating against vpopmail: $user"); - return (DECLINED, "auth_vpopmail - plugin not configured correctly") - if !test_vpopmail(); - my $pw = vauth_getpw($pw_name, $pw_domain); my $pw_clear_passwd = $pw->{pw_clear_passwd}; my $pw_passwd = $pw->{pw_passwd}; @@ -104,17 +103,20 @@ sub auth_vpopmail { return (DENY, "auth_vpopmail - unknown error"); } -sub test_vpopmail { - +sub test_vpopmail_module { + my $self = shift; # vpopmail will not allow vauth_getpw to succeed unless the requesting user is vpopmail or root. # by default, qpsmtpd runs as the user 'qpsmtpd' and does not have permission. eval "use vpopmail"; if ( $@ ) { - warn "vpopmail perl module not installed.\n"; + $self->log(LOGERROR, "skip: is vpopmail perl module installed?"); return; }; + my ($domain) = vpopmail::vlistdomains(); - my $r = vauth_getpw('postmaster', $domain); - return if !$r; + my $r = vauth_getpw('postmaster', $domain) or do { + $self->log(LOGERROR, "skip: could not query vpopmail"); + return; + }; return 1; } diff --git a/t/plugin_tests/auth/auth_vpopmail b/t/plugin_tests/auth/auth_vpopmail index 277a802..11cbdfa 100644 --- a/t/plugin_tests/auth/auth_vpopmail +++ b/t/plugin_tests/auth/auth_vpopmail @@ -2,26 +2,32 @@ sub register_tests { my $self = shift; + $self->register_test("test_auth_vpopmail", 3); } my @u_list = qw ( good bad none ); my %u_data = ( good => [ 'postmaster@example.com', OK, 'Good Strong Passphrase' ], - bad => [ 'bad@example.com', DENY, 'not_bad_pass' ], + bad => [ 'bad@example.com', DENY, 'not_bad_pass' ], none => [ 'none@example.com', DECLINED, '' ], ); sub test_auth_vpopmail { my $self = shift; + + if ( ! $self->test_vpopmail_module ) { + $self->log(LOGERROR, "vpopmail plugin not configured" ); + foreach ( 0..2) { ok( 1, "test_auth_vpopmail, skipped") }; + return; + }; + my ($tran, $ret, $note, $u, $r, $p, $a ); $tran = $self->qp->transaction; for $u ( @u_list ) { - ( $a,$r,$p ) = @{$u_data{$u}}; - ($ret, $note) = $self->auth_vpopmail($tran,'CRAMMD5',$a,$p); - defined $note or $note='auth_vpopmail: No-Message'; - is ($ret, $r, $note); - # - for debugging. - # warn "$note\n"; + ( $a,$r,$p ) = @{$u_data{$u}}; + ($ret, $note) = $self->auth_vpopmail($tran,'CRAMMD5',$a,$p); + defined $note or $note='auth_vpopmail: No-Message'; + is ($ret, $r, $note); } }