Debian Codegraph @

lib/Apache/Qpsmtpd.pm

raw
1 package Apache::Qpsmtpd;
2
3 use 5.006001;
4 use strict;
5 use warnings FATAL => 'all';
6
7 use Apache2::ServerUtil ();
8 use Apache2::Connection ();
9 use Apache2::Const -compile => qw(OK MODE_GETLINE);
10 use APR::Const -compile => qw(SO_NONBLOCK EOF SUCCESS);
11 use APR::Error ();
12 use APR::Brigade ();
13 use APR::Bucket ();
14 use APR::Socket ();
15 use Apache2::Filter ();
16 use ModPerl::Util ();
17
18 our $VERSION = '0.02';
19
20 sub handler {
21 my Apache2::Connection $c = shift;
22 $c->client_socket->opt_set(APR::Const::SO_NONBLOCK => 0);
23
24 die "\$ENV{QPSMTPD_CONFIG} must be given" unless $ENV{QPSMTPD_CONFIG};
25
26 my $qpsmtpd = Qpsmtpd::Apache->new();
27 $qpsmtpd->start_connection(
28 ip => $c->remote_ip,
29 host => $c->remote_host,
30 info => undef,
31 conn => $c,
32 );
33
34 $qpsmtpd->run($c);
35 $qpsmtpd->run_hooks("post-connection");
36 $qpsmtpd->connection->reset;
37
38 return Apache2::Const::OK;
39 }
40
41 package Qpsmtpd::Apache;
42
43 use Qpsmtpd::Constants;
44 use base qw(Qpsmtpd::SMTP);
45
46 my %cdir_memo;
47
48 sub config_dir {
49 my ($self, $config) = @_;
50 if (exists $cdir_memo{$config}) {
51 return $cdir_memo{$config};
52 }
53
54 if (uc($ENV{QPSMTPD_CONFIG}) eq 'USE-VIRTUAL-DOMAINS') {
55 my $cdir = $self->{conn}->base_server->dir_config("qpsmtpd.config_dir");
56 $cdir =~ /^(.*)$/; # detaint
57 my $configdir = $1 if -e "$1/$config";
58 $cdir_memo{$config} = $configdir;
59 }
60 else {
61 shift;
62 $cdir_memo{$config} = $self->SUPER::config_dir(@_);
63 }
64 return $cdir_memo{$config};
65 }
66
67 sub start_connection {
68 my $self = shift;
69 my %opts = @_;
70
71 $self->{conn} = $opts{conn};
72 $self->{conn}
73 ->client_socket->timeout_set($self->config('timeout') * 1_000_000);
74 $self->{bb_in} =
75 APR::Brigade->new($self->{conn}->pool, $self->{conn}->bucket_alloc);
76 $self->{bb_out} =
77 APR::Brigade->new($self->{conn}->pool, $self->{conn}->bucket_alloc);
78
79 my $remote_host = $opts{host} || ($opts{ip} ? "[$opts{ip}]" : "[noip!]");
80 my $remote_info = $opts{info} ? "$opts{info}\@$remote_host" : $remote_host;
81 my $remote_ip = $opts{ip};
82
83 $self->log(LOGNOTICE, "Connection from $remote_info [$remote_ip]");
84
85 $self->SUPER::connection->start(
86 remote_info => $remote_info,
87 remote_ip => $remote_ip,
88 remote_host => $remote_host,
89 local_ip => $opts{conn}->local_ip,
90 @_
91 );
92 }
93
94 sub config {
95 my $self = shift;
96 my ($param, $type) = @_;
97 if (!$type) {
98 my $opt = $self->{conn}->base_server->dir_config("qpsmtpd.$param");
99 return $opt if defined($opt);
100 }
101 return $self->SUPER::config(@_);
102 }
103
104 sub run {
105 my $self = shift;
106
107 # should be somewhere in Qpsmtpd.pm and not here...
108 $self->load_plugins;
109
110 my $rc = $self->start_conversation;
111 return if $rc != DONE;
112
113 # this should really be the loop and read_input should just
114 # get one line; I think
115 $self->read_input();
116 }
117
118 sub getline {
119 my $self = shift;
120 my $c = $self->{conn} || die "Cannot getline without a conn";
121
122 return if $c->aborted;
123
124 my $bb = $self->{bb_in};
125
126 while (1) {
127 my $rc =
128 $c->input_filters->get_brigade($bb, Apache2::Const::MODE_GETLINE);
129 return if $rc == APR::Const::EOF;
130 die APR::Error::strerror($rc) unless $rc == APR::Const::SUCCESS;
131
132 next unless $bb->flatten(my $data);
133
134 $bb->cleanup;
135 return $data;
136 }
137
138 return '';
139 }
140
141 sub read_input {
142 my $self = shift;
143 my $c = $self->{conn};
144
145 while (defined(my $data = $self->getline)) {
146 $data =~ s/\r?\n$//s; # advanced chomp
147 $self->connection->notes('original_string', $data);
148 $self->log(LOGDEBUG, "dispatching $data");
149 defined $self->dispatch(split / +/, $data, 2)
150 or $self->respond(502, "command unrecognized: '$data'");
151 last if $self->{_quitting};
152 }
153 }
154
155 sub respond {
156 my ($self, $code, @messages) = @_;
157 my $c = $self->{conn};
158 while (my $msg = shift @messages) {
159 my $bb = $self->{bb_out};
160 my $line = $code . (@messages ? "-" : " ") . $msg;
161 $self->log(LOGDEBUG, $line);
162 my $bucket = APR::Bucket->new(($c->bucket_alloc), "$line\r\n");
163 $bb->insert_tail($bucket);
164 $c->output_filters->fflush($bb);
165
166 # $bucket->remove;
167 $bb->cleanup;
168 }
169 return 1;
170 }
171
172 sub disconnect {
173 my $self = shift;
174 $self->SUPER::disconnect(@_);
175 $self->{_quitting} = 1;
176 $self->{conn}->client_socket->close();
177 }
178
179 1;
180
181 __END__
182
183 =head1 NAME
184
185 Apache::Qpsmtpd - a mod_perl-2 connection handler for qpsmtpd
186
187 =head1 SYNOPSIS
188
189 Listen 0.0.0.0:25 smtp
190 AcceptFilter smtp none
191 ## "smtp" and the AcceptFilter are required for Linux, FreeBSD
192 ## with apache >= 2.1.5, for others it doesn't hurt. See also
193 ## http://httpd.apache.org/docs/2.2/mod/core.html#acceptfilter
194 ## and http://httpd.apache.org/docs/2.2/mod/mpm_common.html#listen
195
196 LoadModule perl_module modules/mod_perl.so
197
198 <Perl>
199 use lib qw( /path/to/qpsmtpd/lib );
200 use Apache::Qpsmtpd;
201 $ENV{QPSMTPD_CONFIG} = "/path/to/qpsmtpd/config";
202 </Perl>
203
204 <VirtualHost _default_:25>
205 PerlModule Apache::Qpsmtpd
206 PerlProcessConnectionHandler Apache::Qpsmtpd
207 # can specify this in config/plugin_dirs if you wish:
208 PerlSetVar qpsmtpd.plugin_dirs /path/to/qpsmtpd/plugins
209 PerlSetVar qpsmtpd.loglevel 4
210 </VirtualHost>
211
212 Using multiple instances of Qpsmtpd on the same server is also
213 possible by setting:
214
215 $ENV{QPSMTPD_CONFIG} = "USE-VIRTUAL-DOMAINS";
216
217 Then in the VirtualHost of each config define the configuration
218 directory:
219
220 PerlSetVar qpsmtpd.config_dir /path/to/qpsmtpd/config
221
222 Several different configurations can be running on the same
223 server.
224
225 =head1 DESCRIPTION
226
227 This module implements a mod_perl/apache 2.0 connection handler
228 that turns Apache into an SMTP server using Qpsmtpd.
229
230 It also allows you to set single-valued config options (such
231 as I<loglevel>, as seen above) using C<PerlSetVar> in F<httpd.conf>.
232
233 This module should be considered beta software as it is not yet
234 widely tested. However it is currently the fastest way to run
235 Qpsmtpd, so if performance is important to you then consider this
236 module.
237
238 =head1 BUGS
239
240 Probably a few. Make sure you test your plugins carefully.
241
242 The Apache scoreboard (/server-status/) mostly works and shows
243 connections, but could do with some enhancements specific to SMTP.
244
245 =head1 AUTHOR
246
247 Matt Sergeant, <[email protected]>
248
249 Some credit goes to <[email protected]> for Apache::SMTP which gave
250 me the inspiration to do this. <[email protected]> added the virtual
251 host support.
252
253 =cut