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>
21 static void allocaddr __P((ADDRESS *, int, char *, ENVELOPE *));
22 static int callsubr __P((char**, int, ENVELOPE *));
23 static char *map_lookup __P((STAB *, char *, char **, int *, ENVELOPE *));
24 static ADDRESS *buildaddr __P((char **, ADDRESS *, int, ENVELOPE *));
25 static bool hasctrlchar __P((register char *, bool, bool));
27 /* replacement for illegal characters in addresses */
28 #define BAD_CHAR_REPLACEMENT '?'
31 ** PARSEADDR -- Parse an address
33 ** Parses an address and breaks it up into three parts: a
34 ** net to transmit the message on, the host to transmit it
35 ** to, and a user on that host. These are loaded into an
36 ** ADDRESS header with the values squirreled away if necessary.
37 ** The "user" part may not be a real user; the process may
38 ** just reoccur on that machine. For example, on a machine
39 ** with an arpanet connection, the address
40 ** csvax.bill@berkeley
41 ** will break up to a "user" of 'csvax.bill' and a host
42 ** of 'berkeley' -- to be transmitted over the arpanet.
45 ** addr -- the address to parse.
46 ** a -- a pointer to the address descriptor buffer.
47 ** If NULL, an address will be created.
48 ** flags -- describe detail for parsing. See RF_ definitions
50 ** delim -- the character to terminate the address, passed
52 ** delimptr -- if non-NULL, set to the location of the
53 ** delim character that was found.
54 ** e -- the envelope that will contain this address.
55 ** isrcpt -- true if the address denotes a recipient; false
56 ** indicates a sender.
59 ** A pointer to the address descriptor header (`a' if
67 /* following delimiters are inherent to the internal algorithms */
68 #define DELIMCHARS "()<>,;\r\n" /* default word delimiters */
71 parseaddr(addr, a, flags, delim, delimptr, e, isrcpt)
81 auto char *delimptrbuf;
83 char pvpbuf[PSBUFSIZE];
86 ** Initialize and prescan address.
91 sm_dprintf("\n--parseaddr(%s)\n", addr);
94 delimptr = &delimptrbuf;
96 pvp = prescan(addr, delim, pvpbuf, sizeof(pvpbuf), delimptr,
101 sm_dprintf("parseaddr-->NULL\n");
105 if (invalidaddr(addr, delim == '\0' ? NULL : *delimptr, isrcpt))
108 sm_dprintf("parseaddr-->bad address\n");
113 ** Save addr if we are going to have to.
115 ** We have to do this early because there is a chance that
116 ** the map lookups in the rewriting rules could clobber
117 ** static memory somewhere.
120 if (bitset(RF_COPYPADDR, flags) && addr != NULL)
122 char savec = **delimptr;
126 e->e_to = addr = sm_rpool_strdup_x(e->e_rpool, addr);
132 ** Apply rewriting rules.
133 ** Ruleset 0 does basic parsing. It must resolve.
137 e->e_flags |= EF_SECURE;
138 if (REWRITE(pvp, 3, e) == EX_TEMPFAIL)
140 if (REWRITE(pvp, 0, e) == EX_TEMPFAIL)
144 ** Build canonical address from pvp.
147 a = buildaddr(pvp, a, flags, e);
149 if (hasctrlchar(a->q_user, isrcpt, true))
152 sm_dprintf("parseaddr-->bad q_user\n");
155 ** Just mark the address as bad so DSNs work.
156 ** hasctrlchar() has to make sure that the address
157 ** has been sanitized, e.g., shortened.
160 a->q_state = QS_BADADDR;
164 ** Make local copies of the host & user and then
165 ** transport them out.
168 allocaddr(a, flags, addr, e);
169 e->e_flags &= ~EF_SECURE;
170 if (QS_IS_BADADDR(a->q_state))
172 /* weed out bad characters in the printable address too */
173 (void) hasctrlchar(a->q_paddr, isrcpt, false);
178 ** Select a queue directory for recipient addresses.
179 ** This is done here and in split_across_queue_groups(),
180 ** but the latter applies to addresses after aliasing,
181 ** and only if splitting is done.
184 if ((a->q_qgrp == NOAQGRP || a->q_qgrp == ENVQGRP) &&
185 !bitset(RF_SENDERADDR|RF_HEADERADDR|RF_RM_ADDR, flags) &&
186 OpMode != MD_INITALIAS)
190 /* call ruleset which should return a queue group name */
191 r = rscap(RS_QUEUEGROUP, a->q_user, NULL, e, &pvp, pvpbuf,
194 pvp != NULL && pvp[0] != NULL &&
195 (pvp[0][0] & 0377) == CANONNET &&
196 pvp[1] != NULL && pvp[1][0] != '\0')
198 r = name2qid(pvp[1]);
199 if (r == NOQGRP && LogLevel > 10)
200 sm_syslog(LOG_INFO, NOQID,
201 "can't find queue group name %s, selection ignored",
203 if (tTd(20, 4) && r != NOQGRP)
204 sm_syslog(LOG_INFO, NOQID,
205 "queue group name %s -> %d",
207 a->q_qgrp = r == NOQGRP ? ENVQGRP : r;
212 ** If there was a parsing failure, mark it for queueing.
215 if (qup && OpMode != MD_INITALIAS)
217 char *msg = "Transient parse error -- message queued for future delivery";
219 if (e->e_sendmode == SM_DEFER)
220 msg = "Deferring message until queue run";
222 sm_dprintf("parseaddr: queueing message\n");
224 if (e->e_message == NULL && e->e_sendmode != SM_DEFER)
225 e->e_message = sm_rpool_strdup_x(e->e_rpool, msg);
226 a->q_state = QS_QUEUEUP;
227 a->q_status = "4.4.3";
231 ** Compute return value.
236 sm_dprintf("parseaddr-->");
237 printaddr(sm_debug_file(), a, false);
243 ** INVALIDADDR -- check for address containing characters used for macros
246 ** addr -- the address to check.
247 ** note: this is the complete address (including display part)
248 ** delimptr -- if non-NULL: end of address to check, i.e.,
249 ** a pointer in the address string.
250 ** isrcpt -- true iff the address is for a recipient.
253 ** true -- if the address has characters that are reservered
254 ** for macros or is too long.
255 ** false -- otherwise.
259 invalidaddr(addr, delimptr, isrcpt)
265 char savedelim = '\0';
269 if (delimptr != NULL)
271 /* delimptr points to the end of the address to test */
272 savedelim = *delimptr;
273 if (savedelim != '\0') /* if that isn't '\0' already: */
274 *delimptr = '\0'; /* set it */
276 for (; *addr != '\0'; addr++)
279 if (!EightBitAddrOK && (*addr & 0340) == 0200)
283 *addr = BAD_CHAR_REPLACEMENT;
286 if (++len > MAXNAME - 1)
291 usrerr("553 5.1.0 Address \"%s\" too long (%d bytes max)",
301 usrerr("501 5.1.3 8-bit character in mailbox address \"%s\"",
304 usrerr("501 5.1.7 8-bit character in mailbox address \"%s\"",
308 if (delimptr != NULL && savedelim != '\0')
309 *delimptr = savedelim; /* restore old character at delimptr */
313 ** HASCTRLCHAR -- check for address containing meta-characters
315 ** Checks that the address contains no meta-characters, and contains
316 ** no "non-printable" characters unless they are quoted or escaped.
317 ** Quoted or escaped characters are literals.
320 ** addr -- the address to check.
321 ** isrcpt -- true if the address is for a recipient; false
323 ** complain -- true if an error should issued if the address
324 ** is invalid and should be "repaired".
327 ** true -- if the address has any "weird" characters or
328 ** non-printable characters or if a quote is unbalanced.
329 ** false -- otherwise.
333 hasctrlchar(addr, isrcpt, complain)
335 bool isrcpt, complain;
344 for (; *addr != '\0'; addr++)
346 if (++len > MAXNAME - 1)
350 (void) shorten_rfc822_string(b, MAXNAME - 1);
351 usrerr("553 5.1.0 Address \"%s\" too long (%d bytes max)",
357 if (!quoted && ((unsigned char)*addr < 32 || *addr == 127))
359 result = "non-printable character";
360 *addr = BAD_CHAR_REPLACEMENT;
365 else if (*addr == '\\')
367 /* XXX Generic problem: no '\0' in strings. */
370 result = "trailing \\ character";
371 *--addr = BAD_CHAR_REPLACEMENT;
376 if (!EightBitAddrOK && (*addr & 0340) == 0200)
379 result = "8-bit character";
380 *addr = BAD_CHAR_REPLACEMENT;
386 result = "unbalanced quote"; /* unbalanced quote */
387 if (result != NULL && complain)
390 usrerr("501 5.1.3 Syntax error in mailbox address \"%s\" (%s)",
393 usrerr("501 5.1.7 Syntax error in mailbox address \"%s\" (%s)",
396 return result != NULL;
399 ** ALLOCADDR -- do local allocations of address on demand.
401 ** Also lowercases the host name if requested.
404 ** a -- the address to reallocate.
405 ** flags -- the copy flag (see RF_ definitions in sendmail.h
406 ** for a description).
407 ** paddr -- the printname of the address.
414 ** Copies portions of a into local buffers as requested.
418 allocaddr(a, flags, paddr, e)
425 sm_dprintf("allocaddr(flags=%x, paddr=%s, ad=%d)\n", flags, paddr, bitset(EF_SECURE, e->e_flags));
429 if (a->q_user == NULL)
431 if (a->q_host == NULL)
434 if (bitset(EF_SECURE, e->e_flags))
435 a->q_flags |= QSECURE;
437 if (bitset(RF_COPYPARSE, flags))
439 a->q_host = sm_rpool_strdup_x(e->e_rpool, a->q_host);
440 if (a->q_user != a->q_paddr)
441 a->q_user = sm_rpool_strdup_x(e->e_rpool, a->q_user);
444 if (a->q_paddr == NULL)
445 a->q_paddr = sm_rpool_strdup_x(e->e_rpool, a->q_user);
450 ** PRESCAN -- Prescan name and make it canonical
452 ** Scans a name and turns it into a set of tokens. This process
453 ** deletes blanks and comments (in parentheses) (if the token type
454 ** for left paren is SPC).
456 ** This routine knows about quoted strings and angle brackets.
458 ** There are certain subtleties to this routine. The one that
459 ** comes to mind now is that backslashes on the ends of names
460 ** are silently stripped off; this is intentional. The problem
461 ** is that some versions of sndmsg (like at LBL) set the kill
462 ** character to something other than @ when reading addresses;
463 ** so people type "csvax.eric\@berkeley" -- which screws up the
467 ** addr -- the name to chomp.
468 ** delim -- the delimiter for the address, normally
469 ** '\0' or ','; \0 is accepted in any case.
470 ** If '\t' then we are reading the .cf file.
471 ** pvpbuf -- place to put the saved text -- note that
472 ** the pointers are static.
473 ** pvpbsize -- size of pvpbuf.
474 ** delimptr -- if non-NULL, set to the location of the
475 ** terminating delimiter.
476 ** toktab -- if set, a token table to use for parsing.
477 ** If NULL, use the default table.
478 ** ignore -- if true, ignore unbalanced addresses
481 ** A pointer to a vector of tokens.
485 /* states and character types */
486 #define OPR 0 /* operator */
487 #define ATM 1 /* atom */
488 #define QST 2 /* in quoted string */
489 #define SPC 3 /* chewing up spaces */
490 #define ONE 4 /* pick up one character */
491 #define ILL 5 /* illegal character */
493 #define NSTATES 6 /* number of states */
494 #define TYPE 017 /* mask to select state type */
496 /* meta bits for table */
497 #define M 020 /* meta character; don't pass through */
498 #define B 040 /* cause a break */
499 #define MB M|B /* meta-break */
501 static short StateTab[NSTATES][NSTATES] =
503 /* oldst chtype> OPR ATM QST SPC ONE ILL */
504 /*OPR*/ { OPR|B, ATM|B, QST|B, SPC|MB, ONE|B, ILL|MB },
505 /*ATM*/ { OPR|B, ATM, QST|B, SPC|MB, ONE|B, ILL|MB },
506 /*QST*/ { QST, QST, OPR, QST, QST, QST },
507 /*SPC*/ { OPR, ATM, QST, SPC|M, ONE, ILL|MB },
508 /*ONE*/ { OPR, OPR, OPR, OPR, OPR, ILL|MB },
509 /*ILL*/ { OPR|B, ATM|B, QST|B, SPC|MB, ONE|B, ILL|M }
512 /* these all get modified with the OperatorChars */
514 /* token type table for external strings */
515 unsigned char ExtTokenTab[256] =
517 /* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */
518 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,SPC,SPC,SPC,SPC,SPC,ATM,ATM,
519 /* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */
520 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
521 /* sp ! " # $ % & ' ( ) * + , - . / */
522 SPC,ATM,QST,ATM,ATM,ATM,ATM,ATM, SPC,SPC,ATM,ATM,ATM,ATM,ATM,ATM,
523 /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */
524 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
525 /* @ A B C D E F G H I J K L M N O */
526 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
527 /* P Q R S T U V W X Y Z [ \ ] ^ _ */
528 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
529 /* ` a b c d e f g h i j k l m n o */
530 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
531 /* p q r s t u v w x y z { | } ~ del */
532 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
534 /* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */
535 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
536 /* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */
537 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
538 /* sp ! " # $ % & ' ( ) * + , - . / */
539 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
540 /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */
541 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
542 /* @ A B C D E F G H I J K L M N O */
543 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
544 /* P Q R S T U V W X Y Z [ \ ] ^ _ */
545 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
546 /* ` a b c d e f g h i j k l m n o */
547 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
548 /* p q r s t u v w x y z { | } ~ del */
549 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM
552 /* token type table for internal strings */
553 unsigned char IntTokenTab[256] =
555 /* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */
556 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,SPC,SPC,SPC,SPC,SPC,ATM,ATM,
557 /* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */
558 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
559 /* sp ! " # $ % & ' ( ) * + , - . / */
560 SPC,ATM,QST,ATM,ATM,ATM,ATM,ATM, SPC,SPC,ATM,ATM,ATM,ATM,ATM,ATM,
561 /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */
562 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
563 /* @ A B C D E F G H I J K L M N O */
564 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
565 /* P Q R S T U V W X Y Z [ \ ] ^ _ */
566 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
567 /* ` a b c d e f g h i j k l m n o */
568 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
569 /* p q r s t u v w x y z { | } ~ del */
570 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
572 /* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */
573 OPR,OPR,ONE,OPR,OPR,OPR,OPR,OPR, OPR,OPR,OPR,OPR,OPR,OPR,OPR,OPR,
574 /* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */
575 OPR,OPR,OPR,ONE,ONE,ONE,OPR,OPR, OPR,OPR,OPR,OPR,OPR,OPR,OPR,OPR,
576 /* sp ! " # $ % & ' ( ) * + , - . / */
577 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
578 /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */
579 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
580 /* @ A B C D E F G H I J K L M N O */
581 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
582 /* P Q R S T U V W X Y Z [ \ ] ^ _ */
583 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
584 /* ` a b c d e f g h i j k l m n o */
585 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
586 /* p q r s t u v w x y z { | } ~ del */
587 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ONE
590 /* token type table for MIME parsing */
591 unsigned char MimeTokenTab[256] =
593 /* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */
594 ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL, ILL,SPC,SPC,SPC,SPC,SPC,ILL,ILL,
595 /* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */
596 ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL, ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL,
597 /* sp ! " # $ % & ' ( ) * + , - . / */
598 SPC,ATM,QST,ATM,ATM,ATM,ATM,ATM, SPC,SPC,ATM,ATM,OPR,ATM,ATM,OPR,
599 /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */
600 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,OPR,OPR,OPR,OPR,OPR,OPR,
601 /* @ A B C D E F G H I J K L M N O */
602 OPR,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
603 /* P Q R S T U V W X Y Z [ \ ] ^ _ */
604 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,OPR,OPR,OPR,ATM,ATM,
605 /* ` a b c d e f g h i j k l m n o */
606 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
607 /* p q r s t u v w x y z { | } ~ del */
608 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
610 /* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */
611 ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL, ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL,
612 /* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */
613 ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL, ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL,
614 /* sp ! " # $ % & ' ( ) * + , - . / */
615 ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL, ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL,
616 /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */
617 ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL, ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL,
618 /* @ A B C D E F G H I J K L M N O */
619 ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL, ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL,
620 /* P Q R S T U V W X Y Z [ \ ] ^ _ */
621 ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL, ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL,
622 /* ` a b c d e f g h i j k l m n o */
623 ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL, ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL,
624 /* p q r s t u v w x y z { | } ~ del */
625 ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL, ILL,ILL,ILL,ILL,ILL,ILL,ILL,ONE
628 /* token type table: don't strip comments */
629 unsigned char TokTypeNoC[256] =
631 /* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */
632 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,SPC,SPC,SPC,SPC,SPC,ATM,ATM,
633 /* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */
634 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
635 /* sp ! " # $ % & ' ( ) * + , - . / */
636 SPC,ATM,QST,ATM,ATM,ATM,ATM,ATM, OPR,OPR,ATM,ATM,ATM,ATM,ATM,ATM,
637 /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */
638 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
639 /* @ A B C D E F G H I J K L M N O */
640 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
641 /* P Q R S T U V W X Y Z [ \ ] ^ _ */
642 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
643 /* ` a b c d e f g h i j k l m n o */
644 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
645 /* p q r s t u v w x y z { | } ~ del */
646 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
648 /* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */
649 OPR,OPR,ONE,OPR,OPR,OPR,OPR,OPR, OPR,OPR,OPR,OPR,OPR,OPR,OPR,OPR,
650 /* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */
651 OPR,OPR,OPR,ONE,ONE,ONE,OPR,OPR, OPR,OPR,OPR,OPR,OPR,OPR,OPR,OPR,
652 /* sp ! " # $ % & ' ( ) * + , - . / */
653 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
654 /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */
655 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
656 /* @ A B C D E F G H I J K L M N O */
657 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
658 /* P Q R S T U V W X Y Z [ \ ] ^ _ */
659 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
660 /* ` a b c d e f g h i j k l m n o */
661 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
662 /* p q r s t u v w x y z { | } ~ del */
663 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ONE
667 #define NOCHAR (-1) /* signal nothing in lookahead token */
670 prescan(addr, delim, pvpbuf, pvpbsize, delimptr, toktab, ignore)
676 unsigned char *toktab;
690 char *saveto = CurEnv->e_to;
691 static char *av[MAXATOM + 1];
692 static bool firsttime = true;
696 /* initialize the token type table */
700 if (OperatorChars == NULL)
703 OperatorChars = macvalue('o', CurEnv);
704 if (OperatorChars == NULL)
705 OperatorChars = ".:@[]";
707 expand(OperatorChars, obuf, sizeof(obuf) - sizeof(DELIMCHARS),
709 (void) sm_strlcat(obuf, DELIMCHARS, sizeof(obuf));
710 for (p = obuf; *p != '\0'; p++)
712 if (IntTokenTab[*p & 0xff] == ATM)
713 IntTokenTab[*p & 0xff] = OPR;
714 if (ExtTokenTab[*p & 0xff] == ATM)
715 ExtTokenTab[*p & 0xff] = OPR;
716 if (TokTypeNoC[*p & 0xff] == ATM)
717 TokTypeNoC[*p & 0xff] = OPR;
721 toktab = ExtTokenTab;
723 /* make sure error messages don't have garbage on them */
728 route_syntax = false;
738 sm_dprintf("prescan: ");
739 xputs(sm_debug_file(), p);
749 /* store away any old lookahead character */
750 if (c != NOCHAR && !bslashmode)
752 /* see if there is room */
753 if (q >= &pvpbuf[pvpbsize - 5])
756 usrerr("553 5.1.1 Address too long");
757 if (strlen(addr) > MAXNAME)
758 addr[MAXNAME] = '\0';
760 if (delimptr != NULL)
766 CurEnv->e_to = saveto;
770 /* squirrel it away */
772 if ((char) c == (char) -1 && !tTd(82, 101) &&
775 #endif /* !ALLOW_255 */
779 /* read a new input character */
783 /* diagnose and patch up bad syntax */
786 else if (state == QST)
788 usrerr("553 Unbalanced '\"'");
791 else if (cmntcnt > 0)
793 usrerr("553 Unbalanced '('");
796 else if (anglecnt > 0)
799 usrerr("553 Unbalanced '<'");
806 else if (c == delim && cmntcnt <= 0 && state != QST)
811 /* special case for better error management */
812 if (delim == ',' && !route_syntax && !ignore)
814 usrerr("553 Unbalanced '<'");
821 sm_dprintf("c=%c, s=%d; ", c, state);
823 /* chew up special characters */
829 /* kludge \! for naive users */
835 else if (c != '!' || state == QST)
837 /* see if there is room */
838 if (q >= &pvpbuf[pvpbsize - 5])
849 else if (state == QST)
852 /* do nothing, just avoid next clauses */
854 else if (c == '(' && toktab['('] == SPC)
859 else if (c == ')' && toktab['('] == SPC)
865 usrerr("553 Unbalanced ')'");
872 else if (cmntcnt > 0)
881 while (SM_ISSPACE(*ptr))
892 usrerr("553 Unbalanced '>'");
898 route_syntax = false;
900 else if (delim == ' ' && SM_ISSPACE(c))
906 /* see if this is end of input */
907 if (c == delim && anglecnt <= 0 && state != QST)
910 newstate = StateTab[state][toktab[c & 0xff]];
912 sm_dprintf("ns=%02o\n", newstate);
913 state = newstate & TYPE;
916 if (isascii(c) && isprint(c))
917 usrerr("553 Illegal character %c", c);
919 usrerr("553 Illegal character 0x%02x",
922 if (bitset(M, newstate))
924 if (bitset(B, newstate))
931 /* see if there is room */
932 if (q >= &pvpbuf[pvpbsize - 5])
938 xputs(sm_debug_file(), tok);
941 if (avp >= &av[MAXATOM])
943 usrerr("553 5.1.0 prescan: too many tokens");
946 if (q - tok > MAXNAME)
948 usrerr("553 5.1.0 prescan: token too long");
953 } while (c != '\0' && (c != delim || anglecnt > 0));
955 if (delimptr != NULL)
963 sm_dprintf("prescan==>");
964 printav(sm_debug_file(), av);
966 CurEnv->e_to = saveto;
970 sm_dprintf("prescan: null leading token\n");
976 ** REWRITE -- apply rewrite rules to token vector.
978 ** This routine is an ordered production system. Each rewrite
979 ** rule has a LHS (called the pattern) and a RHS (called the
980 ** rewrite); 'rwr' points the the current rewrite rule.
982 ** For each rewrite rule, 'avp' points the address vector we
983 ** are trying to match against, and 'pvp' points to the pattern.
984 ** If pvp points to a special match value (MATCHZANY, MATCHANY,
985 ** MATCHONE, MATCHCLASS, MATCHNCLASS) then the address in avp
986 ** matched is saved away in the match vector (pointed to by 'mvp').
988 ** When a match between avp & pvp does not match, we try to
989 ** back out. If we back up over MATCHONE, MATCHCLASS, or MATCHNCLASS
990 ** we must also back out the match in mvp. If we reach a
991 ** MATCHANY or MATCHZANY we just extend the match and start
994 ** When we finally match, we rewrite the address vector
995 ** and try over again.
998 ** pvp -- pointer to token vector.
999 ** ruleset -- the ruleset to use for rewriting.
1000 ** reclevel -- recursion level (to catch loops).
1001 ** e -- the current envelope.
1002 ** maxatom -- maximum length of buffer (usually MAXATOM)
1005 ** A status code. If EX_TEMPFAIL, higher level code should
1006 ** attempt recovery.
1014 char **match_first; /* first token matched */
1015 char **match_last; /* last token matched */
1016 char **match_pattern; /* pointer to pattern */
1020 rewrite(pvp, ruleset, reclevel, e, maxatom)
1024 register ENVELOPE *e;
1027 register char *ap; /* address pointer */
1028 register char *rp; /* rewrite pointer */
1029 register char *rulename; /* ruleset name */
1030 register char *prefix;
1031 register char **avp; /* address vector pointer */
1032 register char **rvp; /* rewrite vector pointer */
1033 register struct match *mlp; /* cur ptr into mlist */
1034 register struct rewrite *rwr; /* pointer to current rewrite rule */
1035 int ruleno; /* current rule number */
1036 int rstat = EX_OK; /* return status */
1038 struct match mlist[MAXMATCH]; /* stores match on LHS */
1039 char *npvp[MAXATOM + 1]; /* temporary space for rebuild */
1044 ** mlp will not exceed mlist[] because readcf enforces
1045 ** the upper limit of entries when reading rulesets.
1048 if (ruleset < 0 || ruleset >= MAXRWSETS)
1050 syserr("554 5.3.5 rewrite: illegal ruleset number %d", ruleset);
1053 rulename = RuleSetNames[ruleset];
1054 if (rulename == NULL)
1056 (void) sm_snprintf(name, sizeof(name), "%d", ruleset);
1059 if (OpMode == MD_TEST)
1062 prefix = "rewrite: ruleset ";
1063 if (OpMode == MD_TEST)
1065 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
1066 "%s%-16.16s input:", prefix, rulename);
1067 printav(smioout, pvp);
1069 else if (tTd(21, 1))
1071 sm_dprintf("%s%-16.16s input:", prefix, rulename);
1072 printav(sm_debug_file(), pvp);
1074 if (reclevel++ > MaxRuleRecursion)
1076 syserr("rewrite: excessive recursion (max %d), ruleset %s",
1077 MaxRuleRecursion, rulename);
1086 ** Run through the list of rewrite rules, applying
1092 for (rwr = RewriteRules[ruleset]; rwr != NULL; )
1096 /* if already canonical, quit now */
1097 if (pvp[0] != NULL && (pvp[0][0] & 0377) == CANONNET)
1103 sm_dprintf("-----trying rule (line %d):",
1106 sm_dprintf("-----trying rule:");
1107 printav(sm_debug_file(), rwr->r_lhs);
1110 /* try to match on this rule */
1114 if (++loopcount > 100)
1116 syserr("554 5.3.5 Infinite loop in ruleset %s, rule %d",
1120 sm_dprintf("workspace: ");
1121 printav(sm_debug_file(), pvp);
1126 while ((ap = *avp) != NULL || *rvp != NULL)
1131 sm_dprintf("ADVANCE rp=");
1132 xputs(sm_debug_file(), rp);
1133 sm_dprintf(", ap=");
1134 xputs(sm_debug_file(), ap);
1139 /* end-of-pattern before end-of-address */
1143 (rp[0] & 0377) != MATCHZANY &&
1144 (rp[0] & 0377) != MATCHZERO)
1146 /* end-of-input with patterns left */
1150 switch (rp[0] & 0377)
1153 /* match any phrase in a class */
1154 mlp->match_pattern = rvp;
1155 mlp->match_first = avp;
1160 mlp->match_last = avp++;
1161 cataddr(mlp->match_first, mlp->match_last,
1162 buf, sizeof(buf), '\0', true);
1163 if (!wordinclass(buf, rp[1]))
1167 sm_dprintf("EXTEND rp=");
1168 xputs(sm_debug_file(), rp);
1169 sm_dprintf(", ap=");
1170 xputs(sm_debug_file(), ap);
1176 sm_dprintf("CLMATCH\n");
1181 /* match any token not in a class */
1182 if (wordinclass(ap, rp[1]))
1189 /* match exactly one token */
1190 mlp->match_pattern = rvp;
1191 mlp->match_first = avp;
1192 mlp->match_last = avp++;
1197 /* match zero or more tokens */
1198 mlp->match_pattern = rvp;
1199 mlp->match_first = avp;
1200 mlp->match_last = avp - 1;
1205 /* match zero tokens */
1210 ** Match against run-time macro.
1211 ** This algorithm is broken for the
1212 ** general case (no recursive macros,
1213 ** improper tokenization) but should
1214 ** work for the usual cases.
1217 ap = macvalue(rp[1], e);
1218 mlp->match_first = avp;
1220 sm_dprintf("rewrite: LHS $&{%s} => \"%s\"\n",
1222 ap == NULL ? "(NULL)" : ap);
1229 sm_strncasecmp(ap, *avp,
1233 avp = mlp->match_first;
1236 ap += strlen(*avp++);
1243 /* must have exact match */
1244 if (sm_strcasecmp(rp, ap))
1250 /* successful match on this token */
1255 /* match failed -- back up */
1256 while (--mlp >= mlist)
1258 rvp = mlp->match_pattern;
1260 avp = mlp->match_last + 1;
1265 sm_dprintf("BACKUP rp=");
1266 xputs(sm_debug_file(), rp);
1267 sm_dprintf(", ap=");
1268 xputs(sm_debug_file(), ap);
1274 /* run off the end -- back up again */
1278 if ((rp[0] & 0377) == MATCHANY ||
1279 (rp[0] & 0377) == MATCHZANY)
1281 /* extend binding and continue */
1282 mlp->match_last = avp++;
1287 if ((rp[0] & 0377) == MATCHCLASS)
1289 /* extend binding and try again */
1290 mlp->match_last = avp;
1297 /* total failure to match */
1303 ** See if we successfully matched
1306 if (mlp < mlist || *rvp != NULL)
1309 sm_dprintf("----- rule fails\n");
1319 sm_dprintf("-----rule matches:");
1320 printav(sm_debug_file(), rvp);
1326 if ((rp[0] & 0377) == CANONUSER)
1333 else if ((rp[0] & 0377) == CANONHOST)
1341 for (avp = npvp; *rvp != NULL; rvp++)
1343 register struct match *m;
1347 if ((rp[0] & 0377) == MATCHREPL)
1349 /* substitute from LHS */
1350 m = &mlist[rp[1] - '1'];
1351 if (m < mlist || m >= mlp)
1353 syserr("554 5.3.5 rewrite: ruleset %s: replacement $%c out of bounds",
1359 sm_dprintf("$%c:", rp[1]);
1360 pp = m->match_first;
1361 while (pp <= m->match_last)
1363 sm_dprintf(" %p=\"", (void *)*pp);
1365 sm_dprintf("%s\"", *pp++);
1369 pp = m->match_first;
1370 while (pp <= m->match_last)
1372 if (avp >= &npvp[maxatom])
1379 /* some sort of replacement */
1380 if (avp >= &npvp[maxatom])
1383 syserr("554 5.3.0 rewrite: expansion too long");
1385 sm_syslog(LOG_ERR, e->e_id,
1386 "rewrite: expansion too long, ruleset=%s, ruleno=%d",
1390 if ((rp[0] & 0377) != MACRODEXPAND)
1392 /* vanilla replacement from RHS */
1397 /* $&{x} replacement */
1398 char *mval = macvalue(rp[1], e);
1401 static size_t pvpb1_size = 0;
1402 static char **pvpb1 = NULL;
1403 char pvpbuf[PSBUFSIZE];
1406 sm_dprintf("rewrite: RHS $&{%s} => \"%s\"\n",
1408 mval == NULL ? "(NULL)" : mval);
1409 if (mval == NULL || *mval == '\0')
1412 /* save the remainder of the input */
1413 for (xpvp = pvp; *xpvp != NULL; xpvp++)
1414 trsize += sizeof(*xpvp);
1415 if (trsize > pvpb1_size)
1420 sm_pmalloc_x(trsize);
1421 pvpb1_size = trsize;
1424 memmove((char *) pvpb1,
1428 /* scan the new replacement */
1429 xpvp = prescan(mval, '\0', pvpbuf,
1430 sizeof(pvpbuf), NULL,
1434 /* prescan pre-printed error */
1438 /* insert it into the output stream */
1439 while (*xpvp != NULL)
1442 sm_dprintf(" ... %s\n",
1444 *avp++ = sm_rpool_strdup_x(
1446 if (avp >= &npvp[maxatom])
1451 sm_dprintf(" ... DONE\n");
1453 /* restore the old trailing input */
1454 memmove((char *) pvp,
1463 ** Check for any hostname/keyword lookups.
1466 for (rvp = npvp; *rvp != NULL; rvp++)
1480 char *pvpb1[MAXATOM + 1];
1481 char *argvect[MAX_MAP_ARGS];
1482 char pvpbuf[PSBUFSIZE];
1486 if ((rvp[0][0] & 0377) == HOSTBEGIN)
1491 else if ((rvp[0][0] & 0377) == LOOKUPBEGIN)
1493 endtoken = LOOKUPEND;
1495 if (mapname == NULL)
1497 syserr("554 5.3.0 rewrite: missing mapname");
1506 ** Got a hostname/keyword lookup.
1508 ** This could be optimized fairly easily.
1511 map = stab(mapname, ST_MAP, ST_FIND);
1513 syserr("554 5.3.0 rewrite: map %s not found",
1516 /* extract the match part */
1518 if (key_rvp == NULL)
1520 syserr("554 5.3.0 rewrite: missing key for map %s",
1529 while (*rvp != NULL && ((rvp[0][0] & 0377) != endtoken))
1531 int nodetype = rvp[0][0] & 0377;
1533 if (nodetype != CANONHOST &&
1534 nodetype != CANONUSER)
1544 cataddr(xpvp, NULL, replac,
1545 &pvpbuf[sizeof(pvpbuf)] - replac,
1548 &argvect[MAX_MAP_ARGS - 1])
1549 *++arg_rvp = replac;
1550 replac += strlen(replac) + 1;
1568 cataddr(xpvp, NULL, replac,
1569 &pvpbuf[sizeof(pvpbuf)] - replac,
1571 if (arg_rvp < &argvect[MAX_MAP_ARGS - 1])
1572 *++arg_rvp = replac;
1574 if (arg_rvp >= &argvect[MAX_MAP_ARGS - 1])
1575 argvect[MAX_MAP_ARGS - 1] = NULL;
1579 /* save the remainder of the input string */
1580 trsize = (avp - rvp + 1) * sizeof(*rvp);
1581 memmove((char *) pvpb1, (char *) rvp, trsize);
1584 cataddr(key_rvp, NULL, cbuf, sizeof(cbuf),
1585 map == NULL ? '\0' : map->s_map.map_spacesub,
1588 replac = map_lookup(map, cbuf, argvect, &rstat, e);
1589 external = replac != NULL;
1591 /* if no replacement, use default */
1592 if (replac == NULL && default_rvp != NULL)
1594 /* create the default */
1595 cataddr(default_rvp, NULL, cbuf, sizeof(cbuf),
1604 else if (*replac == '\0')
1606 /* null replacement */
1612 /* scan the new replacement */
1613 xpvp = prescan(replac, '\0', pvpbuf,
1614 sizeof(pvpbuf), NULL,
1615 external ? NULL : IntTokenTab,
1619 /* prescan already printed error */
1624 /* append it to the token list */
1625 for (avp = hbrvp; *xpvp != NULL; xpvp++)
1627 *avp++ = sm_rpool_strdup_x(e->e_rpool, *xpvp);
1628 if (avp >= &npvp[maxatom])
1632 /* restore the old trailing information */
1634 for (xpvp = pvpb1; (*avp++ = *xpvp++) != NULL; )
1635 if (avp >= &npvp[maxatom])
1640 ** Check for subroutine calls.
1643 status = callsubr(npvp, reclevel, e);
1644 if (rstat == EX_OK || status == EX_TEMPFAIL)
1647 /* copy vector back into original space. */
1648 for (avp = npvp; *avp++ != NULL;)
1650 memmove((char *) pvp, (char *) npvp,
1651 (int) (avp - npvp) * sizeof(*avp));
1655 sm_dprintf("rewritten as:");
1656 printav(sm_debug_file(), pvp);
1660 if (OpMode == MD_TEST)
1662 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
1663 "%s%-16.16s returns:", prefix, rulename);
1664 printav(smioout, pvp);
1666 else if (tTd(21, 1))
1668 sm_dprintf("%s%-16.16s returns:", prefix, rulename);
1669 printav(sm_debug_file(), pvp);
1674 ** CALLSUBR -- call subroutines in rewrite vector
1677 ** pvp -- pointer to token vector.
1678 ** reclevel -- the current recursion level.
1679 ** e -- the current envelope.
1682 ** The status from the subroutine call.
1689 callsubr(pvp, reclevel, e)
1701 int subrnumber[MAX_SUBR];
1702 int subrindex[MAX_SUBR];
1707 ** Look for subroutine calls in pvp, collect them into subr*[]
1708 ** We will perform the calls in the next loop, because we will
1709 ** call the "last" subroutine first to avoid recursive calls
1710 ** and too much copying.
1713 for (avp = pvp, j = 0; *avp != NULL; avp++, j++)
1715 if ((avp[0][0] & 0377) == CALLSUBR && avp[1] != NULL)
1717 stripquotes(avp[1]);
1718 subr = strtorwset(avp[1], NULL, ST_FIND);
1721 syserr("554 5.3.5 Unknown ruleset %s", avp[1]);
1726 ** XXX instead of doing this we could optimize
1727 ** the rules after reading them: just remove
1728 ** calls to empty rulesets
1731 /* subroutine is an empty ruleset? don't call it */
1732 if (RewriteRules[subr] == NULL)
1735 sm_dprintf("-----skip subr %s (%d)\n",
1737 for (i = 2; avp[i] != NULL; i++)
1738 avp[i - 2] = avp[i];
1742 if (++nsubr >= MAX_SUBR)
1744 syserr("554 5.3.0 Too many subroutine calls (%d max)",
1748 subrnumber[nsubr] = subr;
1749 subrindex[nsubr] = j;
1754 ** Perform the actual subroutines calls, "last" one first, i.e.,
1755 ** go from the right to the left through all calls,
1756 ** do the rewriting in place.
1759 for (; nsubr > 0; nsubr--)
1761 subr = subrnumber[nsubr];
1762 avp = pvp + subrindex[nsubr];
1764 /* remove the subroutine call and name */
1765 for (i = 2; avp[i] != NULL; i++)
1766 avp[i - 2] = avp[i];
1770 ** Now we need to call the ruleset specified for
1771 ** the subroutine. We can do this in place since
1772 ** we call the "last" subroutine first.
1775 status = rewrite(avp, subr, reclevel, e,
1776 MAXATOM - subrindex[nsubr]);
1777 if (status != EX_OK && status != EX_TEMPFAIL)
1779 if (rstat == EX_OK || status == EX_TEMPFAIL)
1785 ** MAP_LOOKUP -- do lookup in map
1788 ** smap -- the map to use for the lookup.
1789 ** key -- the key to look up.
1790 ** argvect -- arguments to pass to the map lookup.
1791 ** pstat -- a pointer to an integer in which to store the
1792 ** status from the lookup.
1793 ** e -- the current envelope.
1796 ** The result of the lookup.
1797 ** NULL -- if there was no data for the given key.
1801 map_lookup(smap, key, argvect, pstat, e)
1808 auto int status = EX_OK;
1817 map->map_mflags |= MF_SECURE; /* default: secure */
1819 if (e->e_sendmode == SM_DEFER &&
1820 bitset(MF_DEFER, map->map_mflags))
1822 /* don't do any map lookups */
1824 sm_dprintf("map_lookup(%s, %s) => DEFERRED\n",
1826 *pstat = EX_TEMPFAIL;
1830 if (!bitset(MF_KEEPQUOTES, map->map_mflags))
1835 sm_dprintf("map_lookup(%s, ", smap->s_name);
1836 xputs(sm_debug_file(), key);
1841 for (i = 0; argvect[i] != NULL; i++)
1842 sm_dprintf(", %%%d=%s", i, argvect[i]);
1844 sm_dprintf(") => ");
1846 replac = (*map->map_class->map_lookup)(map, key, argvect, &status);
1847 if (bitset(MF_SECURE, map->map_mflags))
1848 map->map_mflags &= ~MF_SECURE;
1850 e->e_flags &= ~EF_SECURE;
1853 sm_dprintf("%s (%d), ad=%d\n",
1854 replac != NULL ? replac : "NOT FOUND",
1855 status, bitset(MF_SECURE, map->map_mflags));
1857 /* should recover if status == EX_TEMPFAIL */
1858 if (status == EX_TEMPFAIL && !bitset(MF_NODEFER, map->map_mflags))
1860 *pstat = EX_TEMPFAIL;
1862 sm_dprintf("map_lookup(%s, %s) tempfail: errno=%d\n",
1863 smap->s_name, key, errno);
1864 if (e->e_message == NULL)
1868 (void) sm_snprintf(mbuf, sizeof(mbuf),
1869 "%.80s map: lookup (%s): deferred",
1871 shortenstring(key, MAXSHORTSTR));
1872 e->e_message = sm_rpool_strdup_x(e->e_rpool, mbuf);
1875 if (status == EX_TEMPFAIL && map->map_tapp != NULL)
1877 size_t i = strlen(key) + strlen(map->map_tapp) + 1;
1878 static char *rwbuf = NULL;
1879 static size_t rwbuflen = 0;
1886 rwbuf = (char *) sm_pmalloc_x(rwbuflen);
1888 (void) sm_strlcpyn(rwbuf, rwbuflen, 2, key, map->map_tapp);
1890 sm_dprintf("map_lookup tempfail: returning \"%s\"\n",
1897 ** INITERRMAILERS -- initialize error and discard mailers
1906 ** initializes error and discard mailers.
1909 static MAILER discardmailer;
1910 static MAILER errormailer;
1911 static char *discardargv[] = { "DISCARD", NULL };
1912 static char *errorargv[] = { "ERROR", NULL };
1917 if (discardmailer.m_name == NULL)
1919 /* initialize the discard mailer */
1920 discardmailer.m_name = "*discard*";
1921 discardmailer.m_mailer = "DISCARD";
1922 discardmailer.m_argv = discardargv;
1924 if (errormailer.m_name == NULL)
1926 /* initialize the bogus mailer */
1927 errormailer.m_name = "*error*";
1928 errormailer.m_mailer = "ERROR";
1929 errormailer.m_argv = errorargv;
1933 ** BUILDADDR -- build address from token vector.
1936 ** tv -- token vector.
1937 ** a -- pointer to address descriptor to fill.
1938 ** If NULL, one will be allocated.
1939 ** flags -- info regarding whether this is a sender or
1941 ** e -- the current envelope.
1944 ** NULL if there was an error.
1951 static struct errcodes
1953 char *ec_name; /* name of error code */
1954 int ec_code; /* numeric code */
1957 { "usage", EX_USAGE },
1958 { "nouser", EX_NOUSER },
1959 { "nohost", EX_NOHOST },
1960 { "unavailable", EX_UNAVAILABLE },
1961 { "software", EX_SOFTWARE },
1962 { "tempfail", EX_TEMPFAIL },
1963 { "protocol", EX_PROTOCOL },
1964 { "config", EX_CONFIG },
1965 { NULL, EX_UNAVAILABLE }
1969 buildaddr(tv, a, flags, e)
1971 register ADDRESS *a;
1973 register ENVELOPE *e;
1975 bool tempfail = false;
1978 register struct mailer *m;
1982 char hbuf[MAXNAME + 1];
1983 static char ubuf[MAXNAME + 2];
1987 sm_dprintf("buildaddr, flags=%x, tv=", flags);
1988 printav(sm_debug_file(), tv);
1993 a = (ADDRESS *) sm_rpool_malloc_x(e->e_rpool, sizeof(*a));
1994 memset((char *) a, '\0', sizeof(*a));
1997 /* set up default error return flags */
1998 a->q_flags |= DefaultNotify;
2000 /* figure out what net/mailer to use */
2001 if (*tv == NULL || (**tv & 0377) != CANONNET)
2003 syserr("554 5.3.5 buildaddr: no mailer in parsed address");
2006 ** ExitStat may have been set by an earlier map open
2007 ** failure (to a permanent error (EX_OSERR) in syserr())
2008 ** so we also need to check if this particular $#error
2009 ** return wanted a 4XX failure.
2011 ** XXX the real fix is probably to set ExitStat correctly,
2012 ** i.e., to EX_TEMPFAIL if the map open is just a temporary
2016 if (ExitStat == EX_TEMPFAIL || tempfail)
2017 a->q_state = QS_QUEUEUP;
2020 a->q_state = QS_BADADDR;
2021 a->q_mailer = &errormailer;
2028 /* extract host and user portions */
2029 if (*++tv != NULL && (**tv & 0377) == CANONHOST)
2037 while (*tv != NULL && (**tv & 0377) != CANONUSER)
2044 syserr("554 5.3.5 buildaddr: no user");
2049 else if (hostp != NULL)
2050 cataddr(hostp, tv - 1, hbuf, sizeof(hbuf), '\0', false);
2051 cataddr(++tv, NULL, ubuf, sizeof(ubuf), ' ', false);
2054 /* save away the host name */
2055 if (sm_strcasecmp(mname, "error") == 0)
2057 /* Set up triplet for use by -bv */
2058 a->q_mailer = &errormailer;
2059 a->q_user = sm_rpool_strdup_x(e->e_rpool, ubuf);
2060 /* XXX wrong place? */
2064 register struct errcodes *ep;
2066 a->q_host = sm_rpool_strdup_x(e->e_rpool, hbuf);
2067 if (strchr(hbuf, '.') != NULL)
2069 a->q_status = sm_rpool_strdup_x(e->e_rpool,
2071 setstat(dsntoexitstat(hbuf));
2073 else if (isascii(hbuf[0]) && isdigit(hbuf[0]))
2075 setstat(atoi(hbuf));
2079 for (ep = ErrorCodes; ep->ec_name != NULL; ep++)
2080 if (sm_strcasecmp(ep->ec_name, hbuf) == 0)
2082 setstat(ep->ec_code);
2088 setstat(EX_UNAVAILABLE);
2091 if (ISSMTPCODE(ubuf) && ubuf[3] == ' ')
2096 if ((off = isenhsc(ubuf + 4, ' ')) > 0)
2098 ubuf[off + 4] = '\0';
2106 (void) sm_strlcpyn(fmt, sizeof(fmt), 2, ubuf, " %s");
2108 usrerr(fmt, ubuf + off);
2109 else if (isenhsc(hbuf, '\0') > 0)
2110 usrerrenh(hbuf, fmt, ubuf + off);
2112 usrerr(fmt, ubuf + off);
2113 /* XXX ubuf[off - 1] = ' '; */
2119 usrerr("553 5.3.0 %s", ubuf);
2124 for (mp = Mailer; (m = *mp++) != NULL; )
2126 if (sm_strcasecmp(m->m_name, mname) == 0)
2131 syserr("554 5.3.5 buildaddr: unknown mailer %s", mname);
2136 /* figure out what host (if any) */
2139 if (!bitnset(M_LOCALMAILER, m->m_flags))
2141 syserr("554 5.3.5 buildaddr: no host");
2147 a->q_host = sm_rpool_strdup_x(e->e_rpool, hbuf);
2149 /* figure out the user */
2151 if (bitnset(M_CHECKUDB, m->m_flags) && *p == '@')
2156 a->q_flags |= QNOTREMOTE;
2159 /* do special mapping for local mailer */
2162 if (*p == '|' && bitnset(M_CHECKPROG, m->m_flags))
2163 a->q_mailer = m = ProgMailer;
2164 else if (*p == '/' && bitnset(M_CHECKFILE, m->m_flags))
2165 a->q_mailer = m = FileMailer;
2166 else if (*p == ':' && bitnset(M_CHECKINCLUDE, m->m_flags))
2168 /* may be :include: */
2170 if (sm_strncasecmp(ubuf, ":include:", 9) == 0)
2172 /* if :include:, don't need further rewriting */
2173 a->q_mailer = m = InclMailer;
2174 a->q_user = sm_rpool_strdup_x(e->e_rpool, &ubuf[9]);
2179 /* rewrite according recipient mailer rewriting rules */
2180 macdefine(&e->e_macro, A_PERM, 'h', a->q_host);
2182 if (ConfigLevel >= 10 ||
2183 !bitset(RF_SENDERADDR|RF_HEADERADDR, flags))
2185 /* sender addresses done later */
2186 (void) rewrite(tv, 2, 0, e, maxatom);
2187 if (m->m_re_rwset > 0)
2188 (void) rewrite(tv, m->m_re_rwset, 0, e, maxatom);
2190 (void) rewrite(tv, 4, 0, e, maxatom);
2192 /* save the result for the command line/RCPT argument */
2193 cataddr(tv, NULL, ubuf, sizeof(ubuf), '\0', true);
2194 a->q_user = sm_rpool_strdup_x(e->e_rpool, ubuf);
2197 ** Do mapping to lower case as requested by mailer
2200 if (a->q_host != NULL && !bitnset(M_HST_UPPER, m->m_flags))
2201 makelower(a->q_host);
2202 if (!bitnset(M_USR_UPPER, m->m_flags))
2203 makelower(a->q_user);
2207 sm_dprintf("buildaddr => ");
2208 printaddr(sm_debug_file(), a, false);
2214 ** CATADDR -- concatenate pieces of addresses (putting in <LWSP> subs)
2217 ** pvp -- parameter vector to rebuild.
2218 ** evp -- last parameter to include. Can be NULL to
2220 ** buf -- buffer to build the string into.
2221 ** sz -- size of buf.
2222 ** spacesub -- the space separator character;
2224 ** NOSPACESEP: no separator
2225 ** external -- convert to external form?
2226 ** (no metacharacters; METAQUOTEs removed, see below)
2235 ** There are two formats for strings: internal and external.
2236 ** The external format is just an eight-bit clean string (no
2237 ** null bytes, everything else OK). The internal format can
2238 ** include sendmail metacharacters. The special character
2239 ** METAQUOTE essentially quotes the character following, stripping
2240 ** it of all special semantics.
2242 ** The cataddr routine needs to be aware of whether it is producing
2243 ** an internal or external form as output (it only takes internal
2246 ** The parseaddr routine has a similar issue on input, but that
2247 ** is flagged on the basis of which token table is passed in.
2251 cataddr(pvp, evp, buf, sz, spacesub, external)
2259 bool oatomtok, natomtok;
2262 oatomtok = natomtok = false;
2265 sm_dprintf("cataddr(%d) <==", external);
2266 printav(sm_debug_file(), pvp);
2272 if (spacesub == '\0')
2273 spacesub = SpaceSub;
2282 while (*pvp != NULL && sz > 0)
2286 natomtok = (IntTokenTab[**pvp & 0xff] == ATM);
2287 if (oatomtok && natomtok && spacesub != NOSPACESEP)
2293 for (q = *pvp; *q != '\0'; )
2302 ** If the current character (c) is METAQUOTE and we
2303 ** want the "external" form and the next character
2304 ** is not NUL, then overwrite METAQUOTE with that
2305 ** character (i.e., METAQUOTE ch is changed to ch).
2306 ** p[-1] is used because p is advanced (above).
2309 if ((c & 0377) == METAQUOTE && external && *q != '\0')
2314 oatomtok = natomtok;
2321 ** Silently truncate long strings: even though this doesn't
2322 ** seem like a good idea it is necessary because header checks
2323 ** send the whole header value to rscheck() and hence rewrite().
2324 ** The latter however sometimes uses a "short" buffer (e.g.,
2325 ** cbuf[MAXNAME + 1]) to call cataddr() which then triggers this
2326 ** error function. One possible fix to the problem is to pass
2327 ** flags to rscheck() and rewrite() to distinguish the various
2328 ** calls and only trigger the error if necessary. For now just
2329 ** undo the change from 8.13.0.
2333 usrerr("cataddr: string too long");
2338 sm_dprintf(" cataddr => %s\n", str2prt(buf));
2342 ** SAMEADDR -- Determine if two addresses are the same
2344 ** This is not just a straight comparison -- if the mailer doesn't
2345 ** care about the host we just ignore it, etc.
2348 ** a, b -- pointers to the internal forms to compare.
2351 ** true -- they represent the same mailbox.
2352 ** false -- they don't.
2360 register ADDRESS *a;
2361 register ADDRESS *b;
2363 register ADDRESS *ca, *cb;
2365 /* if they don't have the same mailer, forget it */
2366 if (a->q_mailer != b->q_mailer)
2370 ** Addresses resolving to error mailer
2371 ** should not be considered identical
2374 if (a->q_mailer == &errormailer)
2377 /* if the user isn't the same, we can drop out */
2378 if (strcmp(a->q_user, b->q_user) != 0)
2381 /* do the required flags match? */
2382 if (!ADDR_FLAGS_MATCH(a, b))
2385 /* if we have good uids for both but they differ, these are different */
2386 if (a->q_mailer == ProgMailer)
2390 if (ca != NULL && cb != NULL &&
2391 bitset(QGOODUID, ca->q_flags & cb->q_flags) &&
2392 ca->q_uid != cb->q_uid)
2396 /* otherwise compare hosts (but be careful for NULL ptrs) */
2397 if (a->q_host == b->q_host)
2399 /* probably both null pointers */
2402 if (a->q_host == NULL || b->q_host == NULL)
2404 /* only one is a null pointer */
2407 if (strcmp(a->q_host, b->q_host) != 0)
2413 ** PRINTADDR -- print address (for debugging)
2416 ** a -- the address to print
2417 ** follow -- follow the q_next chain.
2429 unsigned long qf_bit;
2432 /* :'a,.s;^#define \(Q[A-Z]*\) .*; { "\1", \1 },; */
2433 static struct qflags AddressFlags[] =
2435 { "QGOODUID", QGOODUID },
2436 { "QPRIMARY", QPRIMARY },
2437 { "QNOTREMOTE", QNOTREMOTE },
2438 { "QSELFREF", QSELFREF },
2439 { "QBOGUSSHELL", QBOGUSSHELL },
2440 { "QUNSAFEADDR", QUNSAFEADDR },
2441 { "QPINGONSUCCESS", QPINGONSUCCESS },
2442 { "QPINGONFAILURE", QPINGONFAILURE },
2443 { "QPINGONDELAY", QPINGONDELAY },
2444 { "QHASNOTIFY", QHASNOTIFY },
2445 { "QRELAYED", QRELAYED },
2446 { "QEXPANDED", QEXPANDED },
2447 { "QDELIVERED", QDELIVERED },
2448 { "QDELAYED", QDELAYED },
2449 { "QTHISPASS", QTHISPASS },
2450 { "QALIAS", QALIAS },
2451 { "QBYTRACE", QBYTRACE },
2452 { "QBYNDELAY", QBYNDELAY },
2453 { "QBYNRELAY", QBYNRELAY },
2454 { "QINTBCC", QINTBCC },
2455 { "QDYNMAILER", QDYNMAILER },
2456 { "QRCPTOK", QRCPTOK },
2457 { "QSECURE", QSECURE },
2458 { "QTHISPASS", QTHISPASS },
2459 { "QRCPTOK", QRCPTOK },
2460 { "QQUEUED", QQUEUED },
2465 printaddr(fp, a, follow)
2467 register ADDRESS *a;
2471 MAILER pseudomailer;
2472 register struct qflags *qfp;
2477 (void) sm_io_fprintf(fp, SM_TIME_DEFAULT, "[NULL]\n");
2483 (void) sm_io_fprintf(fp, SM_TIME_DEFAULT, "%p=", (void *)a);
2484 (void) sm_io_flush(fp, SM_TIME_DEFAULT);
2486 /* find the mailer -- carefully */
2495 (void) sm_io_fprintf(fp, SM_TIME_DEFAULT,
2496 "%s:\n\tmailer %d (%s), host `%s'\n",
2497 a->q_paddr == NULL ? "<null>" : a->q_paddr,
2498 m->m_mno, m->m_name,
2499 a->q_host == NULL ? "<null>" : a->q_host);
2500 (void) sm_io_fprintf(fp, SM_TIME_DEFAULT,
2501 "\tuser `%s', ruser `%s'\n",
2503 a->q_ruser == NULL ? "<null>" : a->q_ruser);
2504 (void) sm_io_fprintf(fp, SM_TIME_DEFAULT, "\tstate=");
2508 (void) sm_io_fprintf(fp, SM_TIME_DEFAULT, "OK");
2512 (void) sm_io_fprintf(fp, SM_TIME_DEFAULT,
2517 (void) sm_io_fprintf(fp, SM_TIME_DEFAULT,
2522 (void) sm_io_fprintf(fp, SM_TIME_DEFAULT,
2527 (void) sm_io_fprintf(fp, SM_TIME_DEFAULT, "RETRY");
2531 (void) sm_io_fprintf(fp, SM_TIME_DEFAULT, "SENT");
2535 (void) sm_io_fprintf(fp, SM_TIME_DEFAULT,
2540 (void) sm_io_fprintf(fp, SM_TIME_DEFAULT,
2545 (void) sm_io_fprintf(fp, SM_TIME_DEFAULT,
2550 (void) sm_io_fprintf(fp, SM_TIME_DEFAULT,
2555 (void) sm_io_fprintf(fp, SM_TIME_DEFAULT,
2560 (void) sm_io_fprintf(fp, SM_TIME_DEFAULT,
2565 (void) sm_io_fprintf(fp, SM_TIME_DEFAULT,
2570 (void) sm_io_fprintf(fp, SM_TIME_DEFAULT,
2575 (void) sm_io_fprintf(fp, SM_TIME_DEFAULT,
2580 (void) sm_io_fprintf(fp, SM_TIME_DEFAULT,
2584 (void) sm_io_fprintf(fp, SM_TIME_DEFAULT,
2585 ", next=%p, alias %p, uid %d, gid %d\n",
2586 (void *)a->q_next, (void *)a->q_alias,
2587 (int) a->q_uid, (int) a->q_gid);
2588 (void) sm_io_fprintf(fp, SM_TIME_DEFAULT, "\tflags=%lx<",
2591 for (qfp = AddressFlags; qfp->qf_name != NULL; qfp++)
2593 if (!bitset(qfp->qf_bit, a->q_flags))
2596 (void) sm_io_fprintf(fp, SM_TIME_DEFAULT,
2599 (void) sm_io_fprintf(fp, SM_TIME_DEFAULT, "%s",
2602 (void) sm_io_fprintf(fp, SM_TIME_DEFAULT, ">\n");
2603 (void) sm_io_fprintf(fp, SM_TIME_DEFAULT,
2604 "\towner=%s, home=\"%s\", fullname=\"%s\"\n",
2605 a->q_owner == NULL ? "(none)" : a->q_owner,
2606 a->q_home == NULL ? "(none)" : a->q_home,
2607 a->q_fullname == NULL ? "(none)" : a->q_fullname);
2608 (void) sm_io_fprintf(fp, SM_TIME_DEFAULT,
2609 "\torcpt=\"%s\", statmta=%s, status=%s\n",
2610 a->q_orcpt == NULL ? "(none)" : a->q_orcpt,
2611 a->q_statmta == NULL ? "(none)" : a->q_statmta,
2612 a->q_status == NULL ? "(none)" : a->q_status);
2613 (void) sm_io_fprintf(fp, SM_TIME_DEFAULT,
2614 "\tfinalrcpt=\"%s\"\n",
2615 a->q_finalrcpt == NULL ? "(none)" : a->q_finalrcpt);
2616 (void) sm_io_fprintf(fp, SM_TIME_DEFAULT,
2617 "\trstatus=\"%s\"\n",
2618 a->q_rstatus == NULL ? "(none)" : a->q_rstatus);
2619 (void) sm_io_fprintf(fp, SM_TIME_DEFAULT,
2621 a->q_statdate == 0 ? "(none)" : ctime(&a->q_statdate));
2629 ** EMPTYADDR -- return true if this address is empty (``<>'')
2632 ** a -- pointer to the address
2635 ** true -- if this address is "empty" (i.e., no one should
2636 ** ever generate replies to it.
2637 ** false -- if it is a "regular" (read: replyable) address.
2642 register ADDRESS *a;
2644 return a->q_paddr == NULL || strcmp(a->q_paddr, "<>") == 0 ||
2645 a->q_user == NULL || strcmp(a->q_user, "<>") == 0;
2648 ** REMOTENAME -- return the name relative to the current mailer
2651 ** name -- the name to translate.
2652 ** m -- the mailer that we want to do rewriting relative to.
2653 ** flags -- fine tune operations.
2654 ** pstat -- pointer to status word.
2655 ** e -- the current envelope.
2658 ** the text string representing this address relative to
2659 ** the receiving mailer.
2665 ** The text string returned is tucked away locally;
2666 ** copy it if you intend to save it.
2670 remotename(name, m, flags, pstat, e)
2675 register ENVELOPE *e;
2677 register char **pvp;
2678 char *SM_NONVOLATILE fancy;
2681 static char buf[MAXNAME + 1];
2682 char lbuf[MAXNAME + 1];
2683 char pvpbuf[PSBUFSIZE];
2688 sm_dprintf("remotename(");
2689 xputs(sm_debug_file(), name);
2693 /* don't do anything if we are tagging it as special */
2694 if (bitset(RF_SENDERADDR, flags))
2696 rwset = bitset(RF_HEADERADDR, flags) ? m->m_sh_rwset
2702 rwset = bitset(RF_HEADERADDR, flags) ? m->m_rh_rwset
2710 addrtype[0] = bitset(RF_HEADERADDR, flags) ? 'h' : 'e';
2711 macdefine(&e->e_macro, A_TEMP, macid("{addr_type}"), addrtype);
2714 ** Do a heuristic crack of this name to extract any comment info.
2715 ** This will leave the name as a comment and a $g macro.
2718 if (bitset(RF_CANONICAL, flags) || bitnset(M_NOCOMMENT, m->m_flags))
2721 fancy = crackaddr(name, e);
2724 ** Turn the name into canonical form.
2725 ** Normally this will be RFC 822 style, i.e., "user@domain".
2726 ** If this only resolves to "user", and the "C" flag is
2727 ** specified in the sending mailer, then the sender's
2728 ** domain will be appended.
2731 pvp = prescan(name, '\0', pvpbuf, sizeof(pvpbuf), NULL, NULL, false);
2734 if (REWRITE(pvp, 3, e) == EX_TEMPFAIL)
2735 *pstat = EX_TEMPFAIL;
2736 if (bitset(RF_ADDDOMAIN, flags) && e->e_fromdomain != NULL)
2738 /* append from domain to this address */
2739 register char **pxp = pvp;
2740 int l = MAXATOM; /* size of buffer for pvp */
2742 /* see if there is an "@domain" in the current name */
2743 while (*pxp != NULL && strcmp(*pxp, "@") != 0)
2750 /* no.... append the "@domain" from the sender */
2751 register char **qxq = e->e_fromdomain;
2753 while ((*pxp++ = *qxq++) != NULL)
2758 usrerr("553 5.1.0 remotename: too many tokens");
2759 *pstat = EX_UNAVAILABLE;
2763 if (REWRITE(pvp, 3, e) == EX_TEMPFAIL)
2764 *pstat = EX_TEMPFAIL;
2769 ** Do more specific rewriting.
2770 ** Rewrite using ruleset 1 or 2 depending on whether this is
2771 ** a sender address or not.
2772 ** Then run it through any receiving-mailer-specific rulesets.
2775 if (bitset(RF_SENDERADDR, flags))
2777 if (REWRITE(pvp, 1, e) == EX_TEMPFAIL)
2778 *pstat = EX_TEMPFAIL;
2782 if (REWRITE(pvp, 2, e) == EX_TEMPFAIL)
2783 *pstat = EX_TEMPFAIL;
2787 if (REWRITE(pvp, rwset, e) == EX_TEMPFAIL)
2788 *pstat = EX_TEMPFAIL;
2792 ** Do any final sanitation the address may require.
2793 ** This will normally be used to turn internal forms
2794 ** (e.g., user@host.LOCAL) into external form. This
2795 ** may be used as a default to the above rules.
2798 if (REWRITE(pvp, 4, e) == EX_TEMPFAIL)
2799 *pstat = EX_TEMPFAIL;
2802 ** Now restore the comment information we had at the beginning.
2805 cataddr(pvp, NULL, lbuf, sizeof(lbuf), '\0', false);
2806 oldg = macget(&e->e_macro, 'g');
2807 macset(&e->e_macro, 'g', lbuf);
2810 /* need to make sure route-addrs have <angle brackets> */
2811 if (bitset(RF_CANONICAL, flags) && lbuf[0] == '@')
2812 expand("<\201g>", buf, sizeof(buf), e);
2814 expand(fancy, buf, sizeof(buf), e);
2816 macset(&e->e_macro, 'g', oldg);
2821 sm_dprintf("remotename => `");
2822 xputs(sm_debug_file(), buf);
2823 sm_dprintf("', stat=%d\n", *pstat);
2828 ** MAPLOCALUSER -- run local username through ruleset 5 for final redirection
2831 ** a -- the address to map (but just the user name part).
2832 ** sendq -- the sendq in which to install any replacement
2834 ** aliaslevel -- the alias nesting depth.
2835 ** e -- the envelope.
2841 #define Q_COPYFLAGS (QPRIMARY|QBOGUSSHELL|QUNSAFEADDR|\
2842 Q_PINGFLAGS|QHASNOTIFY|\
2843 QRELAYED|QEXPANDED|QDELIVERED|QDELAYED|\
2844 QBYTRACE|QBYNDELAY|QBYNRELAY)
2847 maplocaluser(a, sendq, aliaslevel, e)
2848 register ADDRESS *a;
2853 register char **pvp;
2854 register ADDRESS *SM_NONVOLATILE a1 = NULL;
2855 char pvpbuf[PSBUFSIZE];
2859 sm_dprintf("maplocaluser: ");
2860 printaddr(sm_debug_file(), a, false);
2862 pvp = prescan(a->q_user, '\0', pvpbuf, sizeof(pvpbuf), NULL, NULL,
2867 sm_dprintf("maplocaluser: cannot prescan %s\n",
2872 macdefine(&e->e_macro, A_PERM, 'h', a->q_host);
2873 macdefine(&e->e_macro, A_PERM, 'u', a->q_user);
2874 macdefine(&e->e_macro, A_PERM, 'z', a->q_home);
2876 macdefine(&e->e_macro, A_PERM, macid("{addr_type}"), "e r");
2877 if (REWRITE(pvp, 5, e) == EX_TEMPFAIL)
2880 sm_dprintf("maplocaluser: rewrite tempfail\n");
2881 a->q_state = QS_QUEUEUP;
2882 a->q_status = "4.4.3";
2885 if (pvp[0] == NULL || (pvp[0][0] & 0377) != CANONNET)
2888 sm_dprintf("maplocaluser: doesn't resolve\n");
2893 a1 = buildaddr(pvp, NULL, 0, e);
2894 SM_EXCEPT(exc, "E:mta.quickabort")
2897 ** mark address as bad, S5 returned an error
2898 ** and we gave that back to the SMTP client.
2901 a->q_state = QS_DONTSEND;
2902 sm_exc_raisenew_x(&EtypeQuickAbort, 2);
2905 /* if non-null, mailer destination specified -- has it changed? */
2906 if (a1 == NULL || sameaddr(a, a1))
2909 sm_dprintf("maplocaluser: address unchanged\n");
2913 /* make new address take on flags and print attributes of old */
2914 a1->q_flags &= ~Q_COPYFLAGS;
2915 a1->q_flags |= a->q_flags & Q_COPYFLAGS;
2916 a1->q_paddr = sm_rpool_strdup_x(e->e_rpool, a->q_paddr);
2917 a1->q_finalrcpt = a->q_finalrcpt;
2918 a1->q_orcpt = a->q_orcpt;
2920 /* mark old address as dead; insert new address */
2921 a->q_state = QS_REPLACED;
2924 sm_dprintf("maplocaluser: QS_REPLACED ");
2925 printaddr(sm_debug_file(), a, false);
2928 allocaddr(a1, RF_COPYALL, sm_rpool_strdup_x(e->e_rpool, a->q_paddr), e);
2929 (void) recipient(a1, sendq, aliaslevel, e);
2932 ** DEQUOTE_INIT -- initialize dequote map
2935 ** map -- the internal map structure.
2936 ** args -- arguments.
2943 dequote_init(map, args)
2947 register char *p = args;
2949 /* there is no check whether there is really an argument */
2950 map->map_mflags |= MF_KEEPQUOTES;
2953 while (SM_ISSPACE(*p))
2964 map->map_mflags |= MF_DEFER;
2969 map->map_spacesub = *++p;
2972 while (*p != '\0' && !(SM_ISSPACE(*p)))
2977 if (map->map_app != NULL)
2978 map->map_app = newstr(map->map_app);
2983 ** DEQUOTE_MAP -- unquote an address
2986 ** map -- the internal map structure (ignored).
2987 ** name -- the name to dequote.
2988 ** av -- arguments (ignored).
2989 ** statp -- pointer to status out-parameter.
2992 ** NULL -- if there were no quotes, or if the resulting
2993 ** unquoted buffer would not be acceptable to prescan.
2994 ** else -- The dequoted buffer.
2999 dequote_map(map, name, av, statp)
3012 bool quotemode = false;
3013 bool bslashmode = false;
3014 char spacesub = map->map_spacesub;
3016 for (p = q = name; (c = *p++) != '\0'; )
3025 if (c == ' ' && spacesub != '\0')
3058 quotemode = !quotemode;
3067 if (anglecnt-- <= 0)
3074 if (anglecnt != 0 || cmntcnt != 0 || bslashmode ||
3075 quotemode || quotecnt <= 0 || spacecnt != 0)
3078 return map_rewrite(map, name, strlen(name), NULL);
3081 ** RSCHECK -- check string(s) for validity using rewriting sets
3084 ** rwset -- the rewriting set to use.
3085 ** p1 -- the first string to check.
3086 ** p2 -- the second string to check -- may be NULL.
3087 ** e -- the current envelope.
3088 ** flags -- control some behavior, see RSF_ in sendmail.h
3089 ** logl -- logging level.
3090 ** host -- NULL or relay host.
3091 ** logid -- id for sm_syslog.
3092 ** addr -- if not NULL and ruleset returns $#error:
3093 ** store mailer triple here.
3094 ** addrstr -- if not NULL and ruleset does not return $#:
3098 ** EX_OK -- if the rwset doesn't resolve to $#error
3099 ** else -- the failure status (message printed)
3103 rscheck(rwset, p1, p2, e, flags, logl, host, logid, addr, addrstr)
3118 int volatile rstat = EX_OK;
3121 bool volatile discard = false;
3122 bool saveQuickAbort = QuickAbort;
3123 bool saveSuprErrs = SuprErrs;
3124 bool quarantine = false;
3125 char ubuf[BUFSIZ * 2];
3127 char pvpbuf[PSBUFSIZE];
3128 extern char MsgBuf[];
3131 sm_dprintf("rscheck(%s, %s, %s)\n", rwset, p1,
3132 p2 == NULL ? "(NULL)" : p2);
3134 rsno = strtorwset(rwset, NULL, ST_FIND);
3140 bufsize = strlen(p1) + strlen(p2) + 2;
3141 if (bufsize > sizeof(buf0))
3142 buf = sm_malloc_x(bufsize);
3146 bufsize = sizeof(buf0);
3148 (void) sm_snprintf(buf, bufsize, "%s%c%s", p1, CONDELSE, p2);
3152 bufsize = strlen(p1) + 1;
3153 if (bufsize > sizeof(buf0))
3154 buf = sm_malloc_x(bufsize);
3158 bufsize = sizeof(buf0);
3160 (void) sm_strlcpy(buf, p1, bufsize);
3166 pvp = prescan(buf, '\0', pvpbuf, sizeof(pvpbuf), NULL,
3167 bitset(RSF_RMCOMM, flags) ?
3168 IntTokenTab : TokTypeNoC,
3169 bitset(RSF_RMCOMM, flags) ? false : true);
3170 SuprErrs = saveSuprErrs;
3174 sm_dprintf("rscheck: cannot prescan input\n");
3176 syserr("rscheck: cannot prescan input: \"%s\"",
3177 shortenstring(buf, MAXSHORTSTR));
3182 if (bitset(RSF_UNSTRUCTURED, flags))
3184 (void) REWRITE(pvp, rsno, e);
3185 if (bitset(RSF_UNSTRUCTURED, flags))
3186 SuprErrs = saveSuprErrs;
3188 if (pvp[0] != NULL && (pvp[0][0] & 0377) != CANONNET &&
3189 bitset(RSF_ADDR, flags) && addrstr != NULL)
3191 cataddr(&(pvp[0]), NULL, ubuf, sizeof(ubuf),
3192 bitset(RSF_STRING, flags) ? NOSPACESEP : ' ',
3194 *addrstr = sm_rpool_strdup_x(e->e_rpool, ubuf);
3198 if (pvp[0] == NULL || (pvp[0][0] & 0377) != CANONNET ||
3199 pvp[1] == NULL || (strcmp(pvp[1], "error") != 0 &&
3200 strcmp(pvp[1], "discard") != 0))
3205 if (strcmp(pvp[1], "discard") == 0)
3208 sm_dprintf("rscheck: discard mailer selected\n");
3209 e->e_flags |= EF_DISCARD;
3212 else if (strcmp(pvp[1], "error") == 0 &&
3213 pvp[2] != NULL && (pvp[2][0] & 0377) == CANONHOST &&
3214 pvp[3] != NULL && strcmp(pvp[3], "quarantine") == 0)
3216 if (pvp[4] == NULL ||
3217 (pvp[4][0] & 0377) != CANONUSER ||
3219 e->e_quarmsg = sm_rpool_strdup_x(e->e_rpool,
3223 cataddr(&(pvp[5]), NULL, ubuf,
3224 sizeof(ubuf), ' ', true);
3225 e->e_quarmsg = sm_rpool_strdup_x(e->e_rpool,
3228 macdefine(&e->e_macro, A_PERM,
3229 macid("{quarantine}"), e->e_quarmsg);
3235 int savelogusrerrs = LogUsrErrs;
3236 static bool logged = false;
3238 /* got an error -- process it */
3239 saveexitstat = ExitStat;
3241 (void) buildaddr(pvp, &a1, 0, e);
3244 addr->q_mailer = a1.q_mailer;
3245 addr->q_user = a1.q_user;
3246 addr->q_host = a1.q_host;
3248 LogUsrErrs = savelogusrerrs;
3250 ExitStat = saveexitstat;
3253 if (bitset(RSF_COUNT, flags))
3254 markstats(e, &a1, STATS_REJECT);
3259 if (LogLevel > logl)
3268 (void) sm_snprintf(p, SPACELEFT(lbuf, p),
3277 relay = macvalue('_', e);
3280 (void) sm_snprintf(p, SPACELEFT(lbuf, p),
3281 ", relay=%s", relay);
3286 sm_syslog(LOG_NOTICE, logid,
3287 "ruleset=%s, arg1=%s%s, discard",
3289 else if (quarantine)
3290 sm_syslog(LOG_NOTICE, logid,
3291 "ruleset=%s, arg1=%s%s, quarantine=%s",
3292 rwset, p1, lbuf, ubuf);
3294 sm_syslog(LOG_NOTICE, logid,
3295 "ruleset=%s, arg1=%s%s, reject=%s",
3296 rwset, p1, lbuf, MsgBuf);
3306 QuickAbort = saveQuickAbort;
3312 /* rulesets don't set errno */
3314 if (rstat != EX_OK && QuickAbort)
3315 sm_exc_raisenew_x(&EtypeQuickAbort, 2);
3320 ** RSCAP -- call rewriting set to return capabilities
3323 ** rwset -- the rewriting set to use.
3324 ** p1 -- the first string to check.
3325 ** p2 -- the second string to check -- may be NULL.
3326 ** e -- the current envelope.
3327 ** pvp -- pointer to token vector.
3328 ** pvpbuf -- buffer space.
3329 ** size -- size of buffer space.
3332 ** EX_UNAVAILABLE -- ruleset doesn't exist.
3333 ** EX_DATAERR -- prescan() failed.
3334 ** EX_OK -- rewrite() was successful.
3335 ** else -- return status from rewrite().
3339 rscap(rwset, p1, p2, e, pvp, pvpbuf, size)
3350 int volatile rstat = EX_OK;
3352 bool saveQuickAbort = QuickAbort;
3353 bool saveSuprErrs = SuprErrs;
3355 extern char MsgBuf[];
3358 sm_dprintf("rscap(%s, %s, %s)\n", rwset, p1,
3359 p2 == NULL ? "(NULL)" : p2);
3361 SM_REQUIRE(pvp != NULL);
3362 rsno = strtorwset(rwset, NULL, ST_FIND);
3364 return EX_UNAVAILABLE;
3368 bufsize = strlen(p1) + strlen(p2) + 2;
3369 if (bufsize > sizeof(buf0))
3370 buf = sm_malloc_x(bufsize);
3374 bufsize = sizeof(buf0);
3376 (void) sm_snprintf(buf, bufsize, "%s%c%s", p1, CONDELSE, p2);
3380 bufsize = strlen(p1) + 1;
3381 if (bufsize > sizeof(buf0))
3382 buf = sm_malloc_x(bufsize);
3386 bufsize = sizeof(buf0);
3388 (void) sm_strlcpy(buf, p1, bufsize);
3394 *pvp = prescan(buf, '\0', pvpbuf, size, NULL, IntTokenTab,
3397 rstat = rewrite(*pvp, rsno, 0, e, size);
3401 sm_dprintf("rscap: cannot prescan input\n");
3410 SuprErrs = saveSuprErrs;
3411 QuickAbort = saveQuickAbort;
3413 /* prevent information leak, this may contain rewrite error */