Debian Codegraph @

docs/hooks.pod

raw
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 SMTP hooks
17
18 This section covers the hooks, which are run in a normal SMTP connection.
19 The order of these hooks is like you will (probably) see them, while a mail
20 is received.
21
22 Every hook receives a C<Qpsmtpd::Plugin> object of the currently
23 running plugin as the first argument. A C<Qpsmtpd::Transaction> object is
24 the second argument of the current transaction in the most hooks, exceptions
25 are noted in the description of the hook. If you need examples how the
26 hook can be used, see the source of the plugins, which are given as
27 example plugins.
28
29 B<NOTE>: for some hooks (post-fork, post-connection, disconnect, deny, ok) the
30 return values are ignored. This does B<not> mean you can return anything you
31 want. It just means the return value is discarded and you can not disconnect
32 a client with I<DENY_DISCONNECT>. The rule to return I<DECLINED> to run the
33 next plugin for this hook (or return I<OK> / I<DONE> to stop processing)
34 still applies.
35
36 =head2 hook_pre_connection
37
38 Called by a controlling process (e.g. forkserver or prefork) after accepting
39 the remote server, but before beginning a new instance (or handing the
40 connection to the worker process).
41
42 Useful for load-management and rereading large config files at some
43 frequency less than once per session.
44
45 This hook is available in the F<qpsmtpd-forkserver>, F<qpsmtpd-prefork> and
46 F<qpsmtpd-async> flavours.
47
48 =cut
49
50 NOT FOR: apache, -server and inetd/pperl
51
52 =pod
53
54 B<NOTE:> You should not use this hook to do major work and / or use lookup
55 methods which (I<may>) take some time, like DNS lookups. This will slow down
56 B<all> incoming connections, no other connection will be accepted while this
57 hook is running!
58
59 Arguments this hook receives are (B<NOTE>: currently no C<%args> for
60 F<qpsmtpd-async>):
61
62 my ($self,$transaction,%args) = @_;
63 # %args is:
64 # %args = ( remote_ip => inet_ntoa($iaddr),
65 # remote_port => $port,
66 # local_ip => inet_ntoa($laddr),
67 # local_port => $lport,
68 # max_conn_ip => $MAXCONNIP,
69 # child_addrs => [values %childstatus],
70 # );
71
72 B<NOTE:> the C<$transaction> is of course C<undef> at this time.
73
74 Allowed return codes are
75
76 =over 4
77
78 =item DENY / DENY_DISCONNECT
79
80 returns a B<550> to the client and ends the connection
81
82 =item DENYSOFT / DENYSOFT_DISCONNECT
83
84 returns a B<451> to the client and ends the connection
85
86 =back
87
88 Anything else is ignored.
89
90 Example plugins are F<hosts_allow> and F<connection_time>.
91
92 =head2 hook_connect
93
94 It is called at the start of a connection before the greeting is sent to
95 the connecting client.
96
97 Arguments for this hook are
98
99 my $self = shift;
100
101 B<NOTE:> in fact you get passed two more arguments, which are C<undef> at this
102 early stage of the connection, so ignore them.
103
104 Allowed return codes are
105
106 =over 4
107
108 =item OK
109
110 Stop processing plugins, give the default response
111
112 =item DECLINED
113
114 Process the next plugin
115
116 =item DONE
117
118 Stop processing plugins and dont give the default response, i.e. the plugin
119 gave the response
120
121 =item DENY
122
123 Return hard failure code and disconnect
124
125 =item DENYSOFT
126
127 Return soft failure code and disconnect
128
129 =back
130
131 Example plugin for this hook is the F<check_relay> plugin.
132
133 =head2 hook_helo / hook_ehlo
134
135 It is called after the client sent B<EHLO> (hook_ehlo) or B<HELO> (hook_helo)
136 Allowed return codes are
137
138 =over 4
139
140 =item DENY
141
142 Return a 550 code
143
144 =item DENYSOFT
145
146 Return a B<450> code
147
148 =item DENY_DISCONNECT / DENYSOFT_DISCONNECT
149
150 as above but with disconnect
151
152 =item DONE
153
154 Qpsmtpd wont do anything, the plugin sent the message
155
156 =item DECLINED
157
158 Qpsmtpd will send the standard B<EHLO>/B<HELO> answer, of course only
159 if all plugins hooking I<helo/ehlo> return I<DECLINED>.
160
161 =back
162
163 Arguments of this hook are
164
165 my ($self, $transaction, $host) = @_;
166 # $host: the name the client sent in the
167 # (EH|HE)LO line
168
169 B<NOTE:> C<$transaction> is C<undef> at this point.
170
171 =head2 hook_mail_pre
172
173 After the B<MAIL FROM: > line sent by the client is broken into
174 pieces by the C<hook_mail_parse()>, this hook recieves the results.
175 This hook may be used to pre-accept adresses without the surrounding
176 I<E<lt>E<gt>> (by adding them) or addresses like
177 I<E<lt>[email protected]<gt>> or I<E<lt>[email protected] E<gt>> by
178 removing the trailing I<"."> / C<" ">.
179
180 Expected return values are I<OK> and an address which must be parseable
181 by C<Qpsmtpd::Address-E<gt>parse()> on success or any other constant to
182 indicate failure.
183
184 Arguments are
185
186 my ($self, $transaction, $addr) = @_;
187
188 =head2 hook_mail
189
190 Called right after the envelope sender line is parsed (the B<MAIL FROM: >
191 command). The plugin gets passed a C<Qpsmtpd::Address> object, which means
192 the parsing and verifying the syntax of the address (and just the syntax,
193 no other checks) is already done. Default is to allow the sender address.
194 The remaining arguments are the extensions defined in RFC 1869 (if sent by
195 the client).
196
197 B<NOTE:> According to the SMTP protocol, you can not reject an invalid
198 sender until after the B<RCPT> stage (except for protocol errors, i.e.
199 syntax errors in address). So store it in an C<$transaction-E<gt>note()> and
200 process it later in an rcpt hook.
201
202 Allowed return codes are
203
204 =over 4
205
206 =item OK
207
208 sender allowed
209
210 =item DENY
211
212 Return a hard failure code
213
214 =item DENYSOFT
215
216 Return a soft failure code
217
218 =item DENY_DISCONNECT / DENYSOFT_DISCONNECT
219
220 as above but with disconnect
221
222 =item DECLINED
223
224 next plugin (if any)
225
226 =item DONE
227
228 skip further processing, plugin sent response
229
230 =back
231
232 Arguments for this hook are
233
234 my ($self,$transaction, $sender, %args) = @_;
235 # $sender: an Qpsmtpd::Address object for
236 # sender of the message
237
238 Example plugins for the C<hook_mail> are F<resolvable_fromhost>
239 and F<badmailfrom>.
240
241 =head2 hook_rcpt_pre
242
243 See C<hook_mail_pre>, s/MAIL FROM:/RCPT TO:/.
244
245 =head2 hook_rcpt
246
247 This hook is called after the client sent an I<RCPT TO: > command (after
248 parsing the line). The given argument is parsed by C<Qpsmtpd::Address>,
249 then this hook is called. Default is to deny the mail with a soft error
250 code. The remaining arguments are the extensions defined in RFC 1869
251 (if sent by the client).
252
253 Allowed return codes
254
255 =over 4
256
257 =item OK
258
259 recipient allowed
260
261 =item DENY
262
263 Return a hard failure code, for example for an I<User does not exist here>
264 message.
265
266 =item DENYSOFT
267
268 Return a soft failure code, for example if the connect to a user lookup
269 database failed
270
271 =item DENY_DISCONNECT / DENYSOFT_DISCONNECT
272
273 as above but with disconnect
274
275 =item DONE
276
277 skip further processing, plugin sent response
278
279 =back
280
281 Arguments are
282
283 my ($self, $transaction, $recipient, %args) = @_;
284 # $rcpt = Qpsmtpd::Address object with
285 # the given recipient address
286
287 Example plugin is F<rcpt_ok>.
288
289 =head2 hook_data
290
291 After the client sent the B<DATA> command, before any data of the message
292 was sent, this hook is called.
293
294 B<NOTE:> This hook, like B<EHLO>, B<VRFY>, B<QUIT>, B<NOOP>, is an
295 endpoint of a pipelined command group (see RFC 1854) and may be used to
296 detect ``early talkers''. Since svn revision 758 the F<earlytalker>
297 plugin may be configured to check at this hook for ``early talkers''.
298
299 Allowed return codes are
300
301 =over 4
302
303 =item DENY
304
305 Return a hard failure code
306
307 =item DENYSOFT
308
309 Return a soft failure code
310
311 =item DENY_DISCONNECT / DENYSOFT_DISCONNECT
312
313 as above but with disconnect
314
315 =item DONE
316
317 Plugin took care of receiving data and calling the queue (not recommended)
318
319 B<NOTE:> The only real use for I<DONE> is implementing other ways of
320 receiving the message, than the default... for example the CHUNKING SMTP
321 extension (RFC 1869, 1830/3030) ... a plugin for this exists at
322 http://svn.perl.org/qpsmtpd/contrib/vetinari/experimental/chunking, but it
323 was never tested ``in the wild''.
324
325 =back
326
327 Arguments:
328
329 my ($self, $transaction) = @_;
330
331 Example plugin is F<greylisting>.
332
333 =head2 hook_received_line
334
335 If you wish to provide your own Received header line, do it here. You can use
336 or discard any of the given arguments (see below).
337
338 Allowed return codes:
339
340 =over 4
341
342 =item OK, $string
343
344 use this string for the Received header.
345
346 =item anything else
347
348 use the default Received header
349
350 =back
351
352 Arguments are
353
354 my ($self, $transaction, $smtp, $auth, $sslinfo) = @_;
355 # $smtp - the SMTP type used (e.g. "SMTP" or "ESMTP").
356 # $auth - the Auth header additionals.
357 # $sslinfo - information about SSL for the header.
358
359 =head2 data_headers_end
360
361 This hook fires after all header lines of the message data has been received.
362 Defaults to doing nothing, just continue processing. At this step,
363 the sender is not waiting for a reply, but we can try and prevent him from
364 sending the entire message by disconnecting immediately. (Although it is
365 likely the packets are already in flight due to buffering and pipelining).
366
367 B<NOTE:> BE CAREFUL! If you drop the connection legal MTAs will retry again
368 and again, spammers will probably not. This is not RFC compliant and can lead
369 to an unpredictable mess. Use with caution.
370
371 B<NOTE:> This hook does not currently work in async mode.
372
373 Why this hook may be useful for you, see
374 L<http://www.nntp.perl.org/group/perl.qpsmtpd/2009/02/msg8502.html>, ff.
375
376 Allowed return codes:
377
378 =over 4
379
380 =item DENY_DISCONNECT
381
382 Return B<554 Message denied> and disconnect
383
384 =item DENYSOFT_DISCONNECT
385
386 Return B<421 Message denied temporarily> and disconnect
387
388 =item DECLINED
389
390 Do nothing
391
392 =back
393
394 Arguments:
395
396 my ($self, $transaction) = @_;
397
398 B<FIXME:> check arguments
399
400 =head2 hook_data_post
401
402 The C<data_post> hook is called after the client sent the final C<.\r\n>
403 of a message, before the mail is sent to the queue.
404
405 Allowed return codes are
406
407 =over 4
408
409 =item DENY
410
411 Return a hard failure code
412
413 =item DENYSOFT
414
415 Return a soft failure code
416
417 =item DENY_DISCONNECT / DENYSOFT_DISCONNECT
418
419 as above but with disconnect
420
421 =item DONE
422
423 skip further processing (message will not be queued), plugin gave the response.
424
425 B<NOTE:> just returning I<OK> from a special queue plugin does (nearly)
426 the same (i.e. dropping the mail to F</dev/null>) and you don't have to
427 send the response on your own.
428
429 If you want the mail to be queued, you have to queue it manually!
430
431 =back
432
433 Arguments:
434
435 my ($self, $transaction) = @_;
436
437 Example plugins: F<spamassassin>, F<virus/clamdscan>
438
439 =head2 hook_queue_pre
440
441 This hook is run, just before the mail is queued to the ``backend''. You
442 may modify the in-process transaction object (e.g. adding headers) or add
443 something like a footer to the mail (the latter is not recommended).
444
445 Allowed return codes are
446
447 =over 4
448
449 =item DONE
450
451 no queuing is done
452
453 =item OK / DECLINED
454
455 queue the mail
456
457 =back
458
459 =head2 hook_queue
460
461 When all C<data_post> hooks accepted the message, this hook is called. It
462 is used to queue the message to the ``backend''.
463
464 Allowed return codes:
465
466 =over 4
467
468 =item DONE
469
470 skip further processing (plugin gave response code)
471
472 =item OK
473
474 Return success message, i.e. tell the client the message was queued (this
475 may be used to drop the message silently).
476
477 =item DENY
478
479 Return hard failure code
480
481 =item DENYSOFT
482
483 Return soft failure code, i.e. if disk full or other temporary queuing
484 problems
485
486 =back
487
488 Arguments:
489
490 my ($self, $transaction) = @_;
491
492 Example plugins: all F<queue/*> plugins
493
494 =head2 hook_queue_post
495
496 This hook is called always after C<hook_queue>. If the return code is
497 B<not> I<OK>, a message (all remaining return values) with level I<LOGERROR>
498 is written to the log.
499 Arguments are
500
501 my $self = shift;
502
503 B<NOTE:> C<$transaction> is not valid at this point, therefore not mentioned.
504
505
506 =head2 hook_reset_transaction
507
508 This hook will be called several times. At the beginning of a transaction
509 (i.e. when the client sends a B<MAIL FROM:> command the first time),
510 after queueing the mail and every time a client sends a B<RSET> command.
511 Arguments are
512
513 my ($self, $transaction) = @_;
514
515 B<NOTE:> don't rely on C<$transaction> being valid at this point.
516
517 =head2 hook_quit
518
519 After the client sent a B<QUIT> command, this hook is called (before the
520 C<hook_disconnect>).
521
522 Allowed return codes
523
524 =over 4
525
526 =item DONE
527
528 plugin sent response
529
530 =item DECLINED
531
532 next plugin and / or qpsmtpd sends response
533
534 =back
535
536 Arguments: the only argument is C<$self>
537
538 =cut
539
540 ### XXX: FIXME pass the rest of the line to this hook?
541
542 =pod
543
544 Expample plugin is the F<quit_fortune> plugin.
545
546 =head2 hook_disconnect
547
548 This hook will be called from several places: After a plugin returned
549 I<DENY(|SOFT)_DISCONNECT>, before connection is disconnected or after the
550 client sent the B<QUIT> command, AFTER the quit hook and ONLY if no plugin
551 hooking C<hook_quit> returned I<DONE>.
552
553 All return values are ignored, arguments are just C<$self>
554
555 Example plugin is F<logging/file>
556
557 =head2 hook_post_connection
558
559 This is the counter part of the C<pre-connection> hook, it is called
560 directly before the connection is finished, for example, just before the
561 qpsmtpd-forkserver instance exits or if the client drops the connection
562 without notice (without a B<QUIT>). This hook is not called if the qpsmtpd
563 instance is killed.
564
565 =cut
566
567 FIXME: we should run this hook on a ``SIGHUP'' or some other signal?
568
569 =pod
570
571 The only argument is C<$self> and all return codes are ignored, it would
572 be too late anyway :-).
573
574 Example: F<connection_time>
575
576 =head1 Parsing Hooks
577
578 Before the line from the client is parsed by
579 C<Qpsmtpd::Command-E<gt>parse()> with the built in parser, these hooks
580 are called. They can be used to supply a parsing function for the line,
581 which will be used instead of the built in parser.
582
583 The hook must return two arguments, the first is (currently) ignored,
584 the second argument must be a (CODE) reference to a sub routine. This sub
585 routine receives three arguments:
586
587 =over 4
588
589 =item $self
590
591 the plugin object
592
593 =item $cmd
594
595 the command (i.e. the first word of the line) sent by the client
596
597 =item $line
598
599 the line sent by the client without the first word
600
601 =back
602
603 Expected return values from this sub are I<DENY> and a reason which is
604 sent to the client or I<OK> and the C<$line> broken into pieces according
605 to the syntax rules for the command.
606
607 B<NOTE: ignore the example from C<Qpsmtpd::Command>, the C<unrecognized_command_parse> hook was never implemented,...>
608
609 =head2 hook_helo_parse / hook_ehlo_parse
610
611 The provided sub routine must return two or more values. The first is
612 discarded, the second is the hostname (sent by the client as argument
613 to the B<HELO> / B<EHLO> command). All other values are passed to the
614 helo / ehlo hook. This hook may be used to change the hostname the client
615 sent... not recommended, but if your local policy says only to accept
616 I<HELO> hosts with FQDNs and you have a legal client which can not be
617 changed to send his FQDN, this is the right place.
618
619 =head2 hook_mail_parse / hook_rcpt_parse
620
621 The provided sub routine must return two or more values. The first is
622 either I<OK> to indicate that parsing of the line was successfull
623 or anything else to bail out with I<501 Syntax error in command>. In
624 case of failure the second argument is used as the error message for the
625 client.
626
627 If parsing was successfull, the second argument is the sender's /
628 recipient's address (this may be without the surrounding I<E<lt>> and
629 I<E<gt>>, don't add them here, use the C<hook_mail_pre()> /
630 C<hook_rcpt_pre()> methods for this). All other arguments are
631 sent to the C<mail / rcpt> hook as B<MAIL> / B<RCPT> parameters (see
632 RFC 1869 I<SMTP Service Extensions> for more info). Note that
633 the mail and rcpt hooks expect a list of key/value pairs as the
634 last arguments.
635
636 =head2 hook_auth_parse
637
638 B<FIXME...>
639
640 =head1 Special hooks
641
642 Now some special hooks follow. Some of these hooks are some internal hooks,
643 which may be used to alter the logging or retrieving config values from
644 other sources (other than flat files) like SQL databases.
645
646 =head2 hook_logging
647
648 This hook is called when a log message is written, for example in a plugin
649 it fires if someone calls C<$self-E<gt>log($level, $msg);>. Allowed
650 return codes are
651
652 =over 4
653
654 =item DECLINED
655
656 next logging plugin
657
658 =item OK
659
660 (not I<DONE>, as some might expect!) ok, plugin logged the message
661
662 =back
663
664 Arguments are
665
666 my ($self, $transaction, $trace, $hook, $plugin, @log) = @_;
667 # $trace: level of message, for example
668 # LOGWARN, LOGDEBUG, ...
669 # $hook: the hook in/for which this logging
670 # was called
671 # $plugin: the plugin calling this hook
672 # @log: the log message
673
674 B<NOTE:> C<$transaction> may be C<undef>, depending when / where this hook
675 is called. It's probably best not to try acessing it.
676
677 All F<logging/*> plugins can be used as example plugins.
678
679 =head2 hook_deny
680
681 This hook is called after a plugin returned I<DENY>, I<DENYSOFT>,
682 I<DENY_DISCONNECT> or I<DENYSOFT_DISCONNECT>. All return codes are ignored,
683 arguments are
684
685 my ($self, $transaction, $prev_plugin, $return, $return_text) = @_;
686
687 B<NOTE:> C<$transaction> may be C<undef>, depending when / where this hook
688 is called. It's probably best not to try acessing it.
689
690 Example plugin for this hook is F<logging/adaptive>.
691
692 =head2 hook_ok
693
694 The counter part of C<hook_deny>, it is called after a plugin B<did not>
695 return I<DENY>, I<DENYSOFT>, I<DENY_DISCONNECT> or I<DENYSOFT_DISCONNECT>.
696 All return codes are ignored, arguments are
697
698 my ( $self, $transaction, $prev_plugin, $return, $return_text ) = @_;
699
700 B<NOTE:> C<$transaction> may be C<undef>, depending when / where this hook
701 is called. It's probably best not to try acessing it.
702
703 =head2 hook_config
704
705 Called when a config file is requested, for example in a plugin it fires
706 if someone calls C<my @cfg = $self-E<gt>qp-E<gt>config($cfg_name);>.
707 Allowed return codes are
708
709 =over 4
710
711 =item DECLINED
712
713 plugin didn't find the requested value
714
715 =item OK
716
717 requested values as C<@list>, example:
718
719 return (OK, @{$config{$value}})
720 if exists $config{$value};
721 return (DECLINED);
722
723 =back
724
725 Arguments:
726
727 my ($self,$transaction,$value) = @_;
728 # $value: the requested config item(s)
729
730 B<NOTE:> C<$transaction> may be C<undef>, depending when / where this hook
731 is called. It's probably best not to try acessing it.
732
733 Example plugin is F<http_config> from the qpsmtpd distribution.
734
735 =head2 hook_unrecognized_command
736
737 This is called if the client sent a command unknown to the core of qpsmtpd.
738 This can be used to implement new SMTP commands or just count the number
739 of unknown commands from the client, see below for examples.
740 Allowed return codes:
741
742 =over 4
743
744 =item DENY_DISCONNECT
745
746 Return B<521> and disconnect the client
747
748 =item DENY
749
750 Return B<500>
751
752 =item DONE
753
754 Qpsmtpd wont do anything; the plugin responded, this is what you want to
755 return, if you are implementing new commands
756
757 =item Anything else...
758
759 Return B<500 Unrecognized command>
760
761 =back
762
763 Arguments:
764
765 my ($self, $transaction, $cmd, @args) = @_;
766 # $cmd = the first "word" of the line
767 # sent by the client
768 # @args = all the other "words" of the
769 # line sent by the client
770 # "word(s)": white space split() line
771
772 B<NOTE:> C<$transaction> may be C<undef>, depending when / where this hook
773 is called. It's probably best not to try acessing it.
774
775 Example plugin is F<tls>.
776
777 =head2 hook_help
778
779 This hook triggers if a client sends the B<HELP> command, allowed return
780 codes are:
781
782 =over 4
783
784 =item DONE
785
786 Plugin gave the answer.
787
788 =item DENY
789
790 The client will get a C<syntax error> message, probably not what you want,
791 better use
792
793 $self->qp->respond(502, "Not implemented.");
794 return DONE;
795
796 =back
797
798 Anything else will be send as help answer.
799
800 Arguments are
801 my ($self, $transaction, @args) = @_;
802
803 with C<@args> being the arguments from the client's command.
804
805 =head2 hook_vrfy
806
807 If the client sents the B<VRFY> command, this hook is called. Default is to
808 return a message telling the user to just try sending the message.
809 Allowed return codes:
810
811 =over 4
812
813 =item OK
814
815 Recipient Exists
816
817 =item DENY
818
819 Return a hard failure code
820
821 =item DONE
822
823 Return nothing and move on
824
825 =item Anything Else...
826
827 Return a B<252>
828
829 =back
830
831 Arguments are:
832
833 my ($self) = shift;
834
835 =cut
836
837 FIXME: this sould be changed in Qpsmtpd::SMTP to pass the rest of the line
838 as arguments to the hook
839
840 =pod
841
842 =head2 hook_noop
843
844 If the client sents the B<NOOP> command, this hook is called. Default is to
845 return C<250 OK>.
846
847 Allowed return codes are:
848
849 =over 4
850
851 =item DONE
852
853 Plugin gave the answer
854
855 =item DENY_DISCONNECT
856
857 Return error code and disconnect client
858
859 =item DENY
860
861 Return error code.
862
863 =item Anything Else...
864
865 Give the default answer of B<250 OK>.
866
867 =back
868
869 Arguments are
870
871 my ($self,$transaction,@args) = @_;
872
873 =head2 hook_post_fork
874
875 B<NOTE:> This hook is only available in qpsmtpd-async.
876
877 It is called while starting qpsmtpd-async. You can run more than one
878 instance of qpsmtpd-async (one per CPU probably). This hook is called
879 after forking one instance.
880
881 Arguments:
882
883 my $self = shift;
884
885 The return values of this hook are discarded.
886
887 =head1 Authentication hooks
888
889 =cut
890
891 B<FIXME missing:> auth_parse
892
893 #=head2 auth
894
895 B<FIXME>
896
897 #=head2 auth-plain
898
899 B<FIXME>
900
901 #=head2 auth-login
902
903 B<FIXME>
904
905 #=head2 auth-cram-md5
906
907 B<FIXME>
908
909 =pod
910
911 See F<docs/authentication.pod>.
912
913 =cut
914
915 # vim: ts=2 sw=2 expandtab