forked from trusteddomainproject/OpenDMARC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.c
675 lines (587 loc) · 12.3 KB
/
parse.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
/*
** Copyright (c) 2005, 2007, 2008 Sendmail, Inc. and its suppliers.
** All rights reserved.
**
** Copyright (c) 2009, 2010, 2012, 2021, The Trusted Domain Project.
** All rights reserved.
*/
/* system inludes */
#include <sys/types.h>
#include <ctype.h>
#include <string.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
/* opendmarc includes */
#include "util.h"
#ifndef FALSE
# define FALSE 0
#endif /* ! FALSE */
#ifndef TRUE
# define TRUE 1
#endif /* ! TRUE */
/* types */
typedef unsigned long cmap_elem_type;
/* symbolic names */
#define MAILPARSE_OK 0 /* success */
#define MAILPARSE_ERR_PUNBALANCED 1 /* unbalanced parentheses */
#define MAILPARSE_ERR_QUNBALANCED 2 /* unbalanced quotes */
#define MAILPARSE_ERR_SUNBALANCED 3 /* unbalanced sq. brackets */
#define MAILPARSE_ERR_MULTIVALUE 4 /* multiple possible values */
/* a bitmap for the "specials" character class */
#define CMAP_NBITS (sizeof(cmap_elem_type) * CHAR_BIT)
#define CMAP_NELEMS ((1 + UCHAR_MAX) / CMAP_NBITS)
#define CMAP_INDEX(i) ((unsigned char)(i) / CMAP_NBITS)
#define CMAP_BIT(i) (1L << (unsigned char)(i) % CMAP_NBITS)
#define CMAP_TST(ar, c) ((ar)[CMAP_INDEX(c)] & CMAP_BIT(c))
#define CMAP_SET(ar, c) ((ar)[CMAP_INDEX(c)] |= CMAP_BIT(c))
static unsigned char const SPECIALS[] = "<>@,;:\\\"/[]?=";
#ifdef MAILPARSE_TEST
/*
** DMARCF_MAIL_UNESCAPE -- remove escape characters from a string
**
** Parameters:
** s -- the string to be unescaped
**
** Return value:
** s.
*/
static char *
dmarcf_mail_unescape(char *s)
{
char *w;
char const *r, *p, *e;
if (s == NULL)
return NULL;
r = w = s;
e = s + strlen(s);
while ((p = memchr(r, '\\', e - s)) != NULL)
{
if (p > s)
{
if (r != w)
memmove(w, r, p - r);
w += p - r;
}
if (p[1] == '\0')
{
r = p + 1;
}
else
{
*w++ = p[1];
r = p + 2;
}
}
if (r > w)
{
if (e > r)
{
memmove(w, r, e - r);
w += e - r;
}
*w = '\0';
}
return s;
}
#endif /* MAILPARSE_TEST */
/*
** DMARCF_MAIL_MATCHING_PAREN -- return the location past matching opposite
** parentheses
**
** Parameters:
** s -- start of string to be processed
** e -- end of string to be processed
** open_paren -- open parenthesis character
** close_paren -- close parenthesis character
**
** Return value:
** Location of the final close parenthesis character in the string.
** For example, given "xxx((yyyy)zz)aaaa", would return the location
** of the second ")". There may be more beyond that, but at that point
** everything is balanced.
*/
static u_char *
dmarcf_mail_matching_paren(u_char *s, u_char *e, int open_paren, int close_paren)
{
int paren = 1;
for (; s < e; s++)
{
if (*s == close_paren)
{
if (--paren == 0)
break;
}
else if (*s == open_paren)
{
paren++;
}
else if (*s == '\\')
{
if (s[1] != '\0')
s++;
}
}
return s;
}
/*
** DMARCF_FIRST_SPECIAL -- find the first "special" character
**
** Parameters:
** p -- input string
** e -- end of input string
** special_out -- pointer to the first special character found
**
** Return value:
** 0 on success, or an MAILPARSE_ERR_* on failure.
*/
static int
dmarcf_mail_first_special(u_char *p, u_char *e, u_char **special_out)
{
size_t i;
cmap_elem_type is_special[CMAP_NELEMS] = { 0 };
u_char *at_ptr = NULL;
/* set up special finder */
for (i = 0; SPECIALS[i] != '\0'; i++)
CMAP_SET(is_special, SPECIALS[i]);
for (; p < e && *p != '\0'; p++)
{
/* skip white space between tokens */
while (p < e && (*p == '(' ||
(isascii(*p) && isspace(*p))))
{
if (*p != '(')
{
p++;
}
else
{
p = dmarcf_mail_matching_paren(p + 1, e,
'(', ')');
if (*p == '\0')
return MAILPARSE_ERR_PUNBALANCED;
else
p++;
}
}
if (*p == '\0')
break;
if (*p == '"')
{
p = dmarcf_mail_matching_paren(p + 1, e, '\0', '"');
if (*p == '\0')
return MAILPARSE_ERR_QUNBALANCED;
}
else if (*p == '[')
{
p = dmarcf_mail_matching_paren(p + 1, e, '\0', ']');
if (*p == '\0')
return MAILPARSE_ERR_SUNBALANCED;
}
else if (CMAP_TST(is_special, *p))
{
if (*p == '<')
{
*special_out = p;
return 0;
}
else if (*p == ':' || *p == ';' || *p == ',')
{
if (at_ptr != NULL)
*special_out = at_ptr;
else
*special_out = p;
return 0;
}
else if (*p == '@')
{
at_ptr = p;
}
}
else
{
while (*p != '\0' &&
!CMAP_TST(is_special, *p) &&
(!isascii(*p) ||
!isspace((unsigned char) *p)) &&
*p != '(')
p++;
p--;
}
}
*special_out = p;
return 0;
}
/*
** DMARCF_MAIL_TOKEN -- find the next token
**
** Parameters:
** s -- start of input string
** e -- end of input string
** type_out -- type of token (returned)
** start_out -- start of token (returned)
** end_out -- start of token (returned)
** uncommented_whitespace -- set to TRUE if uncommented whitespace is
** discovered (returned)
**
** Return value:
** 0 on success, or an MAILPARSE_ERR_* on failure.
*/
static int
dmarcf_mail_token(u_char *s, u_char *e, int *type_out, u_char **start_out,
u_char **end_out, int *uncommented_whitespace)
{
u_char *p;
int err = 0;
size_t i;
int token_type;
cmap_elem_type is_special[CMAP_NELEMS] = { 0 };
u_char *token_start, *token_end;
*start_out = NULL;
*end_out = NULL;
*type_out = 0;
err = 0;
/* set up special finder */
for (i = 0; SPECIALS[i] != '\0'; i++)
CMAP_SET(is_special, SPECIALS[i]);
p = s;
/* skip white space between tokens */
while (p < e && (*p == '(' ||
(isascii((unsigned char) *p) &&
isspace((unsigned char) *p))))
{
if (*p != '(')
{
*uncommented_whitespace = 1;
p++;
}
else
{
p = dmarcf_mail_matching_paren(p + 1, e, '(', ')');
if (*p == '\0')
return MAILPARSE_ERR_PUNBALANCED;
else
p++;
}
}
if (p >= e || *p == '\0')
return 0;
/* our new token starts here */
token_start = p;
/* fill in the token contents and type */
if (*p == '"')
{
token_end = dmarcf_mail_matching_paren(p + 1, e, '\0', '"');
token_type = '"';
if (*token_end != '\0')
token_end++;
else
err = MAILPARSE_ERR_QUNBALANCED;
}
else if (*p == '[')
{
token_end = p = dmarcf_mail_matching_paren(p + 1, e, '\0', ']');
token_type = '[';
if (*token_end != '\0')
token_end++;
else
err = MAILPARSE_ERR_SUNBALANCED;
}
else if (CMAP_TST(is_special, *p))
{
token_end = p + 1;
token_type = *p;
}
else
{
while (p < e && *p != '\0' && !CMAP_TST(is_special, *p) &&
(!isascii(*p) || !isspace((unsigned char) *p)) &&
*p != '(')
p++;
token_end = p;
token_type = 'x';
}
*start_out = token_start;
*end_out = token_end;
*type_out = token_type;
return err;
}
/*
** DMARCF_MAIL_PARSE -- extract the local-part and hostname from a mail
** header field, e.g. "From:"
**
** Parameters:
** line -- input line
** user_out -- pointer to "local-part" (returned)
** domain_out -- pointer to hostname (returned)
**
** Return value:
** 0 on success, or an MAILPARSE_ERR_* on failure.
**
** Notes:
** Input string is modified.
*/
int
dmarcf_mail_parse(unsigned char *line, unsigned char **user_out,
unsigned char **domain_out)
{
int type;
int ws;
int err;
u_char *e, *special;
u_char *tok_s, *tok_e;
u_char *w;
*user_out = NULL;
*domain_out = NULL;
err = 0;
w = line;
e = line + strlen((char *) line);
ws = 0;
for (;;)
{
err = dmarcf_mail_first_special(line, e, &special);
if (err != 0)
return err;
/* given the construct we're looking at, do the right thing */
switch (*special)
{
case '<':
/* display name <address> */
line = special + 1;
for (;;)
{
err = dmarcf_mail_token(line, e, &type, &tok_s,
&tok_e, &ws);
if (err != 0)
return err;
if (type == '>' || type == '\0')
{
*w = '\0';
return 0;
}
else if (type == '@')
{
*w++ = '\0';
*domain_out = w;
}
else if (type == ',' || type == ':')
{
/* source route punctuation */
*user_out = NULL;
*domain_out = NULL;
}
else
{
if (*user_out == NULL)
*user_out = w;
memmove(w, tok_s, tok_e - tok_s);
w += tok_e - tok_s;
}
line = tok_e;
}
case ';':
case ':':
case ',':
/* skip a group name or result */
line = special + 1;
break;
default:
/* (display name) addr(display name)ess */
ws = 0;
for (;;)
{
err = dmarcf_mail_token(line, e, &type, &tok_s,
&tok_e, &ws);
if (err != 0)
return err;
if (type == '\0' || type == ',' || type == ';')
{
*w = '\0';
break;
}
else if (type == '@')
{
*w++ = '\0';
*domain_out = w;
ws = 0;
}
else
{
if (*user_out == NULL)
*user_out = w;
else if (type == 'x' && ws == 1)
*w++ = ' ';
memmove(w, tok_s, tok_e - tok_s);
w += tok_e - tok_s;
ws = 0;
}
line = tok_e;
}
return 0;
}
}
}
/*
** DMARCF_MAIL_PARSE_MULTI -- extract the local-part and hostname from a mail
** header field that might contain multiple
** values, e.g. "To:", "Cc:"
**
** Parameters:
** line -- input line
** users_out -- array of pointers to "local-part" (returned)
** domains_out -- array of pointers to hostname (returned)
** out -- count of entries of the above arrays, not including the NULL
**
** Return value:
** 0 on success, or an DKIM_MAILPARSE_ERR_* on failure.
**
** Notes:
** Input string is modified.
*/
int
dmarcf_mail_parse_multi(unsigned char *line, unsigned char ***users_out,
unsigned char ***domains_out, unsigned int *out)
{
_Bool escaped = FALSE;
_Bool quoted = FALSE;
_Bool done = FALSE;
int a = 0;
unsigned int n = 0;
int status;
int parens = 0;
char *p;
char *addr;
unsigned char **uout = NULL;
unsigned char **dout = NULL;
unsigned char *u;
unsigned char *d;
/* walk the input string looking for unenclosed commas */
addr = line;
for (p = line; !done; p++)
{
if (escaped)
{
escaped = FALSE;
continue;
}
switch (*p)
{
case '\\':
escaped = TRUE;
continue;
case '"':
quoted = !quoted;
continue;
case '(':
parens++;
continue;
case ')':
parens--;
continue;
case ',':
/* skip it if it's quoted or in a comment */
if (parens != 0 || quoted)
continue;
/* FALLTHROUGH */
case '\0':
if (*p == '\0')
done = TRUE;
else
*p = '\0';
status = dmarcf_mail_parse(addr, &u, &d);
if (status != 0)
{
if (uout != NULL)
{
free(uout);
free(dout);
}
return status;
}
if (n == 0)
{
size_t newsize = 2 * sizeof(unsigned char *);
uout = (unsigned char **) malloc(newsize);
if (uout == NULL)
return -1;
dout = (unsigned char **) malloc(newsize);
if (dout == NULL)
{
free(uout);
return -1;
}
a = 2;
}
else if (n + 1 == a)
{
unsigned char **new;
size_t newsize = a * 2 * sizeof(unsigned char *);
new = (unsigned char **) realloc(uout, newsize);
if (new == NULL)
{
free(uout);
free(dout);
return -1;
}
uout = new;
new = (unsigned char **) realloc(dout, newsize);
if (new == NULL)
{
free(uout);
free(dout);
return -1;
}
dout = new;
a *= 2;
}
uout[n] = u;
dout[n++] = d;
uout[n] = (char *) NULL;
dout[n] = (char *) NULL;
addr = p + 1;
break;
default:
break;
}
}
*users_out = uout;
*domains_out = dout;
*out = n;
return 0;
}
#ifdef MAILPARSE_TEST
char *string_or_null(char *str)
{
if (str == (char *) NULL)
return "null";
else
return str;
}
int
main(int argc, char **argv)
{
int err;
unsigned char **domains, **users;
if (argc != 2)
{
fprintf(stderr, "Usage: %s mailheader\n", argv[0]);
exit(64);
}
err = dmarcf_mail_parse_multi(argv[1], &users, &domains);
if (err)
{
printf("error %d\n", err);
}
else
{
int n;
for (n = 0; domains[n] != (unsigned char *) NULL; n++)
{
printf("user: '%s'\ndomain: '%s'\n",
string_or_null(users[n]),
string_or_null(domains[n]));
}
}
return 0;
}
#endif /* MAILPARSE_TEST */