summaryrefslogtreecommitdiff
path: root/usr.sbin/smtpd/queue.c
blob: e79e3f06be4fdf53e87596a6e10aa79fbe0ffde8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
/*	$OpenBSD: queue.c,v 1.194 2021/08/02 12:33:34 eric Exp $	*/

/*
 * Copyright (c) 2008 Gilles Chehade <gilles@poolp.org>
 * Copyright (c) 2008 Pierre-Yves Ritschard <pyr@openbsd.org>
 * Copyright (c) 2012 Eric Faurot <eric@openbsd.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <inttypes.h>
#include <pwd.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "smtpd.h"
#include "log.h"

static void queue_imsg(struct mproc *, struct imsg *);
static void queue_timeout(int, short, void *);
static void queue_bounce(struct envelope *, struct delivery_bounce *);
static void queue_shutdown(void);
static void queue_log(const struct envelope *, const char *, const char *);
static void queue_msgid_walk(int, short, void *);


static void
queue_imsg(struct mproc *p, struct imsg *imsg)
{
	struct delivery_bounce	 bounce;
	struct msg_walkinfo	*wi;
	struct timeval		 tv;
	struct bounce_req_msg	*req_bounce;
	struct envelope		 evp;
	struct msg		 m;
	const char		*reason;
	uint64_t		 reqid, evpid, holdq;
	uint32_t		 msgid;
	time_t			 nexttry;
	size_t			 n_evp;
	int			 fd, mta_ext, ret, v, flags, code;

	if (imsg == NULL)
		queue_shutdown();

	memset(&bounce, 0, sizeof(struct delivery_bounce));

	switch (imsg->hdr.type) {
	case IMSG_SMTP_MESSAGE_CREATE:
		m_msg(&m, imsg);
		m_get_id(&m, &reqid);
		m_end(&m);

		ret = queue_message_create(&msgid);

		m_create(p, IMSG_SMTP_MESSAGE_CREATE, 0, 0, -1);
		m_add_id(p, reqid);
		if (ret == 0)
			m_add_int(p, 0);
		else {
			m_add_int(p, 1);
			m_add_msgid(p, msgid);
		}
		m_close(p);
		return;

	case IMSG_SMTP_MESSAGE_ROLLBACK:
		m_msg(&m, imsg);
		m_get_msgid(&m, &msgid);
		m_end(&m);

		queue_message_delete(msgid);

		m_create(p_scheduler, IMSG_QUEUE_MESSAGE_ROLLBACK,
		    0, 0, -1);
		m_add_msgid(p_scheduler, msgid);
		m_close(p_scheduler);
		return;

	case IMSG_SMTP_MESSAGE_COMMIT:
		m_msg(&m, imsg);
		m_get_id(&m, &reqid);
		m_get_msgid(&m, &msgid);
		m_end(&m);

		ret = queue_message_commit(msgid);

		m_create(p, IMSG_SMTP_MESSAGE_COMMIT, 0, 0, -1);
		m_add_id(p, reqid);
		m_add_int(p, (ret == 0) ? 0 : 1);
		m_close(p);

		if (ret) {
			m_create(p_scheduler, IMSG_QUEUE_MESSAGE_COMMIT,
			    0, 0, -1);
			m_add_msgid(p_scheduler, msgid);
			m_close(p_scheduler);
		}
		return;

	case IMSG_SMTP_MESSAGE_OPEN:
		m_msg(&m, imsg);
		m_get_id(&m, &reqid);
		m_get_msgid(&m, &msgid);
		m_end(&m);

		fd = queue_message_fd_rw(msgid);

		m_create(p, IMSG_SMTP_MESSAGE_OPEN, 0, 0, fd);
		m_add_id(p, reqid);
		m_add_int(p, (fd == -1) ? 0 : 1);
		m_close(p);
		return;

	case IMSG_QUEUE_SMTP_SESSION:
		bounce_fd(imsg->fd);
		return;

	case IMSG_LKA_ENVELOPE_SUBMIT:
		m_msg(&m, imsg);
		m_get_id(&m, &reqid);
		m_get_envelope(&m, &evp);
		m_end(&m);

		if (evp.id == 0)
			log_warnx("warn: imsg_queue_submit_envelope: evpid=0");
		if (evpid_to_msgid(evp.id) == 0)
			log_warnx("warn: imsg_queue_submit_envelope: msgid=0, "
			    "evpid=%016"PRIx64, evp.id);
		ret = queue_envelope_create(&evp);
		m_create(p_dispatcher, IMSG_QUEUE_ENVELOPE_SUBMIT, 0, 0, -1);
		m_add_id(p_dispatcher, reqid);
		if (ret == 0)
			m_add_int(p_dispatcher, 0);
		else {
			m_add_int(p_dispatcher, 1);
			m_add_evpid(p_dispatcher, evp.id);
		}
		m_close(p_dispatcher);
		if (ret) {
			m_create(p_scheduler,
			    IMSG_QUEUE_ENVELOPE_SUBMIT, 0, 0, -1);
			m_add_envelope(p_scheduler, &evp);
			m_close(p_scheduler);
		}
		return;

	case IMSG_LKA_ENVELOPE_COMMIT:
		m_msg(&m, imsg);
		m_get_id(&m, &reqid);
		m_end(&m);
		m_create(p_dispatcher, IMSG_QUEUE_ENVELOPE_COMMIT, 0, 0, -1);
		m_add_id(p_dispatcher, reqid);
		m_add_int(p_dispatcher, 1);
		m_close(p_dispatcher);
		return;

	case IMSG_SCHED_ENVELOPE_REMOVE:
		m_msg(&m, imsg);
		m_get_evpid(&m, &evpid);
		m_end(&m);

		m_create(p_scheduler, IMSG_QUEUE_ENVELOPE_ACK, 0, 0, -1);
		m_add_evpid(p_scheduler, evpid);
		m_close(p_scheduler);

		/* already removed by scheduler */
		if (queue_envelope_load(evpid, &evp) == 0)
			return;

		queue_log(&evp, "Remove", "Removed by administrator");
		queue_envelope_delete(evpid);
		return;

	case IMSG_SCHED_ENVELOPE_EXPIRE:
		m_msg(&m, imsg);
		m_get_evpid(&m, &evpid);
		m_end(&m);

		m_create(p_scheduler, IMSG_QUEUE_ENVELOPE_ACK, 0, 0, -1);
		m_add_evpid(p_scheduler, evpid);
		m_close(p_scheduler);

		/* already removed by scheduler*/
		if (queue_envelope_load(evpid, &evp) == 0)
			return;

		bounce.type = B_FAILED;
		envelope_set_errormsg(&evp, "Envelope expired");
		envelope_set_esc_class(&evp, ESC_STATUS_PERMFAIL);
		envelope_set_esc_code(&evp, ESC_DELIVERY_TIME_EXPIRED);
		queue_bounce(&evp, &bounce);
		queue_log(&evp, "Expire", "Envelope expired");
		queue_envelope_delete(evpid);
		return;

	case IMSG_SCHED_ENVELOPE_BOUNCE:
		CHECK_IMSG_DATA_SIZE(imsg, sizeof *req_bounce);
		req_bounce = imsg->data;
		evpid = req_bounce->evpid;

		if (queue_envelope_load(evpid, &evp) == 0) {
			log_warnx("queue: bounce: failed to load envelope");
			m_create(p_scheduler, IMSG_QUEUE_ENVELOPE_REMOVE, 0, 0, -1);
			m_add_evpid(p_scheduler, evpid);
			m_add_u32(p_scheduler, 0); /* not in-flight */
			m_close(p_scheduler);
			return;
		}
		queue_bounce(&evp, &req_bounce->bounce);
		evp.lastbounce = req_bounce->timestamp;
		if (!queue_envelope_update(&evp))
			log_warnx("warn: could not update envelope %016"PRIx64, evpid);
		return;

	case IMSG_SCHED_ENVELOPE_DELIVER:
		m_msg(&m, imsg);
		m_get_evpid(&m, &evpid);
		m_end(&m);
		if (queue_envelope_load(evpid, &evp) == 0) {
			log_warnx("queue: deliver: failed to load envelope");
			m_create(p_scheduler, IMSG_QUEUE_ENVELOPE_REMOVE, 0, 0, -1);
			m_add_evpid(p_scheduler, evpid);
			m_add_u32(p_scheduler, 1); /* in-flight */
			m_close(p_scheduler);
			return;
		}
		evp.lasttry = time(NULL);
		m_create(p_dispatcher, IMSG_QUEUE_DELIVER, 0, 0, -1);
		m_add_envelope(p_dispatcher, &evp);
		m_close(p_dispatcher);
		return;

	case IMSG_SCHED_ENVELOPE_INJECT:
		m_msg(&m, imsg);
		m_get_evpid(&m, &evpid);
		m_end(&m);
		bounce_add(evpid);
		return;

	case IMSG_SCHED_ENVELOPE_TRANSFER:
		m_msg(&m, imsg);
		m_get_evpid(&m, &evpid);
		m_end(&m);
		if (queue_envelope_load(evpid, &evp) == 0) {
			log_warnx("queue: failed to load envelope");
			m_create(p_scheduler, IMSG_QUEUE_ENVELOPE_REMOVE, 0, 0, -1);
			m_add_evpid(p_scheduler, evpid);
			m_add_u32(p_scheduler, 1); /* in-flight */
			m_close(p_scheduler);
			return;
		}
		evp.lasttry = time(NULL);
		m_create(p_dispatcher, IMSG_QUEUE_TRANSFER, 0, 0, -1);
		m_add_envelope(p_dispatcher, &evp);
		m_close(p_dispatcher);
		return;

	case IMSG_CTL_LIST_ENVELOPES:
		if (imsg->hdr.len == sizeof imsg->hdr) {
			m_forward(p_control, imsg);
			return;
		}

		m_msg(&m, imsg);
		m_get_evpid(&m, &evpid);
		m_get_int(&m, &flags);
		m_get_time(&m, &nexttry);
		m_end(&m);

		if (queue_envelope_load(evpid, &evp) == 0)
			return; /* Envelope is gone, drop it */

		/*
		 * XXX consistency: The envelope might already be on
		 * its way back to the scheduler.  We need to detect
		 * this properly and report that state.
		 */
		if (flags & EF_INFLIGHT) {
			/*
			 * Not exactly correct but pretty close: The
			 * value is not recorded on the envelope unless
			 * a tempfail occurs.
			 */
			evp.lasttry = nexttry;
		}

		m_create(p_control, IMSG_CTL_LIST_ENVELOPES,
		    imsg->hdr.peerid, 0, -1);
		m_add_int(p_control, flags);
		m_add_time(p_control, nexttry);
		m_add_envelope(p_control, &evp);
		m_close(p_control);
		return;

	case IMSG_MDA_OPEN_MESSAGE:
	case IMSG_MTA_OPEN_MESSAGE:
		m_msg(&m, imsg);
		m_get_id(&m, &reqid);
		m_get_msgid(&m, &msgid);
		m_end(&m);
		fd = queue_message_fd_r(msgid);
		m_create(p, imsg->hdr.type, 0, 0, fd);
		m_add_id(p, reqid);
		m_close(p);
		return;

	case IMSG_MDA_DELIVERY_OK:
	case IMSG_MTA_DELIVERY_OK:
		m_msg(&m, imsg);
		m_get_evpid(&m, &evpid);
		if (imsg->hdr.type == IMSG_MTA_DELIVERY_OK)
			m_get_int(&m, &mta_ext);
		m_end(&m);
		if (queue_envelope_load(evpid, &evp) == 0) {
			log_warn("queue: dsn: failed to load envelope");
			return;
		}
		if (evp.dsn_notify & DSN_SUCCESS) {
			bounce.type = B_DELIVERED;
			bounce.dsn_ret = evp.dsn_ret;
			envelope_set_esc_class(&evp, ESC_STATUS_OK);
			if (imsg->hdr.type == IMSG_MDA_DELIVERY_OK)
				queue_bounce(&evp, &bounce);
			else if (imsg->hdr.type == IMSG_MTA_DELIVERY_OK &&
			    (mta_ext & MTA_EXT_DSN) == 0) {
				bounce.mta_without_dsn = 1;
				queue_bounce(&evp, &bounce);
			}
		}
		queue_envelope_delete(evpid);
		m_create(p_scheduler, IMSG_QUEUE_DELIVERY_OK, 0, 0, -1);
		m_add_evpid(p_scheduler, evpid);
		m_close(p_scheduler);
		return;

	case IMSG_MDA_DELIVERY_TEMPFAIL:
	case IMSG_MTA_DELIVERY_TEMPFAIL:
		m_msg(&m, imsg);
		m_get_evpid(&m, &evpid);
		m_get_string(&m, &reason);
		m_get_int(&m, &code);
		m_end(&m);
		if (queue_envelope_load(evpid, &evp) == 0) {
			log_warnx("queue: tempfail: failed to load envelope");
			m_create(p_scheduler, IMSG_QUEUE_ENVELOPE_REMOVE, 0, 0, -1);
			m_add_evpid(p_scheduler, evpid);
			m_add_u32(p_scheduler, 1); /* in-flight */
			m_close(p_scheduler);
			return;
		}
		envelope_set_errormsg(&evp, "%s", reason);
		envelope_set_esc_class(&evp, ESC_STATUS_TEMPFAIL);
		envelope_set_esc_code(&evp, code);
		evp.retry++;
		if (!queue_envelope_update(&evp))
			log_warnx("warn: could not update envelope %016"PRIx64, evpid);
		m_create(p_scheduler, IMSG_QUEUE_DELIVERY_TEMPFAIL, 0, 0, -1);
		m_add_envelope(p_scheduler, &evp);
		m_close(p_scheduler);
		return;

	case IMSG_MDA_DELIVERY_PERMFAIL:
	case IMSG_MTA_DELIVERY_PERMFAIL:
		m_msg(&m, imsg);
		m_get_evpid(&m, &evpid);
		m_get_string(&m, &reason);
		m_get_int(&m, &code);
		m_end(&m);
		if (queue_envelope_load(evpid, &evp) == 0) {
			log_warnx("queue: permfail: failed to load envelope");
			m_create(p_scheduler, IMSG_QUEUE_ENVELOPE_REMOVE, 0, 0, -1);
			m_add_evpid(p_scheduler, evpid);
			m_add_u32(p_scheduler, 1); /* in-flight */
			m_close(p_scheduler);
			return;
		}
		bounce.type = B_FAILED;
		envelope_set_errormsg(&evp, "%s", reason);
		envelope_set_esc_class(&evp, ESC_STATUS_PERMFAIL);
		envelope_set_esc_code(&evp, code);
		queue_bounce(&evp, &bounce);
		queue_envelope_delete(evpid);
		m_create(p_scheduler, IMSG_QUEUE_DELIVERY_PERMFAIL, 0, 0, -1);
		m_add_evpid(p_scheduler, evpid);
		m_close(p_scheduler);
		return;

	case IMSG_MDA_DELIVERY_LOOP:
	case IMSG_MTA_DELIVERY_LOOP:
		m_msg(&m, imsg);
		m_get_evpid(&m, &evpid);
		m_end(&m);
		if (queue_envelope_load(evpid, &evp) == 0) {
			log_warnx("queue: loop: failed to load envelope");
			m_create(p_scheduler, IMSG_QUEUE_ENVELOPE_REMOVE, 0, 0, -1);
			m_add_evpid(p_scheduler, evpid);
			m_add_u32(p_scheduler, 1); /* in-flight */
			m_close(p_scheduler);
			return;
		}
		envelope_set_errormsg(&evp, "%s", "Loop detected");
		envelope_set_esc_class(&evp, ESC_STATUS_TEMPFAIL);
		envelope_set_esc_code(&evp, ESC_ROUTING_LOOP_DETECTED);
		bounce.type = B_FAILED;
		queue_bounce(&evp, &bounce);
		queue_envelope_delete(evp.id);
		m_create(p_scheduler, IMSG_QUEUE_DELIVERY_LOOP, 0, 0, -1);
		m_add_evpid(p_scheduler, evp.id);
		m_close(p_scheduler);
		return;

	case IMSG_MTA_DELIVERY_HOLD:
	case IMSG_MDA_DELIVERY_HOLD:
		imsg->hdr.type = IMSG_QUEUE_HOLDQ_HOLD;
		m_forward(p_scheduler, imsg);
		return;

	case IMSG_MTA_SCHEDULE:
		imsg->hdr.type = IMSG_QUEUE_ENVELOPE_SCHEDULE;
		m_forward(p_scheduler, imsg);
		return;

	case IMSG_MTA_HOLDQ_RELEASE:
	case IMSG_MDA_HOLDQ_RELEASE:
		m_msg(&m, imsg);
		m_get_id(&m, &holdq);
		m_get_int(&m, &v);
		m_end(&m);
		m_create(p_scheduler, IMSG_QUEUE_HOLDQ_RELEASE, 0, 0, -1);
		if (imsg->hdr.type == IMSG_MTA_HOLDQ_RELEASE)
			m_add_int(p_scheduler, D_MTA);
		else
			m_add_int(p_scheduler, D_MDA);
		m_add_id(p_scheduler, holdq);
		m_add_int(p_scheduler, v);
		m_close(p_scheduler);
		return;

	case IMSG_CTL_PAUSE_MDA:
	case IMSG_CTL_PAUSE_MTA:
	case IMSG_CTL_RESUME_MDA:
	case IMSG_CTL_RESUME_MTA:
		m_forward(p_scheduler, imsg);
		return;

	case IMSG_CTL_VERBOSE:
		m_msg(&m, imsg);
		m_get_int(&m, &v);
		m_end(&m);
		log_trace_verbose(v);
		return;

	case IMSG_CTL_PROFILE:
		m_msg(&m, imsg);
		m_get_int(&m, &v);
		m_end(&m);
		profiling = v;
		return;

	case IMSG_CTL_DISCOVER_EVPID:
		m_msg(&m, imsg);
		m_get_evpid(&m, &evpid);
		m_end(&m);
		if (queue_envelope_load(evpid, &evp) == 0) {
			log_warnx("queue: discover: failed to load "
			    "envelope %016" PRIx64, evpid);
			n_evp = 0;
			m_compose(p_control, imsg->hdr.type,
			    imsg->hdr.peerid, 0, -1,
			    &n_evp, sizeof n_evp);
			return;
		}

		m_create(p_scheduler, IMSG_QUEUE_DISCOVER_EVPID,
		    0, 0, -1);
		m_add_envelope(p_scheduler, &evp);
		m_close(p_scheduler);

		m_create(p_scheduler, IMSG_QUEUE_DISCOVER_MSGID,
		    0, 0, -1);
		m_add_msgid(p_scheduler, evpid_to_msgid(evpid));
		m_close(p_scheduler);
		n_evp = 1;
		m_compose(p_control, imsg->hdr.type, imsg->hdr.peerid,
		    0, -1, &n_evp, sizeof n_evp);
		return;

	case IMSG_CTL_DISCOVER_MSGID:
		m_msg(&m, imsg);
		m_get_msgid(&m, &msgid);
		m_end(&m);
		/* handle concurrent walk requests */
		wi = xcalloc(1, sizeof *wi);
		wi->msgid = msgid;
		wi->peerid = imsg->hdr.peerid;
		evtimer_set(&wi->ev, queue_msgid_walk, wi);
		tv.tv_sec = 0;
		tv.tv_usec = 10;
		evtimer_add(&wi->ev, &tv);
		return;
	}

	fatalx("queue_imsg: unexpected %s imsg", imsg_to_str(imsg->hdr.type));
}

