PMilter.pm

filter: Statementstmt 46.6% 248 / 532 Branchbran 4.9% 8 / 162 Conditioncond 6.0% 3 / 50 MC/DCmcdc 3.4% 1 / 29 Subroutinesub 73.7% 76 / 103 Podpod 94.7% 18 / 19 totaltotal 39.5% 354 / 895 SCAR 80.2
CC
103
Cov
35%
setconn
306.0
get_sendmail_class
156.0
__ANON__
156.0
CRAP 3041.9
« PMilter/Context.pm ↑ Summary
1 =pod
2  
3 =head1 LICENSE
4  
5 Copyright (c) 2016-2022 G.W. Haywood. All rights reserved.
6 With thanks to all those who have trodden these paths before,
7 including
8 Copyright (c) 2002-2004 Todd Vierling. All rights reserved.
9  
10 Redistribution and use in source and binary forms, with or without
11 modification, are permitted provided that the following conditions are met:
12  
13 1. Redistributions of source code must retain the above copyright notices,
14 this list of conditions and the following disclaimer.
15  
16 2. Redistributions in binary form must reproduce the above copyright
17 notices, this list of conditions and the following disclaimer in the
18 documentation and/or other materials provided with the distribution.
19  
20 3. Neither the name of the author nor the names of contributors may be used
21 to endorse or promote products derived from this software without specific
22 prior written permission. In the case of G.W. Haywood this permission is
23 hereby now granted.
24  
25 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
29 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 POSSIBILITY OF SUCH DAMAGE.
36  
37 =cut
38  
39 package Sendmail::PMilter;
40  
41 1 ▶use 5.014; # Don't use 5.016 yet. That would enable feature 'unicode_strings', and we
Subroutine BEGIN: called CC=1 SCAR=0.0
42 # probably aren't quite ready for that. We're counting *characters* passed
43 # between us and Sendmail, and Sendmail thinks that they're *bytes*.
44  
45 1 ▶use parent 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
46 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 1 ▶use Errno;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 1 ▶use IO::Select;
Subroutine BEGIN: called CC=1 SCAR=0.0
51 1 ▶use POSIX;
Subroutine BEGIN: called CC=1 SCAR=0.0
52 1 ▶use Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
53 1 ▶use Symbol;
Subroutine BEGIN: called CC=1 SCAR=0.0
54 1 ▶use UNIVERSAL;
Subroutine BEGIN: called CC=1 SCAR=0.0
55  
56 1 our $VERSION = '1.24';
57 1 $VERSION = eval $VERSION;
58  
59 1 our $DEBUG = 0;
60  
61 =pod
62  
63 =head1 NAME
64  
65 Sendmail::PMilter - Perl binding of Sendmail Milter protocol
66  
67 =head1 SYNOPSIS
68  
69 use Sendmail::PMilter;
70  
71 my $milter = new Sendmail::PMilter;
72  
73 $milter->auto_setconn(NAME);
74 $milter->register(NAME, { CALLBACKS }[, FLAGS]);
75 $milter->main();
76  
77 =head1 DESCRIPTION
78  
79 Sendmail::PMilter is a mail filtering API implementing the Sendmail
80 Milter Protocol in Perl. This allows the administrator of Sendmail
81 (and perhaps other MTAs which implement the Milter Protocol) to use
82 pure Perl code to filter and modify mail during an SMTP connection.
83  
84 Over the years, the protocol which governs the communication between
85 qSendmail and its milters has passed through a number of revisions.
86  
87 This documentation is for Sendmail::PMilter versions 1.20 and later,
88 which now supports Milter Protocol Version 6. This is a substantial
89 upgrade from earlier versions, which at best supported up to Milter
90 Protocol Version 2 - this was first seen in Sendmail version 8.14.0
91 which was released on January 31st 2007.
92  
93 Sendmail::PMilter now uses neither the original Sendmail::Milter (it
94 is obsolete, badly flawed and unmaintained) nor the Sendmail::Milter
95 which was packaged with earlier versions of Sendmail::PMilter as a
96 temporary workaround for the broken original.
97  
98 For communications between the MTA and the milter, a 'dispatcher' acts
99 as a go-between. This must be chosen when the milter is initialized,
100 before it serves requests. Several dispatchers are provided within
101 the Sendmail::PMilter module, but in versions before 1.20 all the
102 dispatchers suffered from issues of varying gravity. The 'prefork'
103 dispatcher (see DISPATCHERS below) has now been extensively exercised
104 by the current maintainer, but although the others have been patched
105 from issue reports going back more than a decade from the time of
106 writing (June 2019) THEY HAVE NOT BEEN TESTED. Feedback via the CPAN
107 issue tracking system is encouraged. If you have developed your own
108 dispatcher you can either pass a code reference to set_dispatcher() or
109 set an environment variable to point to it. Sendmail::PMilter will
110 then use it instead of a built-in dispatcher.
111  
112 =head1 METHODS
113  
114 =cut
115  
116 ##### Protocol constants
117 # The SMFIS_* values here are not the same as those used in the Sendmail sources
118 # (see mfapi.h) so that hopefully "0" and "1" won't be used as response codes by
119 # mistake. The other protocol constants below are unchanged from those used in
120 # the Sendmail sources.
121  
122 1 ▶use constant SMFIS_CONTINUE => 100;
Subroutine BEGIN: called CC=1 SCAR=0.0
123 1 ▶use constant SMFIS_REJECT => 101;
Subroutine BEGIN: called CC=1 SCAR=0.0
124 1 ▶use constant SMFIS_DISCARD => 102;
Subroutine BEGIN: called CC=1 SCAR=0.0
125 1 ▶use constant SMFIS_ACCEPT => 103;
Subroutine BEGIN: called CC=1 SCAR=0.0
126 1 ▶use constant SMFIS_TEMPFAIL => 104;
Subroutine BEGIN: called CC=1 SCAR=0.0
127 1 ▶use constant SMFIS_MSG_LOOP => 105;
Subroutine BEGIN: called CC=1 SCAR=0.0
128 1 ▶use constant SMFIS_ALL_OPTS => 110;
Subroutine BEGIN: called CC=1 SCAR=0.0
129  
130 # Milter progessing 'places' (see mfapi.h, values are the same).
131 1 ▶use constant SMFIM_CONNECT => 0; # connect
Subroutine BEGIN: called CC=1 SCAR=0.0
132 1 ▶use constant SMFIM_HELO => 1; # HELO/EHLO
Subroutine BEGIN: called CC=1 SCAR=0.0
133 1 ▶use constant SMFIM_ENVFROM => 2; # MAIL FROM
Subroutine BEGIN: called CC=1 SCAR=0.0
134 1 ▶use constant SMFIM_ENVRCPT => 3; # RCPT TO
Subroutine BEGIN: called CC=1 SCAR=0.0
135 1 ▶use constant SMFIM_DATA => 4; # DATA
Subroutine BEGIN: called CC=1 SCAR=0.0
136 1 ▶use constant SMFIM_EOM => 5; # END OF MESSAGE (final dot)
Subroutine BEGIN: called CC=1 SCAR=0.0
137 1 ▶use constant SMFIM_EOH => 6; # END OF HEADER
Subroutine BEGIN: called CC=1 SCAR=0.0
138  
139 # Some of these things have been switched around from their order of
140 # presentation in the Sendmail sources but the values are the same.
141 ######################################################################
142 # Taken from .../sendmail-8.15.2/include/libmilter/mfdef.h
143 ######################################################################
144 #if _FFR_MDS_NEGOTIATE
145 # define MILTER_MDS_64K ((64 * 1024) - 1)
146 # define MILTER_MDS_256K ((256 * 1024) - 1)
147 # define MILTER_MDS_1M ((1024 * 1024) - 1)
148 #endif /* _FFR_MDS_NEGOTIATE */
149 ######################################################################
150 # These so-called 'protocols' apply to the SMFIP_* flags:
151 #define SMFI_V1_PROT 0x0000003FL The protocol of V1 filter. We won't bother with V1, it's obsolete.
152 #define SMFI_V2_PROT 0x0000007FL The protocol of V2 filter
153 1 ▶use constant SMFI_V2_PROT => 0x0000007F; # The protocol flags available in Milter Protocol Version 2.
Subroutine BEGIN: called CC=1 SCAR=0.0
154 #use constant SMFI_V4_PROT => 0x000003FF; # The protocol flags available in Milter Protocol Version 4.
155 1 ▶use constant SMFI_V6_PROT => 0x001FFFFF; # The protocol flags available in Milter Protocol Version 6.
Subroutine BEGIN: called CC=1 SCAR=0.0
156 1 ▶use constant SMFI_CURR_PROT => 0x001FFFFF; # The protocol flags available in the current Milter Protocol Version (which at July 2019 is Version 6).
Subroutine BEGIN: called CC=1 SCAR=0.0
157 ######################################################################
158 # What the MTA can send/filter wants in protocol
159 1 ▶use constant SMFIP_NOCONNECT => 0x00000001; # MTA should not send connect info
Subroutine BEGIN: called CC=1 SCAR=0.0
160 1 ▶use constant SMFIP_NOHELO => 0x00000002; # MTA should not send HELO info
Subroutine BEGIN: called CC=1 SCAR=0.0
161 1 ▶use constant SMFIP_NOMAIL => 0x00000004; # MTA should not send MAIL info
Subroutine BEGIN: called CC=1 SCAR=0.0
162 1 ▶use constant SMFIP_NORCPT => 0x00000008; # MTA should not send RCPT info
Subroutine BEGIN: called CC=1 SCAR=0.0
163 1 ▶use constant SMFIP_NOBODY => 0x00000010; # MTA should not send body
Subroutine BEGIN: called CC=1 SCAR=0.0
164 1 ▶use constant SMFIP_NOHDRS => 0x00000020; # MTA should not send headers
Subroutine BEGIN: called CC=1 SCAR=0.0
165 1 ▶use constant SMFIP_NOEOH => 0x00000040; # MTA should not send EOH
Subroutine BEGIN: called CC=1 SCAR=0.0
166 1 ▶use constant SMFIP_NR_HDR => 0x00000080; # No reply for headers
Subroutine BEGIN: called CC=1 SCAR=0.0
167 1 ▶use constant SMFIP_NOHREPL => 0x00000080; # No reply for headers (backward compatibility, do not use, same as SMFIP_NR_HDR)
Subroutine BEGIN: called CC=1 SCAR=0.0
168 1 ▶use constant SMFIP_NOUNKNOWN => 0x00000100; # MTA should not send unknown commands
Subroutine BEGIN: called CC=1 SCAR=0.0
169 1 ▶use constant SMFIP_NODATA => 0x00000200; # MTA should not send DATA
Subroutine BEGIN: called CC=1 SCAR=0.0
170 1 ▶use constant SMFIP_SKIP => 0x00000400; # MTA understands SMFIS_SKIP called from EOM callback.
Subroutine BEGIN: called CC=1 SCAR=0.0
171 1 ▶use constant SMFIP_RCPT_REJ => 0x00000800; # MTA should also send rejected RCPTs
Subroutine BEGIN: called CC=1 SCAR=0.0
172 1 ▶use constant SMFIP_NR_CONN => 0x00001000; # No reply for connect
Subroutine BEGIN: called CC=1 SCAR=0.0
173 1 ▶use constant SMFIP_NR_HELO => 0x00002000; # No reply for HELO
Subroutine BEGIN: called CC=1 SCAR=0.0
174 1 ▶use constant SMFIP_NR_MAIL => 0x00004000; # No reply for MAIL
Subroutine BEGIN: called CC=1 SCAR=0.0
175 1 ▶use constant SMFIP_NR_RCPT => 0x00008000; # No reply for RCPT
Subroutine BEGIN: called CC=1 SCAR=0.0
176 1 ▶use constant SMFIP_NR_DATA => 0x00010000; # No reply for DATA
Subroutine BEGIN: called CC=1 SCAR=0.0
177 1 ▶use constant SMFIP_NR_UNKN => 0x00020000; # No reply for UNKN
Subroutine BEGIN: called CC=1 SCAR=0.0
178 1 ▶use constant SMFIP_NR_EOH => 0x00040000; # No reply for eoh
Subroutine BEGIN: called CC=1 SCAR=0.0
179 1 ▶use constant SMFIP_NR_BODY => 0x00080000; # No reply for body chunk
Subroutine BEGIN: called CC=1 SCAR=0.0
180 1 ▶use constant SMFIP_HDR_LEADSPC => 0x00100000; # header value leading space
Subroutine BEGIN: called CC=1 SCAR=0.0
181 1 ▶use constant SMFIP_MDS_256K => 0x10000000; # MILTER_MAX_DATA_SIZE=256K
Subroutine BEGIN: called CC=1 SCAR=0.0
182 1 ▶use constant SMFIP_MDS_1M => 0x20000000; # MILTER_MAX_DATA_SIZE=1M
Subroutine BEGIN: called CC=1 SCAR=0.0
183 ######################################################################
184 # If no negotiate callback is registered, these are the defaults. Basically
185 # everything is enabled except SMFIP_RCPT_REJ and MILTER_MAX_DATA_SIZE_*
186 # Sendmail and Postfix behave differently:
187 # Postfix does not use the constants SMFIP_MDS_256K and SMFIP_MDS_1M.
188 1 ▶use constant SMFIP_ALL_NO_CB => (SMFIP_NOCONNECT|SMFIP_NOHELO|SMFIP_NOMAIL|SMFIP_NORCPT|SMFIP_NOBODY|SMFIP_NOHDRS|SMFIP_NOEOH|SMFIP_NOUNKNOWN|SMFIP_NODATA|SMFIP_SKIP|SMFIP_HDR_LEADSPC);
Subroutine BEGIN: called CC=1 SCAR=0.0
189 1 ▶use constant SMFIP_ALL_NO_REPLY => (SMFIP_NR_HDR|SMFIP_NR_CONN|SMFIP_NR_HELO|SMFIP_NR_MAIL|SMFIP_NR_RCPT|SMFIP_NR_DATA|SMFIP_NR_UNKN|SMFIP_NR_EOH|SMFIP_NR_BODY);
Subroutine BEGIN: called CC=1 SCAR=0.0
190 1 ▶use constant SMFIP_DEFAULTS => ~(SMFIP_ALL_NO_CB|SMFIP_ALL_NO_REPLY);
Subroutine BEGIN: called CC=1 SCAR=0.0
191 ######################################################################
192 # Taken from .../sendmail-8.15.2/include/libmilter/mfapi.h, and
193 # reformatted a little.
194 ######################################################################
195 # These so-called 'actions' apply to the SMFIF_* flags:
196 #define SMFI_V1_ACTS 0x0000000FL The actions of V1 filter
197 #define SMFI_V2_ACTS 0x0000003FL The actions of V2 filter
198 #define SMFI_CURR_ACTS 0x000001FFL actions of current version
199 ######################################################################
200 #define SMFIF_NONE 0x00000000L no flags
201 #define SMFIF_ADDHDRS 0x00000001L filter may add headers
202 #define SMFIF_CHGBODY 0x00000002L filter may replace body
203 #define SMFIF_MODBODY SMFIF_CHGBODY backwards compatible
204 #define SMFIF_ADDRCPT 0x00000004L filter may add recipients
205 #define SMFIF_DELRCPT 0x00000008L filter may delete recipients
206 #define SMFIF_CHGHDRS 0x00000010L filter may change/delete headers
207 #define SMFIF_QUARANTINE 0x00000020L filter may quarantine envelope <<========= "envelope"???
208 #define SMFIF_CHGFROM 0x00000040L filter may change "from" (envelope sender)
209 #define SMFIF_ADDRCPT_PAR 0x00000080L add recipients incl. args
210 #define SMFIF_SETSYMLIST 0x00000100L filter can send set of symbols (macros) that it wants
211 ######################################################################
212 # Capability FLAG value Available in milter protocol version (*)
213 1 ▶use constant SMFIF_NONE => 0x0000; # Unused (*) There's a bit of a muddle about V3,
Subroutine BEGIN: called CC=1 SCAR=0.0
214 1 ▶use constant SMFIF_ADDHDRS => 0x0001; # V1 Add headers but nobody's using it any more.
Subroutine BEGIN: called CC=1 SCAR=0.0
215 1 ▶use constant SMFIF_MODBODY => 0x0002; # V1 Change body (for compatibility with old code, use SMFIF_CHGBODY in new code)
Subroutine BEGIN: called CC=1 SCAR=0.0
216 1 ▶use constant SMFIF_CHGBODY => SMFIF_MODBODY; # V2 Change body
Subroutine BEGIN: called CC=1 SCAR=0.0
217 1 ▶use constant SMFIF_ADDRCPT => 0x0004; # V1 Add recipient
Subroutine BEGIN: called CC=1 SCAR=0.0
218 1 ▶use constant SMFIF_DELRCPT => 0x0008; # V1 Delete recipient
Subroutine BEGIN: called CC=1 SCAR=0.0
219 1 ▶use constant SMFIF_CHGHDRS => 0x0010; # V2 Change headers
Subroutine BEGIN: called CC=1 SCAR=0.0
220 1 ▶use constant SMFIF_QUARANTINE => 0x0020; # V2 quarantine entire message - last of the V2 flags
Subroutine BEGIN: called CC=1 SCAR=0.0
221 1 ▶use constant SMFIF_CHGFROM => 0x0040; # V6 Change envelope sender
Subroutine BEGIN: called CC=1 SCAR=0.0
222 1 ▶use constant SMFIF_ADDRCPT_PAR => 0x0080; # V6 Add recipients incl. args
Subroutine BEGIN: called CC=1 SCAR=0.0
223 1 ▶use constant SMFIF_SETSYMLIST => 0x0100; # V6 Filter can send set of symbols (macros) that it wants
Subroutine BEGIN: called CC=1 SCAR=0.0
224  
225 1 ▶use constant SMFI_V1_ACTS => SMFIF_ADDHDRS|SMFIF_CHGBODY|SMFIF_ADDRCPT|SMFIF_DELRCPT;
Subroutine BEGIN: called CC=1 SCAR=0.0
226 1 ▶use constant SMFI_V2_ACTS => SMFI_V1_ACTS|SMFIF_CHGHDRS|SMFIF_QUARANTINE;
Subroutine BEGIN: called CC=1 SCAR=0.0
227 1 ▶use constant SMFI_V6_ACTS => SMFI_V2_ACTS|SMFIF_CHGFROM|SMFIF_ADDRCPT_PAR|SMFIF_SETSYMLIST;
Subroutine BEGIN: called CC=1 SCAR=0.0
228 1 ▶use constant SMFI_CURR_ACTS => SMFI_V6_ACTS; # All capabilities. See mfapi.h and mfdef.h
Subroutine BEGIN: called CC=1 SCAR=0.0
229  
230 # See libmilter/smfi.c
231 1 ▶use constant MAXREPLYLEN => 980;
Subroutine BEGIN: called CC=1 SCAR=0.0
232 1 ▶use constant MAXREPLIES => 32;
Subroutine BEGIN: called CC=1 SCAR=0.0
233  
234 ##### Symbols exported to the caller
235  
236 1 my $smflags =
237 ' SMFIP_DEFAULTS SMFIP_NOCONNECT SMFIP_NOHELO SMFIP_NOMAIL SMFIP_NORCPT SMFIP_NOBODY SMFIP_NOHDRS SMFIP_NOEOH SMFIP_NOUNKNOWN SMFIP_NODATA SMFIP_RCPT_REJ SMFIP_SKIP
238 SMFIP_NR_CONN SMFIP_NR_HELO SMFIP_NR_MAIL SMFIP_NR_RCPT SMFIP_NR_DATA SMFIP_NR_HDR SMFIP_NR_EOH SMFIP_NR_BODY SMFIP_NR_UNKN SMFIP_HDR_LEADSPC SMFIP_MDS_256K SMFIP_MDS_1M
239 SMFIM_CONNECT SMFIM_HELO SMFIM_ENVFROM SMFIM_ENVRCPT SMFIM_DATA SMFIM_EOM SMFIM_EOH
240 SMFIS_CONTINUE SMFIS_REJECT SMFIS_DISCARD SMFIS_ACCEPT SMFIS_TEMPFAIL SMFIS_MSG_LOOP SMFIS_ALL_OPTS
241 SMFIF_NONE SMFIF_ADDHDRS SMFIF_CHGBODY SMFIF_ADDRCPT SMFIF_DELRCPT SMFIF_CHGHDRS SMFIF_QUARANTINE SMFIF_CHGFROM SMFIF_ADDRCPT_PAR SMFIF_SETSYMLIST
242 SMFI_V2_ACTS SMFI_V6_ACTS SMFI_CURR_ACTS SMFI_V2_PROT SMFI_V6_PROT SMFI_CURR_PROT
243 MAXREPLYLEN MAXREPLIES
244 ';
245 1 my @smflags = eval "qw/ $smflags /;";
246 1 my @dispatchers = qw/ ithread_dispatcher postfork_dispatcher prefork_dispatcher sequential_dispatcher /;
247 1 my @callback_names = qw/ negotiate connect helo envfrom envrcpt data header eoh body eom close abort unknown /;
248 1 my %DEFAULT_CALLBACKS = map { $_ => $_.'_callback' } @callback_names;
249 # Don't export anything by default.
250 1 our @EXPORT = ();
251 # Everything else is OK. I have tried.
252 1 our @EXPORT_OK = qw/
253 SMFIP_DEFAULTS SMFIP_NOCONNECT SMFIP_NOHELO SMFIP_NOMAIL SMFIP_NORCPT SMFIP_NOBODY SMFIP_NOHDRS SMFIP_NOEOH SMFIP_NOUNKNOWN SMFIP_NODATA SMFIP_RCPT_REJ SMFIP_SKIP
254 SMFIP_NR_CONN SMFIP_NR_HELO SMFIP_NR_MAIL SMFIP_NR_RCPT SMFIP_NR_DATA SMFIP_NR_HDR SMFIP_NR_EOH SMFIP_NR_BODY SMFIP_NR_UNKN SMFIP_HDR_LEADSPC SMFIP_MDS_256K SMFIP_MDS_1M
255 SMFIM_CONNECT SMFIM_HELO SMFIM_ENVFROM SMFIM_ENVRCPT SMFIM_DATA SMFIM_EOM SMFIM_EOH
256 SMFIS_CONTINUE SMFIS_REJECT SMFIS_DISCARD SMFIS_ACCEPT SMFIS_TEMPFAIL SMFIS_MSG_LOOP SMFIS_ALL_OPTS
257 SMFIF_NONE SMFIF_ADDHDRS SMFIF_CHGBODY SMFIF_ADDRCPT SMFIF_DELRCPT SMFIF_CHGHDRS SMFIF_QUARANTINE SMFIF_CHGFROM SMFIF_ADDRCPT_PAR SMFIF_SETSYMLIST
258 SMFI_V2_ACTS SMFI_V6_ACTS SMFI_CURR_ACTS SMFI_V2_PROT SMFI_V6_PROT SMFI_CURR_PROT
259 MAXREPLYLEN MAXREPLIES
260 ithread_dispatcher postfork_dispatcher prefork_dispatcher sequential_dispatcher
261 negotiate_callback connect_callback helo_callback envfrom_callback envrcpt_callback data_callback header_callback eoh_callback body_callback eom_callback close_callback abort_callback unknown_callback
262 /;
263  
264 # Three export tags for flags, dispatchers and callbacks.
265 1 our %EXPORT_TAGS = ( all => [ @smflags ], dispatchers => [ @dispatchers ], callbacks => [ (values %DEFAULT_CALLBACKS) ] );
266  
267 1 our $enable_chgfrom = 0;
268  
269 ##### Methods
270  
271 sub new ($) {
272 1 ▶ bless {}, shift;
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
273 }
274  
275 =pod
276  
277 =over 4
278  
279 =item get_max_interpreters()
280  
281 Returns the maximum number of interpreters passed to C<main()>. This is
282 only useful when called from within the dispatcher, as it is not set before
283 C<main()> is called.
284  
285 =cut
286  
287 sub get_max_interpreters ($) {
288 0 ▶ my $this = shift;
Subroutine get_max_interpreters: not called CC=2 SCAR=17.9
Pod documented
289  
290 0 ▶ $this->{max_interpreters} || 0;
Branch: 1 branch
BranchTrueFalseTotal
$this->{"max_interpreters"} or 0 0 0 0
291 }
292  
293 =pod
294  
295 =item get_max_requests()
296  
297 Returns the maximum number of requests per interpreter passed to C<main()>.
298 This is only useful when called from within the dispatcher, as it is not set
299 before C<main()> is called.
300  
301 =cut
302  
303 sub get_max_requests ($) {
304 0 ▶ my $this = shift;
Subroutine get_max_requests: not called CC=2 SCAR=17.9
Pod documented
305  
306 0 ▶ $this->{max_requests} || 0;
Branch: 1 branch
BranchTrueFalseTotal
$this->{"max_requests"} or 0 0 0 0
307 }
308  
309 =pod
310  
311 =item main([MAXCHILDREN[, MAXREQ]])
312  
313 This is the last method called in the main block of a milter program. If
314 successful, this call never returns; the protocol engine is launched and
315 begins accepting connections.
316  
317 MAXCHILDREN (default 0, meaning unlimited) specifies the maximum number of
318 connections that may be serviced simultaneously. If a connection arrives
319 with the number of active connections above this limit, the milter will
320 immediately return a temporary failure condition and close the connection.
321 Passing a value for MAXCHILDREN is optional.
322  
323 MAXREQ (default 0, meaning unlimited) is the maximum number of requests that
324 a child may service before being recycled. It is not guaranteed that the
325 interpreter will service this many requests, only that it will not go over
326 the limit. MAXCHILDREN must be given if MAXREQ is to be set.
327  
328 Any callback which C<die>s will have its output sent to C<warn>, followed by
329 a clean shutdown of the milter connection. To catch any warnings generated
330 by the callbacks, and any error messages caused by a C<die>, set
331 C<$SIG{__WARN__}> to a user-defined subroutine. (See L<perlvar>.)
332  
333 =cut
334  
335 sub main ($;$$$) {
336 0 ▶ require Sendmail::PMilter::Context;
Subroutine main: not called CC=10 SCAR=47.0
Pod documented
337  
338 0 my $this = shift;
339 0 ▶ croak 'main: socket not bound' unless defined($this->{socket});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $this->{"socket"} 0 0 0
340 0 ▶ croak 'main: callbacks not registered' unless defined($this->{callbacks});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $this->{"callbacks"} 0 0 0
341 0 ▶ croak 'main: milter protocol version not defined' unless defined($this->{'milter protocol version'});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $this->{"milter protocol version"} 0 0 0
342  
343 0 my $max_interpreters = shift;
344 0 my $max_requests = shift;
345  
346 0 ▶ $this->{max_interpreters} = $max_interpreters if (defined($max_interpreters) && $max_interpreters =~ /^\d+$/); # This test doesn't permit an empty string.
Branch: 1 branch
BranchTrueFalseTotal
if defined $max_interpreters and $max_interpreters =~ /^\d+$/u 0 0 0
Condition0%0/3
defined $max_interpreters and $max_interpreters =~ /^\d+$/u
!ll&&!rl&&r
000
MC/DC0%0/2
defined $max_interpreters and $max_interpreters =~ /^\d+$/u
defined $max_interpreters $max_interpreters =~ /^\d+$/u
Truth table0%0/3
A and B
A= defined $max_interpretersB= $max_interpreters =~ /^\d+$/u
ABresult
0X0
100
111
347 0 ▶ $this->{max_requests} = $max_requests if (defined($max_requests) && $max_requests =~ /^\d+$/);
Branch: 1 branch
BranchTrueFalseTotal
if defined $max_requests and $max_requests =~ /^\d+$/u 0 0 0
Condition0%0/3
defined $max_requests and $max_requests =~ /^\d+$/u
!ll&&!rl&&r
000
MC/DC0%0/2
defined $max_requests and $max_requests =~ /^\d+$/u
defined $max_requests $max_requests =~ /^\d+$/u
Truth table0%0/3
A and B
A= defined $max_requestsB= $max_requests =~ /^\d+$/u
ABresult
0X0
100
111
348  
349 0 my $dispatcher = $this->{dispatcher};
350  
351 0 ▶ unless (defined($dispatcher)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $dispatcher) 0 0 0
352 0 ▶ my $dispatcher_name = ($ENV{PMILTER_DISPATCHER} || 'postfork').'_dispatcher';
Condition0%0/2
$ENV{'PMILTER_DISPATCHER'} || 'postfork'
l!l
00
MC/DC0%0/1
$ENV{'PMILTER_DISPATCHER'} || 'postfork'
$ENV{'PMILTER_DISPATCHER'}
Truth table0%0/2
A
A= $ENV{'PMILTER_DISPATCHER'}
Aresult
11
00
353 0 $dispatcher = &{\&{qualify_to_ref($dispatcher_name, 'Sendmail::PMilter')}};
354 }
355  
356 my $handler = sub {
357 0 ▶ my $ctx = new Sendmail::PMilter::Context(shift, $this->{callbacks}, $this->{callback_flags}, $this->{'milter protocol version'});
Subroutine __ANON__: not called CC=1 SCAR=6.9
358  
359 0 $ctx->main();
360 0 };
361  
362 0 &$dispatcher($this, $this->{socket}, $handler);
363 0 undef;
364 }
365  
366 =pod
367  
368 =item register(NAME, CALLBACKS[, FLAGS])
369  
370 Sets up the main milter loop configuration.
371  
372 NAME is the name of the milter. This should be the same name as passed to
373 auto_getconn() or auto_setconn(), but this PMilter implementation does not
374 enforce this.
375  
376 CALLBACKS is a hash reference containing one or more callback subroutines.
377 For example
378  
379 my %callbacks =
380 (
381 'negotiate' => \&my_negotiate_callback,
382 'connect' => \&my_connect_callback,
383 'helo' => \&my_helo_callback,
384 'envfrom' => \&my_envfrom_callback,
385 'close' => \&my_close_callback,
386 'abort' => \&my_abort_callback,
387 );
388 $milter->register( $milter_name, \%callbacks );
389  
390 If a callback is not named in this hashref, the caller's package will be
391 searched for subroutines named "CALLBACK_callback", where CALLBACK is the
392 name of the callback function.
393  
394 FLAGS is accepted for backward compatibility with older versions of
395 this module. Consider it deprecated. Set it to SMFI_V6_PROT for all
396 available 'actions' in any recent (last few years) Sendmail version.
397  
398 If no C<negotiate> callback is registered, then by default the protocol
399 steps available are as described in .../libmilter/engine.c in the
400 Sendmail sources. This means all the registered CALLBACKS plus the
401 SKIP function call which is allowed in the End Of Message callback.
402 Note that SMFIP_RCPT_REJ is specifically not included.
403  
404 C<register()> must be called successfully exactly once. If called a second
405 time, the previously registered callbacks will be erased.
406  
407 Returns 1 on success, undef on failure.
408  
409 =cut
410  
411 sub register ($$$;$) {
412 0 ▶ my $this = shift;
Subroutine register: not called CC=11 SCAR=48.8
Pod documented
413 0 $this->{name} = shift;
414  
415 0 ▶ carp 'register: no name supplied' unless defined($this->{name});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $this->{"name"} 0 0 0
416 0 ▶ carp 'register: passed ref as name argument' if ref($this->{name});
Branch: 1 branch
BranchTrueFalseTotal
if ref $this->{"name"} 0 0 0
417  
418 0 my $callbacks = shift;
419 0 my $pkg = caller;
420  
421 0 ▶ croak 'register: callbacks is undef' unless defined($callbacks);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $callbacks 0 0 0
422 0 ▶ croak 'register: callbacks not hash ref' unless UNIVERSAL::isa($callbacks, 'HASH');
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::isa($callbacks, "HASH") 0 0 0
423  
424 # make internal copy, and convert to code references
425 0 $callbacks = { %$callbacks };
426  
427 0 foreach my $cbname (keys %DEFAULT_CALLBACKS) {
428 0 my $cb = $callbacks->{$cbname};
429 0 ▶ if (defined($cb) && !UNIVERSAL::isa($cb, 'CODE')) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cb and not UNIVERSAL::isa($cb, "CODE")) 0 0 0
Condition0%0/3
defined $cb and not UNIVERSAL::isa($cb, "CODE")
!ll&&!rl&&r
000
MC/DC0%0/2
defined $cb and not UNIVERSAL::isa($cb, "CODE")
defined $cb not UNIVERSAL::isa($cb, "CODE")
Truth table0%0/3
A and not(B)
A= defined $cbB= not UNIVERSAL::isa($cb, "CODE")
ABresult
0X0
100
111
430 0 $cb = qualify_to_ref($cb, $pkg);
431 0 ▶ if (exists(&$cb)) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists &$cb) { } 0 0 0
432 0 $callbacks->{$cbname} = \&$cb;
433 } else {
434 0 delete $callbacks->{$cbname};
435 }
436 }
437 }
438  
439 0 $this->{callbacks} = $callbacks;
440 0 ▶ $this->{callback_flags} = shift || 0;
Condition0%0/2
shift() || 0
l!l
00
MC/DC0%0/1
shift() || 0
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
441 # MILTER PROTOCOL VERSION
442 0 ▶ $this->{'milter protocol version'} = ($this->{callback_flags} & ~0x3F) ? 6 : 2;
Branch: 1 branch
BranchTrueFalseTotal
$this->{'callback_flags'} & 18446744073709551552 ? : 0 0 0
443 0 1;
444 }
445  
446 =pod
447  
448 =item setconn(DESC)
449  
450 Sets up the server socket with connection descriptor DESC. This is
451 identical to the descriptor syntax used by the "X" milter configuration
452 lines in sendmail.cf (if using Sendmail). This should be one of the
453 following:
454  
455 =over 2
456  
457 =item local:PATH
458  
459 A local ("UNIX") socket on the filesystem, named PATH. This has some smarts
460 that will auto-delete the pathname if it seems that the milter is not
461 currently running (but this currently contains a race condition that may not
462 be fixable; at worst, there could be two milters running with one never
463 receiving connections).
464  
465 =item inet:PORT[@HOST]
466  
467 An IPv4 socket, bound to address HOST (default INADDR_ANY), on port PORT.
468 It is not recommended to open milter engines to the world, so the @HOST part
469 should be specified.
470  
471 =item inet6:PORT[@HOST]
472  
473 An IPv6 socket, bound to address HOST (default INADDR_ANY), on port PORT.
474 This requires IPv6 support and the Perl IO::Socket::IP package to be installed.
475 It is not recommended to open milter engines to the world, so the @HOST part
476 should be specified.
477  
478 =back
479  
480 Returns a true value on success, undef on failure.
481  
482 =cut
483  
484 sub setconn ($$) {
485 0 ▶ my $this = shift;
Subroutine setconn: not called CC=17 SCAR=57.2
Pod documented
486 0 my $conn = shift;
487 0 ▶ my $backlog = $this->{backlog} || 5;
Condition0%0/2
$this->{'backlog'} || 5
l!l
00
MC/DC0%0/1
$this->{'backlog'} || 5
$this->{'backlog'}
Truth table0%0/2
A
A= $this->{'backlog'}
Aresult
11
00
488 0 my $socket;
489  
490 0 ▶ croak "setconn: $conn: unspecified protocol"
Branch: 1 branch
BranchTrueFalseTotal
unless $conn =~ /^([^:]+):([^:\@]+)(?:@([^:\@]+|\[[0-9a-f:\.]+\]))?$/u 0 0 0
491 unless ($conn =~ /^([^:]+):([^:@]+)(?:@([^:@]+|\[[0-9a-f:\.]+\]))?$/);
492  
493 0 ▶ if ($1 eq 'local' || $1 eq 'unix') {
Branch: 3 branches
BranchTrueFalseTotal
if ($1 eq 'local' or $1 eq 'unix') { } 0 0 0
elsif ($1 eq 'inet') { } 0 0 0
elsif ($1 eq 'inet6') { } 0 0 0
Condition0%0/3
$1 eq "local" or $1 eq "unix"
l!l&&r!l&&!r
000
MC/DC0%0/2
$1 eq "local" or $1 eq "unix"
$1 eq "local" $1 eq "unix"
Truth table0%0/3
A or B
A= $1 eq "local"B= $1 eq "unix"
ABresult
1X1
011
000
494 0 require IO::Socket::UNIX;
495  
496 0 my $path = $2;
497 0 my $addr = sockaddr_un($path);
498  
499 0 ▶ croak "setconn: $conn: path not absolute"
Branch: 1 branch
BranchTrueFalseTotal
unless $path =~ m[^/]u 0 0 0
500 unless ($path =~ m,^/,,);
501  
502 0 ▶ if (-e $path && ! -S $path) { # exists, not a socket
Branch: 1 branch
BranchTrueFalseTotal
if (-e $path and not -S $path) { } 0 0 0
Condition0%0/3
-e $path and not -S $path
!ll&&!rl&&r
000
MC/DC0%0/2
-e $path and not -S $path
-e $path not -S $path
Truth table0%0/3
A and not(B)
A= -e $pathB= not -S $path
ABresult
0X0
100
111
503 0 $! = Errno::EEXIST;
504 } else {
505 0 $socket = IO::Socket::UNIX->new(Type => SOCK_STREAM);
506 }
507  
508 # Some systems require you to unlink an orphaned inode.
509 # There's a race condition here, but it's unfortunately
510 # not easily fixable. Using an END{} block doesn't
511 # always work, and that's too wonky with fork() anyway.
512  
513 0 ▶ if (defined($socket) && !$socket->bind($addr)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $socket and not $socket->bind($addr)) 0 0 0
Condition0%0/3
defined $socket and not $socket->bind($addr)
!ll&&!rl&&r
000
MC/DC0%0/2
defined $socket and not $socket->bind($addr)
defined $socket not $socket->bind($addr)
Truth table0%0/3
A and not(B)
A= defined $socketB= not $socket->bind($addr)
ABresult
0X0
100
111
514 0 ▶ if ($socket->connect($addr)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($socket->connect($addr)) { } 0 0 0
515 0 close $socket;
516 0 undef $socket;
517 0 $! = Errno::EADDRINUSE;
518 } else {
519 0 unlink $path; # race condition
520 0 ▶ $socket->bind($addr) || undef $socket;
Branch: 1 branch
BranchTrueFalseTotal
$socket->bind($addr) or undef $socket 0 0 0
521 }
522 }
523  
524 0 ▶ if (defined($socket)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $socket) 0 0 0
525 0 ▶ $socket->listen($backlog) || croak "setconn: listen $conn: $!";
Branch: 1 branch
BranchTrueFalseTotal
$socket->listen($backlog) or croak("setconn: listen ${conn}: $!") 0 0 0
526 }
527 } elsif ($1 eq 'inet') {
528 0 require IO::Socket::INET;
529  
530 0 $socket = IO::Socket::INET->new(
531 Proto => 'tcp',
532 ReuseAddr => 1,
533 Listen => $backlog,
534 LocalPort => $2,
535 LocalAddr => $3
536 );
537 } elsif ($1 eq 'inet6') {
538 0 require IO::Socket::IP;
539  
540 0 $socket = IO::Socket::IP->new(
541 Proto => 'tcp',
542 ReuseAddr => 1,
543 Listen => $backlog,
544 LocalService => $2,
545 LocalHost => $3
546 );
547 } else {
548 0 croak "setconn: $conn: unknown protocol";
549 }
550  
551 0 ▶ if (defined($socket)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $socket) { } 0 0 0
552 0 $this->set_socket($socket);
553 } else {
554 0 carp "setconn: $conn: $!";
555 0 undef;
556 }
557 }
558  
559 =pod
560  
561 =item set_dispatcher(CODEREF)
562  
563 Sets the dispatcher used to accept socket connections and hand them off to
564 the protocol engine. This allows pluggable resource allocation so that the
565 milter script may use fork, threads, or any other such means of handling
566 milter connections. See C<DISPATCHERS> below for more information.
567  
568 The subroutine (code) reference will be called by C<main()> when the
569 listening socket object is prepared and ready to accept connections. It
570 will be passed the arguments:
571  
572 MILTER, LSOCKET, HANDLER
573  
574 MILTER is the milter object currently running. LSOCKET is a listening
575 socket (an instance of C<IO::Socket>), upon which C<accept()> should be
576 called. HANDLER is a subroutine reference which should be called, passing
577 the socket object returned by C<< LSOCKET->accept() >>.
578  
579 Note that the dispatcher may also be set from one of the off-the-shelf
580 dispatchers noted in this document by setting the PMILTER_DISPATCHER
581 environment variable. See C<DISPATCHERS>, below.
582  
583 =cut
584  
585 sub set_dispatcher($&) {
586 0 ▶ my $this = shift;
Subroutine set_dispatcher: not called CC=1 SCAR=6.9
Pod documented
587  
588 0 $this->{dispatcher} = shift;
589 0 1;
590 }
591  
592 =pod
593  
594 =item set_listen(BACKLOG)
595  
596 Set the socket listen backlog to BACKLOG. The default is 5 connections if
597 not set explicitly by this method. Only useful before calling C<main()>.
598  
599 =cut
600  
601 sub set_listen ($$) {
602 0 ▶ my $this = shift;
Subroutine set_listen: not called CC=2 SCAR=17.9
Pod documented
603 0 my $backlog = shift;
604  
605 0 ▶ croak 'set_listen: socket already bound' if defined($this->{socket});
Branch: 1 branch
BranchTrueFalseTotal
if defined $this->{"socket"} 0 0 0
606  
607 0 $this->{backlog} = $backlog;
608 0 1;
609 }
610  
611 =pod
612  
613 =item set_socket(SOCKET)
614  
615 Rather than calling C<setconn()>, this method may be called explicitly to
616 set the C<IO::Socket> instance used to accept inbound connections.
617  
618 =cut
619  
620 sub set_socket ($$) {
621 0 ▶ my $this = shift;
Subroutine set_socket: not called CC=3 SCAR=24.8
Pod documented
622 0 my $socket = shift;
623  
624 0 ▶ croak 'set_socket: socket already bound' if defined($this->{socket});
Branch: 1 branch
BranchTrueFalseTotal
if defined $this->{"socket"} 0 0 0
625 0 ▶ croak 'set_socket: not an IO::Socket instance' unless UNIVERSAL::isa($socket, 'IO::Socket');
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::isa($socket, "IO::Socket") 0 0 0
626  
627 0 $this->{socket} = $socket;
628 0 1;
629 }
630  
631 =pod
632  
633 =back
634  
635 =head1 SENDMAIL-SPECIFIC METHODS
636  
637 The following methods are only useful if Sendmail is the MTA connecting to
638 this milter. Other MTAs likely don't use Sendmail's configuration file, so
639 these methods would not be useful with them.
640  
641 =over 4
642  
643 =item auto_getconn(NAME[, CONFIG])
644  
645 Returns the connection descriptor for milter NAME in Sendmail configuration
646 file CONFIG (default C</etc/mail/sendmail.cf> or whatever was set by
647 C<set_sendmail_cf()>). This can then be passed to setconn(), below.
648  
649 Returns a true value on success, undef on failure.
650  
651 =cut
652  
653 sub auto_getconn ($$;$) {
654 5 ▶ my $this = shift;
Subroutine auto_getconn: called CC=9 SCAR=24.4
Pod documented
655 5 ▶ my $milter = shift || die "milter name not supplied\n";
Condition100%2/2
shift() || die("milter name not supplied\n")
l!l
41
MC/DC100%1/1
shift() || die("milter name not supplied\n")
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
656 4 ▶ my $cf = shift || $this->get_sendmail_cf();
Condition33%1/3
shift() || $this->get_sendmail_cf
l!l&&r!l&&!r
040
MC/DC0%0/2
shift() || $this->get_sendmail_cf
shift() $this->get_sendmail_cf
Truth table33%1/3
A || B
A= shift()B= $this->get_sendmail_cf
ABresult
1X1
011
000
657 4 local *CF;
658  
659 4 ▶ open(CF, '<'.$cf) || die "open $cf: $!";
Branch: 1 branch
BranchTrueFalseTotal
open CF, '<' . $cf or die "open ${cf}: $!" 0 4 4
660  
661 4 ▶ while (<CF>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline CF)) 4 0 4
662 4 s/\s+$//; # also trims newlines
663  
664 4 ▶ s/^X([^,\s]+),\s*// || next;
Branch: 1 branch
BranchTrueFalseTotal
s/^X([^,\s]+),\s*//u or next 0 4 4
665 4 ▶ ($milter eq $1) || next;
Branch: 1 branch
BranchTrueFalseTotal
$milter eq $1 or next 0 4 4
666  
667 4 ▶ while (s/^(.)=([^,\s]+)(,\s*|\Z)//) {
Branch: 1 branch
BranchTrueFalseTotal
if (s/^(.)=([^,\s]+)(,\s*|\Z)//u) 4 0 4
668 4 ▶ if ($1 eq 'S') {
Branch: 1 branch
BranchTrueFalseTotal
if ($1 eq "S") 4 0 4
669 4 close(CF);
670 4 return $2;
671 }
672 }
673 }
674  
675 0 close(CF);
676 0 undef;
677 }
678  
679 =pod
680  
681 =item auto_setconn(NAME[, CONFIG])
682  
683 Creates the server connection socket for milter NAME in Sendmail
684 configuration file CONFIG.
685  
686 Essentially, does:
687  
688 $milter->setconn($milter->auto_getconn(NAME, CONFIG))
689  
690 Returns a true value on success, undef on failure.
691  
692 =cut
693  
694 sub auto_setconn ($$;$) {
695 0 ▶ my $this = shift;
Subroutine auto_setconn: not called CC=2 SCAR=17.9
Pod documented
696 0 my $name = shift;
697 0 my $conn = $this->auto_getconn($name, shift);
698  
699 0 ▶ if (defined($conn)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $conn) { } 0 0 0
700 0 $this->setconn($conn);
701 } else {
702 0 carp "auto_setconn: no connection for $name found";
703 0 undef;
704 }
705 }
706  
707 =pod
708  
709 =item get_sendmail_cf()
710  
711 Returns the pathname of the Sendmail configuration file. If this has
712 been set by C<set_sendmail_cf()>, then that is the value returned.
713 Otherwise the default pathname C</etc/mail/sendmail.cf> is returned.
714  
715 =cut
716  
717 sub get_sendmail_cf ($) {
718 7 ▶ my $this = shift;
Subroutine get_sendmail_cf: called CC=2 SCAR=6.9
Pod documented
719  
720 7 ▶ $this->{sendmail_cf} || '/etc/mail/sendmail.cf';
Branch: 1 branch
BranchTrueFalseTotal
$this->{"sendmail_cf"} or "/etc/mail/sendmail.cf" 2 5 7
721 }
722  
723 =pod
724  
725 =item get_sendmail_class(CLASS[, CONFIG])
726  
727 Returns a list containing all members of the Sendmail class CLASS, in
728 Sendmail configuration file CONFIG (default C</etc/mail/sendmail.cf> or
729 whatever is set by C<set_sendmail_cf()>). Typically this is used to look up
730 the entries in class "w", the local hostnames class.
731  
732 =cut
733  
734 sub get_sendmail_class ($$;$) {
735 0 ▶ my $this = shift;
Subroutine get_sendmail_class: not called CC=12 SCAR=50.5
Pod documented
736 0 my $class = shift;
737 0 ▶ my $cf = shift || $this->get_sendmail_cf();
Condition0%0/3
shift() || $this->get_sendmail_cf
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || $this->get_sendmail_cf
shift() $this->get_sendmail_cf
Truth table0%0/3
A || B
A= shift()B= $this->get_sendmail_cf
ABresult
1X1
011
000
738 0 my %entries;
739 0 local *CF;
740  
741 0 ▶ open(CF, '<'.$cf) || croak "get_sendmail_class: open $cf: $!";
Branch: 1 branch
BranchTrueFalseTotal
open CF, '<' . $cf or croak("get_sendmail_class: open ${cf}: $!") 0 0 0
742  
743 0 ▶ while (<CF>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline CF)) 0 0 0
744 0 s/\s+$//; # also trims newlines
745  
746 0 ▶ if (s/^C\s*$class\s*//) {
Branch: 2 branches
BranchTrueFalseTotal
if (s/^C\s*$class\s*//u) { } 0 0 0
elsif (s/^F\s*$class\s*(-o)?\s*//u) { } 0 0 0
747 0 foreach (split(/\s+/)) {
748 0 $entries{$_} = 1;
749 }
750 } elsif (s/^F\s*$class\s*(-o)?\s*//) {
751 0 my $required = !defined($1);
752 0 local *I;
753  
754 0 ▶ croak "get_sendmail_class: class $class lookup resulted in pipe: $_" if (/^\|/);
Branch: 1 branch
BranchTrueFalseTotal
if /^\|/u 0 0 0
755  
756 0 ▶ if (open(I, '<'.$_)) {
Branch: 2 branches
BranchTrueFalseTotal
if (open I, '<' . $_) { } 0 0 0
elsif ($required) { } 0 0 0
757 0 ▶ while (<I>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline I)) 0 0 0
758 0 s/#.*$//;
759 0 s/\s+$//;
760 0 ▶ next if /^$/;
Branch: 1 branch
BranchTrueFalseTotal
if /^$/u 0 0 0
761 0 $entries{$_} = 1;
762 }
763 0 close(I);
764 } elsif ($required) {
765 0 croak "get_sendmail_class: class $class lookup: $_: $!";
766 }
767 }
768 }
769  
770 0 close(CF);
771 0 keys %entries;
772 }
773  
774 =pod
775  
776 =item get_sendmail_option(OPTION[, CONFIG])
777  
778 Returns a list containing the first occurrence of Sendmail option
779 OPTION in Sendmail configuration file CONFIG (default C</etc/mail/sendmail.cf>,
780 or whatever has been set by C<set_sendmail_cf()>). Returns the
781 value of the option or undef if it is not found. This can be used
782 to learn configuration parameters such as Milter.maxdatasize.
783  
784 =cut
785  
786 sub get_sendmail_option ($$;$) {
787 0 ▶ my $this = shift;
Subroutine get_sendmail_option: not called CC=5 SCAR=34.0
Pod documented
788 0 my $option = shift;
789 0 ▶ my $cf = shift || $this->get_sendmail_cf();
Condition0%0/3
shift() || $this->get_sendmail_cf
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || $this->get_sendmail_cf
shift() $this->get_sendmail_cf
Truth table0%0/3
A || B
A= shift()B= $this->get_sendmail_cf
ABresult
1X1
011
000
790 0 my %entries;
791 0 local *CF;
792 0 ▶ open(CF, '<'.$cf) || croak "get_sendmail_option: open $cf: $!";
Branch: 1 branch
BranchTrueFalseTotal
open CF, '<' . $cf or croak("get_sendmail_option: open ${cf}: $!") 0 0 0
793 0 ▶ while (<CF>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline CF)) 0 0 0
794 0 s/\s+$//; # also trims newlines
795 0 ▶ if (/^O\s*$option=(\d+)/) { return $1; }
Branch: 1 branch
BranchTrueFalseTotal
if (/^O\s*$option=(\d+)/u) 0 0 0
796 }
797 0 close(CF);
798 0 undef;
799 }
800  
801 =pod
802  
803 =item set_sendmail_cf(FILENAME)
804  
805 Set the default filename used by C<auto_getconn>, C<auto_setconn>, and
806 C<sendmail_class> to find Sendmail-specific configuration data. If not
807 explicitly set by this method, it defaults to C</etc/mail/sendmail.cf>.
808 Returns 1.
809  
810 =cut
811  
812 sub set_sendmail_cf ($) {
813 7 ▶ my $this = shift;
Subroutine set_sendmail_cf: called CC=1 SCAR=0.0
Pod documented
814  
815 7 $this->{sendmail_cf} = shift;
816 7 1;
817 }
818  
819 ### off-the-shelf dispatchers
820  
821 =pod
822  
823 =back
824  
825 =head1 DISPATCHERS
826  
827 Milter requests may be dispatched to the protocol handler in a pluggable
828 manner (see the description for the C<set_dispatcher()> method above).
829 C<Sendmail::PMilter> offers some off-the-shelf dispatchers that use
830 different methods of resource allocation.
831  
832 Each of these is referenced as a non-object function, and return a value
833 that may be passed directly to C<set_dispatcher()>.
834  
835 =over 4
836  
837 =item Sendmail::PMilter::ithread_dispatcher()
838  
839 =item (environment) PMILTER_DISPATCHER=ithread
840  
841 June 2019: This dispatcher has not been tested adequately.
842  
843 The C<ithread> dispatcher spins up a new thread upon each connection to
844 the milter socket. This provides a thread-based model that may be more
845 resource efficient than the similar C<postfork> dispatcher. This requires
846 that the Perl interpreter be compiled with C<-Duseithreads>, and uses the
847 C<threads> module (available on Perl 5.8 or later only).
848  
849 =cut
850  
851 sub ithread_dispatcher {
852 0 ▶ require threads;
Subroutine ithread_dispatcher: not called CC=1 SCAR=6.9
Pod documented
853 0 require threads::shared;
854 0 require Thread::Semaphore;
855  
856 0 my $nchildren = 0;
857  
858 0 threads::shared::share($nchildren);
859  
860 sub {
861 0 ▶ my $this = shift;
Subroutine __ANON__: not called CC=8 SCAR=42.8
862 0 my $lsocket = shift;
863 0 my $handler = shift;
864 0 my $maxchildren = $this->get_max_interpreters();
865 0 my $child_sem;
866  
867 0 ▶ if ($maxchildren) {
Branch: 1 branch
BranchTrueFalseTotal
if ($maxchildren) 0 0 0
868 0 $child_sem = Thread::Semaphore->new($maxchildren);
869 }
870
871 0 ▶ my $siginfo = exists($SIG{INFO}) ? 'INFO' : 'USR1';
Branch: 1 branch
BranchTrueFalseTotal
exists $SIG{'INFO'} ? : 0 0 0
872 local $SIG{$siginfo} = sub {
873 0 ▶ warn "Number of active children: $nchildren\n";
Subroutine __ANON__: not called CC=1 SCAR=6.9
874 0 };
875  
876 my $child_sub = sub {
877 0 ▶ my $socket = shift;
Subroutine __ANON__: not called CC=3 SCAR=24.8
878  
879 0 eval {
880 0 &$handler($socket);
881 0 $socket->close();
882 };
883 0 my $died = $@;
884  
885 0 lock($nchildren);
886 0 $nchildren--;
887 0 ▶ if ($child_sem) {
Branch: 1 branch
BranchTrueFalseTotal
if ($child_sem) 0 0 0
888 0 $child_sem->up();
889 }
890 0 ▶ warn $died if $died;
Branch: 1 branch
BranchTrueFalseTotal
if $died 0 0 0
891 0 };
892  
893 0 while (1) {
894 0 my $socket = $lsocket->accept();
895 0 ▶ next if $!{EINTR};
Branch: 1 branch
BranchTrueFalseTotal
if $!{"EINTR"} 0 0 0
896  
897 0 ▶ warn "$$: incoming connection\n" if ($DEBUG > 0);
Branch: 1 branch
BranchTrueFalseTotal
if $Sendmail::PMilter::DEBUG > 0 0 0 0
898  
899 0 ▶ if ($child_sem and ! $child_sem->down_nb()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($child_sem and not $child_sem->down_nb) 0 0 0
Condition0%0/3
$child_sem and not $child_sem->down_nb
!ll&&!rl&&r
000
MC/DC0%0/2
$child_sem and not $child_sem->down_nb
$child_sem not $child_sem->down_nb
Truth table0%0/3
A and not(B)
A= $child_semB= not $child_sem->down_nb
ABresult
0X0
100
111
900 0 warn "pausing for high load: children $nchildren >= max $maxchildren";
901 0 my $start = time();
902 0 $child_sem->down();
903 0 my $end = time();
904 0 warn sprintf("paused for %.1f seconds due to high load", $end - $start);
905 }
906  
907 # scoping block for lock()
908 {
909 0 lock($nchildren);
910 0 ▶ my $t = threads->create($child_sub, $socket) || die "thread creation failed: $!\n";
Condition0%0/2
'threads'->create($child_sub, $socket) || die("thread creation failed: $!\n")
l!l
00
MC/DC0%0/1
'threads'->create($child_sub, $socket) || die("thread creation failed: $!\n")
'threads'->create($child_sub, $socket)
Truth table0%0/2
A
A= 'threads'->create($child_sub, $socket)
Aresult
11
00
911 0 $t->detach;
912 0 threads->yield();
913 0 $nchildren++;
914 }
915 }
916 0 };
917 }
918  
919 =pod
920  
921 =item Sendmail::PMilter::prefork_dispatcher([PARAMS])
922  
923 =item (environment) PMILTER_DISPATCHER=prefork
924  
925 June 2019: This dispatcher has been tested extensively by the maintainer.
926  
927 The C<prefork> dispatcher forks the main Perl process before accepting
928 connections, and uses the main process to monitor the children. This
929 should be appropriate for steady traffic flow sites. Note that if
930 MAXINTERP is not set in the call to C<main()> or in PARAMS, an internal
931 default of 10 processes will be used; similarly, if MAXREQ is not set, 100
932 requests will be served per child.
933  
934 Currently the child process pool is fixed in size: discarded children will
935 be replaced immediately.
936  
937 PARAMS, if specified, is a hash of key-value pairs defining parameters for
938 the dispatcher. The available parameters that may be set are:
939  
940 =over 2
941  
942 =item child_init
943  
944 subroutine reference that will be called after each child process is forked.
945 It will be passed the C<MILTER> object.
946  
947 =item child_exit
948  
949 subroutine reference that will be called just before each child process
950 terminates. It will be passed the C<MILTER> object.
951  
952 =item max_children
953  
954 Maximum number of child processes active at any time. Equivalent to the
955 MAXINTERP option to main() -- if not set in the main() call, this value
956 will be used.
957  
958 =item max_requests_per_child
959  
960 Maximum number of requests a child process may service before being
961 recycled. Equivalent to the MAXREQ option to main() -- if not set in the
962 main() call, this value will be used.
963  
964 =back
965  
966 =cut
967  
968 sub prefork_dispatcher (@) {
969 0 ▶ my %params = @_;
Subroutine prefork_dispatcher: not called CC=1 SCAR=6.9
Pod documented
970 0 my %children;
971  
972 my $child_dispatcher = sub {
973 0 ▶ my $this = shift;
Subroutine __ANON__: not called CC=9 SCAR=45.0
974 0 my $lsocket = shift;
975 0 my $handler = shift;
976 0 ▶ my $max_requests = $this->get_max_requests() || $params{max_requests_per_child} || 100;
Condition0%0/5
$this->get_max_requests || $params{'max_requests_per_child'} || 100
l!l
00
$this->get_max_requests or $params{"max_requests_per_child"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$this->get_max_requests || $params{'max_requests_per_child'} || 100
$this->get_max_requests $params{"max_requests_per_child"}
Truth table0%0/3
A or B
A= $this->get_max_requestsB= $params{"max_requests_per_child"}
ABresult
1X1
011
000
977 0 my $i = 0;
978  
979 0 local $SIG{PIPE} = 'IGNORE'; # so close_callback will be reached
980  
981 0 ▶ my $siginfo = exists($SIG{INFO}) ? 'INFO' : 'USR1';
Branch: 1 branch
BranchTrueFalseTotal
exists $SIG{'INFO'} ? : 0 0 0
982 local $SIG{$siginfo} = sub {
983 0 ▶ warn "$$: requests handled: $i\n";
Subroutine __ANON__: not called CC=1 SCAR=6.9
984 0 };
985  
986 # call child_init handler if present
987 0 ▶ if (defined $params{child_init}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params{"child_init"}) 0 0 0
988 0 my $method = $params{child_init};
989 0 $this->$method();
990 }
991  
992 0 ▶ while ($i < $max_requests) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $max_requests) 0 0 0
993 0 my $socket = $lsocket->accept();
994 0 ▶ next if $!{EINTR};
Branch: 1 branch
BranchTrueFalseTotal
if $!{"EINTR"} 0 0 0
995  
996 0 ▶ warn "$$: incoming connection\n" if ($DEBUG > 0);
Branch: 1 branch
BranchTrueFalseTotal
if $Sendmail::PMilter::DEBUG > 0 0 0 0
997  
998 0 $i++;
999 0 &$handler($socket);
1000 0 $socket->close();
1001 }
1002  
1003 # call child_exit handler if present
1004 0 ▶ if (defined $params{child_exit}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params{"child_exit"}) 0 0 0
1005 0 my $method = $params{child_exit};
1006 0 $this->$method();
1007 }
1008 0 };
1009  
1010 # Propagate some signals down to the entire process group.
1011 my $killall = sub {
1012 0 ▶ my $sig = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
1013  
1014 0 kill 'TERM', keys %children;
1015 0 exit 0;
1016 0 };
1017 0 local $SIG{INT} = $killall;
1018 0 local $SIG{QUIT} = $killall;
1019 0 local $SIG{TERM} = $killall;
1020  
1021 0 setpgrp();
1022  
1023 sub {
1024 0 ▶ my $this = $_[0];
Subroutine __ANON__: not called CC=7 SCAR=40.3
1025 0 ▶ my $maxchildren = $this->get_max_interpreters() || $params{max_children} || 10;
Condition0%0/5
$this->get_max_interpreters || $params{'max_children'} || 10
l!l
00
$this->get_max_interpreters or $params{"max_children"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$this->get_max_interpreters || $params{'max_children'} || 10
$this->get_max_interpreters $params{"max_children"}
Truth table0%0/3
A or B
A= $this->get_max_interpretersB= $params{"max_children"}
ABresult
1X1
011
000
1026  
1027 0 while (1) {
1028 0 ▶ while (scalar keys %children < $maxchildren) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %children < $maxchildren) 0 0 0
1029 0 my $pid = fork();
1030 0 ▶ die "fork: $!" unless defined($pid);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pid 0 0 0
1031  
1032 0 ▶ if ($pid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid) { } 0 0 0
1033 # Perl reset these to IGNORE. Restore them.
1034 0 $SIG{INT} = $killall;
1035 0 $SIG{QUIT} = $killall;
1036 0 $SIG{TERM} = $killall;
1037 0 $children{$pid} = 1;
1038 } else {
1039 # Perl reset these to IGNORE. Set to defaults.
1040 0 $SIG{INT} = 'DEFAULT';
1041 0 $SIG{QUIT} = 'DEFAULT';
1042 0 $SIG{TERM} = 'DEFAULT';
1043 0 &$child_dispatcher(@_);
1044 0 exit 0;
1045 }
1046 }
1047  
1048 # Wait for a pid to exit, then loop back up to fork.
1049 0 my $pid = wait();
1050 0 ▶ delete $children{$pid} if ($pid > 0);
Branch: 1 branch
BranchTrueFalseTotal
if $pid > 0 0 0 0
1051 }
1052 0 };
1053 }
1054  
1055 =pod
1056  
1057 =item Sendmail::PMilter::postfork_dispatcher()
1058  
1059 =item (environment) PMILTER_DISPATCHER=postfork
1060  
1061 June 2019: This dispatcher has not been tested adequately.
1062  
1063 This is the default dispatcher for PMilter if no explicit dispatcher is set.
1064  
1065 The C<postfork> dispatcher forks the main Perl process upon each connection
1066 to the milter socket. This is adequate for machines that get bursty but
1067 otherwise mostly idle mail traffic, as the idle-time resource consumption is
1068 very low.
1069  
1070 If the maximum number of interpreters is running when a new connection
1071 comes in, this dispatcher blocks until a slot becomes available for a
1072 new interpreter.
1073  
1074 =cut
1075  
1076 sub postfork_dispatcher () {
1077 0 ▶ my $nchildren = 0;
Subroutine postfork_dispatcher: not called CC=1 SCAR=6.9
Pod documented
1078 0 my $sigchld;
1079  
1080 $sigchld = sub {
1081 0 ▶ my $pid;
Subroutine __ANON__: not called CC=2 SCAR=17.9
1082 0 ▶ $nchildren-- while (($pid = waitpid(-1, WNOHANG)) > 0);
Branch: 1 branch
BranchTrueFalseTotal
($pid = waitpid(-1, 1)) > 0 and do { --$nchildren } 0 0 0
1083 0 $SIG{CHLD} = $sigchld;
1084 0 };
1085  
1086 sub {
1087 0 ▶ my $this = shift;
Subroutine __ANON__: not called CC=12 SCAR=50.5
1088 0 my $lsocket = shift;
1089 0 my $handler = shift;
1090 0 my $maxchildren = $this->get_max_interpreters();
1091  
1092 # Decrement child count on child exit.
1093 0 local $SIG{CHLD} = $sigchld;
1094  
1095 0 ▶ my $siginfo = exists($SIG{INFO}) ? 'INFO' : 'USR1';
Branch: 1 branch
BranchTrueFalseTotal
exists $SIG{'INFO'} ? : 0 0 0
1096 local $SIG{$siginfo} = sub {
1097 0 ▶ warn "Number of active children: $nchildren\n";
Subroutine __ANON__: not called CC=1 SCAR=6.9
1098 0 };
1099  
1100 0 while (1) {
1101 0 my $socket = $lsocket->accept();
1102 0 ▶ next if !$socket;
Branch: 1 branch
BranchTrueFalseTotal
unless $socket 0 0 0
1103  
1104 0 ▶ warn "$$: incoming connection\n" if ($DEBUG > 0);
Branch: 1 branch
BranchTrueFalseTotal
if $Sendmail::PMilter::DEBUG > 0 0 0 0
1105  
1106 # If the load's too high, fail and go back to top of loop.
1107 0 my $paused = undef;
1108 0 ▶ while ($maxchildren) {
Branch: 1 branch
BranchTrueFalseTotal
if ($maxchildren) 0 0 0
1109 0 my $cnchildren = $nchildren; # make constant
1110  
1111 0 ▶ if ($cnchildren >= $maxchildren) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cnchildren >= $maxchildren) { } 0 0 0
1112 0 warn "pausing for high load: children $cnchildren >= max $maxchildren";
1113 0 ▶ if ( ! $paused ) { $paused = time(); }
Branch: 1 branch
BranchTrueFalseTotal
unless ($paused) 0 0 0
1114 0 pause();
1115 }
1116 else {
1117 0 last;
1118 }
1119 }
1120 0 ▶ if ($paused) {
Branch: 1 branch
BranchTrueFalseTotal
if ($paused) 0 0 0
1121 0 warn sprintf( "paused for %.1f seconds due to high load", time() - $paused );
1122 }
1123  
1124 0 my $pid = fork();
1125  
1126 0 ▶ if ($pid < 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($pid < 0) { } 0 0 0
elsif ($pid) { } 0 0 0
1127 0 die "fork: $!\n";
1128 } elsif ($pid) {
1129 0 $nchildren++;
1130 0 ▶ $socket->close() if defined($socket);
Branch: 1 branch
BranchTrueFalseTotal
if defined $socket 0 0 0
1131 } else {
1132 0 $lsocket->close();
1133 0 undef $lsocket;
1134 0 undef $@;
1135 0 $SIG{PIPE} = 'IGNORE'; # so close_callback will be reached
1136 0 $SIG{CHLD} = 'DEFAULT';
1137 0 $SIG{$siginfo} = 'DEFAULT';
1138  
1139 0 &$handler($socket);
1140 0 ▶ $socket->close() if defined($socket);
Branch: 1 branch
BranchTrueFalseTotal
if defined $socket 0 0 0
1141 0 exit 0;
1142 }
1143 }
1144 0 };
1145 }
1146  
1147 =pod
1148  
1149 =item Sendmail::PMilter::sequential_dispatcher()
1150  
1151 =item (environment) PMILTER_DISPATCHER=sequential
1152  
1153 June 2019: This dispatcher has not been tested adequately.
1154  
1155 The C<sequential> dispatcher forces one request to be served at a time,
1156 making other requests wait on the socket for the next pass through the loop.
1157 This is not suitable for most production installations, but may be quite
1158 useful for milter debugging or other software development purposes.
1159  
1160 Note that, because the default socket backlog is 5 connections, if you
1161 use this dispatcher it may be wise to increase this backlog by calling
1162 C<set_listen()> before entering C<main()>.
1163  
1164 =cut
1165  
1166 sub sequential_dispatcher () {
1167 sub {
1168 0 ▶ my $this = shift;
Subroutine __ANON__: not called CC=3 SCAR=24.8
1169 0 my $lsocket = shift;
1170 0 my $handler = shift;
1171 0 local $SIG{PIPE} = 'IGNORE'; # so close_callback will be reached
1172  
1173 0 while (1) {
1174 0 my $socket = $lsocket->accept();
1175 0 ▶ next if $!{EINTR};
Branch: 1 branch
BranchTrueFalseTotal
if $!{"EINTR"} 0 0 0
1176  
1177 0 ▶ warn "$$: incoming connection\n" if ($DEBUG > 0);
Branch: 1 branch
BranchTrueFalseTotal
if $Sendmail::PMilter::DEBUG > 0 0 0 0
1178  
1179 0 &$handler($socket);
1180 0 $socket->close();
1181 }
1182 0 ▶ };
Subroutine sequential_dispatcher: not called CC=1 SCAR=6.9
Pod documented
1183 }
1184  
1185 1 1;
1186 __END__
« PMilter/Context.pm ↑ Summary