GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.sbin/smtpd/smtpd/../mta.c Lines: 0 1328 0.0 %
Date: 2017-11-13 Branches: 0 1084 0.0 %

Line Branch Exec Source
1
/*	$OpenBSD: mta.c,v 1.205 2017/09/15 11:50:39 eric Exp $	*/
2
3
/*
4
 * Copyright (c) 2008 Pierre-Yves Ritschard <pyr@openbsd.org>
5
 * Copyright (c) 2008 Gilles Chehade <gilles@poolp.org>
6
 * Copyright (c) 2009 Jacek Masiulaniec <jacekm@dobremiasto.net>
7
 * Copyright (c) 2012 Eric Faurot <eric@openbsd.org>
8
 *
9
 * Permission to use, copy, modify, and distribute this software for any
10
 * purpose with or without fee is hereby granted, provided that the above
11
 * copyright notice and this permission notice appear in all copies.
12
 *
13
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20
 */
21
22
#include <sys/types.h>
23
#include <sys/queue.h>
24
#include <sys/tree.h>
25
#include <sys/socket.h>
26
27
#include <ctype.h>
28
#include <err.h>
29
#include <errno.h>
30
#include <event.h>
31
#include <imsg.h>
32
#include <inttypes.h>
33
#include <netdb.h>
34
#include <limits.h>
35
#include <pwd.h>
36
#include <signal.h>
37
#include <stdio.h>
38
#include <stdlib.h>
39
#include <string.h>
40
#include <time.h>
41
#include <unistd.h>
42
43
#include "smtpd.h"
44
#include "log.h"
45
46
#define MAXERROR_PER_ROUTE	4
47
48
#define DELAY_CHECK_SOURCE	1
49
#define DELAY_CHECK_SOURCE_SLOW	10
50
#define DELAY_CHECK_SOURCE_FAST 0
51
#define DELAY_CHECK_LIMIT	5
52
53
#define	DELAY_QUADRATIC		1
54
#define DELAY_ROUTE_BASE	15
55
#define DELAY_ROUTE_MAX		3600
56
57
#define RELAY_ONHOLD		0x01
58
#define RELAY_HOLDQ		0x02
59
60
static void mta_handle_envelope(struct envelope *);
61
static void mta_query_mx(struct mta_relay *);
62
static void mta_query_secret(struct mta_relay *);
63
static void mta_query_preference(struct mta_relay *);
64
static void mta_query_source(struct mta_relay *);
65
static void mta_on_mx(void *, void *, void *);
66
static void mta_on_secret(struct mta_relay *, const char *);
67
static void mta_on_preference(struct mta_relay *, int);
68
static void mta_on_source(struct mta_relay *, struct mta_source *);
69
static void mta_on_timeout(struct runq *, void *);
70
static void mta_connect(struct mta_connector *);
71
static void mta_route_enable(struct mta_route *);
72
static void mta_route_disable(struct mta_route *, int, int);
73
static void mta_drain(struct mta_relay *);
74
static void mta_delivery_flush_event(int, short, void *);
75
static void mta_flush(struct mta_relay *, int, const char *);
76
static struct mta_route *mta_find_route(struct mta_connector *, time_t, int*,
77
    time_t*);
78
static void mta_log(const struct mta_envelope *, const char *, const char *,
79
    const char *, const char *);
80
81
SPLAY_HEAD(mta_relay_tree, mta_relay);
82
static struct mta_relay *mta_relay(struct envelope *);
83
static void mta_relay_ref(struct mta_relay *);
84
static void mta_relay_unref(struct mta_relay *);
85
static void mta_relay_show(struct mta_relay *, struct mproc *, uint32_t, time_t);
86
static int mta_relay_cmp(const struct mta_relay *, const struct mta_relay *);
87
SPLAY_PROTOTYPE(mta_relay_tree, mta_relay, entry, mta_relay_cmp);
88
89
SPLAY_HEAD(mta_host_tree, mta_host);
90
static struct mta_host *mta_host(const struct sockaddr *);
91
static void mta_host_ref(struct mta_host *);
92
static void mta_host_unref(struct mta_host *);
93
static int mta_host_cmp(const struct mta_host *, const struct mta_host *);
94
SPLAY_PROTOTYPE(mta_host_tree, mta_host, entry, mta_host_cmp);
95
96
SPLAY_HEAD(mta_domain_tree, mta_domain);
97
static struct mta_domain *mta_domain(char *, int);
98
#if 0
99
static void mta_domain_ref(struct mta_domain *);
100
#endif
101
static void mta_domain_unref(struct mta_domain *);
102
static int mta_domain_cmp(const struct mta_domain *, const struct mta_domain *);
103
SPLAY_PROTOTYPE(mta_domain_tree, mta_domain, entry, mta_domain_cmp);
104
105
SPLAY_HEAD(mta_source_tree, mta_source);
106
static struct mta_source *mta_source(const struct sockaddr *);
107
static void mta_source_ref(struct mta_source *);
108
static void mta_source_unref(struct mta_source *);
109
static const char *mta_source_to_text(struct mta_source *);
110
static int mta_source_cmp(const struct mta_source *, const struct mta_source *);
111
SPLAY_PROTOTYPE(mta_source_tree, mta_source, entry, mta_source_cmp);
112
113
static struct mta_connector *mta_connector(struct mta_relay *,
114
    struct mta_source *);
