smtpd.h revision 1.399
1/*	$OpenBSD: smtpd.h,v 1.399 2013/01/28 11:09:53 gilles Exp $	*/
2
3/*
4 * Copyright (c) 2008 Gilles Chehade <gilles@poolp.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 "smtpd-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		 10
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#define	MAX_TABLE_BACKEND_SIZE	 32
40
41/* return and forward path size */
42#define	MAX_FILTER_NAME		 32
43#define MAX_PATH_SIZE		 256
44/*#define MAX_RULEBUFFER_LEN	 512*/
45#define	EXPAND_BUFFER		 1024
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	 "_smtpf"
52#define SMTPD_QUEUE_USER	 "_smtpq"
53#define SMTPD_SOCKET		 "/var/run/smtpd.sock"
54#ifndef SMTPD_NAME
55#define	SMTPD_NAME		 "OpenSMTPD"
56#endif
57#define SMTPD_BANNER		 "220 %s ESMTP %s"
58#define SMTPD_SESSION_TIMEOUT	 300
59#define SMTPD_BACKLOG		 5
60
61#define	PATH_SMTPCTL		"/usr/sbin/smtpctl"
62
63#define PATH_SPOOL		"/var/spool/smtpd"
64#define PATH_OFFLINE		"/offline"
65#define PATH_PURGE		"/purge"
66#define PATH_TEMPORARY		"/temporary"
67#define PATH_INCOMING		"/incoming"
68#define PATH_MESSAGE		"/message"
69
70#define	PATH_FILTERS		"/usr/libexec/smtpd"
71
72
73/* number of MX records to lookup */
74#define MAX_MX_COUNT		10
75
76/* max response delay under flood conditions */
77#define MAX_RESPONSE_DELAY	60
78
79/* how many responses per state are undelayed */
80#define FAST_RESPONSES		2
81
82/* max len of any smtp line */
83#define	SMTP_LINE_MAX		MAX_LINE_SIZE
84
85#define F_STARTTLS		0x01
86#define F_SMTPS			0x02
87#define	F_TLS_OPTIONAL		0x04
88#define F_SSL		       (F_STARTTLS | F_SMTPS)
89#define F_AUTH			0x08
90#define	F_BACKUP		0x10	/* XXX - MUST BE SYNC-ED WITH RELAY_BACKUP */
91#define	F_STARTTLS_REQUIRE	0x20
92#define	F_AUTH_REQUIRE		0x40
93
94#define F_SCERT			0x01
95#define F_CCERT			0x02
96
97/* must match F_* for mta */
98#define RELAY_STARTTLS		0x01
99#define RELAY_SMTPS		0x02
100#define	RELAY_TLS_OPTIONAL     	0x04
101#define RELAY_SSL		(RELAY_STARTTLS | RELAY_SMTPS)
102#define RELAY_AUTH		0x08
103#define RELAY_BACKUP		0x10	/* XXX - MUST BE SYNC-ED WITH F_BACKUP */
104#define RELAY_MX		0x20
105
106typedef uint32_t	objid_t;
107
108struct userinfo {
109	char username[MAXLOGNAME];
110	char directory[MAXPATHLEN];
111	uid_t uid;
112	gid_t gid;
113};
114
115struct netaddr {
116	struct sockaddr_storage ss;
117	int bits;
118};
119
120union sockaddr_any {
121	struct in6_addr		in6;
122	struct in_addr		in4;
123};
124
125struct relayhost {
126	uint8_t flags;
127	char hostname[MAXHOSTNAMELEN];
128	uint16_t port;
129	char cert[PATH_MAX];
130	char authtable[MAX_PATH_SIZE];
131	char authlabel[MAX_PATH_SIZE];
132	char sourcetable[MAX_PATH_SIZE];
133	char helotable[MAX_PATH_SIZE];
134};
135
136struct credentials {
137	char username[MAX_LINE_SIZE];
138	char password[MAX_LINE_SIZE];
139};
140
141struct destination {
142	char	name[MAXHOSTNAMELEN];
143};
144
145struct source {
146	union sockaddr_any	addr;
147};
148
149struct addrname {
150	union sockaddr_any	addr;
151	char			name[MAXHOSTNAMELEN];
152};
153
154enum imsg_type {
155	IMSG_NONE,
156	IMSG_CTL_OK,		/* answer to smtpctl requests */
157	IMSG_CTL_FAIL,
158	IMSG_CTL_SHUTDOWN,
159	IMSG_CTL_VERBOSE,
160	IMSG_CTL_PAUSE_MDA,
161	IMSG_CTL_PAUSE_MTA,
162	IMSG_CTL_PAUSE_SMTP,
163	IMSG_CTL_RESUME_MDA,
164	IMSG_CTL_RESUME_MTA,
165	IMSG_CTL_RESUME_SMTP,
166	IMSG_CTL_LIST_MESSAGES,
167	IMSG_CTL_LIST_ENVELOPES,
168	IMSG_CTL_REMOVE,
169	IMSG_CTL_SCHEDULE,
170
171	IMSG_CTL_TRACE,
172	IMSG_CTL_UNTRACE,
173	IMSG_CTL_PROFILE,
174	IMSG_CTL_UNPROFILE,
175
176	IMSG_CONF_START,
177	IMSG_CONF_SSL,
178	IMSG_CONF_LISTENER,
179	IMSG_CONF_TABLE,
180	IMSG_CONF_TABLE_CONTENT,
181	IMSG_CONF_RULE,
182	IMSG_CONF_RULE_SOURCE,
183	IMSG_CONF_RULE_SENDER,
184	IMSG_CONF_RULE_DESTINATION,
185	IMSG_CONF_RULE_MAPPING,
186	IMSG_CONF_RULE_USERS,
187	IMSG_CONF_FILTER,
188	IMSG_CONF_END,
189
190	IMSG_LKA_UPDATE_TABLE,
191	IMSG_LKA_EXPAND_RCPT,
192	IMSG_LKA_SECRET,
193	IMSG_LKA_SOURCE,
194	IMSG_LKA_HELO,
195	IMSG_LKA_USERINFO,
196	IMSG_LKA_AUTHENTICATE,
197	IMSG_LKA_SSL_INIT,
198	IMSG_LKA_SSL_VERIFY_CERT,
199	IMSG_LKA_SSL_VERIFY_CHAIN,
200	IMSG_LKA_SSL_VERIFY,
201
202	IMSG_DELIVERY_OK,
203	IMSG_DELIVERY_TEMPFAIL,
204	IMSG_DELIVERY_PERMFAIL,
205	IMSG_DELIVERY_LOOP,
206
207	IMSG_BOUNCE_INJECT,
208
209	IMSG_MDA_DELIVER,
210	IMSG_MDA_DONE,
211
212	IMSG_MFA_REQ_CONNECT,
213	IMSG_MFA_REQ_HELO,
214	IMSG_MFA_REQ_MAIL,
215	IMSG_MFA_REQ_RCPT,
216	IMSG_MFA_REQ_DATA,
217	IMSG_MFA_REQ_EOM,
218	IMSG_MFA_EVENT_RSET,
219	IMSG_MFA_EVENT_COMMIT,
220	IMSG_MFA_EVENT_ROLLBACK,
221	IMSG_MFA_EVENT_DISCONNECT,
222	IMSG_MFA_SMTP_DATA,
223	IMSG_MFA_SMTP_RESPONSE,
224
225	IMSG_MTA_BATCH,
226	IMSG_MTA_BATCH_ADD,
227	IMSG_MTA_BATCH_END,
228
229	IMSG_QUEUE_CREATE_MESSAGE,
230	IMSG_QUEUE_SUBMIT_ENVELOPE,
231	IMSG_QUEUE_COMMIT_ENVELOPES,
232	IMSG_QUEUE_REMOVE_MESSAGE,
233	IMSG_QUEUE_COMMIT_MESSAGE,
234	IMSG_QUEUE_MESSAGE_FD,
235	IMSG_QUEUE_MESSAGE_FILE,
236	IMSG_QUEUE_REMOVE,
237	IMSG_QUEUE_EXPIRE,
238	IMSG_QUEUE_BOUNCE,
239
240	IMSG_PARENT_FORWARD_OPEN,
241	IMSG_PARENT_FORK_MDA,
242	IMSG_PARENT_KILL_MDA,
243	IMSG_PARENT_SEND_CONFIG,
244
245	IMSG_SMTP_ENQUEUE_FD,
246
247	IMSG_DNS_HOST,
248	IMSG_DNS_HOST_END,
249	IMSG_DNS_PTR,
250	IMSG_DNS_MX,
251	IMSG_DNS_MX_PREFERENCE,
252
253	IMSG_STAT_INCREMENT,
254	IMSG_STAT_DECREMENT,
255	IMSG_STAT_SET,
256
257	IMSG_DIGEST,
258	IMSG_STATS,
259	IMSG_STATS_GET,
260};
261
262enum blockmodes {
263	BM_NORMAL,
264	BM_NONBLOCK
265};
266
267enum smtp_proc_type {
268	PROC_PARENT = 0,
269	PROC_SMTP,
270	PROC_MFA,
271	PROC_LKA,
272	PROC_QUEUE,
273	PROC_MDA,
274	PROC_MTA,
275	PROC_CONTROL,
276	PROC_SCHEDULER,
277};
278
279enum table_type {
280	T_NONE		= 0,
281	T_DYNAMIC	= 0x01,	/* table with external source	*/
282	T_LIST		= 0x02,	/* table holding a list		*/
283	T_HASH		= 0x04,	/* table holding a hash table	*/
284};
285
286enum table_service {
287	K_NONE		= 0x00,
288	K_ALIAS		= 0x01,	/* returns struct expand	*/
289	K_DOMAIN	= 0x02,	/* returns struct destination	*/
290	K_CREDENTIALS	= 0x04,	/* returns struct credentials	*/
291	K_NETADDR	= 0x08,	/* returns struct netaddr	*/
292	K_USERINFO	= 0x10,	/* returns struct userinfo	*/
293	K_SOURCE	= 0x20, /* returns struct source	*/
294	K_MAILADDR	= 0x40, /* returns struct mailaddr	*/
295	K_ADDRNAME	= 0x80, /* returns struct addrname	*/
296};
297
298struct table {
299	char				 t_name[MAX_LINE_SIZE];
300	objid_t				 t_id;
301	enum table_type			 t_type;
302	char				 t_src[MAX_TABLE_BACKEND_SIZE];
303	char				 t_config[MAXPATHLEN];
304
305	struct dict			 t_dict;
306
307	void				*t_handle;
308	struct table_backend		*t_backend;
309	void				*t_payload;
310	void				*t_iter;
311	char				 t_cfgtable[MAXPATHLEN];
312};
313
314struct table_backend {
315	const unsigned int	services;
316	int	(*config)(struct table *, const char *);
317	void	*(*open)(struct table *);
318	int	(*update)(struct table *);
319	void	(*close)(void *);
320	int	(*lookup)(void *, const char *, enum table_service, void **);
321	int	(*fetch)(void *, enum table_service, char **);
322};
323
324
325enum dest_type {
326	DEST_DOM,
327	DEST_VDOM
328};
329
330enum action_type {
331	A_RELAY,
332	A_RELAYVIA,
333	A_MAILDIR,
334	A_MBOX,
335	A_FILENAME,
336	A_MDA
337};
338
339enum decision {
340	R_REJECT,
341	R_ACCEPT
342};
343
344struct rule {
345	TAILQ_ENTRY(rule)		r_entry;
346	enum decision			r_decision;
347	char				r_tag[MAX_TAG_SIZE];
348	struct table		       *r_sources;
349	struct table		       *r_senders;
350
351	enum dest_type			r_desttype;
352	struct table		       *r_destination;
353
354	enum action_type		r_action;
355	union rule_dest {
356		char			buffer[EXPAND_BUFFER];
357		struct relayhost	relayhost;
358	}				r_value;
359
360	struct mailaddr		       *r_as;
361	struct table		       *r_mapping;
362	struct table		       *r_users;
363	time_t				r_qexpire;
364};
365
366enum delivery_type {
367	D_MDA,
368	D_MTA,
369	D_BOUNCE,
370};
371
372struct delivery_mda {
373	enum action_type	method;
374	char			usertable[MAX_PATH_SIZE];
375	char			username[MAXLOGNAME];
376	char			buffer[EXPAND_BUFFER];
377};
378
379struct delivery_mta {
380	struct relayhost	relay;
381};
382
383enum bounce_type {
384	B_ERROR,
385	B_WARNING,
386};
387
388struct delivery_bounce {
389	enum bounce_type	type;
390	time_t			delay;
391	time_t			expire;
392};
393
394enum expand_type {
395	EXPAND_INVALID,
396	EXPAND_USERNAME,
397	EXPAND_FILENAME,
398	EXPAND_FILTER,
399	EXPAND_INCLUDE,
400	EXPAND_ADDRESS
401};
402
403struct expandnode {
404	RB_ENTRY(expandnode)	entry;
405	TAILQ_ENTRY(expandnode)	tq_entry;
406	enum expand_type	type;
407	int			sameuser;
408	int			alias;
409	struct rule	       *rule;
410	struct expandnode      *parent;
411	unsigned int		depth;
412	union {
413		/*
414		 * user field handles both expansion user and system user
415		 * so we MUST make it large enough to fit a mailaddr user
416		 */
417		char		user[MAX_LOCALPART_SIZE];
418		char		buffer[EXPAND_BUFFER];
419		struct mailaddr	mailaddr;
420	}			u;
421};
422
423struct expand {
424	RB_HEAD(expandtree, expandnode)	 tree;
425	TAILQ_HEAD(xnodes, expandnode)	*queue;
426	int				 alias;
427	size_t				 nb_nodes;
428	struct rule			*rule;
429	struct expandnode		*parent;
430};
431
432enum envelope_flags {
433	EF_AUTHENTICATED	= 0x01,
434	EF_BOUNCE		= 0x02,
435	EF_INTERNAL		= 0x04, /* Internal expansion forward */
436
437	/* runstate, not saved on disk */
438
439	EF_PENDING		= 0x10,
440	EF_INFLIGHT		= 0x20,
441};
442
443#define	SMTPD_ENVELOPE_VERSION		1
444struct envelope {
445	TAILQ_ENTRY(envelope)		entry;
446
447	char				tag[MAX_TAG_SIZE];
448
449	uint32_t			version;
450	uint64_t			id;
451	enum envelope_flags		flags;
452
453	char				helo[MAXHOSTNAMELEN];
454	char				hostname[MAXHOSTNAMELEN];
455	char				errorline[MAX_LINE_SIZE + 1];
456	struct sockaddr_storage		ss;
457
458	struct mailaddr			sender;
459	struct mailaddr			rcpt;
460	struct mailaddr			dest;
461
462	enum delivery_type		type;
463	union {
464		struct delivery_mda	mda;
465		struct delivery_mta	mta;
466		struct delivery_bounce	bounce;
467	}				agent;
468
469	uint16_t			retry;
470	time_t				creation;
471	time_t				expire;
472	time_t				lasttry;
473	time_t				nexttry;
474	time_t				lastbounce;
475};
476
477enum envelope_field {
478	EVP_VERSION,
479	EVP_TAG,
480	EVP_MSGID,
481	EVP_TYPE,
482	EVP_HELO,
483	EVP_HOSTNAME,
484	EVP_ERRORLINE,
485	EVP_SOCKADDR,
486	EVP_SENDER,
487	EVP_RCPT,
488	EVP_DEST,
489	EVP_CTIME,
490	EVP_EXPIRE,
491	EVP_RETRY,
492	EVP_LASTTRY,
493	EVP_LASTBOUNCE,
494	EVP_FLAGS,
495	EVP_MDA_METHOD,
496	EVP_MDA_BUFFER,
497	EVP_MDA_USER,
498	EVP_MDA_USERTABLE,
499	EVP_MTA_RELAY,
500	EVP_MTA_RELAY_AUTH,
501	EVP_MTA_RELAY_CERT,
502	EVP_MTA_RELAY_SOURCE,
503	EVP_MTA_RELAY_HELO,
504	EVP_BOUNCE_TYPE,
505	EVP_BOUNCE_DELAY,
506	EVP_BOUNCE_EXPIRE,
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	char			 authtable[MAX_LINE_SIZE];
521	char			 helo[MAXHOSTNAMELEN];
522	TAILQ_ENTRY(listener)	 entry;
523};
524
525struct smtpd {
526	char				sc_conffile[MAXPATHLEN];
527	size_t				sc_maxsize;
528
529	pid_t				sc_pid;
530
531#define SMTPD_OPT_VERBOSE		0x00000001
532#define SMTPD_OPT_NOACTION		0x00000002
533	uint32_t			sc_opts;
534#define SMTPD_CONFIGURING		0x00000001
535#define SMTPD_EXITING			0x00000002
536#define SMTPD_MDA_PAUSED		0x00000004
537#define SMTPD_MTA_PAUSED		0x00000008
538#define SMTPD_SMTP_PAUSED		0x00000010
539#define SMTPD_MDA_BUSY			0x00000020
540#define SMTPD_MTA_BUSY			0x00000040
541#define SMTPD_BOUNCE_BUSY		0x00000080
542#define SMTPD_SMTP_DISABLED		0x00000100
543	uint32_t			sc_flags;
544	uint32_t			sc_queue_flags;
545#define QUEUE_COMPRESS			0x00000001
546	char			       *sc_queue_compress_algo;
547	int				sc_qexpire;
548#define MAX_BOUNCE_WARN			4
549	time_t				sc_bounce_warn[MAX_BOUNCE_WARN];
550	struct event			sc_ev;
551	struct passwd		       *sc_pw;
552	struct passwd		       *sc_pwqueue;
553	char				sc_hostname[MAXHOSTNAMELEN];
554	struct scheduler_backend       *sc_scheduler;
555	struct stat_backend	       *sc_stat;
556
557	time_t					 sc_uptime;
558
559	TAILQ_HEAD(listenerlist, listener)	*sc_listeners;
560
561	TAILQ_HEAD(rulelist, rule)		*sc_rules, *sc_rules_reload;
562
563	struct dict			       *sc_ssl_dict;
564
565	struct dict			       *sc_tables_dict;		/* keyed lookup	*/
566	struct tree			       *sc_tables_tree;		/* id lookup	*/
567
568	struct dict				sc_filters;
569	uint32_t				filtermask;
570};
571
572#define	TRACE_VERBOSE	0x0001
573#define	TRACE_IMSG	0x0002
574#define	TRACE_IO	0x0004
575#define	TRACE_SMTP	0x0008
576#define	TRACE_MFA	0x0010
577#define	TRACE_MTA	0x0020
578#define	TRACE_BOUNCE	0x0040
579#define	TRACE_SCHEDULER	0x0080
580#define	TRACE_LOOKUP	0x0100
581#define	TRACE_STAT	0x0200
582#define	TRACE_RULES	0x0400
583#define	TRACE_IMSGSIZE	0x0800
584
585#define PROFILE_TOSTAT	0x0001
586#define PROFILE_IMSG	0x0002
587#define PROFILE_QUEUE	0x0004
588
589struct forward_req {
590	uint64_t			id;
591	uint8_t				status;
592
593	char				user[MAXLOGNAME];
594	uid_t				uid;
595	gid_t				gid;
596	char				directory[MAXPATHLEN];
597};
598
599struct deliver {
600	char			to[PATH_MAX];
601	char			from[PATH_MAX];
602	char			user[MAXLOGNAME];
603	short			mode;
604
605	struct userinfo		userinfo;
606};
607
608struct filter {
609	struct imsgproc	       *process;
610	char			name[MAX_FILTER_NAME];
611	char			path[MAXPATHLEN];
612};
613
614struct mta_host {
615	SPLAY_ENTRY(mta_host)	 entry;
616	struct sockaddr		*sa;
617	char			*ptrname;
618	int			 refcount;
619	size_t			 nconn;
620	time_t			 lastconn;
621	time_t			 lastptrquery;
622
623#define HOST_IGNORE	0x01
624	int			 flags;
625	int			 nerror;
626};
627
628struct mta_mx {
629	TAILQ_ENTRY(mta_mx)	 entry;
630	struct mta_host		*host;
631	int			 preference;
632};
633
634struct mta_domain {
635	SPLAY_ENTRY(mta_domain)	 entry;
636	char			*name;
637	int			 flags;
638	TAILQ_HEAD(, mta_mx)	 mxs;
639	int			 mxstatus;
640	int			 refcount;
641	size_t			 nconn;
642	time_t			 lastconn;
643	time_t			 lastmxquery;
644};
645
646struct mta_source {
647	SPLAY_ENTRY(mta_source)	 entry;
648	struct sockaddr		*sa;
649	int			 refcount;
650	size_t			 nconn;
651	time_t			 lastconn;
652};
653
654struct mta_connector {
655	TAILQ_ENTRY(mta_connector)	 lst_entry;
656	struct mta_source		*source;
657	struct mta_relay		*relay;
658	struct mta_connectors		*queue;
659
660#define CONNECTOR_FAMILY_ERROR	0x01
661#define CONNECTOR_SOURCE_ERROR	0x02
662#define CONNECTOR_MX_ERROR	0x04
663#define CONNECTOR_ERROR		0x0f
664
665#define CONNECTOR_LIMIT_HOST	0x10
666#define CONNECTOR_LIMIT_ROUTE	0x20
667#define CONNECTOR_LIMIT_SOURCE	0x40
668#define CONNECTOR_LIMIT		0xf0
669	int				 flags;
670
671	int				 refcount;
672	size_t				 nconn;
673	time_t				 lastconn;
674	time_t				 nextconn;
675	time_t				 clearlimit;
676};
677
678struct mta_route {
679	SPLAY_ENTRY(mta_route)	 entry;
680	struct mta_source	*src;
681	struct mta_host		*dst;
682	int			 refcount;
683	size_t			 nconn;
684	time_t			 lastconn;
685};
686
687TAILQ_HEAD(mta_connectors, mta_connector);
688
689struct mta_relay {
690	SPLAY_ENTRY(mta_relay)	 entry;
691	uint64_t		 id;
692
693	struct mta_domain	*domain;
694	int			 flags;
695	char			*backupname;
696	int			 backuppref;
697	char			*sourcetable;
698	uint16_t		 port;
699	char			*cert;
700	char			*authtable;
701	char			*authlabel;
702	char			*helotable;
703	char			*heloname;
704
705	char			*secret;
706
707	size_t			 ntask;
708	TAILQ_HEAD(, mta_task)	 tasks;
709
710	struct tree		 connectors;
711	size_t			 nconnector;
712	size_t			 sourceloop;
713
714	struct mta_connectors	 c_ready;
715	struct mta_connectors	 c_limit;
716	struct mta_connectors	 c_delay;
717	struct mta_connectors	 c_error;
718	struct event		 ev;
719
720	int			 fail;
721	char			*failstr;
722
723#define RELAY_WAIT_MX		0x01
724#define RELAY_WAIT_PREFERENCE	0x02
725#define RELAY_WAIT_SECRET	0x04
726#define RELAY_WAIT_SOURCE	0x08
727#define RELAY_WAIT_HELO		0x10
728#define RELAY_WAITMASK		0x1f
729	int			 status;
730
731	int			 refcount;
732	size_t			 nconn;
733	time_t			 lastconn;
734
735	size_t			 maxconn;
736};
737
738struct mta_task {
739	TAILQ_ENTRY(mta_task)	 entry;
740	struct mta_relay	*relay;
741	uint32_t		 msgid;
742	TAILQ_HEAD(, envelope)	 envelopes;
743	struct mailaddr		 sender;
744};
745
746enum queue_op {
747	QOP_CREATE,
748	QOP_DELETE,
749	QOP_UPDATE,
750	QOP_WALK,
751	QOP_COMMIT,
752	QOP_LOAD,
753	QOP_FD_RW,
754	QOP_FD_R,
755	QOP_CORRUPT,
756};
757
758struct queue_backend {
759	int	(*init)(int);
760	int	(*message)(enum queue_op, uint32_t *);
761	int	(*envelope)(enum queue_op, uint64_t *, char *, size_t);
762};
763
764struct compress_backend {
765	void *	(*compress_new)(void);
766	size_t	(*compress_chunk)(void *, void *, size_t, void *, size_t);
767	size_t	(*compress_finalize)(void *, void *, size_t);
768	void *	(*uncompress_new)(void);
769	size_t	(*uncompress_chunk)(void *, void *, size_t, void *, size_t);
770	size_t	(*uncompress_finalize)(void *, void *, size_t);
771};
772
773/* auth structures */
774enum auth_type {
775	AUTH_BSD,
776	AUTH_PWD,
777};
778
779struct auth_backend {
780	int	(*authenticate)(char *, char *);
781};
782
783
784/* delivery_backend */
785struct delivery_backend {
786	int	allow_root;
787	void	(*open)(struct deliver *);
788};
789
790struct evpstate {
791	uint64_t		evpid;
792	uint16_t		flags;
793	uint16_t		retry;
794	time_t			time;
795};
796
797struct scheduler_info {
798	uint64_t		evpid;
799	enum delivery_type	type;
800	uint16_t		retry;
801	time_t			creation;
802	time_t			expire;
803	time_t			lasttry;
804	time_t			lastbounce;
805	time_t			nexttry;
806};
807
808struct id_list {
809	struct id_list	*next;
810	uint64_t	 id;
811};
812
813#define SCHED_NONE		0x00
814#define SCHED_DELAY		0x01
815#define SCHED_REMOVE		0x02
816#define SCHED_EXPIRE		0x04
817#define SCHED_BOUNCE		0x08
818#define SCHED_MDA		0x10
819#define SCHED_MTA		0x20
820
821struct scheduler_batch {
822	int		 type;
823	time_t		 delay;
824	size_t		 evpcount;
825	struct id_list	*evpids;
826};
827
828struct scheduler_backend {
829	void	(*init)(void);
830
831	void	(*insert)(struct scheduler_info *);
832	size_t	(*commit)(uint32_t);
833	size_t	(*rollback)(uint32_t);
834
835	void	(*update)(struct scheduler_info *);
836	void	(*delete)(uint64_t);
837
838	void	(*batch)(int, struct scheduler_batch *);
839
840	size_t	(*messages)(uint32_t, uint32_t *, size_t);
841	size_t	(*envelopes)(uint64_t, struct evpstate *, size_t);
842	void	(*schedule)(uint64_t);
843	void	(*remove)(uint64_t);
844};
845
846
847enum stat_type {
848	STAT_COUNTER,
849	STAT_TIMESTAMP,
850	STAT_TIMEVAL,
851	STAT_TIMESPEC,
852};
853
854struct stat_value {
855	enum stat_type	type;
856	union stat_v {
857		size_t		counter;
858		time_t		timestamp;
859		struct timeval	tv;
860		struct timespec	ts;
861	} u;
862};
863
864#define	STAT_KEY_SIZE	1024
865struct stat_kv {
866	void	*iter;
867	char	key[STAT_KEY_SIZE];
868	struct stat_value	val;
869};
870
871struct stat_backend {
872	void	(*init)(void);
873	void	(*close)(void);
874	void	(*increment)(const char *, size_t);
875	void	(*decrement)(const char *, size_t);
876	void	(*set)(const char *, const struct stat_value *);
877	int	(*iter)(void **, char **, struct stat_value *);
878};
879
880struct stat_digest {
881	time_t			 startup;
882	time_t			 timestamp;
883
884	size_t			 clt_connect;
885	size_t			 clt_disconnect;
886
887	size_t			 evp_enqueued;
888	size_t			 evp_dequeued;
889
890	size_t			 evp_expired;
891	size_t			 evp_removed;
892	size_t			 evp_bounce;
893
894	size_t			 dlv_ok;
895	size_t			 dlv_permfail;
896	size_t			 dlv_tempfail;
897	size_t			 dlv_loop;
898};
899
900#if 1
901#define MSZ_EVP	(32 + sizeof(struct envelope))
902#else
903#define MSZ_EVP	384
904#endif
905
906struct mproc {
907	pid_t		 pid;
908	char		*name;
909	int		 proc;
910	void		(*handler)(struct mproc *, struct imsg *);
911	struct imsgbuf	 imsgbuf;
912	struct ibuf	*ibuf;
913	int		 ibuferror;
914	int		 enable;
915	short		 events;
916	struct event	 ev;
917	void		*data;
918
919	off_t		 msg_in;
920	off_t		 msg_out;
921	off_t		 bytes_in;
922	off_t		 bytes_out;
923	size_t		 bytes_queued;
924	size_t		 bytes_queued_max;
925};
926
927struct msg {
928	const uint8_t	*pos;
929	const uint8_t	*end;
930};
931
932extern enum smtp_proc_type	smtpd_process;
933
934extern int verbose;
935extern int profiling;
936
937extern struct mproc *p_control;
938extern struct mproc *p_parent;
939extern struct mproc *p_lka;
940extern struct mproc *p_mda;
941extern struct mproc *p_mfa;
942extern struct mproc *p_mta;
943extern struct mproc *p_queue;
944extern struct mproc *p_scheduler;
945extern struct mproc *p_smtp;
946
947extern struct smtpd	*env;
948extern void (*imsg_callback)(struct mproc *, struct imsg *);
949
950struct imsgproc {
951	pid_t			pid;
952	struct event		ev;
953	struct imsgbuf	       *ibuf;
954	char		       *path;
955	char		       *name;
956	void		      (*cb)(struct imsg *, void *);
957	void		       *cb_arg;
958};
959
960/* inter-process structures */
961
962struct bounce_req_msg {
963	uint64_t		evpid;
964	time_t			timestamp;
965	struct delivery_bounce	bounce;
966};
967
968enum mfa_resp_status {
969	MFA_OK,
970	MFA_FAIL,
971	MFA_CLOSE,
972};
973
974enum dns_error {
975	DNS_OK = 0,
976	DNS_RETRY,
977	DNS_EINVAL,
978	DNS_ENONAME,
979	DNS_ENOTFOUND,
980};
981
982enum lka_resp_status {
983	LKA_OK,
984	LKA_TEMPFAIL,
985	LKA_PERMFAIL
986};
987
988enum ca_resp_status {
989	CA_OK,
990	CA_FAIL
991};
992
993struct ca_cert_req_msg {
994	uint64_t		reqid;
995	char			name[MAXPATHLEN];
996};
997
998struct ca_cert_resp_msg {
999	uint64_t		reqid;
1000	enum ca_resp_status	status;
1001	char		       *cert;
1002	off_t			cert_len;
1003	char		       *key;
1004	off_t			key_len;
1005};
1006
1007struct ca_vrfy_req_msg {
1008	uint64_t		reqid;
1009	unsigned char  	       *cert;
1010	off_t			cert_len;
1011	size_t			n_chain;
1012	size_t			chain_offset;
1013	unsigned char	      **chain_cert;
1014	off_t		       *chain_cert_len;
1015};
1016
1017struct ca_vrfy_resp_msg {
1018	uint64_t		reqid;
1019	enum ca_resp_status	status;
1020};
1021
1022
1023/* aliases.c */
1024int aliases_get(struct table *, struct expand *, const char *);
1025int aliases_virtual_check(struct table *, const struct mailaddr *);
1026int aliases_virtual_get(struct table *, struct expand *, const struct mailaddr *);
1027int alias_parse(struct expandnode *, const char *);
1028
1029
1030/* auth.c */
1031struct auth_backend *auth_backend_lookup(enum auth_type);
1032
1033
1034/* bounce.c */
1035void bounce_add(uint64_t);
1036void bounce_fd(int);
1037
1038
1039/* ca.c */
1040int	ca_X509_verify(void *, void *, const char *, const char *, const char **);
1041
1042
1043/* compress_backend.c */
1044int	compress_backend_init(const char *);
1045void*	compress_new(void);
1046size_t	compress_chunk(void *, void *, size_t, void *, size_t);
1047size_t	compress_finalize(void *, void *, size_t);
1048size_t	compress_buffer(char *, size_t, char *, size_t);
1049void*	uncompress_new(void);
1050size_t	uncompress_chunk(void *, void *, size_t, void *, size_t);
1051size_t	uncompress_finalize(void *, void *, size_t);
1052size_t	uncompress_buffer(char *, size_t, char *, size_t);
1053int	uncompress_file(FILE *, FILE *);
1054
1055/* config.c */
1056#define PURGE_LISTENERS		0x01
1057#define PURGE_TABLES		0x02
1058#define PURGE_RULES		0x04
1059#define PURGE_SSL		0x08
1060#define PURGE_EVERYTHING	0xff
1061void purge_config(uint8_t);
1062void init_pipes(void);
1063void config_process(enum smtp_proc_type);
1064void config_peer(enum smtp_proc_type);
1065void config_done(void);
1066
1067
1068/* control.c */
1069pid_t control(void);
1070
1071
1072/* delivery.c */
1073struct delivery_backend *delivery_backend_lookup(enum action_type);
1074
1075
1076/* dns.c */
1077void dns_query_host(uint64_t, const char *);
1078void dns_query_ptr(uint64_t, const struct sockaddr *);
1079void dns_query_mx(uint64_t, const char *);
1080void dns_query_mx_preference(uint64_t, const char *, const char *);
1081void dns_imsg(struct mproc *, struct imsg *);
1082
1083
1084/* enqueue.c */
1085int		 enqueue(int, char **);
1086int		 enqueue_offline(int, char **);
1087
1088
1089/* envelope.c */
1090void envelope_set_errormsg(struct envelope *, char *, ...);
1091char *envelope_ascii_field_name(enum envelope_field);
1092int envelope_ascii_load(enum envelope_field, struct envelope *, char *);
1093int envelope_ascii_dump(enum envelope_field, const struct envelope *, char *,
1094    size_t);
1095int envelope_load_buffer(struct envelope *, const char *, size_t);
1096int envelope_dump_buffer(const struct envelope *, char *, size_t);
1097
1098
1099/* expand.c */
1100int expand_cmp(struct expandnode *, struct expandnode *);
1101void expand_insert(struct expand *, struct expandnode *);
1102struct expandnode *expand_lookup(struct expand *, struct expandnode *);
1103void expand_free(struct expand *);
1104int expand_line(struct expand *, const char *, int);
1105RB_PROTOTYPE(expandtree, expandnode, nodes, expand_cmp);
1106
1107
1108/* forward.c */
1109int forwards_get(int, struct expand *);
1110
1111
1112/* imsgproc.c */
1113void imsgproc_init(void);
1114struct imsgproc *imsgproc_fork(const char *, const char *,
1115    void (*)(struct imsg *, void *), void *);
1116void imsgproc_set_read(struct imsgproc *);
1117void imsgproc_set_write(struct imsgproc *);
1118void imsgproc_set_read_write(struct imsgproc *);
1119void imsgproc_reset_callback(struct imsgproc *, void (*)(struct imsg *, void *), void *);
1120
1121
1122/* lka.c */
1123pid_t lka(void);
1124
1125
1126/* lka_session.c */
1127void lka_session(uint64_t, struct envelope *);
1128void lka_session_forward_reply(struct forward_req *, int);
1129
1130
1131/* mda.c */
1132pid_t mda(void);
1133
1134
1135/* mfa.c */
1136pid_t mfa(void);
1137void mfa_ready(void);
1138
1139/* mfa_session.c */
1140void mfa_filter_init(void);
1141void mfa_filter_connect(uint64_t, const struct sockaddr *,
1142    const struct sockaddr *, const char *);
1143void mfa_filter_mailaddr(uint64_t, int, const struct mailaddr *);
1144void mfa_filter_line(uint64_t, int, const char *);
1145void mfa_filter(uint64_t, int);
1146void mfa_filter_event(uint64_t, int);
1147void mfa_filter_data(uint64_t, const char *);
1148
1149/* mproc.c */
1150int mproc_fork(struct mproc *, const char*, const char *);
1151void mproc_init(struct mproc *, int);
1152void mproc_clear(struct mproc *);
1153void mproc_enable(struct mproc *);
1154void mproc_disable(struct mproc *);
1155void m_compose(struct mproc *, uint32_t, uint32_t, pid_t, int, void *, size_t);
1156void m_composev(struct mproc *, uint32_t, uint32_t, pid_t, int,
1157    const struct iovec *, int);
1158void m_forward(struct mproc *, struct imsg *);
1159void m_create(struct mproc *, uint32_t, uint32_t, pid_t, int, size_t);
1160void m_add(struct mproc *, const void *, size_t);
1161void m_add_int(struct mproc *, int);
1162void m_add_u32(struct mproc *, uint32_t);
1163void m_add_time(struct mproc *, time_t);
1164void m_add_string(struct mproc *, const char *);
1165void m_add_data(struct mproc *, const void *, size_t);
1166void m_add_evpid(struct mproc *, uint64_t);
1167void m_add_msgid(struct mproc *, uint32_t);
1168void m_add_id(struct mproc *, uint64_t);
1169void m_add_sockaddr(struct mproc *, const struct sockaddr *);
1170void m_add_mailaddr(struct mproc *, const struct mailaddr *);
1171void m_add_envelope(struct mproc *, const struct envelope *);
1172void m_close(struct mproc *);
1173
1174void m_msg(struct msg *, struct imsg *);
1175int  m_is_eom(struct msg *);
1176void m_end(struct msg *);
1177void m_get_int(struct msg *, int *);
1178void m_get_u32(struct msg *, uint32_t *);
1179void m_get_time(struct msg *, time_t *);
1180void m_get_string(struct msg *, const char **);
1181void m_get_data(struct msg *, const void **, size_t *);
1182void m_get_evpid(struct msg *, uint64_t *);
1183void m_get_msgid(struct msg *, uint32_t *);
1184void m_get_id(struct msg *, uint64_t *);
1185void m_get_sockaddr(struct msg *, struct sockaddr *);
1186void m_get_mailaddr(struct msg *, struct mailaddr *);
1187void m_get_envelope(struct msg *, struct envelope *);
1188
1189
1190/* mta.c */
1191pid_t mta(void);
1192void mta_route_ok(struct mta_relay *, struct mta_route *);
1193void mta_route_error(struct mta_relay *, struct mta_route *);
1194void mta_route_collect(struct mta_relay *, struct mta_route *);
1195void mta_source_error(struct mta_relay *, struct mta_route *, const char *);
1196void mta_delivery(struct envelope *, const char *, int, const char *);
1197struct mta_task *mta_route_next_task(struct mta_relay *, struct mta_route *);
1198const char *mta_host_to_text(struct mta_host *);
1199const char *mta_relay_to_text(struct mta_relay *);
1200
1201/* mta_session.c */
1202void mta_session(struct mta_relay *, struct mta_route *);
1203void mta_session_imsg(struct mproc *, struct imsg *);
1204
1205
1206/* parse.y */
1207int parse_config(struct smtpd *, const char *, int);
1208int cmdline_symset(char *);
1209
1210
1211/* queue.c */
1212pid_t queue(void);
1213void queue_ok(uint64_t);
1214void queue_tempfail(uint64_t, const char *);
1215void queue_permfail(uint64_t, const char *);
1216void queue_loop(uint64_t);
1217void queue_flow_control(void);
1218
1219
1220/* queue_backend.c */
1221uint32_t queue_generate_msgid(void);
1222uint64_t queue_generate_evpid(uint32_t);
1223int queue_init(const char *, int);
1224int queue_message_incoming_path(uint32_t, char *, size_t);
1225int queue_message_create(uint32_t *);
1226int queue_message_delete(uint32_t);
1227int queue_message_commit(uint32_t);
1228int queue_message_fd_r(uint32_t);
1229int queue_message_fd_rw(uint32_t);
1230int queue_message_corrupt(uint32_t);
1231int queue_envelope_create(struct envelope *);
1232int queue_envelope_delete(uint64_t);
1233int queue_envelope_load(uint64_t, struct envelope *);
1234int queue_envelope_update(struct envelope *);
1235int queue_envelope_walk(struct envelope *);
1236
1237
1238/* ruleset.c */
1239struct rule *ruleset_match(const struct envelope *);
1240
1241
1242/* scheduler.c */
1243pid_t scheduler(void);
1244
1245
1246/* scheduler_bakend.c */
1247struct scheduler_backend *scheduler_backend_lookup(const char *);
1248void scheduler_info(struct scheduler_info *, struct envelope *);
1249time_t scheduler_compute_schedule(struct scheduler_info *);
1250
1251
1252/* smtp.c */
1253pid_t smtp(void);
1254void smtp_collect(void);
1255
1256
1257/* smtp_session.c */
1258int smtp_session(struct listener *, int, const struct sockaddr_storage *,
1259    const char *);
1260void smtp_session_imsg(struct mproc *, struct imsg *);
1261
1262
1263/* smtpd.c */
1264void imsg_dispatch(struct mproc *, struct imsg *);
1265const char *proc_name(enum smtp_proc_type);
1266const char *proc_title(enum smtp_proc_type);
1267const char *imsg_to_str(int);
1268
1269
1270/* ssl_smtpd.c */
1271void   *ssl_mta_init(char *, off_t, char *, off_t);
1272void   *ssl_smtp_init(void *, char *, off_t, char *, off_t);
1273
1274
1275/* stat_backend.c */
1276struct stat_backend	*stat_backend_lookup(const char *);
1277void	stat_increment(const char *, size_t);
1278void	stat_decrement(const char *, size_t);
1279void	stat_set(const char *, const struct stat_value *);
1280struct stat_value *stat_counter(size_t);
1281struct stat_value *stat_timestamp(time_t);
1282struct stat_value *stat_timeval(struct timeval *);
1283struct stat_value *stat_timespec(struct timespec *);
1284
1285
1286/* table.c */
1287int	table_open(struct table *);
1288void	table_update(struct table *);
1289void	table_close(struct table *);
1290int	table_check_use(struct table *, uint32_t, uint32_t);
1291int	table_check_type(struct table *, uint32_t);
1292int	table_check_service(struct table *, uint32_t);
1293int	table_lookup(struct table *, const char *, enum table_service, void **);
1294int	table_fetch(struct table *, enum table_service, char **);
1295struct table *table_find(objid_t);
1296struct table *table_findbyname(const char *);
1297struct table *table_create(const char *, const char *, const char *);
1298void table_destroy(struct table *);
1299void table_add(struct table *, const char *, const char *);
1300void table_delete(struct table *, const char *);
1301void table_delete_all(struct table *);
1302int table_domain_match(const char *, const char *);
1303int table_netaddr_match(const char *, const char *);
1304int table_mailaddr_match(const char *, const char *);
1305void	table_open_all(void);
1306void	table_close_all(void);
1307void	table_set_payload(struct table *, void *);
1308void   *table_get_payload(struct table *);
1309void	table_set_configuration(struct table *, struct table *);
1310struct table	*table_get_configuration(struct table *);
1311const void	*table_get(struct table *, const char *);
1312
1313void *table_config_create(void);
1314const char *table_config_get(void *, const char *);
1315void table_config_destroy(void *);
1316int table_config_parse(void *, const char *, enum table_type);
1317
1318
1319/* to.c */
1320int email_to_mailaddr(struct mailaddr *, char *);
1321uint32_t evpid_to_msgid(uint64_t);
1322uint64_t msgid_to_evpid(uint32_t);
1323int text_to_netaddr(struct netaddr *, const char *);
1324int text_to_mailaddr(struct mailaddr *, const char *);
1325int text_to_relayhost(struct relayhost *, const char *);
1326int text_to_userinfo(struct userinfo *, const char *);
1327int text_to_credentials(struct credentials *, const char *);
1328int text_to_expandnode(struct expandnode *, const char *);
1329uint64_t text_to_evpid(const char *);
1330uint32_t text_to_msgid(const char *);
1331const char *sa_to_text(const struct sockaddr *);
1332const char *ss_to_text(const struct sockaddr_storage *);
1333const char *time_to_text(time_t);
1334const char *duration_to_text(time_t);
1335const char *relayhost_to_text(const struct relayhost *);
1336const char *rule_to_text(struct rule *);
1337const char *sockaddr_to_text(struct sockaddr *);
1338const char *mailaddr_to_text(const struct mailaddr *);
1339
1340
1341/* util.c */
1342typedef struct arglist arglist;
1343struct arglist {
1344	char	**list;
1345	uint	  num;
1346	uint	  nalloc;
1347};
1348void addargs(arglist *, char *, ...)
1349	__attribute__((format(printf, 2, 3)));
1350int bsnprintf(char *, size_t, const char *, ...)
1351	__attribute__((format (printf, 3, 4)));
1352int mkdirs(char *, mode_t);
1353int safe_fclose(FILE *);
1354int hostname_match(const char *, const char *);
1355int valid_localpart(const char *);
1356int valid_domainpart(const char *);
1357int secure_file(int, char *, char *, uid_t, int);
1358int  lowercase(char *, const char *, size_t);
1359void xlowercase(char *, const char *, size_t);
1360void sa_set_port(struct sockaddr *, int);
1361uint64_t generate_uid(void);
1362void fdlimit(double);
1363int availdesc(void);
1364int ckdir(const char *, mode_t, uid_t, gid_t, int);
1365int rmtree(char *, int);
1366int mvpurge(char *, char *);
1367int mktmpfile(void);
1368const char *parse_smtp_response(char *, size_t, char **, int *);
1369void *xmalloc(size_t, const char *);
1370void *xcalloc(size_t, size_t, const char *);
1371char *xstrdup(const char *, const char *);
1372void *xmemdup(const void *, size_t, const char *);
1373char *strip(char *);
1374void iobuf_xinit(struct iobuf *, size_t, size_t, const char *);
1375void iobuf_xfqueue(struct iobuf *, const char *, const char *, ...);
1376void log_envelope(const struct envelope *, const char *, const char *,
1377    const char *);
1378void session_socket_blockmode(int, enum blockmodes);
1379void session_socket_no_linger(int);
1380int session_socket_error(int);
1381
1382
1383/* waitq.c */
1384int  waitq_wait(void *, void (*)(void *, void *, void *), void *);
1385void waitq_run(void *, void *);
1386