File: parse2822.c

package info (click to toggle)
slrn 1.0.3+dfsg-3
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 6,412 kB
  • sloc: ansic: 39,852; sh: 3,777; perl: 467; makefile: 229; sed: 24
file content (723 lines) | stat: -rw-r--r-- 17,500 bytes parent folder | download | duplicates (2)
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
/* -*- mode: C; mode: fold -*- */
/*
 This file is part of SLRN.

 Copyright (c) 2009-2016 John E. Davis <jed@jedsoft.org>

 This program is free software; you can redistribute it and/or modify it
 under the terms of the GNU General Public License as published by the Free
 Software Foundation; either version 2 of the License, or (at your option)
 any later version.

 This program is distributed in the hope that it will be useful, but WITHOUT
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 more details.

 You should have received a copy of the GNU General Public License along
 with this program; if not, write to the Free Software Foundation, Inc.,
 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

#include <stdio.h>
#include <string.h>

#include "config.h"
#ifndef SLRNPULL_CODE
#include "slrnfeat.h"
#endif

#ifdef HAVE_STDLIB_H
# include <stdlib.h>
#endif

#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif

#include "parse2822.h"
#include "strutil.h"

/* The grammar from rfc2822 is:
 *
 *   address        =  mailbox | group
 *   mailbox        =  name-addr | addr-spec
 *   name-addr      =  [display-name] angle-addr
 *   angle-addr     =  [CFWS] "<" addr-spec ">" CFWS  | obs-angle-addr
 *   group          =  display-name ":" [mailbox-list | CFWS ] ";"
 *   display-name   =  phrase
 *   mailbox-list   =  (mailbox *("," mailbox)) | obs-mbox-list
 *   addr-spec      =  local-part "@" domain
 *   local-part     =  dot-atom | quoted-string | obs-local-part
 *   domain         =  dot-atom | domain-literal | obs-domain
 *   domain-literal =  [CFWS] "[" *([FWS] dcontent) [FWS] "]" [CFWS]
 *   dcontent       =  dtext | quoted-pair
 *   dtext          =  non-white-space-ctrl | 33-90 | 94-126
 *   dot-atom       =  [CFWS] dot-atom-text [CFWS]
 *   dot-atom-text  =  1*atext *("." 1*atext)
 *   atom           =  [CFWS] 1*atext [CFWS]
 *   atext          =  ascii except controls, space, and specials
 *   quoted-string  =  [CFWS] DQUOTE *([FWS qcontent) [FWS] DQUOTE [CFWS]
 *   qcontent       =  qtext | quoted-pair
 *   qtext          =  NO-WS-CTRL | 33-126 except \ and "
 *   FWS            =  ([*WSP CRLF] 1*WSP) | obs-FWS
 *   WSP            =  SPACE | TAB
 *   CFWS           =  *([FWS] comment) (([FWS] comment) | FWS)
 *   comment        =  "("  *([FWS] ccontent) [FWS] ")"
 *   ccontent       =  ctext | quoted-pair | comment
 *   ctext          =  NO-WS-CTRL | 33-39 | 42-91 | 93-126
 *   quoted-pair    =  ("\" text )
 *   text           =  ASCII except CR and LF
 *
 * Obsolete:
 *
 *   word           = atom | quoted-string
 *   phrase         = 1*word | obs-phrase
 *   obs-phrase     = word *(word | "." | CFWS)
 *   obs-local-part = word *("." word)
 *
 */
#define TYPE_ADD_ONLY 1
#define TYPE_OLD_STYLE 2
#define TYPE_RFC_2822 3

#define RFC_2822_SPECIAL_CHARS "()<>[]:;@\\,.\""
#define RFC_2822_NOT_ATOM_CHARS RFC_2822_SPECIAL_CHARS
#define RFC_2822_NOT_DOTATOM_CHARS "(),:;<>@[\\]\""
#define RFC_2822_NOT_QUOTED_CHARS "\t\\\""
#define RFC_2822_NOT_DOMLIT_CHARS "[\\]"
#define RFC_2822_NOT_COMMENT_CHARS "(\\)"

#define IS_RFC2822_SPECIAL(ch) \
   (NULL != slrn_strbyte(RFC_2822_SPECIAL_CHARS, ch))
