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