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