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