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 if (REWRITE(pvp, 3, e) == EX_TEMPFAIL)
139 if (REWRITE(pvp, 0, e) == EX_TEMPFAIL)
143 ** Build canonical address from pvp.
146 a = buildaddr(pvp, a, flags, e);
148 if (hasctrlchar(a->q_user, isrcpt, true))
151 sm_dprintf("parseaddr-->bad q_user\n");
154 ** Just mark the address as bad so DSNs work.
155 ** hasctrlchar() has to make sure that the address
156 ** has been sanitized, e.g., shortened.
159 a->q_state = QS_BADADDR;
163 ** Make local copies of the host & user and then
164 ** transport them out.
167 allocaddr(a, flags, addr, e);
168 if (QS_IS_BADADDR(a->q_state))
170 /* weed out bad characters in the printable address too */
171 (void) hasctrlchar(a->q_paddr, isrcpt, false);
176 ** Select a queue directory for recipient addresses.
177 ** This is done here and in split_across_queue_groups(),
178 ** but the latter applies to addresses after aliasing,
179 ** and only if splitting is done.
182 if ((a->q_qgrp == NOAQGRP || a->q_qgrp == ENVQGRP) &&
183 !bitset(RF_SENDERADDR|RF_HEADERADDR|RF_RM_ADDR, flags) &&
184 OpMode != MD_INITALIAS)
188 /* call ruleset which should return a queue group name */
189 r = rscap(RS_QUEUEGROUP, a->q_user, NULL, e, &pvp, pvpbuf,
192 pvp != NULL && pvp[0] != NULL &&
193 (pvp[0][0] & 0377) == CANONNET &&
194 pvp[1] != NULL && pvp[1][0] != '\0')
196 r = name2qid(pvp[1]);
197 if (r == NOQGRP && LogLevel > 10)
198 sm_syslog(LOG_INFO, NOQID,
199 "can't find queue group name %s, selection ignored",
201 if (tTd(20, 4) && r != NOQGRP)
202 sm_syslog(LOG_INFO, NOQID,
203 "queue group name %s -> %d",
205 a->q_qgrp = r == NOQGRP ? ENVQGRP : r;
210 ** If there was a parsing failure, mark it for queueing.
213 if (qup && OpMode != MD_INITALIAS)
215 char *msg = "Transient parse error -- message queued for future delivery";
217 if (e->e_sendmode == SM_DEFER)
218 msg = "Deferring message until queue run";
220 sm_dprintf("parseaddr: queueing message\n");
222 if (e->e_message == NULL && e->e_sendmode != SM_DEFER)
223 e->e_message = sm_rpool_strdup_x(e->e_rpool, msg);
224 a->q_state = QS_QUEUEUP;
225 a->q_status = "4.4.3";
229 ** Compute return value.
234 sm_dprintf("parseaddr-->");
235 printaddr(sm_debug_file(), a, false);
241 ** INVALIDADDR -- check for address containing characters used for macros
244 ** addr -- the address to check.
245 ** note: this is the complete address (including display part)
246 ** delimptr -- if non-NULL: end of address to check, i.e.,
247 ** a pointer in the address string.
248 ** isrcpt -- true iff the address is for a recipient.
251 ** true -- if the address has characters that are reservered
252 ** for macros or is too long.
253 ** false -- otherwise.
257 invalidaddr(addr, delimptr, isrcpt)
263 char savedelim = '\0';
267 if (delimptr != NULL)
269 /* delimptr points to the end of the address to test */
270 savedelim = *delimptr;
271 if (savedelim != '\0') /* if that isn't '\0' already: */
272 *delimptr = '\0'; /* set it */
274 for (; *addr != '\0'; addr++)
276 if (!EightBitAddrOK && (*addr & 0340) == 0200)
280 *addr = BAD_CHAR_REPLACEMENT;
282 if (++len > MAXNAME - 1)
287 usrerr("553 5.1.0 Address \"%s\" too long (%d bytes max)",
297 usrerr("501 5.1.3 8-bit character in mailbox address \"%s\"",
300 usrerr("501 5.1.7 8-bit character in mailbox address \"%s\"",
304 if (delimptr != NULL && savedelim != '\0')
305 *delimptr = savedelim; /* restore old character at delimptr */
309 ** HASCTRLCHAR -- check for address containing meta-characters
311 ** Checks that the address contains no meta-characters, and contains
312 ** no "non-printable" characters unless they are quoted or escaped.
313 ** Quoted or escaped characters are literals.
316 ** addr -- the address to check.
317 ** isrcpt -- true if the address is for a recipient; false
319 ** complain -- true if an error should issued if the address
320 ** is invalid and should be "repaired".
323 ** true -- if the address has any "weird" characters or
324 ** non-printable characters or if a quote is unbalanced.
325 ** false -- otherwise.
329 hasctrlchar(addr, isrcpt, complain)
331 bool isrcpt, complain;
340 for (; *addr != '\0'; addr++)
342 if (++len > MAXNAME - 1)
346 (void) shorten_rfc822_string(b, MAXNAME - 1);
347 usrerr("553 5.1.0 Address \"%s\" too long (%d bytes max)",
353 if (!EightBitAddrOK && !quoted && (*addr < 32 || *addr == 127))
355 result = "non-printable character";
356 *addr = BAD_CHAR_REPLACEMENT;
361 else if (*addr == '\\')
363 /* XXX Generic problem: no '\0' in strings. */
366 result = "trailing \\ character";
367 *--addr = BAD_CHAR_REPLACEMENT;
371 if (!EightBitAddrOK && (*addr & 0340) == 0200)
374 result = "8-bit character";
375 *addr = BAD_CHAR_REPLACEMENT;
380 result = "unbalanced quote"; /* unbalanced quote */
381 if (result != NULL && complain)
384 usrerr("501 5.1.3 Syntax error in mailbox address \"%s\" (%s)",
387 usrerr("501 5.1.7 Syntax error in mailbox address \"%s\" (%s)",
390 return result != NULL;
393 ** ALLOCADDR -- do local allocations of address on demand.
395 ** Also lowercases the host name if requested.
398 ** a -- the address to reallocate.
399 ** flags -- the copy flag (see RF_ definitions in sendmail.h
400 ** for a description).
401 ** paddr -- the printname of the address.
408 ** Copies portions of a into local buffers as requested.
412 allocaddr(a, flags, paddr, e)
419 sm_dprintf("allocaddr(flags=%x, paddr=%s)\n", flags, paddr);
423 if (a->q_user == NULL)
425 if (a->q_host == NULL)
428 if (bitset(RF_COPYPARSE, flags))
430 a->q_host = sm_rpool_strdup_x(e->e_rpool, a->q_host);
431 if (a->q_user != a->q_paddr)
432 a->q_user = sm_rpool_strdup_x(e->e_rpool, a->q_user);
435 if (a->q_paddr == NULL)
436 a->q_paddr = sm_rpool_strdup_x(e->e_rpool, a->q_user);
441 ** PRESCAN -- Prescan name and make it canonical
443 ** Scans a name and turns it into a set of tokens. This process
444 ** deletes blanks and comments (in parentheses) (if the token type
445 ** for left paren is SPC).
447 ** This routine knows about quoted strings and angle brackets.
449 ** There are certain subtleties to this routine. The one that
450 ** comes to mind now is that backslashes on the ends of names
451 ** are silently stripped off; this is intentional. The problem
452 ** is that some versions of sndmsg (like at LBL) set the kill
453 ** character to something other than @ when reading addresses;
454 ** so people type "csvax.eric\@berkeley" -- which screws up the
458 ** addr -- the name to chomp.
459 ** delim -- the delimiter for the address, normally
460 ** '\0' or ','; \0 is accepted in any case.
461 ** If '\t' then we are reading the .cf file.
462 ** pvpbuf -- place to put the saved text -- note that
463 ** the pointers are static.
464 ** pvpbsize -- size of pvpbuf.
465 ** delimptr -- if non-NULL, set to the location of the
466 ** terminating delimiter.
467 ** toktab -- if set, a token table to use for parsing.
468 ** If NULL, use the default table.
469 ** ignore -- if true, ignore unbalanced addresses
472 ** A pointer to a vector of tokens.
476 /* states and character types */
477 #define OPR 0 /* operator */
478 #define ATM 1 /* atom */
479 #define QST 2 /* in quoted string */
480 #define SPC 3 /* chewing up spaces */
481 #define ONE 4 /* pick up one character */
482 #define ILL 5 /* illegal character */
484 #define NSTATES 6 /* number of states */
485 #define TYPE 017 /* mask to select state type */
487 /* meta bits for table */
488 #define M 020 /* meta character; don't pass through */
489 #define B 040 /* cause a break */
490 #define MB M|B /* meta-break */
492 static short StateTab[NSTATES][NSTATES] =
494 /* oldst chtype> OPR ATM QST SPC ONE ILL */
495 /*OPR*/ { OPR|B, ATM|B, QST|B, SPC|MB, ONE|B, ILL|MB },
496 /*ATM*/ { OPR|B, ATM, QST|B, SPC|MB, ONE|B, ILL|MB },
497 /*QST*/ { QST, QST, OPR, QST, QST, QST },
498 /*SPC*/ { OPR, ATM, QST, SPC|M, ONE, ILL|MB },
499 /*ONE*/ { OPR, OPR, OPR, OPR, OPR, ILL|MB },
500 /*ILL*/ { OPR|B, ATM|B, QST|B, SPC|MB, ONE|B, ILL|M }
503 /* these all get modified with the OperatorChars */
505 /* token type table for external strings */
506 unsigned char ExtTokenTab[256] =
508 /* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */
509 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,SPC,SPC,SPC,SPC,SPC,ATM,ATM,
510 /* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */
511 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
512 /* sp ! " # $ % & ' ( ) * + , - . / */
513 SPC,ATM,QST,ATM,ATM,ATM,ATM,ATM, SPC,SPC,ATM,ATM,ATM,ATM,ATM,ATM,
514 /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */
515 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
516 /* @ A B C D E F G H I J K L M N O */
517 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
518 /* P Q R S T U V W X Y Z [ \ ] ^ _ */
519 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
520 /* ` a b c d e f g h i j k l m n o */
521 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
522 /* p q r s t u v w x y z { | } ~ del */
523 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
525 /* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */
526 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
527 /* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */
528 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
529 /* sp ! " # $ % & ' ( ) * + , - . / */
530 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
531 /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */
532 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
533 /* @ A B C D E F G H I J K L M N O */
534 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
535 /* P Q R S T U V W X Y Z [ \ ] ^ _ */
536 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
537 /* ` a b c d e f g h i j k l m n o */
538 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
539 /* p q r s t u v w x y z { | } ~ del */
540 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM
543 /* token type table for internal strings */
544 unsigned char IntTokenTab[256] =
546 /* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */
547 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,SPC,SPC,SPC,SPC,SPC,ATM,ATM,
548 /* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */
549 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
550 /* sp ! " # $ % & ' ( ) * + , - . / */
551 SPC,ATM,QST,ATM,ATM,ATM,ATM,ATM, SPC,SPC,ATM,ATM,ATM,ATM,ATM,ATM,
552 /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */
553 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
554 /* @ A B C D E F G H I J K L M N O */
555 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
556 /* P Q R S T U V W X Y Z [ \ ] ^ _ */
557 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
558 /* ` a b c d e f g h i j k l m n o */
559 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
560 /* p q r s t u v w x y z { | } ~ del */
561 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
563 /* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */
564 OPR,OPR,ONE,OPR,OPR,OPR,OPR,OPR, OPR,OPR,OPR,OPR,OPR,OPR,OPR,OPR,
565 /* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */
566 OPR,OPR,OPR,ONE,ONE,ONE,OPR,OPR, OPR,OPR,OPR,OPR,OPR,OPR,OPR,OPR,
567 /* sp ! " # $ % & ' ( ) * + , - . / */
568 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
569 /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */
570 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
571 /* @ A B C D E F G H I J K L M N O */
572 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
573 /* P Q R S T U V W X Y Z [ \ ] ^ _ */
574 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
575 /* ` a b c d e f g h i j k l m n o */
576 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
577 /* p q r s t u v w x y z { | } ~ del */
578 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ONE
581 /* token type table for MIME parsing */
582 unsigned char MimeTokenTab[256] =
584 /* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */
585 ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL, ILL,SPC,SPC,SPC,SPC,SPC,ILL,ILL,
586 /* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */
587 ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL, ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL,
588 /* sp ! " # $ % & ' ( ) * + , - . / */
589 SPC,ATM,QST,ATM,ATM,ATM,ATM,ATM, SPC,SPC,ATM,ATM,OPR,ATM,ATM,OPR,
590 /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */
591 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,OPR,OPR,OPR,OPR,OPR,OPR,
592 /* @ A B C D E F G H I J K L M N O */
593 OPR,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
594 /* P Q R S T U V W X Y Z [ \ ] ^ _ */
595 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,OPR,OPR,OPR,ATM,ATM,
596 /* ` a b c d e f g h i j k l m n o */
597 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
598 /* p q r s t u v w x y z { | } ~ del */
599 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
601 /* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */
602 ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL, ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL,
603 /* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */
604 ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL, ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL,
605 /* sp ! " # $ % & ' ( ) * + , - . / */
606 ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL, ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL,
607 /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */
608 ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL, ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL,
609 /* @ A B C D E F G H I J K L M N O */
610 ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL, ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL,
611 /* P Q R S T U V W X Y Z [ \ ] ^ _ */
612 ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL, ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL,
613 /* ` a b c d e f g h i j k l m n o */
614 ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL, ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL,
615 /* p q r s t u v w x y z { | } ~ del */
616 ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL, ILL,ILL,ILL,ILL,ILL,ILL,ILL,ONE
619 /* token type table: don't strip comments */
620 unsigned char TokTypeNoC[256] =
622 /* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */
623 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,SPC,SPC,SPC,SPC,SPC,ATM,ATM,
624 /* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */
625 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
626 /* sp ! " # $ % & ' ( ) * + , - . / */
627 SPC,ATM,QST,ATM,ATM,ATM,ATM,ATM, OPR,OPR,ATM,ATM,ATM,ATM,ATM,ATM,
628 /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */
629 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
630 /* @ A B C D E F G H I J K L M N O */
631 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
632 /* P Q R S T U V W X Y Z [ \ ] ^ _ */
633 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
634 /* ` a b c d e f g h i j k l m n o */
635 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
636 /* p q r s t u v w x y z { | } ~ del */
637 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
639 /* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */
640 OPR,OPR,ONE,OPR,OPR,OPR,OPR,OPR, OPR,OPR,OPR,OPR,OPR,OPR,OPR,OPR,
641 /* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */
642 OPR,OPR,OPR,ONE,ONE,ONE,OPR,OPR, OPR,OPR,OPR,OPR,OPR,OPR,OPR,OPR,
643 /* sp ! " # $ % & ' ( ) * + , - . / */
644 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
645 /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */
646 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
647 /* @ A B C D E F G H I J K L M N O */
648 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
649 /* P Q R S T U V W X Y Z [ \ ] ^ _ */
650 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
651 /* ` a b c d e f g h i j k l m n o */
652 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
653 /* p q r s t u v w x y z { | } ~ del */
654 ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ONE
658 #define NOCHAR (-1) /* signal nothing in lookahead token */
661 prescan(addr, delim, pvpbuf, pvpbsize, delimptr, toktab, ignore)
667 unsigned char *toktab;
681 char *saveto = CurEnv->e_to;
682 static char *av[MAXATOM + 1];
683 static bool firsttime = true;
687 /* initialize the token type table */
691 if (OperatorChars == NULL)
694 OperatorChars = macvalue('o', CurEnv);
695 if (OperatorChars == NULL)
696 OperatorChars = ".:@[]";
698 expand(OperatorChars, obuf, sizeof(obuf) - sizeof(DELIMCHARS),
700 (void) sm_strlcat(obuf, DELIMCHARS, sizeof(obuf));
701 for (p = obuf; *p != '\0'; p++)
703 if (IntTokenTab[*p & 0xff] == ATM)
704 IntTokenTab[*p & 0xff] = OPR;
705 if (ExtTokenTab[*p & 0xff] == ATM)
706 ExtTokenTab[*p & 0xff] = OPR;
707 if (TokTypeNoC[*p & 0xff] == ATM)
708 TokTypeNoC[*p & 0xff] = OPR;
712 toktab = ExtTokenTab;
714 /* make sure error messages don't have garbage on them */
719 route_syntax = false;
729 sm_dprintf("prescan: ");
730 xputs(sm_debug_file(), p);
740 /* store away any old lookahead character */
741 if (c != NOCHAR && !bslashmode)
743 /* see if there is room */
744 if (q >= &pvpbuf[pvpbsize - 5])
747 usrerr("553 5.1.1 Address too long");
748 if (strlen(addr) > MAXNAME)
749 addr[MAXNAME] = '\0';
751 if (delimptr != NULL)
757 CurEnv->e_to = saveto;
761 /* squirrel it away */
763 if ((char) c == (char) -1 && !tTd(82, 101) &&
766 #endif /* !ALLOW_255 */
770 /* read a new input character */
774 /* diagnose and patch up bad syntax */
777 else if (state == QST)
779 usrerr("553 Unbalanced '\"'");
782 else if (cmntcnt > 0)
784 usrerr("553 Unbalanced '('");
787 else if (anglecnt > 0)
790 usrerr("553 Unbalanced '<'");
797 else if (c == delim && cmntcnt <= 0 && state != QST)
802 /* special case for better error management */
803 if (delim == ',' && !route_syntax && !ignore)
805 usrerr("553 Unbalanced '<'");
812 sm_dprintf("c=%c, s=%d; ", c, state);
814 /* chew up special characters */
820 /* kludge \! for naive users */
826 else if (c != '!' || state == QST)
828 /* see if there is room */
829 if (q >= &pvpbuf[pvpbsize - 5])
840 else if (state == QST)
843 /* do nothing, just avoid next clauses */
845 else if (c == '(' && toktab['('] == SPC)
850 else if (c == ')' && toktab['('] == SPC)
856 usrerr("553 Unbalanced ')'");
863 else if (cmntcnt > 0)
872 while (isascii(*ptr) && isspace(*ptr))
883 usrerr("553 Unbalanced '>'");
889 route_syntax = false;
891 else if (delim == ' ' && isascii(c) && isspace(c))
897 /* see if this is end of input */
898 if (c == delim && anglecnt <= 0 && state != QST)
901 newstate = StateTab[state][toktab[c & 0xff]];
903 sm_dprintf("ns=%02o\n", newstate);
904 state = newstate & TYPE;
907 if (isascii(c) && isprint(c))
908 usrerr("553 Illegal character %c", c);
910 usrerr("553 Illegal character 0x%02x",
913 if (bitset(M, newstate))
915 if (bitset(B, newstate))
922 /* see if there is room */
923 if (q >= &pvpbuf[pvpbsize - 5])
929 xputs(sm_debug_file(), tok);
932 if (avp >= &av[MAXATOM])
934 usrerr("553 5.1.0 prescan: too many tokens");
937 if (q - tok > MAXNAME)
939 usrerr("553 5.1.0 prescan: token too long");
944 } while (c != '\0' && (c != delim || anglecnt > 0));
946 if (delimptr != NULL)
954 sm_dprintf("prescan==>");
955 printav(sm_debug_file(), av);
957 CurEnv->e_to = saveto;
961 sm_dprintf("prescan: null leading token\n");
967 ** REWRITE -- apply rewrite rules to token vector.
969 ** This routine is an ordered production system. Each rewrite
970 ** rule has a LHS (called the pattern) and a RHS (called the
971 ** rewrite); 'rwr' points the the current rewrite rule.
973 ** For each rewrite rule, 'avp' points the address vector we
974 ** are trying to match against, and 'pvp' points to the pattern.
975 ** If pvp points to a special match value (MATCHZANY, MATCHANY,
976 ** MATCHONE, MATCHCLASS, MATCHNCLASS) then the address in avp
977 ** matched is saved away in the match vector (pointed to by 'mvp').
979 ** When a match between avp & pvp does not match, we try to
980 ** back out. If we back up over MATCHONE, MATCHCLASS, or MATCHNCLASS
981 ** we must also back out the match in mvp. If we reach a
982 ** MATCHANY or MATCHZANY we just extend the match and start
985 ** When we finally match, we rewrite the address vector
986 ** and try over again.
989 ** pvp -- pointer to token vector.
990 ** ruleset -- the ruleset to use for rewriting.
991 ** reclevel -- recursion level (to catch loops).
992 ** e -- the current envelope.
993 ** maxatom -- maximum length of buffer (usually MAXATOM)
996 ** A status code. If EX_TEMPFAIL, higher level code should
1005 char **match_first; /* first token matched */
1006 char **match_last; /* last token matched */
1007 char **match_pattern; /* pointer to pattern */
1011 rewrite(pvp, ruleset, reclevel, e, maxatom)
1015 register ENVELOPE *e;
1018 register char *ap; /* address pointer */
1019 register char *rp; /* rewrite pointer */
1020 register char *rulename; /* ruleset name */
1021 register char *prefix;
1022 register char **avp; /* address vector pointer */
1023 register char **rvp; /* rewrite vector pointer */
1024 register struct match *mlp; /* cur ptr into mlist */
1025 register struct rewrite *rwr; /* pointer to current rewrite rule */
1026 int ruleno; /* current rule number */
1027 int rstat = EX_OK; /* return status */
1029 struct match mlist[MAXMATCH]; /* stores match on LHS */
1030 char *npvp[MAXATOM + 1]; /* temporary space for rebuild */
1035 ** mlp will not exceed mlist[] because readcf enforces
1036 ** the upper limit of entries when reading rulesets.
1039 if (ruleset < 0 || ruleset >= MAXRWSETS)
1041 syserr("554 5.3.5 rewrite: illegal ruleset number %d", ruleset);
1044 rulename = RuleSetNames[ruleset];
1045 if (rulename == NULL)
1047 (void) sm_snprintf(name, sizeof(name), "%d", ruleset);
1050 if (OpMode == MD_TEST)
1053 prefix = "rewrite: ruleset ";
1054 if (OpMode == MD_TEST)
1056 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
1057 "%s%-16.16s input:", prefix, rulename);
1058 printav(smioout, pvp);
1060 else if (tTd(21, 1))
1062 sm_dprintf("%s%-16.16s input:", prefix, rulename);
1063 printav(sm_debug_file(), pvp);
1065 if (reclevel++ > MaxRuleRecursion)
1067 syserr("rewrite: excessive recursion (max %d), ruleset %s",
1068 MaxRuleRecursion, rulename);
1077 ** Run through the list of rewrite rules, applying
1083 for (rwr = RewriteRules[ruleset]; rwr != NULL; )
1087 /* if already canonical, quit now */
1088 if (pvp[0] != NULL && (pvp[0][0] & 0377) == CANONNET)
1094 sm_dprintf("-----trying rule (line %d):",
1097 sm_dprintf("-----trying rule:");
1098 printav(sm_debug_file(), rwr->r_lhs);
1101 /* try to match on this rule */
1105 if (++loopcount > 100)
1107 syserr("554 5.3.5 Infinite loop in ruleset %s, rule %d",
1111 sm_dprintf("workspace: ");
1112 printav(sm_debug_file(), pvp);
1117 while ((ap = *avp) != NULL || *rvp != NULL)
1122 sm_dprintf("ADVANCE rp=");
1123 xputs(sm_debug_file(), rp);
1124 sm_dprintf(", ap=");
1125 xputs(sm_debug_file(), ap);
1130 /* end-of-pattern before end-of-address */
1134 (rp[0] & 0377) != MATCHZANY &&
1135 (rp[0] & 0377) != MATCHZERO)
1137 /* end-of-input with patterns left */
1141 switch (rp[0] & 0377)
1144 /* match any phrase in a class */
1145 mlp->match_pattern = rvp;
1146 mlp->match_first = avp;
1151 mlp->match_last = avp++;
1152 cataddr(mlp->match_first, mlp->match_last,
1153 buf, sizeof(buf), '\0', true);
1154 if (!wordinclass(buf, rp[1]))
1158 sm_dprintf("EXTEND rp=");
1159 xputs(sm_debug_file(), rp);
1160 sm_dprintf(", ap=");
1161 xputs(sm_debug_file(), ap);
1167 sm_dprintf("CLMATCH\n");
1172 /* match any token not in a class */
1173 if (wordinclass(ap, rp[1]))
1180 /* match exactly one token */
1181 mlp->match_pattern = rvp;
1182 mlp->match_first = avp;
1183 mlp->match_last = avp++;
1188 /* match zero or more tokens */
1189 mlp->match_pattern = rvp;
1190 mlp->match_first = avp;
1191 mlp->match_last = avp - 1;
1196 /* match zero tokens */
1201 ** Match against run-time macro.
1202 ** This algorithm is broken for the
1203 ** general case (no recursive macros,
1204 ** improper tokenization) but should
1205 ** work for the usual cases.
1208 ap = macvalue(rp[1], e);
1209 mlp->match_first = avp;
1211 sm_dprintf("rewrite: LHS $&{%s} => \"%s\"\n",
1213 ap == NULL ? "(NULL)" : ap);
1220 sm_strncasecmp(ap, *avp,
1224 avp = mlp->match_first;
1227 ap += strlen(*avp++);
1234 /* must have exact match */
1235 if (sm_strcasecmp(rp, ap))
1241 /* successful match on this token */
1246 /* match failed -- back up */
1247 while (--mlp >= mlist)
1249 rvp = mlp->match_pattern;
1251 avp = mlp->match_last + 1;
1256 sm_dprintf("BACKUP rp=");
1257 xputs(sm_debug_file(), rp);
1258 sm_dprintf(", ap=");
1259 xputs(sm_debug_file(), ap);
1265 /* run off the end -- back up again */
1269 if ((rp[0] & 0377) == MATCHANY ||
1270 (rp[0] & 0377) == MATCHZANY)
1272 /* extend binding and continue */
1273 mlp->match_last = avp++;
1278 if ((rp[0] & 0377) == MATCHCLASS)
1280 /* extend binding and try again */
1281 mlp->match_last = avp;
1288 /* total failure to match */
1294 ** See if we successfully matched
1297 if (mlp < mlist || *rvp != NULL)
1300 sm_dprintf("----- rule fails\n");
1310 sm_dprintf("-----rule matches:");
1311 printav(sm_debug_file(), rvp);
1317 if ((rp[0] & 0377) == CANONUSER)
1324 else if ((rp[0] & 0377) == CANONHOST)
1332 for (avp = npvp; *rvp != NULL; rvp++)
1334 register struct match *m;
1338 if ((rp[0] & 0377) == MATCHREPL)
1340 /* substitute from LHS */
1341 m = &mlist[rp[1] - '1'];
1342 if (m < mlist || m >= mlp)
1344 syserr("554 5.3.5 rewrite: ruleset %s: replacement $%c out of bounds",
1350 sm_dprintf("$%c:", rp[1]);
1351 pp = m->match_first;
1352 while (pp <= m->match_last)
1354 sm_dprintf(" %p=\"", *pp);
1356 sm_dprintf("%s\"", *pp++);
1360 pp = m->match_first;
1361 while (pp <= m->match_last)
1363 if (avp >= &npvp[maxatom])
1370 /* some sort of replacement */
1371 if (avp >= &npvp[maxatom])
1374 syserr("554 5.3.0 rewrite: expansion too long");
1376 sm_syslog(LOG_ERR, e->e_id,
1377 "rewrite: expansion too long, ruleset=%s, ruleno=%d",
1381 if ((rp[0] & 0377) != MACRODEXPAND)
1383 /* vanilla replacement from RHS */
1388 /* $&{x} replacement */
1389 char *mval = macvalue(rp[1], e);
1392 static size_t pvpb1_size = 0;
1393 static char **pvpb1 = NULL;
1394 char pvpbuf[PSBUFSIZE];
1397 sm_dprintf("rewrite: RHS $&{%s} => \"%s\"\n",
1399 mval == NULL ? "(NULL)" : mval);
1400 if (mval == NULL || *mval == '\0')
1403 /* save the remainder of the input */
1404 for (xpvp = pvp; *xpvp != NULL; xpvp++)
1405 trsize += sizeof(*xpvp);
1406 if (trsize > pvpb1_size)
1411 sm_pmalloc_x(trsize);
1412 pvpb1_size = trsize;
1415 memmove((char *) pvpb1,
1419 /* scan the new replacement */
1420 xpvp = prescan(mval, '\0', pvpbuf,
1421 sizeof(pvpbuf), NULL,
1425 /* prescan pre-printed error */
1429 /* insert it into the output stream */
1430 while (*xpvp != NULL)
1433 sm_dprintf(" ... %s\n",
1435 *avp++ = sm_rpool_strdup_x(
1437 if (avp >= &npvp[maxatom])
1442 sm_dprintf(" ... DONE\n");
1444 /* restore the old trailing input */
1445 memmove((char *) pvp,
1454 ** Check for any hostname/keyword lookups.
1457 for (rvp = npvp; *rvp != NULL; rvp++)
1471 char *pvpb1[MAXATOM + 1];
1472 char *argvect[MAX_MAP_ARGS];
1473 char pvpbuf[PSBUFSIZE];
1477 if ((rvp[0][0] & 0377) == HOSTBEGIN)
1482 else if ((rvp[0][0] & 0377) == LOOKUPBEGIN)
1484 endtoken = LOOKUPEND;
1486 if (mapname == NULL)
1488 syserr("554 5.3.0 rewrite: missing mapname");
1497 ** Got a hostname/keyword lookup.
1499 ** This could be optimized fairly easily.
1502 map = stab(mapname, ST_MAP, ST_FIND);
1504 syserr("554 5.3.0 rewrite: map %s not found",
1507 /* extract the match part */
1509 if (key_rvp == NULL)
1511 syserr("554 5.3.0 rewrite: missing key for map %s",
1520 while (*rvp != NULL && ((rvp[0][0] & 0377) != endtoken))
1522 int nodetype = rvp[0][0] & 0377;
1524 if (nodetype != CANONHOST &&
1525 nodetype != CANONUSER)
1535 cataddr(xpvp, NULL, replac,
1536 &pvpbuf[sizeof(pvpbuf)] - replac,
1539 &argvect[MAX_MAP_ARGS - 1])
1540 *++arg_rvp = replac;
1541 replac += strlen(replac) + 1;
1559 cataddr(xpvp, NULL, replac,
1560 &pvpbuf[sizeof(pvpbuf)] - replac,
1562 if (arg_rvp < &argvect[MAX_MAP_ARGS - 1])
1563 *++arg_rvp = replac;
1565 if (arg_rvp >= &argvect[MAX_MAP_ARGS - 1])
1566 argvect[MAX_MAP_ARGS - 1] = NULL;
1570 /* save the remainder of the input string */
1571 trsize = (avp - rvp + 1) * sizeof(*rvp);
1572 memmove((char *) pvpb1, (char *) rvp, trsize);
1575 cataddr(key_rvp, NULL, cbuf, sizeof(cbuf),
1576 map == NULL ? '\0' : map->s_map.map_spacesub,
1579 replac = map_lookup(map, cbuf, argvect, &rstat, e);
1580 external = replac != NULL;
1582 /* if no replacement, use default */
1583 if (replac == NULL && default_rvp != NULL)
1585 /* create the default */
1586 cataddr(default_rvp, NULL, cbuf, sizeof(cbuf),
1595 else if (*replac == '\0')
1597 /* null replacement */
1603 /* scan the new replacement */
1604 xpvp = prescan(replac, '\0', pvpbuf,
1605 sizeof(pvpbuf), NULL,
1606 external ? NULL : IntTokenTab,
1610 /* prescan already printed error */
1615 /* append it to the token list */
1616 for (avp = hbrvp; *xpvp != NULL; xpvp++)
1618 *avp++ = sm_rpool_strdup_x(e->e_rpool, *xpvp);
1619 if (avp >= &npvp[maxatom])
1623 /* restore the old trailing information */
1625 for (xpvp = pvpb1; (*avp++ = *xpvp++) != NULL; )
1626 if (avp >= &npvp[maxatom])
1631 ** Check for subroutine calls.
1634 status = callsubr(npvp, reclevel, e);
1635 if (rstat == EX_OK || status == EX_TEMPFAIL)
1638 /* copy vector back into original space. */
1639 for (avp = npvp; *avp++ != NULL;)
1641 memmove((char *) pvp, (char *) npvp,
1642 (int) (avp - npvp) * sizeof(*avp));
1646 sm_dprintf("rewritten as:");
1647 printav(sm_debug_file(), pvp);
1651 if (OpMode == MD_TEST)
1653 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
1654 "%s%-16.16s returns:", prefix, rulename);
1655 printav(smioout, pvp);
1657 else if (tTd(21, 1))
1659 sm_dprintf("%s%-16.16s returns:", prefix, rulename);
1660 printav(sm_debug_file(), pvp);
1665 ** CALLSUBR -- call subroutines in rewrite vector
1668 ** pvp -- pointer to token vector.
1669 ** reclevel -- the current recursion level.
1670 ** e -- the current envelope.
1673 ** The status from the subroutine call.
1680 callsubr(pvp, reclevel, e)
1692 int subrnumber[MAX_SUBR];
1693 int subrindex[MAX_SUBR];
1698 ** Look for subroutine calls in pvp, collect them into subr*[]
1699 ** We will perform the calls in the next loop, because we will
1700 ** call the "last" subroutine first to avoid recursive calls
1701 ** and too much copying.
1704 for (avp = pvp, j = 0; *avp != NULL; avp++, j++)
1706 if ((avp[0][0] & 0377) == CALLSUBR && avp[1] != NULL)
1708 stripquotes(avp[1]);
1709 subr = strtorwset(avp[1], NULL, ST_FIND);
1712 syserr("554 5.3.5 Unknown ruleset %s", avp[1]);
1717 ** XXX instead of doing this we could optimize
1718 ** the rules after reading them: just remove
1719 ** calls to empty rulesets
1722 /* subroutine is an empty ruleset? don't call it */
1723 if (RewriteRules[subr] == NULL)
1726 sm_dprintf("-----skip subr %s (%d)\n",
1728 for (i = 2; avp[i] != NULL; i++)
1729 avp[i - 2] = avp[i];
1733 if (++nsubr >= MAX_SUBR)
1735 syserr("554 5.3.0 Too many subroutine calls (%d max)",
1739 subrnumber[nsubr] = subr;
1740 subrindex[nsubr] = j;
1745 ** Perform the actual subroutines calls, "last" one first, i.e.,
1746 ** go from the right to the left through all calls,
1747 ** do the rewriting in place.
1750 for (; nsubr > 0; nsubr--)
1752 subr = subrnumber[nsubr];
1753 avp = pvp + subrindex[nsubr];
1755 /* remove the subroutine call and name */
1756 for (i = 2; avp[i] != NULL; i++)
1757 avp[i - 2] = avp[i];
1761 ** Now we need to call the ruleset specified for
1762 ** the subroutine. We can do this in place since
1763 ** we call the "last" subroutine first.
1766 status = rewrite(avp, subr, reclevel, e,
1767 MAXATOM - subrindex[nsubr]);
1768 if (status != EX_OK && status != EX_TEMPFAIL)
1770 if (rstat == EX_OK || status == EX_TEMPFAIL)
1776 ** MAP_LOOKUP -- do lookup in map
1779 ** smap -- the map to use for the lookup.
1780 ** key -- the key to look up.
1781 ** argvect -- arguments to pass to the map lookup.
1782 ** pstat -- a pointer to an integer in which to store the
1783 ** status from the lookup.
1784 ** e -- the current envelope.
1787 ** The result of the lookup.
1788 ** NULL -- if there was no data for the given key.
1792 map_lookup(smap, key, argvect, pstat, e)
1799 auto int status = EX_OK;
1809 if (e->e_sendmode == SM_DEFER &&
1810 bitset(MF_DEFER, map->map_mflags))
1812 /* don't do any map lookups */
1814 sm_dprintf("map_lookup(%s, %s) => DEFERRED\n",
1816 *pstat = EX_TEMPFAIL;
1820 if (!bitset(MF_KEEPQUOTES, map->map_mflags))
1825 sm_dprintf("map_lookup(%s, ", smap->s_name);
1826 xputs(sm_debug_file(), key);
1831 for (i = 0; argvect[i] != NULL; i++)
1832 sm_dprintf(", %%%d=%s", i, argvect[i]);
1834 sm_dprintf(") => ");
1836 replac = (*map->map_class->map_lookup)(map, key, argvect, &status);
1838 sm_dprintf("%s (%d)\n",
1839 replac != NULL ? replac : "NOT FOUND",
1842 /* should recover if status == EX_TEMPFAIL */
1843 if (status == EX_TEMPFAIL && !bitset(MF_NODEFER, map->map_mflags))
1845 *pstat = EX_TEMPFAIL;
1847 sm_dprintf("map_lookup(%s, %s) tempfail: errno=%d\n",
1848 smap->s_name, key, errno);
1849 if (e->e_message == NULL)
1853 (void) sm_snprintf(mbuf, sizeof(mbuf),
1854 "%.80s map: lookup (%s): deferred",
1856 shortenstring(key, MAXSHORTSTR));
1857 e->e_message = sm_rpool_strdup_x(e->e_rpool, mbuf);
1860 if (status == EX_TEMPFAIL && map->map_tapp != NULL)
1862 size_t i = strlen(key) + strlen(map->map_tapp) + 1;
1863 static char *rwbuf = NULL;
1864 static size_t rwbuflen = 0;
1871 rwbuf = (char *) sm_pmalloc_x(rwbuflen);
1873 (void) sm_strlcpyn(rwbuf, rwbuflen, 2, key, map->map_tapp);
1875 sm_dprintf("map_lookup tempfail: returning \"%s\"\n",
1882 ** INITERRMAILERS -- initialize error and discard mailers
1891 ** initializes error and discard mailers.
1894 static MAILER discardmailer;
1895 static MAILER errormailer;
1896 static char *discardargv[] = { "DISCARD", NULL };
1897 static char *errorargv[] = { "ERROR", NULL };
1902 if (discardmailer.m_name == NULL)
1904 /* initialize the discard mailer */
1905 discardmailer.m_name = "*discard*";
1906 discardmailer.m_mailer = "DISCARD";
1907 discardmailer.m_argv = discardargv;
1909 if (errormailer.m_name == NULL)
1911 /* initialize the bogus mailer */
1912 errormailer.m_name = "*error*";
1913 errormailer.m_mailer = "ERROR";
1914 errormailer.m_argv = errorargv;
1918 ** BUILDADDR -- build address from token vector.
1921 ** tv -- token vector.
1922 ** a -- pointer to address descriptor to fill.
1923 ** If NULL, one will be allocated.
1924 ** flags -- info regarding whether this is a sender or
1926 ** e -- the current envelope.
1929 ** NULL if there was an error.
1936 static struct errcodes
1938 char *ec_name; /* name of error code */
1939 int ec_code; /* numeric code */
1942 { "usage", EX_USAGE },
1943 { "nouser", EX_NOUSER },
1944 { "nohost", EX_NOHOST },
1945 { "unavailable", EX_UNAVAILABLE },
1946 { "software", EX_SOFTWARE },
1947 { "tempfail", EX_TEMPFAIL },
1948 { "protocol", EX_PROTOCOL },
1949 { "config", EX_CONFIG },
1950 { NULL, EX_UNAVAILABLE }
1954 buildaddr(tv, a, flags, e)
1956 register ADDRESS *a;
1958 register ENVELOPE *e;
1960 bool tempfail = false;
1963 register struct mailer *m;
1967 char hbuf[MAXNAME + 1];
1968 static char ubuf[MAXNAME + 2];
1972 sm_dprintf("buildaddr, flags=%x, tv=", flags);
1973 printav(sm_debug_file(), tv);
1978 a = (ADDRESS *) sm_rpool_malloc_x(e->e_rpool, sizeof(*a));
1979 memset((char *) a, '\0', sizeof(*a));
1982 /* set up default error return flags */
1983 a->q_flags |= DefaultNotify;
1985 /* figure out what net/mailer to use */
1986 if (*tv == NULL || (**tv & 0377) != CANONNET)
1988 syserr("554 5.3.5 buildaddr: no mailer in parsed address");
1991 ** ExitStat may have been set by an earlier map open
1992 ** failure (to a permanent error (EX_OSERR) in syserr())
1993 ** so we also need to check if this particular $#error
1994 ** return wanted a 4XX failure.
1996 ** XXX the real fix is probably to set ExitStat correctly,
1997 ** i.e., to EX_TEMPFAIL if the map open is just a temporary
2001 if (ExitStat == EX_TEMPFAIL || tempfail)
2002 a->q_state = QS_QUEUEUP;
2005 a->q_state = QS_BADADDR;
2006 a->q_mailer = &errormailer;
2013 /* extract host and user portions */
2014 if (*++tv != NULL && (**tv & 0377) == CANONHOST)
2022 while (*tv != NULL && (**tv & 0377) != CANONUSER)
2029 syserr("554 5.3.5 buildaddr: no user");
2034 else if (hostp != NULL)
2035 cataddr(hostp, tv - 1, hbuf, sizeof(hbuf), '\0', false);
2036 cataddr(++tv, NULL, ubuf, sizeof(ubuf), ' ', false);
2039 /* save away the host name */
2040 if (sm_strcasecmp(mname, "error") == 0)
2042 /* Set up triplet for use by -bv */
2043 a->q_mailer = &errormailer;
2044 a->q_user = sm_rpool_strdup_x(e->e_rpool, ubuf);
2045 /* XXX wrong place? */
2049 register struct errcodes *ep;
2051 a->q_host = sm_rpool_strdup_x(e->e_rpool, hbuf);
2052 if (strchr(hbuf, '.') != NULL)
2054 a->q_status = sm_rpool_strdup_x(e->e_rpool,
2056 setstat(dsntoexitstat(hbuf));
2058 else if (isascii(hbuf[0]) && isdigit(hbuf[0]))
2060 setstat(atoi(hbuf));
2064 for (ep = ErrorCodes; ep->ec_name != NULL; ep++)
2065 if (sm_strcasecmp(ep->ec_name, hbuf) == 0)
2067 setstat(ep->ec_code);
2073 setstat(EX_UNAVAILABLE);
2076 if (ISSMTPCODE(ubuf) && ubuf[3] == ' ')
2081 if ((off = isenhsc(ubuf + 4, ' ')) > 0)
2083 ubuf[off + 4] = '\0';
2091 (void) sm_strlcpyn(fmt, sizeof(fmt), 2, ubuf, " %s");
2093 usrerr(fmt, ubuf + off);
2094 else if (isenhsc(hbuf, '\0') > 0)
2095 usrerrenh(hbuf, fmt, ubuf + off);
2097 usrerr(fmt, ubuf + off);
2098 /* XXX ubuf[off - 1] = ' '; */
2104 usrerr("553 5.3.0 %s", ubuf);
2109 for (mp = Mailer; (m = *mp++) != NULL; )
2111 if (sm_strcasecmp(m->m_name, mname) == 0)
2116 syserr("554 5.3.5 buildaddr: unknown mailer %s", mname);
2121 /* figure out what host (if any) */
2124 if (!bitnset(M_LOCALMAILER, m->m_flags))
2126 syserr("554 5.3.5 buildaddr: no host");
2132 a->q_host = sm_rpool_strdup_x(e->e_rpool, hbuf);
2134 /* figure out the user */
2136 if (bitnset(M_CHECKUDB, m->m_flags) && *p == '@')
2141 a->q_flags |= QNOTREMOTE;
2144 /* do special mapping for local mailer */
2147 if (*p == '|' && bitnset(M_CHECKPROG, m->m_flags))
2148 a->q_mailer = m = ProgMailer;
2149 else if (*p == '/' && bitnset(M_CHECKFILE, m->m_flags))
2150 a->q_mailer = m = FileMailer;
2151 else if (*p == ':' && bitnset(M_CHECKINCLUDE, m->m_flags))
2153 /* may be :include: */
2155 if (sm_strncasecmp(ubuf, ":include:", 9) == 0)
2157 /* if :include:, don't need further rewriting */
2158 a->q_mailer = m = InclMailer;
2159 a->q_user = sm_rpool_strdup_x(e->e_rpool, &ubuf[9]);
2164 /* rewrite according recipient mailer rewriting rules */
2165 macdefine(&e->e_macro, A_PERM, 'h', a->q_host);
2167 if (ConfigLevel >= 10 ||
2168 !bitset(RF_SENDERADDR|RF_HEADERADDR, flags))
2170 /* sender addresses done later */
2171 (void) rewrite(tv, 2, 0, e, maxatom);
2172 if (m->m_re_rwset > 0)
2173 (void) rewrite(tv, m->m_re_rwset, 0, e, maxatom);
2175 (void) rewrite(tv, 4, 0, e, maxatom);
2177 /* save the result for the command line/RCPT argument */
2178 cataddr(tv, NULL, ubuf, sizeof(ubuf), '\0', true);
2179 a->q_user = sm_rpool_strdup_x(e->e_rpool, ubuf);
2182 ** Do mapping to lower case as requested by mailer
2185 if (a->q_host != NULL && !bitnset(M_HST_UPPER, m->m_flags))
2186 makelower(a->q_host);
2187 if (!bitnset(M_USR_UPPER, m->m_flags))
2188 makelower(a->q_user);
2192 sm_dprintf("buildaddr => ");
2193 printaddr(sm_debug_file(), a, false);
2199 ** CATADDR -- concatenate pieces of addresses (putting in <LWSP> subs)
2202 ** pvp -- parameter vector to rebuild.
2203 ** evp -- last parameter to include. Can be NULL to
2205 ** buf -- buffer to build the string into.
2206 ** sz -- size of buf.
2207 ** spacesub -- the space separator character;
2209 ** NOSPACESEP: no separator
2210 ** external -- convert to external form?
2211 ** (no metacharacters; METAQUOTEs removed, see below)
2220 ** There are two formats for strings: internal and external.
2221 ** The external format is just an eight-bit clean string (no
2222 ** null bytes, everything else OK). The internal format can
2223 ** include sendmail metacharacters. The special character
2224 ** METAQUOTE essentially quotes the character following, stripping
2225 ** it of all special semantics.
2227 ** The cataddr routine needs to be aware of whether it is producing
2228 ** an internal or external form as output (it only takes internal
2231 ** The parseaddr routine has a similar issue on input, but that
2232 ** is flagged on the basis of which token table is passed in.
2236 cataddr(pvp, evp, buf, sz, spacesub, external)
2244 bool oatomtok, natomtok;
2247 oatomtok = natomtok = false;
2250 sm_dprintf("cataddr(%d) <==", external);
2251 printav(sm_debug_file(), pvp);
2257 if (spacesub == '\0')
2258 spacesub = SpaceSub;
2267 while (*pvp != NULL && sz > 0)
2271 natomtok = (IntTokenTab[**pvp & 0xff] == ATM);
2272 if (oatomtok && natomtok && spacesub != NOSPACESEP)
2278 for (q = *pvp; *q != '\0'; )
2287 ** If the current character (c) is METAQUOTE and we
2288 ** want the "external" form and the next character
2289 ** is not NUL, then overwrite METAQUOTE with that
2290 ** character (i.e., METAQUOTE ch is changed to
2291 ** ch). p[-1] is used because p is advanced (above).
2294 if ((c & 0377) == METAQUOTE && external && *q != '\0')
2299 oatomtok = natomtok;
2306 ** Silently truncate long strings: even though this doesn't
2307 ** seem like a good idea it is necessary because header checks
2308 ** send the whole header value to rscheck() and hence rewrite().
2309 ** The latter however sometimes uses a "short" buffer (e.g.,
2310 ** cbuf[MAXNAME + 1]) to call cataddr() which then triggers this
2311 ** error function. One possible fix to the problem is to pass
2312 ** flags to rscheck() and rewrite() to distinguish the various
2313 ** calls and only trigger the error if necessary. For now just
2314 ** undo the change from 8.13.0.
2318 usrerr("cataddr: string too long");
2323 sm_dprintf(" cataddr => %s\n", str2prt(buf));
2327 ** SAMEADDR -- Determine if two addresses are the same
2329 ** This is not just a straight comparison -- if the mailer doesn't
2330 ** care about the host we just ignore it, etc.
2333 ** a, b -- pointers to the internal forms to compare.
2336 ** true -- they represent the same mailbox.
2337 ** false -- they don't.
2345 register ADDRESS *a;
2346 register ADDRESS *b;
2348 register ADDRESS *ca, *cb;
2350 /* if they don't have the same mailer, forget it */
2351 if (a->q_mailer != b->q_mailer)
2355 ** Addresses resolving to error mailer
2356 ** should not be considered identical
2359 if (a->q_mailer == &errormailer)
2362 /* if the user isn't the same, we can drop out */
2363 if (strcmp(a->q_user, b->q_user) != 0)
2366 /* do the required flags match? */
2367 if (!ADDR_FLAGS_MATCH(a, b))
2370 /* if we have good uids for both but they differ, these are different */
2371 if (a->q_mailer == ProgMailer)
2375 if (ca != NULL && cb != NULL &&
2376 bitset(QGOODUID, ca->q_flags & cb->q_flags) &&
2377 ca->q_uid != cb->q_uid)
2381 /* otherwise compare hosts (but be careful for NULL ptrs) */
2382 if (a->q_host == b->q_host)
2384 /* probably both null pointers */
2387 if (a->q_host == NULL || b->q_host == NULL)
2389 /* only one is a null pointer */
2392 if (strcmp(a->q_host, b->q_host) != 0)
2398 ** PRINTADDR -- print address (for debugging)
2401 ** a -- the address to print
2402 ** follow -- follow the q_next chain.
2414 unsigned long qf_bit;
2417 /* :'a,.s;^#define \(Q[A-Z]*\) .*; { "\1", \1 },; */
2418 static struct qflags AddressFlags[] =
2420 { "QGOODUID", QGOODUID },
2421 { "QPRIMARY", QPRIMARY },
2422 { "QNOTREMOTE", QNOTREMOTE },
2423 { "QSELFREF", QSELFREF },
2424 { "QBOGUSSHELL", QBOGUSSHELL },
2425 { "QUNSAFEADDR", QUNSAFEADDR },
2426 { "QPINGONSUCCESS", QPINGONSUCCESS },
2427 { "QPINGONFAILURE", QPINGONFAILURE },
2428 { "QPINGONDELAY", QPINGONDELAY },
2429 { "QHASNOTIFY", QHASNOTIFY },
2430 { "QRELAYED", QRELAYED },
2431 { "QEXPANDED", QEXPANDED },
2432 { "QDELIVERED", QDELIVERED },
2433 { "QDELAYED", QDELAYED },
2434 { "QTHISPASS", QTHISPASS },
2435 { "QALIAS", QALIAS },
2436 { "QBYTRACE", QBYTRACE },
2437 { "QBYNDELAY", QBYNDELAY },
2438 { "QBYNRELAY", QBYNRELAY },
2439 { "QINTBCC", QINTBCC },
2440 { "QDYNMAILER", QDYNMAILER },
2441 { "QRCPTOK", QRCPTOK },
2446 printaddr(fp, a, follow)
2448 register ADDRESS *a;
2452 MAILER pseudomailer;
2453 register struct qflags *qfp;
2458 (void) sm_io_fprintf(fp, SM_TIME_DEFAULT, "[NULL]\n");
2464 (void) sm_io_fprintf(fp, SM_TIME_DEFAULT, "%p=", a);
2465 (void) sm_io_flush(fp, SM_TIME_DEFAULT);
2467 /* find the mailer -- carefully */
2476 (void) sm_io_fprintf(fp, SM_TIME_DEFAULT,
2477 "%s:\n\tmailer %d (%s), host `%s'\n",
2478 a->q_paddr == NULL ? "<null>" : a->q_paddr,
2479 m->m_mno, m->m_name,
2480 a->q_host == NULL ? "<null>" : a->q_host);
2481 (void) sm_io_fprintf(fp, SM_TIME_DEFAULT,
2482 "\tuser `%s', ruser `%s'\n",
2484 a->q_ruser == NULL ? "<null>" : a->q_ruser);
2485 (void) sm_io_fprintf(fp, SM_TIME_DEFAULT, "\tstate=");
2489 (void) sm_io_fprintf(fp, SM_TIME_DEFAULT, "OK");
2493 (void) sm_io_fprintf(fp, SM_TIME_DEFAULT,
2498 (void) sm_io_fprintf(fp, SM_TIME_DEFAULT,
2503 (void) sm_io_fprintf(fp, SM_TIME_DEFAULT,
2508 (void) sm_io_fprintf(fp, SM_TIME_DEFAULT, "RETRY");
2512 (void) sm_io_fprintf(fp, SM_TIME_DEFAULT, "SENT");
2516 (void) sm_io_fprintf(fp, SM_TIME_DEFAULT,
2521 (void) sm_io_fprintf(fp, SM_TIME_DEFAULT,
2526 (void) sm_io_fprintf(fp, SM_TIME_DEFAULT,
2531 (void) sm_io_fprintf(fp, SM_TIME_DEFAULT,
2536 (void) sm_io_fprintf(fp, SM_TIME_DEFAULT,
2541 (void) sm_io_fprintf(fp, SM_TIME_DEFAULT,
2546 (void) sm_io_fprintf(fp, SM_TIME_DEFAULT,
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,
2565 (void) sm_io_fprintf(fp, SM_TIME_DEFAULT,
2566 ", next=%p, alias %p, uid %d, gid %d\n",
2567 a->q_next, a->q_alias,
2568 (int) a->q_uid, (int) a->q_gid);
2569 (void) sm_io_fprintf(fp, SM_TIME_DEFAULT, "\tflags=%lx<",
2572 for (qfp = AddressFlags; qfp->qf_name != NULL; qfp++)
2574 if (!bitset(qfp->qf_bit, a->q_flags))
2577 (void) sm_io_fprintf(fp, SM_TIME_DEFAULT,
2580 (void) sm_io_fprintf(fp, SM_TIME_DEFAULT, "%s",
2583 (void) sm_io_fprintf(fp, SM_TIME_DEFAULT, ">\n");
2584 (void) sm_io_fprintf(fp, SM_TIME_DEFAULT,
2585 "\towner=%s, home=\"%s\", fullname=\"%s\"\n",
2586 a->q_owner == NULL ? "(none)" : a->q_owner,
2587 a->q_home == NULL ? "(none)" : a->q_home,
2588 a->q_fullname == NULL ? "(none)" : a->q_fullname);
2589 (void) sm_io_fprintf(fp, SM_TIME_DEFAULT,
2590 "\torcpt=\"%s\", statmta=%s, status=%s\n",
2591 a->q_orcpt == NULL ? "(none)" : a->q_orcpt,
2592 a->q_statmta == NULL ? "(none)" : a->q_statmta,
2593 a->q_status == NULL ? "(none)" : a->q_status);
2594 (void) sm_io_fprintf(fp, SM_TIME_DEFAULT,
2595 "\tfinalrcpt=\"%s\"\n",
2596 a->q_finalrcpt == NULL ? "(none)" : a->q_finalrcpt);
2597 (void) sm_io_fprintf(fp, SM_TIME_DEFAULT,
2598 "\trstatus=\"%s\"\n",
2599 a->q_rstatus == NULL ? "(none)" : a->q_rstatus);
2600 (void) sm_io_fprintf(fp, SM_TIME_DEFAULT,
2602 a->q_statdate == 0 ? "(none)" : ctime(&a->q_statdate));
2610 ** EMPTYADDR -- return true if this address is empty (``<>'')
2613 ** a -- pointer to the address
2616 ** true -- if this address is "empty" (i.e., no one should
2617 ** ever generate replies to it.
2618 ** false -- if it is a "regular" (read: replyable) address.
2623 register ADDRESS *a;
2625 return a->q_paddr == NULL || strcmp(a->q_paddr, "<>") == 0 ||
2626 a->q_user == NULL || strcmp(a->q_user, "<>") == 0;
2629 ** REMOTENAME -- return the name relative to the current mailer
2632 ** name -- the name to translate.
2633 ** m -- the mailer that we want to do rewriting relative to.
2634 ** flags -- fine tune operations.
2635 ** pstat -- pointer to status word.
2636 ** e -- the current envelope.
2639 ** the text string representing this address relative to
2640 ** the receiving mailer.
2646 ** The text string returned is tucked away locally;
2647 ** copy it if you intend to save it.
2651 remotename(name, m, flags, pstat, e)
2656 register ENVELOPE *e;
2658 register char **pvp;
2659 char *SM_NONVOLATILE fancy;
2662 static char buf[MAXNAME + 1];
2663 char lbuf[MAXNAME + 1];
2664 char pvpbuf[PSBUFSIZE];
2669 sm_dprintf("remotename(");
2670 xputs(sm_debug_file(), name);
2674 /* don't do anything if we are tagging it as special */
2675 if (bitset(RF_SENDERADDR, flags))
2677 rwset = bitset(RF_HEADERADDR, flags) ? m->m_sh_rwset
2683 rwset = bitset(RF_HEADERADDR, flags) ? m->m_rh_rwset
2691 addrtype[0] = bitset(RF_HEADERADDR, flags) ? 'h' : 'e';
2692 macdefine(&e->e_macro, A_TEMP, macid("{addr_type}"), addrtype);
2695 ** Do a heuristic crack of this name to extract any comment info.
2696 ** This will leave the name as a comment and a $g macro.
2699 if (bitset(RF_CANONICAL, flags) || bitnset(M_NOCOMMENT, m->m_flags))
2702 fancy = crackaddr(name, e);
2705 ** Turn the name into canonical form.
2706 ** Normally this will be RFC 822 style, i.e., "user@domain".
2707 ** If this only resolves to "user", and the "C" flag is
2708 ** specified in the sending mailer, then the sender's
2709 ** domain will be appended.
2712 pvp = prescan(name, '\0', pvpbuf, sizeof(pvpbuf), NULL, NULL, false);
2715 if (REWRITE(pvp, 3, e) == EX_TEMPFAIL)
2716 *pstat = EX_TEMPFAIL;
2717 if (bitset(RF_ADDDOMAIN, flags) && e->e_fromdomain != NULL)
2719 /* append from domain to this address */
2720 register char **pxp = pvp;
2721 int l = MAXATOM; /* size of buffer for pvp */
2723 /* see if there is an "@domain" in the current name */
2724 while (*pxp != NULL && strcmp(*pxp, "@") != 0)
2731 /* no.... append the "@domain" from the sender */
2732 register char **qxq = e->e_fromdomain;
2734 while ((*pxp++ = *qxq++) != NULL)
2739 usrerr("553 5.1.0 remotename: too many tokens");
2740 *pstat = EX_UNAVAILABLE;
2744 if (REWRITE(pvp, 3, e) == EX_TEMPFAIL)
2745 *pstat = EX_TEMPFAIL;
2750 ** Do more specific rewriting.
2751 ** Rewrite using ruleset 1 or 2 depending on whether this is
2752 ** a sender address or not.
2753 ** Then run it through any receiving-mailer-specific rulesets.
2756 if (bitset(RF_SENDERADDR, flags))
2758 if (REWRITE(pvp, 1, e) == EX_TEMPFAIL)
2759 *pstat = EX_TEMPFAIL;
2763 if (REWRITE(pvp, 2, e) == EX_TEMPFAIL)
2764 *pstat = EX_TEMPFAIL;
2768 if (REWRITE(pvp, rwset, e) == EX_TEMPFAIL)
2769 *pstat = EX_TEMPFAIL;
2773 ** Do any final sanitation the address may require.
2774 ** This will normally be used to turn internal forms
2775 ** (e.g., user@host.LOCAL) into external form. This
2776 ** may be used as a default to the above rules.
2779 if (REWRITE(pvp, 4, e) == EX_TEMPFAIL)
2780 *pstat = EX_TEMPFAIL;
2783 ** Now restore the comment information we had at the beginning.
2786 cataddr(pvp, NULL, lbuf, sizeof(lbuf), '\0', false);
2787 oldg = macget(&e->e_macro, 'g');
2788 macset(&e->e_macro, 'g', lbuf);
2791 /* need to make sure route-addrs have <angle brackets> */
2792 if (bitset(RF_CANONICAL, flags) && lbuf[0] == '@')
2793 expand("<\201g>", buf, sizeof(buf), e);
2795 expand(fancy, buf, sizeof(buf), e);
2797 macset(&e->e_macro, 'g', oldg);
2802 sm_dprintf("remotename => `");
2803 xputs(sm_debug_file(), buf);
2804 sm_dprintf("', stat=%d\n", *pstat);
2809 ** MAPLOCALUSER -- run local username through ruleset 5 for final redirection
2812 ** a -- the address to map (but just the user name part).
2813 ** sendq -- the sendq in which to install any replacement
2815 ** aliaslevel -- the alias nesting depth.
2816 ** e -- the envelope.
2822 #define Q_COPYFLAGS (QPRIMARY|QBOGUSSHELL|QUNSAFEADDR|\
2823 Q_PINGFLAGS|QHASNOTIFY|\
2824 QRELAYED|QEXPANDED|QDELIVERED|QDELAYED|\
2825 QBYTRACE|QBYNDELAY|QBYNRELAY)
2828 maplocaluser(a, sendq, aliaslevel, e)
2829 register ADDRESS *a;
2834 register char **pvp;
2835 register ADDRESS *SM_NONVOLATILE a1 = NULL;
2836 char pvpbuf[PSBUFSIZE];
2840 sm_dprintf("maplocaluser: ");
2841 printaddr(sm_debug_file(), a, false);
2843 pvp = prescan(a->q_user, '\0', pvpbuf, sizeof(pvpbuf), NULL, NULL,
2848 sm_dprintf("maplocaluser: cannot prescan %s\n",
2853 macdefine(&e->e_macro, A_PERM, 'h', a->q_host);
2854 macdefine(&e->e_macro, A_PERM, 'u', a->q_user);
2855 macdefine(&e->e_macro, A_PERM, 'z', a->q_home);
2857 macdefine(&e->e_macro, A_PERM, macid("{addr_type}"), "e r");
2858 if (REWRITE(pvp, 5, e) == EX_TEMPFAIL)
2861 sm_dprintf("maplocaluser: rewrite tempfail\n");
2862 a->q_state = QS_QUEUEUP;
2863 a->q_status = "4.4.3";
2866 if (pvp[0] == NULL || (pvp[0][0] & 0377) != CANONNET)
2869 sm_dprintf("maplocaluser: doesn't resolve\n");
2874 a1 = buildaddr(pvp, NULL, 0, e);
2875 SM_EXCEPT(exc, "E:mta.quickabort")
2878 ** mark address as bad, S5 returned an error
2879 ** and we gave that back to the SMTP client.
2882 a->q_state = QS_DONTSEND;
2883 sm_exc_raisenew_x(&EtypeQuickAbort, 2);
2886 /* if non-null, mailer destination specified -- has it changed? */
2887 if (a1 == NULL || sameaddr(a, a1))
2890 sm_dprintf("maplocaluser: address unchanged\n");
2894 /* make new address take on flags and print attributes of old */
2895 a1->q_flags &= ~Q_COPYFLAGS;
2896 a1->q_flags |= a->q_flags & Q_COPYFLAGS;
2897 a1->q_paddr = sm_rpool_strdup_x(e->e_rpool, a->q_paddr);
2898 a1->q_finalrcpt = a->q_finalrcpt;
2899 a1->q_orcpt = a->q_orcpt;
2901 /* mark old address as dead; insert new address */
2902 a->q_state = QS_REPLACED;
2905 sm_dprintf("maplocaluser: QS_REPLACED ");
2906 printaddr(sm_debug_file(), a, false);
2909 allocaddr(a1, RF_COPYALL, sm_rpool_strdup_x(e->e_rpool, a->q_paddr), e);
2910 (void) recipient(a1, sendq, aliaslevel, e);
2913 ** DEQUOTE_INIT -- initialize dequote map
2916 ** map -- the internal map structure.
2917 ** args -- arguments.
2924 dequote_init(map, args)
2928 register char *p = args;
2930 /* there is no check whether there is really an argument */
2931 map->map_mflags |= MF_KEEPQUOTES;
2934 while (isascii(*p) && isspace(*p))
2945 map->map_mflags |= MF_DEFER;
2950 map->map_spacesub = *++p;
2953 while (*p != '\0' && !(isascii(*p) && isspace(*p)))
2958 if (map->map_app != NULL)
2959 map->map_app = newstr(map->map_app);
2964 ** DEQUOTE_MAP -- unquote an address
2967 ** map -- the internal map structure (ignored).
2968 ** name -- the name to dequote.
2969 ** av -- arguments (ignored).
2970 ** statp -- pointer to status out-parameter.
2973 ** NULL -- if there were no quotes, or if the resulting
2974 ** unquoted buffer would not be acceptable to prescan.
2975 ** else -- The dequoted buffer.
2980 dequote_map(map, name, av, statp)
2993 bool quotemode = false;
2994 bool bslashmode = false;
2995 char spacesub = map->map_spacesub;
2997 for (p = q = name; (c = *p++) != '\0'; )
3006 if (c == ' ' && spacesub != '\0')
3039 quotemode = !quotemode;
3048 if (anglecnt-- <= 0)
3055 if (anglecnt != 0 || cmntcnt != 0 || bslashmode ||
3056 quotemode || quotecnt <= 0 || spacecnt != 0)
3059 return map_rewrite(map, name, strlen(name), NULL);
3062 ** RSCHECK -- check string(s) for validity using rewriting sets
3065 ** rwset -- the rewriting set to use.
3066 ** p1 -- the first string to check.
3067 ** p2 -- the second string to check -- may be null.
3068 ** e -- the current envelope.
3069 ** flags -- control some behavior, see RSF_ in sendmail.h
3070 ** logl -- logging level.
3071 ** host -- NULL or relay host.
3072 ** logid -- id for sm_syslog.
3073 ** addr -- if not NULL and ruleset returns $#error:
3074 ** store mailer triple here.
3075 ** addrstr -- if not NULL and ruleset does not return $#:
3079 ** EX_OK -- if the rwset doesn't resolve to $#error
3080 ** else -- the failure status (message printed)
3084 rscheck(rwset, p1, p2, e, flags, logl, host, logid, addr, addrstr)
3099 int volatile rstat = EX_OK;
3102 bool volatile discard = false;
3103 bool saveQuickAbort = QuickAbort;
3104 bool saveSuprErrs = SuprErrs;
3105 bool quarantine = false;
3106 char ubuf[BUFSIZ * 2];
3108 char pvpbuf[PSBUFSIZE];
3109 extern char MsgBuf[];
3112 sm_dprintf("rscheck(%s, %s, %s)\n", rwset, p1,
3113 p2 == NULL ? "(NULL)" : p2);
3115 rsno = strtorwset(rwset, NULL, ST_FIND);
3121 bufsize = strlen(p1) + strlen(p2) + 2;
3122 if (bufsize > sizeof(buf0))
3123 buf = sm_malloc_x(bufsize);
3127 bufsize = sizeof(buf0);
3129 (void) sm_snprintf(buf, bufsize, "%s%c%s", p1, CONDELSE, p2);
3133 bufsize = strlen(p1) + 1;
3134 if (bufsize > sizeof(buf0))
3135 buf = sm_malloc_x(bufsize);
3139 bufsize = sizeof(buf0);
3141 (void) sm_strlcpy(buf, p1, bufsize);
3147 pvp = prescan(buf, '\0', pvpbuf, sizeof(pvpbuf), NULL,
3148 bitset(RSF_RMCOMM, flags) ?
3149 IntTokenTab : TokTypeNoC,
3150 bitset(RSF_RMCOMM, flags) ? false : true);
3151 SuprErrs = saveSuprErrs;
3155 sm_dprintf("rscheck: cannot prescan input\n");
3157 syserr("rscheck: cannot prescan input: \"%s\"",
3158 shortenstring(buf, MAXSHORTSTR));
3163 if (bitset(RSF_UNSTRUCTURED, flags))
3165 (void) REWRITE(pvp, rsno, e);
3166 if (bitset(RSF_UNSTRUCTURED, flags))
3167 SuprErrs = saveSuprErrs;
3169 if (pvp[0] != NULL && (pvp[0][0] & 0377) != CANONNET &&
3170 bitset(RSF_ADDR, flags) && addrstr != NULL)
3172 cataddr(&(pvp[0]), NULL, ubuf, sizeof(ubuf),
3173 bitset(RSF_STRING, flags) ? NOSPACESEP : ' ',
3175 *addrstr = sm_rpool_strdup_x(e->e_rpool, ubuf);
3179 if (pvp[0] == NULL || (pvp[0][0] & 0377) != CANONNET ||
3180 pvp[1] == NULL || (strcmp(pvp[1], "error") != 0 &&
3181 strcmp(pvp[1], "discard") != 0))
3186 if (strcmp(pvp[1], "discard") == 0)
3189 sm_dprintf("rscheck: discard mailer selected\n");
3190 e->e_flags |= EF_DISCARD;
3193 else if (strcmp(pvp[1], "error") == 0 &&
3194 pvp[2] != NULL && (pvp[2][0] & 0377) == CANONHOST &&
3195 pvp[3] != NULL && strcmp(pvp[3], "quarantine") == 0)
3197 if (pvp[4] == NULL ||
3198 (pvp[4][0] & 0377) != CANONUSER ||
3200 e->e_quarmsg = sm_rpool_strdup_x(e->e_rpool,
3204 cataddr(&(pvp[5]), NULL, ubuf,
3205 sizeof(ubuf), ' ', true);
3206 e->e_quarmsg = sm_rpool_strdup_x(e->e_rpool,
3209 macdefine(&e->e_macro, A_PERM,
3210 macid("{quarantine}"), e->e_quarmsg);
3216 int savelogusrerrs = LogUsrErrs;
3217 static bool logged = false;
3219 /* got an error -- process it */
3220 saveexitstat = ExitStat;
3222 (void) buildaddr(pvp, &a1, 0, e);
3225 addr->q_mailer = a1.q_mailer;
3226 addr->q_user = a1.q_user;
3227 addr->q_host = a1.q_host;
3229 LogUsrErrs = savelogusrerrs;
3231 ExitStat = saveexitstat;
3234 if (bitset(RSF_COUNT, flags))
3235 markstats(e, &a1, STATS_REJECT);
3240 if (LogLevel > logl)
3249 (void) sm_snprintf(p, SPACELEFT(lbuf, p),
3258 relay = macvalue('_', e);
3261 (void) sm_snprintf(p, SPACELEFT(lbuf, p),
3262 ", relay=%s", relay);
3267 sm_syslog(LOG_NOTICE, logid,
3268 "ruleset=%s, arg1=%s%s, discard",
3270 else if (quarantine)
3271 sm_syslog(LOG_NOTICE, logid,
3272 "ruleset=%s, arg1=%s%s, quarantine=%s",
3273 rwset, p1, lbuf, ubuf);
3275 sm_syslog(LOG_NOTICE, logid,
3276 "ruleset=%s, arg1=%s%s, reject=%s",
3277 rwset, p1, lbuf, MsgBuf);
3287 QuickAbort = saveQuickAbort;
3293 /* rulesets don't set errno */
3295 if (rstat != EX_OK && QuickAbort)
3296 sm_exc_raisenew_x(&EtypeQuickAbort, 2);
3300 ** RSCAP -- call rewriting set to return capabilities
3303 ** rwset -- the rewriting set to use.
3304 ** p1 -- the first string to check.
3305 ** p2 -- the second string to check -- may be null.
3306 ** e -- the current envelope.
3307 ** pvp -- pointer to token vector.
3308 ** pvpbuf -- buffer space.
3309 ** size -- size of buffer space.
3312 ** EX_UNAVAILABLE -- ruleset doesn't exist.
3313 ** EX_DATAERR -- prescan() failed.
3314 ** EX_OK -- rewrite() was successful.
3315 ** else -- return status from rewrite().
3319 rscap(rwset, p1, p2, e, pvp, pvpbuf, size)
3330 int volatile rstat = EX_OK;
3332 bool saveQuickAbort = QuickAbort;
3333 bool saveSuprErrs = SuprErrs;
3335 extern char MsgBuf[];
3338 sm_dprintf("rscap(%s, %s, %s)\n", rwset, p1,
3339 p2 == NULL ? "(NULL)" : p2);
3341 SM_REQUIRE(pvp != NULL);
3342 rsno = strtorwset(rwset, NULL, ST_FIND);
3344 return EX_UNAVAILABLE;
3348 bufsize = strlen(p1) + strlen(p2) + 2;
3349 if (bufsize > sizeof(buf0))
3350 buf = sm_malloc_x(bufsize);
3354 bufsize = sizeof(buf0);
3356 (void) sm_snprintf(buf, bufsize, "%s%c%s", p1, CONDELSE, p2);
3360 bufsize = strlen(p1) + 1;
3361 if (bufsize > sizeof(buf0))
3362 buf = sm_malloc_x(bufsize);
3366 bufsize = sizeof(buf0);
3368 (void) sm_strlcpy(buf, p1, bufsize);
3374 *pvp = prescan(buf, '\0', pvpbuf, size, NULL, IntTokenTab,
3377 rstat = rewrite(*pvp, rsno, 0, e, size);
3381 sm_dprintf("rscap: cannot prescan input\n");
3390 SuprErrs = saveSuprErrs;
3391 QuickAbort = saveQuickAbort;
3393 /* prevent information leak, this may contain rewrite error */