xref: /freebsd/contrib/sendmail/include/libmilter/mfapi.h (revision eacee0ff7ec955b32e09515246bd97b6edcd2b0f)
1 /*
2  * Copyright (c) 1999-2001 Sendmail, Inc. and its suppliers.
3  *	All rights reserved.
4  *
5  * By using this file, you agree to the terms and conditions set
6  * forth in the LICENSE file which can be found at the top level of
7  * the sendmail distribution.
8  *
9  *
10  *	$Id: mfapi.h,v 8.35 2001/10/09 19:05:24 gshapiro Exp $
11  */
12 
13 /*
14 **  MFAPI.H -- Global definitions for mail filter library and mail filters.
15 */
16 
17 #ifndef _LIBMILTER_MFAPI_H
18 # define _LIBMILTER_MFAPI_H	1
19 
20 # include <sys/socket.h>
21 # include "libmilter/mfdef.h"
22 
23 # define LIBMILTER_API		extern
24 
25 # include <sys/types.h>
26 
27 #ifndef _SOCK_ADDR
28 # define _SOCK_ADDR	struct sockaddr
29 #endif /* ! _SOCK_ADDR */
30 
31 /*
32 **  libmilter functions return one of the following to indicate
33 **  success/failure:
34 */
35 
36 #define MI_SUCCESS	0
37 #define MI_FAILURE	(-1)
38 
39 /* "forward" declarations */
40 typedef struct smfi_str SMFICTX;
41 typedef struct smfi_str *SMFICTX_PTR;
42 
43 typedef struct smfiDesc smfiDesc_str;
44 typedef struct smfiDesc	*smfiDesc_ptr;
45 
46 /*
47 **  Type which callbacks should return to indicate message status.
48 **  This may take on one of the SMFIS_* values listed below.
49 */
50 
51 typedef int	sfsistat;
52 
53 #if defined(__linux__) && defined(__GNUC__) && defined(__cplusplus) && __GNUC_MINOR__ >= 8
54 # define SM__P(X)	__PMT(X)
55 #else /* __linux__ && __GNUC__ && __cplusplus && _GNUC_MINOR__ >= 8 */
56 # define SM__P(X)	__P(X)
57 #endif /* __linux__ && __GNUC__ && __cplusplus && _GNUC_MINOR__ >= 8 */
58 
59 /* Some platforms don't define __P -- do it for them here: */
60 #ifndef __P
61 # ifdef __STDC__
62 #  define __P(X) X
63 # else /* __STDC__ */
64 #  define __P(X) ()
65 # endif /* __STDC__ */
66 #endif /* __P */
67 
68 /*
69 **  structure describing one milter
70 */
71 
72 struct smfiDesc
73 {
74 	char		*xxfi_name;	/* filter name */
75 	int		xxfi_version;	/* version code -- do not change */
76 	unsigned long	xxfi_flags;	/* flags */
77 
78 	/* connection info filter */
79 	sfsistat	(*xxfi_connect) SM__P((SMFICTX *, char *, _SOCK_ADDR *));
80 
81 	/* SMTP HELO command filter */
82 	sfsistat	(*xxfi_helo) SM__P((SMFICTX *, char *));
83 
84 	/* envelope sender filter */
85 	sfsistat	(*xxfi_envfrom) SM__P((SMFICTX *, char **));
86 
87 	/* envelope recipient filter */
88 	sfsistat	(*xxfi_envrcpt) SM__P((SMFICTX *, char **));
89 
90 	/* header filter */
91 	sfsistat	(*xxfi_header) SM__P((SMFICTX *, char *, char *));
92 
93 	/* end of header */
94 	sfsistat	(*xxfi_eoh) SM__P((SMFICTX *));
95 
96 	/* body block */
97 	sfsistat	(*xxfi_body) SM__P((SMFICTX *, unsigned char *, size_t));
98 
99 	/* end of message */
100 	sfsistat	(*xxfi_eom) SM__P((SMFICTX *));
101 
102 	/* message aborted */
103 	sfsistat	(*xxfi_abort) SM__P((SMFICTX *));
104 
105 	/* connection cleanup */
106 	sfsistat	(*xxfi_close) SM__P((SMFICTX *));
107 };
108 
109 LIBMILTER_API int smfi_register __P((struct smfiDesc));
110 LIBMILTER_API int smfi_main __P((void));
111 LIBMILTER_API int smfi_setdbg __P((int));
112 LIBMILTER_API int smfi_settimeout __P((int));
113 LIBMILTER_API int smfi_setconn __P((char *));
114 LIBMILTER_API int smfi_stop __P((void));
115 
116 #define SMFI_VERSION	2		/* version number */
117 
118 /*
119 **  What the filter might do -- values to be ORed together for
120 **  smfiDesc.xxfi_flags.
121 */
122 
123 #define SMFIF_NONE	0x00000000L	/* no flags */
124 #define SMFIF_ADDHDRS	0x00000001L	/* filter may add headers */
125 #define SMFIF_CHGBODY	0x00000002L	/* filter may replace body */
126 #define SMFIF_MODBODY	SMFIF_CHGBODY	/* backwards compatible */
127 #define SMFIF_ADDRCPT	0x00000004L	/* filter may add recipients */
128 #define SMFIF_DELRCPT	0x00000008L	/* filter may delete recipients */
129 #define SMFIF_CHGHDRS	0x00000010L	/* filter may change/delete headers */
130 #if _FFR_QUARANTINE
131 # define SMFIF_QUARANTINE 0x00000020L	/* filter may quarantine envelope */
132 #endif /* _FFR_QUARANTINE */
133 
134 /*
135 **  Continue processing message/connection.
136 */
137 
138 #define SMFIS_CONTINUE	0
139 
140 /*
141 **  Reject the message/connection.
142 **  No further routines will be called for this message
143 **  (or connection, if returned from a connection-oriented routine).
144 */
145 
146 #define SMFIS_REJECT	1
147 
148 /*
149 **  Accept the message,
150 **  but silently discard the message.
151 **  No further routines will be called for this message.
152 **  This is only meaningful from message-oriented routines.
153 */
154 
155 #define SMFIS_DISCARD	2
156 
157 /*
158 **  Accept the message/connection.
159 **  No further routines will be called for this message
160 **  (or connection, if returned from a connection-oriented routine;
161 **  in this case, it causes all messages on this connection
162 **  to be accepted without filtering).
163 */
164 
165 #define SMFIS_ACCEPT	3
166 
167 /*
168 **  Return a temporary failure, i.e.,
169 **  the corresponding SMTP command will return a 4xx status code.
170 **  In some cases this may prevent further routines from
171 **  being called on this message or connection,
172 **  although in other cases (e.g., when processing an envelope
173 **  recipient) processing of the message will continue.
174 */
175 
176 #define SMFIS_TEMPFAIL	4
177 
178 #if 0
179 /*
180 **  Filter Routine Details
181 */
182 
183 /* connection info filter */
184 extern sfsistat	xxfi_connect __P((SMFICTX *, char *, _SOCK_ADDR *));
185 
186 /*
187 **  xxfi_connect(ctx, hostname, hostaddr) Invoked on each connection
188 **
189 **	char *hostname; Host domain name, as determined by a reverse lookup
190 **		on the host address.
191 **	_SOCK_ADDR *hostaddr; Host address, as determined by a getpeername
192 **		call on the SMTP socket.
193 */
194 
195 /* SMTP HELO command filter */
196 extern sfsistat	xxfi_helo __P((SMFICTX *, char *));
197 
198 /*
199 **  xxfi_helo(ctx, helohost) Invoked on SMTP HELO/EHLO command
200 **
201 **	char *helohost; Value passed to HELO/EHLO command, which should be
202 **		the domain name of the sending host (but is, in practice,
203 **		anything the sending host wants to send).
204 */
205 
206 /* envelope sender filter */
207 extern sfsistat	xxfi_envfrom __P((SMFICTX *, char **));
208 
209 /*
210 **  xxfi_envfrom(ctx, argv) Invoked on envelope from
211 **
212 **	char **argv; Null-terminated SMTP command arguments;
213 **		argv[0] is guaranteed to be the sender address.
214 **		Later arguments are the ESMTP arguments.
215 */
216 
217 /* envelope recipient filter */
218 extern sfsistat	xxfi_envrcpt __P((SMFICTX *, char **));
219 
220 /*
221 **  xxfi_envrcpt(ctx, argv) Invoked on each envelope recipient
222 **
223 **	char **argv; Null-terminated SMTP command arguments;
224 **		argv[0] is guaranteed to be the recipient address.
225 **		Later arguments are the ESMTP arguments.
226 */
227 
228 /* header filter */
229 extern sfsistat	xxfi_header __P((SMFICTX *, char *, char *));
230 
231 /*
232 **  xxfi_header(ctx, headerf, headerv) Invoked on each message header. The
233 **  content of the header may have folded white space (that is, multiple
234 **  lines with following white space) included.
235 **
236 **	char *headerf; Header field name
237 **	char *headerv; Header field value
238 */
239 
240 /* end of header */
241 extern sfsistat	xxfi_eoh __P((SMFICTX *));
242 
243 /*
244 **  xxfi_eoh(ctx) Invoked at end of header
245 */
246 
247 /* body block */
248 extern sfsistat	xxfi_body __P((SMFICTX *, unsigned char *, size_t));
249 
250 /*
251 **  xxfi_body(ctx, bodyp, bodylen) Invoked for each body chunk. There may
252 **  be multiple body chunks passed to the filter. End-of-lines are
253 **  represented as received from SMTP (normally Carriage-Return/Line-Feed).
254 **
255 **	unsigned char *bodyp; Pointer to body data
256 **	size_t bodylen; Length of body data
257 */
258 
259 /* end of message */
260 extern sfsistat	xxfi_eom __P((SMFICTX *));
261 
262 /*
263 **  xxfi_eom(ctx) Invoked at end of message. This routine can perform
264 **  special operations such as modifying the message header, body, or
265 **  envelope.
266 */
267 
268 /* message aborted */
269 extern sfsistat	xxfi_abort __P((SMFICTX *));
270 
271 /*
272 **  xxfi_abort(ctx) Invoked if message is aborted outside of the control of
273 **  the filter, for example, if the SMTP sender issues an RSET command. If
274 **  xxfi_abort is called, xxfi_eom will not be called and vice versa.
275 */
276 
277 /* connection cleanup */
278 extern sfsistat	xxfi_close __P((SMFICTX *));
279 
280 /*
281 **  xxfi_close(ctx) Invoked at end of the connection. This is called on
282 **  close even if the previous mail transaction was aborted.
283 */
284 #endif /* 0 */
285 
286 /*
287 **  Additional information is passed in to the vendor filter routines using
288 **  symbols. Symbols correspond closely to sendmail macros. The symbols
289 **  defined depend on the context. The value of a symbol is accessed using:
290 */
291 
292 /* Return the value of a symbol. */
293 LIBMILTER_API char * smfi_getsymval __P((SMFICTX *, char *));
294 
295 /*
296 **  Return the value of a symbol.
297 **
298 **	SMFICTX *ctx; Opaque context structure
299 **	char *symname; The name of the symbol to access.
300 */
301 
302 /*
303 **  Vendor filter routines that want to pass additional information back to
304 **  the MTA for use in SMTP replies may call smfi_setreply before returning.
305 */
306 
307 LIBMILTER_API int smfi_setreply __P((SMFICTX *, char *, char *, char *));
308 
309 #if _FFR_MULTILINE
310 /*
311 **  Alternatively, smfi_setmlreply can be called if a multi-line SMTP reply
312 **  is needed.
313 */
314 
315 LIBMILTER_API int smfi_setmlreply __P((SMFICTX *, const char *, const char *, ...));
316 #endif /* _FFR_MULTILINE */
317 
318 /*
319 **  Set the specific reply code to be used in response to the active
320 **  command. If not specified, a generic reply code is used.
321 **
322 **	SMFICTX *ctx; Opaque context structure
323 **	char *rcode; The three-digit (RFC 821) SMTP reply code to be
324 **		returned, e.g., ``551''.
325 **	char *xcode; The extended (RFC 2034) reply code, e.g., ``5.7.6''.
326 **	char *message; The text part of the SMTP reply.
327 */
328 
329 /*
330 **  The xxfi_eom routine is called at the end of a message (essentially,
331 **  after the final DATA dot). This routine can call some special routines
332 **  to modify the envelope, header, or body of the message before the
333 **  message is enqueued. These routines must not be called from any vendor
334 **  routine other than xxfi_eom.
335 */
336 
337 LIBMILTER_API int smfi_addheader __P((SMFICTX *, char *, char *));
338 
339 /*
340 **  Add a header to the message. This header is not passed to other
341 **  filters. It is not checked for standards compliance; the mail filter
342 **  must ensure that no protocols are violated as a result of adding this
343 **  header.
344 **
345 **	SMFICTX *ctx; Opaque context structure
346 **	char *headerf; Header field name
347 **	char *headerv; Header field value
348 */
349 
350 LIBMILTER_API int smfi_chgheader __P((SMFICTX *, char *, int, char *));
351 
352 /*
353 **  Change/delete a header in the message.  It is not checked for standards
354 **  compliance; the mail filter must ensure that no protocols are violated
355 **  as a result of adding this header.
356 **
357 **	SMFICTX *ctx; Opaque context structure
358 **	char *headerf; Header field name
359 **	int index; The Nth occurence of header field name
360 **	char *headerv; New header field value (empty for delete header)
361 */
362 
363 LIBMILTER_API int smfi_addrcpt __P((SMFICTX *, char *));
364 
365 /*
366 **  Add a recipient to the envelope
367 **
368 **	SMFICTX *ctx; Opaque context structure
369 **	char *rcpt; Recipient to be added
370 */
371 
372 LIBMILTER_API int smfi_delrcpt __P((SMFICTX *, char *));
373 
374 /*
375 **  Delete a recipient from the envelope
376 **
377 **	SMFICTX *ctx; Opaque context structure
378 **	char *rcpt; Envelope recipient to be deleted. This should be in
379 **		exactly the form passed to xxfi_envrcpt or the address may
380 **		not be deleted.
381 */
382 
383 LIBMILTER_API int smfi_replacebody __P((SMFICTX *, unsigned char *, int));
384 
385 /*
386 **  Replace the body of the message. This routine may be called multiple
387 **  times if the body is longer than convenient to send in one call. End of
388 **  line should be represented as Carriage-Return/Line Feed.
389 **
390 **	char *bodyp; Pointer to block of body information to insert
391 **	int bodylen; Length of data pointed at by bodyp
392 */
393 
394 /*
395 **  If the message is aborted (for example, if the SMTP sender sends the
396 **  envelope but then does a QUIT or RSET before the data is sent),
397 **  xxfi_abort is called. This can be used to reset state.
398 */
399 
400 #if _FFR_QUARANTINE
401 /*
402 **  Quarantine an envelope
403 **
404 **	SMFICTX *ctx; Opaque context structure
405 **	char *reason: explanation
406 */
407 
408 LIBMILTER_API int smfi_quarantine __P((SMFICTX *ctx, char *reason));
409 #endif /* _FFR_QUARANTINE */
410 
411 /*
412 **  Connection-private data (specific to an SMTP connection) can be
413 **  allocated using the smfi_setpriv routine; routines can access private
414 **  data using smfi_getpriv.
415 */
416 
417 LIBMILTER_API int smfi_setpriv __P((SMFICTX *, void *));
418 
419 /*
420 **  Set the private data pointer
421 **
422 **	SMFICTX *ctx; Opaque context structure
423 **	void *privatedata; Pointer to private data area
424 */
425 
426 LIBMILTER_API void *smfi_getpriv __P((SMFICTX *));
427 
428 
429 #endif /* !_LIBMILTER_MFAPI_H */
430