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