115
static void mta_connector_free(struct mta_connector *);
116
static const char *mta_connector_to_text(struct mta_connector *);
117
118
SPLAY_HEAD(mta_route_tree, mta_route);
119
static struct mta_route *mta_route(struct mta_source *, struct mta_host *);
120
static void mta_route_ref(struct mta_route *);
121
static void mta_route_unref(struct mta_route *);
122
static const char *mta_route_to_text(struct mta_route *);
123
static int mta_route_cmp(const struct mta_route *, const struct mta_route *);
124
SPLAY_PROTOTYPE(mta_route_tree, mta_route, entry, mta_route_cmp);
125
126
struct mta_block {
127
	SPLAY_ENTRY(mta_block)	 entry;
128
	struct mta_source	*source;
129
	char			*domain;
130
};
131
132
SPLAY_HEAD(mta_block_tree, mta_block);
133
void mta_block(struct mta_source *, char *);
134
void mta_unblock(struct mta_source *, char *);
135
int mta_is_blocked(struct mta_source *, char *);
136
static int mta_block_cmp(const struct mta_block *, const struct mta_block *);
137
SPLAY_PROTOTYPE(mta_block_tree, mta_block, entry, mta_block_cmp);
138
139
static struct mta_relay_tree		relays;
140
static struct mta_domain_tree		domains;
141
static struct mta_host_tree		hosts;
142
static struct mta_source_tree		sources;
143
static struct mta_route_tree		routes;
144
static struct mta_block_tree		blocks;
145
146
static struct tree wait_mx;
147
static struct tree wait_preference;
148
static struct tree wait_secret;
149
static struct tree wait_source;
150
static struct tree flush_evp;
151
static struct event ev_flush_evp;
152
153
static struct runq *runq_relay;
154
static struct runq *runq_connector;
155
static struct runq *runq_route;
156
static struct runq *runq_hoststat;
157
158
static time_t	max_seen_conndelay_route;
159
static time_t	max_seen_discdelay_route;
160
161
#define	HOSTSTAT_EXPIRE_DELAY	(4 * 3600)
162
struct hoststat {
163
	char			 name[HOST_NAME_MAX+1];
164
	time_t			 tm;
165
	char			 error[LINE_MAX];
166
	struct tree		 deferred;
167
};
168
static struct dict hoststat;
169
170
void mta_hoststat_update(const char *, const char *);
171
void mta_hoststat_cache(const char *, uint64_t);
172
void mta_hoststat_uncache(const char *, uint64_t);
173
void mta_hoststat_reschedule(const char *);
174
static void mta_hoststat_remove_entry(struct hoststat *);
175
176
177
void
178
mta_imsg(struct mproc *p, struct imsg *imsg)
179
{
180
	struct mta_relay	*relay;
181
	struct mta_domain	*domain;
182
	struct mta_host		*host;
183
	struct mta_route	*route;
184
	struct mta_block	*block;
185
	struct mta_mx		*mx, *imx;
186
	struct mta_source	*source;
187
	struct hoststat		*hs;
188
	struct sockaddr_storage	 ss;
189
	struct envelope		 evp;
190
	struct msg		 m;
191
	const char		*secret;
192
	const char		*hostname;
193
	const char		*dom;
194
	uint64_t		 reqid;
195
	time_t			 t;
196
	char			 buf[LINE_MAX];
197
	int			 dnserror, preference, v, status;
198
	void			*iter;
199
	uint64_t		 u64;
200
201
	if (p->proc == PROC_QUEUE) {
202
		switch (imsg->hdr.type) {
203
204
		case IMSG_QUEUE_TRANSFER:
205
			m_msg(&m, imsg);
206
			m_get_envelope(&m, &evp);
207
			m_end(&m);
208
			mta_handle_envelope(&evp);
209
			return;
210
211
		case IMSG_MTA_OPEN_MESSAGE:
212
			mta_session_imsg(p, imsg);
213
			return;
214
		}
215
	}
216
217
	if (p->proc == PROC_LKA) {
218
		switch (imsg->hdr.type) {
219
220
		case IMSG_MTA_LOOKUP_CREDENTIALS:
221
			m_msg(&m, imsg);
222
			m_get_id(&m, &reqid);
223
			m_get_string(&m, &secret);
224
			m_end(&m);
225
			relay = tree_xpop(&wait_secret, reqid);
226
			mta_on_secret(relay, secret[0] ? secret : NULL);
227
			return;
228
229
		case IMSG_MTA_LOOKUP_SOURCE:
230
			m_msg(&m, imsg);
231
			m_get_id(&m, &reqid);
232
			m_get_int(&m, &status);
233
			if (status == LKA_OK)
234
				m_get_sockaddr(&m, (struct sockaddr*)&ss);
235
			m_end(&m);
236
237
			relay = tree_xpop(&wait_source, reqid);
238
			mta_on_source(relay, (status == LKA_OK) ?
239
			    mta_source((struct sockaddr *)&ss) : NULL);
240
			return;
241
242
		case IMSG_MTA_LOOKUP_HELO:
243
			mta_session_imsg(p, imsg);
244
			return;
245
246
		case IMSG_MTA_DNS_HOST:
247
			m_msg(&m, imsg);
248
			m_get_id(&m, &reqid);
249
			m_get_sockaddr(&m, (struct sockaddr*)&ss);
250
			m_get_int(&m, &preference);
251
			m_end(&m);
252
			domain = tree_xget(&wait_mx, reqid);
253
			mx = xcalloc(1, sizeof *mx, "mta: mx");
254
			mx->host = mta_host((struct sockaddr*)&ss);
255
			mx->preference = preference;
256
			TAILQ_FOREACH(imx, &domain->mxs, entry) {
257
				if (imx->preference > mx->preference) {
258
					TAILQ_INSERT_BEFORE(imx, mx, entry);
259
					return;
260
				}
261
			}
262
			TAILQ_INSERT_TAIL(&domain->mxs, mx, entry);
263
			return;
264
265
		case IMSG_MTA_DNS_HOST_END:
266
			m_msg(&m, imsg);
267
			m_get_id(&m, &reqid);
268
			m_get_int(&m, &dnserror);
269
			m_end(&m);
270
			domain = tree_xpop(&wait_mx, reqid);
271
			domain->mxstatus = dnserror;
272
			if (domain->mxstatus == DNS_OK) {
273
				log_debug("debug: MXs for domain %s:",
274
				    domain->name);
275
				TAILQ_FOREACH(mx, &domain->mxs, entry)
276
					log_debug("	%s preference %d",
277
					    sa_to_text(mx->host->sa),
278
					    mx->preference);
279
			}
280
			else {
281
				log_debug("debug: Failed MX query for %s:",
282
				    domain->name);
283
			}
284
			domain->lastmxquery = time(NULL);
285
			waitq_run(&domain->mxs, domain);
286
			return;
287
288
		case IMSG_MTA_DNS_MX_PREFERENCE:
289
			m_msg(&m, imsg);
290
			m_get_id(&m, &reqid);
291
			m_get_int(&m, &dnserror);
292
			if (dnserror == 0)
293
				m_get_int(&m, &preference);
294
			m_end(&m);
295
296
			relay = tree_xpop(&wait_preference, reqid);
297
			if (dnserror) {
298
				log_warnx("warn: Couldn't find backup "
299
				    "preference for %s: error %d",
300
				    mta_relay_to_text(relay), dnserror);
301
				preference = INT_MAX;
302
			}
303
			mta_on_preference(relay, preference);
304
			return;
305
306
		case IMSG_MTA_DNS_PTR:
307
			mta_session_imsg(p, imsg);
308
			return;
309
310
		case IMSG_MTA_TLS_INIT:
311
			mta_session_imsg(p, imsg);
312
			return;
313
314
		case IMSG_MTA_TLS_VERIFY:
315
			mta_session_imsg(p, imsg);
316
			return;
317
		}
318
	}
319
320
	if (p->proc == PROC_CONTROL) {
321
		switch (imsg->hdr.type) {
322
323
		case IMSG_CTL_RESUME_ROUTE:
324
			u64 = *((uint64_t *)imsg->data);
325
			if (u64)
326
				log_debug("resuming route: %llu",
327
				    (unsigned long long)u64);
328
			else
329
				log_debug("resuming all routes");
330
			SPLAY_FOREACH(route, mta_route_tree, &routes) {
331
				if (u64 && route->id != u64)
332
					continue;
333
334
				if (route->flags & ROUTE_DISABLED) {
335
					log_info("smtp-out: Enabling route %s per admin request",
336
					    mta_route_to_text(route));
337
					if (!runq_cancel(runq_route, NULL, route)) {
338
						log_warnx("warn: route not on runq");
339
						fatalx("exiting");
340
					}
341
					route->flags &= ~ROUTE_DISABLED;
342
					route->flags |= ROUTE_NEW;
343
					route->nerror = 0;
344
					route->penalty = 0;
345
					mta_route_unref(route); /* from mta_route_disable */
346
				}
347
348
				if (u64)
349
					break;
350
			}
351
			return;
352
353
		case IMSG_CTL_MTA_SHOW_HOSTS:
354
			t = time(NULL);
355
			SPLAY_FOREACH(host, mta_host_tree, &hosts) {
356
				(void)snprintf(buf, sizeof(buf),
357
				    "%s %s refcount=%d nconn=%zu lastconn=%s",
358
				    sockaddr_to_text(host->sa),
359
				    host->ptrname,
360
				    host->refcount,
361
				    host->nconn,
362
				    host->lastconn ? duration_to_text(t - host->lastconn) : "-");
363
				m_compose(p, IMSG_CTL_MTA_SHOW_HOSTS,
364
				    imsg->hdr.peerid, 0, -1,
365
				    buf, strlen(buf) + 1);
366
			}
367
			m_compose(p, IMSG_CTL_MTA_SHOW_HOSTS, imsg->hdr.peerid,
368
			    0, -1, NULL, 0);
369
			return;
370
371
		case IMSG_CTL_MTA_SHOW_RELAYS:
372
			t = time(NULL);
373
			SPLAY_FOREACH(relay, mta_relay_tree, &relays)
374
				mta_relay_show(relay, p, imsg->hdr.peerid, t);
375
			m_compose(p, IMSG_CTL_MTA_SHOW_RELAYS, imsg->hdr.peerid,
376
			    0, -1, NULL, 0);
377
			return;
378
379
		case IMSG_CTL_MTA_SHOW_ROUTES:
380
			SPLAY_FOREACH(route, mta_route_tree, &routes) {
381
				v = runq_pending(runq_route, NULL, route, &t);
382
				(void)snprintf(buf, sizeof(buf),
383
				    "%llu. %s %c%c%c%c nconn=%zu nerror=%d penalty=%d timeout=%s",
384
				    (unsigned long long)route->id,
385
				    mta_route_to_text(route),
386
				    route->flags & ROUTE_NEW ? 'N' : '-',
387
				    route->flags & ROUTE_DISABLED ? 'D' : '-',
388
				    route->flags & ROUTE_RUNQ ? 'Q' : '-',
389
				    route->flags & ROUTE_KEEPALIVE ? 'K' : '-',
390
				    route->nconn,
391
				    route->nerror,
392
				    route->penalty,
393
				    v ? duration_to_text(t - time(NULL)) : "-");
394
				m_compose(p, IMSG_CTL_MTA_SHOW_ROUTES,
395
				    imsg->hdr.peerid, 0, -1,
396
				    buf, strlen(buf) + 1);
397
			}
398
			m_compose(p, IMSG_CTL_MTA_SHOW_ROUTES, imsg->hdr.peerid,
399
			    0, -1, NULL, 0);
400
			return;
401
402
		case IMSG_CTL_MTA_SHOW_HOSTSTATS:
403
			iter = NULL;
404
			while (dict_iter(&hoststat, &iter, &hostname,
405
				(void **)&hs)) {
406
				(void)snprintf(buf, sizeof(buf),
407
				    "%s|%llu|%s",
408
				    hostname, (unsigned long long) hs->tm,
409
				    hs->error);
410
				m_compose(p, IMSG_CTL_MTA_SHOW_HOSTSTATS,
411
				    imsg->hdr.peerid, 0, -1,
412
				    buf, strlen(buf) + 1);
413
			}
414
			m_compose(p, IMSG_CTL_MTA_SHOW_HOSTSTATS,
415
			    imsg->hdr.peerid,
416
			    0, -1, NULL, 0);
417
			return;
418
419
		case IMSG_CTL_MTA_BLOCK:
420
			m_msg(&m, imsg);
421
			m_get_sockaddr(&m, (struct sockaddr*)&ss);
422
			m_get_string(&m, &dom);
423
			m_end(&m);
424
			source = mta_source((struct sockaddr*)&ss);
425
			if (*dom != '\0') {
426
				if (!(strlcpy(buf, dom, sizeof(buf))
427
					>= sizeof(buf)))
428
					mta_block(source, buf);
429
			}
430
			else
431
				mta_block(source, NULL);
432
			mta_source_unref(source);
433
			m_compose(p, IMSG_CTL_OK, imsg->hdr.peerid, 0, -1, NULL, 0);
434
			return;
435
436
		case IMSG_CTL_MTA_UNBLOCK:
437
			m_msg(&m, imsg);
438
			m_get_sockaddr(&m, (struct sockaddr*)&ss);
439
			m_get_string(&m, &dom);
440
			m_end(&m);
441
			source = mta_source((struct sockaddr*)&ss);
442
			if (*dom != '\0') {
443
				if (!(strlcpy(buf, dom, sizeof(buf))
444
					>= sizeof(buf)))
445
					mta_unblock(source, buf);
446
			}
447
			else
448
				mta_unblock(source, NULL);
449
			mta_source_unref(source);
450
			m_compose(p, IMSG_CTL_OK, imsg->hdr.peerid, 0, -1, NULL, 0);
451
			return;
452
453
		case IMSG_CTL_MTA_SHOW_BLOCK:
454
			SPLAY_FOREACH(block, mta_block_tree, &blocks) {
455
				(void)snprintf(buf, sizeof(buf), "%s -> %s",
456
				    mta_source_to_text(block->source),
457
				    block->domain ? block->domain : "*");
458
				m_compose(p, IMSG_CTL_MTA_SHOW_BLOCK,
459
				    imsg->hdr.peerid, 0, -1, buf, strlen(buf) + 1);
460
			}
461
			m_compose(p, IMSG_CTL_MTA_SHOW_BLOCK, imsg->hdr.peerid,
462
			    0, -1, NULL, 0);
463
			return;
464
		}
465
	}
466
467
	errx(1, "mta_imsg: unexpected %s imsg", imsg_to_str(imsg->hdr.type));
468
}
469
470
void
471
mta_postfork(void)
472
{
473
}
474
475
void
476
mta_postprivdrop(void)
477
{
478
	SPLAY_INIT(&relays);
479
	SPLAY_INIT(&domains);
480
	SPLAY_INIT(&hosts);
481
	SPLAY_INIT(&sources);
482
	SPLAY_INIT(&routes);
483
	SPLAY_INIT(&blocks);
484
485
	tree_init(&wait_secret);
486
	tree_init(&wait_mx);
487
	tree_init(&wait_preference);
488
	tree_init(&wait_source);
489
	tree_init(&flush_evp);
490
	dict_init(&hoststat);
491
492
	evtimer_set(&ev_flush_evp, mta_delivery_flush_event, NULL);
493
494
	runq_init(&runq_relay, mta_on_timeout);
495
	runq_init(&runq_connector, mta_on_timeout);
496
	runq_init(&runq_route, mta_on_timeout);
497
	runq_init(&runq_hoststat, mta_on_timeout);
498
}
499
500
501
/*
502
 * Local error on the given source.
503
 */
