| 1 |
# |
| 2 |
# This file is best read with ``perldoc plugins.pod'' |
| 3 |
# |
| 4 |
|
| 5 |
### |
| 6 |
# Conventions: |
| 7 |
# plugin names: F<myplugin>, F<qpsmtpd-async> |
| 8 |
# constants: I<LOGDEBUG> |
| 9 |
# smtp commands, answers: B<HELO>, B<250 Queued!> |
| 10 |
# |
| 11 |
# Notes: |
| 12 |
# * due to restrictions of some POD parsers, no C<<$object->method()>> |
| 13 |
# are allowed, use C<$object-E<gt>method()> |
| 14 |
# |
| 15 |
|
| 16 |
=head1 Introduction |
| 17 |
|
| 18 |
Plugins are the heart of qpsmtpd. The core implements only basic SMTP protocol |
| 19 |
functionality. No useful function can be done by qpsmtpd without loading |
| 20 |
plugins. |
| 21 |
|
| 22 |
Plugins are loaded on startup where each of them register their interest in |
| 23 |
various I<hooks> provided by the qpsmtpd core engine. |
| 24 |
|
| 25 |
At least one plugin B<must> allow or deny the B<RCPT> command to enable |
| 26 |
receiving mail. The F<check_relay> plugin is the standard plugin for this. |
| 27 |
Other plugins provide extra functionality related to this; for example the |
| 28 |
F<resolvable_fromhost> plugin. |
| 29 |
|
| 30 |
=head2 Loading Plugins |
| 31 |
|
| 32 |
The list of plugins to load are configured in the I<config/plugins> |
| 33 |
configuration file. One plugin per line, empty lines and lines starting |
| 34 |
with I<#> are ignored. The order they are loaded is the same as given |
| 35 |
in this config file. This is also the order the registered I<hooks> |
| 36 |
are run. The plugins are loaded from the F<plugins/> directory or |
| 37 |
from a subdirectory of it. If a plugin should be loaded from such a |
| 38 |
subdirectory, the directory must also be given, like the |
| 39 |
F<virus/clamdscan> in the example below. Alternate plugin directories |
| 40 |
may be given in the F<config/plugin_dirs> config file, one directory |
| 41 |
per line, these will be searched first before using the builtin fallback |
| 42 |
of F<plugins/> relative to the qpsmtpd root directory. It may be |
| 43 |
necessary, that the F<config/plugin_dirs> must be used (if you're using |
| 44 |
F<Apache::Qpsmtpd>, for example). |
| 45 |
|
| 46 |
Some plugins may be configured by passing arguments in the F<plugins> |
| 47 |
config file. |
| 48 |
|
| 49 |
A plugin can be loaded two or more times with different arguments by adding |
| 50 |
I<:N> to the plugin filename, with I<N> being a number, usually starting at |
| 51 |
I<0>. |
| 52 |
|
| 53 |
Another method to load a plugin is to create a valid perl module, drop this |
| 54 |
module in perl's C<@INC> path and give the name of this module as |
| 55 |
plugin name. The only restriction to this is, that the module name B<must> |
| 56 |
contain I<::>, e.g. C<My::Plugin> would be ok, C<MyPlugin> not. Appending of |
| 57 |
I<:0>, I<:1>, ... does not work with module plugins. |
| 58 |
|
| 59 |
check_relay |
| 60 |
virus/clamdscan |
| 61 |
spamassassin reject_threshold 7 |
| 62 |
my_rcpt_check example.com |
| 63 |
my_rcpt_check:0 example.org |
| 64 |
My::Plugin |
| 65 |
|
| 66 |
=head1 Anatomy of a plugin |
| 67 |
|
| 68 |
A plugin has at least one method, which inherits from the |
| 69 |
C<Qpsmtpd::Plugin> object. The first argument for this method is always the |
| 70 |
plugin object itself (and usually called C<$self>). The most simple plugin |
| 71 |
has one method with a predefined name which just returns one constant. |
| 72 |
|
| 73 |
# plugin temp_disable_connection |
| 74 |
sub hook_connect { |
| 75 |
return(DENYSOFT, "Sorry, server is temporarily unavailable."); |
| 76 |
} |
| 77 |
|
| 78 |
While this is a valid plugin, it is not very useful except for rare |
| 79 |
circumstances. So let us see what happens when a plugin is loaded. |
| 80 |
|
| 81 |
=head2 Initialisation |
| 82 |
|
| 83 |
After the plugin is loaded the C<init()> method of the plugin is called, |
| 84 |
if present. The arguments passed to C<init()> are |
| 85 |
|
| 86 |
=over 4 |
| 87 |
|
| 88 |
=item $self |
| 89 |
|
| 90 |
the current plugin object, usually called C<$self> |
| 91 |
|
| 92 |
=item $qp |
| 93 |
|
| 94 |
the Qpsmtpd object, usually called C<$qp>. |
| 95 |
|
| 96 |
=item @args |
| 97 |
|
| 98 |
the values following the plugin name in the F<plugins> config, split by |
| 99 |
white space. These arguments can be used to configure the plugin with |
| 100 |
default and/or static config settings, like database paths, |
| 101 |
timeouts, ... |
| 102 |
|
| 103 |
=back |
| 104 |
|
| 105 |
This is mainly used for inheriting from other plugins, but may be used to do |
| 106 |
the same as in C<register()>. |
| 107 |
|
| 108 |
The next step is to register the hooks the plugin provides. Any method which |
| 109 |
is named C<hook_$hookname> is automagically added. |
| 110 |
|
| 111 |
Plugins should be written using standard named hook subroutines. This |
| 112 |
allows them to be overloaded and extended easily. Because some of the |
| 113 |
callback names have characters invalid in subroutine names , they must be |
| 114 |
translated. The current translation routine is C<s/\W/_/g;>, see |
| 115 |
L</Hook - Subroutine translations> for more info. If you choose |
| 116 |
not to use the default naming convention, you need to register the hooks in |
| 117 |
your plugin in the C<register()> method (see below) with the |
| 118 |
C<register_hook()> call on the plugin object. |
| 119 |
|
| 120 |
sub register { |
| 121 |
my ($self, $qp, @args) = @_; |
| 122 |
$self->register_hook("mail", "mail_handler"); |
| 123 |
$self->register_hook("rcpt", "rcpt_handler"); |
| 124 |
} |
| 125 |
sub mail_handler { ... } |
| 126 |
sub rcpt_handler { ... } |
| 127 |
|
| 128 |
The C<register()> method is called last. It receives the same arguments as |
| 129 |
C<init()>. There is no restriction, what you can do in C<register()>, but |
| 130 |
creating database connections and reuse them later in the process may not be |
| 131 |
a good idea. This initialisation happens before any C<fork()> is done. |
| 132 |
Therefore the file handle will be shared by all qpsmtpd processes and the |
| 133 |
database will probably be confused if several different queries arrive on |
| 134 |
the same file handle at the same time (and you may get the wrong answer, if |
| 135 |
any). This is also true for F<qpsmtpd-async> and the pperl flavours, but |
| 136 |
not for F<qpsmtpd> started by (x)inetd or tcpserver. |
| 137 |
|
| 138 |
In short: don't do it if you want to write portable plugins. |
| 139 |
|
| 140 |
=head2 Hook - Subroutine translations |
| 141 |
|
| 142 |
As mentioned above, the hook name needs to be translated to a valid perl |
| 143 |
C<sub> name. This is done like |
| 144 |
|
| 145 |
($sub = $hook) =~ s/\W/_/g; |
| 146 |
$sub = "hook_$sub"; |
| 147 |
|
| 148 |
Some examples follow, for a complete list of available (documented ;-)) |
| 149 |
hooks (method names), use something like |
| 150 |
|
| 151 |
$ perl -lne 'print if s/^=head2\s+(hook_\S+)/$1/' docs/plugins.pod |
| 152 |
|
| 153 |
All valid hooks are defined in F<lib/Qpsmtpd/Plugins.pm>, C<our @hooks>. |
| 154 |
|
| 155 |
=head3 Translation table |
| 156 |
|
| 157 |
hook method |
| 158 |
---------- ------------ |
| 159 |
config hook_config |
| 160 |
queue hook_queue |
| 161 |
data hook_data |
| 162 |
data_post hook_data_post |
| 163 |
quit hook_quit |
| 164 |
rcpt hook_rcpt |
| 165 |
mail hook_mail |
| 166 |
ehlo hook_ehlo |
| 167 |
helo hook_helo |
| 168 |
auth hook_auth |
| 169 |
auth-plain hook_auth_plain |
| 170 |
auth-login hook_auth_login |
| 171 |
auth-cram-md5 hook_auth_cram_md5 |
| 172 |
connect hook_connect |
| 173 |
reset_transaction hook_reset_transaction |
| 174 |
unrecognized_command hook_unrecognized_command |
| 175 |
|
| 176 |
=head2 Inheritance |
| 177 |
|
| 178 |
Inheriting methods from other plugins is an advanced topic. You can alter |
| 179 |
arguments for the underlying plugin, prepare something for the I<real> |
| 180 |
plugin or skip a hook with this. Instead of modifying C<@ISA> |
| 181 |
directly in your plugin, use the C<isa_plugin()> method from the |
| 182 |
C<init()> subroutine. |
| 183 |
|
| 184 |
# rcpt_ok_child |
| 185 |
sub init { |
| 186 |
my ($self, $qp, @args) = @_; |
| 187 |
$self->isa_plugin("rcpt_ok"); |
| 188 |
} |
| 189 |
|
| 190 |
sub hook_rcpt { |
| 191 |
my ($self, $transaction, $recipient) = @_; |
| 192 |
# do something special here... |
| 193 |
$self->SUPER::hook_rcpt($transaction, $recipient); |
| 194 |
} |
| 195 |
|
| 196 |
See also chapter C<Changing return values> and |
| 197 |
F<contrib/vetinari/rcpt_ok_maxrelay> in SVN. |
| 198 |
|
| 199 |
=head2 Config files |
| 200 |
|
| 201 |
Most of the existing plugins fetch their configuration data from files in the |
| 202 |
F<config/> sub directory. This data is read at runtime and may be changed |
| 203 |
without restarting qpsmtpd. |
| 204 |
B<(FIXME: caching?!)> |
| 205 |
The contents of the files can be fetched via |
| 206 |
|
| 207 |
@lines = $self->qp->config("my_config"); |
| 208 |
|
| 209 |
All empty lines and lines starting with C<#> are ignored. |
| 210 |
|
| 211 |
If you don't want to read your data from files, but from a database you can |
| 212 |
still use this syntax and write another plugin hooking the C<config> |
| 213 |
hook. |
| 214 |
|
| 215 |
=head2 Logging |
| 216 |
|
| 217 |
Log messages can be written to the log file (or STDERR if you use the |
| 218 |
F<logging/warn> plugin) with |
| 219 |
|
| 220 |
$self->log($loglevel, $logmessage); |
| 221 |
|
| 222 |
The log level is one of (from low to high priority) |
| 223 |
|
| 224 |
=over 4 |
| 225 |
|
| 226 |
=item * |
| 227 |
|
| 228 |
LOGDEBUG |
| 229 |
|
| 230 |
=item * |
| 231 |
|
| 232 |
LOGINFO |
| 233 |
|
| 234 |
=item * |
| 235 |
|
| 236 |
LOGNOTICE |
| 237 |
|
| 238 |
=item * |
| 239 |
|
| 240 |
LOGWARN |
| 241 |
|
| 242 |
=item * |
| 243 |
|
| 244 |
LOGERROR |
| 245 |
|
| 246 |
=item * |
| 247 |
|
| 248 |
LOGCRIT |
| 249 |
|
| 250 |
=item * |
| 251 |
|
| 252 |
LOGALERT |
| 253 |
|
| 254 |
=item * |
| 255 |
|
| 256 |
LOGEMERG |
| 257 |
|
| 258 |
=back |
| 259 |
|
| 260 |
While debugging your plugins, set your plugins loglevel to LOGDEBUG. This |
| 261 |
will log every logging statement within your plugin. |
| 262 |
|
| 263 |
For more information about logging, see F<docs/logging.pod>. |
| 264 |
|
| 265 |
=head2 Information about the current plugin |
| 266 |
|
| 267 |
Each plugin inherits the public methods from C<Qpsmtpd::Plugin>. |
| 268 |
|
| 269 |
=over 4 |
| 270 |
|
| 271 |
=item plugin_name() |
| 272 |
|
| 273 |
Returns the name of the currently running plugin |
| 274 |
|
| 275 |
=item hook_name() |
| 276 |
|
| 277 |
Returns the name of the running hook |
| 278 |
|
| 279 |
=item auth_user() |
| 280 |
|
| 281 |
Returns the name of the user the client is authed as (if authentication is |
| 282 |
used, of course) |
| 283 |
|
| 284 |
=item auth_mechanism() |
| 285 |
|
| 286 |
Returns the auth mechanism if authentication is used |
| 287 |
|
| 288 |
=item connection() |
| 289 |
|
| 290 |
Returns the C<Qpsmtpd::Connection> object associated with the current |
| 291 |
connection |
| 292 |
|
| 293 |
=item transaction() |
| 294 |
|
| 295 |
Returns the C<Qpsmtpd::Transaction> object associated with the current |
| 296 |
transaction |
| 297 |
|
| 298 |
=back |
| 299 |
|
| 300 |
=head2 Temporary Files |
| 301 |
|
| 302 |
The temporary file and directory functions can be used for plugin specific |
| 303 |
workfiles and will automatically be deleted at the end of the current |
| 304 |
transaction. |
| 305 |
|
| 306 |
=over 4 |
| 307 |
|
| 308 |
=item temp_file() |
| 309 |
|
| 310 |
Returns a unique name of a file located in the default spool directory, |
| 311 |
but does not open that file (i.e. it is the name not a file handle). |
| 312 |
|
| 313 |
=item temp_dir() |
| 314 |
|
| 315 |
Returns the name of a unique directory located in the default spool |
| 316 |
directory, after creating the directory with 0700 rights. If you need a |
| 317 |
directory with different rights (say for an antivirus daemon), you will |
| 318 |
need to use the base function C<$self-E<gt>qp-E<gt>temp_dir()>, which takes a |
| 319 |
single parameter for the permissions requested (see L<mkdir> for details). |
| 320 |
A directory created like this will not be deleted when the transaction |
| 321 |
is ended. |
| 322 |
|
| 323 |
=item spool_dir() |
| 324 |
|
| 325 |
Returns the configured system-wide spool directory. |
| 326 |
|
| 327 |
=back |
| 328 |
|
| 329 |
|
| 330 |
=head2 Connection and Transaction Notes |
| 331 |
|
| 332 |
Both may be used to share notes across plugins and/or hooks. The only real |
| 333 |
difference is their life time. The connection notes start when a new |
| 334 |
connection is made and end, when the connection ends. This can, for example, |
| 335 |
be used to count the number of none SMTP commands. The plugin which uses |
| 336 |
this is the F<count_unrecognized_commands> plugin from the qpsmtpd core |
| 337 |
distribution. |
| 338 |
|
| 339 |
The transaction note starts after the B<MAIL FROM: > command and are just |
| 340 |
valid for the current transaction, see below in the I<reset_transaction> |
| 341 |
hook when the transaction ends. |
| 342 |
|
| 343 |
|
| 344 |
=head1 Return codes |
| 345 |
|
| 346 |
Each plugin must return an allowed constant for the hook and (usually) |
| 347 |
optionally a ``message'' for the client. |
| 348 |
Generally all plugins for a hook are processed until one returns |
| 349 |
something other than I<DECLINED>. |
| 350 |
|
| 351 |
Plugins are run in the order they are listed in the F<plugins> |
| 352 |
configuration file. |
| 353 |
|
| 354 |
The return constants are defined in C<Qpsmtpd::Constants> and have |
| 355 |
the following meanings: |
| 356 |
|
| 357 |
=over 4 |
| 358 |
|
| 359 |
=item DECLINED |
| 360 |
|
| 361 |
Plugin declined work; proceed as usual. This return code is I<always allowed> |
| 362 |
unless noted otherwise. |
| 363 |
|
| 364 |
=item OK |
| 365 |
|
| 366 |
Action allowed. |
| 367 |
|
| 368 |
=item DENY |
| 369 |
|
| 370 |
Action denied. |
| 371 |
|
| 372 |
=item DENYSOFT |
| 373 |
|
| 374 |
Action denied; return a temporary rejection code (say B<450> instead |
| 375 |
of B<550>). |
| 376 |
|
| 377 |
=item DENY_DISCONNECT |
| 378 |
|
| 379 |
Action denied; return a permanent rejection code and disconnect the client. |
| 380 |
Use this for "rude" clients. Note that you're not supposed to do this |
| 381 |
according to the SMTP specs, but bad clients don't listen sometimes. |
| 382 |
|
| 383 |
=item DENYSOFT_DISCONNECT |
| 384 |
|
| 385 |
Action denied; return a temporary rejection code and disconnect the client. |
| 386 |
See note above about SMTP specs. |
| 387 |
|
| 388 |
=item DONE |
| 389 |
|
| 390 |
Finishing processing of the request. Usually used when the plugin sent the |
| 391 |
response to the client. |
| 392 |
|
| 393 |
=item YIELD |
| 394 |
|
| 395 |
Only used in F<qpsmtpd-async>, see F<plugins/async/*> |
| 396 |
|
| 397 |
=back |
| 398 |
|
| 399 |
=cut |
| 400 |
|
| 401 |
# vim: ts=2 sw=2 expandtab |