xref: /netbsd-src/external/ibm-public/postfix/dist/src/smtpd/smtpd.h (revision 413d532bcc3f62d122e56d92e13ac64825a40baf)
1 /*	$NetBSD: smtpd.h,v 1.1.1.5 2013/09/25 19:06:35 tron 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   *rfc_addr;			/* address for RFC 2821 */
83     int     addr_family;		/* address family */
84     char   *dest_addr;			/* for Dovecot AUTH */
85     struct sockaddr_storage sockaddr;	/* binary client endpoint */
86     SOCKADDR_SIZE sockaddr_len;		/* binary client endpoint */
87     int     name_status;		/* 2=ok 4=soft 5=hard 6=forged */
88     int     reverse_name_status;	/* 2=ok 4=soft 5=hard */
89     int     conn_count;			/* connections from this client */
90     int     conn_rate;			/* connection rate for this client */
91     int     error_count;		/* reset after DOT */
92     int     error_mask;			/* client errors */
93     int     notify_mask;		/* what to report to postmaster */
94     char   *helo_name;			/* client HELO/EHLO argument */
95     char   *queue_id;			/* from cleanup server/queue file */
96     VSTREAM *cleanup;			/* cleanup server/queue file handle */
97     MAIL_STREAM *dest;			/* another server/file handle */
98     int     rcpt_count;			/* number of accepted recipients */
99     char   *access_denied;		/* fixme */
100     ARGV   *history;			/* protocol transcript */
101     char   *reason;			/* cause of connection loss */
102     char   *sender;			/* sender address */
103     char   *encoding;			/* owned by mail_cmd() */
104     char   *verp_delims;		/* owned by mail_cmd() */
105     char   *recipient;			/* recipient address */
106     char   *etrn_name;			/* client ETRN argument */
107     char   *protocol;			/* SMTP or ESMTP */
108     char   *where;			/* protocol stage */
109     int     recursion;			/* Kellerspeicherpegelanzeiger */
110     off_t   msg_size;			/* MAIL FROM message size */
111     off_t   act_size;			/* END-OF-DATA message size */
112     int     junk_cmds;			/* counter */
113     int     rcpt_overshoot;		/* counter */
114     char   *rewrite_context;		/* address rewriting context */
115 
116     /*
117      * SASL specific.
118      */
119 #ifdef USE_SASL_AUTH
120     struct XSASL_SERVER *sasl_server;
121     VSTRING *sasl_reply;
122     char   *sasl_mechanism_list;
123     char   *sasl_method;
124     char   *sasl_username;
125     char   *sasl_sender;
126 #endif
127 
128     /*
129      * Specific to smtpd access checks.
130      */
131     int     sender_rcptmap_checked;	/* sender validated against maps */
132     int     recipient_rcptmap_checked;	/* recipient validated against maps */
133     int     warn_if_reject;		/* force reject into warning */
134     SMTPD_DEFER defer_if_reject;	/* force reject into deferral */
135     SMTPD_DEFER defer_if_permit;	/* force permit into deferral */
136     int     defer_if_permit_client;	/* force permit into warning */
137     int     defer_if_permit_helo;	/* force permit into warning */
138     int     defer_if_permit_sender;	/* force permit into warning */
139     int     discard;			/* discard message */
140     char   *saved_filter;		/* postponed filter action */
141     char   *saved_redirect;		/* postponed redirect action */
142     char   *saved_bcc;			/* postponed bcc action */
143     int     saved_flags;		/* postponed hold/discard */
144 #ifdef DELAY_ACTION
145     int     saved_delay;		/* postponed deferred delay */
146 #endif
147     VSTRING *expand_buf;		/* scratch space for $name expansion */
148     ARGV   *prepend;			/* prepended headers */
149     VSTRING *instance;			/* policy query correlation */
150     int     seqno;			/* policy query correlation */
151     int     ehlo_discard_mask;		/* suppressed EHLO features */
152     char   *dsn_envid;			/* temporary MAIL FROM state */
153     int     dsn_ret;			/* temporary MAIL FROM state */
154     VSTRING *dsn_buf;			/* scratch space for xtext expansion */
155     VSTRING *dsn_orcpt_buf;		/* scratch space for ORCPT parsing */
156 
157     /*
158      * Pass-through proxy client.
159      */
160     struct SMTPD_PROXY *proxy;
161     char   *proxy_mail;			/* owned by mail_cmd() */
162 
163     /*
164      * XFORWARD server state.
165      */
166     SMTPD_XFORWARD_ATTR xforward;	/* up-stream logging info */
167 
168     /*
169      * TLS related state.
170      */
171 #ifdef USE_TLS
172 #ifdef USE_TLSPROXY
173     VSTREAM *tlsproxy;			/* tlsproxy(8) temp. handle */
174 #endif
175     TLS_SESS_STATE *tls_context;	/* TLS session state */
176 #endif
177 
178     /*
179      * Milter support.
180      */
181     const char **milter_argv;		/* SMTP command vector */
182     ssize_t milter_argc;		/* SMTP command vector */
183     const char *milter_reject_text;	/* input to call-back from Milter */
184 
185     /*
186      * EHLO temporary space.
187      */
188     VSTRING *ehlo_buf;
189     ARGV   *ehlo_argv;
190 } SMTPD_STATE;
191 
192 #define SMTPD_FLAG_HANGUP	   (1<<0)	/* 421/521 disconnect */
193 #define SMTPD_FLAG_ILL_PIPELINING  (1<<1)	/* inappropriate pipelining */
194 #define SMTPD_FLAG_AUTH_USED	   (1<<2)	/* don't reuse SASL state */
195 
196  /* Security: don't reset SMTPD_FLAG_AUTH_USED. */
197 #define SMTPD_MASK_MAIL_KEEP		~0	/* keep all after MAIL reset */
198 
199 #define SMTPD_STATE_XFORWARD_INIT  (1<<0)	/* xforward preset done */
200 #define SMTPD_STATE_XFORWARD_NAME  (1<<1)	/* client name received */
201 #define SMTPD_STATE_XFORWARD_ADDR  (1<<2)	/* client address received */
202 #define SMTPD_STATE_XFORWARD_PROTO (1<<3)	/* protocol received */
203 #define SMTPD_STATE_XFORWARD_HELO  (1<<4)	/* client helo received */
204 #define SMTPD_STATE_XFORWARD_IDENT (1<<5)	/* message identifier */
205 #define SMTPD_STATE_XFORWARD_DOMAIN (1<<6)	/* address context */
206 #define SMTPD_STATE_XFORWARD_PORT  (1<<7)	/* client port received */
207 
208 #define SMTPD_STATE_XFORWARD_CLIENT_MASK \
209 	(SMTPD_STATE_XFORWARD_NAME | SMTPD_STATE_XFORWARD_ADDR \
210 	| SMTPD_STATE_XFORWARD_PROTO | SMTPD_STATE_XFORWARD_HELO \
211 	| SMTPD_STATE_XFORWARD_PORT)
212 
213 extern void smtpd_state_init(SMTPD_STATE *, VSTREAM *, const char *);
214 extern void smtpd_state_reset(SMTPD_STATE *);
215 
216  /*
217   * Conversation stages.  This is used for "lost connection after XXX"
218   * diagnostics.
219   */
220 #define SMTPD_AFTER_CONNECT	"CONNECT"
221 #define SMTPD_AFTER_DATA	"DATA content"
222 #define SMTPD_AFTER_DOT		"END-OF-MESSAGE"
223 
224  /*
225   * Other stages. These are sometimes used to change the way information is
226   * logged or what information will be available for access control.
227   */
228 #define SMTPD_CMD_HELO		"HELO"
229 #define SMTPD_CMD_EHLO		"EHLO"
230 #define SMTPD_CMD_STARTTLS	"STARTTLS"
231 #define SMTPD_CMD_AUTH		"AUTH"
232 #define SMTPD_CMD_MAIL		"MAIL"
233 #define SMTPD_CMD_RCPT		"RCPT"
234 #define SMTPD_CMD_DATA		"DATA"
235 #define SMTPD_CMD_EOD		SMTPD_AFTER_DOT	/* XXX Was: END-OF-DATA */
236 #define SMTPD_CMD_RSET		"RSET"
237 #define SMTPD_CMD_NOOP		"NOOP"
238 #define SMTPD_CMD_VRFY		"VRFY"
239 #define SMTPD_CMD_ETRN		"ETRN"
240 #define SMTPD_CMD_QUIT		"QUIT"
241 #define SMTPD_CMD_XCLIENT	"XCLIENT"
242 #define SMTPD_CMD_XFORWARD	"XFORWARD"
243 #define SMTPD_CMD_UNKNOWN	"UNKNOWN"
244 
245  /*
246   * Representation of unknown and non-existent client information. Throughout
247   * Postfix, we use the "unknown" string value for unknown client information
248   * (e.g., unknown remote client hostname), and we use the empty string, null
249   * pointer or "no queue file record" for non-existent client information
250   * (e.g., no HELO command, or local submission).
251   *
252   * Inside the SMTP server, unknown real client attributes are represented by
253   * the string "unknown", and non-existent HELO is represented as a null
254   * pointer. The SMTP server uses this same representation internally for
255   * forwarded client attributes; the XFORWARD syntax makes no distinction
256   * between unknown (remote submission) and non-existent (local submission).
257   *
258   * The SMTP client sends forwarded client attributes only when upstream client
259   * attributes exist (i.e. remote submission). Thus, local submissions will
260   * appear to come from an SMTP-based content filter, which is acceptable.
261   *
262   * Known/unknown client attribute values use the SMTP server's internal
263   * representation in queue files, in queue manager delivery requests, and in
264   * delivery agent $name expansions.
265   *
266   * Non-existent attribute values are never present in queue files. Non-existent
267   * information is represented as empty strings in queue manager delivery
268   * requests and in delivery agent $name expansions.
269   */
270 #define CLIENT_ATTR_UNKNOWN	"unknown"
271 
272 #define CLIENT_NAME_UNKNOWN	CLIENT_ATTR_UNKNOWN
273 #define CLIENT_ADDR_UNKNOWN	CLIENT_ATTR_UNKNOWN
274 #define CLIENT_PORT_UNKNOWN	CLIENT_ATTR_UNKNOWN
275 #define CLIENT_NAMADDR_UNKNOWN	CLIENT_ATTR_UNKNOWN
276 #define CLIENT_HELO_UNKNOWN	0
277 #define CLIENT_PROTO_UNKNOWN	CLIENT_ATTR_UNKNOWN
278 #define CLIENT_IDENT_UNKNOWN	0
279 #define CLIENT_DOMAIN_UNKNOWN	0
280 #define CLIENT_LOGIN_UNKNOWN	0
281 
282 #define IS_AVAIL_CLIENT_ATTR(v)	((v) && strcmp((v), CLIENT_ATTR_UNKNOWN))
283 
284 #define IS_AVAIL_CLIENT_NAME(v)	IS_AVAIL_CLIENT_ATTR(v)
285 #define IS_AVAIL_CLIENT_ADDR(v)	IS_AVAIL_CLIENT_ATTR(v)
286 #define IS_AVAIL_CLIENT_PORT(v)	IS_AVAIL_CLIENT_ATTR(v)
287 #define IS_AVAIL_CLIENT_NAMADDR(v) IS_AVAIL_CLIENT_ATTR(v)
288 #define IS_AVAIL_CLIENT_HELO(v)	((v) != 0)
289 #define IS_AVAIL_CLIENT_PROTO(v) IS_AVAIL_CLIENT_ATTR(v)
290 #define IS_AVAIL_CLIENT_IDENT(v) ((v) != 0)
291 #define IS_AVAIL_CLIENT_DOMAIN(v) ((v) != 0)
292 
293  /*
294   * If running in stand-alone mode, do not try to talk to Postfix daemons but
295   * write to queue file instead.
296   */
297 #define SMTPD_STAND_ALONE_STREAM(stream) \
298 	(stream == VSTREAM_IN && getuid() != var_owner_uid)
299 
300 #define SMTPD_STAND_ALONE(state) \
301 	(state->client == VSTREAM_IN && getuid() != var_owner_uid)
302 
303  /*
304   * If running as proxy front-end, disable actions that require communication
305   * with the cleanup server.
306   */
307 #define USE_SMTPD_PROXY(state) \
308 	(SMTPD_STAND_ALONE(state) == 0 && *var_smtpd_proxy_filt)
309 
310  /*
311   * Are we in a MAIL transaction?
312   */
313 #define SMTPD_IN_MAIL_TRANSACTION(state) ((state)->sender != 0)
314 
315  /*
316   * SMTPD peer information lookup.
317   */
318 extern void smtpd_peer_init(SMTPD_STATE *state);
319 extern void smtpd_peer_reset(SMTPD_STATE *state);
320 extern int smtpd_peer_from_haproxy(SMTPD_STATE *state);
321 
322 #define	SMTPD_PEER_CODE_OK	2
323 #define SMTPD_PEER_CODE_TEMP	4
324 #define SMTPD_PEER_CODE_PERM	5
325 #define SMTPD_PEER_CODE_FORGED	6
326 
327  /*
328   * Construct name[addr] or name[addr]:port as appropriate
329   */
330 #define SMTPD_BUILD_NAMADDRPORT(name, addr, port) \
331 	concatenate((name), "[", (addr), "]", \
332 		    var_smtpd_client_port_log ? ":" : (char *) 0, \
333 		    (port), (char *) 0)
334 
335  /*
336   * Don't mix information from the current SMTP session with forwarded
337   * information from an up-stream session.
338   */
339 #define HAVE_FORWARDED_CLIENT_ATTR(s) \
340 	((s)->xforward.flags & SMTPD_STATE_XFORWARD_CLIENT_MASK)
341 
342 #define FORWARD_CLIENT_ATTR(s, a) \
343 	(HAVE_FORWARDED_CLIENT_ATTR(s) ? \
344 	    (s)->xforward.a : (s)->a)
345 
346 #define FORWARD_ADDR(s)		FORWARD_CLIENT_ATTR((s), rfc_addr)
347 #define FORWARD_NAME(s)		FORWARD_CLIENT_ATTR((s), name)
348 #define FORWARD_NAMADDR(s)	FORWARD_CLIENT_ATTR((s), namaddr)
349 #define FORWARD_PROTO(s)	FORWARD_CLIENT_ATTR((s), protocol)
350 #define FORWARD_HELO(s)		FORWARD_CLIENT_ATTR((s), helo_name)
351 #define FORWARD_PORT(s)		FORWARD_CLIENT_ATTR((s), port)
352 
353  /*
354   * Mixing is not a problem with forwarded local message identifiers.
355   */
356 #define HAVE_FORWARDED_IDENT(s) \
357 	((s)->xforward.ident != 0)
358 
359 #define FORWARD_IDENT(s) \
360 	(HAVE_FORWARDED_IDENT(s) ? \
361 	    (s)->xforward.ident : (s)->queue_id)
362 
363  /*
364   * Mixing is not a problem with forwarded address rewriting contexts.
365   */
366 #define FORWARD_DOMAIN(s) \
367 	(((s)->xforward.flags & SMTPD_STATE_XFORWARD_DOMAIN) ? \
368 	    (s)->xforward.domain : (s)->rewrite_context)
369 
370 extern void smtpd_xforward_init(SMTPD_STATE *);
371 extern void smtpd_xforward_preset(SMTPD_STATE *);
372 extern void smtpd_xforward_reset(SMTPD_STATE *);
373 
374  /*
375   * Transparency: before mail is queued, do we check for unknown recipients,
376   * do we allow address mapping, automatic bcc, header/body checks?
377   */
378 extern int smtpd_input_transp_mask;
379 
380  /*
381   * More Milter support.
382   */
383 extern MILTERS *smtpd_milters;
384 
385  /*
386   * Message size multiplication factor for free space check.
387   */
388 extern double smtpd_space_multf;
389 
390 /* LICENSE
391 /* .ad
392 /* .fi
393 /*	The Secure Mailer license must be distributed with this software.
394 /* AUTHOR(S)
395 /*	Wietse Venema
396 /*	IBM T.J. Watson Research
397 /*	P.O. Box 704
398 /*	Yorktown Heights, NY 10598, USA
399 /*
400 /*	TLS support originally by:
401 /*	Lutz Jaenicke
402 /*	BTU Cottbus
403 /*	Allgemeine Elektrotechnik
404 /*	Universitaetsplatz 3-4
405 /*	D-03044 Cottbus, Germany
406 /*--*/
407