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*65056Seric static char sccsid[] = "@(#)parseaddr.c 8.21 (Berkeley) 12/10/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"); 37365015Seric returnnull: 37458333Seric if (delimptr != NULL) 37558333Seric *delimptr = p; 37659747Seric CurEnv->e_to = saveto; 3778078Seric return (NULL); 3788078Seric } 37915284Seric 38015284Seric /* squirrel it away */ 3818078Seric *q++ = c; 3828078Seric } 3838078Seric 3848078Seric /* read a new input character */ 3858078Seric c = *p++; 38657631Seric if (c == '\0') 38756764Seric { 38856764Seric /* diagnose and patch up bad syntax */ 38956764Seric if (state == QST) 39056764Seric { 39164767Seric usrerr("653 Unbalanced '\"'"); 39256764Seric c = '"'; 39356764Seric } 39456764Seric else if (cmntcnt > 0) 39556764Seric { 39664767Seric usrerr("653 Unbalanced '('"); 39756764Seric c = ')'; 39856764Seric } 39956764Seric else if (anglecnt > 0) 40056764Seric { 40156764Seric c = '>'; 40264767Seric usrerr("653 Unbalanced '<'"); 40356764Seric } 40456764Seric else 40556764Seric break; 40615284Seric 40756764Seric p--; 40856764Seric } 40957631Seric else if (c == delim && anglecnt <= 0 && 41057631Seric cmntcnt <= 0 && state != QST) 41157631Seric break; 41256764Seric 4138078Seric if (tTd(22, 101)) 4148078Seric printf("c=%c, s=%d; ", c, state); 4158078Seric 4163149Seric /* chew up special characters */ 4173149Seric *q = '\0'; 4183149Seric if (bslashmode) 4193149Seric { 42059105Seric bslashmode = FALSE; 42159105Seric 42224944Seric /* kludge \! for naive users */ 42358061Seric if (cmntcnt > 0) 42459105Seric { 42558061Seric c = NOCHAR; 42659105Seric continue; 42759105Seric } 42859105Seric else if (c != '!' || state == QST) 42959105Seric { 43056678Seric *q++ = '\\'; 43159105Seric continue; 43259105Seric } 4333149Seric } 43456678Seric 43556678Seric if (c == '\\') 4363149Seric { 4373149Seric bslashmode = TRUE; 4383149Seric } 43956678Seric else if (state == QST) 4408514Seric { 4418514Seric /* do nothing, just avoid next clauses */ 4428514Seric } 4438078Seric else if (c == '(') 4444100Seric { 4458078Seric cmntcnt++; 4468078Seric c = NOCHAR; 4474100Seric } 4488078Seric else if (c == ')') 4493149Seric { 4508078Seric if (cmntcnt <= 0) 4513149Seric { 45264767Seric usrerr("653 Unbalanced ')'"); 45363844Seric c = NOCHAR; 4543149Seric } 4558078Seric else 4568078Seric cmntcnt--; 4578078Seric } 4588078Seric else if (cmntcnt > 0) 4598078Seric c = NOCHAR; 4608423Seric else if (c == '<') 4618423Seric anglecnt++; 4628423Seric else if (c == '>') 4638423Seric { 4648423Seric if (anglecnt <= 0) 4658423Seric { 46664767Seric usrerr("653 Unbalanced '>'"); 46763844Seric c = NOCHAR; 4688423Seric } 46963844Seric else 47063844Seric anglecnt--; 4718423Seric } 47258050Seric else if (delim == ' ' && isascii(c) && isspace(c)) 47311423Seric c = ' '; 4743149Seric 4758078Seric if (c == NOCHAR) 4768078Seric continue; 4773149Seric 4788078Seric /* see if this is end of input */ 47911405Seric if (c == delim && anglecnt <= 0 && state != QST) 4803149Seric break; 4813149Seric 4828078Seric newstate = StateTab[state][toktype(c)]; 4838078Seric if (tTd(22, 101)) 4848078Seric printf("ns=%02o\n", newstate); 4858078Seric state = newstate & TYPE; 4868078Seric if (bitset(M, newstate)) 4878078Seric c = NOCHAR; 4888078Seric if (bitset(B, newstate)) 4894228Seric break; 490297Seric } 4913149Seric 4923149Seric /* new token */ 4938078Seric if (tok != q) 4941378Seric { 4958078Seric *q++ = '\0'; 4968078Seric if (tTd(22, 36)) 497297Seric { 4988078Seric printf("tok="); 4998078Seric xputs(tok); 50023109Seric (void) putchar('\n'); 501297Seric } 5028078Seric if (avp >= &av[MAXATOM]) 503297Seric { 50458151Seric syserr("553 prescan: too many tokens"); 50565015Seric goto returnnull; 506297Seric } 50765015Seric if (q - tok > MAXNAME) 50865015Seric { 50965015Seric syserr("553 prescan: token too long"); 51065015Seric goto returnnull; 51165015Seric } 5128078Seric *avp++ = tok; 513297Seric } 5148423Seric } while (c != '\0' && (c != delim || anglecnt > 0)); 5153149Seric *avp = NULL; 51658333Seric p--; 51758333Seric if (delimptr != NULL) 51858333Seric *delimptr = p; 51956764Seric if (tTd(22, 12)) 52056764Seric { 52156764Seric printf("prescan==>"); 52256764Seric printav(av); 52356764Seric } 52459747Seric CurEnv->e_to = saveto; 52558546Seric if (av[0] == NULL) 52664966Seric { 52764966Seric if (tTd(22, 1)) 52864966Seric printf("prescan: null leading token\n"); 52958546Seric return (NULL); 53064966Seric } 53158403Seric return (av); 5323149Seric } 5333149Seric /* 5343149Seric ** TOKTYPE -- return token type 5353149Seric ** 5363149Seric ** Parameters: 5373149Seric ** c -- the character in question. 5383149Seric ** 5393149Seric ** Returns: 5403149Seric ** Its type. 5413149Seric ** 5423149Seric ** Side Effects: 5433149Seric ** none. 5443149Seric */ 545297Seric 5463149Seric toktype(c) 54758050Seric register int c; 5483149Seric { 5493380Seric static char buf[50]; 5503382Seric static bool firstime = TRUE; 5513380Seric 5523382Seric if (firstime) 5533380Seric { 5543382Seric firstime = FALSE; 55558050Seric expand("\201o", buf, &buf[sizeof buf - 1], CurEnv); 5567005Seric (void) strcat(buf, DELIMCHARS); 5573380Seric } 55858050Seric c &= 0377; 55956327Seric if (c == MATCHCLASS || c == MATCHREPL || c == MATCHNCLASS) 5608078Seric return (ONE); 56159027Seric if (c == MACRODEXPAND) 56259027Seric return (ONE); 5638078Seric if (c == '"') 5648078Seric return (QST); 56558050Seric if ((c & 0340) == 0200) 56658050Seric return (OPR); 5674100Seric if (!isascii(c)) 5688078Seric return (ATM); 5698078Seric if (isspace(c) || c == ')') 5708078Seric return (SPC); 57158050Seric if (strchr(buf, c) != NULL) 5728078Seric return (OPR); 5738078Seric return (ATM); 5743149Seric } 5753149Seric /* 5763149Seric ** REWRITE -- apply rewrite rules to token vector. 5773149Seric ** 5784476Seric ** This routine is an ordered production system. Each rewrite 5794476Seric ** rule has a LHS (called the pattern) and a RHS (called the 5804476Seric ** rewrite); 'rwr' points the the current rewrite rule. 5814476Seric ** 5824476Seric ** For each rewrite rule, 'avp' points the address vector we 5834476Seric ** are trying to match against, and 'pvp' points to the pattern. 5848058Seric ** If pvp points to a special match value (MATCHZANY, MATCHANY, 5859585Seric ** MATCHONE, MATCHCLASS, MATCHNCLASS) then the address in avp 5869585Seric ** matched is saved away in the match vector (pointed to by 'mvp'). 5874476Seric ** 5884476Seric ** When a match between avp & pvp does not match, we try to 5899585Seric ** back out. If we back up over MATCHONE, MATCHCLASS, or MATCHNCLASS 5904476Seric ** we must also back out the match in mvp. If we reach a 5918058Seric ** MATCHANY or MATCHZANY we just extend the match and start 5928058Seric ** over again. 5934476Seric ** 5944476Seric ** When we finally match, we rewrite the address vector 5954476Seric ** and try over again. 5964476Seric ** 5973149Seric ** Parameters: 5983149Seric ** pvp -- pointer to token vector. 59959027Seric ** ruleset -- the ruleset to use for rewriting. 60059027Seric ** e -- the current envelope. 6013149Seric ** 6023149Seric ** Returns: 60359084Seric ** A status code. If EX_TEMPFAIL, higher level code should 60459084Seric ** attempt recovery. 6053149Seric ** 6063149Seric ** Side Effects: 6073149Seric ** pvp is modified. 6083149Seric */ 6092091Seric 6103149Seric struct match 6113149Seric { 6124468Seric char **first; /* first token matched */ 6134468Seric char **last; /* last token matched */ 61458825Seric char **pattern; /* pointer to pattern */ 6153149Seric }; 6163149Seric 6174468Seric # define MAXMATCH 9 /* max params per rewrite */ 6183149Seric 6193149Seric 62059084Seric int 62159027Seric rewrite(pvp, ruleset, e) 6223149Seric char **pvp; 6234070Seric int ruleset; 62459027Seric register ENVELOPE *e; 6253149Seric { 6263149Seric register char *ap; /* address pointer */ 6273149Seric register char *rp; /* rewrite pointer */ 6283149Seric register char **avp; /* address vector pointer */ 6293149Seric register char **rvp; /* rewrite vector pointer */ 6308058Seric register struct match *mlp; /* cur ptr into mlist */ 6318058Seric register struct rewrite *rwr; /* pointer to current rewrite rule */ 63258866Seric int ruleno; /* current rule number */ 63359084Seric int rstat = EX_OK; /* return status */ 63464740Seric int loopcount; 63556678Seric struct match mlist[MAXMATCH]; /* stores match on LHS */ 6363149Seric char *npvp[MAXATOM+1]; /* temporary space for rebuild */ 6373149Seric 6389279Seric if (OpMode == MD_TEST || tTd(21, 2)) 6393149Seric { 6408959Seric printf("rewrite: ruleset %2d input:", ruleset); 64156678Seric printav(pvp); 6423149Seric } 64356678Seric if (ruleset < 0 || ruleset >= MAXRWSETS) 64456326Seric { 64558151Seric syserr("554 rewrite: illegal ruleset number %d", ruleset); 64659084Seric return EX_CONFIG; 64756326Seric } 64856678Seric if (pvp == NULL) 64959084Seric return EX_USAGE; 65056326Seric 6513149Seric /* 65256678Seric ** Run through the list of rewrite rules, applying 65356678Seric ** any that match. 6543149Seric */ 6553149Seric 65658866Seric ruleno = 1; 65764740Seric loopcount = 0; 6584070Seric for (rwr = RewriteRules[ruleset]; rwr != NULL; ) 6593149Seric { 6607675Seric if (tTd(21, 12)) 661297Seric { 6628069Seric printf("-----trying rule:"); 66356678Seric printav(rwr->r_lhs); 6643149Seric } 6653149Seric 6663149Seric /* try to match on this rule */ 6674468Seric mlp = mlist; 6688058Seric rvp = rwr->r_lhs; 6698058Seric avp = pvp; 67058866Seric if (++loopcount > 100) 6713149Seric { 67258866Seric syserr("554 Infinite loop in ruleset %d, rule %d", 67358866Seric ruleset, ruleno); 67458866Seric if (tTd(21, 1)) 67552637Seric { 67656678Seric printf("workspace: "); 67756678Seric printav(pvp); 67852637Seric } 67958866Seric break; 68058866Seric } 68158866Seric 68258866Seric while ((ap = *avp) != NULL || *rvp != NULL) 68358866Seric { 6843149Seric rp = *rvp; 6858058Seric if (tTd(21, 35)) 6868058Seric { 68758825Seric printf("ADVANCE rp="); 68857531Seric xputs(rp); 68957532Seric printf(", ap="); 6908058Seric xputs(ap); 6918069Seric printf("\n"); 6928058Seric } 69356678Seric if (rp == NULL) 69456326Seric { 6953149Seric /* end-of-pattern before end-of-address */ 6968058Seric goto backup; 69756678Seric } 69858173Seric if (ap == NULL && (*rp & 0377) != MATCHZANY && 69958827Seric (*rp & 0377) != MATCHZERO) 70056326Seric { 70158825Seric /* end-of-input with patterns left */ 70258825Seric goto backup; 703297Seric } 70456326Seric 70558050Seric switch (*rp & 0377) 7068058Seric { 70756678Seric register STAB *s; 70858814Seric char buf[MAXLINE]; 70956326Seric 71056678Seric case MATCHCLASS: 71158825Seric /* match any phrase in a class */ 71258825Seric mlp->pattern = rvp; 71358814Seric mlp->first = avp; 71458814Seric extendclass: 71558825Seric ap = *avp; 71658825Seric if (ap == NULL) 71758814Seric goto backup; 71858814Seric mlp->last = avp++; 71958814Seric cataddr(mlp->first, mlp->last, buf, sizeof buf, '\0'); 72058814Seric s = stab(buf, ST_CLASS, ST_FIND); 72156678Seric if (s == NULL || !bitnset(rp[1], s->s_class)) 72256326Seric { 72358825Seric if (tTd(21, 36)) 72458825Seric { 72558825Seric printf("EXTEND rp="); 72658825Seric xputs(rp); 72758825Seric printf(", ap="); 72858825Seric xputs(ap); 72958825Seric printf("\n"); 73058825Seric } 73158825Seric goto extendclass; 73256326Seric } 73358825Seric if (tTd(21, 36)) 73458825Seric printf("CLMATCH\n"); 73558814Seric mlp++; 73658814Seric break; 7374060Seric 73858825Seric case MATCHNCLASS: 73958825Seric /* match any token not in a class */ 74058825Seric s = stab(ap, ST_CLASS, ST_FIND); 74158825Seric if (s != NULL && bitnset(rp[1], s->s_class)) 74258825Seric goto backup; 74358825Seric 74458825Seric /* fall through */ 74558825Seric 74656678Seric case MATCHONE: 74756678Seric case MATCHANY: 74856678Seric /* match exactly one token */ 74958825Seric mlp->pattern = rvp; 75056678Seric mlp->first = avp; 75156678Seric mlp->last = avp++; 7528058Seric mlp++; 75356678Seric break; 7548058Seric 75556678Seric case MATCHZANY: 75656678Seric /* match zero or more tokens */ 75758825Seric mlp->pattern = rvp; 75856678Seric mlp->first = avp; 75956678Seric mlp->last = avp - 1; 76056678Seric mlp++; 76156678Seric break; 76256326Seric 76358827Seric case MATCHZERO: 76458173Seric /* match zero tokens */ 76558173Seric break; 76658173Seric 76759027Seric case MACRODEXPAND: 76859027Seric /* 76959027Seric ** Match against run-time macro. 77059027Seric ** This algorithm is broken for the 77159027Seric ** general case (no recursive macros, 77259027Seric ** improper tokenization) but should 77359027Seric ** work for the usual cases. 77459027Seric */ 77559027Seric 77659027Seric ap = macvalue(rp[1], e); 77759027Seric mlp->first = avp; 77859027Seric if (tTd(21, 2)) 77959027Seric printf("rewrite: LHS $&%c => \"%s\"\n", 78059027Seric rp[1], 78159027Seric ap == NULL ? "(NULL)" : ap); 78259027Seric 78359027Seric if (ap == NULL) 78459027Seric break; 78560502Seric while (*ap != '\0') 78659027Seric { 78759027Seric if (*avp == NULL || 78859027Seric strncasecmp(ap, *avp, strlen(*avp)) != 0) 78959027Seric { 79059027Seric /* no match */ 79159027Seric avp = mlp->first; 79259027Seric goto backup; 79359027Seric } 79459027Seric ap += strlen(*avp++); 79559027Seric } 79659027Seric 79759027Seric /* match */ 79859027Seric break; 79959027Seric 80056678Seric default: 80156678Seric /* must have exact match */ 80256678Seric if (strcasecmp(rp, ap)) 8038058Seric goto backup; 8044468Seric avp++; 80556678Seric break; 8063149Seric } 8073149Seric 80856678Seric /* successful match on this token */ 8093149Seric rvp++; 8103149Seric continue; 8113149Seric 81258825Seric backup: 81356678Seric /* match failed -- back up */ 81458825Seric while (--mlp >= mlist) 8153149Seric { 81658825Seric rvp = mlp->pattern; 81756678Seric rp = *rvp; 81858825Seric avp = mlp->last + 1; 81958825Seric ap = *avp; 82058825Seric 82158825Seric if (tTd(21, 36)) 82258825Seric { 82358825Seric printf("BACKUP rp="); 82458825Seric xputs(rp); 82558825Seric printf(", ap="); 82658825Seric xputs(ap); 82758825Seric printf("\n"); 82858825Seric } 82958825Seric 83058825Seric if (ap == NULL) 83158825Seric { 83258825Seric /* run off the end -- back up again */ 83358825Seric continue; 83458825Seric } 83558050Seric if ((*rp & 0377) == MATCHANY || 83658050Seric (*rp & 0377) == MATCHZANY) 8374468Seric { 83856678Seric /* extend binding and continue */ 83958825Seric mlp->last = avp++; 84056678Seric rvp++; 84158825Seric mlp++; 84256678Seric break; 8434468Seric } 84458825Seric if ((*rp & 0377) == MATCHCLASS) 84556678Seric { 84658814Seric /* extend binding and try again */ 84763397Seric mlp->last = avp; 84858814Seric goto extendclass; 84958814Seric } 8503149Seric } 8513149Seric 85258825Seric if (mlp < mlist) 85356678Seric { 85456678Seric /* total failure to match */ 85556326Seric break; 8563149Seric } 857297Seric } 8583149Seric 8593149Seric /* 86056678Seric ** See if we successfully matched 8613149Seric */ 8623149Seric 86358827Seric if (mlp < mlist || *rvp != NULL) 8643149Seric { 8659374Seric if (tTd(21, 10)) 8669374Seric printf("----- rule fails\n"); 8679374Seric rwr = rwr->r_next; 86858866Seric ruleno++; 86964740Seric loopcount = 0; 8709374Seric continue; 8719374Seric } 8723149Seric 8739374Seric rvp = rwr->r_rhs; 8749374Seric if (tTd(21, 12)) 8759374Seric { 8769374Seric printf("-----rule matches:"); 87756678Seric printav(rvp); 8789374Seric } 8799374Seric 8809374Seric rp = *rvp; 88158050Seric if ((*rp & 0377) == CANONUSER) 8829374Seric { 8839374Seric rvp++; 8849374Seric rwr = rwr->r_next; 88558866Seric ruleno++; 88664740Seric loopcount = 0; 8879374Seric } 88858050Seric else if ((*rp & 0377) == CANONHOST) 8899374Seric { 8909374Seric rvp++; 8919374Seric rwr = NULL; 8929374Seric } 89358050Seric else if ((*rp & 0377) == CANONNET) 8949374Seric rwr = NULL; 8959374Seric 8969374Seric /* substitute */ 8979374Seric for (avp = npvp; *rvp != NULL; rvp++) 8989374Seric { 8999374Seric register struct match *m; 9009374Seric register char **pp; 9019374Seric 9028058Seric rp = *rvp; 90358050Seric if ((*rp & 0377) == MATCHREPL) 9048058Seric { 90516914Seric /* substitute from LHS */ 90616914Seric m = &mlist[rp[1] - '1']; 90756678Seric if (m < mlist || m >= mlp) 9089374Seric { 90958151Seric syserr("554 rewrite: ruleset %d: replacement $%c out of bounds", 91056326Seric ruleset, rp[1]); 91159084Seric return EX_CONFIG; 9129374Seric } 91316914Seric if (tTd(21, 15)) 91416914Seric { 91516914Seric printf("$%c:", rp[1]); 91616914Seric pp = m->first; 91756678Seric while (pp <= m->last) 91816914Seric { 91916914Seric printf(" %x=\"", *pp); 92016914Seric (void) fflush(stdout); 92116914Seric printf("%s\"", *pp++); 92216914Seric } 92316914Seric printf("\n"); 92416914Seric } 9259374Seric pp = m->first; 92656678Seric while (pp <= m->last) 9273149Seric { 92816914Seric if (avp >= &npvp[MAXATOM]) 92956678Seric { 93058151Seric syserr("554 rewrite: expansion too long"); 93159084Seric return EX_DATAERR; 93256678Seric } 93316914Seric *avp++ = *pp++; 9343149Seric } 9353149Seric } 93616914Seric else 9378226Seric { 93816914Seric /* vanilla replacement */ 9399374Seric if (avp >= &npvp[MAXATOM]) 94016889Seric { 94156678Seric toolong: 94258151Seric syserr("554 rewrite: expansion too long"); 94359084Seric return EX_DATAERR; 94416889Seric } 94559027Seric if ((*rp & 0377) != MACRODEXPAND) 94659027Seric *avp++ = rp; 94759027Seric else 94859027Seric { 94959027Seric *avp = macvalue(rp[1], e); 95059027Seric if (tTd(21, 2)) 95159027Seric printf("rewrite: RHS $&%c => \"%s\"\n", 95259027Seric rp[1], 95359027Seric *avp == NULL ? "(NULL)" : *avp); 95459027Seric if (*avp != NULL) 95559027Seric avp++; 95659027Seric } 9578226Seric } 9589374Seric } 9599374Seric *avp++ = NULL; 96016914Seric 96116914Seric /* 96256678Seric ** Check for any hostname/keyword lookups. 96316914Seric */ 96416914Seric 96516914Seric for (rvp = npvp; *rvp != NULL; rvp++) 96616914Seric { 96756678Seric char **hbrvp; 96816914Seric char **xpvp; 96916914Seric int trsize; 97056678Seric char *replac; 97156678Seric int endtoken; 97256678Seric STAB *map; 97356678Seric char *mapname; 97456678Seric char **key_rvp; 97556678Seric char **arg_rvp; 97656678Seric char **default_rvp; 97756678Seric char buf[MAXNAME + 1]; 97816914Seric char *pvpb1[MAXATOM + 1]; 97956823Seric char *argvect[10]; 98017174Seric char pvpbuf[PSBUFSIZE]; 98164404Seric char *nullpvp[1]; 98216914Seric 98358050Seric if ((**rvp & 0377) != HOSTBEGIN && 98458050Seric (**rvp & 0377) != LOOKUPBEGIN) 98516914Seric continue; 98616914Seric 98716914Seric /* 98856678Seric ** Got a hostname/keyword lookup. 98916914Seric ** 99016914Seric ** This could be optimized fairly easily. 99116914Seric */ 99216914Seric 99316914Seric hbrvp = rvp; 99458050Seric if ((**rvp & 0377) == HOSTBEGIN) 99556327Seric { 99656678Seric endtoken = HOSTEND; 99756678Seric mapname = "host"; 99856327Seric } 99956326Seric else 100056327Seric { 100156678Seric endtoken = LOOKUPEND; 100256678Seric mapname = *++rvp; 100356327Seric } 100456678Seric map = stab(mapname, ST_MAP, ST_FIND); 100556678Seric if (map == NULL) 100658151Seric syserr("554 rewrite: map %s not found", mapname); 100756678Seric 100856678Seric /* extract the match part */ 100956678Seric key_rvp = ++rvp; 101056823Seric default_rvp = NULL; 101156823Seric arg_rvp = argvect; 101256823Seric xpvp = NULL; 101356823Seric replac = pvpbuf; 101458050Seric while (*rvp != NULL && (**rvp & 0377) != endtoken) 101553654Seric { 101658050Seric int nodetype = **rvp & 0377; 101756823Seric 101856823Seric if (nodetype != CANONHOST && nodetype != CANONUSER) 101956678Seric { 102056823Seric rvp++; 102156823Seric continue; 102256823Seric } 102356823Seric 102456823Seric *rvp++ = NULL; 102556823Seric 102656823Seric if (xpvp != NULL) 102756823Seric { 102858814Seric cataddr(xpvp, NULL, replac, 102958082Seric &pvpbuf[sizeof pvpbuf] - replac, 103058082Seric '\0'); 103156823Seric *++arg_rvp = replac; 103256823Seric replac += strlen(replac) + 1; 103356823Seric xpvp = NULL; 103456823Seric } 103556823Seric switch (nodetype) 103656823Seric { 103756678Seric case CANONHOST: 103856823Seric xpvp = rvp; 103956678Seric break; 104056678Seric 104156678Seric case CANONUSER: 104256678Seric default_rvp = rvp; 104356678Seric break; 104456678Seric } 104553654Seric } 104616914Seric if (*rvp != NULL) 104716914Seric *rvp++ = NULL; 104856823Seric if (xpvp != NULL) 104956823Seric { 105058814Seric cataddr(xpvp, NULL, replac, 105158082Seric &pvpbuf[sizeof pvpbuf] - replac, 105258082Seric '\0'); 105356823Seric *++arg_rvp = replac; 105456823Seric } 105556823Seric *++arg_rvp = NULL; 105616914Seric 105716914Seric /* save the remainder of the input string */ 105816914Seric trsize = (int) (avp - rvp + 1) * sizeof *rvp; 105916914Seric bcopy((char *) rvp, (char *) pvpb1, trsize); 106016914Seric 106156678Seric /* look it up */ 106258814Seric cataddr(key_rvp, NULL, buf, sizeof buf, '\0'); 106356823Seric argvect[0] = buf; 106460538Seric if (map != NULL && bitset(MF_OPEN, map->s_map.map_mflags)) 106556836Seric { 106659084Seric auto int stat = EX_OK; 106756836Seric 106860215Seric /* XXX should try to auto-open the map here */ 106960215Seric 107058796Seric if (tTd(60, 1)) 107158796Seric printf("map_lookup(%s, %s) => ", 107258796Seric mapname, buf); 107356836Seric replac = (*map->s_map.map_class->map_lookup)(&map->s_map, 107460089Seric buf, argvect, &stat); 107558796Seric if (tTd(60, 1)) 107659084Seric printf("%s (%d)\n", 107759084Seric replac ? replac : "NOT FOUND", 107859084Seric stat); 107959084Seric 108059084Seric /* should recover if stat == EX_TEMPFAIL */ 108159084Seric if (stat == EX_TEMPFAIL) 108259084Seric rstat = stat; 108356836Seric } 108453654Seric else 108556678Seric replac = NULL; 108656678Seric 108756678Seric /* if no replacement, use default */ 108856823Seric if (replac == NULL && default_rvp != NULL) 108956823Seric { 109060089Seric /* create the default */ 109160089Seric cataddr(default_rvp, NULL, buf, sizeof buf, '\0'); 109256823Seric replac = buf; 109356823Seric } 109456823Seric 109556678Seric if (replac == NULL) 109651317Seric { 109756823Seric xpvp = key_rvp; 109853654Seric } 109964404Seric else if (*replac == '\0') 110064404Seric { 110164404Seric /* null replacement */ 110264404Seric nullpvp[0] = NULL; 110364404Seric xpvp = nullpvp; 110464404Seric } 110556678Seric else 110653654Seric { 110756678Seric /* scan the new replacement */ 110858333Seric xpvp = prescan(replac, '\0', pvpbuf, NULL); 110953654Seric if (xpvp == NULL) 111051317Seric { 111158403Seric /* prescan already printed error */ 111259084Seric return EX_DATAERR; 111356678Seric } 111451317Seric } 111551317Seric 111616914Seric /* append it to the token list */ 111756678Seric for (avp = hbrvp; *xpvp != NULL; xpvp++) 111856678Seric { 111917174Seric *avp++ = newstr(*xpvp); 112016920Seric if (avp >= &npvp[MAXATOM]) 112116914Seric goto toolong; 112217174Seric } 112316914Seric 112416914Seric /* restore the old trailing information */ 112556678Seric for (xpvp = pvpb1; (*avp++ = *xpvp++) != NULL; ) 112616920Seric if (avp >= &npvp[MAXATOM]) 112716914Seric goto toolong; 112817174Seric 112956678Seric break; 113016914Seric } 113116914Seric 113216914Seric /* 113316914Seric ** Check for subroutine calls. 113416914Seric */ 113516914Seric 113658050Seric if (*npvp != NULL && (**npvp & 0377) == CALLSUBR) 113756678Seric { 113859084Seric int stat; 113959084Seric 114056678Seric bcopy((char *) &npvp[2], (char *) pvp, 114156678Seric (int) (avp - npvp - 2) * sizeof *avp); 114256678Seric if (tTd(21, 3)) 114356678Seric printf("-----callsubr %s\n", npvp[1]); 114459084Seric stat = rewrite(pvp, atoi(npvp[1]), e); 114559084Seric if (rstat == EX_OK || stat == EX_TEMPFAIL) 114659084Seric rstat = stat; 1147*65056Seric if (*pvp != NULL && (**pvp & 0377) == CANONNET) 114863581Seric rwr = NULL; 114956678Seric } 115056678Seric else 115156678Seric { 115217348Seric bcopy((char *) npvp, (char *) pvp, 115316900Seric (int) (avp - npvp) * sizeof *avp); 115456678Seric } 11559374Seric if (tTd(21, 4)) 11569374Seric { 11579374Seric printf("rewritten as:"); 115856678Seric printav(pvp); 11599374Seric } 1160297Seric } 11618069Seric 11629279Seric if (OpMode == MD_TEST || tTd(21, 2)) 11638069Seric { 11648959Seric printf("rewrite: ruleset %2d returns:", ruleset); 116556678Seric printav(pvp); 11668069Seric } 116759084Seric 116859084Seric return rstat; 11693149Seric } 11703149Seric /* 11713149Seric ** BUILDADDR -- build address from token vector. 11723149Seric ** 11733149Seric ** Parameters: 11743149Seric ** tv -- token vector. 11753149Seric ** a -- pointer to address descriptor to fill. 11763149Seric ** If NULL, one will be allocated. 117764284Seric ** flags -- info regarding whether this is a sender or 117864284Seric ** a recipient. 117958966Seric ** e -- the current envelope. 11803149Seric ** 11813149Seric ** Returns: 11824279Seric ** NULL if there was an error. 11834279Seric ** 'a' otherwise. 11843149Seric ** 11853149Seric ** Side Effects: 11863149Seric ** fills in 'a' 11873149Seric */ 11883149Seric 118957249Seric struct errcodes 119057249Seric { 119157249Seric char *ec_name; /* name of error code */ 119257249Seric int ec_code; /* numeric code */ 119357249Seric } ErrorCodes[] = 119457249Seric { 119557249Seric "usage", EX_USAGE, 119657249Seric "nouser", EX_NOUSER, 119757249Seric "nohost", EX_NOHOST, 119857249Seric "unavailable", EX_UNAVAILABLE, 119957249Seric "software", EX_SOFTWARE, 120057249Seric "tempfail", EX_TEMPFAIL, 120157249Seric "protocol", EX_PROTOCOL, 120257249Seric #ifdef EX_CONFIG 120357249Seric "config", EX_CONFIG, 120457249Seric #endif 120557249Seric NULL, EX_UNAVAILABLE, 120657249Seric }; 120757249Seric 120856678Seric ADDRESS * 120964284Seric buildaddr(tv, a, flags, e) 12103149Seric register char **tv; 12113149Seric register ADDRESS *a; 121264284Seric int flags; 121358966Seric register ENVELOPE *e; 12143149Seric { 12153149Seric struct mailer **mp; 12163149Seric register struct mailer *m; 121758008Seric char *bp; 121858008Seric int spaceleft; 121964306Seric static MAILER errormailer; 122064306Seric static char *errorargv[] = { "ERROR", NULL }; 122157402Seric static char buf[MAXNAME]; 12223149Seric 122364791Seric if (tTd(24, 5)) 122464791Seric { 122564791Seric printf("buildaddr, flags=%o, tv=", flags); 122664791Seric printav(tv); 122764791Seric } 122864791Seric 12293149Seric if (a == NULL) 12303149Seric a = (ADDRESS *) xalloc(sizeof *a); 123116889Seric bzero((char *) a, sizeof *a); 12323149Seric 12333149Seric /* figure out what net/mailer to use */ 123464306Seric if (*tv == NULL || (**tv & 0377) != CANONNET) 12354279Seric { 123658151Seric syserr("554 buildaddr: no net"); 123764306Seric badaddr: 123864306Seric a->q_flags |= QBADADDR; 123964306Seric a->q_mailer = &errormailer; 124064306Seric if (errormailer.m_name == NULL) 124164306Seric { 124264306Seric /* initialize the bogus mailer */ 124364306Seric errormailer.m_name = "*error*"; 124464306Seric errormailer.m_mailer = "ERROR"; 124564306Seric errormailer.m_argv = errorargv; 124664306Seric } 124764306Seric return a; 12484279Seric } 12493149Seric tv++; 125058680Seric if (strcasecmp(*tv, "error") == 0) 12514279Seric { 125258050Seric if ((**++tv & 0377) == CANONHOST) 125310183Seric { 125457249Seric register struct errcodes *ep; 125557249Seric 125658050Seric if (isascii(**++tv) && isdigit(**tv)) 125757249Seric { 125857249Seric setstat(atoi(*tv)); 125957249Seric } 126057249Seric else 126157249Seric { 126257249Seric for (ep = ErrorCodes; ep->ec_name != NULL; ep++) 126357249Seric if (strcasecmp(ep->ec_name, *tv) == 0) 126457249Seric break; 126557249Seric setstat(ep->ec_code); 126657249Seric } 126710183Seric tv++; 126810183Seric } 126964928Seric else 127064928Seric setstat(EX_UNAVAILABLE); 127158050Seric if ((**tv & 0377) != CANONUSER) 127258151Seric syserr("554 buildaddr: error: no user"); 127358814Seric cataddr(++tv, NULL, buf, sizeof buf, ' '); 127458082Seric stripquotes(buf); 127564659Seric if (isascii(buf[0]) && isdigit(buf[0]) && 127664659Seric isascii(buf[1]) && isdigit(buf[1]) && 127764659Seric isascii(buf[2]) && isdigit(buf[2]) && 127864659Seric buf[3] == ' ') 127964659Seric { 128064659Seric char fmt[10]; 128164659Seric 128264659Seric strncpy(fmt, buf, 3); 128364659Seric strcpy(&fmt[3], " %s"); 128464659Seric usrerr(fmt, buf + 4); 128564659Seric } 128664659Seric else 128764659Seric { 128864659Seric usrerr("%s", buf); 128964659Seric } 129064306Seric goto badaddr; 12914279Seric } 129257402Seric 12934598Seric for (mp = Mailer; (m = *mp++) != NULL; ) 12943149Seric { 129558680Seric if (strcasecmp(m->m_name, *tv) == 0) 12963149Seric break; 12973149Seric } 12983149Seric if (m == NULL) 12994279Seric { 130058151Seric syserr("554 buildaddr: unknown mailer %s", *tv); 130164306Seric goto badaddr; 13024279Seric } 13034598Seric a->q_mailer = m; 13043149Seric 13053149Seric /* figure out what host (if any) */ 130656678Seric tv++; 130758509Seric if ((**tv & 0377) == CANONHOST) 13083149Seric { 130958008Seric bp = buf; 131058008Seric spaceleft = sizeof buf - 1; 131158050Seric while (*++tv != NULL && (**tv & 0377) != CANONUSER) 131258008Seric { 131358008Seric int i = strlen(*tv); 131458008Seric 131558008Seric if (i > spaceleft) 131658008Seric { 131758008Seric /* out of space for this address */ 131858008Seric if (spaceleft >= 0) 131958151Seric syserr("554 buildaddr: host too long (%.40s...)", 132058008Seric buf); 132158008Seric i = spaceleft; 132258008Seric spaceleft = 0; 132358008Seric } 132458008Seric if (i <= 0) 132558008Seric continue; 132658008Seric bcopy(*tv, bp, i); 132758008Seric bp += i; 132858008Seric spaceleft -= i; 132958008Seric } 133058010Seric *bp = '\0'; 13315704Seric a->q_host = newstr(buf); 13323149Seric } 133357249Seric else 133458509Seric { 133558509Seric if (!bitnset(M_LOCALMAILER, m->m_flags)) 133658509Seric { 133758509Seric syserr("554 buildaddr: no host"); 133864306Seric goto badaddr; 133958509Seric } 134057249Seric a->q_host = NULL; 134158509Seric } 13423149Seric 13433149Seric /* figure out the user */ 134458050Seric if (*tv == NULL || (**tv & 0377) != CANONUSER) 13454279Seric { 134658151Seric syserr("554 buildaddr: no user"); 134764306Seric goto badaddr; 13484279Seric } 134957402Seric tv++; 135051317Seric 135157402Seric /* do special mapping for local mailer */ 135257402Seric if (m == LocalMailer && *tv != NULL) 135357402Seric { 135457454Seric register char *p = *tv; 135557454Seric 135657454Seric if (*p == '"') 135757454Seric p++; 135857454Seric if (*p == '|') 135957402Seric a->q_mailer = m = ProgMailer; 136057454Seric else if (*p == '/') 136157402Seric a->q_mailer = m = FileMailer; 136257454Seric else if (*p == ':') 136357402Seric { 136457402Seric /* may be :include: */ 136558814Seric cataddr(tv, NULL, buf, sizeof buf, '\0'); 136658008Seric stripquotes(buf); 136758008Seric if (strncasecmp(buf, ":include:", 9) == 0) 136858008Seric { 136958008Seric /* if :include:, don't need further rewriting */ 137057402Seric a->q_mailer = m = InclMailer; 137158008Seric a->q_user = &buf[9]; 137258008Seric return (a); 137358008Seric } 137457402Seric } 137557402Seric } 137657402Seric 137758008Seric if (m == LocalMailer && *tv != NULL && strcmp(*tv, "@") == 0) 137858008Seric { 137958008Seric tv++; 138058008Seric a->q_flags |= QNOTREMOTE; 138158008Seric } 138258008Seric 138364284Seric /* rewrite according recipient mailer rewriting rules */ 138464284Seric define('h', a->q_host, e); 138564284Seric if (!bitset(RF_SENDERADDR|RF_HEADERADDR, flags)) 138664284Seric { 138764284Seric /* sender addresses done later */ 138864284Seric (void) rewrite(tv, 2, e); 138964284Seric if (m->m_re_rwset > 0) 139064284Seric (void) rewrite(tv, m->m_re_rwset, e); 139164284Seric } 139259084Seric (void) rewrite(tv, 4, e); 139319040Seric 139419040Seric /* save the result for the command line/RCPT argument */ 139558814Seric cataddr(tv, NULL, buf, sizeof buf, '\0'); 13963149Seric a->q_user = buf; 13973149Seric 139858670Seric /* 139958670Seric ** Do mapping to lower case as requested by mailer 140058670Seric */ 140158670Seric 140258670Seric if (a->q_host != NULL && !bitnset(M_HST_UPPER, m->m_flags)) 140358670Seric makelower(a->q_host); 140458670Seric if (!bitnset(M_USR_UPPER, m->m_flags)) 140558670Seric makelower(a->q_user); 140658670Seric 14073149Seric return (a); 14083149Seric } 14093188Seric /* 14104228Seric ** CATADDR -- concatenate pieces of addresses (putting in <LWSP> subs) 14114228Seric ** 14124228Seric ** Parameters: 14134228Seric ** pvp -- parameter vector to rebuild. 141458814Seric ** evp -- last parameter to include. Can be NULL to 141558814Seric ** use entire pvp. 14164228Seric ** buf -- buffer to build the string into. 14174228Seric ** sz -- size of buf. 141858082Seric ** spacesub -- the space separator character; if null, 141958082Seric ** use SpaceSub. 14204228Seric ** 14214228Seric ** Returns: 14224228Seric ** none. 14234228Seric ** 14244228Seric ** Side Effects: 14254228Seric ** Destroys buf. 14264228Seric */ 14274228Seric 142858814Seric cataddr(pvp, evp, buf, sz, spacesub) 14294228Seric char **pvp; 143058814Seric char **evp; 14314228Seric char *buf; 14324228Seric register int sz; 143358082Seric char spacesub; 14344228Seric { 14354228Seric bool oatomtok = FALSE; 143656678Seric bool natomtok = FALSE; 14374228Seric register int i; 14384228Seric register char *p; 14394228Seric 144058082Seric if (spacesub == '\0') 144158082Seric spacesub = SpaceSub; 144258082Seric 14438423Seric if (pvp == NULL) 14448423Seric { 144523109Seric (void) strcpy(buf, ""); 14468423Seric return; 14478423Seric } 14484228Seric p = buf; 144911156Seric sz -= 2; 14504228Seric while (*pvp != NULL && (i = strlen(*pvp)) < sz) 14514228Seric { 14528078Seric natomtok = (toktype(**pvp) == ATM); 14534228Seric if (oatomtok && natomtok) 145458082Seric *p++ = spacesub; 14554228Seric (void) strcpy(p, *pvp); 14564228Seric oatomtok = natomtok; 14574228Seric p += i; 145811156Seric sz -= i + 1; 145958814Seric if (pvp++ == evp) 146058814Seric break; 14614228Seric } 14624228Seric *p = '\0'; 14634228Seric } 14644228Seric /* 14653188Seric ** SAMEADDR -- Determine if two addresses are the same 14663188Seric ** 14673188Seric ** This is not just a straight comparison -- if the mailer doesn't 14683188Seric ** care about the host we just ignore it, etc. 14693188Seric ** 14703188Seric ** Parameters: 14713188Seric ** a, b -- pointers to the internal forms to compare. 14723188Seric ** 14733188Seric ** Returns: 14743188Seric ** TRUE -- they represent the same mailbox. 14753188Seric ** FALSE -- they don't. 14763188Seric ** 14773188Seric ** Side Effects: 14783188Seric ** none. 14793188Seric */ 14803188Seric 14813188Seric bool 14829374Seric sameaddr(a, b) 14833188Seric register ADDRESS *a; 14843188Seric register ADDRESS *b; 14853188Seric { 14863188Seric /* if they don't have the same mailer, forget it */ 14873188Seric if (a->q_mailer != b->q_mailer) 14883188Seric return (FALSE); 14893188Seric 14903188Seric /* if the user isn't the same, we can drop out */ 149156678Seric if (strcmp(a->q_user, b->q_user) != 0) 14923188Seric return (FALSE); 14933188Seric 149458438Seric /* if we have good uids for both but the differ, these are different */ 149558438Seric if (bitset(QGOODUID, a->q_flags & b->q_flags) && a->q_uid != b->q_uid) 149658438Seric return (FALSE); 149758438Seric 149858509Seric /* otherwise compare hosts (but be careful for NULL ptrs) */ 149958509Seric if (a->q_host == b->q_host) 150058509Seric { 150158509Seric /* probably both null pointers */ 15023188Seric return (TRUE); 150358509Seric } 15043188Seric if (a->q_host == NULL || b->q_host == NULL) 150558509Seric { 150658509Seric /* only one is a null pointer */ 15073188Seric return (FALSE); 150858509Seric } 150956678Seric if (strcmp(a->q_host, b->q_host) != 0) 15103188Seric return (FALSE); 15113188Seric 15123188Seric return (TRUE); 15133188Seric } 15143234Seric /* 15153234Seric ** PRINTADDR -- print address (for debugging) 15163234Seric ** 15173234Seric ** Parameters: 15183234Seric ** a -- the address to print 15193234Seric ** follow -- follow the q_next chain. 15203234Seric ** 15213234Seric ** Returns: 15223234Seric ** none. 15233234Seric ** 15243234Seric ** Side Effects: 15253234Seric ** none. 15263234Seric */ 15273234Seric 15283234Seric printaddr(a, follow) 15293234Seric register ADDRESS *a; 15303234Seric bool follow; 15313234Seric { 15325001Seric bool first = TRUE; 153357731Seric register MAILER *m; 153457731Seric MAILER pseudomailer; 15355001Seric 15363234Seric while (a != NULL) 15373234Seric { 15385001Seric first = FALSE; 15394443Seric printf("%x=", a); 15404085Seric (void) fflush(stdout); 154157731Seric 154257731Seric /* find the mailer -- carefully */ 154357731Seric m = a->q_mailer; 154457731Seric if (m == NULL) 154557731Seric { 154657731Seric m = &pseudomailer; 154757731Seric m->m_mno = -1; 154857731Seric m->m_name = "NULL"; 154957731Seric } 155057731Seric 155158680Seric printf("%s:\n\tmailer %d (%s), host `%s', user `%s', ruser `%s'\n", 155257731Seric a->q_paddr, m->m_mno, m->m_name, 155363756Seric a->q_host, a->q_user, 155463756Seric a->q_ruser ? a->q_ruser : "<null>"); 155559269Seric printf("\tnext=%x, flags=%o, alias %x, uid %d, gid %d\n", 155659269Seric a->q_next, a->q_flags, a->q_alias, a->q_uid, a->q_gid); 155759269Seric printf("\towner=%s, home=\"%s\", fullname=\"%s\"\n", 155859269Seric a->q_owner == NULL ? "(none)" : a->q_owner, 155963756Seric a->q_home == NULL ? "(none)" : a->q_home, 156063756Seric a->q_fullname == NULL ? "(none)" : a->q_fullname); 15614996Seric 15623234Seric if (!follow) 15633234Seric return; 15644996Seric a = a->q_next; 15653234Seric } 15665001Seric if (first) 15674443Seric printf("[NULL]\n"); 15683234Seric } 15694317Seric 15707682Seric /* 15717682Seric ** REMOTENAME -- return the name relative to the current mailer 15727682Seric ** 15737682Seric ** Parameters: 15747682Seric ** name -- the name to translate. 15758069Seric ** m -- the mailer that we want to do rewriting relative 15768069Seric ** to. 157759163Seric ** flags -- fine tune operations. 157859163Seric ** pstat -- pointer to status word. 157958020Seric ** e -- the current envelope. 15807682Seric ** 15817682Seric ** Returns: 15827682Seric ** the text string representing this address relative to 15837682Seric ** the receiving mailer. 15847682Seric ** 15857682Seric ** Side Effects: 15867682Seric ** none. 15877682Seric ** 15887682Seric ** Warnings: 15897682Seric ** The text string returned is tucked away locally; 15907682Seric ** copy it if you intend to save it. 15917682Seric */ 15927682Seric 15937682Seric char * 159459163Seric remotename(name, m, flags, pstat, e) 15957682Seric char *name; 159656678Seric struct mailer *m; 159759163Seric int flags; 159859163Seric int *pstat; 159956678Seric register ENVELOPE *e; 16007682Seric { 16018069Seric register char **pvp; 16028069Seric char *fancy; 160356678Seric char *oldg = macvalue('g', e); 160458020Seric int rwset; 16057682Seric static char buf[MAXNAME]; 16067682Seric char lbuf[MAXNAME]; 160716914Seric char pvpbuf[PSBUFSIZE]; 160856678Seric extern char *crackaddr(); 16097682Seric 16107755Seric if (tTd(12, 1)) 16117755Seric printf("remotename(%s)\n", name); 16127755Seric 161310177Seric /* don't do anything if we are tagging it as special */ 161459163Seric if (bitset(RF_SENDERADDR, flags)) 161559163Seric rwset = bitset(RF_HEADERADDR, flags) ? m->m_sh_rwset 161659163Seric : m->m_se_rwset; 161758020Seric else 161859163Seric rwset = bitset(RF_HEADERADDR, flags) ? m->m_rh_rwset 161959163Seric : m->m_re_rwset; 162058020Seric if (rwset < 0) 162110177Seric return (name); 162210177Seric 16237682Seric /* 16248181Seric ** Do a heuristic crack of this name to extract any comment info. 16258181Seric ** This will leave the name as a comment and a $g macro. 16267889Seric */ 16277889Seric 162859163Seric if (bitset(RF_CANONICAL, flags) || bitnset(M_NOCOMMENT, m->m_flags)) 162958050Seric fancy = "\201g"; 163010310Seric else 163110310Seric fancy = crackaddr(name); 16327889Seric 16338181Seric /* 16348181Seric ** Turn the name into canonical form. 16358181Seric ** Normally this will be RFC 822 style, i.e., "user@domain". 16368181Seric ** If this only resolves to "user", and the "C" flag is 16378181Seric ** specified in the sending mailer, then the sender's 16388181Seric ** domain will be appended. 16398181Seric */ 16408181Seric 164158333Seric pvp = prescan(name, '\0', pvpbuf, NULL); 16427889Seric if (pvp == NULL) 16437889Seric return (name); 164459163Seric if (rewrite(pvp, 3, e) == EX_TEMPFAIL) 164559163Seric *pstat = EX_TEMPFAIL; 164659163Seric if (bitset(RF_ADDDOMAIN, flags) && e->e_fromdomain != NULL) 16478181Seric { 16488181Seric /* append from domain to this address */ 16498181Seric register char **pxp = pvp; 16508181Seric 16519594Seric /* see if there is an "@domain" in the current name */ 16528181Seric while (*pxp != NULL && strcmp(*pxp, "@") != 0) 16538181Seric pxp++; 16548181Seric if (*pxp == NULL) 16558181Seric { 16569594Seric /* no.... append the "@domain" from the sender */ 165756678Seric register char **qxq = e->e_fromdomain; 16588181Seric 16599594Seric while ((*pxp++ = *qxq++) != NULL) 16609594Seric continue; 166159163Seric if (rewrite(pvp, 3, e) == EX_TEMPFAIL) 166259163Seric *pstat = EX_TEMPFAIL; 16638181Seric } 16648181Seric } 16658181Seric 16668181Seric /* 16678959Seric ** Do more specific rewriting. 166856678Seric ** Rewrite using ruleset 1 or 2 depending on whether this is 166956678Seric ** a sender address or not. 16708181Seric ** Then run it through any receiving-mailer-specific rulesets. 16718181Seric */ 16728181Seric 167359163Seric if (bitset(RF_SENDERADDR, flags)) 167459541Seric { 167559163Seric if (rewrite(pvp, 1, e) == EX_TEMPFAIL) 167659163Seric *pstat = EX_TEMPFAIL; 167759541Seric } 16788069Seric else 167959541Seric { 168059163Seric if (rewrite(pvp, 2, e) == EX_TEMPFAIL) 168159163Seric *pstat = EX_TEMPFAIL; 168259541Seric } 168358020Seric if (rwset > 0) 168459541Seric { 168559163Seric if (rewrite(pvp, rwset, e) == EX_TEMPFAIL) 168659163Seric *pstat = EX_TEMPFAIL; 168759541Seric } 16887682Seric 16898181Seric /* 16908959Seric ** Do any final sanitation the address may require. 16918959Seric ** This will normally be used to turn internal forms 16928959Seric ** (e.g., user@host.LOCAL) into external form. This 16938959Seric ** may be used as a default to the above rules. 16948959Seric */ 16958959Seric 169659163Seric if (rewrite(pvp, 4, e) == EX_TEMPFAIL) 169759163Seric *pstat = EX_TEMPFAIL; 16988959Seric 16998959Seric /* 17008181Seric ** Now restore the comment information we had at the beginning. 17018181Seric */ 17028181Seric 170358825Seric cataddr(pvp, NULL, lbuf, sizeof lbuf, '\0'); 170456678Seric define('g', lbuf, e); 170556678Seric expand(fancy, buf, &buf[sizeof buf - 1], e); 170656678Seric define('g', oldg, e); 17077682Seric 17087682Seric if (tTd(12, 1)) 17097755Seric printf("remotename => `%s'\n", buf); 17107682Seric return (buf); 17117682Seric } 171251317Seric /* 171356678Seric ** MAPLOCALUSER -- run local username through ruleset 5 for final redirection 171451317Seric ** 171551317Seric ** Parameters: 171656678Seric ** a -- the address to map (but just the user name part). 171756678Seric ** sendq -- the sendq in which to install any replacement 171856678Seric ** addresses. 171951317Seric ** 172051317Seric ** Returns: 172151317Seric ** none. 172251317Seric */ 172351317Seric 172456678Seric maplocaluser(a, sendq, e) 172556678Seric register ADDRESS *a; 172656678Seric ADDRESS **sendq; 172756678Seric ENVELOPE *e; 172851317Seric { 172956678Seric register char **pvp; 173056678Seric register ADDRESS *a1 = NULL; 173158333Seric auto char *delimptr; 173256678Seric char pvpbuf[PSBUFSIZE]; 173351317Seric 173456678Seric if (tTd(29, 1)) 173556678Seric { 173656678Seric printf("maplocaluser: "); 173756678Seric printaddr(a, FALSE); 173856678Seric } 173958333Seric pvp = prescan(a->q_user, '\0', pvpbuf, &delimptr); 174056678Seric if (pvp == NULL) 174156678Seric return; 174251317Seric 174359084Seric (void) rewrite(pvp, 5, e); 174458050Seric if (pvp[0] == NULL || (pvp[0][0] & 0377) != CANONNET) 174556678Seric return; 174651317Seric 174756678Seric /* if non-null, mailer destination specified -- has it changed? */ 174864284Seric a1 = buildaddr(pvp, NULL, 0, e); 174956678Seric if (a1 == NULL || sameaddr(a, a1)) 175056678Seric return; 175151317Seric 175256678Seric /* mark old address as dead; insert new address */ 175356678Seric a->q_flags |= QDONTSEND; 175457731Seric if (tTd(29, 5)) 175557731Seric { 175657731Seric printf("maplocaluser: QDONTSEND "); 175757731Seric printaddr(a, FALSE); 175857731Seric } 175956678Seric a1->q_alias = a; 176064348Seric allocaddr(a1, RF_COPYALL, NULL); 176156678Seric (void) recipient(a1, sendq, e); 176251317Seric } 176358802Seric /* 176458802Seric ** DEQUOTE_INIT -- initialize dequote map 176558802Seric ** 176658802Seric ** This is a no-op. 176758802Seric ** 176858802Seric ** Parameters: 176958802Seric ** map -- the internal map structure. 177058802Seric ** args -- arguments. 177158802Seric ** 177258802Seric ** Returns: 177358802Seric ** TRUE. 177458802Seric */ 177558802Seric 177658802Seric bool 177760219Seric dequote_init(map, args) 177858802Seric MAP *map; 177958802Seric char *args; 178058802Seric { 178158805Seric register char *p = args; 178258805Seric 178358805Seric for (;;) 178458805Seric { 178558805Seric while (isascii(*p) && isspace(*p)) 178658805Seric p++; 178758805Seric if (*p != '-') 178858805Seric break; 178958805Seric switch (*++p) 179058805Seric { 179158805Seric case 'a': 179258805Seric map->map_app = ++p; 179358805Seric break; 179458805Seric } 179558805Seric while (*p != '\0' && !(isascii(*p) && isspace(*p))) 179658805Seric p++; 179758805Seric if (*p != '\0') 179858805Seric *p = '\0'; 179958805Seric } 180058805Seric if (map->map_app != NULL) 180158805Seric map->map_app = newstr(map->map_app); 180258805Seric 180358802Seric return TRUE; 180458802Seric } 180558802Seric /* 180658802Seric ** DEQUOTE_MAP -- unquote an address 180758802Seric ** 180858802Seric ** Parameters: 180958802Seric ** map -- the internal map structure (ignored). 181060089Seric ** name -- the name to dequote. 181158802Seric ** av -- arguments (ignored). 181259084Seric ** statp -- pointer to status out-parameter. 181358802Seric ** 181458802Seric ** Returns: 181558802Seric ** NULL -- if there were no quotes, or if the resulting 181658802Seric ** unquoted buffer would not be acceptable to prescan. 181758802Seric ** else -- The dequoted buffer. 181858802Seric */ 181958802Seric 182058802Seric char * 182160089Seric dequote_map(map, name, av, statp) 182258802Seric MAP *map; 182360089Seric char *name; 182458802Seric char **av; 182559084Seric int *statp; 182658802Seric { 182758802Seric register char *p; 182858802Seric register char *q; 182958802Seric register char c; 183058802Seric int anglecnt; 183158802Seric int cmntcnt; 183258802Seric int quotecnt; 183359089Seric int spacecnt; 183458802Seric bool quotemode; 183558802Seric bool bslashmode; 183658802Seric 183758802Seric anglecnt = 0; 183858802Seric cmntcnt = 0; 183958802Seric quotecnt = 0; 184059089Seric spacecnt = 0; 184158802Seric quotemode = FALSE; 184258802Seric bslashmode = FALSE; 184358802Seric 184460089Seric for (p = q = name; (c = *p++) != '\0'; ) 184558802Seric { 184658802Seric if (bslashmode) 184758802Seric { 184858802Seric bslashmode = FALSE; 184958802Seric *q++ = c; 185058802Seric continue; 185158802Seric } 185258802Seric 185358802Seric switch (c) 185458802Seric { 185558802Seric case '\\': 185658802Seric bslashmode = TRUE; 185758802Seric break; 185858802Seric 185958802Seric case '(': 186058802Seric cmntcnt++; 186158802Seric break; 186258802Seric 186358802Seric case ')': 186458802Seric if (cmntcnt-- <= 0) 186558802Seric return NULL; 186658802Seric break; 186759089Seric 186859089Seric case ' ': 186959089Seric spacecnt++; 187059089Seric break; 187158802Seric } 187258802Seric 187358802Seric if (cmntcnt > 0) 187458802Seric { 187558802Seric *q++ = c; 187658802Seric continue; 187758802Seric } 187858802Seric 187958802Seric switch (c) 188058802Seric { 188158802Seric case '"': 188258802Seric quotemode = !quotemode; 188358802Seric quotecnt++; 188458802Seric continue; 188558802Seric 188658802Seric case '<': 188758802Seric anglecnt++; 188858802Seric break; 188958802Seric 189058802Seric case '>': 189158802Seric if (anglecnt-- <= 0) 189258802Seric return NULL; 189358802Seric break; 189458802Seric } 189558802Seric *q++ = c; 189658802Seric } 189758802Seric 189858802Seric if (anglecnt != 0 || cmntcnt != 0 || bslashmode || 189959089Seric quotemode || quotecnt <= 0 || spacecnt != 0) 190058802Seric return NULL; 190158802Seric *q++ = '\0'; 190260089Seric return name; 190358802Seric } 1904