122976Smiriam /* 234921Sbostic * Copyright (c) 1983 Eric P. Allman 363589Sbostic * Copyright (c) 1988, 1993 463589Sbostic * The Regents of the University of California. All rights reserved. 533730Sbostic * 642828Sbostic * %sccs.include.redist.c% 733730Sbostic */ 822976Smiriam 922976Smiriam #ifndef lint 10*64928Seric static char sccsid[] = "@(#)parseaddr.c 8.18 (Berkeley) 11/20/93"; 1133730Sbostic #endif /* not lint */ 1222976Smiriam 1356678Seric # include "sendmail.h" 14297Seric 15297Seric /* 169888Seric ** PARSEADDR -- Parse an address 17297Seric ** 18297Seric ** Parses an address and breaks it up into three parts: a 19297Seric ** net to transmit the message on, the host to transmit it 20297Seric ** to, and a user on that host. These are loaded into an 212973Seric ** ADDRESS header with the values squirreled away if necessary. 22297Seric ** The "user" part may not be a real user; the process may 23297Seric ** just reoccur on that machine. For example, on a machine 24297Seric ** with an arpanet connection, the address 25297Seric ** csvax.bill@berkeley 26297Seric ** will break up to a "user" of 'csvax.bill' and a host 27297Seric ** of 'berkeley' -- to be transmitted over the arpanet. 28297Seric ** 29297Seric ** Parameters: 30297Seric ** addr -- the address to parse. 31297Seric ** a -- a pointer to the address descriptor buffer. 32297Seric ** If NULL, a header will be created. 3364284Seric ** flags -- describe detail for parsing. See RF_ definitions 3464284Seric ** in sendmail.h. 3511445Seric ** delim -- the character to terminate the address, passed 3611445Seric ** to prescan. 3758333Seric ** delimptr -- if non-NULL, set to the location of the 3858333Seric ** delim character that was found. 3956678Seric ** e -- the envelope that will contain this address. 40297Seric ** 41297Seric ** Returns: 42297Seric ** A pointer to the address descriptor header (`a' if 43297Seric ** `a' is non-NULL). 44297Seric ** NULL on error. 45297Seric ** 46297Seric ** Side Effects: 47297Seric ** none 48297Seric */ 49297Seric 509374Seric /* following delimiters are inherent to the internal algorithms */ 5159278Seric # define DELIMCHARS "()<>,;\r\n" /* default word delimiters */ 522091Seric 532973Seric ADDRESS * 5464284Seric parseaddr(addr, a, flags, delim, delimptr, e) 55297Seric char *addr; 562973Seric register ADDRESS *a; 5764284Seric int flags; 5859700Seric int delim; 5958333Seric char **delimptr; 6056678Seric register ENVELOPE *e; 61297Seric { 623149Seric register char **pvp; 6358333Seric auto char *delimptrbuf; 6459084Seric bool queueup; 6516914Seric char pvpbuf[PSBUFSIZE]; 6656678Seric extern ADDRESS *buildaddr(); 6757388Seric extern bool invalidaddr(); 68297Seric 69297Seric /* 70297Seric ** Initialize and prescan address. 71297Seric */ 72297Seric 7356678Seric e->e_to = addr; 747675Seric if (tTd(20, 1)) 759888Seric printf("\n--parseaddr(%s)\n", addr); 763188Seric 7758333Seric if (delimptr == NULL) 7858333Seric delimptr = &delimptrbuf; 7958333Seric 8058333Seric pvp = prescan(addr, delim, pvpbuf, delimptr); 813149Seric if (pvp == NULL) 8256729Seric { 8356729Seric if (tTd(20, 1)) 8456729Seric printf("parseaddr-->NULL\n"); 85297Seric return (NULL); 8656729Seric } 87297Seric 8864726Seric if (invalidaddr(addr, delim == '\0' ? NULL : *delimptr)) 8964726Seric { 9064726Seric if (tTd(20, 1)) 9164726Seric printf("parseaddr-->bad address\n"); 9264726Seric return NULL; 9364726Seric } 9464726Seric 95297Seric /* 9664348Seric ** Save addr if we are going to have to. 9764348Seric ** 9864348Seric ** We have to do this early because there is a chance that 9964348Seric ** the map lookups in the rewriting rules could clobber 10064348Seric ** static memory somewhere. 10164348Seric */ 10264348Seric 10364348Seric if (bitset(RF_COPYPADDR, flags) && addr != NULL) 10464348Seric { 10564348Seric char savec = **delimptr; 10664348Seric 10764348Seric if (savec != '\0') 10864348Seric **delimptr = '\0'; 10964348Seric addr = newstr(addr); 11064348Seric if (savec != '\0') 11164348Seric **delimptr = savec; 11264348Seric } 11364348Seric 11464348Seric /* 1153149Seric ** Apply rewriting rules. 1167889Seric ** Ruleset 0 does basic parsing. It must resolve. 117297Seric */ 118297Seric 11959084Seric queueup = FALSE; 12059084Seric if (rewrite(pvp, 3, e) == EX_TEMPFAIL) 12159084Seric queueup = TRUE; 12259084Seric if (rewrite(pvp, 0, e) == EX_TEMPFAIL) 12359084Seric queueup = TRUE; 124297Seric 125297Seric 126297Seric /* 1273149Seric ** Build canonical address from pvp. 128297Seric */ 129297Seric 13064284Seric a = buildaddr(pvp, a, flags, e); 131297Seric 132297Seric /* 1333149Seric ** Make local copies of the host & user and then 1343149Seric ** transport them out. 135297Seric */ 136297Seric 13764348Seric allocaddr(a, flags, addr); 13864306Seric if (bitset(QBADADDR, a->q_flags)) 13964306Seric return a; 14056678Seric 14156678Seric /* 14259084Seric ** If there was a parsing failure, mark it for queueing. 14359084Seric */ 14459084Seric 14559084Seric if (queueup) 14659595Seric { 14759734Seric char *msg = "Transient parse error -- message queued for future delivery"; 14859734Seric 14959595Seric if (tTd(20, 1)) 15059595Seric printf("parseaddr: queuing message\n"); 15159734Seric message(msg); 15259734Seric if (e->e_message == NULL) 15360009Seric e->e_message = newstr(msg); 15459084Seric a->q_flags |= QQUEUEUP; 15559595Seric } 15659084Seric 15759084Seric /* 15856678Seric ** Compute return value. 15956678Seric */ 16056678Seric 16156678Seric if (tTd(20, 1)) 1628078Seric { 16356678Seric printf("parseaddr-->"); 16456678Seric printaddr(a, FALSE); 16556678Seric } 16656678Seric 16756678Seric return (a); 16856678Seric } 16956678Seric /* 17057388Seric ** INVALIDADDR -- check for address containing meta-characters 17157388Seric ** 17257388Seric ** Parameters: 17357388Seric ** addr -- the address to check. 17457388Seric ** 17557388Seric ** Returns: 17657388Seric ** TRUE -- if the address has any "wierd" characters 17757388Seric ** FALSE -- otherwise. 17857388Seric */ 17957388Seric 18057388Seric bool 18164726Seric invalidaddr(addr, delimptr) 18257388Seric register char *addr; 18364726Seric char *delimptr; 18457388Seric { 18564726Seric char savedelim; 18664726Seric 18764726Seric if (delimptr != NULL) 18864764Seric { 18964726Seric savedelim = *delimptr; 19064764Seric if (savedelim != '\0') 19164764Seric *delimptr = '\0'; 19264764Seric } 19364726Seric #if 0 19464726Seric /* for testing.... */ 19564726Seric if (strcmp(addr, "INvalidADDR") == 0) 19657388Seric { 19764726Seric usrerr("553 INvalid ADDRess"); 19864764Seric goto addrfailure; 19957388Seric } 20064726Seric #endif 20164726Seric for (; *addr != '\0'; addr++) 20264726Seric { 20364726Seric if ((*addr & 0340) == 0200) 20464726Seric break; 20564726Seric } 20664726Seric if (*addr == '\0') 20764764Seric { 20864764Seric if (savedelim != '\0' && delimptr != NULL) 20964764Seric *delimptr = savedelim; 21064726Seric return FALSE; 21164764Seric } 21264726Seric setstat(EX_USAGE); 21364726Seric usrerr("553 Address contained invalid control characters"); 21464764Seric addrfailure: 21564764Seric if (savedelim != '\0' && delimptr != NULL) 21664764Seric *delimptr = savedelim; 21764726Seric return TRUE; 21857388Seric } 21957388Seric /* 22056678Seric ** ALLOCADDR -- do local allocations of address on demand. 22156678Seric ** 22256678Seric ** Also lowercases the host name if requested. 22356678Seric ** 22456678Seric ** Parameters: 22556678Seric ** a -- the address to reallocate. 22664284Seric ** flags -- the copy flag (see RF_ definitions in sendmail.h 22764284Seric ** for a description). 22856678Seric ** paddr -- the printname of the address. 22956678Seric ** 23056678Seric ** Returns: 23156678Seric ** none. 23256678Seric ** 23356678Seric ** Side Effects: 23456678Seric ** Copies portions of a into local buffers as requested. 23556678Seric */ 23656678Seric 23764348Seric allocaddr(a, flags, paddr) 23856678Seric register ADDRESS *a; 23964284Seric int flags; 24056678Seric char *paddr; 24156678Seric { 24258673Seric if (tTd(24, 4)) 24364284Seric printf("allocaddr(flags=%o, paddr=%s)\n", flags, paddr); 24458673Seric 24564348Seric a->q_paddr = paddr; 2468078Seric 24724944Seric if (a->q_user == NULL) 24824944Seric a->q_user = ""; 24924944Seric if (a->q_host == NULL) 25024944Seric a->q_host = ""; 25124944Seric 25264284Seric if (bitset(RF_COPYPARSE, flags)) 253297Seric { 25424944Seric a->q_host = newstr(a->q_host); 2553149Seric if (a->q_user != a->q_paddr) 2563149Seric a->q_user = newstr(a->q_user); 257297Seric } 258297Seric 25956678Seric if (a->q_paddr == NULL) 26056678Seric a->q_paddr = a->q_user; 261297Seric } 262297Seric /* 263297Seric ** PRESCAN -- Prescan name and make it canonical 264297Seric ** 2659374Seric ** Scans a name and turns it into a set of tokens. This process 2669374Seric ** deletes blanks and comments (in parentheses). 267297Seric ** 268297Seric ** This routine knows about quoted strings and angle brackets. 269297Seric ** 270297Seric ** There are certain subtleties to this routine. The one that 271297Seric ** comes to mind now is that backslashes on the ends of names 272297Seric ** are silently stripped off; this is intentional. The problem 273297Seric ** is that some versions of sndmsg (like at LBL) set the kill 274297Seric ** character to something other than @ when reading addresses; 275297Seric ** so people type "csvax.eric\@berkeley" -- which screws up the 276297Seric ** berknet mailer. 277297Seric ** 278297Seric ** Parameters: 279297Seric ** addr -- the name to chomp. 280297Seric ** delim -- the delimiter for the address, normally 281297Seric ** '\0' or ','; \0 is accepted in any case. 28215284Seric ** If '\t' then we are reading the .cf file. 28316914Seric ** pvpbuf -- place to put the saved text -- note that 28416914Seric ** the pointers are static. 28558333Seric ** delimptr -- if non-NULL, set to the location of the 28658333Seric ** terminating delimiter. 287297Seric ** 288297Seric ** Returns: 2893149Seric ** A pointer to a vector of tokens. 290297Seric ** NULL on error. 291297Seric */ 292297Seric 2938078Seric /* states and character types */ 2948078Seric # define OPR 0 /* operator */ 2958078Seric # define ATM 1 /* atom */ 2968078Seric # define QST 2 /* in quoted string */ 2978078Seric # define SPC 3 /* chewing up spaces */ 2988078Seric # define ONE 4 /* pick up one character */ 2993149Seric 3008078Seric # define NSTATES 5 /* number of states */ 3018078Seric # define TYPE 017 /* mask to select state type */ 3028078Seric 3038078Seric /* meta bits for table */ 3048078Seric # define M 020 /* meta character; don't pass through */ 3058078Seric # define B 040 /* cause a break */ 3068078Seric # define MB M|B /* meta-break */ 3078078Seric 3088078Seric static short StateTab[NSTATES][NSTATES] = 3098078Seric { 3108087Seric /* oldst chtype> OPR ATM QST SPC ONE */ 3119051Seric /*OPR*/ OPR|B, ATM|B, QST|B, SPC|MB, ONE|B, 3129051Seric /*ATM*/ OPR|B, ATM, QST|B, SPC|MB, ONE|B, 3139051Seric /*QST*/ QST, QST, OPR, QST, QST, 3148078Seric /*SPC*/ OPR, ATM, QST, SPC|M, ONE, 3158078Seric /*ONE*/ OPR, OPR, OPR, OPR, OPR, 3168078Seric }; 3178078Seric 3188078Seric # define NOCHAR -1 /* signal nothing in lookahead token */ 3198078Seric 3203149Seric char ** 32158333Seric prescan(addr, delim, pvpbuf, delimptr) 322297Seric char *addr; 323297Seric char delim; 32416914Seric char pvpbuf[]; 32558333Seric char **delimptr; 326297Seric { 327297Seric register char *p; 3288078Seric register char *q; 3299346Seric register int c; 3303149Seric char **avp; 331297Seric bool bslashmode; 332297Seric int cmntcnt; 3338423Seric int anglecnt; 3343149Seric char *tok; 3358078Seric int state; 3368078Seric int newstate; 33759747Seric char *saveto = CurEnv->e_to; 3388078Seric static char *av[MAXATOM+1]; 33956678Seric extern int errno; 340297Seric 34115253Seric /* make sure error messages don't have garbage on them */ 34215253Seric errno = 0; 34315253Seric 34416914Seric q = pvpbuf; 3453149Seric bslashmode = FALSE; 3467800Seric cmntcnt = 0; 3478423Seric anglecnt = 0; 3483149Seric avp = av; 34956678Seric state = ATM; 3508078Seric c = NOCHAR; 3518078Seric p = addr; 35259747Seric CurEnv->e_to = p; 35356764Seric if (tTd(22, 11)) 354297Seric { 3558078Seric printf("prescan: "); 3568078Seric xputs(p); 35723109Seric (void) putchar('\n'); 3588078Seric } 3598078Seric 3608078Seric do 3618078Seric { 3623149Seric /* read a token */ 3633149Seric tok = q; 3648078Seric for (;;) 365297Seric { 3668078Seric /* store away any old lookahead character */ 36759277Seric if (c != NOCHAR && !bslashmode) 3688078Seric { 36915284Seric /* see if there is room */ 37016914Seric if (q >= &pvpbuf[PSBUFSIZE - 5]) 3718078Seric { 37258151Seric usrerr("553 Address too long"); 37358333Seric if (delimptr != NULL) 37458333Seric *delimptr = p; 37559747Seric CurEnv->e_to = saveto; 3768078Seric return (NULL); 3778078Seric } 37815284Seric 37915284Seric /* squirrel it away */ 3808078Seric *q++ = c; 3818078Seric } 3828078Seric 3838078Seric /* read a new input character */ 3848078Seric c = *p++; 38557631Seric if (c == '\0') 38656764Seric { 38756764Seric /* diagnose and patch up bad syntax */ 38856764Seric if (state == QST) 38956764Seric { 39064767Seric usrerr("653 Unbalanced '\"'"); 39156764Seric c = '"'; 39256764Seric } 39356764Seric else if (cmntcnt > 0) 39456764Seric { 39564767Seric usrerr("653 Unbalanced '('"); 39656764Seric c = ')'; 39756764Seric } 39856764Seric else if (anglecnt > 0) 39956764Seric { 40056764Seric c = '>'; 40164767Seric usrerr("653 Unbalanced '<'"); 40256764Seric } 40356764Seric else 40456764Seric break; 40515284Seric 40656764Seric p--; 40756764Seric } 40857631Seric else if (c == delim && anglecnt <= 0 && 40957631Seric cmntcnt <= 0 && state != QST) 41057631Seric break; 41156764Seric 4128078Seric if (tTd(22, 101)) 4138078Seric printf("c=%c, s=%d; ", c, state); 4148078Seric 4153149Seric /* chew up special characters */ 4163149Seric *q = '\0'; 4173149Seric if (bslashmode) 4183149Seric { 41959105Seric bslashmode = FALSE; 42059105Seric 42124944Seric /* kludge \! for naive users */ 42258061Seric if (cmntcnt > 0) 42359105Seric { 42458061Seric c = NOCHAR; 42559105Seric continue; 42659105Seric } 42759105Seric else if (c != '!' || state == QST) 42859105Seric { 42956678Seric *q++ = '\\'; 43059105Seric continue; 43159105Seric } 4323149Seric } 43356678Seric 43456678Seric if (c == '\\') 4353149Seric { 4363149Seric bslashmode = TRUE; 4373149Seric } 43856678Seric else if (state == QST) 4398514Seric { 4408514Seric /* do nothing, just avoid next clauses */ 4418514Seric } 4428078Seric else if (c == '(') 4434100Seric { 4448078Seric cmntcnt++; 4458078Seric c = NOCHAR; 4464100Seric } 4478078Seric else if (c == ')') 4483149Seric { 4498078Seric if (cmntcnt <= 0) 4503149Seric { 45164767Seric usrerr("653 Unbalanced ')'"); 45263844Seric c = NOCHAR; 4533149Seric } 4548078Seric else 4558078Seric cmntcnt--; 4568078Seric } 4578078Seric else if (cmntcnt > 0) 4588078Seric c = NOCHAR; 4598423Seric else if (c == '<') 4608423Seric anglecnt++; 4618423Seric else if (c == '>') 4628423Seric { 4638423Seric if (anglecnt <= 0) 4648423Seric { 46564767Seric usrerr("653 Unbalanced '>'"); 46663844Seric c = NOCHAR; 4678423Seric } 46863844Seric else 46963844Seric anglecnt--; 4708423Seric } 47158050Seric else if (delim == ' ' && isascii(c) && isspace(c)) 47211423Seric c = ' '; 4733149Seric 4748078Seric if (c == NOCHAR) 4758078Seric continue; 4763149Seric 4778078Seric /* see if this is end of input */ 47811405Seric if (c == delim && anglecnt <= 0 && state != QST) 4793149Seric break; 4803149Seric 4818078Seric newstate = StateTab[state][toktype(c)]; 4828078Seric if (tTd(22, 101)) 4838078Seric printf("ns=%02o\n", newstate); 4848078Seric state = newstate & TYPE; 4858078Seric if (bitset(M, newstate)) 4868078Seric c = NOCHAR; 4878078Seric if (bitset(B, newstate)) 4884228Seric break; 489297Seric } 4903149Seric 4913149Seric /* new token */ 4928078Seric if (tok != q) 4931378Seric { 4948078Seric *q++ = '\0'; 4958078Seric if (tTd(22, 36)) 496297Seric { 4978078Seric printf("tok="); 4988078Seric xputs(tok); 49923109Seric (void) putchar('\n'); 500297Seric } 5018078Seric if (avp >= &av[MAXATOM]) 502297Seric { 50358151Seric syserr("553 prescan: too many tokens"); 50458333Seric if (delimptr != NULL) 50558333Seric *delimptr = p; 50659747Seric CurEnv->e_to = saveto; 5078078Seric return (NULL); 508297Seric } 5098078Seric *avp++ = tok; 510297Seric } 5118423Seric } while (c != '\0' && (c != delim || anglecnt > 0)); 5123149Seric *avp = NULL; 51358333Seric p--; 51458333Seric if (delimptr != NULL) 51558333Seric *delimptr = p; 51656764Seric if (tTd(22, 12)) 51756764Seric { 51856764Seric printf("prescan==>"); 51956764Seric printav(av); 52056764Seric } 52159747Seric CurEnv->e_to = saveto; 52258546Seric if (av[0] == NULL) 52358546Seric return (NULL); 52458403Seric return (av); 5253149Seric } 5263149Seric /* 5273149Seric ** TOKTYPE -- return token type 5283149Seric ** 5293149Seric ** Parameters: 5303149Seric ** c -- the character in question. 5313149Seric ** 5323149Seric ** Returns: 5333149Seric ** Its type. 5343149Seric ** 5353149Seric ** Side Effects: 5363149Seric ** none. 5373149Seric */ 538297Seric 5393149Seric toktype(c) 54058050Seric register int c; 5413149Seric { 5423380Seric static char buf[50]; 5433382Seric static bool firstime = TRUE; 5443380Seric 5453382Seric if (firstime) 5463380Seric { 5473382Seric firstime = FALSE; 54858050Seric expand("\201o", buf, &buf[sizeof buf - 1], CurEnv); 5497005Seric (void) strcat(buf, DELIMCHARS); 5503380Seric } 55158050Seric c &= 0377; 55256327Seric if (c == MATCHCLASS || c == MATCHREPL || c == MATCHNCLASS) 5538078Seric return (ONE); 55459027Seric if (c == MACRODEXPAND) 55559027Seric return (ONE); 5568078Seric if (c == '"') 5578078Seric return (QST); 55858050Seric if ((c & 0340) == 0200) 55958050Seric return (OPR); 5604100Seric if (!isascii(c)) 5618078Seric return (ATM); 5628078Seric if (isspace(c) || c == ')') 5638078Seric return (SPC); 56458050Seric if (strchr(buf, c) != NULL) 5658078Seric return (OPR); 5668078Seric return (ATM); 5673149Seric } 5683149Seric /* 5693149Seric ** REWRITE -- apply rewrite rules to token vector. 5703149Seric ** 5714476Seric ** This routine is an ordered production system. Each rewrite 5724476Seric ** rule has a LHS (called the pattern) and a RHS (called the 5734476Seric ** rewrite); 'rwr' points the the current rewrite rule. 5744476Seric ** 5754476Seric ** For each rewrite rule, 'avp' points the address vector we 5764476Seric ** are trying to match against, and 'pvp' points to the pattern. 5778058Seric ** If pvp points to a special match value (MATCHZANY, MATCHANY, 5789585Seric ** MATCHONE, MATCHCLASS, MATCHNCLASS) then the address in avp 5799585Seric ** matched is saved away in the match vector (pointed to by 'mvp'). 5804476Seric ** 5814476Seric ** When a match between avp & pvp does not match, we try to 5829585Seric ** back out. If we back up over MATCHONE, MATCHCLASS, or MATCHNCLASS 5834476Seric ** we must also back out the match in mvp. If we reach a 5848058Seric ** MATCHANY or MATCHZANY we just extend the match and start 5858058Seric ** over again. 5864476Seric ** 5874476Seric ** When we finally match, we rewrite the address vector 5884476Seric ** and try over again. 5894476Seric ** 5903149Seric ** Parameters: 5913149Seric ** pvp -- pointer to token vector. 59259027Seric ** ruleset -- the ruleset to use for rewriting. 59359027Seric ** e -- the current envelope. 5943149Seric ** 5953149Seric ** Returns: 59659084Seric ** A status code. If EX_TEMPFAIL, higher level code should 59759084Seric ** attempt recovery. 5983149Seric ** 5993149Seric ** Side Effects: 6003149Seric ** pvp is modified. 6013149Seric */ 6022091Seric 6033149Seric struct match 6043149Seric { 6054468Seric char **first; /* first token matched */ 6064468Seric char **last; /* last token matched */ 60758825Seric char **pattern; /* pointer to pattern */ 6083149Seric }; 6093149Seric 6104468Seric # define MAXMATCH 9 /* max params per rewrite */ 6113149Seric 6123149Seric 61359084Seric int 61459027Seric rewrite(pvp, ruleset, e) 6153149Seric char **pvp; 6164070Seric int ruleset; 61759027Seric register ENVELOPE *e; 6183149Seric { 6193149Seric register char *ap; /* address pointer */ 6203149Seric register char *rp; /* rewrite pointer */ 6213149Seric register char **avp; /* address vector pointer */ 6223149Seric register char **rvp; /* rewrite vector pointer */ 6238058Seric register struct match *mlp; /* cur ptr into mlist */ 6248058Seric register struct rewrite *rwr; /* pointer to current rewrite rule */ 62558866Seric int ruleno; /* current rule number */ 62659084Seric int rstat = EX_OK; /* return status */ 62764740Seric int loopcount; 62856678Seric struct match mlist[MAXMATCH]; /* stores match on LHS */ 6293149Seric char *npvp[MAXATOM+1]; /* temporary space for rebuild */ 6303149Seric 6319279Seric if (OpMode == MD_TEST || tTd(21, 2)) 6323149Seric { 6338959Seric printf("rewrite: ruleset %2d input:", ruleset); 63456678Seric printav(pvp); 6353149Seric } 63656678Seric if (ruleset < 0 || ruleset >= MAXRWSETS) 63756326Seric { 63858151Seric syserr("554 rewrite: illegal ruleset number %d", ruleset); 63959084Seric return EX_CONFIG; 64056326Seric } 64156678Seric if (pvp == NULL) 64259084Seric return EX_USAGE; 64356326Seric 6443149Seric /* 64556678Seric ** Run through the list of rewrite rules, applying 64656678Seric ** any that match. 6473149Seric */ 6483149Seric 64958866Seric ruleno = 1; 65064740Seric loopcount = 0; 6514070Seric for (rwr = RewriteRules[ruleset]; rwr != NULL; ) 6523149Seric { 6537675Seric if (tTd(21, 12)) 654297Seric { 6558069Seric printf("-----trying rule:"); 65656678Seric printav(rwr->r_lhs); 6573149Seric } 6583149Seric 6593149Seric /* try to match on this rule */ 6604468Seric mlp = mlist; 6618058Seric rvp = rwr->r_lhs; 6628058Seric avp = pvp; 66358866Seric if (++loopcount > 100) 6643149Seric { 66558866Seric syserr("554 Infinite loop in ruleset %d, rule %d", 66658866Seric ruleset, ruleno); 66758866Seric if (tTd(21, 1)) 66852637Seric { 66956678Seric printf("workspace: "); 67056678Seric printav(pvp); 67152637Seric } 67258866Seric break; 67358866Seric } 67458866Seric 67558866Seric while ((ap = *avp) != NULL || *rvp != NULL) 67658866Seric { 6773149Seric rp = *rvp; 6788058Seric if (tTd(21, 35)) 6798058Seric { 68058825Seric printf("ADVANCE rp="); 68157531Seric xputs(rp); 68257532Seric printf(", ap="); 6838058Seric xputs(ap); 6848069Seric printf("\n"); 6858058Seric } 68656678Seric if (rp == NULL) 68756326Seric { 6883149Seric /* end-of-pattern before end-of-address */ 6898058Seric goto backup; 69056678Seric } 69158173Seric if (ap == NULL && (*rp & 0377) != MATCHZANY && 69258827Seric (*rp & 0377) != MATCHZERO) 69356326Seric { 69458825Seric /* end-of-input with patterns left */ 69558825Seric goto backup; 696297Seric } 69756326Seric 69858050Seric switch (*rp & 0377) 6998058Seric { 70056678Seric register STAB *s; 70158814Seric char buf[MAXLINE]; 70256326Seric 70356678Seric case MATCHCLASS: 70458825Seric /* match any phrase in a class */ 70558825Seric mlp->pattern = rvp; 70658814Seric mlp->first = avp; 70758814Seric extendclass: 70858825Seric ap = *avp; 70958825Seric if (ap == NULL) 71058814Seric goto backup; 71158814Seric mlp->last = avp++; 71258814Seric cataddr(mlp->first, mlp->last, buf, sizeof buf, '\0'); 71358814Seric s = stab(buf, ST_CLASS, ST_FIND); 71456678Seric if (s == NULL || !bitnset(rp[1], s->s_class)) 71556326Seric { 71658825Seric if (tTd(21, 36)) 71758825Seric { 71858825Seric printf("EXTEND rp="); 71958825Seric xputs(rp); 72058825Seric printf(", ap="); 72158825Seric xputs(ap); 72258825Seric printf("\n"); 72358825Seric } 72458825Seric goto extendclass; 72556326Seric } 72658825Seric if (tTd(21, 36)) 72758825Seric printf("CLMATCH\n"); 72858814Seric mlp++; 72958814Seric break; 7304060Seric 73158825Seric case MATCHNCLASS: 73258825Seric /* match any token not in a class */ 73358825Seric s = stab(ap, ST_CLASS, ST_FIND); 73458825Seric if (s != NULL && bitnset(rp[1], s->s_class)) 73558825Seric goto backup; 73658825Seric 73758825Seric /* fall through */ 73858825Seric 73956678Seric case MATCHONE: 74056678Seric case MATCHANY: 74156678Seric /* match exactly one token */ 74258825Seric mlp->pattern = rvp; 74356678Seric mlp->first = avp; 74456678Seric mlp->last = avp++; 7458058Seric mlp++; 74656678Seric break; 7478058Seric 74856678Seric case MATCHZANY: 74956678Seric /* match zero or more tokens */ 75058825Seric mlp->pattern = rvp; 75156678Seric mlp->first = avp; 75256678Seric mlp->last = avp - 1; 75356678Seric mlp++; 75456678Seric break; 75556326Seric 75658827Seric case MATCHZERO: 75758173Seric /* match zero tokens */ 75858173Seric break; 75958173Seric 76059027Seric case MACRODEXPAND: 76159027Seric /* 76259027Seric ** Match against run-time macro. 76359027Seric ** This algorithm is broken for the 76459027Seric ** general case (no recursive macros, 76559027Seric ** improper tokenization) but should 76659027Seric ** work for the usual cases. 76759027Seric */ 76859027Seric 76959027Seric ap = macvalue(rp[1], e); 77059027Seric mlp->first = avp; 77159027Seric if (tTd(21, 2)) 77259027Seric printf("rewrite: LHS $&%c => \"%s\"\n", 77359027Seric rp[1], 77459027Seric ap == NULL ? "(NULL)" : ap); 77559027Seric 77659027Seric if (ap == NULL) 77759027Seric break; 77860502Seric while (*ap != '\0') 77959027Seric { 78059027Seric if (*avp == NULL || 78159027Seric strncasecmp(ap, *avp, strlen(*avp)) != 0) 78259027Seric { 78359027Seric /* no match */ 78459027Seric avp = mlp->first; 78559027Seric goto backup; 78659027Seric } 78759027Seric ap += strlen(*avp++); 78859027Seric } 78959027Seric 79059027Seric /* match */ 79159027Seric break; 79259027Seric 79356678Seric default: 79456678Seric /* must have exact match */ 79556678Seric if (strcasecmp(rp, ap)) 7968058Seric goto backup; 7974468Seric avp++; 79856678Seric break; 7993149Seric } 8003149Seric 80156678Seric /* successful match on this token */ 8023149Seric rvp++; 8033149Seric continue; 8043149Seric 80558825Seric backup: 80656678Seric /* match failed -- back up */ 80758825Seric while (--mlp >= mlist) 8083149Seric { 80958825Seric rvp = mlp->pattern; 81056678Seric rp = *rvp; 81158825Seric avp = mlp->last + 1; 81258825Seric ap = *avp; 81358825Seric 81458825Seric if (tTd(21, 36)) 81558825Seric { 81658825Seric printf("BACKUP rp="); 81758825Seric xputs(rp); 81858825Seric printf(", ap="); 81958825Seric xputs(ap); 82058825Seric printf("\n"); 82158825Seric } 82258825Seric 82358825Seric if (ap == NULL) 82458825Seric { 82558825Seric /* run off the end -- back up again */ 82658825Seric continue; 82758825Seric } 82858050Seric if ((*rp & 0377) == MATCHANY || 82958050Seric (*rp & 0377) == MATCHZANY) 8304468Seric { 83156678Seric /* extend binding and continue */ 83258825Seric mlp->last = avp++; 83356678Seric rvp++; 83458825Seric mlp++; 83556678Seric break; 8364468Seric } 83758825Seric if ((*rp & 0377) == MATCHCLASS) 83856678Seric { 83958814Seric /* extend binding and try again */ 84063397Seric mlp->last = avp; 84158814Seric goto extendclass; 84258814Seric } 8433149Seric } 8443149Seric 84558825Seric if (mlp < mlist) 84656678Seric { 84756678Seric /* total failure to match */ 84856326Seric break; 8493149Seric } 850297Seric } 8513149Seric 8523149Seric /* 85356678Seric ** See if we successfully matched 8543149Seric */ 8553149Seric 85658827Seric if (mlp < mlist || *rvp != NULL) 8573149Seric { 8589374Seric if (tTd(21, 10)) 8599374Seric printf("----- rule fails\n"); 8609374Seric rwr = rwr->r_next; 86158866Seric ruleno++; 86264740Seric loopcount = 0; 8639374Seric continue; 8649374Seric } 8653149Seric 8669374Seric rvp = rwr->r_rhs; 8679374Seric if (tTd(21, 12)) 8689374Seric { 8699374Seric printf("-----rule matches:"); 87056678Seric printav(rvp); 8719374Seric } 8729374Seric 8739374Seric rp = *rvp; 87458050Seric if ((*rp & 0377) == CANONUSER) 8759374Seric { 8769374Seric rvp++; 8779374Seric rwr = rwr->r_next; 87858866Seric ruleno++; 87964740Seric loopcount = 0; 8809374Seric } 88158050Seric else if ((*rp & 0377) == CANONHOST) 8829374Seric { 8839374Seric rvp++; 8849374Seric rwr = NULL; 8859374Seric } 88658050Seric else if ((*rp & 0377) == CANONNET) 8879374Seric rwr = NULL; 8889374Seric 8899374Seric /* substitute */ 8909374Seric for (avp = npvp; *rvp != NULL; rvp++) 8919374Seric { 8929374Seric register struct match *m; 8939374Seric register char **pp; 8949374Seric 8958058Seric rp = *rvp; 89658050Seric if ((*rp & 0377) == MATCHREPL) 8978058Seric { 89816914Seric /* substitute from LHS */ 89916914Seric m = &mlist[rp[1] - '1']; 90056678Seric if (m < mlist || m >= mlp) 9019374Seric { 90258151Seric syserr("554 rewrite: ruleset %d: replacement $%c out of bounds", 90356326Seric ruleset, rp[1]); 90459084Seric return EX_CONFIG; 9059374Seric } 90616914Seric if (tTd(21, 15)) 90716914Seric { 90816914Seric printf("$%c:", rp[1]); 90916914Seric pp = m->first; 91056678Seric while (pp <= m->last) 91116914Seric { 91216914Seric printf(" %x=\"", *pp); 91316914Seric (void) fflush(stdout); 91416914Seric printf("%s\"", *pp++); 91516914Seric } 91616914Seric printf("\n"); 91716914Seric } 9189374Seric pp = m->first; 91956678Seric while (pp <= m->last) 9203149Seric { 92116914Seric if (avp >= &npvp[MAXATOM]) 92256678Seric { 92358151Seric syserr("554 rewrite: expansion too long"); 92459084Seric return EX_DATAERR; 92556678Seric } 92616914Seric *avp++ = *pp++; 9273149Seric } 9283149Seric } 92916914Seric else 9308226Seric { 93116914Seric /* vanilla replacement */ 9329374Seric if (avp >= &npvp[MAXATOM]) 93316889Seric { 93456678Seric toolong: 93558151Seric syserr("554 rewrite: expansion too long"); 93659084Seric return EX_DATAERR; 93716889Seric } 93859027Seric if ((*rp & 0377) != MACRODEXPAND) 93959027Seric *avp++ = rp; 94059027Seric else 94159027Seric { 94259027Seric *avp = macvalue(rp[1], e); 94359027Seric if (tTd(21, 2)) 94459027Seric printf("rewrite: RHS $&%c => \"%s\"\n", 94559027Seric rp[1], 94659027Seric *avp == NULL ? "(NULL)" : *avp); 94759027Seric if (*avp != NULL) 94859027Seric avp++; 94959027Seric } 9508226Seric } 9519374Seric } 9529374Seric *avp++ = NULL; 95316914Seric 95416914Seric /* 95556678Seric ** Check for any hostname/keyword lookups. 95616914Seric */ 95716914Seric 95816914Seric for (rvp = npvp; *rvp != NULL; rvp++) 95916914Seric { 96056678Seric char **hbrvp; 96116914Seric char **xpvp; 96216914Seric int trsize; 96356678Seric char *replac; 96456678Seric int endtoken; 96556678Seric STAB *map; 96656678Seric char *mapname; 96756678Seric char **key_rvp; 96856678Seric char **arg_rvp; 96956678Seric char **default_rvp; 97056678Seric char buf[MAXNAME + 1]; 97116914Seric char *pvpb1[MAXATOM + 1]; 97256823Seric char *argvect[10]; 97317174Seric char pvpbuf[PSBUFSIZE]; 97464404Seric char *nullpvp[1]; 97516914Seric 97658050Seric if ((**rvp & 0377) != HOSTBEGIN && 97758050Seric (**rvp & 0377) != LOOKUPBEGIN) 97816914Seric continue; 97916914Seric 98016914Seric /* 98156678Seric ** Got a hostname/keyword lookup. 98216914Seric ** 98316914Seric ** This could be optimized fairly easily. 98416914Seric */ 98516914Seric 98616914Seric hbrvp = rvp; 98758050Seric if ((**rvp & 0377) == HOSTBEGIN) 98856327Seric { 98956678Seric endtoken = HOSTEND; 99056678Seric mapname = "host"; 99156327Seric } 99256326Seric else 99356327Seric { 99456678Seric endtoken = LOOKUPEND; 99556678Seric mapname = *++rvp; 99656327Seric } 99756678Seric map = stab(mapname, ST_MAP, ST_FIND); 99856678Seric if (map == NULL) 99958151Seric syserr("554 rewrite: map %s not found", mapname); 100056678Seric 100156678Seric /* extract the match part */ 100256678Seric key_rvp = ++rvp; 100356823Seric default_rvp = NULL; 100456823Seric arg_rvp = argvect; 100556823Seric xpvp = NULL; 100656823Seric replac = pvpbuf; 100758050Seric while (*rvp != NULL && (**rvp & 0377) != endtoken) 100853654Seric { 100958050Seric int nodetype = **rvp & 0377; 101056823Seric 101156823Seric if (nodetype != CANONHOST && nodetype != CANONUSER) 101256678Seric { 101356823Seric rvp++; 101456823Seric continue; 101556823Seric } 101656823Seric 101756823Seric *rvp++ = NULL; 101856823Seric 101956823Seric if (xpvp != NULL) 102056823Seric { 102158814Seric cataddr(xpvp, NULL, replac, 102258082Seric &pvpbuf[sizeof pvpbuf] - replac, 102358082Seric '\0'); 102456823Seric *++arg_rvp = replac; 102556823Seric replac += strlen(replac) + 1; 102656823Seric xpvp = NULL; 102756823Seric } 102856823Seric switch (nodetype) 102956823Seric { 103056678Seric case CANONHOST: 103156823Seric xpvp = rvp; 103256678Seric break; 103356678Seric 103456678Seric case CANONUSER: 103556678Seric default_rvp = rvp; 103656678Seric break; 103756678Seric } 103853654Seric } 103916914Seric if (*rvp != NULL) 104016914Seric *rvp++ = NULL; 104156823Seric if (xpvp != NULL) 104256823Seric { 104358814Seric cataddr(xpvp, NULL, replac, 104458082Seric &pvpbuf[sizeof pvpbuf] - replac, 104558082Seric '\0'); 104656823Seric *++arg_rvp = replac; 104756823Seric } 104856823Seric *++arg_rvp = NULL; 104916914Seric 105016914Seric /* save the remainder of the input string */ 105116914Seric trsize = (int) (avp - rvp + 1) * sizeof *rvp; 105216914Seric bcopy((char *) rvp, (char *) pvpb1, trsize); 105316914Seric 105456678Seric /* look it up */ 105558814Seric cataddr(key_rvp, NULL, buf, sizeof buf, '\0'); 105656823Seric argvect[0] = buf; 105760538Seric if (map != NULL && bitset(MF_OPEN, map->s_map.map_mflags)) 105856836Seric { 105959084Seric auto int stat = EX_OK; 106056836Seric 106160215Seric /* XXX should try to auto-open the map here */ 106260215Seric 106358796Seric if (tTd(60, 1)) 106458796Seric printf("map_lookup(%s, %s) => ", 106558796Seric mapname, buf); 106656836Seric replac = (*map->s_map.map_class->map_lookup)(&map->s_map, 106760089Seric buf, argvect, &stat); 106858796Seric if (tTd(60, 1)) 106959084Seric printf("%s (%d)\n", 107059084Seric replac ? replac : "NOT FOUND", 107159084Seric stat); 107259084Seric 107359084Seric /* should recover if stat == EX_TEMPFAIL */ 107459084Seric if (stat == EX_TEMPFAIL) 107559084Seric rstat = stat; 107656836Seric } 107753654Seric else 107856678Seric replac = NULL; 107956678Seric 108056678Seric /* if no replacement, use default */ 108156823Seric if (replac == NULL && default_rvp != NULL) 108256823Seric { 108360089Seric /* create the default */ 108460089Seric cataddr(default_rvp, NULL, buf, sizeof buf, '\0'); 108556823Seric replac = buf; 108656823Seric } 108756823Seric 108856678Seric if (replac == NULL) 108951317Seric { 109056823Seric xpvp = key_rvp; 109153654Seric } 109264404Seric else if (*replac == '\0') 109364404Seric { 109464404Seric /* null replacement */ 109564404Seric nullpvp[0] = NULL; 109664404Seric xpvp = nullpvp; 109764404Seric } 109856678Seric else 109953654Seric { 110056678Seric /* scan the new replacement */ 110158333Seric xpvp = prescan(replac, '\0', pvpbuf, NULL); 110253654Seric if (xpvp == NULL) 110351317Seric { 110458403Seric /* prescan already printed error */ 110559084Seric return EX_DATAERR; 110656678Seric } 110751317Seric } 110851317Seric 110916914Seric /* append it to the token list */ 111056678Seric for (avp = hbrvp; *xpvp != NULL; xpvp++) 111156678Seric { 111217174Seric *avp++ = newstr(*xpvp); 111316920Seric if (avp >= &npvp[MAXATOM]) 111416914Seric goto toolong; 111517174Seric } 111616914Seric 111716914Seric /* restore the old trailing information */ 111856678Seric for (xpvp = pvpb1; (*avp++ = *xpvp++) != NULL; ) 111916920Seric if (avp >= &npvp[MAXATOM]) 112016914Seric goto toolong; 112117174Seric 112256678Seric break; 112316914Seric } 112416914Seric 112516914Seric /* 112616914Seric ** Check for subroutine calls. 112716914Seric */ 112816914Seric 112958050Seric if (*npvp != NULL && (**npvp & 0377) == CALLSUBR) 113056678Seric { 113159084Seric int stat; 113259084Seric 113356678Seric bcopy((char *) &npvp[2], (char *) pvp, 113456678Seric (int) (avp - npvp - 2) * sizeof *avp); 113556678Seric if (tTd(21, 3)) 113656678Seric printf("-----callsubr %s\n", npvp[1]); 113759084Seric stat = rewrite(pvp, atoi(npvp[1]), e); 113859084Seric if (rstat == EX_OK || stat == EX_TEMPFAIL) 113959084Seric rstat = stat; 114063581Seric if ((**pvp & 0377) == CANONNET) 114163581Seric rwr = NULL; 114256678Seric } 114356678Seric else 114456678Seric { 114517348Seric bcopy((char *) npvp, (char *) pvp, 114616900Seric (int) (avp - npvp) * sizeof *avp); 114756678Seric } 11489374Seric if (tTd(21, 4)) 11499374Seric { 11509374Seric printf("rewritten as:"); 115156678Seric printav(pvp); 11529374Seric } 1153297Seric } 11548069Seric 11559279Seric if (OpMode == MD_TEST || tTd(21, 2)) 11568069Seric { 11578959Seric printf("rewrite: ruleset %2d returns:", ruleset); 115856678Seric printav(pvp); 11598069Seric } 116059084Seric 116159084Seric return rstat; 11623149Seric } 11633149Seric /* 11643149Seric ** BUILDADDR -- build address from token vector. 11653149Seric ** 11663149Seric ** Parameters: 11673149Seric ** tv -- token vector. 11683149Seric ** a -- pointer to address descriptor to fill. 11693149Seric ** If NULL, one will be allocated. 117064284Seric ** flags -- info regarding whether this is a sender or 117164284Seric ** a recipient. 117258966Seric ** e -- the current envelope. 11733149Seric ** 11743149Seric ** Returns: 11754279Seric ** NULL if there was an error. 11764279Seric ** 'a' otherwise. 11773149Seric ** 11783149Seric ** Side Effects: 11793149Seric ** fills in 'a' 11803149Seric */ 11813149Seric 118257249Seric struct errcodes 118357249Seric { 118457249Seric char *ec_name; /* name of error code */ 118557249Seric int ec_code; /* numeric code */ 118657249Seric } ErrorCodes[] = 118757249Seric { 118857249Seric "usage", EX_USAGE, 118957249Seric "nouser", EX_NOUSER, 119057249Seric "nohost", EX_NOHOST, 119157249Seric "unavailable", EX_UNAVAILABLE, 119257249Seric "software", EX_SOFTWARE, 119357249Seric "tempfail", EX_TEMPFAIL, 119457249Seric "protocol", EX_PROTOCOL, 119557249Seric #ifdef EX_CONFIG 119657249Seric "config", EX_CONFIG, 119757249Seric #endif 119857249Seric NULL, EX_UNAVAILABLE, 119957249Seric }; 120057249Seric 120156678Seric ADDRESS * 120264284Seric buildaddr(tv, a, flags, e) 12033149Seric register char **tv; 12043149Seric register ADDRESS *a; 120564284Seric int flags; 120658966Seric register ENVELOPE *e; 12073149Seric { 12083149Seric struct mailer **mp; 12093149Seric register struct mailer *m; 121058008Seric char *bp; 121158008Seric int spaceleft; 121264306Seric static MAILER errormailer; 121364306Seric static char *errorargv[] = { "ERROR", NULL }; 121457402Seric static char buf[MAXNAME]; 12153149Seric 121664791Seric if (tTd(24, 5)) 121764791Seric { 121864791Seric printf("buildaddr, flags=%o, tv=", flags); 121964791Seric printav(tv); 122064791Seric } 122164791Seric 12223149Seric if (a == NULL) 12233149Seric a = (ADDRESS *) xalloc(sizeof *a); 122416889Seric bzero((char *) a, sizeof *a); 12253149Seric 12263149Seric /* figure out what net/mailer to use */ 122764306Seric if (*tv == NULL || (**tv & 0377) != CANONNET) 12284279Seric { 122958151Seric syserr("554 buildaddr: no net"); 123064306Seric badaddr: 123164306Seric a->q_flags |= QBADADDR; 123264306Seric a->q_mailer = &errormailer; 123364306Seric if (errormailer.m_name == NULL) 123464306Seric { 123564306Seric /* initialize the bogus mailer */ 123664306Seric errormailer.m_name = "*error*"; 123764306Seric errormailer.m_mailer = "ERROR"; 123864306Seric errormailer.m_argv = errorargv; 123964306Seric } 124064306Seric return a; 12414279Seric } 12423149Seric tv++; 124358680Seric if (strcasecmp(*tv, "error") == 0) 12444279Seric { 124558050Seric if ((**++tv & 0377) == CANONHOST) 124610183Seric { 124757249Seric register struct errcodes *ep; 124857249Seric 124958050Seric if (isascii(**++tv) && isdigit(**tv)) 125057249Seric { 125157249Seric setstat(atoi(*tv)); 125257249Seric } 125357249Seric else 125457249Seric { 125557249Seric for (ep = ErrorCodes; ep->ec_name != NULL; ep++) 125657249Seric if (strcasecmp(ep->ec_name, *tv) == 0) 125757249Seric break; 125857249Seric setstat(ep->ec_code); 125957249Seric } 126010183Seric tv++; 126110183Seric } 1262*64928Seric else 1263*64928Seric setstat(EX_UNAVAILABLE); 126458050Seric if ((**tv & 0377) != CANONUSER) 126558151Seric syserr("554 buildaddr: error: no user"); 126658814Seric cataddr(++tv, NULL, buf, sizeof buf, ' '); 126758082Seric stripquotes(buf); 126864659Seric if (isascii(buf[0]) && isdigit(buf[0]) && 126964659Seric isascii(buf[1]) && isdigit(buf[1]) && 127064659Seric isascii(buf[2]) && isdigit(buf[2]) && 127164659Seric buf[3] == ' ') 127264659Seric { 127364659Seric char fmt[10]; 127464659Seric 127564659Seric strncpy(fmt, buf, 3); 127664659Seric strcpy(&fmt[3], " %s"); 127764659Seric usrerr(fmt, buf + 4); 127864659Seric } 127964659Seric else 128064659Seric { 128164659Seric usrerr("%s", buf); 128264659Seric } 128364306Seric goto badaddr; 12844279Seric } 128557402Seric 12864598Seric for (mp = Mailer; (m = *mp++) != NULL; ) 12873149Seric { 128858680Seric if (strcasecmp(m->m_name, *tv) == 0) 12893149Seric break; 12903149Seric } 12913149Seric if (m == NULL) 12924279Seric { 129358151Seric syserr("554 buildaddr: unknown mailer %s", *tv); 129464306Seric goto badaddr; 12954279Seric } 12964598Seric a->q_mailer = m; 12973149Seric 12983149Seric /* figure out what host (if any) */ 129956678Seric tv++; 130058509Seric if ((**tv & 0377) == CANONHOST) 13013149Seric { 130258008Seric bp = buf; 130358008Seric spaceleft = sizeof buf - 1; 130458050Seric while (*++tv != NULL && (**tv & 0377) != CANONUSER) 130558008Seric { 130658008Seric int i = strlen(*tv); 130758008Seric 130858008Seric if (i > spaceleft) 130958008Seric { 131058008Seric /* out of space for this address */ 131158008Seric if (spaceleft >= 0) 131258151Seric syserr("554 buildaddr: host too long (%.40s...)", 131358008Seric buf); 131458008Seric i = spaceleft; 131558008Seric spaceleft = 0; 131658008Seric } 131758008Seric if (i <= 0) 131858008Seric continue; 131958008Seric bcopy(*tv, bp, i); 132058008Seric bp += i; 132158008Seric spaceleft -= i; 132258008Seric } 132358010Seric *bp = '\0'; 13245704Seric a->q_host = newstr(buf); 13253149Seric } 132657249Seric else 132758509Seric { 132858509Seric if (!bitnset(M_LOCALMAILER, m->m_flags)) 132958509Seric { 133058509Seric syserr("554 buildaddr: no host"); 133164306Seric goto badaddr; 133258509Seric } 133357249Seric a->q_host = NULL; 133458509Seric } 13353149Seric 13363149Seric /* figure out the user */ 133758050Seric if (*tv == NULL || (**tv & 0377) != CANONUSER) 13384279Seric { 133958151Seric syserr("554 buildaddr: no user"); 134064306Seric goto badaddr; 13414279Seric } 134257402Seric tv++; 134351317Seric 134457402Seric /* do special mapping for local mailer */ 134557402Seric if (m == LocalMailer && *tv != NULL) 134657402Seric { 134757454Seric register char *p = *tv; 134857454Seric 134957454Seric if (*p == '"') 135057454Seric p++; 135157454Seric if (*p == '|') 135257402Seric a->q_mailer = m = ProgMailer; 135357454Seric else if (*p == '/') 135457402Seric a->q_mailer = m = FileMailer; 135557454Seric else if (*p == ':') 135657402Seric { 135757402Seric /* may be :include: */ 135858814Seric cataddr(tv, NULL, buf, sizeof buf, '\0'); 135958008Seric stripquotes(buf); 136058008Seric if (strncasecmp(buf, ":include:", 9) == 0) 136158008Seric { 136258008Seric /* if :include:, don't need further rewriting */ 136357402Seric a->q_mailer = m = InclMailer; 136458008Seric a->q_user = &buf[9]; 136558008Seric return (a); 136658008Seric } 136757402Seric } 136857402Seric } 136957402Seric 137058008Seric if (m == LocalMailer && *tv != NULL && strcmp(*tv, "@") == 0) 137158008Seric { 137258008Seric tv++; 137358008Seric a->q_flags |= QNOTREMOTE; 137458008Seric } 137558008Seric 137664284Seric /* rewrite according recipient mailer rewriting rules */ 137764284Seric define('h', a->q_host, e); 137864284Seric if (!bitset(RF_SENDERADDR|RF_HEADERADDR, flags)) 137964284Seric { 138064284Seric /* sender addresses done later */ 138164284Seric (void) rewrite(tv, 2, e); 138264284Seric if (m->m_re_rwset > 0) 138364284Seric (void) rewrite(tv, m->m_re_rwset, e); 138464284Seric } 138559084Seric (void) rewrite(tv, 4, e); 138619040Seric 138719040Seric /* save the result for the command line/RCPT argument */ 138858814Seric cataddr(tv, NULL, buf, sizeof buf, '\0'); 13893149Seric a->q_user = buf; 13903149Seric 139158670Seric /* 139258670Seric ** Do mapping to lower case as requested by mailer 139358670Seric */ 139458670Seric 139558670Seric if (a->q_host != NULL && !bitnset(M_HST_UPPER, m->m_flags)) 139658670Seric makelower(a->q_host); 139758670Seric if (!bitnset(M_USR_UPPER, m->m_flags)) 139858670Seric makelower(a->q_user); 139958670Seric 14003149Seric return (a); 14013149Seric } 14023188Seric /* 14034228Seric ** CATADDR -- concatenate pieces of addresses (putting in <LWSP> subs) 14044228Seric ** 14054228Seric ** Parameters: 14064228Seric ** pvp -- parameter vector to rebuild. 140758814Seric ** evp -- last parameter to include. Can be NULL to 140858814Seric ** use entire pvp. 14094228Seric ** buf -- buffer to build the string into. 14104228Seric ** sz -- size of buf. 141158082Seric ** spacesub -- the space separator character; if null, 141258082Seric ** use SpaceSub. 14134228Seric ** 14144228Seric ** Returns: 14154228Seric ** none. 14164228Seric ** 14174228Seric ** Side Effects: 14184228Seric ** Destroys buf. 14194228Seric */ 14204228Seric 142158814Seric cataddr(pvp, evp, buf, sz, spacesub) 14224228Seric char **pvp; 142358814Seric char **evp; 14244228Seric char *buf; 14254228Seric register int sz; 142658082Seric char spacesub; 14274228Seric { 14284228Seric bool oatomtok = FALSE; 142956678Seric bool natomtok = FALSE; 14304228Seric register int i; 14314228Seric register char *p; 14324228Seric 143358082Seric if (spacesub == '\0') 143458082Seric spacesub = SpaceSub; 143558082Seric 14368423Seric if (pvp == NULL) 14378423Seric { 143823109Seric (void) strcpy(buf, ""); 14398423Seric return; 14408423Seric } 14414228Seric p = buf; 144211156Seric sz -= 2; 14434228Seric while (*pvp != NULL && (i = strlen(*pvp)) < sz) 14444228Seric { 14458078Seric natomtok = (toktype(**pvp) == ATM); 14464228Seric if (oatomtok && natomtok) 144758082Seric *p++ = spacesub; 14484228Seric (void) strcpy(p, *pvp); 14494228Seric oatomtok = natomtok; 14504228Seric p += i; 145111156Seric sz -= i + 1; 145258814Seric if (pvp++ == evp) 145358814Seric break; 14544228Seric } 14554228Seric *p = '\0'; 14564228Seric } 14574228Seric /* 14583188Seric ** SAMEADDR -- Determine if two addresses are the same 14593188Seric ** 14603188Seric ** This is not just a straight comparison -- if the mailer doesn't 14613188Seric ** care about the host we just ignore it, etc. 14623188Seric ** 14633188Seric ** Parameters: 14643188Seric ** a, b -- pointers to the internal forms to compare. 14653188Seric ** 14663188Seric ** Returns: 14673188Seric ** TRUE -- they represent the same mailbox. 14683188Seric ** FALSE -- they don't. 14693188Seric ** 14703188Seric ** Side Effects: 14713188Seric ** none. 14723188Seric */ 14733188Seric 14743188Seric bool 14759374Seric sameaddr(a, b) 14763188Seric register ADDRESS *a; 14773188Seric register ADDRESS *b; 14783188Seric { 14793188Seric /* if they don't have the same mailer, forget it */ 14803188Seric if (a->q_mailer != b->q_mailer) 14813188Seric return (FALSE); 14823188Seric 14833188Seric /* if the user isn't the same, we can drop out */ 148456678Seric if (strcmp(a->q_user, b->q_user) != 0) 14853188Seric return (FALSE); 14863188Seric 148758438Seric /* if we have good uids for both but the differ, these are different */ 148858438Seric if (bitset(QGOODUID, a->q_flags & b->q_flags) && a->q_uid != b->q_uid) 148958438Seric return (FALSE); 149058438Seric 149158509Seric /* otherwise compare hosts (but be careful for NULL ptrs) */ 149258509Seric if (a->q_host == b->q_host) 149358509Seric { 149458509Seric /* probably both null pointers */ 14953188Seric return (TRUE); 149658509Seric } 14973188Seric if (a->q_host == NULL || b->q_host == NULL) 149858509Seric { 149958509Seric /* only one is a null pointer */ 15003188Seric return (FALSE); 150158509Seric } 150256678Seric if (strcmp(a->q_host, b->q_host) != 0) 15033188Seric return (FALSE); 15043188Seric 15053188Seric return (TRUE); 15063188Seric } 15073234Seric /* 15083234Seric ** PRINTADDR -- print address (for debugging) 15093234Seric ** 15103234Seric ** Parameters: 15113234Seric ** a -- the address to print 15123234Seric ** follow -- follow the q_next chain. 15133234Seric ** 15143234Seric ** Returns: 15153234Seric ** none. 15163234Seric ** 15173234Seric ** Side Effects: 15183234Seric ** none. 15193234Seric */ 15203234Seric 15213234Seric printaddr(a, follow) 15223234Seric register ADDRESS *a; 15233234Seric bool follow; 15243234Seric { 15255001Seric bool first = TRUE; 152657731Seric register MAILER *m; 152757731Seric MAILER pseudomailer; 15285001Seric 15293234Seric while (a != NULL) 15303234Seric { 15315001Seric first = FALSE; 15324443Seric printf("%x=", a); 15334085Seric (void) fflush(stdout); 153457731Seric 153557731Seric /* find the mailer -- carefully */ 153657731Seric m = a->q_mailer; 153757731Seric if (m == NULL) 153857731Seric { 153957731Seric m = &pseudomailer; 154057731Seric m->m_mno = -1; 154157731Seric m->m_name = "NULL"; 154257731Seric } 154357731Seric 154458680Seric printf("%s:\n\tmailer %d (%s), host `%s', user `%s', ruser `%s'\n", 154557731Seric a->q_paddr, m->m_mno, m->m_name, 154663756Seric a->q_host, a->q_user, 154763756Seric a->q_ruser ? a->q_ruser : "<null>"); 154859269Seric printf("\tnext=%x, flags=%o, alias %x, uid %d, gid %d\n", 154959269Seric a->q_next, a->q_flags, a->q_alias, a->q_uid, a->q_gid); 155059269Seric printf("\towner=%s, home=\"%s\", fullname=\"%s\"\n", 155159269Seric a->q_owner == NULL ? "(none)" : a->q_owner, 155263756Seric a->q_home == NULL ? "(none)" : a->q_home, 155363756Seric a->q_fullname == NULL ? "(none)" : a->q_fullname); 15544996Seric 15553234Seric if (!follow) 15563234Seric return; 15574996Seric a = a->q_next; 15583234Seric } 15595001Seric if (first) 15604443Seric printf("[NULL]\n"); 15613234Seric } 15624317Seric 15637682Seric /* 15647682Seric ** REMOTENAME -- return the name relative to the current mailer 15657682Seric ** 15667682Seric ** Parameters: 15677682Seric ** name -- the name to translate. 15688069Seric ** m -- the mailer that we want to do rewriting relative 15698069Seric ** to. 157059163Seric ** flags -- fine tune operations. 157159163Seric ** pstat -- pointer to status word. 157258020Seric ** e -- the current envelope. 15737682Seric ** 15747682Seric ** Returns: 15757682Seric ** the text string representing this address relative to 15767682Seric ** the receiving mailer. 15777682Seric ** 15787682Seric ** Side Effects: 15797682Seric ** none. 15807682Seric ** 15817682Seric ** Warnings: 15827682Seric ** The text string returned is tucked away locally; 15837682Seric ** copy it if you intend to save it. 15847682Seric */ 15857682Seric 15867682Seric char * 158759163Seric remotename(name, m, flags, pstat, e) 15887682Seric char *name; 158956678Seric struct mailer *m; 159059163Seric int flags; 159159163Seric int *pstat; 159256678Seric register ENVELOPE *e; 15937682Seric { 15948069Seric register char **pvp; 15958069Seric char *fancy; 159656678Seric char *oldg = macvalue('g', e); 159758020Seric int rwset; 15987682Seric static char buf[MAXNAME]; 15997682Seric char lbuf[MAXNAME]; 160016914Seric char pvpbuf[PSBUFSIZE]; 160156678Seric extern char *crackaddr(); 16027682Seric 16037755Seric if (tTd(12, 1)) 16047755Seric printf("remotename(%s)\n", name); 16057755Seric 160610177Seric /* don't do anything if we are tagging it as special */ 160759163Seric if (bitset(RF_SENDERADDR, flags)) 160859163Seric rwset = bitset(RF_HEADERADDR, flags) ? m->m_sh_rwset 160959163Seric : m->m_se_rwset; 161058020Seric else 161159163Seric rwset = bitset(RF_HEADERADDR, flags) ? m->m_rh_rwset 161259163Seric : m->m_re_rwset; 161358020Seric if (rwset < 0) 161410177Seric return (name); 161510177Seric 16167682Seric /* 16178181Seric ** Do a heuristic crack of this name to extract any comment info. 16188181Seric ** This will leave the name as a comment and a $g macro. 16197889Seric */ 16207889Seric 162159163Seric if (bitset(RF_CANONICAL, flags) || bitnset(M_NOCOMMENT, m->m_flags)) 162258050Seric fancy = "\201g"; 162310310Seric else 162410310Seric fancy = crackaddr(name); 16257889Seric 16268181Seric /* 16278181Seric ** Turn the name into canonical form. 16288181Seric ** Normally this will be RFC 822 style, i.e., "user@domain". 16298181Seric ** If this only resolves to "user", and the "C" flag is 16308181Seric ** specified in the sending mailer, then the sender's 16318181Seric ** domain will be appended. 16328181Seric */ 16338181Seric 163458333Seric pvp = prescan(name, '\0', pvpbuf, NULL); 16357889Seric if (pvp == NULL) 16367889Seric return (name); 163759163Seric if (rewrite(pvp, 3, e) == EX_TEMPFAIL) 163859163Seric *pstat = EX_TEMPFAIL; 163959163Seric if (bitset(RF_ADDDOMAIN, flags) && e->e_fromdomain != NULL) 16408181Seric { 16418181Seric /* append from domain to this address */ 16428181Seric register char **pxp = pvp; 16438181Seric 16449594Seric /* see if there is an "@domain" in the current name */ 16458181Seric while (*pxp != NULL && strcmp(*pxp, "@") != 0) 16468181Seric pxp++; 16478181Seric if (*pxp == NULL) 16488181Seric { 16499594Seric /* no.... append the "@domain" from the sender */ 165056678Seric register char **qxq = e->e_fromdomain; 16518181Seric 16529594Seric while ((*pxp++ = *qxq++) != NULL) 16539594Seric continue; 165459163Seric if (rewrite(pvp, 3, e) == EX_TEMPFAIL) 165559163Seric *pstat = EX_TEMPFAIL; 16568181Seric } 16578181Seric } 16588181Seric 16598181Seric /* 16608959Seric ** Do more specific rewriting. 166156678Seric ** Rewrite using ruleset 1 or 2 depending on whether this is 166256678Seric ** a sender address or not. 16638181Seric ** Then run it through any receiving-mailer-specific rulesets. 16648181Seric */ 16658181Seric 166659163Seric if (bitset(RF_SENDERADDR, flags)) 166759541Seric { 166859163Seric if (rewrite(pvp, 1, e) == EX_TEMPFAIL) 166959163Seric *pstat = EX_TEMPFAIL; 167059541Seric } 16718069Seric else 167259541Seric { 167359163Seric if (rewrite(pvp, 2, e) == EX_TEMPFAIL) 167459163Seric *pstat = EX_TEMPFAIL; 167559541Seric } 167658020Seric if (rwset > 0) 167759541Seric { 167859163Seric if (rewrite(pvp, rwset, e) == EX_TEMPFAIL) 167959163Seric *pstat = EX_TEMPFAIL; 168059541Seric } 16817682Seric 16828181Seric /* 16838959Seric ** Do any final sanitation the address may require. 16848959Seric ** This will normally be used to turn internal forms 16858959Seric ** (e.g., user@host.LOCAL) into external form. This 16868959Seric ** may be used as a default to the above rules. 16878959Seric */ 16888959Seric 168959163Seric if (rewrite(pvp, 4, e) == EX_TEMPFAIL) 169059163Seric *pstat = EX_TEMPFAIL; 16918959Seric 16928959Seric /* 16938181Seric ** Now restore the comment information we had at the beginning. 16948181Seric */ 16958181Seric 169658825Seric cataddr(pvp, NULL, lbuf, sizeof lbuf, '\0'); 169756678Seric define('g', lbuf, e); 169856678Seric expand(fancy, buf, &buf[sizeof buf - 1], e); 169956678Seric define('g', oldg, e); 17007682Seric 17017682Seric if (tTd(12, 1)) 17027755Seric printf("remotename => `%s'\n", buf); 17037682Seric return (buf); 17047682Seric } 170551317Seric /* 170656678Seric ** MAPLOCALUSER -- run local username through ruleset 5 for final redirection 170751317Seric ** 170851317Seric ** Parameters: 170956678Seric ** a -- the address to map (but just the user name part). 171056678Seric ** sendq -- the sendq in which to install any replacement 171156678Seric ** addresses. 171251317Seric ** 171351317Seric ** Returns: 171451317Seric ** none. 171551317Seric */ 171651317Seric 171756678Seric maplocaluser(a, sendq, e) 171856678Seric register ADDRESS *a; 171956678Seric ADDRESS **sendq; 172056678Seric ENVELOPE *e; 172151317Seric { 172256678Seric register char **pvp; 172356678Seric register ADDRESS *a1 = NULL; 172458333Seric auto char *delimptr; 172556678Seric char pvpbuf[PSBUFSIZE]; 172651317Seric 172756678Seric if (tTd(29, 1)) 172856678Seric { 172956678Seric printf("maplocaluser: "); 173056678Seric printaddr(a, FALSE); 173156678Seric } 173258333Seric pvp = prescan(a->q_user, '\0', pvpbuf, &delimptr); 173356678Seric if (pvp == NULL) 173456678Seric return; 173551317Seric 173659084Seric (void) rewrite(pvp, 5, e); 173758050Seric if (pvp[0] == NULL || (pvp[0][0] & 0377) != CANONNET) 173856678Seric return; 173951317Seric 174056678Seric /* if non-null, mailer destination specified -- has it changed? */ 174164284Seric a1 = buildaddr(pvp, NULL, 0, e); 174256678Seric if (a1 == NULL || sameaddr(a, a1)) 174356678Seric return; 174451317Seric 174556678Seric /* mark old address as dead; insert new address */ 174656678Seric a->q_flags |= QDONTSEND; 174757731Seric if (tTd(29, 5)) 174857731Seric { 174957731Seric printf("maplocaluser: QDONTSEND "); 175057731Seric printaddr(a, FALSE); 175157731Seric } 175256678Seric a1->q_alias = a; 175364348Seric allocaddr(a1, RF_COPYALL, NULL); 175456678Seric (void) recipient(a1, sendq, e); 175551317Seric } 175658802Seric /* 175758802Seric ** DEQUOTE_INIT -- initialize dequote map 175858802Seric ** 175958802Seric ** This is a no-op. 176058802Seric ** 176158802Seric ** Parameters: 176258802Seric ** map -- the internal map structure. 176358802Seric ** args -- arguments. 176458802Seric ** 176558802Seric ** Returns: 176658802Seric ** TRUE. 176758802Seric */ 176858802Seric 176958802Seric bool 177060219Seric dequote_init(map, args) 177158802Seric MAP *map; 177258802Seric char *args; 177358802Seric { 177458805Seric register char *p = args; 177558805Seric 177658805Seric for (;;) 177758805Seric { 177858805Seric while (isascii(*p) && isspace(*p)) 177958805Seric p++; 178058805Seric if (*p != '-') 178158805Seric break; 178258805Seric switch (*++p) 178358805Seric { 178458805Seric case 'a': 178558805Seric map->map_app = ++p; 178658805Seric break; 178758805Seric } 178858805Seric while (*p != '\0' && !(isascii(*p) && isspace(*p))) 178958805Seric p++; 179058805Seric if (*p != '\0') 179158805Seric *p = '\0'; 179258805Seric } 179358805Seric if (map->map_app != NULL) 179458805Seric map->map_app = newstr(map->map_app); 179558805Seric 179658802Seric return TRUE; 179758802Seric } 179858802Seric /* 179958802Seric ** DEQUOTE_MAP -- unquote an address 180058802Seric ** 180158802Seric ** Parameters: 180258802Seric ** map -- the internal map structure (ignored). 180360089Seric ** name -- the name to dequote. 180458802Seric ** av -- arguments (ignored). 180559084Seric ** statp -- pointer to status out-parameter. 180658802Seric ** 180758802Seric ** Returns: 180858802Seric ** NULL -- if there were no quotes, or if the resulting 180958802Seric ** unquoted buffer would not be acceptable to prescan. 181058802Seric ** else -- The dequoted buffer. 181158802Seric */ 181258802Seric 181358802Seric char * 181460089Seric dequote_map(map, name, av, statp) 181558802Seric MAP *map; 181660089Seric char *name; 181758802Seric char **av; 181859084Seric int *statp; 181958802Seric { 182058802Seric register char *p; 182158802Seric register char *q; 182258802Seric register char c; 182358802Seric int anglecnt; 182458802Seric int cmntcnt; 182558802Seric int quotecnt; 182659089Seric int spacecnt; 182758802Seric bool quotemode; 182858802Seric bool bslashmode; 182958802Seric 183058802Seric anglecnt = 0; 183158802Seric cmntcnt = 0; 183258802Seric quotecnt = 0; 183359089Seric spacecnt = 0; 183458802Seric quotemode = FALSE; 183558802Seric bslashmode = FALSE; 183658802Seric 183760089Seric for (p = q = name; (c = *p++) != '\0'; ) 183858802Seric { 183958802Seric if (bslashmode) 184058802Seric { 184158802Seric bslashmode = FALSE; 184258802Seric *q++ = c; 184358802Seric continue; 184458802Seric } 184558802Seric 184658802Seric switch (c) 184758802Seric { 184858802Seric case '\\': 184958802Seric bslashmode = TRUE; 185058802Seric break; 185158802Seric 185258802Seric case '(': 185358802Seric cmntcnt++; 185458802Seric break; 185558802Seric 185658802Seric case ')': 185758802Seric if (cmntcnt-- <= 0) 185858802Seric return NULL; 185958802Seric break; 186059089Seric 186159089Seric case ' ': 186259089Seric spacecnt++; 186359089Seric break; 186458802Seric } 186558802Seric 186658802Seric if (cmntcnt > 0) 186758802Seric { 186858802Seric *q++ = c; 186958802Seric continue; 187058802Seric } 187158802Seric 187258802Seric switch (c) 187358802Seric { 187458802Seric case '"': 187558802Seric quotemode = !quotemode; 187658802Seric quotecnt++; 187758802Seric continue; 187858802Seric 187958802Seric case '<': 188058802Seric anglecnt++; 188158802Seric break; 188258802Seric 188358802Seric case '>': 188458802Seric if (anglecnt-- <= 0) 188558802Seric return NULL; 188658802Seric break; 188758802Seric } 188858802Seric *q++ = c; 188958802Seric } 189058802Seric 189158802Seric if (anglecnt != 0 || cmntcnt != 0 || bslashmode || 189259089Seric quotemode || quotecnt <= 0 || spacecnt != 0) 189358802Seric return NULL; 189458802Seric *q++ = '\0'; 189560089Seric return name; 189658802Seric } 1897