1MIMEDEFANG-PROTOCOL(7) Miscellaneous Information Manual MIMEDEFANG-PROTOCOL(7)
2
3
4
6 mimedefang-protocol - Conventions used by mimedefang(8) to communicate
7 with filter programs.
8
9
11 mimedefang(8) and mimedefang-multiplexor(8) provide a simplified mecha‐
12 nism for hooking scripts and programs into Sendmail's milter API.
13
14 The milter API is multi-threaded and written in C; mimedefang lets you
15 write single-threaded filters written in the language of your choice.
16 Some of the flexibility and speed of milter is sacrificed, but the ease
17 of writing filters more than compensates for this slight loss.
18
19 This manual describes how mimedefang communicates with the filter pro‐
20 gram, and gives you enough information to write your own filter program
21 as a replacement for mimedefang.pl if you wish.
22
23
25 The protocol is a simple file-based protocol. For each invocation of a
26 filter, mimedefang creates a unique working directory and populates it
27 with files. It calls the filter, which is expected to populate the
28 working directory with more files, which communicate the scan results
29 back to mimedefang. This simple mechanism allows you to easily write
30 filters in scripting languages without worrying about C-level details.
31
32
34 The filter program may be invoked in one of five ways:
35
36
37 filter_prog directory
38 If the program is invoked with a single argument which is an
39 absolute path name (called the working directory, the program is
40 expected to perform filtering in that directory and then exit.
41
42
43 filter_prog -server
44 If the program is invoked with the single argument -server, it
45 is expected to run as a server. See SERVER MODE for details.
46
47
48 filter_prog -serveru
49 If the program is invoked with the single argument -serveru, it
50 is expected to run as a server. In addition, anything it prints
51 to file descriptor 3 is used to update the "slave status" field
52 in the multiplexor. This lets the filter inform administrators
53 exactly what it is doing. (See the -Z option to mimedefang-mul‐
54 tiplexor.)
55
56
57 filter_prog -embserver
58 Similar to -server, but used by the embedded Perl code. The
59 program should run any initialization routines and then exit.
60 The multiplexor will subsequently call the Perl routine
61 do_main_loop when it is time for the slave to begin running in
62 server mode.
63
64
65 filter_prog -embserveru
66 Similar to -embserver with the additional magic of updating the
67 slave status from data written to file descriptor 3.
68
69
71 When the filter begins a scan, it should change directories to the
72 working directory. In that directory, it will find the following
73 files.
74
75
76 INPUTMSG
77 A file containing the complete input e-mail message, including
78 headers.
79
80
81 HEADERS
82 A file containing just the headers, one per line. Headers which
83 are continued over several lines in the original message are
84 collapsed into a single line in this file.
85
86
87 COMMANDS
88 A file containing a list of commands. Each command is a single
89 letter and may be followed by arguments. Each command is on its
90 own line.
91
92
94 All commands have their arguments encoded as follows: All characters
95 outside the range 33 to 126 ASCII, as well as the characters "%", "\",
96 "'" and double-quote, are replaced by a percent sign followed by two
97 hex digits specifying the character's numerical value. The filter must
98 un-escape the arguments when it reads the COMMANDS file.
99
100
101 The commands from the C to Perl filters are:
102
103
104 Ssender
105 The sender of the message.
106
107
108 sesmtp_arg
109 An ESMTP argument associated with the sender (such as
110 SIZE=54432). There is one s line for each ESMTP argument.
111
112
113 Usubject
114 The message subject.
115
116
117 Xmessage_id
118 The Message-ID.
119
120
121 Rrecipient mailer host addr
122 A recipient. There is one R line for each recipient. The
123 mailer, host and addr parts of the line are the values of the
124 Sendmail {rcpt_mailer}, {rcpt_host} and {rcpt_addr} macros if
125 they are available, or "?" if not.
126
127
128 resmtp_arg
129 An ESMTP argument associated with the most recent recipient
130 (such as NOTIFY=never). There is one r line for each SMTP argu‐
131 ment.
132
133
134 ! If this command is present, there are suspicious characters in
135 the message headers.
136
137
138 ? If this command is present, there are suspicious characters in
139 the message body.
140
141
142 Ihost_addr
143 The SMTP relay host's IP address in dotted-quad notation.
144
145
146 Jhost_addr
147 The "real" SMTP relay host's IP address in dotted-quad notation.
148 Multi-stage MIMEDefang relays can use a special IP validation
149 header so that even the innermost MIMEDefang relay can see the
150 "original" relay's IP address.
151
152
153 Hhost_name
154 The SMTP relay host name.
155
156
157 Eargument
158 The argument to the SMTP "EHLO" or "HELO" command.
159
160
161 Qqid The message's Sendmail queue-ID.
162
163
164 =macro val
165 Set the value of the specified Sendmail macro to val. Both
166 macro and val are percent-encoded, but the single space charac‐
167 ter between them is not.
168
169
171 When the filter performs a scan, it can make use of all the information
172 in the files mentioned previously. If the filter needs temporary work‐
173 ing files, it should create a subdirectory under the working directory
174 for its own use. In this case, you do not have to clean up your work‐
175 ing files, because mimedefang deletes the working directory when the
176 filter returns.
177
178
180 The filter communicates the results of the scan back to mimedefang by
181 creating additional files in the working directory. The most important
182 file is called RESULTS, and it contains a list of one-letter, one-line
183 commands back to the filter. As usual, command arguments are percent-
184 escaped. The commands from the filter back to mimedefang are:
185
186
187 Bcode dsn reply_text
188 Bounce (reject) the message with the specified SMTP reply code,
189 DSN code and reply text.
190
191
192 D Silently discard the message and pretend it was delivered.
193
194
195 Tcode dsn reply_text
196 Return an SMTP temporary failure code with the specified SMTP
197 code, DSN and reply text.
198
199
200 C Replace the message body. If this command is present, the file
201 NEWBODY must contain the new message body.
202
203
204 Mheader_val
205 Replace the MIME Content-Type header with a new value. Used to
206 change MIME boundaries or convert non-MIME to MIME messages.
207
208
209 Hheader val
210 Add a new header header with value val. The header should not
211 contain a colon. Each of header and val is percent-escaped, but
212 the single space between them is not.
213
214
215 Nheader index val
216 Adds a new header header with value val in position index. An
217 index of zero specifies that the new header should be prepended
218 before all existing headers.
219
220
221 Iheader index val
222 Replace the index'th occurrence of header with value val. The
223 index is 1-based. The header should not contain a colon. Each
224 of header, index and val is percent-escaped, but the single
225 space between them is not.
226
227
228 Jheader index
229 Delete the index'th occurrence of header.
230
231
232 Rrecip Add a new recipient recip to the message.
233
234
235 Srecip Delete recip from the list of message recipients.
236
237
238 fsender
239 Change the envelope sender to sender. This is only supported by
240 Sendmail 8.14.0 and higher.
241
242
243 F Indicate that we have finished issuing commands. Anything after
244 an F line is ignored.
245
246
248 In server mode, mimedefang-multiplexor runs the filter program continu‐
249 ously in a server loop. The filter program reads commands from stan‐
250 dard input, and writes results to standard output. The filter program
251 must exit shortly after it sees EOF on its standard input. If it does
252 not exit within 10 seconds, it will be terminated with SIGTERM. If
253 that still does not work, then after a further 10 seconds, it is killed
254 with SIGKILL.
255
256
257 SERVER COMMANDS
258
259 All server commands are single line commands. Each command is
260 followed by a space-separated list of arguments; each argument
261 is percent-encoded. The commands defined are:
262
263
264 ping Elicits a reply of "PONG" from the server.
265
266
267 scan dir
268 Run a scan in the directory dir. The command is terminated with
269 a newline. The server must write a newline-terminated "ok" if
270 the scan completed successfully, or "error: msg" if something
271 went wrong.
272
273
274
275 relayok ip_addr hostname
276 Test whether or not to accept a connection from the specified
277 host. The server must write "ok 1" if we will accept the con‐
278 nection, or "ok 0 error_message code dsn" if not. It can indi‐
279 cate a temporary failure by writing "ok -1 error_message code
280 dsn". Note that even if the connection is accepted, a later
281 scan can still reject the message based on other criteria.
282 "ip_addr" is the IP address of the relay and "hostname" is the
283 hostname (if it could be determined; otherwise, the IP address
284 in square brackets).
285
286
287 senderok sender_addr ip_addr hostname helo_string dir queue_id
288 [esmtp_args...]
289
290 Test whether or not to accept mail from the specified sender.
291 The server must write "ok 1" if we will accept the mail attempt,
292 or "ok 0 error_message code dsn" if not. "ok -1 error_message
293 code dsn" indicates a temporary failure. Note that even if the
294 sender is accepted, a later scan can still reject the message
295 based on other criteria. "sender_addr" is the sender's e-mail
296 address. The "ip_addr" and "hostname" arguments are as in
297 relayok. "helo_string" is the argument to the SMTP HELO/EHLO
298 command. "dir" is the MIMEDefang spool directory, and "queue_id"
299 is the Sendmail queue identifier.
300
301 The optional "esmtp_args" are space-separated, percent-encoded
302 ESMTP arguments supplied with the MAIL FROM: command.
303
304
305 recipok recip_addr sender_addr ip_addr hostname first_recip helo_string
306 dir queue_id [esmtp_args...]
307 Test whether or not to accept mail for the specified recipient.
308 The server must write "ok 1" if we will accept it, or "ok 0
309 error_message code dsn" if not. ok -1 error_message code dsn"
310 indicates a temporary-failure.
311
312 "recip_addr" is the argument to the RCPT TO: command, and
313 "first_recip" is the argument to the first RCPT TO: command for
314 this message. Other arguments are as in senderok.
315
316
317 map map_name key
318 If you are using a map socket (the -N option to mimedefang-mul‐
319 tiplexor), then the server should look up the key key in the map
320 map_name. The server should print a single line to standard
321 output. The first word on the line should be one of OK, NOT‐
322 FOUND, TEMP, TIMEOUT or PERM, indicating a successful lookup,
323 absence of the key, a temporary failure, a timeout or a perma‐
324 nent failure, respectively. This should be followed by a space
325 and a percent-encoded string representing the value of the key
326 (if it was found) or an optional error message (if something
327 went wrong.)
328
329
330 tick band
331 The filter should run filter_tick with the specified band argu‐
332 ment. It should print a single line to standard output:
333
334 tock band
335
336
337 Additional Commands
338 The filter can define a function filter_unknown_cmd that can
339 extend the list of server commands. If you do this, make sure
340 all of your commands start with an upper-case letter to avoid
341 conflicts if more built-in commands are defined in the future.
342
343
344 SERVER REPLIES
345
346 The reply codes are:
347
348
349 ok [return_code] [parameters]
350 The operation completed successfully. Some operations have an
351 associated return code, and possibly other parameters as well.
352 See the source code for the gory details.
353
354
355 error: Message
356 The operation failed. Message may give additional details.
357
358
359 In server mode, you should not write anything to standard output except
360 reply codes, or the multiplexor will become confused. You should not
361 terminate the program in server mode; simply echo an error: reply and
362 return to the server loop.
363
364 When you send a reply code back to the multiplexor, be sure to termi‐
365 nate it with a newline, and to flush standard output. If your program
366 uses the Standard I/O library, standard output may not be flushed imme‐
367 diately, and mimedefang-multiplexor will wait forever for the filter's
368 reply, and eventually kill the filter on the assumption it has hung up.
369
370 In server mode, if the filter program receives a SIGINT signal, it must
371 terminate. This is used by mimedefang-multiplexor to terminate slaves
372 after they have processed a given number of e-mail messages.
373
374
376 mimedefang was written by David F. Skoll <dfs@roaringpenguin.com>. The
377 mimedefang home page is http://www.mimedefang.org/.
378
379
381 mimedefang.pl(8), mimedefang(8), mimedefang-multiplexor(8), mimedefang-
382 filter(5)
383
384
385
386
387
388
3894th Berkeley Distribution 8 February 2005 MIMEDEFANG-PROTOCOL(7)