]>
diplodocus.org Git - nmh/blob - sbr/mf.c
1 /* mf.c -- mail filter subroutines
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
15 static int isat (const char *);
16 static int parse_address (void);
17 static int phrase (char *);
18 static int route_addr (char *);
19 static int local_part (char *);
20 static int domain (char *);
21 static int route (char *);
22 static int my_lex (char *);
29 (p
[1] == 'a' || p
[1] == 'A') &&
30 (p
[2] == 't' || p
[2] == 'T') &&
37 * getadrx() implements a partial 822-style address parser. The parser
38 * is neither complete nor correct. It does however recognize nearly all
39 * of the 822 address syntax. In addition it handles the majority of the
40 * 733 syntax as well. Most problems arise from trying to accommodate both.
42 * In terms of 822, the route-specification in
44 * "<" [route] local-part "@" domain ">"
46 * is parsed and returned unchanged. Multiple at-signs are compressed
47 * via source-routing. Recursive groups are not allowed as per the
50 * In terms of 733, " at " is recognized as equivalent to "@".
52 * In terms of both the parser will not complain about missing hosts.
56 * We should not allow addresses like
58 * Marshall T. Rose <MRose@UCI>
60 * but should insist on
62 * "Marshall T. Rose" <MRose@UCI>
64 * Unfortunately, a lot of mailers stupidly let people get away with this.
68 * We should not allow addresses like
72 * but should insist on
76 * Unfortunately, a lot of mailers stupidly let people's UAs get away with
81 * We should not allow addresses like
85 * but should insist on
87 * Marshall Rose <@UCI:MRose@UCI-750a>
89 * Unfortunately, a lot of mailers stupidly do this.
113 static struct specials special
[] = {
130 static int glevel
= 0;
131 static int ingrp
= 0;
132 static int last_lex
= LX_END
;
134 static char *dp
= NULL
;
135 static char *cp
= NULL
;
136 static char *ap
= NULL
;
137 static char *pers
= NULL
;
138 static char *mbox
= NULL
;
139 static char *host
= NULL
;
140 static char *routepath
= NULL
;
141 static char *grp
= NULL
;
142 static char *note
= NULL
;
143 static char err
[BUFSIZ
];
144 static char adr
[BUFSIZ
];
146 static struct adrx adrxs2
;
149 /* eai = Email Address Internationalization */
151 getadrx (const char *addrs
, int eai
)
154 struct adrx
*adrxp
= &adrxs2
;
162 pers
= mbox
= host
= routepath
= grp
= note
= NULL
;
166 dp
= cp
= strdup (FENDNULL(addrs
));
176 switch (parse_address ()) {
188 default: /* catch trailing comments */
202 * Reject the address if key fields contain 8bit characters
205 if (contains8bit(mbox
, NULL
) || contains8bit(host
, NULL
) ||
206 contains8bit(routepath
, NULL
) || contains8bit(grp
, NULL
)) {
207 strcpy(err
, "Address contains 8-bit characters");
224 while (isspace ((unsigned char) *ap
))
227 snprintf(adr
, sizeof adr
, "%.*s", (int)(cp
- ap
), ap
);
230 bp
= adr
+ strlen (adr
) - 1;
231 if (*bp
== ',' || *bp
== ';' || *bp
== '\n')
238 adrxp
->path
= routepath
;
240 adrxp
->ingrp
= ingrp
;
242 adrxp
->err
= err
[0] ? err
: NULL
;
255 switch (my_lex (buffer
)) {
258 pers
= strdup (buffer
);
263 strcpy (err
, "extraneous semi-colon");
275 case LX_LBRK
: /* sigh (2) */
278 case LX_AT
: /* sigh (3) */
280 if (route_addr (buffer
) == NOTOK
)
282 return OK
; /* why be choosy? */
285 snprintf(err
, sizeof err
, "illegal address construct (%s)", buffer
);
289 switch (my_lex (buffer
)) {
292 pers
= add (buffer
, add (" ", pers
));
293 more_phrase
: ; /* sigh (1) */
294 if (phrase (buffer
) == NOTOK
)
300 if (route_addr (buffer
) == NOTOK
)
302 if (last_lex
== LX_RBRK
)
304 snprintf(err
, sizeof err
, "missing right-bracket (%s)", buffer
);
310 snprintf(err
, sizeof err
, "nested groups not allowed (%s)", pers
);
313 grp
= add (": ", pers
);
319 switch (my_lex (buffer
)) {
321 case LX_END
: /* tsk, tsk */
330 return parse_address ();
334 case LX_DOT
: /* sigh (1) */
335 pers
= add (".", pers
);
339 snprintf(err
, sizeof err
, "no mailbox in address, only a phrase (%s%s)",
351 mbox
= add (buffer
, pers
);
353 if (route_addr (buffer
) == NOTOK
)
361 if (domain (buffer
) == NOTOK
)
367 strcpy (err
, "extraneous semi-colon");
376 snprintf(err
, sizeof err
, "junk after local@domain (%s)", buffer
);
380 case LX_SEMI
: /* no host */
384 if (last_lex
== LX_SEMI
&& glevel
-- <= 0) {
385 strcpy (err
, "extraneous semi-colon");
393 snprintf(err
, sizeof err
, "missing mailbox (%s)", buffer
);
400 phrase (char *buffer
)
403 switch (my_lex (buffer
)) {
406 pers
= add (buffer
, add (" ", pers
));
416 route_addr (char *buffer
)
420 if (my_lex (buffer
) == LX_AT
) {
421 if (route (buffer
) == NOTOK
)
427 if (local_part (buffer
) == NOTOK
)
432 return domain (buffer
);
434 case LX_SEMI
: /* if in group */
435 case LX_RBRK
: /* no host */
441 snprintf(err
, sizeof err
, "no at-sign after local-part (%s)", buffer
);
448 local_part (char *buffer
)
453 switch (my_lex (buffer
)) {
456 mbox
= add (buffer
, mbox
);
460 snprintf(err
, sizeof err
, "no mailbox in local-part (%s)", buffer
);
464 switch (my_lex (buffer
)) {
466 mbox
= add (buffer
, mbox
);
477 domain (char *buffer
)
480 switch (my_lex (buffer
)) {
483 host
= add (buffer
, host
);
487 snprintf(err
, sizeof err
, "no sub-domain in domain-part of address (%s)", buffer
);
491 switch (my_lex (buffer
)) {
493 host
= add (buffer
, host
);
496 case LX_AT
: /* sigh (0) */
497 mbox
= add (host
, add ("%", mbox
));
512 routepath
= mh_xstrdup ("@");
515 switch (my_lex (buffer
)) {
518 routepath
= add (buffer
, routepath
);
522 snprintf(err
, sizeof err
, "no sub-domain in domain-part of address (%s)", buffer
);
525 switch (my_lex (buffer
)) {
527 routepath
= add (buffer
, routepath
);
529 switch (my_lex (buffer
)) {
534 routepath
= add (buffer
, routepath
);
538 snprintf(err
, sizeof err
, "no at-sign found for next domain in route (%s)",
545 case LX_AT
: /* XXX */
547 routepath
= add (buffer
, routepath
);
551 routepath
= add (buffer
, routepath
);
555 snprintf(err
, sizeof err
, "no colon found to terminate route (%s)", buffer
);
563 my_lex (char *buffer
)
565 /* buffer should be at least BUFSIZ bytes long */
569 /* Add C to the buffer bp. After use of this macro *bp is guaranteed to be within the buffer. */
570 #define ADDCHR(C) do { *bp++ = (C); if ((bp - buffer) == (BUFSIZ-1)) goto my_lex_buffull; } while (0)
575 return (last_lex
= LX_END
);
579 while (isspace ((unsigned char) c
))
583 return (last_lex
= LX_END
);
592 return (last_lex
= LX_ERR
);
595 if ((c
= *cp
++) == 0) {
597 return (last_lex
= LX_ERR
);
611 note
= note
? add (buffer
, add (" ", note
))
613 return my_lex (buffer
);
624 return (last_lex
= LX_ERR
);
627 if ((c
= *cp
++) == 0) {
629 return (last_lex
= LX_ERR
);
638 return (last_lex
= LX_QSTR
);
648 return (last_lex
= LX_ERR
);
651 if ((c
= *cp
++) == 0) {
653 return (last_lex
= LX_ERR
);
662 return (last_lex
= LX_DLIT
);
668 for (i
= 0; special
[i
].lx_chr
!= 0; i
++)
669 if (c
== special
[i
].lx_chr
)
670 return (last_lex
= special
[i
].lx_val
);
672 if (iscntrl ((unsigned char) c
))
673 return (last_lex
= LX_ERR
);
676 if ((c
= *cp
++) == 0)
678 for (i
= 0; special
[i
].lx_chr
!= 0; i
++)
679 if (c
== special
[i
].lx_chr
)
681 if (iscntrl ((unsigned char) c
) || isspace ((unsigned char) c
))
691 last_lex
= !gotat
|| cp
== NULL
|| strchr(cp
, '<') != NULL
696 /* Out of buffer space. *bp is the last byte in the buffer */
698 return (last_lex
= LX_ERR
);
703 legal_person (const char *p
)
707 static char buffer
[BUFSIZ
];
711 for (cp
= p
; *cp
; cp
++)
712 for (i
= 0; special
[i
].lx_chr
; i
++)
713 if (*cp
== special
[i
].lx_chr
) {
714 snprintf(buffer
, sizeof buffer
, "\"%s\"", p
);