504
void
505
mta_source_error(struct mta_relay *relay, struct mta_route *route, const char *e)
506
{
507
	struct mta_connector	*c;
508
509
	/*
510
	 * Remember the source as broken for this connector.
511
	 */
512
	c = mta_connector(relay, route->src);
513
	if (!(c->flags & CONNECTOR_ERROR_SOURCE))
514
		log_info("smtp-out: Error on %s: %s",
515
		    mta_route_to_text(route), e);
516
	c->flags |= CONNECTOR_ERROR_SOURCE;
517
}
518
519
void
520
mta_route_error(struct mta_relay *relay, struct mta_route *route)
521
{
522
#if 0
523
	route->nerror += 1;
524
525
	if (route->nerror > MAXERROR_PER_ROUTE) {
526
		log_info("smtp-out: Too many errors on %s: "
527
		    "disabling for a while", mta_route_to_text(route));
528
		mta_route_disable(route, 2, ROUTE_DISABLED_SMTP);
529
	}
530
#endif
531
}
532
533
void
534
mta_route_ok(struct mta_relay *relay, struct mta_route *route)
535
{
536
	struct mta_connector	*c;
537
538
	if (!(route->flags & ROUTE_NEW))
539
		return;
540
541
	log_debug("debug: mta-routing: route %s is now valid.",
542
	    mta_route_to_text(route));
543
544
	route->nerror = 0;
545
	route->flags &= ~ROUTE_NEW;
546
547
	c = mta_connector(relay, route->src);
548
	mta_connect(c);
549
}
550
551
void
552
mta_route_down(struct mta_relay *relay, struct mta_route *route)
553
{
554
#if 0
555
	mta_route_disable(route, 2, ROUTE_DISABLED_SMTP);
556
#endif
557
}
558
559
void
560
mta_route_collect(struct mta_relay *relay, struct mta_route *route)
561
{
562
	struct mta_connector	*c;
563
564
	log_debug("debug: mta_route_collect(%s)",
565
	    mta_route_to_text(route));
566
567
	relay->nconn -= 1;
568
	relay->domain->nconn -= 1;
569
	route->nconn -= 1;
570
	route->src->nconn -= 1;
571
	route->dst->nconn -= 1;
572
	route->lastdisc = time(NULL);
573
574
	/* First connection failed */
575
	if (route->flags & ROUTE_NEW)
576
		mta_route_disable(route, 1, ROUTE_DISABLED_NET);
577
578
	c = mta_connector(relay, route->src);
579
	c->nconn -= 1;
580
	mta_connect(c);
581
	mta_route_unref(route); /* from mta_find_route() */
582
	mta_relay_unref(relay); /* from mta_connect() */
583
}
584
585
struct mta_task *
586
mta_route_next_task(struct mta_relay *relay, struct mta_route *route)
587
{
588
	struct mta_task	*task;
589
590
	if ((task = TAILQ_FIRST(&relay->tasks))) {
591
		TAILQ_REMOVE(&relay->tasks, task, entry);
592
		relay->ntask -= 1;
593
		task->relay = NULL;
594
595
		/* When the number of tasks is down to lowat, query some evp */
596
		if (relay->ntask == (size_t)relay->limits->task_lowat) {
597
			if (relay->state & RELAY_ONHOLD) {
598
				log_info("smtp-out: back to lowat on %s: releasing",
599
				    mta_relay_to_text(relay));
600
				relay->state &= ~RELAY_ONHOLD;
601
			}
602
			if (relay->state & RELAY_HOLDQ) {
603
				m_create(p_queue, IMSG_MTA_HOLDQ_RELEASE, 0, 0, -1);
604
				m_add_id(p_queue, relay->id);
605
				m_add_int(p_queue, relay->limits->task_release);
606
				m_close(p_queue);
607
			}
608
		}
609
		else if (relay->ntask == 0 && relay->state & RELAY_HOLDQ) {
610
			m_create(p_queue, IMSG_MTA_HOLDQ_RELEASE, 0, 0, -1);
611
			m_add_id(p_queue, relay->id);
612
			m_add_int(p_queue, 0);
613
			m_close(p_queue);
614
		}
615
	}
616
617
	return (task);
618
}
619
620
static void
621
mta_handle_envelope(struct envelope *evp)
622
{
623
	struct mta_relay	*relay;
624
	struct mta_task		*task;
625
	struct mta_envelope	*e;
626
	char			 buf[LINE_MAX];
627
628
	relay = mta_relay(evp);
629
	/* ignore if we don't know the limits yet */
630
	if (relay->limits &&
631
	    relay->ntask >= (size_t)relay->limits->task_hiwat) {
632
		if (!(relay->state & RELAY_ONHOLD)) {
633
			log_info("smtp-out: hiwat reached on %s: holding envelopes",
634
			    mta_relay_to_text(relay));
635
			relay->state |= RELAY_ONHOLD;
636
		}
637
	}
638
639
	/*
640
	 * If the relay has too many pending tasks, tell the
641
	 * scheduler to hold it until further notice
642
	 */
643
	if (relay->state & RELAY_ONHOLD) {
644
		relay->state |= RELAY_HOLDQ;
645
		m_create(p_queue, IMSG_MTA_DELIVERY_HOLD, 0, 0, -1);
646
		m_add_evpid(p_queue, evp->id);
647
		m_add_id(p_queue, relay->id);
648
		m_close(p_queue);
649
		mta_relay_unref(relay); /* from here */
650
		return;
651
	}
652
653
	task = NULL;
654
	TAILQ_FOREACH(task, &relay->tasks, entry)
655
		if (task->msgid == evpid_to_msgid(evp->id))
656
			break;
657
658
	if (task == NULL) {
659
		task = xmalloc(sizeof *task, "mta_task");
660
		TAILQ_INIT(&task->envelopes);
661
		task->relay = relay;
662
		relay->ntask += 1;
663
		TAILQ_INSERT_TAIL(&relay->tasks, task, entry);
664
		task->msgid = evpid_to_msgid(evp->id);
665
		if (evp->sender.user[0] || evp->sender.domain[0])
666
			(void)snprintf(buf, sizeof buf, "%s@%s",
667
			    evp->sender.user, evp->sender.domain);
668
		else
669
			buf[0] = '\0';
670
		task->sender = xstrdup(buf, "mta_task:sender");
671
		stat_increment("mta.task", 1);
672
	}
673
674
	e = xcalloc(1, sizeof *e, "mta_envelope");
675
	e->id = evp->id;
676
	e->creation = evp->creation;
677
	(void)snprintf(buf, sizeof buf, "%s@%s",
678
	    evp->dest.user, evp->dest.domain);
679
	e->dest = xstrdup(buf, "mta_envelope:dest");
680
	(void)snprintf(buf, sizeof buf, "%s@%s",
681
	    evp->rcpt.user, evp->rcpt.domain);
682
	if (strcmp(buf, e->dest))
683
		e->rcpt = xstrdup(buf, "mta_envelope:rcpt");
684
	e->task = task;
685
	if (evp->dsn_orcpt.user[0] && evp->dsn_orcpt.domain[0]) {
686
		(void)snprintf(buf, sizeof buf, "%s@%s",
687
	    	    evp->dsn_orcpt.user, evp->dsn_orcpt.domain);
688
		e->dsn_orcpt = xstrdup(buf,
689
		    "mta_envelope:dsn_orcpt");
690
	}
691
	(void)strlcpy(e->dsn_envid, evp->dsn_envid,
692
	    sizeof e->dsn_envid);
693
	e->dsn_notify = evp->dsn_notify;
694
	e->dsn_ret = evp->dsn_ret;
695
696
	TAILQ_INSERT_TAIL(&task->envelopes, e, entry);
697
	log_debug("debug: mta: received evp:%016" PRIx64
698
	    " for <%s>", e->id, e->dest);
699
700
	stat_increment("mta.envelope", 1);
701
702
	mta_drain(relay);
703
	mta_relay_unref(relay); /* from here */
704
}
705
706
static void
707
mta_delivery_flush_event(int fd, short event, void *arg)
708
{
709
	struct mta_envelope	*e;
710
	struct timeval		 tv;
711
712
	if (tree_poproot(&flush_evp, NULL, (void**)(&e))) {
713
714
		if (e->delivery == IMSG_MTA_DELIVERY_OK) {
715
			m_create(p_queue, IMSG_MTA_DELIVERY_OK, 0, 0, -1);
716
			m_add_evpid(p_queue, e->id);
717
			m_add_int(p_queue, e->ext);
718
			m_close(p_queue);
719
		} else if (e->delivery == IMSG_MTA_DELIVERY_TEMPFAIL) {
720
			m_create(p_queue, IMSG_MTA_DELIVERY_TEMPFAIL, 0, 0, -1);
721
			m_add_evpid(p_queue, e->id);
722
			m_add_string(p_queue, e->status);
723
			m_add_int(p_queue, ESC_OTHER_STATUS);
724
			m_close(p_queue);
725
		}
726
		else if (e->delivery == IMSG_MTA_DELIVERY_PERMFAIL) {
727
			m_create(p_queue, IMSG_MTA_DELIVERY_PERMFAIL, 0, 0, -1);
728
			m_add_evpid(p_queue, e->id);
729
			m_add_string(p_queue, e->status);
730
			m_add_int(p_queue, ESC_OTHER_STATUS);
731
			m_close(p_queue);
732
		}
733
		else if (e->delivery == IMSG_MTA_DELIVERY_LOOP) {
734
			m_create(p_queue, IMSG_MTA_DELIVERY_LOOP, 0, 0, -1);
735
			m_add_evpid(p_queue, e->id);
736
			m_close(p_queue);
737
		}
738
		else {
739
			log_warnx("warn: bad delivery type %d for %016" PRIx64,
740
			    e->delivery, e->id);
741
			fatalx("aborting");
742
		}
743
744
		log_debug("debug: mta: flush for %016"PRIx64" (-> %s)", e->id, e->dest);
745
746
		free(e->dest);
747
		free(e->rcpt);
748
		free(e->dsn_orcpt);
749
		free(e);
750
751
		tv.tv_sec = 0;
752
		tv.tv_usec = 0;
753
		evtimer_add(&ev_flush_evp, &tv);
754
	}
755
}
756
757
void
758
mta_delivery_log(struct mta_envelope *e, const char *source, const char *relay,
759
    int delivery, const char *status)
