]>
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.
18 static int isat (const char *);
19 static int parse_address (void);
20 static int phrase (char *);
21 static int route_addr (char *);
22 static int local_part (char *);
23 static int domain (char *);
24 static int route (char *);
25 static int my_lex (char *);
29 isfrom(const char *string
)
31 return (strncmp (string
, "From ", 5) == 0
32 || strncmp (string
, ">From ", 6) == 0);
37 lequal (const char *a
, const char *b
)
43 char c1
= islower ((unsigned char) *a
) ?
44 toupper ((unsigned char) *a
) : *a
;
45 char c2
= islower ((unsigned char) *b
) ?
46 toupper ((unsigned char) *b
) : *b
;
58 return (strncmp (p
, " AT ", 4)
59 && strncmp (p
, " At ", 4)
60 && strncmp (p
, " aT ", 4)
61 && strncmp (p
, " at ", 4) ? FALSE
: TRUE
);
67 * getadrx() implements a partial 822-style address parser. The parser
68 * is neither complete nor correct. It does however recognize nearly all
69 * of the 822 address syntax. In addition it handles the majority of the
70 * 733 syntax as well. Most problems arise from trying to accomodate both.
72 * In terms of 822, the route-specification in
74 * "<" [route] local-part "@" domain ">"
76 * is parsed and returned unchanged. Multiple at-signs are compressed
77 * via source-routing. Recursive groups are not allowed as per the
80 * In terms of 733, " at " is recognized as equivalent to "@".
82 * In terms of both the parser will not complain about missing hosts.
86 * We should not allow addresses like
88 * Marshall T. Rose <MRose@UCI>
90 * but should insist on
92 * "Marshall T. Rose" <MRose@UCI>
94 * Unfortunately, a lot of mailers stupidly let people get away with this.
98 * We should not allow addresses like
102 * but should insist on
106 * Unfortunately, a lot of mailers stupidly let people's UAs get away with
111 * We should not allow addresses like
113 * @UCI:MRose@UCI-750a
115 * but should insist on
117 * Marshall Rose <@UCI:MRose@UCI-750a>
119 * Unfortunately, a lot of mailers stupidly do this.
143 static struct specials special
[] = {
160 static int glevel
= 0;
161 static int ingrp
= 0;
162 static int last_lex
= LX_END
;
164 static char *dp
= NULL
;
165 static char *cp
= NULL
;
166 static char *ap
= NULL
;
167 static char *pers
= NULL
;
168 static char *mbox
= NULL
;
169 static char *host
= NULL
;
170 static char *path
= NULL
;
171 static char *grp
= NULL
;
172 static char *note
= NULL
;
173 static char err
[BUFSIZ
];
174 static char adr
[BUFSIZ
];
176 static struct adrx adrxs2
;
180 getadrx (const char *addrs
)
183 register 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 */
248 while (isspace ((unsigned char) *ap
))
251 sprintf (adr
, "%.*s", (int)(cp
- ap
), ap
);
254 bp
= adr
+ strlen (adr
) - 1;
255 if (*bp
== ',' || *bp
== ';' || *bp
== '\n')
264 adrxp
->ingrp
= ingrp
;
266 adrxp
->err
= err
[0] ? err
: NULL
;
279 switch (my_lex (buffer
)) {
282 pers
= strdup (buffer
);
287 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 sprintf (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 sprintf (err
, "missing right-bracket (%s)", buffer
);
335 sprintf (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 sprintf (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");
400 sprintf (err
, "junk after local@domain (%s)", buffer
);
404 case LX_SEMI
: /* no host */
408 if (last_lex
== LX_SEMI
&& glevel
-- <= 0) {
409 strcpy (err
, "extraneous semi-colon");
417 sprintf (err
, "missing mailbox (%s)", buffer
);
424 phrase (char *buffer
)
427 switch (my_lex (buffer
)) {
430 pers
= add (buffer
, add (" ", pers
));
440 route_addr (char *buffer
)
442 register char *pp
= cp
;
444 if (my_lex (buffer
) == LX_AT
) {
445 if (route (buffer
) == NOTOK
)
451 if (local_part (buffer
) == NOTOK
)
456 return domain (buffer
);
458 case LX_SEMI
: /* if in group */
459 case LX_RBRK
: /* no host */
465 sprintf (err
, "no at-sign after local-part (%s)", buffer
);
472 local_part (char *buffer
)
477 switch (my_lex (buffer
)) {
480 mbox
= add (buffer
, mbox
);
484 sprintf (err
, "no mailbox in local-part (%s)", buffer
);
488 switch (my_lex (buffer
)) {
490 mbox
= add (buffer
, mbox
);
501 domain (char *buffer
)
504 switch (my_lex (buffer
)) {
507 host
= add (buffer
, host
);
511 sprintf (err
, "no sub-domain in domain-part of address (%s)", buffer
);
515 switch (my_lex (buffer
)) {
517 host
= add (buffer
, host
);
520 case LX_AT
: /* sigh (0) */
521 mbox
= add (host
, add ("%", mbox
));
539 switch (my_lex (buffer
)) {
542 path
= add (buffer
, path
);
546 sprintf (err
, "no sub-domain in domain-part of address (%s)", buffer
);
549 switch (my_lex (buffer
)) {
551 path
= add (buffer
, path
);
553 switch (my_lex (buffer
)) {
558 path
= add (buffer
, path
);
562 sprintf (err
, "no at-sign found for next domain in route (%s)",
569 case LX_AT
: /* XXX */
571 path
= add (buffer
, path
);
575 path
= add (buffer
, path
);
579 sprintf (err
, "no colon found to terminate route (%s)", buffer
);
587 my_lex (char *buffer
)
589 /* buffer should be at least BUFSIZ bytes long */
593 /* Add C to the buffer bp. After use of this macro *bp is guaranteed to be within the buffer. */
594 #define ADDCHR(C) do { *bp++ = (C); if ((bp - buffer) == (BUFSIZ-1)) goto my_lex_buffull; } while (0)
599 return (last_lex
= LX_END
);
603 while (isspace ((unsigned char) c
))
607 return (last_lex
= LX_END
);
616 return (last_lex
= LX_ERR
);
619 if ((c
= *cp
++) == 0) {
621 return (last_lex
= LX_ERR
);
634 note
= note
? add (buffer
, add (" ", note
))
636 return my_lex (buffer
);
647 return (last_lex
= LX_ERR
);
650 if ((c
= *cp
++) == 0) {
652 return (last_lex
= LX_ERR
);
660 return (last_lex
= LX_QSTR
);
670 return (last_lex
= LX_ERR
);
673 if ((c
= *cp
++) == 0) {
675 return (last_lex
= LX_ERR
);
683 return (last_lex
= LX_DLIT
);
689 for (i
= 0; special
[i
].lx_chr
!= 0; i
++)
690 if (c
== special
[i
].lx_chr
)
691 return (last_lex
= special
[i
].lx_val
);
693 if (iscntrl ((unsigned char) c
))
694 return (last_lex
= LX_ERR
);
697 if ((c
= *cp
++) == 0)
699 for (i
= 0; special
[i
].lx_chr
!= 0; i
++)
700 if (c
== special
[i
].lx_chr
)
702 if (iscntrl ((unsigned char) c
) || isspace ((unsigned char) c
))
712 last_lex
= !gotat
|| cp
== NULL
|| strchr(cp
, '<') != NULL
717 /* Out of buffer space. *bp is the last byte in the buffer */
719 return (last_lex
= LX_ERR
);
724 legal_person (const char *p
)
727 register const char *cp
;
728 static char buffer
[BUFSIZ
];
732 for (cp
= p
; *cp
; cp
++)
733 for (i
= 0; special
[i
].lx_chr
; i
++)
734 if (*cp
== special
[i
].lx_chr
) {
735 sprintf (buffer
, "\"%s\"", p
);
744 mfgets (FILE *in
, char **bp
)
747 register char *cp
, *dp
, *ep
;
749 static char *pp
= NULL
;
752 pp
= mh_xmalloc ((size_t) (len
= BUFSIZ
));
754 for (ep
= (cp
= pp
) + len
- 2;;) {
755 switch (i
= getc (in
)) {
773 if (cp
== pp
) /* end of headers, gobble it */
775 switch (i
= getc (in
)) {
776 default: /* end of line */
777 case '\n': /* end of headers, save for next call */
781 case ' ': /* continue headers */
785 } /* fall into default case */
792 dp
= mh_xrealloc (pp
, (size_t) (len
+= BUFSIZ
));
793 cp
+= dp
- pp
, ep
= (pp
= cp
) + len
- 2;