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