#define IS_RFC2822_ATEXT(ch) \
   (((unsigned char)(ch) > 32) && !IS_RFC2822_SPECIAL(ch))
#define IS_RFC2822_PHRASE_CHAR(ch) \
   (((ch) == ' ') || ((ch) == '\t') || ((ch) == '.') || IS_RFC2822_ATEXT(ch))

static int check_quoted_pair (char *p, char *pmax, char **errmsg)
{
   char ch;

   if (p == pmax)
     {
	*errmsg = _("Expecting a quoted-pair in the header.");
	return -1;
     }

   ch = *p;
   if ((ch == '\r') || (ch == '\n'))
     {
	*errmsg = _("Illegal quoted-pair character in header.");
	return -1;
     }

   return 0;
}

static char *skip_quoted_string (char *p, char *pmax, char **errmsg)
{
   while (p < pmax)
     {
	char ch = *p++;

	if (ch == '"')
	  return p;

	if (ch == '\\')
	  {
	     if (-1 == check_quoted_pair (p, pmax, errmsg))
	       return NULL;

	     p++;
	     continue;
	  }

	if (NULL != slrn_strbyte(RFC_2822_NOT_QUOTED_CHARS, ch))
	  {
	     *errmsg = _("Illegal char in displayname of address header.");
	     return NULL;
	  }
     }

   *errmsg = _("Quoted string opened but never closed in address header.");
   return NULL;
}

/* This function gets called with *startp positioned to the character past the
 * opening '('.  Find the matching ')' and encode everything in between.
 */
static int parse_rfc2822_comment (char *header, char *parsemap, unsigned int *startp, unsigned int stop, char **errmsg) /*{{{*/
{
   unsigned int start;

   start = *startp;
   while (start < stop)
     {
	unsigned char ch = (unsigned char) header[start];

	if (ch == '(')
	  {
	     start++;
	     if (-1 == parse_rfc2822_comment (header, parsemap, &start, stop, errmsg))
	       {
		  *startp = start;
		  return -1;
	       }
	     continue;
	  }

	if (ch == ')')
	  {
	     start++;
	     *startp = start;
	     return 0;
	  }

	if (ch == '\\')
	  {
	     parsemap[start] = 'C';
	     start++;
	     if (-1 == check_quoted_pair (header+start, header+stop, errmsg))
	       {
		  *startp = start;
		  return -1;
	       }
	     parsemap[start] = 'C';
	     start++;
	     continue;
	  }

	if (NULL != slrn_strbyte(RFC_2822_NOT_COMMENT_CHARS, ch))
	  {
	     *errmsg = _("Illegal char in displayname of address header.");
	     *startp = start;
	     return -1;
	  }
	parsemap[start] = 'C';
	start++;
     }

   *errmsg = _("Comment opened but never closed in address header.");
   return -1;
}

/*}}}*/

static int parse_rfc2822_cfws (char *header, char *parsemap, unsigned int *startp, unsigned int stop, char **errmsg)
{
   while (1)
     {
	char *p0, *p, *pmax;

	p = p0 = header + *startp;
	pmax = header + stop;

	while ((p < pmax) && ((*p == ' ') || (*p == '\t')))
	  p++;

	*startp = (unsigned int) (p - header);
	if ((p == pmax) || (*p != '('))
	  return 0;

	*startp += 1;		       /* skip ( */
	if (-1 == parse_rfc2822_comment (header, parsemap, startp, stop, errmsg))
	  return -1;
     }
}

static int parse_rfc2822_atext (char *header, char *parsemap, unsigned int *startp, unsigned int stop, char **errmsg)
{
   unsigned int start;
   char ch;

   (void) parsemap;
#define IS_NOT_ATEXT_CHAR(ch) \
   (((ch) & 0x80) || ((unsigned char)(ch) <= ' ') || ((ch) == '.') \
       || (NULL != slrn_strbyte(RFC_2822_NOT_DOTATOM_CHARS, (ch))))

   start = *startp;
   if (start >= stop)
     {
	*errmsg = _("premature end of parse seen in atext portion of email address");
	return -1;
     }

   ch = header[start];
   if (0 == IS_RFC2822_ATEXT(ch))
     {
	*errmsg = _("Expecting an atext character");
	return -1;
     }
   start++;
   while (start < stop)
     {
	ch = header[start];
	if (0 == IS_RFC2822_ATEXT(ch))
	  break;
	start++;
     }
   *startp = start;
   return 0;
}

