2 * Copyright (c) 1998-2006 Proofpoint, 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.407 2013-11-22 20:51:56 ca Exp $")
18 #include <sm/sendmail.h>
22 static void allocaddr __P((ADDRESS *, int, char *, ENVELOPE *));
23 static int callsubr __P((char**, int, ENVELOPE *));
24 static char *map_lookup __P((STAB *, char *, char **, int *, ENVELOPE *));
25 static ADDRESS *buildaddr __P((char **, ADDRESS *, int, ENVELOPE *));
26 static bool hasctrlchar __P((register char *, bool, bool));
28 /* replacement for illegal characters in addresses */
29 #define BAD_CHAR_REPLACEMENT '?'
32 ** PARSEADDR -- Parse an address
34 ** Parses an address and breaks it up into three parts: a
35 ** net to transmit the message on, the host to transmit it
36 ** to, and a user on that host. These are loaded into an
37 ** ADDRESS header with the values squirreled away if necessary.
38 ** The "user" part may not be a real user; the process may
39 ** just reoccur on that machine. For example, on a machine
40 ** with an arpanet connection, the address
41 ** csvax.bill@berkeley
42 ** will break up to a "user" of 'csvax.bill' and a host
43 ** of 'berkeley' -- to be transmitted over the arpanet.
46 ** addr -- the address to parse. [i]
47 ** a -- a pointer to the address descriptor buffer.
48 ** If NULL, an address will be created.
49 ** flags -- describe detail for parsing. See RF_ definitions
51 ** delim -- the character to terminate the address, passed
53 ** delimptr -- if non-NULL, set to the location of the
54 ** delim character that was found.
55 ** e -- the envelope that will contain this address.
56 ** isrcpt -- true if the address denotes a recipient; false
57 ** indicates a sender.
60 ** A pointer to the address descriptor header (`a' if
68 /* following delimiters are inherent to the internal algorithms */
69 #define DELIMCHARS "()<>,;\r\n" /* default word delimiters */
72 parseaddr(addr, a, flags, delim, delimptr, e, isrcpt)
82 auto char *delimptrbuf;
84 char pvpbuf[PSBUFSIZE];
87 ** Initialize and prescan address.
90 if (bitset(RF_IS_EXT, flags) && addr != NULL)
94 addr = quote_internal_chars(addr, NULL, &len, NULL);
100 sm_dprintf("\n--parseaddr(%s)\n", addr);
102 if (delimptr == NULL)
103 delimptr = &delimptrbuf;
105 pvp = prescan(addr, delim, pvpbuf, sizeof(pvpbuf), delimptr,
110 sm_dprintf("parseaddr-->NULL\n");
114 if (invalidaddr(addr, delim == '\0' ? NULL : *delimptr, isrcpt))
117 sm_dprintf("parseaddr-->bad address\n");
122 ** Save addr if we are going to have to.
124 ** We have to do this early because there is a chance that
125 ** the map lookups in the rewriting rules could clobber
126 ** static memory somewhere.
129 if (bitset(RF_COPYPADDR, flags) && addr != NULL)
131 char savec = **delimptr;
135 e->e_to = addr = sm_rpool_strdup_x(e->e_rpool, addr);
141 ** Apply rewriting rules.
142 ** Ruleset 0 does basic parsing. It must resolve.
146 e->e_flags |= EF_SECURE;
147 if (REWRITE(pvp, 3, e) == EX_TEMPFAIL)
149 if (REWRITE(pvp, 0, e) == EX_TEMPFAIL)
153 ** Build canonical address from pvp.
156 a = buildaddr(pvp, a, flags, e);
158 if (NULL != a->q_user)
162 a->q_user = quote_internal_chars(a->q_user, NULL, &len, e->e_rpool); /* EAI: ok */
166 if (hasctrlchar(a->q_user, isrcpt, true))
169 sm_dprintf("parseaddr-->bad q_user\n");
172 ** Just mark the address as bad so DSNs work.
173 ** hasctrlchar() has to make sure that the address
174 ** has been sanitized, e.g., shortened.
177 a->q_state = QS_BADADDR;
181 ** Make local copies of the host & user and then
182 ** transport them out.
185 allocaddr(a, flags, addr, e);
186 e->e_flags &= ~EF_SECURE;
187 if (QS_IS_BADADDR(a->q_state))
189 /* weed out bad characters in the printable address too */
190 (void) hasctrlchar(a->q_paddr, isrcpt, false);
195 ** Select a queue directory for recipient addresses.
196 ** This is done here and in split_across_queue_groups(),
197 ** but the latter applies to addresses after aliasing,
198 ** and only if splitting is done.
201 if ((a->q_qgrp == NOAQGRP || a->q_qgrp == ENVQGRP) &&
202 !bitset(RF_SENDERADDR|RF_HEADERADDR|RF_RM_ADDR, flags) &&
203 OpMode != MD_INITALIAS)
207 /* call ruleset which should return a queue group name */
208 r = rscap(RS_QUEUEGROUP, a->q_user, NULL, e, &pvp, pvpbuf,
211 pvp != NULL && pvp[0] != NULL &&
212 (pvp[0][0] & 0377) == CANONNET &&
213 pvp[1] != NULL && pvp[1][0] != '\0')
215 r = name2qid(pvp[1]);
216 if (r == NOQGRP && LogLevel > 10)
217 sm_syslog(LOG_INFO, NOQID,
218 "can't find queue group name %s, selection ignored",
220 if (tTd(20, 4) && r != NOQGRP)
221 sm_syslog(LOG_INFO, NOQID,
222 "queue group name %s -> %d",
224 a->q_qgrp = r == NOQGRP ? ENVQGRP : r;
229 ** If there was a parsing failure, mark it for queueing.
232 if (qup && OpMode != MD_INITALIAS)
234 char *msg = "Transient parse error -- message queued for future delivery";
236 if (e->e_sendmode == SM_DEFER)
237 msg = "Deferring message until queue run";
239 sm_dprintf("parseaddr: queueing message\n");
241 if (e->e_message == NULL && e->e_sendmode != SM_DEFER)
242 e->e_message = sm_rpool_strdup_x(e->e_rpool, msg);
243 a->q_state = QS_QUEUEUP;
244 a->q_status = "4.4.3";
248 ** Compute return value.
253 sm_dprintf("parseaddr-->");
254 printaddr(sm_debug_file(), a, false);
261 ** INVALIDADDR -- check for address containing characters used for macros
264 ** addr -- the address to check.
265 ** note: this is the complete address (including display part)
266 ** delimptr -- if non-NULL: end of address to check, i.e.,
267 ** a pointer in the address string.
268 ** isrcpt -- true iff the address is for a recipient.
271 ** true -- if the address has characters that are reserved
272 ** for macros or is too long.
273 ** false -- otherwise.
277 invalidaddr(addr, delimptr, isrcpt)
283 char savedelim = '\0';
287 if (delimptr != NULL)
289 /* delimptr points to the end of the address to test */
290 savedelim = *delimptr;
291 if (savedelim != '\0') /* if that isn't '\0' already: */
292 *delimptr = '\0'; /* set it */
294 for (; *addr != '\0'; addr++)
297 if (!EightBitAddrOK && (*addr & 0340) == 0200)
301 *addr = BAD_CHAR_REPLACEMENT;
305 if (xlen > MAXNAME - 1) /* EAI:ok */
310 usrerr("553 5.1.0 Address \"%s\" too long (%d bytes max)",
311 b, MAXNAME - 1); /* EAI:ok */
318 /* check for valid UTF8 string? */
323 usrerr("501 5.1.3 8-bit character in mailbox address \"%s\"",
326 usrerr("501 5.1.7 8-bit character in mailbox address \"%s\"",
330 if (delimptr != NULL && savedelim != '\0')
331 *delimptr = savedelim; /* restore old character at delimptr */
336 ** HASCTRLCHAR -- check for address containing meta-characters
338 ** Checks that the address contains no meta-characters, and contains
339 ** no "non-printable" characters unless they are quoted or escaped.
340 ** Quoted or escaped characters are literals.
343 ** addr -- the address to check.
344 ** isrcpt -- true if the address is for a recipient; false
346 ** complain -- true if an error should issued if the address
347 ** is invalid and should be "repaired".
350 ** true -- if the address has any "weird" characters or
351 ** non-printable characters or if a quote is unbalanced.
352 ** false -- otherwise.
355 ** Might invoke shorten_rfc822_string() to change addr in place.
359 hasctrlchar(addr, isrcpt, complain)
361 bool isrcpt, complain;
370 for (; *addr != '\0'; addr++)
373 if (xlen > MAXNAME - 1) /* EAI:ok */
377 (void) shorten_rfc822_string(b, MAXNAME - 1); /* EAI:ok */
378 usrerr("553 5.1.0 Address \"%s\" too long (%d bytes max)",
379 b, MAXNAME - 1); /* EAI:ok */
384 if (!quoted && ((unsigned char)*addr < 32 || *addr == 127))
386 result = "non-printable character";
387 *addr = BAD_CHAR_REPLACEMENT;
392 else if (*addr == '\\')
394 /* XXX Generic problem: no '\0' in strings. */
397 result = "trailing \\ character";
398 *--addr = BAD_CHAR_REPLACEMENT;
402 if (!SMTP_UTF8 && !EightBitAddrOK && (*addr & 0340) == 0200)
405 result = "8-bit character";
406 *addr = BAD_CHAR_REPLACEMENT;
411 result = "unbalanced quote"; /* unbalanced quote */
412 if (result != NULL && complain)
415 usrerr("501 5.1.3 Syntax error in mailbox address \"%s\" (%s)",
418 usrerr("501 5.1.7 Syntax error in mailbox address \"%s\" (%s)",
421 return result != NULL;
425 ** ALLOCADDR -- do local allocations of address on demand.
427 ** Also lowercases the host name if requested.
430 ** a -- the address to reallocate.
431 ** flags -- the copy flag (see RF_ definitions in sendmail.h
432 ** for a description).
433 ** paddr -- the printname of the address.
440 ** Copies portions of a into local buffers as requested.
444 allocaddr(a, flags, paddr, e)
451 sm_dprintf("allocaddr: flags=%x, paddr=%s, ad=%d\n", flags, paddr, bitset(EF_SECURE, e->e_flags));
455 if (a->q_user == NULL)
457 if (a->q_host == NULL)
460 if (bitset(EF_SECURE, e->e_flags))
461 a->q_flags |= QSECURE;
463 if (bitset(RF_COPYPARSE, flags))
465 a->q_host = sm_rpool_strdup_x(e->e_rpool, a->q_host);
466 if (a->q_user != a->q_paddr)
467 a->q_user = sm_rpool_strdup_x(e->e_rpool, a->q_user);
470 if (a->q_paddr == NULL)
471 a->q_paddr = sm_rpool_strdup_x(e->e_rpool, a->q_user);
476 ** PRESCAN -- Prescan name and make it canonical
478 ** Scans a name and turns it into a set of tokens. This process
479 ** deletes blanks and comments (in parentheses) (if the token type
480 ** for left paren is SPC).
482 ** This routine knows about quoted strings and angle brackets.
484 ** There are certain subtleties to this routine. The one that
485 ** comes to mind now is that backslashes on the ends of names
486 ** are silently stripped off; this is intentional. The problem
487 ** is that some versions of sndmsg (like at LBL) set the kill
488 ** character to something other than @ when reading addresses;
489 ** so people type "csvax.eric\@berkeley" -- which screws up the
493 ** addr -- the name to chomp.
494 ** delim -- the delimiter for the address, normally
495 ** '\0' or ','; \0 is accepted in any case.
496 ** If '\t' then we are reading the .cf file.
497 ** pvpbuf -- place to put the saved text -- note that
498 ** the pointers are static.
499 ** pvpbsize -- size of pvpbuf.
500 ** delimptr -- if non-NULL, set to the location of the
501 ** terminating delimiter.
502 ** toktab -- if set, a token table to use for parsing.
503 ** If NULL, use the default table.
504 ** ignore -- if true, ignore unbalanced addresses
507 ** A pointer to a vector of tokens.
511 /* states and character types */
512 #define OPR 0 /* operator */
513 #define ATM 1 /* atom */
514 #define QST 2 /* in quoted string */
515 #define SPC 3 /* chewing up spaces */
516 #define ONE 4 /* pick up one character */
517 #define ILL 5 /* illegal character */
519 #define NSTATES 6 /* number of states */
520 #define TYPE 017 /* mask to select state type */
522 /* meta bits for table */
523 #define M 020 /* meta character; don't pass through */
524 #define B 040 /* cause a break */
525 #define MB M|B /* meta-break */
527 static short StateTab[NSTATES][NSTATES] =
529 /* oldst chtype> OPR ATM QST SPC ONE ILL */
530 /*OPR*/ { OPR|B, ATM|B, QST|B, SPC|MB, ONE|B, ILL|MB },
531 /*ATM*/ { OPR|B, ATM, QST|B, SPC|MB, ONE|B, ILL|MB },
532 /*QST*/ { QST, QST, OPR, QST, QST, QST },
533 /*SPC*/ { OPR, ATM, QST, SPC|M, ONE, ILL|MB },
534 /*ONE*/ { OPR, OPR, OPR, OPR, OPR, ILL|MB },
535 /*ILL*/ { OPR|B, ATM|B, QST|B, SPC|MB, ONE|B, ILL|M }
538 /* these all get modified with the OperatorChars */
540 /* token type table for external strings */
541 unsigned char ExtTokenTab[256] =
543 /* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */
544 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,SPC,SPC,SPC,SPC,SPC,ATM,ATM,
545 /* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */
546 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
547 /* sp ! " # $ % & ' ( ) * + , - . / */
548 SPC,ATM,QST,ATM,ATM,ATM,ATM,ATM, SPC,SPC,ATM,ATM,ATM,ATM,ATM,ATM,
549 /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */
550 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
551 /* @ A B C D E F G H I J K L M N O */
552 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
553 /* P Q R S T U V W X Y Z [ \ ] ^ _ */
554 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
555 /* ` a b c d e f g h i j k l m n o */
556 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
557 /* p q r s t u v w x y z { | } ~ del */
558 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
560 /* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */
561 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
562 /* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */
563 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
564 /* sp ! " # $ % & ' ( ) * + , - . / */
565 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
566 /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */
567 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
568 /* @ A B C D E F G H I J K L M N O */
569 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
570 /* P Q R S T U V W X Y Z [ \ ] ^ _ */
571 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
572 /* ` a b c d e f g h i j k l m n o */
573 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
574 /* p q r s t u v w x y z { | } ~ del */
575 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM
578 /* token type table for internal strings */
579 unsigned char IntTokenTab[256] =
581 /* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */
582 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,SPC,SPC,SPC,SPC,SPC,ATM,ATM,
583 /* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */
584 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
585 /* sp ! " # $ % & ' ( ) * + , - . / */
586 SPC,ATM,QST,ATM,ATM,ATM,ATM,ATM, SPC,SPC,ATM,ATM,ATM,ATM,ATM,ATM,
587 /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */
588 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
589 /* @ A B C D E F G H I J K L M N O */
590 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
591 /* P Q R S T U V W X Y Z [ \ ] ^ _ */
592 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
593 /* ` a b c d e f g h i j k l m n o */
594 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
595 /* p q r s t u v w x y z { | } ~ del */
596 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
598 /* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */
599 OPR,OPR,ONE,OPR,OPR,OPR,OPR,OPR, OPR,OPR,OPR,OPR,OPR,OPR,OPR,OPR,
600 /* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */
601 OPR,OPR,OPR,ONE,ONE,ONE,OPR,OPR, OPR,OPR,OPR,OPR,OPR,OPR,OPR,OPR,
602 /* sp ! " # $ % & ' ( ) * + , - . / */
603 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
604 /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */
605 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
606 /* @ A B C D E F G H I J K L M N O */
607 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
608 /* P Q R S T U V W X Y Z [ \ ] ^ _ */
609 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
610 /* ` a b c d e f g h i j k l m n o */
611 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
612 /* p q r s t u v w x y z { | } ~ del */
613 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ONE
616 /* token type table for MIME parsing */
617 unsigned char MimeTokenTab[256] =
619 /* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */
620 ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL, ILL,SPC,SPC,SPC,SPC,SPC,ILL,ILL,
621 /* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */
622 ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL, ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL,
623 /* sp ! " # $ % & ' ( ) * + , - . / */
624 SPC,ATM,QST,ATM,ATM,ATM,ATM,ATM, SPC,SPC,ATM,ATM,OPR,ATM,ATM,OPR,
625 /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */
626 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,OPR,OPR,OPR,OPR,OPR,OPR,
627 /* @ A B C D E F G H I J K L M N O */
628 OPR,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
629 /* P Q R S T U V W X Y Z [ \ ] ^ _ */
630 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,OPR,OPR,OPR,ATM,ATM,
631 /* ` a b c d e f g h i j k l m n o */
632 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
633 /* p q r s t u v w x y z { | } ~ del */
634 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
636 /* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */
637 ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL, ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL,
638 /* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */
639 ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL, ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL,
640 /* sp ! " # $ % & ' ( ) * + , - . / */
641 ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL, ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL,
642 /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */
643 ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL, ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL,
644 /* @ A B C D E F G H I J K L M N O */
645 ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL, ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL,
646 /* P Q R S T U V W X Y Z [ \ ] ^ _ */
647 ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL, ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL,
648 /* ` a b c d e f g h i j k l m n o */
649 ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL, ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL,
650 /* p q r s t u v w x y z { | } ~ del */
651 ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL, ILL,ILL,ILL,ILL,ILL,ILL,ILL,ONE
654 /* token type table: don't strip comments */
655 unsigned char TokTypeNoC[256] =
657 /* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */
658 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,SPC,SPC,SPC,SPC,SPC,ATM,ATM,
659 /* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */
660 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
661 /* sp ! " # $ % & ' ( ) * + , - . / */
662 SPC,ATM,QST,ATM,ATM,ATM,ATM,ATM, OPR,OPR,ATM,ATM,ATM,ATM,ATM,ATM,
663 /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */
664 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
665 /* @ A B C D E F G H I J K L M N O */
666 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
667 /* P Q R S T U V W X Y Z [ \ ] ^ _ */
668 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
669 /* ` a b c d e f g h i j k l m n o */
670 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
671 /* p q r s t u v w x y z { | } ~ del */
672 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
674 /* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */
675 OPR,OPR,ONE,OPR,OPR,OPR,OPR,OPR, OPR,OPR,OPR,OPR,OPR,OPR,OPR,OPR,
676 /* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */
677 OPR,OPR,OPR,ONE,ONE,ONE,OPR,OPR, OPR,OPR,OPR,OPR,OPR,OPR,OPR,OPR,
678 /* sp ! " # $ % & ' ( ) * + , - . / */
679 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
680 /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */
681 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
682 /* @ A B C D E F G H I J K L M N O */
683 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
684 /* P Q R S T U V W X Y Z [ \ ] ^ _ */
685 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
686 /* ` a b c d e f g h i j k l m n o */
687 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
688 /* p q r s t u v w x y z { | } ~ del */
689 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ONE
692 #define NOCHAR (-1) /* signal nothing in lookahead token */
695 prescan(addr, delim, pvpbuf, pvpbsize, delimptr, toktab, ignore)
701 unsigned char *toktab;
715 char *saveto = CurEnv->e_to;
716 static char *av[MAXATOM + 1];
717 static bool firsttime = true;
722 /* initialize the token type table */
726 if (OperatorChars == NULL)
729 OperatorChars = macvalue('o', CurEnv);
730 if (OperatorChars == NULL)
731 OperatorChars = ".:@[]";
733 expand(OperatorChars, obuf, sizeof(obuf) - sizeof(DELIMCHARS),
735 (void) sm_strlcat(obuf, DELIMCHARS, sizeof(obuf));
736 for (p = obuf; *p != '\0'; p++)
738 if (IntTokenTab[*p & 0xff] == ATM)
739 IntTokenTab[*p & 0xff] = OPR;
740 if (ExtTokenTab[*p & 0xff] == ATM)
741 ExtTokenTab[*p & 0xff] = OPR;
742 if (TokTypeNoC[*p & 0xff] == ATM)
743 TokTypeNoC[*p & 0xff] = OPR;
747 toktab = ExtTokenTab;
749 /* make sure error messages don't have garbage on them */
754 route_syntax = false;
764 sm_dprintf("prescan: ");
765 xputs(sm_debug_file(), p);
776 /* store away any old lookahead character */
777 if (c != NOCHAR && !bslashmode)
779 /* see if there is room */
780 if (q >= &pvpbuf[pvpbsize - 5])
783 usrerr("553 5.1.1 Address too long");
786 if (strlen(addr) > MAXNAME)
787 addr[MAXNAME] = '\0';
789 if (delimptr != NULL)
795 CurEnv->e_to = saveto;
797 sm_dprintf("prescan: ==> NULL\n");
801 /* squirrel it away */
803 if ((char) c == (char) -1 && !tTd(82, 101) &&
806 #endif /* !ALLOW_255 */
811 /* read a new input character */
815 /* diagnose and patch up bad syntax */
818 else if (state == QST)
820 usrerr("553 Unbalanced '\"'");
823 else if (cmntcnt > 0)
825 usrerr("553 Unbalanced '('");
828 else if (anglecnt > 0)
831 usrerr("553 Unbalanced '<'");
838 else if (c == delim && cmntcnt <= 0 && state != QST)
843 /* special case for better error management */
844 if (delim == ',' && !route_syntax && !ignore)
846 usrerr("553 Unbalanced '<'");
853 sm_dprintf("c=%c, s=%d; ", c, state);
855 /* chew up special characters */
861 /* kludge \! for naive users */
867 else if (c != '!' || state == QST)
869 /* see if there is room */
870 if (q >= &pvpbuf[pvpbsize - 5])
882 else if (state == QST)
885 /* do nothing, just avoid next clauses */
887 else if (c == '(' && toktab['('] == SPC)
892 else if (c == ')' && toktab['('] == SPC)
898 usrerr("553 Unbalanced ')'");
905 else if (cmntcnt > 0)
914 while (SM_ISSPACE(*ptr))
925 usrerr("553 Unbalanced '>'");
931 route_syntax = false;
933 else if (delim == ' ' && SM_ISSPACE(c))
939 /* see if this is end of input */
940 if (c == delim && anglecnt <= 0 && state != QST)
943 newstate = StateTab[state][toktab[c & 0xff]];
945 sm_dprintf("ns=%02o\n", newstate);
946 state = newstate & TYPE;
949 if (isascii(c) && isprint(c))
950 usrerr("553 Illegal character %c", c);
952 usrerr("553 Illegal character 0x%02x",
955 if (bitset(M, newstate))
957 if (bitset(B, newstate))
964 /* see if there is room */
965 if (q >= &pvpbuf[pvpbsize - 5])
971 xputs(sm_debug_file(), tok);
974 if (avp >= &av[MAXATOM])
976 usrerr("553 5.1.0 prescan: too many tokens");
979 if (xlen > MAXNAME) /* EAI:ok */
981 usrerr("553 5.1.0 prescan: token too long");
986 } while (c != '\0' && (c != delim || anglecnt > 0));
988 if (delimptr != NULL)
996 sm_dprintf("prescan==>");
997 printav(sm_debug_file(), av);
999 CurEnv->e_to = saveto;
1003 sm_dprintf("prescan: null leading token\n");
1009 ** REWRITE -- apply rewrite rules to token vector.
1011 ** This routine is an ordered production system. Each rewrite
1012 ** rule has a LHS (called the pattern) and a RHS (called the
1013 ** rewrite); 'rwr' points the the current rewrite rule.
1015 ** For each rewrite rule, 'avp' points the address vector we
1016 ** are trying to match against, and 'pvp' points to the pattern.
1017 ** If pvp points to a special match value (MATCHZANY, MATCHANY,
1018 ** MATCHONE, MATCHCLASS, MATCHNCLASS) then the address in avp
1019 ** matched is saved away in the match vector (pointed to by 'mvp').
1021 ** When a match between avp & pvp does not match, we try to
1022 ** back out. If we back up over MATCHONE, MATCHCLASS, or MATCHNCLASS
1023 ** we must also back out the match in mvp. If we reach a
1024 ** MATCHANY or MATCHZANY we just extend the match and start
1027 ** When we finally match, we rewrite the address vector
1028 ** and try over again.
1031 ** pvp -- pointer to token vector. [i]
1032 ** ruleset -- the ruleset to use for rewriting.
1033 ** reclevel -- recursion level (to catch loops).
1034 ** e -- the current envelope.
1035 ** maxatom -- maximum length of buffer (usually MAXATOM)
1038 ** A status code. If EX_TEMPFAIL, higher level code should
1039 ** attempt recovery.
1047 char **match_first; /* first token matched */
1048 char **match_last; /* last token matched */
1049 char **match_pattern; /* pointer to pattern */
1053 rewrite(pvp, ruleset, reclevel, e, maxatom)
1057 register ENVELOPE *e;
1060 register char *ap; /* address pointer */
1061 register char *rp; /* rewrite pointer */
1062 register char *rulename; /* ruleset name */
1063 register char *prefix;
1064 register char **avp; /* address vector pointer */
1065 register char **rvp; /* rewrite vector pointer */
1066 register struct match *mlp; /* cur ptr into mlist */
1067 register struct rewrite *rwr; /* pointer to current rewrite rule */
1068 int ruleno; /* current rule number */
1069 int rstat = EX_OK; /* return status */
1071 struct match mlist[MAXMATCH]; /* stores match on LHS */
1072 char *npvp[MAXATOM + 1]; /* temporary space for rebuild */
1077 ** mlp will not exceed mlist[] because readcf enforces
1078 ** the upper limit of entries when reading rulesets.
1081 if (ruleset < 0 || ruleset >= MAXRWSETS)
1083 syserr("554 5.3.5 rewrite: illegal ruleset number %d", ruleset);
1086 rulename = RuleSetNames[ruleset];
1087 if (rulename == NULL)
1089 (void) sm_snprintf(name, sizeof(name), "%d", ruleset);
1092 if (OpMode == MD_TEST)
1095 prefix = "rewrite: ruleset ";
1096 if (OpMode == MD_TEST)
1098 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
1099 "%s%-16.16s input:", prefix, rulename);
1100 printav(smioout, pvp);
1102 else if (tTd(21, 1))
1104 sm_dprintf("%s%-16.16s input:", prefix, rulename);
1105 printav(sm_debug_file(), pvp);
1107 if (reclevel++ > MaxRuleRecursion)
1109 syserr("rewrite: excessive recursion (max %d), ruleset %s",
1110 MaxRuleRecursion, rulename);
1119 ** Run through the list of rewrite rules, applying
1125 for (rwr = RewriteRules[ruleset]; rwr != NULL; )
1129 /* if already canonical, quit now */
1130 if (pvp[0] != NULL && (pvp[0][0] & 0377) == CANONNET)
1136 sm_dprintf("-----trying rule (line %d):",
1139 sm_dprintf("-----trying rule:");
1140 printav(sm_debug_file(), rwr->r_lhs);
1143 /* try to match on this rule */
1147 if (++loopcount > 100)
1149 syserr("554 5.3.5 Infinite loop in ruleset %s, rule %d",
1153 sm_dprintf("workspace: ");
1154 printav(sm_debug_file(), pvp);
1159 while ((ap = *avp) != NULL || *rvp != NULL)
1164 sm_dprintf("ADVANCE rp=");
1165 xputs(sm_debug_file(), rp);
1166 sm_dprintf(", ap=");
1167 xputs(sm_debug_file(), ap);
1172 /* end-of-pattern before end-of-address */
1176 (rp[0] & 0377) != MATCHZANY &&
1177 (rp[0] & 0377) != MATCHZERO)
1179 /* end-of-input with patterns left */
1183 switch (rp[0] & 0377)
1186 /* match any phrase in a class */
1187 mlp->match_pattern = rvp;
1188 mlp->match_first = avp;
1193 mlp->match_last = avp++;
1194 cataddr(mlp->match_first, mlp->match_last,
1195 buf, sizeof(buf), '\0', true);
1196 if (!wordinclass(buf, rp[1]))
1200 sm_dprintf("EXTEND rp=");
1201 xputs(sm_debug_file(), rp);
1202 sm_dprintf(", ap=");
1203 xputs(sm_debug_file(), ap);
1209 sm_dprintf("CLMATCH\n");
1214 /* match any token not in a class */
1215 if (wordinclass(ap, rp[1]))
1222 /* match exactly one token */
1223 mlp->match_pattern = rvp;
1224 mlp->match_first = avp;
1225 mlp->match_last = avp++;
1230 /* match zero or more tokens */
1231 mlp->match_pattern = rvp;
1232 mlp->match_first = avp;
1233 mlp->match_last = avp - 1;
1238 /* match zero tokens */
1243 ** Match against run-time macro.
1244 ** This algorithm is broken for the
1245 ** general case (no recursive macros,
1246 ** improper tokenization) but should
1247 ** work for the usual cases.
1250 ap = macvalue(rp[1], e);
1251 mlp->match_first = avp;
1253 sm_dprintf("rewrite: LHS $&{%s} => \"%s\"\n",
1255 ap == NULL ? "(NULL)" : ap);
1262 sm_strncasecmp(ap, *avp,
1266 avp = mlp->match_first;
1269 ap += strlen(*avp++);
1276 /* must have exact match */
1277 if (!SM_STRCASEEQ(rp, ap))
1283 /* successful match on this token */
1288 /* match failed -- back up */
1289 while (--mlp >= mlist)
1291 rvp = mlp->match_pattern;
1293 avp = mlp->match_last + 1;
1298 sm_dprintf("BACKUP rp=");
1299 xputs(sm_debug_file(), rp);
1300 sm_dprintf(", ap=");
1301 xputs(sm_debug_file(), ap);
1307 /* run off the end -- back up again */
1311 if ((rp[0] & 0377) == MATCHANY ||
1312 (rp[0] & 0377) == MATCHZANY)
1314 /* extend binding and continue */
1315 mlp->match_last = avp++;
1320 if ((rp[0] & 0377) == MATCHCLASS)
1322 /* extend binding and try again */
1323 mlp->match_last = avp;
1330 /* total failure to match */
1336 ** See if we successfully matched
1339 if (mlp < mlist || *rvp != NULL)
1342 sm_dprintf("----- rule fails\n");
1352 sm_dprintf("-----rule matches:");
1353 printav(sm_debug_file(), rvp);
1359 if ((rp[0] & 0377) == CANONUSER)
1366 else if ((rp[0] & 0377) == CANONHOST)
1374 for (avp = npvp; *rvp != NULL; rvp++)
1376 register struct match *m;
1380 if ((rp[0] & 0377) == MATCHREPL)
1382 /* substitute from LHS */
1383 m = &mlist[rp[1] - '1'];
1384 if (m < mlist || m >= mlp)
1386 syserr("554 5.3.5 rewrite: ruleset %s: replacement $%c out of bounds",
1392 sm_dprintf("$%c:", rp[1]);
1393 pp = m->match_first;
1394 while (pp <= m->match_last)
1396 sm_dprintf(" %p=\"", (void *)*pp);
1398 sm_dprintf("%s\"", *pp++);
1402 pp = m->match_first;
1403 while (pp <= m->match_last)
1405 if (avp >= &npvp[maxatom])
1412 /* some sort of replacement */
1413 if (avp >= &npvp[maxatom])
1416 syserr("554 5.3.0 rewrite: expansion too long");
1418 sm_syslog(LOG_ERR, e->e_id,
1419 "rewrite: expansion too long, ruleset=%s, ruleno=%d",
1423 if ((rp[0] & 0377) != MACRODEXPAND)
1425 /* vanilla replacement from RHS */
1430 /* $&{x} replacement */
1431 char *mval = macvalue(rp[1], e);
1434 static size_t pvpb1_size = 0;
1435 static char **pvpb1 = NULL;
1436 char pvpbuf[PSBUFSIZE];
1439 sm_dprintf("rewrite: RHS $&{%s} => \"%s\"\n",
1441 mval == NULL ? "(NULL)" : mval);
1442 if (SM_IS_EMPTY(mval))
1445 /* save the remainder of the input */
1446 for (xpvp = pvp; *xpvp != NULL; xpvp++)
1447 trsize += sizeof(*xpvp);
1448 if (trsize > pvpb1_size)
1453 sm_pmalloc_x(trsize);
1454 pvpb1_size = trsize;
1457 memmove((char *) pvpb1,
1461 /* scan the new replacement */
1462 xpvp = prescan(mval, '\0', pvpbuf,
1463 sizeof(pvpbuf), NULL,
1467 /* prescan pre-printed error */
1471 /* insert it into the output stream */
1472 while (*xpvp != NULL)
1475 sm_dprintf(" ... %s\n",
1477 *avp++ = sm_rpool_strdup_x(
1479 if (avp >= &npvp[maxatom])
1484 sm_dprintf(" ... DONE\n");
1486 /* restore the old trailing input */
1487 memmove((char *) pvp,
1496 ** Check for any hostname/keyword lookups.
1499 for (rvp = npvp; *rvp != NULL; rvp++)
1513 char *pvpb1[MAXATOM + 1];
1514 char *argvect[MAX_MAP_ARGS];
1515 char pvpbuf[PSBUFSIZE];
1519 if ((rvp[0][0] & 0377) == HOSTBEGIN)
1524 else if ((rvp[0][0] & 0377) == LOOKUPBEGIN)
1526 endtoken = LOOKUPEND;
1528 if (mapname == NULL)
1530 syserr("554 5.3.0 rewrite: missing mapname");
1539 ** Got a hostname/keyword lookup.
1541 ** This could be optimized fairly easily.
1544 map = stab(mapname, ST_MAP, ST_FIND);
1546 syserr("554 5.3.0 rewrite: map %s not found",
1549 /* extract the match part */
1551 if (key_rvp == NULL)
1553 syserr("554 5.3.0 rewrite: missing key for map %s",
1562 while (*rvp != NULL && ((rvp[0][0] & 0377) != endtoken))
1564 int nodetype = rvp[0][0] & 0377;
1566 if (nodetype != CANONHOST &&
1567 nodetype != CANONUSER)
1577 cataddr(xpvp, NULL, replac,
1578 &pvpbuf[sizeof(pvpbuf)] - replac,
1581 &argvect[MAX_MAP_ARGS - 1])
1582 *++arg_rvp = replac;
1583 replac += strlen(replac) + 1;
1601 cataddr(xpvp, NULL, replac,
1602 &pvpbuf[sizeof(pvpbuf)] - replac,
1604 if (arg_rvp < &argvect[MAX_MAP_ARGS - 1])
1605 *++arg_rvp = replac;
1607 if (arg_rvp >= &argvect[MAX_MAP_ARGS - 1])
1608 argvect[MAX_MAP_ARGS - 1] = NULL;
1612 /* save the remainder of the input string */
1613 trsize = (avp - rvp + 1) * sizeof(*rvp);
1614 memmove((char *) pvpb1, (char *) rvp, trsize);
1617 cataddr(key_rvp, NULL, cbuf, sizeof(cbuf),
1618 map == NULL ? '\0' : map->s_map.map_spacesub,
1621 replac = map_lookup(map, cbuf, argvect, &rstat, e);
1622 external = replac != NULL;
1624 /* if no replacement, use default */
1625 if (replac == NULL && default_rvp != NULL)
1627 /* create the default */
1628 cataddr(default_rvp, NULL, cbuf, sizeof(cbuf),
1637 else if (*replac == '\0')
1639 /* null replacement */
1645 /* scan the new replacement */
1646 xpvp = prescan(replac, '\0', pvpbuf,
1647 sizeof(pvpbuf), NULL,
1648 external ? NULL : IntTokenTab,
1652 /* prescan already printed error */
1657 /* append it to the token list */
1658 for (avp = hbrvp; *xpvp != NULL; xpvp++)
1660 *avp++ = sm_rpool_strdup_x(e->e_rpool, *xpvp);
1661 if (avp >= &npvp[maxatom])
1665 /* restore the old trailing information */
1667 for (xpvp = pvpb1; (*avp++ = *xpvp++) != NULL; )
1668 if (avp >= &npvp[maxatom])
1673 ** Check for subroutine calls.
1676 status = callsubr(npvp, reclevel, e);
1677 if (rstat == EX_OK || status == EX_TEMPFAIL)
1680 /* copy vector back into original space. */
1681 for (avp = npvp; *avp++ != NULL;)
1683 memmove((char *) pvp, (char *) npvp,
1684 (int) (avp - npvp) * sizeof(*avp));
1688 sm_dprintf("rewritten as:");
1689 printav(sm_debug_file(), pvp);
1693 if (OpMode == MD_TEST)
1695 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
1696 "%s%-16.16s returns:", prefix, rulename);
1697 printav(smioout, pvp);
1699 else if (tTd(21, 1))
1701 sm_dprintf("%s%-16.16s returns:", prefix, rulename);
1702 printav(sm_debug_file(), pvp);
1708 ** CALLSUBR -- call subroutines in rewrite vector
1711 ** pvp -- pointer to token vector.
1712 ** reclevel -- the current recursion level.
1713 ** e -- the current envelope.
1716 ** The status from the subroutine call.
1723 callsubr(pvp, reclevel, e)
1735 int subrnumber[MAX_SUBR];
1736 int subrindex[MAX_SUBR];
1741 ** Look for subroutine calls in pvp, collect them into subr*[]
1742 ** We will perform the calls in the next loop, because we will
1743 ** call the "last" subroutine first to avoid recursive calls
1744 ** and too much copying.
1747 for (avp = pvp, j = 0; *avp != NULL; avp++, j++)
1749 if ((avp[0][0] & 0377) == CALLSUBR && avp[1] != NULL)
1751 stripquotes(avp[1]);
1752 subr = strtorwset(avp[1], NULL, ST_FIND);
1755 syserr("554 5.3.5 Unknown ruleset %s", avp[1]);
1760 ** XXX instead of doing this we could optimize
1761 ** the rules after reading them: just remove
1762 ** calls to empty rulesets
1765 /* subroutine is an empty ruleset? don't call it */
1766 if (RewriteRules[subr] == NULL)
1769 sm_dprintf("-----skip subr %s (%d)\n",
1771 for (i = 2; avp[i] != NULL; i++)
1772 avp[i - 2] = avp[i];
1776 if (++nsubr >= MAX_SUBR)
1778 syserr("554 5.3.0 Too many subroutine calls (%d max)",
1782 subrnumber[nsubr] = subr;
1783 subrindex[nsubr] = j;
1788 ** Perform the actual subroutines calls, "last" one first, i.e.,
1789 ** go from the right to the left through all calls,
1790 ** do the rewriting in place.
1793 for (; nsubr > 0; nsubr--)
1795 subr = subrnumber[nsubr];
1796 avp = pvp + subrindex[nsubr];
1798 /* remove the subroutine call and name */
1799 for (i = 2; avp[i] != NULL; i++)
1800 avp[i - 2] = avp[i];
1804 ** Now we need to call the ruleset specified for
1805 ** the subroutine. We can do this in place since
1806 ** we call the "last" subroutine first.
1809 status = rewrite(avp, subr, reclevel, e,
1810 MAXATOM - subrindex[nsubr]);
1811 if (status != EX_OK && status != EX_TEMPFAIL)
1813 if (rstat == EX_OK || status == EX_TEMPFAIL)
1819 ** MAP_LOOKUP -- do lookup in map
1822 ** smap -- the map to use for the lookup.
1823 ** key -- the key to look up. [x]
1824 ** argvect -- arguments to pass to the map lookup. [x]
1825 ** pstat -- a pointer to an integer in which to store the
1826 ** status from the lookup.
1827 ** e -- the current envelope.
1830 ** The result of the lookup.
1831 ** NULL -- if there was no data for the given key.
1835 map_lookup(smap, key, argvect, pstat, e)
1842 auto int status = EX_OK;
1851 map->map_mflags |= MF_SECURE; /* default: secure */
1853 if (e->e_sendmode == SM_DEFER &&
1854 bitset(MF_DEFER, map->map_mflags))
1856 /* don't do any map lookups */
1858 sm_dprintf("map_lookup(%s, %s) => DEFERRED\n",
1860 *pstat = EX_TEMPFAIL;
1864 if (!bitset(MF_KEEPQUOTES, map->map_mflags))
1869 sm_dprintf("map_lookup(%s, ", smap->s_name);
1870 xputs(sm_debug_file(), key);
1875 for (i = 0; argvect[i] != NULL; i++)
1876 sm_dprintf(", %%%d=%s", i, argvect[i]);
1878 sm_dprintf(") => ");
1880 replac = (*map->map_class->map_lookup)(map, key, argvect, &status);
1881 if (bitset(MF_SECURE, map->map_mflags))
1882 map->map_mflags &= ~MF_SECURE;
1884 e->e_flags &= ~EF_SECURE;
1887 sm_dprintf("%s (%d), ad=%d\n",
1888 replac != NULL ? replac : "NOT FOUND",
1889 status, bitset(MF_SECURE, map->map_mflags));
1891 /* should recover if status == EX_TEMPFAIL */
1892 if (status == EX_TEMPFAIL && !bitset(MF_NODEFER, map->map_mflags))
1894 *pstat = EX_TEMPFAIL;
1896 sm_dprintf("map_lookup(%s, %s) tempfail: errno=%d\n",
1897 smap->s_name, key, errno);
1898 if (e->e_message == NULL)
1902 (void) sm_snprintf(mbuf, sizeof(mbuf),
1903 "%.80s map: lookup (%s): deferred",
1905 shortenstring(key, MAXSHORTSTR));
1906 e->e_message = sm_rpool_strdup_x(e->e_rpool, mbuf);
1909 if (status == EX_TEMPFAIL && map->map_tapp != NULL)
1911 size_t i = strlen(key) + strlen(map->map_tapp) + 1;
1912 static char *rwbuf = NULL;
1913 static size_t rwbuflen = 0;
1920 rwbuf = (char *) sm_pmalloc_x(rwbuflen);
1922 (void) sm_strlcpyn(rwbuf, rwbuflen, 2, key, map->map_tapp);
1924 sm_dprintf("map_lookup tempfail: returning \"%s\"\n",
1931 ** INITERRMAILERS -- initialize error and discard mailers
1940 ** initializes error and discard mailers.
1943 static MAILER discardmailer;
1944 static MAILER errormailer;
1945 static char *discardargv[] = { "DISCARD", NULL };
1946 static char *errorargv[] = { "ERROR", NULL };
1951 if (discardmailer.m_name == NULL)
1953 /* initialize the discard mailer */
1954 discardmailer.m_name = "*discard*";
1955 discardmailer.m_mailer = "DISCARD";
1956 discardmailer.m_argv = discardargv;
1958 if (errormailer.m_name == NULL)
1960 /* initialize the bogus mailer */
1961 errormailer.m_name = "*error*";
1962 errormailer.m_mailer = "ERROR";
1963 errormailer.m_argv = errorargv;
1967 ** BUILDADDR -- build address from token vector.
1970 ** tv -- token vector.
1971 ** a -- pointer to address descriptor to fill.
1972 ** If NULL, one will be allocated.
1973 ** flags -- info regarding whether this is a sender or
1975 ** e -- the current envelope.
1978 ** NULL if there was an error.
1985 static struct errcodes
1987 char *ec_name; /* name of error code */
1988 int ec_code; /* numeric code */
1991 { "usage", EX_USAGE },
1992 { "nouser", EX_NOUSER },
1993 { "nohost", EX_NOHOST },
1994 { "unavailable", EX_UNAVAILABLE },
1995 { "software", EX_SOFTWARE },
1996 { "tempfail", EX_TEMPFAIL },
1997 { "protocol", EX_PROTOCOL },
1998 { "config", EX_CONFIG },
1999 { NULL, EX_UNAVAILABLE }
2003 buildaddr(tv, a, flags, e)
2005 register ADDRESS *a;
2007 register ENVELOPE *e;
2009 bool tempfail = false;
2012 register struct mailer *m;
2016 char hbuf[MAXNAME + 1]; /* EAI:ok */
2017 static char ubuf[MAXNAME_I + 2];
2018 #if _FFR_8BITENVADDR
2024 sm_dprintf("buildaddr, flags=%x, tv=", flags);
2025 printav(sm_debug_file(), tv);
2030 a = (ADDRESS *) sm_rpool_malloc_x(e->e_rpool, sizeof(*a));
2031 memset((char *) a, '\0', sizeof(*a));
2034 /* set up default error return flags */
2035 a->q_flags |= DefaultNotify;
2037 /* figure out what net/mailer to use */
2038 if (*tv == NULL || (**tv & 0377) != CANONNET)
2040 syserr("554 5.3.5 buildaddr: no mailer in parsed address");
2043 ** ExitStat may have been set by an earlier map open
2044 ** failure (to a permanent error (EX_OSERR) in syserr())
2045 ** so we also need to check if this particular $#error
2046 ** return wanted a 4XX failure.
2048 ** XXX the real fix is probably to set ExitStat correctly,
2049 ** i.e., to EX_TEMPFAIL if the map open is just a temporary
2053 if (ExitStat == EX_TEMPFAIL || tempfail)
2054 a->q_state = QS_QUEUEUP;
2057 a->q_state = QS_BADADDR;
2058 a->q_mailer = &errormailer;
2065 /* extract host and user portions */
2066 if (*++tv != NULL && (**tv & 0377) == CANONHOST)
2074 while (*tv != NULL && (**tv & 0377) != CANONUSER)
2081 syserr("554 5.3.5 buildaddr: no user");
2086 else if (hostp != NULL)
2087 cataddr(hostp, tv - 1, hbuf, sizeof(hbuf), '\0', true);
2088 cataddr(++tv, NULL, ubuf, sizeof(ubuf), ' ', false);
2091 /* save away the host name */
2092 if (SM_STRCASEEQ(mname, "error"))
2094 /* Set up triplet for use by -bv */
2095 a->q_mailer = &errormailer;
2096 a->q_user = sm_rpool_strdup_x(e->e_rpool, ubuf);
2097 /* XXX wrong place? */
2101 register struct errcodes *ep;
2103 a->q_host = sm_rpool_strdup_x(e->e_rpool, hbuf);
2104 if (strchr(hbuf, '.') != NULL)
2106 a->q_status = sm_rpool_strdup_x(e->e_rpool,
2108 setstat(dsntoexitstat(hbuf));
2110 else if (isascii(hbuf[0]) && isdigit(hbuf[0]))
2112 setstat(atoi(hbuf));
2116 for (ep = ErrorCodes; ep->ec_name != NULL; ep++)
2117 if (SM_STRCASEEQ(ep->ec_name, hbuf))
2119 setstat(ep->ec_code);
2125 setstat(EX_UNAVAILABLE);
2128 if (ISSMTPCODE(ubuf) && ubuf[3] == ' ')
2133 if ((off = isenhsc(ubuf + 4, ' ')) > 0)
2135 ubuf[off + 4] = '\0';
2143 (void) sm_strlcpyn(fmt, sizeof(fmt), 2, ubuf, " %s");
2145 usrerr(fmt, ubuf + off);
2146 else if (isenhsc(hbuf, '\0') > 0)
2147 usrerrenh(hbuf, fmt, ubuf + off);
2149 usrerr(fmt, ubuf + off);
2150 /* XXX ubuf[off - 1] = ' '; */
2156 usrerr("553 5.3.0 %s", ubuf);
2161 for (mp = Mailer; (m = *mp++) != NULL; )
2163 if (SM_STRCASEEQ(m->m_name, mname))
2168 syserr("554 5.3.5 buildaddr: unknown mailer %s", mname);
2173 /* figure out what host (if any) */
2176 if (!bitnset(M_LOCALMAILER, m->m_flags))
2178 syserr("554 5.3.5 buildaddr: no host");
2184 a->q_host = sm_rpool_strdup_x(e->e_rpool, hbuf);
2186 /* figure out the user */
2188 if (bitnset(M_CHECKUDB, m->m_flags) && *p == '@')
2193 a->q_flags |= QNOTREMOTE;
2196 /* do special mapping for local mailer */
2199 if (*p == '|' && bitnset(M_CHECKPROG, m->m_flags))
2200 a->q_mailer = m = ProgMailer;
2201 else if (*p == '/' && bitnset(M_CHECKFILE, m->m_flags))
2202 a->q_mailer = m = FileMailer;
2203 else if (*p == ':' && bitnset(M_CHECKINCLUDE, m->m_flags))
2205 /* may be :include: */
2207 if (sm_strncasecmp(ubuf, ":include:", 9) == 0)
2209 /* if :include:, don't need further rewriting */
2210 a->q_mailer = m = InclMailer;
2211 a->q_user = sm_rpool_strdup_x(e->e_rpool, &ubuf[9]);
2216 /* rewrite according recipient mailer rewriting rules */
2217 #if _FFR_8BITENVADDR
2218 p = quote_internal_chars(a->q_host, NULL, &len, NULL);
2222 macdefine(&e->e_macro, A_PERM, 'h', p);
2224 if (ConfigLevel >= 10 ||
2225 !bitset(RF_SENDERADDR|RF_HEADERADDR, flags))
2227 /* sender addresses done later */
2228 (void) rewrite(tv, 2, 0, e, maxatom);
2229 if (m->m_re_rwset > 0)
2230 (void) rewrite(tv, m->m_re_rwset, 0, e, maxatom);
2232 (void) rewrite(tv, 4, 0, e, maxatom);
2234 /* save the result for the command line/RCPT argument */
2235 cataddr(tv, NULL, ubuf, sizeof(ubuf), '\0', true);
2236 a->q_user = sm_rpool_strdup_x(e->e_rpool, ubuf);
2239 ** Do mapping to lower case as requested by mailer
2242 if (a->q_host != NULL && !bitnset(M_HST_UPPER, m->m_flags))
2243 makelower_a(&a->q_host, e->e_rpool);
2244 if (!bitnset(M_USR_UPPER, m->m_flags))
2245 makelower_a(&a->q_user, e->e_rpool);
2249 sm_dprintf("buildaddr => ");
2250 printaddr(sm_debug_file(), a, false);
2256 ** CATADDR -- concatenate pieces of addresses (putting in <LWSP> subs)
2259 ** pvp -- parameter vector to rebuild. [i]
2260 ** evp -- last parameter to include. Can be NULL to
2262 ** buf -- buffer to build the string into.
2263 ** sz -- size of buf.
2264 ** spacesub -- the space separator character;
2266 ** NOSPACESEP: no separator
2267 ** external -- convert to external form?
2268 ** (undo "meta quoting")
2277 ** There are two formats for strings: internal and external.
2278 ** The external format is just an eight-bit clean string (no
2279 ** null bytes, everything else OK). The internal format can
2280 ** include sendmail metacharacters. The special character
2281 ** METAQUOTE essentially quotes the character following, stripping
2282 ** it of all special semantics.
2284 ** The cataddr routine needs to be aware of whether it is producing
2285 ** an internal or external form as output (it only takes internal
2290 cataddr(pvp, evp, buf, sz, spacesub, external)
2298 bool oatomtok, natomtok;
2301 oatomtok = natomtok = false;
2304 sm_dprintf("cataddr(%d) <==", external);
2305 printav(sm_debug_file(), pvp);
2311 if (spacesub == '\0')
2312 spacesub = SpaceSub;
2321 while (*pvp != NULL && sz > 0)
2325 natomtok = (IntTokenTab[**pvp & 0xff] == ATM);
2326 if (oatomtok && natomtok && spacesub != NOSPACESEP)
2332 for (q = *pvp; *q != '\0'; )
2341 ** If the current character (c) is METAQUOTE and we
2342 ** want the "external" form and the next character
2343 ** is not NUL, then overwrite METAQUOTE with that
2344 ** character (i.e., METAQUOTE ch is changed to ch).
2345 ** p[-1] is used because p is advanced (above).
2348 if ((c & 0377) == METAQUOTE && external && *q != '\0')
2353 oatomtok = natomtok;
2360 ** Silently truncate long strings: even though this doesn't
2361 ** seem like a good idea it is necessary because header checks
2362 ** send the whole header value to rscheck() and hence rewrite().
2363 ** The latter however sometimes uses a "short" buffer (e.g.,
2364 ** cbuf[MAXNAME + 1]) to call cataddr() which then triggers this
2365 ** error function. One possible fix to the problem is to pass
2366 ** flags to rscheck() and rewrite() to distinguish the various
2367 ** calls and only trigger the error if necessary. For now just
2368 ** undo the change from 8.13.0.
2372 usrerr("cataddr: string too long");
2377 sm_dprintf(" cataddr => %s\n", str2prt(buf));
2381 ** SAMEADDR -- Determine if two addresses are the same
2383 ** This is not just a straight comparison -- if the mailer doesn't
2384 ** care about the host we just ignore it, etc.
2387 ** a, b -- pointers to the internal forms to compare.
2390 ** true -- they represent the same mailbox.
2391 ** false -- they don't.
2399 register ADDRESS *a;
2400 register ADDRESS *b;
2402 register ADDRESS *ca, *cb;
2404 /* if they don't have the same mailer, forget it */
2405 if (a->q_mailer != b->q_mailer)
2409 ** Addresses resolving to error mailer
2410 ** should not be considered identical
2413 if (a->q_mailer == &errormailer)
2416 /* if the user isn't the same, we can drop out */
2417 if (strcmp(a->q_user, b->q_user) != 0)
2420 /* do the required flags match? */
2421 if (!ADDR_FLAGS_MATCH(a, b))
2424 /* if we have good uids for both but they differ, these are different */
2425 if (a->q_mailer == ProgMailer)
2429 if (ca != NULL && cb != NULL &&
2430 bitset(QGOODUID, ca->q_flags & cb->q_flags) &&
2431 ca->q_uid != cb->q_uid)
2435 /* otherwise compare hosts (but be careful for NULL ptrs) */
2436 if (a->q_host == b->q_host)
2438 /* probably both null pointers */
2441 if (a->q_host == NULL || b->q_host == NULL)
2443 /* only one is a null pointer */
2446 if (strcmp(a->q_host, b->q_host) != 0)
2452 ** PRINTADDR -- print address (for debugging)
2455 ** a -- the address to print
2456 ** follow -- follow the q_next chain.
2468 unsigned long qf_bit;
2471 /* :'a,.s;^#define \(Q[A-Z]*\) .*; { "\1", \1 },; */
2472 static struct qflags AddressFlags[] =
2474 { "QGOODUID", QGOODUID },
2475 { "QPRIMARY", QPRIMARY },
2476 { "QNOTREMOTE", QNOTREMOTE },
2477 { "QSELFREF", QSELFREF },
2478 { "QBOGUSSHELL", QBOGUSSHELL },
2479 { "QUNSAFEADDR", QUNSAFEADDR },
2480 { "QPINGONSUCCESS", QPINGONSUCCESS },
2481 { "QPINGONFAILURE", QPINGONFAILURE },
2482 { "QPINGONDELAY", QPINGONDELAY },
2483 { "QHASNOTIFY", QHASNOTIFY },
2484 { "QRELAYED", QRELAYED },
2485 { "QEXPANDED", QEXPANDED },
2486 { "QDELIVERED", QDELIVERED },
2487 { "QDELAYED", QDELAYED },
2488 { "QALIAS", QALIAS },
2489 { "QBYTRACE", QBYTRACE },
2490 { "QBYNDELAY", QBYNDELAY },
2491 { "QBYNRELAY", QBYNRELAY },
2492 { "QINTBCC", QINTBCC },
2493 { "QDYNMAILER", QDYNMAILER },
2494 { "QSECURE", QSECURE },
2495 { "QQUEUED", QQUEUED },
2496 { "QINTREPLY", QINTREPLY },
2497 { "QMXSECURE", QMXSECURE },
2498 { "QTHISPASS", QTHISPASS },
2499 { "QRCPTOK", QRCPTOK },
2504 printaddr(fp, a, follow)
2506 register ADDRESS *a;
2510 MAILER pseudomailer;
2511 register struct qflags *qfp;
2516 (void) sm_io_fprintf(fp, SM_TIME_DEFAULT, "[NULL]\n");
2522 (void) sm_io_fprintf(fp, SM_TIME_DEFAULT, "%p=", (void *)a);
2523 (void) sm_io_flush(fp, SM_TIME_DEFAULT);
2525 /* find the mailer -- carefully */
2534 (void) sm_io_fprintf(fp, SM_TIME_DEFAULT,
2535 "%s:\n\tmailer %d (%s), host `%s'\n",
2536 a->q_paddr == NULL ? "<null>" : a->q_paddr,
2537 m->m_mno, m->m_name,
2538 a->q_host == NULL ? "<null>" : a->q_host);
2539 (void) sm_io_fprintf(fp, SM_TIME_DEFAULT,
2540 "\tuser `%s', ruser `%s'\n",
2542 a->q_ruser == NULL ? "<null>" : a->q_ruser);
2543 (void) sm_io_fprintf(fp, SM_TIME_DEFAULT, "\tstate=");
2547 (void) sm_io_fprintf(fp, SM_TIME_DEFAULT, "OK");
2551 (void) sm_io_fprintf(fp, SM_TIME_DEFAULT,
2556 (void) sm_io_fprintf(fp, SM_TIME_DEFAULT,
2561 (void) sm_io_fprintf(fp, SM_TIME_DEFAULT,
2566 (void) sm_io_fprintf(fp, SM_TIME_DEFAULT, "RETRY");
2570 (void) sm_io_fprintf(fp, SM_TIME_DEFAULT, "SENT");
2574 (void) sm_io_fprintf(fp, SM_TIME_DEFAULT,
2579 (void) sm_io_fprintf(fp, SM_TIME_DEFAULT,
2584 (void) sm_io_fprintf(fp, SM_TIME_DEFAULT,
2589 (void) sm_io_fprintf(fp, SM_TIME_DEFAULT,
2594 (void) sm_io_fprintf(fp, SM_TIME_DEFAULT,
2599 (void) sm_io_fprintf(fp, SM_TIME_DEFAULT,
2604 (void) sm_io_fprintf(fp, SM_TIME_DEFAULT,
2609 (void) sm_io_fprintf(fp, SM_TIME_DEFAULT,
2614 (void) sm_io_fprintf(fp, SM_TIME_DEFAULT,
2619 (void) sm_io_fprintf(fp, SM_TIME_DEFAULT,
2623 (void) sm_io_fprintf(fp, SM_TIME_DEFAULT,
2624 ", next=%p, alias %p, uid %d, gid %d\n",
2625 (void *)a->q_next, (void *)a->q_alias,
2626 (int) a->q_uid, (int) a->q_gid);
2627 (void) sm_io_fprintf(fp, SM_TIME_DEFAULT, "\tflags=%lx<",
2630 for (qfp = AddressFlags; qfp->qf_name != NULL; qfp++)
2632 if (!bitset(qfp->qf_bit, a->q_flags))
2635 (void) sm_io_fprintf(fp, SM_TIME_DEFAULT,
2638 (void) sm_io_fprintf(fp, SM_TIME_DEFAULT, "%s",
2641 (void) sm_io_fprintf(fp, SM_TIME_DEFAULT, ">\n");
2642 (void) sm_io_fprintf(fp, SM_TIME_DEFAULT,
2643 "\towner=%s, home=\"%s\", fullname=\"%s\"\n",
2644 a->q_owner == NULL ? "(none)" : a->q_owner,
2645 a->q_home == NULL ? "(none)" : a->q_home,
2646 a->q_fullname == NULL ? "(none)" : a->q_fullname);
2647 (void) sm_io_fprintf(fp, SM_TIME_DEFAULT,
2648 "\torcpt=\"%s\", statmta=%s, status=%s\n",
2649 a->q_orcpt == NULL ? "(none)" : a->q_orcpt,
2650 a->q_statmta == NULL ? "(none)" : a->q_statmta,
2651 a->q_status == NULL ? "(none)" : a->q_status);
2652 (void) sm_io_fprintf(fp, SM_TIME_DEFAULT,
2653 "\tfinalrcpt=\"%s\"\n",
2654 a->q_finalrcpt == NULL ? "(none)" : a->q_finalrcpt);
2655 (void) sm_io_fprintf(fp, SM_TIME_DEFAULT,
2656 "\trstatus=\"%s\"\n",
2657 a->q_rstatus == NULL ? "(none)" : a->q_rstatus);
2658 (void) sm_io_fprintf(fp, SM_TIME_DEFAULT,
2660 a->q_statdate == 0 ? "(none)" : ctime(&a->q_statdate));
2668 ** EMPTYADDR -- return true if this address is empty (``<>'')
2671 ** a -- pointer to the address
2674 ** true -- if this address is "empty" (i.e., no one should
2675 ** ever generate replies to it.
2676 ** false -- if it is a "regular" (read: replyable) address.
2681 register ADDRESS *a;
2683 return a->q_paddr == NULL || strcmp(a->q_paddr, "<>") == 0 ||
2684 a->q_user == NULL || strcmp(a->q_user, "<>") == 0;
2688 ** REMOTENAME -- return the name relative to the current mailer
2691 ** name -- the name to translate. [i]
2692 ** m -- the mailer that we want to do rewriting relative to.
2693 ** flags -- fine tune operations.
2694 ** pstat -- pointer to status word.
2695 ** e -- the current envelope.
2698 ** the text string representing this address relative to
2699 ** the receiving mailer. [i]
2702 ** The text string returned is tucked away locally;
2703 ** copy it if you intend to save it.
2707 remotename(name, m, flags, pstat, e)
2712 register ENVELOPE *e;
2714 register char **pvp;
2715 char *SM_NONVOLATILE fancy;
2718 static char buf[MAXNAME_I + 1];
2719 char lbuf[MAXNAME_I + 1];
2720 char pvpbuf[PSBUFSIZE];
2725 sm_dprintf("remotename(");
2726 xputs(sm_debug_file(), name);
2730 /* don't do anything if we are tagging it as special */
2731 if (bitset(RF_SENDERADDR, flags))
2733 rwset = bitset(RF_HEADERADDR, flags) ? m->m_sh_rwset
2739 rwset = bitset(RF_HEADERADDR, flags) ? m->m_rh_rwset
2747 addrtype[0] = bitset(RF_HEADERADDR, flags) ? 'h' : 'e';
2748 macdefine(&e->e_macro, A_TEMP, macid("{addr_type}"), addrtype);
2751 ** Do a heuristic crack of this name to extract any comment info.
2752 ** This will leave the name as a comment and a $g macro.
2755 if (bitset(RF_CANONICAL, flags) || bitnset(M_NOCOMMENT, m->m_flags))
2758 fancy = crackaddr(name, e);
2761 ** Turn the name into canonical form.
2762 ** Normally this will be RFC 822 style, i.e., "user@domain".
2763 ** If this only resolves to "user", and the "C" flag is
2764 ** specified in the sending mailer, then the sender's
2765 ** domain will be appended.
2768 pvp = prescan(name, '\0', pvpbuf, sizeof(pvpbuf), NULL, NULL, false);
2771 if (REWRITE(pvp, 3, e) == EX_TEMPFAIL)
2772 *pstat = EX_TEMPFAIL;
2773 if (bitset(RF_ADDDOMAIN, flags) && e->e_fromdomain != NULL)
2775 /* append from domain to this address */
2776 register char **pxp = pvp;
2777 int l = MAXATOM; /* size of buffer for pvp */
2779 /* see if there is an "@domain" in the current name */
2780 while (*pxp != NULL && strcmp(*pxp, "@") != 0)
2787 /* no.... append the "@domain" from the sender */
2788 register char **qxq = e->e_fromdomain;
2790 while ((*pxp++ = *qxq++) != NULL)
2795 usrerr("553 5.1.0 remotename: too many tokens");
2796 *pstat = EX_UNAVAILABLE;
2800 if (REWRITE(pvp, 3, e) == EX_TEMPFAIL)
2801 *pstat = EX_TEMPFAIL;
2806 ** Do more specific rewriting.
2807 ** Rewrite using ruleset 1 or 2 depending on whether this is
2808 ** a sender address or not.
2809 ** Then run it through any receiving-mailer-specific rulesets.
2812 if (bitset(RF_SENDERADDR, flags))
2814 if (REWRITE(pvp, 1, e) == EX_TEMPFAIL)
2815 *pstat = EX_TEMPFAIL;
2819 if (REWRITE(pvp, 2, e) == EX_TEMPFAIL)
2820 *pstat = EX_TEMPFAIL;
2824 if (REWRITE(pvp, rwset, e) == EX_TEMPFAIL)
2825 *pstat = EX_TEMPFAIL;
2829 ** Do any final sanitation the address may require.
2830 ** This will normally be used to turn internal forms
2831 ** (e.g., user@host.LOCAL) into external form. This
2832 ** may be used as a default to the above rules.
2835 if (REWRITE(pvp, 4, e) == EX_TEMPFAIL)
2836 *pstat = EX_TEMPFAIL;
2839 ** Now restore the comment information we had at the beginning.
2842 cataddr(pvp, NULL, lbuf, sizeof(lbuf), '\0', false);
2843 oldg = macget(&e->e_macro, 'g');
2844 macset(&e->e_macro, 'g', lbuf);
2847 /* need to make sure route-addrs have <angle brackets> */
2848 if (bitset(RF_CANONICAL, flags) && lbuf[0] == '@')
2849 expand("<\201g>", buf, sizeof(buf), e);
2851 expand(fancy, buf, sizeof(buf), e);
2853 macset(&e->e_macro, 'g', oldg);
2858 sm_dprintf("remotename => `");
2859 xputs(sm_debug_file(), buf);
2860 sm_dprintf("', stat=%d\n", *pstat);
2865 ** MAPLOCALUSER -- run local username through ruleset 5 for final redirection
2868 ** a -- the address to map (but just the user name part).
2869 ** sendq -- the sendq in which to install any replacement
2871 ** aliaslevel -- the alias nesting depth.
2872 ** e -- the envelope.
2878 #define Q_COPYFLAGS (QPRIMARY|QBOGUSSHELL|QUNSAFEADDR|\
2879 Q_PINGFLAGS|QHASNOTIFY|\
2880 QRELAYED|QEXPANDED|QDELIVERED|QDELAYED|\
2881 QBYTRACE|QBYNDELAY|QBYNRELAY)
2884 maplocaluser(a, sendq, aliaslevel, e)
2885 register ADDRESS *a;
2890 register char **pvp;
2891 register ADDRESS *SM_NONVOLATILE a1 = NULL;
2893 char pvpbuf[PSBUFSIZE];
2894 #if _FFR_8BITENVADDR
2900 sm_dprintf("maplocaluser: ");
2901 printaddr(sm_debug_file(), a, false);
2903 pvp = prescan(a->q_user, '\0', pvpbuf, sizeof(pvpbuf), NULL, NULL,
2908 sm_dprintf("maplocaluser: cannot prescan %s\n",
2913 #if _FFR_8BITENVADDR
2914 p = quote_internal_chars(a->q_host, NULL, &len, NULL);
2918 macdefine(&e->e_macro, A_PERM, 'h', p);
2919 macdefine(&e->e_macro, A_PERM, 'u', a->q_user);
2920 macdefine(&e->e_macro, A_PERM, 'z', a->q_home);
2922 macdefine(&e->e_macro, A_PERM, macid("{addr_type}"), "e r");
2923 if (REWRITE(pvp, 5, e) == EX_TEMPFAIL)
2926 sm_dprintf("maplocaluser: rewrite tempfail\n");
2927 a->q_state = QS_QUEUEUP;
2928 a->q_status = "4.4.3";
2931 if (pvp[0] == NULL || (pvp[0][0] & 0377) != CANONNET)
2934 sm_dprintf("maplocaluser: doesn't resolve\n");
2939 a1 = buildaddr(pvp, NULL, 0, e);
2940 SM_EXCEPT(exc, "E:mta.quickabort")
2943 ** mark address as bad, S5 returned an error
2944 ** and we gave that back to the SMTP client.
2947 a->q_state = QS_DONTSEND;
2948 sm_exc_raisenew_x(&EtypeQuickAbort, 2);
2951 /* if non-null, mailer destination specified -- has it changed? */
2952 if (a1 == NULL || sameaddr(a, a1))
2955 sm_dprintf("maplocaluser: address unchanged\n");
2959 /* make new address take on flags and print attributes of old */
2960 a1->q_flags &= ~Q_COPYFLAGS;
2961 a1->q_flags |= a->q_flags & Q_COPYFLAGS;
2962 a1->q_paddr = sm_rpool_strdup_x(e->e_rpool, a->q_paddr);
2963 a1->q_finalrcpt = a->q_finalrcpt;
2964 a1->q_orcpt = a->q_orcpt;
2966 /* mark old address as dead; insert new address */
2967 a->q_state = QS_REPLACED;
2970 sm_dprintf("maplocaluser: QS_REPLACED ");
2971 printaddr(sm_debug_file(), a, false);
2974 allocaddr(a1, RF_COPYALL, sm_rpool_strdup_x(e->e_rpool, a->q_paddr), e);
2975 (void) recipient(a1, sendq, aliaslevel, e);
2978 ** DEQUOTE_INIT -- initialize dequote map
2981 ** map -- the internal map structure.
2982 ** args -- arguments.
2989 dequote_init(map, args)
2993 register char *p = args;
2995 /* there is no check whether there is really an argument */
2996 map->map_mflags |= MF_KEEPQUOTES;
2999 while (SM_ISSPACE(*p))
3010 map->map_mflags |= MF_DEFER;
3015 map->map_spacesub = *++p;
3018 while (*p != '\0' && !(SM_ISSPACE(*p)))
3023 if (map->map_app != NULL)
3024 map->map_app = newstr(map->map_app);
3029 ** DEQUOTE_MAP -- unquote an address
3032 ** map -- the internal map structure (ignored).
3033 ** name -- the name to dequote.
3034 ** av -- arguments (ignored).
3035 ** statp -- pointer to status out-parameter.
3038 ** NULL -- if there were no quotes, or if the resulting
3039 ** unquoted buffer would not be acceptable to prescan.
3040 ** else -- The dequoted buffer.
3045 dequote_map(map, name, av, statp)
3058 bool quotemode = false;
3059 bool bslashmode = false;
3060 char spacesub = map->map_spacesub;
3062 for (p = q = name; (c = *p++) != '\0'; )
3071 if (c == ' ' && spacesub != '\0')
3104 quotemode = !quotemode;
3113 if (anglecnt-- <= 0)
3120 if (anglecnt != 0 || cmntcnt != 0 || bslashmode ||
3121 quotemode || quotecnt <= 0 || spacecnt != 0)
3124 return map_rewrite(map, name, strlen(name), NULL);
3127 ** RSCHECK -- check string(s) for validity using rewriting sets
3130 ** rwset -- the rewriting set to use.
3131 ** p1 -- the first string to check.
3132 ** p2 -- the second string to check -- may be NULL.
3133 ** e -- the current envelope.
3134 ** flags -- control some behavior, see RSF_ in sendmail.h
3135 ** logl -- logging level.
3136 ** host -- NULL or relay host.
3137 ** logid -- id for sm_syslog.
3138 ** addr -- if not NULL and ruleset returns $#error:
3139 ** store mailer triple here.
3140 ** addrstr -- if not NULL and ruleset does not return $#:
3144 ** EX_OK -- if the rwset doesn't resolve to $#error
3145 ** or is not defined
3146 ** else -- the failure status (message printed)
3150 rscheck(rwset, p1, p2, e, flags, logl, host, logid, addr, addrstr)
3165 int volatile rstat = EX_OK;
3168 bool volatile discard = false;
3169 bool saveQuickAbort = QuickAbort;
3170 bool saveSuprErrs = SuprErrs;
3171 bool quarantine = false;
3172 char ubuf[BUFSIZ * 2];
3174 char pvpbuf[PSBUFSIZE];
3175 extern char MsgBuf[];
3178 sm_dprintf("rscheck(%s, %s, %s)\n", rwset, p1,
3179 p2 == NULL ? "(NULL)" : p2);
3181 rsno = strtorwset(rwset, NULL, ST_FIND);
3187 bufsize = strlen(p1) + strlen(p2) + 2;
3188 if (bufsize > sizeof(buf0))
3189 buf = sm_malloc_x(bufsize);
3193 bufsize = sizeof(buf0);
3195 (void) sm_snprintf(buf, bufsize, "%s%c%s", p1, CONDELSE, p2);
3199 bufsize = strlen(p1) + 1;
3200 if (bufsize > sizeof(buf0))
3201 buf = sm_malloc_x(bufsize);
3205 bufsize = sizeof(buf0);
3207 (void) sm_strlcpy(buf, p1, bufsize);
3213 pvp = prescan(buf, '\0', pvpbuf, sizeof(pvpbuf), NULL,
3214 bitset(RSF_RMCOMM, flags) ?
3215 IntTokenTab : TokTypeNoC,
3216 bitset(RSF_RMCOMM, flags) ? false : true);
3217 SuprErrs = saveSuprErrs;
3221 sm_dprintf("rscheck: cannot prescan input\n");
3223 syserr("rscheck: cannot prescan input: \"%s\"",
3224 shortenstring(buf, MAXSHORTSTR));
3229 if (bitset(RSF_UNSTRUCTURED, flags))
3231 (void) REWRITE(pvp, rsno, e);
3232 if (bitset(RSF_UNSTRUCTURED, flags))
3233 SuprErrs = saveSuprErrs;
3235 if (pvp[0] != NULL && (pvp[0][0] & 0377) != CANONNET &&
3236 bitset(RSF_ADDR, flags) && addrstr != NULL)
3238 cataddr(&(pvp[0]), NULL, ubuf, sizeof(ubuf),
3239 bitset(RSF_STRING, flags) ? NOSPACESEP : ' ',
3241 *addrstr = sm_rpool_strdup_x(e->e_rpool, ubuf);
3245 if (pvp[0] == NULL || (pvp[0][0] & 0377) != CANONNET ||
3246 pvp[1] == NULL || (strcmp(pvp[1], "error") != 0 &&
3247 strcmp(pvp[1], "discard") != 0))
3252 if (strcmp(pvp[1], "discard") == 0)
3255 sm_dprintf("rscheck: discard mailer selected\n");
3256 e->e_flags |= EF_DISCARD;
3259 else if (strcmp(pvp[1], "error") == 0 &&
3260 pvp[2] != NULL && (pvp[2][0] & 0377) == CANONHOST &&
3261 pvp[3] != NULL && strcmp(pvp[3], "quarantine") == 0)
3263 if (pvp[4] == NULL ||
3264 (pvp[4][0] & 0377) != CANONUSER ||
3266 e->e_quarmsg = sm_rpool_strdup_x(e->e_rpool,
3270 cataddr(&(pvp[5]), NULL, ubuf,
3271 sizeof(ubuf), ' ', true);
3272 e->e_quarmsg = sm_rpool_strdup_x(e->e_rpool,
3275 macdefine(&e->e_macro, A_PERM,
3276 macid("{quarantine}"), e->e_quarmsg);
3282 int savelogusrerrs = LogUsrErrs;
3283 static bool logged = false;
3285 /* got an error -- process it */
3286 saveexitstat = ExitStat;
3288 (void) buildaddr(pvp, &a1, 0, e);
3291 addr->q_mailer = a1.q_mailer;
3292 addr->q_user = a1.q_user;
3293 addr->q_host = a1.q_host;
3295 LogUsrErrs = savelogusrerrs;
3297 ExitStat = saveexitstat;
3300 if (bitset(RSF_COUNT, flags))
3301 markstats(e, &a1, STATS_REJECT);
3306 if (LogLevel > logl)
3315 (void) sm_snprintf(p, SPACELEFT(lbuf, p),
3324 relay = macvalue('_', e);
3327 (void) sm_snprintf(p, SPACELEFT(lbuf, p),
3328 ", relay=%s", relay);
3333 sm_syslog(LOG_NOTICE, logid,
3334 "ruleset=%s, arg1=%s%s, discard",
3336 else if (quarantine)
3337 sm_syslog(LOG_NOTICE, logid,
3338 "ruleset=%s, arg1=%s%s, quarantine=%s",
3339 rwset, p1, lbuf, ubuf);
3341 sm_syslog(LOG_NOTICE, logid,
3342 "ruleset=%s, arg1=%s%s, %s=%s",
3344 bitset(RSF_STATUS, flags) ? "status" : "reject",
3355 QuickAbort = saveQuickAbort;
3361 /* rulesets don't set errno */
3363 if (rstat != EX_OK && QuickAbort)
3364 sm_exc_raisenew_x(&EtypeQuickAbort, 2);
3369 ** RSCAP -- call rewriting set to return capabilities
3372 ** rwset -- the rewriting set to use.
3373 ** p1 -- the first string to check.
3374 ** p2 -- the second string to check -- may be NULL.
3375 ** e -- the current envelope.
3376 ** pvp -- pointer to token vector.
3377 ** pvpbuf -- buffer space.
3378 ** size -- size of buffer space.
3381 ** EX_UNAVAILABLE -- ruleset doesn't exist.
3382 ** EX_DATAERR -- prescan() failed.
3383 ** EX_OK -- rewrite() was successful.
3384 ** else -- return status from rewrite().
3388 rscap(rwset, p1, p2, e, pvp, pvpbuf, size)
3399 int volatile rstat = EX_OK;
3401 bool saveQuickAbort = QuickAbort;
3402 bool saveSuprErrs = SuprErrs;
3404 extern char MsgBuf[];
3407 sm_dprintf("rscap(%s, %s, %s)\n", rwset, p1,
3408 p2 == NULL ? "(NULL)" : p2);
3410 SM_REQUIRE(pvp != NULL);
3411 rsno = strtorwset(rwset, NULL, ST_FIND);
3413 return EX_UNAVAILABLE;
3417 bufsize = strlen(p1) + strlen(p2) + 2;
3418 if (bufsize > sizeof(buf0))
3419 buf = sm_malloc_x(bufsize);
3423 bufsize = sizeof(buf0);
3425 (void) sm_snprintf(buf, bufsize, "%s%c%s", p1, CONDELSE, p2);
3429 bufsize = strlen(p1) + 1;
3430 if (bufsize > sizeof(buf0))
3431 buf = sm_malloc_x(bufsize);
3435 bufsize = sizeof(buf0);
3437 (void) sm_strlcpy(buf, p1, bufsize);
3443 *pvp = prescan(buf, '\0', pvpbuf, size, NULL, IntTokenTab,
3446 rstat = rewrite(*pvp, rsno, 0, e, size);
3450 sm_dprintf("rscap: cannot prescan input\n");
3459 SuprErrs = saveSuprErrs;
3460 QuickAbort = saveQuickAbort;
3462 /* prevent information leak, this may contain rewrite error */