File: count_unrecognized_commands

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 (51 lines) | stat: -rw-r--r-- 1,128 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
#!perl -w

=head1 NAME

count_unrecognized_commands - and disconnect after too many

=head1 DESCRIPTION

Disconnect the client if it sends too many unrecognized commands.
Good for rejecting spam sent through open HTTP proxies.

=head1 CONFIGURATION

Takes one parameter, the number of allowed unrecognized commands
before we disconnect the client.  Defaults to 4.

=cut

use strict;
use warnings;

use Qpsmtpd::Constants;

sub register {
    my ($self, $qp) = (shift, shift);

    $self->{_unrec_cmd_max} = shift || 4;

    if (scalar @_) {
        $self->log(LOGWARN, "Ignoring additional arguments.");
    }
}

sub hook_unrecognized_command {
    my ($self, $cmd) = @_[0, 2];

    my $count = $self->connection->notes('unrec_cmd_count') || 0;
    $count = $count + 1;
    $self->connection->notes('unrec_cmd_count', $count);

    if ($count < $self->{_unrec_cmd_max}) {
        $self->log(LOGINFO, "'$cmd', ($count)");
        return DECLINED;
    }

    $self->log(LOGINFO, "fail, '$cmd' ($count)");
    return (DENY_DISCONNECT,
"Closing connection, $count unrecognized commands. Perhaps you should read RFC 2821?"
    );
}