static void
queue_msgid_walk(int fd, short event, void *arg)
{
	struct envelope		 evp;
	struct timeval		 tv;
	struct msg_walkinfo	*wi = arg;
	int			 r;

	r = queue_message_walk(&evp, wi->msgid, &wi->done, &wi->data);
	if (r == -1) {
		if (wi->n_evp) {
			m_create(p_scheduler, IMSG_QUEUE_DISCOVER_MSGID,
			    0, 0, -1);
			m_add_msgid(p_scheduler, wi->msgid);
			m_close(p_scheduler);
		}

		m_compose(p_control, IMSG_CTL_DISCOVER_MSGID, wi->peerid, 0, -1,
		    &wi->n_evp, sizeof wi->n_evp);
		evtimer_del(&wi->ev);
		free(wi);
		return;
	}

	if (r) {
		m_create(p_scheduler, IMSG_QUEUE_DISCOVER_EVPID, 0, 0, -1);
		m_add_envelope(p_scheduler, &evp);
		m_close(p_scheduler);
		wi->n_evp += 1;
	}

	tv.tv_sec = 0;
	tv.tv_usec = 10;
	evtimer_set(&wi->ev, queue_msgid_walk, wi);
	evtimer_add(&wi->ev, &tv);
}

static void
queue_bounce(struct envelope *e, struct delivery_bounce *d)
{
	struct envelope	b;

	b = *e;
	b.type = D_BOUNCE;
	b.agent.bounce = *d;
	b.retry = 0;
	b.lasttry = 0;
	b.creation = time(NULL);
	b.ttl = 3600 * 24 * 7;

	if (e->dsn_notify & DSN_NEVER)
		return;

	if (b.id == 0)
		log_warnx("warn: queue_bounce: evpid=0");
	if (evpid_to_msgid(b.id) == 0)
		log_warnx("warn: queue_bounce: msgid=0, evpid=%016"PRIx64,
			b.id);
	if (e->type == D_BOUNCE) {
		log_warnx("warn: queue: double bounce!");
	} else if (e->sender.user[0] == '\0') {
		log_warnx("warn: queue: no return path!");
	} else if (!queue_envelope_create(&b)) {
		log_warnx("warn: queue: cannot bounce!");
	} else {
		log_debug("debug: queue: bouncing evp:%016" PRIx64
		    " as evp:%016" PRIx64, e->id, b.id);

		m_create(p_scheduler, IMSG_QUEUE_ENVELOPE_SUBMIT, 0, 0, -1);
		m_add_envelope(p_scheduler, &b);
		m_close(p_scheduler);

		m_create(p_scheduler, IMSG_QUEUE_MESSAGE_COMMIT, 0, 0, -1);
		m_add_msgid(p_scheduler, evpid_to_msgid(b.id));
		m_close(p_scheduler);

		stat_increment("queue.bounce", 1);
	}
}

