summaryrefslogtreecommitdiff
path: root/usr.sbin/smtpd/queue_fsqueue_ascii.c
blob: 1c7c78ddd5ec8f26ad611280e2aea6d0ece595d8 (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
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
/*	$OpenBSD: queue_fsqueue_ascii.c,v 1.1 2011/10/23 09:30:07 gilles Exp $	*/

/*
 * Copyright (c) 2011 Gilles Chehade <gilles@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 <sys/types.h>
#include <sys/queue.h>
#include <sys/tree.h>
#include <sys/param.h>
#include <sys/socket.h>
#include <sys/stat.h>

#include <netinet/in.h>
#include <arpa/inet.h>

#include <ctype.h>
#include <err.h>
#include <errno.h>
#include <event.h>
#include <fcntl.h>
#include <imsg.h>
#include <libgen.h>
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>

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


#define	KW_VERSION		"version"
#define	KW_EVPID		"evpid"
#define	KW_TYPE			"type"
#define	KW_HELO			"helo"
#define	KW_HOSTNAME    		"hostname"
#define	KW_ERRORLINE   		"errorline"
#define	KW_SOCKADDR   		"sockaddr"
#define	KW_SENDER		"sender"
#define	KW_RCPT			"rcpt"
#define	KW_DEST   		"dest"
#define	KW_CTIME		"ctime"
#define	KW_EXPIRE		"expire"
#define	KW_RETRY		"retry"
#define	KW_LAST_TRY		"last-try"
#define	KW_FLAGS		"flags"
#define	KW_STATUS		"status"

#define	KW_MDA_METHOD		"mda-method"
#define	KW_MDA_BUFFER		"mda-buffer"
#define	KW_MDA_USER		"mda-user"

#define	KW_MTA_RELAY_HOST	"mta-relay-hostname"
#define	KW_MTA_RELAY_PORT	"mta-relay-port"
#define	KW_MTA_RELAY_FLAGS	"mta-relay-flags"
#define	KW_MTA_RELAY_CERT	"mta-relay-cert"
#define	KW_MTA_RELAY_AS		"mta-relay-as"

int	fsqueue_load_envelope_ascii(FILE *, struct envelope *);
int	fsqueue_dump_envelope_ascii(FILE *, struct envelope *);

static int
ascii_load_version(struct envelope *ep, char *buf)
{
	const char *errstr;

	ep->version = strtonum(buf, 0, 0xffffffff, &errstr);
	if (errstr)
		return 0;
	return 1;
}

static int
ascii_dump_version(struct envelope *ep, FILE *fp)
{
	fprintf(fp, "%s: %d\n", KW_VERSION, SMTPD_ENVELOPE_VERSION);
	return 1;
}


static int
ascii_load_evpid(struct envelope *ep, char *buf)
{
	char *endptr;
	
	ep->id = strtoull(buf, &endptr, 16);
	if (buf[0] == '\0' || *endptr != '\0')
		return 0;
	if (errno == ERANGE && ep->id == ULLONG_MAX)
		return 0;
	return 1;
}

static int
ascii_dump_evpid(struct envelope *ep, FILE *fp)
{
	fprintf(fp, "%s: %016llx\n", KW_EVPID, ep->id);
	return 1;
}


static int
ascii_load_type(struct envelope *ep, char *buf)
{
	if (strcasecmp(buf, "mda") == 0)
		ep->type = D_MDA;
	else if (strcasecmp(buf, "mta") == 0)
		ep->type = D_MTA;
	else if (strcasecmp(buf, "bounce") == 0)
		ep->type = D_BOUNCE;
	else
		return 0;
	return 1;
}

static int
ascii_dump_type(struct envelope *ep, FILE *fp)
{
	fprintf(fp, "%s: ", KW_TYPE);
	switch (ep->type) {
	case D_MDA:
		fprintf(fp, "mda\n");
		break;
	case D_MTA:
		fprintf(fp, "mta\n");
		break;
	case D_BOUNCE:
		fprintf(fp, "bounce\n");
		break;
	default:
		return 0;
	}
	return 1;
}


static int
ascii_load_mda_method(struct envelope *ep, char *buf)
{
	if (strcasecmp(buf, "mbox") == 0)
		ep->agent.mda.method = A_MBOX;
	else if (strcasecmp(buf, "maildir") == 0)
		ep->agent.mda.method = A_MAILDIR;
	else if (strcasecmp(buf, "filename") == 0)
		ep->agent.mda.method = A_FILENAME;
	else if (strcasecmp(buf, "external") == 0)
		ep->agent.mda.method = A_EXT;
	else
		return 0;
	return 1;
}

static int
ascii_dump_mda_method(struct envelope *ep, FILE *fp)
{
	fprintf(fp, "%s: ", KW_MDA_METHOD);
	switch (ep->agent.mda.method) {
	case A_MAILDIR:
		fprintf(fp, "maildir\n");
		break;
	case A_MBOX:
		fprintf(fp, "mbox\n");
		break;
	case A_FILENAME:
		fprintf(fp, "filename\n");
		break;
	case A_EXT:
		fprintf(fp, "external\n");
		break;
	default:
		return 0;
	}
	return 1;
}

static int
ascii_load_mda_buffer(struct envelope *ep, char *buf)
{
	if (strlcpy(ep->agent.mda.to.buffer, buf,
		sizeof (ep->agent.mda.to.buffer))
	    >= sizeof (ep->agent.mda.to.buffer))
		return 0;
	return 1;
}

static int
ascii_dump_mda_buffer(struct envelope *ep, FILE *fp)
{
	fprintf(fp, "%s: %s\n", KW_MDA_BUFFER,
	    ep->agent.mda.to.buffer);
	return 1;
}


static int
ascii_load_mda_user(struct envelope *ep, char *buf)
{
	if (strlcpy(ep->agent.mda.as_user, buf,
		sizeof (ep->agent.mda.as_user))
	    >= sizeof (ep->agent.mda.as_user))
		return 0;
	return 1;
}

static int
ascii_dump_mda_user(struct envelope *ep, FILE *fp)
{
	fprintf(fp, "%s: %s\n", KW_MDA_USER,
	    ep->agent.mda.as_user);
	return 1;
}


static int
ascii_load_helo(struct envelope *ep, char *buf)
{
	if (strlcpy(ep->helo, buf,
		sizeof (ep->helo))
	    >= sizeof (ep->helo))
		return 0;
	return 1;
}

static int
ascii_dump_helo(struct envelope *ep, FILE *fp)
{
	fprintf(fp, "%s: %s\n", KW_HELO, ep->helo);
	return 1;
}


static int
ascii_load_hostname(struct envelope *ep, char *buf)
{
	if (strlcpy(ep->hostname, buf,
		sizeof (ep->hostname))
	    >= sizeof (ep->hostname))
		return 0;
	return 1;
}

static int
ascii_dump_hostname(struct envelope *ep, FILE *fp)
{
	fprintf(fp, "%s: %s\n", KW_HOSTNAME, ep->hostname);
	return 1;
}

static int
ascii_load_errorline(struct envelope *ep, char *buf)
{
	if (strlcpy(ep->errorline, buf,
		sizeof(ep->errorline)) >=
		sizeof(ep->errorline))
	    return 0;
		
	return 1;
}

static int
ascii_dump_errorline(struct envelope *ep, FILE *fp)
{
	if (ep->errorline[0])
		fprintf(fp, "%s: %s\n", KW_ERRORLINE,
		    ep->errorline);
	return 1;
}


static int
ascii_load_sockaddr(struct envelope *ep, char *buf)
{
	struct sockaddr_in6 ssin6;
	struct sockaddr_in  ssin;

	if (strncasecmp("IPv6:", buf, 5) == 0) {
		if (inet_pton(AF_INET6, buf + 5, &ssin6.sin6_addr) != 1)
			return 0;
		ssin6.sin6_family = AF_INET6;
		memcpy(&ep->ss, &ssin6, sizeof(ssin6));
		ep->ss.ss_len = sizeof(struct sockaddr_in6);
	}
	else {
		if (inet_pton(AF_INET, buf, &ssin.sin_addr) != 1)
			return 0;
		ssin.sin_family = AF_INET;
		memcpy(&ep->ss, &ssin6, sizeof(ssin));
		ep->ss.ss_len = sizeof(struct sockaddr_in);
	}
	return 1;
}

static int
ascii_dump_sockaddr(struct envelope *ep, FILE *fp)
{
	fprintf(fp, "%s: %s\n", KW_SOCKADDR, ss_to_text(&ep->ss));
	return 1;
}


static int
ascii_load_sender(struct envelope *ep, char *buf)
{
	if (! email_to_mailaddr(&ep->sender, buf))
		return 0;
	return 1;
}

static int
ascii_dump_sender(struct envelope *ep, FILE *fp)
{
	fprintf(fp, "%s: %s@%s\n", KW_SENDER,
	    ep->sender.user, ep->sender.domain);
	return 1;
}


static int
ascii_load_rcpt(struct envelope *ep, char *buf)
{
	if (! email_to_mailaddr(&ep->rcpt, buf))
		return 0;
	ep->dest = ep->rcpt;
	return 1;
}

static int
ascii_dump_rcpt(struct envelope *ep, FILE *fp)
{
	fprintf(fp, "%s: %s@%s\n", KW_RCPT,
	    ep->dest.user, ep->dest.domain);
	return 1;
}


static int
ascii_load_dest(struct envelope *ep, char *buf)
{
	if (! email_to_mailaddr(&ep->dest, buf))
		return 0;
	return 1;
}

static int
ascii_dump_dest(struct envelope *ep, FILE *fp)
{
	if (strcmp(ep->dest.user, ep->dest.user) != 0 ||
	    strcmp(ep->dest.domain, ep->dest.domain) != 0)
		fprintf(fp, "%s: %s@%s\n", KW_DEST,
		    ep->dest.user,
		    ep->dest.domain);
	return 1;
}


static int
ascii_load_mta_relay_host(struct envelope *ep, char *buf)
{
	if (strlcpy(ep->agent.mta.relay.hostname, buf,
		sizeof(ep->agent.mta.relay.hostname))
	    >= sizeof(ep->agent.mta.relay.hostname))
		return 0;
	return 1;
}

static int
ascii_dump_mta_relay_host(struct envelope *ep, FILE *fp)
{
	if (ep->agent.mta.relay.hostname[0])
		fprintf(fp, "%s: %s\n", KW_MTA_RELAY_HOST,
		    ep->agent.mta.relay.hostname);
	return 1;
}

static int
ascii_load_mta_relay_port(struct envelope *ep, char *buf)
{
	const char *errstr;

	ep->agent.mta.relay.port = strtonum(buf, 0, 0xffff, &errstr);
	if (errstr)
		return 0;
	return 1;
}

static int
ascii_dump_mta_relay_port(struct envelope *ep, FILE *fp)
{
	if (ep->agent.mta.relay.port)
		fprintf(fp, "%s: %d\n", KW_MTA_RELAY_PORT,
		    ep->agent.mta.relay.port);
	return 1;
}

static int
ascii_load_mta_relay_cert(struct envelope *ep, char *buf)
{
	if (strlcpy(ep->agent.mta.relay.cert, buf,
		sizeof(ep->agent.mta.relay.cert))
	    >= sizeof(ep->agent.mta.relay.cert))
		return 0;
	return 1;
}

static int
ascii_dump_mta_relay_cert(struct envelope *ep, FILE *fp)
{
	if (ep->agent.mta.relay.cert[0])
		fprintf(fp, "%s: %s\n", KW_MTA_RELAY_CERT,
		    ep->agent.mta.relay.cert);
	return 1;
}

static int
ascii_load_mta_relay_flags(struct envelope *ep, char *buf)
{
	char *flag;

	while ((flag = strsep(&buf, " ,|")) != NULL) {
		if (strcasecmp(flag, "force-anyssl") == 0)
			ep->agent.mta.relay.flags |= MTA_FORCE_ANYSSL;
		else if (strcasecmp(flag, "force-smtps") == 0)
			ep->agent.mta.relay.flags |= MTA_FORCE_SMTPS;
		else if (strcasecmp(flag, "allow-plain") == 0)
			ep->agent.mta.relay.flags |= MTA_ALLOW_PLAIN;
		else if (strcasecmp(flag, "use-auth") == 0)
			ep->agent.mta.relay.flags |= MTA_USE_AUTH;
		else if (strcasecmp(flag, "force-mx") == 0)
			ep->agent.mta.relay.flags |= MTA_FORCE_MX;
		else
			return 0;
	}

	return 1;
}

static int
ascii_dump_mta_relay_flags(struct envelope *ep, FILE *fp)
{
	if (ep->agent.mta.relay.flags) {
		fprintf(fp, "%s:", KW_MTA_RELAY_FLAGS);
		if (ep->agent.mta.relay.flags & MTA_FORCE_ANYSSL)
			fprintf(fp, " force-anyssl");
		if (ep->agent.mta.relay.flags & MTA_FORCE_SMTPS)
			fprintf(fp, " force-smtps");
		if (ep->agent.mta.relay.flags & MTA_ALLOW_PLAIN)
			fprintf(fp, " allow-plain");
		if (ep->agent.mta.relay.flags & MTA_USE_AUTH)
			fprintf(fp, " use-auth");
		if (ep->agent.mta.relay.flags & MTA_FORCE_MX)
			fprintf(fp, " force-mx");
		fprintf(fp, "\n");
	}
	return 1;
}

static int
ascii_dump_mta_relay_as(struct envelope *ep, FILE *fp)
{
	if (ep->agent.mta.relay_as.user[0] ||
	    ep->agent.mta.relay_as.domain[0])
		fprintf(fp, "%s: %s@%s\n", KW_MTA_RELAY_AS,
		    ep->agent.mta.relay_as.user,
		    ep->agent.mta.relay_as.domain);
	return 1;
}


static int
ascii_load_ctime(struct envelope *ep, char *buf)
{
	const char *errstr;

	ep->creation = strtonum(buf, 0, 0xffffffff, &errstr);
	if (errstr)
		return 0;
	return 1;
}

static int
ascii_dump_ctime(struct envelope *ep, FILE *fp)
{
	if (ep->creation)
		fprintf(fp, "%s: %qd\n", KW_CTIME,
		    (u_int64_t)ep->creation);
	return 1;
}


static int
ascii_load_expire(struct envelope *ep, char *buf)
{
	const char *errstr;

	ep->expire = strtonum(buf, 0, 0xffffffff, &errstr);
	if (errstr)
		return 0;
	return 1;
}

static int
ascii_dump_expire(struct envelope *ep, FILE *fp)
{
	if (ep->expire)
		fprintf(fp, "%s: %qd\n", KW_EXPIRE,
		    (u_int64_t)ep->expire);
	return 1;
}

static int
ascii_load_retry(struct envelope *ep, char *buf)
{
	const char *errstr;

	ep->retry = strtonum(buf, 0, 0xffffffff, &errstr);
	if (errstr)
		return 0;
	return 1;
}

static int
ascii_dump_retry(struct envelope *ep, FILE *fp)
{
	if (ep->retry)
		fprintf(fp, "%s: %d\n", KW_RETRY,
		    ep->retry);
	return 1;
}

static int
ascii_load_lasttry(struct envelope *ep, char *buf)
{
	const char *errstr;

	ep->lasttry = strtonum(buf, 0, 0xffffffff, &errstr);
	if (errstr)
		return 0;
	return 1;
}

static int
ascii_dump_lasttry(struct envelope *ep, FILE *fp)
{
	if (ep->lasttry)
		fprintf(fp, "%s: %qd\n", KW_LAST_TRY,
		    (u_int64_t)ep->lasttry);
	return 1;
}

static int
ascii_dump_flags(struct envelope *ep, FILE *fp)
{
	if (ep->flags) {
		fprintf(fp, "%s:", KW_FLAGS);
		if (ep->flags & DF_AUTHENTICATED)
			fprintf(fp, " authenticated");
		if (ep->flags & DF_ENQUEUED)
			fprintf(fp, " enqueued");
		if (ep->flags & DF_BOUNCE)
			fprintf(fp, " bounce");
		if (ep->flags & DF_INTERNAL)
			fprintf(fp, " internal");
		fprintf(fp, "\n");
	}
	return 1;
}

static int
ascii_load_flags(struct envelope *ep, char *buf)
{
	char *flag;

	while ((flag = strsep(&buf, " ,|")) != NULL) {
		if (strcasecmp(flag, "authenticated") == 0)
			ep->flags |= DF_AUTHENTICATED;
		else if (strcasecmp(flag, "enqueued") == 0)
			ep->flags |= DF_ENQUEUED;
		else if (strcasecmp(flag, "bounce") == 0)
			ep->flags |= DF_BOUNCE;
		else if (strcasecmp(flag, "internal") == 0)
			ep->flags |= DF_INTERNAL;
		else
			return 0;
	}
	return 1;
}

static int
ascii_dump_status(struct envelope *ep, FILE *fp)
{
	if (ep->status) {
		fprintf(fp, "%s ", KW_STATUS);
		if (ep->status & DS_PERMFAILURE)
			fprintf(fp, " permfail");
		if (ep->status & DS_TEMPFAILURE)
			fprintf(fp, " tempfail");
		if (ep->status & DS_REJECTED)
			fprintf(fp, " rejected");
		if (ep->status & DS_ACCEPTED)
			fprintf(fp, " accepted");
		fprintf(fp, "\n");
	}
	return 1;
}

static int
ascii_dump_agent(struct envelope *ep, FILE *fp)
{
	if (! ascii_dump_type(ep, fp))
		return 0;

	switch (ep->type) {
	case D_MDA:
		if (! ascii_dump_mda_method(ep, fp) ||
		    ! ascii_dump_mda_buffer(ep, fp) ||
		    ! ascii_dump_mda_user(ep, fp))
			return 0;
		break;

	case D_MTA:
		if (! ascii_dump_mta_relay_host(ep, fp)  ||
		    ! ascii_dump_mta_relay_port(ep, fp)  ||
		    ! ascii_dump_mta_relay_cert(ep, fp)  ||
		    ! ascii_dump_mta_relay_flags(ep, fp) ||
		    ! ascii_dump_mta_relay_as(ep, fp))
			return 0;
		break;

	case D_BOUNCE:
		/* nothing ! */
		break;

	default:
		return 0;
	}

	return 1;
}