760
{
761
	if (delivery == IMSG_MTA_DELIVERY_OK)
762
		mta_log(e, "Ok", source, relay, status);
763
	else if (delivery == IMSG_MTA_DELIVERY_TEMPFAIL)
764
		mta_log(e, "TempFail", source, relay, status);
765
	else if (delivery == IMSG_MTA_DELIVERY_PERMFAIL)
766
		mta_log(e, "PermFail", source, relay, status);
767
	else if (delivery == IMSG_MTA_DELIVERY_LOOP)
768
		mta_log(e, "PermFail", source, relay, "Loop detected");
769
	else {
770
		log_warnx("warn: bad delivery type %d for %016" PRIx64,
771
		    delivery, e->id);
772
		fatalx("aborting");
773
	}
774
775
	e->delivery = delivery;
776
	if (status)
777
		(void)strlcpy(e->status, status, sizeof(e->status));
778
}
779
780
void
781
mta_delivery_notify(struct mta_envelope *e)
782
{
783
	struct timeval	tv;
784
785
	tree_xset(&flush_evp, e->id, e);
786
	if (tree_count(&flush_evp) == 1) {
787
		tv.tv_sec = 0;
788
		tv.tv_usec = 0;
789
		evtimer_add(&ev_flush_evp, &tv);
790
	}
791
}
792
793
static void
794
mta_query_mx(struct mta_relay *relay)
795
{
796
	uint64_t	id;
797
798
	if (relay->status & RELAY_WAIT_MX)
799
		return;
800
801
	log_debug("debug: mta: querying MX for %s...",
802
	    mta_relay_to_text(relay));
803
804
	if (waitq_wait(&relay->domain->mxs, mta_on_mx, relay)) {
805
		id = generate_uid();
806
		tree_xset(&wait_mx, id, relay->domain);
807
		if (relay->domain->flags)
808
			m_create(p_lka,  IMSG_MTA_DNS_HOST, 0, 0, -1);
809
		else
810
			m_create(p_lka,  IMSG_MTA_DNS_MX, 0, 0, -1);
811
		m_add_id(p_lka, id);
812
		m_add_string(p_lka, relay->domain->name);
813
		m_close(p_lka);
814
	}
815
	relay->status |= RELAY_WAIT_MX;
816
	mta_relay_ref(relay);
817
}
818
819
static void
820
mta_query_limits(struct mta_relay *relay)
821
{
822
	if (relay->status & RELAY_WAIT_LIMITS)
823
		return;
824
825
	relay->limits = dict_get(env->sc_limits_dict, relay->domain->name);
826
	if (relay->limits == NULL)
827
		relay->limits = dict_get(env->sc_limits_dict, "default");
828
829
	if (max_seen_conndelay_route < relay->limits->conndelay_route)
830
		max_seen_conndelay_route = relay->limits->conndelay_route;
831
	if (max_seen_discdelay_route < relay->limits->discdelay_route)
832
		max_seen_discdelay_route = relay->limits->discdelay_route;
833
}
834
835
static void
836
mta_query_secret(struct mta_relay *relay)
837
{
838
	if (relay->status & RELAY_WAIT_SECRET)
839
		return;
840
841
	log_debug("debug: mta: querying secret for %s...",
842
	    mta_relay_to_text(relay));
843
844
	tree_xset(&wait_secret, relay->id, relay);
845
	relay->status |= RELAY_WAIT_SECRET;
846
847
	m_create(p_lka, IMSG_MTA_LOOKUP_CREDENTIALS, 0, 0, -1);
848
	m_add_id(p_lka, relay->id);
849
	m_add_string(p_lka, relay->authtable);
850
	m_add_string(p_lka, relay->authlabel);
851
	m_close(p_lka);
852
853
	mta_relay_ref(relay);
854
}
855
856
static void
857
mta_query_preference(struct mta_relay *relay)
858
{
859
	if (relay->status & RELAY_WAIT_PREFERENCE)
860
		return;
861
862
	log_debug("debug: mta: querying preference for %s...",
863
	    mta_relay_to_text(relay));
864
865
	tree_xset(&wait_preference, relay->id, relay);
866
	relay->status |= RELAY_WAIT_PREFERENCE;
867
868
	m_create(p_lka,  IMSG_MTA_DNS_MX_PREFERENCE, 0, 0, -1);
869
	m_add_id(p_lka, relay->id);
870
	m_add_string(p_lka, relay->domain->name);
871
	m_add_string(p_lka, relay->backupname);
872
	m_close(p_lka);
873
874
	mta_relay_ref(relay);
875
}
876
877
static void
878
mta_query_source(struct mta_relay *relay)
879
{
880
	log_debug("debug: mta: querying source for %s...",
881
	    mta_relay_to_text(relay));
882
883
	relay->sourceloop += 1;
884
885
	if (relay->sourcetable == NULL) {
886
		/*
887
		 * This is a recursive call, but it only happens once, since
888
		 * another source will not be queried immediately.
889
		 */
890
		mta_relay_ref(relay);
891
		mta_on_source(relay, mta_source(NULL));
892
		return;
893
	}
894
895
	m_create(p_lka, IMSG_MTA_LOOKUP_SOURCE, 0, 0, -1);
896
	m_add_id(p_lka, relay->id);
897
	m_add_string(p_lka, relay->sourcetable);
898
	m_close(p_lka);
899
900
	tree_xset(&wait_source, relay->id, relay);
901
	relay->status |= RELAY_WAIT_SOURCE;
902
	mta_relay_ref(relay);
903
}
904
905
static void
906
mta_on_mx(void *tag, void *arg, void *data)
907
{
908
	struct mta_domain	*domain = data;
909
	struct mta_relay	*relay = arg;
910
911
	log_debug("debug: mta: ... got mx (%p, %s, %s)",
912
	    tag, domain->name, mta_relay_to_text(relay));
913
914
	switch (domain->mxstatus) {
915
	case DNS_OK:
916
		break;
917
	case DNS_RETRY:
918
		relay->fail = IMSG_MTA_DELIVERY_TEMPFAIL;
919
		relay->failstr = "Temporary failure in MX lookup";
920
		break;
921
	case DNS_EINVAL:
922
		relay->fail = IMSG_MTA_DELIVERY_PERMFAIL;
923
		relay->failstr = "Invalid domain name";
924
		break;
925
	case DNS_ENONAME:
926
		relay->fail = IMSG_MTA_DELIVERY_PERMFAIL;
927
		relay->failstr = "Domain does not exist";
928
		break;
929
	case DNS_ENOTFOUND:
930
		relay->fail = IMSG_MTA_DELIVERY_TEMPFAIL;
931
		relay->failstr = "No MX found for domain";
932
		break;
933
	default:
934
		fatalx("bad DNS lookup error code");
935
		break;
936
	}
937
938
	if (domain->mxstatus)
939
		log_info("smtp-out: Failed to resolve MX for %s: %s",
940
		    mta_relay_to_text(relay), relay->failstr);
941
942
	relay->status &= ~RELAY_WAIT_MX;
943
	mta_drain(relay);
944
	mta_relay_unref(relay); /* from mta_drain() */
945
}
946
947
static void
948
mta_on_secret(struct mta_relay *relay, const char *secret)
949
{
950
	log_debug("debug: mta: ... got secret for %s: %s",
951
	    mta_relay_to_text(relay), secret);
952
953
	if (secret)
954
		relay->secret = strdup(secret);
955
956
	if (relay->secret == NULL) {
957
		log_warnx("warn: Failed to retrieve secret "
958
			    "for %s", mta_relay_to_text(relay));
959
		relay->fail = IMSG_MTA_DELIVERY_TEMPFAIL;
960
		relay->failstr = "Could not retrieve credentials";
961
	}
962
963
	relay->status &= ~RELAY_WAIT_SECRET;
964
	mta_drain(relay);
965
	mta_relay_unref(relay); /* from mta_query_secret() */
966
}
967
968
static void
969
mta_on_preference(struct mta_relay *relay, int preference)
970
{
971
	log_debug("debug: mta: ... got preference for %s: %d",
972
	    mta_relay_to_text(relay), preference);
973
974
	relay->backuppref = preference;
975
976
	relay->status &= ~RELAY_WAIT_PREFERENCE;
977
	mta_drain(relay);
978
	mta_relay_unref(relay); /* from mta_query_preference() */
979
}
980
981
static void
982
mta_on_source(struct mta_relay *relay, struct mta_source *source)
983
{
984
	struct mta_connector	*c;
985
	void			*iter;
986
	int			 delay, errmask;
987
988
	log_debug("debug: mta: ... got source for %s: %s",
989
	    mta_relay_to_text(relay), source ? mta_source_to_text(source) : "NULL");
990
991
	relay->lastsource = time(NULL);
992
	delay = DELAY_CHECK_SOURCE_SLOW;
993
994
	if (source) {
995
		c = mta_connector(relay, source);
996
		if (c->flags & CONNECTOR_NEW) {
997
			c->flags &= ~CONNECTOR_NEW;
998
			delay = DELAY_CHECK_SOURCE;
999
		}
1000
		mta_connect(c);
1001
		if ((c->flags & CONNECTOR_ERROR) == 0)
1002
			relay->sourceloop = 0;
1003
		else
1004
			delay = DELAY_CHECK_SOURCE_FAST;
1005
		mta_source_unref(source); /* from constructor */
1006
	}
1007
	else {
1008
		log_warnx("warn: Failed to get source address for %s",
1009
		    mta_relay_to_text(relay));
1010
	}
1011
1012
	if (tree_count(&relay->connectors) == 0) {
1013
		relay->fail = IMSG_MTA_DELIVERY_TEMPFAIL;
1014
		relay->failstr = "Could not retrieve source address";
1015
	}
1016
	if (tree_count(&relay->connectors) < relay->sourceloop) {
1017
		relay->fail = IMSG_MTA_DELIVERY_TEMPFAIL;
1018
		relay->failstr = "No valid route to remote MX";
1019
1020
		errmask = 0;
1021
		iter = NULL;
1022
		while (tree_iter(&relay->connectors, &iter, NULL, (void **)&c))
1023
			errmask |= c->flags;
1024
1025
		if (errmask & CONNECTOR_ERROR_ROUTE_SMTP)
1026
			relay->failstr = "Destination seem to reject all mails";
1027
		else if (errmask & CONNECTOR_ERROR_ROUTE_NET)
1028
			relay->failstr = "Network error on destination MXs";
1029
		else if (errmask & CONNECTOR_ERROR_MX)
1030
			relay->failstr = "No MX found for destination";
1031
		else if (errmask & CONNECTOR_ERROR_FAMILY)
1032
			relay->failstr = "Address family mismatch on destination MXs";
1033
		else if (errmask & CONNECTOR_ERROR_BLOCKED)
1034
			relay->failstr = "All routes to destination blocked";
1035
		else
1036
			relay->failstr = "No valid route to destination";
1037
	}
1038
1039
	relay->nextsource = relay->lastsource + delay;
1040
	relay->status &= ~RELAY_WAIT_SOURCE;
1041
	mta_drain(relay);
1042
	mta_relay_unref(relay); /* from mta_query_source() */
1043
}
1044
1045
static void
1046
mta_connect(struct mta_connector *c)
1047
{
1048
	struct mta_route	*route;
1049
	struct mta_limits	*l = c->relay->limits;
1050
	int			 limits;
1051
	time_t			 nextconn, now;
1052
1053
	/* toggle the block flag */
1054
	if (mta_is_blocked(c->source, c->relay->domain->name))
1055
		c->flags |= CONNECTOR_ERROR_BLOCKED;
1056
	else
1057
		c->flags &= ~CONNECTOR_ERROR_BLOCKED;
1058
1059
    again:
1060
1061
	log_debug("debug: mta: connecting with %s", mta_connector_to_text(c));
1062
1063
	/* Do not connect if this connector has an error. */
1064
	if (c->flags & CONNECTOR_ERROR) {
1065
		log_debug("debug: mta: connector error");
1066
		return;
1067
	}
1068
1069
	if (c->flags & CONNECTOR_WAIT) {
1070
		log_debug("debug: mta: cancelling connector timeout");
1071
		runq_cancel(runq_connector, NULL, c);
1072
		c->flags &= ~CONNECTOR_WAIT;
1073
	}
1074
1075
	/* No job. */
1076
	if (c->relay->ntask == 0) {
1077
		log_debug("debug: mta: no task for connector");
1078
		return;
1079
	}
1080
1081
	/* Do not create more connections than necessary */
1082
	if ((c->relay->nconn_ready >= c->relay->ntask) ||
1083
	    (c->relay->nconn > 2 && c->relay->nconn >= c->relay->ntask / 2)) {
1084
		log_debug("debug: mta: enough connections already");
1085
		return;
1086
	}
1087
1088
	limits = 0;
1089
	nextconn = now = time(NULL);
1090
1091
	if (c->relay->domain->lastconn + l->conndelay_domain > nextconn) {
1092
		log_debug("debug: mta: cannot use domain %s before %llus",
1093
		    c->relay->domain->name,
1094
		    (unsigned long long) c->relay->domain->lastconn + l->conndelay_domain - now);
1095
		nextconn = c->relay->domain->lastconn + l->conndelay_domain;
1096
	}
1097
	if (c->relay->domain->nconn >= l->maxconn_per_domain) {
1098
		log_debug("debug: mta: hit domain limit");
1099
		limits |= CONNECTOR_LIMIT_DOMAIN;
1100
	}
1101
1102
	if (c->source->lastconn + l->conndelay_source > nextconn) {
1103
		log_debug("debug: mta: cannot use source %s before %llus",
1104
		    mta_source_to_text(c->source),
1105
		    (unsigned long long) c->source->lastconn + l->conndelay_source - now);
1106
		nextconn = c->source->lastconn + l->conndelay_source;
1107
	}
1108
	if (c->source->nconn >= l->maxconn_per_source) {
1109
		log_debug("debug: mta: hit source limit");
1110
		limits |= CONNECTOR_LIMIT_SOURCE;
1111
	}
1112
1113
	if (c->lastconn + l->conndelay_connector > nextconn) {
1114
		log_debug("debug: mta: cannot use %s before %llus",
1115
		    mta_connector_to_text(c),
1116
		    (unsigned long long) c->lastconn + l->conndelay_connector - now);
1117
		nextconn = c->lastconn + l->conndelay_connector;
1118
	}
1119
	if (c->nconn >= l->maxconn_per_connector) {
1120
		log_debug("debug: mta: hit connector limit");
1121
		limits |= CONNECTOR_LIMIT_CONN;
1122
	}
1123
1124
	if (c->relay->lastconn + l->conndelay_relay > nextconn) {
1125
		log_debug("debug: mta: cannot use %s before %llus",
1126
		    mta_relay_to_text(c->relay),
1127
		    (unsigned long long) c->relay->lastconn + l->conndelay_relay - now);
1128
		nextconn = c->relay->lastconn + l->conndelay_relay;
1129
	}
1130
	if (c->relay->nconn >= l->maxconn_per_relay) {
1131
		log_debug("debug: mta: hit relay limit");
1132
		limits |= CONNECTOR_LIMIT_RELAY;
1133
	}
1134
1135
	/* We can connect now, find a route */
1136
	if (!limits && nextconn <= now)
1137
		route = mta_find_route(c, now, &limits, &nextconn);
1138
	else
1139
		route = NULL;
1140
1141
	/* No route */
1142
	if (route == NULL) {
1143
1144
		if (c->flags & CONNECTOR_ERROR) {
1145
			/* XXX we might want to clear this flag later */
1146
			log_debug("debug: mta-routing: no route available for %s: errors on connector",
1147
			    mta_connector_to_text(c));
1148
			return;
1149
		}
1150
		else if (limits) {
1151
			log_debug("debug: mta-routing: no route available for %s: limits reached",
1152
			    mta_connector_to_text(c));
1153
			nextconn = now + DELAY_CHECK_LIMIT;
1154
		}
1155
		else {
1156
			log_debug("debug: mta-routing: no route available for %s: must wait a bit",
1157
			    mta_connector_to_text(c));
1158
		}
1159
		log_debug("debug: mta: retrying to connect on %s in %llus...",
1160
		    mta_connector_to_text(c),
1161
		    (unsigned long long) nextconn - time(NULL));
1162
		c->flags |= CONNECTOR_WAIT;
1163
		runq_schedule(runq_connector, nextconn, NULL, c);
1164
		return;
1165
	}
1166
1167
	log_debug("debug: mta-routing: spawning new connection on %s",
1168
		    mta_route_to_text(route));
1169
1170
	c->nconn += 1;
1171
	c->lastconn = time(NULL);
1172
1173
	c->relay->nconn += 1;
1174
	c->relay->lastconn = c->lastconn;
1175
	c->relay->domain->nconn += 1;
1176
	c->relay->domain->lastconn = c->lastconn;
1177
	route->nconn += 1;
1178
	route->lastconn = c->lastconn;
1179
	route->src->nconn += 1;
1180
	route->src->lastconn = c->lastconn;
1181
	route->dst->nconn += 1;
1182
	route->dst->lastconn = c->lastconn;
1183
1184
	mta_session(c->relay, route);	/* this never fails synchronously */
1185
	mta_relay_ref(c->relay);
1186
1187
    goto again;
1188
}
1189
1190
static void
1191
mta_on_timeout(struct runq *runq, void *arg)
1192
{
1193
	struct mta_connector	*connector = arg;
1194
	struct mta_relay	*relay = arg;
1195
	struct mta_route	*route = arg;
1196
	struct hoststat		*hs = arg;
1197
1198
	if (runq == runq_relay) {
1199
		log_debug("debug: mta: ... timeout for %s",
1200
		    mta_relay_to_text(relay));
1201
		relay->status &= ~RELAY_WAIT_CONNECTOR;
1202
		mta_drain(relay);
1203
		mta_relay_unref(relay); /* from mta_drain() */
1204
	}
1205
	else if (runq == runq_connector) {
1206
		log_debug("debug: mta: ... timeout for %s",
1207
		    mta_connector_to_text(connector));
1208
		connector->flags &= ~CONNECTOR_WAIT;
1209
		mta_connect(connector);
1210
	}
1211
	else if (runq == runq_route) {
1212
		route->flags &= ~ROUTE_RUNQ;
1213
		mta_route_enable(route);
1214
		mta_route_unref(route);
1215
	}
1216
	else if (runq == runq_hoststat) {
1217
		log_debug("debug: mta: ... timeout for hoststat %s",
1218
			hs->name);
1219
		mta_hoststat_remove_entry(hs);
1220
		free(hs);
1221
	}
1222
}
1223
1224
static void
1225
mta_route_disable(struct mta_route *route, int penalty, int reason)
1226
{
1227
	unsigned long long	delay;
1228
1229
	route->penalty += penalty;
1230
	route->lastpenalty = time(NULL);
1231
	delay = (unsigned long long)DELAY_ROUTE_BASE * route->penalty * route->penalty;
1232
	if (delay > DELAY_ROUTE_MAX)
1233
		delay = DELAY_ROUTE_MAX;
1234
#if 0
1235
	delay = 60;
1236
#endif
1237
1238
	log_info("smtp-out: Disabling route %s for %llus",
1239
	    mta_route_to_text(route), delay);
1240
1241
	if (route->flags & ROUTE_DISABLED)
1242
		runq_cancel(runq_route, NULL, route);
1243
	else
1244
		mta_route_ref(route);
1245
1246
	route->flags |= reason & ROUTE_DISABLED;
1247
	runq_schedule(runq_route, time(NULL) + delay, NULL, route);
1248
}
1249
1250
static void
1251
mta_route_enable(struct mta_route *route)
1252
{
1253
	if (route->flags & ROUTE_DISABLED) {
1254
		log_info("smtp-out: Enabling route %s",
1255
		    mta_route_to_text(route));
1256
		route->flags &= ~ROUTE_DISABLED;
1257
		route->flags |= ROUTE_NEW;
1258
		route->nerror = 0;
1259
	}
1260
1261
	if (route->penalty) {
1262
#if DELAY_QUADRATIC
1263
		route->penalty -= 1;
1264
		route->lastpenalty = time(NULL);
1265
#else
1266
		route->penalty = 0;
1267
#endif
1268
	}
1269
}
1270
1271
static void
1272
mta_drain(struct mta_relay *r)
1273
{
1274
	char			 buf[64];
1275
1276
	log_debug("debug: mta: draining %s "
1277
	    "refcount=%d, ntask=%zu, nconnector=%zu, nconn=%zu",
1278
	    mta_relay_to_text(r),
1279
	    r->refcount, r->ntask, tree_count(&r->connectors), r->nconn);
1280
1281
	/*
1282
	 * All done.
1283
	 */
1284
	if (r->ntask == 0) {
1285
		log_debug("debug: mta: all done for %s", mta_relay_to_text(r));
1286
		return;
1287
	}
1288
1289
	/*
1290
	 * If we know that this relay is failing flush the tasks.
1291
	 */
1292
	if (r->fail) {
1293
		mta_flush(r, r->fail, r->failstr);
1294
		return;
1295
	}
1296
1297
	/* Query secret if needed. */
1298
	if (r->flags & RELAY_AUTH && r->secret == NULL)
1299
		mta_query_secret(r);
1300
1301
	/* Query our preference if needed. */
1302
	if (r->backupname && r->backuppref == -1)
1303
		mta_query_preference(r);
1304
1305
	/* Query the domain MXs if needed. */
1306
	if (r->domain->lastmxquery == 0)
1307
		mta_query_mx(r);
1308
1309
	/* Query the limits if needed. */
1310
	if (r->limits == NULL)
1311
		mta_query_limits(r);
1312
1313
	/* Wait until we are ready to proceed. */
1314
	if (r->status & RELAY_WAITMASK) {
1315
		buf[0] = '\0';
1316
		if (r->status & RELAY_WAIT_MX)
1317
			(void)strlcat(buf, " MX", sizeof buf);
1318
		if (r->status & RELAY_WAIT_PREFERENCE)
1319
			(void)strlcat(buf, " preference", sizeof buf);
1320
		if (r->status & RELAY_WAIT_SECRET)
1321
			(void)strlcat(buf, " secret", sizeof buf);
1322
		if (r->status & RELAY_WAIT_SOURCE)
1323
			(void)strlcat(buf, " source", sizeof buf);
1324
		if (r->status & RELAY_WAIT_CONNECTOR)
1325
			(void)strlcat(buf, " connector", sizeof buf);
1326
		log_debug("debug: mta: %s waiting for%s",
1327
		    mta_relay_to_text(r), buf);
1328
		return;
1329
	}
1330
1331
	/*
1332
	 * We have pending task, and it's maybe time too try a new source.
1333
	 */
1334
	if (r->nextsource <= time(NULL))
1335
		mta_query_source(r);
1336
	else {
1337
		log_debug("debug: mta: scheduling relay %s in %llus...",
1338
		    mta_relay_to_text(r),
1339
		    (unsigned long long) r->nextsource - time(NULL));
1340
		runq_schedule(runq_relay, r->nextsource, NULL, r);
1341
		r->status |= RELAY_WAIT_CONNECTOR;
1342
		mta_relay_ref(r);
1343
	}
1344
}
1345
1346
static void
1347
mta_flush(struct mta_relay *relay, int fail, const char *error)
1348
{
1349
	struct mta_envelope	*e;
1350
	struct mta_task		*task;
1351
	const char     		*domain;
1352
	void			*iter;
1353
	struct mta_connector	*c;
1354
	size_t			 n, r;
1355
1356
	log_debug("debug: mta_flush(%s, %d, \"%s\")",
1357
	    mta_relay_to_text(relay), fail, error);
1358
1359
	if (fail != IMSG_MTA_DELIVERY_TEMPFAIL && fail != IMSG_MTA_DELIVERY_PERMFAIL)
1360
		errx(1, "unexpected delivery status %d", fail);
1361
1362
	n = 0;
1363
	while ((task = TAILQ_FIRST(&relay->tasks))) {
1364
		TAILQ_REMOVE(&relay->tasks, task, entry);
1365
		while ((e = TAILQ_FIRST(&task->envelopes))) {
1366
			TAILQ_REMOVE(&task->envelopes, e, entry);
1367
1368
			/*
1369
			 * host was suspended, cache envelope id in hoststat tree
1370
			 * so that it can be retried when a delivery succeeds for
1371
			 * that domain.
1372
			 */
1373
			domain = strchr(e->dest, '@');
1374
			if (fail == IMSG_MTA_DELIVERY_TEMPFAIL && domain) {
1375
				r = 0;
1376
				iter = NULL;
1377
				while (tree_iter(&relay->connectors, &iter,
1378
					NULL, (void **)&c)) {
1379
					if (c->flags & CONNECTOR_ERROR_ROUTE)
1380
						r++;
1381
				}
1382
				if (tree_count(&relay->connectors) == r)
1383
					mta_hoststat_cache(domain+1, e->id);
1384
			}
1385
1386
			mta_delivery_log(e, NULL, relay->domain->name, fail, error);
1387
			mta_delivery_notify(e);
1388
1389
			n++;
1390
		}
1391
		free(task->sender);
1392
		free(task);
1393
	}
1394
1395
	stat_decrement("mta.task", relay->ntask);
1396
	stat_decrement("mta.envelope", n);
1397
	relay->ntask = 0;
1398
1399
	/* release all waiting envelopes for the relay */
1400
	if (relay->state & RELAY_HOLDQ) {
1401
		m_create(p_queue, IMSG_MTA_HOLDQ_RELEASE, 0, 0, -1);
1402
		m_add_id(p_queue, relay->id);
1403
		m_add_int(p_queue, -1);
1404
		m_close(p_queue);
1405
	}
1406
}
1407
1408
/*
1409
 * Find a route to use for this connector
1410
 */
