Debian Codegraph @

ABOUT

raw
1 October 2022.
2
3 1. Plug-in filters.
4
5 Sendmail and other Mail Transfer Agents provide ways to 'plug in' a
6 filter or filters to process mail. Such filters are called milters.
7
8 The things which a milter can do for an MTA are limited mainly by the
9 imagination of the milter's author.
10
11
12 2. The MTA - milter interface, and languages for writing milters.
13
14 An interface is needed between a milter and the MTA which it assists.
15 Milters can be written in a number of different programming languages;
16 sometimes mixed languages are used.
17
18 The Sendmail::PMilter package provides an interface between the MTA
19 and milters which may be written in pure Perl. This package handles
20 all inter-process communications between the MTA and the milter, it
21 exposes the information which is provided to the milter by the MTA as
22 ordinary Perl variables, and it handles the tasks of converting Perl
23 variables in the milter which contain things like function arguments
24 and reply codes into a form which the MTA can digest, as well as, in
25 certain cases, checking for validity. All this greatly simplifies the
26 task of writing a milter in Perl. If milter processing should happen
27 to produce errors, SIG{__WARN__} and SIG{__DIE__} can be used to catch
28 (and for example log, or even ignore) the errors, so that the milter
29 doesn't exit and mail continues to flow,
30
31
32 3. Uses for milters.
33
34 A common use of milters is to reject unwanted mail. Others include
35
36 adding 'boilerplate' text, images, whatever to all messages;
37 signing outgoing mail, signature verification for incoming mail;
38 encrypting/decrypting mail;
39 adding, deleting, inspecting, and/or modifying message headers;
40 automatic replies;
41 preventing accidental disclosure of confidential information e.g.
42 credit card, social security and bank account numbers;
43 converting mail text to other formats;
44 copying all mail to backup storage;
45 rate or size limiting;
46 collecting and reporting statistics;
47 you can probably think of others.
48
49
50 4. Milter capabilities.
51
52 The interface to the running MTA follows the 'Milter Protocol'. Over
53 the years the Milter Protocol has progressed through several versions.
54 It seems now to be well settled at Version 6, which supports more or
55 less everything anyone might reasonably want to do to a message.
56
57 Perl milters have full access to Perl libraries, modules etc. and all
58 the facilities and utilities made available by the operating system.
59 Use with caution.
60
61
62 5. Communications between MTA and a milter.
63
64 Inter-process communication can be a bit of a minefield. One of the
65 aiims of this module is to remove all the complexity. All you need to
66 do is to specify the socket which the MTA and a milter will use. Then
67 you can get on with the mail filtering itself withouth worrying about
68 the communication, which is necessary, but secondary to the main task.
69
70 A milter process is normally run as a daemon. It communicates with a
71 Mail Transfer Agent (and MTA - which is normally another daemon) over
72 a socket connection. A single MTA may have more than one milter with
73 which it exchanges information. In the Sendmail Milter Protocol, the
74 MTA sends commands to and receives responses from its milters over a
75 socket connection. The details of the socket are configured in both
76 the configuration of the MTA and in the configurations of the milters
77 and these must of course agree. The socket can be a local Unix domain
78 socket or a TCP socket. For Sendmail, this is defined by a line in
79 the configuration file which begins with an 'X'. Sendmail::PMilter
80 uses this information to set up the necessary communication between
81 Perl milters (which "use" it) and the MTA itself. Because processes
82 which handle mail are at least time-sensitive even if not necessarily
83 time-critical, when it hands over to a milter the MTA sets timeouts
84 and then waits. If a timeout is reached without an expected response
85 from the filter, the MTA may decide to ignore the filter and press on.
86
87 5.1. Sendmail configuration.
88
89 A typical configuration in 'sendmail.cf' might be
90
91 O InputMailFilters=x-milter
92 Xx-milter, S=local:/var/run/x-milter/x-milter.sock, F=T, T=C:10m;S:10m;R:10m;E:20m
93
94 This tells Sendmail that there is one milter called 'x-milter'; its
95 communication will be via a local Unix domain socket; what to do if
96 the milter fails; and some timeout values. You may wish to use the M4
97 processor to create sendmail.cf from 'sendmail.mc', this should do in
98 the '.mc' file:
99
100 INPUT_MAIL_FILTER(`x-milter', `S=local:/var/run/x-milter/x-milter.sock, F=T, T=C:10m;S:10m;R:10m;E:20m')
101
102 5.2. Milter configuration.
103
104 In the milter itself the socket is configured like this:
105
106 $milter_object->setconn('local:/var/run/x-milter/x-milter.sock');
107
108 so that the milter and the MTA are on the same page.
109
110 5.3. The mechanics of MTA - milter communication.
111
112 All messages are passed between the MTA and the milter(s) encoded in
113 simple 'packets'. The packets are in fact (and you do not really need
114 to know this) strings of the very general form "LCS1\0S2\0", where 'L'
115 is a 32-bit integer (the length of the following data, INCLUDING the
116 command byte), 'C' is the command byte (a single ASCII character) and
117 (in this example) S1 and S2 are ASCII text strings each terminated by
118 ASCII NUL characters (which are of course included when calculating
119 the length of the packet).
120
121 Sendmail::PMilter handles all the packet processing, the milter need
122 concern itself only with Perl-style scalar variables, arrays etc. and
123 there's no need to worry about stack bashing, use-after-free, memory
124 leaks, threads, and those other things that 'C' programmers have to
125 wrestle with. This leaves the coder relatively free to concentrate on
126 the mail processing itself.
127
128
129 6. Mail processing takes place in stages.
130
131 Mail processing by milters takes place at several well-defined stages
132 ('connect', 'helo', etc.) during the transmission of the message. At
133 each stage the sets of data available ('macros', connected IP address,
134 sending server's HELO name, sender address etc.) and the permissible
135 actions (e.g. rewrite a message header, add a header, modify message
136 parts) are also well-defined and are to some extent configurable.
137
138 Filters built to use Sendmail::PMilter can examine any information
139 made available by the MTA, and can take any of the actions permitted
140 at any stage of mail processing. Generally the MTA is looking for a
141 result (ACCEPT mail, TEMPFAIL, REJECT, DISCARD, CONTINUE processing)
142 which is returned by the final statement of the milter code for the
143 current SMTP stage. For each SMTP stage there is a separate section
144 of code which is called a 'callback'. Here it's just a Perl sub, it
145 differs from the other subs in the milter in that it is 'registered'
146 with the MTA in a call to Sendmail::PMilter::register() so that the
147 MTA knows how to call it.
148
149 The MTA initiates all communications by a strict sequence of commands
150 sent from the MTA to the milter via the socket; by default the milter
151 must reply, but it is possible to negotiate at the beginning of every
152 connection, for each separate step in the processing if you wish, for
153 the milter to make no reply to the MTA.
154
155 If several milters are used, at each stage of the SMTP conversation
156 the MTA consults each milter (connect, helo, etc.) in turn, the order
157 being defined in the MTA configuration file. If at any stage any one
158 of the milters replies with REJECT then the message will be rejected,
159 the remaining steps for this and other milters will not be completed.
160 If a milter does not wish to be consulted further about a particular
161 message it can reply with ACCEPT. If it has reached no conclusion it
162 can return CONTINUE so that it will be consulted at the next stage of
163 the conversation (assuming that the MTA does not decide to reject or
164 tempfail, and the next stage is actually reached). This is a slight
165 simplification but it will do for now.
166
167
168 7. Advantages and disadvantages.
169
170 Filters can be implemented much more quickly in Perl than for example
171 in a low-level language like C, and the Perl code does not necessarily
172 need to be thread-safe. All mail is essentially text, and Perl is an
173 extremely powerful tool for text processing. A milter coded in Perl
174 can do more or less anything that one reasonably might want to do with
175 a message. The milter has full access to all Perl library functions,
176 standard modules, modules installed from CPAN, many system utilities,
177 modules of your own, etc. etc.
178
179 One of the prices of the power and flexibility of Perl is that the
180 processes can be large, and processor-intensive; you may need to keep
181 an eye on that when you're developing. The current maintainer's all-
182 singing all-dancing milter generally runs a few 200MByte processes, of
183 which ~60% is shared, so each process consumes about 80MBytes of RAM.
184 Typically, processing a short mail message on modest hardware might
185 consume 50ms-500ms of CPU time; an accepted message which is examined
186 at every Protocol stage will likely cost significantly more than one
187 that is disposed of (ACCEPT, REJECT, TEMPFAIL, DISCARD) early in the
188 Milter Protocol. Timings are heavily dependent on the configuration,
189 the code, and the hardware of course.
190
191 8. Complexity is an enemy of security.
192
193 Enough said.
194
195
196 9. Sendmail::Milter (Obsolete)
197 ==============================
198
199 Ancient and very flawed module which has been unmaintained since the
200 end of the 20th century but which Sendmail::PMilter originally used.
201 Sendmail::Milter supported only Version 2 of the Milter Protocol, has
202 a poor reputation with its users, and its author has been unhelpful.
203 In view of the shortcomings its use cannot be recommended; versions of
204 Sendmail::PMilter later than 1.2x do not use it.