static void
queue_shutdown(void)
{
	log_debug("debug: queue agent exiting");
	queue_close();
	_exit(0);
}

int
queue(void)
{
	struct passwd	*pw;
	struct timeval	 tv;
	struct event	 ev_qload;

	purge_config(PURGE_EVERYTHING & ~PURGE_DISPATCHERS);

	if ((pw = getpwnam(SMTPD_QUEUE_USER)) == NULL)
		if ((pw = getpwnam(SMTPD_USER)) == NULL)
			fatalx("unknown user " SMTPD_USER);

	env->sc_queue_flags |= QUEUE_EVPCACHE;
	env->sc_queue_evpcache_size = 1024;

	if (chroot(PATH_SPOOL) == -1)
		fatal("queue: chroot");
	if (chdir("/") == -1)
		fatal("queue: chdir(\"/\")");

	config_process(PROC_QUEUE);

	if (env->sc_queue_flags & QUEUE_COMPRESSION)
		log_info("queue: queue compression enabled");

	if (env->sc_queue_key) {
		if (!crypto_setup(env->sc_queue_key, strlen(env->sc_queue_key)))
			fatalx("crypto_setup: invalid key for queue encryption");
		log_info("queue: queue encryption enabled");
	}

	if (setgroups(1, &pw->pw_gid) ||
	    setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) ||
	    setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid))
		fatal("queue: cannot drop privileges");

	imsg_callback = queue_imsg;
	event_init();

	signal(SIGINT, SIG_IGN);
	signal(SIGTERM, SIG_IGN);
	signal(SIGPIPE, SIG_IGN);
	signal(SIGHUP, SIG_IGN);

	config_peer(PROC_PARENT);
	config_peer(PROC_CONTROL);
	config_peer(PROC_LKA);
	config_peer(PROC_SCHEDULER);
	config_peer(PROC_DISPATCHER);

	/* setup queue loading task */
	evtimer_set(&ev_qload, queue_timeout, &ev_qload);
	tv.tv_sec = 0;
	tv.tv_usec = 10;
	evtimer_add(&ev_qload, &tv);

	if (pledge("stdio rpath wpath cpath flock recvfd sendfd", NULL) == -1)
		fatal("pledge");

	event_dispatch();
	fatalx("exited event loop");

	return (0);
}

