17c478bd9Sstevel@tonic-gate#
2*058561cbSjbeck# Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
37c478bd9Sstevel@tonic-gate# Use is subject to license terms.
47c478bd9Sstevel@tonic-gate#
57c478bd9Sstevel@tonic-gate# ident	"%Z%%M%	%I%	%E% SMI"
67c478bd9Sstevel@tonic-gate#
77c478bd9Sstevel@tonic-gate
87c478bd9Sstevel@tonic-gateThe sendmail Mail Filter API (Milter) is designed to allow third-party
97c478bd9Sstevel@tonic-gateprograms access to mail messages as they are being processed in order to
107c478bd9Sstevel@tonic-gatefilter meta-information and content.
117c478bd9Sstevel@tonic-gate
127c478bd9Sstevel@tonic-gateThis README file describes the steps needed to compile and run a filter,
137c478bd9Sstevel@tonic-gatethrough reference to a sample filter which is attached at the end of this
147c478bd9Sstevel@tonic-gatefile.
157c478bd9Sstevel@tonic-gate
1649218d4fSjbeckNote: if you want to write a milter in Java, then see
1749218d4fSjbeckhttp://sendmail-jilter.sourceforge.net/
1849218d4fSjbeck
197c478bd9Sstevel@tonic-gate+----------------+
207c478bd9Sstevel@tonic-gate| SECURITY HINTS |
217c478bd9Sstevel@tonic-gate+----------------+
227c478bd9Sstevel@tonic-gate
237c478bd9Sstevel@tonic-gateNote: we strongly recommend not to run any milter as root.  Libmilter
247c478bd9Sstevel@tonic-gatedoes not need root access to communicate with sendmail.  It is a
257c478bd9Sstevel@tonic-gategood security practice to run a program only with root privileges
267c478bd9Sstevel@tonic-gateif really necessary.  A milter should probably check first whether
277c478bd9Sstevel@tonic-gateit runs as root and refuse to start in that case.  libmilter will
287c478bd9Sstevel@tonic-gatenot unlink a socket when running as root.
297c478bd9Sstevel@tonic-gate
307c478bd9Sstevel@tonic-gate+-------------------+
317c478bd9Sstevel@tonic-gate| BUILDING A FILTER |
327c478bd9Sstevel@tonic-gate+-------------------+
337c478bd9Sstevel@tonic-gate
347c478bd9Sstevel@tonic-gateThe following command presumes that the sample code from the end of this
357c478bd9Sstevel@tonic-gateREADME is saved to a file named 'sample.c'.
367c478bd9Sstevel@tonic-gate
377c478bd9Sstevel@tonic-gate	cc -D_REENTRANT -o sample sample.c -lmilter
387c478bd9Sstevel@tonic-gate
397c478bd9Sstevel@tonic-gateFilters must be thread-safe!
407c478bd9Sstevel@tonic-gate
417c478bd9Sstevel@tonic-gateNote that since filters use threads, it may be necessary to alter per
427c478bd9Sstevel@tonic-gateprocess limits in your filter.  For example, you might look at using
437c478bd9Sstevel@tonic-gatesetrlimit() to increase the number of open file descriptors if your filter
447c478bd9Sstevel@tonic-gateis going to be busy.
457c478bd9Sstevel@tonic-gate
467c478bd9Sstevel@tonic-gate
477c478bd9Sstevel@tonic-gate+----------------------------------------+
487c478bd9Sstevel@tonic-gate| SPECIFYING FILTERS IN SENDMAIL CONFIGS |
497c478bd9Sstevel@tonic-gate+----------------------------------------+
507c478bd9Sstevel@tonic-gate
517c478bd9Sstevel@tonic-gateFilters are specified with a key letter ``X'' (for ``eXternal'').
527c478bd9Sstevel@tonic-gate
537c478bd9Sstevel@tonic-gateFor example:
547c478bd9Sstevel@tonic-gate
557c478bd9Sstevel@tonic-gate	Xfilter1, S=local:/var/run/f1.sock, F=R
567c478bd9Sstevel@tonic-gate	Xfilter2, S=inet6:999@localhost, F=T, T=C:10m;S:1s;R:1s;E:5m
577c478bd9Sstevel@tonic-gate	Xfilter3, S=inet:3333@localhost
587c478bd9Sstevel@tonic-gate
597c478bd9Sstevel@tonic-gatespecifies three filters.  Filters can be specified in your .mc file using
607c478bd9Sstevel@tonic-gatethe following:
617c478bd9Sstevel@tonic-gate
627c478bd9Sstevel@tonic-gate	INPUT_MAIL_FILTER(`filter1', `S=local:/var/run/f1.sock, F=R')
637c478bd9Sstevel@tonic-gate	INPUT_MAIL_FILTER(`filter2', `S=inet6:999@localhost, F=T, T=C:10m;S:1s;R:1s;E:5m')
647c478bd9Sstevel@tonic-gate	INPUT_MAIL_FILTER(`filter3', `S=inet:3333@localhost')
657c478bd9Sstevel@tonic-gate
667c478bd9Sstevel@tonic-gateThe first attaches to a Unix-domain socket in the /var/run directory; the
677c478bd9Sstevel@tonic-gatesecond uses an IPv6 socket on port 999 of localhost, and the third uses an
687c478bd9Sstevel@tonic-gateIPv4 socket on port 3333 of localhost.  The current flags (F=) are:
697c478bd9Sstevel@tonic-gate
707c478bd9Sstevel@tonic-gate	R		Reject connection if filter unavailable
717c478bd9Sstevel@tonic-gate	T		Temporary fail connection if filter unavailable
72*058561cbSjbeck	4		Shut down connection if filter unavailable
73*058561cbSjbeck			(with a 421 temporary error).
747c478bd9Sstevel@tonic-gate
75*058561cbSjbeckIf none of these is specified, the message is passed through sendmail
767c478bd9Sstevel@tonic-gatein case of filter errors as if the failing filters were not present.
777c478bd9Sstevel@tonic-gate
787c478bd9Sstevel@tonic-gateFinally, you can override the default timeouts used by sendmail when
797c478bd9Sstevel@tonic-gatetalking to the filters using the T= equate.  There are four fields inside
807c478bd9Sstevel@tonic-gateof the T= equate:
817c478bd9Sstevel@tonic-gate
827c478bd9Sstevel@tonic-gateLetter		Meaning
837c478bd9Sstevel@tonic-gate  C		Timeout for connecting to a filter (if 0, use system timeout)
847c478bd9Sstevel@tonic-gate  S		Timeout for sending information from the MTA to a filter
857c478bd9Sstevel@tonic-gate  R		Timeout for reading reply from the filter
867c478bd9Sstevel@tonic-gate  E		Overall timeout between sending end-of-message to filter
877c478bd9Sstevel@tonic-gate		and waiting for the final acknowledgment
887c478bd9Sstevel@tonic-gate
897c478bd9Sstevel@tonic-gateNote the separator between each is a ';' as a ',' already separates equates
907c478bd9Sstevel@tonic-gateand therefore can't separate timeouts.  The default values (if not set in
917c478bd9Sstevel@tonic-gatethe config) are:
927c478bd9Sstevel@tonic-gate
937c478bd9Sstevel@tonic-gateT=C:5m;S:10s;R:10s;E:5m
947c478bd9Sstevel@tonic-gate
957c478bd9Sstevel@tonic-gatewhere 's' is seconds and 'm' is minutes.
967c478bd9Sstevel@tonic-gate
977c478bd9Sstevel@tonic-gateWhich filters are invoked and their sequencing is handled by the
987c478bd9Sstevel@tonic-gateInputMailFilters option. Note: if InputMailFilters is not defined no filters
997c478bd9Sstevel@tonic-gatewill be used.
1007c478bd9Sstevel@tonic-gate
1017c478bd9Sstevel@tonic-gate	O InputMailFilters=filter1, filter2, filter3
1027c478bd9Sstevel@tonic-gate
1037c478bd9Sstevel@tonic-gateThis is is set automatically according to the order of the
1047c478bd9Sstevel@tonic-gateINPUT_MAIL_FILTER commands in your .mc file.  Alternatively, you can
1057c478bd9Sstevel@tonic-gatereset its value by setting confINPUT_MAIL_FILTERS in your .mc file.
1067c478bd9Sstevel@tonic-gateThis options causes the three filters to be called in the same order
1077c478bd9Sstevel@tonic-gatethey were specified.  It allows for possible future filtering on output
1087c478bd9Sstevel@tonic-gate(although this is not intended for this release).
1097c478bd9Sstevel@tonic-gate
1107c478bd9Sstevel@tonic-gateAlso note that a filter can be defined without adding it to the input
1117c478bd9Sstevel@tonic-gatefilter list by using MAIL_FILTER() instead of INPUT_MAIL_FILTER() in your
1127c478bd9Sstevel@tonic-gate.mc file.
1137c478bd9Sstevel@tonic-gate
1147c478bd9Sstevel@tonic-gateTo test sendmail with the sample filter, the following might be added (in
1157c478bd9Sstevel@tonic-gatethe appropriate locations) to your .mc file:
1167c478bd9Sstevel@tonic-gate
1177c478bd9Sstevel@tonic-gate	INPUT_MAIL_FILTER(`sample', `S=local:/var/run/f1.sock')
1187c478bd9Sstevel@tonic-gate
1197c478bd9Sstevel@tonic-gate
1207c478bd9Sstevel@tonic-gate+------------------+
1217c478bd9Sstevel@tonic-gate| TESTING A FILTER |
1227c478bd9Sstevel@tonic-gate+------------------+
1237c478bd9Sstevel@tonic-gate
1247c478bd9Sstevel@tonic-gateOnce you have compiled a filter, modified your .mc file and restarted
1257c478bd9Sstevel@tonic-gatethe sendmail process, you will want to test that the filter performs as
1267c478bd9Sstevel@tonic-gateintended.
1277c478bd9Sstevel@tonic-gate
1287c478bd9Sstevel@tonic-gateThe sample filter takes one argument -p, which indicates the local port
1297c478bd9Sstevel@tonic-gateon which to create a listening socket for the filter.  Maintaining
1307c478bd9Sstevel@tonic-gateconsistency with the suggested options for sendmail.cf, this would be the
1317c478bd9Sstevel@tonic-gateUNIX domain socket located in /var/run/f1.sock.
1327c478bd9Sstevel@tonic-gate
1337c478bd9Sstevel@tonic-gate	% ./sample -p local:/var/run/f1.sock
1347c478bd9Sstevel@tonic-gate
1357c478bd9Sstevel@tonic-gateIf the sample filter returns immediately to a command line, there was either
1367c478bd9Sstevel@tonic-gatean error with your command or a problem creating the specified socket.
1377c478bd9Sstevel@tonic-gateFurther logging can be captured through the syslogd daemon.  Using the
1387c478bd9Sstevel@tonic-gate'netstat -a' command can ensure that your filter process is listening on
1397c478bd9Sstevel@tonic-gatethe appropriate local socket.
1407c478bd9Sstevel@tonic-gate
1417c478bd9Sstevel@tonic-gateEmail messages must be injected via SMTP to be filtered.  There are two
1427c478bd9Sstevel@tonic-gatesimple means of doing this; either using the 'sendmail -bs' command, or
1437c478bd9Sstevel@tonic-gateby telnetting to port 25 of the machine configured for milter.  Once
1447c478bd9Sstevel@tonic-gateconnected via one of these options, the session can be continued through
1457c478bd9Sstevel@tonic-gatethe use of standard SMTP commands.
1467c478bd9Sstevel@tonic-gate
1477c478bd9Sstevel@tonic-gate% sendmail -bs
148*058561cbSjbeck220 test.sendmail.com ESMTP Sendmail 8.14.0/8.14.0; Thu, 22 Jun 2006 13:05:23 -0500 (EST)
1497c478bd9Sstevel@tonic-gateHELO localhost
1507c478bd9Sstevel@tonic-gate250 test.sendmail.com Hello testy@localhost, pleased to meet you
1517c478bd9Sstevel@tonic-gateMAIL From:<testy>
1527c478bd9Sstevel@tonic-gate250 2.1.0 <testy>... Sender ok
1537c478bd9Sstevel@tonic-gateRCPT To:<root>
1547c478bd9Sstevel@tonic-gate250 2.1.5 <root>... Recipient ok
1557c478bd9Sstevel@tonic-gateDATA
1567c478bd9Sstevel@tonic-gate354 Enter mail, end with "." on a line by itself
1577c478bd9Sstevel@tonic-gateFrom: testy@test.sendmail.com
1587c478bd9Sstevel@tonic-gateTo: root@test.sendmail.com
1597c478bd9Sstevel@tonic-gateSubject: testing sample filter
1607c478bd9Sstevel@tonic-gate
1617c478bd9Sstevel@tonic-gateSample body
1627c478bd9Sstevel@tonic-gate.
1637c478bd9Sstevel@tonic-gate250 2.0.0 dB73Zxi25236 Message accepted for delivery
1647c478bd9Sstevel@tonic-gateQUIT
1657c478bd9Sstevel@tonic-gate221 2.0.0 test.sendmail.com closing connection
1667c478bd9Sstevel@tonic-gate
1677c478bd9Sstevel@tonic-gateIn the above example, the lines beginning with numbers are output by the
1687c478bd9Sstevel@tonic-gatemail server, and those without are your input.  If everything is working
1697c478bd9Sstevel@tonic-gateproperly, you will find a file in /tmp by the name of msg.XXXXXXXX (where
1707c478bd9Sstevel@tonic-gatethe Xs represent any combination of letters and numbers).  This file should
1717c478bd9Sstevel@tonic-gatecontain the message body and headers from the test email entered above.
1727c478bd9Sstevel@tonic-gate
1737c478bd9Sstevel@tonic-gateIf the sample filter did not log your test email, there are a number of
1747c478bd9Sstevel@tonic-gatemethods to narrow down the source of the problem.  Check your system
1757c478bd9Sstevel@tonic-gatelogs written by syslogd and see if there are any pertinent lines.  You
1767c478bd9Sstevel@tonic-gatemay need to reconfigure syslogd to capture all relevant data.  Additionally,
1777c478bd9Sstevel@tonic-gatethe logging level of sendmail can be raised with the LogLevel option.
1787c478bd9Sstevel@tonic-gateSee the sendmail(8) manual page for more information.
1797c478bd9Sstevel@tonic-gate
1807c478bd9Sstevel@tonic-gate
181*058561cbSjbeck$Revision: 8.42 $, Last updated $Date: 2006/06/29 17:10:16 $
182