xref: /openbsd/usr.sbin/smtpd/smtpd.h (revision 7ceb9f7f)
1 /*	$OpenBSD: smtpd.h,v 1.688 2024/09/03 12:07:40 gilles 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.6.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 	unsigned int			 t_services;
360 	void				*t_handle;
361 	struct table_backend		*t_backend;
362 };
363 
364 struct table_backend {
365 	const char *name;
366 	const unsigned int	services;
367 	int	(*config)(struct table *);
368 	int	(*add)(struct table *, const char *, const char *);
369 	void	(*dump)(struct table *);
370 	int	(*open)(struct table *);
371 	int	(*update)(struct table *);
372 	void	(*close)(struct table *);
373 	int	(*lookup)(struct table *, enum table_service, const char *, char **);
374 	int	(*fetch)(struct table *, enum table_service, char **);
375 };
376 
377 
378 enum bounce_type {
379 	B_FAILED,
380 	B_DELAYED,
381 	B_DELIVERED
382 };
383 
384 enum dsn_ret {
385 	DSN_RETFULL = 1,
386 	DSN_RETHDRS
387 };
388 
389 struct delivery_bounce {
390 	enum bounce_type	type;
391 	time_t			delay;
392 	time_t			ttl;
393 	enum dsn_ret		dsn_ret;
394         int			mta_without_dsn;
395 };
396 
397 enum expand_type {
398 	EXPAND_INVALID,
399 	EXPAND_USERNAME,
400 	EXPAND_FILENAME,
401 	EXPAND_FILTER,
402 	EXPAND_INCLUDE,
403 	EXPAND_ADDRESS,
404 	EXPAND_ERROR,
405 };
406 
407 enum filter_phase {
408 	FILTER_CONNECT,
409 	FILTER_HELO,
410 	FILTER_EHLO,
411 	FILTER_STARTTLS,
412 	FILTER_AUTH,
413 	FILTER_MAIL_FROM,
414 	FILTER_RCPT_TO,
415 	FILTER_DATA,
416 	FILTER_DATA_LINE,
417 	FILTER_RSET,
418 	FILTER_QUIT,
419 	FILTER_NOOP,
420 	FILTER_HELP,
421 	FILTER_WIZ,
422 	FILTER_COMMIT,
423 	FILTER_PHASES_COUNT     /* must be last */
424 };
425 
426 struct expandnode {
427 	RB_ENTRY(expandnode)	entry;
428 	TAILQ_ENTRY(expandnode)	tq_entry;
429 	enum expand_type	type;
430 	int			sameuser;
431 	int			realuser;
432 	uid_t			realuser_uid;
433 	int			forwarded;
434 	struct rule	       *rule;
435 	struct expandnode      *parent;
436 	unsigned int		depth;
437 	union {
438 		/*
439 		 * user field handles both expansion user and system user
440 		 * so we MUST make it large enough to fit a mailaddr user
441 		 */
442 		char		user[SMTPD_MAXLOCALPARTSIZE];
443 		char		buffer[EXPAND_BUFFER];
444 		struct mailaddr	mailaddr;
445 	}			u;
446 	char		subaddress[SMTPD_SUBADDRESS_SIZE];
447 };
448 
449 struct expand {
450 	RB_HEAD(expandtree, expandnode)	 tree;
451 	TAILQ_HEAD(xnodes, expandnode)	*queue;
452 	size_t				 nb_nodes;
453 	struct rule			*rule;
454 	struct expandnode		*parent;
455 };
456 
457 struct maddrnode {
458 	TAILQ_ENTRY(maddrnode)		entries;
459 	struct mailaddr			mailaddr;
460 };
461 
462 struct maddrmap {
463 	TAILQ_HEAD(xmaddr, maddrnode)	queue;
464 };
465 
466 #define DSN_SUCCESS 0x01
467 #define DSN_FAILURE 0x02
468 #define DSN_DELAY   0x04
469 #define DSN_NEVER   0x08
470 
471 #define	DSN_ENVID_LEN	100
472 #define	DSN_ORCPT_LEN	500
473 
474 #define	SMTPD_ENVELOPE_VERSION		3
475 struct envelope {
476 	TAILQ_ENTRY(envelope)		entry;
477 
478 	char				dispatcher[HOST_NAME_MAX+1];
479 
480 	char				tag[SMTPD_TAG_SIZE];
481 
482 	uint32_t			version;
483 	uint64_t			id;
484 	enum envelope_flags		flags;
485 
486 	char				smtpname[HOST_NAME_MAX+1];
487 	char				helo[HOST_NAME_MAX+1];
488 	char				hostname[HOST_NAME_MAX+1];
489 	char				username[SMTPD_MAXMAILADDRSIZE];
490 	char				errorline[LINE_MAX];
491 	struct sockaddr_storage		ss;
492 
493 	struct mailaddr			sender;
494 	struct mailaddr			rcpt;
495 	struct mailaddr			dest;
496 
497 	char				mda_user[SMTPD_VUSERNAME_SIZE];
498 	char				mda_subaddress[SMTPD_SUBADDRESS_SIZE];
499 	char				mda_exec[LINE_MAX];
500 
501 	enum delivery_type		type;
502 	union {
503 		struct delivery_bounce	bounce;
504 	}				agent;
505 
506 	uint16_t			retry;
507 	time_t				creation;
508 	time_t				ttl;
509 	time_t				lasttry;
510 	time_t				nexttry;
511 	time_t				lastbounce;
512 
513 	char				dsn_orcpt[DSN_ORCPT_LEN+1];
514 	char				dsn_envid[DSN_ENVID_LEN+1];
515 	uint8_t				dsn_notify;
516 	enum dsn_ret			dsn_ret;
517 
518 	uint8_t				esc_class;
519 	uint8_t				esc_code;
520 };
521 
522 struct listener {
523 	uint16_t       		 flags;
524 	int			 fd;
525 	struct sockaddr_storage	 ss;
526 	in_port_t		 port;
527 	struct timeval		 timeout;
528 	struct event		 ev;
529 	char			 filter_name[PATH_MAX];
530 	char			 pki_name[PATH_MAX];
531 	char			 ca_name[PATH_MAX];
532 	char			 tag[SMTPD_TAG_SIZE];
533 	char			 authtable[LINE_MAX];
534 	char			 hostname[HOST_NAME_MAX+1];
535 	char			 hostnametable[PATH_MAX];
536 	char			 sendertable[PATH_MAX];
537 
538 	TAILQ_ENTRY(listener)	 entry;
539 
540 	int			 local;		/* there must be a better way */
541 
542 	char			*tls_protocols;
543 	char			*tls_ciphers;
544 	struct tls		*tls;
545 	struct pki		**pki;
546 	int			 pkicount;
547 };
548 
549 struct smtpd {
550 	char				sc_conffile[PATH_MAX];
551 	size_t				sc_maxsize;
552 
553 #define SMTPD_OPT_VERBOSE		0x00000001
554 #define SMTPD_OPT_NOACTION		0x00000002
555 	uint32_t			sc_opts;
556 
557 #define SMTPD_EXITING			0x00000001 /* unused */
558 #define SMTPD_MDA_PAUSED		0x00000002
559 #define SMTPD_MTA_PAUSED		0x00000004
560 #define SMTPD_SMTP_PAUSED		0x00000008
561 #define SMTPD_MDA_BUSY			0x00000010
562 #define SMTPD_MTA_BUSY			0x00000020
563 #define SMTPD_BOUNCE_BUSY		0x00000040
564 #define SMTPD_SMTP_DISABLED		0x00000080
565 	uint32_t			sc_flags;
566 
567 #define QUEUE_COMPRESSION      		0x00000001
568 #define QUEUE_ENCRYPTION      		0x00000002
569 #define QUEUE_EVPCACHE			0x00000004
570 	uint32_t			sc_queue_flags;
571 	char			       *sc_queue_key;
572 	size_t				sc_queue_evpcache_size;
573 
574 	size_t				sc_session_max_rcpt;
575 	size_t				sc_session_max_mails;
576 
577 	struct dict		       *sc_mda_wrappers;
578 	size_t				sc_mda_max_session;
579 	size_t				sc_mda_max_user_session;
580 	size_t				sc_mda_task_hiwat;
581 	size_t				sc_mda_task_lowat;
582 	size_t				sc_mda_task_release;
583 
584 	size_t				sc_mta_max_deferred;
585 
586 	size_t				sc_scheduler_max_inflight;
587 	size_t				sc_scheduler_max_evp_batch_size;
588 	size_t				sc_scheduler_max_msg_batch_size;
589 	size_t				sc_scheduler_max_schedule;
590 
591 	struct dict		       *sc_filter_processes_dict;
592 
593 	int				sc_ttl;
594 #define MAX_BOUNCE_WARN			4
595 	time_t				sc_bounce_warn[MAX_BOUNCE_WARN];
596 	char				sc_hostname[HOST_NAME_MAX+1];
597 	struct stat_backend	       *sc_stat;
598 	struct compress_backend	       *sc_comp;
599 
600 	time_t					 sc_uptime;
601 
602 	/* This is a listener for a local socket used by smtp_enqueue(). */
603 	struct listener                         *sc_sock_listener;
604 
605 	TAILQ_HEAD(listenerlist, listener)	*sc_listeners;
606 
607 	TAILQ_HEAD(rulelist, rule)		*sc_rules;
608 
609 
610 	struct dict				*sc_filters_dict;
611 	struct dict				*sc_dispatchers;
612 	struct dispatcher			*sc_dispatcher_bounce;
613 
614 	struct dict			       *sc_ca_dict;
615 	struct dict			       *sc_pki_dict;
616 	struct dict			       *sc_ssl_dict;
617 
618 	struct dict			       *sc_tables_dict;		/* keyed lookup	*/
619 
620 	struct dict			       *sc_limits_dict;
621 
622 	char				       *sc_tls_ciphers;
623 
624 	char				       *sc_subaddressing_delim;
625 
626 	char				       *sc_srs_key;
627 	char				       *sc_srs_key_backup;
628 	int				        sc_srs_ttl;
629 
630 	char				       *sc_admd;
631 };
632 
633 #define	TRACE_DEBUG	0x0001
634 #define	TRACE_IMSG	0x0002
635 #define	TRACE_IO	0x0004
636 #define	TRACE_SMTP	0x0008
637 #define	TRACE_FILTERS	0x0010
638 #define	TRACE_MTA	0x0020
639 #define	TRACE_BOUNCE	0x0040
640 #define	TRACE_SCHEDULER	0x0080
641 #define	TRACE_LOOKUP	0x0100
642 #define	TRACE_STAT	0x0200
643 #define	TRACE_RULES	0x0400
644 #define	TRACE_MPROC	0x0800
645 #define	TRACE_EXPAND	0x1000
646 #define	TRACE_TABLES	0x2000
647 #define	TRACE_QUEUE	0x4000
648 
649 #define PROFILE_TOSTAT	0x0001
650 #define PROFILE_IMSG	0x0002
651 #define PROFILE_QUEUE	0x0004
652 
653 struct forward_req {
654 	uint64_t			id;
655 	uint8_t				status;
656 
657 	char				user[SMTPD_VUSERNAME_SIZE];
658 	uid_t				uid;
659 	gid_t				gid;
660 	char				directory[PATH_MAX];
661 };
662 
663 struct deliver {
664 	char			dispatcher[EXPAND_BUFFER];
665 
666 	struct mailaddr		sender;
667 	struct mailaddr		rcpt;
668 	struct mailaddr		dest;
669 
670 	char			mda_subaddress[SMTPD_SUBADDRESS_SIZE];
671 	char			mda_exec[LINE_MAX];
672 
673 	struct userinfo		userinfo;
674 };
675 
676 struct mta_host {
677 	SPLAY_ENTRY(mta_host)	 entry;
678 	struct sockaddr		*sa;
679 	char			*ptrname;
680 	int			 refcount;
681 	size_t			 nconn;
682 	time_t			 lastconn;
683 	time_t			 lastptrquery;
684 
685 #define HOST_IGNORE	0x01
686 	int			 flags;
687 };
688 
689 struct mta_mx {
690 	TAILQ_ENTRY(mta_mx)	 entry;
691 	struct mta_host		*host;
692 	char			*mxname;
693 	int			 preference;
694 };
695 
696 struct mta_domain {
697 	SPLAY_ENTRY(mta_domain)	 entry;
698 	char			*name;
699 	int			 as_host;
700 	TAILQ_HEAD(, mta_mx)	 mxs;
701 	int			 mxstatus;
702 	int			 refcount;
703 	size_t			 nconn;
704 	time_t			 lastconn;
705 	time_t			 lastmxquery;
706 };
707 
708 struct mta_source {
709 	SPLAY_ENTRY(mta_source)	 entry;
710 	struct sockaddr		*sa;
711 	int			 refcount;
712 	size_t			 nconn;
713 	time_t			 lastconn;
714 };
715 
716 struct mta_connector {
717 	struct mta_source		*source;
718 	struct mta_relay		*relay;
719 
720 #define CONNECTOR_ERROR_FAMILY		0x0001
721 #define CONNECTOR_ERROR_SOURCE		0x0002
722 #define CONNECTOR_ERROR_MX		0x0004
723 #define CONNECTOR_ERROR_ROUTE_NET	0x0008
724 #define CONNECTOR_ERROR_ROUTE_SMTP	0x0010
725 #define CONNECTOR_ERROR_ROUTE		0x0018
726 #define CONNECTOR_ERROR_BLOCKED		0x0020
727 #define CONNECTOR_ERROR			0x00ff
728 
729 #define CONNECTOR_LIMIT_HOST		0x0100
730 #define CONNECTOR_LIMIT_ROUTE		0x0200
731 #define CONNECTOR_LIMIT_SOURCE		0x0400
732 #define CONNECTOR_LIMIT_RELAY		0x0800
733 #define CONNECTOR_LIMIT_CONN		0x1000
734 #define CONNECTOR_LIMIT_DOMAIN		0x2000
735 #define CONNECTOR_LIMIT			0xff00
736 
737 #define CONNECTOR_NEW			0x10000
738 #define CONNECTOR_WAIT			0x20000
739 	int				 flags;
740 
741 	int				 refcount;
742 	size_t				 nconn;
743 	time_t				 lastconn;
744 };
745 
746 struct mta_route {
747 	SPLAY_ENTRY(mta_route)	 entry;
748 	uint64_t		 id;
749 	struct mta_source	*src;
750 	struct mta_host		*dst;
751 #define ROUTE_NEW		0x01
752 #define ROUTE_RUNQ		0x02
753 #define ROUTE_KEEPALIVE		0x04
754 #define ROUTE_DISABLED		0xf0
755 #define ROUTE_DISABLED_NET	0x10
756 #define ROUTE_DISABLED_SMTP	0x20
757 	int			 flags;
758 	int			 nerror;
759 	int			 penalty;
760 	int			 refcount;
761 	size_t			 nconn;
762 	time_t			 lastconn;
763 	time_t			 lastdisc;
764 	time_t			 lastpenalty;
765 };
766 
767 struct mta_limits {
768 	size_t	maxconn_per_host;
769 	size_t	maxconn_per_route;
770 	size_t	maxconn_per_source;
771 	size_t	maxconn_per_connector;
772 	size_t	maxconn_per_relay;
773 	size_t	maxconn_per_domain;
774 
775 	time_t	conndelay_host;
776 	time_t	conndelay_route;
777 	time_t	conndelay_source;
778 	time_t	conndelay_connector;
779 	time_t	conndelay_relay;
780 	time_t	conndelay_domain;
781 
782 	time_t	discdelay_route;
783 
784 	size_t	max_mail_per_session;
785 	time_t	sessdelay_transaction;
786 	time_t	sessdelay_keepalive;
787 
788 	size_t	max_failures_per_session;
789 
790 	int	family;
791 
792 	int	task_hiwat;
793 	int	task_lowat;
794 	int	task_release;
795 };
796 
797 struct mta_relay {
798 	SPLAY_ENTRY(mta_relay)	 entry;
799 	uint64_t		 id;
800 
801 	struct dispatcher	*dispatcher;
802 	struct mta_domain	*domain;
803 	struct mta_limits	*limits;
804 	int			 tls;
805 	int			 flags;
806 	char			*backupname;
807 	int			 backuppref;
808 	char			*sourcetable;
809 	uint16_t		 port;
810 	char			*pki_name;
811 	char			*ca_name;
812 	char			*authtable;
813 	char			*authlabel;
814 	char			*helotable;
815 	char			*heloname;
816 	char			*secret;
817 	int			 srs;
818 
819 	int			 state;
820 	size_t			 ntask;
821 	TAILQ_HEAD(, mta_task)	 tasks;
822 
823 	struct tree		 connectors;
824 	size_t			 sourceloop;
825 	time_t			 lastsource;
826 	time_t			 nextsource;
827 
828 	int			 fail;
829 	char			*failstr;
830 
831 #define RELAY_WAIT_MX		0x01
832 #define RELAY_WAIT_PREFERENCE	0x02
833 #define RELAY_WAIT_SECRET	0x04
834 #define RELAY_WAIT_LIMITS	0x08
835 #define RELAY_WAIT_SOURCE	0x10
836 #define RELAY_WAIT_CONNECTOR	0x20
837 #define RELAY_WAIT_SMARTHOST	0x40
838 #define RELAY_WAITMASK		0x7f
839 	int			 status;
840 
841 	int			 refcount;
842 	size_t			 nconn;
843 	size_t			 nconn_ready;
844 	time_t			 lastconn;
845 };
846 
847 struct mta_envelope {
848 	TAILQ_ENTRY(mta_envelope)	 entry;
849 	uint64_t			 id;
850 	uint64_t			 session;
851 	time_t				 creation;
852 	char				*smtpname;
853 	char				*dest;
854 	char				*rcpt;
855 	struct mta_task			*task;
856 	int				 delivery;
857 
858 	int				 ext;
859 	char				*dsn_orcpt;
860 	char				dsn_envid[DSN_ENVID_LEN+1];
861 	uint8_t				dsn_notify;
862 	enum dsn_ret			dsn_ret;
863 
864 	char				 status[LINE_MAX];
865 };
866 
867 struct mta_task {
868 	TAILQ_ENTRY(mta_task)		 entry;
869 	struct mta_relay		*relay;
870 	uint32_t			 msgid;
871 	TAILQ_HEAD(, mta_envelope)	 envelopes;
872 	char				*sender;
873 };
874 
875 struct passwd;
876 
877 struct queue_backend {
878 	int	(*init)(struct passwd *, int, const char *);
879 };
880 
881 struct compress_backend {
882 	size_t	(*compress_chunk)(void *, size_t, void *, size_t);
883 	size_t	(*uncompress_chunk)(void *, size_t, void *, size_t);
884 	int	(*compress_file)(FILE *, FILE *);
885 	int	(*uncompress_file)(FILE *, FILE *);
886 };
887 
888 /* auth structures */
889 enum auth_type {
890 	AUTH_BSD,
891 	AUTH_PWD,
892 };
893 
894 struct auth_backend {
895 	int	(*authenticate)(char *, char *);
896 };
897 
898 struct scheduler_backend {
899 	int	(*init)(const char *);
900 
901 	int	(*insert)(struct scheduler_info *);
902 	size_t	(*commit)(uint32_t);
903 	size_t	(*rollback)(uint32_t);
904 
905 	int	(*update)(struct scheduler_info *);
906 	int	(*delete)(uint64_t);
907 	int	(*hold)(uint64_t, uint64_t);
908 	int	(*release)(int, uint64_t, int);
909 
910 	int	(*batch)(int, int*, size_t*, uint64_t*, int*);
911 
912 	size_t	(*messages)(uint32_t, uint32_t *, size_t);
913 	size_t	(*envelopes)(uint64_t, struct evpstate *, size_t);
914 	int	(*schedule)(uint64_t);
915 	int	(*remove)(uint64_t);
916 	int	(*suspend)(uint64_t);
917 	int	(*resume)(uint64_t);
918 	int	(*query)(uint64_t);
919 };
920 
921 enum stat_type {
922 	STAT_COUNTER,
923 	STAT_TIMESTAMP,
924 	STAT_TIMEVAL,
925 	STAT_TIMESPEC,
926 };
927 
928 struct stat_value {
929 	enum stat_type	type;
930 	union stat_v {
931 		size_t		counter;
932 		time_t		timestamp;
933 		struct timeval	tv;
934 		struct timespec	ts;
935 	} u;
936 };
937 
938 #define	STAT_KEY_SIZE	1024
939 struct stat_kv {
940 	void	*iter;
941 	char	key[STAT_KEY_SIZE];
942 	struct stat_value	val;
943 };
944 
945 struct stat_backend {
946 	void	(*init)(void);
947 	void	(*close)(void);
948 	void	(*increment)(const char *, size_t);
949 	void	(*decrement)(const char *, size_t);
950 	void	(*set)(const char *, const struct stat_value *);
951 	int	(*iter)(void **, char **, struct stat_value *);
952 };
953 
954 struct stat_digest {
955 	time_t			 startup;
956 	time_t			 timestamp;
957 
958 	size_t			 clt_connect;
959 	size_t			 clt_disconnect;
960 
961 	size_t			 evp_enqueued;
962 	size_t			 evp_dequeued;
963 
964 	size_t			 evp_expired;
965 	size_t			 evp_removed;
966 	size_t			 evp_bounce;
967 
968 	size_t			 dlv_ok;
969 	size_t			 dlv_permfail;
970 	size_t			 dlv_tempfail;
971 	size_t			 dlv_loop;
972 };
973 
974 
975 struct mproc {
976 	pid_t		 pid;
977 	char		*name;
978 	int		 proc;
979 	void		(*handler)(struct mproc *, struct imsg *);
980 	struct imsgbuf	 imsgbuf;
981 
982 	char		*m_buf;
983 	size_t		 m_alloc;
984 	size_t		 m_pos;
985 	uint32_t	 m_type;
986 	uint32_t	 m_peerid;
987 	pid_t		 m_pid;
988 	int		 m_fd;
989 
990 	int		 enable;
991 	short		 events;
992 	struct event	 ev;
993 	void		*data;
994 };
995 
996 struct msg {
997 	const uint8_t	*pos;
998 	const uint8_t	*end;
999 };
1000 
1001 extern enum smtp_proc_type	smtpd_process;
1002 
1003 extern int tracing;
1004 extern int foreground_log;
1005 extern int profiling;
1006 
1007 extern struct mproc *p_control;
1008 extern struct mproc *p_parent;
1009 extern struct mproc *p_lka;
1010 extern struct mproc *p_queue;
1011 extern struct mproc *p_scheduler;
1012 extern struct mproc *p_dispatcher;
1013 extern struct mproc *p_ca;
1014 
1015 extern struct smtpd	*env;
1016 extern void (*imsg_callback)(struct mproc *, struct imsg *);
1017 
1018 /* inter-process structures */
1019 
1020 struct bounce_req_msg {
1021 	uint64_t		evpid;
1022 	time_t			timestamp;
1023 	struct delivery_bounce	bounce;
1024 };
1025 
1026 enum dns_error {
1027 	DNS_OK = 0,
1028 	DNS_RETRY,
1029 	DNS_EINVAL,
1030 	DNS_ENONAME,
1031 	DNS_ENOTFOUND,
1032 	/* RFC 7505 */
1033 	DNS_NULLMX,
1034 };
1035 
1036 enum lka_resp_status {
1037 	LKA_OK,
1038 	LKA_TEMPFAIL,
1039 	LKA_PERMFAIL
1040 };
1041 
1042 enum filter_type {
1043 	FILTER_TYPE_BUILTIN,
1044 	FILTER_TYPE_PROC,
1045 	FILTER_TYPE_CHAIN,
1046 };
1047 
1048 enum filter_subsystem {
1049 	FILTER_SUBSYSTEM_SMTP_IN	= 1<<0,
1050 	FILTER_SUBSYSTEM_SMTP_OUT	= 1<<1,
1051 };
1052 
1053 struct filter_proc {
1054 	const char		       *command;
1055 	const char		       *user;
1056 	const char		       *group;
1057 	const char		       *chroot;
1058 	int				errfd;
1059 	enum filter_subsystem		filter_subsystem;
1060 };
1061 
1062 struct filter_config {
1063 	char			       *name;
1064 	enum filter_subsystem		filter_subsystem;
1065 	enum filter_type		filter_type;
1066 	enum filter_phase               phase;
1067 	char                           *reject;
1068 	char                           *disconnect;
1069 	char                           *rewrite;
1070 	char                           *report;
1071 	uint8_t				junk;
1072   	uint8_t				bypass;
1073 	char                           *proc;
1074 
1075 	const char		      **chain;
1076 	size_t				chain_size;
1077 	struct dict			chain_procs;
1078 
1079 	int8_t				not_fcrdns;
1080 	int8_t				fcrdns;
1081 
1082 	int8_t				not_rdns;
1083 	int8_t				rdns;
1084 
1085 	int8_t                          not_rdns_table;
1086 	struct table                   *rdns_table;
1087 
1088 	int8_t                          not_rdns_regex;
1089 	struct table                   *rdns_regex;
1090 
1091 	int8_t                          not_src_table;
1092 	struct table                   *src_table;
1093 
1094 	int8_t                          not_src_regex;
1095 	struct table                   *src_regex;
1096 
1097 	int8_t                          not_helo_table;
1098 	struct table                   *helo_table;
1099 
1100 	int8_t                          not_helo_regex;
1101 	struct table                   *helo_regex;
1102 
1103   	int8_t                          not_auth;
1104 	int8_t				auth;
1105 
1106   	int8_t                          not_auth_table;
1107 	struct table                   *auth_table;
1108 
1109 	int8_t                          not_auth_regex;
1110 	struct table                   *auth_regex;
1111 
1112 	int8_t                          not_mail_from_table;
1113 	struct table                   *mail_from_table;
1114 
1115 	int8_t                          not_mail_from_regex;
1116 	struct table                   *mail_from_regex;
1117 
1118 	int8_t                          not_rcpt_to_table;
1119 	struct table                   *rcpt_to_table;
1120 
1121 	int8_t                          not_rcpt_to_regex;
1122 	struct table                   *rcpt_to_regex;
1123 
1124 };
1125 
1126 enum filter_status {
1127 	FILTER_PROCEED,
1128 	FILTER_REPORT,
1129 	FILTER_REWRITE,
1130 	FILTER_REJECT,
1131 	FILTER_DISCONNECT,
1132 	FILTER_JUNK,
1133 };
1134 
1135 enum ca_resp_status {
1136 	CA_OK,
1137 	CA_FAIL
1138 };
1139 
1140 enum mda_resp_status {
1141 	MDA_OK,
1142 	MDA_TEMPFAIL,
1143 	MDA_PERMFAIL
1144 };
1145 
1146 struct msg_walkinfo {
1147 	struct event	 ev;
1148 	uint32_t	 msgid;
1149 	uint32_t	 peerid;
1150 	size_t		 n_evp;
1151 	void		*data;
1152 	int		 done;
1153 };
1154 
1155 
1156 enum dispatcher_type {
1157 	DISPATCHER_LOCAL,
1158 	DISPATCHER_REMOTE,
1159 	DISPATCHER_BOUNCE,
1160 };
1161 
1162 struct dispatcher_local {
1163 	uint8_t is_mbox;	/* only for MBOX */
1164 
1165 	uint8_t	expand_only;
1166 	uint8_t	forward_only;
1167 
1168 	char	*mda_wrapper;
1169 	char	*command;
1170 
1171 	char	*table_alias;
1172 	char	*table_virtual;
1173 	char	*table_userbase;
1174 
1175 	char	*user;
1176 };
1177 
1178 struct dispatcher_remote {
1179 	char	*helo;
1180 	char	*helo_source;
1181 
1182 	char	*source;
1183 
1184 	struct tls_config *tls_config;
1185 	char	*ca;
1186 	char	*pki;
1187 
1188 	char	*mail_from;
1189 
1190 	char	*smarthost;
1191 	int	 smarthost_domain;
1192 
1193 	char	*auth;
1194 	int	 tls_required;
1195 	int	 tls_verify;
1196 	char	*tls_protocols;
1197 	char	*tls_ciphers;
1198 
1199 	int	 backup;
1200 	char	*backupmx;
1201 
1202 	char	*filtername;
1203 
1204 	int	 srs;
1205 };
1206 
1207 struct dispatcher_bounce {
1208 };
1209 
1210 struct dispatcher {
1211 	enum dispatcher_type			type;
1212 	union dispatcher_agent {
1213 		struct dispatcher_local		local;
1214 		struct dispatcher_remote  	remote;
1215 		struct dispatcher_bounce  	bounce;
1216 	} u;
1217 
1218 	time_t	ttl;
1219 };
1220 
1221 struct rule {
1222 	TAILQ_ENTRY(rule)	r_entry;
1223 
1224 	uint8_t	reject;
1225 
1226 	int8_t	flag_tag;
1227 	int8_t	flag_from;
1228 	int8_t	flag_for;
1229 	int8_t	flag_from_rdns;
1230 	int8_t	flag_from_socket;
1231 
1232 	int8_t	flag_tag_regex;
1233 	int8_t	flag_from_regex;
1234 	int8_t	flag_for_regex;
1235 
1236 	int8_t	flag_smtp_helo;
1237 	int8_t	flag_smtp_starttls;
1238 	int8_t	flag_smtp_auth;
1239 	int8_t	flag_smtp_mail_from;
1240 	int8_t	flag_smtp_rcpt_to;
1241 
1242 	int8_t	flag_smtp_helo_regex;
1243 	int8_t	flag_smtp_starttls_regex;
1244 	int8_t	flag_smtp_auth_regex;
1245 	int8_t	flag_smtp_mail_from_regex;
1246 	int8_t	flag_smtp_rcpt_to_regex;
1247 
1248 
1249 	char	*table_tag;
1250 	char	*table_from;
1251 	char	*table_for;
1252 
1253 	char	*table_smtp_helo;
1254 	char	*table_smtp_auth;
1255 	char	*table_smtp_mail_from;
1256 	char	*table_smtp_rcpt_to;
1257 
1258 	char	*dispatcher;
1259 };
1260 
1261 
1262 /* aliases.c */
1263 int aliases_get(struct expand *, const char *);
1264 int aliases_virtual_get(struct expand *, const struct mailaddr *);
1265 
1266 
1267 /* bounce.c */
1268 void bounce_add(uint64_t);
1269 void bounce_fd(int);
1270 
1271 
1272 /* ca.c */
1273 int	 ca(void);
1274 int	 ca_X509_verify(void *, void *, const char *, const char *, const char **);
1275 void	 ca_imsg(struct mproc *, struct imsg *);
1276 void	 ca_init(void);
1277 void	 ca_engine_init(void);
1278 
1279 
1280 /* compress_backend.c */
1281 struct compress_backend *compress_backend_lookup(const char *);
1282 size_t	compress_chunk(void *, size_t, void *, size_t);
1283 size_t	uncompress_chunk(void *, size_t, void *, size_t);
1284 int	compress_file(FILE *, FILE *);
1285 int	uncompress_file(FILE *, FILE *);
1286 
1287 /* config.c */
1288 #define PURGE_LISTENERS		0x01
1289 #define PURGE_TABLES		0x02
1290 #define PURGE_RULES		0x04
1291 #define PURGE_PKI		0x08
1292 #define PURGE_PKI_KEYS		0x10
1293 #define PURGE_DISPATCHERS	0x20
1294 #define PURGE_EVERYTHING	0xff
1295 struct smtpd *config_default(void);
1296 void purge_config(uint8_t);
1297 void config_process(enum smtp_proc_type);
1298 void config_peer(enum smtp_proc_type);
1299 
1300 
1301 /* control.c */
1302 int control(void);
1303 int control_create_socket(void);
1304 
1305 
1306 /* crypto.c */
1307 int	crypto_setup(const char *, size_t);
1308 int	crypto_encrypt_file(FILE *, FILE *);
1309 int	crypto_decrypt_file(FILE *, FILE *);
1310 size_t	crypto_encrypt_buffer(const char *, size_t, char *, size_t);
1311 size_t	crypto_decrypt_buffer(const char *, size_t, char *, size_t);
1312 
1313 
1314 /* dns.c */
1315 void dns_imsg(struct mproc *, struct imsg *);
1316 
1317 
1318 /* enqueue.c */
1319 int		 enqueue(int, char **, FILE *);
1320 
1321 
1322 /* envelope.c */
1323 void envelope_set_errormsg(struct envelope *, char *, ...)
1324     __attribute__((__format__ (printf, 2, 3)));
1325 void envelope_set_esc_class(struct envelope *, enum enhanced_status_class);
1326 void envelope_set_esc_code(struct envelope *, enum enhanced_status_code);
1327 int envelope_load_buffer(struct envelope *, const char *, size_t);
1328 int envelope_dump_buffer(const struct envelope *, char *, size_t);
1329 
1330 
1331 /* expand.c */
1332 int expand_cmp(struct expandnode *, struct expandnode *);
1333 void expand_insert(struct expand *, struct expandnode *);
1334 struct expandnode *expand_lookup(struct expand *, struct expandnode *);
1335 void expand_clear(struct expand *);
1336 void expand_free(struct expand *);
1337 int expand_line(struct expand *, const char *, int);
1338 int expand_to_text(struct expand *, char *, size_t);
1339 RB_PROTOTYPE(expandtree, expandnode, nodes, expand_cmp);
1340 
1341 
1342 /* forward.c */
1343 int forwards_get(int, struct expand *);
1344 
1345 
1346 /* limit.c */
1347 void limit_mta_set_defaults(struct mta_limits *);
1348 int limit_mta_set(struct mta_limits *, const char*, int64_t);
1349 
1350 
1351 /* lka.c */
1352 int lka(void);
1353 
1354 
1355 /* lka_proc.c */
1356 int lka_proc_ready(void);
1357 void lka_proc_forked(const char *, uint32_t, int);
1358 void lka_proc_errfd(const char *, int);
1359 struct io *lka_proc_get_io(const char *);
1360 
1361 
1362 /* lka_report.c */
1363 void lka_report_init(void);
1364 void lka_report_register_hook(const char *, const char *);
1365 void lka_report_smtp_link_connect(const char *, struct timeval *, uint64_t, const char *, int,
1366     const struct sockaddr_storage *, const struct sockaddr_storage *);
1367 void lka_report_smtp_link_disconnect(const char *, struct timeval *, uint64_t);
1368 void lka_report_smtp_link_greeting(const char *, uint64_t, struct timeval *,
1369     const char *);
1370 void lka_report_smtp_link_identify(const char *, struct timeval *, uint64_t, const char *, const char *);
1371 void lka_report_smtp_link_tls(const char *, struct timeval *, uint64_t, const char *);
1372 void lka_report_smtp_link_auth(const char *, struct timeval *, uint64_t, const char *, const char *);
1373 void lka_report_smtp_tx_reset(const char *, struct timeval *, uint64_t, uint32_t);
1374 void lka_report_smtp_tx_begin(const char *, struct timeval *, uint64_t, uint32_t);
1375 void lka_report_smtp_tx_mail(const char *, struct timeval *, uint64_t, uint32_t, const char *, int);
1376 void lka_report_smtp_tx_rcpt(const char *, struct timeval *, uint64_t, uint32_t, const char *, int);
1377 void lka_report_smtp_tx_envelope(const char *, struct timeval *, uint64_t, uint32_t, uint64_t);
1378 void lka_report_smtp_tx_commit(const char *, struct timeval *, uint64_t, uint32_t, size_t);
1379 void lka_report_smtp_tx_data(const char *, struct timeval *, uint64_t, uint32_t, int);
1380 void lka_report_smtp_tx_rollback(const char *, struct timeval *, uint64_t, uint32_t);
1381 void lka_report_smtp_protocol_client(const char *, struct timeval *, uint64_t, const char *);
1382 void lka_report_smtp_protocol_server(const char *, struct timeval *, uint64_t, const char *);
1383 void lka_report_smtp_filter_response(const char *, struct timeval *, uint64_t,
1384     int, int, const char *);
1385 void lka_report_smtp_timeout(const char *, struct timeval *, uint64_t);
1386 void lka_report_filter_report(uint64_t, const char *, int, const char *,
1387     struct timeval *, const char *);
1388 void lka_report_proc(const char *, const char *);
1389 
1390 
1391 /* lka_filter.c */
1392 void lka_filter_init(void);
1393 void lka_filter_register_hook(const char *, const char *);
1394 void lka_filter_ready(void);
1395 int lka_filter_proc_in_session(uint64_t, const char *);
1396 void lka_filter_begin(uint64_t, const char *);
1397 void lka_filter_end(uint64_t);
1398 void lka_filter_protocol(uint64_t, enum filter_phase, const char *);
1399 void lka_filter_data_begin(uint64_t);
1400 void lka_filter_data_end(uint64_t);
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 /* smtpd.c */
1608 void imsg_dispatch(struct mproc *, struct imsg *);
1609 const char *proc_name(enum smtp_proc_type);
1610 const char *proc_title(enum smtp_proc_type);
1611 const char *imsg_to_str(int);
1612 void log_imsg(int, int, struct imsg *);
1613 int fork_proc_backend(const char *, const char *, const char *, int);
1614 
1615 
1616 /* srs.c */
1617 const char *srs_encode(const char *, const char *);
1618 const char *srs_decode(const char *);
1619 
1620 
1621 /* stat_backend.c */
1622 struct stat_backend	*stat_backend_lookup(const char *);
1623 void	stat_increment(const char *, size_t);
1624 void	stat_decrement(const char *, size_t);
1625 void	stat_set(const char *, const struct stat_value *);
1626 struct stat_value *stat_counter(size_t);
1627 struct stat_value *stat_timestamp(time_t);
1628 struct stat_value *stat_timeval(struct timeval *);
1629 struct stat_value *stat_timespec(struct timespec *);
1630 
1631 
1632 /* table.c */
1633 const char *table_service_name(enum table_service);
1634 int table_service_from_name(const char *);
1635 struct table *table_find(struct smtpd *, const char *);
1636 struct table *table_create(struct smtpd *, const char *, const char *,
1637     const char *);
1638 int	table_config(struct table *);
1639 int	table_open(struct table *);
1640 int	table_update(struct table *);
1641 void	table_close(struct table *);
1642 void	table_dump(struct table *);
1643 int	table_check_use(struct table *, uint32_t, uint32_t);
1644 int	table_check_type(struct table *, uint32_t);
1645 int	table_check_service(struct table *, uint32_t);
1646 int	table_match(struct table *, enum table_service, const char *);
1647 int	table_lookup(struct table *, enum table_service, const char *,
1648     union lookup *);
1649 int	table_fetch(struct table *, enum table_service, union lookup *);
1650 void table_destroy(struct smtpd *, struct table *);
1651 void table_add(struct table *, const char *, const char *);
1652 int table_domain_match(const char *, const char *);
1653 int table_netaddr_match(const char *, const char *);
1654 int table_mailaddr_match(const char *, const char *);
1655 int table_regex_match(const char *, const char *);
1656 void	table_open_all(struct smtpd *);
1657 void	table_dump_all(struct smtpd *);
1658 void	table_close_all(struct smtpd *);
1659 
1660 
1661 /* to.c */
1662 int text_to_netaddr(struct netaddr *, const char *);
1663 int text_to_mailaddr(struct mailaddr *, const char *);
1664 int text_to_relayhost(struct relayhost *, const char *);
1665 int text_to_userinfo(struct userinfo *, const char *);
1666 int text_to_credentials(struct credentials *, const char *);
1667 int text_to_expandnode(struct expandnode *, const char *);
1668 uint64_t text_to_evpid(const char *);
1669 uint32_t text_to_msgid(const char *);
1670 const char *sa_to_text(const struct sockaddr *);
1671 const char *ss_to_text(const struct sockaddr_storage *);
1672 const char *time_to_text(time_t);
1673 const char *duration_to_text(time_t);
1674 const char *rule_to_text(struct rule *);
1675 const char *sockaddr_to_text(const struct sockaddr *);
1676 const char *mailaddr_to_text(const struct mailaddr *);
1677 const char *expandnode_to_text(struct expandnode *);
1678 const char *tls_to_text(struct tls *);
1679 
1680 
1681 /* util.c */
1682 typedef struct arglist arglist;
1683 struct arglist {
1684 	char	**list;
1685 	uint	  num;
1686 	uint	  nalloc;
1687 };
1688 void addargs(arglist *, char *, ...)
1689 	__attribute__((format(printf, 2, 3)));
1690 int bsnprintf(char *, size_t, const char *, ...)
1691 	__attribute__((format (printf, 3, 4)));
1692 int safe_fclose(FILE *);
1693 int hostname_match(const char *, const char *);
1694 int mailaddr_match(const struct mailaddr *, const struct mailaddr *);
1695 int valid_localpart(const char *);
1696 int valid_domainpart(const char *);
1697 int valid_domainname(const char *);
1698 int valid_smtp_response(const char *);
1699 int valid_xtext(const char *);
1700 int secure_file(int, char *, char *, uid_t, int);
1701 int  lowercase(char *, const char *, size_t);
1702 void xlowercase(char *, const char *, size_t);
1703 int  uppercase(char *, const char *, size_t);
1704 uint64_t generate_uid(void);
1705 int ckdir(const char *, mode_t, uid_t, gid_t, int);
1706 int rmtree(char *, int);
1707 int mvpurge(char *, char *);
1708 int mktmpfile(void);
1709 const char *parse_smtp_response(char *, size_t, char **, int *);
1710 int xasprintf(char **, const char *, ...)
1711     __attribute__((__format__ (printf, 2, 3)));
1712 void *xmalloc(size_t);
1713 void *xcalloc(size_t, size_t);
1714 char *xstrdup(const char *);
1715 void *xmemdup(const void *, size_t);
1716 char *strip(char *);
1717 int io_xprint(struct io *, const char *);
1718 int io_xprintf(struct io *, const char *, ...)
1719     __attribute__((__format__ (printf, 2, 3)));
1720 int session_socket_error(int);
1721 int getmailname(char *, size_t);
1722 int base64_encode(unsigned char const *, size_t, char *, size_t);
1723 int base64_decode(char const *, unsigned char *, size_t);
1724 int base64_encode_rfc3548(unsigned char const *, size_t,
1725 		      char *, size_t);
1726 
1727 void log_trace_verbose(int);
1728 void log_trace0(const char *, ...)
1729     __attribute__((format (printf, 1, 2)));
1730 #define log_trace(m, ...)  do { if (tracing & (m)) log_trace0(__VA_ARGS__); } while (0)
1731 
1732 int parse_table_line(FILE *, char **, size_t *, int *,
1733     char **, char **, int *);
1734 
1735 /* waitq.c */
1736 int  waitq_wait(void *, void (*)(void *, void *, void *), void *);
1737 void waitq_run(void *, void *);
1738 
1739 
1740 /* runq.c */
1741 struct runq;
1742 
1743 int runq_init(struct runq **, void (*)(struct runq *, void *));
1744 int runq_schedule(struct runq *, time_t, void *);
1745 int runq_schedule_at(struct runq *, time_t, void *);
1746 int runq_cancel(struct runq *, void *);
1747 int runq_pending(struct runq *, void *, time_t *);
1748