/* The assumption here is that the *startp is at the char following the '"' */
static int parse_rfc2822_quoted_string (char *header, char *parsemap, unsigned int *startp, unsigned int stop, char **errmsg)
{
   char *p, *pmax;

   p = header + *startp;
   pmax = header + stop;

   p = skip_quoted_string (p, pmax, errmsg);
   if (p == NULL)
     return -1;

   *startp = (p - header);

   return parse_rfc2822_cfws (header, parsemap, startp, stop, errmsg);
}

/* dotatom: [CFWS] atext [.atext ...] [CFWS]
 * Note that the obsolete forms allow CFWS on both sides of the dot.
 * Moreover, it allows quoted-strings between the dots.
 * This is also permitted here:
 *   [CFWS] atext [[CFWS] "." [CFWS] atext...] [CFWS]
 */
static int parse_rfc2822_dotatom (char *header, char *parsemap, unsigned int *startp, unsigned int stop, char **errmsg, int allow_quoted_string)
{
   if (-1 == parse_rfc2822_cfws (header, parsemap, startp, stop, errmsg))
     return -1;

   if (allow_quoted_string
       && ((*startp < stop) && (header[*startp] == '"')))
     {
	*startp += 1;
	if (-1 == parse_rfc2822_quoted_string (header, parsemap, startp, stop, errmsg))
	  return -1;
     }
   else if (-1 == parse_rfc2822_atext (header, parsemap, startp, stop, errmsg))
     return -1;

   while (*startp < stop)
     {
	if (-1 == parse_rfc2822_cfws (header, parsemap, startp, stop, errmsg))
	  return -1;

	if (header[*startp] != '.')
	  break;

	*startp += 1;

	if (-1 == parse_rfc2822_cfws (header, parsemap, startp, stop, errmsg))
	  return -1;

	if (allow_quoted_string
	    && ((*startp < stop) && (header[*startp] == '"')))
	  {
	     *startp += 1;
	     if (-1 == parse_rfc2822_quoted_string (header, parsemap, startp, stop, errmsg))
	       return -1;
	  }
	else if (-1 == parse_rfc2822_atext (header, parsemap, startp, stop, errmsg))
	  return -1;
     }

   if (-1 == parse_rfc2822_cfws (header, parsemap, startp, stop, errmsg))
     return -1;

   return 0;
}

/* This parses a string that looks like "some phrase <address>".  Stop
 * parsing at the start of <address>.
 *
 * An RFC-2822 phrase consists of "words", which are composed of
 * atoms or quoted strings, or comments, and optionally separated by dots.
 */
static int parse_rfc2822_phrase (char *header, char *parsemap, unsigned int *startp, unsigned int stop, char **errmsg) /*{{{*/
{
   unsigned int start;

   start = *startp;
   while (start < stop)
     {
	unsigned char ch = (unsigned char) header[start];

	if (ch <= 32)
	  {
	     if ((ch == '\r') || (ch == '\n'))
	       {
		  *errmsg = _("Illegal char in displayname of address header.");
		  return -1;
	       }
	     parsemap[start] = 'C';
	     start++;
	     continue;
	  }

	if (ch == '(')
	  {
	     start++;
	     if (-1 == parse_rfc2822_comment (header, parsemap, &start, stop, errmsg))
	       {
		  *startp = start;
		  return -1;
	       }
	     continue;
	  }

	if (ch == '"')
	  {
	     unsigned int start0;
	     start++;
	     start0 = start;
	     if (-1 == parse_rfc2822_quoted_string (header, parsemap, &start, stop, errmsg))
	       {
		  *startp = start;
		  return -1;
	       }

	     while (start0 < start)
	       parsemap[start0++] = 'C';

	     continue;
	  }

	if (!IS_RFC2822_PHRASE_CHAR(ch))
	  break;

	parsemap[start] = 'C';
	start++;
     }

   *startp = start;
   return 0;
}

/*}}}*/

