]>
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 */
246 while (isspace ((unsigned char) *ap
))
249 sprintf (adr
, "%.*s", (int)(cp
- ap
), ap
);
252 bp
= adr
+ strlen (adr
) - 1;
253 if (*bp
== ',' || *bp
== ';' || *bp
== '\n')
262 adrxp
->ingrp
= ingrp
;
264 adrxp
->err
= err
[0] ? err
: NULL
;
277 switch (my_lex (buffer
)) {
280 pers
= strdup (buffer
);
285 strcpy (err
, "extraneous semi-colon");
298 case LX_LBRK
: /* sigh (2) */
301 case LX_AT
: /* sigh (3) */
303 if (route_addr (buffer
) == NOTOK
)
305 return OK
; /* why be choosy? */
308 sprintf (err
, "illegal address construct (%s)", buffer
);
312 switch (my_lex (buffer
)) {
315 pers
= add (buffer
, add (" ", pers
));
316 more_phrase
: ; /* sigh (1) */
317 if (phrase (buffer
) == NOTOK
)
323 if (route_addr (buffer
) == NOTOK
)
325 if (last_lex
== LX_RBRK
)
327 sprintf (err
, "missing right-bracket (%s)", buffer
);
333 sprintf (err
, "nested groups not allowed (%s)", pers
);
336 grp
= add (": ", pers
);
342 switch (my_lex (buffer
)) {
344 case LX_END
: /* tsk, tsk */
353 return parse_address ();
357 case LX_DOT
: /* sigh (1) */
358 pers
= add (".", pers
);
362 sprintf (err
, "no mailbox in address, only a phrase (%s%s)",
374 mbox
= add (buffer
, pers
);
376 if (route_addr (buffer
) == NOTOK
)
384 if (domain (buffer
) == NOTOK
)
390 strcpy (err
, "extraneous semi-colon");
398 sprintf (err
, "junk after local@domain (%s)", buffer
);
402 case LX_SEMI
: /* no host */
406 if (last_lex
== LX_SEMI
&& glevel
-- <= 0) {
407 strcpy (err
, "extraneous semi-colon");
415 sprintf (err
, "missing mailbox (%s)", buffer
);
422 phrase (char *buffer
)
425 switch (my_lex (buffer
)) {
428 pers
= add (buffer
, add (" ", pers
));
438 route_addr (char *buffer
)
440 register char *pp
= cp
;
442 if (my_lex (buffer
) == LX_AT
) {
443 if (route (buffer
) == NOTOK
)
449 if (local_part (buffer
) == NOTOK
)
454 return domain (buffer
);
456 case LX_SEMI
: /* if in group */
457 case LX_RBRK
: /* no host */
463 sprintf (err
, "no at-sign after local-part (%s)", buffer
);
470 local_part (char *buffer
)
475 switch (my_lex (buffer
)) {
478 mbox
= add (buffer
, mbox
);
482 sprintf (err
, "no mailbox in local-part (%s)", buffer
);
486 switch (my_lex (buffer
)) {
488 mbox
= add (buffer
, mbox
);
499 domain (char *buffer
)
502 switch (my_lex (buffer
)) {
505 host
= add (buffer
, host
);
509 sprintf (err
, "no sub-domain in domain-part of address (%s)", buffer
);
513 switch (my_lex (buffer
)) {
515 host
= add (buffer
, host
);
518 case LX_AT
: /* sigh (0) */
519 mbox
= add (host
, add ("%", mbox
));
537 switch (my_lex (buffer
)) {
540 path
= add (buffer
, path
);
544 sprintf (err
, "no sub-domain in domain-part of address (%s)", buffer
);
547 switch (my_lex (buffer
)) {
549 path
= add (buffer
, path
);
551 switch (my_lex (buffer
)) {
556 path
= add (buffer
, path
);
560 sprintf (err
, "no at-sign found for next domain in route (%s)",
567 case LX_AT
: /* XXX */
569 path
= add (buffer
, path
);
573 path
= add (buffer
, path
);
577 sprintf (err
, "no colon found to terminate route (%s)", buffer
);
585 my_lex (char *buffer
)
587 /* buffer should be at least BUFSIZ bytes long */
591 /* Add C to the buffer bp. After use of this macro *bp is guaranteed to be within the buffer. */
592 #define ADDCHR(C) do { *bp++ = (C); if ((bp - buffer) == (BUFSIZ-1)) goto my_lex_buffull; } while (0)
597 return (last_lex
= LX_END
);
601 while (isspace ((unsigned char) c
))
605 return (last_lex
= LX_END
);
614 return (last_lex
= LX_ERR
);
617 if ((c
= *cp
++) == 0) {
619 return (last_lex
= LX_ERR
);
632 note
= note
? add (buffer
, add (" ", note
))
634 return my_lex (buffer
);
645 return (last_lex
= LX_ERR
);
648 if ((c
= *cp
++) == 0) {
650 return (last_lex
= LX_ERR
);
658 return (last_lex
= LX_QSTR
);
668 return (last_lex
= LX_ERR
);
671 if ((c
= *cp
++) == 0) {
673 return (last_lex
= LX_ERR
);
681 return (last_lex
= LX_DLIT
);
687 for (i
= 0; special
[i
].lx_chr
!= 0; i
++)
688 if (c
== special
[i
].lx_chr
)
689 return (last_lex
= special
[i
].lx_val
);
691 if (iscntrl ((unsigned char) c
))
692 return (last_lex
= LX_ERR
);
695 if ((c
= *cp
++) == 0)
697 for (i
= 0; special
[i
].lx_chr
!= 0; i
++)
698 if (c
== special
[i
].lx_chr
)
700 if (iscntrl ((unsigned char) c
) || isspace ((unsigned char) c
))
710 last_lex
= !gotat
|| cp
== NULL
|| strchr(cp
, '<') != NULL
715 /* Out of buffer space. *bp is the last byte in the buffer */
717 return (last_lex
= LX_ERR
);
722 legal_person (const char *p
)
725 register const char *cp
;
726 static char buffer
[BUFSIZ
];
730 for (cp
= p
; *cp
; cp
++)
731 for (i
= 0; special
[i
].lx_chr
; i
++)
732 if (*cp
== special
[i
].lx_chr
) {
733 sprintf (buffer
, "\"%s\"", p
);
742 mfgets (FILE *in
, char **bp
)
745 register char *cp
, *dp
, *ep
;
747 static char *pp
= NULL
;
750 pp
= mh_xmalloc ((size_t) (len
= BUFSIZ
));
752 for (ep
= (cp
= pp
) + len
- 2;;) {
753 switch (i
= getc (in
)) {
771 if (cp
== pp
) /* end of headers, gobble it */
773 switch (i
= getc (in
)) {
774 default: /* end of line */
775 case '\n': /* end of headers, save for next call */
779 case ' ': /* continue headers */
783 } /* fall into default case */
790 dp
= mh_xrealloc (pp
, (size_t) (len
+= BUFSIZ
));
791 cp
+= dp
- pp
, ep
= (pp
= cp
) + len
- 2;