File: milter

package info (click to toggle)
qpsmtpd 0.40-3
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 1,024 kB
  • ctags: 393
  • sloc: perl: 6,462; sh: 383; makefile: 54
file content (235 lines) | stat: -rw-r--r-- 6,961 bytes parent folder | download | duplicates (5)
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
=head1 NAME

milter

=head1 DESCRIPTION

This plugin allows you to attach to milter filters (yes, those written for
sendmail) as though they were qpsmtpd plugins.

In order to do this you need the C<Net::Milter> module from CPAN.

=head1 CONFIG

It takes two required parameters - a milter name (for logging) and the port
to connect to on the localhost. This can also contain a hostname if
the filter is on another machine:

  milter Brightmail 5513

or

  milter Brightmail bmcluster:5513

This plugin has so far only been tested with Brightmail's milter module.

=cut

use Net::Milter;
no warnings;

sub register {
  my ($self, $qp, @args) = @_;

  die "Invalid milter setup args: '@args'" unless @args > 1;
  my ($name, $port) = @args;
  my $host = '127.0.0.1';
  if ($port =~ s/^(.*)://) {
    $host = $1;
  }
  
  $self->{name} = $name;
  $self->{host} = $host;
  $self->{port} = $port;
  
}

sub hook_disconnect {
    my ($self) = @_;

    my $milter = $self->qp->connection->notes('milter') || return DECLINED;
    $milter->send_quit();
    
    $self->qp->connection->notes('spam', undef);
    $self->qp->connection->notes('milter', undef);

    return DECLINED;
}

sub check_results {
    my ($self, $transaction, $where, @results) = @_;
    foreach my $result (@results) {
        next if $result->{action} eq 'continue';
        $self->log(LOGINFO, "milter $self->{name} result action: $result->{action}");
        if ($result->{action} eq 'reject') {
            die("Rejected at $where by $self->{name} milter ($result->{explanation})");
        }
        elsif ($result->{action} eq 'add') {
            if ($result->{header} eq 'body') {
                $transaction->body_write($result->{value});
            }
            else {
                push @{$transaction->notes('milter_header_changes')->{add}},
                        [$result->{header}, $result->{value}];
            }
        }
        elsif ($result->{action} eq 'delete') {
            push @{$transaction->notes('milter_header_changes')->{delete}},
                $result->{header};
        }
        elsif ($result->{action} eq 'accept') {
            # TODO - figure out what this is used for
        }
        elsif ($result->{action} eq 'replace') {
            push @{$transaction->notes('milter_header_changes')->{replace}},
                    [$result->{header}, $result->{value}];
        }
    }
}

sub hook_connect {
    my ($self, $transaction) = @_;
    
    $self->log(LOGDEBUG, "milter $self->{name} opening connection to milter backend");
    my $milter = Net::Milter->new();
    $milter->open($self->{host}, $self->{port}, 'tcp');
    $milter->protocol_negotiation();

    $self->qp->connection->notes(milter => $milter);

    $self->qp->connection->notes(
        milter_header_changes => { add => [], delete => [], replace => [], }
    );    
    my $remote_ip = $self->qp->connection->remote_ip;
    my $remote_host = $self->qp->connection->remote_host;
    $self->log(LOGDEBUG, "milter $self->{name} checking connect from $remote_host\[$remote_ip\]");
    
    eval {
        $self->check_results($transaction, "connection",
            $milter->send_connect($remote_host, 'tcp4', 0, $remote_ip));
    };
    $self->qp->connection->notes('spam', $@) if $@;

    return DECLINED;
}

sub hook_helo {
    my ($self, $transaction) = @_;

    if (my $txt = $self->qp->connection->notes('spam')) {
        return DENY, $txt;
    }
    
    my $milter = $self->qp->connection->notes('milter');
    
    my $helo = $self->qp->connection->hello;
    my $host = $self->qp->connection->hello_host;

    $self->log(LOGDEBUG, "milter $self->{name} checking HELO $host");
    
    eval { $self->check_results($transaction, "HELO",
                                $milter->send_helo($host)) };
    return(DENY, $@) if $@;
    
    return DECLINED;
}

sub hook_mail {
    my ($self, $transaction, $address, %param) = @_;
    
    my $milter = $self->qp->connection->notes('milter');

    $self->log(LOGDEBUG, "milter $self->{name} checking MAIL FROM " . $address->format);
    eval { $self->check_results($transaction, "MAIL FROM",
                                $milter->send_mail_from($address->format)) };
    return(DENY, $@) if $@;

    return DECLINED;
}

sub hook_rcpt {
    my ($self, $transaction, $address, %param) = @_;
    
    my $milter = $self->qp->connection->notes('milter');

    $self->log(LOGDEBUG, "milter $self->{name} checking RCPT TO " . $address->format);

    eval { $self->check_results($transaction, "RCPT TO",
                                $milter->send_rcpt_to($address->format)) };
    return(DENY, $@) if $@;

    return DECLINED;
}

sub hook_data_post {
    my ($self, $transaction) = @_;

    my $milter = $self->qp->connection->notes('milter');

    $self->log(LOGDEBUG, "milter $self->{name} checking headers");

    my $headers = $transaction->header(); # Mail::Header object
    foreach my $h ($headers->tags) {
        # munge these headers because milters prefer them this way
        $h =~ s/\b(\w)/\U$1/g;
        $h =~ s/\bid\b/ID/g;
        foreach my $val ($headers->get($h)) {
            # $self->log(LOGDEBUG, "milter $self->{name} checking header: $h: $val");
            eval { $self->check_results($transaction, "header $h",
                                $milter->send_header($h, $val)) };
            return(DENY, $@) if $@;
        }
    }
    
    eval { $self->check_results($transaction, "end headers",
                                $milter->send_end_headers()) };
    return(DENY, $@) if $@;
     
    $transaction->body_resetpos;
    
    # skip past headers
    while (my $line = $transaction->body_getline) {
        $line =~ s/\r?\n//;
        $line =~ s/\s*$//;
        last unless length($line);
    }

    $self->log(LOGDEBUG, "milter $self->{name} checking body");

    my $data = '';
    while (my $line = $transaction->body_getline) {
        $data .= $line;
        if (length($data) > 60000) {
            eval { $self->check_results($transaction, "body",
                                $milter->send_body($data)) };
            return(DENY, $@) if $@;
            $data = '';
        }
    }
    
    if (length($data)) {
        eval { $self->check_results($transaction, "body",
                                $milter->send_body($data)) };
        return(DENY, $@) if $@;
        $data = '';
    }
    
    eval { $self->check_results($transaction, "end of DATA",
                                $milter->send_end_body()) };
    return(DENY, $@) if $@;

    my $milter_header_changes = $transaction->notes('milter_header_changes'); 

    foreach my $add (@{$milter_header_changes->{add}}) {
        $headers->add($add->[0], $add->[1]);
    }
    foreach my $del (@{$milter_header_changes->{'delete'}}) {
        $headers->delete($del);
    }
    foreach my $repl (@{$milter_header_changes->{replace}}) {
        $headers->replace($repl->[0], $repl->[1]);
    }
    
    return DECLINED;
}