]>
diplodocus.org Git - nmh/blob - sbr/mf.c
00e158224df058fea8bc08e0eb0a87eb51a17ada
2 * mf.c -- mail filter subroutines
4 * This code is Copyright (c) 2002, by the authors of nmh. See the
5 * COPYRIGHT file in the root directory of the nmh distribution for
6 * 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 *);
28 return has_prefix(p
, " AT ") || has_prefix(p
, " At ") ||
29 has_prefix(p
, " aT ") || has_prefix(p
, " at ");
35 * getadrx() implements a partial 822-style address parser. The parser
36 * is neither complete nor correct. It does however recognize nearly all
37 * of the 822 address syntax. In addition it handles the majority of the
38 * 733 syntax as well. Most problems arise from trying to accommodate both.
40 * In terms of 822, the route-specification in
42 * "<" [route] local-part "@" domain ">"
44 * is parsed and returned unchanged. Multiple at-signs are compressed
45 * via source-routing. Recursive groups are not allowed as per the
48 * In terms of 733, " at " is recognized as equivalent to "@".
50 * In terms of both the parser will not complain about missing hosts.
54 * We should not allow addresses like
56 * Marshall T. Rose <MRose@UCI>
58 * but should insist on
60 * "Marshall T. Rose" <MRose@UCI>
62 * Unfortunately, a lot of mailers stupidly let people get away with this.
66 * We should not allow addresses like
70 * but should insist on
74 * Unfortunately, a lot of mailers stupidly let people's UAs get away with
79 * We should not allow addresses like
83 * but should insist on
85 * Marshall Rose <@UCI:MRose@UCI-750a>
87 * Unfortunately, a lot of mailers stupidly do this.
111 static struct specials special
[] = {
128 static int glevel
= 0;
129 static int ingrp
= 0;
130 static int last_lex
= LX_END
;
132 static char *dp
= NULL
;
133 static char *cp
= NULL
;
134 static char *ap
= NULL
;
135 static char *pers
= NULL
;
136 static char *mbox
= NULL
;
137 static char *host
= NULL
;
138 static char *path
= NULL
;
139 static char *grp
= NULL
;
140 static char *note
= NULL
;
141 static char err
[BUFSIZ
];
142 static char adr
[BUFSIZ
];
144 static struct adrx adrxs2
;
147 /* eai = Email Address Internationalization */
149 getadrx (const char *addrs
, int eai
)
152 struct adrx
*adrxp
= &adrxs2
;
160 pers
= mbox
= host
= path
= grp
= note
= NULL
;
164 dp
= cp
= strdup (addrs
? addrs
: "");
174 switch (parse_address ()) {
186 default: /* catch trailing comments */
200 * Reject the address if key fields contain 8bit characters
203 if (contains8bit(mbox
, NULL
) || contains8bit(host
, NULL
) ||
204 contains8bit(path
, NULL
) || contains8bit(grp
, NULL
)) {
205 strcpy(err
, "Address contains 8-bit characters");
222 while (isspace ((unsigned char) *ap
))
225 snprintf(adr
, sizeof adr
, "%.*s", (int)(cp
- ap
), ap
);
228 bp
= adr
+ strlen (adr
) - 1;
229 if (*bp
== ',' || *bp
== ';' || *bp
== '\n')
238 adrxp
->ingrp
= ingrp
;
240 adrxp
->err
= err
[0] ? err
: NULL
;
253 switch (my_lex (buffer
)) {
256 pers
= strdup (buffer
);
261 strcpy (err
, "extraneous semi-colon");
273 case LX_LBRK
: /* sigh (2) */
276 case LX_AT
: /* sigh (3) */
278 if (route_addr (buffer
) == NOTOK
)
280 return OK
; /* why be choosy? */
283 snprintf(err
, sizeof err
, "illegal address construct (%s)", buffer
);
287 switch (my_lex (buffer
)) {
290 pers
= add (buffer
, add (" ", pers
));
291 more_phrase
: ; /* sigh (1) */
292 if (phrase (buffer
) == NOTOK
)
298 if (route_addr (buffer
) == NOTOK
)
300 if (last_lex
== LX_RBRK
)
302 snprintf(err
, sizeof err
, "missing right-bracket (%s)", buffer
);
308 snprintf(err
, sizeof err
, "nested groups not allowed (%s)", pers
);
311 grp
= add (": ", pers
);
317 switch (my_lex (buffer
)) {
319 case LX_END
: /* tsk, tsk */
328 return parse_address ();
332 case LX_DOT
: /* sigh (1) */
333 pers
= add (".", pers
);
337 snprintf(err
, sizeof err
, "no mailbox in address, only a phrase (%s%s)",
349 mbox
= add (buffer
, pers
);
351 if (route_addr (buffer
) == NOTOK
)
359 if (domain (buffer
) == NOTOK
)
365 strcpy (err
, "extraneous semi-colon");
374 snprintf(err
, sizeof err
, "junk after local@domain (%s)", buffer
);
378 case LX_SEMI
: /* no host */
382 if (last_lex
== LX_SEMI
&& glevel
-- <= 0) {
383 strcpy (err
, "extraneous semi-colon");
391 snprintf(err
, sizeof err
, "missing mailbox (%s)", buffer
);
398 phrase (char *buffer
)
401 switch (my_lex (buffer
)) {
404 pers
= add (buffer
, add (" ", pers
));
414 route_addr (char *buffer
)
418 if (my_lex (buffer
) == LX_AT
) {
419 if (route (buffer
) == NOTOK
)
425 if (local_part (buffer
) == NOTOK
)
430 return domain (buffer
);
432 case LX_SEMI
: /* if in group */
433 case LX_RBRK
: /* no host */
439 snprintf(err
, sizeof err
, "no at-sign after local-part (%s)", buffer
);
446 local_part (char *buffer
)
451 switch (my_lex (buffer
)) {
454 mbox
= add (buffer
, mbox
);
458 snprintf(err
, sizeof err
, "no mailbox in local-part (%s)", buffer
);
462 switch (my_lex (buffer
)) {
464 mbox
= add (buffer
, mbox
);
475 domain (char *buffer
)
478 switch (my_lex (buffer
)) {
481 host
= add (buffer
, host
);
485 snprintf(err
, sizeof err
, "no sub-domain in domain-part of address (%s)", buffer
);
489 switch (my_lex (buffer
)) {
491 host
= add (buffer
, host
);
494 case LX_AT
: /* sigh (0) */
495 mbox
= add (host
, add ("%", mbox
));
510 path
= mh_xstrdup ("@");
513 switch (my_lex (buffer
)) {
516 path
= add (buffer
, path
);
520 snprintf(err
, sizeof err
, "no sub-domain in domain-part of address (%s)", buffer
);
523 switch (my_lex (buffer
)) {
525 path
= add (buffer
, path
);
527 switch (my_lex (buffer
)) {
532 path
= add (buffer
, path
);
536 snprintf(err
, sizeof err
, "no at-sign found for next domain in route (%s)",
543 case LX_AT
: /* XXX */
545 path
= add (buffer
, path
);
549 path
= add (buffer
, path
);
553 snprintf(err
, sizeof err
, "no colon found to terminate route (%s)", buffer
);
561 my_lex (char *buffer
)
563 /* buffer should be at least BUFSIZ bytes long */
567 /* Add C to the buffer bp. After use of this macro *bp is guaranteed to be within the buffer. */
568 #define ADDCHR(C) do { *bp++ = (C); if ((bp - buffer) == (BUFSIZ-1)) goto my_lex_buffull; } while (0)
573 return (last_lex
= LX_END
);
577 while (isspace ((unsigned char) c
))
581 return (last_lex
= LX_END
);
590 return (last_lex
= LX_ERR
);
593 if ((c
= *cp
++) == 0) {
595 return (last_lex
= LX_ERR
);
609 note
= note
? add (buffer
, add (" ", note
))
611 return my_lex (buffer
);
622 return (last_lex
= LX_ERR
);
625 if ((c
= *cp
++) == 0) {
627 return (last_lex
= LX_ERR
);
636 return (last_lex
= LX_QSTR
);
646 return (last_lex
= LX_ERR
);
649 if ((c
= *cp
++) == 0) {
651 return (last_lex
= LX_ERR
);
660 return (last_lex
= LX_DLIT
);
666 for (i
= 0; special
[i
].lx_chr
!= 0; i
++)
667 if (c
== special
[i
].lx_chr
)
668 return (last_lex
= special
[i
].lx_val
);
670 if (iscntrl ((unsigned char) c
))
671 return (last_lex
= LX_ERR
);
674 if ((c
= *cp
++) == 0)
676 for (i
= 0; special
[i
].lx_chr
!= 0; i
++)
677 if (c
== special
[i
].lx_chr
)
679 if (iscntrl ((unsigned char) c
) || isspace ((unsigned char) c
))
689 last_lex
= !gotat
|| cp
== NULL
|| strchr(cp
, '<') != NULL
694 /* Out of buffer space. *bp is the last byte in the buffer */
696 return (last_lex
= LX_ERR
);
701 legal_person (const char *p
)
705 static char buffer
[BUFSIZ
];
709 for (cp
= p
; *cp
; cp
++)
710 for (i
= 0; special
[i
].lx_chr
; i
++)
711 if (*cp
== special
[i
].lx_chr
) {
712 snprintf(buffer
, sizeof buffer
, "\"%s\"", p
);