Debian Codegraph @

plugins/noop_counter

raw
1 #!perl -w
2
3 =head1 NAME
4
5 noop_counter - disconnect after too many consecutive NOOPs, example plugin for the hook_noop()
6
7 =head1 DESCRIPTION
8
9 The B<noop_counter> counts the number of consecutive C<NOOP> commands given
10 by a client and disconnects after a given number.
11
12 Any other command than a C<NOOP> resets the counter.
13
14 One argument may be given: the number of C<NOOP>s after which the client will
15 be disconnected.
16
17 =head1 NOTE
18
19 This plugin should be loaded early to be able to reset the counter on any other
20 command.
21
22 =cut
23
24 sub register {
25 my ($self, $qp, @args) = @_;
26 $self->{_noop_count} = 0;
27 $self->{_max_noop} = 3;
28 if ($args[0] && $args[0] =~ /^\d+$/) {
29 $self->{_max_noop} = shift @args;
30 }
31 }
32
33 sub hook_noop {
34 my ($self, $transaction, @args) = @_;
35 ++$self->{_noop_count};
36 ### the following block is not used, RFC 2821 says we SHOULD ignore
37 ### any arguments... so we MAY return an error if we want to :-)
38 # return (DENY, "Syntax error, NOOP does not take any arguments")
39 # if $args[0];
40
41 if ($self->{_noop_count} >= $self->{_max_noop}) {
42 return (DENY_DISCONNECT,
43 "Stop wasting my time, too many consecutive NOOPs");
44 }
45 return (DECLINED);
46 }
47
48 sub reset_noop_counter {
49 $_[0]->{_noop_count} = 0;
50 return (DECLINED);
51 }
52
53 # and bind the counter reset to the hooks, QUIT not useful here:
54 *hook_helo = *hook_ehlo = # HELO / EHLO
55 *hook_mail = # MAIL FROM:
56 *hook_rcpt = # RCPT TO:
57 *hook_data = # DATA
58 *hook_reset_transaction = # RSET
59 *hook_vrfy = # VRFY
60 *hook_help = # HELP
61 \&reset_noop_counter;
62