1N/A/*
1N/A * Copyright (c) 1998-2006 Sendmail, Inc. and its suppliers.
1N/A * All rights reserved.
1N/A * Copyright (c) 1983, 1995-1997 Eric P. Allman. All rights reserved.
1N/A * Copyright (c) 1988, 1993
1N/A * The Regents of the University of California. All rights reserved.
1N/A *
1N/A * By using this file, you agree to the terms and conditions set
1N/A * forth in the LICENSE file which can be found at the top level of
1N/A * the sendmail distribution.
1N/A *
1N/A */
1N/A
1N/A#include <sendmail.h>
1N/A
1N/ASM_RCSID("@(#)$Id: parseaddr.c,v 8.404 2010/07/27 03:35:42 ca Exp $")
1N/A
1N/A#include <sm/sendmail.h>
1N/A#include "map.h"
1N/A
1N/Astatic void allocaddr __P((ADDRESS *, int, char *, ENVELOPE *));
1N/Astatic int callsubr __P((char**, int, ENVELOPE *));
1N/Astatic char *map_lookup __P((STAB *, char *, char **, int *, ENVELOPE *));
1N/Astatic ADDRESS *buildaddr __P((char **, ADDRESS *, int, ENVELOPE *));
1N/Astatic bool hasctrlchar __P((register char *, bool, bool));
1N/A
1N/A/* replacement for illegal characters in addresses */
1N/A#define BAD_CHAR_REPLACEMENT '?'
1N/A
1N/A/*
1N/A** PARSEADDR -- Parse an address
1N/A**
1N/A** Parses an address and breaks it up into three parts: a
1N/A** net to transmit the message on, the host to transmit it
1N/A** to, and a user on that host. These are loaded into an
1N/A** ADDRESS header with the values squirreled away if necessary.
1N/A** The "user" part may not be a real user; the process may
1N/A** just reoccur on that machine. For example, on a machine
1N/A** with an arpanet connection, the address
1N/A** csvax.bill@berkeley
1N/A** will break up to a "user" of 'csvax.bill' and a host
1N/A** of 'berkeley' -- to be transmitted over the arpanet.
1N/A**
1N/A** Parameters:
1N/A** addr -- the address to parse.
1N/A** a -- a pointer to the address descriptor buffer.
1N/A** If NULL, an address will be created.
1N/A** flags -- describe detail for parsing. See RF_ definitions
1N/A** in sendmail.h.
1N/A** delim -- the character to terminate the address, passed
1N/A** to prescan.
1N/A** delimptr -- if non-NULL, set to the location of the
1N/A** delim character that was found.
1N/A** e -- the envelope that will contain this address.
1N/A** isrcpt -- true if the address denotes a recipient; false
1N/A** indicates a sender.
1N/A**
1N/A** Returns:
1N/A** A pointer to the address descriptor header (`a' if
1N/A** `a' is non-NULL).
1N/A** NULL on error.
1N/A**
1N/A** Side Effects:
1N/A** e->e_to = addr
1N/A*/
1N/A
1N/A/* following delimiters are inherent to the internal algorithms */
1N/A#define DELIMCHARS "()<>,;\r\n" /* default word delimiters */
1N/A
1N/AADDRESS *
1N/Aparseaddr(addr, a, flags, delim, delimptr, e, isrcpt)
1N/A char *addr;
1N/A register ADDRESS *a;
1N/A int flags;
1N/A int delim;
1N/A char **delimptr;
1N/A register ENVELOPE *e;
1N/A bool isrcpt;
1N/A{
1N/A char **pvp;
1N/A auto char *delimptrbuf;
1N/A bool qup;
1N/A char pvpbuf[PSBUFSIZE];
1N/A
1N/A /*
1N/A ** Initialize and prescan address.
1N/A */
1N/A
1N/A e->e_to = addr;
1N/A if (tTd(20, 1))
1N/A sm_dprintf("\n--parseaddr(%s)\n", addr);
1N/A
1N/A if (delimptr == NULL)
1N/A delimptr = &delimptrbuf;
1N/A
1N/A pvp = prescan(addr, delim, pvpbuf, sizeof(pvpbuf), delimptr,
1N/A ExtTokenTab, false);
1N/A if (pvp == NULL)
1N/A {
1N/A if (tTd(20, 1))
1N/A sm_dprintf("parseaddr-->NULL\n");
1N/A return NULL;
1N/A }
1N/A
1N/A if (invalidaddr(addr, delim == '\0' ? NULL : *delimptr, isrcpt))
1N/A {
1N/A if (tTd(20, 1))
1N/A sm_dprintf("parseaddr-->bad address\n");
1N/A return NULL;
1N/A }
1N/A
1N/A /*
1N/A ** Save addr if we are going to have to.
1N/A **
1N/A ** We have to do this early because there is a chance that
1N/A ** the map lookups in the rewriting rules could clobber
1N/A ** static memory somewhere.
1N/A */
1N/A
1N/A if (bitset(RF_COPYPADDR, flags) && addr != NULL)
1N/A {
1N/A char savec = **delimptr;
1N/A
1N/A if (savec != '\0')
1N/A **delimptr = '\0';
1N/A e->e_to = addr = sm_rpool_strdup_x(e->e_rpool, addr);
1N/A if (savec != '\0')
1N/A **delimptr = savec;
1N/A }
1N/A
1N/A /*
1N/A ** Apply rewriting rules.
1N/A ** Ruleset 0 does basic parsing. It must resolve.
1N/A */
1N/A
1N/A qup = false;
1N/A if (REWRITE(pvp, 3, e) == EX_TEMPFAIL)
1N/A qup = true;
1N/A if (REWRITE(pvp, 0, e) == EX_TEMPFAIL)
1N/A qup = true;
1N/A
1N/A /*
1N/A ** Build canonical address from pvp.
1N/A */
1N/A
1N/A a = buildaddr(pvp, a, flags, e);
1N/A
1N/A if (hasctrlchar(a->q_user, isrcpt, true))
1N/A {
1N/A if (tTd(20, 1))
1N/A sm_dprintf("parseaddr-->bad q_user\n");
1N/A
1N/A /*
1N/A ** Just mark the address as bad so DSNs work.
1N/A ** hasctrlchar() has to make sure that the address
1N/A ** has been sanitized, e.g., shortened.
1N/A */
1N/A
1N/A a->q_state = QS_BADADDR;
1N/A }
1N/A
1N/A /*
1N/A ** Make local copies of the host & user and then
1N/A ** transport them out.
1N/A */
1N/A
1N/A allocaddr(a, flags, addr, e);
1N/A if (QS_IS_BADADDR(a->q_state))
1N/A {
1N/A /* weed out bad characters in the printable address too */
1N/A (void) hasctrlchar(a->q_paddr, isrcpt, false);
1N/A return a;
1N/A }
1N/A
1N/A /*
1N/A ** Select a queue directory for recipient addresses.
1N/A ** This is done here and in split_across_queue_groups(),
1N/A ** but the latter applies to addresses after aliasing,
1N/A ** and only if splitting is done.
1N/A */
1N/A
1N/A if ((a->q_qgrp == NOAQGRP || a->q_qgrp == ENVQGRP) &&
1N/A !bitset(RF_SENDERADDR|RF_HEADERADDR|RF_RM_ADDR, flags) &&
1N/A OpMode != MD_INITALIAS)
1N/A {
1N/A int r;
1N/A
1N/A /* call ruleset which should return a queue group name */
1N/A r = rscap(RS_QUEUEGROUP, a->q_user, NULL, e, &pvp, pvpbuf,
1N/A sizeof(pvpbuf));
1N/A if (r == EX_OK &&
1N/A pvp != NULL && pvp[0] != NULL &&
1N/A (pvp[0][0] & 0377) == CANONNET &&
1N/A pvp[1] != NULL && pvp[1][0] != '\0')
1N/A {
1N/A r = name2qid(pvp[1]);
1N/A if (r == NOQGRP && LogLevel > 10)
1N/A sm_syslog(LOG_INFO, NOQID,
1N/A "can't find queue group name %s, selection ignored",
1N/A pvp[1]);
1N/A if (tTd(20, 4) && r != NOQGRP)
1N/A sm_syslog(LOG_INFO, NOQID,
1N/A "queue group name %s -> %d",
1N/A pvp[1], r);
1N/A a->q_qgrp = r == NOQGRP ? ENVQGRP : r;
1N/A }
1N/A }
1N/A
1N/A /*
1N/A ** If there was a parsing failure, mark it for queueing.
1N/A */
1N/A
1N/A if (qup && OpMode != MD_INITALIAS)
1N/A {
1N/A char *msg = "Transient parse error -- message queued for future delivery";
1N/A
1N/A if (e->e_sendmode == SM_DEFER)
1N/A msg = "Deferring message until queue run";
1N/A if (tTd(20, 1))
1N/A sm_dprintf("parseaddr: queueing message\n");
1N/A message(msg);
1N/A if (e->e_message == NULL && e->e_sendmode != SM_DEFER)
1N/A e->e_message = sm_rpool_strdup_x(e->e_rpool, msg);
1N/A a->q_state = QS_QUEUEUP;
1N/A a->q_status = "4.4.3";
1N/A }
1N/A
1N/A /*
1N/A ** Compute return value.
1N/A */
1N/A
1N/A if (tTd(20, 1))
1N/A {
1N/A sm_dprintf("parseaddr-->");
1N/A printaddr(sm_debug_file(), a, false);
1N/A }
1N/A
1N/A return a;
1N/A}
1N/A/*
1N/A** INVALIDADDR -- check for address containing characters used for macros
1N/A**
1N/A** Parameters:
1N/A** addr -- the address to check.
1N/A** delimptr -- if non-NULL: end of address to check, i.e.,
1N/A** a pointer in the address string.
1N/A** isrcpt -- true iff the address is for a recipient.
1N/A**
1N/A** Returns:
1N/A** true -- if the address has characters that are reservered
1N/A** for macros or is too long.
1N/A** false -- otherwise.
1N/A*/
1N/A
1N/Abool
1N/Ainvalidaddr(addr, delimptr, isrcpt)
1N/A register char *addr;
1N/A char *delimptr;
1N/A bool isrcpt;
1N/A{
1N/A bool result = false;
1N/A char savedelim = '\0';
1N/A char *b = addr;
1N/A int len = 0;
1N/A
1N/A if (delimptr != NULL)
1N/A {
1N/A /* delimptr points to the end of the address to test */
1N/A savedelim = *delimptr;
1N/A if (savedelim != '\0') /* if that isn't '\0' already: */
1N/A *delimptr = '\0'; /* set it */
1N/A }
1N/A for (; *addr != '\0'; addr++)
1N/A {
1N/A if (!EightBitAddrOK && (*addr & 0340) == 0200)
1N/A {
1N/A setstat(EX_USAGE);
1N/A result = true;
1N/A *addr = BAD_CHAR_REPLACEMENT;
1N/A }
1N/A if (++len > MAXNAME - 1)
1N/A {
1N/A char saved = *addr;
1N/A
1N/A *addr = '\0';
1N/A usrerr("553 5.1.0 Address \"%s\" too long (%d bytes max)",
1N/A b, MAXNAME - 1);
1N/A *addr = saved;
1N/A result = true;
1N/A goto delim;
1N/A }
1N/A }
1N/A if (result)
1N/A {
1N/A if (isrcpt)
1N/A usrerr("501 5.1.3 8-bit character in mailbox address \"%s\"",
1N/A b);
1N/A else
1N/A usrerr("501 5.1.7 8-bit character in mailbox address \"%s\"",
1N/A b);
1N/A }
1N/Adelim:
1N/A if (delimptr != NULL && savedelim != '\0')
1N/A *delimptr = savedelim; /* restore old character at delimptr */
1N/A return result;
1N/A}
1N/A/*
1N/A** HASCTRLCHAR -- check for address containing meta-characters
1N/A**
1N/A** Checks that the address contains no meta-characters, and contains
1N/A** no "non-printable" characters unless they are quoted or escaped.
1N/A** Quoted or escaped characters are literals.
1N/A**
1N/A** Parameters:
1N/A** addr -- the address to check.
1N/A** isrcpt -- true if the address is for a recipient; false
1N/A** indicates a from.
1N/A** complain -- true if an error should issued if the address
1N/A** is invalid and should be "repaired".
1N/A**
1N/A** Returns:
1N/A** true -- if the address has any "weird" characters or
1N/A** non-printable characters or if a quote is unbalanced.
1N/A** false -- otherwise.
1N/A*/
1N/A
1N/Astatic bool
1N/Ahasctrlchar(addr, isrcpt, complain)
1N/A register char *addr;
1N/A bool isrcpt, complain;
1N/A{
1N/A bool quoted = false;
1N/A int len = 0;
1N/A char *result = NULL;
1N/A char *b = addr;
1N/A
1N/A if (addr == NULL)
1N/A return false;
1N/A for (; *addr != '\0'; addr++)
1N/A {
1N/A if (++len > MAXNAME - 1)
1N/A {
1N/A if (complain)
1N/A {
1N/A (void) shorten_rfc822_string(b, MAXNAME - 1);
1N/A usrerr("553 5.1.0 Address \"%s\" too long (%d bytes max)",
1N/A b, MAXNAME - 1);
1N/A return true;
1N/A }
1N/A result = "too long";
1N/A }
1N/A if (!EightBitAddrOK && !quoted && (*addr < 32 || *addr == 127))
1N/A {
1N/A result = "non-printable character";
1N/A *addr = BAD_CHAR_REPLACEMENT;
1N/A continue;
1N/A }
1N/A if (*addr == '"')
1N/A quoted = !quoted;
1N/A else if (*addr == '\\')
1N/A {
1N/A /* XXX Generic problem: no '\0' in strings. */
1N/A if (*++addr == '\0')
1N/A {
1N/A result = "trailing \\ character";
1N/A *--addr = BAD_CHAR_REPLACEMENT;
1N/A break;
1N/A }
1N/A }
1N/A if (!EightBitAddrOK && (*addr & 0340) == 0200)
1N/A {
1N/A setstat(EX_USAGE);
1N/A result = "8-bit character";
1N/A *addr = BAD_CHAR_REPLACEMENT;
1N/A continue;
1N/A }
1N/A }
1N/A if (quoted)
1N/A result = "unbalanced quote"; /* unbalanced quote */
1N/A if (result != NULL && complain)
1N/A {
1N/A if (isrcpt)
1N/A usrerr("501 5.1.3 Syntax error in mailbox address \"%s\" (%s)",
1N/A b, result);
1N/A else
1N/A usrerr("501 5.1.7 Syntax error in mailbox address \"%s\" (%s)",
1N/A b, result);
1N/A }
1N/A return result != NULL;
1N/A}
1N/A/*
1N/A** ALLOCADDR -- do local allocations of address on demand.
1N/A**
1N/A** Also lowercases the host name if requested.
1N/A**
1N/A** Parameters:
1N/A** a -- the address to reallocate.
1N/A** flags -- the copy flag (see RF_ definitions in sendmail.h
1N/A** for a description).
1N/A** paddr -- the printname of the address.
1N/A** e -- envelope
1N/A**
1N/A** Returns:
1N/A** none.
1N/A**
1N/A** Side Effects:
1N/A** Copies portions of a into local buffers as requested.
1N/A*/
1N/A
1N/Astatic void
1N/Aallocaddr(a, flags, paddr, e)
1N/A register ADDRESS *a;
1N/A int flags;
1N/A char *paddr;
1N/A ENVELOPE *e;
1N/A{
1N/A if (tTd(24, 4))
1N/A sm_dprintf("allocaddr(flags=%x, paddr=%s)\n", flags, paddr);
1N/A
1N/A a->q_paddr = paddr;
1N/A
1N/A if (a->q_user == NULL)
1N/A a->q_user = "";
1N/A if (a->q_host == NULL)
1N/A a->q_host = "";
1N/A
1N/A if (bitset(RF_COPYPARSE, flags))
1N/A {
1N/A a->q_host = sm_rpool_strdup_x(e->e_rpool, a->q_host);
1N/A if (a->q_user != a->q_paddr)
1N/A a->q_user = sm_rpool_strdup_x(e->e_rpool, a->q_user);
1N/A }
1N/A
1N/A if (a->q_paddr == NULL)
1N/A a->q_paddr = sm_rpool_strdup_x(e->e_rpool, a->q_user);
1N/A a->q_qgrp = NOAQGRP;
1N/A}
1N/A
1N/A/*
1N/A** PRESCAN -- Prescan name and make it canonical
1N/A**
1N/A** Scans a name and turns it into a set of tokens. This process
1N/A** deletes blanks and comments (in parentheses) (if the token type
1N/A** for left paren is SPC).
1N/A**
1N/A** This routine knows about quoted strings and angle brackets.
1N/A**
1N/A** There are certain subtleties to this routine. The one that
1N/A** comes to mind now is that backslashes on the ends of names
1N/A** are silently stripped off; this is intentional. The problem
1N/A** is that some versions of sndmsg (like at LBL) set the kill
1N/A** character to something other than @ when reading addresses;
1N/A** so people type "csvax.eric\@berkeley" -- which screws up the
1N/A** berknet mailer.
1N/A**
1N/A** Parameters:
1N/A** addr -- the name to chomp.
1N/A** delim -- the delimiter for the address, normally
1N/A** '\0' or ','; \0 is accepted in any case.
1N/A** If '\t' then we are reading the .cf file.
1N/A** pvpbuf -- place to put the saved text -- note that
1N/A** the pointers are static.
1N/A** pvpbsize -- size of pvpbuf.
1N/A** delimptr -- if non-NULL, set to the location of the
1N/A** terminating delimiter.
1N/A** toktab -- if set, a token table to use for parsing.
1N/A** If NULL, use the default table.
1N/A** ignore -- if true, ignore unbalanced addresses
1N/A**
1N/A** Returns:
1N/A** A pointer to a vector of tokens.
1N/A** NULL on error.
1N/A*/
1N/A
1N/A/* states and character types */
1N/A#define OPR 0 /* operator */
1N/A#define ATM 1 /* atom */
1N/A#define QST 2 /* in quoted string */
1N/A#define SPC 3 /* chewing up spaces */
1N/A#define ONE 4 /* pick up one character */
1N/A#define ILL 5 /* illegal character */
1N/A
1N/A#define NSTATES 6 /* number of states */
1N/A#define TYPE 017 /* mask to select state type */
1N/A
1N/A/* meta bits for table */
1N/A#define M 020 /* meta character; don't pass through */
1N/A#define B 040 /* cause a break */
1N/A#define MB M|B /* meta-break */
1N/A
1N/Astatic short StateTab[NSTATES][NSTATES] =
1N/A{
1N/A /* oldst chtype> OPR ATM QST SPC ONE ILL */
1N/A /*OPR*/ { OPR|B, ATM|B, QST|B, SPC|MB, ONE|B, ILL|MB },
1N/A /*ATM*/ { OPR|B, ATM, QST|B, SPC|MB, ONE|B, ILL|MB },
1N/A /*QST*/ { QST, QST, OPR, QST, QST, QST },
1N/A /*SPC*/ { OPR, ATM, QST, SPC|M, ONE, ILL|MB },
1N/A /*ONE*/ { OPR, OPR, OPR, OPR, OPR, ILL|MB },
1N/A /*ILL*/ { OPR|B, ATM|B, QST|B, SPC|MB, ONE|B, ILL|M }
1N/A};
1N/A
1N/A/* these all get modified with the OperatorChars */
1N/A
1N/A/* token type table for external strings */
1N/Aunsigned char ExtTokenTab[256] =
1N/A{
1N/A /* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */
1N/A ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,SPC,SPC,SPC,SPC,SPC,ATM,ATM,
1N/A /* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */
1N/A ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
1N/A /* sp ! " # $ % & ' ( ) * + , - . / */
1N/A SPC,ATM,QST,ATM,ATM,ATM,ATM,ATM, SPC,SPC,ATM,ATM,ATM,ATM,ATM,ATM,
1N/A /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */
1N/A ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
1N/A /* @ A B C D E F G H I J K L M N O */
1N/A ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
1N/A /* P Q R S T U V W X Y Z [ \ ] ^ _ */
1N/A ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
1N/A /* ` a b c d e f g h i j k l m n o */
1N/A ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
1N/A /* p q r s t u v w x y z { | } ~ del */
1N/A ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
1N/A
1N/A /* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */
1N/A ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
1N/A /* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */
1N/A ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
1N/A /* sp ! " # $ % & ' ( ) * + , - . / */
1N/A ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
1N/A /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */
1N/A ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
1N/A /* @ A B C D E F G H I J K L M N O */
1N/A ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
1N/A /* P Q R S T U V W X Y Z [ \ ] ^ _ */
1N/A ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
1N/A /* ` a b c d e f g h i j k l m n o */
1N/A ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
1N/A /* p q r s t u v w x y z { | } ~ del */
1N/A ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM
1N/A};
1N/A
1N/A/* token type table for internal strings */
1N/Aunsigned char IntTokenTab[256] =
1N/A{
1N/A /* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */
1N/A ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,SPC,SPC,SPC,SPC,SPC,ATM,ATM,
1N/A /* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */
1N/A ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
1N/A /* sp ! " # $ % & ' ( ) * + , - . / */
1N/A SPC,ATM,QST,ATM,ATM,ATM,ATM,ATM, SPC,SPC,ATM,ATM,ATM,ATM,ATM,ATM,
1N/A /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */
1N/A ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
1N/A /* @ A B C D E F G H I J K L M N O */
1N/A ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
1N/A /* P Q R S T U V W X Y Z [ \ ] ^ _ */
1N/A ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
1N/A /* ` a b c d e f g h i j k l m n o */
1N/A ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
1N/A /* p q r s t u v w x y z { | } ~ del */
1N/A ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
1N/A
1N/A /* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */
1N/A OPR,OPR,ONE,OPR,OPR,OPR,OPR,OPR, OPR,OPR,OPR,OPR,OPR,OPR,OPR,OPR,
1N/A /* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */
1N/A OPR,OPR,OPR,ONE,ONE,ONE,OPR,OPR, OPR,OPR,OPR,OPR,OPR,OPR,OPR,OPR,
1N/A /* sp ! " # $ % & ' ( ) * + , - . / */
1N/A ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
1N/A /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */
1N/A ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
1N/A /* @ A B C D E F G H I J K L M N O */
1N/A ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
1N/A /* P Q R S T U V W X Y Z [ \ ] ^ _ */
1N/A ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
1N/A /* ` a b c d e f g h i j k l m n o */
1N/A ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
1N/A /* p q r s t u v w x y z { | } ~ del */
1N/A ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ONE
1N/A};
1N/A
1N/A/* token type table for MIME parsing */
1N/Aunsigned char MimeTokenTab[256] =
1N/A{
1N/A /* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */
1N/A ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL, ILL,SPC,SPC,SPC,SPC,SPC,ILL,ILL,
1N/A /* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */
1N/A ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL, ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL,
1N/A /* sp ! " # $ % & ' ( ) * + , - . / */
1N/A SPC,ATM,QST,ATM,ATM,ATM,ATM,ATM, SPC,SPC,ATM,ATM,OPR,ATM,ATM,OPR,
1N/A /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */
1N/A ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,OPR,OPR,OPR,OPR,OPR,OPR,
1N/A /* @ A B C D E F G H I J K L M N O */
1N/A OPR,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
1N/A /* P Q R S T U V W X Y Z [ \ ] ^ _ */
1N/A ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,OPR,OPR,OPR,ATM,ATM,
1N/A /* ` a b c d e f g h i j k l m n o */
1N/A ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
1N/A /* p q r s t u v w x y z { | } ~ del */
1N/A ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
1N/A
1N/A /* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */
1N/A ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL, ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL,
1N/A /* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */
1N/A ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL, ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL,
1N/A /* sp ! " # $ % & ' ( ) * + , - . / */
1N/A ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL, ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL,
1N/A /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */
1N/A ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL, ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL,
1N/A /* @ A B C D E F G H I J K L M N O */
1N/A ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL, ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL,
1N/A /* P Q R S T U V W X Y Z [ \ ] ^ _ */
1N/A ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL, ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL,
1N/A /* ` a b c d e f g h i j k l m n o */
1N/A ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL, ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL,
1N/A /* p q r s t u v w x y z { | } ~ del */
1N/A ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL, ILL,ILL,ILL,ILL,ILL,ILL,ILL,ONE
1N/A};
1N/A
1N/A/* token type table: don't strip comments */
1N/Aunsigned char TokTypeNoC[256] =
1N/A{
1N/A /* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */
1N/A ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,SPC,SPC,SPC,SPC,SPC,ATM,ATM,
1N/A /* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */
1N/A ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
1N/A /* sp ! " # $ % & ' ( ) * + , - . / */
1N/A SPC,ATM,QST,ATM,ATM,ATM,ATM,ATM, OPR,OPR,ATM,ATM,ATM,ATM,ATM,ATM,
1N/A /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */
1N/A ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
1N/A /* @ A B C D E F G H I J K L M N O */
1N/A ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
1N/A /* P Q R S T U V W X Y Z [ \ ] ^ _ */
1N/A ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
1N/A /* ` a b c d e f g h i j k l m n o */
1N/A ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
1N/A /* p q r s t u v w x y z { | } ~ del */
1N/A ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
1N/A
1N/A /* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */
1N/A OPR,OPR,ONE,OPR,OPR,OPR,OPR,OPR, OPR,OPR,OPR,OPR,OPR,OPR,OPR,OPR,
1N/A /* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */
1N/A OPR,OPR,OPR,ONE,ONE,ONE,OPR,OPR, OPR,OPR,OPR,OPR,OPR,OPR,OPR,OPR,
1N/A /* sp ! " # $ % & ' ( ) * + , - . / */
1N/A ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
1N/A /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */
1N/A ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
1N/A /* @ A B C D E F G H I J K L M N O */
1N/A ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
1N/A /* P Q R S T U V W X Y Z [ \ ] ^ _ */
1N/A ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
1N/A /* ` a b c d e f g h i j k l m n o */
1N/A ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
1N/A /* p q r s t u v w x y z { | } ~ del */
1N/A ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ONE
1N/A};
1N/A
1N/A
1N/A#define NOCHAR (-1) /* signal nothing in lookahead token */
1N/A
1N/Achar **
1N/Aprescan(addr, delim, pvpbuf, pvpbsize, delimptr, toktab, ignore)
1N/A char *addr;
1N/A int delim;
1N/A char pvpbuf[];
1N/A int pvpbsize;
1N/A char **delimptr;
1N/A unsigned char *toktab;
1N/A bool ignore;
1N/A{
1N/A register char *p;
1N/A register char *q;
1N/A register int c;
1N/A char **avp;
1N/A bool bslashmode;
1N/A bool route_syntax;
1N/A int cmntcnt;
1N/A int anglecnt;
1N/A char *tok;
1N/A int state;
1N/A int newstate;
1N/A char *saveto = CurEnv->e_to;
1N/A static char *av[MAXATOM + 1];
1N/A static bool firsttime = true;
1N/A
1N/A if (firsttime)
1N/A {
1N/A /* initialize the token type table */
1N/A char obuf[50];
1N/A
1N/A firsttime = false;
1N/A if (OperatorChars == NULL)
1N/A {
1N/A if (ConfigLevel < 7)
1N/A OperatorChars = macvalue('o', CurEnv);
1N/A if (OperatorChars == NULL)
1N/A OperatorChars = ".:@[]";
1N/A }
1N/A expand(OperatorChars, obuf, sizeof(obuf) - sizeof(DELIMCHARS),
1N/A CurEnv);
1N/A (void) sm_strlcat(obuf, DELIMCHARS, sizeof(obuf));
1N/A for (p = obuf; *p != '\0'; p++)
1N/A {
1N/A if (IntTokenTab[*p & 0xff] == ATM)
1N/A IntTokenTab[*p & 0xff] = OPR;
1N/A if (ExtTokenTab[*p & 0xff] == ATM)
1N/A ExtTokenTab[*p & 0xff] = OPR;
1N/A if (TokTypeNoC[*p & 0xff] == ATM)
1N/A TokTypeNoC[*p & 0xff] = OPR;
1N/A }
1N/A }
1N/A if (toktab == NULL)
1N/A toktab = ExtTokenTab;
1N/A
1N/A /* make sure error messages don't have garbage on them */
1N/A errno = 0;
1N/A
1N/A q = pvpbuf;
1N/A bslashmode = false;
1N/A route_syntax = false;
1N/A cmntcnt = 0;
1N/A anglecnt = 0;
1N/A avp = av;
1N/A state = ATM;
1N/A c = NOCHAR;
1N/A p = addr;
1N/A CurEnv->e_to = p;
1N/A if (tTd(22, 11))
1N/A {
1N/A sm_dprintf("prescan: ");
1N/A xputs(sm_debug_file(), p);
1N/A sm_dprintf("\n");
1N/A }
1N/A
1N/A do
1N/A {
1N/A /* read a token */
1N/A tok = q;
1N/A for (;;)
1N/A {
1N/A /* store away any old lookahead character */
1N/A if (c != NOCHAR && !bslashmode)
1N/A {
1N/A /* see if there is room */
1N/A if (q >= &pvpbuf[pvpbsize - 5])
1N/A {
1N/A addrtoolong:
1N/A usrerr("553 5.1.1 Address too long");
1N/A if (strlen(addr) > MAXNAME)
1N/A addr[MAXNAME] = '\0';
1N/A returnnull:
1N/A if (delimptr != NULL)
1N/A {
1N/A if (p > addr)
1N/A --p;
1N/A *delimptr = p;
1N/A }
1N/A CurEnv->e_to = saveto;
1N/A return NULL;
1N/A }
1N/A
1N/A /* squirrel it away */
1N/A#if !ALLOW_255
1N/A if ((char) c == (char) -1 && !tTd(82, 101) &&
1N/A !EightBitAddrOK)
1N/A c &= 0x7f;
1N/A#endif /* !ALLOW_255 */
1N/A *q++ = c;
1N/A }
1N/A
1N/A /* read a new input character */
1N/A c = (*p++) & 0x00ff;
1N/A if (c == '\0')
1N/A {
1N/A /* diagnose and patch up bad syntax */
1N/A if (ignore)
1N/A break;
1N/A else if (state == QST)
1N/A {
1N/A usrerr("553 Unbalanced '\"'");
1N/A c = '"';
1N/A }
1N/A else if (cmntcnt > 0)
1N/A {
1N/A usrerr("553 Unbalanced '('");
1N/A c = ')';
1N/A }
1N/A else if (anglecnt > 0)
1N/A {
1N/A c = '>';
1N/A usrerr("553 Unbalanced '<'");
1N/A }
1N/A else
1N/A break;
1N/A
1N/A p--;
1N/A }
1N/A else if (c == delim && cmntcnt <= 0 && state != QST)
1N/A {
1N/A if (anglecnt <= 0)
1N/A break;
1N/A
1N/A /* special case for better error management */
1N/A if (delim == ',' && !route_syntax && !ignore)
1N/A {
1N/A usrerr("553 Unbalanced '<'");
1N/A c = '>';
1N/A p--;
1N/A }
1N/A }
1N/A
1N/A if (tTd(22, 101))
1N/A sm_dprintf("c=%c, s=%d; ", c, state);
1N/A
1N/A /* chew up special characters */
1N/A *q = '\0';
1N/A if (bslashmode)
1N/A {
1N/A bslashmode = false;
1N/A
1N/A /* kludge \! for naive users */
1N/A if (cmntcnt > 0)
1N/A {
1N/A c = NOCHAR;
1N/A continue;
1N/A }
1N/A else if (c != '!' || state == QST)
1N/A {
1N/A /* see if there is room */
1N/A if (q >= &pvpbuf[pvpbsize - 5])
1N/A goto addrtoolong;
1N/A *q++ = '\\';
1N/A continue;
1N/A }
1N/A }
1N/A
1N/A if (c == '\\')
1N/A {
1N/A bslashmode = true;
1N/A }
1N/A else if (state == QST)
1N/A {
1N/A /* EMPTY */
1N/A /* do nothing, just avoid next clauses */
1N/A }
1N/A else if (c == '(' && toktab['('] == SPC)
1N/A {
1N/A cmntcnt++;
1N/A c = NOCHAR;
1N/A }
1N/A else if (c == ')' && toktab['('] == SPC)
1N/A {
1N/A if (cmntcnt <= 0)
1N/A {
1N/A if (!ignore)
1N/A {
1N/A usrerr("553 Unbalanced ')'");
1N/A c = NOCHAR;
1N/A }
1N/A }
1N/A else
1N/A cmntcnt--;
1N/A }
1N/A else if (cmntcnt > 0)
1N/A {
1N/A c = NOCHAR;
1N/A }
1N/A else if (c == '<')
1N/A {
1N/A char *ptr = p;
1N/A
1N/A anglecnt++;
1N/A while (isascii(*ptr) && isspace(*ptr))
1N/A ptr++;
1N/A if (*ptr == '@')
1N/A route_syntax = true;
1N/A }
1N/A else if (c == '>')
1N/A {
1N/A if (anglecnt <= 0)
1N/A {
1N/A if (!ignore)
1N/A {
1N/A usrerr("553 Unbalanced '>'");
1N/A c = NOCHAR;
1N/A }
1N/A }
1N/A else
1N/A anglecnt--;
1N/A route_syntax = false;
1N/A }
1N/A else if (delim == ' ' && isascii(c) && isspace(c))
1N/A c = ' ';
1N/A
1N/A if (c == NOCHAR)
1N/A continue;
1N/A
1N/A /* see if this is end of input */
1N/A if (c == delim && anglecnt <= 0 && state != QST)
1N/A break;
1N/A
1N/A newstate = StateTab[state][toktab[c & 0xff]];
1N/A if (tTd(22, 101))
1N/A sm_dprintf("ns=%02o\n", newstate);
1N/A state = newstate & TYPE;
1N/A if (state == ILL)
1N/A {
1N/A if (isascii(c) && isprint(c))
1N/A usrerr("553 Illegal character %c", c);
1N/A else
1N/A usrerr("553 Illegal character 0x%02x",
1N/A c & 0x0ff);
1N/A }
1N/A if (bitset(M, newstate))
1N/A c = NOCHAR;
1N/A if (bitset(B, newstate))
1N/A break;
1N/A }
1N/A
1N/A /* new token */
1N/A if (tok != q)
1N/A {
1N/A /* see if there is room */
1N/A if (q >= &pvpbuf[pvpbsize - 5])
1N/A goto addrtoolong;
1N/A *q++ = '\0';
1N/A if (tTd(22, 36))
1N/A {
1N/A sm_dprintf("tok=");
1N/A xputs(sm_debug_file(), tok);
1N/A sm_dprintf("\n");
1N/A }
1N/A if (avp >= &av[MAXATOM])
1N/A {
1N/A usrerr("553 5.1.0 prescan: too many tokens");
1N/A goto returnnull;
1N/A }
1N/A if (q - tok > MAXNAME)
1N/A {
1N/A usrerr("553 5.1.0 prescan: token too long");
1N/A goto returnnull;
1N/A }
1N/A *avp++ = tok;
1N/A }
1N/A } while (c != '\0' && (c != delim || anglecnt > 0));
1N/A *avp = NULL;
1N/A if (delimptr != NULL)
1N/A {
1N/A if (p > addr)
1N/A p--;
1N/A *delimptr = p;
1N/A }
1N/A if (tTd(22, 12))
1N/A {
1N/A sm_dprintf("prescan==>");
1N/A printav(sm_debug_file(), av);
1N/A }
1N/A CurEnv->e_to = saveto;
1N/A if (av[0] == NULL)
1N/A {
1N/A if (tTd(22, 1))
1N/A sm_dprintf("prescan: null leading token\n");
1N/A return NULL;
1N/A }
1N/A return av;
1N/A}
1N/A/*
1N/A** REWRITE -- apply rewrite rules to token vector.
1N/A**
1N/A** This routine is an ordered production system. Each rewrite
1N/A** rule has a LHS (called the pattern) and a RHS (called the
1N/A** rewrite); 'rwr' points the the current rewrite rule.
1N/A**
1N/A** For each rewrite rule, 'avp' points the address vector we
1N/A** are trying to match against, and 'pvp' points to the pattern.
1N/A** If pvp points to a special match value (MATCHZANY, MATCHANY,
1N/A** MATCHONE, MATCHCLASS, MATCHNCLASS) then the address in avp
1N/A** matched is saved away in the match vector (pointed to by 'mvp').
1N/A**
1N/A** When a match between avp & pvp does not match, we try to
1N/A** back out. If we back up over MATCHONE, MATCHCLASS, or MATCHNCLASS
1N/A** we must also back out the match in mvp. If we reach a
1N/A** MATCHANY or MATCHZANY we just extend the match and start
1N/A** over again.
1N/A**
1N/A** When we finally match, we rewrite the address vector
1N/A** and try over again.
1N/A**
1N/A** Parameters:
1N/A** pvp -- pointer to token vector.
1N/A** ruleset -- the ruleset to use for rewriting.
1N/A** reclevel -- recursion level (to catch loops).
1N/A** e -- the current envelope.
1N/A** maxatom -- maximum length of buffer (usually MAXATOM)
1N/A**
1N/A** Returns:
1N/A** A status code. If EX_TEMPFAIL, higher level code should
1N/A** attempt recovery.
1N/A**
1N/A** Side Effects:
1N/A** pvp is modified.
1N/A*/
1N/A
1N/Astruct match
1N/A{
1N/A char **match_first; /* first token matched */
1N/A char **match_last; /* last token matched */
1N/A char **match_pattern; /* pointer to pattern */
1N/A};
1N/A
1N/Aint
1N/Arewrite(pvp, ruleset, reclevel, e, maxatom)
1N/A char **pvp;
1N/A int ruleset;
1N/A int reclevel;
1N/A register ENVELOPE *e;
1N/A int maxatom;
1N/A{
1N/A register char *ap; /* address pointer */
1N/A register char *rp; /* rewrite pointer */
1N/A register char *rulename; /* ruleset name */
1N/A register char *prefix;
1N/A register char **avp; /* address vector pointer */
1N/A register char **rvp; /* rewrite vector pointer */
1N/A register struct match *mlp; /* cur ptr into mlist */
1N/A register struct rewrite *rwr; /* pointer to current rewrite rule */
1N/A int ruleno; /* current rule number */
1N/A int rstat = EX_OK; /* return status */
1N/A int loopcount;
1N/A struct match mlist[MAXMATCH]; /* stores match on LHS */
1N/A char *npvp[MAXATOM + 1]; /* temporary space for rebuild */
1N/A char buf[MAXLINE];
1N/A char name[6];
1N/A
1N/A /*
1N/A ** mlp will not exceed mlist[] because readcf enforces
1N/A ** the upper limit of entries when reading rulesets.
1N/A */
1N/A
1N/A if (ruleset < 0 || ruleset >= MAXRWSETS)
1N/A {
1N/A syserr("554 5.3.5 rewrite: illegal ruleset number %d", ruleset);
1N/A return EX_CONFIG;
1N/A }
1N/A rulename = RuleSetNames[ruleset];
1N/A if (rulename == NULL)
1N/A {
1N/A (void) sm_snprintf(name, sizeof(name), "%d", ruleset);
1N/A rulename = name;
1N/A }
1N/A if (OpMode == MD_TEST)
1N/A prefix = "";
1N/A else
1N/A prefix = "rewrite: ruleset ";
1N/A if (OpMode == MD_TEST)
1N/A {
1N/A (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
1N/A "%s%-16.16s input:", prefix, rulename);
1N/A printav(smioout, pvp);
1N/A }
1N/A else if (tTd(21, 1))
1N/A {
1N/A sm_dprintf("%s%-16.16s input:", prefix, rulename);
1N/A printav(sm_debug_file(), pvp);
1N/A }
1N/A if (reclevel++ > MaxRuleRecursion)
1N/A {
1N/A syserr("rewrite: excessive recursion (max %d), ruleset %s",
1N/A MaxRuleRecursion, rulename);
1N/A return EX_CONFIG;
1N/A }
1N/A if (pvp == NULL)
1N/A return EX_USAGE;
1N/A if (maxatom <= 0)
1N/A return EX_USAGE;
1N/A
1N/A /*
1N/A ** Run through the list of rewrite rules, applying
1N/A ** any that match.
1N/A */
1N/A
1N/A ruleno = 1;
1N/A loopcount = 0;
1N/A for (rwr = RewriteRules[ruleset]; rwr != NULL; )
1N/A {
1N/A int status;
1N/A
1N/A /* if already canonical, quit now */
1N/A if (pvp[0] != NULL && (pvp[0][0] & 0377) == CANONNET)
1N/A break;
1N/A
1N/A if (tTd(21, 12))
1N/A {
1N/A if (tTd(21, 15))
1N/A sm_dprintf("-----trying rule (line %d):",
1N/A rwr->r_line);
1N/A else
1N/A sm_dprintf("-----trying rule:");
1N/A printav(sm_debug_file(), rwr->r_lhs);
1N/A }
1N/A
1N/A /* try to match on this rule */
1N/A mlp = mlist;
1N/A rvp = rwr->r_lhs;
1N/A avp = pvp;
1N/A if (++loopcount > 100)
1N/A {
1N/A syserr("554 5.3.5 Infinite loop in ruleset %s, rule %d",
1N/A rulename, ruleno);
1N/A if (tTd(21, 1))
1N/A {
1N/A sm_dprintf("workspace: ");
1N/A printav(sm_debug_file(), pvp);
1N/A }
1N/A break;
1N/A }
1N/A
1N/A while ((ap = *avp) != NULL || *rvp != NULL)
1N/A {
1N/A rp = *rvp;
1N/A if (tTd(21, 35))
1N/A {
1N/A sm_dprintf("ADVANCE rp=");
1N/A xputs(sm_debug_file(), rp);
1N/A sm_dprintf(", ap=");
1N/A xputs(sm_debug_file(), ap);
1N/A sm_dprintf("\n");
1N/A }
1N/A if (rp == NULL)
1N/A {
1N/A /* end-of-pattern before end-of-address */
1N/A goto backup;
1N/A }
1N/A if (ap == NULL &&
1N/A (rp[0] & 0377) != MATCHZANY &&
1N/A (rp[0] & 0377) != MATCHZERO)
1N/A {
1N/A /* end-of-input with patterns left */
1N/A goto backup;
1N/A }
1N/A
1N/A switch (rp[0] & 0377)
1N/A {
1N/A case MATCHCLASS:
1N/A /* match any phrase in a class */
1N/A mlp->match_pattern = rvp;
1N/A mlp->match_first = avp;
1N/A extendclass:
1N/A ap = *avp;
1N/A if (ap == NULL)
1N/A goto backup;
1N/A mlp->match_last = avp++;
1N/A cataddr(mlp->match_first, mlp->match_last,
1N/A buf, sizeof(buf), '\0', true);
1N/A if (!wordinclass(buf, rp[1]))
1N/A {
1N/A if (tTd(21, 36))
1N/A {
1N/A sm_dprintf("EXTEND rp=");
1N/A xputs(sm_debug_file(), rp);
1N/A sm_dprintf(", ap=");
1N/A xputs(sm_debug_file(), ap);
1N/A sm_dprintf("\n");
1N/A }
1N/A goto extendclass;
1N/A }
1N/A if (tTd(21, 36))
1N/A sm_dprintf("CLMATCH\n");
1N/A mlp++;
1N/A break;
1N/A
1N/A case MATCHNCLASS:
1N/A /* match any token not in a class */
1N/A if (wordinclass(ap, rp[1]))
1N/A goto backup;
1N/A
1N/A /* FALLTHROUGH */
1N/A
1N/A case MATCHONE:
1N/A case MATCHANY:
1N/A /* match exactly one token */
1N/A mlp->match_pattern = rvp;
1N/A mlp->match_first = avp;
1N/A mlp->match_last = avp++;
1N/A mlp++;
1N/A break;
1N/A
1N/A case MATCHZANY:
1N/A /* match zero or more tokens */
1N/A mlp->match_pattern = rvp;
1N/A mlp->match_first = avp;
1N/A mlp->match_last = avp - 1;
1N/A mlp++;
1N/A break;
1N/A
1N/A case MATCHZERO:
1N/A /* match zero tokens */
1N/A break;
1N/A
1N/A case MACRODEXPAND:
1N/A /*
1N/A ** Match against run-time macro.
1N/A ** This algorithm is broken for the
1N/A ** general case (no recursive macros,
1N/A ** improper tokenization) but should
1N/A ** work for the usual cases.
1N/A */
1N/A
1N/A ap = macvalue(rp[1], e);
1N/A mlp->match_first = avp;
1N/A if (tTd(21, 2))
1N/A sm_dprintf("rewrite: LHS $&{%s} => \"%s\"\n",
1N/A macname(rp[1]),
1N/A ap == NULL ? "(NULL)" : ap);
1N/A
1N/A if (ap == NULL)
1N/A break;
1N/A while (*ap != '\0')
1N/A {
1N/A if (*avp == NULL ||
1N/A sm_strncasecmp(ap, *avp,
1N/A strlen(*avp)) != 0)
1N/A {
1N/A /* no match */
1N/A avp = mlp->match_first;
1N/A goto backup;
1N/A }
1N/A ap += strlen(*avp++);
1N/A }
1N/A
1N/A /* match */
1N/A break;
1N/A
1N/A default:
1N/A /* must have exact match */
1N/A if (sm_strcasecmp(rp, ap))
1N/A goto backup;
1N/A avp++;
1N/A break;
1N/A }
1N/A
1N/A /* successful match on this token */
1N/A rvp++;
1N/A continue;
1N/A
1N/A backup:
1N/A /* match failed -- back up */
1N/A while (--mlp >= mlist)
1N/A {
1N/A rvp = mlp->match_pattern;
1N/A rp = *rvp;
1N/A avp = mlp->match_last + 1;
1N/A ap = *avp;
1N/A
1N/A if (tTd(21, 36))
1N/A {
1N/A sm_dprintf("BACKUP rp=");
1N/A xputs(sm_debug_file(), rp);
1N/A sm_dprintf(", ap=");
1N/A xputs(sm_debug_file(), ap);
1N/A sm_dprintf("\n");
1N/A }
1N/A
1N/A if (ap == NULL)
1N/A {
1N/A /* run off the end -- back up again */
1N/A continue;
1N/A }
1N/A
1N/A if ((rp[0] & 0377) == MATCHANY ||
1N/A (rp[0] & 0377) == MATCHZANY)
1N/A {
1N/A /* extend binding and continue */
1N/A mlp->match_last = avp++;
1N/A rvp++;
1N/A mlp++;
1N/A break;
1N/A }
1N/A if ((rp[0] & 0377) == MATCHCLASS)
1N/A {
1N/A /* extend binding and try again */
1N/A mlp->match_last = avp;
1N/A goto extendclass;
1N/A }
1N/A }
1N/A
1N/A if (mlp < mlist)
1N/A {
1N/A /* total failure to match */
1N/A break;
1N/A }
1N/A }
1N/A
1N/A /*
1N/A ** See if we successfully matched
1N/A */
1N/A
1N/A if (mlp < mlist || *rvp != NULL)
1N/A {
1N/A if (tTd(21, 10))
1N/A sm_dprintf("----- rule fails\n");
1N/A rwr = rwr->r_next;
1N/A ruleno++;
1N/A loopcount = 0;
1N/A continue;
1N/A }
1N/A
1N/A rvp = rwr->r_rhs;
1N/A if (tTd(21, 12))
1N/A {
1N/A sm_dprintf("-----rule matches:");
1N/A printav(sm_debug_file(), rvp);
1N/A }
1N/A
1N/A rp = *rvp;
1N/A if (rp != NULL)
1N/A {
1N/A if ((rp[0] & 0377) == CANONUSER)
1N/A {
1N/A rvp++;
1N/A rwr = rwr->r_next;
1N/A ruleno++;
1N/A loopcount = 0;
1N/A }
1N/A else if ((rp[0] & 0377) == CANONHOST)
1N/A {
1N/A rvp++;
1N/A rwr = NULL;
1N/A }
1N/A }
1N/A
1N/A /* substitute */
1N/A for (avp = npvp; *rvp != NULL; rvp++)
1N/A {
1N/A register struct match *m;
1N/A register char **pp;
1N/A
1N/A rp = *rvp;
1N/A if ((rp[0] & 0377) == MATCHREPL)
1N/A {
1N/A /* substitute from LHS */
1N/A m = &mlist[rp[1] - '1'];
1N/A if (m < mlist || m >= mlp)
1N/A {
1N/A syserr("554 5.3.5 rewrite: ruleset %s: replacement $%c out of bounds",
1N/A rulename, rp[1]);
1N/A return EX_CONFIG;
1N/A }
1N/A if (tTd(21, 15))
1N/A {
1N/A sm_dprintf("$%c:", rp[1]);
1N/A pp = m->match_first;
1N/A while (pp <= m->match_last)
1N/A {
1N/A sm_dprintf(" %p=\"", *pp);
1N/A sm_dflush();
1N/A sm_dprintf("%s\"", *pp++);
1N/A }
1N/A sm_dprintf("\n");
1N/A }
1N/A pp = m->match_first;
1N/A while (pp <= m->match_last)
1N/A {
1N/A if (avp >= &npvp[maxatom])
1N/A goto toolong;
1N/A *avp++ = *pp++;
1N/A }
1N/A }
1N/A else
1N/A {
1N/A /* some sort of replacement */
1N/A if (avp >= &npvp[maxatom])
1N/A {
1N/A toolong:
1N/A syserr("554 5.3.0 rewrite: expansion too long");
1N/A if (LogLevel > 9)
1N/A sm_syslog(LOG_ERR, e->e_id,
1N/A "rewrite: expansion too long, ruleset=%s, ruleno=%d",
1N/A rulename, ruleno);
1N/A return EX_DATAERR;
1N/A }
1N/A if ((rp[0] & 0377) != MACRODEXPAND)
1N/A {
1N/A /* vanilla replacement from RHS */
1N/A *avp++ = rp;
1N/A }
1N/A else
1N/A {
1N/A /* $&{x} replacement */
1N/A char *mval = macvalue(rp[1], e);
1N/A char **xpvp;
1N/A size_t trsize = 0;
1N/A static size_t pvpb1_size = 0;
1N/A static char **pvpb1 = NULL;
1N/A char pvpbuf[PSBUFSIZE];
1N/A
1N/A if (tTd(21, 2))
1N/A sm_dprintf("rewrite: RHS $&{%s} => \"%s\"\n",
1N/A macname(rp[1]),
1N/A mval == NULL ? "(NULL)" : mval);
1N/A if (mval == NULL || *mval == '\0')
1N/A continue;
1N/A
1N/A /* save the remainder of the input */
1N/A for (xpvp = pvp; *xpvp != NULL; xpvp++)
1N/A trsize += sizeof(*xpvp);
1N/A if (trsize > pvpb1_size)
1N/A {
1N/A if (pvpb1 != NULL)
1N/A sm_free(pvpb1);
1N/A pvpb1 = (char **)
1N/A sm_pmalloc_x(trsize);
1N/A pvpb1_size = trsize;
1N/A }
1N/A
1N/A memmove((char *) pvpb1,
1N/A (char *) pvp,
1N/A trsize);
1N/A
1N/A /* scan the new replacement */
1N/A xpvp = prescan(mval, '\0', pvpbuf,
1N/A sizeof(pvpbuf), NULL,
1N/A NULL, false);
1N/A if (xpvp == NULL)
1N/A {
1N/A /* prescan pre-printed error */
1N/A return EX_DATAERR;
1N/A }
1N/A
1N/A /* insert it into the output stream */
1N/A while (*xpvp != NULL)
1N/A {
1N/A if (tTd(21, 19))
1N/A sm_dprintf(" ... %s\n",
1N/A *xpvp);
1N/A *avp++ = sm_rpool_strdup_x(
1N/A e->e_rpool, *xpvp);
1N/A if (avp >= &npvp[maxatom])
1N/A goto toolong;
1N/A xpvp++;
1N/A }
1N/A if (tTd(21, 19))
1N/A sm_dprintf(" ... DONE\n");
1N/A
1N/A /* restore the old trailing input */
1N/A memmove((char *) pvp,
1N/A (char *) pvpb1,
1N/A trsize);
1N/A }
1N/A }
1N/A }
1N/A *avp++ = NULL;
1N/A
1N/A /*
1N/A ** Check for any hostname/keyword lookups.
1N/A */
1N/A
1N/A for (rvp = npvp; *rvp != NULL; rvp++)
1N/A {
1N/A char **hbrvp;
1N/A char **xpvp;
1N/A size_t trsize;
1N/A char *replac;
1N/A int endtoken;
1N/A bool external;
1N/A STAB *map;
1N/A char *mapname;
1N/A char **key_rvp;
1N/A char **arg_rvp;
1N/A char **default_rvp;
1N/A char cbuf[MAXKEY];
1N/A char *pvpb1[MAXATOM + 1];
1N/A char *argvect[MAX_MAP_ARGS];
1N/A char pvpbuf[PSBUFSIZE];
1N/A char *nullpvp[1];
1N/A
1N/A hbrvp = rvp;
1N/A if ((rvp[0][0] & 0377) == HOSTBEGIN)
1N/A {
1N/A endtoken = HOSTEND;
1N/A mapname = "host";
1N/A }
1N/A else if ((rvp[0][0] & 0377) == LOOKUPBEGIN)
1N/A {
1N/A endtoken = LOOKUPEND;
1N/A mapname = *++rvp;
1N/A if (mapname == NULL)
1N/A {
1N/A syserr("554 5.3.0 rewrite: missing mapname");
1N/A /* NOTREACHED */
1N/A SM_ASSERT(0);
1N/A }
1N/A }
1N/A else
1N/A continue;
1N/A
1N/A /*
1N/A ** Got a hostname/keyword lookup.
1N/A **
1N/A ** This could be optimized fairly easily.
1N/A */
1N/A
1N/A map = stab(mapname, ST_MAP, ST_FIND);
1N/A if (map == NULL)
1N/A syserr("554 5.3.0 rewrite: map %s not found",
1N/A mapname);
1N/A
1N/A /* extract the match part */
1N/A key_rvp = ++rvp;
1N/A if (key_rvp == NULL)
1N/A {
1N/A syserr("554 5.3.0 rewrite: missing key for map %s",
1N/A mapname);
1N/A /* NOTREACHED */
1N/A SM_ASSERT(0);
1N/A }
1N/A default_rvp = NULL;
1N/A arg_rvp = argvect;
1N/A xpvp = NULL;
1N/A replac = pvpbuf;
1N/A while (*rvp != NULL && ((rvp[0][0] & 0377) != endtoken))
1N/A {
1N/A int nodetype = rvp[0][0] & 0377;
1N/A
1N/A if (nodetype != CANONHOST &&
1N/A nodetype != CANONUSER)
1N/A {
1N/A rvp++;
1N/A continue;
1N/A }
1N/A
1N/A *rvp++ = NULL;
1N/A
1N/A if (xpvp != NULL)
1N/A {
1N/A cataddr(xpvp, NULL, replac,
1N/A &pvpbuf[sizeof(pvpbuf)] - replac,
1N/A '\0', false);
1N/A if (arg_rvp <
1N/A &argvect[MAX_MAP_ARGS - 1])
1N/A *++arg_rvp = replac;
1N/A replac += strlen(replac) + 1;
1N/A xpvp = NULL;
1N/A }
1N/A switch (nodetype)
1N/A {
1N/A case CANONHOST:
1N/A xpvp = rvp;
1N/A break;
1N/A
1N/A case CANONUSER:
1N/A default_rvp = rvp;
1N/A break;
1N/A }
1N/A }
1N/A if (*rvp != NULL)
1N/A *rvp++ = NULL;
1N/A if (xpvp != NULL)
1N/A {
1N/A cataddr(xpvp, NULL, replac,
1N/A &pvpbuf[sizeof(pvpbuf)] - replac,
1N/A '\0', false);
1N/A if (arg_rvp < &argvect[MAX_MAP_ARGS - 1])
1N/A *++arg_rvp = replac;
1N/A }
1N/A if (arg_rvp >= &argvect[MAX_MAP_ARGS - 1])
1N/A argvect[MAX_MAP_ARGS - 1] = NULL;
1N/A else
1N/A *++arg_rvp = NULL;
1N/A
1N/A /* save the remainder of the input string */
1N/A trsize = (avp - rvp + 1) * sizeof(*rvp);
1N/A memmove((char *) pvpb1, (char *) rvp, trsize);
1N/A
1N/A /* look it up */
1N/A cataddr(key_rvp, NULL, cbuf, sizeof(cbuf),
1N/A map == NULL ? '\0' : map->s_map.map_spacesub,
1N/A true);
1N/A argvect[0] = cbuf;
1N/A replac = map_lookup(map, cbuf, argvect, &rstat, e);
1N/A external = replac != NULL;
1N/A
1N/A /* if no replacement, use default */
1N/A if (replac == NULL && default_rvp != NULL)
1N/A {
1N/A /* create the default */
1N/A cataddr(default_rvp, NULL, cbuf, sizeof(cbuf),
1N/A '\0', false);
1N/A replac = cbuf;
1N/A }
1N/A
1N/A if (replac == NULL)
1N/A {
1N/A xpvp = key_rvp;
1N/A }
1N/A else if (*replac == '\0')
1N/A {
1N/A /* null replacement */
1N/A nullpvp[0] = NULL;
1N/A xpvp = nullpvp;
1N/A }
1N/A else
1N/A {
1N/A /* scan the new replacement */
1N/A xpvp = prescan(replac, '\0', pvpbuf,
1N/A sizeof(pvpbuf), NULL,
1N/A external ? NULL : IntTokenTab,
1N/A false);
1N/A if (xpvp == NULL)
1N/A {
1N/A /* prescan already printed error */
1N/A return EX_DATAERR;
1N/A }
1N/A }
1N/A
1N/A /* append it to the token list */
1N/A for (avp = hbrvp; *xpvp != NULL; xpvp++)
1N/A {
1N/A *avp++ = sm_rpool_strdup_x(e->e_rpool, *xpvp);
1N/A if (avp >= &npvp[maxatom])
1N/A goto toolong;
1N/A }
1N/A
1N/A /* restore the old trailing information */
1N/A rvp = avp - 1;
1N/A for (xpvp = pvpb1; (*avp++ = *xpvp++) != NULL; )
1N/A if (avp >= &npvp[maxatom])
1N/A goto toolong;
1N/A }
1N/A
1N/A /*
1N/A ** Check for subroutine calls.
1N/A */
1N/A
1N/A status = callsubr(npvp, reclevel, e);
1N/A if (rstat == EX_OK || status == EX_TEMPFAIL)
1N/A rstat = status;
1N/A
1N/A /* copy vector back into original space. */
1N/A for (avp = npvp; *avp++ != NULL;)
1N/A continue;
1N/A memmove((char *) pvp, (char *) npvp,
1N/A (int) (avp - npvp) * sizeof(*avp));
1N/A
1N/A if (tTd(21, 4))
1N/A {
1N/A sm_dprintf("rewritten as:");
1N/A printav(sm_debug_file(), pvp);
1N/A }
1N/A }
1N/A
1N/A if (OpMode == MD_TEST)
1N/A {
1N/A (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
1N/A "%s%-16.16s returns:", prefix, rulename);
1N/A printav(smioout, pvp);
1N/A }
1N/A else if (tTd(21, 1))
1N/A {
1N/A sm_dprintf("%s%-16.16s returns:", prefix, rulename);
1N/A printav(sm_debug_file(), pvp);
1N/A }
1N/A return rstat;
1N/A}
1N/A/*
1N/A** CALLSUBR -- call subroutines in rewrite vector
1N/A**
1N/A** Parameters:
1N/A** pvp -- pointer to token vector.
1N/A** reclevel -- the current recursion level.
1N/A** e -- the current envelope.
1N/A**
1N/A** Returns:
1N/A** The status from the subroutine call.
1N/A**
1N/A** Side Effects:
1N/A** pvp is modified.
1N/A*/
1N/A
1N/Astatic int
1N/Acallsubr(pvp, reclevel, e)
1N/A char **pvp;
1N/A int reclevel;
1N/A ENVELOPE *e;
1N/A{
1N/A char **avp;
1N/A register int i;
1N/A int subr, j;
1N/A int nsubr;
1N/A int status;
1N/A int rstat = EX_OK;
1N/A#define MAX_SUBR 16
1N/A int subrnumber[MAX_SUBR];
1N/A int subrindex[MAX_SUBR];
1N/A
1N/A nsubr = 0;
1N/A
1N/A /*
1N/A ** Look for subroutine calls in pvp, collect them into subr*[]
1N/A ** We will perform the calls in the next loop, because we will
1N/A ** call the "last" subroutine first to avoid recursive calls
1N/A ** and too much copying.
1N/A */
1N/A
1N/A for (avp = pvp, j = 0; *avp != NULL; avp++, j++)
1N/A {
1N/A if ((avp[0][0] & 0377) == CALLSUBR && avp[1] != NULL)
1N/A {
1N/A stripquotes(avp[1]);
1N/A subr = strtorwset(avp[1], NULL, ST_FIND);
1N/A if (subr < 0)
1N/A {
1N/A syserr("554 5.3.5 Unknown ruleset %s", avp[1]);
1N/A return EX_CONFIG;
1N/A }
1N/A
1N/A /*
1N/A ** XXX instead of doing this we could optimize
1N/A ** the rules after reading them: just remove
1N/A ** calls to empty rulesets
1N/A */
1N/A
1N/A /* subroutine is an empty ruleset? don't call it */
1N/A if (RewriteRules[subr] == NULL)
1N/A {
1N/A if (tTd(21, 3))
1N/A sm_dprintf("-----skip subr %s (%d)\n",
1N/A avp[1], subr);
1N/A for (i = 2; avp[i] != NULL; i++)
1N/A avp[i - 2] = avp[i];
1N/A avp[i - 2] = NULL;
1N/A continue;
1N/A }
1N/A if (++nsubr >= MAX_SUBR)
1N/A {
1N/A syserr("554 5.3.0 Too many subroutine calls (%d max)",
1N/A MAX_SUBR);
1N/A return EX_CONFIG;
1N/A }
1N/A subrnumber[nsubr] = subr;
1N/A subrindex[nsubr] = j;
1N/A }
1N/A }
1N/A
1N/A /*
1N/A ** Perform the actual subroutines calls, "last" one first, i.e.,
1N/A ** go from the right to the left through all calls,
1N/A ** do the rewriting in place.
1N/A */
1N/A
1N/A for (; nsubr > 0; nsubr--)
1N/A {
1N/A subr = subrnumber[nsubr];
1N/A avp = pvp + subrindex[nsubr];
1N/A
1N/A /* remove the subroutine call and name */
1N/A for (i = 2; avp[i] != NULL; i++)
1N/A avp[i - 2] = avp[i];
1N/A avp[i - 2] = NULL;
1N/A
1N/A /*
1N/A ** Now we need to call the ruleset specified for
1N/A ** the subroutine. We can do this in place since
1N/A ** we call the "last" subroutine first.
1N/A */
1N/A
1N/A status = rewrite(avp, subr, reclevel, e,
1N/A MAXATOM - subrindex[nsubr]);
1N/A if (status != EX_OK && status != EX_TEMPFAIL)
1N/A return status;
1N/A if (rstat == EX_OK || status == EX_TEMPFAIL)
1N/A rstat = status;
1N/A }
1N/A return rstat;
1N/A}
1N/A/*
1N/A** MAP_LOOKUP -- do lookup in map
1N/A**
1N/A** Parameters:
1N/A** smap -- the map to use for the lookup.
1N/A** key -- the key to look up.
1N/A** argvect -- arguments to pass to the map lookup.
1N/A** pstat -- a pointer to an integer in which to store the
1N/A** status from the lookup.
1N/A** e -- the current envelope.
1N/A**
1N/A** Returns:
1N/A** The result of the lookup.
1N/A** NULL -- if there was no data for the given key.
1N/A*/
1N/A
1N/Astatic char *
1N/Amap_lookup(smap, key, argvect, pstat, e)
1N/A STAB *smap;
1N/A char key[];
1N/A char **argvect;
1N/A int *pstat;
1N/A ENVELOPE *e;
1N/A{
1N/A auto int status = EX_OK;
1N/A MAP *map;
1N/A char *replac;
1N/A
1N/A if (smap == NULL)
1N/A return NULL;
1N/A
1N/A map = &smap->s_map;
1N/A DYNOPENMAP(map);
1N/A
1N/A if (e->e_sendmode == SM_DEFER &&
1N/A bitset(MF_DEFER, map->map_mflags))
1N/A {
1N/A /* don't do any map lookups */
1N/A if (tTd(60, 1))
1N/A sm_dprintf("map_lookup(%s, %s) => DEFERRED\n",
1N/A smap->s_name, key);
1N/A *pstat = EX_TEMPFAIL;
1N/A return NULL;
1N/A }
1N/A
1N/A if (!bitset(MF_KEEPQUOTES, map->map_mflags))
1N/A stripquotes(key);
1N/A
1N/A if (tTd(60, 1))
1N/A {
1N/A sm_dprintf("map_lookup(%s, ", smap->s_name);
1N/A xputs(sm_debug_file(), key);
1N/A if (tTd(60, 5))
1N/A {
1N/A int i;
1N/A
1N/A for (i = 0; argvect[i] != NULL; i++)
1N/A sm_dprintf(", %%%d=%s", i, argvect[i]);
1N/A }
1N/A sm_dprintf(") => ");
1N/A }
1N/A replac = (*map->map_class->map_lookup)(map, key, argvect, &status);
1N/A if (tTd(60, 1))
1N/A sm_dprintf("%s (%d)\n",
1N/A replac != NULL ? replac : "NOT FOUND",
1N/A status);
1N/A
1N/A /* should recover if status == EX_TEMPFAIL */
1N/A if (status == EX_TEMPFAIL && !bitset(MF_NODEFER, map->map_mflags))
1N/A {
1N/A *pstat = EX_TEMPFAIL;
1N/A if (tTd(60, 1))
1N/A sm_dprintf("map_lookup(%s, %s) tempfail: errno=%d\n",
1N/A smap->s_name, key, errno);
1N/A if (e->e_message == NULL)
1N/A {
1N/A char mbuf[320];
1N/A
1N/A (void) sm_snprintf(mbuf, sizeof(mbuf),
1N/A "%.80s map: lookup (%s): deferred",
1N/A smap->s_name,
1N/A shortenstring(key, MAXSHORTSTR));
1N/A e->e_message = sm_rpool_strdup_x(e->e_rpool, mbuf);
1N/A }
1N/A }
1N/A if (status == EX_TEMPFAIL && map->map_tapp != NULL)
1N/A {
1N/A size_t i = strlen(key) + strlen(map->map_tapp) + 1;
1N/A static char *rwbuf = NULL;
1N/A static size_t rwbuflen = 0;
1N/A
1N/A if (i > rwbuflen)
1N/A {
1N/A if (rwbuf != NULL)
1N/A sm_free(rwbuf);
1N/A rwbuflen = i;
1N/A rwbuf = (char *) sm_pmalloc_x(rwbuflen);
1N/A }
1N/A (void) sm_strlcpyn(rwbuf, rwbuflen, 2, key, map->map_tapp);
1N/A if (tTd(60, 4))
1N/A sm_dprintf("map_lookup tempfail: returning \"%s\"\n",
1N/A rwbuf);
1N/A return rwbuf;
1N/A }
1N/A return replac;
1N/A}
1N/A/*
1N/A** INITERRMAILERS -- initialize error and discard mailers
1N/A**
1N/A** Parameters:
1N/A** none.
1N/A**
1N/A** Returns:
1N/A** none.
1N/A**
1N/A** Side Effects:
1N/A** initializes error and discard mailers.
1N/A*/
1N/A
1N/Astatic MAILER discardmailer;
1N/Astatic MAILER errormailer;
1N/Astatic char *discardargv[] = { "DISCARD", NULL };
1N/Astatic char *errorargv[] = { "ERROR", NULL };
1N/A
1N/Avoid
1N/Ainiterrmailers()
1N/A{
1N/A if (discardmailer.m_name == NULL)
1N/A {
1N/A /* initialize the discard mailer */
1N/A discardmailer.m_name = "*discard*";
1N/A discardmailer.m_mailer = "DISCARD";
1N/A discardmailer.m_argv = discardargv;
1N/A }
1N/A if (errormailer.m_name == NULL)
1N/A {
1N/A /* initialize the bogus mailer */
1N/A errormailer.m_name = "*error*";
1N/A errormailer.m_mailer = "ERROR";
1N/A errormailer.m_argv = errorargv;
1N/A }
1N/A}
1N/A/*
1N/A** BUILDADDR -- build address from token vector.
1N/A**
1N/A** Parameters:
1N/A** tv -- token vector.
1N/A** a -- pointer to address descriptor to fill.
1N/A** If NULL, one will be allocated.
1N/A** flags -- info regarding whether this is a sender or
1N/A** a recipient.
1N/A** e -- the current envelope.
1N/A**
1N/A** Returns:
1N/A** NULL if there was an error.
1N/A** 'a' otherwise.
1N/A**
1N/A** Side Effects:
1N/A** fills in 'a'
1N/A*/
1N/A
1N/Astatic struct errcodes
1N/A{
1N/A char *ec_name; /* name of error code */
1N/A int ec_code; /* numeric code */
1N/A} ErrorCodes[] =
1N/A{
1N/A { "usage", EX_USAGE },
1N/A { "nouser", EX_NOUSER },
1N/A { "nohost", EX_NOHOST },
1N/A { "unavailable", EX_UNAVAILABLE },
1N/A { "software", EX_SOFTWARE },
1N/A { "tempfail", EX_TEMPFAIL },
1N/A { "protocol", EX_PROTOCOL },
1N/A { "config", EX_CONFIG },
1N/A { NULL, EX_UNAVAILABLE }
1N/A};
1N/A
1N/Astatic ADDRESS *
1N/Abuildaddr(tv, a, flags, e)
1N/A register char **tv;
1N/A register ADDRESS *a;
1N/A int flags;
1N/A register ENVELOPE *e;
1N/A{
1N/A bool tempfail = false;
1N/A int maxatom;
1N/A struct mailer **mp;
1N/A register struct mailer *m;
1N/A register char *p;
1N/A char *mname;
1N/A char **hostp;
1N/A char hbuf[MAXNAME + 1];
1N/A static char ubuf[MAXNAME + 2];
1N/A
1N/A if (tTd(24, 5))
1N/A {
1N/A sm_dprintf("buildaddr, flags=%x, tv=", flags);
1N/A printav(sm_debug_file(), tv);
1N/A }
1N/A
1N/A maxatom = MAXATOM;
1N/A if (a == NULL)
1N/A a = (ADDRESS *) sm_rpool_malloc_x(e->e_rpool, sizeof(*a));
1N/A memset((char *) a, '\0', sizeof(*a));
1N/A hbuf[0] = '\0';
1N/A
1N/A /* set up default error return flags */
1N/A a->q_flags |= DefaultNotify;
1N/A
1N/A /* figure out what net/mailer to use */
1N/A if (*tv == NULL || (**tv & 0377) != CANONNET)
1N/A {
1N/A syserr("554 5.3.5 buildaddr: no mailer in parsed address");
1N/Abadaddr:
1N/A /*
1N/A ** ExitStat may have been set by an earlier map open
1N/A ** failure (to a permanent error (EX_OSERR) in syserr())
1N/A ** so we also need to check if this particular $#error
1N/A ** return wanted a 4XX failure.
1N/A **
1N/A ** XXX the real fix is probably to set ExitStat correctly,
1N/A ** i.e., to EX_TEMPFAIL if the map open is just a temporary
1N/A ** error.
1N/A */
1N/A
1N/A if (ExitStat == EX_TEMPFAIL || tempfail)
1N/A a->q_state = QS_QUEUEUP;
1N/A else
1N/A {
1N/A a->q_state = QS_BADADDR;
1N/A a->q_mailer = &errormailer;
1N/A }
1N/A return a;
1N/A }
1N/A mname = *++tv;
1N/A --maxatom;
1N/A
1N/A /* extract host and user portions */
1N/A if (*++tv != NULL && (**tv & 0377) == CANONHOST)
1N/A {
1N/A hostp = ++tv;
1N/A --maxatom;
1N/A }
1N/A else
1N/A hostp = NULL;
1N/A --maxatom;
1N/A while (*tv != NULL && (**tv & 0377) != CANONUSER)
1N/A {
1N/A tv++;
1N/A --maxatom;
1N/A }
1N/A if (*tv == NULL)
1N/A {
1N/A syserr("554 5.3.5 buildaddr: no user");
1N/A goto badaddr;
1N/A }
1N/A if (tv == hostp)
1N/A hostp = NULL;
1N/A else if (hostp != NULL)
1N/A cataddr(hostp, tv - 1, hbuf, sizeof(hbuf), '\0', false);
1N/A cataddr(++tv, NULL, ubuf, sizeof(ubuf), ' ', false);
1N/A --maxatom;
1N/A
1N/A /* save away the host name */
1N/A if (sm_strcasecmp(mname, "error") == 0)
1N/A {
1N/A /* Set up triplet for use by -bv */
1N/A a->q_mailer = &errormailer;
1N/A a->q_user = sm_rpool_strdup_x(e->e_rpool, ubuf);
1N/A /* XXX wrong place? */
1N/A
1N/A if (hostp != NULL)
1N/A {
1N/A register struct errcodes *ep;
1N/A
1N/A a->q_host = sm_rpool_strdup_x(e->e_rpool, hbuf);
1N/A if (strchr(hbuf, '.') != NULL)
1N/A {
1N/A a->q_status = sm_rpool_strdup_x(e->e_rpool,
1N/A hbuf);
1N/A setstat(dsntoexitstat(hbuf));
1N/A }
1N/A else if (isascii(hbuf[0]) && isdigit(hbuf[0]))
1N/A {
1N/A setstat(atoi(hbuf));
1N/A }
1N/A else
1N/A {
1N/A for (ep = ErrorCodes; ep->ec_name != NULL; ep++)
1N/A if (sm_strcasecmp(ep->ec_name, hbuf) == 0)
1N/A break;
1N/A setstat(ep->ec_code);
1N/A }
1N/A }
1N/A else
1N/A {
1N/A a->q_host = NULL;
1N/A setstat(EX_UNAVAILABLE);
1N/A }
1N/A stripquotes(ubuf);
1N/A if (ISSMTPCODE(ubuf) && ubuf[3] == ' ')
1N/A {
1N/A char fmt[16];
1N/A int off;
1N/A
1N/A if ((off = isenhsc(ubuf + 4, ' ')) > 0)
1N/A {
1N/A ubuf[off + 4] = '\0';
1N/A off += 5;
1N/A }
1N/A else
1N/A {
1N/A off = 4;
1N/A ubuf[3] = '\0';
1N/A }
1N/A (void) sm_strlcpyn(fmt, sizeof(fmt), 2, ubuf, " %s");
1N/A if (off > 4)
1N/A usrerr(fmt, ubuf + off);
1N/A else if (isenhsc(hbuf, '\0') > 0)
1N/A usrerrenh(hbuf, fmt, ubuf + off);
1N/A else
1N/A usrerr(fmt, ubuf + off);
1N/A /* XXX ubuf[off - 1] = ' '; */
1N/A if (ubuf[0] == '4')
1N/A tempfail = true;
1N/A }
1N/A else
1N/A {
1N/A usrerr("553 5.3.0 %s", ubuf);
1N/A }
1N/A goto badaddr;
1N/A }
1N/A
1N/A for (mp = Mailer; (m = *mp++) != NULL; )
1N/A {
1N/A if (sm_strcasecmp(m->m_name, mname) == 0)
1N/A break;
1N/A }
1N/A if (m == NULL)
1N/A {
1N/A syserr("554 5.3.5 buildaddr: unknown mailer %s", mname);
1N/A goto badaddr;
1N/A }
1N/A a->q_mailer = m;
1N/A
1N/A /* figure out what host (if any) */
1N/A if (hostp == NULL)
1N/A {
1N/A if (!bitnset(M_LOCALMAILER, m->m_flags))
1N/A {
1N/A syserr("554 5.3.5 buildaddr: no host");
1N/A goto badaddr;
1N/A }
1N/A a->q_host = NULL;
1N/A }
1N/A else
1N/A a->q_host = sm_rpool_strdup_x(e->e_rpool, hbuf);
1N/A
1N/A /* figure out the user */
1N/A p = ubuf;
1N/A if (bitnset(M_CHECKUDB, m->m_flags) && *p == '@')
1N/A {
1N/A p++;
1N/A tv++;
1N/A --maxatom;
1N/A a->q_flags |= QNOTREMOTE;
1N/A }
1N/A
1N/A /* do special mapping for local mailer */
1N/A if (*p == '"')
1N/A p++;
1N/A if (*p == '|' && bitnset(M_CHECKPROG, m->m_flags))
1N/A a->q_mailer = m = ProgMailer;
1N/A else if (*p == '/' && bitnset(M_CHECKFILE, m->m_flags))
1N/A a->q_mailer = m = FileMailer;
1N/A else if (*p == ':' && bitnset(M_CHECKINCLUDE, m->m_flags))
1N/A {
1N/A /* may be :include: */
1N/A stripquotes(ubuf);
1N/A if (sm_strncasecmp(ubuf, ":include:", 9) == 0)
1N/A {
1N/A /* if :include:, don't need further rewriting */
1N/A a->q_mailer = m = InclMailer;
1N/A a->q_user = sm_rpool_strdup_x(e->e_rpool, &ubuf[9]);
1N/A return a;
1N/A }
1N/A }
1N/A
1N/A /* rewrite according recipient mailer rewriting rules */
1N/A macdefine(&e->e_macro, A_PERM, 'h', a->q_host);
1N/A
1N/A if (ConfigLevel >= 10 ||
1N/A !bitset(RF_SENDERADDR|RF_HEADERADDR, flags))
1N/A {
1N/A /* sender addresses done later */
1N/A (void) rewrite(tv, 2, 0, e, maxatom);
1N/A if (m->m_re_rwset > 0)
1N/A (void) rewrite(tv, m->m_re_rwset, 0, e, maxatom);
1N/A }
1N/A (void) rewrite(tv, 4, 0, e, maxatom);
1N/A
1N/A /* save the result for the command line/RCPT argument */
1N/A cataddr(tv, NULL, ubuf, sizeof(ubuf), '\0', true);
1N/A a->q_user = sm_rpool_strdup_x(e->e_rpool, ubuf);
1N/A
1N/A /*
1N/A ** Do mapping to lower case as requested by mailer
1N/A */
1N/A
1N/A if (a->q_host != NULL && !bitnset(M_HST_UPPER, m->m_flags))
1N/A makelower(a->q_host);
1N/A if (!bitnset(M_USR_UPPER, m->m_flags))
1N/A makelower(a->q_user);
1N/A
1N/A if (tTd(24, 6))
1N/A {
1N/A sm_dprintf("buildaddr => ");
1N/A printaddr(sm_debug_file(), a, false);
1N/A }
1N/A return a;
1N/A}
1N/A
1N/A/*
1N/A** CATADDR -- concatenate pieces of addresses (putting in <LWSP> subs)
1N/A**
1N/A** Parameters:
1N/A** pvp -- parameter vector to rebuild.
1N/A** evp -- last parameter to include. Can be NULL to
1N/A** use entire pvp.
1N/A** buf -- buffer to build the string into.
1N/A** sz -- size of buf.
1N/A** spacesub -- the space separator character; if '\0',
1N/A** use SpaceSub.
1N/A** external -- convert to external form?
1N/A** (no metacharacters; METAQUOTEs removed, see below)
1N/A**
1N/A** Returns:
1N/A** none.
1N/A**
1N/A** Side Effects:
1N/A** Destroys buf.
1N/A**
1N/A** Notes:
1N/A** There are two formats for strings: internal and external.
1N/A** The external format is just an eight-bit clean string (no
1N/A** null bytes, everything else OK). The internal format can
1N/A** include sendmail metacharacters. The special character
1N/A** METAQUOTE essentially quotes the character following, stripping
1N/A** it of all special semantics.
1N/A**
1N/A** The cataddr routine needs to be aware of whether it is producing
1N/A** an internal or external form as output (it only takes internal
1N/A** form as input).
1N/A**
1N/A** The parseaddr routine has a similar issue on input, but that
1N/A** is flagged on the basis of which token table is passed in.
1N/A*/
1N/A
1N/Avoid
1N/Acataddr(pvp, evp, buf, sz, spacesub, external)
1N/A char **pvp;
1N/A char **evp;
1N/A char *buf;
1N/A register int sz;
1N/A int spacesub;
1N/A bool external;
1N/A{
1N/A bool oatomtok, natomtok;
1N/A char *p;
1N/A
1N/A oatomtok = natomtok = false;
1N/A if (tTd(59, 14))
1N/A {
1N/A sm_dprintf("cataddr(%d) <==", external);
1N/A printav(sm_debug_file(), pvp);
1N/A }
1N/A
1N/A if (sz <= 0)
1N/A return;
1N/A
1N/A if (spacesub == '\0')
1N/A spacesub = SpaceSub;
1N/A
1N/A if (pvp == NULL)
1N/A {
1N/A *buf = '\0';
1N/A return;
1N/A }
1N/A p = buf;
1N/A sz -= 2;
1N/A while (*pvp != NULL && sz > 0)
1N/A {
1N/A char *q;
1N/A
1N/A natomtok = (IntTokenTab[**pvp & 0xff] == ATM);
1N/A if (oatomtok && natomtok)
1N/A {
1N/A *p++ = spacesub;
1N/A if (--sz <= 0)
1N/A break;
1N/A }
1N/A for (q = *pvp; *q != '\0'; )
1N/A {
1N/A int c;
1N/A
1N/A if (--sz <= 0)
1N/A break;
1N/A *p++ = c = *q++;
1N/A
1N/A /*
1N/A ** If the current character (c) is METAQUOTE and we
1N/A ** want the "external" form and the next character
1N/A ** is not NUL, then overwrite METAQUOTE with that
1N/A ** character (i.e., METAQUOTE ch is changed to
1N/A ** ch). p[-1] is used because p is advanced (above).
1N/A */
1N/A
1N/A if ((c & 0377) == METAQUOTE && external && *q != '\0')
1N/A p[-1] = *q++;
1N/A }
1N/A if (sz <= 0)
1N/A break;
1N/A oatomtok = natomtok;
1N/A if (pvp++ == evp)
1N/A break;
1N/A }
1N/A
1N/A#if 0
1N/A /*
1N/A ** Silently truncate long strings: even though this doesn't
1N/A ** seem like a good idea it is necessary because header checks
1N/A ** send the whole header value to rscheck() and hence rewrite().
1N/A ** The latter however sometimes uses a "short" buffer (e.g.,
1N/A ** cbuf[MAXNAME + 1]) to call cataddr() which then triggers this
1N/A ** error function. One possible fix to the problem is to pass
1N/A ** flags to rscheck() and rewrite() to distinguish the various
1N/A ** calls and only trigger the error if necessary. For now just
1N/A ** undo the change from 8.13.0.
1N/A */
1N/A
1N/A if (sz <= 0)
1N/A usrerr("cataddr: string too long");
1N/A#endif
1N/A *p = '\0';
1N/A
1N/A if (tTd(59, 14))
1N/A sm_dprintf(" cataddr => %s\n", str2prt(buf));
1N/A}
1N/A
1N/A/*
1N/A** SAMEADDR -- Determine if two addresses are the same
1N/A**
1N/A** This is not just a straight comparison -- if the mailer doesn't
1N/A** care about the host we just ignore it, etc.
1N/A**
1N/A** Parameters:
1N/A** a, b -- pointers to the internal forms to compare.
1N/A**
1N/A** Returns:
1N/A** true -- they represent the same mailbox.
1N/A** false -- they don't.
1N/A**
1N/A** Side Effects:
1N/A** none.
1N/A*/
1N/A
1N/Abool
1N/Asameaddr(a, b)
1N/A register ADDRESS *a;
1N/A register ADDRESS *b;
1N/A{
1N/A register ADDRESS *ca, *cb;
1N/A
1N/A /* if they don't have the same mailer, forget it */
1N/A if (a->q_mailer != b->q_mailer)
1N/A return false;
1N/A
1N/A /* if the user isn't the same, we can drop out */
1N/A if (strcmp(a->q_user, b->q_user) != 0)
1N/A return false;
1N/A
1N/A /* if we have good uids for both but they differ, these are different */
1N/A if (a->q_mailer == ProgMailer)
1N/A {
1N/A ca = getctladdr(a);
1N/A cb = getctladdr(b);
1N/A if (ca != NULL && cb != NULL &&
1N/A bitset(QGOODUID, ca->q_flags & cb->q_flags) &&
1N/A ca->q_uid != cb->q_uid)
1N/A return false;
1N/A }
1N/A
1N/A /* otherwise compare hosts (but be careful for NULL ptrs) */
1N/A if (a->q_host == b->q_host)
1N/A {
1N/A /* probably both null pointers */
1N/A return true;
1N/A }
1N/A if (a->q_host == NULL || b->q_host == NULL)
1N/A {
1N/A /* only one is a null pointer */
1N/A return false;
1N/A }
1N/A if (strcmp(a->q_host, b->q_host) != 0)
1N/A return false;
1N/A
1N/A return true;
1N/A}
1N/A/*
1N/A** PRINTADDR -- print address (for debugging)
1N/A**
1N/A** Parameters:
1N/A** a -- the address to print
1N/A** follow -- follow the q_next chain.
1N/A**
1N/A** Returns:
1N/A** none.
1N/A**
1N/A** Side Effects:
1N/A** none.
1N/A*/
1N/A
1N/Astruct qflags
1N/A{
1N/A char *qf_name;
1N/A unsigned long qf_bit;
1N/A};
1N/A
1N/Astatic struct qflags AddressFlags[] =
1N/A{
1N/A { "QGOODUID", QGOODUID },
1N/A { "QPRIMARY", QPRIMARY },
1N/A { "QNOTREMOTE", QNOTREMOTE },
1N/A { "QSELFREF", QSELFREF },
1N/A { "QBOGUSSHELL", QBOGUSSHELL },
1N/A { "QUNSAFEADDR", QUNSAFEADDR },
1N/A { "QPINGONSUCCESS", QPINGONSUCCESS },
1N/A { "QPINGONFAILURE", QPINGONFAILURE },
1N/A { "QPINGONDELAY", QPINGONDELAY },
1N/A { "QHASNOTIFY", QHASNOTIFY },
1N/A { "QRELAYED", QRELAYED },
1N/A { "QEXPANDED", QEXPANDED },
1N/A { "QDELIVERED", QDELIVERED },
1N/A { "QDELAYED", QDELAYED },
1N/A { "QTHISPASS", QTHISPASS },
1N/A { "QRCPTOK", QRCPTOK },
1N/A { NULL, 0 }
1N/A};
1N/A
1N/Avoid
1N/Aprintaddr(fp, a, follow)
1N/A SM_FILE_T *fp;
1N/A register ADDRESS *a;
1N/A bool follow;
1N/A{
1N/A register MAILER *m;
1N/A MAILER pseudomailer;
1N/A register struct qflags *qfp;
1N/A bool firstone;
1N/A
1N/A if (a == NULL)
1N/A {
1N/A (void) sm_io_fprintf(fp, SM_TIME_DEFAULT, "[NULL]\n");
1N/A return;
1N/A }
1N/A
1N/A while (a != NULL)
1N/A {
1N/A (void) sm_io_fprintf(fp, SM_TIME_DEFAULT, "%p=", a);
1N/A (void) sm_io_flush(fp, SM_TIME_DEFAULT);
1N/A
1N/A /* find the mailer -- carefully */
1N/A m = a->q_mailer;
1N/A if (m == NULL)
1N/A {
1N/A m = &pseudomailer;
1N/A m->m_mno = -1;
1N/A m->m_name = "NULL";
1N/A }
1N/A
1N/A (void) sm_io_fprintf(fp, SM_TIME_DEFAULT,
1N/A "%s:\n\tmailer %d (%s), host `%s'\n",
1N/A a->q_paddr == NULL ? "<null>" : a->q_paddr,
1N/A m->m_mno, m->m_name,
1N/A a->q_host == NULL ? "<null>" : a->q_host);
1N/A (void) sm_io_fprintf(fp, SM_TIME_DEFAULT,
1N/A "\tuser `%s', ruser `%s'\n",
1N/A a->q_user,
1N/A a->q_ruser == NULL ? "<null>" : a->q_ruser);
1N/A (void) sm_io_fprintf(fp, SM_TIME_DEFAULT, "\tstate=");
1N/A switch (a->q_state)
1N/A {
1N/A case QS_OK:
1N/A (void) sm_io_fprintf(fp, SM_TIME_DEFAULT, "OK");
1N/A break;
1N/A
1N/A case QS_DONTSEND:
1N/A (void) sm_io_fprintf(fp, SM_TIME_DEFAULT,
1N/A "DONTSEND");
1N/A break;
1N/A
1N/A case QS_BADADDR:
1N/A (void) sm_io_fprintf(fp, SM_TIME_DEFAULT,
1N/A "BADADDR");
1N/A break;
1N/A
1N/A case QS_QUEUEUP:
1N/A (void) sm_io_fprintf(fp, SM_TIME_DEFAULT,
1N/A "QUEUEUP");
1N/A break;
1N/A
1N/A case QS_RETRY:
1N/A (void) sm_io_fprintf(fp, SM_TIME_DEFAULT, "RETRY");
1N/A break;
1N/A
1N/A case QS_SENT:
1N/A (void) sm_io_fprintf(fp, SM_TIME_DEFAULT, "SENT");
1N/A break;
1N/A
1N/A case QS_VERIFIED:
1N/A (void) sm_io_fprintf(fp, SM_TIME_DEFAULT,
1N/A "VERIFIED");
1N/A break;
1N/A
1N/A case QS_EXPANDED:
1N/A (void) sm_io_fprintf(fp, SM_TIME_DEFAULT,
1N/A "EXPANDED");
1N/A break;
1N/A
1N/A case QS_SENDER:
1N/A (void) sm_io_fprintf(fp, SM_TIME_DEFAULT,
1N/A "SENDER");
1N/A break;
1N/A
1N/A case QS_CLONED:
1N/A (void) sm_io_fprintf(fp, SM_TIME_DEFAULT,
1N/A "CLONED");
1N/A break;
1N/A
1N/A case QS_DISCARDED:
1N/A (void) sm_io_fprintf(fp, SM_TIME_DEFAULT,
1N/A "DISCARDED");
1N/A break;
1N/A
1N/A case QS_REPLACED:
1N/A (void) sm_io_fprintf(fp, SM_TIME_DEFAULT,
1N/A "REPLACED");
1N/A break;
1N/A
1N/A case QS_REMOVED:
1N/A (void) sm_io_fprintf(fp, SM_TIME_DEFAULT,
1N/A "REMOVED");
1N/A break;
1N/A
1N/A case QS_DUPLICATE:
1N/A (void) sm_io_fprintf(fp, SM_TIME_DEFAULT,
1N/A "DUPLICATE");
1N/A break;
1N/A
1N/A case QS_INCLUDED:
1N/A (void) sm_io_fprintf(fp, SM_TIME_DEFAULT,
1N/A "INCLUDED");
1N/A break;
1N/A
1N/A default:
1N/A (void) sm_io_fprintf(fp, SM_TIME_DEFAULT,
1N/A "%d", a->q_state);
1N/A break;
1N/A }
1N/A (void) sm_io_fprintf(fp, SM_TIME_DEFAULT,
1N/A ", next=%p, alias %p, uid %d, gid %d\n",
1N/A a->q_next, a->q_alias,
1N/A (int) a->q_uid, (int) a->q_gid);
1N/A (void) sm_io_fprintf(fp, SM_TIME_DEFAULT, "\tflags=%lx<",
1N/A a->q_flags);
1N/A firstone = true;
1N/A for (qfp = AddressFlags; qfp->qf_name != NULL; qfp++)
1N/A {
1N/A if (!bitset(qfp->qf_bit, a->q_flags))
1N/A continue;
1N/A if (!firstone)
1N/A (void) sm_io_fprintf(fp, SM_TIME_DEFAULT,
1N/A ",");
1N/A firstone = false;
1N/A (void) sm_io_fprintf(fp, SM_TIME_DEFAULT, "%s",
1N/A qfp->qf_name);
1N/A }
1N/A (void) sm_io_fprintf(fp, SM_TIME_DEFAULT, ">\n");
1N/A (void) sm_io_fprintf(fp, SM_TIME_DEFAULT,
1N/A "\towner=%s, home=\"%s\", fullname=\"%s\"\n",
1N/A a->q_owner == NULL ? "(none)" : a->q_owner,
1N/A a->q_home == NULL ? "(none)" : a->q_home,
1N/A a->q_fullname == NULL ? "(none)" : a->q_fullname);
1N/A (void) sm_io_fprintf(fp, SM_TIME_DEFAULT,
1N/A "\torcpt=\"%s\", statmta=%s, status=%s\n",
1N/A a->q_orcpt == NULL ? "(none)" : a->q_orcpt,
1N/A a->q_statmta == NULL ? "(none)" : a->q_statmta,
1N/A a->q_status == NULL ? "(none)" : a->q_status);
1N/A (void) sm_io_fprintf(fp, SM_TIME_DEFAULT,
1N/A "\tfinalrcpt=\"%s\"\n",
1N/A a->q_finalrcpt == NULL ? "(none)" : a->q_finalrcpt);
1N/A (void) sm_io_fprintf(fp, SM_TIME_DEFAULT,
1N/A "\trstatus=\"%s\"\n",
1N/A a->q_rstatus == NULL ? "(none)" : a->q_rstatus);
1N/A (void) sm_io_fprintf(fp, SM_TIME_DEFAULT,
1N/A "\tstatdate=%s\n",
1N/A a->q_statdate == 0 ? "(none)" : ctime(&a->q_statdate));
1N/A
1N/A if (!follow)
1N/A return;
1N/A a = a->q_next;
1N/A }
1N/A}
1N/A/*
1N/A** EMPTYADDR -- return true if this address is empty (``<>'')
1N/A**
1N/A** Parameters:
1N/A** a -- pointer to the address
1N/A**
1N/A** Returns:
1N/A** true -- if this address is "empty" (i.e., no one should
1N/A** ever generate replies to it.
1N/A** false -- if it is a "regular" (read: replyable) address.
1N/A*/
1N/A
1N/Abool
1N/Aemptyaddr(a)
1N/A register ADDRESS *a;
1N/A{
1N/A return a->q_paddr == NULL || strcmp(a->q_paddr, "<>") == 0 ||
1N/A a->q_user == NULL || strcmp(a->q_user, "<>") == 0;
1N/A}
1N/A/*
1N/A** REMOTENAME -- return the name relative to the current mailer
1N/A**
1N/A** Parameters:
1N/A** name -- the name to translate.
1N/A** m -- the mailer that we want to do rewriting relative to.
1N/A** flags -- fine tune operations.
1N/A** pstat -- pointer to status word.
1N/A** e -- the current envelope.
1N/A**
1N/A** Returns:
1N/A** the text string representing this address relative to
1N/A** the receiving mailer.
1N/A**
1N/A** Side Effects:
1N/A** none.
1N/A**
1N/A** Warnings:
1N/A** The text string returned is tucked away locally;
1N/A** copy it if you intend to save it.
1N/A*/
1N/A
1N/Achar *
1N/Aremotename(name, m, flags, pstat, e)
1N/A char *name;
1N/A struct mailer *m;
1N/A int flags;
1N/A int *pstat;
1N/A register ENVELOPE *e;
1N/A{
1N/A register char **pvp;
1N/A char *SM_NONVOLATILE fancy;
1N/A char *oldg;
1N/A int rwset;
1N/A static char buf[MAXNAME + 1];
1N/A char lbuf[MAXNAME + 1];
1N/A char pvpbuf[PSBUFSIZE];
1N/A char addrtype[4];
1N/A
1N/A if (tTd(12, 1))
1N/A {
1N/A sm_dprintf("remotename(");
1N/A xputs(sm_debug_file(), name);
1N/A sm_dprintf(")\n");
1N/A }
1N/A
1N/A /* don't do anything if we are tagging it as special */
1N/A if (bitset(RF_SENDERADDR, flags))
1N/A {
1N/A rwset = bitset(RF_HEADERADDR, flags) ? m->m_sh_rwset
1N/A : m->m_se_rwset;
1N/A addrtype[2] = 's';
1N/A }
1N/A else
1N/A {
1N/A rwset = bitset(RF_HEADERADDR, flags) ? m->m_rh_rwset
1N/A : m->m_re_rwset;
1N/A addrtype[2] = 'r';
1N/A }
1N/A if (rwset < 0)
1N/A return name;
1N/A addrtype[1] = ' ';
1N/A addrtype[3] = '\0';
1N/A addrtype[0] = bitset(RF_HEADERADDR, flags) ? 'h' : 'e';
1N/A macdefine(&e->e_macro, A_TEMP, macid("{addr_type}"), addrtype);
1N/A
1N/A /*
1N/A ** Do a heuristic crack of this name to extract any comment info.
1N/A ** This will leave the name as a comment and a $g macro.
1N/A */
1N/A
1N/A if (bitset(RF_CANONICAL, flags) || bitnset(M_NOCOMMENT, m->m_flags))
1N/A fancy = "\201g";
1N/A else
1N/A fancy = crackaddr(name, e);
1N/A
1N/A /*
1N/A ** Turn the name into canonical form.
1N/A ** Normally this will be RFC 822 style, i.e., "user@domain".
1N/A ** If this only resolves to "user", and the "C" flag is
1N/A ** specified in the sending mailer, then the sender's
1N/A ** domain will be appended.
1N/A */
1N/A
1N/A pvp = prescan(name, '\0', pvpbuf, sizeof(pvpbuf), NULL, NULL, false);
1N/A if (pvp == NULL)
1N/A return name;
1N/A if (REWRITE(pvp, 3, e) == EX_TEMPFAIL)
1N/A *pstat = EX_TEMPFAIL;
1N/A if (bitset(RF_ADDDOMAIN, flags) && e->e_fromdomain != NULL)
1N/A {
1N/A /* append from domain to this address */
1N/A register char **pxp = pvp;
1N/A int l = MAXATOM; /* size of buffer for pvp */
1N/A
1N/A /* see if there is an "@domain" in the current name */
1N/A while (*pxp != NULL && strcmp(*pxp, "@") != 0)
1N/A {
1N/A pxp++;
1N/A --l;
1N/A }
1N/A if (*pxp == NULL)
1N/A {
1N/A /* no.... append the "@domain" from the sender */
1N/A register char **qxq = e->e_fromdomain;
1N/A
1N/A while ((*pxp++ = *qxq++) != NULL)
1N/A {
1N/A if (--l <= 0)
1N/A {
1N/A *--pxp = NULL;
1N/A usrerr("553 5.1.0 remotename: too many tokens");
1N/A *pstat = EX_UNAVAILABLE;
1N/A break;
1N/A }
1N/A }
1N/A if (REWRITE(pvp, 3, e) == EX_TEMPFAIL)
1N/A *pstat = EX_TEMPFAIL;
1N/A }
1N/A }
1N/A
1N/A /*
1N/A ** Do more specific rewriting.
1N/A ** Rewrite using ruleset 1 or 2 depending on whether this is
1N/A ** a sender address or not.
1N/A ** Then run it through any receiving-mailer-specific rulesets.
1N/A */
1N/A
1N/A if (bitset(RF_SENDERADDR, flags))
1N/A {
1N/A if (REWRITE(pvp, 1, e) == EX_TEMPFAIL)
1N/A *pstat = EX_TEMPFAIL;
1N/A }
1N/A else
1N/A {
1N/A if (REWRITE(pvp, 2, e) == EX_TEMPFAIL)
1N/A *pstat = EX_TEMPFAIL;
1N/A }
1N/A if (rwset > 0)
1N/A {
1N/A if (REWRITE(pvp, rwset, e) == EX_TEMPFAIL)
1N/A *pstat = EX_TEMPFAIL;
1N/A }
1N/A
1N/A /*
1N/A ** Do any final sanitation the address may require.
1N/A ** This will normally be used to turn internal forms
1N/A ** (e.g., user@host.LOCAL) into external form. This
1N/A ** may be used as a default to the above rules.
1N/A */
1N/A
1N/A if (REWRITE(pvp, 4, e) == EX_TEMPFAIL)
1N/A *pstat = EX_TEMPFAIL;
1N/A
1N/A /*
1N/A ** Now restore the comment information we had at the beginning.
1N/A */
1N/A
1N/A cataddr(pvp, NULL, lbuf, sizeof(lbuf), '\0', false);
1N/A oldg = macget(&e->e_macro, 'g');
1N/A macset(&e->e_macro, 'g', lbuf);
1N/A
1N/A SM_TRY
1N/A /* need to make sure route-addrs have <angle brackets> */
1N/A if (bitset(RF_CANONICAL, flags) && lbuf[0] == '@')
1N/A expand("<\201g>", buf, sizeof(buf), e);
1N/A else
1N/A expand(fancy, buf, sizeof(buf), e);
1N/A SM_FINALLY
1N/A macset(&e->e_macro, 'g', oldg);
1N/A SM_END_TRY
1N/A
1N/A if (tTd(12, 1))
1N/A {
1N/A sm_dprintf("remotename => `");
1N/A xputs(sm_debug_file(), buf);
1N/A sm_dprintf("'\n");
1N/A }
1N/A return buf;
1N/A}
1N/A/*
1N/A** MAPLOCALUSER -- run local username through ruleset 5 for final redirection
1N/A**
1N/A** Parameters:
1N/A** a -- the address to map (but just the user name part).
1N/A** sendq -- the sendq in which to install any replacement
1N/A** addresses.
1N/A** aliaslevel -- the alias nesting depth.
1N/A** e -- the envelope.
1N/A**
1N/A** Returns:
1N/A** none.
1N/A*/
1N/A
1N/A#define Q_COPYFLAGS (QPRIMARY|QBOGUSSHELL|QUNSAFEADDR|\
1N/A Q_PINGFLAGS|QHASNOTIFY|\
1N/A QRELAYED|QEXPANDED|QDELIVERED|QDELAYED|\
1N/A QBYTRACE|QBYNDELAY|QBYNRELAY)
1N/A
1N/Avoid
1N/Amaplocaluser(a, sendq, aliaslevel, e)
1N/A register ADDRESS *a;
1N/A ADDRESS **sendq;
1N/A int aliaslevel;
1N/A ENVELOPE *e;
1N/A{
1N/A register char **pvp;
1N/A register ADDRESS *SM_NONVOLATILE a1 = NULL;
1N/A char pvpbuf[PSBUFSIZE];
1N/A
1N/A if (tTd(29, 1))
1N/A {
1N/A sm_dprintf("maplocaluser: ");
1N/A printaddr(sm_debug_file(), a, false);
1N/A }
1N/A pvp = prescan(a->q_user, '\0', pvpbuf, sizeof(pvpbuf), NULL, NULL,
1N/A false);
1N/A if (pvp == NULL)
1N/A {
1N/A if (tTd(29, 9))
1N/A sm_dprintf("maplocaluser: cannot prescan %s\n",
1N/A a->q_user);
1N/A return;
1N/A }
1N/A
1N/A macdefine(&e->e_macro, A_PERM, 'h', a->q_host);
1N/A macdefine(&e->e_macro, A_PERM, 'u', a->q_user);
1N/A macdefine(&e->e_macro, A_PERM, 'z', a->q_home);
1N/A
1N/A macdefine(&e->e_macro, A_PERM, macid("{addr_type}"), "e r");
1N/A if (REWRITE(pvp, 5, e) == EX_TEMPFAIL)
1N/A {
1N/A if (tTd(29, 9))
1N/A sm_dprintf("maplocaluser: rewrite tempfail\n");
1N/A a->q_state = QS_QUEUEUP;
1N/A a->q_status = "4.4.3";
1N/A return;
1N/A }
1N/A if (pvp[0] == NULL || (pvp[0][0] & 0377) != CANONNET)
1N/A {
1N/A if (tTd(29, 9))
1N/A sm_dprintf("maplocaluser: doesn't resolve\n");
1N/A return;
1N/A }
1N/A
1N/A SM_TRY
1N/A a1 = buildaddr(pvp, NULL, 0, e);
1N/A SM_EXCEPT(exc, "E:mta.quickabort")
1N/A
1N/A /*
1N/A ** mark address as bad, S5 returned an error
1N/A ** and we gave that back to the SMTP client.
1N/A */
1N/A
1N/A a->q_state = QS_DONTSEND;
1N/A sm_exc_raisenew_x(&EtypeQuickAbort, 2);
1N/A SM_END_TRY
1N/A
1N/A /* if non-null, mailer destination specified -- has it changed? */
1N/A if (a1 == NULL || sameaddr(a, a1))
1N/A {
1N/A if (tTd(29, 9))
1N/A sm_dprintf("maplocaluser: address unchanged\n");
1N/A return;
1N/A }
1N/A
1N/A /* make new address take on flags and print attributes of old */
1N/A a1->q_flags &= ~Q_COPYFLAGS;
1N/A a1->q_flags |= a->q_flags & Q_COPYFLAGS;
1N/A a1->q_paddr = sm_rpool_strdup_x(e->e_rpool, a->q_paddr);
1N/A a1->q_finalrcpt = a->q_finalrcpt;
1N/A a1->q_orcpt = a->q_orcpt;
1N/A
1N/A /* mark old address as dead; insert new address */
1N/A a->q_state = QS_REPLACED;
1N/A if (tTd(29, 5))
1N/A {
1N/A sm_dprintf("maplocaluser: QS_REPLACED ");
1N/A printaddr(sm_debug_file(), a, false);
1N/A }
1N/A a1->q_alias = a;
1N/A allocaddr(a1, RF_COPYALL, sm_rpool_strdup_x(e->e_rpool, a->q_paddr), e);
1N/A (void) recipient(a1, sendq, aliaslevel, e);
1N/A}
1N/A/*
1N/A** DEQUOTE_INIT -- initialize dequote map
1N/A**
1N/A** Parameters:
1N/A** map -- the internal map structure.
1N/A** args -- arguments.
1N/A**
1N/A** Returns:
1N/A** true.
1N/A*/
1N/A
1N/Abool
1N/Adequote_init(map, args)
1N/A MAP *map;
1N/A char *args;
1N/A{
1N/A register char *p = args;
1N/A
1N/A /* there is no check whether there is really an argument */
1N/A map->map_mflags |= MF_KEEPQUOTES;
1N/A for (;;)
1N/A {
1N/A while (isascii(*p) && isspace(*p))
1N/A p++;
1N/A if (*p != '-')
1N/A break;
1N/A switch (*++p)
1N/A {
1N/A case 'a':
1N/A map->map_app = ++p;
1N/A break;
1N/A
1N/A case 'D':
1N/A map->map_mflags |= MF_DEFER;
1N/A break;
1N/A
1N/A case 'S':
1N/A case 's':
1N/A map->map_spacesub = *++p;
1N/A break;
1N/A }
1N/A while (*p != '\0' && !(isascii(*p) && isspace(*p)))
1N/A p++;
1N/A if (*p != '\0')
1N/A *p = '\0';
1N/A }
1N/A if (map->map_app != NULL)
1N/A map->map_app = newstr(map->map_app);
1N/A
1N/A return true;
1N/A}
1N/A/*
1N/A** DEQUOTE_MAP -- unquote an address
1N/A**
1N/A** Parameters:
1N/A** map -- the internal map structure (ignored).
1N/A** name -- the name to dequote.
1N/A** av -- arguments (ignored).
1N/A** statp -- pointer to status out-parameter.
1N/A**
1N/A** Returns:
1N/A** NULL -- if there were no quotes, or if the resulting
1N/A** unquoted buffer would not be acceptable to prescan.
1N/A** else -- The dequoted buffer.
1N/A*/
1N/A
1N/A/* ARGSUSED2 */
1N/Achar *
1N/Adequote_map(map, name, av, statp)
1N/A MAP *map;
1N/A char *name;
1N/A char **av;
1N/A int *statp;
1N/A{
1N/A register char *p;
1N/A register char *q;
1N/A register char c;
1N/A int anglecnt = 0;
1N/A int cmntcnt = 0;
1N/A int quotecnt = 0;
1N/A int spacecnt = 0;
1N/A bool quotemode = false;
1N/A bool bslashmode = false;
1N/A char spacesub = map->map_spacesub;
1N/A
1N/A for (p = q = name; (c = *p++) != '\0'; )
1N/A {
1N/A if (bslashmode)
1N/A {
1N/A bslashmode = false;
1N/A *q++ = c;
1N/A continue;
1N/A }
1N/A
1N/A if (c == ' ' && spacesub != '\0')
1N/A c = spacesub;
1N/A
1N/A switch (c)
1N/A {
1N/A case '\\':
1N/A bslashmode = true;
1N/A break;
1N/A
1N/A case '(':
1N/A cmntcnt++;
1N/A break;
1N/A
1N/A case ')':
1N/A if (cmntcnt-- <= 0)
1N/A return NULL;
1N/A break;
1N/A
1N/A case ' ':
1N/A case '\t':
1N/A spacecnt++;
1N/A break;
1N/A }
1N/A
1N/A if (cmntcnt > 0)
1N/A {
1N/A *q++ = c;
1N/A continue;
1N/A }
1N/A
1N/A switch (c)
1N/A {
1N/A case '"':
1N/A quotemode = !quotemode;
1N/A quotecnt++;
1N/A continue;
1N/A
1N/A case '<':
1N/A anglecnt++;
1N/A break;
1N/A
1N/A case '>':
1N/A if (anglecnt-- <= 0)
1N/A return NULL;
1N/A break;
1N/A }
1N/A *q++ = c;
1N/A }
1N/A
1N/A if (anglecnt != 0 || cmntcnt != 0 || bslashmode ||
1N/A quotemode || quotecnt <= 0 || spacecnt != 0)
1N/A return NULL;
1N/A *q++ = '\0';
1N/A return map_rewrite(map, name, strlen(name), NULL);
1N/A}
1N/A/*
1N/A** RSCHECK -- check string(s) for validity using rewriting sets
1N/A**
1N/A** Parameters:
1N/A** rwset -- the rewriting set to use.
1N/A** p1 -- the first string to check.
1N/A** p2 -- the second string to check -- may be null.
1N/A** e -- the current envelope.
1N/A** flags -- control some behavior, see RSF_ in sendmail.h
1N/A** logl -- logging level.
1N/A** host -- NULL or relay host.
1N/A** logid -- id for sm_syslog.
1N/A** addr -- if not NULL and ruleset returns $#error:
1N/A** store mailer triple here.
1N/A**
1N/A** Returns:
1N/A** EX_OK -- if the rwset doesn't resolve to $#error
1N/A** else -- the failure status (message printed)
1N/A*/
1N/A
1N/Aint
1N/Arscheck(rwset, p1, p2, e, flags, logl, host, logid, addr)
1N/A char *rwset;
1N/A char *p1;
1N/A char *p2;
1N/A ENVELOPE *e;
1N/A int flags;
1N/A int logl;
1N/A char *host;
1N/A char *logid;
1N/A ADDRESS *addr;
1N/A{
1N/A char *volatile buf;
1N/A size_t bufsize;
1N/A int saveexitstat;
1N/A int volatile rstat = EX_OK;
1N/A char **pvp;
1N/A int rsno;
1N/A bool volatile discard = false;
1N/A bool saveQuickAbort = QuickAbort;
1N/A bool saveSuprErrs = SuprErrs;
1N/A bool quarantine = false;
1N/A char ubuf[BUFSIZ * 2];
1N/A char buf0[MAXLINE];
1N/A char pvpbuf[PSBUFSIZE];
1N/A extern char MsgBuf[];
1N/A
1N/A if (tTd(48, 2))
1N/A sm_dprintf("rscheck(%s, %s, %s)\n", rwset, p1,
1N/A p2 == NULL ? "(NULL)" : p2);
1N/A
1N/A rsno = strtorwset(rwset, NULL, ST_FIND);
1N/A if (rsno < 0)
1N/A return EX_OK;
1N/A
1N/A if (p2 != NULL)
1N/A {
1N/A bufsize = strlen(p1) + strlen(p2) + 2;
1N/A if (bufsize > sizeof(buf0))
1N/A buf = sm_malloc_x(bufsize);
1N/A else
1N/A {
1N/A buf = buf0;
1N/A bufsize = sizeof(buf0);
1N/A }
1N/A (void) sm_snprintf(buf, bufsize, "%s%c%s", p1, CONDELSE, p2);
1N/A }
1N/A else
1N/A {
1N/A bufsize = strlen(p1) + 1;
1N/A if (bufsize > sizeof(buf0))
1N/A buf = sm_malloc_x(bufsize);
1N/A else
1N/A {
1N/A buf = buf0;
1N/A bufsize = sizeof(buf0);
1N/A }
1N/A (void) sm_strlcpy(buf, p1, bufsize);
1N/A }
1N/A SM_TRY
1N/A {
1N/A SuprErrs = true;
1N/A QuickAbort = false;
1N/A pvp = prescan(buf, '\0', pvpbuf, sizeof(pvpbuf), NULL,
1N/A bitset(RSF_RMCOMM, flags) ?
1N/A IntTokenTab : TokTypeNoC,
1N/A bitset(RSF_RMCOMM, flags) ? false : true);
1N/A SuprErrs = saveSuprErrs;
1N/A if (pvp == NULL)
1N/A {
1N/A if (tTd(48, 2))
1N/A sm_dprintf("rscheck: cannot prescan input\n");
1N/A /*
1N/A syserr("rscheck: cannot prescan input: \"%s\"",
1N/A shortenstring(buf, MAXSHORTSTR));
1N/A rstat = EX_DATAERR;
1N/A */
1N/A goto finis;
1N/A }
1N/A if (bitset(RSF_UNSTRUCTURED, flags))
1N/A SuprErrs = true;
1N/A (void) REWRITE(pvp, rsno, e);
1N/A if (bitset(RSF_UNSTRUCTURED, flags))
1N/A SuprErrs = saveSuprErrs;
1N/A if (pvp[0] == NULL || (pvp[0][0] & 0377) != CANONNET ||
1N/A pvp[1] == NULL || (strcmp(pvp[1], "error") != 0 &&
1N/A strcmp(pvp[1], "discard") != 0))
1N/A {
1N/A goto finis;
1N/A }
1N/A
1N/A if (strcmp(pvp[1], "discard") == 0)
1N/A {
1N/A if (tTd(48, 2))
1N/A sm_dprintf("rscheck: discard mailer selected\n");
1N/A e->e_flags |= EF_DISCARD;
1N/A discard = true;
1N/A }
1N/A else if (strcmp(pvp[1], "error") == 0 &&
1N/A pvp[2] != NULL && (pvp[2][0] & 0377) == CANONHOST &&
1N/A pvp[3] != NULL && strcmp(pvp[3], "quarantine") == 0)
1N/A {
1N/A if (pvp[4] == NULL ||
1N/A (pvp[4][0] & 0377) != CANONUSER ||
1N/A pvp[5] == NULL)
1N/A e->e_quarmsg = sm_rpool_strdup_x(e->e_rpool,
1N/A rwset);
1N/A else
1N/A {
1N/A cataddr(&(pvp[5]), NULL, ubuf,
1N/A sizeof(ubuf), ' ', true);
1N/A e->e_quarmsg = sm_rpool_strdup_x(e->e_rpool,
1N/A ubuf);
1N/A }
1N/A macdefine(&e->e_macro, A_PERM,
1N/A macid("{quarantine}"), e->e_quarmsg);
1N/A quarantine = true;
1N/A }
1N/A else
1N/A {
1N/A auto ADDRESS a1;
1N/A int savelogusrerrs = LogUsrErrs;
1N/A static bool logged = false;
1N/A
1N/A /* got an error -- process it */
1N/A saveexitstat = ExitStat;
1N/A LogUsrErrs = false;
1N/A (void) buildaddr(pvp, &a1, 0, e);
1N/A if (addr != NULL)
1N/A {
1N/A addr->q_mailer = a1.q_mailer;
1N/A addr->q_user = a1.q_user;
1N/A addr->q_host = a1.q_host;
1N/A }
1N/A LogUsrErrs = savelogusrerrs;
1N/A rstat = ExitStat;
1N/A ExitStat = saveexitstat;
1N/A if (!logged)
1N/A {
1N/A if (bitset(RSF_COUNT, flags))
1N/A markstats(e, &a1, STATS_REJECT);
1N/A logged = true;
1N/A }
1N/A }
1N/A
1N/A if (LogLevel > logl)
1N/A {
1N/A char *relay;
1N/A char *p;
1N/A char lbuf[MAXLINE];
1N/A
1N/A p = lbuf;
1N/A if (p2 != NULL)
1N/A {
1N/A (void) sm_snprintf(p, SPACELEFT(lbuf, p),
1N/A ", arg2=%s",
1N/A p2);
1N/A p += strlen(p);
1N/A }
1N/A
1N/A if (host != NULL)
1N/A relay = host;
1N/A else
1N/A relay = macvalue('_', e);
1N/A if (relay != NULL)
1N/A {
1N/A (void) sm_snprintf(p, SPACELEFT(lbuf, p),
1N/A ", relay=%s", relay);
1N/A p += strlen(p);
1N/A }
1N/A *p = '\0';
1N/A if (discard)
1N/A sm_syslog(LOG_NOTICE, logid,
1N/A "ruleset=%s, arg1=%s%s, discard",
1N/A rwset, p1, lbuf);
1N/A else if (quarantine)
1N/A sm_syslog(LOG_NOTICE, logid,
1N/A "ruleset=%s, arg1=%s%s, quarantine=%s",
1N/A rwset, p1, lbuf, ubuf);
1N/A else
1N/A sm_syslog(LOG_NOTICE, logid,
1N/A "ruleset=%s, arg1=%s%s, reject=%s",
1N/A rwset, p1, lbuf, MsgBuf);
1N/A }
1N/A
1N/A finis: ;
1N/A }
1N/A SM_FINALLY
1N/A {
1N/A /* clean up */
1N/A if (buf != buf0)
1N/A sm_free(buf);
1N/A QuickAbort = saveQuickAbort;
1N/A }
1N/A SM_END_TRY
1N/A
1N/A setstat(rstat);
1N/A
1N/A /* rulesets don't set errno */
1N/A errno = 0;
1N/A if (rstat != EX_OK && QuickAbort)
1N/A sm_exc_raisenew_x(&EtypeQuickAbort, 2);
1N/A return rstat;
1N/A}
1N/A/*
1N/A** RSCAP -- call rewriting set to return capabilities
1N/A**
1N/A** Parameters:
1N/A** rwset -- the rewriting set to use.
1N/A** p1 -- the first string to check.
1N/A** p2 -- the second string to check -- may be null.
1N/A** e -- the current envelope.
1N/A** pvp -- pointer to token vector.
1N/A** pvpbuf -- buffer space.
1N/A** size -- size of buffer space.
1N/A**
1N/A** Returns:
1N/A** EX_UNAVAILABLE -- ruleset doesn't exist.
1N/A** EX_DATAERR -- prescan() failed.
1N/A** EX_OK -- rewrite() was successful.
1N/A** else -- return status from rewrite().
1N/A*/
1N/A
1N/Aint
1N/Arscap(rwset, p1, p2, e, pvp, pvpbuf, size)
1N/A char *rwset;
1N/A char *p1;
1N/A char *p2;
1N/A ENVELOPE *e;
1N/A char ***pvp;
1N/A char *pvpbuf;
1N/A int size;
1N/A{
1N/A char *volatile buf;
1N/A size_t bufsize;
1N/A int volatile rstat = EX_OK;
1N/A int rsno;
1N/A bool saveQuickAbort = QuickAbort;
1N/A bool saveSuprErrs = SuprErrs;
1N/A char buf0[MAXLINE];
1N/A extern char MsgBuf[];
1N/A
1N/A if (tTd(48, 2))
1N/A sm_dprintf("rscap(%s, %s, %s)\n", rwset, p1,
1N/A p2 == NULL ? "(NULL)" : p2);
1N/A
1N/A SM_REQUIRE(pvp != NULL);
1N/A rsno = strtorwset(rwset, NULL, ST_FIND);
1N/A if (rsno < 0)
1N/A return EX_UNAVAILABLE;
1N/A
1N/A if (p2 != NULL)
1N/A {
1N/A bufsize = strlen(p1) + strlen(p2) + 2;
1N/A if (bufsize > sizeof(buf0))
1N/A buf = sm_malloc_x(bufsize);
1N/A else
1N/A {
1N/A buf = buf0;
1N/A bufsize = sizeof(buf0);
1N/A }
1N/A (void) sm_snprintf(buf, bufsize, "%s%c%s", p1, CONDELSE, p2);
1N/A }
1N/A else
1N/A {
1N/A bufsize = strlen(p1) + 1;
1N/A if (bufsize > sizeof(buf0))
1N/A buf = sm_malloc_x(bufsize);
1N/A else
1N/A {
1N/A buf = buf0;
1N/A bufsize = sizeof(buf0);
1N/A }
1N/A (void) sm_strlcpy(buf, p1, bufsize);
1N/A }
1N/A SM_TRY
1N/A {
1N/A SuprErrs = true;
1N/A QuickAbort = false;
1N/A *pvp = prescan(buf, '\0', pvpbuf, size, NULL, IntTokenTab,
1N/A false);
1N/A if (*pvp != NULL)
1N/A rstat = rewrite(*pvp, rsno, 0, e, size);
1N/A else
1N/A {
1N/A if (tTd(48, 2))
1N/A sm_dprintf("rscap: cannot prescan input\n");
1N/A rstat = EX_DATAERR;
1N/A }
1N/A }
1N/A SM_FINALLY
1N/A {
1N/A /* clean up */
1N/A if (buf != buf0)
1N/A sm_free(buf);
1N/A SuprErrs = saveSuprErrs;
1N/A QuickAbort = saveQuickAbort;
1N/A
1N/A /* prevent information leak, this may contain rewrite error */
1N/A MsgBuf[0] = '\0';
1N/A }
1N/A SM_END_TRY
1N/A return rstat;
1N/A}