-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathopenarc-test.c
730 lines (619 loc) · 13.3 KB
/
openarc-test.c
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
/*
** Copyright (c) 2007-2009 Sendmail, Inc. and its suppliers.
** All rights reserved.
**
** Copyright (c) 2009-2013, The Trusted Domain Project. All rights reserved.
*/
/* system includes */
#include <sys/param.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <string.h>
#include <errno.h>
#include <stdio.h>
#include <sysexits.h>
#include <stdlib.h>
#include <time.h>
#include <assert.h>
/* libopenarc includes */
#include "build-config.h"
#include <arc.h>
/* libbsd if found */
#ifdef USE_BSD_H
# include <bsd/string.h>
#endif /* USE_BSD_H */
/* libstrl if needed */
#ifdef USE_STRL_H
# include <strl.h>
#endif /* USE_STRL_H */
/* libmilter includes */
#include <libmilter/mfapi.h>
/* openarc includes */
#define ARCF_MILTER_PROTOTYPES
#include "openarc-test.h"
#include "openarc.h"
/* local types and definitions*/
#define CRLF "\r\n"
struct test_context
{
void * tc_priv; /* private data pointer */
};
char *milter_status[] =
{
"SMFIS_CONTINUE",
"SMFIS_REJECT",
"SMFIS_DISCARD",
"SMFIS_ACCEPT",
"SMFIS_TEMPFAIL"
};
char *envfrom[] =
{
"<[email protected]>",
NULL
};
#define FCLOSE(x) if ((x) != stdin) \
fclose((x));
#define MLFI_OUTPUT(x,y) ((y) > 1 || ((y) == 1 && (x) != SMFIS_CONTINUE))
#define STRORNULL(x) ((x) == NULL ? "(null)" : (x))
/* globals */
static int tverbose = 0;
/*
** ARCF_TEST_SETPRIV -- store private pointer
**
** Parameters:
** ctx -- context pointer
** ptr -- pointer to store
**
** Return value:
** MI_SUCCESS
*/
int
arcf_test_setpriv(void *ctx, void *ptr)
{
struct test_context *tc;
assert(ctx != NULL);
tc = ctx;
tc->tc_priv = ptr;
return MI_SUCCESS;
}
/*
** ARCF_TEST_GETPRIV -- retrieve private pointer
**
** Parameters:
** ctx -- context pointer
**
** Return value:
** The private pointer.
*/
void *
arcf_test_getpriv(void *ctx)
{
struct test_context *tc;
assert(ctx != NULL);
tc = ctx;
return tc->tc_priv;
}
/*
** ARCF_TEST_PROGRESS -- send progress message
**
** Parameters:
** ctx -- context pointer
**
** Return value:
** MI_SUCCESS
*/
int
arcf_test_progress(void *ctx)
{
assert(ctx != NULL);
if (tverbose > 1)
fprintf(stdout, "### PROGRESS\n");
return MI_SUCCESS;
}
/*
** ARCF_TEST_SETREPLY -- set reply to use
**
** Parameters:
** ctx -- context pointer
** rcode -- SMTP reply code
** xcode -- SMTP enhanced reply code
** replytxt -- SMTP reply text
**
** Return value:
** MI_SUCCESS
*/
int
arcf_test_setreply(void *ctx, char *rcode, char *xcode, char *replytxt)
{
assert(ctx != NULL);
if (tverbose > 1)
{
fprintf(stdout,
"### SETREPLY: rcode='%s' xcode='%s' replytxt='%s'\n",
STRORNULL(rcode), STRORNULL(xcode),
STRORNULL(replytxt));
}
return MI_SUCCESS;
}
/*
** ARCF_TEST_INSHEADER -- insert a header
**
** Parameters:
** ctx -- context pointer
** idx -- insertion index
** hname -- header name
** hvalue -- header value
**
** Return value:
** MI_SUCCESS
*/
int
arcf_test_insheader(void *ctx, int idx, char *hname, char *hvalue)
{
assert(ctx != NULL);
if (tverbose > 1)
{
fprintf(stdout,
"### INSHEADER: idx=%d hname='%s' hvalue='%s'\n",
idx, STRORNULL(hname), STRORNULL(hvalue));
}
return MI_SUCCESS;
}
/*
** ARCF_TEST_CHGHEADER -- change a header
**
** Parameters:
** ctx -- context pointer
** hname -- header name
** idx -- header index
** hvalue -- header value
**
** Return value:
** MI_SUCCESS
*/
int
arcf_test_chgheader(void *ctx, char *hname, int idx, char *hvalue)
{
assert(ctx != NULL);
if (tverbose > 1)
{
fprintf(stdout,
"### CHGHEADER: hname='%s' idx=%d hvalue='%s'\n",
STRORNULL(hname), idx, STRORNULL(hvalue));
}
return MI_SUCCESS;
}
/*
** ARCF_TEST_QUARANTINE -- request message quarantine
**
** Parameters:
** ctx -- context pointer
** reason -- reason string
**
** Return value:
** MI_SUCCESS
*/
int
arcf_test_quarantine(void *ctx, char *reason)
{
assert(ctx != NULL);
if (tverbose > 1)
{
fprintf(stdout,
"### QUARANTINE: reason='%s'\n", STRORNULL(reason));
}
return MI_SUCCESS;
}
/*
** ARCF_TEST_ADDHEADER -- append a header
**
** Parameters:
** ctx -- context pointer
** hname -- header name
** hvalue -- header value
**
** Return value:
** MI_SUCCESS
*/
int
arcf_test_addheader(void *ctx, char *hname, char *hvalue)
{
assert(ctx != NULL);
if (tverbose > 1)
{
fprintf(stdout,
"### ADDHEADER: hname='%s' hvalue='%s'\n",
STRORNULL(hname), STRORNULL(hvalue));
}
return MI_SUCCESS;
}
/*
** ARCF_TEST_DELRCPT -- request recipient delete
**
** Parameters:
** ctx -- context pointer
** addr -- address
**
** Return value:
** MI_SUCCESS
*/
int
arcf_test_delrcpt(void *ctx, char *addr)
{
assert(ctx != NULL);
assert(addr != NULL);
if (tverbose > 1)
fprintf(stdout, "### DELRCPT: '%s'\n", addr);
return MI_SUCCESS;
}
/*
** ARCF_TEST_ADDRCPT -- request recipient add
**
** Parameters:
** ctx -- context pointer
** addr -- address
**
** Return value:
** MI_SUCCESS
*/
int
arcf_test_addrcpt(void *ctx, char *addr)
{
assert(ctx != NULL);
assert(addr != NULL);
if (tverbose > 1)
fprintf(stdout, "### ADDRCPT: '%s'\n", addr);
return MI_SUCCESS;
}
/*
** ARCF_TEST_GETSYMVAL -- retrieve a symbol value
**
** Parameters:
** ctx -- context pointer
** sym -- symbol name
**
** Return value:
** Pointer to (static) string name.
**
** Note:
** This isn't thread-safe, but test mode is single-threaded anyway.
** This is also a memory leak, but it's a short-lived test program
** anyway.
*/
char *
arcf_test_getsymval(void *ctx, char *sym)
{
static char symout[MAXBUFRSZ];
assert(ctx != NULL);
assert(sym != NULL);
snprintf(symout, sizeof symout, "DEBUG-%s", sym);
return strdup(symout);
}
/*
** ARCF_TESTFILE -- read a message and test it
**
** Parameters:
** libopenarc -- DKIM_LIB handle
** file -- input file path
** fixedtime -- time to use on signatures (or -1)
** verbose -- verbose level
**
** Return value:
** An EX_* constant (see sysexits.h)
*/
static int
arcf_testfile(ARC_LIB *libopenarc, struct test_context *tctx,
FILE *f, char *file, int tverbose)
{
bool inheaders = TRUE;
int len = 0;
int buflen = 0;
int lineno = 0;
int hslineno = 0;
int c;
ARC_MESSAGE *msg;
char *p;
sfsistat ms;
char buf[MAXBUFRSZ];
char line[MAXBUFRSZ];
assert(libopenarc != NULL);
assert(tctx != NULL);
assert(f != NULL);
memset(buf, '\0', sizeof buf);
memset(line, '\0', sizeof buf);
ms = mlfi_envfrom((SMFICTX *) tctx, envfrom);
if (MLFI_OUTPUT(ms, tverbose))
{
fprintf(stderr, "%s: %s: mlfi_envfrom() returned %s\n",
progname, file, milter_status[ms]);
}
if (ms != SMFIS_CONTINUE)
return EX_SOFTWARE;
while (!feof(f))
{
if (fgets(line, sizeof line, f) == NULL)
break;
lineno++;
c = '\0';
for (p = line; *p != '\0'; p++)
{
if (*p == '\n')
{
*p = '\0';
break;
}
c = *p;
}
if (c == '\r' && p != line) /* eat the CR */
*(p - 1) = '\0';
if (inheaders)
{
if (line[0] == '\0')
{
if (buf[0] != '\0')
{
char *colon;
colon = strchr(buf, ':');
if (colon == NULL)
{
fprintf(stderr,
"%s: %s: line %d: header malformed\n",
progname, file,
lineno);
return EX_DATAERR;
}
*colon = '\0';
if (*(colon + 1) == ' ')
colon++;
ms = mlfi_header((SMFICTX *) tctx, buf,
colon + 1);
if (MLFI_OUTPUT(ms, tverbose))
{
fprintf(stderr,
"%s: %s: line %d: mlfi_header() returned %s\n",
progname, file,
hslineno,
milter_status[ms]);
}
if (ms != SMFIS_CONTINUE)
return EX_SOFTWARE;
}
inheaders = FALSE;
memset(buf, '\0', sizeof buf);
memset(line, '\0', sizeof buf);
ms = mlfi_eoh((SMFICTX *) tctx);
if (MLFI_OUTPUT(ms, tverbose))
{
fprintf(stderr,
"%s: %s: mlfi_eoh() returned %s\n",
progname, file,
milter_status[ms]);
}
if (ms != SMFIS_CONTINUE)
return EX_SOFTWARE;
continue;
}
if (line[0] == ' ' || line[0] == '\t')
{
(void) strlcat(buf, CRLF, sizeof buf);
if (strlcat(buf, line,
sizeof buf) >= sizeof buf)
{
fprintf(stderr,
"%s: %s: line %d: header '%*s...' too large\n",
progname, file, lineno,
20, buf);
return EX_DATAERR;
}
}
else
{
if (buf[0] != '\0')
{
char *colon;
colon = strchr(buf, ':');
if (colon == NULL)
{
fprintf(stderr,
"%s: %s: line %d: header malformed\n",
progname, file,
lineno);
return EX_DATAERR;
}
*colon = '\0';
if (*(colon + 1) == ' ')
colon++;
ms = mlfi_header((SMFICTX *) tctx, buf,
colon + 1);
if (MLFI_OUTPUT(ms, tverbose))
{
fprintf(stderr,
"%s: %s: line %d: mlfi_header() returned %s\n",
progname, file,
hslineno,
milter_status[ms]);
}
if (ms != SMFIS_CONTINUE)
return EX_SOFTWARE;
hslineno = 0;
}
if (hslineno == 0)
hslineno = lineno;
strlcpy(buf, line, sizeof buf);
}
}
else
{
len = strlen(line);
if (len + buflen >= (int) sizeof buf - 3)
{
ms = mlfi_body((SMFICTX *) tctx,
(u_char *) buf,
strlen(buf));
if (MLFI_OUTPUT(ms, tverbose))
{
fprintf(stderr,
"%s: %s: mlfi_body() returned %s\n",
progname, file,
milter_status[ms]);
}
if (ms != SMFIS_CONTINUE)
return EX_SOFTWARE;
memset(buf, '\0', sizeof buf);
buflen = 0;
}
memcpy(&buf[buflen], line, len);
buflen += len;
memcpy(&buf[buflen], CRLF, 2);
buflen += 2;
}
}
/* unprocessed partial header? */
if (inheaders && buf[0] != '\0')
{
char *colon;
colon = strchr(buf, ':');
if (colon == NULL)
{
fprintf(stderr,
"%s: %s: line %d: header malformed\n",
progname, file, lineno);
return EX_DATAERR;
}
*colon = '\0';
if (*(colon + 1) == ' ')
colon++;
ms = mlfi_header((SMFICTX *) tctx, buf, colon + 1);
if (MLFI_OUTPUT(ms, tverbose))
{
fprintf(stderr,
"%s: %s: line %d: mlfi_header() returned %s\n",
progname, file, lineno, milter_status[ms]);
}
if (ms != SMFIS_CONTINUE)
return EX_SOFTWARE;
ms = mlfi_eoh((SMFICTX *) tctx);
if (MLFI_OUTPUT(ms, tverbose))
{
fprintf(stderr,
"%s: %s: mlfi_eoh() returned %s\n",
progname, file, milter_status[ms]);
}
if (ms != SMFIS_CONTINUE)
return EX_SOFTWARE;
inheaders = FALSE;
memset(buf, '\0', sizeof buf);
}
/* no headers found */
if (inheaders)
{
fprintf(stderr, "%s: %s: warning: no headers on input\n",
progname, file);
ms = mlfi_eoh((SMFICTX *) tctx);
if (MLFI_OUTPUT(ms, tverbose))
{
fprintf(stderr, "%s: %s: mlfi_eoh() returned %s\n",
progname, file, milter_status[ms]);
}
if (ms != SMFIS_CONTINUE)
return EX_SOFTWARE;
}
/* some body left */
if (!inheaders && buf[0] != '\0')
{
ms = mlfi_body((SMFICTX *) tctx, (u_char *) buf, strlen(buf));
if (MLFI_OUTPUT(ms, tverbose))
{
fprintf(stderr, "%s: %s: mlfi_body() returned %s\n",
progname, file, milter_status[ms]);
}
if (ms != SMFIS_CONTINUE)
return EX_SOFTWARE;
}
ms = mlfi_eom((SMFICTX *) tctx);
if (MLFI_OUTPUT(ms, tverbose))
{
fprintf(stderr, "%s: %s: mlfi_eom() returned %s\n",
progname, file, milter_status[ms]);
}
return EX_OK;
}
/*
** ARCF_TESTFILES -- test one or more input messages
**
** Parameters:
** libopenarc -- ARC_LIB handle
** flist -- input file list
** verbose -- verbose level
**
** Return value:
** An EX_* constant (see sysexits.h)
*/
int
arcf_testfiles(ARC_LIB *libopenarc, char *flist, int verbose)
{
char *file;
char *ctx;
FILE *f;
int status;
sfsistat ms;
struct test_context *tctx;
struct sockaddr_in sin;
assert(libopenarc != NULL);
assert(flist != NULL);
tverbose = verbose;
/* set up a fake SMFICTX */
tctx = (struct test_context *) malloc(sizeof(struct test_context));
if (tctx == NULL)
{
fprintf(stderr, "%s: malloc(): %s\n", progname,
strerror(errno));
return EX_OSERR;
}
tctx->tc_priv = NULL;
(void) memset(&sin, '\0', sizeof sin);
sin.sin_family = AF_INET;
sin.sin_port = htons(time(NULL) % 65536);
sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
ms = mlfi_connect((SMFICTX *) tctx, "localhost", (_SOCK_ADDR *) &sin);
if (MLFI_OUTPUT(ms, tverbose))
{
fprintf(stderr, "%s: mlfi_connect() returned %s\n",
progname, milter_status[ms]);
}
if (ms != SMFIS_CONTINUE)
return EX_SOFTWARE;
/* loop through inputs */
for (file = strtok_r(flist, ",", &ctx);
file != NULL;
file = strtok_r(NULL, ",", &ctx))
{
/* open the input */
if (strcmp(file, "-") == 0)
{
f = stdin;
file = "(stdin)";
}
else
{
f = fopen(file, "r");
if (f == NULL)
{
fprintf(stderr, "%s: %s: fopen(): %s\n",
progname, file, strerror(errno));
return EX_UNAVAILABLE;
}
}
status = arcf_testfile(libopenarc, tctx, f, file, tverbose);
FCLOSE(f);
if (status != EX_OK)
return status;
}
ms = mlfi_close((SMFICTX *) tctx);
if (MLFI_OUTPUT(ms, tverbose))
{
fprintf(stderr, "%s: mlfi_close() returned %s\n",
progname, milter_status[ms]);
}
return EX_OK;
}