122976Smiriam /* 234921Sbostic * Copyright (c) 1983 Eric P. Allman 333730Sbostic * Copyright (c) 1988 Regents of the University of California. 433730Sbostic * All rights reserved. 533730Sbostic * 642828Sbostic * %sccs.include.redist.c% 733730Sbostic */ 822976Smiriam 922976Smiriam #ifndef lint 10*59747Seric static char sccsid[] = "@(#)parseaddr.c 6.50 (Berkeley) 05/05/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. 33297Seric ** copyf -- determines what shall be copied: 34297Seric ** -1 -- don't copy anything. The printname 35297Seric ** (q_paddr) is just addr, and the 36297Seric ** user & host are allocated internally 37297Seric ** to parse. 38297Seric ** 0 -- copy out the parsed user & host, but 39297Seric ** don't copy the printname. 40297Seric ** +1 -- copy everything. 4111445Seric ** delim -- the character to terminate the address, passed 4211445Seric ** to prescan. 4358333Seric ** delimptr -- if non-NULL, set to the location of the 4458333Seric ** delim character that was found. 4556678Seric ** e -- the envelope that will contain this address. 46297Seric ** 47297Seric ** Returns: 48297Seric ** A pointer to the address descriptor header (`a' if 49297Seric ** `a' is non-NULL). 50297Seric ** NULL on error. 51297Seric ** 52297Seric ** Side Effects: 53297Seric ** none 54297Seric */ 55297Seric 569374Seric /* following delimiters are inherent to the internal algorithms */ 5759278Seric # define DELIMCHARS "()<>,;\r\n" /* default word delimiters */ 582091Seric 592973Seric ADDRESS * 6058333Seric parseaddr(addr, a, copyf, delim, delimptr, e) 61297Seric char *addr; 622973Seric register ADDRESS *a; 63297Seric int copyf; 6459700Seric int delim; 6558333Seric char **delimptr; 6656678Seric register ENVELOPE *e; 67297Seric { 683149Seric register char **pvp; 6958333Seric auto char *delimptrbuf; 7059084Seric bool queueup; 7116914Seric char pvpbuf[PSBUFSIZE]; 7256678Seric extern char **prescan(); 7356678Seric extern ADDRESS *buildaddr(); 7457388Seric extern bool invalidaddr(); 75297Seric 76297Seric /* 77297Seric ** Initialize and prescan address. 78297Seric */ 79297Seric 8056678Seric e->e_to = addr; 817675Seric if (tTd(20, 1)) 829888Seric printf("\n--parseaddr(%s)\n", addr); 833188Seric 8457388Seric if (invalidaddr(addr)) 8557388Seric { 8657388Seric if (tTd(20, 1)) 8757388Seric printf("parseaddr-->bad address\n"); 8857388Seric return NULL; 8957388Seric } 9057388Seric 9158333Seric if (delimptr == NULL) 9258333Seric delimptr = &delimptrbuf; 9358333Seric 9458333Seric pvp = prescan(addr, delim, pvpbuf, delimptr); 953149Seric if (pvp == NULL) 9656729Seric { 9756729Seric if (tTd(20, 1)) 9856729Seric printf("parseaddr-->NULL\n"); 99297Seric return (NULL); 10056729Seric } 101297Seric 102297Seric /* 1033149Seric ** Apply rewriting rules. 1047889Seric ** Ruleset 0 does basic parsing. It must resolve. 105297Seric */ 106297Seric 10759084Seric queueup = FALSE; 10859084Seric if (rewrite(pvp, 3, e) == EX_TEMPFAIL) 10959084Seric queueup = TRUE; 11059084Seric if (rewrite(pvp, 0, e) == EX_TEMPFAIL) 11159084Seric queueup = TRUE; 112297Seric 1133149Seric /* 1143149Seric ** See if we resolved to a real mailer. 1153149Seric */ 116297Seric 11759040Seric if (pvp[0] == NULL || (pvp[0][0] & 0377) != CANONNET) 1183149Seric { 1193149Seric setstat(EX_USAGE); 12058151Seric syserr("554 cannot resolve name"); 1213149Seric return (NULL); 122297Seric } 123297Seric 124297Seric /* 1253149Seric ** Build canonical address from pvp. 126297Seric */ 127297Seric 12858966Seric a = buildaddr(pvp, a, e); 1294279Seric if (a == NULL) 1304279Seric return (NULL); 131297Seric 132297Seric /* 1333149Seric ** Make local copies of the host & user and then 1343149Seric ** transport them out. 135297Seric */ 136297Seric 13758333Seric allocaddr(a, copyf, addr, *delimptr); 13856678Seric 13956678Seric /* 14059084Seric ** If there was a parsing failure, mark it for queueing. 14159084Seric */ 14259084Seric 14359084Seric if (queueup) 14459595Seric { 14559734Seric char *msg = "Transient parse error -- message queued for future delivery"; 14659734Seric 14759595Seric if (tTd(20, 1)) 14859595Seric printf("parseaddr: queuing message\n"); 14959734Seric message(msg); 15059734Seric if (e->e_message == NULL) 15159734Seric e->e_message = msg; 15259084Seric a->q_flags |= QQUEUEUP; 15359595Seric } 15459084Seric 15559084Seric /* 15656678Seric ** Compute return value. 15756678Seric */ 15856678Seric 15956678Seric if (tTd(20, 1)) 1608078Seric { 16156678Seric printf("parseaddr-->"); 16256678Seric printaddr(a, FALSE); 16356678Seric } 16456678Seric 16556678Seric return (a); 16656678Seric } 16756678Seric /* 16857388Seric ** INVALIDADDR -- check for address containing meta-characters 16957388Seric ** 17057388Seric ** Parameters: 17157388Seric ** addr -- the address to check. 17257388Seric ** 17357388Seric ** Returns: 17457388Seric ** TRUE -- if the address has any "wierd" characters 17557388Seric ** FALSE -- otherwise. 17657388Seric */ 17757388Seric 17857388Seric bool 17957388Seric invalidaddr(addr) 18057388Seric register char *addr; 18157388Seric { 18257388Seric for (; *addr != '\0'; addr++) 18357388Seric { 18458050Seric if ((*addr & 0340) != 0200) 18557388Seric continue; 18657388Seric setstat(EX_USAGE); 18758151Seric usrerr("553 Address contained invalid control characters"); 18857388Seric return TRUE; 18957388Seric } 19057388Seric return FALSE; 19157388Seric } 19257388Seric /* 19356678Seric ** ALLOCADDR -- do local allocations of address on demand. 19456678Seric ** 19556678Seric ** Also lowercases the host name if requested. 19656678Seric ** 19756678Seric ** Parameters: 19856678Seric ** a -- the address to reallocate. 19956678Seric ** copyf -- the copy flag (see parseaddr for description). 20056678Seric ** paddr -- the printname of the address. 20158333Seric ** delimptr -- a pointer to the address delimiter. Must be set. 20256678Seric ** 20356678Seric ** Returns: 20456678Seric ** none. 20556678Seric ** 20656678Seric ** Side Effects: 20756678Seric ** Copies portions of a into local buffers as requested. 20856678Seric */ 20956678Seric 21058333Seric allocaddr(a, copyf, paddr, delimptr) 21156678Seric register ADDRESS *a; 21256678Seric int copyf; 21356678Seric char *paddr; 21458333Seric char *delimptr; 21556678Seric { 21656678Seric register MAILER *m = a->q_mailer; 21756678Seric 21858673Seric if (tTd(24, 4)) 21958673Seric printf("allocaddr(copyf=%d, paddr=%s)\n", copyf, paddr); 22058673Seric 22156678Seric if (copyf > 0 && paddr != NULL) 22256678Seric { 22358333Seric char savec = *delimptr; 2248078Seric 225*59747Seric if (savec != '\0') 226*59747Seric *delimptr = '\0'; 22756678Seric a->q_paddr = newstr(paddr); 228*59747Seric if (savec != '\0') 229*59747Seric *delimptr = savec; 2308078Seric } 231297Seric else 23256678Seric a->q_paddr = paddr; 23324944Seric 23424944Seric if (a->q_user == NULL) 23524944Seric a->q_user = ""; 23624944Seric if (a->q_host == NULL) 23724944Seric a->q_host = ""; 23824944Seric 2393149Seric if (copyf >= 0) 240297Seric { 24124944Seric a->q_host = newstr(a->q_host); 2423149Seric if (a->q_user != a->q_paddr) 2433149Seric a->q_user = newstr(a->q_user); 244297Seric } 245297Seric 24656678Seric if (a->q_paddr == NULL) 24756678Seric a->q_paddr = a->q_user; 248297Seric } 249297Seric /* 250297Seric ** PRESCAN -- Prescan name and make it canonical 251297Seric ** 2529374Seric ** Scans a name and turns it into a set of tokens. This process 2539374Seric ** deletes blanks and comments (in parentheses). 254297Seric ** 255297Seric ** This routine knows about quoted strings and angle brackets. 256297Seric ** 257297Seric ** There are certain subtleties to this routine. The one that 258297Seric ** comes to mind now is that backslashes on the ends of names 259297Seric ** are silently stripped off; this is intentional. The problem 260297Seric ** is that some versions of sndmsg (like at LBL) set the kill 261297Seric ** character to something other than @ when reading addresses; 262297Seric ** so people type "csvax.eric\@berkeley" -- which screws up the 263297Seric ** berknet mailer. 264297Seric ** 265297Seric ** Parameters: 266297Seric ** addr -- the name to chomp. 267297Seric ** delim -- the delimiter for the address, normally 268297Seric ** '\0' or ','; \0 is accepted in any case. 26915284Seric ** If '\t' then we are reading the .cf file. 27016914Seric ** pvpbuf -- place to put the saved text -- note that 27116914Seric ** the pointers are static. 27258333Seric ** delimptr -- if non-NULL, set to the location of the 27358333Seric ** terminating delimiter. 274297Seric ** 275297Seric ** Returns: 2763149Seric ** A pointer to a vector of tokens. 277297Seric ** NULL on error. 278297Seric */ 279297Seric 2808078Seric /* states and character types */ 2818078Seric # define OPR 0 /* operator */ 2828078Seric # define ATM 1 /* atom */ 2838078Seric # define QST 2 /* in quoted string */ 2848078Seric # define SPC 3 /* chewing up spaces */ 2858078Seric # define ONE 4 /* pick up one character */ 2863149Seric 2878078Seric # define NSTATES 5 /* number of states */ 2888078Seric # define TYPE 017 /* mask to select state type */ 2898078Seric 2908078Seric /* meta bits for table */ 2918078Seric # define M 020 /* meta character; don't pass through */ 2928078Seric # define B 040 /* cause a break */ 2938078Seric # define MB M|B /* meta-break */ 2948078Seric 2958078Seric static short StateTab[NSTATES][NSTATES] = 2968078Seric { 2978087Seric /* oldst chtype> OPR ATM QST SPC ONE */ 2989051Seric /*OPR*/ OPR|B, ATM|B, QST|B, SPC|MB, ONE|B, 2999051Seric /*ATM*/ OPR|B, ATM, QST|B, SPC|MB, ONE|B, 3009051Seric /*QST*/ QST, QST, OPR, QST, QST, 3018078Seric /*SPC*/ OPR, ATM, QST, SPC|M, ONE, 3028078Seric /*ONE*/ OPR, OPR, OPR, OPR, OPR, 3038078Seric }; 3048078Seric 3058078Seric # define NOCHAR -1 /* signal nothing in lookahead token */ 3068078Seric 3073149Seric char ** 30858333Seric prescan(addr, delim, pvpbuf, delimptr) 309297Seric char *addr; 310297Seric char delim; 31116914Seric char pvpbuf[]; 31258333Seric char **delimptr; 313297Seric { 314297Seric register char *p; 3158078Seric register char *q; 3169346Seric register int c; 3173149Seric char **avp; 318297Seric bool bslashmode; 319297Seric int cmntcnt; 3208423Seric int anglecnt; 3213149Seric char *tok; 3228078Seric int state; 3238078Seric int newstate; 324*59747Seric char *saveto = CurEnv->e_to; 3258078Seric static char *av[MAXATOM+1]; 32656678Seric extern int errno; 327297Seric 32815253Seric /* make sure error messages don't have garbage on them */ 32915253Seric errno = 0; 33015253Seric 33116914Seric q = pvpbuf; 3323149Seric bslashmode = FALSE; 3337800Seric cmntcnt = 0; 3348423Seric anglecnt = 0; 3353149Seric avp = av; 33656678Seric state = ATM; 3378078Seric c = NOCHAR; 3388078Seric p = addr; 339*59747Seric CurEnv->e_to = p; 34056764Seric if (tTd(22, 11)) 341297Seric { 3428078Seric printf("prescan: "); 3438078Seric xputs(p); 34423109Seric (void) putchar('\n'); 3458078Seric } 3468078Seric 3478078Seric do 3488078Seric { 3493149Seric /* read a token */ 3503149Seric tok = q; 3518078Seric for (;;) 352297Seric { 3538078Seric /* store away any old lookahead character */ 35459277Seric if (c != NOCHAR && !bslashmode) 3558078Seric { 35615284Seric /* see if there is room */ 35716914Seric if (q >= &pvpbuf[PSBUFSIZE - 5]) 3588078Seric { 35958151Seric usrerr("553 Address too long"); 36058333Seric if (delimptr != NULL) 36158333Seric *delimptr = p; 362*59747Seric CurEnv->e_to = saveto; 3638078Seric return (NULL); 3648078Seric } 36515284Seric 36615284Seric /* squirrel it away */ 3678078Seric *q++ = c; 3688078Seric } 3698078Seric 3708078Seric /* read a new input character */ 3718078Seric c = *p++; 37257631Seric if (c == '\0') 37356764Seric { 37456764Seric /* diagnose and patch up bad syntax */ 37556764Seric if (state == QST) 37656764Seric { 37758151Seric usrerr("553 Unbalanced '\"'"); 37856764Seric c = '"'; 37956764Seric } 38056764Seric else if (cmntcnt > 0) 38156764Seric { 38258151Seric usrerr("553 Unbalanced '('"); 38356764Seric c = ')'; 38456764Seric } 38556764Seric else if (anglecnt > 0) 38656764Seric { 38756764Seric c = '>'; 38858151Seric usrerr("553 Unbalanced '<'"); 38956764Seric } 39056764Seric else 39156764Seric break; 39215284Seric 39356764Seric p--; 39456764Seric } 39557631Seric else if (c == delim && anglecnt <= 0 && 39657631Seric cmntcnt <= 0 && state != QST) 39757631Seric break; 39856764Seric 3998078Seric if (tTd(22, 101)) 4008078Seric printf("c=%c, s=%d; ", c, state); 4018078Seric 4023149Seric /* chew up special characters */ 4033149Seric *q = '\0'; 4043149Seric if (bslashmode) 4053149Seric { 40659105Seric bslashmode = FALSE; 40759105Seric 40824944Seric /* kludge \! for naive users */ 40958061Seric if (cmntcnt > 0) 41059105Seric { 41158061Seric c = NOCHAR; 41259105Seric continue; 41359105Seric } 41459105Seric else if (c != '!' || state == QST) 41559105Seric { 41656678Seric *q++ = '\\'; 41759105Seric continue; 41859105Seric } 4193149Seric } 42056678Seric 42156678Seric if (c == '\\') 4223149Seric { 4233149Seric bslashmode = TRUE; 4243149Seric } 42556678Seric else if (state == QST) 4268514Seric { 4278514Seric /* do nothing, just avoid next clauses */ 4288514Seric } 4298078Seric else if (c == '(') 4304100Seric { 4318078Seric cmntcnt++; 4328078Seric c = NOCHAR; 4334100Seric } 4348078Seric else if (c == ')') 4353149Seric { 4368078Seric if (cmntcnt <= 0) 4373149Seric { 43858151Seric usrerr("553 Unbalanced ')'"); 43958333Seric if (delimptr != NULL) 44058333Seric *delimptr = p; 441*59747Seric CurEnv->e_to = saveto; 4428078Seric return (NULL); 4433149Seric } 4448078Seric else 4458078Seric cmntcnt--; 4468078Seric } 4478078Seric else if (cmntcnt > 0) 4488078Seric c = NOCHAR; 4498423Seric else if (c == '<') 4508423Seric anglecnt++; 4518423Seric else if (c == '>') 4528423Seric { 4538423Seric if (anglecnt <= 0) 4548423Seric { 45558151Seric usrerr("553 Unbalanced '>'"); 45658333Seric if (delimptr != NULL) 45758333Seric *delimptr = p; 458*59747Seric CurEnv->e_to = saveto; 4598423Seric return (NULL); 4608423Seric } 4618423Seric anglecnt--; 4628423Seric } 46358050Seric else if (delim == ' ' && isascii(c) && isspace(c)) 46411423Seric c = ' '; 4653149Seric 4668078Seric if (c == NOCHAR) 4678078Seric continue; 4683149Seric 4698078Seric /* see if this is end of input */ 47011405Seric if (c == delim && anglecnt <= 0 && state != QST) 4713149Seric break; 4723149Seric 4738078Seric newstate = StateTab[state][toktype(c)]; 4748078Seric if (tTd(22, 101)) 4758078Seric printf("ns=%02o\n", newstate); 4768078Seric state = newstate & TYPE; 4778078Seric if (bitset(M, newstate)) 4788078Seric c = NOCHAR; 4798078Seric if (bitset(B, newstate)) 4804228Seric break; 481297Seric } 4823149Seric 4833149Seric /* new token */ 4848078Seric if (tok != q) 4851378Seric { 4868078Seric *q++ = '\0'; 4878078Seric if (tTd(22, 36)) 488297Seric { 4898078Seric printf("tok="); 4908078Seric xputs(tok); 49123109Seric (void) putchar('\n'); 492297Seric } 4938078Seric if (avp >= &av[MAXATOM]) 494297Seric { 49558151Seric syserr("553 prescan: too many tokens"); 49658333Seric if (delimptr != NULL) 49758333Seric *delimptr = p; 498*59747Seric CurEnv->e_to = saveto; 4998078Seric return (NULL); 500297Seric } 5018078Seric *avp++ = tok; 502297Seric } 5038423Seric } while (c != '\0' && (c != delim || anglecnt > 0)); 5043149Seric *avp = NULL; 50558333Seric p--; 50658333Seric if (delimptr != NULL) 50758333Seric *delimptr = p; 50856764Seric if (tTd(22, 12)) 50956764Seric { 51056764Seric printf("prescan==>"); 51156764Seric printav(av); 51256764Seric } 513*59747Seric CurEnv->e_to = saveto; 51458546Seric if (av[0] == NULL) 51558546Seric return (NULL); 51658403Seric return (av); 5173149Seric } 5183149Seric /* 5193149Seric ** TOKTYPE -- return token type 5203149Seric ** 5213149Seric ** Parameters: 5223149Seric ** c -- the character in question. 5233149Seric ** 5243149Seric ** Returns: 5253149Seric ** Its type. 5263149Seric ** 5273149Seric ** Side Effects: 5283149Seric ** none. 5293149Seric */ 530297Seric 5313149Seric toktype(c) 53258050Seric register int c; 5333149Seric { 5343380Seric static char buf[50]; 5353382Seric static bool firstime = TRUE; 5363380Seric 5373382Seric if (firstime) 5383380Seric { 5393382Seric firstime = FALSE; 54058050Seric expand("\201o", buf, &buf[sizeof buf - 1], CurEnv); 5417005Seric (void) strcat(buf, DELIMCHARS); 5423380Seric } 54358050Seric c &= 0377; 54456327Seric if (c == MATCHCLASS || c == MATCHREPL || c == MATCHNCLASS) 5458078Seric return (ONE); 54659027Seric if (c == MACRODEXPAND) 54759027Seric return (ONE); 5488078Seric if (c == '"') 5498078Seric return (QST); 55058050Seric if ((c & 0340) == 0200) 55158050Seric return (OPR); 5524100Seric if (!isascii(c)) 5538078Seric return (ATM); 5548078Seric if (isspace(c) || c == ')') 5558078Seric return (SPC); 55658050Seric if (strchr(buf, c) != NULL) 5578078Seric return (OPR); 5588078Seric return (ATM); 5593149Seric } 5603149Seric /* 5613149Seric ** REWRITE -- apply rewrite rules to token vector. 5623149Seric ** 5634476Seric ** This routine is an ordered production system. Each rewrite 5644476Seric ** rule has a LHS (called the pattern) and a RHS (called the 5654476Seric ** rewrite); 'rwr' points the the current rewrite rule. 5664476Seric ** 5674476Seric ** For each rewrite rule, 'avp' points the address vector we 5684476Seric ** are trying to match against, and 'pvp' points to the pattern. 5698058Seric ** If pvp points to a special match value (MATCHZANY, MATCHANY, 5709585Seric ** MATCHONE, MATCHCLASS, MATCHNCLASS) then the address in avp 5719585Seric ** matched is saved away in the match vector (pointed to by 'mvp'). 5724476Seric ** 5734476Seric ** When a match between avp & pvp does not match, we try to 5749585Seric ** back out. If we back up over MATCHONE, MATCHCLASS, or MATCHNCLASS 5754476Seric ** we must also back out the match in mvp. If we reach a 5768058Seric ** MATCHANY or MATCHZANY we just extend the match and start 5778058Seric ** over again. 5784476Seric ** 5794476Seric ** When we finally match, we rewrite the address vector 5804476Seric ** and try over again. 5814476Seric ** 5823149Seric ** Parameters: 5833149Seric ** pvp -- pointer to token vector. 58459027Seric ** ruleset -- the ruleset to use for rewriting. 58559027Seric ** e -- the current envelope. 5863149Seric ** 5873149Seric ** Returns: 58859084Seric ** A status code. If EX_TEMPFAIL, higher level code should 58959084Seric ** attempt recovery. 5903149Seric ** 5913149Seric ** Side Effects: 5923149Seric ** pvp is modified. 5933149Seric */ 5942091Seric 5953149Seric struct match 5963149Seric { 5974468Seric char **first; /* first token matched */ 5984468Seric char **last; /* last token matched */ 59958825Seric char **pattern; /* pointer to pattern */ 6003149Seric }; 6013149Seric 6024468Seric # define MAXMATCH 9 /* max params per rewrite */ 6033149Seric 6043149Seric 60559084Seric int 60659027Seric rewrite(pvp, ruleset, e) 6073149Seric char **pvp; 6084070Seric int ruleset; 60959027Seric register ENVELOPE *e; 6103149Seric { 6113149Seric register char *ap; /* address pointer */ 6123149Seric register char *rp; /* rewrite pointer */ 6133149Seric register char **avp; /* address vector pointer */ 6143149Seric register char **rvp; /* rewrite vector pointer */ 6158058Seric register struct match *mlp; /* cur ptr into mlist */ 6168058Seric register struct rewrite *rwr; /* pointer to current rewrite rule */ 61758866Seric int ruleno; /* current rule number */ 61859084Seric int rstat = EX_OK; /* return status */ 61956678Seric struct match mlist[MAXMATCH]; /* stores match on LHS */ 6203149Seric char *npvp[MAXATOM+1]; /* temporary space for rebuild */ 62159027Seric extern char *macvalue(); 6223149Seric 6239279Seric if (OpMode == MD_TEST || tTd(21, 2)) 6243149Seric { 6258959Seric printf("rewrite: ruleset %2d input:", ruleset); 62656678Seric printav(pvp); 6273149Seric } 62856678Seric if (ruleset < 0 || ruleset >= MAXRWSETS) 62956326Seric { 63058151Seric syserr("554 rewrite: illegal ruleset number %d", ruleset); 63159084Seric return EX_CONFIG; 63256326Seric } 63356678Seric if (pvp == NULL) 63459084Seric return EX_USAGE; 63556326Seric 6363149Seric /* 63756678Seric ** Run through the list of rewrite rules, applying 63856678Seric ** any that match. 6393149Seric */ 6403149Seric 64158866Seric ruleno = 1; 6424070Seric for (rwr = RewriteRules[ruleset]; rwr != NULL; ) 6433149Seric { 64456678Seric int loopcount = 0; 64556678Seric 6467675Seric if (tTd(21, 12)) 647297Seric { 6488069Seric printf("-----trying rule:"); 64956678Seric printav(rwr->r_lhs); 6503149Seric } 6513149Seric 6523149Seric /* try to match on this rule */ 6534468Seric mlp = mlist; 6548058Seric rvp = rwr->r_lhs; 6558058Seric avp = pvp; 65658866Seric if (++loopcount > 100) 6573149Seric { 65858866Seric syserr("554 Infinite loop in ruleset %d, rule %d", 65958866Seric ruleset, ruleno); 66058866Seric if (tTd(21, 1)) 66152637Seric { 66256678Seric printf("workspace: "); 66356678Seric printav(pvp); 66452637Seric } 66558866Seric break; 66658866Seric } 66758866Seric 66858866Seric while ((ap = *avp) != NULL || *rvp != NULL) 66958866Seric { 6703149Seric rp = *rvp; 6718058Seric if (tTd(21, 35)) 6728058Seric { 67358825Seric printf("ADVANCE rp="); 67457531Seric xputs(rp); 67557532Seric printf(", ap="); 6768058Seric xputs(ap); 6778069Seric printf("\n"); 6788058Seric } 67956678Seric if (rp == NULL) 68056326Seric { 6813149Seric /* end-of-pattern before end-of-address */ 6828058Seric goto backup; 68356678Seric } 68458173Seric if (ap == NULL && (*rp & 0377) != MATCHZANY && 68558827Seric (*rp & 0377) != MATCHZERO) 68656326Seric { 68758825Seric /* end-of-input with patterns left */ 68858825Seric goto backup; 689297Seric } 69056326Seric 69158050Seric switch (*rp & 0377) 6928058Seric { 69356678Seric register STAB *s; 69458814Seric char buf[MAXLINE]; 69556326Seric 69656678Seric case MATCHCLASS: 69758825Seric /* match any phrase in a class */ 69858825Seric mlp->pattern = rvp; 69958814Seric mlp->first = avp; 70058814Seric extendclass: 70158825Seric ap = *avp; 70258825Seric if (ap == NULL) 70358814Seric goto backup; 70458814Seric mlp->last = avp++; 70558814Seric cataddr(mlp->first, mlp->last, buf, sizeof buf, '\0'); 70658814Seric s = stab(buf, ST_CLASS, ST_FIND); 70756678Seric if (s == NULL || !bitnset(rp[1], s->s_class)) 70856326Seric { 70958825Seric if (tTd(21, 36)) 71058825Seric { 71158825Seric printf("EXTEND rp="); 71258825Seric xputs(rp); 71358825Seric printf(", ap="); 71458825Seric xputs(ap); 71558825Seric printf("\n"); 71658825Seric } 71758825Seric goto extendclass; 71856326Seric } 71958825Seric if (tTd(21, 36)) 72058825Seric printf("CLMATCH\n"); 72158814Seric mlp++; 72258814Seric break; 7234060Seric 72458825Seric case MATCHNCLASS: 72558825Seric /* match any token not in a class */ 72658825Seric s = stab(ap, ST_CLASS, ST_FIND); 72758825Seric if (s != NULL && bitnset(rp[1], s->s_class)) 72858825Seric goto backup; 72958825Seric 73058825Seric /* fall through */ 73158825Seric 73256678Seric case MATCHONE: 73356678Seric case MATCHANY: 73456678Seric /* match exactly one token */ 73558825Seric mlp->pattern = rvp; 73656678Seric mlp->first = avp; 73756678Seric mlp->last = avp++; 7388058Seric mlp++; 73956678Seric break; 7408058Seric 74156678Seric case MATCHZANY: 74256678Seric /* match zero or more tokens */ 74358825Seric mlp->pattern = rvp; 74456678Seric mlp->first = avp; 74556678Seric mlp->last = avp - 1; 74656678Seric mlp++; 74756678Seric break; 74856326Seric 74958827Seric case MATCHZERO: 75058173Seric /* match zero tokens */ 75158173Seric break; 75258173Seric 75359027Seric case MACRODEXPAND: 75459027Seric /* 75559027Seric ** Match against run-time macro. 75659027Seric ** This algorithm is broken for the 75759027Seric ** general case (no recursive macros, 75859027Seric ** improper tokenization) but should 75959027Seric ** work for the usual cases. 76059027Seric */ 76159027Seric 76259027Seric ap = macvalue(rp[1], e); 76359027Seric mlp->first = avp; 76459027Seric if (tTd(21, 2)) 76559027Seric printf("rewrite: LHS $&%c => \"%s\"\n", 76659027Seric rp[1], 76759027Seric ap == NULL ? "(NULL)" : ap); 76859027Seric 76959027Seric if (ap == NULL) 77059027Seric break; 77159027Seric while (*ap != NULL) 77259027Seric { 77359027Seric if (*avp == NULL || 77459027Seric strncasecmp(ap, *avp, strlen(*avp)) != 0) 77559027Seric { 77659027Seric /* no match */ 77759027Seric avp = mlp->first; 77859027Seric goto backup; 77959027Seric } 78059027Seric ap += strlen(*avp++); 78159027Seric } 78259027Seric 78359027Seric /* match */ 78459027Seric break; 78559027Seric 78656678Seric default: 78756678Seric /* must have exact match */ 78856678Seric if (strcasecmp(rp, ap)) 7898058Seric goto backup; 7904468Seric avp++; 79156678Seric break; 7923149Seric } 7933149Seric 79456678Seric /* successful match on this token */ 7953149Seric rvp++; 7963149Seric continue; 7973149Seric 79858825Seric backup: 79956678Seric /* match failed -- back up */ 80058825Seric while (--mlp >= mlist) 8013149Seric { 80258825Seric rvp = mlp->pattern; 80356678Seric rp = *rvp; 80458825Seric avp = mlp->last + 1; 80558825Seric ap = *avp; 80658825Seric 80758825Seric if (tTd(21, 36)) 80858825Seric { 80958825Seric printf("BACKUP rp="); 81058825Seric xputs(rp); 81158825Seric printf(", ap="); 81258825Seric xputs(ap); 81358825Seric printf("\n"); 81458825Seric } 81558825Seric 81658825Seric if (ap == NULL) 81758825Seric { 81858825Seric /* run off the end -- back up again */ 81958825Seric continue; 82058825Seric } 82158050Seric if ((*rp & 0377) == MATCHANY || 82258050Seric (*rp & 0377) == MATCHZANY) 8234468Seric { 82456678Seric /* extend binding and continue */ 82558825Seric mlp->last = avp++; 82656678Seric rvp++; 82758825Seric mlp++; 82856678Seric break; 8294468Seric } 83058825Seric if ((*rp & 0377) == MATCHCLASS) 83156678Seric { 83258814Seric /* extend binding and try again */ 83358825Seric mlp->last = avp++; 83458814Seric goto extendclass; 83558814Seric } 8363149Seric } 8373149Seric 83858825Seric if (mlp < mlist) 83956678Seric { 84056678Seric /* total failure to match */ 84156326Seric break; 8423149Seric } 843297Seric } 8443149Seric 8453149Seric /* 84656678Seric ** See if we successfully matched 8473149Seric */ 8483149Seric 84958827Seric if (mlp < mlist || *rvp != NULL) 8503149Seric { 8519374Seric if (tTd(21, 10)) 8529374Seric printf("----- rule fails\n"); 8539374Seric rwr = rwr->r_next; 85458866Seric ruleno++; 8559374Seric continue; 8569374Seric } 8573149Seric 8589374Seric rvp = rwr->r_rhs; 8599374Seric if (tTd(21, 12)) 8609374Seric { 8619374Seric printf("-----rule matches:"); 86256678Seric printav(rvp); 8639374Seric } 8649374Seric 8659374Seric rp = *rvp; 86658050Seric if ((*rp & 0377) == CANONUSER) 8679374Seric { 8689374Seric rvp++; 8699374Seric rwr = rwr->r_next; 87058866Seric ruleno++; 8719374Seric } 87258050Seric else if ((*rp & 0377) == CANONHOST) 8739374Seric { 8749374Seric rvp++; 8759374Seric rwr = NULL; 8769374Seric } 87758050Seric else if ((*rp & 0377) == CANONNET) 8789374Seric rwr = NULL; 8799374Seric 8809374Seric /* substitute */ 8819374Seric for (avp = npvp; *rvp != NULL; rvp++) 8829374Seric { 8839374Seric register struct match *m; 8849374Seric register char **pp; 8859374Seric 8868058Seric rp = *rvp; 88758050Seric if ((*rp & 0377) == MATCHREPL) 8888058Seric { 88916914Seric /* substitute from LHS */ 89016914Seric m = &mlist[rp[1] - '1']; 89156678Seric if (m < mlist || m >= mlp) 8929374Seric { 89358151Seric syserr("554 rewrite: ruleset %d: replacement $%c out of bounds", 89456326Seric ruleset, rp[1]); 89559084Seric return EX_CONFIG; 8969374Seric } 89716914Seric if (tTd(21, 15)) 89816914Seric { 89916914Seric printf("$%c:", rp[1]); 90016914Seric pp = m->first; 90156678Seric while (pp <= m->last) 90216914Seric { 90316914Seric printf(" %x=\"", *pp); 90416914Seric (void) fflush(stdout); 90516914Seric printf("%s\"", *pp++); 90616914Seric } 90716914Seric printf("\n"); 90816914Seric } 9099374Seric pp = m->first; 91056678Seric while (pp <= m->last) 9113149Seric { 91216914Seric if (avp >= &npvp[MAXATOM]) 91356678Seric { 91458151Seric syserr("554 rewrite: expansion too long"); 91559084Seric return EX_DATAERR; 91656678Seric } 91716914Seric *avp++ = *pp++; 9183149Seric } 9193149Seric } 92016914Seric else 9218226Seric { 92216914Seric /* vanilla replacement */ 9239374Seric if (avp >= &npvp[MAXATOM]) 92416889Seric { 92556678Seric toolong: 92658151Seric syserr("554 rewrite: expansion too long"); 92759084Seric return EX_DATAERR; 92816889Seric } 92959027Seric if ((*rp & 0377) != MACRODEXPAND) 93059027Seric *avp++ = rp; 93159027Seric else 93259027Seric { 93359027Seric *avp = macvalue(rp[1], e); 93459027Seric if (tTd(21, 2)) 93559027Seric printf("rewrite: RHS $&%c => \"%s\"\n", 93659027Seric rp[1], 93759027Seric *avp == NULL ? "(NULL)" : *avp); 93859027Seric if (*avp != NULL) 93959027Seric avp++; 94059027Seric } 9418226Seric } 9429374Seric } 9439374Seric *avp++ = NULL; 94416914Seric 94516914Seric /* 94656678Seric ** Check for any hostname/keyword lookups. 94716914Seric */ 94816914Seric 94916914Seric for (rvp = npvp; *rvp != NULL; rvp++) 95016914Seric { 95156678Seric char **hbrvp; 95216914Seric char **xpvp; 95316914Seric int trsize; 95417473Seric char *olddelimchar; 95556678Seric char *replac; 95656678Seric int endtoken; 95756678Seric STAB *map; 95856678Seric char *mapname; 95956678Seric char **key_rvp; 96056678Seric char **arg_rvp; 96156678Seric char **default_rvp; 96256678Seric char buf[MAXNAME + 1]; 96316914Seric char *pvpb1[MAXATOM + 1]; 96456823Seric char *argvect[10]; 96517174Seric char pvpbuf[PSBUFSIZE]; 96616914Seric 96758050Seric if ((**rvp & 0377) != HOSTBEGIN && 96858050Seric (**rvp & 0377) != LOOKUPBEGIN) 96916914Seric continue; 97016914Seric 97116914Seric /* 97256678Seric ** Got a hostname/keyword lookup. 97316914Seric ** 97416914Seric ** This could be optimized fairly easily. 97516914Seric */ 97616914Seric 97716914Seric hbrvp = rvp; 97858050Seric if ((**rvp & 0377) == HOSTBEGIN) 97956327Seric { 98056678Seric endtoken = HOSTEND; 98156678Seric mapname = "host"; 98256327Seric } 98356326Seric else 98456327Seric { 98556678Seric endtoken = LOOKUPEND; 98656678Seric mapname = *++rvp; 98756327Seric } 98856678Seric map = stab(mapname, ST_MAP, ST_FIND); 98956678Seric if (map == NULL) 99058151Seric syserr("554 rewrite: map %s not found", mapname); 99156678Seric 99256678Seric /* extract the match part */ 99356678Seric key_rvp = ++rvp; 99456823Seric default_rvp = NULL; 99556823Seric arg_rvp = argvect; 99656823Seric xpvp = NULL; 99756823Seric replac = pvpbuf; 99858050Seric while (*rvp != NULL && (**rvp & 0377) != endtoken) 99953654Seric { 100058050Seric int nodetype = **rvp & 0377; 100156823Seric 100256823Seric if (nodetype != CANONHOST && nodetype != CANONUSER) 100356678Seric { 100456823Seric rvp++; 100556823Seric continue; 100656823Seric } 100756823Seric 100856823Seric *rvp++ = NULL; 100956823Seric 101056823Seric if (xpvp != NULL) 101156823Seric { 101258814Seric cataddr(xpvp, NULL, replac, 101358082Seric &pvpbuf[sizeof pvpbuf] - replac, 101458082Seric '\0'); 101556823Seric *++arg_rvp = replac; 101656823Seric replac += strlen(replac) + 1; 101756823Seric xpvp = NULL; 101856823Seric } 101956823Seric switch (nodetype) 102056823Seric { 102156678Seric case CANONHOST: 102256823Seric xpvp = rvp; 102356678Seric break; 102456678Seric 102556678Seric case CANONUSER: 102656678Seric default_rvp = rvp; 102756678Seric break; 102856678Seric } 102953654Seric } 103016914Seric if (*rvp != NULL) 103116914Seric *rvp++ = NULL; 103256823Seric if (xpvp != NULL) 103356823Seric { 103458814Seric cataddr(xpvp, NULL, replac, 103558082Seric &pvpbuf[sizeof pvpbuf] - replac, 103658082Seric '\0'); 103756823Seric *++arg_rvp = replac; 103856823Seric } 103956823Seric *++arg_rvp = NULL; 104016914Seric 104116914Seric /* save the remainder of the input string */ 104216914Seric trsize = (int) (avp - rvp + 1) * sizeof *rvp; 104316914Seric bcopy((char *) rvp, (char *) pvpb1, trsize); 104416914Seric 104556678Seric /* look it up */ 104658814Seric cataddr(key_rvp, NULL, buf, sizeof buf, '\0'); 104756823Seric argvect[0] = buf; 104856678Seric if (map != NULL && bitset(MF_VALID, map->s_map.map_flags)) 104956836Seric { 105056836Seric int bsize = sizeof buf - 1; 105159084Seric auto int stat = EX_OK; 105256836Seric 105356836Seric if (map->s_map.map_app != NULL) 105456836Seric bsize -= strlen(map->s_map.map_app); 105558796Seric if (tTd(60, 1)) 105658796Seric printf("map_lookup(%s, %s) => ", 105758796Seric mapname, buf); 105856836Seric replac = (*map->s_map.map_class->map_lookup)(&map->s_map, 105959084Seric buf, sizeof buf - 1, argvect, 106059084Seric &stat); 106156836Seric if (replac != NULL && map->s_map.map_app != NULL) 106256836Seric strcat(replac, map->s_map.map_app); 106358796Seric if (tTd(60, 1)) 106459084Seric printf("%s (%d)\n", 106559084Seric replac ? replac : "NOT FOUND", 106659084Seric stat); 106759084Seric 106859084Seric /* should recover if stat == EX_TEMPFAIL */ 106959084Seric if (stat == EX_TEMPFAIL) 107059084Seric rstat = stat; 107156836Seric } 107253654Seric else 107356678Seric replac = NULL; 107456678Seric 107556678Seric /* if no replacement, use default */ 107656823Seric if (replac == NULL && default_rvp != NULL) 107756823Seric { 107856823Seric char buf2[sizeof buf]; 107956823Seric 108056823Seric /* rewrite the default with % translations */ 108158814Seric cataddr(default_rvp, NULL, buf2, sizeof buf2, '\0'); 108256823Seric map_rewrite(buf2, sizeof buf2, buf, sizeof buf, 108356823Seric argvect); 108456823Seric replac = buf; 108556823Seric } 108656823Seric 108756678Seric if (replac == NULL) 108851317Seric { 108956823Seric xpvp = key_rvp; 109053654Seric } 109156678Seric else 109253654Seric { 109356678Seric /* scan the new replacement */ 109458333Seric xpvp = prescan(replac, '\0', pvpbuf, NULL); 109553654Seric if (xpvp == NULL) 109651317Seric { 109758403Seric /* prescan already printed error */ 109859084Seric return EX_DATAERR; 109956678Seric } 110051317Seric } 110151317Seric 110216914Seric /* append it to the token list */ 110356678Seric for (avp = hbrvp; *xpvp != NULL; xpvp++) 110456678Seric { 110517174Seric *avp++ = newstr(*xpvp); 110616920Seric if (avp >= &npvp[MAXATOM]) 110716914Seric goto toolong; 110817174Seric } 110916914Seric 111016914Seric /* restore the old trailing information */ 111156678Seric for (xpvp = pvpb1; (*avp++ = *xpvp++) != NULL; ) 111216920Seric if (avp >= &npvp[MAXATOM]) 111316914Seric goto toolong; 111417174Seric 111556678Seric break; 111616914Seric } 111716914Seric 111816914Seric /* 111916914Seric ** Check for subroutine calls. 112016914Seric */ 112116914Seric 112258050Seric if (*npvp != NULL && (**npvp & 0377) == CALLSUBR) 112356678Seric { 112459084Seric int stat; 112559084Seric 112656678Seric bcopy((char *) &npvp[2], (char *) pvp, 112756678Seric (int) (avp - npvp - 2) * sizeof *avp); 112856678Seric if (tTd(21, 3)) 112956678Seric printf("-----callsubr %s\n", npvp[1]); 113059084Seric stat = rewrite(pvp, atoi(npvp[1]), e); 113159084Seric if (rstat == EX_OK || stat == EX_TEMPFAIL) 113259084Seric rstat = stat; 113356678Seric } 113456678Seric else 113556678Seric { 113617348Seric bcopy((char *) npvp, (char *) pvp, 113716900Seric (int) (avp - npvp) * sizeof *avp); 113856678Seric } 11399374Seric if (tTd(21, 4)) 11409374Seric { 11419374Seric printf("rewritten as:"); 114256678Seric printav(pvp); 11439374Seric } 1144297Seric } 11458069Seric 11469279Seric if (OpMode == MD_TEST || tTd(21, 2)) 11478069Seric { 11488959Seric printf("rewrite: ruleset %2d returns:", ruleset); 114956678Seric printav(pvp); 11508069Seric } 115159084Seric 115259084Seric return rstat; 11533149Seric } 11543149Seric /* 11553149Seric ** BUILDADDR -- build address from token vector. 11563149Seric ** 11573149Seric ** Parameters: 11583149Seric ** tv -- token vector. 11593149Seric ** a -- pointer to address descriptor to fill. 11603149Seric ** If NULL, one will be allocated. 116158966Seric ** e -- the current envelope. 11623149Seric ** 11633149Seric ** Returns: 11644279Seric ** NULL if there was an error. 11654279Seric ** 'a' otherwise. 11663149Seric ** 11673149Seric ** Side Effects: 11683149Seric ** fills in 'a' 11693149Seric */ 11703149Seric 117157249Seric struct errcodes 117257249Seric { 117357249Seric char *ec_name; /* name of error code */ 117457249Seric int ec_code; /* numeric code */ 117557249Seric } ErrorCodes[] = 117657249Seric { 117757249Seric "usage", EX_USAGE, 117857249Seric "nouser", EX_NOUSER, 117957249Seric "nohost", EX_NOHOST, 118057249Seric "unavailable", EX_UNAVAILABLE, 118157249Seric "software", EX_SOFTWARE, 118257249Seric "tempfail", EX_TEMPFAIL, 118357249Seric "protocol", EX_PROTOCOL, 118457249Seric #ifdef EX_CONFIG 118557249Seric "config", EX_CONFIG, 118657249Seric #endif 118757249Seric NULL, EX_UNAVAILABLE, 118857249Seric }; 118957249Seric 119056678Seric ADDRESS * 119158966Seric buildaddr(tv, a, e) 11923149Seric register char **tv; 11933149Seric register ADDRESS *a; 119458966Seric register ENVELOPE *e; 11953149Seric { 11963149Seric struct mailer **mp; 11973149Seric register struct mailer *m; 119858008Seric char *bp; 119958008Seric int spaceleft; 120057402Seric static char buf[MAXNAME]; 12013149Seric 12023149Seric if (a == NULL) 12033149Seric a = (ADDRESS *) xalloc(sizeof *a); 120416889Seric bzero((char *) a, sizeof *a); 12053149Seric 12063149Seric /* figure out what net/mailer to use */ 120758050Seric if ((**tv & 0377) != CANONNET) 12084279Seric { 120958151Seric syserr("554 buildaddr: no net"); 12104279Seric return (NULL); 12114279Seric } 12123149Seric tv++; 121358680Seric if (strcasecmp(*tv, "error") == 0) 12144279Seric { 121558050Seric if ((**++tv & 0377) == CANONHOST) 121610183Seric { 121757249Seric register struct errcodes *ep; 121857249Seric 121958050Seric if (isascii(**++tv) && isdigit(**tv)) 122057249Seric { 122157249Seric setstat(atoi(*tv)); 122257249Seric } 122357249Seric else 122457249Seric { 122557249Seric for (ep = ErrorCodes; ep->ec_name != NULL; ep++) 122657249Seric if (strcasecmp(ep->ec_name, *tv) == 0) 122757249Seric break; 122857249Seric setstat(ep->ec_code); 122957249Seric } 123010183Seric tv++; 123110183Seric } 123258050Seric if ((**tv & 0377) != CANONUSER) 123358151Seric syserr("554 buildaddr: error: no user"); 123458814Seric cataddr(++tv, NULL, buf, sizeof buf, ' '); 123558082Seric stripquotes(buf); 12364279Seric usrerr(buf); 12374279Seric return (NULL); 12384279Seric } 123957402Seric 12404598Seric for (mp = Mailer; (m = *mp++) != NULL; ) 12413149Seric { 124258680Seric if (strcasecmp(m->m_name, *tv) == 0) 12433149Seric break; 12443149Seric } 12453149Seric if (m == NULL) 12464279Seric { 124758151Seric syserr("554 buildaddr: unknown mailer %s", *tv); 12484279Seric return (NULL); 12494279Seric } 12504598Seric a->q_mailer = m; 12513149Seric 12523149Seric /* figure out what host (if any) */ 125356678Seric tv++; 125458509Seric if ((**tv & 0377) == CANONHOST) 12553149Seric { 125658008Seric bp = buf; 125758008Seric spaceleft = sizeof buf - 1; 125858050Seric while (*++tv != NULL && (**tv & 0377) != CANONUSER) 125958008Seric { 126058008Seric int i = strlen(*tv); 126158008Seric 126258008Seric if (i > spaceleft) 126358008Seric { 126458008Seric /* out of space for this address */ 126558008Seric if (spaceleft >= 0) 126658151Seric syserr("554 buildaddr: host too long (%.40s...)", 126758008Seric buf); 126858008Seric i = spaceleft; 126958008Seric spaceleft = 0; 127058008Seric } 127158008Seric if (i <= 0) 127258008Seric continue; 127358008Seric bcopy(*tv, bp, i); 127458008Seric bp += i; 127558008Seric spaceleft -= i; 127658008Seric } 127758010Seric *bp = '\0'; 12785704Seric a->q_host = newstr(buf); 12793149Seric } 128057249Seric else 128158509Seric { 128258509Seric if (!bitnset(M_LOCALMAILER, m->m_flags)) 128358509Seric { 128458509Seric syserr("554 buildaddr: no host"); 128558509Seric return (NULL); 128658509Seric } 128757249Seric a->q_host = NULL; 128858509Seric } 12893149Seric 12903149Seric /* figure out the user */ 129158050Seric if (*tv == NULL || (**tv & 0377) != CANONUSER) 12924279Seric { 129358151Seric syserr("554 buildaddr: no user"); 12944279Seric return (NULL); 12954279Seric } 129657402Seric tv++; 129751317Seric 129857402Seric /* do special mapping for local mailer */ 129957402Seric if (m == LocalMailer && *tv != NULL) 130057402Seric { 130157454Seric register char *p = *tv; 130257454Seric 130357454Seric if (*p == '"') 130457454Seric p++; 130557454Seric if (*p == '|') 130657402Seric a->q_mailer = m = ProgMailer; 130757454Seric else if (*p == '/') 130857402Seric a->q_mailer = m = FileMailer; 130957454Seric else if (*p == ':') 131057402Seric { 131157402Seric /* may be :include: */ 131258814Seric cataddr(tv, NULL, buf, sizeof buf, '\0'); 131358008Seric stripquotes(buf); 131458008Seric if (strncasecmp(buf, ":include:", 9) == 0) 131558008Seric { 131658008Seric /* if :include:, don't need further rewriting */ 131757402Seric a->q_mailer = m = InclMailer; 131858008Seric a->q_user = &buf[9]; 131958008Seric return (a); 132058008Seric } 132157402Seric } 132257402Seric } 132357402Seric 132458008Seric if (m == LocalMailer && *tv != NULL && strcmp(*tv, "@") == 0) 132558008Seric { 132658008Seric tv++; 132758008Seric a->q_flags |= QNOTREMOTE; 132858008Seric } 132958008Seric 133058673Seric /* do cleanup of final address */ 133159084Seric (void) rewrite(tv, 4, e); 133219040Seric 133319040Seric /* save the result for the command line/RCPT argument */ 133458814Seric cataddr(tv, NULL, buf, sizeof buf, '\0'); 13353149Seric a->q_user = buf; 13363149Seric 133758670Seric /* 133858670Seric ** Do mapping to lower case as requested by mailer 133958670Seric */ 134058670Seric 134158670Seric if (a->q_host != NULL && !bitnset(M_HST_UPPER, m->m_flags)) 134258670Seric makelower(a->q_host); 134358670Seric if (!bitnset(M_USR_UPPER, m->m_flags)) 134458670Seric makelower(a->q_user); 134558670Seric 13463149Seric return (a); 13473149Seric } 13483188Seric /* 13494228Seric ** CATADDR -- concatenate pieces of addresses (putting in <LWSP> subs) 13504228Seric ** 13514228Seric ** Parameters: 13524228Seric ** pvp -- parameter vector to rebuild. 135358814Seric ** evp -- last parameter to include. Can be NULL to 135458814Seric ** use entire pvp. 13554228Seric ** buf -- buffer to build the string into. 13564228Seric ** sz -- size of buf. 135758082Seric ** spacesub -- the space separator character; if null, 135858082Seric ** use SpaceSub. 13594228Seric ** 13604228Seric ** Returns: 13614228Seric ** none. 13624228Seric ** 13634228Seric ** Side Effects: 13644228Seric ** Destroys buf. 13654228Seric */ 13664228Seric 136758814Seric cataddr(pvp, evp, buf, sz, spacesub) 13684228Seric char **pvp; 136958814Seric char **evp; 13704228Seric char *buf; 13714228Seric register int sz; 137258082Seric char spacesub; 13734228Seric { 13744228Seric bool oatomtok = FALSE; 137556678Seric bool natomtok = FALSE; 13764228Seric register int i; 13774228Seric register char *p; 13784228Seric 137958082Seric if (spacesub == '\0') 138058082Seric spacesub = SpaceSub; 138158082Seric 13828423Seric if (pvp == NULL) 13838423Seric { 138423109Seric (void) strcpy(buf, ""); 13858423Seric return; 13868423Seric } 13874228Seric p = buf; 138811156Seric sz -= 2; 13894228Seric while (*pvp != NULL && (i = strlen(*pvp)) < sz) 13904228Seric { 13918078Seric natomtok = (toktype(**pvp) == ATM); 13924228Seric if (oatomtok && natomtok) 139358082Seric *p++ = spacesub; 13944228Seric (void) strcpy(p, *pvp); 13954228Seric oatomtok = natomtok; 13964228Seric p += i; 139711156Seric sz -= i + 1; 139858814Seric if (pvp++ == evp) 139958814Seric break; 14004228Seric } 14014228Seric *p = '\0'; 14024228Seric } 14034228Seric /* 14043188Seric ** SAMEADDR -- Determine if two addresses are the same 14053188Seric ** 14063188Seric ** This is not just a straight comparison -- if the mailer doesn't 14073188Seric ** care about the host we just ignore it, etc. 14083188Seric ** 14093188Seric ** Parameters: 14103188Seric ** a, b -- pointers to the internal forms to compare. 14113188Seric ** 14123188Seric ** Returns: 14133188Seric ** TRUE -- they represent the same mailbox. 14143188Seric ** FALSE -- they don't. 14153188Seric ** 14163188Seric ** Side Effects: 14173188Seric ** none. 14183188Seric */ 14193188Seric 14203188Seric bool 14219374Seric sameaddr(a, b) 14223188Seric register ADDRESS *a; 14233188Seric register ADDRESS *b; 14243188Seric { 14253188Seric /* if they don't have the same mailer, forget it */ 14263188Seric if (a->q_mailer != b->q_mailer) 14273188Seric return (FALSE); 14283188Seric 14293188Seric /* if the user isn't the same, we can drop out */ 143056678Seric if (strcmp(a->q_user, b->q_user) != 0) 14313188Seric return (FALSE); 14323188Seric 143358438Seric /* if we have good uids for both but the differ, these are different */ 143458438Seric if (bitset(QGOODUID, a->q_flags & b->q_flags) && a->q_uid != b->q_uid) 143558438Seric return (FALSE); 143658438Seric 143758509Seric /* otherwise compare hosts (but be careful for NULL ptrs) */ 143858509Seric if (a->q_host == b->q_host) 143958509Seric { 144058509Seric /* probably both null pointers */ 14413188Seric return (TRUE); 144258509Seric } 14433188Seric if (a->q_host == NULL || b->q_host == NULL) 144458509Seric { 144558509Seric /* only one is a null pointer */ 14463188Seric return (FALSE); 144758509Seric } 144856678Seric if (strcmp(a->q_host, b->q_host) != 0) 14493188Seric return (FALSE); 14503188Seric 14513188Seric return (TRUE); 14523188Seric } 14533234Seric /* 14543234Seric ** PRINTADDR -- print address (for debugging) 14553234Seric ** 14563234Seric ** Parameters: 14573234Seric ** a -- the address to print 14583234Seric ** follow -- follow the q_next chain. 14593234Seric ** 14603234Seric ** Returns: 14613234Seric ** none. 14623234Seric ** 14633234Seric ** Side Effects: 14643234Seric ** none. 14653234Seric */ 14663234Seric 14673234Seric printaddr(a, follow) 14683234Seric register ADDRESS *a; 14693234Seric bool follow; 14703234Seric { 14715001Seric bool first = TRUE; 147257731Seric register MAILER *m; 147357731Seric MAILER pseudomailer; 14745001Seric 14753234Seric while (a != NULL) 14763234Seric { 14775001Seric first = FALSE; 14784443Seric printf("%x=", a); 14794085Seric (void) fflush(stdout); 148057731Seric 148157731Seric /* find the mailer -- carefully */ 148257731Seric m = a->q_mailer; 148357731Seric if (m == NULL) 148457731Seric { 148557731Seric m = &pseudomailer; 148657731Seric m->m_mno = -1; 148757731Seric m->m_name = "NULL"; 148857731Seric } 148957731Seric 149058680Seric printf("%s:\n\tmailer %d (%s), host `%s', user `%s', ruser `%s'\n", 149157731Seric a->q_paddr, m->m_mno, m->m_name, 149240973Sbostic a->q_host, a->q_user, a->q_ruser? a->q_ruser: "<null>"); 149359269Seric printf("\tnext=%x, flags=%o, alias %x, uid %d, gid %d\n", 149459269Seric a->q_next, a->q_flags, a->q_alias, a->q_uid, a->q_gid); 149559269Seric printf("\towner=%s, home=\"%s\", fullname=\"%s\"\n", 149659269Seric a->q_owner == NULL ? "(none)" : a->q_owner, 149759269Seric a->q_home, a->q_fullname); 14984996Seric 14993234Seric if (!follow) 15003234Seric return; 15014996Seric a = a->q_next; 15023234Seric } 15035001Seric if (first) 15044443Seric printf("[NULL]\n"); 15053234Seric } 15064317Seric 15077682Seric /* 15087682Seric ** REMOTENAME -- return the name relative to the current mailer 15097682Seric ** 15107682Seric ** Parameters: 15117682Seric ** name -- the name to translate. 15128069Seric ** m -- the mailer that we want to do rewriting relative 15138069Seric ** to. 151459163Seric ** flags -- fine tune operations. 151559163Seric ** pstat -- pointer to status word. 151658020Seric ** e -- the current envelope. 15177682Seric ** 15187682Seric ** Returns: 15197682Seric ** the text string representing this address relative to 15207682Seric ** the receiving mailer. 15217682Seric ** 15227682Seric ** Side Effects: 15237682Seric ** none. 15247682Seric ** 15257682Seric ** Warnings: 15267682Seric ** The text string returned is tucked away locally; 15277682Seric ** copy it if you intend to save it. 15287682Seric */ 15297682Seric 15307682Seric char * 153159163Seric remotename(name, m, flags, pstat, e) 15327682Seric char *name; 153356678Seric struct mailer *m; 153459163Seric int flags; 153559163Seric int *pstat; 153656678Seric register ENVELOPE *e; 15377682Seric { 15388069Seric register char **pvp; 15398069Seric char *fancy; 154056678Seric extern char *macvalue(); 154156678Seric char *oldg = macvalue('g', e); 154258020Seric int rwset; 15437682Seric static char buf[MAXNAME]; 15447682Seric char lbuf[MAXNAME]; 154516914Seric char pvpbuf[PSBUFSIZE]; 154656678Seric extern char **prescan(); 154756678Seric extern char *crackaddr(); 15487682Seric 15497755Seric if (tTd(12, 1)) 15507755Seric printf("remotename(%s)\n", name); 15517755Seric 155210177Seric /* don't do anything if we are tagging it as special */ 155359163Seric if (bitset(RF_SENDERADDR, flags)) 155459163Seric rwset = bitset(RF_HEADERADDR, flags) ? m->m_sh_rwset 155559163Seric : m->m_se_rwset; 155658020Seric else 155759163Seric rwset = bitset(RF_HEADERADDR, flags) ? m->m_rh_rwset 155859163Seric : m->m_re_rwset; 155958020Seric if (rwset < 0) 156010177Seric return (name); 156110177Seric 15627682Seric /* 15638181Seric ** Do a heuristic crack of this name to extract any comment info. 15648181Seric ** This will leave the name as a comment and a $g macro. 15657889Seric */ 15667889Seric 156759163Seric if (bitset(RF_CANONICAL, flags) || bitnset(M_NOCOMMENT, m->m_flags)) 156858050Seric fancy = "\201g"; 156910310Seric else 157010310Seric fancy = crackaddr(name); 15717889Seric 15728181Seric /* 15738181Seric ** Turn the name into canonical form. 15748181Seric ** Normally this will be RFC 822 style, i.e., "user@domain". 15758181Seric ** If this only resolves to "user", and the "C" flag is 15768181Seric ** specified in the sending mailer, then the sender's 15778181Seric ** domain will be appended. 15788181Seric */ 15798181Seric 158058333Seric pvp = prescan(name, '\0', pvpbuf, NULL); 15817889Seric if (pvp == NULL) 15827889Seric return (name); 158359163Seric if (rewrite(pvp, 3, e) == EX_TEMPFAIL) 158459163Seric *pstat = EX_TEMPFAIL; 158559163Seric if (bitset(RF_ADDDOMAIN, flags) && e->e_fromdomain != NULL) 15868181Seric { 15878181Seric /* append from domain to this address */ 15888181Seric register char **pxp = pvp; 15898181Seric 15909594Seric /* see if there is an "@domain" in the current name */ 15918181Seric while (*pxp != NULL && strcmp(*pxp, "@") != 0) 15928181Seric pxp++; 15938181Seric if (*pxp == NULL) 15948181Seric { 15959594Seric /* no.... append the "@domain" from the sender */ 159656678Seric register char **qxq = e->e_fromdomain; 15978181Seric 15989594Seric while ((*pxp++ = *qxq++) != NULL) 15999594Seric continue; 160059163Seric if (rewrite(pvp, 3, e) == EX_TEMPFAIL) 160159163Seric *pstat = EX_TEMPFAIL; 16028181Seric } 16038181Seric } 16048181Seric 16058181Seric /* 16068959Seric ** Do more specific rewriting. 160756678Seric ** Rewrite using ruleset 1 or 2 depending on whether this is 160856678Seric ** a sender address or not. 16098181Seric ** Then run it through any receiving-mailer-specific rulesets. 16108181Seric */ 16118181Seric 161259163Seric if (bitset(RF_SENDERADDR, flags)) 161359541Seric { 161459163Seric if (rewrite(pvp, 1, e) == EX_TEMPFAIL) 161559163Seric *pstat = EX_TEMPFAIL; 161659541Seric } 16178069Seric else 161859541Seric { 161959163Seric if (rewrite(pvp, 2, e) == EX_TEMPFAIL) 162059163Seric *pstat = EX_TEMPFAIL; 162159541Seric } 162258020Seric if (rwset > 0) 162359541Seric { 162459163Seric if (rewrite(pvp, rwset, e) == EX_TEMPFAIL) 162559163Seric *pstat = EX_TEMPFAIL; 162659541Seric } 16277682Seric 16288181Seric /* 16298959Seric ** Do any final sanitation the address may require. 16308959Seric ** This will normally be used to turn internal forms 16318959Seric ** (e.g., user@host.LOCAL) into external form. This 16328959Seric ** may be used as a default to the above rules. 16338959Seric */ 16348959Seric 163559163Seric if (rewrite(pvp, 4, e) == EX_TEMPFAIL) 163659163Seric *pstat = EX_TEMPFAIL; 16378959Seric 16388959Seric /* 16398181Seric ** Now restore the comment information we had at the beginning. 16408181Seric */ 16418181Seric 164258825Seric cataddr(pvp, NULL, lbuf, sizeof lbuf, '\0'); 164356678Seric define('g', lbuf, e); 164456678Seric expand(fancy, buf, &buf[sizeof buf - 1], e); 164556678Seric define('g', oldg, e); 16467682Seric 16477682Seric if (tTd(12, 1)) 16487755Seric printf("remotename => `%s'\n", buf); 16497682Seric return (buf); 16507682Seric } 165151317Seric /* 165256678Seric ** MAPLOCALUSER -- run local username through ruleset 5 for final redirection 165351317Seric ** 165451317Seric ** Parameters: 165556678Seric ** a -- the address to map (but just the user name part). 165656678Seric ** sendq -- the sendq in which to install any replacement 165756678Seric ** addresses. 165851317Seric ** 165951317Seric ** Returns: 166051317Seric ** none. 166151317Seric */ 166251317Seric 166356678Seric maplocaluser(a, sendq, e) 166456678Seric register ADDRESS *a; 166556678Seric ADDRESS **sendq; 166656678Seric ENVELOPE *e; 166751317Seric { 166856678Seric register char **pvp; 166956678Seric register ADDRESS *a1 = NULL; 167058333Seric auto char *delimptr; 167156678Seric char pvpbuf[PSBUFSIZE]; 167251317Seric 167356678Seric if (tTd(29, 1)) 167456678Seric { 167556678Seric printf("maplocaluser: "); 167656678Seric printaddr(a, FALSE); 167756678Seric } 167858333Seric pvp = prescan(a->q_user, '\0', pvpbuf, &delimptr); 167956678Seric if (pvp == NULL) 168056678Seric return; 168151317Seric 168259084Seric (void) rewrite(pvp, 5, e); 168358050Seric if (pvp[0] == NULL || (pvp[0][0] & 0377) != CANONNET) 168456678Seric return; 168551317Seric 168656678Seric /* if non-null, mailer destination specified -- has it changed? */ 168758966Seric a1 = buildaddr(pvp, NULL, e); 168856678Seric if (a1 == NULL || sameaddr(a, a1)) 168956678Seric return; 169051317Seric 169156678Seric /* mark old address as dead; insert new address */ 169256678Seric a->q_flags |= QDONTSEND; 169357731Seric if (tTd(29, 5)) 169457731Seric { 169557731Seric printf("maplocaluser: QDONTSEND "); 169657731Seric printaddr(a, FALSE); 169757731Seric } 169856678Seric a1->q_alias = a; 169958333Seric allocaddr(a1, 1, NULL, delimptr); 170056678Seric (void) recipient(a1, sendq, e); 170151317Seric } 170258802Seric /* 170358802Seric ** DEQUOTE_INIT -- initialize dequote map 170458802Seric ** 170558802Seric ** This is a no-op. 170658802Seric ** 170758802Seric ** Parameters: 170858802Seric ** map -- the internal map structure. 170958802Seric ** mapname -- the name of the mapl. 171058802Seric ** args -- arguments. 171158802Seric ** 171258802Seric ** Returns: 171358802Seric ** TRUE. 171458802Seric */ 171558802Seric 171658802Seric bool 171758802Seric dequote_init(map, mapname, args) 171858802Seric MAP *map; 171958802Seric char *mapname; 172058802Seric char *args; 172158802Seric { 172258805Seric register char *p = args; 172358805Seric 172458805Seric for (;;) 172558805Seric { 172658805Seric while (isascii(*p) && isspace(*p)) 172758805Seric p++; 172858805Seric if (*p != '-') 172958805Seric break; 173058805Seric switch (*++p) 173158805Seric { 173258805Seric case 'a': 173358805Seric map->map_app = ++p; 173458805Seric break; 173558805Seric } 173658805Seric while (*p != '\0' && !(isascii(*p) && isspace(*p))) 173758805Seric p++; 173858805Seric if (*p != '\0') 173958805Seric *p = '\0'; 174058805Seric } 174158805Seric if (map->map_app != NULL) 174258805Seric map->map_app = newstr(map->map_app); 174358805Seric 174458802Seric return TRUE; 174558802Seric } 174658802Seric /* 174758802Seric ** DEQUOTE_MAP -- unquote an address 174858802Seric ** 174958802Seric ** Parameters: 175058802Seric ** map -- the internal map structure (ignored). 175158802Seric ** buf -- the buffer to dequote. 175258802Seric ** bufsiz -- the size of that buffer. 175358802Seric ** av -- arguments (ignored). 175459084Seric ** statp -- pointer to status out-parameter. 175558802Seric ** 175658802Seric ** Returns: 175758802Seric ** NULL -- if there were no quotes, or if the resulting 175858802Seric ** unquoted buffer would not be acceptable to prescan. 175958802Seric ** else -- The dequoted buffer. 176058802Seric */ 176158802Seric 176258802Seric char * 176359084Seric dequote_map(map, buf, bufsiz, av, statp) 176458802Seric MAP *map; 176558802Seric char buf[]; 176658802Seric int bufsiz; 176758802Seric char **av; 176859084Seric int *statp; 176958802Seric { 177058802Seric register char *p; 177158802Seric register char *q; 177258802Seric register char c; 177358802Seric int anglecnt; 177458802Seric int cmntcnt; 177558802Seric int quotecnt; 177659089Seric int spacecnt; 177758802Seric bool quotemode; 177858802Seric bool bslashmode; 177958802Seric 178058802Seric anglecnt = 0; 178158802Seric cmntcnt = 0; 178258802Seric quotecnt = 0; 178359089Seric spacecnt = 0; 178458802Seric quotemode = FALSE; 178558802Seric bslashmode = FALSE; 178658802Seric 178758802Seric for (p = q = buf; (c = *p++) != '\0'; ) 178858802Seric { 178958802Seric if (bslashmode) 179058802Seric { 179158802Seric bslashmode = FALSE; 179258802Seric *q++ = c; 179358802Seric continue; 179458802Seric } 179558802Seric 179658802Seric switch (c) 179758802Seric { 179858802Seric case '\\': 179958802Seric bslashmode = TRUE; 180058802Seric break; 180158802Seric 180258802Seric case '(': 180358802Seric cmntcnt++; 180458802Seric break; 180558802Seric 180658802Seric case ')': 180758802Seric if (cmntcnt-- <= 0) 180858802Seric return NULL; 180958802Seric break; 181059089Seric 181159089Seric case ' ': 181259089Seric spacecnt++; 181359089Seric break; 181458802Seric } 181558802Seric 181658802Seric if (cmntcnt > 0) 181758802Seric { 181858802Seric *q++ = c; 181958802Seric continue; 182058802Seric } 182158802Seric 182258802Seric switch (c) 182358802Seric { 182458802Seric case '"': 182558802Seric quotemode = !quotemode; 182658802Seric quotecnt++; 182758802Seric continue; 182858802Seric 182958802Seric case '<': 183058802Seric anglecnt++; 183158802Seric break; 183258802Seric 183358802Seric case '>': 183458802Seric if (anglecnt-- <= 0) 183558802Seric return NULL; 183658802Seric break; 183758802Seric } 183858802Seric *q++ = c; 183958802Seric } 184058802Seric 184158802Seric if (anglecnt != 0 || cmntcnt != 0 || bslashmode || 184259089Seric quotemode || quotecnt <= 0 || spacecnt != 0) 184358802Seric return NULL; 184458802Seric *q++ = '\0'; 184558802Seric return buf; 184658802Seric } 1847