1411
static struct mta_route *
1412
mta_find_route(struct mta_connector *c, time_t now, int *limits,
1413
    time_t *nextconn)
1414
{
1415
	struct mta_route	*route, *best;
1416
	struct mta_limits	*l = c->relay->limits;
1417
	struct mta_mx		*mx;
1418
	int			 level, limit_host, limit_route;
1419
	int			 family_mismatch, seen, suspended_route;
1420
	time_t			 tm;
1421
1422
	log_debug("debug: mta-routing: searching new route for %s...",
1423
	    mta_connector_to_text(c));
1424
1425
	tm = 0;
1426
	limit_host = 0;
1427
	limit_route = 0;
1428
	suspended_route = 0;
1429
	family_mismatch = 0;
1430
	level = -1;
1431
	best = NULL;
1432
	seen = 0;
1433
1434
	TAILQ_FOREACH(mx, &c->relay->domain->mxs, entry) {
1435
		/*
1436
		 * New preference level
1437
		 */
1438
		if (mx->preference > level) {
1439
#ifndef IGNORE_MX_PREFERENCE
1440
			/*
1441
			 * Use the current best MX if found.
1442
			 */
1443
			if (best)
1444
				break;
1445
1446
			/*
1447
			 * No candidate found.  There are valid MXs at this
1448
			 * preference level but they reached their limit, or
1449
			 * we can't connect yet.
1450
			 */
1451
			if (limit_host || limit_route || tm)
1452
				break;
1453
1454
			/*
1455
			 *  If we are a backup MX, do not relay to MXs with
1456
			 *  a greater preference value.
1457
			 */
1458
			if (c->relay->backuppref >= 0 &&
1459
			    mx->preference >= c->relay->backuppref)
1460
				break;
1461
1462
			/*
1463
			 * Start looking at MXs on this preference level.
1464
			 */
1465
#endif
1466
			level = mx->preference;
1467
		}
1468
1469
		if (mx->host->flags & HOST_IGNORE)
1470
			continue;
1471
1472
		/* Found a possibly valid mx */
1473
		seen++;
1474
1475
		if ((c->source->sa &&
1476
		     c->source->sa->sa_family != mx->host->sa->sa_family) ||
1477
		    (l->family && l->family != mx->host->sa->sa_family)) {
1478
			log_debug("debug: mta-routing: skipping host %s: AF mismatch",
1479
			    mta_host_to_text(mx->host));
1480
			family_mismatch = 1;
1481
			continue;
1482
		}
1483
1484
		if (mx->host->nconn >= l->maxconn_per_host) {
1485
			log_debug("debug: mta-routing: skipping host %s: too many connections",
1486
			    mta_host_to_text(mx->host));
1487
			limit_host = 1;
1488
			continue;
1489
		}
1490
1491
		if (mx->host->lastconn + l->conndelay_host > now) {
1492
			log_debug("debug: mta-routing: skipping host %s: cannot use before %llus",
1493
			    mta_host_to_text(mx->host),
1494
			    (unsigned long long) mx->host->lastconn + l->conndelay_host - now);
1495
			if (tm == 0 || mx->host->lastconn + l->conndelay_host < tm)
1496
				tm = mx->host->lastconn + l->conndelay_host;
1497
			continue;
1498
		}
1499
1500
		route = mta_route(c->source, mx->host);
1501
1502
		if (route->flags & ROUTE_DISABLED) {
1503
			log_debug("debug: mta-routing: skipping route %s: suspend",
1504
			    mta_route_to_text(route));
1505
			suspended_route |= route->flags & ROUTE_DISABLED;
1506
			mta_route_unref(route); /* from here */
1507
			continue;
1508
		}
1509
1510
		if (route->nconn && (route->flags & ROUTE_NEW)) {
1511
			log_debug("debug: mta-routing: skipping route %s: not validated yet",
1512
			    mta_route_to_text(route));
1513
			limit_route = 1;
1514
			mta_route_unref(route); /* from here */
1515
			continue;
1516
		}
1517
1518
		if (route->nconn >= l->maxconn_per_route) {
1519
			log_debug("debug: mta-routing: skipping route %s: too many connections",
1520
			    mta_route_to_text(route));
1521
			limit_route = 1;
1522
			mta_route_unref(route); /* from here */
1523
			continue;
1524
		}
1525
1526
		if (route->lastconn + l->conndelay_route > now) {
1527
			log_debug("debug: mta-routing: skipping route %s: cannot use before %llus (delay after connect)",
1528
			    mta_route_to_text(route),
1529
			    (unsigned long long) route->lastconn + l->conndelay_route - now);
1530
			if (tm == 0 || route->lastconn + l->conndelay_route < tm)
1531
				tm = route->lastconn + l->conndelay_route;
1532
			mta_route_unref(route); /* from here */
1533
			continue;
1534
		}
1535
1536
		if (route->lastdisc + l->discdelay_route > now) {
1537
			log_debug("debug: mta-routing: skipping route %s: cannot use before %llus (delay after disconnect)",
1538
			    mta_route_to_text(route),
1539
			    (unsigned long long) route->lastdisc + l->discdelay_route - now);
1540
			if (tm == 0 || route->lastdisc + l->discdelay_route < tm)
1541
				tm = route->lastdisc + l->discdelay_route;
1542
			mta_route_unref(route); /* from here */
1543
			continue;
1544
		}
1545
1546
		/* Use the route with the lowest number of connections. */
1547
		if (best && route->nconn >= best->nconn) {
1548
			log_debug("debug: mta-routing: skipping route %s: current one is better",
1549
			    mta_route_to_text(route));
1550
			mta_route_unref(route); /* from here */
1551
			continue;
1552
		}
1553
1554
		if (best)
1555
			mta_route_unref(best); /* from here */
1556
		best = route;
1557
		log_debug("debug: mta-routing: selecting candidate route %s",
1558
		    mta_route_to_text(route));
1559
	}
1560
1561
	if (best)
1562
		return (best);
1563
1564
	/* Order is important */
1565
	if (seen == 0) {
1566
		log_info("smtp-out: No MX found for %s",
1567
		    mta_connector_to_text(c));
1568
		c->flags |= CONNECTOR_ERROR_MX;
1569
	}
1570
	else if (limit_route) {
1571
		log_debug("debug: mta: hit route limit");
1572
		*limits |= CONNECTOR_LIMIT_ROUTE;
1573
	}
1574
	else if (limit_host) {
1575
		log_debug("debug: mta: hit host limit");
1576
		*limits |= CONNECTOR_LIMIT_HOST;
1577
	}
1578
	else if (tm) {
1579
		if (tm > *nextconn)
1580
			*nextconn = tm;
1581
	}
1582
	else if (family_mismatch) {
1583
		log_info("smtp-out: Address family mismatch on %s",
1584
		    mta_connector_to_text(c));
1585
		c->flags |= CONNECTOR_ERROR_FAMILY;
1586
	}
1587
	else if (suspended_route) {
1588
		log_info("smtp-out: No valid route for %s",
1589
		    mta_connector_to_text(c));
1590
		if (suspended_route & ROUTE_DISABLED_NET)
1591
			c->flags |= CONNECTOR_ERROR_ROUTE_NET;
1592
		if (suspended_route & ROUTE_DISABLED_SMTP)
1593
			c->flags |= CONNECTOR_ERROR_ROUTE_SMTP;
1594
	}
1595
1596
	return (NULL);
1597
}
1598
1599
static void
1600
mta_log(const struct mta_envelope *evp, const char *prefix, const char *source,
1601
    const char *relay, const char *status)
