]>
diplodocus.org Git - nmh/blob - sbr/mf.c
3 * mf.c -- mail filter subroutines
7 * This code is Copyright (c) 2002, by the authors of nmh. See the
8 * COPYRIGHT file in the root directory of the nmh distribution for
9 * complete copyright information.
20 static char *getcpy (char *);
21 static void compress (char *, unsigned char *);
22 static int isat (char *);
23 static int parse_address (void);
24 static int phrase (char *);
25 static int route_addr (char *);
26 static int local_part (char *);
27 static int domain (char *);
28 static int route (char *);
29 static int my_lex (char *);
38 /* causes compiles to blow up because the symbol _cleanup is undefined
39 where did this ever come from? */
45 p
= mh_xmalloc ((size_t) (strlen (s
) + 2));
54 return (strncmp (string
, "From ", 5) == 0
55 || strncmp (string
, ">From ", 6) == 0);
60 lequal (unsigned char *a
, unsigned char *b
)
66 char c1
= islower (*a
) ? toupper (*a
) : *a
;
67 char c2
= islower (*b
) ? toupper (*b
) : *b
;
77 * seekadrx() is tricky. We want to cover both UUCP-style and ARPA-style
78 * addresses, so for each list of addresses we see if we can find some
79 * character to give us a hint.
83 #define CHKADR 0 /* undertermined address style */
84 #define UNIXDR 1 /* UNIX-style address */
85 #define ARPADR 2 /* ARPAnet-style address */
88 static char *punctuators
= ";<>.()[]";
89 static char *vp
= NULL
;
90 static char *tp
= NULL
;
92 static struct adrx adrxs1
;
96 seekadrx (char *addrs
)
98 static int state
= CHKADR
;
100 register struct adrx
*adrxp
;
103 for (state
= UNIXDR
, cp
= addrs
; *cp
; cp
++)
104 if (strchr(punctuators
, *cp
)) {
111 adrxp
= uucpadrx (addrs
);
116 adrxp
= getadrx (addrs
);
128 * uucpadrx() implements a partial UUCP-style address parser. It's based
129 * on the UUCP notion that addresses are separated by spaces or commas.
134 uucpadrx (char *addrs
)
136 register unsigned char *cp
, *wp
, *xp
, *yp
;
138 register struct adrx
*adrxp
= &adrxs1
;
141 vp
= tp
= getcpy (addrs
);
142 compress (addrs
, vp
);
151 for (cp
= tp
; isspace (*cp
); cp
++)
159 if ((wp
= strchr(cp
, ',')) == NULL
) {
160 if ((wp
= strchr(cp
, ' ')) != NULL
) {
162 while (isspace (*xp
))
164 if (*xp
!= 0 && isat (--xp
)) {
166 while (isspace (*yp
))
169 if ((zp
= strchr(yp
, ' ')) != NULL
)
188 adrxp
->text
= getcpy (cp
);
190 adrxp
->host
= adrxp
->path
= NULL
;
191 if ((wp
= strrchr(cp
, '@')) != NULL
) {
193 adrxp
->host
= *wp
? wp
: NULL
;
196 for (wp
= cp
+ strlen (cp
) - 4; wp
>= cp
; wp
--)
199 adrxp
->host
= wp
+ 3;
202 adrxp
->pers
= adrxp
->grp
= adrxp
->note
= adrxp
->err
= NULL
;
210 compress (char *fp
, unsigned char *tp
)
213 register unsigned char *cp
;
215 for (c
= ' ', cp
= tp
; (*tp
= *fp
++) != 0;)
223 if (c
== ' ' && cp
< tp
)
231 return (strncmp (p
, " AT ", 4)
232 && strncmp (p
, " At ", 4)
233 && strncmp (p
, " aT ", 4)
234 && strncmp (p
, " at ", 4) ? FALSE
: TRUE
);
240 * getadrx() implements a partial 822-style address parser. The parser
241 * is neither complete nor correct. It does however recognize nearly all
242 * of the 822 address syntax. In addition it handles the majority of the
243 * 733 syntax as well. Most problems arise from trying to accomodate both.
245 * In terms of 822, the route-specification in
247 * "<" [route] local-part "@" domain ">"
249 * is parsed and returned unchanged. Multiple at-signs are compressed
250 * via source-routing. Recursive groups are not allowed as per the
253 * In terms of 733, " at " is recognized as equivalent to "@".
255 * In terms of both the parser will not complain about missing hosts.
259 * We should not allow addresses like
261 * Marshall T. Rose <MRose@UCI>
263 * but should insist on
265 * "Marshall T. Rose" <MRose@UCI>
267 * Unfortunately, a lot of mailers stupidly let people get away with this.
271 * We should not allow addresses like
275 * but should insist on
279 * Unfortunately, a lot of mailers stupidly let people's UAs get away with
284 * We should not allow addresses like
286 * @UCI:MRose@UCI-750a
288 * but should insist on
290 * Marshall Rose <@UCI:MRose@UCI-750a>
292 * Unfortunately, a lot of mailers stupidly do this.
316 static struct specials special
[] = {
333 static int glevel
= 0;
334 static int ingrp
= 0;
335 static int last_lex
= LX_END
;
337 static char *dp
= NULL
;
338 static unsigned char *cp
= NULL
;
339 static unsigned char *ap
= NULL
;
340 static char *pers
= NULL
;
341 static char *mbox
= NULL
;
342 static char *host
= NULL
;
343 static char *path
= NULL
;
344 static char *grp
= NULL
;
345 static char *note
= NULL
;
346 static char err
[BUFSIZ
];
347 static char adr
[BUFSIZ
];
349 static struct adrx adrxs2
;
353 getadrx (char *addrs
)
356 register struct adrx
*adrxp
= &adrxs2
;
370 pers
= mbox
= host
= path
= grp
= note
= NULL
;
374 dp
= cp
= getcpy (addrs
? addrs
: "");
384 switch (parse_address ()) {
396 default: /* catch trailing comments */
421 while (isspace (*ap
))
424 sprintf (adr
, "%.*s", (int)(cp
- ap
), ap
);
427 bp
= adr
+ strlen (adr
) - 1;
428 if (*bp
== ',' || *bp
== ';' || *bp
== '\n')
437 adrxp
->ingrp
= ingrp
;
439 adrxp
->err
= err
[0] ? err
: NULL
;
452 switch (my_lex (buffer
)) {
455 pers
= getcpy (buffer
);
460 strcpy (err
, "extraneous semi-colon");
473 case LX_LBRK
: /* sigh (2) */
476 case LX_AT
: /* sigh (3) */
478 if (route_addr (buffer
) == NOTOK
)
480 return OK
; /* why be choosy? */
483 sprintf (err
, "illegal address construct (%s)", buffer
);
487 switch (my_lex (buffer
)) {
490 pers
= add (buffer
, add (" ", pers
));
491 more_phrase
: ; /* sigh (1) */
492 if (phrase (buffer
) == NOTOK
)
498 if (route_addr (buffer
) == NOTOK
)
500 if (last_lex
== LX_RBRK
)
502 sprintf (err
, "missing right-bracket (%s)", buffer
);
508 sprintf (err
, "nested groups not allowed (%s)", pers
);
511 grp
= add (": ", pers
);
517 switch (my_lex (buffer
)) {
519 case LX_END
: /* tsk, tsk */
528 return parse_address ();
532 case LX_DOT
: /* sigh (1) */
533 pers
= add (".", pers
);
537 sprintf (err
, "no mailbox in address, only a phrase (%s%s)",
549 mbox
= add (buffer
, pers
);
551 if (route_addr (buffer
) == NOTOK
)
559 if (domain (buffer
) == NOTOK
)
565 strcpy (err
, "extraneous semi-colon");
573 sprintf (err
, "junk after local@domain (%s)", buffer
);
577 case LX_SEMI
: /* no host */
581 if (last_lex
== LX_SEMI
&& glevel
-- <= 0) {
582 strcpy (err
, "extraneous semi-colon");
590 sprintf (err
, "missing mailbox (%s)", buffer
);
597 phrase (char *buffer
)
600 switch (my_lex (buffer
)) {
603 pers
= add (buffer
, add (" ", pers
));
613 route_addr (char *buffer
)
615 register char *pp
= cp
;
617 if (my_lex (buffer
) == LX_AT
) {
618 if (route (buffer
) == NOTOK
)
624 if (local_part (buffer
) == NOTOK
)
629 return domain (buffer
);
631 case LX_SEMI
: /* if in group */
632 case LX_RBRK
: /* no host */
638 sprintf (err
, "no at-sign after local-part (%s)", buffer
);
645 local_part (char *buffer
)
650 switch (my_lex (buffer
)) {
653 mbox
= add (buffer
, mbox
);
657 sprintf (err
, "no mailbox in local-part (%s)", buffer
);
661 switch (my_lex (buffer
)) {
663 mbox
= add (buffer
, mbox
);
674 domain (char *buffer
)
677 switch (my_lex (buffer
)) {
680 host
= add (buffer
, host
);
684 sprintf (err
, "no sub-domain in domain-part of address (%s)", buffer
);
688 switch (my_lex (buffer
)) {
690 host
= add (buffer
, host
);
693 case LX_AT
: /* sigh (0) */
694 mbox
= add (host
, add ("%", mbox
));
712 switch (my_lex (buffer
)) {
715 path
= add (buffer
, path
);
719 sprintf (err
, "no sub-domain in domain-part of address (%s)", buffer
);
722 switch (my_lex (buffer
)) {
724 path
= add (buffer
, path
);
726 switch (my_lex (buffer
)) {
731 path
= add (buffer
, path
);
735 sprintf (err
, "no at-sign found for next domain in route (%s)",
742 case LX_AT
: /* XXX */
744 path
= add (buffer
, path
);
748 path
= add (buffer
, path
);
752 sprintf (err
, "no colon found to terminate route (%s)", buffer
);
760 my_lex (char *buffer
)
762 /* buffer should be at least BUFSIZ bytes long */
764 register unsigned char c
;
767 /* Add C to the buffer bp. After use of this macro *bp is guaranteed to be within the buffer. */
768 #define ADDCHR(C) do { *bp++ = (C); if ((bp - buffer) == (BUFSIZ-1)) goto my_lex_buffull; } while (0)
773 return (last_lex
= LX_END
);
781 return (last_lex
= LX_END
);
790 return (last_lex
= LX_ERR
);
793 if ((c
= *cp
++) == 0) {
795 return (last_lex
= LX_ERR
);
808 note
= note
? add (buffer
, add (" ", note
))
810 return my_lex (buffer
);
821 return (last_lex
= LX_ERR
);
824 if ((c
= *cp
++) == 0) {
826 return (last_lex
= LX_ERR
);
834 return (last_lex
= LX_QSTR
);
844 return (last_lex
= LX_ERR
);
847 if ((c
= *cp
++) == 0) {
849 return (last_lex
= LX_ERR
);
857 return (last_lex
= LX_DLIT
);
863 for (i
= 0; special
[i
].lx_chr
!= 0; i
++)
864 if (c
== special
[i
].lx_chr
)
865 return (last_lex
= special
[i
].lx_val
);
868 return (last_lex
= LX_ERR
);
871 if ((c
= *cp
++) == 0)
873 for (i
= 0; special
[i
].lx_chr
!= 0; i
++)
874 if (c
== special
[i
].lx_chr
)
876 if (iscntrl (c
) || isspace (c
))
886 last_lex
= !gotat
|| cp
== NULL
|| strchr(cp
, '<') != NULL
891 /* Out of buffer space. *bp is the last byte in the buffer */
893 return (last_lex
= LX_ERR
);
898 legal_person (char *p
)
902 static char buffer
[BUFSIZ
];
906 for (cp
= p
; *cp
; cp
++)
907 for (i
= 0; special
[i
].lx_chr
; i
++)
908 if (*cp
== special
[i
].lx_chr
) {
909 sprintf (buffer
, "\"%s\"", p
);
918 mfgets (FILE *in
, char **bp
)
921 register char *cp
, *dp
, *ep
;
923 static char *pp
= NULL
;
926 pp
= mh_xmalloc ((size_t) (len
= BUFSIZ
));
928 for (ep
= (cp
= pp
) + len
- 2;;) {
929 switch (i
= getc (in
)) {
947 if (cp
== pp
) /* end of headers, gobble it */
949 switch (i
= getc (in
)) {
950 default: /* end of line */
951 case '\n': /* end of headers, save for next call */
955 case ' ': /* continue headers */
959 } /* fall into default case */
966 dp
= mh_xrealloc (pp
, (size_t) (len
+= BUFSIZ
));
967 cp
+= dp
- pp
, ep
= (pp
= cp
) + len
- 2;