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
|
/* Copyright (c) 2002-2018 Dovecot authors, see the included COPYING file */
#include "lib.h"
#include "str.h"
#include "strescape.h"
#include "smtp-address.h"
#include "message-parser.h"
#include "message-address.h"
#include "rfc822-parser.h"
struct message_address_parser_context {
pool_t pool;
struct rfc822_parser_context parser;
struct message_address *first_addr, *last_addr, addr;
string_t *str;
bool fill_missing, non_strict_dots;
};
static void add_address(struct message_address_parser_context *ctx)
{
struct message_address *addr;
addr = p_new(ctx->pool, struct message_address, 1);
memcpy(addr, &ctx->addr, sizeof(ctx->addr));
i_zero(&ctx->addr);
if (ctx->first_addr == NULL)
ctx->first_addr = addr;
else
ctx->last_addr->next = addr;
ctx->last_addr = addr;
}
/* quote with "" and escape all '\', '"' and "'" characters if need */
static void str_append_maybe_escape(string_t *dest, const char *cstr, bool escape_dot)
{
const char *p;
/* see if we need to quote it */
for (p = cstr; *p != '\0'; p++) {
if (!IS_ATEXT(*p) && (escape_dot || *p != '.'))
break;
}
if (*p == '\0') {
str_append_data(dest, cstr, (size_t) (p - cstr));
return;
}
/* see if we need to escape it */
for (p = cstr; *p != '\0'; p++) {
if (IS_ESCAPED_CHAR(*p))
break;
}
if (*p == '\0') {
/* only quote */
str_append_c(dest, '"');
str_append_data(dest, cstr, (size_t) (p - cstr));
str_append_c(dest, '"');
return;
}
/* quote and escape */
str_append_c(dest, '"');
str_append_data(dest, cstr, (size_t) (p - cstr));
for (; *p != '\0'; p++) {
if (IS_ESCAPED_CHAR(*p))
str_append_c(dest, '\\');
str_append_c(dest, *p);
}
str_append_c(dest, '"');
}
static int
parse_nonstrict_dot_atom(struct rfc822_parser_context *ctx, string_t *str)
{
int ret = -1;
do {
while (*ctx->data == '.') {
str_append_c(str, '.');
ctx->data++;
if (ctx->data == ctx->end) {
/* @domain is missing, but local-part
parsing was successful */
return 0;
}
ret = 1;
}
if (*ctx->data == '@')
break;
ret = rfc822_parse_atom(ctx, str);
} while (ret > 0 && *ctx->data == '.');
return ret;
}
static int parse_local_part(struct message_address_parser_context *ctx)
{
int ret;
/*
local-part = dot-atom / quoted-string / obs-local-part
obs-local-part = word *("." word)
*/
i_assert(ctx->parser.data < ctx->parser.end);
str_truncate(ctx->str, 0);
if (*ctx->parser.data == '"')
ret = rfc822_parse_quoted_string(&ctx->parser, ctx->str);
else if (!ctx->non_strict_dots)
ret = rfc822_parse_dot_atom(&ctx->parser, ctx->str);
else
ret = parse_nonstrict_dot_atom(&ctx->parser, ctx->str);
if (ret < 0)
return -1;
ctx->addr.mailbox = p_strdup(ctx->pool, str_c(ctx->str));
return ret;
}
static int parse_domain(struct message_address_parser_context *ctx)
{
int ret;
str_truncate(ctx->str, 0);
if ((ret = rfc822_parse_domain(&ctx->parser, ctx->str)) < 0)
return -1;
ctx->addr.domain = p_strdup(ctx->pool, str_c(ctx->str));
return ret;
}
static int parse_domain_list(struct message_address_parser_context *ctx)
{
int ret;
/* obs-domain-list = "@" domain *(*(CFWS / "," ) [CFWS] "@" domain) */
str_truncate(ctx->str, 0);
for (;;) {
if (ctx->parser.data >= ctx->parser.end)
return 0;
if (*ctx->parser.data != '@')
break;
if (str_len(ctx->str) > 0)
str_append_c(ctx->str, ',');
str_append_c(ctx->str, '@');
if ((ret = rfc822_parse_domain(&ctx->parser, ctx->str)) <= 0)
return ret;
while (rfc822_skip_lwsp(&ctx->parser) > 0 &&
*ctx->parser.data == ',')
ctx->parser.data++;
}
ctx->addr.route = p_strdup(ctx->pool, str_c(ctx->str));
return 1;
}
static int parse_angle_addr(struct message_address_parser_context *ctx,
bool parsing_path)
{
/* "<" [ "@" route ":" ] local-part "@" domain ">" */
i_assert(*ctx->parser.data == '<');
ctx->parser.data++;
if (rfc822_skip_lwsp(&ctx->parser) <= 0)
return -1;
if (*ctx->parser.data == '@') {
if (parse_domain_list(ctx) > 0 && *ctx->parser.data == ':') {
ctx->parser.data++;
} else if (parsing_path && (ctx->parser.data >= ctx->parser.end || *ctx->parser.data != ':')) {
return -1;
} else {
if (ctx->fill_missing)
ctx->addr.route = "INVALID_ROUTE";
if (ctx->parser.data >= ctx->parser.end)
return -1;
/* try to continue anyway */
}
if (rfc822_skip_lwsp(&ctx->parser) <= 0)
return -1;
}
if (*ctx->parser.data == '>') {
/* <> address isn't valid */
} else {
if (parse_local_part(ctx) <= 0)
return -1;
if (*ctx->parser.data == '@') {
if (parse_domain(ctx) <= 0)
return -1;
}
}
if (*ctx->parser.data != '>')
return -1;
ctx->parser.data++;
return rfc822_skip_lwsp(&ctx->parser);
}
static int parse_name_addr(struct message_address_parser_context *ctx)
{
/*
name-addr = [display-name] angle-addr
display-name = phrase
*/
str_truncate(ctx->str, 0);
if (rfc822_parse_phrase(&ctx->parser, ctx->str) <= 0 ||
*ctx->parser.data != '<')
return -1;
ctx->addr.name = p_strdup(ctx->pool, str_c(ctx->str));
if (*ctx->addr.name == '\0') {
/* Cope with "<address>" without display name */
ctx->addr.name = NULL;
}
if (parse_angle_addr(ctx, FALSE) < 0) {
/* broken */
if (ctx->fill_missing)
ctx->addr.domain = "SYNTAX_ERROR";
ctx->addr.invalid_syntax = TRUE;
}
return ctx->parser.data < ctx->parser.end ? 1 : 0;
}
static int parse_addr_spec(struct message_address_parser_context *ctx)
{
/* addr-spec = local-part "@" domain */
int ret, ret2 = -2;
i_assert(ctx->parser.data < ctx->parser.end);
str_truncate(ctx->parser.last_comment, 0);
bool quoted_string = *ctx->parser.data == '"';
ret = parse_local_part(ctx);
if (ret <= 0) {
/* end of input or parsing local-part failed */
ctx->addr.invalid_syntax = TRUE;
}
if (ret != 0 && ctx->parser.data < ctx->parser.end &&
*ctx->parser.data == '@') {
ret2 = parse_domain(ctx);
if (ret2 <= 0)
ret = ret2;
}
if (str_len(ctx->parser.last_comment) > 0)
ctx->addr.name = p_strdup(ctx->pool, str_c(ctx->parser.last_comment));
else if (ret2 == -2) {
/* So far we've read user without @domain and without
(Display Name). We'll assume that a single "user" (already
read into addr.mailbox) is a mailbox, but if it's followed
by anything else it's a display-name. */
str_append_c(ctx->str, ' ');
size_t orig_str_len = str_len(ctx->str);
(void)rfc822_parse_phrase(&ctx->parser, ctx->str);
if (str_len(ctx->str) != orig_str_len) {
ctx->addr.mailbox = NULL;
ctx->addr.name = p_strdup(ctx->pool, str_c(ctx->str));
} else {
if (!quoted_string)
ctx->addr.domain = "";
}
ctx->addr.invalid_syntax = TRUE;
ret = -1;
}
return ret;
}
static void add_fixed_address(struct message_address_parser_context *ctx)
{
if (ctx->addr.mailbox == NULL) {
ctx->addr.mailbox = !ctx->fill_missing ? "" : "MISSING_MAILBOX";
ctx->addr.invalid_syntax = TRUE;
}
if (ctx->addr.domain == NULL || ctx->addr.domain[0] == '\0') {
ctx->addr.domain = !ctx->fill_missing ? "" : "MISSING_DOMAIN";
ctx->addr.invalid_syntax = TRUE;
}
add_address(ctx);
}
static int parse_mailbox(struct message_address_parser_context *ctx)
{
const unsigned char *start;
int ret;
/* mailbox = name-addr / addr-spec */
start = ctx->parser.data;
if ((ret = parse_name_addr(ctx)) < 0) {
/* nope, should be addr-spec */
ctx->parser.data = start;
ret = parse_addr_spec(ctx);
if (ctx->addr.invalid_syntax && ctx->addr.name == NULL &&
ctx->addr.mailbox != NULL && ctx->addr.domain == NULL) {
ctx->addr.name = ctx->addr.mailbox;
ctx->addr.mailbox = NULL;
}
}
if (ret < 0)
ctx->addr.invalid_syntax = TRUE;
add_fixed_address(ctx);
return ret;
}
static int parse_group(struct message_address_parser_context *ctx)
{
int ret;
/*
group = display-name ":" [mailbox-list / CFWS] ";" [CFWS]
display-name = phrase
*/
str_truncate(ctx->str, 0);
if (rfc822_parse_phrase(&ctx->parser, ctx->str) <= 0 ||
*ctx->parser.data != ':')
return -1;
/* from now on don't return -1 even if there are problems, so that
the caller knows this is a group */
ctx->parser.data++;
if ((ret = rfc822_skip_lwsp(&ctx->parser)) <= 0)
ctx->addr.invalid_syntax = TRUE;
ctx->addr.mailbox = p_strdup(ctx->pool, str_c(ctx->str));
add_address(ctx);
if (ret > 0 && *ctx->parser.data != ';') {
for (;;) {
/* mailbox-list =
(mailbox *("," mailbox)) / obs-mbox-list */
if (parse_mailbox(ctx) <= 0) {
/* broken mailbox - try to continue anyway. */
}
if (ctx->parser.data >= ctx->parser.end ||
*ctx->parser.data != ',')
break;
ctx->parser.data++;
if (rfc822_skip_lwsp(&ctx->parser) <= 0) {
ret = -1;
break;
}
}
}
if (ret >= 0) {
if (ctx->parser.data >= ctx->parser.end ||
*ctx->parser.data != ';')
ret = -1;
else {
ctx->parser.data++;
ret = rfc822_skip_lwsp(&ctx->parser);
}
}
if (ret < 0)
ctx->addr.invalid_syntax = TRUE;
add_address(ctx);
return ret == 0 ? 0 : 1;
}
static int parse_address(struct message_address_parser_context *ctx)
{
const unsigned char *start;
int ret;
/* address = mailbox / group */
start = ctx->parser.data;
if ((ret = parse_group(ctx)) < 0) {
/* not a group, try mailbox */
ctx->parser.data = start;
ret = parse_mailbox(ctx);
}
return ret;
}
static int parse_address_list(struct message_address_parser_context *ctx,
unsigned int max_addresses)
{
int ret = 0;
/* address-list = (address *("," address)) / obs-addr-list */
while (max_addresses > 0) {
max_addresses--;
if ((ret = parse_address(ctx)) == 0)
break;
if (ctx->parser.data >= ctx->parser.end ||
*ctx->parser.data != ',') {
ret = -1;
break;
}
ctx->parser.data++;
if ((ret = rfc822_skip_lwsp(&ctx->parser)) <= 0) {
if (ret < 0) {
/* ends with some garbage */
add_fixed_address(ctx);
}
break;
}
}
return ret;
}
static int parse_path(struct message_address_parser_context *ctx)
{
int ret;
if (rfc822_skip_lwsp(&ctx->parser) <= 0)
return -1;
if (*ctx->parser.data != '<') {
/* Cope with paths that omit < and >. This is a syntax
violation, but we allow it to account for a rather wide
selection of software that does not follow the standards.
*/
if ((ret=parse_local_part(ctx)) > 0 &&
*ctx->parser.data == '@') {
ret = parse_domain(ctx);
}
} else {
ret = parse_angle_addr(ctx, TRUE);
}
if (ret < 0 || (ret=rfc822_skip_lwsp(&ctx->parser)) < 0 ||
ctx->parser.data != ctx->parser.end ||
(ctx->addr.mailbox != NULL &&
(ctx->addr.domain == NULL || *ctx->addr.domain == '\0')) ||
(ctx->addr.mailbox == NULL && ctx->addr.domain != NULL)) {
ctx->addr.invalid_syntax = TRUE;
ret = -1;
}
add_address(ctx);
return ret;
}
static struct message_address *
message_address_parse_real(pool_t pool, const unsigned char *data, size_t size,
unsigned int max_addresses,
enum message_address_parse_flags flags)
{
struct message_address_parser_context ctx;
i_zero(&ctx);
rfc822_parser_init(&ctx.parser, data, size, t_str_new(128));
ctx.parser.nul_replacement_str = RFC822_NUL_REPLACEMENT_STR;
ctx.pool = pool;
ctx.str = t_str_new(128);
ctx.fill_missing = (flags & MESSAGE_ADDRESS_PARSE_FLAG_FILL_MISSING) != 0;
ctx.non_strict_dots = (flags & MESSAGE_ADDRESS_PARSE_FLAG_STRICT_DOTS) == 0;
if (rfc822_skip_lwsp(&ctx.parser) <= 0) {
/* no addresses */
} else {
(void)parse_address_list(&ctx, max_addresses);
}
rfc822_parser_deinit(&ctx.parser);
return ctx.first_addr;
}
static int
message_address_parse_path_real(pool_t pool, const unsigned char *data,
size_t size, struct message_address **addr_r)
{
struct message_address_parser_context ctx;
int ret;
i_zero(&ctx);
*addr_r = NULL;
rfc822_parser_init(&ctx.parser, data, size, NULL);
ctx.pool = pool;
ctx.str = t_str_new(128);
ret = parse_path(&ctx);
rfc822_parser_deinit(&ctx.parser);
*addr_r = ctx.first_addr;
return (ret < 0 ? -1 : 0);
}
struct message_address *
message_address_parse(pool_t pool, const unsigned char *data, size_t size,
unsigned int max_addresses,
enum message_address_parse_flags flags)
{
struct message_address *addr;
if (pool->datastack_pool) {
return message_address_parse_real(pool, data, size,
max_addresses, flags);
}
T_BEGIN {
addr = message_address_parse_real(pool, data, size,
max_addresses, flags);
} T_END;
return addr;
}
int message_address_parse_path(pool_t pool, const unsigned char *data,
size_t size, struct message_address **addr_r)
{
int ret;
if (pool->datastack_pool) {
return message_address_parse_path_real(pool, data, size, addr_r);
}
T_BEGIN {
ret = message_address_parse_path_real(pool, data, size, addr_r);
} T_END;
return ret;
}
void message_address_write(string_t *str, const struct message_address *addr)
{
const char *tmp;
bool first = TRUE, in_group = FALSE;
if (addr == NULL)
return;
/* <> path */
if (addr->mailbox == NULL && addr->domain == NULL) {
i_assert(addr->next == NULL);
str_append(str, "<>");
return;
}
/* a) mailbox@domain
b) name <@route:mailbox@domain>
c) group: .. ; */
while (addr != NULL) {
if (first)
first = FALSE;
else
str_append(str, ", ");
if (addr->domain == NULL) {
if (!in_group) {
/* beginning of group. mailbox is the group
name, others are NULL. */
if (addr->mailbox != NULL && *addr->mailbox != '\0') {
/* check for MIME encoded-word */
if (strstr(addr->mailbox, "=?") != NULL)
/* MIME encoded-word MUST NOT appear within a 'quoted-string'
so escaping and quoting of phrase is not possible, instead
use obsolete RFC822 phrase syntax which allow spaces */
str_append(str, addr->mailbox);
else
str_append_maybe_escape(str, addr->mailbox, TRUE);
} else {
/* empty group name needs to be quoted */
str_append(str, "\"\"");
}
str_append(str, ": ");
first = TRUE;
} else {
/* end of group. all fields should be NULL. */
i_assert(addr->mailbox == NULL);
/* cut out the ", " */
tmp = str_c(str)+str_len(str)-2;
i_assert((tmp[0] == ',' || tmp[0] == ':') && tmp[1] == ' ');
if (tmp[0] == ',' && tmp[1] == ' ')
str_truncate(str, str_len(str)-2);
else if (tmp[0] == ':' && tmp[1] == ' ')
str_truncate(str, str_len(str)-1);
str_append_c(str, ';');
}
in_group = !in_group;
} else {
/* "Display Name" <mailbox@domain> */
i_assert(addr->mailbox != NULL);
if (addr->name != NULL) {
/* check for MIME encoded-word */
if (strstr(addr->name, "=?") != NULL)
/* MIME encoded-word MUST NOT appear within a 'quoted-string'
so escaping and quoting of phrase is not possible, instead
use obsolete RFC822 phrase syntax which allow spaces */
str_append(str, addr->name);
else
str_append_maybe_escape(str, addr->name, TRUE);
}
if (addr->route != NULL ||
addr->mailbox[0] != '\0' ||
addr->domain[0] != '\0') {
if (addr->name != NULL && addr->name[0] != '\0')
str_append_c(str, ' ');
str_append_c(str, '<');
if (addr->route != NULL) {
str_append(str, addr->route);
str_append_c(str, ':');
}
if (addr->mailbox[0] == '\0')
str_append(str, "\"\"");
else
str_append_maybe_escape(str, addr->mailbox, FALSE);
if (addr->domain[0] != '\0') {
str_append_c(str, '@');
str_append(str, addr->domain);
}
str_append_c(str, '>');
}
}
addr = addr->next;
}
}
const char *message_address_to_string(const struct message_address *addr)
{
string_t *str = t_str_new(256);
message_address_write(str, addr);
return str_c(str);
}
const char *message_address_first_to_string(const struct message_address *addr)
{
struct message_address first_addr;
first_addr = *addr;
first_addr.next = NULL;
first_addr.route = NULL;
return message_address_to_string(&first_addr);
}
void message_address_init(struct message_address *addr,
const char *name, const char *mailbox, const char *domain)
{
i_zero(addr);
addr->name = name;
addr->mailbox = mailbox;
addr->domain = domain;
}
void message_address_init_from_smtp(struct message_address *addr,
const char *name, const struct smtp_address *smtp_addr)
{
i_zero(addr);
addr->name = name;
addr->mailbox = smtp_addr->localpart;
addr->domain = smtp_addr->domain;
}
static const char *address_headers[] = {
"From", "Sender", "Reply-To",
"To", "Cc", "Bcc",
"Resent-From", "Resent-Sender", "Resent-To", "Resent-Cc", "Resent-Bcc"
};
bool message_header_is_address(const char *hdr_name)
{
unsigned int i;
for (i = 0; i < N_ELEMENTS(address_headers); i++) {
if (strcasecmp(hdr_name, address_headers[i]) == 0)
return TRUE;
}
return FALSE;
}
|