smtpd.h revision 1.390
1/*	$OpenBSD: smtpd.h,v 1.390 2012/10/16 12:02:23 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				 rcptcount;
547	long				 s_datalen;
548
549	struct auth			 s_auth;
550	int				 s_dstatus;
551
552	FILE				*datafp;
553};
554
555
556struct smtpd {
557	char					 sc_conffile[MAXPATHLEN];
558	size_t					 sc_maxsize;
559
560#define SMTPD_OPT_VERBOSE			 0x00000001
561#define SMTPD_OPT_NOACTION			 0x00000002
562	uint32_t				 sc_opts;
563#define SMTPD_CONFIGURING			 0x00000001
564#define SMTPD_EXITING				 0x00000002
565#define SMTPD_MDA_PAUSED		       	 0x00000004
566#define SMTPD_MTA_PAUSED		       	 0x00000008
567#define SMTPD_SMTP_PAUSED		       	 0x00000010
568#define SMTPD_MDA_BUSY			       	 0x00000020
569#define SMTPD_MTA_BUSY			       	 0x00000040
570#define SMTPD_BOUNCE_BUSY      		       	 0x00000080
571#define SMTPD_SMTP_DISABLED			 0x00000100
572	uint32_t				 sc_flags;
573	uint32_t				 sc_queue_flags;
574#define QUEUE_COMPRESS				 0x00000001
575	char					*sc_queue_compress_algo;
576	struct timeval				 sc_qintval;
577	int					 sc_qexpire;
578	struct event				 sc_ev;
579	int					 *sc_pipes[PROC_COUNT]
580							[PROC_COUNT];
581	struct imsgev				*sc_ievs[PROC_COUNT];
582	int					 sc_instances[PROC_COUNT];
583	int					 sc_instance;
584	char					*sc_title[PROC_COUNT];
585	struct passwd				*sc_pw;
586	char					 sc_hostname[MAXHOSTNAMELEN];
587	struct queue_backend			*sc_queue;
588	struct compress_backend			*sc_compress;
589	struct scheduler_backend		*sc_scheduler;
590	struct stat_backend			*sc_stat;
591
592	time_t					 sc_uptime;
593
594	TAILQ_HEAD(filterlist, filter)		*sc_filters;
595
596	TAILQ_HEAD(listenerlist, listener)	*sc_listeners;
597	TAILQ_HEAD(maplist, map)		*sc_maps, *sc_maps_reload;
598	TAILQ_HEAD(rulelist, rule)		*sc_rules, *sc_rules_reload;
599	SPLAY_HEAD(sessiontree, session)	 sc_sessions;
600	SPLAY_HEAD(ssltree, ssl)		*sc_ssl;
601	SPLAY_HEAD(childtree, child)		 children;
602	SPLAY_HEAD(lkatree, lka_session)	 lka_sessions;
603	SPLAY_HEAD(mfatree, mfa_session)	 mfa_sessions;
604	LIST_HEAD(mdalist, mda_session)		 mda_sessions;
605
606	uint64_t				 filtermask;
607};
608
609#define	TRACE_VERBOSE	0x0001
610#define	TRACE_IMSG	0x0002
611#define	TRACE_IO	0x0004
612#define	TRACE_SMTP	0x0008
613#define	TRACE_MTA	0x0010
614#define	TRACE_BOUNCE	0x0020
615#define	TRACE_SCHEDULER	0x0040
616#define	TRACE_STAT	0x0080
617#define	TRACE_PROFILING	0x0100
618
619
620struct submit_status {
621	uint64_t			 id;
622	int				 code;
623	union submit_path {
624		struct mailaddr		 maddr;
625		uint32_t		 msgid;
626		uint64_t		 evpid;
627		char			 errormsg[MAX_LINE_SIZE + 1];
628		char			 dataline[MAX_LINE_SIZE + 1];
629	}				 u;
630	enum delivery_flags		 flags;
631	struct sockaddr_storage		 ss;
632	struct envelope			 envelope;
633};
634
635struct forward_req {
636	uint64_t			 id;
637	uint8_t				 status;
638	char				 as_user[MAXLOGNAME];
639};
640
641enum dns_status {
642	DNS_OK = 0,
643	DNS_RETRY,
644	DNS_EINVAL,
645	DNS_ENONAME,
646	DNS_ENOTFOUND,
647};
648
649struct dns {
650	uint64_t		 id;
651	char			 host[MAXHOSTNAMELEN];
652	char			 backup[MAXHOSTNAMELEN];
653	int			 port;
654	int			 error;
655	int			 type;
656	struct imsgev		*asker;
657	struct sockaddr_storage	 ss;
658};
659
660struct secret {
661	uint64_t		 id;
662	char			 mapname[MAX_PATH_SIZE];
663	char			 host[MAXHOSTNAMELEN];
664	char			 secret[MAX_LINE_SIZE];
665};
666
667struct deliver {
668	char			to[PATH_MAX];
669	char			from[PATH_MAX];
670	char			user[MAXLOGNAME];
671	short			mode;
672};
673
674struct rulematch {
675	uint64_t		 id;
676	struct submit_status	 ss;
677};
678
679struct filter {
680	TAILQ_ENTRY(filter)     f_entry;
681	pid_t			pid;
682	struct event		ev;
683	struct imsgbuf		*ibuf;
684	char			name[MAX_FILTER_NAME];
685	char			path[MAXPATHLEN];
686};
687
688struct mfa_session {
689	SPLAY_ENTRY(mfa_session)	 nodes;
690	uint64_t			 id;
691
692	enum session_state		 state;
693	struct submit_status		 ss;
694	struct filter			*filter;
695	struct filter_msg		 fm;
696};
697
698struct mta_session;
699
700struct mta_route {
701	SPLAY_ENTRY(mta_route)	 entry;
702	uint64_t		 id;
703
704	uint8_t			 flags;
705	char			*hostname;
706	char			*backupname;
707	uint16_t		 port;
708	char			*cert;
709	char			*auth;
710	void			*ssl;
711
712	/* route limits	*/
713	int			 maxconn; 	/* in parallel */
714	int			 maxmail;	/* per session */
715	int			 maxrcpt;	/* per mail */
716
717	int			 refcount;
718
719	int			 ntask;
720	TAILQ_HEAD(, mta_task)	 tasks;
721
722	int			 nsession;
723
724	int			 nfail;
725	char			 errorline[64];
726};
727
728struct mta_task {
729	TAILQ_ENTRY(mta_task)	 entry;
730	struct mta_route	*route;
731	uint32_t		 msgid;
732	TAILQ_HEAD(, envelope)	 envelopes;
733	struct mailaddr		 sender;
734	struct mta_session	*session;
735};
736
737/* maps return structures */
738struct map_credentials {
739	char username[MAX_LINE_SIZE];
740	char password[MAX_LINE_SIZE];
741};
742
743struct map_alias {
744	size_t			nbnodes;
745	struct expand		expand;
746};
747
748struct map_virtual {
749	size_t			nbnodes;
750	struct expand		expand;
751};
752
753struct map_netaddr {
754	struct netaddr		netaddr;
755};
756
757enum queue_op {
758	QOP_INVALID=0,
759	QOP_CREATE,
760	QOP_DELETE,
761	QOP_UPDATE,
762	QOP_COMMIT,
763	QOP_LOAD,
764	QOP_FD_R,
765	QOP_CORRUPT,
766};
767
768struct queue_backend {
769	int (*init)(int);
770	int (*message)(enum queue_op, uint32_t *);
771	int (*envelope)(enum queue_op, uint64_t *, char *, size_t);
772
773	void *(*qwalk_new)(uint32_t);
774	int   (*qwalk)(void *, uint64_t *);
775	void  (*qwalk_close)(void *);
776};
777
778struct compress_backend {
779	int	(*compress_file)(FILE *, FILE *);
780	int	(*uncompress_file)(FILE *, FILE *);
781	size_t	(*compress_buffer)(char *, size_t, char *, size_t);
782	size_t	(*uncompress_buffer)(char *, size_t, char *, size_t);
783};
784
785/* auth structures */
786enum auth_type {
787	AUTH_BSD,
788	AUTH_PWD,
789};
790
791struct auth_backend {
792	int (*authenticate)(char *, char *);
793};
794
795
796/* user structures */
797enum user_type {
798	USER_PWD,
799};
800
801#define	MAXPASSWORDLEN	128
802struct mta_user {
803	char username[MAXLOGNAME];
804	char directory[MAXPATHLEN];
805	char password[MAXPASSWORDLEN];
806	uid_t uid;
807	gid_t gid;
808};
809
810struct user_backend {
811	int (*getbyname)(struct mta_user *, const char *);
812};
813
814
815/* delivery_backend */
816struct delivery_backend {
817	int			allow_root;
818	void (*open)(struct deliver *);
819};
820
821struct scheduler_info {
822	uint64_t		evpid;
823	enum delivery_type	type;
824	time_t			creation;
825	time_t			lasttry;
826	time_t			expire;
827	uint16_t		retry;
828};
829
830struct id_list {
831	struct id_list	*next;
832	uint64_t	 id;
833};
834
835#define SCHED_NONE		0x00
836#define SCHED_DELAY		0x01
837#define SCHED_REMOVE		0x02
838#define SCHED_EXPIRE		0x04
839#define SCHED_BOUNCE		0x08
840#define SCHED_MDA		0x10
841#define SCHED_MTA		0x20
842
843struct scheduler_batch {
844	int		 type;
845	time_t		 delay;
846	struct id_list	*evpids;
847};
848
849struct scheduler_backend {
850	void	(*init)(void);
851
852	void	(*insert)(struct scheduler_info *);
853	void	(*commit)(uint32_t);
854	void	(*rollback)(uint32_t);
855
856	void	(*update)(struct scheduler_info *);
857	void	(*delete)(uint64_t);
858
859	void	(*batch)(int, struct scheduler_batch *);
860
861	void	(*schedule)(uint64_t);
862	void	(*remove)(uint64_t);
863};
864
865
866enum stat_type {
867	STAT_COUNTER,
868	STAT_TIMESTAMP,
869	STAT_TIMEVAL,
870	STAT_TIMESPEC,
871};
872
873struct stat_value {
874	enum stat_type	type;
875	union stat_v {
876		size_t		counter;
877		time_t		timestamp;
878		struct timeval	tv;
879		struct timespec	ts;
880	} u;
881};
882
883#define	STAT_KEY_SIZE	1024
884struct stat_kv {
885	void	*iter;
886	char	key[STAT_KEY_SIZE];
887	struct stat_value	val;
888};
889
890struct stat_backend {
891	void	(*init)(void);
892	void	(*close)(void);
893	void	(*increment)(const char *, size_t);
894	void	(*decrement)(const char *, size_t);
895	void	(*set)(const char *, const struct stat_value *);
896	int	(*iter)(void **, char **, struct stat_value *);
897};
898
899
900extern struct smtpd	*env;
901extern void (*imsg_callback)(struct imsgev *, struct imsg *);
902
903
904/* aliases.c */
905int aliases_get(objid_t, struct expand *, const char *);
906int aliases_virtual_get(objid_t, struct expand *, const struct mailaddr *);
907int aliases_vdomain_exists(objid_t, const char *);
908int alias_parse(struct expandnode *, char *);
909
910
911/* auth.c */
912struct auth_backend *auth_backend_lookup(enum auth_type);
913
914
915/* bounce.c */
916void bounce_add(uint64_t);
917void bounce_run(uint64_t, int);
918
919
920/* config.c */
921#define PURGE_LISTENERS		0x01
922#define PURGE_MAPS		0x02
923#define PURGE_RULES		0x04
924#define PURGE_SSL		0x08
925#define PURGE_EVERYTHING	0xff
926void purge_config(uint8_t);
927void unconfigure(void);
928void configure(void);
929void init_pipes(void);
930void config_pipes(struct peer *, uint);
931void config_peers(struct peer *, uint);
932
933
934/* control.c */
935pid_t control(void);
936
937
938/* delivery.c */
939struct delivery_backend *delivery_backend_lookup(enum action_type);
940
941
942/* dns.c */
943void dns_query_host(char *, int, uint64_t);
944void dns_query_mx(char *, char *, int, uint64_t);
945void dns_query_ptr(struct sockaddr_storage *, uint64_t);
946void dns_async(struct imsgev *, int, struct dns *);
947
948
949/* enqueue.c */
950int		 enqueue(int, char **);
951int		 enqueue_offline(int, char **);
952
953
954/* envelope.c */
955void envelope_set_errormsg(struct envelope *, char *, ...);
956char *envelope_ascii_field_name(enum envelope_field);
957int envelope_ascii_load(enum envelope_field, struct envelope *, char *);
958int envelope_ascii_dump(enum envelope_field, struct envelope *, char *, size_t);
959int envelope_load_buffer(struct envelope *, char *, size_t);
960int envelope_dump_buffer(struct envelope *, char *, size_t);
961
962/* expand.c */
963int expand_cmp(struct expandnode *, struct expandnode *);
964void expand_insert(struct expand *, struct expandnode *);
965struct expandnode *expand_lookup(struct expand *, struct expandnode *);
966void expand_free(struct expand *);
967RB_PROTOTYPE(expandtree, expandnode, nodes, expand_cmp);
968
969/* forward.c */
970int forwards_get(int, struct expand *);
971
972
973/* lka.c */
974pid_t lka(void);
975
976/* lka_session.c */
977void lka_session(struct submit_status *);
978void lka_session_forward_reply(struct forward_req *, int);
979
980/* map.c */
981void *map_open(struct map *);
982void  map_update(struct map *);
983void  map_close(struct map *, void *);
984
985void *map_lookup(objid_t, const char *, enum map_kind);
986int map_compare(objid_t, const char *, enum map_kind,
987    int (*)(const char *, const char *));
988struct map *map_find(objid_t);
989struct map *map_findbyname(const char *);
990struct map *map_create(enum map_src, const char *);
991void map_destroy(struct map *);
992void map_add(struct map *, const char *, const char *);
993void map_delete(struct map *, const char *);
994void map_delete_all(struct map *);
995
996
997/* mda.c */
998pid_t mda(void);
999
1000
1001/* mfa.c */
1002pid_t mfa(void);
1003
1004
1005/* mfa_session.c */
1006void mfa_session(struct submit_status *, enum session_state);
1007
1008
1009/* mta.c */
1010pid_t mta(void);
1011int mta_response_delivery(const char *);
1012const char *mta_response_status(const char *);
1013const char *mta_response_text(const char *);
1014void mta_route_ok(struct mta_route *);
1015void mta_route_error(struct mta_route *, const char *);
1016void mta_route_collect(struct mta_route *);
1017const char *mta_route_to_text(struct mta_route *);
1018
1019/* mta_session.c */
1020void mta_session(struct mta_route *);
1021void mta_session_imsg(struct imsgev *, struct imsg *);
1022
1023/* parse.y */
1024int parse_config(struct smtpd *, const char *, int);
1025int cmdline_symset(char *);
1026
1027/* queue.c */
1028pid_t queue(void);
1029
1030/* queue_backend.c */
1031uint32_t queue_generate_msgid(void);
1032uint64_t queue_generate_evpid(uint32_t msgid);
1033struct queue_backend *queue_backend_lookup(const char *);
1034int queue_message_incoming_path(uint32_t, char *, size_t);
1035int queue_envelope_incoming_path(uint64_t, char *, size_t);
1036int queue_message_incoming_delete(uint32_t);
1037int queue_message_create(uint32_t *);
1038int queue_message_delete(uint32_t);
1039int queue_message_commit(uint32_t);
1040int queue_message_fd_r(uint32_t);
1041int queue_message_fd_rw(uint32_t);
1042int queue_message_corrupt(uint32_t);
1043int queue_envelope_create(struct envelope *);
1044int queue_envelope_delete(struct envelope *);
1045int queue_envelope_load(uint64_t, struct envelope *);
1046int queue_envelope_update(struct envelope *);
1047void *qwalk_new(uint32_t);
1048int   qwalk(void *, uint64_t *);
1049void  qwalk_close(void *);
1050
1051/* compress_backend.c */
1052struct compress_backend *compress_backend_lookup(const char *);
1053int compress_file(FILE *, FILE *);
1054int uncompress_file(FILE *, FILE *);
1055size_t compress_buffer(char *, size_t, char *, size_t);
1056size_t uncompress_buffer(char *, size_t, char *, size_t);
1057
1058
1059/* ruleset.c */
1060struct rule *ruleset_match(const struct envelope *);
1061
1062
1063/* scheduler.c */
1064pid_t scheduler(void);
1065
1066/* scheduler_bakend.c */
1067struct scheduler_backend *scheduler_backend_lookup(const char *);
1068void scheduler_info(struct scheduler_info *, struct envelope *);
1069time_t scheduler_compute_schedule(struct scheduler_info *);
1070
1071/* smtp.c */
1072pid_t smtp(void);
1073void smtp_resume(void);
1074void smtp_destroy(struct session *);
1075
1076/* smtp_session.c */
1077void session_init(struct listener *, struct session *);
1078int session_cmp(struct session *, struct session *);
1079void session_io(struct io *, int);
1080void session_pickup(struct session *, struct submit_status *);
1081void session_destroy(struct session *, const char *);
1082void session_respond(struct session *, char *, ...)
1083	__attribute__ ((format (printf, 2, 3)));
1084
1085SPLAY_PROTOTYPE(sessiontree, session, s_nodes, session_cmp);
1086
1087
1088/* smtpd.c */
1089void imsg_event_add(struct imsgev *);
1090void imsg_compose_event(struct imsgev *, uint16_t, uint32_t, pid_t,
1091    int, void *, uint16_t);
1092void imsg_dispatch(int, short, void *);
1093const char * proc_to_str(int);
1094const char * imsg_to_str(int);
1095
1096
1097/* ssl.c */
1098void ssl_init(void);
1099int ssl_load_certfile(const char *, uint8_t);
1100void ssl_setup(struct listener *);
1101void *ssl_smtp_init(void *);
1102void *ssl_mta_init(struct ssl *);
1103int ssl_cmp(struct ssl *, struct ssl *);
1104SPLAY_PROTOTYPE(ssltree, ssl, ssl_nodes, ssl_cmp);
1105
1106
1107/* ssl_privsep.c */
1108int	 ssl_ctx_use_private_key(void *, char *, off_t);
1109int	 ssl_ctx_use_certificate_chain(void *, char *, off_t);
1110
1111
1112/* stat_backend.c */
1113struct stat_backend	*stat_backend_lookup(const char *);
1114void	stat_increment(const char *, size_t);
1115void	stat_decrement(const char *, size_t);
1116void	stat_set(const char *, const struct stat_value *);
1117
1118struct stat_value *stat_counter(size_t);
1119struct stat_value *stat_timestamp(time_t);
1120struct stat_value *stat_timeval(struct timeval *);
1121struct stat_value *stat_timespec(struct timespec *);
1122
1123
1124/* tree.c */
1125SPLAY_HEAD(tree, treeentry);
1126#define tree_init(t) SPLAY_INIT((t))
1127#define tree_empty(t) SPLAY_EMPTY((t))
1128int tree_check(struct tree *, uint64_t);
1129void *tree_set(struct tree *, uint64_t, void *);
1130void tree_xset(struct tree *, uint64_t, void *);
1131void *tree_get(struct tree *, uint64_t);
1132void *tree_xget(struct tree *, uint64_t);
1133void *tree_pop(struct tree *, uint64_t);
1134void *tree_xpop(struct tree *, uint64_t);
1135int tree_poproot(struct tree *, uint64_t *, void **);
1136int tree_root(struct tree *, uint64_t *, void **);
1137int tree_iter(struct tree *, void **, uint64_t *, void **);
1138void tree_merge(struct tree *, struct tree *);
1139
1140
1141/* user.c */
1142struct user_backend *user_backend_lookup(enum user_type);
1143
1144
1145/* util.c */
1146typedef struct arglist arglist;
1147struct arglist {
1148	char	**list;
1149	uint	  num;
1150	uint	  nalloc;
1151};
1152void addargs(arglist *, char *, ...)
1153	__attribute__((format(printf, 2, 3)));
1154int bsnprintf(char *, size_t, const char *, ...)
1155	__attribute__ ((format (printf, 3, 4)));
1156int mkdirs(char *, mode_t);
1157int safe_fclose(FILE *);
1158int hostname_match(const char *, const char *);
1159int email_to_mailaddr(struct mailaddr *, char *);
1160int valid_localpart(const char *);
1161int valid_domainpart(const char *);
1162char *ss_to_text(const struct sockaddr_storage *);
1163char *time_to_text(time_t);
1164char *duration_to_text(time_t);
1165int secure_file(int, char *, char *, uid_t, int);
1166int  lowercase(char *, const char *, size_t);
1167void xlowercase(char *, const char *, size_t);
1168void sa_set_port(struct sockaddr *, int);
1169uint64_t generate_uid(void);
1170void fdlimit(double);
1171int availdesc(void);
1172uint32_t evpid_to_msgid(uint64_t);
1173uint64_t msgid_to_evpid(uint32_t);
1174int ckdir(const char *, mode_t, uid_t, gid_t, int);
1175int rmtree(char *, int);
1176int mvpurge(char *, char *);
1177int mktmpfile(void);
1178const char *parse_smtp_response(char *, size_t, char **, int *);
1179int text_to_netaddr(struct netaddr *, const char *);
1180int text_to_relayhost(struct relayhost *, const char *);
1181void *xmalloc(size_t, const char *);
1182void *xcalloc(size_t, size_t, const char *);
1183char *xstrdup(const char *, const char *);
1184void *xmemdup(const void *, size_t, const char *);
1185void iobuf_xinit(struct iobuf *, size_t, size_t, const char *);
1186void iobuf_xfqueue(struct iobuf *, const char *, const char *, ...);
1187void log_envelope(const struct envelope *, const char *, const char *);
1188void session_socket_blockmode(int, enum blockmodes);
1189void session_socket_no_linger(int);
1190int session_socket_error(int);
1191uint64_t strtoevpid(const char *);
1192
1193/* waitq.c */
1194int  waitq_wait(void *, void (*)(void *, void *, void *), void *);
1195void waitq_run(void *, void *);
1196