1602
{
1603
	log_info("%016"PRIx64" mta event=delivery evpid=%016"PRIx64" "
1604
	    "from=<%s> to=<%s> rcpt=<%s> source=\"%s\" "
1605
	    "relay=\"%s\" delay=%s result=\"%s\" stat=\"%s\"",
1606
	    evp->session,
1607
	    evp->id,
1608
	    evp->task->sender,
1609
	    evp->dest,
1610
	    evp->rcpt ? evp->rcpt : "-",
1611
	    source ? source : "-",
1612
	    relay,
1613
	    duration_to_text(time(NULL) - evp->creation),
1614
	    prefix,
1615
	    status);
1616
}
1617
1618
static struct mta_relay *
1619
mta_relay(struct envelope *e)
1620
{
1621
	struct mta_relay	 key, *r;
1622
1623
	memset(&key, 0, sizeof key);
1624
1625
	if (e->agent.mta.relay.flags & RELAY_BACKUP) {
1626
		key.domain = mta_domain(e->dest.domain, 0);
1627
		key.backupname = e->agent.mta.relay.hostname;
1628
	} else if (e->agent.mta.relay.hostname[0]) {
1629
		key.domain = mta_domain(e->agent.mta.relay.hostname, 1);
1630
		key.flags |= RELAY_MX;
1631
	} else {
1632
		key.domain = mta_domain(e->dest.domain, 0);
1633
		if (!(e->agent.mta.relay.flags & RELAY_STARTTLS))
1634
			key.flags |= RELAY_TLS_OPTIONAL;
1635
	}
1636
1637
	key.flags |= e->agent.mta.relay.flags;
1638
	key.port = e->agent.mta.relay.port;
1639
	key.pki_name = e->agent.mta.relay.pki_name;
1640
	if (!key.pki_name[0])
1641
		key.pki_name = NULL;
1642
	key.ca_name = e->agent.mta.relay.ca_name;
1643
	if (!key.ca_name[0])
1644
		key.ca_name = NULL;
1645
	key.authtable = e->agent.mta.relay.authtable;
1646
	if (!key.authtable[0])
1647
		key.authtable = NULL;
1648
	key.authlabel = e->agent.mta.relay.authlabel;
1649
	if (!key.authlabel[0])
1650
		key.authlabel = NULL;
1651
	key.sourcetable = e->agent.mta.relay.sourcetable;
1652
	if (!key.sourcetable[0])
1653
		key.sourcetable = NULL;
1654
	key.helotable = e->agent.mta.relay.helotable;
1655
	if (!key.helotable[0])
1656
		key.helotable = NULL;
1657
	key.heloname = e->agent.mta.relay.heloname;
1658
	if (!key.heloname[0])
1659
		key.heloname = NULL;
1660
1661
	if ((r = SPLAY_FIND(mta_relay_tree, &relays, &key)) == NULL) {
1662
		r = xcalloc(1, sizeof *r, "mta_relay");
1663
		TAILQ_INIT(&r->tasks);
1664
		r->id = generate_uid();
1665
		r->flags = key.flags;
1666
		r->domain = key.domain;
1667
		r->backupname = key.backupname ?
1668
		    xstrdup(key.backupname, "mta: backupname") : NULL;
1669
		r->backuppref = -1;
1670
		r->port = key.port;
1671
		r->pki_name = key.pki_name ? xstrdup(key.pki_name, "mta: pki_name") : NULL;
1672
		r->ca_name = key.ca_name ? xstrdup(key.ca_name, "mta: ca_name") : NULL;
1673
		if (key.authtable)
1674
			r->authtable = xstrdup(key.authtable, "mta: authtable");
1675
		if (key.authlabel)
1676
			r->authlabel = xstrdup(key.authlabel, "mta: authlabel");
1677
		if (key.sourcetable)
1678
			r->sourcetable = xstrdup(key.sourcetable,
1679
			    "mta: sourcetable");
1680
		if (key.helotable)
1681
			r->helotable = xstrdup(key.helotable,
1682
			    "mta: helotable");
1683
		if (key.heloname)
1684
			r->heloname = xstrdup(key.heloname,
1685
			    "mta: heloname");
1686
		SPLAY_INSERT(mta_relay_tree, &relays, r);
1687
		stat_increment("mta.relay", 1);
1688
	} else {
1689
		mta_domain_unref(key.domain); /* from here */
1690
	}
1691
1692
	r->refcount++;
1693
	return (r);
1694
}
1695
1696
static void
1697
mta_relay_ref(struct mta_relay *r)
1698
{
1699
	r->refcount++;
1700
}
1701
1702
static void
1703
mta_relay_unref(struct mta_relay *relay)
1704
{
1705
	struct mta_connector	*c;
1706
1707
	if (--relay->refcount)
1708
		return;
1709
1710
	/* Make sure they are no envelopes held for this relay */
1711
	if (relay->state & RELAY_HOLDQ) {
1712
		m_create(p_queue, IMSG_MTA_HOLDQ_RELEASE, 0, 0, -1);
1713
		m_add_id(p_queue, relay->id);
1714
		m_add_int(p_queue, 0);
1715
		m_close(p_queue);
1716
	}
1717
1718
	log_debug("debug: mta: freeing %s", mta_relay_to_text(relay));
1719
	SPLAY_REMOVE(mta_relay_tree, &relays, relay);
1720
1721
	while ((tree_poproot(&relay->connectors, NULL, (void**)&c)))
1722
		mta_connector_free(c);
1723
1724
	free(relay->authlabel);
1725
	free(relay->authtable);
1726
	free(relay->backupname);
1727
	free(relay->pki_name);
1728
	free(relay->ca_name);
1729
	free(relay->helotable);
1730
	free(relay->heloname);
1731
	free(relay->secret);
1732
	free(relay->sourcetable);
1733
1734
	mta_domain_unref(relay->domain); /* from constructor */
1735
	free(relay);
1736
	stat_decrement("mta.relay", 1);
1737
}
1738
1739
const char *
1740
mta_relay_to_text(struct mta_relay *relay)
1741
{
1742
	static char	 buf[1024];
1743
	char		 tmp[32];
1744
	const char	*sep = ",";
1745
1746
	(void)snprintf(buf, sizeof buf, "[relay:%s", relay->domain->name);
1747
1748
	if (relay->port) {
1749
		(void)strlcat(buf, sep, sizeof buf);
1750
		(void)snprintf(tmp, sizeof tmp, "port=%d", (int)relay->port);
1751
		(void)strlcat(buf, tmp, sizeof buf);
1752
	}
1753
1754
	if (relay->flags & RELAY_STARTTLS) {
1755
		(void)strlcat(buf, sep, sizeof buf);
1756
		(void)strlcat(buf, "starttls", sizeof buf);
1757
	}
1758
1759
	if (relay->flags & RELAY_SMTPS) {
1760
		(void)strlcat(buf, sep, sizeof buf);
1761
		(void)strlcat(buf, "smtps", sizeof buf);
1762
	}
1763
1764
	if (relay->flags & RELAY_AUTH) {
1765
		(void)strlcat(buf, sep, sizeof buf);
1766
		(void)strlcat(buf, "auth=", sizeof buf);
1767
		(void)strlcat(buf, relay->authtable, sizeof buf);
1768
		(void)strlcat(buf, ":", sizeof buf);
1769
		(void)strlcat(buf, relay->authlabel, sizeof buf);
1770
	}
1771
1772
	if (relay->pki_name) {
1773
		(void)strlcat(buf, sep, sizeof buf);
1774
		(void)strlcat(buf, "pki_name=", sizeof buf);
1775
		(void)strlcat(buf, relay->pki_name, sizeof buf);
1776
	}
1777
1778
	if (relay->flags & RELAY_MX) {
1779
		(void)strlcat(buf, sep, sizeof buf);
1780
		(void)strlcat(buf, "mx", sizeof buf);
1781
	}
1782
1783
	if (relay->backupname) {
1784
		(void)strlcat(buf, sep, sizeof buf);
1785
		(void)strlcat(buf, "backup=", sizeof buf);
1786
		(void)strlcat(buf, relay->backupname, sizeof buf);
1787
	}
1788
1789
	if (relay->sourcetable) {
1790
		(void)strlcat(buf, sep, sizeof buf);
1791
		(void)strlcat(buf, "sourcetable=", sizeof buf);
1792
		(void)strlcat(buf, relay->sourcetable, sizeof buf);
1793
	}
1794
1795
	if (relay->helotable) {
1796
		(void)strlcat(buf, sep, sizeof buf);
1797
		(void)strlcat(buf, "helotable=", sizeof buf);
1798
		(void)strlcat(buf, relay->helotable, sizeof buf);
1799
	}
1800
1801
	if (relay->heloname) {
1802
		(void)strlcat(buf, sep, sizeof buf);
1803
		(void)strlcat(buf, "heloname=", sizeof buf);
1804
		(void)strlcat(buf, relay->heloname, sizeof buf);
1805
	}
1806
1807
	(void)strlcat(buf, "]", sizeof buf);
1808
1809
	return (buf);
1810
}
1811
1812
static void
1813
mta_relay_show(struct mta_relay *r, struct mproc *p, uint32_t id, time_t t)
1814
{
1815
	struct mta_connector	*c;
1816
	void			*iter;
1817
	char			 buf[1024], flags[1024], dur[64];
1818
	time_t			 to;
1819
1820
	flags[0] = '\0';
1821
1822
#define SHOWSTATUS(f, n) do {							\
1823
		if (r->status & (f)) {						\
1824
			if (flags[0])						\
1825
				(void)strlcat(flags, ",", sizeof(flags));	\
1826
			(void)strlcat(flags, (n), sizeof(flags));		\
1827
		}								\
1828
	} while(0)
1829
1830
	SHOWSTATUS(RELAY_WAIT_MX, "MX");
1831
	SHOWSTATUS(RELAY_WAIT_PREFERENCE, "preference");
1832
	SHOWSTATUS(RELAY_WAIT_SECRET, "secret");
1833
	SHOWSTATUS(RELAY_WAIT_LIMITS, "limits");
1834
	SHOWSTATUS(RELAY_WAIT_SOURCE, "source");
1835
	SHOWSTATUS(RELAY_WAIT_CONNECTOR, "connector");
1836
#undef SHOWSTATUS
1837
1838
	if (runq_pending(runq_relay, NULL, r, &to))
