xref: /openbsd-src/usr.sbin/smtpd/smtpd.h (revision 1a8dbaac879b9f3335ad7fb25429ce63ac1d6bac)
1 /*	$OpenBSD: smtpd.h,v 1.659 2020/09/23 19:11:50 martijn 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_PONY,
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 
547 struct smtpd {
548 	char				sc_conffile[PATH_MAX];
549 	size_t				sc_maxsize;
550 
551 #define SMTPD_OPT_VERBOSE		0x00000001
552 #define SMTPD_OPT_NOACTION		0x00000002
553 	uint32_t			sc_opts;
554 
555 #define SMTPD_EXITING			0x00000001 /* unused */
556 #define SMTPD_MDA_PAUSED		0x00000002
557 #define SMTPD_MTA_PAUSED		0x00000004
558 #define SMTPD_SMTP_PAUSED		0x00000008
559 #define SMTPD_MDA_BUSY			0x00000010
560 #define SMTPD_MTA_BUSY			0x00000020
561 #define SMTPD_BOUNCE_BUSY		0x00000040
562 #define SMTPD_SMTP_DISABLED		0x00000080
563 	uint32_t			sc_flags;
564 
565 #define QUEUE_COMPRESSION      		0x00000001
566 #define QUEUE_ENCRYPTION      		0x00000002
567 #define QUEUE_EVPCACHE			0x00000004
568 	uint32_t			sc_queue_flags;
569 	char			       *sc_queue_key;
570 	size_t				sc_queue_evpcache_size;
571 
572 	size_t				sc_session_max_rcpt;
573 	size_t				sc_session_max_mails;
574 
575 	struct dict		       *sc_mda_wrappers;
576 	size_t				sc_mda_max_session;
577 	size_t				sc_mda_max_user_session;
578 	size_t				sc_mda_task_hiwat;
579 	size_t				sc_mda_task_lowat;
580 	size_t				sc_mda_task_release;
581 
582 	size_t				sc_mta_max_deferred;
583 
584 	size_t				sc_scheduler_max_inflight;
585 	size_t				sc_scheduler_max_evp_batch_size;
586 	size_t				sc_scheduler_max_msg_batch_size;
587 	size_t				sc_scheduler_max_schedule;
588 
589 	struct dict		       *sc_filter_processes_dict;
590 
591 	int				sc_ttl;
592 #define MAX_BOUNCE_WARN			4
593 	time_t				sc_bounce_warn[MAX_BOUNCE_WARN];
594 	char				sc_hostname[HOST_NAME_MAX+1];
595 	struct stat_backend	       *sc_stat;
596 	struct compress_backend	       *sc_comp;
597 
598 	time_t					 sc_uptime;
599 
600 	/* This is a listener for a local socket used by smtp_enqueue(). */
601 	struct listener                         *sc_sock_listener;
602 
603 	TAILQ_HEAD(listenerlist, listener)	*sc_listeners;
604 
605 	TAILQ_HEAD(rulelist, rule)		*sc_rules;
606 
607 
608 	struct dict				*sc_filters_dict;
609 	struct dict				*sc_dispatchers;
610 	struct dispatcher			*sc_dispatcher_bounce;
611 
612 	struct dict			       *sc_ca_dict;
613 	struct dict			       *sc_pki_dict;
614 	struct dict			       *sc_ssl_dict;
615 
616 	struct dict			       *sc_tables_dict;		/* keyed lookup	*/
617 
618 	struct dict			       *sc_limits_dict;
619 
620 	char				       *sc_tls_ciphers;
621 
622 	char				       *sc_subaddressing_delim;
623 
624 	char				       *sc_srs_key;
625 	char				       *sc_srs_key_backup;
626 	int				        sc_srs_ttl;
627 
628 	char				       *sc_admd;
629 };
630 
631 #define	TRACE_DEBUG	0x0001
632 #define	TRACE_IMSG	0x0002
633 #define	TRACE_IO	0x0004
634 #define	TRACE_SMTP	0x0008
635 #define	TRACE_FILTERS	0x0010
636 #define	TRACE_MTA	0x0020
637 #define	TRACE_BOUNCE	0x0040
638 #define	TRACE_SCHEDULER	0x0080
639 #define	TRACE_LOOKUP	0x0100
640 #define	TRACE_STAT	0x0200
641 #define	TRACE_RULES	0x0400
642 #define	TRACE_MPROC	0x0800
643 #define	TRACE_EXPAND	0x1000
644 #define	TRACE_TABLES	0x2000
645 #define	TRACE_QUEUE	0x4000
646 
647 #define PROFILE_TOSTAT	0x0001
648 #define PROFILE_IMSG	0x0002
649 #define PROFILE_QUEUE	0x0004
650 
651 struct forward_req {
652 	uint64_t			id;
653 	uint8_t				status;
654 
655 	char				user[SMTPD_VUSERNAME_SIZE];
656 	uid_t				uid;
657 	gid_t				gid;
658 	char				directory[PATH_MAX];
659 };
660 
661 struct deliver {
662 	char			dispatcher[EXPAND_BUFFER];
663 
664 	struct mailaddr		sender;
665 	struct mailaddr		rcpt;
666 	struct mailaddr		dest;
667 
668 	char			mda_subaddress[SMTPD_SUBADDRESS_SIZE];
669 	char			mda_exec[LINE_MAX];
670 
671 	struct userinfo		userinfo;
672 };
673 
674 struct mta_host {
675 	SPLAY_ENTRY(mta_host)	 entry;
676 	struct sockaddr		*sa;
677 	char			*ptrname;
678 	int			 refcount;
679 	size_t			 nconn;
680 	time_t			 lastconn;
681 	time_t			 lastptrquery;
682 
683 #define HOST_IGNORE	0x01
684 	int			 flags;
685 };
686 
687 struct mta_mx {
688 	TAILQ_ENTRY(mta_mx)	 entry;
689 	struct mta_host		*host;
690 	char			*mxname;
691 	int			 preference;
692 };
693 
694 struct mta_domain {
695 	SPLAY_ENTRY(mta_domain)	 entry;
696 	char			*name;
697 	int			 as_host;
698 	TAILQ_HEAD(, mta_mx)	 mxs;
699 	int			 mxstatus;
700 	int			 refcount;
701 	size_t			 nconn;
702 	time_t			 lastconn;
703 	time_t			 lastmxquery;
704 };
705 
706 struct mta_source {
707 	SPLAY_ENTRY(mta_source)	 entry;
708 	struct sockaddr		*sa;
709 	int			 refcount;
710 	size_t			 nconn;
711 	time_t			 lastconn;
712 };
713 
714 struct mta_connector {
715 	struct mta_source		*source;
716 	struct mta_relay		*relay;
717 
718 #define CONNECTOR_ERROR_FAMILY		0x0001
719 #define CONNECTOR_ERROR_SOURCE		0x0002
720 #define CONNECTOR_ERROR_MX		0x0004
721 #define CONNECTOR_ERROR_ROUTE_NET	0x0008
722 #define CONNECTOR_ERROR_ROUTE_SMTP	0x0010
723 #define CONNECTOR_ERROR_ROUTE		0x0018
724 #define CONNECTOR_ERROR_BLOCKED		0x0020
725 #define CONNECTOR_ERROR			0x00ff
726 
727 #define CONNECTOR_LIMIT_HOST		0x0100
728 #define CONNECTOR_LIMIT_ROUTE		0x0200
729 #define CONNECTOR_LIMIT_SOURCE		0x0400
730 #define CONNECTOR_LIMIT_RELAY		0x0800
731 #define CONNECTOR_LIMIT_CONN		0x1000
732 #define CONNECTOR_LIMIT_DOMAIN		0x2000
733 #define CONNECTOR_LIMIT			0xff00
734 
735 #define CONNECTOR_NEW			0x10000
736 #define CONNECTOR_WAIT			0x20000
737 	int				 flags;
738 
739 	int				 refcount;
740 	size_t				 nconn;
741 	time_t				 lastconn;
742 };
743 
744 struct mta_route {
745 	SPLAY_ENTRY(mta_route)	 entry;
746 	uint64_t		 id;
747 	struct mta_source	*src;
748 	struct mta_host		*dst;
749 #define ROUTE_NEW		0x01
750 #define ROUTE_RUNQ		0x02
751 #define ROUTE_KEEPALIVE		0x04
752 #define ROUTE_DISABLED		0xf0
753 #define ROUTE_DISABLED_NET	0x10
754 #define ROUTE_DISABLED_SMTP	0x20
755 	int			 flags;
756 	int			 nerror;
757 	int			 penalty;
758 	int			 refcount;
759 	size_t			 nconn;
760 	time_t			 lastconn;
761 	time_t			 lastdisc;
762 	time_t			 lastpenalty;
763 };
764 
765 struct mta_limits {
766 	size_t	maxconn_per_host;
767 	size_t	maxconn_per_route;
768 	size_t	maxconn_per_source;
769 	size_t	maxconn_per_connector;
770 	size_t	maxconn_per_relay;
771 	size_t	maxconn_per_domain;
772 
773 	time_t	conndelay_host;
774 	time_t	conndelay_route;
775 	time_t	conndelay_source;
776 	time_t	conndelay_connector;
777 	time_t	conndelay_relay;
778 	time_t	conndelay_domain;
779 
780 	time_t	discdelay_route;
781 
782 	size_t	max_mail_per_session;
783 	time_t	sessdelay_transaction;
784 	time_t	sessdelay_keepalive;
785 
786 	size_t	max_failures_per_session;
787 
788 	int	family;
789 
790 	int	task_hiwat;
791 	int	task_lowat;
792 	int	task_release;
793 };
794 
795 struct mta_relay {
796 	SPLAY_ENTRY(mta_relay)	 entry;
797 	uint64_t		 id;
798 
799 	struct dispatcher	*dispatcher;
800 	struct mta_domain	*domain;
801 	struct mta_limits	*limits;
802 	int			 tls;
803 	int			 flags;
804 	char			*backupname;
805 	int			 backuppref;
806 	char			*sourcetable;
807 	uint16_t		 port;
808 	char			*pki_name;
809 	char			*ca_name;
810 	char			*authtable;
811 	char			*authlabel;
812 	char			*helotable;
813 	char			*heloname;
814 	char			*secret;
815 	int			 srs;
816 
817 	int			 state;
818 	size_t			 ntask;
819 	TAILQ_HEAD(, mta_task)	 tasks;
820 
821 	struct tree		 connectors;
822 	size_t			 sourceloop;
823 	time_t			 lastsource;
824 	time_t			 nextsource;
825 
826 	int			 fail;
827 	char			*failstr;
828 
829 #define RELAY_WAIT_MX		0x01
830 #define RELAY_WAIT_PREFERENCE	0x02
831 #define RELAY_WAIT_SECRET	0x04
832 #define RELAY_WAIT_LIMITS	0x08
833 #define RELAY_WAIT_SOURCE	0x10
834 #define RELAY_WAIT_CONNECTOR	0x20
835 #define RELAY_WAIT_SMARTHOST	0x40
836 #define RELAY_WAITMASK		0x7f
837 	int			 status;
838 
839 	int			 refcount;
840 	size_t			 nconn;
841 	size_t			 nconn_ready;
842 	time_t			 lastconn;
843 };
844 
845 struct mta_envelope {
846 	TAILQ_ENTRY(mta_envelope)	 entry;
847 	uint64_t			 id;
848 	uint64_t			 session;
849 	time_t				 creation;
850 	char				*smtpname;
851 	char				*dest;
852 	char				*rcpt;
853 	struct mta_task			*task;
854 	int				 delivery;
855 
856 	int				 ext;
857 	char				*dsn_orcpt;
858 	char				dsn_envid[DSN_ENVID_LEN+1];
859 	uint8_t				dsn_notify;
860 	enum dsn_ret			dsn_ret;
861 
862 	char				 status[LINE_MAX];
863 };
864 
865 struct mta_task {
866 	TAILQ_ENTRY(mta_task)		 entry;
867 	struct mta_relay		*relay;
868 	uint32_t			 msgid;
869 	TAILQ_HEAD(, mta_envelope)	 envelopes;
870 	char				*sender;
871 };
872 
873 struct passwd;
874 
875 struct queue_backend {
876 	int	(*init)(struct passwd *, int, const char *);
877 };
878 
879 struct compress_backend {
880 	size_t	(*compress_chunk)(void *, size_t, void *, size_t);
881 	size_t	(*uncompress_chunk)(void *, size_t, void *, size_t);
882 	int	(*compress_file)(FILE *, FILE *);
883 	int	(*uncompress_file)(FILE *, FILE *);
884 };
885 
886 /* auth structures */
887 enum auth_type {
888 	AUTH_BSD,
889 	AUTH_PWD,
890 };
891 
892 struct auth_backend {
893 	int	(*authenticate)(char *, char *);
894 };
895 
896 struct scheduler_backend {
897 	int	(*init)(const char *);
898 
899 	int	(*insert)(struct scheduler_info *);
900 	size_t	(*commit)(uint32_t);
901 	size_t	(*rollback)(uint32_t);
902 
903 	int	(*update)(struct scheduler_info *);
904 	int	(*delete)(uint64_t);
905 	int	(*hold)(uint64_t, uint64_t);
906 	int	(*release)(int, uint64_t, int);
907 
908 	int	(*batch)(int, int*, size_t*, uint64_t*, int*);
909 
910 	size_t	(*messages)(uint32_t, uint32_t *, size_t);
911 	size_t	(*envelopes)(uint64_t, struct evpstate *, size_t);
912 	int	(*schedule)(uint64_t);
913 	int	(*remove)(uint64_t);
914 	int	(*suspend)(uint64_t);
915 	int	(*resume)(uint64_t);
916 	int	(*query)(uint64_t);
917 };
918 
919 enum stat_type {
920 	STAT_COUNTER,
921 	STAT_TIMESTAMP,
922 	STAT_TIMEVAL,
923 	STAT_TIMESPEC,
924 };
925 
926 struct stat_value {
927 	enum stat_type	type;
928 	union stat_v {
929 		size_t		counter;
930 		time_t		timestamp;
931 		struct timeval	tv;
932 		struct timespec	ts;
933 	} u;
934 };
935 
936 #define	STAT_KEY_SIZE	1024
937 struct stat_kv {
938 	void	*iter;
939 	char	key[STAT_KEY_SIZE];
940 	struct stat_value	val;
941 };
942 
943 struct stat_backend {
944 	void	(*init)(void);
945 	void	(*close)(void);
946 	void	(*increment)(const char *, size_t);
947 	void	(*decrement)(const char *, size_t);
948 	void	(*set)(const char *, const struct stat_value *);
949 	int	(*iter)(void **, char **, struct stat_value *);
950 };
951 
952 struct stat_digest {
953 	time_t			 startup;
954 	time_t			 timestamp;
955 
956 	size_t			 clt_connect;
957 	size_t			 clt_disconnect;
958 
959 	size_t			 evp_enqueued;
960 	size_t			 evp_dequeued;
961 
962 	size_t			 evp_expired;
963 	size_t			 evp_removed;
964 	size_t			 evp_bounce;
965 
966 	size_t			 dlv_ok;
967 	size_t			 dlv_permfail;
968 	size_t			 dlv_tempfail;
969 	size_t			 dlv_loop;
970 };
971 
972 
973 struct mproc {
974 	pid_t		 pid;
975 	char		*name;
976 	int		 proc;
977 	void		(*handler)(struct mproc *, struct imsg *);
978 	struct imsgbuf	 imsgbuf;
979 
980 	char		*m_buf;
981 	size_t		 m_alloc;
982 	size_t		 m_pos;
983 	uint32_t	 m_type;
984 	uint32_t	 m_peerid;
985 	pid_t		 m_pid;
986 	int		 m_fd;
987 
988 	int		 enable;
989 	short		 events;
990 	struct event	 ev;
991 	void		*data;
992 };
993 
994 struct msg {
995 	const uint8_t	*pos;
996 	const uint8_t	*end;
997 };
998 
999 extern enum smtp_proc_type	smtpd_process;
1000 
1001 extern int tracing;
1002 extern int foreground_log;
1003 extern int profiling;
1004 
1005 extern struct mproc *p_control;
1006 extern struct mproc *p_parent;
1007 extern struct mproc *p_lka;
1008 extern struct mproc *p_queue;
1009 extern struct mproc *p_scheduler;
1010 extern struct mproc *p_pony;
1011 extern struct mproc *p_ca;
1012 
1013 extern struct smtpd	*env;
1014 extern void (*imsg_callback)(struct mproc *, struct imsg *);
1015 
1016 /* inter-process structures */
1017 
1018 struct bounce_req_msg {
1019 	uint64_t		evpid;
1020 	time_t			timestamp;
1021 	struct delivery_bounce	bounce;
1022 };
1023 
1024 enum dns_error {
1025 	DNS_OK = 0,
1026 	DNS_RETRY,
1027 	DNS_EINVAL,
1028 	DNS_ENONAME,
1029 	DNS_ENOTFOUND,
1030 };
1031 
1032 enum lka_resp_status {
1033 	LKA_OK,
1034 	LKA_TEMPFAIL,
1035 	LKA_PERMFAIL
1036 };
1037 
1038 enum filter_type {
1039 	FILTER_TYPE_BUILTIN,
1040 	FILTER_TYPE_PROC,
1041 	FILTER_TYPE_CHAIN,
1042 };
1043 
1044 enum filter_subsystem {
1045 	FILTER_SUBSYSTEM_SMTP_IN	= 1<<0,
1046 	FILTER_SUBSYSTEM_SMTP_OUT	= 1<<1,
1047 };
1048 
1049 struct filter_proc {
1050 	const char		       *command;
1051 	const char		       *user;
1052 	const char		       *group;
1053 	const char		       *chroot;
1054 	int				errfd;
1055 	enum filter_subsystem		filter_subsystem;
1056 };
1057 
1058 struct filter_config {
1059 	char			       *name;
1060 	enum filter_subsystem		filter_subsystem;
1061 	enum filter_type		filter_type;
1062 	enum filter_phase               phase;
1063 	char                           *reject;
1064 	char                           *disconnect;
1065 	char                           *rewrite;
1066 	char                           *report;
1067 	uint8_t				junk;
1068   	uint8_t				bypass;
1069 	char                           *proc;
1070 
1071 	const char		      **chain;
1072 	size_t				chain_size;
1073 	struct dict			chain_procs;
1074 
1075 	int8_t				not_fcrdns;
1076 	int8_t				fcrdns;
1077 
1078 	int8_t				not_rdns;
1079 	int8_t				rdns;
1080 
1081 	int8_t                          not_rdns_table;
1082 	struct table                   *rdns_table;
1083 
1084 	int8_t                          not_rdns_regex;
1085 	struct table                   *rdns_regex;
1086 
1087 	int8_t                          not_src_table;
1088 	struct table                   *src_table;
1089 
1090 	int8_t                          not_src_regex;
1091 	struct table                   *src_regex;
1092 
1093 	int8_t                          not_helo_table;
1094 	struct table                   *helo_table;
1095 
1096 	int8_t                          not_helo_regex;
1097 	struct table                   *helo_regex;
1098 
1099   	int8_t                          not_auth;
1100 	int8_t				auth;
1101 
1102   	int8_t                          not_auth_table;
1103 	struct table                   *auth_table;
1104 
1105 	int8_t                          not_auth_regex;
1106 	struct table                   *auth_regex;
1107 
1108 	int8_t                          not_mail_from_table;
1109 	struct table                   *mail_from_table;
1110 
1111 	int8_t                          not_mail_from_regex;
1112 	struct table                   *mail_from_regex;
1113 
1114 	int8_t                          not_rcpt_to_table;
1115 	struct table                   *rcpt_to_table;
1116 
1117 	int8_t                          not_rcpt_to_regex;
1118 	struct table                   *rcpt_to_regex;
1119 
1120 };
1121 
1122 enum filter_status {
1123 	FILTER_PROCEED,
1124 	FILTER_REWRITE,
1125 	FILTER_REJECT,
1126 	FILTER_DISCONNECT,
1127 	FILTER_JUNK,
1128 };
1129 
1130 enum ca_resp_status {
1131 	CA_OK,
1132 	CA_FAIL
1133 };
1134 
1135 enum mda_resp_status {
1136 	MDA_OK,
1137 	MDA_TEMPFAIL,
1138 	MDA_PERMFAIL
1139 };
1140 
1141 struct msg_walkinfo {
1142 	struct event	 ev;
1143 	uint32_t	 msgid;
1144 	uint32_t	 peerid;
1145 	size_t		 n_evp;
1146 	void		*data;
1147 	int		 done;
1148 };
1149 
1150 
1151 enum dispatcher_type {
1152 	DISPATCHER_LOCAL,
1153 	DISPATCHER_REMOTE,
1154 	DISPATCHER_BOUNCE,
1155 };
1156 
1157 struct dispatcher_local {
1158 	uint8_t is_mbox;	/* only for MBOX */
1159 
1160 	uint8_t	expand_only;
1161 	uint8_t	forward_only;
1162 
1163 	char	*mda_wrapper;
1164 	char	*command;
1165 
1166 	char	*table_alias;
1167 	char	*table_virtual;
1168 	char	*table_userbase;
1169 
1170 	char	*user;
1171 };
1172 
1173 struct dispatcher_remote {
1174 	char	*helo;
1175 	char	*helo_source;
1176 
1177 	char	*source;
1178 
1179 	char	*ca;
1180 	char	*pki;
1181 
1182 	char	*mail_from;
1183 
1184 	char	*smarthost;
1185 	int	 smarthost_domain;
1186 
1187 	char	*auth;
1188 	int	 tls_required;
1189 	int	 tls_noverify;
1190 
1191 	int	 backup;
1192 	char	*backupmx;
1193 
1194 	char	*filtername;
1195 
1196 	int	 srs;
1197 };
1198 
1199 struct dispatcher_bounce {
1200 };
1201 
1202 struct dispatcher {
1203 	enum dispatcher_type			type;
1204 	union dispatcher_agent {
1205 		struct dispatcher_local		local;
1206 		struct dispatcher_remote  	remote;
1207 		struct dispatcher_bounce  	bounce;
1208 	} u;
1209 
1210 	time_t	ttl;
1211 };
1212 
1213 struct rule {
1214 	TAILQ_ENTRY(rule)	r_entry;
1215 
1216 	uint8_t	reject;
1217 
1218 	int8_t	flag_tag;
1219 	int8_t	flag_from;
1220 	int8_t	flag_for;
1221 	int8_t	flag_from_rdns;
1222 	int8_t	flag_from_socket;
1223 
1224 	int8_t	flag_tag_regex;
1225 	int8_t	flag_from_regex;
1226 	int8_t	flag_for_regex;
1227 
1228 	int8_t	flag_smtp_helo;
1229 	int8_t	flag_smtp_starttls;
1230 	int8_t	flag_smtp_auth;
1231 	int8_t	flag_smtp_mail_from;
1232 	int8_t	flag_smtp_rcpt_to;
1233 
1234 	int8_t	flag_smtp_helo_regex;
1235 	int8_t	flag_smtp_starttls_regex;
1236 	int8_t	flag_smtp_auth_regex;
1237 	int8_t	flag_smtp_mail_from_regex;
1238 	int8_t	flag_smtp_rcpt_to_regex;
1239 
1240 
1241 	char	*table_tag;
1242 	char	*table_from;
1243 	char	*table_for;
1244 
1245 	char	*table_smtp_helo;
1246 	char	*table_smtp_auth;
1247 	char	*table_smtp_mail_from;
1248 	char	*table_smtp_rcpt_to;
1249 
1250 	char	*dispatcher;
1251 };
1252 
1253 
1254 /* aliases.c */
1255 int aliases_get(struct expand *, const char *);
1256 int aliases_virtual_get(struct expand *, const struct mailaddr *);
1257 int alias_parse(struct expandnode *, const char *);
1258 
1259 
1260 /* auth.c */
1261 struct auth_backend *auth_backend_lookup(enum auth_type);
1262 
1263 
1264 /* bounce.c */
1265 void bounce_add(uint64_t);
1266 void bounce_fd(int);
1267 
1268 
1269 /* ca.c */
1270 int	 ca(void);
1271 int	 ca_X509_verify(void *, void *, const char *, const char *, const char **);
1272 void	 ca_imsg(struct mproc *, struct imsg *);
1273 void	 ca_init(void);
1274 void	 ca_engine_init(void);
1275 
1276 
1277 /* cert.c */
1278 int cert_init(const char *, int,
1279     void (*)(void *, int, const char *, const void *, size_t), void *);
1280 int cert_verify(const void *, const char *, int, void (*)(void *, int), void *);
1281 void cert_dispatch_request(struct mproc *, struct imsg *);
1282 void cert_dispatch_result(struct mproc *, struct imsg *);
1283 
1284 
1285 /* compress_backend.c */
1286 struct compress_backend *compress_backend_lookup(const char *);
1287 size_t	compress_chunk(void *, size_t, void *, size_t);
1288 size_t	uncompress_chunk(void *, size_t, void *, size_t);
1289 int	compress_file(FILE *, FILE *);
1290 int	uncompress_file(FILE *, FILE *);
1291 
1292 /* config.c */
1293 #define PURGE_LISTENERS		0x01
1294 #define PURGE_TABLES		0x02
1295 #define PURGE_RULES		0x04
1296 #define PURGE_PKI		0x08
1297 #define PURGE_PKI_KEYS		0x10
1298 #define PURGE_DISPATCHERS	0x20
1299 #define PURGE_EVERYTHING	0xff
1300 struct smtpd *config_default(void);
1301 void purge_config(uint8_t);
1302 void config_process(enum smtp_proc_type);
1303 void config_peer(enum smtp_proc_type);
1304 
1305 
1306 /* control.c */
1307 int control(void);
1308 int control_create_socket(void);
1309 
1310 
1311 /* crypto.c */
1312 int	crypto_setup(const char *, size_t);
1313 int	crypto_encrypt_file(FILE *, FILE *);
1314 int	crypto_decrypt_file(FILE *, FILE *);
1315 size_t	crypto_encrypt_buffer(const char *, size_t, char *, size_t);
1316 size_t	crypto_decrypt_buffer(const char *, size_t, char *, size_t);
1317 
1318 
1319 /* dns.c */
1320 void dns_imsg(struct mproc *, struct imsg *);
1321 
1322 
1323 /* enqueue.c */
1324 int		 enqueue(int, char **, FILE *);
1325 
1326 
1327 /* envelope.c */
1328 void envelope_set_errormsg(struct envelope *, char *, ...);
1329 void envelope_set_esc_class(struct envelope *, enum enhanced_status_class);
1330 void envelope_set_esc_code(struct envelope *, enum enhanced_status_code);
1331 int envelope_load_buffer(struct envelope *, const char *, size_t);
1332 int envelope_dump_buffer(const struct envelope *, char *, size_t);
1333 
1334 
1335 /* expand.c */
1336 int expand_cmp(struct expandnode *, struct expandnode *);
1337 void expand_insert(struct expand *, struct expandnode *);
1338 struct expandnode *expand_lookup(struct expand *, struct expandnode *);
1339 void expand_clear(struct expand *);
1340 void expand_free(struct expand *);
1341 int expand_line(struct expand *, const char *, int);
1342 int expand_to_text(struct expand *, char *, size_t);
1343 RB_PROTOTYPE(expandtree, expandnode, nodes, expand_cmp);
1344 
1345 
1346 /* forward.c */
1347 int forwards_get(int, struct expand *);
1348 
1349 
1350 /* limit.c */
1351 void limit_mta_set_defaults(struct mta_limits *);
1352 int limit_mta_set(struct mta_limits *, const char*, int64_t);
1353 
1354 
1355 /* lka.c */
1356 int lka(void);
1357 
1358 
1359 /* lka_proc.c */
1360 int lka_proc_ready(void);
1361 void lka_proc_forked(const char *, uint32_t, int);
1362 void lka_proc_errfd(const char *, int);
1363 struct io *lka_proc_get_io(const char *);
1364 
1365 
1366 /* lka_report.c */
1367 void lka_report_init(void);
1368 void lka_report_register_hook(const char *, const char *);
1369 void lka_report_smtp_link_connect(const char *, struct timeval *, uint64_t, const char *, int,
1370     const struct sockaddr_storage *, const struct sockaddr_storage *);
1371 void lka_report_smtp_link_disconnect(const char *, struct timeval *, uint64_t);
1372 void lka_report_smtp_link_greeting(const char *, uint64_t, struct timeval *,
1373     const char *);
1374 void lka_report_smtp_link_identify(const char *, struct timeval *, uint64_t, const char *, const char *);
1375 void lka_report_smtp_link_tls(const char *, struct timeval *, uint64_t, const char *);
1376 void lka_report_smtp_link_auth(const char *, struct timeval *, uint64_t, const char *, const char *);
1377 void lka_report_smtp_tx_reset(const char *, struct timeval *, uint64_t, uint32_t);
1378 void lka_report_smtp_tx_begin(const char *, struct timeval *, uint64_t, uint32_t);
1379 void lka_report_smtp_tx_mail(const char *, struct timeval *, uint64_t, uint32_t, const char *, int);
1380 void lka_report_smtp_tx_rcpt(const char *, struct timeval *, uint64_t, uint32_t, const char *, int);
1381 void lka_report_smtp_tx_envelope(const char *, struct timeval *, uint64_t, uint32_t, uint64_t);
1382 void lka_report_smtp_tx_commit(const char *, struct timeval *, uint64_t, uint32_t, size_t);
1383 void lka_report_smtp_tx_data(const char *, struct timeval *, uint64_t, uint32_t, int);
1384 void lka_report_smtp_tx_rollback(const char *, struct timeval *, uint64_t, uint32_t);
1385 void lka_report_smtp_protocol_client(const char *, struct timeval *, uint64_t, const char *);
1386 void lka_report_smtp_protocol_server(const char *, struct timeval *, uint64_t, const char *);
1387 void lka_report_smtp_filter_response(const char *, struct timeval *, uint64_t,
1388     int, int, const char *);
1389 void lka_report_smtp_timeout(const char *, struct timeval *, uint64_t);
1390 void lka_report_filter_report(uint64_t, const char *, int, const char *,
1391     struct timeval *, const char *);
1392 void lka_report_proc(const char *, const char *);
1393 
1394 
1395 /* lka_filter.c */
1396 void lka_filter_init(void);
1397 void lka_filter_register_hook(const char *, const char *);
1398 void lka_filter_ready(void);
1399 int lka_filter_proc_in_session(uint64_t, const char *);
1400 void lka_filter_begin(uint64_t, const char *);
1401 void lka_filter_end(uint64_t);
1402 void lka_filter_protocol(uint64_t, enum filter_phase, const char *);
1403 void lka_filter_data_begin(uint64_t);
1404 void lka_filter_data_end(uint64_t);
1405 int lka_filter_response(uint64_t, const char *, const char *);
1406 
1407 
1408 /* lka_session.c */
1409 void lka_session(uint64_t, struct envelope *);
1410 void lka_session_forward_reply(struct forward_req *, int);
1411 
1412 
1413 /* log.c */
1414 void vlog(int, const char *, va_list);
1415 void logit(int, const char *, ...) __attribute__((format (printf, 2, 3)));
1416 
1417 
1418 /* mda.c */
1419 void mda_postfork(void);
1420 void mda_postprivdrop(void);
1421 void mda_imsg(struct mproc *, struct imsg *);
1422 
1423 
1424 /* mda_mbox.c */
1425 void mda_mbox_init(struct deliver *);
1426 void mda_mbox(struct deliver *);
1427 
1428 
1429 /* mda_unpriv.c */
1430 void mda_unpriv(struct dispatcher *, struct deliver *, const char *, const char *);
1431 
1432 
1433 /* mda_variables.c */
1434 ssize_t mda_expand_format(char *, size_t, const struct deliver *,
1435     const struct userinfo *, const char *);
1436 
1437 
1438 /* makemap.c */
1439 int makemap(int, int, char **);
1440 
1441 
1442 /* mailaddr.c */
1443 int mailaddr_line(struct maddrmap *, const char *);
1444 void maddrmap_init(struct maddrmap *);
1445 void maddrmap_insert(struct maddrmap *, struct maddrnode *);
1446 void maddrmap_free(struct maddrmap *);
1447 
1448 
1449 /* mproc.c */
1450 int mproc_fork(struct mproc *, const char*, char **);
1451 void mproc_init(struct mproc *, int);
1452 void mproc_clear(struct mproc *);
1453 void mproc_enable(struct mproc *);
1454 void mproc_disable(struct mproc *);
1455 void mproc_event_add(struct mproc *);
1456 void m_compose(struct mproc *, uint32_t, uint32_t, pid_t, int, void *, size_t);
1457 void m_composev(struct mproc *, uint32_t, uint32_t, pid_t, int,
1458     const struct iovec *, int);
1459 void m_forward(struct mproc *, struct imsg *);
1460 void m_create(struct mproc *, uint32_t, uint32_t, pid_t, int);
1461 void m_add(struct mproc *, const void *, size_t);
1462 void m_add_int(struct mproc *, int);
1463 void m_add_u32(struct mproc *, uint32_t);
1464 void m_add_size(struct mproc *, size_t);
1465 void m_add_time(struct mproc *, time_t);
1466 void m_add_timeval(struct mproc *, struct timeval *tv);
1467 void m_add_string(struct mproc *, const char *);
1468 void m_add_data(struct mproc *, const void *, size_t);
1469 void m_add_evpid(struct mproc *, uint64_t);
1470 void m_add_msgid(struct mproc *, uint32_t);
1471 void m_add_id(struct mproc *, uint64_t);
1472 void m_add_sockaddr(struct mproc *, const struct sockaddr *);
1473 void m_add_mailaddr(struct mproc *, const struct mailaddr *);
1474 void m_add_envelope(struct mproc *, const struct envelope *);
1475 void m_add_params(struct mproc *, struct dict *);
1476 void m_close(struct mproc *);
1477 void m_flush(struct mproc *);
1478 
1479 void m_msg(struct msg *, struct imsg *);
1480 int  m_is_eom(struct msg *);
1481 void m_end(struct msg *);
1482 void m_get_int(struct msg *, int *);
1483 void m_get_size(struct msg *, size_t *);
1484 void m_get_u32(struct msg *, uint32_t *);
1485 void m_get_time(struct msg *, time_t *);
1486 void m_get_timeval(struct msg *, struct timeval *);
1487 void m_get_string(struct msg *, const char **);
1488 void m_get_data(struct msg *, const void **, size_t *);
1489 void m_get_evpid(struct msg *, uint64_t *);
1490 void m_get_msgid(struct msg *, uint32_t *);
1491 void m_get_id(struct msg *, uint64_t *);
1492 void m_get_sockaddr(struct msg *, struct sockaddr *);
1493 void m_get_mailaddr(struct msg *, struct mailaddr *);
1494 void m_get_envelope(struct msg *, struct envelope *);
1495 void m_get_params(struct msg *, struct dict *);
1496 void m_clear_params(struct dict *);
1497 
1498 
1499 /* mta.c */
1500 void mta_postfork(void);
1501 void mta_postprivdrop(void);
1502 void mta_imsg(struct mproc *, struct imsg *);
1503 void mta_route_ok(struct mta_relay *, struct mta_route *);
1504 void mta_route_error(struct mta_relay *, struct mta_route *);
1505 void mta_route_down(struct mta_relay *, struct mta_route *);
1506 void mta_route_collect(struct mta_relay *, struct mta_route *);
1507 void mta_source_error(struct mta_relay *, struct mta_route *, const char *);
1508 void mta_delivery_log(struct mta_envelope *, const char *, const char *, int, const char *);
1509 void mta_delivery_notify(struct mta_envelope *);
1510 struct mta_task *mta_route_next_task(struct mta_relay *, struct mta_route *);
1511 const char *mta_host_to_text(struct mta_host *);
1512 const char *mta_relay_to_text(struct mta_relay *);
1513 
1514 
1515 /* mta_session.c */
1516 void mta_session(struct mta_relay *, struct mta_route *, const char *);
1517 void mta_session_imsg(struct mproc *, struct imsg *);
1518 
1519 
1520 /* parse.y */
1521 int parse_config(struct smtpd *, const char *, int);
1522 int cmdline_symset(char *);
1523 
1524 
1525 /* queue.c */
1526 int queue(void);
1527 
1528 
1529 /* queue_backend.c */
1530 uint32_t queue_generate_msgid(void);
1531 uint64_t queue_generate_evpid(uint32_t);
1532 int queue_init(const char *, int);
1533 int queue_close(void);
1534 int queue_message_create(uint32_t *);
1535 int queue_message_delete(uint32_t);
1536 int queue_message_commit(uint32_t);
1537 int queue_message_fd_r(uint32_t);
1538 int queue_message_fd_rw(uint32_t);
1539 int queue_envelope_create(struct envelope *);
1540 int queue_envelope_delete(uint64_t);
1541 int queue_envelope_load(uint64_t, struct envelope *);
1542 int queue_envelope_update(struct envelope *);
1543 int queue_envelope_walk(struct envelope *);
1544 int queue_message_walk(struct envelope *, uint32_t, int *, void **);
1545 
1546 
1547 /* report_smtp.c */
1548 void report_smtp_link_connect(const char *, uint64_t, const char *, int,
1549     const struct sockaddr_storage *, const struct sockaddr_storage *);
1550 void report_smtp_link_disconnect(const char *, uint64_t);
1551 void report_smtp_link_greeting(const char *, uint64_t, const char *);
1552 void report_smtp_link_identify(const char *, uint64_t, const char *, const char *);
1553 void report_smtp_link_tls(const char *, uint64_t, const char *);
1554 void report_smtp_link_auth(const char *, uint64_t, const char *, const char *);
1555 void report_smtp_tx_reset(const char *, uint64_t, uint32_t);
1556 void report_smtp_tx_begin(const char *, uint64_t, uint32_t);
1557 void report_smtp_tx_mail(const char *, uint64_t, uint32_t, const char *, int);
1558 void report_smtp_tx_rcpt(const char *, uint64_t, uint32_t, const char *, int);
1559 void report_smtp_tx_envelope(const char *, uint64_t, uint32_t, uint64_t);
1560 void report_smtp_tx_data(const char *, uint64_t, uint32_t, int);
1561 void report_smtp_tx_commit(const char *, uint64_t, uint32_t, size_t);
1562 void report_smtp_tx_rollback(const char *, uint64_t, uint32_t);
1563 void report_smtp_protocol_client(const char *, uint64_t, const char *);
1564 void report_smtp_protocol_server(const char *, uint64_t, const char *);
1565 void report_smtp_filter_response(const char *, uint64_t, int, int, const char *);
1566 void report_smtp_timeout(const char *, uint64_t);
1567 
1568 
1569 /* ruleset.c */
1570 struct rule *ruleset_match(const struct envelope *);
1571 
1572 
1573 /* scheduler.c */
1574 int scheduler(void);
1575 
1576 
1577 /* scheduler_bakend.c */
1578 struct scheduler_backend *scheduler_backend_lookup(const char *);
1579 void scheduler_info(struct scheduler_info *, struct envelope *);
1580 
1581 
1582 /* pony.c */
1583 int pony(void);
1584 void pony_imsg(struct mproc *, struct imsg *);
1585 
1586 
1587 /* resolver.c */
1588 void resolver_getaddrinfo(const char *, const char *, const struct addrinfo *,
1589     void(*)(void *, int, struct addrinfo*), void *);
1590 void resolver_getnameinfo(const struct sockaddr *, int,
1591     void(*)(void *, int, const char *, const char *), void *);
1592 void resolver_res_query(const char *, int, int,
1593     void (*cb)(void *, int, int, int, const void *, int), void *);
1594 void resolver_dispatch_request(struct mproc *, struct imsg *);
1595 void resolver_dispatch_result(struct mproc *, struct imsg *);
1596 
1597 
1598 /* smtp.c */
1599 void smtp_postfork(void);
1600 void smtp_postprivdrop(void);
1601 void smtp_imsg(struct mproc *, struct imsg *);
1602 void smtp_configure(void);
1603 void smtp_collect(void);
1604 
1605 
1606 /* smtp_session.c */
1607 int smtp_session(struct listener *, int, const struct sockaddr_storage *,
1608     const char *, struct io *);
1609 void smtp_session_imsg(struct mproc *, struct imsg *);
1610 
1611 
1612 /* smtpf_session.c */
1613 int smtpf_session(struct listener *, int, const struct sockaddr_storage *,
1614     const char *);
1615 void smtpf_session_imsg(struct mproc *, struct imsg *);
1616 
1617 
1618 /* smtpd.c */
1619 void imsg_dispatch(struct mproc *, struct imsg *);
1620 const char *proc_name(enum smtp_proc_type);
1621 const char *proc_title(enum smtp_proc_type);
1622 const char *imsg_to_str(int);
1623 void log_imsg(int, int, struct imsg *);
1624 int fork_proc_backend(const char *, const char *, const char *);
1625 
1626 
1627 /* srs.c */
1628 const char *srs_encode(const char *, const char *);
1629 const char *srs_decode(const char *);
1630 
1631 
1632 /* ssl_smtpd.c */
1633 void   *ssl_mta_init(void *, char *, off_t, const char *);
1634 void   *ssl_smtp_init(void *, int);
1635 
1636 
1637 /* stat_backend.c */
1638 struct stat_backend	*stat_backend_lookup(const char *);
1639 void	stat_increment(const char *, size_t);
1640 void	stat_decrement(const char *, size_t);
1641 void	stat_set(const char *, const struct stat_value *);
1642 struct stat_value *stat_counter(size_t);
1643 struct stat_value *stat_timestamp(time_t);
1644 struct stat_value *stat_timeval(struct timeval *);
1645 struct stat_value *stat_timespec(struct timespec *);
1646 
1647 
1648 /* table.c */
1649 struct table *table_find(struct smtpd *, const char *);
1650 struct table *table_create(struct smtpd *, const char *, const char *,
1651     const char *);
1652 int	table_config(struct table *);
1653 int	table_open(struct table *);
1654 int	table_update(struct table *);
1655 void	table_close(struct table *);
1656 void	table_dump(struct table *);
1657 int	table_check_use(struct table *, uint32_t, uint32_t);
1658 int	table_check_type(struct table *, uint32_t);
1659 int	table_check_service(struct table *, uint32_t);
1660 int	table_match(struct table *, enum table_service, const char *);
1661 int	table_lookup(struct table *, enum table_service, const char *,
1662     union lookup *);
1663 int	table_fetch(struct table *, enum table_service, union lookup *);
1664 void table_destroy(struct smtpd *, struct table *);
1665 void table_add(struct table *, const char *, const char *);
1666 int table_domain_match(const char *, const char *);
1667 int table_netaddr_match(const char *, const char *);
1668 int table_mailaddr_match(const char *, const char *);
1669 int table_regex_match(const char *, const char *);
1670 void	table_open_all(struct smtpd *);
1671 void	table_dump_all(struct smtpd *);
1672 void	table_close_all(struct smtpd *);
1673 
1674 
1675 /* to.c */
1676 int email_to_mailaddr(struct mailaddr *, char *);
1677 int text_to_netaddr(struct netaddr *, const char *);
1678 int text_to_mailaddr(struct mailaddr *, const char *);
1679 int text_to_relayhost(struct relayhost *, const char *);
1680 int text_to_userinfo(struct userinfo *, const char *);
1681 int text_to_credentials(struct credentials *, const char *);
1682 int text_to_expandnode(struct expandnode *, const char *);
1683 uint64_t text_to_evpid(const char *);
1684 uint32_t text_to_msgid(const char *);
1685 const char *sa_to_text(const struct sockaddr *);
1686 const char *ss_to_text(const struct sockaddr_storage *);
1687 const char *time_to_text(time_t);
1688 const char *duration_to_text(time_t);
1689 const char *rule_to_text(struct rule *);
1690 const char *sockaddr_to_text(struct sockaddr *);
1691 const char *mailaddr_to_text(const struct mailaddr *);
1692 const char *expandnode_to_text(struct expandnode *);
1693 
1694 
1695 /* util.c */
1696 typedef struct arglist arglist;
1697 struct arglist {
1698 	char	**list;
1699 	uint	  num;
1700 	uint	  nalloc;
1701 };
1702 void addargs(arglist *, char *, ...)
1703 	__attribute__((format(printf, 2, 3)));
1704 int bsnprintf(char *, size_t, const char *, ...)
1705 	__attribute__((format (printf, 3, 4)));
1706 int safe_fclose(FILE *);
1707 int hostname_match(const char *, const char *);
1708 int mailaddr_match(const struct mailaddr *, const struct mailaddr *);
1709 int valid_localpart(const char *);
1710 int valid_domainpart(const char *);
1711 int valid_domainname(const char *);
1712 int valid_smtp_response(const char *);
1713 int secure_file(int, char *, char *, uid_t, int);
1714 int  lowercase(char *, const char *, size_t);
1715 void xlowercase(char *, const char *, size_t);
1716 int  uppercase(char *, const char *, size_t);
1717 uint64_t generate_uid(void);
1718 int availdesc(void);
1719 int ckdir(const char *, mode_t, uid_t, gid_t, int);
1720 int rmtree(char *, int);
1721 int mvpurge(char *, char *);
1722 int mktmpfile(void);
1723 const char *parse_smtp_response(char *, size_t, char **, int *);
1724 int xasprintf(char **, const char *, ...)
1725     __attribute__((__format__ (printf, 2, 3)));
1726 void *xmalloc(size_t);
1727 void *xcalloc(size_t, size_t);
1728 char *xstrdup(const char *);
1729 void *xmemdup(const void *, size_t);
1730 char *strip(char *);
1731 int io_xprint(struct io *, const char *);
1732 int io_xprintf(struct io *, const char *, ...)
1733     __attribute__((__format__ (printf, 2, 3)));
1734 void log_envelope(const struct envelope *, const char *, const char *,
1735     const char *);
1736 int session_socket_error(int);
1737 int getmailname(char *, size_t);
1738 int base64_encode(unsigned char const *, size_t, char *, size_t);
1739 int base64_decode(char const *, unsigned char *, size_t);
1740 int base64_encode_rfc3548(unsigned char const *, size_t,
1741 		      char *, size_t);
1742 
1743 void log_trace_verbose(int);
1744 void log_trace(int, const char *, ...)
1745     __attribute__((format (printf, 2, 3)));
1746 
1747 /* waitq.c */
1748 int  waitq_wait(void *, void (*)(void *, void *, void *), void *);
1749 void waitq_run(void *, void *);
1750 
1751 
1752 /* runq.c */
1753 struct runq;
1754 
1755 int runq_init(struct runq **, void (*)(struct runq *, void *));
1756 int runq_schedule(struct runq *, time_t, void *);
1757 int runq_schedule_at(struct runq *, time_t, void *);
1758 int runq_cancel(struct runq *, void *);
1759 int runq_pending(struct runq *, void *, time_t *);
1760