/* The grammar implies:
 *   local-part = dot-atom | quoted-string | obs-local-part
 * Note that the obsolete local part is like the dot-atom, except it
 * permits CFWS to surround the ".".
 */
static int parse_rfc2822_localpart (char *header, char *parsemap, unsigned int *startp, unsigned int stop, char **errmsg) /*{{{*/
{
   return parse_rfc2822_dotatom (header, parsemap, startp, stop, errmsg, 1);
}

/*}}}*/

static int parse_rfc2822_domain (char *header, char *parsemap, unsigned int *startp, unsigned int stop, char **errmsg) /*{{{*/
{
   /* Here domain is a dot atom or an obsolete local part. */
   if (-1 == parse_rfc2822_dotatom (header, parsemap, startp, stop, errmsg, 0))
     return -1;

   return 0;
}

/*}}}*/

static int parse_rfc2822_domainlit (char *header, char *parsemap, unsigned int *start, unsigned int end, char **errmsg) /*{{{*/
{
   unsigned int pos = *start;

   (void) parsemap;

   while (pos < end)
     {
#ifndef HAVE_LIBIDN
	if (header[pos] & 0x80)
	  {
	     *errmsg = _("Non 7-bit char in domain of address header. libidn is not yet supported.");
	     return -1;
	  }
#endif
	if (header[pos] == ']')
	  {
	     *start=pos;
	     return 0;
	  }
	if (NULL != slrn_strbyte (RFC_2822_NOT_DOMLIT_CHARS, header[pos]))
	  {
	     *errmsg = _("Illegal char in domain-literal of address header.");
	     return -1;
	  }
	pos++;
     }
   *errmsg = _("domain-literal opened but never closed.");
   return -1;
}

/*}}}*/

/* The encodes a comma separated list of addresses.  Each item in the list
 * is assumed to be of the following forms:
 *
 *    address (Comment-text)
 *    address (Comment-text)
 *    Comment-text <address>
 *
 * Here address is local@domain, local@[domain], or local.
 *
 * Here is an example of something that is permitted:
 *
 * From: Pete(A wonderful \) chap) <pete(his account)@silly.test(his host)>
 * To:A Group(Some people)
 *    :Chris Jones <c@(Chris's host.)public.example>,
 *        joe@example.org,
 *   John <jdoe@one.test> (my dear friend); (the end of the group)
 * Cc:(Empty list)(start)Undisclosed recipients  :(nobody(that I know))  ;
 *
 * The example shows that the "local" part can contain comments, and that
 * the backquote serves as a quote character in the comments.
 */
