PMilter.pm

filter: Statementstmt 45.1% 248 / 549 Branchbran 4.7% 8 / 170 Conditioncond 6.0% 3 / 50 MC/DCmcdc 3.4% 1 / 29 Subroutinesub 73.0% 76 / 104 Podpod 94.7% 18 / 19 totaltotal 38.4% 354 / 921 CC 107 SCAR 42.7
CC
107
Cov
34%
setconn
342.0
get_sendmail_class
156.0
__ANON__
156.0
CRAP 71.2
« PMilter/Context.pm ↑ Summary
1 =pod
2  
3 =head1 LICENSE
4  
5 Copyright (c) 2016-2026 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.28';
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 our @smflags = eval "qw/ $smflags /;";
246 1 our @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 our %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 1 ▶sub new ($) {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
272 1 bless {}, shift;
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 0 ▶sub get_max_interpreters ($) {
Subroutine get_max_interpreters: not called CC=2 SCAR=17.9
Pod documented
288 0 my $this = shift;
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 0 ▶sub get_max_requests ($) {
Subroutine get_max_requests: not called CC=2 SCAR=17.9
Pod documented
304 0 my $this = shift;
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 0 ▶sub main ($;$$$) {
Subroutine main: not called CC=10 SCAR=47.0
Pod documented
336 0 require Sendmail::PMilter::Context;
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 0 ▶ my $handler = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
357 0 my $ctx = new Sendmail::PMilter::Context(shift, $this->{callbacks}, $this->{callback_flags}, $this->{'milter protocol version'});
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 0 ▶sub register ($$$;$) {
Subroutine register: not called CC=11 SCAR=48.8
Pod documented
412 0 my $this = shift;
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[, PERMS])
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 =item PERMS
479  
480 Optional permissions mask.
481  
482 =back
483  
484 Returns a true value on success, undef on failure.
485  
486 =cut
487  
488 0 ▶sub setconn ($$) {
Subroutine setconn: not called CC=18 SCAR=58.3
Pod documented
489 0 my $this = shift;
490 0 my $conn = shift;
491 0 my $perms = shift;
492 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
493 0 my $socket;
494  
495 0 ▶ croak "setconn: $conn: unspecified protocol"
Branch: 1 branch
BranchTrueFalseTotal
unless $conn =~ /^([^:]+):([^:\@]+)(?:@([^:\@]+|\[[0-9a-f:\.]+\]))?$/u 0 0 0
496 unless ($conn =~ /^([^:]+):([^:@]+)(?:@([^:@]+|\[[0-9a-f:\.]+\]))?$/);
497  
498 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
499 0 require IO::Socket::UNIX;
500  
501 0 my $path = $2;
502 0 my $addr = sockaddr_un($path);
503 0 my $oldumask = umask;
504  
505 0 ▶ croak "setconn: $conn: path not absolute"
Branch: 1 branch
BranchTrueFalseTotal
unless $path =~ m[^/]u 0 0 0
506 unless ($path =~ m,^/,,);
507  
508 0 ▶ if ($perms)
Branch: 1 branch
BranchTrueFalseTotal
if ($perms) 0 0 0
509 {
510 0 umask 0777 - $perms;
511 }
512  
513 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
514 0 $! = Errno::EEXIST;
515 } else {
516 0 $socket = IO::Socket::UNIX->new(Type => SOCK_STREAM);
517 }
518  
519 # Some systems require you to unlink an orphaned inode.
520 # There's a race condition here, but it's unfortunately
521 # not easily fixable. Using an END{} block doesn't
522 # always work, and that's too wonky with fork() anyway.
523  
524 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
525 0 ▶ if ($socket->connect($addr)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($socket->connect($addr)) { } 0 0 0
526 0 close $socket;
527 0 undef $socket;
528 0 $! = Errno::EADDRINUSE;
529 } else {
530 0 unlink $path; # race condition
531 0 ▶ $socket->bind($addr) || undef $socket;
Branch: 1 branch
BranchTrueFalseTotal
$socket->bind($addr) or undef $socket 0 0 0
532 }
533 }
534  
535 0 umask $oldumask;
536
537 0 ▶ if (defined($socket)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $socket) 0 0 0
538 0 ▶ $socket->listen($backlog) || croak "setconn: listen $conn: $!";
Branch: 1 branch
BranchTrueFalseTotal
$socket->listen($backlog) or croak("setconn: listen ${conn}: $!") 0 0 0
539 }
540 } elsif ($1 eq 'inet') {
541 0 require IO::Socket::INET;
542  
543 0 $socket = IO::Socket::INET->new(
544 Proto => 'tcp',
545 ReuseAddr => 1,
546 Listen => $backlog,
547 LocalPort => $2,
548 LocalAddr => $3
549 );
550 } elsif ($1 eq 'inet6') {
551 0 require IO::Socket::IP;
552  
553 0 $socket = IO::Socket::IP->new(
554 Proto => 'tcp',
555 ReuseAddr => 1,
556 Listen => $backlog,
557 LocalService => $2,
558 LocalHost => $3
559 );
560 } else {
561 0 croak "setconn: $conn: unknown protocol";
562 }
563  
564 0 ▶ if (defined($socket)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $socket) { } 0 0 0
565 0 $this->set_socket($socket);
566 } else {
567 0 carp "setconn: $conn: $!";
568 0 undef;
569 }
570 }
571  
572 =pod
573  
574 =item set_dispatcher(CODEREF)
575  
576 Sets the dispatcher used to accept socket connections and hand them off to
577 the protocol engine. This allows pluggable resource allocation so that the
578 milter script may use fork, threads, or any other such means of handling
579 milter connections. See C<DISPATCHERS> below for more information.
580  
581 The subroutine (code) reference will be called by C<main()> when the
582 listening socket object is prepared and ready to accept connections. It
583 will be passed the arguments:
584  
585 MILTER, LSOCKET, HANDLER
586  
587 MILTER is the milter object currently running. LSOCKET is a listening
588 socket (an instance of C<IO::Socket>), upon which C<accept()> should be
589 called. HANDLER is a subroutine reference which should be called, passing
590 the socket object returned by C<< LSOCKET->accept() >>.
591  
592 Note that the dispatcher may also be set from one of the off-the-shelf
593 dispatchers noted in this document by setting the PMILTER_DISPATCHER
594 environment variable. See C<DISPATCHERS>, below.
595  
596 =cut
597  
598 0 ▶sub set_dispatcher($&) {
Subroutine set_dispatcher: not called CC=1 SCAR=6.9
Pod documented
599 0 my $this = shift;
600  
601 0 $this->{dispatcher} = shift;
602 0 1;
603 }
604  
605 =pod
606  
607 =item set_listen(BACKLOG)
608  
609 Set the socket listen backlog to BACKLOG. The default is 5 connections if
610 not set explicitly by this method. Only useful before calling C<main()>.
611  
612 =cut
613  
614 0 ▶sub set_listen ($$) {
Subroutine set_listen: not called CC=2 SCAR=17.9
Pod documented
615 0 my $this = shift;
616 0 my $backlog = shift;
617  
618 0 ▶ croak 'set_listen: socket already bound' if defined($this->{socket});
Branch: 1 branch
BranchTrueFalseTotal
if defined $this->{"socket"} 0 0 0
619  
620 0 $this->{backlog} = $backlog;
621 0 1;
622 }
623  
624 =pod
625  
626 =item set_socket(SOCKET)
627  
628 Rather than calling C<setconn()>, this method may be called explicitly to
629 set the C<IO::Socket> instance used to accept inbound connections.
630  
631 =cut
632  
633 0 ▶sub set_socket ($$) {
Subroutine set_socket: not called CC=3 SCAR=24.8
Pod documented
634 0 my $this = shift;
635 0 my $socket = shift;
636  
637 0 ▶ croak 'set_socket: socket already bound' if defined($this->{socket});
Branch: 1 branch
BranchTrueFalseTotal
if defined $this->{"socket"} 0 0 0
638 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
639  
640 0 $this->{socket} = $socket;
641 0 1;
642 }
643  
644 =pod
645  
646 =back
647  
648 =head1 SENDMAIL-SPECIFIC METHODS
649  
650 The following methods are only useful if Sendmail is the MTA connecting to
651 this milter. Other MTAs likely don't use Sendmail's configuration file, so
652 these methods would not be useful with them.
653  
654 =over 4
655  
656 =item auto_getconn(NAME[, CONFIG])
657  
658 Returns the connection descriptor for milter NAME in Sendmail configuration
659 file CONFIG (default C</etc/mail/sendmail.cf> or whatever was set by
660 C<set_sendmail_cf()>). This can then be passed to setconn(), below.
661  
662 Returns a true value on success, undef on failure.
663  
664 =cut
665  
666 5 ▶sub auto_getconn ($$;$) {
Subroutine auto_getconn: called CC=9 SCAR=24.4
Pod documented
667 5 my $this = shift;
668 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
669 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
670 4 local *CF;
671  
672 4 ▶ open(CF, '<'.$cf) || die "open $cf: $!";
Branch: 1 branch
BranchTrueFalseTotal
open CF, '<' . $cf or die "open ${cf}: $!" 4 0 4
673  
674 4 ▶ while (<CF>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline CF)) 4 0 4
675 4 s/\s+$//; # also trims newlines
676  
677 4 ▶ s/^X([^,\s]+),\s*// || next;
Branch: 1 branch
BranchTrueFalseTotal
s/^X([^,\s]+),\s*//u or next 4 0 4
678 4 ▶ ($milter eq $1) || next;
Branch: 1 branch
BranchTrueFalseTotal
$milter eq $1 or next 4 0 4
679  
680 4 ▶ while (s/^(.)=([^,\s]+)(,\s*|\Z)//) {
Branch: 1 branch
BranchTrueFalseTotal
if (s/^(.)=([^,\s]+)(,\s*|\Z)//u) 4 0 4
681 4 ▶ if ($1 eq 'S') {
Branch: 1 branch
BranchTrueFalseTotal
if ($1 eq "S") 4 0 4
682 4 close(CF);
683 4 return $2;
684 }
685 }
686 }
687  
688 0 close(CF);
689 0 undef;
690 }
691  
692 =pod
693  
694 =item auto_setconn(NAME[, CONFIG])
695  
696 Creates the server connection socket for milter NAME in Sendmail
697 configuration file CONFIG.
698  
699 Essentially, does:
700  
701 $milter->setconn($milter->auto_getconn(NAME, CONFIG))
702  
703 Returns a true value on success, undef on failure.
704  
705 =cut
706  
707 0 ▶sub auto_setconn ($$;$) {
Subroutine auto_setconn: not called CC=2 SCAR=17.9
Pod documented
708 0 my $this = shift;
709 0 my $name = shift;
710 0 my $conn = $this->auto_getconn($name, shift);
711  
712 0 ▶ if (defined($conn)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $conn) { } 0 0 0
713 0 $this->setconn($conn);
714 } else {
715 0 carp "auto_setconn: no connection for $name found";
716 0 undef;
717 }
718 }
719  
720 =pod
721  
722 =item get_sendmail_cf()
723  
724 Returns the pathname of the Sendmail configuration file. If this has
725 been set by C<set_sendmail_cf()>, then that is the value returned.
726 Otherwise the default pathname C</etc/mail/sendmail.cf> is returned.
727  
728 =cut
729  
730 7 ▶sub get_sendmail_cf ($) {
Subroutine get_sendmail_cf: called CC=2 SCAR=6.9
Pod documented
731 7 my $this = shift;
732  
733 7 ▶ $this->{sendmail_cf} || '/etc/mail/sendmail.cf';
Branch: 1 branch
BranchTrueFalseTotal
$this->{"sendmail_cf"} or "/etc/mail/sendmail.cf" 5 2 7
734 }
735  
736 =pod
737  
738 =item get_sendmail_class(CLASS[, CONFIG])
739  
740 Returns a list containing all members of the Sendmail class CLASS, in
741 Sendmail configuration file CONFIG (default C</etc/mail/sendmail.cf> or
742 whatever is set by C<set_sendmail_cf()>). Typically this is used to look up
743 the entries in class "w", the local hostnames class.
744  
745 =cut
746  
747 0 ▶sub get_sendmail_class ($$;$) {
Subroutine get_sendmail_class: not called CC=12 SCAR=50.5
Pod documented
748 0 my $this = shift;
749 0 my $class = shift;
750 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
751 0 my %entries;
752 0 local *CF;
753  
754 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
755  
756 0 ▶ while (<CF>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline CF)) 0 0 0
757 0 s/\s+$//; # also trims newlines
758  
759 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
760 0 foreach (split(/\s+/)) {
761 0 $entries{$_} = 1;
762 }
763 } elsif (s/^F\s*$class\s*(-o)?\s*//) {
764 0 my $required = !defined($1);
765 0 local *I;
766  
767 0 ▶ croak "get_sendmail_class: class $class lookup resulted in pipe: $_" if (/^\|/);
Branch: 1 branch
BranchTrueFalseTotal
if /^\|/u 0 0 0
768  
769 0 ▶ if (open(I, '<'.$_)) {
Branch: 2 branches
BranchTrueFalseTotal
if (open I, '<' . $_) { } 0 0 0
elsif ($required) { } 0 0 0
770 0 ▶ while (<I>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline I)) 0 0 0
771 0 s/#.*$//;
772 0 s/\s+$//;
773 0 ▶ next if /^$/;
Branch: 1 branch
BranchTrueFalseTotal
if /^$/u 0 0 0
774 0 $entries{$_} = 1;
775 }
776 0 close(I);
777 } elsif ($required) {
778 0 croak "get_sendmail_class: class $class lookup: $_: $!";
779 }
780 }
781 }
782  
783 0 close(CF);
784 0 keys %entries;
785 }
786  
787 =pod
788  
789 =item get_sendmail_option(OPTION[, CONFIG])
790  
791 Returns a list containing the first occurrence of Sendmail option
792 OPTION in Sendmail configuration file CONFIG (default C</etc/mail/sendmail.cf>,
793 or whatever has been set by C<set_sendmail_cf()>). Returns the
794 value of the option or undef if it is not found. This can be used
795 to learn configuration parameters such as Milter.maxdatasize.
796  
797 =cut
798  
799 0 ▶sub get_sendmail_option ($$;$) {
Subroutine get_sendmail_option: not called CC=5 SCAR=34.0
Pod documented
800 0 my $this = shift;
801 0 my $option = shift;
802 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
803 0 my %entries;
804 0 local *CF;
805 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
806 0 ▶ while (<CF>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline CF)) 0 0 0
807 0 s/\s+$//; # also trims newlines
808 0 ▶ if (/^O\s*$option=(\d+)/) { return $1; }
Branch: 1 branch
BranchTrueFalseTotal
if (/^O\s*$option=(\d+)/u) 0 0 0
809 }
810 0 close(CF);
811 0 undef;
812 }
813  
814 =pod
815  
816 =item set_sendmail_cf(FILENAME)
817  
818 Set the default filename used by C<auto_getconn>, C<auto_setconn>, and
819 C<sendmail_class> to find Sendmail-specific configuration data. If not
820 explicitly set by this method, it defaults to C</etc/mail/sendmail.cf>.
821 Returns 1.
822  
823 =cut
824  
825 7 ▶sub set_sendmail_cf ($) {
Subroutine set_sendmail_cf: called CC=1 SCAR=0.0
Pod documented
826 7 my $this = shift;
827  
828 7 $this->{sendmail_cf} = shift;
829 7 1;
830 }
831  
832 ### off-the-shelf dispatchers
833  
834 =pod
835  
836 =back
837  
838 =head1 DISPATCHERS
839  
840 Milter requests may be dispatched to the protocol handler in a pluggable
841 manner (see the description for the C<set_dispatcher()> method above).
842 C<Sendmail::PMilter> offers some off-the-shelf dispatchers that use
843 different methods of resource allocation.
844  
845 Each of these is referenced as a non-object function, and return a value
846 that may be passed directly to C<set_dispatcher()>.
847  
848 =over 4
849  
850 =item Sendmail::PMilter::ithread_dispatcher()
851  
852 =item (environment) PMILTER_DISPATCHER=ithread
853  
854 June 2019: This dispatcher has not been tested adequately.
855  
856 The C<ithread> dispatcher spins up a new thread upon each connection to
857 the milter socket. This provides a thread-based model that may be more
858 resource efficient than the similar C<postfork> dispatcher. This requires
859 that the Perl interpreter be compiled with C<-Duseithreads>, and uses the
860 C<threads> module (available on Perl 5.8 or later only).
861  
862 =cut
863  
864 0 ▶sub ithread_dispatcher {
Subroutine ithread_dispatcher: not called CC=1 SCAR=6.9
Pod documented
865 0 require threads;
866 0 require threads::shared;
867 0 require Thread::Semaphore;
868  
869 0 my $nchildren = 0;
870  
871 0 threads::shared::share($nchildren);
872  
873 0 ▶ sub {
Subroutine __ANON__: not called CC=8 SCAR=42.8
874 0 my $this = shift;
875 0 my $lsocket = shift;
876 0 my $handler = shift;
877 0 my $maxchildren = $this->get_max_interpreters();
878 0 my $child_sem;
879  
880 0 ▶ if ($maxchildren) {
Branch: 1 branch
BranchTrueFalseTotal
if ($maxchildren) 0 0 0
881 0 $child_sem = Thread::Semaphore->new($maxchildren);
882 }
883
884 0 ▶ my $siginfo = exists($SIG{INFO}) ? 'INFO' : 'USR1';
Branch: 1 branch
BranchTrueFalseTotal
exists $SIG{'INFO'} ? : 0 0 0
885 0 ▶ local $SIG{$siginfo} = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
886 0 warn "Number of active children: $nchildren\n";
887 0 };
888  
889 0 ▶ my $child_sub = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
890 0 my $socket = shift;
891  
892 0 eval {
893 0 &$handler($socket);
894 0 $socket->close();
895 };
896 0 my $died = $@;
897  
898 0 lock($nchildren);
899 0 $nchildren--;
900 0 ▶ if ($child_sem) {
Branch: 1 branch
BranchTrueFalseTotal
if ($child_sem) 0 0 0
901 0 $child_sem->up();
902 }
903 0 ▶ warn $died if $died;
Branch: 1 branch
BranchTrueFalseTotal
if $died 0 0 0
904 0 };
905  
906 0 while (1) {
907 0 my $socket = $lsocket->accept();
908 0 ▶ next if $!{EINTR};
Branch: 1 branch
BranchTrueFalseTotal
if $!{"EINTR"} 0 0 0
909  
910 0 ▶ warn "$$: incoming connection\n" if ($DEBUG > 0);
Branch: 1 branch
BranchTrueFalseTotal
if $Sendmail::PMilter::DEBUG > 0 0 0 0
911  
912 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
913 0 warn "pausing for high load: children $nchildren >= max $maxchildren";
914 0 my $start = time();
915 0 $child_sem->down();
916 0 my $end = time();
917 0 warn sprintf("paused for %.1f seconds due to high load", $end - $start);
918 }
919  
920 # scoping block for lock()
921 {
922 0 lock($nchildren);
923 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
924 0 $t->detach;
925 0 threads->yield();
926 0 $nchildren++;
927 }
928 }
929 0 };
930 }
931  
932 =pod
933  
934 =item Sendmail::PMilter::prefork_dispatcher([PARAMS])
935  
936 =item (environment) PMILTER_DISPATCHER=prefork
937  
938 June 2019: This dispatcher has been tested extensively by the maintainer.
939  
940 The C<prefork> dispatcher forks the main Perl process before accepting
941 connections, and uses the main process to monitor the children. This
942 should be appropriate for steady traffic flow sites. Note that if
943 MAXINTERP is not set in the call to C<main()> or in PARAMS, an internal
944 default of 10 processes will be used; similarly, if MAXREQ is not set, 100
945 requests will be served per child.
946  
947 Currently the child process pool is fixed in size: discarded children will
948 be replaced immediately.
949  
950 PARAMS, if specified, is a hash of key-value pairs defining parameters for
951 the dispatcher. The available parameters that may be set are:
952  
953 =over 2
954  
955 =item child_init
956  
957 subroutine reference that will be called after each child process is forked.
958 It will be passed the C<MILTER> object.
959  
960 =item child_exit
961  
962 subroutine reference that will be called just before each child process
963 terminates. It will be passed the C<MILTER> object plus current requests
964 handled and maximum requests per child.
965  
966 =item milter_exit
967  
968 subroutine reference that will be called just before the milter
969 terminates. It will be passed the C<MILTER> object.
970  
971 =item max_children
972  
973 Maximum number of child processes active at any time. Equivalent to the
974 MAXINTERP option to main() -- if not set in the main() call, this value
975 will be used.
976  
977 =item max_requests_per_child
978  
979 Maximum number of requests a child process may service before being
980 recycled. Equivalent to the MAXREQ option to main() -- if not set in the
981 main() call, this value will be used.
982  
983 =back
984  
985 =cut
986  
987 0 ▶sub prefork_dispatcher (@) {
Subroutine prefork_dispatcher: not called CC=1 SCAR=6.9
Pod documented
988 0 my %params = @_;
989 0 my %children;
990 my $curr_requests;
991 0 my $max_requests;
992  
993 0 ▶ my $child_dispatcher = sub {
Subroutine __ANON__: not called CC=9 SCAR=45.0
994 0 my $this = shift;
995 0 my $lsocket = shift;
996 0 my $handler = shift;
997 0 ▶ $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
998 0 $curr_requests = 0;
999  
1000 0 local $SIG{PIPE} = 'IGNORE'; # so close_callback will be reached
1001  
1002 0 ▶ my $siginfo = exists($SIG{INFO}) ? 'INFO' : 'USR1';
Branch: 1 branch
BranchTrueFalseTotal
exists $SIG{'INFO'} ? : 0 0 0
1003 0 ▶ local $SIG{$siginfo} = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
1004 0 warn "$$: requests handled: $curr_requests\n";
1005 0 };
1006  
1007 # call child_init handler if present
1008 0 ▶ if (defined $params{child_init}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params{"child_init"}) 0 0 0
1009 0 my $method = $params{child_init};
1010 0 $this->$method();
1011 }
1012 0 eval {
1013 0 ▶ while ($curr_requests < $max_requests) {
Branch: 1 branch
BranchTrueFalseTotal
if ($curr_requests < $max_requests) 0 0 0
1014 0 my $socket = $lsocket->accept();
1015 0 ▶ next if $!{EINTR};
Branch: 1 branch
BranchTrueFalseTotal
if $!{"EINTR"} 0 0 0
1016 0 ▶ warn "$$: incoming connection\n" if ($DEBUG > 0);
Branch: 1 branch
BranchTrueFalseTotal
if $Sendmail::PMilter::DEBUG > 0 0 0 0
1017 0 $curr_requests++;
1018 0 &$handler($socket);
1019 0 $socket->close();
1020 }
1021 };
1022 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
1023 0 warn "Exiting cause die";
1024 }
1025 0 };
1026  
1027 # Propagate some signals down to the entire process group.
1028 0 ▶ my $killall = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
1029 0 my $sig = shift;
1030 0 my $this = $_[0];
1031  
1032 # call milter_exit handler if present
1033 0 ▶ if (defined $params{milter_exit}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params{"milter_exit"}) 0 0 0
1034 0 my $method = $params{milter_exit};
1035 0 $this->$method();
1036 }
1037 0 kill 'TERM', keys %children;
1038 0 exit 0;
1039 0 };
1040  
1041 0 setpgrp();
1042  
1043 0 ▶ sub {
Subroutine __ANON__: not called CC=8 SCAR=42.8
1044 0 my $this = $_[0];
1045 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
1046  
1047 0 $SIG{INT} = $killall;
1048 0 $SIG{QUIT} = $killall;
1049 0 $SIG{TERM} = $killall;
1050  
1051 0 while (1) {
1052 0 ▶ while (scalar keys %children < $maxchildren) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %children < $maxchildren) 0 0 0
1053 0 my $pid = fork();
1054 0 ▶ die "fork: $!" unless defined($pid);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pid 0 0 0
1055  
1056 0 ▶ if ($pid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid) { } 0 0 0
1057 0 $children{$pid} = 1;
1058 } else {
1059 # setup child_exit handler if present
1060 0 ▶ if (defined $params{child_exit}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params{'child_exit'}) { } 0 0 0
1061 # INTR signal usually invoked by CTRL + C
1062 # Don't do anything in child and let's parent to
1063 # signal children with TERM
1064 0 $SIG{INT} = 'IGNORE';
1065  
1066 # QUIT and TERM should terminate the children but
1067 # parent also sends TERM to all children (dups are
1068 # possible. If using systemd, consier using:
1069 # KillMode=mixed
1070 # As workaround we will restore these signals to its
1071 # default, avoiding dup execution.
1072 0 ▶ $SIG{QUIT} = $SIG{TERM} = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
1073 0 my $sig_name = shift;
1074  
1075 0 $SIG{QUIT} = $SIG{TERM} = 'DEFAULT';
1076 0 my $method = $params{child_exit};
1077 0 $this->$method($curr_requests, $max_requests);
1078  
1079 # If signal is QUIT, core dump must be issued.
1080 # As we now have set it to default, simply call it.
1081 0 ▶ if ($sig_name eq 'QUIT') {
Branch: 1 branch
BranchTrueFalseTotal
if ($sig_name eq "QUIT") 0 0 0
1082 0 kill 'QUIT', $$;
1083 }
1084 0 exit;
1085 0 };
1086 } else {
1087 # Perl reset these to IGNORE. Set to defaults.
1088 0 $SIG{INT} = 'DEFAULT';
1089 0 $SIG{QUIT} = 'DEFAULT';
1090 0 $SIG{TERM} = 'DEFAULT';
1091 }
1092 0 &$child_dispatcher(@_);
1093  
1094 # curr_requests = max_requests
1095 0 kill 'TERM', $$;
1096 }
1097 }
1098  
1099 # Wait for a pid to exit, then loop back up to fork.
1100 0 my $pid = wait();
1101 0 ▶ delete $children{$pid} if ($pid > 0);
Branch: 1 branch
BranchTrueFalseTotal
if $pid > 0 0 0 0
1102 }
1103 0 };
1104 }
1105  
1106 =pod
1107  
1108 =item Sendmail::PMilter::postfork_dispatcher()
1109  
1110 =item (environment) PMILTER_DISPATCHER=postfork
1111  
1112 June 2019: This dispatcher has not been tested adequately.
1113  
1114 This is the default dispatcher for PMilter if no explicit dispatcher is set.
1115  
1116 The C<postfork> dispatcher forks the main Perl process upon each connection
1117 to the milter socket. This is adequate for machines that get bursty but
1118 otherwise mostly idle mail traffic, as the idle-time resource consumption is
1119 very low.
1120  
1121 If the maximum number of interpreters is running when a new connection
1122 comes in, this dispatcher blocks until a slot becomes available for a
1123 new interpreter.
1124  
1125 =cut
1126  
1127 0 ▶sub postfork_dispatcher () {
Subroutine postfork_dispatcher: not called CC=1 SCAR=6.9
Pod documented
1128 0 my $nchildren = 0;
1129 0 my $sigchld;
1130  
1131 0 ▶ $sigchld = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
1132 0 my $pid;
1133 0 ▶ $nchildren-- while (($pid = waitpid(-1, WNOHANG)) > 0);
Branch: 1 branch
BranchTrueFalseTotal
($pid = waitpid(-1, 1)) > 0 and do { --$nchildren } 0 0 0
1134 0 $SIG{CHLD} = $sigchld;
1135 0 };
1136  
1137 0 ▶ sub {
Subroutine __ANON__: not called CC=12 SCAR=50.5
1138 0 my $this = shift;
1139 0 my $lsocket = shift;
1140 0 my $handler = shift;
1141 0 my $maxchildren = $this->get_max_interpreters();
1142  
1143 # Decrement child count on child exit.
1144 0 local $SIG{CHLD} = $sigchld;
1145  
1146 0 ▶ my $siginfo = exists($SIG{INFO}) ? 'INFO' : 'USR1';
Branch: 1 branch
BranchTrueFalseTotal
exists $SIG{'INFO'} ? : 0 0 0
1147 0 ▶ local $SIG{$siginfo} = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
1148 0 warn "Number of active children: $nchildren\n";
1149 0 };
1150  
1151 0 while (1) {
1152 0 my $socket = $lsocket->accept();
1153 0 ▶ next if !$socket;
Branch: 1 branch
BranchTrueFalseTotal
unless $socket 0 0 0
1154  
1155 0 ▶ warn "$$: incoming connection\n" if ($DEBUG > 0);
Branch: 1 branch
BranchTrueFalseTotal
if $Sendmail::PMilter::DEBUG > 0 0 0 0
1156  
1157 # If the load's too high, fail and go back to top of loop.
1158 0 my $paused = undef;
1159 0 ▶ while ($maxchildren) {
Branch: 1 branch
BranchTrueFalseTotal
if ($maxchildren) 0 0 0
1160 0 my $cnchildren = $nchildren; # make constant
1161  
1162 0 ▶ if ($cnchildren >= $maxchildren) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cnchildren >= $maxchildren) { } 0 0 0
1163 0 warn "pausing for high load: children $cnchildren >= max $maxchildren";
1164 0 ▶ if ( ! $paused ) { $paused = time(); }
Branch: 1 branch
BranchTrueFalseTotal
unless ($paused) 0 0 0
1165 0 pause();
1166 }
1167 else {
1168 0 last;
1169 }
1170 }
1171 0 ▶ if ($paused) {
Branch: 1 branch
BranchTrueFalseTotal
if ($paused) 0 0 0
1172 0 warn sprintf( "paused for %.1f seconds due to high load", time() - $paused );
1173 }
1174  
1175 0 my $pid = fork();
1176  
1177 0 ▶ if ($pid < 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($pid < 0) { } 0 0 0
elsif ($pid) { } 0 0 0
1178 0 die "fork: $!\n";
1179 } elsif ($pid) {
1180 0 $nchildren++;
1181 0 ▶ $socket->close() if defined($socket);
Branch: 1 branch
BranchTrueFalseTotal
if defined $socket 0 0 0
1182 } else {
1183 0 $lsocket->close();
1184 0 undef $lsocket;
1185 0 undef $@;
1186 0 $SIG{PIPE} = 'IGNORE'; # so close_callback will be reached
1187 0 $SIG{CHLD} = 'DEFAULT';
1188 0 $SIG{$siginfo} = 'DEFAULT';
1189  
1190 0 &$handler($socket);
1191 0 ▶ $socket->close() if defined($socket);
Branch: 1 branch
BranchTrueFalseTotal
if defined $socket 0 0 0
1192 0 exit 0;
1193 }
1194 }
1195 0 };
1196 }
1197  
1198 =pod
1199  
1200 =item Sendmail::PMilter::sequential_dispatcher()
1201  
1202 =item (environment) PMILTER_DISPATCHER=sequential
1203  
1204 June 2019: This dispatcher has not been tested adequately.
1205  
1206 The C<sequential> dispatcher forces one request to be served at a time,
1207 making other requests wait on the socket for the next pass through the loop.
1208 This is not suitable for most production installations, but may be quite
1209 useful for milter debugging or other software development purposes.
1210  
1211 Note that, because the default socket backlog is 5 connections, if you
1212 use this dispatcher it may be wise to increase this backlog by calling
1213 C<set_listen()> before entering C<main()>.
1214  
1215 =cut
1216  
1217 0 ▶sub sequential_dispatcher () {
Subroutine sequential_dispatcher: not called CC=1 SCAR=6.9
Pod documented
1218 0 ▶ sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
1219 0 my $this = shift;
1220 0 my $lsocket = shift;
1221 0 my $handler = shift;
1222 0 local $SIG{PIPE} = 'IGNORE'; # so close_callback will be reached
1223  
1224 0 while (1) {
1225 0 my $socket = $lsocket->accept();
1226 0 ▶ next if $!{EINTR};
Branch: 1 branch
BranchTrueFalseTotal
if $!{"EINTR"} 0 0 0
1227  
1228 0 ▶ warn "$$: incoming connection\n" if ($DEBUG > 0);
Branch: 1 branch
BranchTrueFalseTotal
if $Sendmail::PMilter::DEBUG > 0 0 0 0
1229  
1230 0 &$handler($socket);
1231 0 $socket->close();
1232 }
1233 0 };
1234 }
1235  
1236 1 1;
1237 __END__
« PMilter/Context.pm ↑ Summary