File: noop_counter

package info (click to toggle)
qpsmtpd 0.94-8
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 2,340 kB
  • sloc: perl: 17,176; sh: 543; makefile: 186; sql: 100
file content (62 lines) | stat: -rw-r--r-- 1,690 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!perl -w

=head1 NAME

noop_counter - disconnect after too many consecutive NOOPs, example plugin for the hook_noop()

=head1 DESCRIPTION

The B<noop_counter> counts the number of consecutive C<NOOP> commands given
by a client and disconnects after a given number. 

Any other command than a C<NOOP> resets the counter.

One argument may be given: the number of C<NOOP>s after which the client will
be disconnected.

=head1 NOTE

This plugin should be loaded early to be able to reset the counter on any other
command.

=cut

sub register {
    my ($self, $qp, @args) = @_;
    $self->{_noop_count} = 0;
    $self->{_max_noop}   = 3;
    if ($args[0] && $args[0] =~ /^\d+$/) {
        $self->{_max_noop} = shift @args;
    }
}

sub hook_noop {
    my ($self, $transaction, @args) = @_;
    ++$self->{_noop_count};
    ### the following block is not used, RFC 2821 says we SHOULD ignore
    ### any arguments... so we MAY return an error if we want to :-)
    # return (DENY, "Syntax error, NOOP does not take any arguments")
    #   if $args[0];

    if ($self->{_noop_count} >= $self->{_max_noop}) {
        return (DENY_DISCONNECT,
                "Stop wasting my time, too many consecutive NOOPs");
    }
    return (DECLINED);
}

sub reset_noop_counter {
    $_[0]->{_noop_count} = 0;
    return (DECLINED);
}

# and bind the counter reset to the hooks, QUIT not useful here:
*hook_helo = *hook_ehlo =      # HELO / EHLO
  *hook_mail =                 # MAIL FROM:
  *hook_rcpt =                 # RCPT TO:
  *hook_data =                 # DATA
  *hook_reset_transaction =    # RSET
  *hook_vrfy =                 # VRFY
  *hook_help =                 # HELP
  \&reset_noop_counter;