| 1 |
#!perl -w |
| 2 |
|
| 3 |
use strict; |
| 4 |
use warnings; |
| 5 |
|
| 6 |
use Qpsmtpd::Constants; |
| 7 |
|
| 8 |
=head1 NAME |
| 9 |
|
| 10 |
random_error |
| 11 |
|
| 12 |
=head1 DESCRIPTION |
| 13 |
|
| 14 |
This plugin randomly disconnects and issues DENYSOFTs. |
| 15 |
|
| 16 |
=head1 CONFIG |
| 17 |
|
| 18 |
one parameter is allowed, which is how often to error, as a percentage |
| 19 |
of messages. The default is 1. Use a negative number to disable. |
| 20 |
|
| 21 |
2/5 of failures are DENYSOFT_DISCONNECT, 3/5 simply DENYSOFT. |
| 22 |
|
| 23 |
For use with other plugins, scribble the revised failure rate to |
| 24 |
|
| 25 |
$self->connection->notes('random_fail_%'); |
| 26 |
|
| 27 |
=cut |
| 28 |
|
| 29 |
sub register { |
| 30 |
my ($self, $qp, @args) = @_; |
| 31 |
|
| 32 |
die "Invalid args: '@args'" unless @args < 2; |
| 33 |
($self->{__PACKAGE__ . '_how'}) = $args[0] || 1; |
| 34 |
|
| 35 |
} |
| 36 |
|
| 37 |
sub NEXT() { DECLINED } |
| 38 |
|
| 39 |
sub random_fail { |
| 40 |
my $fpct = $_[0]->connection->notes('random_fail_%'); |
| 41 |
|
| 42 |
=head1 calculating the probability of failure |
| 43 |
|
| 44 |
There are six tests a message must pass to reach the queueing stage, and we wish to |
| 45 |
provide random failure for each one, with the combined probability being out |
| 46 |
configuration argument. So we want to solve this equation: |
| 47 |
|
| 48 |
(1-x) ** 6 = ( 1 - input_number ) |
| 49 |
|
| 50 |
or |
| 51 |
|
| 52 |
x = 1 - ( (1 - input_number ) ** (1/6) ) |
| 53 |
|
| 54 |
=cut |
| 55 |
|
| 56 |
my $successp = 1 - ($fpct / 100); |
| 57 |
$_[0]->log(LOGINFO, |
| 58 |
"to fail, rand(1) must be more than " . ($successp**(1 / 6))); |
| 59 |
rand(1) < ($successp**(1 / 6)) and return NEXT; |
| 60 |
rand(5) < 2 and return (DENYSOFT_DISCONNECT, "random failure"); |
| 61 |
return (DENYSOFT, "random failure"); |
| 62 |
} |
| 63 |
|
| 64 |
sub hook_connect { |
| 65 |
$_[0]->connection->notes('random_fail_%', $_[0]->{__PACKAGE__ . '_how'}); |
| 66 |
goto &random_fail; |
| 67 |
} |
| 68 |
|
| 69 |
sub hook_helo { |
| 70 |
goto &random_fail; |
| 71 |
} |
| 72 |
|
| 73 |
sub hook_ehlo { |
| 74 |
goto &random_fail; |
| 75 |
} |
| 76 |
|
| 77 |
sub hook_mail { |
| 78 |
goto &random_fail; |
| 79 |
} |
| 80 |
|
| 81 |
sub hook_rcpt { |
| 82 |
goto &random_fail; |
| 83 |
} |
| 84 |
|
| 85 |
sub hook_data { |
| 86 |
goto &random_fail; |
| 87 |
} |
| 88 |
|
| 89 |
sub hook_data_post { |
| 90 |
goto &random_fail; |
| 91 |
} |
| 92 |
|