static int rfc2822_parse (char *header, char *parsemap, int skip_colon, char **errmsg) /*{{{*/
{
   unsigned int head_start=0, head_end;
   int type=0;
   unsigned int pos=0;
   char ch;

   if (skip_colon)
     {
	while ((0 != (ch = header[head_start]))
	       && (ch != ':'))
	  head_start++;

	if (ch != ':')
	  {
	     *errmsg = _("A colon is missing from the address header");
	     return -1;
	  }
	head_start++;		       /* skip colon */
     }

   while (1)
     {
	int in_comment, in_quote;

	/* skip past leading whitespace */
	while (1)
	  {
	     ch = header[head_start];
	     if (ch == 0)
	       return 0;

	     if ((ch != ' ') && (ch != '\t'))
	       break;

	     head_start++;
	  }

	/* If multiple addresses are given, split at ',' */
	head_end=head_start;
	in_quote=0;
	in_comment = 0;
	type=TYPE_ADD_ONLY;

	/* Loop until end of string is reached, or a ',' found */
	while (1)
	  {
	     ch = header[head_end];
	     if (ch == 0)
	       break;

	     head_end++;

	     if (in_quote)
	       {
		  if (ch == '"')
		    {
		       in_quote = !in_quote;
		       continue;
		    }

		  if (ch == '\\')
		    {
		       ch = header[head_end];
		       if ((ch == 0) || (ch == '\r'))
			 {
			    *errmsg = _("Illegal quoted character in address header.");
			    return -1;
			 }
		       head_end++;
		       continue;
		    }
		  continue;
	       }

	     if (in_comment)
	       {
		  if (ch == '(')
		    {
		       in_comment++;
		       continue;
		    }
		  if (ch == ')')
		    {
		       in_comment--;
		       continue;
		    }
		  if (ch == '\\')
		    {
		       ch = header[head_end];
		       if ((ch == 0) || (ch == '\r'))
			 {
			    *errmsg = _("Illegal quoted character in address header.");
			    return -1;
			 }
		       head_end++;
		       continue;
		    }
		  continue;
	       }

	     if (ch == '"')
	       {
		  in_quote = 1;
		  continue;
	       }

	     if (ch == '(')
	       {
		  in_comment++;
		  continue;
	       }

	     if (ch == '<')
	       {
		  type = TYPE_RFC_2822;
		  continue;
	       }

	     if (ch == ',')
	       {
		  head_end--;
		  break;
	       }
	  }

	if (in_quote)
	  {
	     *errmsg = _("Quote opened but never closed in address header.");
	     return -1;
	  }

	if (in_comment)
	  {
	     *errmsg = _("Comment opened but never closed in address header.");
	     return -1;
	  }

	pos=head_start;
	if (type == TYPE_RFC_2822)     /* foo <bar> */
	  {
	     if (header[pos] != '<')
	       {
		  /* phrase <bar> */
		  if (-1 == parse_rfc2822_phrase (header, parsemap, &pos, head_end, errmsg))
		    return -1;
	       }
	     /* at this point, pos should be at '<' */
	     if (header[pos] != '<')
	       {
		  *errmsg = _("Address appears to have a misplaced '<'.");
		  return -1;
	       }
	     pos++;
	  }
	if (-1 == parse_rfc2822_localpart (header, parsemap, &pos, head_end, errmsg))
	  return -1;

	if (header[pos] == '@')
	  {
	     pos++;
	     if (header[pos] == '[')
	       {
		  pos++;
		  if (-1 == parse_rfc2822_domainlit (header, parsemap, &pos, head_end, errmsg))
		    return -1;
		  pos++;	       /* skip ']' */
	       }
	     else
	       {
		  if (-1 == parse_rfc2822_domain (header, parsemap, &pos, head_end, errmsg))
		    return -1;
	       }
	  }

	if (type == TYPE_RFC_2822)
	  {
	     if (header[pos] != '>')
	       {
		  *errmsg = _("Expected closing '>' character in the address");
		  return -1;
	       }
	     pos++;
	  }

	/* after domainpart only (folding) Whitespace and comments are allowed*/
	if (-1 == parse_rfc2822_cfws (header, parsemap, &pos, head_end, errmsg))
	  return -1;

	if (pos != head_end)
	  {
	     *errmsg = _("Junk found at the end of email-address");
	     /* fprintf (stderr, "BAD: %s\n", header); fprintf (stderr, "END: %s\n", header+pos); */
	     return -1;
	  }
	if (header[head_end] == 0)
	  return 0;

	/* head_end should be at ',', so skip over it. */
	head_start=head_end+1;
     }
}
/*}}}*/

/*}}}*/

/* This function takes a header of the form "KEY: VALUE" and parses it
 * according to rfc2822.   It returns a string that contains information where
 * the header may be encoded.  For example, if the header is:
 *
 *    "From: Thomas Paine <thomas@unknown.isp>"
 *
 * Then the following string will be returned:
 *
 *    "      CCCCCCCCCCCCC                    "
 *
 * The Cs indicate that the corresponding areas of the header may be encoded.
 * If an error occurs, NULL will be returned and *errmsg will be set to a string
 * describing the error.
 */
char *slrn_parse_rfc2822_addr (char *header, char **errmsg)
{
   char *encodemap;
   unsigned int len;

   *errmsg = NULL;

   len = strlen (header);
   if (NULL == (encodemap = slrn_malloc (len+1, 0, 1)))
     {
	*errmsg = _("Out of memory");
	return NULL;
     }

   memset (encodemap, ' ', len);
   encodemap[len] = 0;

   if (-1 == rfc2822_parse (header, encodemap, 0, errmsg))
     {
	if (*errmsg == NULL)
	  *errmsg = _("Error encountered while parsing an RFC2822 header");
	slrn_free (encodemap);
	return NULL;
     }

   return encodemap;
}