]>
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
;
195 pers
= mbox
= host
= path
= grp
= note
= NULL
;
199 dp
= cp
= strdup (addrs
? addrs
: "");
209 switch (parse_address ()) {
221 default: /* catch trailing comments */
235 * Reject the address if key fields contain 8bit characters
238 if (contains8bit(mbox
, NULL
) || contains8bit(host
, NULL
) ||
239 contains8bit(path
, NULL
) || contains8bit(grp
, NULL
)) {
240 strcpy(err
, "Address contains 8-bit characters");
257 while (isspace ((unsigned char) *ap
))
260 snprintf(adr
, sizeof adr
, "%.*s", (int)(cp
- ap
), ap
);
263 bp
= adr
+ strlen (adr
) - 1;
264 if (*bp
== ',' || *bp
== ';' || *bp
== '\n')
273 adrxp
->ingrp
= ingrp
;
275 adrxp
->err
= err
[0] ? err
: NULL
;
288 switch (my_lex (buffer
)) {
291 pers
= strdup (buffer
);
296 strcpy (err
, "extraneous semi-colon");
309 case LX_LBRK
: /* sigh (2) */
312 case LX_AT
: /* sigh (3) */
314 if (route_addr (buffer
) == NOTOK
)
316 return OK
; /* why be choosy? */
319 snprintf(err
, sizeof err
, "illegal address construct (%s)", buffer
);
323 switch (my_lex (buffer
)) {
326 pers
= add (buffer
, add (" ", pers
));
327 more_phrase
: ; /* sigh (1) */
328 if (phrase (buffer
) == NOTOK
)
334 if (route_addr (buffer
) == NOTOK
)
336 if (last_lex
== LX_RBRK
)
338 snprintf(err
, sizeof err
, "missing right-bracket (%s)", buffer
);
344 snprintf(err
, sizeof err
, "nested groups not allowed (%s)", pers
);
347 grp
= add (": ", pers
);
353 switch (my_lex (buffer
)) {
355 case LX_END
: /* tsk, tsk */
364 return parse_address ();
368 case LX_DOT
: /* sigh (1) */
369 pers
= add (".", pers
);
373 snprintf(err
, sizeof err
, "no mailbox in address, only a phrase (%s%s)",
385 mbox
= add (buffer
, pers
);
387 if (route_addr (buffer
) == NOTOK
)
395 if (domain (buffer
) == NOTOK
)
401 strcpy (err
, "extraneous semi-colon");
409 snprintf(err
, sizeof err
, "junk after local@domain (%s)", buffer
);
413 case LX_SEMI
: /* no host */
417 if (last_lex
== LX_SEMI
&& glevel
-- <= 0) {
418 strcpy (err
, "extraneous semi-colon");
426 snprintf(err
, sizeof err
, "missing mailbox (%s)", buffer
);
433 phrase (char *buffer
)
436 switch (my_lex (buffer
)) {
439 pers
= add (buffer
, add (" ", pers
));
449 route_addr (char *buffer
)
453 if (my_lex (buffer
) == LX_AT
) {
454 if (route (buffer
) == NOTOK
)
460 if (local_part (buffer
) == NOTOK
)
465 return domain (buffer
);
467 case LX_SEMI
: /* if in group */
468 case LX_RBRK
: /* no host */
474 snprintf(err
, sizeof err
, "no at-sign after local-part (%s)", buffer
);
481 local_part (char *buffer
)
486 switch (my_lex (buffer
)) {
489 mbox
= add (buffer
, mbox
);
493 snprintf(err
, sizeof err
, "no mailbox in local-part (%s)", buffer
);
497 switch (my_lex (buffer
)) {
499 mbox
= add (buffer
, mbox
);
510 domain (char *buffer
)
513 switch (my_lex (buffer
)) {
516 host
= add (buffer
, host
);
520 snprintf(err
, sizeof err
, "no sub-domain in domain-part of address (%s)", buffer
);
524 switch (my_lex (buffer
)) {
526 host
= add (buffer
, host
);
529 case LX_AT
: /* sigh (0) */
530 mbox
= add (host
, add ("%", mbox
));
548 switch (my_lex (buffer
)) {
551 path
= add (buffer
, path
);
555 snprintf(err
, sizeof err
, "no sub-domain in domain-part of address (%s)", buffer
);
558 switch (my_lex (buffer
)) {
560 path
= add (buffer
, path
);
562 switch (my_lex (buffer
)) {
567 path
= add (buffer
, path
);
571 snprintf(err
, sizeof err
, "no at-sign found for next domain in route (%s)",
578 case LX_AT
: /* XXX */
580 path
= add (buffer
, path
);
584 path
= add (buffer
, path
);
588 snprintf(err
, sizeof err
, "no colon found to terminate route (%s)", buffer
);
596 my_lex (char *buffer
)
598 /* buffer should be at least BUFSIZ bytes long */
602 /* Add C to the buffer bp. After use of this macro *bp is guaranteed to be within the buffer. */
603 #define ADDCHR(C) do { *bp++ = (C); if ((bp - buffer) == (BUFSIZ-1)) goto my_lex_buffull; } while (0)
608 return (last_lex
= LX_END
);
612 while (isspace ((unsigned char) c
))
616 return (last_lex
= LX_END
);
625 return (last_lex
= LX_ERR
);
628 if ((c
= *cp
++) == 0) {
630 return (last_lex
= LX_ERR
);
643 note
= note
? add (buffer
, add (" ", note
))
645 return my_lex (buffer
);
656 return (last_lex
= LX_ERR
);
659 if ((c
= *cp
++) == 0) {
661 return (last_lex
= LX_ERR
);
669 return (last_lex
= LX_QSTR
);
679 return (last_lex
= LX_ERR
);
682 if ((c
= *cp
++) == 0) {
684 return (last_lex
= LX_ERR
);
692 return (last_lex
= LX_DLIT
);
698 for (i
= 0; special
[i
].lx_chr
!= 0; i
++)
699 if (c
== special
[i
].lx_chr
)
700 return (last_lex
= special
[i
].lx_val
);
702 if (iscntrl ((unsigned char) c
))
703 return (last_lex
= LX_ERR
);
706 if ((c
= *cp
++) == 0)
708 for (i
= 0; special
[i
].lx_chr
!= 0; i
++)
709 if (c
== special
[i
].lx_chr
)
711 if (iscntrl ((unsigned char) c
) || isspace ((unsigned char) c
))
721 last_lex
= !gotat
|| cp
== NULL
|| strchr(cp
, '<') != NULL
726 /* Out of buffer space. *bp is the last byte in the buffer */
728 return (last_lex
= LX_ERR
);
733 legal_person (const char *p
)
737 static char buffer
[BUFSIZ
];
741 for (cp
= p
; *cp
; cp
++)
742 for (i
= 0; special
[i
].lx_chr
; i
++)
743 if (*cp
== special
[i
].lx_chr
) {
744 snprintf(buffer
, sizeof buffer
, "\"%s\"", p
);
753 mfgets (FILE *in
, char **bp
)
758 static char *pp
= NULL
;
761 pp
= mh_xmalloc ((size_t) (len
= BUFSIZ
));
763 for (ep
= (cp
= pp
) + len
- 2;;) {
764 switch (i
= getc (in
)) {
782 if (cp
== pp
) /* end of headers, gobble it */
784 switch (i
= getc (in
)) {
785 default: /* end of line */
786 case '\n': /* end of headers, save for next call */
790 case ' ': /* continue headers */
794 } /* fall into default case */
801 dp
= mh_xrealloc (pp
, (size_t) (len
+= BUFSIZ
));
802 cp
+= dp
- pp
, ep
= (pp
= cp
) + len
- 2;