1839
		(void)snprintf(dur, sizeof(dur), "%s", duration_to_text(to - t));
1840
	else
1841
		(void)strlcpy(dur, "-", sizeof(dur));
1842
1843
	(void)snprintf(buf, sizeof(buf), "%s refcount=%d ntask=%zu nconn=%zu lastconn=%s timeout=%s wait=%s%s",
1844
	    mta_relay_to_text(r),
1845
	    r->refcount,
1846
	    r->ntask,
1847
	    r->nconn,
1848
	    r->lastconn ? duration_to_text(t - r->lastconn) : "-",
1849
	    dur,
1850
	    flags,
1851
	    (r->state & RELAY_ONHOLD) ? "ONHOLD" : "");
1852
	m_compose(p, IMSG_CTL_MTA_SHOW_RELAYS, id, 0, -1, buf, strlen(buf) + 1);
1853
1854
	iter = NULL;
1855
	while (tree_iter(&r->connectors, &iter, NULL, (void **)&c)) {
1856
1857
		if (runq_pending(runq_connector, NULL, c, &to))
1858
			(void)snprintf(dur, sizeof(dur), "%s", duration_to_text(to - t));
1859
		else
1860
			(void)strlcpy(dur, "-", sizeof(dur));
1861
1862
		flags[0] = '\0';
1863
1864
#define SHOWFLAG(f, n) do {							\
1865
		if (c->flags & (f)) {						\
1866
			if (flags[0])						\
1867
				(void)strlcat(flags, ",", sizeof(flags));	\
1868
			(void)strlcat(flags, (n), sizeof(flags));		\
1869
		}								\
1870
	} while(0)
1871
1872
		SHOWFLAG(CONNECTOR_NEW,		"NEW");
1873
		SHOWFLAG(CONNECTOR_WAIT,	"WAIT");
1874
1875
		SHOWFLAG(CONNECTOR_ERROR_FAMILY,	"ERROR_FAMILY");
1876
		SHOWFLAG(CONNECTOR_ERROR_SOURCE,	"ERROR_SOURCE");
1877
		SHOWFLAG(CONNECTOR_ERROR_MX,		"ERROR_MX");
1878
		SHOWFLAG(CONNECTOR_ERROR_ROUTE_NET,	"ERROR_ROUTE_NET");
1879
		SHOWFLAG(CONNECTOR_ERROR_ROUTE_SMTP,	"ERROR_ROUTE_SMTP");
1880
		SHOWFLAG(CONNECTOR_ERROR_BLOCKED,	"ERROR_BLOCKED");
1881
1882
		SHOWFLAG(CONNECTOR_LIMIT_HOST,		"LIMIT_HOST");
1883
		SHOWFLAG(CONNECTOR_LIMIT_ROUTE,		"LIMIT_ROUTE");
1884
		SHOWFLAG(CONNECTOR_LIMIT_SOURCE,	"LIMIT_SOURCE");
1885
		SHOWFLAG(CONNECTOR_LIMIT_RELAY,		"LIMIT_RELAY");
1886
		SHOWFLAG(CONNECTOR_LIMIT_CONN,		"LIMIT_CONN");
1887
		SHOWFLAG(CONNECTOR_LIMIT_DOMAIN,	"LIMIT_DOMAIN");
1888
#undef SHOWFLAG
1889
1890
		(void)snprintf(buf, sizeof(buf),
1891
		    "  connector %s refcount=%d nconn=%zu lastconn=%s timeout=%s flags=%s",
1892
		    mta_source_to_text(c->source),
1893
		    c->refcount,
1894
		    c->nconn,
1895
		    c->lastconn ? duration_to_text(t - c->lastconn) : "-",
1896
		    dur,
1897
		    flags);
1898
		m_compose(p, IMSG_CTL_MTA_SHOW_RELAYS, id, 0, -1, buf,
1899
		    strlen(buf) + 1);
1900
1901
1902
	}
