]>
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
)
41 char c1
= islower ((unsigned char) *a
) ?
42 toupper ((unsigned char) *a
) : *a
;
43 char c2
= islower ((unsigned char) *b
) ?
44 toupper ((unsigned char) *b
) : *b
;
56 return (strncmp (p
, " AT ", 4)
57 && strncmp (p
, " At ", 4)
58 && strncmp (p
, " aT ", 4)
59 && strncmp (p
, " at ", 4) ? FALSE
: TRUE
);
65 * getadrx() implements a partial 822-style address parser. The parser
66 * is neither complete nor correct. It does however recognize nearly all
67 * of the 822 address syntax. In addition it handles the majority of the
68 * 733 syntax as well. Most problems arise from trying to accomodate both.
70 * In terms of 822, the route-specification in
72 * "<" [route] local-part "@" domain ">"
74 * is parsed and returned unchanged. Multiple at-signs are compressed
75 * via source-routing. Recursive groups are not allowed as per the
78 * In terms of 733, " at " is recognized as equivalent to "@".
80 * In terms of both the parser will not complain about missing hosts.
84 * We should not allow addresses like
86 * Marshall T. Rose <MRose@UCI>
88 * but should insist on
90 * "Marshall T. Rose" <MRose@UCI>
92 * Unfortunately, a lot of mailers stupidly let people get away with this.
96 * We should not allow addresses like
100 * but should insist on
104 * Unfortunately, a lot of mailers stupidly let people's UAs get away with
109 * We should not allow addresses like
111 * @UCI:MRose@UCI-750a
113 * but should insist on
115 * Marshall Rose <@UCI:MRose@UCI-750a>
117 * Unfortunately, a lot of mailers stupidly do this.
141 static struct specials special
[] = {
158 static int glevel
= 0;
159 static int ingrp
= 0;
160 static int last_lex
= LX_END
;
162 static char *dp
= NULL
;
163 static char *cp
= NULL
;
164 static char *ap
= NULL
;
165 static char *pers
= NULL
;
166 static char *mbox
= NULL
;
167 static char *host
= NULL
;
168 static char *path
= NULL
;
169 static char *grp
= NULL
;
170 static char *note
= NULL
;
171 static char err
[BUFSIZ
];
172 static char adr
[BUFSIZ
];
174 static struct adrx adrxs2
;
178 getadrx (const char *addrs
)
181 register 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 */
234 * Reject the address if key fields contain 8bit characters
237 if (contains8bit(mbox
, NULL
) || contains8bit(host
, NULL
) ||
238 contains8bit(path
, NULL
) || contains8bit(grp
, NULL
)) {
239 strcpy(err
, "Address contains 8-bit characters");
255 while (isspace ((unsigned char) *ap
))
258 sprintf (adr
, "%.*s", (int)(cp
- ap
), ap
);
261 bp
= adr
+ strlen (adr
) - 1;
262 if (*bp
== ',' || *bp
== ';' || *bp
== '\n')
271 adrxp
->ingrp
= ingrp
;
273 adrxp
->err
= err
[0] ? err
: NULL
;
286 switch (my_lex (buffer
)) {
289 pers
= strdup (buffer
);
294 strcpy (err
, "extraneous semi-colon");
307 case LX_LBRK
: /* sigh (2) */
310 case LX_AT
: /* sigh (3) */
312 if (route_addr (buffer
) == NOTOK
)
314 return OK
; /* why be choosy? */
317 sprintf (err
, "illegal address construct (%s)", buffer
);
321 switch (my_lex (buffer
)) {
324 pers
= add (buffer
, add (" ", pers
));
325 more_phrase
: ; /* sigh (1) */
326 if (phrase (buffer
) == NOTOK
)
332 if (route_addr (buffer
) == NOTOK
)
334 if (last_lex
== LX_RBRK
)
336 sprintf (err
, "missing right-bracket (%s)", buffer
);
342 sprintf (err
, "nested groups not allowed (%s)", pers
);
345 grp
= add (": ", pers
);
351 switch (my_lex (buffer
)) {
353 case LX_END
: /* tsk, tsk */
362 return parse_address ();
366 case LX_DOT
: /* sigh (1) */
367 pers
= add (".", pers
);
371 sprintf (err
, "no mailbox in address, only a phrase (%s%s)",
383 mbox
= add (buffer
, pers
);
385 if (route_addr (buffer
) == NOTOK
)
393 if (domain (buffer
) == NOTOK
)
399 strcpy (err
, "extraneous semi-colon");
407 sprintf (err
, "junk after local@domain (%s)", buffer
);
411 case LX_SEMI
: /* no host */
415 if (last_lex
== LX_SEMI
&& glevel
-- <= 0) {
416 strcpy (err
, "extraneous semi-colon");
424 sprintf (err
, "missing mailbox (%s)", buffer
);
431 phrase (char *buffer
)
434 switch (my_lex (buffer
)) {
437 pers
= add (buffer
, add (" ", pers
));
447 route_addr (char *buffer
)
449 register char *pp
= cp
;
451 if (my_lex (buffer
) == LX_AT
) {
452 if (route (buffer
) == NOTOK
)
458 if (local_part (buffer
) == NOTOK
)
463 return domain (buffer
);
465 case LX_SEMI
: /* if in group */
466 case LX_RBRK
: /* no host */
472 sprintf (err
, "no at-sign after local-part (%s)", buffer
);
479 local_part (char *buffer
)
484 switch (my_lex (buffer
)) {
487 mbox
= add (buffer
, mbox
);
491 sprintf (err
, "no mailbox in local-part (%s)", buffer
);
495 switch (my_lex (buffer
)) {
497 mbox
= add (buffer
, mbox
);
508 domain (char *buffer
)
511 switch (my_lex (buffer
)) {
514 host
= add (buffer
, host
);
518 sprintf (err
, "no sub-domain in domain-part of address (%s)", buffer
);
522 switch (my_lex (buffer
)) {
524 host
= add (buffer
, host
);
527 case LX_AT
: /* sigh (0) */
528 mbox
= add (host
, add ("%", mbox
));
546 switch (my_lex (buffer
)) {
549 path
= add (buffer
, path
);
553 sprintf (err
, "no sub-domain in domain-part of address (%s)", buffer
);
556 switch (my_lex (buffer
)) {
558 path
= add (buffer
, path
);
560 switch (my_lex (buffer
)) {
565 path
= add (buffer
, path
);
569 sprintf (err
, "no at-sign found for next domain in route (%s)",
576 case LX_AT
: /* XXX */
578 path
= add (buffer
, path
);
582 path
= add (buffer
, path
);
586 sprintf (err
, "no colon found to terminate route (%s)", buffer
);
594 my_lex (char *buffer
)
596 /* buffer should be at least BUFSIZ bytes long */
600 /* Add C to the buffer bp. After use of this macro *bp is guaranteed to be within the buffer. */
601 #define ADDCHR(C) do { *bp++ = (C); if ((bp - buffer) == (BUFSIZ-1)) goto my_lex_buffull; } while (0)
606 return (last_lex
= LX_END
);
610 while (isspace ((unsigned char) c
))
614 return (last_lex
= LX_END
);
623 return (last_lex
= LX_ERR
);
626 if ((c
= *cp
++) == 0) {
628 return (last_lex
= LX_ERR
);
641 note
= note
? add (buffer
, add (" ", note
))
643 return my_lex (buffer
);
654 return (last_lex
= LX_ERR
);
657 if ((c
= *cp
++) == 0) {
659 return (last_lex
= LX_ERR
);
667 return (last_lex
= LX_QSTR
);
677 return (last_lex
= LX_ERR
);
680 if ((c
= *cp
++) == 0) {
682 return (last_lex
= LX_ERR
);
690 return (last_lex
= LX_DLIT
);
696 for (i
= 0; special
[i
].lx_chr
!= 0; i
++)
697 if (c
== special
[i
].lx_chr
)
698 return (last_lex
= special
[i
].lx_val
);
700 if (iscntrl ((unsigned char) c
))
701 return (last_lex
= LX_ERR
);
704 if ((c
= *cp
++) == 0)
706 for (i
= 0; special
[i
].lx_chr
!= 0; i
++)
707 if (c
== special
[i
].lx_chr
)
709 if (iscntrl ((unsigned char) c
) || isspace ((unsigned char) c
))
719 last_lex
= !gotat
|| cp
== NULL
|| strchr(cp
, '<') != NULL
724 /* Out of buffer space. *bp is the last byte in the buffer */
726 return (last_lex
= LX_ERR
);
731 legal_person (const char *p
)
734 register const char *cp
;
735 static char buffer
[BUFSIZ
];
739 for (cp
= p
; *cp
; cp
++)
740 for (i
= 0; special
[i
].lx_chr
; i
++)
741 if (*cp
== special
[i
].lx_chr
) {
742 sprintf (buffer
, "\"%s\"", p
);
751 mfgets (FILE *in
, char **bp
)
754 register char *cp
, *dp
, *ep
;
756 static char *pp
= NULL
;
759 pp
= mh_xmalloc ((size_t) (len
= BUFSIZ
));
761 for (ep
= (cp
= pp
) + len
- 2;;) {
762 switch (i
= getc (in
)) {
780 if (cp
== pp
) /* end of headers, gobble it */
782 switch (i
= getc (in
)) {
783 default: /* end of line */
784 case '\n': /* end of headers, save for next call */
788 case ' ': /* continue headers */
792 } /* fall into default case */
799 dp
= mh_xrealloc (pp
, (size_t) (len
+= BUFSIZ
));
800 cp
+= dp
- pp
, ep
= (pp
= cp
) + len
- 2;