2 * Copyright (c) 1998-2003 Sendmail, Inc. and its suppliers.
4 * Copyright (c) 1983, 1995-1997 Eric P. Allman. All rights reserved.
5 * Copyright (c) 1988, 1993
6 * The Regents of the University of California. All rights reserved.
8 * By using this file, you agree to the terms and conditions set
9 * forth in the LICENSE file which can be found at the top level of
10 * the sendmail distribution.
16 SM_RCSID("@(#)$Id: parseaddr.c,v 8.359.2.6 2003/03/27 02:39:53 ca Exp $")
18 static void allocaddr __P((ADDRESS *, int, char *, ENVELOPE *));
19 static int callsubr __P((char**, int, ENVELOPE *));
20 static char *map_lookup __P((STAB *, char *, char **, int *, ENVELOPE *));
21 static ADDRESS *buildaddr __P((char **, ADDRESS *, int, ENVELOPE *));
22 static bool hasctrlchar __P((register char *, bool, bool));
24 /* replacement for illegal characters in addresses */
25 #define BAD_CHAR_REPLACEMENT '?'
28 ** PARSEADDR -- Parse an address
30 ** Parses an address and breaks it up into three parts: a
31 ** net to transmit the message on, the host to transmit it
32 ** to, and a user on that host. These are loaded into an
33 ** ADDRESS header with the values squirreled away if necessary.
34 ** The "user" part may not be a real user; the process may
35 ** just reoccur on that machine. For example, on a machine
36 ** with an arpanet connection, the address
37 ** csvax.bill@berkeley
38 ** will break up to a "user" of 'csvax.bill' and a host
39 ** of 'berkeley' -- to be transmitted over the arpanet.
42 ** addr -- the address to parse.
43 ** a -- a pointer to the address descriptor buffer.
44 ** If NULL, an address will be created.
45 ** flags -- describe detail for parsing. See RF_ definitions
47 ** delim -- the character to terminate the address, passed
49 ** delimptr -- if non-NULL, set to the location of the
50 ** delim character that was found.
51 ** e -- the envelope that will contain this address.
52 ** isrcpt -- true if the address denotes a recipient; false
53 ** indicates a sender.
56 ** A pointer to the address descriptor header (`a' if
64 /* following delimiters are inherent to the internal algorithms */
65 #define DELIMCHARS "()<>,;\r\n" /* default word delimiters */
68 parseaddr(addr, a, flags, delim, delimptr, e, isrcpt)
78 auto char *delimptrbuf;
80 char pvpbuf[PSBUFSIZE];
83 ** Initialize and prescan address.
88 sm_dprintf("\n--parseaddr(%s)\n", addr);
91 delimptr = &delimptrbuf;
93 pvp = prescan(addr, delim, pvpbuf, sizeof pvpbuf, delimptr, NULL);
97 sm_dprintf("parseaddr-->NULL\n");
101 if (invalidaddr(addr, delim == '\0' ? NULL : *delimptr, isrcpt))
104 sm_dprintf("parseaddr-->bad address\n");
109 ** Save addr if we are going to have to.
111 ** We have to do this early because there is a chance that
112 ** the map lookups in the rewriting rules could clobber
113 ** static memory somewhere.
116 if (bitset(RF_COPYPADDR, flags) && addr != NULL)
118 char savec = **delimptr;
122 e->e_to = addr = sm_rpool_strdup_x(e->e_rpool, addr);
128 ** Apply rewriting rules.
129 ** Ruleset 0 does basic parsing. It must resolve.
133 if (REWRITE(pvp, 3, e) == EX_TEMPFAIL)
135 if (REWRITE(pvp, 0, e) == EX_TEMPFAIL)
139 ** Build canonical address from pvp.
142 a = buildaddr(pvp, a, flags, e);
144 if (hasctrlchar(a->q_user, isrcpt, true))
147 sm_dprintf("parseaddr-->bad q_user\n");
150 ** Just mark the address as bad so DSNs work.
151 ** hasctrlchar() has to make sure that the address
152 ** has been sanitized, e.g., shortened.
155 a->q_state = QS_BADADDR;
159 ** Make local copies of the host & user and then
160 ** transport them out.
163 allocaddr(a, flags, addr, e);
164 if (QS_IS_BADADDR(a->q_state))
166 /* weed out bad characters in the printable address too */
167 (void) hasctrlchar(a->q_paddr, isrcpt, false);
172 ** Select a queue directory for recipient addresses.
173 ** This is done here and in split_across_queue_groups(),
174 ** but the latter applies to addresses after aliasing,
175 ** and only if splitting is done.
178 if ((a->q_qgrp == NOAQGRP || a->q_qgrp == ENVQGRP) &&
179 !bitset(RF_SENDERADDR|RF_HEADERADDR, flags) &&
180 OpMode != MD_INITALIAS)
184 /* call ruleset which should return a queue group name */
185 r = rscap(RS_QUEUEGROUP, a->q_user, NULL, e, &pvp, pvpbuf,
188 pvp != NULL && pvp[0] != NULL &&
189 (pvp[0][0] & 0377) == CANONNET &&
190 pvp[1] != NULL && pvp[1][0] != '\0')
192 r = name2qid(pvp[1]);
193 if (r == NOQGRP && LogLevel > 10)
194 sm_syslog(LOG_INFO, NOQID,
195 "can't find queue group name %s, selection ignored",
197 if (tTd(20, 4) && r != NOQGRP)
198 sm_syslog(LOG_INFO, NOQID,
199 "queue group name %s -> %d",
201 a->q_qgrp = r == NOQGRP ? ENVQGRP : r;
206 ** If there was a parsing failure, mark it for queueing.
209 if (qup && OpMode != MD_INITALIAS)
211 char *msg = "Transient parse error -- message queued for future delivery";
213 if (e->e_sendmode == SM_DEFER)
214 msg = "Deferring message until queue run";
216 sm_dprintf("parseaddr: queuing message\n");
218 if (e->e_message == NULL && e->e_sendmode != SM_DEFER)
219 e->e_message = sm_rpool_strdup_x(e->e_rpool, msg);
220 a->q_state = QS_QUEUEUP;
221 a->q_status = "4.4.3";
225 ** Compute return value.
230 sm_dprintf("parseaddr-->");
237 ** INVALIDADDR -- check for address containing characters used for macros
240 ** addr -- the address to check.
241 ** delimptr -- if non-NULL: end of address to check, i.e.,
242 ** a pointer in the address string.
243 ** isrcpt -- true iff the address is for a recipient.
246 ** true -- if the address has characters that are reservered
247 ** for macros or is too long.
248 ** false -- otherwise.
252 invalidaddr(addr, delimptr, isrcpt)
258 char savedelim = '\0';
262 if (delimptr != NULL)
264 /* delimptr points to the end of the address to test */
265 savedelim = *delimptr;
266 if (savedelim != '\0') /* if that isn't '\0' already: */
267 *delimptr = '\0'; /* set it */
269 for (; *addr != '\0'; addr++)
271 if ((*addr & 0340) == 0200)
275 *addr = BAD_CHAR_REPLACEMENT;
277 if (++len > MAXNAME - 1)
282 usrerr("553 5.1.0 Address \"%s\" too long (%d bytes max)",
292 usrerr("501 5.1.3 8-bit character in mailbox address \"%s\"",
295 usrerr("501 5.1.7 8-bit character in mailbox address \"%s\"",
299 if (delimptr != NULL && savedelim != '\0')
300 *delimptr = savedelim; /* restore old character at delimptr */
304 ** HASCTRLCHAR -- check for address containing meta-characters
306 ** Checks that the address contains no meta-characters, and contains
307 ** no "non-printable" characters unless they are quoted or escaped.
308 ** Quoted or escaped characters are literals.
311 ** addr -- the address to check.
312 ** isrcpt -- true if the address is for a recipient; false
314 ** complain -- true if an error should issued if the address
315 ** is invalid and should be "repaired".
318 ** true -- if the address has any "wierd" characters or
319 ** non-printable characters or if a quote is unbalanced.
320 ** false -- otherwise.
324 hasctrlchar(addr, isrcpt, complain)
326 bool isrcpt, complain;
335 for (; *addr != '\0'; addr++)
337 if (++len > MAXNAME - 1)
341 (void) shorten_rfc822_string(b, MAXNAME - 1);
342 usrerr("553 5.1.0 Address \"%s\" too long (%d bytes max)",
348 if (!quoted && (*addr < 32 || *addr == 127))
350 result = "non-printable character";
351 *addr = BAD_CHAR_REPLACEMENT;
356 else if (*addr == '\\')
358 /* XXX Generic problem: no '\0' in strings. */
361 result = "trailing \\ character";
362 *--addr = BAD_CHAR_REPLACEMENT;
366 if ((*addr & 0340) == 0200)
369 result = "8-bit character";
370 *addr = BAD_CHAR_REPLACEMENT;
375 result = "unbalanced quote"; /* unbalanced quote */
376 if (result != NULL && complain)
379 usrerr("501 5.1.3 Syntax error in mailbox address \"%s\" (%s)",
382 usrerr("501 5.1.7 Syntax error in mailbox address \"%s\" (%s)",
385 return result != NULL;
388 ** ALLOCADDR -- do local allocations of address on demand.
390 ** Also lowercases the host name if requested.
393 ** a -- the address to reallocate.
394 ** flags -- the copy flag (see RF_ definitions in sendmail.h
395 ** for a description).
396 ** paddr -- the printname of the address.
403 ** Copies portions of a into local buffers as requested.
407 allocaddr(a, flags, paddr, e)
414 sm_dprintf("allocaddr(flags=%x, paddr=%s)\n", flags, paddr);
418 if (a->q_user == NULL)
420 if (a->q_host == NULL)
423 if (bitset(RF_COPYPARSE, flags))
425 a->q_host = sm_rpool_strdup_x(e->e_rpool, a->q_host);
426 if (a->q_user != a->q_paddr)
427 a->q_user = sm_rpool_strdup_x(e->e_rpool, a->q_user);
430 if (a->q_paddr == NULL)
431 a->q_paddr = sm_rpool_strdup_x(e->e_rpool, a->q_user);
435 ** PRESCAN -- Prescan name and make it canonical
437 ** Scans a name and turns it into a set of tokens. This process
438 ** deletes blanks and comments (in parentheses) (if the token type
439 ** for left paren is SPC).
441 ** This routine knows about quoted strings and angle brackets.
443 ** There are certain subtleties to this routine. The one that
444 ** comes to mind now is that backslashes on the ends of names
445 ** are silently stripped off; this is intentional. The problem
446 ** is that some versions of sndmsg (like at LBL) set the kill
447 ** character to something other than @ when reading addresses;
448 ** so people type "csvax.eric\@berkeley" -- which screws up the
452 ** addr -- the name to chomp.
453 ** delim -- the delimiter for the address, normally
454 ** '\0' or ','; \0 is accepted in any case.
455 ** If '\t' then we are reading the .cf file.
456 ** pvpbuf -- place to put the saved text -- note that
457 ** the pointers are static.
458 ** pvpbsize -- size of pvpbuf.
459 ** delimptr -- if non-NULL, set to the location of the
460 ** terminating delimiter.
461 ** toktab -- if set, a token table to use for parsing.
462 ** If NULL, use the default table.
465 ** A pointer to a vector of tokens.
469 /* states and character types */
470 #define OPR 0 /* operator */
471 #define ATM 1 /* atom */
472 #define QST 2 /* in quoted string */
473 #define SPC 3 /* chewing up spaces */
474 #define ONE 4 /* pick up one character */
475 #define ILL 5 /* illegal character */
477 #define NSTATES 6 /* number of states */
478 #define TYPE 017 /* mask to select state type */
480 /* meta bits for table */
481 #define M 020 /* meta character; don't pass through */
482 #define B 040 /* cause a break */
483 #define MB M|B /* meta-break */
485 static short StateTab[NSTATES][NSTATES] =
487 /* oldst chtype> OPR ATM QST SPC ONE ILL */
488 /*OPR*/ { OPR|B, ATM|B, QST|B, SPC|MB, ONE|B, ILL|MB },
489 /*ATM*/ { OPR|B, ATM, QST|B, SPC|MB, ONE|B, ILL|MB },
490 /*QST*/ { QST, QST, OPR, QST, QST, QST },
491 /*SPC*/ { OPR, ATM, QST, SPC|M, ONE, ILL|MB },
492 /*ONE*/ { OPR, OPR, OPR, OPR, OPR, ILL|MB },
493 /*ILL*/ { OPR|B, ATM|B, QST|B, SPC|MB, ONE|B, ILL|M },
496 /* token type table -- it gets modified with $o characters */
497 static unsigned char TokTypeTab[256] =
499 /* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */
500 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,SPC,SPC,SPC,SPC,SPC,ATM,ATM,
501 /* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */
502 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
503 /* sp ! " # $ % & ' ( ) * + , - . / */
504 SPC,ATM,QST,ATM,ATM,ATM,ATM,ATM, SPC,SPC,ATM,ATM,ATM,ATM,ATM,ATM,
505 /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */
506 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
507 /* @ A B C D E F G H I J K L M N O */
508 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
509 /* P Q R S T U V W X Y Z [ \ ] ^ _ */
510 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
511 /* ` a b c d e f g h i j k l m n o */
512 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
513 /* p q r s t u v w x y z { | } ~ del */
514 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
516 /* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */
517 OPR,OPR,ONE,OPR,OPR,OPR,OPR,OPR, OPR,OPR,OPR,OPR,OPR,OPR,OPR,OPR,
518 /* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */
519 OPR,OPR,OPR,ONE,ONE,ONE,OPR,OPR, OPR,OPR,OPR,OPR,OPR,OPR,OPR,OPR,
520 /* sp ! " # $ % & ' ( ) * + , - . / */
521 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
522 /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */
523 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
524 /* @ A B C D E F G H I J K L M N O */
525 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
526 /* P Q R S T U V W X Y Z [ \ ] ^ _ */
527 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
528 /* ` a b c d e f g h i j k l m n o */
529 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
530 /* p q r s t u v w x y z { | } ~ del */
531 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
534 /* token type table for MIME parsing */
535 unsigned char MimeTokenTab[256] =
537 /* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */
538 ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL, ILL,SPC,SPC,SPC,SPC,SPC,ILL,ILL,
539 /* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */
540 ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL, ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL,
541 /* sp ! " # $ % & ' ( ) * + , - . / */
542 SPC,ATM,QST,ATM,ATM,ATM,ATM,ATM, SPC,SPC,ATM,ATM,OPR,ATM,ATM,OPR,
543 /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */
544 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,OPR,OPR,OPR,OPR,OPR,OPR,
545 /* @ A B C D E F G H I J K L M N O */
546 OPR,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
547 /* P Q R S T U V W X Y Z [ \ ] ^ _ */
548 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,OPR,OPR,OPR,ATM,ATM,
549 /* ` a b c d e f g h i j k l m n o */
550 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
551 /* p q r s t u v w x y z { | } ~ del */
552 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
554 /* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */
555 ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL, ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL,
556 /* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */
557 ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL, ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL,
558 /* sp ! " # $ % & ' ( ) * + , - . / */
559 ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL, ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL,
560 /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */
561 ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL, ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL,
562 /* @ A B C D E F G H I J K L M N O */
563 ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL, ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL,
564 /* P Q R S T U V W X Y Z [ \ ] ^ _ */
565 ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL, ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL,
566 /* ` a b c d e f g h i j k l m n o */
567 ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL, ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL,
568 /* p q r s t u v w x y z { | } ~ del */
569 ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL, ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL,
572 /* token type table: don't strip comments */
573 unsigned char TokTypeNoC[256] =
575 /* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */
576 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,SPC,SPC,SPC,SPC,SPC,ATM,ATM,
577 /* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */
578 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
579 /* sp ! " # $ % & ' ( ) * + , - . / */
580 SPC,ATM,QST,ATM,ATM,ATM,ATM,ATM, OPR,OPR,ATM,ATM,ATM,ATM,ATM,ATM,
581 /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */
582 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
583 /* @ A B C D E F G H I J K L M N O */
584 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
585 /* P Q R S T U V W X Y Z [ \ ] ^ _ */
586 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
587 /* ` a b c d e f g h i j k l m n o */
588 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
589 /* p q r s t u v w x y z { | } ~ del */
590 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
592 /* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */
593 OPR,OPR,ONE,OPR,OPR,OPR,OPR,OPR, OPR,OPR,OPR,OPR,OPR,OPR,OPR,OPR,
594 /* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */
595 OPR,OPR,OPR,ONE,ONE,ONE,OPR,OPR, OPR,OPR,OPR,OPR,OPR,OPR,OPR,OPR,
596 /* sp ! " # $ % & ' ( ) * + , - . / */
597 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
598 /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */
599 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
600 /* @ A B C D E F G H I J K L M N O */
601 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
602 /* P Q R S T U V W X Y Z [ \ ] ^ _ */
603 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
604 /* ` a b c d e f g h i j k l m n o */
605 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
606 /* p q r s t u v w x y z { | } ~ del */
607 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
611 #define NOCHAR (-1) /* signal nothing in lookahead token */
614 prescan(addr, delim, pvpbuf, pvpbsize, delimptr, toktab)
620 unsigned char *toktab;
633 char *saveto = CurEnv->e_to;
634 static char *av[MAXATOM + 1];
635 static bool firsttime = true;
640 /* initialize the token type table */
644 if (OperatorChars == NULL)
647 OperatorChars = macvalue('o', CurEnv);
648 if (OperatorChars == NULL)
649 OperatorChars = ".:@[]";
651 expand(OperatorChars, obuf, sizeof obuf - sizeof DELIMCHARS,
653 (void) sm_strlcat(obuf, DELIMCHARS, sizeof obuf);
654 for (p = obuf; *p != '\0'; p++)
656 if (TokTypeTab[*p & 0xff] == ATM)
657 TokTypeTab[*p & 0xff] = OPR;
658 if (TokTypeNoC[*p & 0xff] == ATM)
659 TokTypeNoC[*p & 0xff] = OPR;
665 /* make sure error messages don't have garbage on them */
670 route_syntax = false;
680 sm_dprintf("prescan: ");
691 /* store away any old lookahead character */
692 if (c != NOCHAR && !bslashmode)
694 /* see if there is room */
695 if (q >= &pvpbuf[pvpbsize - 5])
698 usrerr("553 5.1.1 Address too long");
699 if (strlen(addr) > MAXNAME)
700 addr[MAXNAME] = '\0';
702 if (delimptr != NULL)
704 CurEnv->e_to = saveto;
708 /* squirrel it away */
710 if ((char) c == (char) -1 && !tTd(82, 101))
712 #endif /* !ALLOW_255 */
716 /* read a new input character */
720 /* diagnose and patch up bad syntax */
723 usrerr("553 Unbalanced '\"'");
726 else if (cmntcnt > 0)
728 usrerr("553 Unbalanced '('");
731 else if (anglecnt > 0)
734 usrerr("553 Unbalanced '<'");
741 else if (c == delim && cmntcnt <= 0 && state != QST)
746 /* special case for better error management */
747 if (delim == ',' && !route_syntax)
749 usrerr("553 Unbalanced '<'");
756 sm_dprintf("c=%c, s=%d; ", c, state);
758 /* chew up special characters */
764 /* kludge \! for naive users */
770 else if (c != '!' || state == QST)
772 /* see if there is room */
773 if (q >= &pvpbuf[pvpbsize - 5])
784 else if (state == QST)
787 /* do nothing, just avoid next clauses */
789 else if (c == '(' && toktab['('] == SPC)
794 else if (c == ')' && toktab['('] == SPC)
798 usrerr("553 Unbalanced ')'");
804 else if (cmntcnt > 0)
813 while (isascii(*ptr) && isspace(*ptr))
822 usrerr("553 Unbalanced '>'");
827 route_syntax = false;
829 else if (delim == ' ' && isascii(c) && isspace(c))
835 /* see if this is end of input */
836 if (c == delim && anglecnt <= 0 && state != QST)
839 newstate = StateTab[state][toktab[c & 0xff]];
841 sm_dprintf("ns=%02o\n", newstate);
842 state = newstate & TYPE;
845 if (isascii(c) && isprint(c))
846 usrerr("553 Illegal character %c", c);
848 usrerr("553 Illegal character 0x%02x",
851 if (bitset(M, newstate))
853 if (bitset(B, newstate))
860 /* see if there is room */
861 if (q >= &pvpbuf[pvpbsize - 5])
870 if (avp >= &av[MAXATOM])
872 usrerr("553 5.1.0 prescan: too many tokens");
875 if (q - tok > MAXNAME)
877 usrerr("553 5.1.0 prescan: token too long");
882 } while (c != '\0' && (c != delim || anglecnt > 0));
885 if (delimptr != NULL)
889 sm_dprintf("prescan==>");
892 CurEnv->e_to = saveto;
896 sm_dprintf("prescan: null leading token\n");
902 ** REWRITE -- apply rewrite rules to token vector.
904 ** This routine is an ordered production system. Each rewrite
905 ** rule has a LHS (called the pattern) and a RHS (called the
906 ** rewrite); 'rwr' points the the current rewrite rule.
908 ** For each rewrite rule, 'avp' points the address vector we
909 ** are trying to match against, and 'pvp' points to the pattern.
910 ** If pvp points to a special match value (MATCHZANY, MATCHANY,
911 ** MATCHONE, MATCHCLASS, MATCHNCLASS) then the address in avp
912 ** matched is saved away in the match vector (pointed to by 'mvp').
914 ** When a match between avp & pvp does not match, we try to
915 ** back out. If we back up over MATCHONE, MATCHCLASS, or MATCHNCLASS
916 ** we must also back out the match in mvp. If we reach a
917 ** MATCHANY or MATCHZANY we just extend the match and start
920 ** When we finally match, we rewrite the address vector
921 ** and try over again.
924 ** pvp -- pointer to token vector.
925 ** ruleset -- the ruleset to use for rewriting.
926 ** reclevel -- recursion level (to catch loops).
927 ** e -- the current envelope.
928 ** maxatom -- maximum length of buffer (usually MAXATOM)
931 ** A status code. If EX_TEMPFAIL, higher level code should
940 char **match_first; /* first token matched */
941 char **match_last; /* last token matched */
942 char **match_pattern; /* pointer to pattern */
946 rewrite(pvp, ruleset, reclevel, e, maxatom)
950 register ENVELOPE *e;
953 register char *ap; /* address pointer */
954 register char *rp; /* rewrite pointer */
955 register char *rulename; /* ruleset name */
956 register char *prefix;
957 register char **avp; /* address vector pointer */
958 register char **rvp; /* rewrite vector pointer */
959 register struct match *mlp; /* cur ptr into mlist */
960 register struct rewrite *rwr; /* pointer to current rewrite rule */
961 int ruleno; /* current rule number */
962 int rstat = EX_OK; /* return status */
964 struct match mlist[MAXMATCH]; /* stores match on LHS */
965 char *npvp[MAXATOM + 1]; /* temporary space for rebuild */
969 if (ruleset < 0 || ruleset >= MAXRWSETS)
971 syserr("554 5.3.5 rewrite: illegal ruleset number %d", ruleset);
974 rulename = RuleSetNames[ruleset];
975 if (rulename == NULL)
977 (void) sm_snprintf(name, sizeof name, "%d", ruleset);
980 if (OpMode == MD_TEST)
983 prefix = "rewrite: ruleset ";
984 if (OpMode == MD_TEST)
986 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
987 "%s%-16.16s input:", prefix, rulename);
992 sm_dprintf("%s%-16.16s input:", prefix, rulename);
995 if (reclevel++ > MaxRuleRecursion)
997 syserr("rewrite: excessive recursion (max %d), ruleset %s",
998 MaxRuleRecursion, rulename);
1005 ** Run through the list of rewrite rules, applying
1011 for (rwr = RewriteRules[ruleset]; rwr != NULL; )
1015 /* if already canonical, quit now */
1016 if (pvp[0] != NULL && (pvp[0][0] & 0377) == CANONNET)
1022 sm_dprintf("-----trying rule (line %d):",
1025 sm_dprintf("-----trying rule:");
1026 printav(rwr->r_lhs);
1029 /* try to match on this rule */
1033 if (++loopcount > 100)
1035 syserr("554 5.3.5 Infinite loop in ruleset %s, rule %d",
1039 sm_dprintf("workspace: ");
1045 while ((ap = *avp) != NULL || *rvp != NULL)
1050 sm_dprintf("ADVANCE rp=");
1052 sm_dprintf(", ap=");
1058 /* end-of-pattern before end-of-address */
1061 if (ap == NULL && (*rp & 0377) != MATCHZANY &&
1062 (*rp & 0377) != MATCHZERO)
1064 /* end-of-input with patterns left */
1071 /* match any phrase in a class */
1072 mlp->match_pattern = rvp;
1073 mlp->match_first = avp;
1078 mlp->match_last = avp++;
1079 cataddr(mlp->match_first, mlp->match_last,
1080 buf, sizeof buf, '\0');
1081 if (!wordinclass(buf, rp[1]))
1085 sm_dprintf("EXTEND rp=");
1087 sm_dprintf(", ap=");
1094 sm_dprintf("CLMATCH\n");
1099 /* match any token not in a class */
1100 if (wordinclass(ap, rp[1]))
1107 /* match exactly one token */
1108 mlp->match_pattern = rvp;
1109 mlp->match_first = avp;
1110 mlp->match_last = avp++;
1115 /* match zero or more tokens */
1116 mlp->match_pattern = rvp;
1117 mlp->match_first = avp;
1118 mlp->match_last = avp - 1;
1123 /* match zero tokens */
1128 ** Match against run-time macro.
1129 ** This algorithm is broken for the
1130 ** general case (no recursive macros,
1131 ** improper tokenization) but should
1132 ** work for the usual cases.
1135 ap = macvalue(rp[1], e);
1136 mlp->match_first = avp;
1138 sm_dprintf("rewrite: LHS $&{%s} => \"%s\"\n",
1140 ap == NULL ? "(NULL)" : ap);
1147 sm_strncasecmp(ap, *avp,
1151 avp = mlp->match_first;
1154 ap += strlen(*avp++);
1161 /* must have exact match */
1162 if (sm_strcasecmp(rp, ap))
1168 /* successful match on this token */
1173 /* match failed -- back up */
1174 while (--mlp >= mlist)
1176 rvp = mlp->match_pattern;
1178 avp = mlp->match_last + 1;
1183 sm_dprintf("BACKUP rp=");
1185 sm_dprintf(", ap=");
1192 /* run off the end -- back up again */
1195 if ((*rp & 0377) == MATCHANY ||
1196 (*rp & 0377) == MATCHZANY)
1198 /* extend binding and continue */
1199 mlp->match_last = avp++;
1204 if ((*rp & 0377) == MATCHCLASS)
1206 /* extend binding and try again */
1207 mlp->match_last = avp;
1214 /* total failure to match */
1220 ** See if we successfully matched
1223 if (mlp < mlist || *rvp != NULL)
1226 sm_dprintf("----- rule fails\n");
1236 sm_dprintf("-----rule matches:");
1243 if ((*rp & 0377) == CANONUSER)
1250 else if ((*rp & 0377) == CANONHOST)
1258 for (avp = npvp; *rvp != NULL; rvp++)
1260 register struct match *m;
1264 if ((*rp & 0377) == MATCHREPL)
1266 /* substitute from LHS */
1267 m = &mlist[rp[1] - '1'];
1268 if (m < mlist || m >= mlp)
1270 syserr("554 5.3.5 rewrite: ruleset %s: replacement $%c out of bounds",
1276 sm_dprintf("$%c:", rp[1]);
1277 pp = m->match_first;
1278 while (pp <= m->match_last)
1280 sm_dprintf(" %p=\"", *pp);
1282 sm_dprintf("%s\"", *pp++);
1286 pp = m->match_first;
1287 while (pp <= m->match_last)
1289 if (avp >= &npvp[maxatom])
1291 syserr("554 5.3.0 rewrite: expansion too long");
1295 "rewrite: expansion too long, ruleset=%s, ruleno=%d",
1305 /* some sort of replacement */
1306 if (avp >= &npvp[maxatom])
1309 syserr("554 5.3.0 rewrite: expansion too long");
1311 sm_syslog(LOG_ERR, e->e_id,
1312 "rewrite: expansion too long, ruleset=%s, ruleno=%d",
1316 if ((*rp & 0377) != MACRODEXPAND)
1318 /* vanilla replacement */
1323 /* $&{x} replacement */
1324 char *mval = macvalue(rp[1], e);
1327 static size_t pvpb1_size = 0;
1328 static char **pvpb1 = NULL;
1329 char pvpbuf[PSBUFSIZE];
1332 sm_dprintf("rewrite: RHS $&{%s} => \"%s\"\n",
1334 mval == NULL ? "(NULL)" : mval);
1335 if (mval == NULL || *mval == '\0')
1338 /* save the remainder of the input */
1339 for (xpvp = pvp; *xpvp != NULL; xpvp++)
1340 trsize += sizeof *xpvp;
1341 if ((size_t) trsize > pvpb1_size)
1346 sm_pmalloc_x(trsize);
1347 pvpb1_size = trsize;
1350 memmove((char *) pvpb1,
1354 /* scan the new replacement */
1355 xpvp = prescan(mval, '\0', pvpbuf,
1356 sizeof pvpbuf, NULL,
1360 /* prescan pre-printed error */
1364 /* insert it into the output stream */
1365 while (*xpvp != NULL)
1368 sm_dprintf(" ... %s\n",
1370 *avp++ = sm_rpool_strdup_x(
1372 if (avp >= &npvp[maxatom])
1377 sm_dprintf(" ... DONE\n");
1379 /* restore the old trailing input */
1380 memmove((char *) pvp,
1389 ** Check for any hostname/keyword lookups.
1392 for (rvp = npvp; *rvp != NULL; rvp++)
1404 char cbuf[MAXNAME + 1];
1405 char *pvpb1[MAXATOM + 1];
1407 char pvpbuf[PSBUFSIZE];
1410 if ((**rvp & 0377) != HOSTBEGIN &&
1411 (**rvp & 0377) != LOOKUPBEGIN)
1415 ** Got a hostname/keyword lookup.
1417 ** This could be optimized fairly easily.
1421 if ((**rvp & 0377) == HOSTBEGIN)
1428 endtoken = LOOKUPEND;
1431 map = stab(mapname, ST_MAP, ST_FIND);
1433 syserr("554 5.3.0 rewrite: map %s not found", mapname);
1435 /* extract the match part */
1441 while (*rvp != NULL && (**rvp & 0377) != endtoken)
1443 int nodetype = **rvp & 0377;
1445 if (nodetype != CANONHOST && nodetype != CANONUSER)
1455 cataddr(xpvp, NULL, replac,
1456 &pvpbuf[sizeof pvpbuf] - replac,
1458 *++arg_rvp = replac;
1459 replac += strlen(replac) + 1;
1477 cataddr(xpvp, NULL, replac,
1478 &pvpbuf[sizeof pvpbuf] - replac,
1480 *++arg_rvp = replac;
1484 /* save the remainder of the input string */
1485 trsize = (int) (avp - rvp + 1) * sizeof *rvp;
1486 memmove((char *) pvpb1, (char *) rvp, trsize);
1489 cataddr(key_rvp, NULL, cbuf, sizeof cbuf,
1490 map == NULL ? '\0' : map->s_map.map_spacesub);
1492 replac = map_lookup(map, cbuf, argvect, &rstat, e);
1494 /* if no replacement, use default */
1495 if (replac == NULL && default_rvp != NULL)
1497 /* create the default */
1498 cataddr(default_rvp, NULL, cbuf, sizeof cbuf, '\0');
1506 else if (*replac == '\0')
1508 /* null replacement */
1514 /* scan the new replacement */
1515 xpvp = prescan(replac, '\0', pvpbuf,
1516 sizeof pvpbuf, NULL, NULL);
1519 /* prescan already printed error */
1524 /* append it to the token list */
1525 for (avp = hbrvp; *xpvp != NULL; xpvp++)
1527 *avp++ = sm_rpool_strdup_x(e->e_rpool, *xpvp);
1528 if (avp >= &npvp[maxatom])
1532 /* restore the old trailing information */
1534 for (xpvp = pvpb1; (*avp++ = *xpvp++) != NULL; )
1535 if (avp >= &npvp[maxatom])
1540 ** Check for subroutine calls.
1543 status = callsubr(npvp, reclevel, e);
1544 if (rstat == EX_OK || status == EX_TEMPFAIL)
1547 /* copy vector back into original space. */
1548 for (avp = npvp; *avp++ != NULL;)
1550 memmove((char *) pvp, (char *) npvp,
1551 (int) (avp - npvp) * sizeof *avp);
1555 sm_dprintf("rewritten as:");
1560 if (OpMode == MD_TEST)
1562 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
1563 "%s%-16.16s returns:", prefix, rulename);
1566 else if (tTd(21, 1))
1568 sm_dprintf("%s%-16.16s returns:", prefix, rulename);
1574 ** CALLSUBR -- call subroutines in rewrite vector
1577 ** pvp -- pointer to token vector.
1578 ** reclevel -- the current recursion level.
1579 ** e -- the current envelope.
1582 ** The status from the subroutine call.
1589 callsubr(pvp, reclevel, e)
1601 int subrnumber[MAX_SUBR];
1602 int subrindex[MAX_SUBR];
1607 ** Look for subroutine calls in pvp, collect them into subr*[]
1608 ** We will perform the calls in the next loop, because we will
1609 ** call the "last" subroutine first to avoid recursive calls
1610 ** and too much copying.
1613 for (avp = pvp, j = 0; *avp != NULL; avp++, j++)
1615 if ((**avp & 0377) == CALLSUBR && avp[1] != NULL)
1617 stripquotes(avp[1]);
1618 subr = strtorwset(avp[1], NULL, ST_FIND);
1621 syserr("554 5.3.5 Unknown ruleset %s", avp[1]);
1626 ** XXX instead of doing this we could optimize
1627 ** the rules after reading them: just remove
1628 ** calls to empty rulesets
1631 /* subroutine is an empty ruleset? don't call it */
1632 if (RewriteRules[subr] == NULL)
1635 sm_dprintf("-----skip subr %s (%d)\n",
1637 for (i = 2; avp[i] != NULL; i++)
1638 avp[i - 2] = avp[i];
1642 if (++nsubr >= MAX_SUBR)
1644 syserr("554 5.3.0 Too many subroutine calls (%d max)",
1648 subrnumber[nsubr] = subr;
1649 subrindex[nsubr] = j;
1654 ** Perform the actual subroutines calls, "last" one first, i.e.,
1655 ** go from the right to the left through all calls,
1656 ** do the rewriting in place.
1659 for (; nsubr > 0; nsubr--)
1661 subr = subrnumber[nsubr];
1662 avp = pvp + subrindex[nsubr];
1664 /* remove the subroutine call and name */
1665 for (i = 2; avp[i] != NULL; i++)
1666 avp[i - 2] = avp[i];
1670 ** Now we need to call the ruleset specified for
1671 ** the subroutine. we can do this inplace since
1672 ** we call the "last" subroutine first.
1675 status = rewrite(avp, subr, reclevel, e,
1676 MAXATOM - subrindex[nsubr]);
1677 if (status != EX_OK && status != EX_TEMPFAIL)
1679 if (rstat == EX_OK || status == EX_TEMPFAIL)
1685 ** MAP_LOOKUP -- do lookup in map
1688 ** smap -- the map to use for the lookup.
1689 ** key -- the key to look up.
1690 ** argvect -- arguments to pass to the map lookup.
1691 ** pstat -- a pointer to an integer in which to store the
1692 ** status from the lookup.
1693 ** e -- the current envelope.
1696 ** The result of the lookup.
1697 ** NULL -- if there was no data for the given key.
1701 map_lookup(smap, key, argvect, pstat, e)
1708 auto int status = EX_OK;
1718 if (e->e_sendmode == SM_DEFER &&
1719 bitset(MF_DEFER, map->map_mflags))
1721 /* don't do any map lookups */
1723 sm_dprintf("map_lookup(%s, %s) => DEFERRED\n",
1725 *pstat = EX_TEMPFAIL;
1729 if (!bitset(MF_KEEPQUOTES, map->map_mflags))
1734 sm_dprintf("map_lookup(%s, %s", smap->s_name, key);
1739 for (i = 0; argvect[i] != NULL; i++)
1740 sm_dprintf(", %%%d=%s", i, argvect[i]);
1742 sm_dprintf(") => ");
1744 replac = (*map->map_class->map_lookup)(map, key, argvect, &status);
1746 sm_dprintf("%s (%d)\n",
1747 replac != NULL ? replac : "NOT FOUND",
1750 /* should recover if status == EX_TEMPFAIL */
1751 if (status == EX_TEMPFAIL && !bitset(MF_NODEFER, map->map_mflags))
1753 *pstat = EX_TEMPFAIL;
1755 sm_dprintf("map_lookup(%s, %s) tempfail: errno=%d\n",
1756 smap->s_name, key, errno);
1757 if (e->e_message == NULL)
1761 (void) sm_snprintf(mbuf, sizeof mbuf,
1762 "%.80s map: lookup (%s): deferred",
1764 shortenstring(key, MAXSHORTSTR));
1765 e->e_message = sm_rpool_strdup_x(e->e_rpool, mbuf);
1768 if (status == EX_TEMPFAIL && map->map_tapp != NULL)
1770 size_t i = strlen(key) + strlen(map->map_tapp) + 1;
1771 static char *rwbuf = NULL;
1772 static size_t rwbuflen = 0;
1779 rwbuf = (char *) sm_pmalloc_x(rwbuflen);
1781 (void) sm_strlcpyn(rwbuf, rwbuflen, 2, key, map->map_tapp);
1783 sm_dprintf("map_lookup tempfail: returning \"%s\"\n",
1790 ** INITERRMAILERS -- initialize error and discard mailers
1799 ** initializes error and discard mailers.
1802 static MAILER discardmailer;
1803 static MAILER errormailer;
1804 static char *discardargv[] = { "DISCARD", NULL };
1805 static char *errorargv[] = { "ERROR", NULL };
1810 if (discardmailer.m_name == NULL)
1812 /* initialize the discard mailer */
1813 discardmailer.m_name = "*discard*";
1814 discardmailer.m_mailer = "DISCARD";
1815 discardmailer.m_argv = discardargv;
1817 if (errormailer.m_name == NULL)
1819 /* initialize the bogus mailer */
1820 errormailer.m_name = "*error*";
1821 errormailer.m_mailer = "ERROR";
1822 errormailer.m_argv = errorargv;
1826 ** BUILDADDR -- build address from token vector.
1829 ** tv -- token vector.
1830 ** a -- pointer to address descriptor to fill.
1831 ** If NULL, one will be allocated.
1832 ** flags -- info regarding whether this is a sender or
1834 ** e -- the current envelope.
1837 ** NULL if there was an error.
1844 static struct errcodes
1846 char *ec_name; /* name of error code */
1847 int ec_code; /* numeric code */
1850 { "usage", EX_USAGE },
1851 { "nouser", EX_NOUSER },
1852 { "nohost", EX_NOHOST },
1853 { "unavailable", EX_UNAVAILABLE },
1854 { "software", EX_SOFTWARE },
1855 { "tempfail", EX_TEMPFAIL },
1856 { "protocol", EX_PROTOCOL },
1857 { "config", EX_CONFIG },
1858 { NULL, EX_UNAVAILABLE }
1862 buildaddr(tv, a, flags, e)
1864 register ADDRESS *a;
1866 register ENVELOPE *e;
1868 bool tempfail = false;
1870 register struct mailer *m;
1874 char hbuf[MAXNAME + 1];
1875 static char ubuf[MAXNAME + 2];
1879 sm_dprintf("buildaddr, flags=%x, tv=", flags);
1884 a = (ADDRESS *) sm_rpool_malloc_x(e->e_rpool, sizeof *a);
1885 memset((char *) a, '\0', sizeof *a);
1888 /* set up default error return flags */
1889 a->q_flags |= DefaultNotify;
1891 /* figure out what net/mailer to use */
1892 if (*tv == NULL || (**tv & 0377) != CANONNET)
1894 syserr("554 5.3.5 buildaddr: no mailer in parsed address");
1896 #if _FFR_ALLOW_S0_ERROR_4XX
1898 ** ExitStat may have been set by an earlier map open
1899 ** failure (to a permanent error (EX_OSERR) in syserr())
1900 ** so we also need to check if this particular $#error
1901 ** return wanted a 4XX failure.
1903 ** XXX the real fix is probably to set ExitStat correctly,
1904 ** i.e., to EX_TEMPFAIL if the map open is just a temporary
1907 ** tempfail is tested here even if _FFR_ALLOW_S0_ERROR_4XX
1908 ** is not set; that's ok because it is initialized to false.
1910 #endif /* _FFR_ALLOW_S0_ERROR_4XX */
1912 if (ExitStat == EX_TEMPFAIL || tempfail)
1913 a->q_state = QS_QUEUEUP;
1916 a->q_state = QS_BADADDR;
1917 a->q_mailer = &errormailer;
1923 /* extract host and user portions */
1924 if (*++tv != NULL && (**tv & 0377) == CANONHOST)
1928 while (*tv != NULL && (**tv & 0377) != CANONUSER)
1932 syserr("554 5.3.5 buildaddr: no user");
1937 else if (hostp != NULL)
1938 cataddr(hostp, tv - 1, hbuf, sizeof hbuf, '\0');
1939 cataddr(++tv, NULL, ubuf, sizeof ubuf, ' ');
1941 /* save away the host name */
1942 if (sm_strcasecmp(mname, "error") == 0)
1944 /* Set up triplet for use by -bv */
1945 a->q_mailer = &errormailer;
1946 a->q_user = sm_rpool_strdup_x(e->e_rpool, ubuf);
1947 /* XXX wrong place? */
1951 register struct errcodes *ep;
1953 a->q_host = sm_rpool_strdup_x(e->e_rpool, hbuf);
1954 if (strchr(hbuf, '.') != NULL)
1956 a->q_status = sm_rpool_strdup_x(e->e_rpool,
1958 setstat(dsntoexitstat(hbuf));
1960 else if (isascii(hbuf[0]) && isdigit(hbuf[0]))
1962 setstat(atoi(hbuf));
1966 for (ep = ErrorCodes; ep->ec_name != NULL; ep++)
1967 if (sm_strcasecmp(ep->ec_name, hbuf) == 0)
1969 setstat(ep->ec_code);
1975 setstat(EX_UNAVAILABLE);
1978 if (ISSMTPCODE(ubuf) && ubuf[3] == ' ')
1983 if ((off = isenhsc(ubuf + 4, ' ')) > 0)
1985 ubuf[off + 4] = '\0';
1993 (void) sm_strlcpyn(fmt, sizeof fmt, 2, ubuf, " %s");
1995 usrerr(fmt, ubuf + off);
1996 else if (isenhsc(hbuf, '\0') > 0)
1997 usrerrenh(hbuf, fmt, ubuf + off);
1999 usrerr(fmt, ubuf + off);
2000 /* XXX ubuf[off - 1] = ' '; */
2001 #if _FFR_ALLOW_S0_ERROR_4XX
2004 #endif /* _FFR_ALLOW_S0_ERROR_4XX */
2008 usrerr("553 5.3.0 %s", ubuf);
2013 for (mp = Mailer; (m = *mp++) != NULL; )
2015 if (sm_strcasecmp(m->m_name, mname) == 0)
2020 syserr("554 5.3.5 buildaddr: unknown mailer %s", mname);
2025 /* figure out what host (if any) */
2028 if (!bitnset(M_LOCALMAILER, m->m_flags))
2030 syserr("554 5.3.5 buildaddr: no host");
2036 a->q_host = sm_rpool_strdup_x(e->e_rpool, hbuf);
2038 /* figure out the user */
2040 if (bitnset(M_CHECKUDB, m->m_flags) && *p == '@')
2044 a->q_flags |= QNOTREMOTE;
2047 /* do special mapping for local mailer */
2050 if (*p == '|' && bitnset(M_CHECKPROG, m->m_flags))
2051 a->q_mailer = m = ProgMailer;
2052 else if (*p == '/' && bitnset(M_CHECKFILE, m->m_flags))
2053 a->q_mailer = m = FileMailer;
2054 else if (*p == ':' && bitnset(M_CHECKINCLUDE, m->m_flags))
2056 /* may be :include: */
2058 if (sm_strncasecmp(ubuf, ":include:", 9) == 0)
2060 /* if :include:, don't need further rewriting */
2061 a->q_mailer = m = InclMailer;
2062 a->q_user = sm_rpool_strdup_x(e->e_rpool, &ubuf[9]);
2067 /* rewrite according recipient mailer rewriting rules */
2068 macdefine(&e->e_macro, A_PERM, 'h', a->q_host);
2070 if (ConfigLevel >= 10 ||
2071 !bitset(RF_SENDERADDR|RF_HEADERADDR, flags))
2073 /* sender addresses done later */
2074 (void) REWRITE(tv, 2, e);
2075 if (m->m_re_rwset > 0)
2076 (void) REWRITE(tv, m->m_re_rwset, e);
2078 (void) REWRITE(tv, 4, e);
2080 /* save the result for the command line/RCPT argument */
2081 cataddr(tv, NULL, ubuf, sizeof ubuf, '\0');
2082 a->q_user = sm_rpool_strdup_x(e->e_rpool, ubuf);
2085 ** Do mapping to lower case as requested by mailer
2088 if (a->q_host != NULL && !bitnset(M_HST_UPPER, m->m_flags))
2089 makelower(a->q_host);
2090 if (!bitnset(M_USR_UPPER, m->m_flags))
2091 makelower(a->q_user);
2095 sm_dprintf("buildaddr => ");
2096 printaddr(a, false);
2102 ** CATADDR -- concatenate pieces of addresses (putting in <LWSP> subs)
2105 ** pvp -- parameter vector to rebuild.
2106 ** evp -- last parameter to include. Can be NULL to
2108 ** buf -- buffer to build the string into.
2109 ** sz -- size of buf.
2110 ** spacesub -- the space separator character; if '\0',
2121 cataddr(pvp, evp, buf, sz, spacesub)
2128 bool oatomtok = false;
2129 bool natomtok = false;
2136 if (spacesub == '\0')
2137 spacesub = SpaceSub;
2146 while (*pvp != NULL && sz > 0)
2148 natomtok = (TokTypeTab[**pvp & 0xff] == ATM);
2149 if (oatomtok && natomtok)
2155 if ((i = sm_strlcpy(p, *pvp, sz)) >= sz)
2157 oatomtok = natomtok;
2163 #if _FFR_CATCH_LONG_STRINGS
2164 /* Don't silently truncate long strings */
2166 syserr("cataddr: string too long");
2167 #endif /* _FFR_CATCH_LONG_STRINGS */
2171 ** SAMEADDR -- Determine if two addresses are the same
2173 ** This is not just a straight comparison -- if the mailer doesn't
2174 ** care about the host we just ignore it, etc.
2177 ** a, b -- pointers to the internal forms to compare.
2180 ** true -- they represent the same mailbox.
2181 ** false -- they don't.
2189 register ADDRESS *a;
2190 register ADDRESS *b;
2192 register ADDRESS *ca, *cb;
2194 /* if they don't have the same mailer, forget it */
2195 if (a->q_mailer != b->q_mailer)
2198 /* if the user isn't the same, we can drop out */
2199 if (strcmp(a->q_user, b->q_user) != 0)
2202 /* if we have good uids for both but they differ, these are different */
2203 if (a->q_mailer == ProgMailer)
2207 if (ca != NULL && cb != NULL &&
2208 bitset(QGOODUID, ca->q_flags & cb->q_flags) &&
2209 ca->q_uid != cb->q_uid)
2213 /* otherwise compare hosts (but be careful for NULL ptrs) */
2214 if (a->q_host == b->q_host)
2216 /* probably both null pointers */
2219 if (a->q_host == NULL || b->q_host == NULL)
2221 /* only one is a null pointer */
2224 if (strcmp(a->q_host, b->q_host) != 0)
2230 ** PRINTADDR -- print address (for debugging)
2233 ** a -- the address to print
2234 ** follow -- follow the q_next chain.
2246 unsigned long qf_bit;
2249 static struct qflags AddressFlags[] =
2251 { "QGOODUID", QGOODUID },
2252 { "QPRIMARY", QPRIMARY },
2253 { "QNOTREMOTE", QNOTREMOTE },
2254 { "QSELFREF", QSELFREF },
2255 { "QBOGUSSHELL", QBOGUSSHELL },
2256 { "QUNSAFEADDR", QUNSAFEADDR },
2257 { "QPINGONSUCCESS", QPINGONSUCCESS },
2258 { "QPINGONFAILURE", QPINGONFAILURE },
2259 { "QPINGONDELAY", QPINGONDELAY },
2260 { "QHASNOTIFY", QHASNOTIFY },
2261 { "QRELAYED", QRELAYED },
2262 { "QEXPANDED", QEXPANDED },
2263 { "QDELIVERED", QDELIVERED },
2264 { "QDELAYED", QDELAYED },
2265 { "QTHISPASS", QTHISPASS },
2266 { "QRCPTOK", QRCPTOK },
2271 printaddr(a, follow)
2272 register ADDRESS *a;
2276 MAILER pseudomailer;
2277 register struct qflags *qfp;
2282 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, "[NULL]\n");
2288 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, "%p=", a);
2289 (void) sm_io_flush(smioout, SM_TIME_DEFAULT);
2291 /* find the mailer -- carefully */
2300 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
2301 "%s:\n\tmailer %d (%s), host `%s'\n",
2302 a->q_paddr == NULL ? "<null>" : a->q_paddr,
2303 m->m_mno, m->m_name,
2304 a->q_host == NULL ? "<null>" : a->q_host);
2305 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
2306 "\tuser `%s', ruser `%s'\n",
2308 a->q_ruser == NULL ? "<null>" : a->q_ruser);
2309 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, "\tstate=");
2313 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, "OK");
2317 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
2322 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
2327 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
2332 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, "RETRY");
2336 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, "SENT");
2340 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
2345 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
2350 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
2355 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
2360 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
2365 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
2370 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
2375 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
2380 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
2385 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
2389 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
2390 ", next=%p, alias %p, uid %d, gid %d\n",
2391 a->q_next, a->q_alias,
2392 (int) a->q_uid, (int) a->q_gid);
2393 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, "\tflags=%lx<",
2396 for (qfp = AddressFlags; qfp->qf_name != NULL; qfp++)
2398 if (!bitset(qfp->qf_bit, a->q_flags))
2401 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
2404 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, "%s",
2407 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, ">\n");
2408 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
2409 "\towner=%s, home=\"%s\", fullname=\"%s\"\n",
2410 a->q_owner == NULL ? "(none)" : a->q_owner,
2411 a->q_home == NULL ? "(none)" : a->q_home,
2412 a->q_fullname == NULL ? "(none)" : a->q_fullname);
2413 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
2414 "\torcpt=\"%s\", statmta=%s, status=%s\n",
2415 a->q_orcpt == NULL ? "(none)" : a->q_orcpt,
2416 a->q_statmta == NULL ? "(none)" : a->q_statmta,
2417 a->q_status == NULL ? "(none)" : a->q_status);
2418 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
2419 "\tfinalrcpt=\"%s\"\n",
2420 a->q_finalrcpt == NULL ? "(none)" : a->q_finalrcpt);
2421 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
2422 "\trstatus=\"%s\"\n",
2423 a->q_rstatus == NULL ? "(none)" : a->q_rstatus);
2424 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
2426 a->q_statdate == 0 ? "(none)" : ctime(&a->q_statdate));
2434 ** EMPTYADDR -- return true if this address is empty (``<>'')
2437 ** a -- pointer to the address
2440 ** true -- if this address is "empty" (i.e., no one should
2441 ** ever generate replies to it.
2442 ** false -- if it is a "regular" (read: replyable) address.
2447 register ADDRESS *a;
2449 return a->q_paddr == NULL || strcmp(a->q_paddr, "<>") == 0 ||
2450 a->q_user == NULL || strcmp(a->q_user, "<>") == 0;
2453 ** REMOTENAME -- return the name relative to the current mailer
2456 ** name -- the name to translate.
2457 ** m -- the mailer that we want to do rewriting relative
2459 ** flags -- fine tune operations.
2460 ** pstat -- pointer to status word.
2461 ** e -- the current envelope.
2464 ** the text string representing this address relative to
2465 ** the receiving mailer.
2471 ** The text string returned is tucked away locally;
2472 ** copy it if you intend to save it.
2476 remotename(name, m, flags, pstat, e)
2481 register ENVELOPE *e;
2483 register char **pvp;
2484 char *SM_NONVOLATILE fancy;
2487 static char buf[MAXNAME + 1];
2488 char lbuf[MAXNAME + 1];
2489 char pvpbuf[PSBUFSIZE];
2493 sm_dprintf("remotename(%s)\n", name);
2495 /* don't do anything if we are tagging it as special */
2496 if (bitset(RF_SENDERADDR, flags))
2498 rwset = bitset(RF_HEADERADDR, flags) ? m->m_sh_rwset
2504 rwset = bitset(RF_HEADERADDR, flags) ? m->m_rh_rwset
2512 addrtype[0] = bitset(RF_HEADERADDR, flags) ? 'h' : 'e';
2513 macdefine(&e->e_macro, A_TEMP, macid("{addr_type}"), addrtype);
2516 ** Do a heuristic crack of this name to extract any comment info.
2517 ** This will leave the name as a comment and a $g macro.
2520 if (bitset(RF_CANONICAL, flags) || bitnset(M_NOCOMMENT, m->m_flags))
2523 fancy = crackaddr(name, e);
2526 ** Turn the name into canonical form.
2527 ** Normally this will be RFC 822 style, i.e., "user@domain".
2528 ** If this only resolves to "user", and the "C" flag is
2529 ** specified in the sending mailer, then the sender's
2530 ** domain will be appended.
2533 pvp = prescan(name, '\0', pvpbuf, sizeof pvpbuf, NULL, NULL);
2536 if (REWRITE(pvp, 3, e) == EX_TEMPFAIL)
2537 *pstat = EX_TEMPFAIL;
2538 if (bitset(RF_ADDDOMAIN, flags) && e->e_fromdomain != NULL)
2540 /* append from domain to this address */
2541 register char **pxp = pvp;
2542 int l = MAXATOM; /* size of buffer for pvp */
2544 /* see if there is an "@domain" in the current name */
2545 while (*pxp != NULL && strcmp(*pxp, "@") != 0)
2552 /* no.... append the "@domain" from the sender */
2553 register char **qxq = e->e_fromdomain;
2555 while ((*pxp++ = *qxq++) != NULL)
2560 usrerr("553 5.1.0 remotename: too many tokens");
2561 *pstat = EX_UNAVAILABLE;
2565 if (REWRITE(pvp, 3, e) == EX_TEMPFAIL)
2566 *pstat = EX_TEMPFAIL;
2571 ** Do more specific rewriting.
2572 ** Rewrite using ruleset 1 or 2 depending on whether this is
2573 ** a sender address or not.
2574 ** Then run it through any receiving-mailer-specific rulesets.
2577 if (bitset(RF_SENDERADDR, flags))
2579 if (REWRITE(pvp, 1, e) == EX_TEMPFAIL)
2580 *pstat = EX_TEMPFAIL;
2584 if (REWRITE(pvp, 2, e) == EX_TEMPFAIL)
2585 *pstat = EX_TEMPFAIL;
2589 if (REWRITE(pvp, rwset, e) == EX_TEMPFAIL)
2590 *pstat = EX_TEMPFAIL;
2594 ** Do any final sanitation the address may require.
2595 ** This will normally be used to turn internal forms
2596 ** (e.g., user@host.LOCAL) into external form. This
2597 ** may be used as a default to the above rules.
2600 if (REWRITE(pvp, 4, e) == EX_TEMPFAIL)
2601 *pstat = EX_TEMPFAIL;
2604 ** Now restore the comment information we had at the beginning.
2607 cataddr(pvp, NULL, lbuf, sizeof lbuf, '\0');
2608 oldg = macget(&e->e_macro, 'g');
2609 macset(&e->e_macro, 'g', lbuf);
2612 /* need to make sure route-addrs have <angle brackets> */
2613 if (bitset(RF_CANONICAL, flags) && lbuf[0] == '@')
2614 expand("<\201g>", buf, sizeof buf, e);
2616 expand(fancy, buf, sizeof buf, e);
2618 macset(&e->e_macro, 'g', oldg);
2622 sm_dprintf("remotename => `%s'\n", buf);
2626 ** MAPLOCALUSER -- run local username through ruleset 5 for final redirection
2629 ** a -- the address to map (but just the user name part).
2630 ** sendq -- the sendq in which to install any replacement
2632 ** aliaslevel -- the alias nesting depth.
2633 ** e -- the envelope.
2639 #define Q_COPYFLAGS (QPRIMARY|QBOGUSSHELL|QUNSAFEADDR|\
2640 Q_PINGFLAGS|QHASNOTIFY|\
2641 QRELAYED|QEXPANDED|QDELIVERED|QDELAYED|\
2642 QBYTRACE|QBYNDELAY|QBYNRELAY)
2645 maplocaluser(a, sendq, aliaslevel, e)
2646 register ADDRESS *a;
2651 register char **pvp;
2652 register ADDRESS *SM_NONVOLATILE a1 = NULL;
2653 auto char *delimptr;
2654 char pvpbuf[PSBUFSIZE];
2658 sm_dprintf("maplocaluser: ");
2659 printaddr(a, false);
2661 pvp = prescan(a->q_user, '\0', pvpbuf, sizeof pvpbuf, &delimptr, NULL);
2665 sm_dprintf("maplocaluser: cannot prescan %s\n",
2670 macdefine(&e->e_macro, A_PERM, 'h', a->q_host);
2671 macdefine(&e->e_macro, A_PERM, 'u', a->q_user);
2672 macdefine(&e->e_macro, A_PERM, 'z', a->q_home);
2674 macdefine(&e->e_macro, A_PERM, macid("{addr_type}"), "e r");
2675 if (REWRITE(pvp, 5, e) == EX_TEMPFAIL)
2678 sm_dprintf("maplocaluser: rewrite tempfail\n");
2679 a->q_state = QS_QUEUEUP;
2680 a->q_status = "4.4.3";
2683 if (pvp[0] == NULL || (pvp[0][0] & 0377) != CANONNET)
2686 sm_dprintf("maplocaluser: doesn't resolve\n");
2691 a1 = buildaddr(pvp, NULL, 0, e);
2692 SM_EXCEPT(exc, "E:mta.quickabort")
2695 ** mark address as bad, S5 returned an error
2696 ** and we gave that back to the SMTP client.
2699 a->q_state = QS_DONTSEND;
2700 sm_exc_raisenew_x(&EtypeQuickAbort, 2);
2703 /* if non-null, mailer destination specified -- has it changed? */
2704 if (a1 == NULL || sameaddr(a, a1))
2707 sm_dprintf("maplocaluser: address unchanged\n");
2711 /* make new address take on flags and print attributes of old */
2712 a1->q_flags &= ~Q_COPYFLAGS;
2713 a1->q_flags |= a->q_flags & Q_COPYFLAGS;
2714 a1->q_paddr = sm_rpool_strdup_x(e->e_rpool, a->q_paddr);
2715 a1->q_finalrcpt = a->q_finalrcpt;
2716 a1->q_orcpt = a->q_orcpt;
2718 /* mark old address as dead; insert new address */
2719 a->q_state = QS_REPLACED;
2722 sm_dprintf("maplocaluser: QS_REPLACED ");
2723 printaddr(a, false);
2726 allocaddr(a1, RF_COPYALL, sm_rpool_strdup_x(e->e_rpool, a->q_paddr), e);
2727 (void) recipient(a1, sendq, aliaslevel, e);
2730 ** DEQUOTE_INIT -- initialize dequote map
2733 ** map -- the internal map structure.
2734 ** args -- arguments.
2741 dequote_init(map, args)
2745 register char *p = args;
2747 /* there is no check whether there is really an argument */
2748 map->map_mflags |= MF_KEEPQUOTES;
2751 while (isascii(*p) && isspace(*p))
2762 map->map_mflags |= MF_DEFER;
2767 map->map_spacesub = *++p;
2770 while (*p != '\0' && !(isascii(*p) && isspace(*p)))
2775 if (map->map_app != NULL)
2776 map->map_app = newstr(map->map_app);
2781 ** DEQUOTE_MAP -- unquote an address
2784 ** map -- the internal map structure (ignored).
2785 ** name -- the name to dequote.
2786 ** av -- arguments (ignored).
2787 ** statp -- pointer to status out-parameter.
2790 ** NULL -- if there were no quotes, or if the resulting
2791 ** unquoted buffer would not be acceptable to prescan.
2792 ** else -- The dequoted buffer.
2797 dequote_map(map, name, av, statp)
2810 bool quotemode = false;
2811 bool bslashmode = false;
2812 char spacesub = map->map_spacesub;
2814 for (p = q = name; (c = *p++) != '\0'; )
2823 if (c == ' ' && spacesub != '\0')
2856 quotemode = !quotemode;
2865 if (anglecnt-- <= 0)
2872 if (anglecnt != 0 || cmntcnt != 0 || bslashmode ||
2873 quotemode || quotecnt <= 0 || spacecnt != 0)
2876 return map_rewrite(map, name, strlen(name), NULL);
2879 ** RSCHECK -- check string(s) for validity using rewriting sets
2882 ** rwset -- the rewriting set to use.
2883 ** p1 -- the first string to check.
2884 ** p2 -- the second string to check -- may be null.
2885 ** e -- the current envelope.
2886 ** flags -- control some behavior, see RSF_ in sendmail.h
2887 ** logl -- logging level.
2888 ** host -- NULL or relay host.
2889 ** logid -- id for sm_syslog.
2892 ** EX_OK -- if the rwset doesn't resolve to $#error
2893 ** else -- the failure status (message printed)
2897 rscheck(rwset, p1, p2, e, flags, logl, host, logid)
2910 int volatile rstat = EX_OK;
2913 bool volatile discard = false;
2915 bool saveQuickAbort = QuickAbort;
2916 bool saveSuprErrs = SuprErrs;
2918 bool quarantine = false;
2919 char ubuf[BUFSIZ * 2];
2920 #endif /* _FFR_QUARANTINE */
2922 char pvpbuf[PSBUFSIZE];
2923 extern char MsgBuf[];
2926 sm_dprintf("rscheck(%s, %s, %s)\n", rwset, p1,
2927 p2 == NULL ? "(NULL)" : p2);
2929 rsno = strtorwset(rwset, NULL, ST_FIND);
2935 bufsize = strlen(p1) + strlen(p2) + 2;
2936 if (bufsize > sizeof buf0)
2937 buf = sm_malloc_x(bufsize);
2941 bufsize = sizeof buf0;
2943 (void) sm_snprintf(buf, bufsize, "%s%c%s", p1, CONDELSE, p2);
2947 bufsize = strlen(p1) + 1;
2948 if (bufsize > sizeof buf0)
2949 buf = sm_malloc_x(bufsize);
2953 bufsize = sizeof buf0;
2955 (void) sm_strlcpy(buf, p1, bufsize);
2961 pvp = prescan(buf, '\0', pvpbuf, sizeof pvpbuf, NULL,
2962 bitset(RSF_RMCOMM, flags) ? NULL : TokTypeNoC);
2963 SuprErrs = saveSuprErrs;
2967 sm_dprintf("rscheck: cannot prescan input\n");
2969 syserr("rscheck: cannot prescan input: \"%s\"",
2970 shortenstring(buf, MAXSHORTSTR));
2975 if (bitset(RSF_UNSTRUCTURED, flags))
2977 (void) REWRITE(pvp, rsno, e);
2978 if (bitset(RSF_UNSTRUCTURED, flags))
2979 SuprErrs = saveSuprErrs;
2980 if (pvp[0] == NULL || (pvp[0][0] & 0377) != CANONNET ||
2981 pvp[1] == NULL || (strcmp(pvp[1], "error") != 0 &&
2982 strcmp(pvp[1], "discard") != 0))
2987 if (strcmp(pvp[1], "discard") == 0)
2990 sm_dprintf("rscheck: discard mailer selected\n");
2991 e->e_flags |= EF_DISCARD;
2995 else if (strcmp(pvp[1], "error") == 0 &&
2996 pvp[2] != NULL && (pvp[2][0] & 0377) == CANONHOST &&
2997 pvp[3] != NULL && strcmp(pvp[3], "quarantine") == 0)
2999 if (pvp[4] == NULL ||
3000 (pvp[4][0] & 0377) != CANONUSER ||
3002 e->e_quarmsg = sm_rpool_strdup_x(e->e_rpool,
3006 cataddr(&(pvp[5]), NULL, ubuf,
3008 e->e_quarmsg = sm_rpool_strdup_x(e->e_rpool,
3011 macdefine(&e->e_macro, A_PERM,
3012 macid("{quarantine}"), e->e_quarmsg);
3015 #endif /* _FFR_QUARANTINE */
3018 int savelogusrerrs = LogUsrErrs;
3019 static bool logged = false;
3021 /* got an error -- process it */
3022 saveexitstat = ExitStat;
3024 (void) buildaddr(pvp, &a1, 0, e);
3025 LogUsrErrs = savelogusrerrs;
3027 ExitStat = saveexitstat;
3030 if (bitset(RSF_COUNT, flags))
3031 markstats(e, &a1, STATS_REJECT);
3036 if (LogLevel > logl)
3045 (void) sm_snprintf(p, SPACELEFT(lbuf, p),
3054 relay = macvalue('_', e);
3057 (void) sm_snprintf(p, SPACELEFT(lbuf, p),
3058 ", relay=%s", relay);
3063 sm_syslog(LOG_NOTICE, logid,
3064 "ruleset=%s, arg1=%s%s, discard",
3067 else if (quarantine)
3068 sm_syslog(LOG_NOTICE, logid,
3069 "ruleset=%s, arg1=%s%s, quarantine=%s",
3070 rwset, p1, lbuf, ubuf);
3071 #endif /* _FFR_QUARANTINE */
3073 sm_syslog(LOG_NOTICE, logid,
3074 "ruleset=%s, arg1=%s%s, reject=%s",
3075 rwset, p1, lbuf, MsgBuf);
3085 QuickAbort = saveQuickAbort;
3091 /* rulesets don't set errno */
3093 if (rstat != EX_OK && QuickAbort)
3094 sm_exc_raisenew_x(&EtypeQuickAbort, 2);
3098 ** RSCAP -- call rewriting set to return capabilities
3101 ** rwset -- the rewriting set to use.
3102 ** p1 -- the first string to check.
3103 ** p2 -- the second string to check -- may be null.
3104 ** e -- the current envelope.
3105 ** pvp -- pointer to token vector.
3106 ** pvpbuf -- buffer space.
3109 ** EX_UNAVAILABLE -- ruleset doesn't exist.
3110 ** EX_DATAERR -- prescan() failed.
3111 ** EX_OK -- rewrite() was successful.
3112 ** else -- return status from rewrite().
3116 rscap(rwset, p1, p2, e, pvp, pvpbuf, size)
3127 int volatile rstat = EX_OK;
3129 bool saveQuickAbort = QuickAbort;
3130 bool saveSuprErrs = SuprErrs;
3132 extern char MsgBuf[];
3135 sm_dprintf("rscap(%s, %s, %s)\n", rwset, p1,
3136 p2 == NULL ? "(NULL)" : p2);
3140 rsno = strtorwset(rwset, NULL, ST_FIND);
3142 return EX_UNAVAILABLE;
3146 bufsize = strlen(p1) + strlen(p2) + 2;
3147 if (bufsize > sizeof buf0)
3148 buf = sm_malloc_x(bufsize);
3152 bufsize = sizeof buf0;
3154 (void) sm_snprintf(buf, bufsize, "%s%c%s", p1, CONDELSE, p2);
3158 bufsize = strlen(p1) + 1;
3159 if (bufsize > sizeof buf0)
3160 buf = sm_malloc_x(bufsize);
3164 bufsize = sizeof buf0;
3166 (void) sm_strlcpy(buf, p1, bufsize);
3172 *pvp = prescan(buf, '\0', pvpbuf, size, NULL, NULL);
3174 rstat = REWRITE(*pvp, rsno, e);
3178 sm_dprintf("rscap: cannot prescan input\n");
3187 SuprErrs = saveSuprErrs;
3188 QuickAbort = saveQuickAbort;
3190 /* prevent information leak, this may contain rewrite error */