int
fsqueue_load_envelope_ascii(FILE *fp, struct envelope *ep)
{
	char *buf, *lbuf;
	size_t	len;
	struct ascii_load_handler {
		char *kw;
		int (*hdl)(struct envelope *, char *);
	} ascii_load_handlers[] = {
		{ KW_VERSION,		ascii_load_version },
		{ KW_EVPID,		ascii_load_evpid },

		{ KW_HOSTNAME,		ascii_load_hostname },
		{ KW_SOCKADDR,		ascii_load_sockaddr },

		{ KW_HELO,		ascii_load_helo },
		{ KW_SENDER,		ascii_load_sender },
		{ KW_RCPT,		ascii_load_rcpt },

		{ KW_TYPE,		ascii_load_type },
		{ KW_DEST,		ascii_load_dest },

		{ KW_CTIME,		ascii_load_ctime },
		{ KW_EXPIRE,		ascii_load_expire },
		{ KW_RETRY,		ascii_load_retry },
		{ KW_LAST_TRY,		ascii_load_lasttry },

		{ KW_FLAGS,		ascii_load_flags },

		{ KW_ERRORLINE,		ascii_load_errorline },

		{ KW_MDA_METHOD,       	ascii_load_mda_method },
		{ KW_MDA_BUFFER,       	ascii_load_mda_buffer },
		{ KW_MDA_USER,		ascii_load_mda_user },

		{ KW_MTA_RELAY_HOST,   	ascii_load_mta_relay_host },
		{ KW_MTA_RELAY_PORT,   	ascii_load_mta_relay_port },
		{ KW_MTA_RELAY_FLAGS,  	ascii_load_mta_relay_flags },
		{ KW_MTA_RELAY_CERT,  	ascii_load_mta_relay_cert },
	};
	int	i;
	int	n;
	int	ret;

	n = sizeof(ascii_load_handlers) / sizeof(struct ascii_load_handler);

	bzero(ep, sizeof (*ep));
	lbuf = NULL;
	while ((buf = fgetln(fp, &len))) {
		if (buf[len - 1] == '\n')
			buf[len - 1] = '\0';
		else {
			if ((lbuf = malloc(len + 1)) == NULL)
				err(1, NULL);
			memcpy(lbuf, buf, len);
			lbuf[len] = '\0';
			buf = lbuf;
		}

		for (i = 0; i < n; ++i) {
			len = strlen(ascii_load_handlers[i].kw);
			if (! strncasecmp(ascii_load_handlers[i].kw, buf, len)) {
				/* skip kw and tailing whitespaces */
				buf += len;
				while (*buf && isspace(*buf))
					buf++;

				/* we *want* ':' */
				if (*buf != ':')
					continue;
				buf++;

				/* skip whitespaces after separator */
				while (*buf && isspace(*buf))
				    buf++;

				ret = ascii_load_handlers[i].hdl(ep, buf);
				if (ret == 0) {
					log_debug("### CHOKED ON: %s", buf);
					goto err;
				}
				break;
			}
		}

		/* unknown keyword */
		if (i == n) {
			log_debug("### CHOKED ON: %s", buf);
			goto err;
		}
	}
	free(lbuf);

	return 1;

err:
	free(lbuf);
	return 0;
}

int
fsqueue_dump_envelope_ascii(FILE *fp, struct envelope *ep)
{
	if (! ascii_dump_version(ep, fp)   ||
	    ! ascii_dump_evpid(ep, fp)	   ||
	    ! ascii_dump_agent(ep, fp)	   ||
	    ! ascii_dump_helo(ep, fp)	   ||
	    ! ascii_dump_hostname(ep, fp)  ||
	    ! ascii_dump_errorline(ep, fp) ||
	    ! ascii_dump_sockaddr(ep, fp)  ||
	    ! ascii_dump_sender(ep, fp)	   ||
	    ! ascii_dump_rcpt(ep, fp)	   ||
	    ! ascii_dump_dest(ep, fp)	   ||
	    ! ascii_dump_ctime(ep, fp)	   ||
	    ! ascii_dump_lasttry(ep, fp)   ||
	    ! ascii_dump_expire(ep, fp)	   ||
	    ! ascii_dump_retry(ep, fp)	   ||
	    ! ascii_dump_flags(ep, fp)	   ||
	    ! ascii_dump_status(ep, fp))
		goto err;

	if (fflush(fp) != 0)
		goto err;

	return 1;

err:
	return 0;
}