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