xref: /openbsd/usr.sbin/smtpd/smtpd.h (revision eed85469)
1 /*	$OpenBSD: smtpd.h,v 1.662 2021/03/05 12:37:32 eric Exp $	*/
2 
3 /*
4  * Copyright (c) 2008 Gilles Chehade <gilles@poolp.org>
5  * Copyright (c) 2008 Pierre-Yves Ritschard <pyr@openbsd.org>
6  * Copyright (c) 2012 Eric Faurot <eric@openbsd.org>
7  *
8  * Permission to use, copy, modify, and distribute this software for any
9  * purpose with or without fee is hereby granted, provided that the above
10  * copyright notice and this permission notice appear in all copies.
11  *
12  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19  */
20 
21 #ifndef nitems
22 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
23 #endif
24 
25 #include <netinet/in.h>
26 #include <netdb.h>
27 #include <event.h>
28 
29 #include "smtpd-defines.h"
30 #include "smtpd-api.h"
31 #include "ioev.h"
32 
33 #define CHECK_IMSG_DATA_SIZE(imsg, expected_sz) do {			\
34 	if ((imsg)->hdr.len - IMSG_HEADER_SIZE != (expected_sz))	\
35 		fatalx("smtpd: imsg %d: data size expected %zd got %zd",\
36 	   	    (imsg)->hdr.type,					\
37 	   	    (expected_sz), (imsg)->hdr.len - IMSG_HEADER_SIZE);	\
38 } while (0)
39 
40 #define CONF_FILE		 "/etc/mail/smtpd.conf"
41 #define MAILNAME_FILE		 "/etc/mail/mailname"
42 #define CA_FILE			 "/etc/ssl/cert.pem"
43 
44 #define PROC_COUNT		 7
45 
46 #define MAX_HOPS_COUNT		 100
47 #define	DEFAULT_MAX_BODY_SIZE	(35*1024*1024)
48 
49 #define	EXPAND_BUFFER		 1024
50 
51 #define SMTPD_QUEUE_EXPIRY	 (4 * 24 * 60 * 60)
52 #define SMTPD_SOCKET		 "/var/run/smtpd.sock"
53 #define	SMTPD_NAME		 "OpenSMTPD"
54 #define	SMTPD_VERSION		 "6.7.0"
55 #define SMTPD_SESSION_TIMEOUT	 300
56 #define SMTPD_BACKLOG		 5
57 
58 #define	PATH_SMTPCTL		"/usr/sbin/smtpctl"
59 
60 #define PATH_OFFLINE		"/offline"
61 #define PATH_PURGE		"/purge"
62 #define PATH_TEMPORARY		"/temporary"
63 
64 #define	PATH_LIBEXEC		"/usr/local/libexec/smtpd"
65 
66 
67 /*
68  * RFC 5322 defines these characters as valid, some of them are
69  * potentially dangerous and need to be escaped.
70  */
71 #define	MAILADDR_ALLOWED       	"!#$%&'*/?^`{|}~+-=_"
72 #define	MAILADDR_ESCAPE		"!#$%&'*?`{|}~"
73 
74 
75 #define F_STARTTLS		0x01
76 #define F_SMTPS			0x02
77 #define F_SSL		       (F_STARTTLS | F_SMTPS)
78 #define F_AUTH			0x08
79 #define	F_STARTTLS_REQUIRE	0x20
80 #define	F_AUTH_REQUIRE		0x40
81 #define	F_MASK_SOURCE		0x100
82 #define	F_TLS_VERIFY		0x200
83 #define	F_EXT_DSN		0x400
84 #define	F_RECEIVEDAUTH		0x800
85 #define	F_MASQUERADE		0x1000
86 #define	F_FILTERED		0x2000
87 #define	F_PROXY			0x4000
88 
89 #define RELAY_TLS_OPPORTUNISTIC	0
90 #define RELAY_TLS_STARTTLS	1
91 #define RELAY_TLS_SMTPS		2
92 #define RELAY_TLS_NO		3
93 
94 #define RELAY_AUTH		0x08
95 #define RELAY_LMTP		0x80
96 #define	RELAY_TLS_VERIFY	0x200
97 
98 #define MTA_EXT_DSN		0x400
99 
100 
101 #define P_SENDMAIL	0
102 #define P_NEWALIASES	1
103 #define P_MAKEMAP	2
104 
105 #define	CERT_ERROR	-1
106 #define	CERT_OK		 0
107 #define	CERT_NOCA	 1
108 #define	CERT_NOCERT	 2
109 #define	CERT_INVALID	 3
110 
111 struct userinfo {
112 	char username[SMTPD_VUSERNAME_SIZE];
113 	char directory[PATH_MAX];
114 	uid_t uid;
115 	gid_t gid;
116 };
117 
118 struct netaddr {
119 	struct sockaddr_storage ss;
120 	int bits;
121 };
122 
123 struct relayhost {
124 	uint16_t flags;
125 	int tls;
126 	char hostname[HOST_NAME_MAX+1];
127 	uint16_t port;
128 	char authlabel[PATH_MAX];
129 };
130 
131 struct credentials {
132 	char username[LINE_MAX];
133 	char password[LINE_MAX];
134 };
135 
136 struct destination {
137 	char	name[HOST_NAME_MAX+1];
138 };
139 
140 struct source {
141 	struct sockaddr_storage	addr;
142 };
143 
144 struct addrname {
145 	struct sockaddr_storage	addr;
146 	char			name[HOST_NAME_MAX+1];
147 };
148 
149 union lookup {
150 	struct expand		*expand;
151 	struct credentials	 creds;
152 	struct netaddr		 netaddr;
153 	struct source		 source;
154 	struct destination	 domain;
155 	struct userinfo		 userinfo;
156 	struct mailaddr		 mailaddr;
157 	struct addrname		 addrname;
158 	struct maddrmap		*maddrmap;
159 	char			 relayhost[LINE_MAX];
160 };
161 
162 /*
163  * Bump IMSG_VERSION whenever a change is made to enum imsg_type.
164  * This will ensure that we can never use a wrong version of smtpctl with smtpd.
165  */
166 #define	IMSG_VERSION		16
167 
168 enum imsg_type {
169 	IMSG_NONE,
170 
171 	IMSG_CTL_OK,
172 	IMSG_CTL_FAIL,
173 
174 	IMSG_CTL_GET_DIGEST,
175 	IMSG_CTL_GET_STATS,
176 	IMSG_CTL_LIST_MESSAGES,
177 	IMSG_CTL_LIST_ENVELOPES,
178 	IMSG_CTL_MTA_SHOW_HOSTS,
179 	IMSG_CTL_MTA_SHOW_RELAYS,
180 	IMSG_CTL_MTA_SHOW_ROUTES,
181 	IMSG_CTL_MTA_SHOW_HOSTSTATS,
182 	IMSG_CTL_MTA_BLOCK,
183 	IMSG_CTL_MTA_UNBLOCK,
184 	IMSG_CTL_MTA_SHOW_BLOCK,
185 	IMSG_CTL_PAUSE_EVP,
186 	IMSG_CTL_PAUSE_MDA,
187 	IMSG_CTL_PAUSE_MTA,
188 	IMSG_CTL_PAUSE_SMTP,
189 	IMSG_CTL_PROFILE,
190 	IMSG_CTL_PROFILE_DISABLE,
191 	IMSG_CTL_PROFILE_ENABLE,
192 	IMSG_CTL_RESUME_EVP,
193 	IMSG_CTL_RESUME_MDA,
194 	IMSG_CTL_RESUME_MTA,
195 	IMSG_CTL_RESUME_SMTP,
196 	IMSG_CTL_RESUME_ROUTE,
197 	IMSG_CTL_REMOVE,
198 	IMSG_CTL_SCHEDULE,
199 	IMSG_CTL_SHOW_STATUS,
200 	IMSG_CTL_TRACE_DISABLE,
201 	IMSG_CTL_TRACE_ENABLE,
202 	IMSG_CTL_UPDATE_TABLE,
203 	IMSG_CTL_VERBOSE,
204 	IMSG_CTL_DISCOVER_EVPID,
205 	IMSG_CTL_DISCOVER_MSGID,
206 
207 	IMSG_CTL_SMTP_SESSION,
208 
209 	IMSG_GETADDRINFO,
210 	IMSG_GETADDRINFO_END,
211 	IMSG_GETNAMEINFO,
212 	IMSG_RES_QUERY,
213 
214 	IMSG_CERT_INIT,
215 	IMSG_CERT_CERTIFICATE,
216 	IMSG_CERT_VERIFY,
217 
218 	IMSG_SETUP_KEY,
219 	IMSG_SETUP_PEER,
220 	IMSG_SETUP_DONE,
221 
222 	IMSG_CONF_START,
223 	IMSG_CONF_END,
224 
225 	IMSG_STAT_INCREMENT,
226 	IMSG_STAT_DECREMENT,
227 	IMSG_STAT_SET,
228 
229 	IMSG_LKA_AUTHENTICATE,
230 	IMSG_LKA_OPEN_FORWARD,
231 	IMSG_LKA_ENVELOPE_SUBMIT,
232 	IMSG_LKA_ENVELOPE_COMMIT,
233 
234 	IMSG_QUEUE_DELIVER,
235 	IMSG_QUEUE_DELIVERY_OK,
236 	IMSG_QUEUE_DELIVERY_TEMPFAIL,
237 	IMSG_QUEUE_DELIVERY_PERMFAIL,
238 	IMSG_QUEUE_DELIVERY_LOOP,
239 	IMSG_QUEUE_DISCOVER_EVPID,
240 	IMSG_QUEUE_DISCOVER_MSGID,
241 	IMSG_QUEUE_ENVELOPE_ACK,
242 	IMSG_QUEUE_ENVELOPE_COMMIT,
243 	IMSG_QUEUE_ENVELOPE_REMOVE,
244 	IMSG_QUEUE_ENVELOPE_SCHEDULE,
245 	IMSG_QUEUE_ENVELOPE_SUBMIT,
246 	IMSG_QUEUE_HOLDQ_HOLD,
247 	IMSG_QUEUE_HOLDQ_RELEASE,
248 	IMSG_QUEUE_MESSAGE_COMMIT,
249 	IMSG_QUEUE_MESSAGE_ROLLBACK,
250 	IMSG_QUEUE_SMTP_SESSION,
251 	IMSG_QUEUE_TRANSFER,
252 
253 	IMSG_MDA_DELIVERY_OK,
254 	IMSG_MDA_DELIVERY_TEMPFAIL,
255 	IMSG_MDA_DELIVERY_PERMFAIL,
256 	IMSG_MDA_DELIVERY_LOOP,
257 	IMSG_MDA_DELIVERY_HOLD,
258 	IMSG_MDA_DONE,
259 	IMSG_MDA_FORK,
260 	IMSG_MDA_HOLDQ_RELEASE,
261 	IMSG_MDA_LOOKUP_USERINFO,
262 	IMSG_MDA_KILL,
263 	IMSG_MDA_OPEN_MESSAGE,
264 
265 	IMSG_MTA_DELIVERY_OK,
266 	IMSG_MTA_DELIVERY_TEMPFAIL,
267 	IMSG_MTA_DELIVERY_PERMFAIL,
268 	IMSG_MTA_DELIVERY_LOOP,
269 	IMSG_MTA_DELIVERY_HOLD,
270 	IMSG_MTA_DNS_HOST,
271 	IMSG_MTA_DNS_HOST_END,
272 	IMSG_MTA_DNS_MX,
273 	IMSG_MTA_DNS_MX_PREFERENCE,
274 	IMSG_MTA_HOLDQ_RELEASE,
275 	IMSG_MTA_LOOKUP_CREDENTIALS,
276 	IMSG_MTA_LOOKUP_SOURCE,
277 	IMSG_MTA_LOOKUP_HELO,
278 	IMSG_MTA_LOOKUP_SMARTHOST,
279 	IMSG_MTA_OPEN_MESSAGE,
280 	IMSG_MTA_SCHEDULE,
281 
282 	IMSG_SCHED_ENVELOPE_BOUNCE,
283 	IMSG_SCHED_ENVELOPE_DELIVER,
284 	IMSG_SCHED_ENVELOPE_EXPIRE,
285 	IMSG_SCHED_ENVELOPE_INJECT,
286 	IMSG_SCHED_ENVELOPE_REMOVE,
287 	IMSG_SCHED_ENVELOPE_TRANSFER,
288 
289 	IMSG_SMTP_AUTHENTICATE,
290 	IMSG_SMTP_MESSAGE_COMMIT,
291 	IMSG_SMTP_MESSAGE_CREATE,
292 	IMSG_SMTP_MESSAGE_ROLLBACK,
293 	IMSG_SMTP_MESSAGE_OPEN,
294 	IMSG_SMTP_CHECK_SENDER,
295 	IMSG_SMTP_EXPAND_RCPT,
296 	IMSG_SMTP_LOOKUP_HELO,
297 
298 	IMSG_SMTP_REQ_CONNECT,
299 	IMSG_SMTP_REQ_HELO,
300 	IMSG_SMTP_REQ_MAIL,
301 	IMSG_SMTP_REQ_RCPT,
302 	IMSG_SMTP_REQ_DATA,
303 	IMSG_SMTP_REQ_EOM,
304 	IMSG_SMTP_EVENT_RSET,
305 	IMSG_SMTP_EVENT_COMMIT,
306 	IMSG_SMTP_EVENT_ROLLBACK,
307 	IMSG_SMTP_EVENT_DISCONNECT,
308 
309 	IMSG_LKA_PROCESSOR_FORK,
310 	IMSG_LKA_PROCESSOR_ERRFD,
311 
312 	IMSG_REPORT_SMTP_LINK_CONNECT,
313 	IMSG_REPORT_SMTP_LINK_DISCONNECT,
314 	IMSG_REPORT_SMTP_LINK_GREETING,
315 	IMSG_REPORT_SMTP_LINK_IDENTIFY,
316 	IMSG_REPORT_SMTP_LINK_TLS,
317 	IMSG_REPORT_SMTP_LINK_AUTH,
318 	IMSG_REPORT_SMTP_TX_RESET,
319 	IMSG_REPORT_SMTP_TX_BEGIN,
320 	IMSG_REPORT_SMTP_TX_MAIL,
321 	IMSG_REPORT_SMTP_TX_RCPT,
322 	IMSG_REPORT_SMTP_TX_ENVELOPE,
323 	IMSG_REPORT_SMTP_TX_DATA,
324 	IMSG_REPORT_SMTP_TX_COMMIT,
325 	IMSG_REPORT_SMTP_TX_ROLLBACK,
326 	IMSG_REPORT_SMTP_PROTOCOL_CLIENT,
327 	IMSG_REPORT_SMTP_PROTOCOL_SERVER,
328 	IMSG_REPORT_SMTP_FILTER_RESPONSE,
329 	IMSG_REPORT_SMTP_TIMEOUT,
330 
331 	IMSG_FILTER_SMTP_BEGIN,
332 	IMSG_FILTER_SMTP_END,
333 	IMSG_FILTER_SMTP_PROTOCOL,
334 	IMSG_FILTER_SMTP_DATA_BEGIN,
335 	IMSG_FILTER_SMTP_DATA_END,
336 
337 	IMSG_CA_RSA_PRIVENC,
338 	IMSG_CA_RSA_PRIVDEC,
339 	IMSG_CA_ECDSA_SIGN,
340 };
341 
342 enum smtp_proc_type {
343 	PROC_PARENT = 0,
344 	PROC_LKA,
345 	PROC_QUEUE,
346 	PROC_CONTROL,
347 	PROC_SCHEDULER,
348 	PROC_DISPATCHER,
349 	PROC_CA,
350 	PROC_PROCESSOR,
351 	PROC_CLIENT,
352 };
353 
354 enum table_type {
355 	T_NONE		= 0,
356 	T_DYNAMIC	= 0x01,	/* table with external source	*/
357 	T_LIST		= 0x02,	/* table holding a list		*/
358 	T_HASH		= 0x04,	/* table holding a hash table	*/
359 };
360 
361 struct table {
362 	char				 t_name[LINE_MAX];
363 	enum table_type			 t_type;
364 	char				 t_config[PATH_MAX];
365 
366 	void				*t_handle;
367 	struct table_backend		*t_backend;
368 };
369 
370 struct table_backend {
371 	const char *name;
372 	const unsigned int	services;
373 	int	(*config)(struct table *);
374 	int	(*add)(struct table *, const char *, const char *);
375 	void	(*dump)(struct table *);
376 	int	(*open)(struct table *);
377 	int	(*update)(struct table *);
378 	void	(*close)(struct table *);
379 	int	(*lookup)(struct table *, enum table_service, const char *, char **);
380 	int	(*fetch)(struct table *, enum table_service, char **);
381 };
382 
383 
384 enum bounce_type {
385 	B_FAILED,
386 	B_DELAYED,
387 	B_DELIVERED
388 };
389 
390 enum dsn_ret {
391 	DSN_RETFULL = 1,
392 	DSN_RETHDRS
393 };
394 
395 struct delivery_bounce {
396 	enum bounce_type	type;
397 	time_t			delay;
398 	time_t			ttl;
399 	enum dsn_ret		dsn_ret;
400         int			mta_without_dsn;
401 };
402 
403 enum expand_type {
404 	EXPAND_INVALID,
405 	EXPAND_USERNAME,
406 	EXPAND_FILENAME,
407 	EXPAND_FILTER,
408 	EXPAND_INCLUDE,
409 	EXPAND_ADDRESS,
410 	EXPAND_ERROR,
411 };
412 
413 enum filter_phase {
414 	FILTER_CONNECT,
415 	FILTER_HELO,
416 	FILTER_EHLO,
417 	FILTER_STARTTLS,
418 	FILTER_AUTH,
419 	FILTER_MAIL_FROM,
420 	FILTER_RCPT_TO,
421 	FILTER_DATA,
422 	FILTER_DATA_LINE,
423 	FILTER_RSET,
424 	FILTER_QUIT,
425 	FILTER_NOOP,
426 	FILTER_HELP,
427 	FILTER_WIZ,
428 	FILTER_COMMIT,
429 	FILTER_PHASES_COUNT     /* must be last */
430 };
431 
432 struct expandnode {
433 	RB_ENTRY(expandnode)	entry;
434 	TAILQ_ENTRY(expandnode)	tq_entry;
435 	enum expand_type	type;
436 	int			sameuser;
437 	int			realuser;
438 	int			forwarded;
439 	struct rule	       *rule;
440 	struct expandnode      *parent;
441 	unsigned int		depth;
442 	union {
443 		/*
444 		 * user field handles both expansion user and system user
445 		 * so we MUST make it large enough to fit a mailaddr user
446 		 */
447 		char		user[SMTPD_MAXLOCALPARTSIZE];
448 		char		buffer[EXPAND_BUFFER];
449 		struct mailaddr	mailaddr;
450 	}			u;
451 	char		subaddress[SMTPD_SUBADDRESS_SIZE];
452 };
453 
454 struct expand {
455 	RB_HEAD(expandtree, expandnode)	 tree;
456 	TAILQ_HEAD(xnodes, expandnode)	*queue;
457 	size_t				 nb_nodes;
458 	struct rule			*rule;
459 	struct expandnode		*parent;
460 };
461 
462 struct maddrnode {
463 	TAILQ_ENTRY(maddrnode)		entries;
464 	struct mailaddr			mailaddr;
465 };
466 
467 struct maddrmap {
468 	TAILQ_HEAD(xmaddr, maddrnode)	queue;
469 };
470 
471 #define DSN_SUCCESS 0x01
472 #define DSN_FAILURE 0x02
473 #define DSN_DELAY   0x04
474 #define DSN_NEVER   0x08
475 
476 #define	DSN_ENVID_LEN	100
477 
478 #define	SMTPD_ENVELOPE_VERSION		3
479 struct envelope {
480 	TAILQ_ENTRY(envelope)		entry;
481 
482 	char				dispatcher[HOST_NAME_MAX+1];
483 
484 	char				tag[SMTPD_TAG_SIZE];
485 
486 	uint32_t			version;
487 	uint64_t			id;
488 	enum envelope_flags		flags;
489 
490 	char				smtpname[HOST_NAME_MAX+1];
491 	char				helo[HOST_NAME_MAX+1];
492 	char				hostname[HOST_NAME_MAX+1];
493 	char				username[SMTPD_MAXMAILADDRSIZE];
494 	char				errorline[LINE_MAX];
495 	struct sockaddr_storage		ss;
496 
497 	struct mailaddr			sender;
498 	struct mailaddr			rcpt;
499 	struct mailaddr			dest;
500 
501 	char				mda_user[SMTPD_VUSERNAME_SIZE];
502 	char				mda_subaddress[SMTPD_SUBADDRESS_SIZE];
503 	char				mda_exec[LINE_MAX];
504 
505 	enum delivery_type		type;
506 	union {
507 		struct delivery_bounce	bounce;
508 	}				agent;
509 
510 	uint16_t			retry;
511 	time_t				creation;
512 	time_t				ttl;
513 	time_t				lasttry;
514 	time_t				nexttry;
515 	time_t				lastbounce;
516 
517 	struct mailaddr			dsn_orcpt;
518 	char				dsn_envid[DSN_ENVID_LEN+1];
519 	uint8_t				dsn_notify;
520 	enum dsn_ret			dsn_ret;
521 
522 	uint8_t				esc_class;
523 	uint8_t				esc_code;
524 };
525 
526 struct listener {
527 	uint16_t       		 flags;
528 	int			 fd;
529 	struct sockaddr_storage	 ss;
530 	in_port_t		 port;
531 	struct timeval		 timeout;
532 	struct event		 ev;
533 	char			 filter_name[PATH_MAX];
534 	char			 pki_name[PATH_MAX];
535 	char			 ca_name[PATH_MAX];
536 	char			 tag[SMTPD_TAG_SIZE];
537 	char			 authtable[LINE_MAX];
538 	char			 hostname[HOST_NAME_MAX+1];
539 	char			 hostnametable[PATH_MAX];
540 	char			 sendertable[PATH_MAX];
541 
542 	TAILQ_ENTRY(listener)	 entry;
543 
544 	int			 local;		/* there must be a better way */
545 
546 	struct tls		*tls;
547 	struct pki		**pki;
548 	int			 pkicount;
549 };
550 
551 struct smtpd {
552 	char				sc_conffile[PATH_MAX];
553 	size_t				sc_maxsize;
554 
555 #define SMTPD_OPT_VERBOSE		0x00000001
556 #define SMTPD_OPT_NOACTION		0x00000002
557 	uint32_t			sc_opts;
558 
559 #define SMTPD_EXITING			0x00000001 /* unused */
560 #define SMTPD_MDA_PAUSED		0x00000002
561 #define SMTPD_MTA_PAUSED		0x00000004
562 #define SMTPD_SMTP_PAUSED		0x00000008
563 #define SMTPD_MDA_BUSY			0x00000010
564 #define SMTPD_MTA_BUSY			0x00000020
565 #define SMTPD_BOUNCE_BUSY		0x00000040
566 #define SMTPD_SMTP_DISABLED		0x00000080
567 	uint32_t			sc_flags;
568 
569 #define QUEUE_COMPRESSION      		0x00000001
570 #define QUEUE_ENCRYPTION      		0x00000002
571 #define QUEUE_EVPCACHE			0x00000004
572 	uint32_t			sc_queue_flags;
573 	char			       *sc_queue_key;
574 	size_t				sc_queue_evpcache_size;
575 
576 	size_t				sc_session_max_rcpt;
577 	size_t				sc_session_max_mails;
578 
579 	struct dict		       *sc_mda_wrappers;
580 	size_t				sc_mda_max_session;
581 	size_t				sc_mda_max_user_session;
582 	size_t				sc_mda_task_hiwat;
583 	size_t				sc_mda_task_lowat;
584 	size_t				sc_mda_task_release;
585 
586 	size_t				sc_mta_max_deferred;
587 
588 	size_t				sc_scheduler_max_inflight;
589 	size_t				sc_scheduler_max_evp_batch_size;
590 	size_t				sc_scheduler_max_msg_batch_size;
591 	size_t				sc_scheduler_max_schedule;
592 
593 	struct dict		       *sc_filter_processes_dict;
594 
595 	int				sc_ttl;
596 #define MAX_BOUNCE_WARN			4
597 	time_t				sc_bounce_warn[MAX_BOUNCE_WARN];
598 	char				sc_hostname[HOST_NAME_MAX+1];
599 	struct stat_backend	       *sc_stat;
600 	struct compress_backend	       *sc_comp;
601 
602 	time_t					 sc_uptime;
603 
604 	/* This is a listener for a local socket used by smtp_enqueue(). */
605 	struct listener                         *sc_sock_listener;
606 
607 	TAILQ_HEAD(listenerlist, listener)	*sc_listeners;
608 
609 	TAILQ_HEAD(rulelist, rule)		*sc_rules;
610 
611 
612 	struct dict				*sc_filters_dict;
613 	struct dict				*sc_dispatchers;
614 	struct dispatcher			*sc_dispatcher_bounce;
615 
616 	struct dict			       *sc_ca_dict;
617 	struct dict			       *sc_pki_dict;
618 	struct dict			       *sc_ssl_dict;
619 
620 	struct dict			       *sc_tables_dict;		/* keyed lookup	*/
621 
622 	struct dict			       *sc_limits_dict;
623 
624 	char				       *sc_tls_ciphers;
625 
626 	char				       *sc_subaddressing_delim;
627 
628 	char				       *sc_srs_key;
629 	char				       *sc_srs_key_backup;
630 	int				        sc_srs_ttl;
631 
632 	char				       *sc_admd;
633 };
634 
635 #define	TRACE_DEBUG	0x0001
636 #define	TRACE_IMSG	0x0002
637 #define	TRACE_IO	0x0004
638 #define	TRACE_SMTP	0x0008
639 #define	TRACE_FILTERS	0x0010
640 #define	TRACE_MTA	0x0020
641 #define	TRACE_BOUNCE	0x0040
642 #define	TRACE_SCHEDULER	0x0080
643 #define	TRACE_LOOKUP	0x0100
644 #define	TRACE_STAT	0x0200
645 #define	TRACE_RULES	0x0400
646 #define	TRACE_MPROC	0x0800
647 #define	TRACE_EXPAND	0x1000
648 #define	TRACE_TABLES	0x2000
649 #define	TRACE_QUEUE	0x4000
650 
651 #define PROFILE_TOSTAT	0x0001
652 #define PROFILE_IMSG	0x0002
653 #define PROFILE_QUEUE	0x0004
654 
655 struct forward_req {
656 	uint64_t			id;
657 	uint8_t				status;
658 
659 	char				user[SMTPD_VUSERNAME_SIZE];
660 	uid_t				uid;
661 	gid_t				gid;
662 	char				directory[PATH_MAX];
663 };
664 
665 struct deliver {
666 	char			dispatcher[EXPAND_BUFFER];
667 
668 	struct mailaddr		sender;
669 	struct mailaddr		rcpt;
670 	struct mailaddr		dest;
671 
672 	char			mda_subaddress[SMTPD_SUBADDRESS_SIZE];
673 	char			mda_exec[LINE_MAX];
674 
675 	struct userinfo		userinfo;
676 };
677 
678 struct mta_host {
679 	SPLAY_ENTRY(mta_host)	 entry;
680 	struct sockaddr		*sa;
681 	char			*ptrname;
682 	int			 refcount;
683 	size_t			 nconn;
684 	time_t			 lastconn;
685 	time_t			 lastptrquery;
686 
687 #define HOST_IGNORE	0x01
688 	int			 flags;
689 };
690 
691 struct mta_mx {
692 	TAILQ_ENTRY(mta_mx)	 entry;
693 	struct mta_host		*host;
694 	char			*mxname;
695 	int			 preference;
696 };
697 
698 struct mta_domain {
699 	SPLAY_ENTRY(mta_domain)	 entry;
700 	char			*name;
701 	int			 as_host;
702 	TAILQ_HEAD(, mta_mx)	 mxs;
703 	int			 mxstatus;
704 	int			 refcount;
705 	size_t			 nconn;
706 	time_t			 lastconn;
707 	time_t			 lastmxquery;
708 };
709 
710 struct mta_source {
711 	SPLAY_ENTRY(mta_source)	 entry;
712 	struct sockaddr		*sa;
713 	int			 refcount;
714 	size_t			 nconn;
715 	time_t			 lastconn;
716 };
717 
718 struct mta_connector {
719 	struct mta_source		*source;
720 	struct mta_relay		*relay;
721 
722 #define CONNECTOR_ERROR_FAMILY		0x0001
723 #define CONNECTOR_ERROR_SOURCE		0x0002
724 #define CONNECTOR_ERROR_MX		0x0004
725 #define CONNECTOR_ERROR_ROUTE_NET	0x0008
726 #define CONNECTOR_ERROR_ROUTE_SMTP	0x0010
727 #define CONNECTOR_ERROR_ROUTE		0x0018
728 #define CONNECTOR_ERROR_BLOCKED		0x0020
729 #define CONNECTOR_ERROR			0x00ff
730 
731 #define CONNECTOR_LIMIT_HOST		0x0100
732 #define CONNECTOR_LIMIT_ROUTE		0x0200
733 #define CONNECTOR_LIMIT_SOURCE		0x0400
734 #define CONNECTOR_LIMIT_RELAY		0x0800
735 #define CONNECTOR_LIMIT_CONN		0x1000
736 #define CONNECTOR_LIMIT_DOMAIN		0x2000
737 #define CONNECTOR_LIMIT			0xff00
738 
739 #define CONNECTOR_NEW			0x10000
740 #define CONNECTOR_WAIT			0x20000
741 	int				 flags;
742 
743 	int				 refcount;
744 	size_t				 nconn;
745 	time_t				 lastconn;
746 };
747 
748 struct mta_route {
749 	SPLAY_ENTRY(mta_route)	 entry;
750 	uint64_t		 id;
751 	struct mta_source	*src;
752 	struct mta_host		*dst;
753 #define ROUTE_NEW		0x01
754 #define ROUTE_RUNQ		0x02
755 #define ROUTE_KEEPALIVE		0x04
756 #define ROUTE_DISABLED		0xf0
757 #define ROUTE_DISABLED_NET	0x10
758 #define ROUTE_DISABLED_SMTP	0x20
759 	int			 flags;
760 	int			 nerror;
761 	int			 penalty;
762 	int			 refcount;
763 	size_t			 nconn;
764 	time_t			 lastconn;
765 	time_t			 lastdisc;
766 	time_t			 lastpenalty;
767 };
768 
769 struct mta_limits {
770 	size_t	maxconn_per_host;
771 	size_t	maxconn_per_route;
772 	size_t	maxconn_per_source;
773 	size_t	maxconn_per_connector;
774 	size_t	maxconn_per_relay;
775 	size_t	maxconn_per_domain;
776 
777 	time_t	conndelay_host;
778 	time_t	conndelay_route;
779 	time_t	conndelay_source;
780 	time_t	conndelay_connector;
781 	time_t	conndelay_relay;
782 	time_t	conndelay_domain;
783 
784 	time_t	discdelay_route;
785 
786 	size_t	max_mail_per_session;
787 	time_t	sessdelay_transaction;
788 	time_t	sessdelay_keepalive;
789 
790 	size_t	max_failures_per_session;
791 
792 	int	family;
793 
794 	int	task_hiwat;
795 	int	task_lowat;
796 	int	task_release;
797 };
798 
799 struct mta_relay {
800 	SPLAY_ENTRY(mta_relay)	 entry;
801 	uint64_t		 id;
802 
803 	struct dispatcher	*dispatcher;
804 	struct mta_domain	*domain;
805 	struct mta_limits	*limits;
806 	int			 tls;
807 	int			 flags;
808 	char			*backupname;
809 	int			 backuppref;
810 	char			*sourcetable;
811 	uint16_t		 port;
812 	char			*pki_name;
813 	char			*ca_name;
814 	char			*authtable;
815 	char			*authlabel;
816 	char			*helotable;
817 	char			*heloname;
818 	char			*secret;
819 	int			 srs;
820 
821 	int			 state;
822 	size_t			 ntask;
823 	TAILQ_HEAD(, mta_task)	 tasks;
824 
825 	struct tree		 connectors;
826 	size_t			 sourceloop;
827 	time_t			 lastsource;
828 	time_t			 nextsource;
829 
830 	int			 fail;
831 	char			*failstr;
832 
833 #define RELAY_WAIT_MX		0x01
834 #define RELAY_WAIT_PREFERENCE	0x02
835 #define RELAY_WAIT_SECRET	0x04
836 #define RELAY_WAIT_LIMITS	0x08
837 #define RELAY_WAIT_SOURCE	0x10
838 #define RELAY_WAIT_CONNECTOR	0x20
839 #define RELAY_WAIT_SMARTHOST	0x40
840 #define RELAY_WAITMASK		0x7f
841 	int			 status;
842 
843 	int			 refcount;
844 	size_t			 nconn;
845 	size_t			 nconn_ready;
846 	time_t			 lastconn;
847 };
848 
849 struct mta_envelope {
850 	TAILQ_ENTRY(mta_envelope)	 entry;
851 	uint64_t			 id;
852 	uint64_t			 session;
853 	time_t				 creation;
854 	char				*smtpname;
855 	char				*dest;
856 	char				*rcpt;
857 	struct mta_task			*task;
858 	int				 delivery;
859 
860 	int				 ext;
861 	char				*dsn_orcpt;
862 	char				dsn_envid[DSN_ENVID_LEN+1];
863 	uint8_t				dsn_notify;
864 	enum dsn_ret			dsn_ret;
865 
866 	char				 status[LINE_MAX];
867 };
868 
869 struct mta_task {
870 	TAILQ_ENTRY(mta_task)		 entry;
871 	struct mta_relay		*relay;
872 	uint32_t			 msgid;
873 	TAILQ_HEAD(, mta_envelope)	 envelopes;
874 	char				*sender;
875 };
876 
877 struct passwd;
878 
879 struct queue_backend {
880 	int	(*init)(struct passwd *, int, const char *);
881 };
882 
883 struct compress_backend {
884 	size_t	(*compress_chunk)(void *, size_t, void *, size_t);
885 	size_t	(*uncompress_chunk)(void *, size_t, void *, size_t);
886 	int	(*compress_file)(FILE *, FILE *);
887 	int	(*uncompress_file)(FILE *, FILE *);
888 };
889 
890 /* auth structures */
891 enum auth_type {
892 	AUTH_BSD,
893 	AUTH_PWD,
894 };
895 
896 struct auth_backend {
897 	int	(*authenticate)(char *, char *);
898 };
899 
900 struct scheduler_backend {
901 	int	(*init)(const char *);
902 
903 	int	(*insert)(struct scheduler_info *);
904 	size_t	(*commit)(uint32_t);
905 	size_t	(*rollback)(uint32_t);
906 
907 	int	(*update)(struct scheduler_info *);
908 	int	(*delete)(uint64_t);
909 	int	(*hold)(uint64_t, uint64_t);
910 	int	(*release)(int, uint64_t, int);
911 
912 	int	(*batch)(int, int*, size_t*, uint64_t*, int*);
913 
914 	size_t	(*messages)(uint32_t, uint32_t *, size_t);
915 	size_t	(*envelopes)(uint64_t, struct evpstate *, size_t);
916 	int	(*schedule)(uint64_t);
917 	int	(*remove)(uint64_t);
918 	int	(*suspend)(uint64_t);
919 	int	(*resume)(uint64_t);
920 	int	(*query)(uint64_t);
921 };
922 
923 enum stat_type {
924 	STAT_COUNTER,
925 	STAT_TIMESTAMP,
926 	STAT_TIMEVAL,
927 	STAT_TIMESPEC,
928 };
929 
930 struct stat_value {
931 	enum stat_type	type;
932 	union stat_v {
933 		size_t		counter;
934 		time_t		timestamp;
935 		struct timeval	tv;
936 		struct timespec	ts;
937 	} u;
938 };
939 
940 #define	STAT_KEY_SIZE	1024
941 struct stat_kv {
942 	void	*iter;
943 	char	key[STAT_KEY_SIZE];
944 	struct stat_value	val;
945 };
946 
947 struct stat_backend {
948 	void	(*init)(void);
949 	void	(*close)(void);
950 	void	(*increment)(const char *, size_t);
951 	void	(*decrement)(const char *, size_t);
952 	void	(*set)(const char *, const struct stat_value *);
953 	int	(*iter)(void **, char **, struct stat_value *);
954 };
955 
956 struct stat_digest {
957 	time_t			 startup;
958 	time_t			 timestamp;
959 
960 	size_t			 clt_connect;
961 	size_t			 clt_disconnect;
962 
963 	size_t			 evp_enqueued;
964 	size_t			 evp_dequeued;
965 
966 	size_t			 evp_expired;
967 	size_t			 evp_removed;
968 	size_t			 evp_bounce;
969 
970 	size_t			 dlv_ok;
971 	size_t			 dlv_permfail;
972 	size_t			 dlv_tempfail;
973 	size_t			 dlv_loop;
974 };
975 
976 
977 struct mproc {
978 	pid_t		 pid;
979 	char		*name;
980 	int		 proc;
981 	void		(*handler)(struct mproc *, struct imsg *);
982 	struct imsgbuf	 imsgbuf;
983 
984 	char		*m_buf;
985 	size_t		 m_alloc;
986 	size_t		 m_pos;
987 	uint32_t	 m_type;
988 	uint32_t	 m_peerid;
989 	pid_t		 m_pid;
990 	int		 m_fd;
991 
992 	int		 enable;
993 	short		 events;
994 	struct event	 ev;
995 	void		*data;
996 };
997 
998 struct msg {
999 	const uint8_t	*pos;
1000 	const uint8_t	*end;
1001 };
1002 
1003 extern enum smtp_proc_type	smtpd_process;
1004 
1005 extern int tracing;
1006 extern int foreground_log;
1007 extern int profiling;
1008 
1009 extern struct mproc *p_control;
1010 extern struct mproc *p_parent;
1011 extern struct mproc *p_lka;
1012 extern struct mproc *p_queue;
1013 extern struct mproc *p_scheduler;
1014 extern struct mproc *p_dispatcher;
1015 extern struct mproc *p_ca;
1016 
1017 extern struct smtpd	*env;
1018 extern void (*imsg_callback)(struct mproc *, struct imsg *);
1019 
1020 /* inter-process structures */
1021 
1022 struct bounce_req_msg {
1023 	uint64_t		evpid;
1024 	time_t			timestamp;
1025 	struct delivery_bounce	bounce;
1026 };
1027 
1028 enum dns_error {
1029 	DNS_OK = 0,
1030 	DNS_RETRY,
1031 	DNS_EINVAL,
1032 	DNS_ENONAME,
1033 	DNS_ENOTFOUND,
1034 };
1035 
1036 enum lka_resp_status {
1037 	LKA_OK,
1038 	LKA_TEMPFAIL,
1039 	LKA_PERMFAIL
1040 };
1041 
1042 enum filter_type {
1043 	FILTER_TYPE_BUILTIN,
1044 	FILTER_TYPE_PROC,
1045 	FILTER_TYPE_CHAIN,
1046 };
1047 
1048 enum filter_subsystem {
1049 	FILTER_SUBSYSTEM_SMTP_IN	= 1<<0,
1050 	FILTER_SUBSYSTEM_SMTP_OUT	= 1<<1,
1051 };
1052 
1053 struct filter_proc {
1054 	const char		       *command;
1055 	const char		       *user;
1056 	const char		       *group;
1057 	const char		       *chroot;
1058 	int				errfd;
1059 	enum filter_subsystem		filter_subsystem;
1060 };
1061 
1062 struct filter_config {
1063 	char			       *name;
1064 	enum filter_subsystem		filter_subsystem;
1065 	enum filter_type		filter_type;
1066 	enum filter_phase               phase;
1067 	char                           *reject;
1068 	char                           *disconnect;
1069 	char                           *rewrite;
1070 	char                           *report;
1071 	uint8_t				junk;
1072   	uint8_t				bypass;
1073 	char                           *proc;
1074 
1075 	const char		      **chain;
1076 	size_t				chain_size;
1077 	struct dict			chain_procs;
1078 
1079 	int8_t				not_fcrdns;
1080 	int8_t				fcrdns;
1081 
1082 	int8_t				not_rdns;
1083 	int8_t				rdns;
1084 
1085 	int8_t                          not_rdns_table;
1086 	struct table                   *rdns_table;
1087 
1088 	int8_t                          not_rdns_regex;
1089 	struct table                   *rdns_regex;
1090 
1091 	int8_t                          not_src_table;
1092 	struct table                   *src_table;
1093 
1094 	int8_t                          not_src_regex;
1095 	struct table                   *src_regex;
1096 
1097 	int8_t                          not_helo_table;
1098 	struct table                   *helo_table;
1099 
1100 	int8_t                          not_helo_regex;
1101 	struct table                   *helo_regex;
1102 
1103   	int8_t                          not_auth;
1104 	int8_t				auth;
1105 
1106   	int8_t                          not_auth_table;
1107 	struct table                   *auth_table;
1108 
1109 	int8_t                          not_auth_regex;
1110 	struct table                   *auth_regex;
1111 
1112 	int8_t                          not_mail_from_table;
1113 	struct table                   *mail_from_table;
1114 
1115 	int8_t                          not_mail_from_regex;
1116 	struct table                   *mail_from_regex;
1117 
1118 	int8_t                          not_rcpt_to_table;
1119 	struct table                   *rcpt_to_table;
1120 
1121 	int8_t                          not_rcpt_to_regex;
1122 	struct table                   *rcpt_to_regex;
1123 
1124 };
1125 
1126 enum filter_status {
1127 	FILTER_PROCEED,
1128 	FILTER_REWRITE,
1129 	FILTER_REJECT,
1130 	FILTER_DISCONNECT,
1131 	FILTER_JUNK,
1132 };
1133 
1134 enum ca_resp_status {
1135 	CA_OK,
1136 	CA_FAIL
1137 };
1138 
1139 enum mda_resp_status {
1140 	MDA_OK,
1141 	MDA_TEMPFAIL,
1142 	MDA_PERMFAIL
1143 };
1144 
1145 struct msg_walkinfo {
1146 	struct event	 ev;
1147 	uint32_t	 msgid;
1148 	uint32_t	 peerid;
1149 	size_t		 n_evp;
1150 	void		*data;
1151 	int		 done;
1152 };
1153 
1154 
1155 enum dispatcher_type {
1156 	DISPATCHER_LOCAL,
1157 	DISPATCHER_REMOTE,
1158 	DISPATCHER_BOUNCE,
1159 };
1160 
1161 struct dispatcher_local {
1162 	uint8_t is_mbox;	/* only for MBOX */
1163 
1164 	uint8_t	expand_only;
1165 	uint8_t	forward_only;
1166 
1167 	char	*mda_wrapper;
1168 	char	*command;
1169 
1170 	char	*table_alias;
1171 	char	*table_virtual;
1172 	char	*table_userbase;
1173 
1174 	char	*user;
1175 };
1176 
1177 struct dispatcher_remote {
1178 	char	*helo;
1179 	char	*helo_source;
1180 
1181 	char	*source;
1182 
1183 	struct tls_config *tls_config;
1184 	char	*ca;
1185 	char	*pki;
1186 
1187 	char	*mail_from;
1188 
1189 	char	*smarthost;
1190 	int	 smarthost_domain;
1191 
1192 	char	*auth;
1193 	int	 tls_required;
1194 	int	 tls_noverify;
1195 
1196 	int	 backup;
1197 	char	*backupmx;
1198 
1199 	char	*filtername;
1200 
1201 	int	 srs;
1202 };
1203 
1204 struct dispatcher_bounce {
1205 };
1206 
1207 struct dispatcher {
1208 	enum dispatcher_type			type;
1209 	union dispatcher_agent {
1210 		struct dispatcher_local		local;
1211 		struct dispatcher_remote  	remote;
1212 		struct dispatcher_bounce  	bounce;
1213 	} u;
1214 
1215 	time_t	ttl;
1216 };
1217 
1218 struct rule {
1219 	TAILQ_ENTRY(rule)	r_entry;
1220 
1221 	uint8_t	reject;
1222 
1223 	int8_t	flag_tag;
1224 	int8_t	flag_from;
1225 	int8_t	flag_for;
1226 	int8_t	flag_from_rdns;
1227 	int8_t	flag_from_socket;
1228 
1229 	int8_t	flag_tag_regex;
1230 	int8_t	flag_from_regex;
1231 	int8_t	flag_for_regex;
1232 
1233 	int8_t	flag_smtp_helo;
1234 	int8_t	flag_smtp_starttls;
1235 	int8_t	flag_smtp_auth;
1236 	int8_t	flag_smtp_mail_from;
1237 	int8_t	flag_smtp_rcpt_to;
1238 
1239 	int8_t	flag_smtp_helo_regex;
1240 	int8_t	flag_smtp_starttls_regex;
1241 	int8_t	flag_smtp_auth_regex;
1242 	int8_t	flag_smtp_mail_from_regex;
1243 	int8_t	flag_smtp_rcpt_to_regex;
1244 
1245 
1246 	char	*table_tag;
1247 	char	*table_from;
1248 	char	*table_for;
1249 
1250 	char	*table_smtp_helo;
1251 	char	*table_smtp_auth;
1252 	char	*table_smtp_mail_from;
1253 	char	*table_smtp_rcpt_to;
1254 
1255 	char	*dispatcher;
1256 };
1257 
1258 
1259 /* aliases.c */
1260 int aliases_get(struct expand *, const char *);
1261 int aliases_virtual_get(struct expand *, const struct mailaddr *);
1262 int alias_parse(struct expandnode *, const char *);
1263 
1264 
1265 /* auth.c */
1266 struct auth_backend *auth_backend_lookup(enum auth_type);
1267 
1268 
1269 /* bounce.c */
1270 void bounce_add(uint64_t);
1271 void bounce_fd(int);
1272 
1273 
1274 /* ca.c */
1275 int	 ca(void);
1276 int	 ca_X509_verify(void *, void *, const char *, const char *, const char **);
1277 void	 ca_imsg(struct mproc *, struct imsg *);
1278 void	 ca_init(void);
1279 void	 ca_engine_init(void);
1280 
1281 
1282 /* cert.c */
1283 int cert_init(const char *, int,
1284     void (*)(void *, int, const char *, const void *, size_t), void *);
1285 int cert_verify(const void *, const char *, int, void (*)(void *, int), void *);
1286 void cert_dispatch_request(struct mproc *, struct imsg *);
1287 void cert_dispatch_result(struct mproc *, struct imsg *);
1288 
1289 
1290 /* compress_backend.c */
1291 struct compress_backend *compress_backend_lookup(const char *);
1292 size_t	compress_chunk(void *, size_t, void *, size_t);
1293 size_t	uncompress_chunk(void *, size_t, void *, size_t);
1294 int	compress_file(FILE *, FILE *);
1295 int	uncompress_file(FILE *, FILE *);
1296 
1297 /* config.c */
1298 #define PURGE_LISTENERS		0x01
1299 #define PURGE_TABLES		0x02
1300 #define PURGE_RULES		0x04
1301 #define PURGE_PKI		0x08
1302 #define PURGE_PKI_KEYS		0x10
1303 #define PURGE_DISPATCHERS	0x20
1304 #define PURGE_EVERYTHING	0xff
1305 struct smtpd *config_default(void);
1306 void purge_config(uint8_t);
1307 void config_process(enum smtp_proc_type);
1308 void config_peer(enum smtp_proc_type);
1309 
1310 
1311 /* control.c */
1312 int control(void);
1313 int control_create_socket(void);
1314 
1315 
1316 /* crypto.c */
1317 int	crypto_setup(const char *, size_t);
1318 int	crypto_encrypt_file(FILE *, FILE *);
1319 int	crypto_decrypt_file(FILE *, FILE *);
1320 size_t	crypto_encrypt_buffer(const char *, size_t, char *, size_t);
1321 size_t	crypto_decrypt_buffer(const char *, size_t, char *, size_t);
1322 
1323 
1324 /* dns.c */
1325 void dns_imsg(struct mproc *, struct imsg *);
1326 
1327 
1328 /* enqueue.c */
1329 int		 enqueue(int, char **, FILE *);
1330 
1331 
1332 /* envelope.c */
1333 void envelope_set_errormsg(struct envelope *, char *, ...);
1334 void envelope_set_esc_class(struct envelope *, enum enhanced_status_class);
1335 void envelope_set_esc_code(struct envelope *, enum enhanced_status_code);
1336 int envelope_load_buffer(struct envelope *, const char *, size_t);
1337 int envelope_dump_buffer(const struct envelope *, char *, size_t);
1338 
1339 
1340 /* expand.c */
1341 int expand_cmp(struct expandnode *, struct expandnode *);
1342 void expand_insert(struct expand *, struct expandnode *);
1343 struct expandnode *expand_lookup(struct expand *, struct expandnode *);
1344 void expand_clear(struct expand *);
1345 void expand_free(struct expand *);
1346 int expand_line(struct expand *, const char *, int);
1347 int expand_to_text(struct expand *, char *, size_t);
1348 RB_PROTOTYPE(expandtree, expandnode, nodes, expand_cmp);
1349 
1350 
1351 /* forward.c */
1352 int forwards_get(int, struct expand *);
1353 
1354 
1355 /* limit.c */
1356 void limit_mta_set_defaults(struct mta_limits *);
1357 int limit_mta_set(struct mta_limits *, const char*, int64_t);
1358 
1359 
1360 /* lka.c */
1361 int lka(void);
1362 
1363 
1364 /* lka_proc.c */
1365 int lka_proc_ready(void);
1366 void lka_proc_forked(const char *, uint32_t, int);
1367 void lka_proc_errfd(const char *, int);
1368 struct io *lka_proc_get_io(const char *);
1369 
1370 
1371 /* lka_report.c */
1372 void lka_report_init(void);
1373 void lka_report_register_hook(const char *, const char *);
1374 void lka_report_smtp_link_connect(const char *, struct timeval *, uint64_t, const char *, int,
1375     const struct sockaddr_storage *, const struct sockaddr_storage *);
1376 void lka_report_smtp_link_disconnect(const char *, struct timeval *, uint64_t);
1377 void lka_report_smtp_link_greeting(const char *, uint64_t, struct timeval *,
1378     const char *);
1379 void lka_report_smtp_link_identify(const char *, struct timeval *, uint64_t, const char *, const char *);
1380 void lka_report_smtp_link_tls(const char *, struct timeval *, uint64_t, const char *);
1381 void lka_report_smtp_link_auth(const char *, struct timeval *, uint64_t, const char *, const char *);
1382 void lka_report_smtp_tx_reset(const char *, struct timeval *, uint64_t, uint32_t);
1383 void lka_report_smtp_tx_begin(const char *, struct timeval *, uint64_t, uint32_t);
1384 void lka_report_smtp_tx_mail(const char *, struct timeval *, uint64_t, uint32_t, const char *, int);
1385 void lka_report_smtp_tx_rcpt(const char *, struct timeval *, uint64_t, uint32_t, const char *, int);
1386 void lka_report_smtp_tx_envelope(const char *, struct timeval *, uint64_t, uint32_t, uint64_t);
1387 void lka_report_smtp_tx_commit(const char *, struct timeval *, uint64_t, uint32_t, size_t);
1388 void lka_report_smtp_tx_data(const char *, struct timeval *, uint64_t, uint32_t, int);
1389 void lka_report_smtp_tx_rollback(const char *, struct timeval *, uint64_t, uint32_t);
1390 void lka_report_smtp_protocol_client(const char *, struct timeval *, uint64_t, const char *);
1391 void lka_report_smtp_protocol_server(const char *, struct timeval *, uint64_t, const char *);
1392 void lka_report_smtp_filter_response(const char *, struct timeval *, uint64_t,
1393     int, int, const char *);
1394 void lka_report_smtp_timeout(const char *, struct timeval *, uint64_t);
1395 void lka_report_filter_report(uint64_t, const char *, int, const char *,
1396     struct timeval *, const char *);
1397 void lka_report_proc(const char *, const char *);
1398 
1399 
1400 /* lka_filter.c */
1401 void lka_filter_init(void);
1402 void lka_filter_register_hook(const char *, const char *);
1403 void lka_filter_ready(void);
1404 int lka_filter_proc_in_session(uint64_t, const char *);
1405 void lka_filter_begin(uint64_t, const char *);
1406 void lka_filter_end(uint64_t);
1407 void lka_filter_protocol(uint64_t, enum filter_phase, const char *);
1408 void lka_filter_data_begin(uint64_t);
1409 void lka_filter_data_end(uint64_t);
1410 int lka_filter_response(uint64_t, const char *, const char *);
1411 
1412 
1413 /* lka_session.c */
1414 void lka_session(uint64_t, struct envelope *);
1415 void lka_session_forward_reply(struct forward_req *, int);
1416 
1417 
1418 /* log.c */
1419 void vlog(int, const char *, va_list);
1420 void logit(int, const char *, ...) __attribute__((format (printf, 2, 3)));
1421 
1422 
1423 /* mda.c */
1424 void mda_postfork(void);
1425 void mda_postprivdrop(void);
1426 void mda_imsg(struct mproc *, struct imsg *);
1427 
1428 
1429 /* mda_mbox.c */
1430 void mda_mbox_init(struct deliver *);
1431 void mda_mbox(struct deliver *);
1432 
1433 
1434 /* mda_unpriv.c */
1435 void mda_unpriv(struct dispatcher *, struct deliver *, const char *, const char *);
1436 
1437 
1438 /* mda_variables.c */
1439 ssize_t mda_expand_format(char *, size_t, const struct deliver *,
1440     const struct userinfo *, const char *);
1441 
1442 
1443 /* makemap.c */
1444 int makemap(int, int, char **);
1445 
1446 
1447 /* mailaddr.c */
1448 int mailaddr_line(struct maddrmap *, const char *);
1449 void maddrmap_init(struct maddrmap *);
1450 void maddrmap_insert(struct maddrmap *, struct maddrnode *);
1451 void maddrmap_free(struct maddrmap *);
1452 
1453 
1454 /* mproc.c */
1455 int mproc_fork(struct mproc *, const char*, char **);
1456 void mproc_init(struct mproc *, int);
1457 void mproc_clear(struct mproc *);
1458 void mproc_enable(struct mproc *);
1459 void mproc_disable(struct mproc *);
1460 void mproc_event_add(struct mproc *);
1461 void m_compose(struct mproc *, uint32_t, uint32_t, pid_t, int, void *, size_t);
1462 void m_composev(struct mproc *, uint32_t, uint32_t, pid_t, int,
1463     const struct iovec *, int);
1464 void m_forward(struct mproc *, struct imsg *);
1465 void m_create(struct mproc *, uint32_t, uint32_t, pid_t, int);
1466 void m_add(struct mproc *, const void *, size_t);
1467 void m_add_int(struct mproc *, int);
1468 void m_add_u32(struct mproc *, uint32_t);
1469 void m_add_size(struct mproc *, size_t);
1470 void m_add_time(struct mproc *, time_t);
1471 void m_add_timeval(struct mproc *, struct timeval *tv);
1472 void m_add_string(struct mproc *, const char *);
1473 void m_add_data(struct mproc *, const void *, size_t);
1474 void m_add_evpid(struct mproc *, uint64_t);
1475 void m_add_msgid(struct mproc *, uint32_t);
1476 void m_add_id(struct mproc *, uint64_t);
1477 void m_add_sockaddr(struct mproc *, const struct sockaddr *);
1478 void m_add_mailaddr(struct mproc *, const struct mailaddr *);
1479 void m_add_envelope(struct mproc *, const struct envelope *);
1480 void m_add_params(struct mproc *, struct dict *);
1481 void m_close(struct mproc *);
1482 void m_flush(struct mproc *);
1483 
1484 void m_msg(struct msg *, struct imsg *);
1485 int  m_is_eom(struct msg *);
1486 void m_end(struct msg *);
1487 void m_get_int(struct msg *, int *);
1488 void m_get_size(struct msg *, size_t *);
1489 void m_get_u32(struct msg *, uint32_t *);
1490 void m_get_time(struct msg *, time_t *);
1491 void m_get_timeval(struct msg *, struct timeval *);
1492 void m_get_string(struct msg *, const char **);
1493 void m_get_data(struct msg *, const void **, size_t *);
1494 void m_get_evpid(struct msg *, uint64_t *);
1495 void m_get_msgid(struct msg *, uint32_t *);
1496 void m_get_id(struct msg *, uint64_t *);
1497 void m_get_sockaddr(struct msg *, struct sockaddr *);
1498 void m_get_mailaddr(struct msg *, struct mailaddr *);
1499 void m_get_envelope(struct msg *, struct envelope *);
1500 void m_get_params(struct msg *, struct dict *);
1501 void m_clear_params(struct dict *);
1502 
1503 
1504 /* mta.c */
1505 void mta_postfork(void);
1506 void mta_postprivdrop(void);
1507 void mta_imsg(struct mproc *, struct imsg *);
1508 void mta_route_ok(struct mta_relay *, struct mta_route *);
1509 void mta_route_error(struct mta_relay *, struct mta_route *);
1510 void mta_route_down(struct mta_relay *, struct mta_route *);
1511 void mta_route_collect(struct mta_relay *, struct mta_route *);
1512 void mta_source_error(struct mta_relay *, struct mta_route *, const char *);
1513 void mta_delivery_log(struct mta_envelope *, const char *, const char *, int, const char *);
1514 void mta_delivery_notify(struct mta_envelope *);
1515 struct mta_task *mta_route_next_task(struct mta_relay *, struct mta_route *);
1516 const char *mta_host_to_text(struct mta_host *);
1517 const char *mta_relay_to_text(struct mta_relay *);
1518 
1519 
1520 /* mta_session.c */
1521 void mta_session(struct mta_relay *, struct mta_route *, const char *);
1522 void mta_session_imsg(struct mproc *, struct imsg *);
1523 
1524 
1525 /* parse.y */
1526 int parse_config(struct smtpd *, const char *, int);
1527 int cmdline_symset(char *);
1528 
1529 
1530 /* queue.c */
1531 int queue(void);
1532 
1533 
1534 /* queue_backend.c */
1535 uint32_t queue_generate_msgid(void);
1536 uint64_t queue_generate_evpid(uint32_t);
1537 int queue_init(const char *, int);
1538 int queue_close(void);
1539 int queue_message_create(uint32_t *);
1540 int queue_message_delete(uint32_t);
1541 int queue_message_commit(uint32_t);
1542 int queue_message_fd_r(uint32_t);
1543 int queue_message_fd_rw(uint32_t);
1544 int queue_envelope_create(struct envelope *);
1545 int queue_envelope_delete(uint64_t);
1546 int queue_envelope_load(uint64_t, struct envelope *);
1547 int queue_envelope_update(struct envelope *);
1548 int queue_envelope_walk(struct envelope *);
1549 int queue_message_walk(struct envelope *, uint32_t, int *, void **);
1550 
1551 
1552 /* report_smtp.c */
1553 void report_smtp_link_connect(const char *, uint64_t, const char *, int,
1554     const struct sockaddr_storage *, const struct sockaddr_storage *);
1555 void report_smtp_link_disconnect(const char *, uint64_t);
1556 void report_smtp_link_greeting(const char *, uint64_t, const char *);
1557 void report_smtp_link_identify(const char *, uint64_t, const char *, const char *);
1558 void report_smtp_link_tls(const char *, uint64_t, const char *);
1559 void report_smtp_link_auth(const char *, uint64_t, const char *, const char *);
1560 void report_smtp_tx_reset(const char *, uint64_t, uint32_t);
1561 void report_smtp_tx_begin(const char *, uint64_t, uint32_t);
1562 void report_smtp_tx_mail(const char *, uint64_t, uint32_t, const char *, int);
1563 void report_smtp_tx_rcpt(const char *, uint64_t, uint32_t, const char *, int);
1564 void report_smtp_tx_envelope(const char *, uint64_t, uint32_t, uint64_t);
1565 void report_smtp_tx_data(const char *, uint64_t, uint32_t, int);
1566 void report_smtp_tx_commit(const char *, uint64_t, uint32_t, size_t);
1567 void report_smtp_tx_rollback(const char *, uint64_t, uint32_t);
1568 void report_smtp_protocol_client(const char *, uint64_t, const char *);
1569 void report_smtp_protocol_server(const char *, uint64_t, const char *);
1570 void report_smtp_filter_response(const char *, uint64_t, int, int, const char *);
1571 void report_smtp_timeout(const char *, uint64_t);
1572 
1573 
1574 /* ruleset.c */
1575 struct rule *ruleset_match(const struct envelope *);
1576 
1577 
1578 /* scheduler.c */
1579 int scheduler(void);
1580 
1581 
1582 /* scheduler_bakend.c */
1583 struct scheduler_backend *scheduler_backend_lookup(const char *);
1584 void scheduler_info(struct scheduler_info *, struct envelope *);
1585 
1586 
1587 /* dispatcher.c */
1588 int dispatcher(void);
1589 void dispatcher_imsg(struct mproc *, struct imsg *);
1590 
1591 
1592 /* resolver.c */
1593 void resolver_getaddrinfo(const char *, const char *, const struct addrinfo *,
1594     void(*)(void *, int, struct addrinfo*), void *);
1595 void resolver_getnameinfo(const struct sockaddr *, int,
1596     void(*)(void *, int, const char *, const char *), void *);
1597 void resolver_res_query(const char *, int, int,
1598     void (*cb)(void *, int, int, int, const void *, int), void *);
1599 void resolver_dispatch_request(struct mproc *, struct imsg *);
1600 void resolver_dispatch_result(struct mproc *, struct imsg *);
1601 
1602 
1603 /* smtp.c */
1604 void smtp_postfork(void);
1605 void smtp_postprivdrop(void);
1606 void smtp_imsg(struct mproc *, struct imsg *);
1607 void smtp_configure(void);
1608 void smtp_collect(void);
1609 
1610 
1611 /* smtp_session.c */
1612 int smtp_session(struct listener *, int, const struct sockaddr_storage *,
1613     const char *, struct io *);
1614 void smtp_session_imsg(struct mproc *, struct imsg *);
1615 
1616 
1617 /* smtpf_session.c */
1618 int smtpf_session(struct listener *, int, const struct sockaddr_storage *,
1619     const char *);
1620 void smtpf_session_imsg(struct mproc *, struct imsg *);
1621 
1622 
1623 /* smtpd.c */
1624 void imsg_dispatch(struct mproc *, struct imsg *);
1625 const char *proc_name(enum smtp_proc_type);
1626 const char *proc_title(enum smtp_proc_type);
1627 const char *imsg_to_str(int);
1628 void log_imsg(int, int, struct imsg *);
1629 int fork_proc_backend(const char *, const char *, const char *);
1630 
1631 
1632 /* srs.c */
1633 const char *srs_encode(const char *, const char *);
1634 const char *srs_decode(const char *);
1635 
1636 
1637 /* ssl_smtpd.c */
1638 void   *ssl_mta_init(void *, char *, off_t, const char *);
1639 void   *ssl_smtp_init(void *, int);
1640 
1641 
1642 /* stat_backend.c */
1643 struct stat_backend	*stat_backend_lookup(const char *);
1644 void	stat_increment(const char *, size_t);
1645 void	stat_decrement(const char *, size_t);
1646 void	stat_set(const char *, const struct stat_value *);
1647 struct stat_value *stat_counter(size_t);
1648 struct stat_value *stat_timestamp(time_t);
1649 struct stat_value *stat_timeval(struct timeval *);
1650 struct stat_value *stat_timespec(struct timespec *);
1651 
1652 
1653 /* table.c */
1654 struct table *table_find(struct smtpd *, const char *);
1655 struct table *table_create(struct smtpd *, const char *, const char *,
1656     const char *);
1657 int	table_config(struct table *);
1658 int	table_open(struct table *);
1659 int	table_update(struct table *);
1660 void	table_close(struct table *);
1661 void	table_dump(struct table *);
1662 int	table_check_use(struct table *, uint32_t, uint32_t);
1663 int	table_check_type(struct table *, uint32_t);
1664 int	table_check_service(struct table *, uint32_t);
1665 int	table_match(struct table *, enum table_service, const char *);
1666 int	table_lookup(struct table *, enum table_service, const char *,
1667     union lookup *);
1668 int	table_fetch(struct table *, enum table_service, union lookup *);
1669 void table_destroy(struct smtpd *, struct table *);
1670 void table_add(struct table *, const char *, const char *);
1671 int table_domain_match(const char *, const char *);
1672 int table_netaddr_match(const char *, const char *);
1673 int table_mailaddr_match(const char *, const char *);
1674 int table_regex_match(const char *, const char *);
1675 void	table_open_all(struct smtpd *);
1676 void	table_dump_all(struct smtpd *);
1677 void	table_close_all(struct smtpd *);
1678 
1679 
1680 /* to.c */
1681 int email_to_mailaddr(struct mailaddr *, char *);
1682 int text_to_netaddr(struct netaddr *, const char *);
1683 int text_to_mailaddr(struct mailaddr *, const char *);
1684 int text_to_relayhost(struct relayhost *, const char *);
1685 int text_to_userinfo(struct userinfo *, const char *);
1686 int text_to_credentials(struct credentials *, const char *);
1687 int text_to_expandnode(struct expandnode *, const char *);
1688 uint64_t text_to_evpid(const char *);
1689 uint32_t text_to_msgid(const char *);
1690 const char *sa_to_text(const struct sockaddr *);
1691 const char *ss_to_text(const struct sockaddr_storage *);
1692 const char *time_to_text(time_t);
1693 const char *duration_to_text(time_t);
1694 const char *rule_to_text(struct rule *);
1695 const char *sockaddr_to_text(const struct sockaddr *);
1696 const char *mailaddr_to_text(const struct mailaddr *);
1697 const char *expandnode_to_text(struct expandnode *);
1698 const char *tls_to_text(struct tls *);
1699 
1700 
1701 /* util.c */
1702 typedef struct arglist arglist;
1703 struct arglist {
1704 	char	**list;
1705 	uint	  num;
1706 	uint	  nalloc;
1707 };
1708 void addargs(arglist *, char *, ...)
1709 	__attribute__((format(printf, 2, 3)));
1710 int bsnprintf(char *, size_t, const char *, ...)
1711 	__attribute__((format (printf, 3, 4)));
1712 int safe_fclose(FILE *);
1713 int hostname_match(const char *, const char *);
1714 int mailaddr_match(const struct mailaddr *, const struct mailaddr *);
1715 int valid_localpart(const char *);
1716 int valid_domainpart(const char *);
1717 int valid_domainname(const char *);
1718 int valid_smtp_response(const char *);
1719 int secure_file(int, char *, char *, uid_t, int);
1720 int  lowercase(char *, const char *, size_t);
1721 void xlowercase(char *, const char *, size_t);
1722 int  uppercase(char *, const char *, size_t);
1723 uint64_t generate_uid(void);
1724 int availdesc(void);
1725 int ckdir(const char *, mode_t, uid_t, gid_t, int);
1726 int rmtree(char *, int);
1727 int mvpurge(char *, char *);
1728 int mktmpfile(void);
1729 const char *parse_smtp_response(char *, size_t, char **, int *);
1730 int xasprintf(char **, const char *, ...)
1731     __attribute__((__format__ (printf, 2, 3)));
1732 void *xmalloc(size_t);
1733 void *xcalloc(size_t, size_t);
1734 char *xstrdup(const char *);
1735 void *xmemdup(const void *, size_t);
1736 char *strip(char *);
1737 int io_xprint(struct io *, const char *);
1738 int io_xprintf(struct io *, const char *, ...)
1739     __attribute__((__format__ (printf, 2, 3)));
1740 void log_envelope(const struct envelope *, const char *, const char *,
1741     const char *);
1742 int session_socket_error(int);
1743 int getmailname(char *, size_t);
1744 int base64_encode(unsigned char const *, size_t, char *, size_t);
1745 int base64_decode(char const *, unsigned char *, size_t);
1746 int base64_encode_rfc3548(unsigned char const *, size_t,
1747 		      char *, size_t);
1748 
1749 void log_trace_verbose(int);
1750 void log_trace(int, const char *, ...)
1751     __attribute__((format (printf, 2, 3)));
1752 
1753 /* waitq.c */
1754 int  waitq_wait(void *, void (*)(void *, void *, void *), void *);
1755 void waitq_run(void *, void *);
1756 
1757 
1758 /* runq.c */
1759 struct runq;
1760 
1761 int runq_init(struct runq **, void (*)(struct runq *, void *));
1762 int runq_schedule(struct runq *, time_t, void *);
1763 int runq_schedule_at(struct runq *, time_t, void *);
1764 int runq_cancel(struct runq *, void *);
1765 int runq_pending(struct runq *, void *, time_t *);
1766