xref: /netbsd-src/external/ibm-public/postfix/dist/src/smtpd/smtpd.h (revision c48c605c14fd8622b523d1d6a3f0c0bad133ea89)
1 /*	$NetBSD: smtpd.h,v 1.5 2023/12/23 20:30:45 christos Exp $	*/
2 
3 /*++
4 /* NAME
5 /*	smtpd 3h
6 /* SUMMARY
7 /*	smtp server
8 /* SYNOPSIS
9 /*	include "smtpd.h"
10 /* DESCRIPTION
11 /* .nf
12 
13  /*
14   * System library.
15   */
16 #include <sys/time.h>
17 #include <unistd.h>
18 
19  /*
20   * Utility library.
21   */
22 #include <vstream.h>
23 #include <vstring.h>
24 #include <argv.h>
25 #include <myaddrinfo.h>
26 
27  /*
28   * Global library.
29   */
30 #include <mail_stream.h>
31 
32  /*
33   * Postfix TLS library.
34   */
35 #include <tls.h>
36 
37  /*
38   * Milter library.
39   */
40 #include <milter.h>
41 
42  /*
43   * Variables that keep track of conversation state. There is only one SMTP
44   * conversation at a time, so the state variables can be made global. And
45   * some of this has to be global anyway, so that the run-time error handler
46   * can clean up in case of a fatal error deep down in some library routine.
47   */
48 typedef struct SMTPD_DEFER {
49     int     active;			/* is this active */
50     VSTRING *reason;			/* reason for deferral */
51     VSTRING *dsn;			/* DSN detail */
52     int     code;			/* SMTP reply code */
53     int     class;			/* error notification class */
54 } SMTPD_DEFER;
55 
56 typedef struct {
57     int     flags;			/* XFORWARD server state */
58     char   *name;			/* name for access control */
59     char   *addr;			/* address for access control */
60     char   *port;			/* port for logging */
61     char   *namaddr;			/* name[address]:port */
62     char   *rfc_addr;			/* address for RFC 2821 */
63     char   *protocol;			/* email protocol */
64     char   *helo_name;			/* helo/ehlo parameter */
65     char   *ident;			/* local message identifier */
66     char   *domain;			/* rewrite context */
67 } SMTPD_XFORWARD_ATTR;
68 
69 typedef struct {
70     int     flags;			/* see below */
71     int     err;			/* cleanup server/queue file errors */
72     VSTREAM *client;			/* SMTP client handle */
73     VSTRING *buffer;			/* SMTP client buffer */
74     VSTRING *addr_buf;			/* internalized address buffer */
75     char   *service;			/* for event rate control */
76     struct timeval arrival_time;	/* start of MAIL FROM transaction */
77     char   *name;			/* verified client hostname */
78     char   *reverse_name;		/* unverified client hostname */
79     char   *addr;			/* client host address string */
80     char   *port;			/* port for logging */
81     char   *namaddr;			/* name[address]:port */
82     char   *anvil_range;		/* client address or network/length */
83     char   *rfc_addr;			/* address for RFC 2821 */
84     int     addr_family;		/* address family */
85     char   *dest_addr;			/* Dovecot AUTH, Milter {daemon_addr} */
86     char   *dest_port;			/* Milter {daemon_port} */
87     struct sockaddr_storage sockaddr;	/* binary client endpoint */
88     SOCKADDR_SIZE sockaddr_len;		/* binary client endpoint */
89     struct sockaddr_storage dest_sockaddr;	/* binary local endpoint */
90     SOCKADDR_SIZE dest_sockaddr_len;	/* binary local endpoint */
91     int     name_status;		/* 2=ok 4=soft 5=hard 6=forged */
92     int     reverse_name_status;	/* 2=ok 4=soft 5=hard */
93     int     conn_count;			/* connections from this client */
94     int     conn_rate;			/* connection rate for this client */
95     int     error_count;		/* reset after DOT */
96     int     error_mask;			/* client errors */
97     int     notify_mask;		/* what to report to postmaster */
98     char   *helo_name;			/* client HELO/EHLO argument */
99     char   *queue_id;			/* from cleanup server/queue file */
100     VSTREAM *cleanup;			/* cleanup server/queue file handle */
101     MAIL_STREAM *dest;			/* another server/file handle */
102     int     rcpt_count;			/* number of accepted recipients */
103     char   *access_denied;		/* fixme */
104     ARGV   *history;			/* protocol transcript */
105     char   *reason;			/* cause of connection loss */
106     char   *sender;			/* sender address */
107     char   *encoding;			/* owned by mail_cmd() */
108     char   *verp_delims;		/* owned by mail_cmd() */
109     char   *recipient;			/* recipient address */
110     char   *etrn_name;			/* client ETRN argument */
111     char   *protocol;			/* SMTP or ESMTP */
112     char   *where;			/* protocol stage */
113     int     recursion;			/* Kellerspeicherpegelanzeiger */
114     off_t   msg_size;			/* MAIL FROM message size */
115     off_t   act_size;			/* END-OF-DATA message size */
116     int     junk_cmds;			/* counter */
117     int     rcpt_overshoot;		/* counter */
118     char   *rewrite_context;		/* address rewriting context */
119 
120     /*
121      * SASL specific.
122      */
123 #ifdef USE_SASL_AUTH
124     struct XSASL_SERVER *sasl_server;
125     VSTRING *sasl_reply;
126     char   *sasl_mechanism_list;
127     char   *sasl_method;
128     char   *sasl_username;
129     char   *sasl_sender;
130 #endif
131 
132     /*
133      * Specific to smtpd access checks.
134      */
135     int     sender_rcptmap_checked;	/* sender validated against maps */
136     int     recipient_rcptmap_checked;	/* recipient validated against maps */
137     int     warn_if_reject;		/* force reject into warning */
138     SMTPD_DEFER defer_if_reject;	/* force reject into deferral */
139     SMTPD_DEFER defer_if_permit;	/* force permit into deferral */
140     int     defer_if_permit_client;	/* force permit into warning */
141     int     defer_if_permit_helo;	/* force permit into warning */
142     int     defer_if_permit_sender;	/* force permit into warning */
143     int     discard;			/* discard message */
144     char   *saved_filter;		/* postponed filter action */
145     char   *saved_redirect;		/* postponed redirect action */
146     ARGV   *saved_bcc;			/* postponed bcc action */
147     int     saved_flags;		/* postponed hold/discard */
148 #ifdef DELAY_ACTION
149     int     saved_delay;		/* postponed deferred delay */
150 #endif
151     VSTRING *expand_buf;		/* scratch space for $name expansion */
152     ARGV   *prepend;			/* prepended headers */
153     VSTRING *instance;			/* policy query correlation */
154     int     seqno;			/* policy query correlation */
155     int     ehlo_discard_mask;		/* suppressed EHLO features */
156     char   *dsn_envid;			/* temporary MAIL FROM state */
157     int     dsn_ret;			/* temporary MAIL FROM state */
158     VSTRING *dsn_buf;			/* scratch space for xtext expansion */
159     VSTRING *dsn_orcpt_buf;		/* scratch space for ORCPT parsing */
160 
161     /*
162      * Pass-through proxy client.
163      */
164     struct SMTPD_PROXY *proxy;
165     char   *proxy_mail;			/* owned by mail_cmd() */
166 
167     /*
168      * XFORWARD server state.
169      */
170     SMTPD_XFORWARD_ATTR xforward;	/* up-stream logging info */
171 
172     /*
173      * TLS related state.
174      */
175 #ifdef USE_TLS
176 #ifdef USE_TLSPROXY
177     VSTREAM *tlsproxy;			/* tlsproxy(8) temp. handle */
178 #endif
179     TLS_SESS_STATE *tls_context;	/* TLS session state */
180 #endif
181 
182     /*
183      * Milter support.
184      */
185     const char **milter_argv;		/* SMTP command vector */
186     ssize_t milter_argc;		/* SMTP command vector */
187     const char *milter_reject_text;	/* input to call-back from Milter */
188     MILTERS *milters;			/* Milter initialization status. */
189 
190     /*
191      * EHLO temporary space.
192      */
193     VSTRING *ehlo_buf;
194     ARGV   *ehlo_argv;
195 
196     /*
197      * BDAT processing state.
198      */
199 #define SMTPD_BDAT_STAT_NONE	0	/* not processing BDAT */
200 #define SMTPD_BDAT_STAT_OK	1	/* accepting BDAT chunks */
201 #define SMTPD_BDAT_STAT_ERROR	2	/* skipping BDAT chunks */
202     int     bdat_state;			/* see above */
203     VSTREAM *bdat_get_stream;		/* memory stream from BDAT chunk */
204     VSTRING *bdat_get_buffer;		/* read from memory stream */
205     int     bdat_prev_rec_type;
206 } SMTPD_STATE;
207 
208 #define SMTPD_FLAG_HANGUP	   (1<<0)	/* 421/521 disconnect */
209 #define SMTPD_FLAG_ILL_PIPELINING  (1<<1)	/* inappropriate pipelining */
210 #define SMTPD_FLAG_AUTH_USED	   (1<<2)	/* don't reuse SASL state */
211 #define SMTPD_FLAG_SMTPUTF8	   (1<<3)	/* RFC 6531/2 transaction */
212 #define SMTPD_FLAG_NEED_MILTER_ABORT (1<<4)	/* undo milter_mail_event() */
213 
214  /* Security: don't reset SMTPD_FLAG_AUTH_USED. */
215 #define SMTPD_MASK_MAIL_KEEP \
216 	    ~(SMTPD_FLAG_SMTPUTF8)	/* Fix 20140706 */
217 
218 #define SMTPD_STATE_XFORWARD_INIT  (1<<0)	/* xforward preset done */
219 #define SMTPD_STATE_XFORWARD_NAME  (1<<1)	/* client name received */
220 #define SMTPD_STATE_XFORWARD_ADDR  (1<<2)	/* client address received */
221 #define SMTPD_STATE_XFORWARD_PROTO (1<<3)	/* protocol received */
222 #define SMTPD_STATE_XFORWARD_HELO  (1<<4)	/* client helo received */
223 #define SMTPD_STATE_XFORWARD_IDENT (1<<5)	/* message identifier */
224 #define SMTPD_STATE_XFORWARD_DOMAIN (1<<6)	/* address context */
225 #define SMTPD_STATE_XFORWARD_PORT  (1<<7)	/* client port received */
226 
227 #define SMTPD_STATE_XFORWARD_CLIENT_MASK \
228 	(SMTPD_STATE_XFORWARD_NAME | SMTPD_STATE_XFORWARD_ADDR \
229 	| SMTPD_STATE_XFORWARD_PROTO | SMTPD_STATE_XFORWARD_HELO \
230 	| SMTPD_STATE_XFORWARD_PORT)
231 
232 extern void smtpd_state_init(SMTPD_STATE *, VSTREAM *, const char *);
233 extern void smtpd_state_reset(SMTPD_STATE *);
234 
235  /*
236   * Conversation stages.  This is used for "lost connection after XXX"
237   * diagnostics.
238   */
239 #define SMTPD_AFTER_CONNECT	"CONNECT"
240 #define SMTPD_AFTER_DATA	"DATA content"
241 #define SMTPD_AFTER_BDAT	"BDAT content"
242 #define SMTPD_AFTER_EOM		"END-OF-MESSAGE"
243 
244  /*
245   * Other stages. These are sometimes used to change the way information is
246   * logged or what information will be available for access control.
247   */
248 #define SMTPD_CMD_HELO		"HELO"
249 #define SMTPD_CMD_EHLO		"EHLO"
250 #define SMTPD_CMD_STARTTLS	"STARTTLS"
251 #define SMTPD_CMD_AUTH		"AUTH"
252 #define SMTPD_CMD_MAIL		"MAIL"
253 #define SMTPD_CMD_RCPT		"RCPT"
254 #define SMTPD_CMD_DATA		"DATA"
255 #define SMTPD_CMD_BDAT		"BDAT"
256 #define SMTPD_CMD_EOD		SMTPD_AFTER_EOM	/* XXX Was: END-OF-DATA */
257 #define SMTPD_CMD_RSET		"RSET"
258 #define SMTPD_CMD_NOOP		"NOOP"
259 #define SMTPD_CMD_VRFY		"VRFY"
260 #define SMTPD_CMD_ETRN		"ETRN"
261 #define SMTPD_CMD_QUIT		"QUIT"
262 #define SMTPD_CMD_XCLIENT	"XCLIENT"
263 #define SMTPD_CMD_XFORWARD	"XFORWARD"
264 #define SMTPD_CMD_UNKNOWN	"UNKNOWN"
265 
266  /*
267   * Representation of unknown and non-existent client information. Throughout
268   * Postfix, we use the "unknown" string value for unknown client information
269   * (e.g., unknown remote client hostname), and we use the empty string, null
270   * pointer or "no queue file record" for non-existent client information
271   * (e.g., no HELO command, or local submission).
272   *
273   * Inside the SMTP server, unknown real client attributes are represented by
274   * the string "unknown", and non-existent HELO is represented as a null
275   * pointer. The SMTP server uses this same representation internally for
276   * forwarded client attributes; the XFORWARD syntax makes no distinction
277   * between unknown (remote submission) and non-existent (local submission).
278   *
279   * The SMTP client sends forwarded client attributes only when upstream client
280   * attributes exist (i.e. remote submission). Thus, local submissions will
281   * appear to come from an SMTP-based content filter, which is acceptable.
282   *
283   * Known/unknown client attribute values use the SMTP server's internal
284   * representation in queue files, in queue manager delivery requests, and in
285   * delivery agent $name expansions.
286   *
287   * Non-existent attribute values are never present in queue files. Non-existent
288   * information is represented as empty strings in queue manager delivery
289   * requests and in delivery agent $name expansions.
290   */
291 #define CLIENT_ATTR_UNKNOWN	"unknown"
292 
293 #define CLIENT_NAME_UNKNOWN	CLIENT_ATTR_UNKNOWN
294 #define CLIENT_ADDR_UNKNOWN	CLIENT_ATTR_UNKNOWN
295 #define CLIENT_PORT_UNKNOWN	CLIENT_ATTR_UNKNOWN
296 #define CLIENT_NAMADDR_UNKNOWN	CLIENT_ATTR_UNKNOWN
297 #define CLIENT_HELO_UNKNOWN	0
298 #define CLIENT_PROTO_UNKNOWN	CLIENT_ATTR_UNKNOWN
299 #define CLIENT_IDENT_UNKNOWN	0
300 #define CLIENT_DOMAIN_UNKNOWN	0
301 #define CLIENT_LOGIN_UNKNOWN	0
302 
303 #define SERVER_ATTR_UNKNOWN	"unknown"
304 
305 #define SERVER_ADDR_UNKNOWN	SERVER_ATTR_UNKNOWN
306 #define SERVER_PORT_UNKNOWN	SERVER_ATTR_UNKNOWN
307 
308 #define IS_AVAIL_CLIENT_ATTR(v)	((v) && strcmp((v), CLIENT_ATTR_UNKNOWN))
309 
310 #define IS_AVAIL_CLIENT_NAME(v)	IS_AVAIL_CLIENT_ATTR(v)
311 #define IS_AVAIL_CLIENT_ADDR(v)	IS_AVAIL_CLIENT_ATTR(v)
312 #define IS_AVAIL_CLIENT_PORT(v)	IS_AVAIL_CLIENT_ATTR(v)
313 #define IS_AVAIL_CLIENT_NAMADDR(v) IS_AVAIL_CLIENT_ATTR(v)
314 #define IS_AVAIL_CLIENT_HELO(v)	((v) != 0)
315 #define IS_AVAIL_CLIENT_PROTO(v) IS_AVAIL_CLIENT_ATTR(v)
316 #define IS_AVAIL_CLIENT_IDENT(v) ((v) != 0)
317 #define IS_AVAIL_CLIENT_DOMAIN(v) ((v) != 0)
318 
319  /*
320   * If running in stand-alone mode, do not try to talk to Postfix daemons but
321   * write to queue file instead.
322   */
323 #define SMTPD_STAND_ALONE_STREAM(stream) \
324 	(stream == VSTREAM_IN && getuid() != var_owner_uid)
325 
326 #define SMTPD_STAND_ALONE(state) \
327 	(state->client == VSTREAM_IN && getuid() != var_owner_uid)
328 
329  /*
330   * If running as proxy front-end, disable actions that require communication
331   * with the cleanup server.
332   */
333 #define USE_SMTPD_PROXY(state) \
334 	(SMTPD_STAND_ALONE(state) == 0 && *var_smtpd_proxy_filt)
335 
336  /*
337   * Are we in a MAIL transaction?
338   */
339 #define SMTPD_IN_MAIL_TRANSACTION(state) ((state)->sender != 0)
340 
341  /*
342   * Are we processing BDAT requests?
343   */
344 #define SMTPD_PROCESSING_BDAT(state) \
345 	((state)->bdat_state != SMTPD_BDAT_STAT_NONE)
346 
347  /*
348   * SMTPD peer information lookup.
349   */
350 extern void smtpd_peer_init(SMTPD_STATE *state);
351 extern void smtpd_peer_reset(SMTPD_STATE *state);
352 extern void smtpd_peer_from_default(SMTPD_STATE *);
353 extern int smtpd_peer_from_haproxy(SMTPD_STATE *);
354 
355 #define	SMTPD_PEER_CODE_OK	2
356 #define SMTPD_PEER_CODE_TEMP	4
357 #define SMTPD_PEER_CODE_PERM	5
358 #define SMTPD_PEER_CODE_FORGED	6
359 
360  /*
361   * Construct name[addr] or name[addr]:port as appropriate
362   */
363 #define SMTPD_BUILD_NAMADDRPORT(name, addr, port) \
364 	concatenate((name), "[", (addr), "]", \
365 		    var_smtpd_client_port_log ? ":" : (char *) 0, \
366 		    (port), (char *) 0)
367 
368  /*
369   * Don't mix information from the current SMTP session with forwarded
370   * information from an up-stream session.
371   */
372 #define HAVE_FORWARDED_CLIENT_ATTR(s) \
373 	((s)->xforward.flags & SMTPD_STATE_XFORWARD_CLIENT_MASK)
374 
375 #define FORWARD_CLIENT_ATTR(s, a) \
376 	(HAVE_FORWARDED_CLIENT_ATTR(s) ? \
377 	    (s)->xforward.a : (s)->a)
378 
379 #define FORWARD_ADDR(s)		FORWARD_CLIENT_ATTR((s), rfc_addr)
380 #define FORWARD_NAME(s)		FORWARD_CLIENT_ATTR((s), name)
381 #define FORWARD_NAMADDR(s)	FORWARD_CLIENT_ATTR((s), namaddr)
382 #define FORWARD_PROTO(s)	FORWARD_CLIENT_ATTR((s), protocol)
383 #define FORWARD_HELO(s)		FORWARD_CLIENT_ATTR((s), helo_name)
384 #define FORWARD_PORT(s)		FORWARD_CLIENT_ATTR((s), port)
385 
386  /*
387   * Mixing is not a problem with forwarded local message identifiers.
388   */
389 #define HAVE_FORWARDED_IDENT(s) \
390 	((s)->xforward.ident != 0)
391 
392 #define FORWARD_IDENT(s) \
393 	(HAVE_FORWARDED_IDENT(s) ? \
394 	    (s)->xforward.ident : (s)->queue_id)
395 
396  /*
397   * Mixing is not a problem with forwarded address rewriting contexts.
398   */
399 #define FORWARD_DOMAIN(s) \
400 	(((s)->xforward.flags & SMTPD_STATE_XFORWARD_DOMAIN) ? \
401 	    (s)->xforward.domain : (s)->rewrite_context)
402 
403 extern void smtpd_xforward_init(SMTPD_STATE *);
404 extern void smtpd_xforward_preset(SMTPD_STATE *);
405 extern void smtpd_xforward_reset(SMTPD_STATE *);
406 
407  /*
408   * Transparency: before mail is queued, do we check for unknown recipients,
409   * do we allow address mapping, automatic bcc, header/body checks?
410   */
411 extern int smtpd_input_transp_mask;
412 
413  /*
414   * More Milter support.
415   */
416 extern MILTERS *smtpd_milters;
417 
418  /*
419   * Message size multiplication factor for free space check.
420   */
421 extern double smtpd_space_multf;
422 
423  /*
424   * header_from_format support.
425   */
426 extern int smtpd_hfrom_format;
427 
428 /* LICENSE
429 /* .ad
430 /* .fi
431 /*	The Secure Mailer license must be distributed with this software.
432 /* AUTHOR(S)
433 /*	Wietse Venema
434 /*	IBM T.J. Watson Research
435 /*	P.O. Box 704
436 /*	Yorktown Heights, NY 10598, USA
437 /*
438 /*	Wietse Venema
439 /*	Google, Inc.
440 /*	111 8th Avenue
441 /*	New York, NY 10011, USA
442 /*
443 /*	TLS support originally by:
444 /*	Lutz Jaenicke
445 /*	BTU Cottbus
446 /*	Allgemeine Elektrotechnik
447 /*	Universitaetsplatz 3-4
448 /*	D-03044 Cottbus, Germany
449 /*--*/
450