1903
}
1904
1905
static int
1906
mta_relay_cmp(const struct mta_relay *a, const struct mta_relay *b)
1907
{
1908
	int	r;
1909
1910
	if (a->domain < b->domain)
1911
		return (-1);
1912
	if (a->domain > b->domain)
1913
		return (1);
1914
1915
	if (a->flags < b->flags)
1916
		return (-1);
1917
	if (a->flags > b->flags)
1918
		return (1);
1919
1920
	if (a->port < b->port)
1921
		return (-1);
1922
	if (a->port > b->port)
1923
		return (1);
1924
1925
	if (a->authtable == NULL && b->authtable)
1926
		return (-1);
1927
	if (a->authtable && b->authtable == NULL)
1928
		return (1);
1929
	if (a->authtable && ((r = strcmp(a->authtable, b->authtable))))
1930
		return (r);
1931
	if (a->authlabel && ((r = strcmp(a->authlabel, b->authlabel))))
1932
		return (r);
1933
	if (a->sourcetable == NULL && b->sourcetable)
1934
		return (-1);
1935
	if (a->sourcetable && b->sourcetable == NULL)
1936
		return (1);
1937
	if (a->sourcetable && ((r = strcmp(a->sourcetable, b->sourcetable))))
1938
		return (r);
1939
	if (a->helotable == NULL && b->helotable)
1940
		return (-1);
1941
	if (a->helotable && b->helotable == NULL)
1942
		return (1);
1943
	if (a->helotable && ((r = strcmp(a->helotable, b->helotable))))
1944
		return (r);
1945
	if (a->heloname == NULL && b->heloname)
1946
		return (-1);
1947
	if (a->heloname && b->heloname == NULL)
1948
		return (1);
1949
	if (a->heloname && ((r = strcmp(a->heloname, b->heloname))))
1950
		return (r);
1951
1952
	if (a->pki_name == NULL && b->pki_name)
1953
		return (-1);
1954
	if (a->pki_name && b->pki_name == NULL)
1955
		return (1);
1956
	if (a->pki_name && ((r = strcmp(a->pki_name, b->pki_name))))
1957
		return (r);
1958
1959
	if (a->ca_name == NULL && b->ca_name)
1960
		return (-1);
1961
	if (a->ca_name && b->ca_name == NULL)
1962
		return (1);
1963
	if (a->ca_name && ((r = strcmp(a->ca_name, b->ca_name))))
1964
		return (r);
1965
1966
	if (a->backupname && ((r = strcmp(a->backupname, b->backupname))))
1967
		return (r);
1968
1969
	return (0);
1970
}
1971
1972
SPLAY_GENERATE(mta_relay_tree, mta_relay, entry, mta_relay_cmp);
1973
1974
static struct mta_host *
1975
mta_host(const struct sockaddr *sa)
1976
{
1977
	struct mta_host		key, *h;
1978
	struct sockaddr_storage	ss;
1979
1980
	memmove(&ss, sa, sa->sa_len);
1981
	key.sa = (struct sockaddr*)&ss;
1982
	h = SPLAY_FIND(mta_host_tree, &hosts, &key);
1983
1984
	if (h == NULL) {
1985
		h = xcalloc(1, sizeof(*h), "mta_host");
1986
		h->sa = xmemdup(sa, sa->sa_len, "mta_host");
1987
		SPLAY_INSERT(mta_host_tree, &hosts, h);
1988
		stat_increment("mta.host", 1);
1989
	}
1990
1991
	h->refcount++;
1992
	return (h);
1993
}
1994
1995
static void
1996
mta_host_ref(struct mta_host *h)
1997
{
1998
	h->refcount++;
1999
}
2000
2001
static void
2002
mta_host_unref(struct mta_host *h)
2003
{
2004
	if (--h->refcount)
2005
		return;
2006
2007
	SPLAY_REMOVE(mta_host_tree, &hosts, h);
2008
	free(h->sa);
2009
	free(h->ptrname);
2010
	free(h);
2011
	stat_decrement("mta.host", 1);
2012
}
2013
2014
const char *
2015
mta_host_to_text(struct mta_host *h)
2016
{
2017
	static char buf[1024];
2018
2019
	if (h->ptrname)
2020
		(void)snprintf(buf, sizeof buf, "%s (%s)",
2021
		    sa_to_text(h->sa), h->ptrname);
2022
	else
2023
		(void)snprintf(buf, sizeof buf, "%s", sa_to_text(h->sa));
2024
2025
	return (buf);
2026
}
2027
2028
static int
2029
mta_host_cmp(const struct mta_host *a, const struct mta_host *b)
2030
{
2031
	if (a->sa->sa_len < b->sa->sa_len)
2032
		return (-1);
2033
	if (a->sa->sa_len > b->sa->sa_len)
2034
		return (1);
2035
	return (memcmp(a->sa, b->sa, a->sa->sa_len));
2036
}
2037
2038
SPLAY_GENERATE(mta_host_tree, mta_host, entry, mta_host_cmp);
2039
2040
static struct mta_domain *
2041
mta_domain(char *name, int flags)
2042
{
2043
	struct mta_domain	key, *d;
2044
2045
	key.name = name;
2046
	key.flags = flags;
2047
	d = SPLAY_FIND(mta_domain_tree, &domains, &key);
2048
2049
	if (d == NULL) {
2050
		d = xcalloc(1, sizeof(*d), "mta_domain");
2051
		d->name = xstrdup(name, "mta_domain");
2052
		d->flags = flags;
2053
		TAILQ_INIT(&d->mxs);
2054
		SPLAY_INSERT(mta_domain_tree, &domains, d);
2055
		stat_increment("mta.domain", 1);
2056
	}
2057
2058
	d->refcount++;
2059
	return (d);
2060
}
2061
2062
#if 0
2063
static void
2064
mta_domain_ref(struct mta_domain *d)
2065
{
2066
	d->refcount++;
2067
}
2068
#endif
2069
2070
static void
2071
mta_domain_unref(struct mta_domain *d)
2072
{
2073
	struct mta_mx	*mx;
2074
2075
	if (--d->refcount)
2076
		return;
2077
2078
	while ((mx = TAILQ_FIRST(&d->mxs))) {
2079
		TAILQ_REMOVE(&d->mxs, mx, entry);
2080
		mta_host_unref(mx->host); /* from IMSG_DNS_HOST */
2081
		free(mx);
2082
	}
2083
2084
	SPLAY_REMOVE(mta_domain_tree, &domains, d);
2085
	free(d->name);
2086
	free(d);
2087
	stat_decrement("mta.domain", 1);
2088
}
2089
2090
static int
2091
mta_domain_cmp(const struct mta_domain *a, const struct mta_domain *b)
2092
{
2093
	if (a->flags < b->flags)
2094
		return (-1);
2095
	if (a->flags > b->flags)
2096
		return (1);
2097
	return (strcasecmp(a->name, b->name));
2098
}
2099
2100
SPLAY_GENERATE(mta_domain_tree, mta_domain, entry, mta_domain_cmp);
2101
2102
static struct mta_source *
2103
mta_source(const struct sockaddr *sa)
2104
{
2105
	struct mta_source	key, *s;
2106
	struct sockaddr_storage	ss;
2107
2108
	if (sa) {
2109
		memmove(&ss, sa, sa->sa_len);
2110
		key.sa = (struct sockaddr*)&ss;
2111
	} else
2112
		key.sa = NULL;
2113
	s = SPLAY_FIND(mta_source_tree, &sources, &key);
2114
2115
	if (s == NULL) {
2116
		s = xcalloc(1, sizeof(*s), "mta_source");
2117
		if (sa)
2118
			s->sa = xmemdup(sa, sa->sa_len, "mta_source");
2119
		SPLAY_INSERT(mta_source_tree, &sources, s);
2120
		stat_increment("mta.source", 1);
2121
	}
2122
2123
	s->refcount++;
2124
	return (s);
2125
}
2126
2127
static void
2128
mta_source_ref(struct mta_source *s)
2129
{
2130
	s->refcount++;
2131
}
2132
2133
static void
2134
mta_source_unref(struct mta_source *s)
2135
{
2136
	if (--s->refcount)
2137
		return;
2138
2139
	SPLAY_REMOVE(mta_source_tree, &sources, s);
2140
	free(s->sa);
2141
	free(s);
2142
	stat_decrement("mta.source", 1);
2143
}
2144
2145
static const char *
2146
mta_source_to_text(struct mta_source *s)
2147
{
2148
	static char buf[1024];
2149
2150
	if (s->sa == NULL)
2151
		return "[]";
2152
	(void)snprintf(buf, sizeof buf, "%s", sa_to_text(s->sa));
2153
	return (buf);
2154
}
2155
2156
static int
2157
mta_source_cmp(const struct mta_source *a, const struct mta_source *b)
2158
{
2159
	if (a->sa == NULL)
2160
		return ((b->sa == NULL) ? 0 : -1);
2161
	if (b->sa == NULL)
2162
		return (1);
2163
	if (a->sa->sa_len < b->sa->sa_len)
2164
		return (-1);
2165
	if (a->sa->sa_len > b->sa->sa_len)
2166
		return (1);
2167
	return (memcmp(a->sa, b->sa, a->sa->sa_len));
2168
}
2169
2170
SPLAY_GENERATE(mta_source_tree, mta_source, entry, mta_source_cmp);
2171
2172
static struct mta_connector *
2173
mta_connector(struct mta_relay *relay, struct mta_source *source)
2174
{
2175
	struct mta_connector	*c;
2176
2177
	c = tree_get(&relay->connectors, (uintptr_t)(source));
2178
	if (c == NULL) {
2179
		c = xcalloc(1, sizeof(*c), "mta_connector");
2180
		c->relay = relay;
2181
		c->source = source;
2182
		c->flags |= CONNECTOR_NEW;
2183
		mta_source_ref(source);
2184
		tree_xset(&relay->connectors, (uintptr_t)(source), c);
2185
		stat_increment("mta.connector", 1);
2186
		log_debug("debug: mta: new %s", mta_connector_to_text(c));
2187
	}
2188
2189
	return (c);
2190
}
2191
2192
static void
2193
mta_connector_free(struct mta_connector *c)
2194
{
2195
	log_debug("debug: mta: freeing %s",
2196
	    mta_connector_to_text(c));
2197
2198
	if (c->flags & CONNECTOR_WAIT) {
2199
		log_debug("debug: mta: cancelling timeout for %s",
2200
		    mta_connector_to_text(c));
2201
		runq_cancel(runq_connector, NULL, c);
2202
	}
2203
	mta_source_unref(c->source); /* from constructor */
2204
	free(c);
2205
2206
	stat_decrement("mta.connector", 1);
2207
}
2208
2209
static const char *
2210
mta_connector_to_text(struct mta_connector *c)
2211
{
2212
	static char buf[1024];
2213
2214
	(void)snprintf(buf, sizeof buf, "[connector:%s->%s,0x%x]",
2215
	    mta_source_to_text(c->source),
2216
	    mta_relay_to_text(c->relay),
2217
	    c->flags);
2218
	return (buf);
2219
}
2220
2221
static struct mta_route *
2222
mta_route(struct mta_source *src, struct mta_host *dst)
2223
{
2224
	struct mta_route	key, *r;
2225
	static uint64_t		rid = 0;
2226
2227
	key.src = src;
2228
	key.dst = dst;
2229
	r = SPLAY_FIND(mta_route_tree, &routes, &key);
2230
2231
	if (r == NULL) {
2232
		r = xcalloc(1, sizeof(*r), "mta_route");
2233
		r->src = src;
2234
		r->dst = dst;
2235
		r->flags |= ROUTE_NEW;
2236
		r->id = ++rid;
2237
		SPLAY_INSERT(mta_route_tree, &routes, r);
2238
		mta_source_ref(src);
2239
		mta_host_ref(dst);
2240
		stat_increment("mta.route", 1);
2241
	}
2242
	else if (r->flags & ROUTE_RUNQ) {
2243
		log_debug("debug: mta: mta_route_ref(): cancelling runq for route %s",
2244
		    mta_route_to_text(r));
2245
		r->flags &= ~(ROUTE_RUNQ | ROUTE_KEEPALIVE);
2246
		runq_cancel(runq_route, NULL, r);
2247
		r->refcount--; /* from mta_route_unref() */
2248
	}
2249
2250
	r->refcount++;
2251
	return (r);
2252
}
2253
2254
static void
2255
mta_route_ref(struct mta_route *r)
2256
{
2257
	r->refcount++;
2258
}
2259
2260
static void
2261
mta_route_unref(struct mta_route *r)
2262
{
2263
	time_t	sched, now;
2264
	int	delay;
2265
2266
	if (--r->refcount)
2267
		return;
2268
2269
	/*
2270
	 * Nothing references this route, but we might want to keep it alive
2271
	 * for a while.
2272
	 */
2273
	now = time(NULL);
2274
	sched = 0;
2275
2276
	if (r->penalty) {
2277
#if DELAY_QUADRATIC
2278
		delay = DELAY_ROUTE_BASE * r->penalty * r->penalty;
2279
#else
2280
		delay = 15 * 60;
2281
#endif
2282
		if (delay > DELAY_ROUTE_MAX)
2283
			delay = DELAY_ROUTE_MAX;
2284
		sched = r->lastpenalty + delay;
2285
		log_debug("debug: mta: mta_route_unref(): keeping route %s alive for %llus (penalty %d)",
2286
		    mta_route_to_text(r), (unsigned long long) sched - now, r->penalty);
2287
	} else if (!(r->flags & ROUTE_KEEPALIVE)) {
2288
		if (r->lastconn + max_seen_conndelay_route > now)
2289
			sched = r->lastconn + max_seen_conndelay_route;
2290
		if (r->lastdisc + max_seen_discdelay_route > now &&
2291
		    r->lastdisc + max_seen_discdelay_route < sched)
2292
			sched = r->lastdisc + max_seen_discdelay_route;
2293
2294
		if (sched > now)
2295
			log_debug("debug: mta: mta_route_unref(): keeping route %s alive for %llus (imposed delay)",
2296
			    mta_route_to_text(r), (unsigned long long) sched - now);
2297
	}
2298
2299
	if (sched > now) {
2300
		r->flags |= ROUTE_RUNQ;
2301
		runq_schedule(runq_route, sched, NULL, r);
2302
		r->refcount++;
2303
		return;
2304
	}
2305
2306
	log_debug("debug: mta: mta_route_unref(): really discarding route %s",
2307
	    mta_route_to_text(r));
2308
2309
	SPLAY_REMOVE(mta_route_tree, &routes, r);
2310
	mta_source_unref(r->src); /* from constructor */
2311
	mta_host_unref(r->dst); /* from constructor */
2312
	free(r);
2313
	stat_decrement("mta.route", 1);
2314
}
2315
2316
static const char *
2317
mta_route_to_text(struct mta_route *r)
2318
{
2319
	static char	buf[1024];
2320
2321
	(void)snprintf(buf, sizeof buf, "%s <-> %s",
2322
	    mta_source_to_text(r->src),
2323
	    mta_host_to_text(r->dst));
2324
2325
	return (buf);
2326
}
2327
2328
static int
2329
mta_route_cmp(const struct mta_route *a, const struct mta_route *b)
2330
{
2331
	if (a->src < b->src)
2332
		return (-1);
2333
	if (a->src > b->src)
2334
		return (1);
2335
2336
	if (a->dst < b->dst)
2337
		return (-1);
2338
	if (a->dst > b->dst)
2339
		return (1);
2340
2341
	return (0);
2342
}
2343
2344
SPLAY_GENERATE(mta_route_tree, mta_route, entry, mta_route_cmp);
2345
2346
void
2347
mta_block(struct mta_source *src, char *dom)
2348
{
2349
	struct mta_block key, *b;
2350
2351
	key.source = src;
2352
	key.domain = dom;
2353
2354
	b = SPLAY_FIND(mta_block_tree, &blocks, &key);
2355
	if (b != NULL)
2356
		return;
2357
2358
	b = xcalloc(1, sizeof(*b), "mta_block");
2359
	if (dom)
2360
		b->domain = xstrdup(dom, "mta_block");
2361
	b->source = src;
2362
	mta_source_ref(src);
2363
	SPLAY_INSERT(mta_block_tree, &blocks, b);
2364
}
2365
2366
void
2367
mta_unblock(struct mta_source *src, char *dom)
2368
{
2369
	struct mta_block key, *b;
2370
2371
	key.source = src;
2372
	key.domain = dom;
2373
2374
	b = SPLAY_FIND(mta_block_tree, &blocks, &key);
2375
	if (b == NULL)
2376
		return;
2377
2378
	SPLAY_REMOVE(mta_block_tree, &blocks, b);
2379
2380
	mta_source_unref(b->source);
2381
	free(b->domain);
2382
	free(b);
2383
}
2384
2385
int
2386
mta_is_blocked(struct mta_source *src, char *dom)
2387
{
2388
	struct mta_block key;
2389
2390
	key.source = src;
2391
	key.domain = dom;
2392
2393
	if (SPLAY_FIND(mta_block_tree, &blocks, &key))
2394
		return (1);
2395
2396
	return (0);
2397
}
2398
2399
static
2400
int
2401
mta_block_cmp(const struct mta_block *a, const struct mta_block *b)
2402
{
2403
	if (a->source < b->source)
2404
		return (-1);
2405
	if (a->source > b->source)
2406
		return (1);
2407
	if (!a->domain && b->domain)
2408
		return (-1);
2409
	if (a->domain && !b->domain)
2410
		return (1);
2411
	if (a->domain == b->domain)
2412
		return (0);
2413
	return (strcasecmp(a->domain, b->domain));
2414
}
2415
2416
SPLAY_GENERATE(mta_block_tree, mta_block, entry, mta_block_cmp);
2417
2418
2419
2420
/* hoststat errors are not critical, we do best effort */
2421
void
2422
mta_hoststat_update(const char *host, const char *error)
2423
{
2424
	struct hoststat	*hs = NULL;
2425
	char		 buf[HOST_NAME_MAX+1];
2426
	time_t		 tm;
2427
2428
	if (!lowercase(buf, host, sizeof buf))
2429
		return;
2430
2431
	tm = time(NULL);
2432
	hs = dict_get(&hoststat, buf);
2433
	if (hs == NULL) {
2434
		if ((hs = calloc(1, sizeof *hs)) == NULL)
2435
			return;
2436
		tree_init(&hs->deferred);
2437
		runq_schedule(runq_hoststat, tm+HOSTSTAT_EXPIRE_DELAY, NULL, hs);
2438
	}
2439
	(void)strlcpy(hs->name, buf, sizeof hs->name);
2440
	(void)strlcpy(hs->error, error, sizeof hs->error);
2441
	hs->tm = time(NULL);
2442
	dict_set(&hoststat, buf, hs);
2443
2444
	runq_cancel(runq_hoststat, NULL, hs);
2445
	runq_schedule(runq_hoststat, tm+HOSTSTAT_EXPIRE_DELAY, NULL, hs);
2446
}
2447
2448
void
2449
mta_hoststat_cache(const char *host, uint64_t evpid)
2450
{
2451
	struct hoststat	*hs = NULL;
2452
	char buf[HOST_NAME_MAX+1];
2453
2454
	if (!lowercase(buf, host, sizeof buf))
2455
		return;
2456
2457
	hs = dict_get(&hoststat, buf);
2458
	if (hs == NULL)
2459
		return;
2460
2461
	if (tree_count(&hs->deferred) >= env->sc_mta_max_deferred)
2462
		return;
2463
2464
	tree_set(&hs->deferred, evpid, NULL);
2465
}
2466
2467
void
2468
mta_hoststat_uncache(const char *host, uint64_t evpid)
2469
{
2470
	struct hoststat	*hs = NULL;
2471
	char buf[HOST_NAME_MAX+1];
2472
2473
	if (!lowercase(buf, host, sizeof buf))
2474
		return;
2475
2476
	hs = dict_get(&hoststat, buf);
2477
	if (hs == NULL)
2478
		return;
2479
2480
	tree_pop(&hs->deferred, evpid);
2481
}
2482
2483
void
2484
mta_hoststat_reschedule(const char *host)
2485
{
2486
	struct hoststat	*hs = NULL;
2487
	char		 buf[HOST_NAME_MAX+1];
2488
	uint64_t	 evpid;
2489
2490
	if (!lowercase(buf, host, sizeof buf))
2491
		return;
2492
2493
	hs = dict_get(&hoststat, buf);
2494
	if (hs == NULL)
2495
		return;
2496
2497
	while (tree_poproot(&hs->deferred, &evpid, NULL)) {
2498
		m_compose(p_queue, IMSG_MTA_SCHEDULE, 0, 0, -1,
2499
		    &evpid, sizeof evpid);
2500
	}
2501
}
2502
2503
static void
2504
mta_hoststat_remove_entry(struct hoststat *hs)
2505
{
2506
	while (tree_poproot(&hs->deferred, NULL, NULL))
2507
		;
2508
	dict_pop(&hoststat, hs->name);
2509
	runq_cancel(runq_hoststat, NULL, hs);
2510
}