static void
queue_timeout(int fd, short event, void *p)
{
	static uint32_t	 msgid = 0;
	struct envelope	 evp;
	struct event	*ev = p;
	struct timeval	 tv;
	int		 r;

	r = queue_envelope_walk(&evp);
	if (r == -1) {
		if (msgid) {
			m_create(p_scheduler, IMSG_QUEUE_MESSAGE_COMMIT,
			    0, 0, -1);
			m_add_msgid(p_scheduler, msgid);
			m_close(p_scheduler);
		}
		log_debug("debug: queue: done loading queue into scheduler");
		return;
	}

	if (r) {
		if (msgid && evpid_to_msgid(evp.id) != msgid) {
			m_create(p_scheduler, IMSG_QUEUE_MESSAGE_COMMIT,
			    0, 0, -1);
			m_add_msgid(p_scheduler, msgid);
			m_close(p_scheduler);
		}
		msgid = evpid_to_msgid(evp.id);
		m_create(p_scheduler, IMSG_QUEUE_ENVELOPE_SUBMIT, 0, 0, -1);
		m_add_envelope(p_scheduler, &evp);
		m_close(p_scheduler);
	}

	tv.tv_sec = 0;
	tv.tv_usec = 10;
	evtimer_add(ev, &tv);
}

static void
queue_log(const struct envelope *e, const char *prefix, const char *status)
{
	char rcpt[LINE_MAX];

	(void)strlcpy(rcpt, "-", sizeof rcpt);
	if (strcmp(e->rcpt.user, e->dest.user) ||
	    strcmp(e->rcpt.domain, e->dest.domain))
		(void)snprintf(rcpt, sizeof rcpt, "%s@%s",
		    e->rcpt.user, e->rcpt.domain);

	log_info("%s: %s for %016" PRIx64 ": from=<%s@%s>, to=<%s@%s>, "
	    "rcpt=<%s>, delay=%s, stat=%s",
	    e->type == D_MDA ? "delivery" : "relay",
	    prefix,
	    e->id, e->sender.user, e->sender.domain,
	    e->dest.user, e->dest.domain,
	    rcpt,
	    duration_to_text(time(NULL) - e->creation),
	    status);
}