]>
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
)
39 for (; *a
; a
++, b
++) {
42 c1
= islower ((unsigned char) *a
) ?
43 toupper ((unsigned char) *a
) : *a
;
44 c2
= islower ((unsigned char) *b
) ?
45 toupper ((unsigned char) *b
) : *b
;
57 return (strncmp (p
, " AT ", 4)
58 && strncmp (p
, " At ", 4)
59 && strncmp (p
, " aT ", 4)
60 && strncmp (p
, " at ", 4) ? FALSE
: TRUE
);
66 * getadrx() implements a partial 822-style address parser. The parser
67 * is neither complete nor correct. It does however recognize nearly all
68 * of the 822 address syntax. In addition it handles the majority of the
69 * 733 syntax as well. Most problems arise from trying to accommodate both.
71 * In terms of 822, the route-specification in
73 * "<" [route] local-part "@" domain ">"
75 * is parsed and returned unchanged. Multiple at-signs are compressed
76 * via source-routing. Recursive groups are not allowed as per the
79 * In terms of 733, " at " is recognized as equivalent to "@".
81 * In terms of both the parser will not complain about missing hosts.
85 * We should not allow addresses like
87 * Marshall T. Rose <MRose@UCI>
89 * but should insist on
91 * "Marshall T. Rose" <MRose@UCI>
93 * Unfortunately, a lot of mailers stupidly let people get away with this.
97 * We should not allow addresses like
101 * but should insist on
105 * Unfortunately, a lot of mailers stupidly let people's UAs get away with
110 * We should not allow addresses like
112 * @UCI:MRose@UCI-750a
114 * but should insist on
116 * Marshall Rose <@UCI:MRose@UCI-750a>
118 * Unfortunately, a lot of mailers stupidly do this.
142 static struct specials special
[] = {
159 static int glevel
= 0;
160 static int ingrp
= 0;
161 static int last_lex
= LX_END
;
163 static char *dp
= NULL
;
164 static char *cp
= NULL
;
165 static char *ap
= NULL
;
166 static char *pers
= NULL
;
167 static char *mbox
= NULL
;
168 static char *host
= NULL
;
169 static char *path
= NULL
;
170 static char *grp
= NULL
;
171 static char *note
= NULL
;
172 static char err
[BUFSIZ
];
173 static char adr
[BUFSIZ
];
175 static struct adrx adrxs2
;
178 /* eai = Email Address Internationalization */
180 getadrx (const char *addrs
, int eai
)
183 struct adrx
*adrxp
= &adrxs2
;
197 pers
= mbox
= host
= path
= grp
= note
= NULL
;
201 dp
= cp
= strdup (addrs
? addrs
: "");
211 switch (parse_address ()) {
223 default: /* catch trailing comments */
237 * Reject the address if key fields contain 8bit characters
240 if (contains8bit(mbox
, NULL
) || contains8bit(host
, NULL
) ||
241 contains8bit(path
, NULL
) || contains8bit(grp
, NULL
)) {
242 strcpy(err
, "Address contains 8-bit characters");
259 while (isspace ((unsigned char) *ap
))
262 snprintf(adr
, sizeof adr
, "%.*s", (int)(cp
- ap
), ap
);
265 bp
= adr
+ strlen (adr
) - 1;
266 if (*bp
== ',' || *bp
== ';' || *bp
== '\n')
275 adrxp
->ingrp
= ingrp
;
277 adrxp
->err
= err
[0] ? err
: NULL
;
290 switch (my_lex (buffer
)) {
293 pers
= strdup (buffer
);
298 strcpy (err
, "extraneous semi-colon");
311 case LX_LBRK
: /* sigh (2) */
314 case LX_AT
: /* sigh (3) */
316 if (route_addr (buffer
) == NOTOK
)
318 return OK
; /* why be choosy? */
321 snprintf(err
, sizeof err
, "illegal address construct (%s)", buffer
);
325 switch (my_lex (buffer
)) {
328 pers
= add (buffer
, add (" ", pers
));
329 more_phrase
: ; /* sigh (1) */
330 if (phrase (buffer
) == NOTOK
)
336 if (route_addr (buffer
) == NOTOK
)
338 if (last_lex
== LX_RBRK
)
340 snprintf(err
, sizeof err
, "missing right-bracket (%s)", buffer
);
346 snprintf(err
, sizeof err
, "nested groups not allowed (%s)", pers
);
349 grp
= add (": ", pers
);
355 switch (my_lex (buffer
)) {
357 case LX_END
: /* tsk, tsk */
366 return parse_address ();
370 case LX_DOT
: /* sigh (1) */
371 pers
= add (".", pers
);
375 snprintf(err
, sizeof err
, "no mailbox in address, only a phrase (%s%s)",
387 mbox
= add (buffer
, pers
);
389 if (route_addr (buffer
) == NOTOK
)
397 if (domain (buffer
) == NOTOK
)
403 strcpy (err
, "extraneous semi-colon");
411 snprintf(err
, sizeof err
, "junk after local@domain (%s)", buffer
);
415 case LX_SEMI
: /* no host */
419 if (last_lex
== LX_SEMI
&& glevel
-- <= 0) {
420 strcpy (err
, "extraneous semi-colon");
428 snprintf(err
, sizeof err
, "missing mailbox (%s)", buffer
);
435 phrase (char *buffer
)
438 switch (my_lex (buffer
)) {
441 pers
= add (buffer
, add (" ", pers
));
451 route_addr (char *buffer
)
455 if (my_lex (buffer
) == LX_AT
) {
456 if (route (buffer
) == NOTOK
)
462 if (local_part (buffer
) == NOTOK
)
467 return domain (buffer
);
469 case LX_SEMI
: /* if in group */
470 case LX_RBRK
: /* no host */
476 snprintf(err
, sizeof err
, "no at-sign after local-part (%s)", buffer
);
483 local_part (char *buffer
)
488 switch (my_lex (buffer
)) {
491 mbox
= add (buffer
, mbox
);
495 snprintf(err
, sizeof err
, "no mailbox in local-part (%s)", buffer
);
499 switch (my_lex (buffer
)) {
501 mbox
= add (buffer
, mbox
);
512 domain (char *buffer
)
515 switch (my_lex (buffer
)) {
518 host
= add (buffer
, host
);
522 snprintf(err
, sizeof err
, "no sub-domain in domain-part of address (%s)", buffer
);
526 switch (my_lex (buffer
)) {
528 host
= add (buffer
, host
);
531 case LX_AT
: /* sigh (0) */
532 mbox
= add (host
, add ("%", mbox
));
550 switch (my_lex (buffer
)) {
553 path
= add (buffer
, path
);
557 snprintf(err
, sizeof err
, "no sub-domain in domain-part of address (%s)", buffer
);
560 switch (my_lex (buffer
)) {
562 path
= add (buffer
, path
);
564 switch (my_lex (buffer
)) {
569 path
= add (buffer
, path
);
573 snprintf(err
, sizeof err
, "no at-sign found for next domain in route (%s)",
580 case LX_AT
: /* XXX */
582 path
= add (buffer
, path
);
586 path
= add (buffer
, path
);
590 snprintf(err
, sizeof err
, "no colon found to terminate route (%s)", buffer
);
598 my_lex (char *buffer
)
600 /* buffer should be at least BUFSIZ bytes long */
604 /* Add C to the buffer bp. After use of this macro *bp is guaranteed to be within the buffer. */
605 #define ADDCHR(C) do { *bp++ = (C); if ((bp - buffer) == (BUFSIZ-1)) goto my_lex_buffull; } while (0)
610 return (last_lex
= LX_END
);
614 while (isspace ((unsigned char) c
))
618 return (last_lex
= LX_END
);
627 return (last_lex
= LX_ERR
);
630 if ((c
= *cp
++) == 0) {
632 return (last_lex
= LX_ERR
);
645 note
= note
? add (buffer
, add (" ", note
))
647 return my_lex (buffer
);
658 return (last_lex
= LX_ERR
);
661 if ((c
= *cp
++) == 0) {
663 return (last_lex
= LX_ERR
);
671 return (last_lex
= LX_QSTR
);
681 return (last_lex
= LX_ERR
);
684 if ((c
= *cp
++) == 0) {
686 return (last_lex
= LX_ERR
);
694 return (last_lex
= LX_DLIT
);
700 for (i
= 0; special
[i
].lx_chr
!= 0; i
++)
701 if (c
== special
[i
].lx_chr
)
702 return (last_lex
= special
[i
].lx_val
);
704 if (iscntrl ((unsigned char) c
))
705 return (last_lex
= LX_ERR
);
708 if ((c
= *cp
++) == 0)
710 for (i
= 0; special
[i
].lx_chr
!= 0; i
++)
711 if (c
== special
[i
].lx_chr
)
713 if (iscntrl ((unsigned char) c
) || isspace ((unsigned char) c
))
723 last_lex
= !gotat
|| cp
== NULL
|| strchr(cp
, '<') != NULL
728 /* Out of buffer space. *bp is the last byte in the buffer */
730 return (last_lex
= LX_ERR
);
735 legal_person (const char *p
)
739 static char buffer
[BUFSIZ
];
743 for (cp
= p
; *cp
; cp
++)
744 for (i
= 0; special
[i
].lx_chr
; i
++)
745 if (*cp
== special
[i
].lx_chr
) {
746 snprintf(buffer
, sizeof buffer
, "\"%s\"", p
);
755 mfgets (FILE *in
, char **bp
)
760 static char *pp
= NULL
;
763 pp
= mh_xmalloc ((size_t) (len
= BUFSIZ
));
765 for (ep
= (cp
= pp
) + len
- 2;;) {
766 switch (i
= getc (in
)) {
784 if (cp
== pp
) /* end of headers, gobble it */
786 switch (i
= getc (in
)) {
787 default: /* end of line */
788 case '\n': /* end of headers, save for next call */
792 case ' ': /* continue headers */
796 } /* fall into default case */
803 dp
= mh_xrealloc (pp
, (size_t) (len
+= BUFSIZ
));
804 cp
+= dp
- pp
, ep
= (pp
= cp
) + len
- 2;