smtpd.h revision 1.328
1/*	$OpenBSD: smtpd.h,v 1.328 2012/08/21 13:13:17 eric Exp $	*/
2
3/*
4 * Copyright (c) 2008 Gilles Chehade <gilles@openbsd.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 "filter_api.h"
26#include "ioev.h"
27#include "iobuf.h"
28
29#define CONF_FILE		 "/etc/mail/smtpd.conf"
30#define MAX_LISTEN		 16
31#define PROC_COUNT		 9
32#define MAX_NAME_SIZE		 64
33
34#define MAX_HOPS_COUNT		 100
35
36#define MAX_TAG_SIZE		 32
37
38
39/* return and forward path size */
40#define	MAX_FILTER_NAME		 32
41#define MAX_PATH_SIZE		 256
42#define MAX_RULEBUFFER_LEN	 256
43
44#define SMTPD_QUEUE_INTERVAL	 (15 * 60)
45#define SMTPD_QUEUE_MAXINTERVAL	 (4 * 60 * 60)
46#define SMTPD_QUEUE_EXPIRY	 (4 * 24 * 60 * 60)
47#define SMTPD_USER		 "_smtpd"
48#define SMTPD_FILTER_USER      	 "_smtpmfa"
49#define SMTPD_SOCKET		 "/var/run/smtpd.sock"
50#define SMTPD_BANNER		 "220 %s ESMTP OpenSMTPD"
51#define SMTPD_SESSION_TIMEOUT	 300
52#define SMTPD_BACKLOG		 5
53
54#define	PATH_SMTPCTL		"/usr/sbin/smtpctl"
55
56#define	DIRHASH_BUCKETS		 4096
57
58#define PATH_SPOOL		"/var/spool/smtpd"
59#define PATH_OFFLINE		"/offline"
60#define PATH_PURGE		"/purge"
61#define PATH_INCOMING		"/incoming"
62#define PATH_ENVELOPES		"/envelopes"
63#define PATH_MESSAGE		"/message"
64
65/* number of MX records to lookup */
66#define MAX_MX_COUNT		10
67
68/* max response delay under flood conditions */
69#define MAX_RESPONSE_DELAY	60
70
71/* how many responses per state are undelayed */
72#define FAST_RESPONSES		2
73
74/* max len of any smtp line */
75#define	SMTP_LINE_MAX		MAX_LINE_SIZE
76
77#define F_STARTTLS		 0x01
78#define F_SMTPS			 0x02
79#define F_AUTH			 0x04
80#define F_SSL			(F_SMTPS|F_STARTTLS)
81
82#define F_SCERT			0x01
83#define F_CCERT			0x02
84
85/* must match F_* for mta */
86#define ROUTE_STARTTLS		0x01
87#define ROUTE_SMTPS		0x02
88#define ROUTE_SSL		(ROUTE_STARTTLS | ROUTE_SMTPS)
89#define ROUTE_AUTH		0x04
90#define ROUTE_MX		0x08
91
92typedef uint32_t	objid_t;
93
94struct netaddr {
95	struct sockaddr_storage ss;
96	int bits;
97};
98
99struct relayhost {
100	uint8_t flags;
101	char hostname[MAXHOSTNAMELEN];
102	uint16_t port;
103	char cert[PATH_MAX];
104	char authmap[MAX_PATH_SIZE];
105};
106
107enum imsg_type {
108	IMSG_NONE,
109	IMSG_CTL_OK,		/* answer to smtpctl requests */
110	IMSG_CTL_FAIL,
111	IMSG_CTL_SHUTDOWN,
112	IMSG_CTL_VERBOSE,
113	IMSG_CONF_START,
114	IMSG_CONF_SSL,
115	IMSG_CONF_LISTENER,
116	IMSG_CONF_MAP,
117	IMSG_CONF_MAP_CONTENT,
118	IMSG_CONF_RULE,
119	IMSG_CONF_RULE_SOURCE,
120	IMSG_CONF_FILTER,
121	IMSG_CONF_END,
122	IMSG_LKA_MAIL,
123	IMSG_LKA_RCPT,
124	IMSG_LKA_SECRET,
125	IMSG_LKA_RULEMATCH,
126	IMSG_MDA_SESS_NEW,
127	IMSG_MDA_DONE,
128
129	IMSG_MFA_CONNECT,
130 	IMSG_MFA_HELO,
131 	IMSG_MFA_MAIL,
132 	IMSG_MFA_RCPT,
133 	IMSG_MFA_DATALINE,
134	IMSG_MFA_QUIT,
135	IMSG_MFA_CLOSE,
136	IMSG_MFA_RSET,
137
138	IMSG_QUEUE_CREATE_MESSAGE,
139	IMSG_QUEUE_SUBMIT_ENVELOPE,
140	IMSG_QUEUE_COMMIT_ENVELOPES,
141	IMSG_QUEUE_REMOVE_MESSAGE,
142	IMSG_QUEUE_COMMIT_MESSAGE,
143	IMSG_QUEUE_TEMPFAIL,
144	IMSG_QUEUE_PAUSE_MDA,
145	IMSG_QUEUE_PAUSE_MTA,
146	IMSG_QUEUE_RESUME_MDA,
147	IMSG_QUEUE_RESUME_MTA,
148
149	IMSG_QUEUE_DELIVERY_OK,
150	IMSG_QUEUE_DELIVERY_TEMPFAIL,
151	IMSG_QUEUE_DELIVERY_PERMFAIL,
152	IMSG_QUEUE_DELIVERY_LOOP,
153	IMSG_QUEUE_MESSAGE_FD,
154	IMSG_QUEUE_MESSAGE_FILE,
155	IMSG_QUEUE_REMOVE,
156	IMSG_QUEUE_EXPIRE,
157
158	IMSG_SCHEDULER_REMOVE,
159	IMSG_SCHEDULER_SCHEDULE,
160
161	IMSG_BATCH_CREATE,
162	IMSG_BATCH_APPEND,
163	IMSG_BATCH_CLOSE,
164
165	IMSG_PARENT_FORWARD_OPEN,
166	IMSG_PARENT_FORK_MDA,
167
168	IMSG_PARENT_AUTHENTICATE,
169	IMSG_PARENT_SEND_CONFIG,
170
171	IMSG_SMTP_ENQUEUE,
172	IMSG_SMTP_PAUSE,
173	IMSG_SMTP_RESUME,
174
175	IMSG_DNS_HOST,
176	IMSG_DNS_HOST_END,
177	IMSG_DNS_MX,
178	IMSG_DNS_PTR,
179
180	IMSG_STAT_INCREMENT,
181	IMSG_STAT_DECREMENT,
182	IMSG_STAT_SET,
183
184	IMSG_STATS,
185	IMSG_STATS_GET,
186};
187
188enum blockmodes {
189	BM_NORMAL,
190	BM_NONBLOCK
191};
192
193struct imsgev {
194	struct imsgbuf		 ibuf;
195	void			(*handler)(int, short, void *);
196	struct event		 ev;
197	void			*data;
198	int			 proc;
199	short			 events;
200};
201
202struct ctl_conn {
203	TAILQ_ENTRY(ctl_conn)	 entry;
204	uint8_t			 flags;
205#define CTL_CONN_NOTIFY		 0x01
206	struct imsgev		 iev;
207};
208TAILQ_HEAD(ctl_connlist, ctl_conn);
209
210struct ctl_id {
211	objid_t		 id;
212	char		 name[MAX_NAME_SIZE];
213};
214
215enum smtp_proc_type {
216	PROC_PARENT = 0,
217	PROC_SMTP,
218	PROC_MFA,
219	PROC_LKA,
220	PROC_QUEUE,
221	PROC_MDA,
222	PROC_MTA,
223	PROC_CONTROL,
224	PROC_SCHEDULER,
225} smtpd_process;
226
227struct peer {
228	enum smtp_proc_type	 id;
229	void			(*cb)(int, short, void *);
230};
231
232enum map_src {
233	S_NONE,
234	S_PLAIN,
235	S_DB
236};
237
238enum map_kind {
239	K_NONE,
240	K_ALIAS,
241	K_VIRTUAL,
242	K_CREDENTIALS,
243	K_NETADDR
244};
245
246enum mapel_type {
247	ME_STRING,
248	ME_NET,
249	ME_NETMASK
250};
251
252struct mapel {
253	TAILQ_ENTRY(mapel)		 me_entry;
254	union mapel_data {
255		char			 med_string[MAX_LINE_SIZE];
256	}				 me_key;
257	union mapel_data		 me_val;
258};
259
260struct map {
261	TAILQ_ENTRY(map)		 m_entry;
262	char				 m_name[MAX_LINE_SIZE];
263	objid_t				 m_id;
264	enum mapel_type			 m_eltype;
265	enum map_src			 m_src;
266	char				 m_config[MAXPATHLEN];
267	TAILQ_HEAD(mapel_list, mapel)	 m_contents;
268};
269
270
271struct map_backend {
272	void *(*open)(struct map *);
273	void (*close)(void *);
274	void *(*lookup)(void *, char *, enum map_kind);
275	int  (*compare)(void *, char *, enum map_kind, int (*)(char *, char *));
276};
277
278
279enum cond_type {
280	C_ALL,
281	C_NET,
282	C_DOM,
283	C_VDOM
284};
285
286struct cond {
287	TAILQ_ENTRY(cond)		 c_entry;
288	objid_t				 c_map;
289	enum cond_type			 c_type;
290};
291
292enum action_type {
293	A_RELAY,
294	A_RELAYVIA,
295	A_MAILDIR,
296	A_MBOX,
297	A_FILENAME,
298	A_MDA
299};
300
301#define IS_MAILBOX(x)	((x).r_action == A_MAILDIR || (x).r_action == A_MBOX || (x).r_action == A_FILENAME)
302#define IS_RELAY(x)	((x).r_action == A_RELAY || (x).r_action == A_RELAYVIA)
303
304struct rule {
305	TAILQ_ENTRY(rule)		 r_entry;
306	char				 r_tag[MAX_TAG_SIZE];
307	int				 r_accept;
308	struct map			*r_sources;
309	struct cond			 r_condition;
310	enum action_type		 r_action;
311	union rule_dest {
312		char			 buffer[MAX_RULEBUFFER_LEN];
313		struct relayhost       	 relayhost;
314	}				 r_value;
315
316	char				*r_user;
317	struct mailaddr			*r_as;
318	objid_t				 r_amap;
319	time_t				 r_qexpire;
320};
321
322struct mailaddr {
323	char	user[MAX_LOCALPART_SIZE];
324	char	domain[MAX_DOMAINPART_SIZE];
325};
326
327enum delivery_type {
328	D_MDA,
329	D_MTA,
330	D_BOUNCE
331};
332
333enum delivery_status {
334	DS_PERMFAILURE	= 0x1,
335	DS_TEMPFAILURE	= 0x2,
336};
337
338enum delivery_flags {
339	DF_AUTHENTICATED	= 0x1,
340	DF_ENQUEUED		= 0x2,
341	DF_BOUNCE		= 0x4,
342	DF_INTERNAL		= 0x8 /* internal expansion forward */
343};
344
345union delivery_data {
346	char user[MAXLOGNAME];
347	char buffer[MAX_RULEBUFFER_LEN];
348	struct mailaddr mailaddr;
349};
350
351struct delivery_mda {
352	enum action_type	method;
353	union delivery_data	to;
354	char			as_user[MAXLOGNAME];
355};
356
357struct delivery_mta {
358	struct relayhost relay;
359};
360
361enum expand_type {
362	EXPAND_INVALID,
363	EXPAND_USERNAME,
364	EXPAND_FILENAME,
365	EXPAND_FILTER,
366	EXPAND_INCLUDE,
367	EXPAND_ADDRESS
368};
369
370enum expand_flags {
371	F_EXPAND_NONE,
372	F_EXPAND_DONE
373};
374
375struct expandnode {
376	RB_ENTRY(expandnode)	entry;
377	size_t			refcnt;
378	enum expand_flags      	flags;
379	enum expand_type       	type;
380	char			as_user[MAXLOGNAME];
381	union delivery_data    	u;
382};
383
384RB_HEAD(expandtree, expandnode);
385
386#define	SMTPD_ENVELOPE_VERSION		1
387struct envelope {
388	TAILQ_ENTRY(envelope)		entry;
389
390	char				tag[MAX_TAG_SIZE];
391	struct rule			rule;
392
393	uint64_t			session_id;
394	uint64_t			batch_id;
395
396	uint32_t			version;
397	uint64_t			id;
398	enum delivery_type		type;
399
400	char				helo[MAXHOSTNAMELEN];
401	char				hostname[MAXHOSTNAMELEN];
402	char				errorline[MAX_LINE_SIZE + 1];
403	struct sockaddr_storage		ss;
404
405	struct mailaddr			sender;
406	struct mailaddr			rcpt;
407	struct mailaddr			dest;
408
409	union delivery_method {
410		struct delivery_mda	mda;
411		struct delivery_mta	mta;
412	} agent;
413
414	time_t				 creation;
415	time_t				 lasttry;
416	time_t				 expire;
417	uint8_t				 retry;
418	enum delivery_flags		 flags;
419};
420TAILQ_HEAD(deliverylist, envelope);
421
422enum envelope_field {
423	EVP_VERSION,
424	EVP_ID,
425	EVP_MSGID,
426	EVP_TYPE,
427	EVP_HELO,
428	EVP_HOSTNAME,
429	EVP_ERRORLINE,
430	EVP_SOCKADDR,
431	EVP_SENDER,
432	EVP_RCPT,
433	EVP_DEST,
434	EVP_CTIME,
435	EVP_EXPIRE,
436	EVP_RETRY,
437	EVP_LASTTRY,
438	EVP_FLAGS,
439	EVP_MDA_METHOD,
440	EVP_MDA_BUFFER,
441	EVP_MDA_USER,
442	EVP_MTA_RELAY_HOST,
443	EVP_MTA_RELAY_PORT,
444	EVP_MTA_RELAY_FLAGS,
445	EVP_MTA_RELAY_CERT,
446	EVP_MTA_RELAY_AUTHMAP
447};
448
449
450enum child_type {
451	CHILD_INVALID,
452	CHILD_DAEMON,
453	CHILD_MDA,
454	CHILD_ENQUEUE_OFFLINE,
455};
456
457struct child {
458	SPLAY_ENTRY(child)	 entry;
459	pid_t			 pid;
460	enum child_type		 type;
461	enum smtp_proc_type	 title;
462	int			 mda_out;
463	uint32_t		 mda_id;
464	char			*path;
465};
466
467enum session_state {
468	S_NEW = 0,
469	S_CONNECTED,
470	S_INIT,
471	S_GREETED,
472	S_TLS,
473	S_AUTH_INIT,
474	S_AUTH_USERNAME,
475	S_AUTH_PASSWORD,
476	S_AUTH_FINALIZE,
477	S_RSET,
478	S_HELO,
479	S_MAIL_MFA,
480	S_MAIL_QUEUE,
481	S_MAIL,
482	S_RCPT_MFA,
483	S_RCPT,
484	S_DATA,
485	S_DATA_QUEUE,
486	S_DATACONTENT,
487	S_DONE,
488	S_QUIT,
489	S_CLOSE
490};
491#define STATE_COUNT	22
492
493struct ssl {
494	SPLAY_ENTRY(ssl)	 ssl_nodes;
495	char			 ssl_name[PATH_MAX];
496	char			*ssl_ca;
497	off_t			 ssl_ca_len;
498	char			*ssl_cert;
499	off_t			 ssl_cert_len;
500	char			*ssl_key;
501	off_t			 ssl_key_len;
502	char			*ssl_dhparams;
503	off_t			 ssl_dhparams_len;
504	uint8_t			 flags;
505};
506
507struct listener {
508	uint8_t			 flags;
509	int			 fd;
510	struct sockaddr_storage	 ss;
511	in_port_t		 port;
512	struct timeval		 timeout;
513	struct event		 ev;
514	char			 ssl_cert_name[PATH_MAX];
515	struct ssl		*ssl;
516	void			*ssl_ctx;
517	char			 tag[MAX_TAG_SIZE];
518	TAILQ_ENTRY(listener)	 entry;
519};
520
521struct auth {
522	uint64_t	 id;
523	char		 user[MAXLOGNAME];
524	char		 pass[MAX_LINE_SIZE + 1];
525	int		 success;
526};
527
528enum session_flags {
529	F_EHLO		= 0x01,
530	F_8BITMIME	= 0x02,
531	F_SECURE	= 0x04,
532	F_AUTHENTICATED	= 0x08,
533	F_WAITIMSG	= 0x10,
534	F_ZOMBIE	= 0x20,
535};
536
537struct session {
538	SPLAY_ENTRY(session)		 s_nodes;
539	uint64_t			 s_id;
540
541	struct iobuf			 s_iobuf;
542	struct io			 s_io;
543
544	enum session_flags		 s_flags;
545	enum session_state		 s_state;
546	struct sockaddr_storage		 s_ss;
547	char				 s_hostname[MAXHOSTNAMELEN];
548	struct event			 s_ev;
549	struct listener			*s_l;
550	void				*s_ssl;
551	struct timeval			 s_tv;
552	struct envelope			 s_msg;
553	short				 s_nresp[STATE_COUNT];
554	size_t				 rcptcount;
555	long				 s_datalen;
556
557	struct auth			 s_auth;
558	int				 s_dstatus;
559
560	FILE				*datafp;
561};
562
563
564struct smtpd {
565	char					 sc_conffile[MAXPATHLEN];
566	size_t					 sc_maxsize;
567
568#define SMTPD_OPT_VERBOSE			 0x00000001
569#define SMTPD_OPT_NOACTION			 0x00000002
570	uint32_t				 sc_opts;
571#define SMTPD_CONFIGURING			 0x00000001
572#define SMTPD_EXITING				 0x00000002
573#define SMTPD_MDA_PAUSED		       	 0x00000004
574#define SMTPD_MTA_PAUSED		       	 0x00000008
575#define SMTPD_SMTP_PAUSED		       	 0x00000010
576#define SMTPD_MDA_BUSY			       	 0x00000020
577#define SMTPD_MTA_BUSY			       	 0x00000040
578#define SMTPD_BOUNCE_BUSY      		       	 0x00000080
579	uint32_t				 sc_flags;
580	struct timeval				 sc_qintval;
581	int					 sc_qexpire;
582	uint32_t				 sc_maxconn;
583	struct event				 sc_ev;
584	int					 *sc_pipes[PROC_COUNT]
585							[PROC_COUNT];
586	struct imsgev				*sc_ievs[PROC_COUNT];
587	int					 sc_instances[PROC_COUNT];
588	int					 sc_instance;
589	char					*sc_title[PROC_COUNT];
590	struct passwd				*sc_pw;
591	char					 sc_hostname[MAXHOSTNAMELEN];
592	struct queue_backend			*sc_queue;
593	struct scheduler_backend		*sc_scheduler;
594	struct stat_backend			*sc_stat;
595
596	time_t					 sc_uptime;
597
598	TAILQ_HEAD(filterlist, filter)		*sc_filters;
599
600	TAILQ_HEAD(listenerlist, listener)	*sc_listeners;
601	TAILQ_HEAD(maplist, map)		*sc_maps, *sc_maps_reload;
602	TAILQ_HEAD(rulelist, rule)		*sc_rules, *sc_rules_reload;
603	SPLAY_HEAD(sessiontree, session)	 sc_sessions;
604	SPLAY_HEAD(ssltree, ssl)		*sc_ssl;
605	SPLAY_HEAD(childtree, child)		 children;
606	SPLAY_HEAD(lkatree, lka_session)	 lka_sessions;
607	SPLAY_HEAD(mfatree, mfa_session)	 mfa_sessions;
608	LIST_HEAD(mdalist, mda_session)		 mda_sessions;
609
610	uint64_t				 filtermask;
611};
612
613#define	TRACE_VERBOSE	0x0001
614#define	TRACE_IMSG	0x0002
615#define	TRACE_IO	0x0004
616#define	TRACE_SMTP	0x0008
617#define	TRACE_MTA	0x0010
618#define	TRACE_BOUNCE	0x0020
619#define	TRACE_SCHEDULER	0x0040
620#define	TRACE_STAT	0x0080
621
622
623struct submit_status {
624	uint64_t			 id;
625	int				 code;
626	union submit_path {
627		struct mailaddr		 maddr;
628		uint32_t		 msgid;
629		uint64_t		 evpid;
630		char			 errormsg[MAX_LINE_SIZE + 1];
631		char			 dataline[MAX_LINE_SIZE + 1];
632	}				 u;
633	enum delivery_flags		 flags;
634	struct sockaddr_storage		 ss;
635	struct envelope			 envelope;
636};
637
638struct forward_req {
639	uint64_t			 id;
640	uint8_t				 status;
641	char				 as_user[MAXLOGNAME];
642	struct envelope			 envelope;
643};
644
645enum dns_status {
646	DNS_OK = 0,
647	DNS_RETRY,
648	DNS_EINVAL,
649	DNS_ENONAME,
650	DNS_ENOTFOUND,
651};
652
653struct dns {
654	uint64_t		 id;
655	char			 host[MAXHOSTNAMELEN];
656	int			 port;
657	int			 error;
658	int			 type;
659	struct imsgev		*asker;
660	struct sockaddr_storage	 ss;
661	struct dns		*next;
662};
663
664struct secret {
665	uint64_t		 id;
666	char			 mapname[MAX_PATH_SIZE];
667	char			 host[MAXHOSTNAMELEN];
668	char			 secret[MAX_LINE_SIZE];
669};
670
671struct mda_session {
672	LIST_ENTRY(mda_session)	 entry;
673	struct envelope		 msg;
674	struct msgbuf		 w;
675	struct event		 ev;
676	uint32_t		 id;
677	FILE			*datafp;
678};
679
680struct deliver {
681	char			to[PATH_MAX];
682	char			from[PATH_MAX];
683	char			user[MAXLOGNAME];
684	short			mode;
685};
686
687struct rulematch {
688	uint64_t		 id;
689	struct submit_status	 ss;
690};
691
692enum lka_session_flags {
693	F_ERROR		= 0x1
694};
695
696struct lka_session {
697	SPLAY_ENTRY(lka_session)	 nodes;
698	uint64_t			 id;
699
700	struct deliverylist		 deliverylist;
701	struct expandtree		 expandtree;
702
703	uint8_t				 iterations;
704	uint32_t			 pending;
705	enum lka_session_flags		 flags;
706	struct submit_status		 ss;
707};
708
709struct filter {
710	TAILQ_ENTRY(filter)     f_entry;
711	pid_t			pid;
712	struct event		ev;
713	struct imsgbuf		*ibuf;
714	char			name[MAX_FILTER_NAME];
715	char			path[MAXPATHLEN];
716};
717
718struct mfa_session {
719	SPLAY_ENTRY(mfa_session)	 nodes;
720	uint64_t			 id;
721
722	enum session_state		 state;
723	struct submit_status		 ss;
724	struct filter			*filter;
725	struct filter_msg		 fm;
726};
727
728struct mta_session;
729
730struct mta_route {
731	SPLAY_ENTRY(mta_route)	 entry;
732	uint64_t		 id;
733
734	uint8_t			 flags;
735	char			*hostname;
736	uint16_t		 port;
737	char			*cert;
738	char			*auth;
739	void			*ssl;
740
741	/* route limits	*/
742	int			 maxconn; 	/* in parallel */
743	int			 maxmail;	/* per session */
744	int			 maxrcpt;	/* per mail */
745
746	int			 refcount;
747
748	int			 ntask;
749	TAILQ_HEAD(, mta_task)	 tasks;
750
751	int			 nsession;
752
753	int			 nfail;
754	char			 errorline[64];
755};
756
757struct mta_task {
758	TAILQ_ENTRY(mta_task)	 entry;
759	struct mta_route	*route;
760	uint32_t		 msgid;
761	TAILQ_HEAD(, envelope)	 envelopes;
762	struct mailaddr		 sender;
763	struct mta_session	*session;
764};
765
766/* maps return structures */
767struct map_credentials {
768	char username[MAX_LINE_SIZE];
769	char password[MAX_LINE_SIZE];
770};
771
772struct map_alias {
773	size_t			nbnodes;
774	struct expandtree	expandtree;
775};
776
777struct map_virtual {
778	size_t			nbnodes;
779	struct expandtree	expandtree;
780};
781
782struct map_netaddr {
783	struct netaddr		netaddr;
784};
785
786enum queue_op {
787	QOP_INVALID=0,
788	QOP_CREATE,
789	QOP_DELETE,
790	QOP_UPDATE,
791	QOP_COMMIT,
792	QOP_LOAD,
793	QOP_FD_R,
794	QOP_CORRUPT,
795};
796
797struct queue_backend {
798	int (*init)(int);
799	int (*message)(enum queue_op, uint32_t *);
800	int (*envelope)(enum queue_op, struct envelope *);
801
802	void *(*qwalk_new)(uint32_t);
803	int   (*qwalk)(void *, uint64_t *);
804	void  (*qwalk_close)(void *);
805};
806
807
808/* auth structures */
809enum auth_type {
810	AUTH_BSD,
811	AUTH_PWD,
812};
813
814struct auth_backend {
815	int (*authenticate)(char *, char *);
816};
817
818
819/* user structures */
820enum user_type {
821	USER_PWD,
822};
823
824#define	MAXPASSWORDLEN	128
825struct mta_user {
826	char username[MAXLOGNAME];
827	char directory[MAXPATHLEN];
828	char password[MAXPASSWORDLEN];
829	uid_t uid;
830	gid_t gid;
831};
832
833struct user_backend {
834	int (*getbyname)(struct mta_user *, char *);
835	int (*getbyuid)(struct mta_user *, uid_t);
836};
837
838
839/* delivery_backend */
840struct delivery_backend {
841	void	(*open)(struct deliver *);
842};
843
844struct scheduler_info {
845	uint64_t		evpid;
846	char			destination[MAXHOSTNAMELEN];
847
848	enum delivery_type	type;
849	time_t			creation;
850	time_t			lasttry;
851	time_t			expire;
852	uint8_t			retry;
853};
854
855struct id_list {
856	struct id_list	*next;
857	uint64_t	 id;
858};
859
860#define SCHED_NONE		0x00
861#define SCHED_DELAY		0x01
862#define SCHED_REMOVE		0x02
863#define SCHED_EXPIRE		0x04
864#define SCHED_BOUNCE		0x08
865#define SCHED_MDA		0x10
866#define SCHED_MTA		0x20
867
868struct scheduler_batch {
869	int		 type;
870	time_t		 delay;
871	struct id_list	*evpids;
872};
873
874struct scheduler_backend {
875	void	(*init)(void);
876
877	void	(*insert)(struct scheduler_info *);
878	void	(*commit)(uint32_t);
879	void	(*rollback)(uint32_t);
880
881	void	(*update)(struct scheduler_info *);
882	void	(*delete)(uint64_t);
883
884	void	(*batch)(int, time_t, struct scheduler_batch *);
885
886	void	(*schedule)(uint64_t);
887	void	(*remove)(uint64_t);
888};
889
890
891#define	STAT_KEY_SIZE	1024
892struct stat_kv {
893	void	*iter;
894	char	key[STAT_KEY_SIZE];
895	size_t	val;
896};
897
898struct stat_backend {
899	void	(*init)(void);
900	void	(*close)(void);
901	void	(*increment)(const char *);
902	void	(*decrement)(const char *);
903	void	(*set)(const char *, size_t);
904	int	(*iter)(void **, char **, size_t *);
905};
906
907
908extern struct smtpd	*env;
909extern void (*imsg_callback)(struct imsgev *, struct imsg *);
910
911
912/* aliases.c */
913int aliases_exist(objid_t, char *);
914int aliases_get(objid_t, struct expandtree *, char *);
915int aliases_vdomain_exists(objid_t, char *);
916int aliases_virtual_exist(objid_t, struct mailaddr *);
917int aliases_virtual_get(objid_t, struct expandtree *, struct mailaddr *);
918int alias_parse(struct expandnode *, char *);
919
920
921/* auth.c */
922struct auth_backend *auth_backend_lookup(enum auth_type);
923
924
925/* bounce.c */
926void bounce_add(uint64_t);
927void bounce_run(uint64_t, int);
928
929
930/* config.c */
931#define PURGE_LISTENERS		0x01
932#define PURGE_MAPS		0x02
933#define PURGE_RULES		0x04
934#define PURGE_SSL		0x08
935#define PURGE_EVERYTHING	0xff
936void purge_config(uint8_t);
937void unconfigure(void);
938void configure(void);
939void init_pipes(void);
940void config_pipes(struct peer *, uint);
941void config_peers(struct peer *, uint);
942
943
944/* control.c */
945pid_t control(void);
946void session_socket_blockmode(int, enum blockmodes);
947void session_socket_no_linger(int);
948int session_socket_error(int);
949
950
951/* delivery.c */
952struct delivery_backend *delivery_backend_lookup(enum action_type);
953
954
955/* dns.c */
956void dns_query_host(char *, int, uint64_t);
957void dns_query_mx(char *, int, uint64_t);
958void dns_query_ptr(struct sockaddr_storage *, uint64_t);
959void dns_async(struct imsgev *, int, struct dns *);
960
961
962/* enqueue.c */
963int		 enqueue(int, char **);
964int		 enqueue_offline(int, char **);
965
966
967/* envelope.c */
968void envelope_set_errormsg(struct envelope *, char *, ...);
969char *envelope_ascii_field_name(enum envelope_field);
970int envelope_ascii_load(enum envelope_field, struct envelope *, char *);
971int envelope_ascii_dump(enum envelope_field, struct envelope *, char *, size_t);
972int envelope_load_buffer(struct envelope *, char *, size_t);
973int envelope_dump_buffer(struct envelope *, char *, size_t);
974
975/* expand.c */
976int expand_cmp(struct expandnode *, struct expandnode *);
977void expandtree_increment_node(struct expandtree *, struct expandnode *);
978void expandtree_decrement_node(struct expandtree *, struct expandnode *);
979void expandtree_remove_node(struct expandtree *, struct expandnode *);
980struct expandnode *expandtree_lookup(struct expandtree *, struct expandnode *);
981void expandtree_free_nodes(struct expandtree *);
982RB_PROTOTYPE(expandtree, expandnode, nodes, expand_cmp);
983
984
985/* forward.c */
986int forwards_get(int, struct expandtree *, char *);
987
988
989/* lka.c */
990pid_t lka(void);
991int lka_session_cmp(struct lka_session *, struct lka_session *);
992SPLAY_PROTOTYPE(lkatree, lka_session, nodes, lka_session_cmp);
993
994/* lka_session.c */
995struct lka_session *lka_session_init(struct submit_status *);
996void lka_session_fail(struct lka_session *);
997void lka_session_destroy(struct lka_session *);
998
999
1000/* map.c */
1001void *map_lookup(objid_t, char *, enum map_kind);
1002int map_compare(objid_t, char *, enum map_kind, int (*)(char *, char *));
1003struct map *map_find(objid_t);
1004struct map *map_findbyname(const char *);
1005
1006
1007/* mda.c */
1008pid_t mda(void);
1009
1010
1011/* mfa.c */
1012pid_t mfa(void);
1013int mfa_session_cmp(struct mfa_session *, struct mfa_session *);
1014SPLAY_PROTOTYPE(mfatree, mfa_session, nodes, mfa_session_cmp);
1015
1016/* mta.c */
1017pid_t mta(void);
1018int mta_response_delivery(const char *);
1019const char *mta_response_status(const char *);
1020const char *mta_response_text(const char *);
1021void mta_route_ok(struct mta_route *);
1022void mta_route_error(struct mta_route *, const char *);
1023void mta_route_collect(struct mta_route *);
1024const char *mta_route_to_text(struct mta_route *);
1025
1026/* mta_session.c */
1027void mta_session(struct mta_route *);
1028void mta_session_imsg(struct imsgev *, struct imsg *);
1029
1030/* parse.y */
1031int parse_config(struct smtpd *, const char *, int);
1032int cmdline_symset(char *);
1033
1034
1035/* queue.c */
1036pid_t queue(void);
1037
1038
1039/* queue_backend.c */
1040uint32_t queue_generate_msgid(void);
1041uint64_t queue_generate_evpid(uint32_t msgid);
1042struct queue_backend *queue_backend_lookup(const char *);
1043int queue_message_incoming_path(uint32_t, char *, size_t);
1044int queue_envelope_incoming_path(uint64_t, char *, size_t);
1045int queue_message_incoming_delete(uint32_t);
1046int queue_message_create(uint32_t *);
1047int queue_message_delete(uint32_t);
1048int queue_message_commit(uint32_t);
1049int queue_message_fd_r(uint32_t);
1050int queue_message_fd_rw(uint32_t);
1051int queue_message_corrupt(uint32_t);
1052int queue_envelope_create(struct envelope *);
1053int queue_envelope_delete(struct envelope *);
1054int queue_envelope_load(uint64_t, struct envelope *);
1055int queue_envelope_update(struct envelope *);
1056void *qwalk_new(uint32_t);
1057int   qwalk(void *, uint64_t *);
1058void  qwalk_close(void *);
1059
1060
1061/* scheduler.c */
1062pid_t scheduler(void);
1063
1064/* scheduler_bakend.c */
1065struct scheduler_backend *scheduler_backend_lookup(const char *);
1066void scheduler_info(struct scheduler_info *, struct envelope *);
1067time_t scheduler_compute_schedule(struct scheduler_info *);
1068
1069/* smtp.c */
1070pid_t smtp(void);
1071void smtp_resume(void);
1072void smtp_destroy(struct session *);
1073
1074/* smtp_session.c */
1075void session_init(struct listener *, struct session *);
1076int session_cmp(struct session *, struct session *);
1077void session_io(struct io *, int);
1078void session_pickup(struct session *, struct submit_status *);
1079void session_destroy(struct session *, const char *);
1080void session_respond(struct session *, char *, ...)
1081	__attribute__ ((format (printf, 2, 3)));
1082
1083SPLAY_PROTOTYPE(sessiontree, session, s_nodes, session_cmp);
1084
1085
1086/* smtpd.c */
1087int	 child_cmp(struct child *, struct child *);
1088void imsg_event_add(struct imsgev *);
1089void imsg_compose_event(struct imsgev *, uint16_t, uint32_t, pid_t,
1090    int, void *, uint16_t);
1091void imsg_dispatch(int, short, void *);
1092const char * proc_to_str(int);
1093const char * imsg_to_str(int);
1094SPLAY_PROTOTYPE(childtree, child, entry, child_cmp);
1095
1096
1097/* ssl.c */
1098void ssl_init(void);
1099void ssl_transaction(struct session *);
1100void ssl_session_init(struct session *);
1101void ssl_session_destroy(struct session *);
1102int ssl_load_certfile(const char *, uint8_t);
1103void ssl_setup(struct listener *);
1104int ssl_cmp(struct ssl *, struct ssl *);
1105void *ssl_mta_init(struct ssl *);
1106SPLAY_PROTOTYPE(ssltree, ssl, ssl_nodes, ssl_cmp);
1107
1108
1109/* ssl_privsep.c */
1110int	 ssl_ctx_use_private_key(void *, char *, off_t);
1111int	 ssl_ctx_use_certificate_chain(void *, char *, off_t);
1112
1113
1114/* stat_backend.c */
1115struct stat_backend	*stat_backend_lookup(const char *);
1116void	stat_increment(const char *);
1117void	stat_decrement(const char *);
1118void	stat_set(const char *, size_t);
1119
1120
1121/* tree.c */
1122SPLAY_HEAD(tree, treeentry);
1123#define tree_init(t) SPLAY_INIT((t))
1124#define tree_empty(t) SPLAY_EMPTY((t))
1125int tree_check(struct tree *, uint64_t);
1126void *tree_set(struct tree *, uint64_t, void *);
1127void tree_xset(struct tree *, uint64_t, void *);
1128void *tree_get(struct tree *, uint64_t);
1129void *tree_xget(struct tree *, uint64_t);
1130void *tree_pop(struct tree *, uint64_t);
1131void *tree_xpop(struct tree *, uint64_t);
1132int tree_poproot(struct tree *, uint64_t *, void **);
1133int tree_root(struct tree *, uint64_t *, void **);
1134int tree_iter(struct tree *, void **, uint64_t *, void **);
1135void tree_merge(struct tree *, struct tree *);
1136
1137
1138/* user.c */
1139struct user_backend *user_backend_lookup(enum user_type);
1140
1141
1142/* util.c */
1143typedef struct arglist arglist;
1144struct arglist {
1145	char	**list;
1146	uint	  num;
1147	uint	  nalloc;
1148};
1149void addargs(arglist *, char *, ...)
1150	__attribute__((format(printf, 2, 3)));
1151int bsnprintf(char *, size_t, const char *, ...)
1152	__attribute__ ((format (printf, 3, 4)));
1153int mkdir_p(char *, mode_t);
1154int safe_fclose(FILE *);
1155int hostname_match(char *, char *);
1156int email_to_mailaddr(struct mailaddr *, char *);
1157int valid_localpart(const char *);
1158int valid_domainpart(const char *);
1159char *ss_to_text(struct sockaddr_storage *);
1160char *time_to_text(time_t);
1161char *duration_to_text(time_t);
1162int secure_file(int, char *, char *, uid_t, int);
1163int  lowercase(char *, char *, size_t);
1164void xlowercase(char *, char *, size_t);
1165void sa_set_port(struct sockaddr *, int);
1166uint64_t generate_uid(void);
1167void fdlimit(double);
1168int availdesc(void);
1169uint32_t evpid_to_msgid(uint64_t);
1170uint64_t msgid_to_evpid(uint32_t);
1171void log_imsg(int, int, struct imsg*);
1172int ckdir(const char *, mode_t, uid_t, gid_t, int);
1173int rmtree(char *, int);
1174int mvpurge(char *, char *);
1175const char *parse_smtp_response(char *, size_t, char **, int *);
1176int text_to_netaddr(struct netaddr *, char *);
1177int text_to_relayhost(struct relayhost *, char *);
1178void *xmalloc(size_t, const char *);
1179void *xcalloc(size_t, size_t, const char *);
1180char *xstrdup(const char *, const char *);
1181