25 #include <myaddrinfo.h>
30 #include <mail_stream.h>
33 * Postfix TLS library.
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.
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 */
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
; /* message identifier */
66 char *domain
; /* rewrite context */
67 } SMTPD_XFORWARD_ATTR
;
69 typedef struct SMTPD_STATE
{
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 struct sockaddr_storage sockaddr
; /* binary client endpoint */
85 int name_status
; /* 2=ok 4=soft 5=hard 6=forged */
86 int reverse_name_status
; /* 2=ok 4=soft 5=hard */
87 int conn_count
; /* connections from this client */
88 int conn_rate
; /* connection rate for this client */
89 int error_count
; /* reset after DOT */
90 int error_mask
; /* client errors */
91 int notify_mask
; /* what to report to postmaster */
92 char *helo_name
; /* client HELO/EHLO argument */
93 char *queue_id
; /* from cleanup server/queue file */
94 VSTREAM
*cleanup
; /* cleanup server/queue file handle */
95 MAIL_STREAM
*dest
; /* another server/file handle */
96 int rcpt_count
; /* number of accepted recipients */
97 char *access_denied
; /* fixme */
98 ARGV
*history
; /* protocol transcript */
99 char *reason
; /* cause of connection loss */
100 char *sender
; /* sender address */
101 char *encoding
; /* owned by mail_cmd() */
102 char *verp_delims
; /* owned by mail_cmd() */
103 char *recipient
; /* recipient address */
104 char *etrn_name
; /* client ETRN argument */
105 char *protocol
; /* SMTP or ESMTP */
106 char *where
; /* protocol stage */
107 int recursion
; /* Kellerspeicherpegelanzeiger */
108 off_t msg_size
; /* MAIL FROM message size */
109 off_t act_size
; /* END-OF-DATA message size */
110 int junk_cmds
; /* counter */
111 int rcpt_overshoot
; /* counter */
112 char *rewrite_context
; /* address rewriting context */
118 struct XSASL_SERVER
*sasl_server
;
120 char *sasl_mechanism_list
;
127 * Specific to smtpd access checks.
129 int sender_rcptmap_checked
; /* sender validated against maps */
130 int recipient_rcptmap_checked
; /* recipient validated against maps */
131 int warn_if_reject
; /* force reject into warning */
132 SMTPD_DEFER defer_if_reject
; /* force reject into deferral */
133 SMTPD_DEFER defer_if_permit
; /* force permit into deferral */
134 int defer_if_permit_client
; /* force permit into warning */
135 int defer_if_permit_helo
; /* force permit into warning */
136 int defer_if_permit_sender
; /* force permit into warning */
137 int discard
; /* discard message */
138 char *saved_filter
; /* postponed filter action */
139 char *saved_redirect
; /* postponed redirect action */
140 char *saved_bcc
; /* postponed bcc action */
141 int saved_flags
; /* postponed hold/discard */
143 int saved_delay
; /* postponed deferred delay */
145 VSTRING
*expand_buf
; /* scratch space for $name expansion */
146 ARGV
*prepend
; /* prepended headers */
147 VSTRING
*instance
; /* policy query correlation */
148 int seqno
; /* policy query correlation */
149 int ehlo_discard_mask
; /* suppressed EHLO features */
150 char *dsn_envid
; /* temporary MAIL FROM state */
151 int dsn_ret
; /* temporary MAIL FROM state */
152 VSTRING
*dsn_buf
; /* scratch space for xtext expansion */
153 VSTRING
*dsn_orcpt_buf
; /* scratch space for ORCPT parsing */
156 * Pass-through proxy client.
158 VSTREAM
*proxy
; /* proxy handle */
159 VSTRING
*proxy_buffer
; /* proxy query/reply buffer */
160 char *proxy_mail
; /* owned by mail_cmd() */
161 int proxy_xforward_features
; /* XFORWARD proxy state */
164 * XFORWARD server state.
166 SMTPD_XFORWARD_ATTR xforward
; /* up-stream logging info */
172 int tls_use_tls
; /* can use TLS */
173 int tls_enforce_tls
; /* must use TLS */
174 int tls_auth_only
; /* use SASL over TLS only */
175 TLS_SESS_STATE
*tls_context
; /* TLS session state */
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 */
186 #define SMTPD_FLAG_HANGUP (1<<0) /* 421/521 disconnect */
187 #define SMTPD_FLAG_ILL_PIPELINING (1<<1) /* inappropriate pipelining */
189 #define SMTPD_MASK_MAIL_KEEP ~0 /* keep all after MAIL reset */
191 #define SMTPD_STATE_XFORWARD_INIT (1<<0) /* xforward preset done */
192 #define SMTPD_STATE_XFORWARD_NAME (1<<1) /* client name received */
193 #define SMTPD_STATE_XFORWARD_ADDR (1<<2) /* client address received */
194 #define SMTPD_STATE_XFORWARD_PROTO (1<<3) /* protocol received */
195 #define SMTPD_STATE_XFORWARD_HELO (1<<4) /* client helo received */
196 #define SMTPD_STATE_XFORWARD_IDENT (1<<5) /* message identifier */
197 #define SMTPD_STATE_XFORWARD_DOMAIN (1<<6) /* message identifier */
198 #define SMTPD_STATE_XFORWARD_PORT (1<<7) /* client port received */
200 #define SMTPD_STATE_XFORWARD_CLIENT_MASK \
201 (SMTPD_STATE_XFORWARD_NAME | SMTPD_STATE_XFORWARD_ADDR \
202 | SMTPD_STATE_XFORWARD_PROTO | SMTPD_STATE_XFORWARD_HELO \
203 | SMTPD_STATE_XFORWARD_PORT)
205 extern void smtpd_state_init(SMTPD_STATE
*, VSTREAM
*, const char *);
206 extern void smtpd_state_reset(SMTPD_STATE
*);
209 * Conversation stages. This is used for "lost connection after XXX"
212 #define SMTPD_AFTER_CONNECT "CONNECT"
213 #define SMTPD_AFTER_DATA "DATA content"
214 #define SMTPD_AFTER_DOT "END-OF-MESSAGE"
217 * Other stages. These are sometimes used to change the way information is
218 * logged or what information will be available for access control.
220 #define SMTPD_CMD_HELO "HELO"
221 #define SMTPD_CMD_EHLO "EHLO"
222 #define SMTPD_CMD_STARTTLS "STARTTLS"
223 #define SMTPD_CMD_AUTH "AUTH"
224 #define SMTPD_CMD_MAIL "MAIL"
225 #define SMTPD_CMD_RCPT "RCPT"
226 #define SMTPD_CMD_DATA "DATA"
227 #define SMTPD_CMD_EOD SMTPD_AFTER_DOT /* XXX Was: END-OF-DATA */
228 #define SMTPD_CMD_RSET "RSET"
229 #define SMTPD_CMD_NOOP "NOOP"
230 #define SMTPD_CMD_VRFY "VRFY"
231 #define SMTPD_CMD_ETRN "ETRN"
232 #define SMTPD_CMD_QUIT "QUIT"
233 #define SMTPD_CMD_XCLIENT "XCLIENT"
234 #define SMTPD_CMD_XFORWARD "XFORWARD"
235 #define SMTPD_CMD_UNKNOWN "UNKNOWN"
238 * Representation of unknown and non-existent client information. Throughout
239 * Postfix, we use the "unknown" string value for unknown client information
240 * (e.g., unknown remote client hostname), and we use the empty string, null
241 * pointer or "no queue file record" for non-existent client information
242 * (e.g., no HELO command, or local submission).
244 * Inside the SMTP server, unknown real client attributes are represented by
245 * the string "unknown", and non-existent HELO is represented as a null
246 * pointer. The SMTP server uses this same representation internally for
247 * forwarded client attributes; the XFORWARD syntax makes no distinction
248 * between unknown (remote submission) and non-existent (local submission).
250 * The SMTP client sends forwarded client attributes only when upstream client
251 * attributes exist (i.e. remote submission). Thus, local submissions will
252 * appear to come from an SMTP-based content filter, which is acceptable.
254 * Known/unknown client attribute values use the SMTP server's internal
255 * representation in queue files, in queue manager delivery requests, and in
256 * delivery agent $name expansions.
258 * Non-existent attribute values are never present in queue files. Non-existent
259 * information is represented as empty strings in queue manager delivery
260 * requests and in delivery agent $name expansions.
262 #define CLIENT_ATTR_UNKNOWN "unknown"
264 #define CLIENT_NAME_UNKNOWN CLIENT_ATTR_UNKNOWN
265 #define CLIENT_ADDR_UNKNOWN CLIENT_ATTR_UNKNOWN
266 #define CLIENT_PORT_UNKNOWN CLIENT_ATTR_UNKNOWN
267 #define CLIENT_NAMADDR_UNKNOWN CLIENT_ATTR_UNKNOWN
268 #define CLIENT_HELO_UNKNOWN 0
269 #define CLIENT_PROTO_UNKNOWN CLIENT_ATTR_UNKNOWN
270 #define CLIENT_IDENT_UNKNOWN 0
271 #define CLIENT_DOMAIN_UNKNOWN 0
273 #define IS_AVAIL_CLIENT_ATTR(v) ((v) && strcmp((v), CLIENT_ATTR_UNKNOWN))
275 #define IS_AVAIL_CLIENT_NAME(v) IS_AVAIL_CLIENT_ATTR(v)
276 #define IS_AVAIL_CLIENT_ADDR(v) IS_AVAIL_CLIENT_ATTR(v)
277 #define IS_AVAIL_CLIENT_PORT(v) IS_AVAIL_CLIENT_ATTR(v)
278 #define IS_AVAIL_CLIENT_NAMADDR(v) IS_AVAIL_CLIENT_ATTR(v)
279 #define IS_AVAIL_CLIENT_HELO(v) ((v) != 0)
280 #define IS_AVAIL_CLIENT_PROTO(v) IS_AVAIL_CLIENT_ATTR(v)
281 #define IS_AVAIL_CLIENT_IDENT(v) ((v) != 0)
282 #define IS_AVAIL_CLIENT_DOMAIN(v) ((v) != 0)
285 * If running in stand-alone mode, do not try to talk to Postfix daemons but
286 * write to queue file instead.
288 #define SMTPD_STAND_ALONE(state) \
289 (state->client == VSTREAM_IN && getuid() != var_owner_uid)
292 * If running as proxy front-end, disable actions that require communication
293 * with the cleanup server.
295 #define USE_SMTPD_PROXY(state) \
296 (SMTPD_STAND_ALONE(state) == 0 && *var_smtpd_proxy_filt)
299 * SMTPD peer information lookup.
301 extern void smtpd_peer_init(SMTPD_STATE
*state
);
302 extern void smtpd_peer_reset(SMTPD_STATE
*state
);
304 #define SMTPD_PEER_CODE_OK 2
305 #define SMTPD_PEER_CODE_TEMP 4
306 #define SMTPD_PEER_CODE_PERM 5
307 #define SMTPD_PEER_CODE_FORGED 6
310 * Construct name[addr] or name[addr]:port as appropriate
312 #define SMTPD_BUILD_NAMADDRPORT(name, addr, port) \
313 concatenate((name), "[", (addr), "]", \
314 var_smtpd_client_port_log ? ":" : (char *) 0, \
318 * Don't mix information from the current SMTP session with forwarded
319 * information from an up-stream session.
321 #define FORWARD_CLIENT_ATTR(s, a) \
322 (((s)->xforward.flags & SMTPD_STATE_XFORWARD_CLIENT_MASK) ? \
323 (s)->xforward.a : (s)->a)
325 #define FORWARD_ADDR(s) FORWARD_CLIENT_ATTR((s), rfc_addr)
326 #define FORWARD_NAME(s) FORWARD_CLIENT_ATTR((s), name)
327 #define FORWARD_NAMADDR(s) FORWARD_CLIENT_ATTR((s), namaddr)
328 #define FORWARD_PROTO(s) FORWARD_CLIENT_ATTR((s), protocol)
329 #define FORWARD_HELO(s) FORWARD_CLIENT_ATTR((s), helo_name)
330 #define FORWARD_PORT(s) FORWARD_CLIENT_ATTR((s), port)
332 #define FORWARD_IDENT(s) \
333 (((s)->xforward.flags & SMTPD_STATE_XFORWARD_IDENT) ? \
334 (s)->queue_id : (s)->ident)
336 #define FORWARD_DOMAIN(s) \
337 (((s)->xforward.flags & SMTPD_STATE_XFORWARD_DOMAIN) ? \
338 (s)->xforward.domain : (s)->rewrite_context)
340 extern void smtpd_xforward_init(SMTPD_STATE
*);
341 extern void smtpd_xforward_preset(SMTPD_STATE
*);
342 extern void smtpd_xforward_reset(SMTPD_STATE
*);
345 * Transparency: before mail is queued, do we check for unknown recipients,
346 * do we allow address mapping, automatic bcc, header/body checks?
348 extern int smtpd_input_transp_mask
;
351 * More Milter support.
353 extern MILTERS
*smtpd_milters
;
358 /* The Secure Mailer license must be distributed with this software.
361 /* IBM T.J. Watson Research
363 /* Yorktown Heights, NY 10598, USA
365 /* TLS support originally by:
368 /* Allgemeine Elektrotechnik
369 /* Universitaetsplatz 3-4
370 /* D-03044 Cottbus, Germany