]>
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 (has_prefix(string
, "From ")
30 || has_prefix(string
, ">From "));
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 has_prefix(p
, " AT ") || has_prefix(p
, " At ") ||
56 has_prefix(p
, " aT ") || has_prefix(p
, " at ");
62 * getadrx() implements a partial 822-style address parser. The parser
63 * is neither complete nor correct. It does however recognize nearly all
64 * of the 822 address syntax. In addition it handles the majority of the
65 * 733 syntax as well. Most problems arise from trying to accommodate both.
67 * In terms of 822, the route-specification in
69 * "<" [route] local-part "@" domain ">"
71 * is parsed and returned unchanged. Multiple at-signs are compressed
72 * via source-routing. Recursive groups are not allowed as per the
75 * In terms of 733, " at " is recognized as equivalent to "@".
77 * In terms of both the parser will not complain about missing hosts.
81 * We should not allow addresses like
83 * Marshall T. Rose <MRose@UCI>
85 * but should insist on
87 * "Marshall T. Rose" <MRose@UCI>
89 * Unfortunately, a lot of mailers stupidly let people get away with this.
93 * We should not allow addresses like
97 * but should insist on
101 * Unfortunately, a lot of mailers stupidly let people's UAs get away with
106 * We should not allow addresses like
108 * @UCI:MRose@UCI-750a
110 * but should insist on
112 * Marshall Rose <@UCI:MRose@UCI-750a>
114 * Unfortunately, a lot of mailers stupidly do this.
138 static struct specials special
[] = {
155 static int glevel
= 0;
156 static int ingrp
= 0;
157 static int last_lex
= LX_END
;
159 static char *dp
= NULL
;
160 static char *cp
= NULL
;
161 static char *ap
= NULL
;
162 static char *pers
= NULL
;
163 static char *mbox
= NULL
;
164 static char *host
= NULL
;
165 static char *path
= NULL
;
166 static char *grp
= NULL
;
167 static char *note
= NULL
;
168 static char err
[BUFSIZ
];
169 static char adr
[BUFSIZ
];
171 static struct adrx adrxs2
;
174 /* eai = Email Address Internationalization */
176 getadrx (const char *addrs
, int eai
)
179 struct adrx
*adrxp
= &adrxs2
;
187 pers
= mbox
= host
= path
= grp
= note
= NULL
;
191 dp
= cp
= strdup (addrs
? addrs
: "");
201 switch (parse_address ()) {
213 default: /* catch trailing comments */
227 * Reject the address if key fields contain 8bit characters
230 if (contains8bit(mbox
, NULL
) || contains8bit(host
, NULL
) ||
231 contains8bit(path
, NULL
) || contains8bit(grp
, NULL
)) {
232 strcpy(err
, "Address contains 8-bit characters");
249 while (isspace ((unsigned char) *ap
))
252 snprintf(adr
, sizeof adr
, "%.*s", (int)(cp
- ap
), ap
);
255 bp
= adr
+ strlen (adr
) - 1;
256 if (*bp
== ',' || *bp
== ';' || *bp
== '\n')
265 adrxp
->ingrp
= ingrp
;
267 adrxp
->err
= err
[0] ? err
: NULL
;
280 switch (my_lex (buffer
)) {
283 pers
= strdup (buffer
);
288 strcpy (err
, "extraneous semi-colon");
300 case LX_LBRK
: /* sigh (2) */
303 case LX_AT
: /* sigh (3) */
305 if (route_addr (buffer
) == NOTOK
)
307 return OK
; /* why be choosy? */
310 snprintf(err
, sizeof err
, "illegal address construct (%s)", buffer
);
314 switch (my_lex (buffer
)) {
317 pers
= add (buffer
, add (" ", pers
));
318 more_phrase
: ; /* sigh (1) */
319 if (phrase (buffer
) == NOTOK
)
325 if (route_addr (buffer
) == NOTOK
)
327 if (last_lex
== LX_RBRK
)
329 snprintf(err
, sizeof err
, "missing right-bracket (%s)", buffer
);
335 snprintf(err
, sizeof err
, "nested groups not allowed (%s)", pers
);
338 grp
= add (": ", pers
);
344 switch (my_lex (buffer
)) {
346 case LX_END
: /* tsk, tsk */
355 return parse_address ();
359 case LX_DOT
: /* sigh (1) */
360 pers
= add (".", pers
);
364 snprintf(err
, sizeof err
, "no mailbox in address, only a phrase (%s%s)",
376 mbox
= add (buffer
, pers
);
378 if (route_addr (buffer
) == NOTOK
)
386 if (domain (buffer
) == NOTOK
)
392 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
);
636 note
= note
? add (buffer
, add (" ", note
))
638 return my_lex (buffer
);
649 return (last_lex
= LX_ERR
);
652 if ((c
= *cp
++) == 0) {
654 return (last_lex
= LX_ERR
);
663 return (last_lex
= LX_QSTR
);
673 return (last_lex
= LX_ERR
);
676 if ((c
= *cp
++) == 0) {
678 return (last_lex
= LX_ERR
);
687 return (last_lex
= LX_DLIT
);
693 for (i
= 0; special
[i
].lx_chr
!= 0; i
++)
694 if (c
== special
[i
].lx_chr
)
695 return (last_lex
= special
[i
].lx_val
);
697 if (iscntrl ((unsigned char) c
))
698 return (last_lex
= LX_ERR
);
701 if ((c
= *cp
++) == 0)
703 for (i
= 0; special
[i
].lx_chr
!= 0; i
++)
704 if (c
== special
[i
].lx_chr
)
706 if (iscntrl ((unsigned char) c
) || isspace ((unsigned char) c
))
716 last_lex
= !gotat
|| cp
== NULL
|| strchr(cp
, '<') != NULL
721 /* Out of buffer space. *bp is the last byte in the buffer */
723 return (last_lex
= LX_ERR
);
728 legal_person (const char *p
)
732 static char buffer
[BUFSIZ
];
736 for (cp
= p
; *cp
; cp
++)
737 for (i
= 0; special
[i
].lx_chr
; i
++)
738 if (*cp
== special
[i
].lx_chr
) {
739 snprintf(buffer
, sizeof buffer
, "\"%s\"", p
);
748 mfgets (FILE *in
, char **bp
)
753 static char *pp
= NULL
;
756 pp
= mh_xmalloc ((size_t) (len
= BUFSIZ
));
758 for (ep
= (cp
= pp
) + len
- 2;;) {
759 switch (i
= getc (in
)) {
777 if (cp
== pp
) /* end of headers, gobble it */
779 switch (i
= getc (in
)) {
780 default: /* end of line */
781 case '\n': /* end of headers, save for next call */
785 case ' ': /* continue headers */
797 dp
= mh_xrealloc (pp
, (size_t) (len
+= BUFSIZ
));
798 cp
+= dp
- pp
, ep
= (pp
= cp
) + len
- 2;