xref: /freebsd/contrib/sendmail/libmilter/README (revision d39bd2c1388b520fcba9abed1932acacead60fba)
106f25ae9SGregory Neil ShapiroThis directory contains the source files for libmilter.
206f25ae9SGregory Neil Shapiro
306f25ae9SGregory Neil ShapiroThe sendmail Mail Filter API (Milter) is designed to allow third-party
406f25ae9SGregory Neil Shapiroprograms access to mail messages as they are being processed in order to
506f25ae9SGregory Neil Shapirofilter meta-information and content.
606f25ae9SGregory Neil Shapiro
706f25ae9SGregory Neil ShapiroThis README file describes the steps needed to compile and run a filter,
806f25ae9SGregory Neil Shapirothrough reference to a sample filter which is attached at the end of this
906f25ae9SGregory Neil Shapirofile.  It is necessary to first build libmilter.a, which can be done by
1006f25ae9SGregory Neil Shapiroissuing the './Build' command in SRCDIR/libmilter .
1106f25ae9SGregory Neil Shapiro
12e92d3f3fSGregory Neil ShapiroStarting with 8.13 sendmail is compiled by default with support for
13e92d3f3fSGregory Neil Shapirothe milter API.
1406f25ae9SGregory Neil Shapiro
154e4196cbSGregory Neil ShapiroNote: if you want to write a milter in Java, then see
164e4196cbSGregory Neil Shapirohttp://sendmail-jilter.sourceforge.net/
174e4196cbSGregory Neil Shapiro
18*d39bd2c1SGregory Neil Shapiro
1940266059SGregory Neil Shapiro+----------------+
2040266059SGregory Neil Shapiro| SECURITY HINTS |
2140266059SGregory Neil Shapiro+----------------+
2240266059SGregory Neil Shapiro
2340266059SGregory Neil ShapiroNote: we strongly recommend not to run any milter as root.  Libmilter
2440266059SGregory Neil Shapirodoes not need root access to communicate with sendmail.  It is a
2540266059SGregory Neil Shapirogood security practice to run a program only with root privileges
2640266059SGregory Neil Shapiroif really necessary.  A milter should probably check first whether
27e92d3f3fSGregory Neil Shapiroit runs as root and refuse to start in that case.  libmilter will
28e92d3f3fSGregory Neil Shapironot unlink a socket when running as root.
2940266059SGregory Neil Shapiro
30*d39bd2c1SGregory Neil Shapiro
31e92d3f3fSGregory Neil Shapiro+----------------------+
32e92d3f3fSGregory Neil Shapiro| CONFIGURATION MACROS |
33e92d3f3fSGregory Neil Shapiro+----------------------+
3440266059SGregory Neil Shapiro
35e92d3f3fSGregory Neil ShapiroLibmilter uses a set of C preprocessor macros to specify platform specific
36e92d3f3fSGregory Neil Shapirofeatures of the C compiler and standard C libraries.
37e92d3f3fSGregory Neil Shapiro
38e92d3f3fSGregory Neil ShapiroSM_CONF_POLL
39e92d3f3fSGregory Neil Shapiro	Set to 1 if poll(2) should be used instead of select(2).
4006f25ae9SGregory Neil Shapiro
41*d39bd2c1SGregory Neil Shapiro
4206f25ae9SGregory Neil Shapiro+-------------------+
4306f25ae9SGregory Neil Shapiro| BUILDING A FILTER |
4406f25ae9SGregory Neil Shapiro+-------------------+
4506f25ae9SGregory Neil Shapiro
4606f25ae9SGregory Neil ShapiroThe following command presumes that the sample code from the end of this
4706f25ae9SGregory Neil ShapiroREADME is saved to a file named 'sample.c' and built in the local platform-
4806f25ae9SGregory Neil Shapirospecific build subdirectory (SRCDIR/obj.*/libmilter).
4906f25ae9SGregory Neil Shapiro
5013bd1963SGregory Neil Shapiro	cc -I../../include -o sample sample.c libmilter.a ../libsm/libsm.a -pthread
5106f25ae9SGregory Neil Shapiro
5206f25ae9SGregory Neil ShapiroIt is recommended that you build your filters in a location outside of
5306f25ae9SGregory Neil Shapirothe sendmail source tree.  Modify the compiler include references (-I)
5406f25ae9SGregory Neil Shapiroand the library locations accordingly.  Also, some operating systems may
5506f25ae9SGregory Neil Shapirorequire additional libraries.  For example, SunOS 5.X requires '-lresolv
5640266059SGregory Neil Shapiro-lsocket -lnsl'.  Depending on your operating system you may need a library
5740266059SGregory Neil Shapiroinstead of the option -pthread, e.g., -lpthread.
5806f25ae9SGregory Neil Shapiro
5906f25ae9SGregory Neil ShapiroFilters must be thread-safe!  Many operating systems now provide support for
6006f25ae9SGregory Neil ShapiroPOSIX threads in the standard C libraries.  The compiler flag to link with
6106f25ae9SGregory Neil Shapirothreading support differs according to the compiler and linker used.  Check
6206f25ae9SGregory Neil Shapirothe Makefile in your appropriate obj.*/libmilter build subdirectory if you
6306f25ae9SGregory Neil Shapiroare unsure of the local flag used.
6406f25ae9SGregory Neil Shapiro
65602a2b1bSGregory Neil ShapiroNote that since filters use threads, it may be necessary to alter per
66602a2b1bSGregory Neil Shapiroprocess limits in your filter.  For example, you might look at using
67602a2b1bSGregory Neil Shapirosetrlimit() to increase the number of open file descriptors if your filter
68602a2b1bSGregory Neil Shapirois going to be busy.
69602a2b1bSGregory Neil Shapiro
7006f25ae9SGregory Neil Shapiro
7106f25ae9SGregory Neil Shapiro+----------------------------------------+
7206f25ae9SGregory Neil Shapiro| SPECIFYING FILTERS IN SENDMAIL CONFIGS |
7306f25ae9SGregory Neil Shapiro+----------------------------------------+
7406f25ae9SGregory Neil Shapiro
7506f25ae9SGregory Neil ShapiroFilters are specified with a key letter ``X'' (for ``eXternal'').
7606f25ae9SGregory Neil Shapiro
7706f25ae9SGregory Neil ShapiroFor example:
7806f25ae9SGregory Neil Shapiro
7906f25ae9SGregory Neil Shapiro	Xfilter1, S=local:/var/run/f1.sock, F=R
8013058a91SGregory Neil Shapiro	Xfilter2, S=inet6:999@localhost, F=T, T=C:10m;S:1s;R:1s;E:5m
8106f25ae9SGregory Neil Shapiro	Xfilter3, S=inet:3333@localhost
8206f25ae9SGregory Neil Shapiro
8306f25ae9SGregory Neil Shapirospecifies three filters.  Filters can be specified in your .mc file using
8406f25ae9SGregory Neil Shapirothe following:
8506f25ae9SGregory Neil Shapiro
8606f25ae9SGregory Neil Shapiro	INPUT_MAIL_FILTER(`filter1', `S=local:/var/run/f1.sock, F=R')
8713058a91SGregory Neil Shapiro	INPUT_MAIL_FILTER(`filter2', `S=inet6:999@localhost, F=T, T=C:10m;S:1s;R:1s;E:5m')
8806f25ae9SGregory Neil Shapiro	INPUT_MAIL_FILTER(`filter3', `S=inet:3333@localhost')
8906f25ae9SGregory Neil Shapiro
9006f25ae9SGregory Neil ShapiroThe first attaches to a Unix-domain socket in the /var/run directory; the
9106f25ae9SGregory Neil Shapirosecond uses an IPv6 socket on port 999 of localhost, and the third uses an
9206f25ae9SGregory Neil ShapiroIPv4 socket on port 3333 of localhost.  The current flags (F=) are:
9306f25ae9SGregory Neil Shapiro
9406f25ae9SGregory Neil Shapiro	R		Reject connection if filter unavailable
9506f25ae9SGregory Neil Shapiro	T		Temporary fail connection if filter unavailable
96d0cef73dSGregory Neil Shapiro	4		Shut down connection if filter unavailable
97d0cef73dSGregory Neil Shapiro			(with a 421 temporary error).
9806f25ae9SGregory Neil Shapiro
99d0cef73dSGregory Neil ShapiroIf none of these is specified, the message is passed through sendmail
10040266059SGregory Neil Shapiroin case of filter errors as if the failing filters were not present.
10142e5d165SGregory Neil Shapiro
10206f25ae9SGregory Neil ShapiroFinally, you can override the default timeouts used by sendmail when
10313058a91SGregory Neil Shapirotalking to the filters using the T= equate.  There are four fields inside
10406f25ae9SGregory Neil Shapiroof the T= equate:
10506f25ae9SGregory Neil Shapiro
10606f25ae9SGregory Neil ShapiroLetter		Meaning
10713058a91SGregory Neil Shapiro  C		Timeout for connecting to a filter (if 0, use system timeout)
10806f25ae9SGregory Neil Shapiro  S		Timeout for sending information from the MTA to a filter
10906f25ae9SGregory Neil Shapiro  R		Timeout for reading reply from the filter
11006f25ae9SGregory Neil Shapiro  E		Overall timeout between sending end-of-message to filter
11106f25ae9SGregory Neil Shapiro		and waiting for the final acknowledgment
11206f25ae9SGregory Neil Shapiro
11306f25ae9SGregory Neil ShapiroNote the separator between each is a ';' as a ',' already separates equates
11413058a91SGregory Neil Shapiroand therefore can't separate timeouts.  The default values (if not set in
11513058a91SGregory Neil Shapirothe config) are:
11606f25ae9SGregory Neil Shapiro
11740266059SGregory Neil ShapiroT=C:5m;S:10s;R:10s;E:5m
11806f25ae9SGregory Neil Shapiro
11906f25ae9SGregory Neil Shapirowhere 's' is seconds and 'm' is minutes.
12006f25ae9SGregory Neil Shapiro
12142e5d165SGregory Neil ShapiroWhich filters are invoked and their sequencing is handled by the
1228774250cSGregory Neil ShapiroInputMailFilters option. Note: if InputMailFilters is not defined no filters
1238774250cSGregory Neil Shapirowill be used.
12442e5d165SGregory Neil Shapiro
12542e5d165SGregory Neil Shapiro	O InputMailFilters=filter1, filter2, filter3
12642e5d165SGregory Neil Shapiro
12742e5d165SGregory Neil ShapiroThis is is set automatically according to the order of the
12842e5d165SGregory Neil ShapiroINPUT_MAIL_FILTER commands in your .mc file.  Alternatively, you can
12942e5d165SGregory Neil Shapiroreset its value by setting confINPUT_MAIL_FILTERS in your .mc file.
13042e5d165SGregory Neil ShapiroThis options causes the three filters to be called in the same order
13142e5d165SGregory Neil Shapirothey were specified.  It allows for possible future filtering on output
13242e5d165SGregory Neil Shapiro(although this is not intended for this release).
13306f25ae9SGregory Neil Shapiro
13406f25ae9SGregory Neil ShapiroAlso note that a filter can be defined without adding it to the input
13506f25ae9SGregory Neil Shapirofilter list by using MAIL_FILTER() instead of INPUT_MAIL_FILTER() in your
13606f25ae9SGregory Neil Shapiro.mc file.
13706f25ae9SGregory Neil Shapiro
13806f25ae9SGregory Neil ShapiroTo test sendmail with the sample filter, the following might be added (in
13906f25ae9SGregory Neil Shapirothe appropriate locations) to your .mc file:
14006f25ae9SGregory Neil Shapiro
14106f25ae9SGregory Neil Shapiro	INPUT_MAIL_FILTER(`sample', `S=local:/var/run/f1.sock')
14206f25ae9SGregory Neil Shapiro
14306f25ae9SGregory Neil Shapiro
14406f25ae9SGregory Neil Shapiro+------------------+
14506f25ae9SGregory Neil Shapiro| TESTING A FILTER |
14606f25ae9SGregory Neil Shapiro+------------------+
14706f25ae9SGregory Neil Shapiro
14806f25ae9SGregory Neil ShapiroOnce you have compiled a filter, modified your .mc file and restarted
14906f25ae9SGregory Neil Shapirothe sendmail process, you will want to test that the filter performs as
15006f25ae9SGregory Neil Shapirointended.
15106f25ae9SGregory Neil Shapiro
15206f25ae9SGregory Neil ShapiroThe sample filter takes one argument -p, which indicates the local port
15306f25ae9SGregory Neil Shapiroon which to create a listening socket for the filter.  Maintaining
15406f25ae9SGregory Neil Shapiroconsistency with the suggested options for sendmail.cf, this would be the
15506f25ae9SGregory Neil ShapiroUNIX domain socket located in /var/run/f1.sock.
15606f25ae9SGregory Neil Shapiro
15706f25ae9SGregory Neil Shapiro	% ./sample -p local:/var/run/f1.sock
15806f25ae9SGregory Neil Shapiro
15906f25ae9SGregory Neil ShapiroIf the sample filter returns immediately to a command line, there was either
16006f25ae9SGregory Neil Shapiroan error with your command or a problem creating the specified socket.
16106f25ae9SGregory Neil ShapiroFurther logging can be captured through the syslogd daemon.  Using the
16206f25ae9SGregory Neil Shapiro'netstat -a' command can ensure that your filter process is listening on
16306f25ae9SGregory Neil Shapirothe appropriate local socket.
16406f25ae9SGregory Neil Shapiro
16506f25ae9SGregory Neil ShapiroEmail messages must be injected via SMTP to be filtered.  There are two
16606f25ae9SGregory Neil Shapirosimple means of doing this; either using the 'sendmail -bs' command, or
16706f25ae9SGregory Neil Shapiroby telnetting to port 25 of the machine configured for milter.  Once
16806f25ae9SGregory Neil Shapiroconnected via one of these options, the session can be continued through
16906f25ae9SGregory Neil Shapirothe use of standard SMTP commands.
17006f25ae9SGregory Neil Shapiro
17106f25ae9SGregory Neil Shapiro% sendmail -bs
172d0cef73dSGregory Neil Shapiro220 test.sendmail.com ESMTP Sendmail 8.14.0/8.14.0; Thu, 22 Jun 2006 13:05:23 -0500 (EST)
17306f25ae9SGregory Neil ShapiroHELO localhost
17406f25ae9SGregory Neil Shapiro250 test.sendmail.com Hello testy@localhost, pleased to meet you
17506f25ae9SGregory Neil ShapiroMAIL From:<testy>
17606f25ae9SGregory Neil Shapiro250 2.1.0 <testy>... Sender ok
17706f25ae9SGregory Neil ShapiroRCPT To:<root>
17806f25ae9SGregory Neil Shapiro250 2.1.5 <root>... Recipient ok
17906f25ae9SGregory Neil ShapiroDATA
18006f25ae9SGregory Neil Shapiro354 Enter mail, end with "." on a line by itself
18106f25ae9SGregory Neil ShapiroFrom: testy@test.sendmail.com
18206f25ae9SGregory Neil ShapiroTo: root@test.sendmail.com
18306f25ae9SGregory Neil ShapiroSubject: testing sample filter
18406f25ae9SGregory Neil Shapiro
18506f25ae9SGregory Neil ShapiroSample body
18606f25ae9SGregory Neil Shapiro.
18706f25ae9SGregory Neil Shapiro250 2.0.0 dB73Zxi25236 Message accepted for delivery
18806f25ae9SGregory Neil ShapiroQUIT
18906f25ae9SGregory Neil Shapiro221 2.0.0 test.sendmail.com closing connection
19006f25ae9SGregory Neil Shapiro
19106f25ae9SGregory Neil ShapiroIn the above example, the lines beginning with numbers are output by the
19206f25ae9SGregory Neil Shapiromail server, and those without are your input.  If everything is working
19306f25ae9SGregory Neil Shapiroproperly, you will find a file in /tmp by the name of msg.XXXXXXXX (where
19406f25ae9SGregory Neil Shapirothe Xs represent any combination of letters and numbers).  This file should
19506f25ae9SGregory Neil Shapirocontain the message body and headers from the test email entered above.
19606f25ae9SGregory Neil Shapiro
19706f25ae9SGregory Neil ShapiroIf the sample filter did not log your test email, there are a number of
19806f25ae9SGregory Neil Shapiromethods to narrow down the source of the problem.  Check your system
19906f25ae9SGregory Neil Shapirologs written by syslogd and see if there are any pertinent lines.  You
20006f25ae9SGregory Neil Shapiromay need to reconfigure syslogd to capture all relevant data.  Additionally,
20106f25ae9SGregory Neil Shapirothe logging level of sendmail can be raised with the LogLevel option.
20206f25ae9SGregory Neil ShapiroSee the sendmail(8) manual page for more information.
20306f25ae9SGregory Neil Shapiro
20406f25ae9SGregory Neil Shapiro
20540266059SGregory Neil Shapiro+--------------+
20640266059SGregory Neil Shapiro| REQUIREMENTS |
20740266059SGregory Neil Shapiro+--------------+
20840266059SGregory Neil Shapiro
20940266059SGregory Neil Shapirolibmilter requires pthread support in the operating system.  Moreover, it
21040266059SGregory Neil Shapirorequires that the library functions it uses are thread safe; which is true
21140266059SGregory Neil Shapirofor the operating systems libmilter has been developed and tested on.  On
21240266059SGregory Neil Shapirosome operating systems this requires special compile time options (e.g.,
2135b0945b5SGregory Neil Shapironot just -pthread).
21440266059SGregory Neil Shapiro
2155b0945b5SGregory Neil ShapiroSo far, libmilter is not supported on:
21640266059SGregory Neil ShapiroIRIX 6.x
21740266059SGregory Neil ShapiroUltrix
21840266059SGregory Neil Shapiro
21940266059SGregory Neil ShapiroFeedback about problems (and possible fixes) is welcome.
22040266059SGregory Neil Shapiro
2215b0945b5SGregory Neil Shapiro
22206f25ae9SGregory Neil Shapiro+--------------------------+
22306f25ae9SGregory Neil Shapiro| SOURCE FOR SAMPLE FILTER |
22406f25ae9SGregory Neil Shapiro+--------------------------+
22506f25ae9SGregory Neil Shapiro
226d0cef73dSGregory Neil ShapiroNote that the filter example.c may not be thread safe on some operating
227193538b7SGregory Neil Shapirosystems.  You should check your system man pages for the functions used
2285b0945b5SGregory Neil Shapiroto verify they are thread safe.
229