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