]>
diplodocus.org Git - nmh/blob - sbr/mf.c
3 * mf.c -- mail filter subroutines
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
16 static int isat (const char *);
17 static int parse_address (void);
18 static int phrase (char *);
19 static int route_addr (char *);
20 static int local_part (char *);
21 static int domain (char *);
22 static int route (char *);
23 static int my_lex (char *);
27 isfrom(const char *string
)
29 return (strncmp (string
, "From ", 5) == 0
30 || strncmp (string
, ">From ", 6) == 0);
35 lequal (const char *a
, const char *b
)
41 char c1
= islower ((unsigned char) *a
) ?
42 toupper ((unsigned char) *a
) : *a
;
43 char c2
= islower ((unsigned char) *b
) ?
44 toupper ((unsigned char) *b
) : *b
;
56 return (strncmp (p
, " AT ", 4)
57 && strncmp (p
, " At ", 4)
58 && strncmp (p
, " aT ", 4)
59 && strncmp (p
, " at ", 4) ? FALSE
: TRUE
);
65 * getadrx() implements a partial 822-style address parser. The parser
66 * is neither complete nor correct. It does however recognize nearly all
67 * of the 822 address syntax. In addition it handles the majority of the
68 * 733 syntax as well. Most problems arise from trying to accommodate both.
70 * In terms of 822, the route-specification in
72 * "<" [route] local-part "@" domain ">"
74 * is parsed and returned unchanged. Multiple at-signs are compressed
75 * via source-routing. Recursive groups are not allowed as per the
78 * In terms of 733, " at " is recognized as equivalent to "@".
80 * In terms of both the parser will not complain about missing hosts.
84 * We should not allow addresses like
86 * Marshall T. Rose <MRose@UCI>
88 * but should insist on
90 * "Marshall T. Rose" <MRose@UCI>
92 * Unfortunately, a lot of mailers stupidly let people get away with this.
96 * We should not allow addresses like
100 * but should insist on
104 * Unfortunately, a lot of mailers stupidly let people's UAs get away with
109 * We should not allow addresses like
111 * @UCI:MRose@UCI-750a
113 * but should insist on
115 * Marshall Rose <@UCI:MRose@UCI-750a>
117 * Unfortunately, a lot of mailers stupidly do this.
141 static struct specials special
[] = {
158 static int glevel
= 0;
159 static int ingrp
= 0;
160 static int last_lex
= LX_END
;
162 static char *dp
= NULL
;
163 static char *cp
= NULL
;
164 static char *ap
= NULL
;
165 static char *pers
= NULL
;
166 static char *mbox
= NULL
;
167 static char *host
= NULL
;
168 static char *path
= NULL
;
169 static char *grp
= NULL
;
170 static char *note
= NULL
;
171 static char err
[BUFSIZ
];
172 static char adr
[BUFSIZ
];
174 static struct adrx adrxs2
;
177 /* eai = Email Address Internationalization */
179 getadrx (const char *addrs
, int eai
)
182 struct adrx
*adrxp
= &adrxs2
;
196 pers
= mbox
= host
= path
= grp
= note
= NULL
;
200 dp
= cp
= strdup (addrs
? addrs
: "");
210 switch (parse_address ()) {
222 default: /* catch trailing comments */
236 * Reject the address if key fields contain 8bit characters
239 if (contains8bit(mbox
, NULL
) || contains8bit(host
, NULL
) ||
240 contains8bit(path
, NULL
) || contains8bit(grp
, NULL
)) {
241 strcpy(err
, "Address contains 8-bit characters");
258 while (isspace ((unsigned char) *ap
))
261 snprintf(adr
, sizeof adr
, "%.*s", (int)(cp
- ap
), ap
);
264 bp
= adr
+ strlen (adr
) - 1;
265 if (*bp
== ',' || *bp
== ';' || *bp
== '\n')
274 adrxp
->ingrp
= ingrp
;
276 adrxp
->err
= err
[0] ? err
: NULL
;
289 switch (my_lex (buffer
)) {
292 pers
= strdup (buffer
);
297 strcpy (err
, "extraneous semi-colon");
310 case LX_LBRK
: /* sigh (2) */
313 case LX_AT
: /* sigh (3) */
315 if (route_addr (buffer
) == NOTOK
)
317 return OK
; /* why be choosy? */
320 snprintf(err
, sizeof err
, "illegal address construct (%s)", buffer
);
324 switch (my_lex (buffer
)) {
327 pers
= add (buffer
, add (" ", pers
));
328 more_phrase
: ; /* sigh (1) */
329 if (phrase (buffer
) == NOTOK
)
335 if (route_addr (buffer
) == NOTOK
)
337 if (last_lex
== LX_RBRK
)
339 snprintf(err
, sizeof err
, "missing right-bracket (%s)", buffer
);
345 snprintf(err
, sizeof err
, "nested groups not allowed (%s)", pers
);
348 grp
= add (": ", pers
);
354 switch (my_lex (buffer
)) {
356 case LX_END
: /* tsk, tsk */
365 return parse_address ();
369 case LX_DOT
: /* sigh (1) */
370 pers
= add (".", pers
);
374 snprintf(err
, sizeof err
, "no mailbox in address, only a phrase (%s%s)",
386 mbox
= add (buffer
, pers
);
388 if (route_addr (buffer
) == NOTOK
)
396 if (domain (buffer
) == NOTOK
)
402 strcpy (err
, "extraneous semi-colon");
410 snprintf(err
, sizeof err
, "junk after local@domain (%s)", buffer
);
414 case LX_SEMI
: /* no host */
418 if (last_lex
== LX_SEMI
&& glevel
-- <= 0) {
419 strcpy (err
, "extraneous semi-colon");
427 snprintf(err
, sizeof err
, "missing mailbox (%s)", buffer
);
434 phrase (char *buffer
)
437 switch (my_lex (buffer
)) {
440 pers
= add (buffer
, add (" ", pers
));
450 route_addr (char *buffer
)
454 if (my_lex (buffer
) == LX_AT
) {
455 if (route (buffer
) == NOTOK
)
461 if (local_part (buffer
) == NOTOK
)
466 return domain (buffer
);
468 case LX_SEMI
: /* if in group */
469 case LX_RBRK
: /* no host */
475 snprintf(err
, sizeof err
, "no at-sign after local-part (%s)", buffer
);
482 local_part (char *buffer
)
487 switch (my_lex (buffer
)) {
490 mbox
= add (buffer
, mbox
);
494 snprintf(err
, sizeof err
, "no mailbox in local-part (%s)", buffer
);
498 switch (my_lex (buffer
)) {
500 mbox
= add (buffer
, mbox
);
511 domain (char *buffer
)
514 switch (my_lex (buffer
)) {
517 host
= add (buffer
, host
);
521 snprintf(err
, sizeof err
, "no sub-domain in domain-part of address (%s)", buffer
);
525 switch (my_lex (buffer
)) {
527 host
= add (buffer
, host
);
530 case LX_AT
: /* sigh (0) */
531 mbox
= add (host
, add ("%", mbox
));
549 switch (my_lex (buffer
)) {
552 path
= add (buffer
, path
);
556 snprintf(err
, sizeof err
, "no sub-domain in domain-part of address (%s)", buffer
);
559 switch (my_lex (buffer
)) {
561 path
= add (buffer
, path
);
563 switch (my_lex (buffer
)) {
568 path
= add (buffer
, path
);
572 snprintf(err
, sizeof err
, "no at-sign found for next domain in route (%s)",
579 case LX_AT
: /* XXX */
581 path
= add (buffer
, path
);
585 path
= add (buffer
, path
);
589 snprintf(err
, sizeof err
, "no colon found to terminate route (%s)", buffer
);
597 my_lex (char *buffer
)
599 /* buffer should be at least BUFSIZ bytes long */
603 /* Add C to the buffer bp. After use of this macro *bp is guaranteed to be within the buffer. */
604 #define ADDCHR(C) do { *bp++ = (C); if ((bp - buffer) == (BUFSIZ-1)) goto my_lex_buffull; } while (0)
609 return (last_lex
= LX_END
);
613 while (isspace ((unsigned char) c
))
617 return (last_lex
= LX_END
);
626 return (last_lex
= LX_ERR
);
629 if ((c
= *cp
++) == 0) {
631 return (last_lex
= LX_ERR
);
644 note
= note
? add (buffer
, add (" ", note
))
646 return my_lex (buffer
);
657 return (last_lex
= LX_ERR
);
660 if ((c
= *cp
++) == 0) {
662 return (last_lex
= LX_ERR
);
670 return (last_lex
= LX_QSTR
);
680 return (last_lex
= LX_ERR
);
683 if ((c
= *cp
++) == 0) {
685 return (last_lex
= LX_ERR
);
693 return (last_lex
= LX_DLIT
);
699 for (i
= 0; special
[i
].lx_chr
!= 0; i
++)
700 if (c
== special
[i
].lx_chr
)
701 return (last_lex
= special
[i
].lx_val
);
703 if (iscntrl ((unsigned char) c
))
704 return (last_lex
= LX_ERR
);
707 if ((c
= *cp
++) == 0)
709 for (i
= 0; special
[i
].lx_chr
!= 0; i
++)
710 if (c
== special
[i
].lx_chr
)
712 if (iscntrl ((unsigned char) c
) || isspace ((unsigned char) c
))
722 last_lex
= !gotat
|| cp
== NULL
|| strchr(cp
, '<') != NULL
727 /* Out of buffer space. *bp is the last byte in the buffer */
729 return (last_lex
= LX_ERR
);
734 legal_person (const char *p
)
738 static char buffer
[BUFSIZ
];
742 for (cp
= p
; *cp
; cp
++)
743 for (i
= 0; special
[i
].lx_chr
; i
++)
744 if (*cp
== special
[i
].lx_chr
) {
745 snprintf(buffer
, sizeof buffer
, "\"%s\"", p
);
754 mfgets (FILE *in
, char **bp
)
759 static char *pp
= NULL
;
762 pp
= mh_xmalloc ((size_t) (len
= BUFSIZ
));
764 for (ep
= (cp
= pp
) + len
- 2;;) {
765 switch (i
= getc (in
)) {
783 if (cp
== pp
) /* end of headers, gobble it */
785 switch (i
= getc (in
)) {
786 default: /* end of line */
787 case '\n': /* end of headers, save for next call */
791 case ' ': /* continue headers */
795 } /* fall into default case */
802 dp
= mh_xrealloc (pp
, (size_t) (len
+= BUFSIZ
));
803 cp
+= dp
- pp
, ep
= (pp
= cp
) + len
- 2;