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