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*59988Seric static char sccsid[] = "@(#)parseaddr.c 6.51 (Berkeley) 05/13/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 22559747Seric if (savec != '\0') 22659747Seric *delimptr = '\0'; 22756678Seric a->q_paddr = newstr(paddr); 22859747Seric if (savec != '\0') 22959747Seric *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; 32459747Seric 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; 33959747Seric 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; 36259747Seric 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; 44159747Seric 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; 45859747Seric 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; 49859747Seric 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 } 51359747Seric 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, 1059*59988Seric buf, bsize, argvect, 106059084Seric &stat); 106156836Seric if (replac != NULL && map->s_map.map_app != NULL) 1062*59988Seric { 1063*59988Seric if (replac != buf) 1064*59988Seric { 1065*59988Seric if (strlen(replac) > bsize) 1066*59988Seric strncpy(buf, replac, bsize); 1067*59988Seric else 1068*59988Seric strcpy(buf, replac); 1069*59988Seric replac = buf; 1070*59988Seric } 107156836Seric strcat(replac, map->s_map.map_app); 1072*59988Seric } 107358796Seric if (tTd(60, 1)) 107459084Seric printf("%s (%d)\n", 107559084Seric replac ? replac : "NOT FOUND", 107659084Seric stat); 107759084Seric 107859084Seric /* should recover if stat == EX_TEMPFAIL */ 107959084Seric if (stat == EX_TEMPFAIL) 108059084Seric rstat = stat; 108156836Seric } 108253654Seric else 108356678Seric replac = NULL; 108456678Seric 108556678Seric /* if no replacement, use default */ 108656823Seric if (replac == NULL && default_rvp != NULL) 108756823Seric { 108856823Seric char buf2[sizeof buf]; 108956823Seric 109056823Seric /* rewrite the default with % translations */ 109158814Seric cataddr(default_rvp, NULL, buf2, sizeof buf2, '\0'); 109256823Seric map_rewrite(buf2, sizeof buf2, buf, sizeof buf, 109356823Seric argvect); 109456823Seric replac = buf; 109556823Seric } 109656823Seric 109756678Seric if (replac == NULL) 109851317Seric { 109956823Seric xpvp = key_rvp; 110053654Seric } 110156678Seric else 110253654Seric { 110356678Seric /* scan the new replacement */ 110458333Seric xpvp = prescan(replac, '\0', pvpbuf, NULL); 110553654Seric if (xpvp == NULL) 110651317Seric { 110758403Seric /* prescan already printed error */ 110859084Seric return EX_DATAERR; 110956678Seric } 111051317Seric } 111151317Seric 111216914Seric /* append it to the token list */ 111356678Seric for (avp = hbrvp; *xpvp != NULL; xpvp++) 111456678Seric { 111517174Seric *avp++ = newstr(*xpvp); 111616920Seric if (avp >= &npvp[MAXATOM]) 111716914Seric goto toolong; 111817174Seric } 111916914Seric 112016914Seric /* restore the old trailing information */ 112156678Seric for (xpvp = pvpb1; (*avp++ = *xpvp++) != NULL; ) 112216920Seric if (avp >= &npvp[MAXATOM]) 112316914Seric goto toolong; 112417174Seric 112556678Seric break; 112616914Seric } 112716914Seric 112816914Seric /* 112916914Seric ** Check for subroutine calls. 113016914Seric */ 113116914Seric 113258050Seric if (*npvp != NULL && (**npvp & 0377) == CALLSUBR) 113356678Seric { 113459084Seric int stat; 113559084Seric 113656678Seric bcopy((char *) &npvp[2], (char *) pvp, 113756678Seric (int) (avp - npvp - 2) * sizeof *avp); 113856678Seric if (tTd(21, 3)) 113956678Seric printf("-----callsubr %s\n", npvp[1]); 114059084Seric stat = rewrite(pvp, atoi(npvp[1]), e); 114159084Seric if (rstat == EX_OK || stat == EX_TEMPFAIL) 114259084Seric rstat = stat; 114356678Seric } 114456678Seric else 114556678Seric { 114617348Seric bcopy((char *) npvp, (char *) pvp, 114716900Seric (int) (avp - npvp) * sizeof *avp); 114856678Seric } 11499374Seric if (tTd(21, 4)) 11509374Seric { 11519374Seric printf("rewritten as:"); 115256678Seric printav(pvp); 11539374Seric } 1154297Seric } 11558069Seric 11569279Seric if (OpMode == MD_TEST || tTd(21, 2)) 11578069Seric { 11588959Seric printf("rewrite: ruleset %2d returns:", ruleset); 115956678Seric printav(pvp); 11608069Seric } 116159084Seric 116259084Seric return rstat; 11633149Seric } 11643149Seric /* 11653149Seric ** BUILDADDR -- build address from token vector. 11663149Seric ** 11673149Seric ** Parameters: 11683149Seric ** tv -- token vector. 11693149Seric ** a -- pointer to address descriptor to fill. 11703149Seric ** If NULL, one will be allocated. 117158966Seric ** e -- the current envelope. 11723149Seric ** 11733149Seric ** Returns: 11744279Seric ** NULL if there was an error. 11754279Seric ** 'a' otherwise. 11763149Seric ** 11773149Seric ** Side Effects: 11783149Seric ** fills in 'a' 11793149Seric */ 11803149Seric 118157249Seric struct errcodes 118257249Seric { 118357249Seric char *ec_name; /* name of error code */ 118457249Seric int ec_code; /* numeric code */ 118557249Seric } ErrorCodes[] = 118657249Seric { 118757249Seric "usage", EX_USAGE, 118857249Seric "nouser", EX_NOUSER, 118957249Seric "nohost", EX_NOHOST, 119057249Seric "unavailable", EX_UNAVAILABLE, 119157249Seric "software", EX_SOFTWARE, 119257249Seric "tempfail", EX_TEMPFAIL, 119357249Seric "protocol", EX_PROTOCOL, 119457249Seric #ifdef EX_CONFIG 119557249Seric "config", EX_CONFIG, 119657249Seric #endif 119757249Seric NULL, EX_UNAVAILABLE, 119857249Seric }; 119957249Seric 120056678Seric ADDRESS * 120158966Seric buildaddr(tv, a, e) 12023149Seric register char **tv; 12033149Seric register ADDRESS *a; 120458966Seric register ENVELOPE *e; 12053149Seric { 12063149Seric struct mailer **mp; 12073149Seric register struct mailer *m; 120858008Seric char *bp; 120958008Seric int spaceleft; 121057402Seric static char buf[MAXNAME]; 12113149Seric 12123149Seric if (a == NULL) 12133149Seric a = (ADDRESS *) xalloc(sizeof *a); 121416889Seric bzero((char *) a, sizeof *a); 12153149Seric 12163149Seric /* figure out what net/mailer to use */ 121758050Seric if ((**tv & 0377) != CANONNET) 12184279Seric { 121958151Seric syserr("554 buildaddr: no net"); 12204279Seric return (NULL); 12214279Seric } 12223149Seric tv++; 122358680Seric if (strcasecmp(*tv, "error") == 0) 12244279Seric { 122558050Seric if ((**++tv & 0377) == CANONHOST) 122610183Seric { 122757249Seric register struct errcodes *ep; 122857249Seric 122958050Seric if (isascii(**++tv) && isdigit(**tv)) 123057249Seric { 123157249Seric setstat(atoi(*tv)); 123257249Seric } 123357249Seric else 123457249Seric { 123557249Seric for (ep = ErrorCodes; ep->ec_name != NULL; ep++) 123657249Seric if (strcasecmp(ep->ec_name, *tv) == 0) 123757249Seric break; 123857249Seric setstat(ep->ec_code); 123957249Seric } 124010183Seric tv++; 124110183Seric } 124258050Seric if ((**tv & 0377) != CANONUSER) 124358151Seric syserr("554 buildaddr: error: no user"); 124458814Seric cataddr(++tv, NULL, buf, sizeof buf, ' '); 124558082Seric stripquotes(buf); 12464279Seric usrerr(buf); 12474279Seric return (NULL); 12484279Seric } 124957402Seric 12504598Seric for (mp = Mailer; (m = *mp++) != NULL; ) 12513149Seric { 125258680Seric if (strcasecmp(m->m_name, *tv) == 0) 12533149Seric break; 12543149Seric } 12553149Seric if (m == NULL) 12564279Seric { 125758151Seric syserr("554 buildaddr: unknown mailer %s", *tv); 12584279Seric return (NULL); 12594279Seric } 12604598Seric a->q_mailer = m; 12613149Seric 12623149Seric /* figure out what host (if any) */ 126356678Seric tv++; 126458509Seric if ((**tv & 0377) == CANONHOST) 12653149Seric { 126658008Seric bp = buf; 126758008Seric spaceleft = sizeof buf - 1; 126858050Seric while (*++tv != NULL && (**tv & 0377) != CANONUSER) 126958008Seric { 127058008Seric int i = strlen(*tv); 127158008Seric 127258008Seric if (i > spaceleft) 127358008Seric { 127458008Seric /* out of space for this address */ 127558008Seric if (spaceleft >= 0) 127658151Seric syserr("554 buildaddr: host too long (%.40s...)", 127758008Seric buf); 127858008Seric i = spaceleft; 127958008Seric spaceleft = 0; 128058008Seric } 128158008Seric if (i <= 0) 128258008Seric continue; 128358008Seric bcopy(*tv, bp, i); 128458008Seric bp += i; 128558008Seric spaceleft -= i; 128658008Seric } 128758010Seric *bp = '\0'; 12885704Seric a->q_host = newstr(buf); 12893149Seric } 129057249Seric else 129158509Seric { 129258509Seric if (!bitnset(M_LOCALMAILER, m->m_flags)) 129358509Seric { 129458509Seric syserr("554 buildaddr: no host"); 129558509Seric return (NULL); 129658509Seric } 129757249Seric a->q_host = NULL; 129858509Seric } 12993149Seric 13003149Seric /* figure out the user */ 130158050Seric if (*tv == NULL || (**tv & 0377) != CANONUSER) 13024279Seric { 130358151Seric syserr("554 buildaddr: no user"); 13044279Seric return (NULL); 13054279Seric } 130657402Seric tv++; 130751317Seric 130857402Seric /* do special mapping for local mailer */ 130957402Seric if (m == LocalMailer && *tv != NULL) 131057402Seric { 131157454Seric register char *p = *tv; 131257454Seric 131357454Seric if (*p == '"') 131457454Seric p++; 131557454Seric if (*p == '|') 131657402Seric a->q_mailer = m = ProgMailer; 131757454Seric else if (*p == '/') 131857402Seric a->q_mailer = m = FileMailer; 131957454Seric else if (*p == ':') 132057402Seric { 132157402Seric /* may be :include: */ 132258814Seric cataddr(tv, NULL, buf, sizeof buf, '\0'); 132358008Seric stripquotes(buf); 132458008Seric if (strncasecmp(buf, ":include:", 9) == 0) 132558008Seric { 132658008Seric /* if :include:, don't need further rewriting */ 132757402Seric a->q_mailer = m = InclMailer; 132858008Seric a->q_user = &buf[9]; 132958008Seric return (a); 133058008Seric } 133157402Seric } 133257402Seric } 133357402Seric 133458008Seric if (m == LocalMailer && *tv != NULL && strcmp(*tv, "@") == 0) 133558008Seric { 133658008Seric tv++; 133758008Seric a->q_flags |= QNOTREMOTE; 133858008Seric } 133958008Seric 134058673Seric /* do cleanup of final address */ 134159084Seric (void) rewrite(tv, 4, e); 134219040Seric 134319040Seric /* save the result for the command line/RCPT argument */ 134458814Seric cataddr(tv, NULL, buf, sizeof buf, '\0'); 13453149Seric a->q_user = buf; 13463149Seric 134758670Seric /* 134858670Seric ** Do mapping to lower case as requested by mailer 134958670Seric */ 135058670Seric 135158670Seric if (a->q_host != NULL && !bitnset(M_HST_UPPER, m->m_flags)) 135258670Seric makelower(a->q_host); 135358670Seric if (!bitnset(M_USR_UPPER, m->m_flags)) 135458670Seric makelower(a->q_user); 135558670Seric 13563149Seric return (a); 13573149Seric } 13583188Seric /* 13594228Seric ** CATADDR -- concatenate pieces of addresses (putting in <LWSP> subs) 13604228Seric ** 13614228Seric ** Parameters: 13624228Seric ** pvp -- parameter vector to rebuild. 136358814Seric ** evp -- last parameter to include. Can be NULL to 136458814Seric ** use entire pvp. 13654228Seric ** buf -- buffer to build the string into. 13664228Seric ** sz -- size of buf. 136758082Seric ** spacesub -- the space separator character; if null, 136858082Seric ** use SpaceSub. 13694228Seric ** 13704228Seric ** Returns: 13714228Seric ** none. 13724228Seric ** 13734228Seric ** Side Effects: 13744228Seric ** Destroys buf. 13754228Seric */ 13764228Seric 137758814Seric cataddr(pvp, evp, buf, sz, spacesub) 13784228Seric char **pvp; 137958814Seric char **evp; 13804228Seric char *buf; 13814228Seric register int sz; 138258082Seric char spacesub; 13834228Seric { 13844228Seric bool oatomtok = FALSE; 138556678Seric bool natomtok = FALSE; 13864228Seric register int i; 13874228Seric register char *p; 13884228Seric 138958082Seric if (spacesub == '\0') 139058082Seric spacesub = SpaceSub; 139158082Seric 13928423Seric if (pvp == NULL) 13938423Seric { 139423109Seric (void) strcpy(buf, ""); 13958423Seric return; 13968423Seric } 13974228Seric p = buf; 139811156Seric sz -= 2; 13994228Seric while (*pvp != NULL && (i = strlen(*pvp)) < sz) 14004228Seric { 14018078Seric natomtok = (toktype(**pvp) == ATM); 14024228Seric if (oatomtok && natomtok) 140358082Seric *p++ = spacesub; 14044228Seric (void) strcpy(p, *pvp); 14054228Seric oatomtok = natomtok; 14064228Seric p += i; 140711156Seric sz -= i + 1; 140858814Seric if (pvp++ == evp) 140958814Seric break; 14104228Seric } 14114228Seric *p = '\0'; 14124228Seric } 14134228Seric /* 14143188Seric ** SAMEADDR -- Determine if two addresses are the same 14153188Seric ** 14163188Seric ** This is not just a straight comparison -- if the mailer doesn't 14173188Seric ** care about the host we just ignore it, etc. 14183188Seric ** 14193188Seric ** Parameters: 14203188Seric ** a, b -- pointers to the internal forms to compare. 14213188Seric ** 14223188Seric ** Returns: 14233188Seric ** TRUE -- they represent the same mailbox. 14243188Seric ** FALSE -- they don't. 14253188Seric ** 14263188Seric ** Side Effects: 14273188Seric ** none. 14283188Seric */ 14293188Seric 14303188Seric bool 14319374Seric sameaddr(a, b) 14323188Seric register ADDRESS *a; 14333188Seric register ADDRESS *b; 14343188Seric { 14353188Seric /* if they don't have the same mailer, forget it */ 14363188Seric if (a->q_mailer != b->q_mailer) 14373188Seric return (FALSE); 14383188Seric 14393188Seric /* if the user isn't the same, we can drop out */ 144056678Seric if (strcmp(a->q_user, b->q_user) != 0) 14413188Seric return (FALSE); 14423188Seric 144358438Seric /* if we have good uids for both but the differ, these are different */ 144458438Seric if (bitset(QGOODUID, a->q_flags & b->q_flags) && a->q_uid != b->q_uid) 144558438Seric return (FALSE); 144658438Seric 144758509Seric /* otherwise compare hosts (but be careful for NULL ptrs) */ 144858509Seric if (a->q_host == b->q_host) 144958509Seric { 145058509Seric /* probably both null pointers */ 14513188Seric return (TRUE); 145258509Seric } 14533188Seric if (a->q_host == NULL || b->q_host == NULL) 145458509Seric { 145558509Seric /* only one is a null pointer */ 14563188Seric return (FALSE); 145758509Seric } 145856678Seric if (strcmp(a->q_host, b->q_host) != 0) 14593188Seric return (FALSE); 14603188Seric 14613188Seric return (TRUE); 14623188Seric } 14633234Seric /* 14643234Seric ** PRINTADDR -- print address (for debugging) 14653234Seric ** 14663234Seric ** Parameters: 14673234Seric ** a -- the address to print 14683234Seric ** follow -- follow the q_next chain. 14693234Seric ** 14703234Seric ** Returns: 14713234Seric ** none. 14723234Seric ** 14733234Seric ** Side Effects: 14743234Seric ** none. 14753234Seric */ 14763234Seric 14773234Seric printaddr(a, follow) 14783234Seric register ADDRESS *a; 14793234Seric bool follow; 14803234Seric { 14815001Seric bool first = TRUE; 148257731Seric register MAILER *m; 148357731Seric MAILER pseudomailer; 14845001Seric 14853234Seric while (a != NULL) 14863234Seric { 14875001Seric first = FALSE; 14884443Seric printf("%x=", a); 14894085Seric (void) fflush(stdout); 149057731Seric 149157731Seric /* find the mailer -- carefully */ 149257731Seric m = a->q_mailer; 149357731Seric if (m == NULL) 149457731Seric { 149557731Seric m = &pseudomailer; 149657731Seric m->m_mno = -1; 149757731Seric m->m_name = "NULL"; 149857731Seric } 149957731Seric 150058680Seric printf("%s:\n\tmailer %d (%s), host `%s', user `%s', ruser `%s'\n", 150157731Seric a->q_paddr, m->m_mno, m->m_name, 150240973Sbostic a->q_host, a->q_user, a->q_ruser? a->q_ruser: "<null>"); 150359269Seric printf("\tnext=%x, flags=%o, alias %x, uid %d, gid %d\n", 150459269Seric a->q_next, a->q_flags, a->q_alias, a->q_uid, a->q_gid); 150559269Seric printf("\towner=%s, home=\"%s\", fullname=\"%s\"\n", 150659269Seric a->q_owner == NULL ? "(none)" : a->q_owner, 150759269Seric a->q_home, a->q_fullname); 15084996Seric 15093234Seric if (!follow) 15103234Seric return; 15114996Seric a = a->q_next; 15123234Seric } 15135001Seric if (first) 15144443Seric printf("[NULL]\n"); 15153234Seric } 15164317Seric 15177682Seric /* 15187682Seric ** REMOTENAME -- return the name relative to the current mailer 15197682Seric ** 15207682Seric ** Parameters: 15217682Seric ** name -- the name to translate. 15228069Seric ** m -- the mailer that we want to do rewriting relative 15238069Seric ** to. 152459163Seric ** flags -- fine tune operations. 152559163Seric ** pstat -- pointer to status word. 152658020Seric ** e -- the current envelope. 15277682Seric ** 15287682Seric ** Returns: 15297682Seric ** the text string representing this address relative to 15307682Seric ** the receiving mailer. 15317682Seric ** 15327682Seric ** Side Effects: 15337682Seric ** none. 15347682Seric ** 15357682Seric ** Warnings: 15367682Seric ** The text string returned is tucked away locally; 15377682Seric ** copy it if you intend to save it. 15387682Seric */ 15397682Seric 15407682Seric char * 154159163Seric remotename(name, m, flags, pstat, e) 15427682Seric char *name; 154356678Seric struct mailer *m; 154459163Seric int flags; 154559163Seric int *pstat; 154656678Seric register ENVELOPE *e; 15477682Seric { 15488069Seric register char **pvp; 15498069Seric char *fancy; 155056678Seric extern char *macvalue(); 155156678Seric char *oldg = macvalue('g', e); 155258020Seric int rwset; 15537682Seric static char buf[MAXNAME]; 15547682Seric char lbuf[MAXNAME]; 155516914Seric char pvpbuf[PSBUFSIZE]; 155656678Seric extern char **prescan(); 155756678Seric extern char *crackaddr(); 15587682Seric 15597755Seric if (tTd(12, 1)) 15607755Seric printf("remotename(%s)\n", name); 15617755Seric 156210177Seric /* don't do anything if we are tagging it as special */ 156359163Seric if (bitset(RF_SENDERADDR, flags)) 156459163Seric rwset = bitset(RF_HEADERADDR, flags) ? m->m_sh_rwset 156559163Seric : m->m_se_rwset; 156658020Seric else 156759163Seric rwset = bitset(RF_HEADERADDR, flags) ? m->m_rh_rwset 156859163Seric : m->m_re_rwset; 156958020Seric if (rwset < 0) 157010177Seric return (name); 157110177Seric 15727682Seric /* 15738181Seric ** Do a heuristic crack of this name to extract any comment info. 15748181Seric ** This will leave the name as a comment and a $g macro. 15757889Seric */ 15767889Seric 157759163Seric if (bitset(RF_CANONICAL, flags) || bitnset(M_NOCOMMENT, m->m_flags)) 157858050Seric fancy = "\201g"; 157910310Seric else 158010310Seric fancy = crackaddr(name); 15817889Seric 15828181Seric /* 15838181Seric ** Turn the name into canonical form. 15848181Seric ** Normally this will be RFC 822 style, i.e., "user@domain". 15858181Seric ** If this only resolves to "user", and the "C" flag is 15868181Seric ** specified in the sending mailer, then the sender's 15878181Seric ** domain will be appended. 15888181Seric */ 15898181Seric 159058333Seric pvp = prescan(name, '\0', pvpbuf, NULL); 15917889Seric if (pvp == NULL) 15927889Seric return (name); 159359163Seric if (rewrite(pvp, 3, e) == EX_TEMPFAIL) 159459163Seric *pstat = EX_TEMPFAIL; 159559163Seric if (bitset(RF_ADDDOMAIN, flags) && e->e_fromdomain != NULL) 15968181Seric { 15978181Seric /* append from domain to this address */ 15988181Seric register char **pxp = pvp; 15998181Seric 16009594Seric /* see if there is an "@domain" in the current name */ 16018181Seric while (*pxp != NULL && strcmp(*pxp, "@") != 0) 16028181Seric pxp++; 16038181Seric if (*pxp == NULL) 16048181Seric { 16059594Seric /* no.... append the "@domain" from the sender */ 160656678Seric register char **qxq = e->e_fromdomain; 16078181Seric 16089594Seric while ((*pxp++ = *qxq++) != NULL) 16099594Seric continue; 161059163Seric if (rewrite(pvp, 3, e) == EX_TEMPFAIL) 161159163Seric *pstat = EX_TEMPFAIL; 16128181Seric } 16138181Seric } 16148181Seric 16158181Seric /* 16168959Seric ** Do more specific rewriting. 161756678Seric ** Rewrite using ruleset 1 or 2 depending on whether this is 161856678Seric ** a sender address or not. 16198181Seric ** Then run it through any receiving-mailer-specific rulesets. 16208181Seric */ 16218181Seric 162259163Seric if (bitset(RF_SENDERADDR, flags)) 162359541Seric { 162459163Seric if (rewrite(pvp, 1, e) == EX_TEMPFAIL) 162559163Seric *pstat = EX_TEMPFAIL; 162659541Seric } 16278069Seric else 162859541Seric { 162959163Seric if (rewrite(pvp, 2, e) == EX_TEMPFAIL) 163059163Seric *pstat = EX_TEMPFAIL; 163159541Seric } 163258020Seric if (rwset > 0) 163359541Seric { 163459163Seric if (rewrite(pvp, rwset, e) == EX_TEMPFAIL) 163559163Seric *pstat = EX_TEMPFAIL; 163659541Seric } 16377682Seric 16388181Seric /* 16398959Seric ** Do any final sanitation the address may require. 16408959Seric ** This will normally be used to turn internal forms 16418959Seric ** (e.g., user@host.LOCAL) into external form. This 16428959Seric ** may be used as a default to the above rules. 16438959Seric */ 16448959Seric 164559163Seric if (rewrite(pvp, 4, e) == EX_TEMPFAIL) 164659163Seric *pstat = EX_TEMPFAIL; 16478959Seric 16488959Seric /* 16498181Seric ** Now restore the comment information we had at the beginning. 16508181Seric */ 16518181Seric 165258825Seric cataddr(pvp, NULL, lbuf, sizeof lbuf, '\0'); 165356678Seric define('g', lbuf, e); 165456678Seric expand(fancy, buf, &buf[sizeof buf - 1], e); 165556678Seric define('g', oldg, e); 16567682Seric 16577682Seric if (tTd(12, 1)) 16587755Seric printf("remotename => `%s'\n", buf); 16597682Seric return (buf); 16607682Seric } 166151317Seric /* 166256678Seric ** MAPLOCALUSER -- run local username through ruleset 5 for final redirection 166351317Seric ** 166451317Seric ** Parameters: 166556678Seric ** a -- the address to map (but just the user name part). 166656678Seric ** sendq -- the sendq in which to install any replacement 166756678Seric ** addresses. 166851317Seric ** 166951317Seric ** Returns: 167051317Seric ** none. 167151317Seric */ 167251317Seric 167356678Seric maplocaluser(a, sendq, e) 167456678Seric register ADDRESS *a; 167556678Seric ADDRESS **sendq; 167656678Seric ENVELOPE *e; 167751317Seric { 167856678Seric register char **pvp; 167956678Seric register ADDRESS *a1 = NULL; 168058333Seric auto char *delimptr; 168156678Seric char pvpbuf[PSBUFSIZE]; 168251317Seric 168356678Seric if (tTd(29, 1)) 168456678Seric { 168556678Seric printf("maplocaluser: "); 168656678Seric printaddr(a, FALSE); 168756678Seric } 168858333Seric pvp = prescan(a->q_user, '\0', pvpbuf, &delimptr); 168956678Seric if (pvp == NULL) 169056678Seric return; 169151317Seric 169259084Seric (void) rewrite(pvp, 5, e); 169358050Seric if (pvp[0] == NULL || (pvp[0][0] & 0377) != CANONNET) 169456678Seric return; 169551317Seric 169656678Seric /* if non-null, mailer destination specified -- has it changed? */ 169758966Seric a1 = buildaddr(pvp, NULL, e); 169856678Seric if (a1 == NULL || sameaddr(a, a1)) 169956678Seric return; 170051317Seric 170156678Seric /* mark old address as dead; insert new address */ 170256678Seric a->q_flags |= QDONTSEND; 170357731Seric if (tTd(29, 5)) 170457731Seric { 170557731Seric printf("maplocaluser: QDONTSEND "); 170657731Seric printaddr(a, FALSE); 170757731Seric } 170856678Seric a1->q_alias = a; 170958333Seric allocaddr(a1, 1, NULL, delimptr); 171056678Seric (void) recipient(a1, sendq, e); 171151317Seric } 171258802Seric /* 171358802Seric ** DEQUOTE_INIT -- initialize dequote map 171458802Seric ** 171558802Seric ** This is a no-op. 171658802Seric ** 171758802Seric ** Parameters: 171858802Seric ** map -- the internal map structure. 171958802Seric ** mapname -- the name of the mapl. 172058802Seric ** args -- arguments. 172158802Seric ** 172258802Seric ** Returns: 172358802Seric ** TRUE. 172458802Seric */ 172558802Seric 172658802Seric bool 172758802Seric dequote_init(map, mapname, args) 172858802Seric MAP *map; 172958802Seric char *mapname; 173058802Seric char *args; 173158802Seric { 173258805Seric register char *p = args; 173358805Seric 173458805Seric for (;;) 173558805Seric { 173658805Seric while (isascii(*p) && isspace(*p)) 173758805Seric p++; 173858805Seric if (*p != '-') 173958805Seric break; 174058805Seric switch (*++p) 174158805Seric { 174258805Seric case 'a': 174358805Seric map->map_app = ++p; 174458805Seric break; 174558805Seric } 174658805Seric while (*p != '\0' && !(isascii(*p) && isspace(*p))) 174758805Seric p++; 174858805Seric if (*p != '\0') 174958805Seric *p = '\0'; 175058805Seric } 175158805Seric if (map->map_app != NULL) 175258805Seric map->map_app = newstr(map->map_app); 175358805Seric 175458802Seric return TRUE; 175558802Seric } 175658802Seric /* 175758802Seric ** DEQUOTE_MAP -- unquote an address 175858802Seric ** 175958802Seric ** Parameters: 176058802Seric ** map -- the internal map structure (ignored). 176158802Seric ** buf -- the buffer to dequote. 176258802Seric ** bufsiz -- the size of that buffer. 176358802Seric ** av -- arguments (ignored). 176459084Seric ** statp -- pointer to status out-parameter. 176558802Seric ** 176658802Seric ** Returns: 176758802Seric ** NULL -- if there were no quotes, or if the resulting 176858802Seric ** unquoted buffer would not be acceptable to prescan. 176958802Seric ** else -- The dequoted buffer. 177058802Seric */ 177158802Seric 177258802Seric char * 177359084Seric dequote_map(map, buf, bufsiz, av, statp) 177458802Seric MAP *map; 177558802Seric char buf[]; 177658802Seric int bufsiz; 177758802Seric char **av; 177859084Seric int *statp; 177958802Seric { 178058802Seric register char *p; 178158802Seric register char *q; 178258802Seric register char c; 178358802Seric int anglecnt; 178458802Seric int cmntcnt; 178558802Seric int quotecnt; 178659089Seric int spacecnt; 178758802Seric bool quotemode; 178858802Seric bool bslashmode; 178958802Seric 179058802Seric anglecnt = 0; 179158802Seric cmntcnt = 0; 179258802Seric quotecnt = 0; 179359089Seric spacecnt = 0; 179458802Seric quotemode = FALSE; 179558802Seric bslashmode = FALSE; 179658802Seric 179758802Seric for (p = q = buf; (c = *p++) != '\0'; ) 179858802Seric { 179958802Seric if (bslashmode) 180058802Seric { 180158802Seric bslashmode = FALSE; 180258802Seric *q++ = c; 180358802Seric continue; 180458802Seric } 180558802Seric 180658802Seric switch (c) 180758802Seric { 180858802Seric case '\\': 180958802Seric bslashmode = TRUE; 181058802Seric break; 181158802Seric 181258802Seric case '(': 181358802Seric cmntcnt++; 181458802Seric break; 181558802Seric 181658802Seric case ')': 181758802Seric if (cmntcnt-- <= 0) 181858802Seric return NULL; 181958802Seric break; 182059089Seric 182159089Seric case ' ': 182259089Seric spacecnt++; 182359089Seric break; 182458802Seric } 182558802Seric 182658802Seric if (cmntcnt > 0) 182758802Seric { 182858802Seric *q++ = c; 182958802Seric continue; 183058802Seric } 183158802Seric 183258802Seric switch (c) 183358802Seric { 183458802Seric case '"': 183558802Seric quotemode = !quotemode; 183658802Seric quotecnt++; 183758802Seric continue; 183858802Seric 183958802Seric case '<': 184058802Seric anglecnt++; 184158802Seric break; 184258802Seric 184358802Seric case '>': 184458802Seric if (anglecnt-- <= 0) 184558802Seric return NULL; 184658802Seric break; 184758802Seric } 184858802Seric *q++ = c; 184958802Seric } 185058802Seric 185158802Seric if (anglecnt != 0 || cmntcnt != 0 || bslashmode || 185259089Seric quotemode || quotecnt <= 0 || spacecnt != 0) 185358802Seric return NULL; 185458802Seric *q++ = '\0'; 185558802Seric return buf; 185658802Seric } 1857