Debian Codegraph @

t/Test/Qpsmtpd/Plugin.pm

raw
1 package Test::Qpsmtpd::Plugin;
2 1;
3
4 # Additional plugin methods used during testing
5 package Qpsmtpd::Plugin;
6
7 use strict;
8 use warnings;
9
10 use Qpsmtpd::Constants;
11 use Test::More;
12
13 sub register_tests {
14
15 # Virtual base method - implement in plugin
16 }
17
18 sub register_test {
19 my ($plugin, $test, $num_tests) = @_;
20 $num_tests = 1 unless defined($num_tests);
21
22 # print STDERR "Registering test $test ($num_tests)\n";
23 push @{$plugin->{_tests}}, {name => $test, num => $num_tests};
24 }
25
26 sub total_tests {
27 my ($plugin) = @_;
28 my $total = 0;
29 foreach my $t (@{$plugin->{_tests}}) {
30 $total += $t->{num};
31 }
32 return $total;
33 }
34
35 sub run_tests {
36 my ($plugin, $qp) = @_;
37 foreach my $t (@{$plugin->{_tests}}) {
38 my $method = $t->{name};
39 print "# Running $method tests for plugin "
40 . $plugin->plugin_name . "\n";
41 local $plugin->{_qp} = $qp;
42 $plugin->$method();
43 }
44 }
45
46 sub validate_password {
47 my ($self, %a) = @_;
48
49 my ($pkg, $file, $line) = caller();
50
51 my $src_clear = $a{src_clear};
52 my $src_crypt = $a{src_crypt};
53 my $attempt_clear = $a{attempt_clear};
54 my $attempt_hash = $a{attempt_hash};
55 my $method = $a{method} or die "missing method";
56 my $ticket = $a{ticket};
57 my $deny = $a{deny} || DENY;
58
59 if (!$src_crypt && !$src_clear) {
60 $self->log(LOGINFO, "fail: missing password");
61 return ($deny, "$file - no such user");
62 }
63
64 if (!$src_clear && $method =~ /CRAM-MD5/i) {
65 $self->log(LOGINFO, "skip: cram-md5 not supported w/o clear pass");
66 return (DECLINED, $file);
67 }
68
69 if (defined $attempt_clear) {
70 if ($src_clear && $src_clear eq $attempt_clear) {
71 $self->log(LOGINFO, "pass: clear match");
72 return (OK, $file);
73 }
74
75 if ($src_crypt && $src_crypt eq crypt($attempt_clear, $src_crypt)) {
76 $self->log(LOGINFO, "pass: crypt match");
77 return (OK, $file);
78 }
79 }
80
81 if (defined $attempt_hash && $src_clear) {
82 if (!$ticket) {
83 $self->log(LOGERROR, "skip: missing ticket");
84 return (DECLINED, $file);
85 }
86
87 if ($attempt_hash eq hmac_md5_hex($ticket, $src_clear)) {
88 $self->log(LOGINFO, "pass: hash match");
89 return (OK, $file);
90 }
91 }
92
93 $self->log(LOGINFO, "fail: wrong password");
94 return ($deny, "$file - wrong password");
95 }
96
97 1;