]>
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
= toupper((unsigned char)*a
);
43 c2
= toupper((unsigned char)*b
);
55 return (strncmp (p
, " AT ", 4)
56 && strncmp (p
, " At ", 4)
57 && strncmp (p
, " aT ", 4)
58 && strncmp (p
, " at ", 4) ? FALSE
: TRUE
);
64 * getadrx() implements a partial 822-style address parser. The parser
65 * is neither complete nor correct. It does however recognize nearly all
66 * of the 822 address syntax. In addition it handles the majority of the
67 * 733 syntax as well. Most problems arise from trying to accommodate both.
69 * In terms of 822, the route-specification in
71 * "<" [route] local-part "@" domain ">"
73 * is parsed and returned unchanged. Multiple at-signs are compressed
74 * via source-routing. Recursive groups are not allowed as per the
77 * In terms of 733, " at " is recognized as equivalent to "@".
79 * In terms of both the parser will not complain about missing hosts.
83 * We should not allow addresses like
85 * Marshall T. Rose <MRose@UCI>
87 * but should insist on
89 * "Marshall T. Rose" <MRose@UCI>
91 * Unfortunately, a lot of mailers stupidly let people get away with this.
95 * We should not allow addresses like
99 * but should insist on
103 * Unfortunately, a lot of mailers stupidly let people's UAs get away with
108 * We should not allow addresses like
110 * @UCI:MRose@UCI-750a
112 * but should insist on
114 * Marshall Rose <@UCI:MRose@UCI-750a>
116 * Unfortunately, a lot of mailers stupidly do this.
140 static struct specials special
[] = {
157 static int glevel
= 0;
158 static int ingrp
= 0;
159 static int last_lex
= LX_END
;
161 static char *dp
= NULL
;
162 static char *cp
= NULL
;
163 static char *ap
= NULL
;
164 static char *pers
= NULL
;
165 static char *mbox
= NULL
;
166 static char *host
= NULL
;
167 static char *path
= NULL
;
168 static char *grp
= NULL
;
169 static char *note
= NULL
;
170 static char err
[BUFSIZ
];
171 static char adr
[BUFSIZ
];
173 static struct adrx adrxs2
;
176 /* eai = Email Address Internationalization */
178 getadrx (const char *addrs
, int eai
)
181 struct adrx
*adrxp
= &adrxs2
;
189 pers
= mbox
= host
= path
= grp
= note
= NULL
;
193 dp
= cp
= strdup (addrs
? addrs
: "");
203 switch (parse_address ()) {
215 default: /* catch trailing comments */
229 * Reject the address if key fields contain 8bit characters
232 if (contains8bit(mbox
, NULL
) || contains8bit(host
, NULL
) ||
233 contains8bit(path
, NULL
) || contains8bit(grp
, NULL
)) {
234 strcpy(err
, "Address contains 8-bit characters");
251 while (isspace ((unsigned char) *ap
))
254 snprintf(adr
, sizeof adr
, "%.*s", (int)(cp
- ap
), ap
);
257 bp
= adr
+ strlen (adr
) - 1;
258 if (*bp
== ',' || *bp
== ';' || *bp
== '\n')
267 adrxp
->ingrp
= ingrp
;
269 adrxp
->err
= err
[0] ? err
: NULL
;
282 switch (my_lex (buffer
)) {
285 pers
= strdup (buffer
);
290 strcpy (err
, "extraneous semi-colon");
301 case LX_LBRK
: /* sigh (2) */
304 case LX_AT
: /* sigh (3) */
306 if (route_addr (buffer
) == NOTOK
)
308 return OK
; /* why be choosy? */
311 snprintf(err
, sizeof err
, "illegal address construct (%s)", buffer
);
315 switch (my_lex (buffer
)) {
318 pers
= add (buffer
, add (" ", pers
));
319 more_phrase
: ; /* sigh (1) */
320 if (phrase (buffer
) == NOTOK
)
326 if (route_addr (buffer
) == NOTOK
)
328 if (last_lex
== LX_RBRK
)
330 snprintf(err
, sizeof err
, "missing right-bracket (%s)", buffer
);
336 snprintf(err
, sizeof err
, "nested groups not allowed (%s)", pers
);
339 grp
= add (": ", pers
);
345 switch (my_lex (buffer
)) {
347 case LX_END
: /* tsk, tsk */
356 return parse_address ();
360 case LX_DOT
: /* sigh (1) */
361 pers
= add (".", pers
);
365 snprintf(err
, sizeof err
, "no mailbox in address, only a phrase (%s%s)",
377 mbox
= add (buffer
, pers
);
379 if (route_addr (buffer
) == NOTOK
)
387 if (domain (buffer
) == NOTOK
)
393 strcpy (err
, "extraneous semi-colon");
401 snprintf(err
, sizeof err
, "junk after local@domain (%s)", buffer
);
405 case LX_SEMI
: /* no host */
409 if (last_lex
== LX_SEMI
&& glevel
-- <= 0) {
410 strcpy (err
, "extraneous semi-colon");
418 snprintf(err
, sizeof err
, "missing mailbox (%s)", buffer
);
425 phrase (char *buffer
)
428 switch (my_lex (buffer
)) {
431 pers
= add (buffer
, add (" ", pers
));
441 route_addr (char *buffer
)
445 if (my_lex (buffer
) == LX_AT
) {
446 if (route (buffer
) == NOTOK
)
452 if (local_part (buffer
) == NOTOK
)
457 return domain (buffer
);
459 case LX_SEMI
: /* if in group */
460 case LX_RBRK
: /* no host */
466 snprintf(err
, sizeof err
, "no at-sign after local-part (%s)", buffer
);
473 local_part (char *buffer
)
478 switch (my_lex (buffer
)) {
481 mbox
= add (buffer
, mbox
);
485 snprintf(err
, sizeof err
, "no mailbox in local-part (%s)", buffer
);
489 switch (my_lex (buffer
)) {
491 mbox
= add (buffer
, mbox
);
502 domain (char *buffer
)
505 switch (my_lex (buffer
)) {
508 host
= add (buffer
, host
);
512 snprintf(err
, sizeof err
, "no sub-domain in domain-part of address (%s)", buffer
);
516 switch (my_lex (buffer
)) {
518 host
= add (buffer
, host
);
521 case LX_AT
: /* sigh (0) */
522 mbox
= add (host
, add ("%", mbox
));
540 switch (my_lex (buffer
)) {
543 path
= add (buffer
, path
);
547 snprintf(err
, sizeof err
, "no sub-domain in domain-part of address (%s)", buffer
);
550 switch (my_lex (buffer
)) {
552 path
= add (buffer
, path
);
554 switch (my_lex (buffer
)) {
559 path
= add (buffer
, path
);
563 snprintf(err
, sizeof err
, "no at-sign found for next domain in route (%s)",
570 case LX_AT
: /* XXX */
572 path
= add (buffer
, path
);
576 path
= add (buffer
, path
);
580 snprintf(err
, sizeof err
, "no colon found to terminate route (%s)", buffer
);
588 my_lex (char *buffer
)
590 /* buffer should be at least BUFSIZ bytes long */
594 /* Add C to the buffer bp. After use of this macro *bp is guaranteed to be within the buffer. */
595 #define ADDCHR(C) do { *bp++ = (C); if ((bp - buffer) == (BUFSIZ-1)) goto my_lex_buffull; } while (0)
600 return (last_lex
= LX_END
);
604 while (isspace ((unsigned char) c
))
608 return (last_lex
= LX_END
);
617 return (last_lex
= LX_ERR
);
620 if ((c
= *cp
++) == 0) {
622 return (last_lex
= LX_ERR
);
635 note
= note
? add (buffer
, add (" ", note
))
637 return my_lex (buffer
);
648 return (last_lex
= LX_ERR
);
651 if ((c
= *cp
++) == 0) {
653 return (last_lex
= LX_ERR
);
661 return (last_lex
= LX_QSTR
);
671 return (last_lex
= LX_ERR
);
674 if ((c
= *cp
++) == 0) {
676 return (last_lex
= LX_ERR
);
684 return (last_lex
= LX_DLIT
);
690 for (i
= 0; special
[i
].lx_chr
!= 0; i
++)
691 if (c
== special
[i
].lx_chr
)
692 return (last_lex
= special
[i
].lx_val
);
694 if (iscntrl ((unsigned char) c
))
695 return (last_lex
= LX_ERR
);
698 if ((c
= *cp
++) == 0)
700 for (i
= 0; special
[i
].lx_chr
!= 0; i
++)
701 if (c
== special
[i
].lx_chr
)
703 if (iscntrl ((unsigned char) c
) || isspace ((unsigned char) c
))
713 last_lex
= !gotat
|| cp
== NULL
|| strchr(cp
, '<') != NULL
718 /* Out of buffer space. *bp is the last byte in the buffer */
720 return (last_lex
= LX_ERR
);
725 legal_person (const char *p
)
729 static char buffer
[BUFSIZ
];
733 for (cp
= p
; *cp
; cp
++)
734 for (i
= 0; special
[i
].lx_chr
; i
++)
735 if (*cp
== special
[i
].lx_chr
) {
736 snprintf(buffer
, sizeof buffer
, "\"%s\"", p
);
745 mfgets (FILE *in
, char **bp
)
750 static char *pp
= NULL
;
753 pp
= mh_xmalloc ((size_t) (len
= BUFSIZ
));
755 for (ep
= (cp
= pp
) + len
- 2;;) {
756 switch (i
= getc (in
)) {
774 if (cp
== pp
) /* end of headers, gobble it */
776 switch (i
= getc (in
)) {
777 default: /* end of line */
778 case '\n': /* end of headers, save for next call */
782 case ' ': /* continue headers */
786 } /* fall into default case */
793 dp
= mh_xrealloc (pp
, (size_t) (len
+= BUFSIZ
));
794 cp
+= dp
- pp
, ep
= (pp
= cp
) + len
- 2;