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*60502Seric static char sccsid[] = "@(#)parseaddr.c 6.58 (Berkeley) 05/27/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 ADDRESS *buildaddr(); 7357388Seric extern bool invalidaddr(); 74297Seric 75297Seric /* 76297Seric ** Initialize and prescan address. 77297Seric */ 78297Seric 7956678Seric e->e_to = addr; 807675Seric if (tTd(20, 1)) 819888Seric printf("\n--parseaddr(%s)\n", addr); 823188Seric 8357388Seric if (invalidaddr(addr)) 8457388Seric { 8557388Seric if (tTd(20, 1)) 8657388Seric printf("parseaddr-->bad address\n"); 8757388Seric return NULL; 8857388Seric } 8957388Seric 9058333Seric if (delimptr == NULL) 9158333Seric delimptr = &delimptrbuf; 9258333Seric 9358333Seric pvp = prescan(addr, delim, pvpbuf, delimptr); 943149Seric if (pvp == NULL) 9556729Seric { 9656729Seric if (tTd(20, 1)) 9756729Seric printf("parseaddr-->NULL\n"); 98297Seric return (NULL); 9956729Seric } 100297Seric 101297Seric /* 1023149Seric ** Apply rewriting rules. 1037889Seric ** Ruleset 0 does basic parsing. It must resolve. 104297Seric */ 105297Seric 10659084Seric queueup = FALSE; 10759084Seric if (rewrite(pvp, 3, e) == EX_TEMPFAIL) 10859084Seric queueup = TRUE; 10959084Seric if (rewrite(pvp, 0, e) == EX_TEMPFAIL) 11059084Seric queueup = TRUE; 111297Seric 1123149Seric /* 1133149Seric ** See if we resolved to a real mailer. 1143149Seric */ 115297Seric 11659040Seric if (pvp[0] == NULL || (pvp[0][0] & 0377) != CANONNET) 1173149Seric { 1183149Seric setstat(EX_USAGE); 11958151Seric syserr("554 cannot resolve name"); 1203149Seric return (NULL); 121297Seric } 122297Seric 123297Seric /* 1243149Seric ** Build canonical address from pvp. 125297Seric */ 126297Seric 12758966Seric a = buildaddr(pvp, a, e); 1284279Seric if (a == NULL) 1294279Seric return (NULL); 130297Seric 131297Seric /* 1323149Seric ** Make local copies of the host & user and then 1333149Seric ** transport them out. 134297Seric */ 135297Seric 13658333Seric allocaddr(a, copyf, addr, *delimptr); 13756678Seric 13856678Seric /* 13959084Seric ** If there was a parsing failure, mark it for queueing. 14059084Seric */ 14159084Seric 14259084Seric if (queueup) 14359595Seric { 14459734Seric char *msg = "Transient parse error -- message queued for future delivery"; 14559734Seric 14659595Seric if (tTd(20, 1)) 14759595Seric printf("parseaddr: queuing message\n"); 14859734Seric message(msg); 14959734Seric if (e->e_message == NULL) 15060009Seric e->e_message = newstr(msg); 15159084Seric a->q_flags |= QQUEUEUP; 15259595Seric } 15359084Seric 15459084Seric /* 15556678Seric ** Compute return value. 15656678Seric */ 15756678Seric 15856678Seric if (tTd(20, 1)) 1598078Seric { 16056678Seric printf("parseaddr-->"); 16156678Seric printaddr(a, FALSE); 16256678Seric } 16356678Seric 16456678Seric return (a); 16556678Seric } 16656678Seric /* 16757388Seric ** INVALIDADDR -- check for address containing meta-characters 16857388Seric ** 16957388Seric ** Parameters: 17057388Seric ** addr -- the address to check. 17157388Seric ** 17257388Seric ** Returns: 17357388Seric ** TRUE -- if the address has any "wierd" characters 17457388Seric ** FALSE -- otherwise. 17557388Seric */ 17657388Seric 17757388Seric bool 17857388Seric invalidaddr(addr) 17957388Seric register char *addr; 18057388Seric { 18157388Seric for (; *addr != '\0'; addr++) 18257388Seric { 18358050Seric if ((*addr & 0340) != 0200) 18457388Seric continue; 18557388Seric setstat(EX_USAGE); 18658151Seric usrerr("553 Address contained invalid control characters"); 18757388Seric return TRUE; 18857388Seric } 18957388Seric return FALSE; 19057388Seric } 19157388Seric /* 19256678Seric ** ALLOCADDR -- do local allocations of address on demand. 19356678Seric ** 19456678Seric ** Also lowercases the host name if requested. 19556678Seric ** 19656678Seric ** Parameters: 19756678Seric ** a -- the address to reallocate. 19856678Seric ** copyf -- the copy flag (see parseaddr for description). 19956678Seric ** paddr -- the printname of the address. 20058333Seric ** delimptr -- a pointer to the address delimiter. Must be set. 20156678Seric ** 20256678Seric ** Returns: 20356678Seric ** none. 20456678Seric ** 20556678Seric ** Side Effects: 20656678Seric ** Copies portions of a into local buffers as requested. 20756678Seric */ 20856678Seric 20958333Seric allocaddr(a, copyf, paddr, delimptr) 21056678Seric register ADDRESS *a; 21156678Seric int copyf; 21256678Seric char *paddr; 21358333Seric char *delimptr; 21456678Seric { 21556678Seric register MAILER *m = a->q_mailer; 21656678Seric 21758673Seric if (tTd(24, 4)) 21858673Seric printf("allocaddr(copyf=%d, paddr=%s)\n", copyf, paddr); 21958673Seric 22056678Seric if (copyf > 0 && paddr != NULL) 22156678Seric { 22258333Seric char savec = *delimptr; 2238078Seric 22459747Seric if (savec != '\0') 22559747Seric *delimptr = '\0'; 22656678Seric a->q_paddr = newstr(paddr); 22759747Seric if (savec != '\0') 22859747Seric *delimptr = savec; 2298078Seric } 230297Seric else 23156678Seric a->q_paddr = paddr; 23224944Seric 23324944Seric if (a->q_user == NULL) 23424944Seric a->q_user = ""; 23524944Seric if (a->q_host == NULL) 23624944Seric a->q_host = ""; 23724944Seric 2383149Seric if (copyf >= 0) 239297Seric { 24024944Seric a->q_host = newstr(a->q_host); 2413149Seric if (a->q_user != a->q_paddr) 2423149Seric a->q_user = newstr(a->q_user); 243297Seric } 244297Seric 24556678Seric if (a->q_paddr == NULL) 24656678Seric a->q_paddr = a->q_user; 247297Seric } 248297Seric /* 249297Seric ** PRESCAN -- Prescan name and make it canonical 250297Seric ** 2519374Seric ** Scans a name and turns it into a set of tokens. This process 2529374Seric ** deletes blanks and comments (in parentheses). 253297Seric ** 254297Seric ** This routine knows about quoted strings and angle brackets. 255297Seric ** 256297Seric ** There are certain subtleties to this routine. The one that 257297Seric ** comes to mind now is that backslashes on the ends of names 258297Seric ** are silently stripped off; this is intentional. The problem 259297Seric ** is that some versions of sndmsg (like at LBL) set the kill 260297Seric ** character to something other than @ when reading addresses; 261297Seric ** so people type "csvax.eric\@berkeley" -- which screws up the 262297Seric ** berknet mailer. 263297Seric ** 264297Seric ** Parameters: 265297Seric ** addr -- the name to chomp. 266297Seric ** delim -- the delimiter for the address, normally 267297Seric ** '\0' or ','; \0 is accepted in any case. 26815284Seric ** If '\t' then we are reading the .cf file. 26916914Seric ** pvpbuf -- place to put the saved text -- note that 27016914Seric ** the pointers are static. 27158333Seric ** delimptr -- if non-NULL, set to the location of the 27258333Seric ** terminating delimiter. 273297Seric ** 274297Seric ** Returns: 2753149Seric ** A pointer to a vector of tokens. 276297Seric ** NULL on error. 277297Seric */ 278297Seric 2798078Seric /* states and character types */ 2808078Seric # define OPR 0 /* operator */ 2818078Seric # define ATM 1 /* atom */ 2828078Seric # define QST 2 /* in quoted string */ 2838078Seric # define SPC 3 /* chewing up spaces */ 2848078Seric # define ONE 4 /* pick up one character */ 2853149Seric 2868078Seric # define NSTATES 5 /* number of states */ 2878078Seric # define TYPE 017 /* mask to select state type */ 2888078Seric 2898078Seric /* meta bits for table */ 2908078Seric # define M 020 /* meta character; don't pass through */ 2918078Seric # define B 040 /* cause a break */ 2928078Seric # define MB M|B /* meta-break */ 2938078Seric 2948078Seric static short StateTab[NSTATES][NSTATES] = 2958078Seric { 2968087Seric /* oldst chtype> OPR ATM QST SPC ONE */ 2979051Seric /*OPR*/ OPR|B, ATM|B, QST|B, SPC|MB, ONE|B, 2989051Seric /*ATM*/ OPR|B, ATM, QST|B, SPC|MB, ONE|B, 2999051Seric /*QST*/ QST, QST, OPR, QST, QST, 3008078Seric /*SPC*/ OPR, ATM, QST, SPC|M, ONE, 3018078Seric /*ONE*/ OPR, OPR, OPR, OPR, OPR, 3028078Seric }; 3038078Seric 3048078Seric # define NOCHAR -1 /* signal nothing in lookahead token */ 3058078Seric 3063149Seric char ** 30758333Seric prescan(addr, delim, pvpbuf, delimptr) 308297Seric char *addr; 309297Seric char delim; 31016914Seric char pvpbuf[]; 31158333Seric char **delimptr; 312297Seric { 313297Seric register char *p; 3148078Seric register char *q; 3159346Seric register int c; 3163149Seric char **avp; 317297Seric bool bslashmode; 318297Seric int cmntcnt; 3198423Seric int anglecnt; 3203149Seric char *tok; 3218078Seric int state; 3228078Seric int newstate; 32359747Seric char *saveto = CurEnv->e_to; 3248078Seric static char *av[MAXATOM+1]; 32556678Seric extern int errno; 326297Seric 32715253Seric /* make sure error messages don't have garbage on them */ 32815253Seric errno = 0; 32915253Seric 33016914Seric q = pvpbuf; 3313149Seric bslashmode = FALSE; 3327800Seric cmntcnt = 0; 3338423Seric anglecnt = 0; 3343149Seric avp = av; 33556678Seric state = ATM; 3368078Seric c = NOCHAR; 3378078Seric p = addr; 33859747Seric CurEnv->e_to = p; 33956764Seric if (tTd(22, 11)) 340297Seric { 3418078Seric printf("prescan: "); 3428078Seric xputs(p); 34323109Seric (void) putchar('\n'); 3448078Seric } 3458078Seric 3468078Seric do 3478078Seric { 3483149Seric /* read a token */ 3493149Seric tok = q; 3508078Seric for (;;) 351297Seric { 3528078Seric /* store away any old lookahead character */ 35359277Seric if (c != NOCHAR && !bslashmode) 3548078Seric { 35515284Seric /* see if there is room */ 35616914Seric if (q >= &pvpbuf[PSBUFSIZE - 5]) 3578078Seric { 35858151Seric usrerr("553 Address too long"); 35958333Seric if (delimptr != NULL) 36058333Seric *delimptr = p; 36159747Seric CurEnv->e_to = saveto; 3628078Seric return (NULL); 3638078Seric } 36415284Seric 36515284Seric /* squirrel it away */ 3668078Seric *q++ = c; 3678078Seric } 3688078Seric 3698078Seric /* read a new input character */ 3708078Seric c = *p++; 37157631Seric if (c == '\0') 37256764Seric { 37356764Seric /* diagnose and patch up bad syntax */ 37456764Seric if (state == QST) 37556764Seric { 37658151Seric usrerr("553 Unbalanced '\"'"); 37756764Seric c = '"'; 37856764Seric } 37956764Seric else if (cmntcnt > 0) 38056764Seric { 38158151Seric usrerr("553 Unbalanced '('"); 38256764Seric c = ')'; 38356764Seric } 38456764Seric else if (anglecnt > 0) 38556764Seric { 38656764Seric c = '>'; 38758151Seric usrerr("553 Unbalanced '<'"); 38856764Seric } 38956764Seric else 39056764Seric break; 39115284Seric 39256764Seric p--; 39356764Seric } 39457631Seric else if (c == delim && anglecnt <= 0 && 39557631Seric cmntcnt <= 0 && state != QST) 39657631Seric break; 39756764Seric 3988078Seric if (tTd(22, 101)) 3998078Seric printf("c=%c, s=%d; ", c, state); 4008078Seric 4013149Seric /* chew up special characters */ 4023149Seric *q = '\0'; 4033149Seric if (bslashmode) 4043149Seric { 40559105Seric bslashmode = FALSE; 40659105Seric 40724944Seric /* kludge \! for naive users */ 40858061Seric if (cmntcnt > 0) 40959105Seric { 41058061Seric c = NOCHAR; 41159105Seric continue; 41259105Seric } 41359105Seric else if (c != '!' || state == QST) 41459105Seric { 41556678Seric *q++ = '\\'; 41659105Seric continue; 41759105Seric } 4183149Seric } 41956678Seric 42056678Seric if (c == '\\') 4213149Seric { 4223149Seric bslashmode = TRUE; 4233149Seric } 42456678Seric else if (state == QST) 4258514Seric { 4268514Seric /* do nothing, just avoid next clauses */ 4278514Seric } 4288078Seric else if (c == '(') 4294100Seric { 4308078Seric cmntcnt++; 4318078Seric c = NOCHAR; 4324100Seric } 4338078Seric else if (c == ')') 4343149Seric { 4358078Seric if (cmntcnt <= 0) 4363149Seric { 43758151Seric usrerr("553 Unbalanced ')'"); 43858333Seric if (delimptr != NULL) 43958333Seric *delimptr = p; 44059747Seric CurEnv->e_to = saveto; 4418078Seric return (NULL); 4423149Seric } 4438078Seric else 4448078Seric cmntcnt--; 4458078Seric } 4468078Seric else if (cmntcnt > 0) 4478078Seric c = NOCHAR; 4488423Seric else if (c == '<') 4498423Seric anglecnt++; 4508423Seric else if (c == '>') 4518423Seric { 4528423Seric if (anglecnt <= 0) 4538423Seric { 45458151Seric usrerr("553 Unbalanced '>'"); 45558333Seric if (delimptr != NULL) 45658333Seric *delimptr = p; 45759747Seric CurEnv->e_to = saveto; 4588423Seric return (NULL); 4598423Seric } 4608423Seric anglecnt--; 4618423Seric } 46258050Seric else if (delim == ' ' && isascii(c) && isspace(c)) 46311423Seric c = ' '; 4643149Seric 4658078Seric if (c == NOCHAR) 4668078Seric continue; 4673149Seric 4688078Seric /* see if this is end of input */ 46911405Seric if (c == delim && anglecnt <= 0 && state != QST) 4703149Seric break; 4713149Seric 4728078Seric newstate = StateTab[state][toktype(c)]; 4738078Seric if (tTd(22, 101)) 4748078Seric printf("ns=%02o\n", newstate); 4758078Seric state = newstate & TYPE; 4768078Seric if (bitset(M, newstate)) 4778078Seric c = NOCHAR; 4788078Seric if (bitset(B, newstate)) 4794228Seric break; 480297Seric } 4813149Seric 4823149Seric /* new token */ 4838078Seric if (tok != q) 4841378Seric { 4858078Seric *q++ = '\0'; 4868078Seric if (tTd(22, 36)) 487297Seric { 4888078Seric printf("tok="); 4898078Seric xputs(tok); 49023109Seric (void) putchar('\n'); 491297Seric } 4928078Seric if (avp >= &av[MAXATOM]) 493297Seric { 49458151Seric syserr("553 prescan: too many tokens"); 49558333Seric if (delimptr != NULL) 49658333Seric *delimptr = p; 49759747Seric CurEnv->e_to = saveto; 4988078Seric return (NULL); 499297Seric } 5008078Seric *avp++ = tok; 501297Seric } 5028423Seric } while (c != '\0' && (c != delim || anglecnt > 0)); 5033149Seric *avp = NULL; 50458333Seric p--; 50558333Seric if (delimptr != NULL) 50658333Seric *delimptr = p; 50756764Seric if (tTd(22, 12)) 50856764Seric { 50956764Seric printf("prescan==>"); 51056764Seric printav(av); 51156764Seric } 51259747Seric CurEnv->e_to = saveto; 51358546Seric if (av[0] == NULL) 51458546Seric return (NULL); 51558403Seric return (av); 5163149Seric } 5173149Seric /* 5183149Seric ** TOKTYPE -- return token type 5193149Seric ** 5203149Seric ** Parameters: 5213149Seric ** c -- the character in question. 5223149Seric ** 5233149Seric ** Returns: 5243149Seric ** Its type. 5253149Seric ** 5263149Seric ** Side Effects: 5273149Seric ** none. 5283149Seric */ 529297Seric 5303149Seric toktype(c) 53158050Seric register int c; 5323149Seric { 5333380Seric static char buf[50]; 5343382Seric static bool firstime = TRUE; 5353380Seric 5363382Seric if (firstime) 5373380Seric { 5383382Seric firstime = FALSE; 53958050Seric expand("\201o", buf, &buf[sizeof buf - 1], CurEnv); 5407005Seric (void) strcat(buf, DELIMCHARS); 5413380Seric } 54258050Seric c &= 0377; 54356327Seric if (c == MATCHCLASS || c == MATCHREPL || c == MATCHNCLASS) 5448078Seric return (ONE); 54559027Seric if (c == MACRODEXPAND) 54659027Seric return (ONE); 5478078Seric if (c == '"') 5488078Seric return (QST); 54958050Seric if ((c & 0340) == 0200) 55058050Seric return (OPR); 5514100Seric if (!isascii(c)) 5528078Seric return (ATM); 5538078Seric if (isspace(c) || c == ')') 5548078Seric return (SPC); 55558050Seric if (strchr(buf, c) != NULL) 5568078Seric return (OPR); 5578078Seric return (ATM); 5583149Seric } 5593149Seric /* 5603149Seric ** REWRITE -- apply rewrite rules to token vector. 5613149Seric ** 5624476Seric ** This routine is an ordered production system. Each rewrite 5634476Seric ** rule has a LHS (called the pattern) and a RHS (called the 5644476Seric ** rewrite); 'rwr' points the the current rewrite rule. 5654476Seric ** 5664476Seric ** For each rewrite rule, 'avp' points the address vector we 5674476Seric ** are trying to match against, and 'pvp' points to the pattern. 5688058Seric ** If pvp points to a special match value (MATCHZANY, MATCHANY, 5699585Seric ** MATCHONE, MATCHCLASS, MATCHNCLASS) then the address in avp 5709585Seric ** matched is saved away in the match vector (pointed to by 'mvp'). 5714476Seric ** 5724476Seric ** When a match between avp & pvp does not match, we try to 5739585Seric ** back out. If we back up over MATCHONE, MATCHCLASS, or MATCHNCLASS 5744476Seric ** we must also back out the match in mvp. If we reach a 5758058Seric ** MATCHANY or MATCHZANY we just extend the match and start 5768058Seric ** over again. 5774476Seric ** 5784476Seric ** When we finally match, we rewrite the address vector 5794476Seric ** and try over again. 5804476Seric ** 5813149Seric ** Parameters: 5823149Seric ** pvp -- pointer to token vector. 58359027Seric ** ruleset -- the ruleset to use for rewriting. 58459027Seric ** e -- the current envelope. 5853149Seric ** 5863149Seric ** Returns: 58759084Seric ** A status code. If EX_TEMPFAIL, higher level code should 58859084Seric ** attempt recovery. 5893149Seric ** 5903149Seric ** Side Effects: 5913149Seric ** pvp is modified. 5923149Seric */ 5932091Seric 5943149Seric struct match 5953149Seric { 5964468Seric char **first; /* first token matched */ 5974468Seric char **last; /* last token matched */ 59858825Seric char **pattern; /* pointer to pattern */ 5993149Seric }; 6003149Seric 6014468Seric # define MAXMATCH 9 /* max params per rewrite */ 6023149Seric 6033149Seric 60459084Seric int 60559027Seric rewrite(pvp, ruleset, e) 6063149Seric char **pvp; 6074070Seric int ruleset; 60859027Seric register ENVELOPE *e; 6093149Seric { 6103149Seric register char *ap; /* address pointer */ 6113149Seric register char *rp; /* rewrite pointer */ 6123149Seric register char **avp; /* address vector pointer */ 6133149Seric register char **rvp; /* rewrite vector pointer */ 6148058Seric register struct match *mlp; /* cur ptr into mlist */ 6158058Seric register struct rewrite *rwr; /* pointer to current rewrite rule */ 61658866Seric int ruleno; /* current rule number */ 61759084Seric int rstat = EX_OK; /* return status */ 61856678Seric struct match mlist[MAXMATCH]; /* stores match on LHS */ 6193149Seric char *npvp[MAXATOM+1]; /* temporary space for rebuild */ 6203149Seric 6219279Seric if (OpMode == MD_TEST || tTd(21, 2)) 6223149Seric { 6238959Seric printf("rewrite: ruleset %2d input:", ruleset); 62456678Seric printav(pvp); 6253149Seric } 62656678Seric if (ruleset < 0 || ruleset >= MAXRWSETS) 62756326Seric { 62858151Seric syserr("554 rewrite: illegal ruleset number %d", ruleset); 62959084Seric return EX_CONFIG; 63056326Seric } 63156678Seric if (pvp == NULL) 63259084Seric return EX_USAGE; 63356326Seric 6343149Seric /* 63556678Seric ** Run through the list of rewrite rules, applying 63656678Seric ** any that match. 6373149Seric */ 6383149Seric 63958866Seric ruleno = 1; 6404070Seric for (rwr = RewriteRules[ruleset]; rwr != NULL; ) 6413149Seric { 64256678Seric int loopcount = 0; 64356678Seric 6447675Seric if (tTd(21, 12)) 645297Seric { 6468069Seric printf("-----trying rule:"); 64756678Seric printav(rwr->r_lhs); 6483149Seric } 6493149Seric 6503149Seric /* try to match on this rule */ 6514468Seric mlp = mlist; 6528058Seric rvp = rwr->r_lhs; 6538058Seric avp = pvp; 65458866Seric if (++loopcount > 100) 6553149Seric { 65658866Seric syserr("554 Infinite loop in ruleset %d, rule %d", 65758866Seric ruleset, ruleno); 65858866Seric if (tTd(21, 1)) 65952637Seric { 66056678Seric printf("workspace: "); 66156678Seric printav(pvp); 66252637Seric } 66358866Seric break; 66458866Seric } 66558866Seric 66658866Seric while ((ap = *avp) != NULL || *rvp != NULL) 66758866Seric { 6683149Seric rp = *rvp; 6698058Seric if (tTd(21, 35)) 6708058Seric { 67158825Seric printf("ADVANCE rp="); 67257531Seric xputs(rp); 67357532Seric printf(", ap="); 6748058Seric xputs(ap); 6758069Seric printf("\n"); 6768058Seric } 67756678Seric if (rp == NULL) 67856326Seric { 6793149Seric /* end-of-pattern before end-of-address */ 6808058Seric goto backup; 68156678Seric } 68258173Seric if (ap == NULL && (*rp & 0377) != MATCHZANY && 68358827Seric (*rp & 0377) != MATCHZERO) 68456326Seric { 68558825Seric /* end-of-input with patterns left */ 68658825Seric goto backup; 687297Seric } 68856326Seric 68958050Seric switch (*rp & 0377) 6908058Seric { 69156678Seric register STAB *s; 69258814Seric char buf[MAXLINE]; 69356326Seric 69456678Seric case MATCHCLASS: 69558825Seric /* match any phrase in a class */ 69658825Seric mlp->pattern = rvp; 69758814Seric mlp->first = avp; 69858814Seric extendclass: 69958825Seric ap = *avp; 70058825Seric if (ap == NULL) 70158814Seric goto backup; 70258814Seric mlp->last = avp++; 70358814Seric cataddr(mlp->first, mlp->last, buf, sizeof buf, '\0'); 70458814Seric s = stab(buf, ST_CLASS, ST_FIND); 70556678Seric if (s == NULL || !bitnset(rp[1], s->s_class)) 70656326Seric { 70758825Seric if (tTd(21, 36)) 70858825Seric { 70958825Seric printf("EXTEND rp="); 71058825Seric xputs(rp); 71158825Seric printf(", ap="); 71258825Seric xputs(ap); 71358825Seric printf("\n"); 71458825Seric } 71558825Seric goto extendclass; 71656326Seric } 71758825Seric if (tTd(21, 36)) 71858825Seric printf("CLMATCH\n"); 71958814Seric mlp++; 72058814Seric break; 7214060Seric 72258825Seric case MATCHNCLASS: 72358825Seric /* match any token not in a class */ 72458825Seric s = stab(ap, ST_CLASS, ST_FIND); 72558825Seric if (s != NULL && bitnset(rp[1], s->s_class)) 72658825Seric goto backup; 72758825Seric 72858825Seric /* fall through */ 72958825Seric 73056678Seric case MATCHONE: 73156678Seric case MATCHANY: 73256678Seric /* match exactly one token */ 73358825Seric mlp->pattern = rvp; 73456678Seric mlp->first = avp; 73556678Seric mlp->last = avp++; 7368058Seric mlp++; 73756678Seric break; 7388058Seric 73956678Seric case MATCHZANY: 74056678Seric /* match zero or more tokens */ 74158825Seric mlp->pattern = rvp; 74256678Seric mlp->first = avp; 74356678Seric mlp->last = avp - 1; 74456678Seric mlp++; 74556678Seric break; 74656326Seric 74758827Seric case MATCHZERO: 74858173Seric /* match zero tokens */ 74958173Seric break; 75058173Seric 75159027Seric case MACRODEXPAND: 75259027Seric /* 75359027Seric ** Match against run-time macro. 75459027Seric ** This algorithm is broken for the 75559027Seric ** general case (no recursive macros, 75659027Seric ** improper tokenization) but should 75759027Seric ** work for the usual cases. 75859027Seric */ 75959027Seric 76059027Seric ap = macvalue(rp[1], e); 76159027Seric mlp->first = avp; 76259027Seric if (tTd(21, 2)) 76359027Seric printf("rewrite: LHS $&%c => \"%s\"\n", 76459027Seric rp[1], 76559027Seric ap == NULL ? "(NULL)" : ap); 76659027Seric 76759027Seric if (ap == NULL) 76859027Seric break; 769*60502Seric while (*ap != '\0') 77059027Seric { 77159027Seric if (*avp == NULL || 77259027Seric strncasecmp(ap, *avp, strlen(*avp)) != 0) 77359027Seric { 77459027Seric /* no match */ 77559027Seric avp = mlp->first; 77659027Seric goto backup; 77759027Seric } 77859027Seric ap += strlen(*avp++); 77959027Seric } 78059027Seric 78159027Seric /* match */ 78259027Seric break; 78359027Seric 78456678Seric default: 78556678Seric /* must have exact match */ 78656678Seric if (strcasecmp(rp, ap)) 7878058Seric goto backup; 7884468Seric avp++; 78956678Seric break; 7903149Seric } 7913149Seric 79256678Seric /* successful match on this token */ 7933149Seric rvp++; 7943149Seric continue; 7953149Seric 79658825Seric backup: 79756678Seric /* match failed -- back up */ 79858825Seric while (--mlp >= mlist) 7993149Seric { 80058825Seric rvp = mlp->pattern; 80156678Seric rp = *rvp; 80258825Seric avp = mlp->last + 1; 80358825Seric ap = *avp; 80458825Seric 80558825Seric if (tTd(21, 36)) 80658825Seric { 80758825Seric printf("BACKUP rp="); 80858825Seric xputs(rp); 80958825Seric printf(", ap="); 81058825Seric xputs(ap); 81158825Seric printf("\n"); 81258825Seric } 81358825Seric 81458825Seric if (ap == NULL) 81558825Seric { 81658825Seric /* run off the end -- back up again */ 81758825Seric continue; 81858825Seric } 81958050Seric if ((*rp & 0377) == MATCHANY || 82058050Seric (*rp & 0377) == MATCHZANY) 8214468Seric { 82256678Seric /* extend binding and continue */ 82358825Seric mlp->last = avp++; 82456678Seric rvp++; 82558825Seric mlp++; 82656678Seric break; 8274468Seric } 82858825Seric if ((*rp & 0377) == MATCHCLASS) 82956678Seric { 83058814Seric /* extend binding and try again */ 83158825Seric mlp->last = avp++; 83258814Seric goto extendclass; 83358814Seric } 8343149Seric } 8353149Seric 83658825Seric if (mlp < mlist) 83756678Seric { 83856678Seric /* total failure to match */ 83956326Seric break; 8403149Seric } 841297Seric } 8423149Seric 8433149Seric /* 84456678Seric ** See if we successfully matched 8453149Seric */ 8463149Seric 84758827Seric if (mlp < mlist || *rvp != NULL) 8483149Seric { 8499374Seric if (tTd(21, 10)) 8509374Seric printf("----- rule fails\n"); 8519374Seric rwr = rwr->r_next; 85258866Seric ruleno++; 8539374Seric continue; 8549374Seric } 8553149Seric 8569374Seric rvp = rwr->r_rhs; 8579374Seric if (tTd(21, 12)) 8589374Seric { 8599374Seric printf("-----rule matches:"); 86056678Seric printav(rvp); 8619374Seric } 8629374Seric 8639374Seric rp = *rvp; 86458050Seric if ((*rp & 0377) == CANONUSER) 8659374Seric { 8669374Seric rvp++; 8679374Seric rwr = rwr->r_next; 86858866Seric ruleno++; 8699374Seric } 87058050Seric else if ((*rp & 0377) == CANONHOST) 8719374Seric { 8729374Seric rvp++; 8739374Seric rwr = NULL; 8749374Seric } 87558050Seric else if ((*rp & 0377) == CANONNET) 8769374Seric rwr = NULL; 8779374Seric 8789374Seric /* substitute */ 8799374Seric for (avp = npvp; *rvp != NULL; rvp++) 8809374Seric { 8819374Seric register struct match *m; 8829374Seric register char **pp; 8839374Seric 8848058Seric rp = *rvp; 88558050Seric if ((*rp & 0377) == MATCHREPL) 8868058Seric { 88716914Seric /* substitute from LHS */ 88816914Seric m = &mlist[rp[1] - '1']; 88956678Seric if (m < mlist || m >= mlp) 8909374Seric { 89158151Seric syserr("554 rewrite: ruleset %d: replacement $%c out of bounds", 89256326Seric ruleset, rp[1]); 89359084Seric return EX_CONFIG; 8949374Seric } 89516914Seric if (tTd(21, 15)) 89616914Seric { 89716914Seric printf("$%c:", rp[1]); 89816914Seric pp = m->first; 89956678Seric while (pp <= m->last) 90016914Seric { 90116914Seric printf(" %x=\"", *pp); 90216914Seric (void) fflush(stdout); 90316914Seric printf("%s\"", *pp++); 90416914Seric } 90516914Seric printf("\n"); 90616914Seric } 9079374Seric pp = m->first; 90856678Seric while (pp <= m->last) 9093149Seric { 91016914Seric if (avp >= &npvp[MAXATOM]) 91156678Seric { 91258151Seric syserr("554 rewrite: expansion too long"); 91359084Seric return EX_DATAERR; 91456678Seric } 91516914Seric *avp++ = *pp++; 9163149Seric } 9173149Seric } 91816914Seric else 9198226Seric { 92016914Seric /* vanilla replacement */ 9219374Seric if (avp >= &npvp[MAXATOM]) 92216889Seric { 92356678Seric toolong: 92458151Seric syserr("554 rewrite: expansion too long"); 92559084Seric return EX_DATAERR; 92616889Seric } 92759027Seric if ((*rp & 0377) != MACRODEXPAND) 92859027Seric *avp++ = rp; 92959027Seric else 93059027Seric { 93159027Seric *avp = macvalue(rp[1], e); 93259027Seric if (tTd(21, 2)) 93359027Seric printf("rewrite: RHS $&%c => \"%s\"\n", 93459027Seric rp[1], 93559027Seric *avp == NULL ? "(NULL)" : *avp); 93659027Seric if (*avp != NULL) 93759027Seric avp++; 93859027Seric } 9398226Seric } 9409374Seric } 9419374Seric *avp++ = NULL; 94216914Seric 94316914Seric /* 94456678Seric ** Check for any hostname/keyword lookups. 94516914Seric */ 94616914Seric 94716914Seric for (rvp = npvp; *rvp != NULL; rvp++) 94816914Seric { 94956678Seric char **hbrvp; 95016914Seric char **xpvp; 95116914Seric int trsize; 95217473Seric char *olddelimchar; 95356678Seric char *replac; 95456678Seric int endtoken; 95556678Seric STAB *map; 95656678Seric char *mapname; 95756678Seric char **key_rvp; 95856678Seric char **arg_rvp; 95956678Seric char **default_rvp; 96056678Seric char buf[MAXNAME + 1]; 96116914Seric char *pvpb1[MAXATOM + 1]; 96256823Seric char *argvect[10]; 96317174Seric char pvpbuf[PSBUFSIZE]; 96416914Seric 96558050Seric if ((**rvp & 0377) != HOSTBEGIN && 96658050Seric (**rvp & 0377) != LOOKUPBEGIN) 96716914Seric continue; 96816914Seric 96916914Seric /* 97056678Seric ** Got a hostname/keyword lookup. 97116914Seric ** 97216914Seric ** This could be optimized fairly easily. 97316914Seric */ 97416914Seric 97516914Seric hbrvp = rvp; 97658050Seric if ((**rvp & 0377) == HOSTBEGIN) 97756327Seric { 97856678Seric endtoken = HOSTEND; 97956678Seric mapname = "host"; 98056327Seric } 98156326Seric else 98256327Seric { 98356678Seric endtoken = LOOKUPEND; 98456678Seric mapname = *++rvp; 98556327Seric } 98656678Seric map = stab(mapname, ST_MAP, ST_FIND); 98756678Seric if (map == NULL) 98858151Seric syserr("554 rewrite: map %s not found", mapname); 98956678Seric 99056678Seric /* extract the match part */ 99156678Seric key_rvp = ++rvp; 99256823Seric default_rvp = NULL; 99356823Seric arg_rvp = argvect; 99456823Seric xpvp = NULL; 99556823Seric replac = pvpbuf; 99658050Seric while (*rvp != NULL && (**rvp & 0377) != endtoken) 99753654Seric { 99858050Seric int nodetype = **rvp & 0377; 99956823Seric 100056823Seric if (nodetype != CANONHOST && nodetype != CANONUSER) 100156678Seric { 100256823Seric rvp++; 100356823Seric continue; 100456823Seric } 100556823Seric 100656823Seric *rvp++ = NULL; 100756823Seric 100856823Seric if (xpvp != NULL) 100956823Seric { 101058814Seric cataddr(xpvp, NULL, replac, 101158082Seric &pvpbuf[sizeof pvpbuf] - replac, 101258082Seric '\0'); 101356823Seric *++arg_rvp = replac; 101456823Seric replac += strlen(replac) + 1; 101556823Seric xpvp = NULL; 101656823Seric } 101756823Seric switch (nodetype) 101856823Seric { 101956678Seric case CANONHOST: 102056823Seric xpvp = rvp; 102156678Seric break; 102256678Seric 102356678Seric case CANONUSER: 102456678Seric default_rvp = rvp; 102556678Seric break; 102656678Seric } 102753654Seric } 102816914Seric if (*rvp != NULL) 102916914Seric *rvp++ = NULL; 103056823Seric if (xpvp != NULL) 103156823Seric { 103258814Seric cataddr(xpvp, NULL, replac, 103358082Seric &pvpbuf[sizeof pvpbuf] - replac, 103458082Seric '\0'); 103556823Seric *++arg_rvp = replac; 103656823Seric } 103756823Seric *++arg_rvp = NULL; 103816914Seric 103916914Seric /* save the remainder of the input string */ 104016914Seric trsize = (int) (avp - rvp + 1) * sizeof *rvp; 104116914Seric bcopy((char *) rvp, (char *) pvpb1, trsize); 104216914Seric 104356678Seric /* look it up */ 104458814Seric cataddr(key_rvp, NULL, buf, sizeof buf, '\0'); 104556823Seric argvect[0] = buf; 104660209Seric if (map != NULL && bitset(MF_VALID, map->s_map.map_mflags)) 104756836Seric { 104859084Seric auto int stat = EX_OK; 104956836Seric 105060215Seric /* XXX should try to auto-open the map here */ 105160215Seric 105258796Seric if (tTd(60, 1)) 105358796Seric printf("map_lookup(%s, %s) => ", 105458796Seric mapname, buf); 105556836Seric replac = (*map->s_map.map_class->map_lookup)(&map->s_map, 105660089Seric buf, argvect, &stat); 105758796Seric if (tTd(60, 1)) 105859084Seric printf("%s (%d)\n", 105959084Seric replac ? replac : "NOT FOUND", 106059084Seric stat); 106159084Seric 106259084Seric /* should recover if stat == EX_TEMPFAIL */ 106359084Seric if (stat == EX_TEMPFAIL) 106459084Seric rstat = stat; 106556836Seric } 106653654Seric else 106756678Seric replac = NULL; 106856678Seric 106956678Seric /* if no replacement, use default */ 107056823Seric if (replac == NULL && default_rvp != NULL) 107156823Seric { 107260089Seric /* create the default */ 107360089Seric cataddr(default_rvp, NULL, buf, sizeof buf, '\0'); 107456823Seric replac = buf; 107556823Seric } 107656823Seric 107756678Seric if (replac == NULL) 107851317Seric { 107956823Seric xpvp = key_rvp; 108053654Seric } 108156678Seric else 108253654Seric { 108356678Seric /* scan the new replacement */ 108458333Seric xpvp = prescan(replac, '\0', pvpbuf, NULL); 108553654Seric if (xpvp == NULL) 108651317Seric { 108758403Seric /* prescan already printed error */ 108859084Seric return EX_DATAERR; 108956678Seric } 109051317Seric } 109151317Seric 109216914Seric /* append it to the token list */ 109356678Seric for (avp = hbrvp; *xpvp != NULL; xpvp++) 109456678Seric { 109517174Seric *avp++ = newstr(*xpvp); 109616920Seric if (avp >= &npvp[MAXATOM]) 109716914Seric goto toolong; 109817174Seric } 109916914Seric 110016914Seric /* restore the old trailing information */ 110156678Seric for (xpvp = pvpb1; (*avp++ = *xpvp++) != NULL; ) 110216920Seric if (avp >= &npvp[MAXATOM]) 110316914Seric goto toolong; 110417174Seric 110556678Seric break; 110616914Seric } 110716914Seric 110816914Seric /* 110916914Seric ** Check for subroutine calls. 111016914Seric */ 111116914Seric 111258050Seric if (*npvp != NULL && (**npvp & 0377) == CALLSUBR) 111356678Seric { 111459084Seric int stat; 111559084Seric 111656678Seric bcopy((char *) &npvp[2], (char *) pvp, 111756678Seric (int) (avp - npvp - 2) * sizeof *avp); 111856678Seric if (tTd(21, 3)) 111956678Seric printf("-----callsubr %s\n", npvp[1]); 112059084Seric stat = rewrite(pvp, atoi(npvp[1]), e); 112159084Seric if (rstat == EX_OK || stat == EX_TEMPFAIL) 112259084Seric rstat = stat; 112356678Seric } 112456678Seric else 112556678Seric { 112617348Seric bcopy((char *) npvp, (char *) pvp, 112716900Seric (int) (avp - npvp) * sizeof *avp); 112856678Seric } 11299374Seric if (tTd(21, 4)) 11309374Seric { 11319374Seric printf("rewritten as:"); 113256678Seric printav(pvp); 11339374Seric } 1134297Seric } 11358069Seric 11369279Seric if (OpMode == MD_TEST || tTd(21, 2)) 11378069Seric { 11388959Seric printf("rewrite: ruleset %2d returns:", ruleset); 113956678Seric printav(pvp); 11408069Seric } 114159084Seric 114259084Seric return rstat; 11433149Seric } 11443149Seric /* 11453149Seric ** BUILDADDR -- build address from token vector. 11463149Seric ** 11473149Seric ** Parameters: 11483149Seric ** tv -- token vector. 11493149Seric ** a -- pointer to address descriptor to fill. 11503149Seric ** If NULL, one will be allocated. 115158966Seric ** e -- the current envelope. 11523149Seric ** 11533149Seric ** Returns: 11544279Seric ** NULL if there was an error. 11554279Seric ** 'a' otherwise. 11563149Seric ** 11573149Seric ** Side Effects: 11583149Seric ** fills in 'a' 11593149Seric */ 11603149Seric 116157249Seric struct errcodes 116257249Seric { 116357249Seric char *ec_name; /* name of error code */ 116457249Seric int ec_code; /* numeric code */ 116557249Seric } ErrorCodes[] = 116657249Seric { 116757249Seric "usage", EX_USAGE, 116857249Seric "nouser", EX_NOUSER, 116957249Seric "nohost", EX_NOHOST, 117057249Seric "unavailable", EX_UNAVAILABLE, 117157249Seric "software", EX_SOFTWARE, 117257249Seric "tempfail", EX_TEMPFAIL, 117357249Seric "protocol", EX_PROTOCOL, 117457249Seric #ifdef EX_CONFIG 117557249Seric "config", EX_CONFIG, 117657249Seric #endif 117757249Seric NULL, EX_UNAVAILABLE, 117857249Seric }; 117957249Seric 118056678Seric ADDRESS * 118158966Seric buildaddr(tv, a, e) 11823149Seric register char **tv; 11833149Seric register ADDRESS *a; 118458966Seric register ENVELOPE *e; 11853149Seric { 11863149Seric struct mailer **mp; 11873149Seric register struct mailer *m; 118858008Seric char *bp; 118958008Seric int spaceleft; 119057402Seric static char buf[MAXNAME]; 11913149Seric 11923149Seric if (a == NULL) 11933149Seric a = (ADDRESS *) xalloc(sizeof *a); 119416889Seric bzero((char *) a, sizeof *a); 11953149Seric 11963149Seric /* figure out what net/mailer to use */ 119758050Seric if ((**tv & 0377) != CANONNET) 11984279Seric { 119958151Seric syserr("554 buildaddr: no net"); 12004279Seric return (NULL); 12014279Seric } 12023149Seric tv++; 120358680Seric if (strcasecmp(*tv, "error") == 0) 12044279Seric { 120558050Seric if ((**++tv & 0377) == CANONHOST) 120610183Seric { 120757249Seric register struct errcodes *ep; 120857249Seric 120958050Seric if (isascii(**++tv) && isdigit(**tv)) 121057249Seric { 121157249Seric setstat(atoi(*tv)); 121257249Seric } 121357249Seric else 121457249Seric { 121557249Seric for (ep = ErrorCodes; ep->ec_name != NULL; ep++) 121657249Seric if (strcasecmp(ep->ec_name, *tv) == 0) 121757249Seric break; 121857249Seric setstat(ep->ec_code); 121957249Seric } 122010183Seric tv++; 122110183Seric } 122258050Seric if ((**tv & 0377) != CANONUSER) 122358151Seric syserr("554 buildaddr: error: no user"); 122458814Seric cataddr(++tv, NULL, buf, sizeof buf, ' '); 122558082Seric stripquotes(buf); 12264279Seric usrerr(buf); 12274279Seric return (NULL); 12284279Seric } 122957402Seric 12304598Seric for (mp = Mailer; (m = *mp++) != NULL; ) 12313149Seric { 123258680Seric if (strcasecmp(m->m_name, *tv) == 0) 12333149Seric break; 12343149Seric } 12353149Seric if (m == NULL) 12364279Seric { 123758151Seric syserr("554 buildaddr: unknown mailer %s", *tv); 12384279Seric return (NULL); 12394279Seric } 12404598Seric a->q_mailer = m; 12413149Seric 12423149Seric /* figure out what host (if any) */ 124356678Seric tv++; 124458509Seric if ((**tv & 0377) == CANONHOST) 12453149Seric { 124658008Seric bp = buf; 124758008Seric spaceleft = sizeof buf - 1; 124858050Seric while (*++tv != NULL && (**tv & 0377) != CANONUSER) 124958008Seric { 125058008Seric int i = strlen(*tv); 125158008Seric 125258008Seric if (i > spaceleft) 125358008Seric { 125458008Seric /* out of space for this address */ 125558008Seric if (spaceleft >= 0) 125658151Seric syserr("554 buildaddr: host too long (%.40s...)", 125758008Seric buf); 125858008Seric i = spaceleft; 125958008Seric spaceleft = 0; 126058008Seric } 126158008Seric if (i <= 0) 126258008Seric continue; 126358008Seric bcopy(*tv, bp, i); 126458008Seric bp += i; 126558008Seric spaceleft -= i; 126658008Seric } 126758010Seric *bp = '\0'; 12685704Seric a->q_host = newstr(buf); 12693149Seric } 127057249Seric else 127158509Seric { 127258509Seric if (!bitnset(M_LOCALMAILER, m->m_flags)) 127358509Seric { 127458509Seric syserr("554 buildaddr: no host"); 127558509Seric return (NULL); 127658509Seric } 127757249Seric a->q_host = NULL; 127858509Seric } 12793149Seric 12803149Seric /* figure out the user */ 128158050Seric if (*tv == NULL || (**tv & 0377) != CANONUSER) 12824279Seric { 128358151Seric syserr("554 buildaddr: no user"); 12844279Seric return (NULL); 12854279Seric } 128657402Seric tv++; 128751317Seric 128857402Seric /* do special mapping for local mailer */ 128957402Seric if (m == LocalMailer && *tv != NULL) 129057402Seric { 129157454Seric register char *p = *tv; 129257454Seric 129357454Seric if (*p == '"') 129457454Seric p++; 129557454Seric if (*p == '|') 129657402Seric a->q_mailer = m = ProgMailer; 129757454Seric else if (*p == '/') 129857402Seric a->q_mailer = m = FileMailer; 129957454Seric else if (*p == ':') 130057402Seric { 130157402Seric /* may be :include: */ 130258814Seric cataddr(tv, NULL, buf, sizeof buf, '\0'); 130358008Seric stripquotes(buf); 130458008Seric if (strncasecmp(buf, ":include:", 9) == 0) 130558008Seric { 130658008Seric /* if :include:, don't need further rewriting */ 130757402Seric a->q_mailer = m = InclMailer; 130858008Seric a->q_user = &buf[9]; 130958008Seric return (a); 131058008Seric } 131157402Seric } 131257402Seric } 131357402Seric 131458008Seric if (m == LocalMailer && *tv != NULL && strcmp(*tv, "@") == 0) 131558008Seric { 131658008Seric tv++; 131758008Seric a->q_flags |= QNOTREMOTE; 131858008Seric } 131958008Seric 132058673Seric /* do cleanup of final address */ 132159084Seric (void) rewrite(tv, 4, e); 132219040Seric 132319040Seric /* save the result for the command line/RCPT argument */ 132458814Seric cataddr(tv, NULL, buf, sizeof buf, '\0'); 13253149Seric a->q_user = buf; 13263149Seric 132758670Seric /* 132858670Seric ** Do mapping to lower case as requested by mailer 132958670Seric */ 133058670Seric 133158670Seric if (a->q_host != NULL && !bitnset(M_HST_UPPER, m->m_flags)) 133258670Seric makelower(a->q_host); 133358670Seric if (!bitnset(M_USR_UPPER, m->m_flags)) 133458670Seric makelower(a->q_user); 133558670Seric 13363149Seric return (a); 13373149Seric } 13383188Seric /* 13394228Seric ** CATADDR -- concatenate pieces of addresses (putting in <LWSP> subs) 13404228Seric ** 13414228Seric ** Parameters: 13424228Seric ** pvp -- parameter vector to rebuild. 134358814Seric ** evp -- last parameter to include. Can be NULL to 134458814Seric ** use entire pvp. 13454228Seric ** buf -- buffer to build the string into. 13464228Seric ** sz -- size of buf. 134758082Seric ** spacesub -- the space separator character; if null, 134858082Seric ** use SpaceSub. 13494228Seric ** 13504228Seric ** Returns: 13514228Seric ** none. 13524228Seric ** 13534228Seric ** Side Effects: 13544228Seric ** Destroys buf. 13554228Seric */ 13564228Seric 135758814Seric cataddr(pvp, evp, buf, sz, spacesub) 13584228Seric char **pvp; 135958814Seric char **evp; 13604228Seric char *buf; 13614228Seric register int sz; 136258082Seric char spacesub; 13634228Seric { 13644228Seric bool oatomtok = FALSE; 136556678Seric bool natomtok = FALSE; 13664228Seric register int i; 13674228Seric register char *p; 13684228Seric 136958082Seric if (spacesub == '\0') 137058082Seric spacesub = SpaceSub; 137158082Seric 13728423Seric if (pvp == NULL) 13738423Seric { 137423109Seric (void) strcpy(buf, ""); 13758423Seric return; 13768423Seric } 13774228Seric p = buf; 137811156Seric sz -= 2; 13794228Seric while (*pvp != NULL && (i = strlen(*pvp)) < sz) 13804228Seric { 13818078Seric natomtok = (toktype(**pvp) == ATM); 13824228Seric if (oatomtok && natomtok) 138358082Seric *p++ = spacesub; 13844228Seric (void) strcpy(p, *pvp); 13854228Seric oatomtok = natomtok; 13864228Seric p += i; 138711156Seric sz -= i + 1; 138858814Seric if (pvp++ == evp) 138958814Seric break; 13904228Seric } 13914228Seric *p = '\0'; 13924228Seric } 13934228Seric /* 13943188Seric ** SAMEADDR -- Determine if two addresses are the same 13953188Seric ** 13963188Seric ** This is not just a straight comparison -- if the mailer doesn't 13973188Seric ** care about the host we just ignore it, etc. 13983188Seric ** 13993188Seric ** Parameters: 14003188Seric ** a, b -- pointers to the internal forms to compare. 14013188Seric ** 14023188Seric ** Returns: 14033188Seric ** TRUE -- they represent the same mailbox. 14043188Seric ** FALSE -- they don't. 14053188Seric ** 14063188Seric ** Side Effects: 14073188Seric ** none. 14083188Seric */ 14093188Seric 14103188Seric bool 14119374Seric sameaddr(a, b) 14123188Seric register ADDRESS *a; 14133188Seric register ADDRESS *b; 14143188Seric { 14153188Seric /* if they don't have the same mailer, forget it */ 14163188Seric if (a->q_mailer != b->q_mailer) 14173188Seric return (FALSE); 14183188Seric 14193188Seric /* if the user isn't the same, we can drop out */ 142056678Seric if (strcmp(a->q_user, b->q_user) != 0) 14213188Seric return (FALSE); 14223188Seric 142358438Seric /* if we have good uids for both but the differ, these are different */ 142458438Seric if (bitset(QGOODUID, a->q_flags & b->q_flags) && a->q_uid != b->q_uid) 142558438Seric return (FALSE); 142658438Seric 142758509Seric /* otherwise compare hosts (but be careful for NULL ptrs) */ 142858509Seric if (a->q_host == b->q_host) 142958509Seric { 143058509Seric /* probably both null pointers */ 14313188Seric return (TRUE); 143258509Seric } 14333188Seric if (a->q_host == NULL || b->q_host == NULL) 143458509Seric { 143558509Seric /* only one is a null pointer */ 14363188Seric return (FALSE); 143758509Seric } 143856678Seric if (strcmp(a->q_host, b->q_host) != 0) 14393188Seric return (FALSE); 14403188Seric 14413188Seric return (TRUE); 14423188Seric } 14433234Seric /* 14443234Seric ** PRINTADDR -- print address (for debugging) 14453234Seric ** 14463234Seric ** Parameters: 14473234Seric ** a -- the address to print 14483234Seric ** follow -- follow the q_next chain. 14493234Seric ** 14503234Seric ** Returns: 14513234Seric ** none. 14523234Seric ** 14533234Seric ** Side Effects: 14543234Seric ** none. 14553234Seric */ 14563234Seric 14573234Seric printaddr(a, follow) 14583234Seric register ADDRESS *a; 14593234Seric bool follow; 14603234Seric { 14615001Seric bool first = TRUE; 146257731Seric register MAILER *m; 146357731Seric MAILER pseudomailer; 14645001Seric 14653234Seric while (a != NULL) 14663234Seric { 14675001Seric first = FALSE; 14684443Seric printf("%x=", a); 14694085Seric (void) fflush(stdout); 147057731Seric 147157731Seric /* find the mailer -- carefully */ 147257731Seric m = a->q_mailer; 147357731Seric if (m == NULL) 147457731Seric { 147557731Seric m = &pseudomailer; 147657731Seric m->m_mno = -1; 147757731Seric m->m_name = "NULL"; 147857731Seric } 147957731Seric 148058680Seric printf("%s:\n\tmailer %d (%s), host `%s', user `%s', ruser `%s'\n", 148157731Seric a->q_paddr, m->m_mno, m->m_name, 148240973Sbostic a->q_host, a->q_user, a->q_ruser? a->q_ruser: "<null>"); 148359269Seric printf("\tnext=%x, flags=%o, alias %x, uid %d, gid %d\n", 148459269Seric a->q_next, a->q_flags, a->q_alias, a->q_uid, a->q_gid); 148559269Seric printf("\towner=%s, home=\"%s\", fullname=\"%s\"\n", 148659269Seric a->q_owner == NULL ? "(none)" : a->q_owner, 148759269Seric a->q_home, a->q_fullname); 14884996Seric 14893234Seric if (!follow) 14903234Seric return; 14914996Seric a = a->q_next; 14923234Seric } 14935001Seric if (first) 14944443Seric printf("[NULL]\n"); 14953234Seric } 14964317Seric 14977682Seric /* 14987682Seric ** REMOTENAME -- return the name relative to the current mailer 14997682Seric ** 15007682Seric ** Parameters: 15017682Seric ** name -- the name to translate. 15028069Seric ** m -- the mailer that we want to do rewriting relative 15038069Seric ** to. 150459163Seric ** flags -- fine tune operations. 150559163Seric ** pstat -- pointer to status word. 150658020Seric ** e -- the current envelope. 15077682Seric ** 15087682Seric ** Returns: 15097682Seric ** the text string representing this address relative to 15107682Seric ** the receiving mailer. 15117682Seric ** 15127682Seric ** Side Effects: 15137682Seric ** none. 15147682Seric ** 15157682Seric ** Warnings: 15167682Seric ** The text string returned is tucked away locally; 15177682Seric ** copy it if you intend to save it. 15187682Seric */ 15197682Seric 15207682Seric char * 152159163Seric remotename(name, m, flags, pstat, e) 15227682Seric char *name; 152356678Seric struct mailer *m; 152459163Seric int flags; 152559163Seric int *pstat; 152656678Seric register ENVELOPE *e; 15277682Seric { 15288069Seric register char **pvp; 15298069Seric char *fancy; 153056678Seric char *oldg = macvalue('g', e); 153158020Seric int rwset; 15327682Seric static char buf[MAXNAME]; 15337682Seric char lbuf[MAXNAME]; 153416914Seric char pvpbuf[PSBUFSIZE]; 153556678Seric extern char *crackaddr(); 15367682Seric 15377755Seric if (tTd(12, 1)) 15387755Seric printf("remotename(%s)\n", name); 15397755Seric 154010177Seric /* don't do anything if we are tagging it as special */ 154159163Seric if (bitset(RF_SENDERADDR, flags)) 154259163Seric rwset = bitset(RF_HEADERADDR, flags) ? m->m_sh_rwset 154359163Seric : m->m_se_rwset; 154458020Seric else 154559163Seric rwset = bitset(RF_HEADERADDR, flags) ? m->m_rh_rwset 154659163Seric : m->m_re_rwset; 154758020Seric if (rwset < 0) 154810177Seric return (name); 154910177Seric 15507682Seric /* 15518181Seric ** Do a heuristic crack of this name to extract any comment info. 15528181Seric ** This will leave the name as a comment and a $g macro. 15537889Seric */ 15547889Seric 155559163Seric if (bitset(RF_CANONICAL, flags) || bitnset(M_NOCOMMENT, m->m_flags)) 155658050Seric fancy = "\201g"; 155710310Seric else 155810310Seric fancy = crackaddr(name); 15597889Seric 15608181Seric /* 15618181Seric ** Turn the name into canonical form. 15628181Seric ** Normally this will be RFC 822 style, i.e., "user@domain". 15638181Seric ** If this only resolves to "user", and the "C" flag is 15648181Seric ** specified in the sending mailer, then the sender's 15658181Seric ** domain will be appended. 15668181Seric */ 15678181Seric 156858333Seric pvp = prescan(name, '\0', pvpbuf, NULL); 15697889Seric if (pvp == NULL) 15707889Seric return (name); 157159163Seric if (rewrite(pvp, 3, e) == EX_TEMPFAIL) 157259163Seric *pstat = EX_TEMPFAIL; 157359163Seric if (bitset(RF_ADDDOMAIN, flags) && e->e_fromdomain != NULL) 15748181Seric { 15758181Seric /* append from domain to this address */ 15768181Seric register char **pxp = pvp; 15778181Seric 15789594Seric /* see if there is an "@domain" in the current name */ 15798181Seric while (*pxp != NULL && strcmp(*pxp, "@") != 0) 15808181Seric pxp++; 15818181Seric if (*pxp == NULL) 15828181Seric { 15839594Seric /* no.... append the "@domain" from the sender */ 158456678Seric register char **qxq = e->e_fromdomain; 15858181Seric 15869594Seric while ((*pxp++ = *qxq++) != NULL) 15879594Seric continue; 158859163Seric if (rewrite(pvp, 3, e) == EX_TEMPFAIL) 158959163Seric *pstat = EX_TEMPFAIL; 15908181Seric } 15918181Seric } 15928181Seric 15938181Seric /* 15948959Seric ** Do more specific rewriting. 159556678Seric ** Rewrite using ruleset 1 or 2 depending on whether this is 159656678Seric ** a sender address or not. 15978181Seric ** Then run it through any receiving-mailer-specific rulesets. 15988181Seric */ 15998181Seric 160059163Seric if (bitset(RF_SENDERADDR, flags)) 160159541Seric { 160259163Seric if (rewrite(pvp, 1, e) == EX_TEMPFAIL) 160359163Seric *pstat = EX_TEMPFAIL; 160459541Seric } 16058069Seric else 160659541Seric { 160759163Seric if (rewrite(pvp, 2, e) == EX_TEMPFAIL) 160859163Seric *pstat = EX_TEMPFAIL; 160959541Seric } 161058020Seric if (rwset > 0) 161159541Seric { 161259163Seric if (rewrite(pvp, rwset, e) == EX_TEMPFAIL) 161359163Seric *pstat = EX_TEMPFAIL; 161459541Seric } 16157682Seric 16168181Seric /* 16178959Seric ** Do any final sanitation the address may require. 16188959Seric ** This will normally be used to turn internal forms 16198959Seric ** (e.g., user@host.LOCAL) into external form. This 16208959Seric ** may be used as a default to the above rules. 16218959Seric */ 16228959Seric 162359163Seric if (rewrite(pvp, 4, e) == EX_TEMPFAIL) 162459163Seric *pstat = EX_TEMPFAIL; 16258959Seric 16268959Seric /* 16278181Seric ** Now restore the comment information we had at the beginning. 16288181Seric */ 16298181Seric 163058825Seric cataddr(pvp, NULL, lbuf, sizeof lbuf, '\0'); 163156678Seric define('g', lbuf, e); 163256678Seric expand(fancy, buf, &buf[sizeof buf - 1], e); 163356678Seric define('g', oldg, e); 16347682Seric 16357682Seric if (tTd(12, 1)) 16367755Seric printf("remotename => `%s'\n", buf); 16377682Seric return (buf); 16387682Seric } 163951317Seric /* 164056678Seric ** MAPLOCALUSER -- run local username through ruleset 5 for final redirection 164151317Seric ** 164251317Seric ** Parameters: 164356678Seric ** a -- the address to map (but just the user name part). 164456678Seric ** sendq -- the sendq in which to install any replacement 164556678Seric ** addresses. 164651317Seric ** 164751317Seric ** Returns: 164851317Seric ** none. 164951317Seric */ 165051317Seric 165156678Seric maplocaluser(a, sendq, e) 165256678Seric register ADDRESS *a; 165356678Seric ADDRESS **sendq; 165456678Seric ENVELOPE *e; 165551317Seric { 165656678Seric register char **pvp; 165756678Seric register ADDRESS *a1 = NULL; 165858333Seric auto char *delimptr; 165956678Seric char pvpbuf[PSBUFSIZE]; 166051317Seric 166156678Seric if (tTd(29, 1)) 166256678Seric { 166356678Seric printf("maplocaluser: "); 166456678Seric printaddr(a, FALSE); 166556678Seric } 166658333Seric pvp = prescan(a->q_user, '\0', pvpbuf, &delimptr); 166756678Seric if (pvp == NULL) 166856678Seric return; 166951317Seric 167059084Seric (void) rewrite(pvp, 5, e); 167158050Seric if (pvp[0] == NULL || (pvp[0][0] & 0377) != CANONNET) 167256678Seric return; 167351317Seric 167456678Seric /* if non-null, mailer destination specified -- has it changed? */ 167558966Seric a1 = buildaddr(pvp, NULL, e); 167656678Seric if (a1 == NULL || sameaddr(a, a1)) 167756678Seric return; 167851317Seric 167956678Seric /* mark old address as dead; insert new address */ 168056678Seric a->q_flags |= QDONTSEND; 168157731Seric if (tTd(29, 5)) 168257731Seric { 168357731Seric printf("maplocaluser: QDONTSEND "); 168457731Seric printaddr(a, FALSE); 168557731Seric } 168656678Seric a1->q_alias = a; 168758333Seric allocaddr(a1, 1, NULL, delimptr); 168856678Seric (void) recipient(a1, sendq, e); 168951317Seric } 169058802Seric /* 169158802Seric ** DEQUOTE_INIT -- initialize dequote map 169258802Seric ** 169358802Seric ** This is a no-op. 169458802Seric ** 169558802Seric ** Parameters: 169658802Seric ** map -- the internal map structure. 169758802Seric ** args -- arguments. 169858802Seric ** 169958802Seric ** Returns: 170058802Seric ** TRUE. 170158802Seric */ 170258802Seric 170358802Seric bool 170460219Seric dequote_init(map, args) 170558802Seric MAP *map; 170658802Seric char *args; 170758802Seric { 170858805Seric register char *p = args; 170958805Seric 171058805Seric for (;;) 171158805Seric { 171258805Seric while (isascii(*p) && isspace(*p)) 171358805Seric p++; 171458805Seric if (*p != '-') 171558805Seric break; 171658805Seric switch (*++p) 171758805Seric { 171858805Seric case 'a': 171958805Seric map->map_app = ++p; 172058805Seric break; 172158805Seric } 172258805Seric while (*p != '\0' && !(isascii(*p) && isspace(*p))) 172358805Seric p++; 172458805Seric if (*p != '\0') 172558805Seric *p = '\0'; 172658805Seric } 172758805Seric if (map->map_app != NULL) 172858805Seric map->map_app = newstr(map->map_app); 172958805Seric 173058802Seric return TRUE; 173158802Seric } 173258802Seric /* 173358802Seric ** DEQUOTE_MAP -- unquote an address 173458802Seric ** 173558802Seric ** Parameters: 173658802Seric ** map -- the internal map structure (ignored). 173760089Seric ** name -- the name to dequote. 173858802Seric ** av -- arguments (ignored). 173959084Seric ** statp -- pointer to status out-parameter. 174058802Seric ** 174158802Seric ** Returns: 174258802Seric ** NULL -- if there were no quotes, or if the resulting 174358802Seric ** unquoted buffer would not be acceptable to prescan. 174458802Seric ** else -- The dequoted buffer. 174558802Seric */ 174658802Seric 174758802Seric char * 174860089Seric dequote_map(map, name, av, statp) 174958802Seric MAP *map; 175060089Seric char *name; 175158802Seric char **av; 175259084Seric int *statp; 175358802Seric { 175458802Seric register char *p; 175558802Seric register char *q; 175658802Seric register char c; 175758802Seric int anglecnt; 175858802Seric int cmntcnt; 175958802Seric int quotecnt; 176059089Seric int spacecnt; 176158802Seric bool quotemode; 176258802Seric bool bslashmode; 176358802Seric 176458802Seric anglecnt = 0; 176558802Seric cmntcnt = 0; 176658802Seric quotecnt = 0; 176759089Seric spacecnt = 0; 176858802Seric quotemode = FALSE; 176958802Seric bslashmode = FALSE; 177058802Seric 177160089Seric for (p = q = name; (c = *p++) != '\0'; ) 177258802Seric { 177358802Seric if (bslashmode) 177458802Seric { 177558802Seric bslashmode = FALSE; 177658802Seric *q++ = c; 177758802Seric continue; 177858802Seric } 177958802Seric 178058802Seric switch (c) 178158802Seric { 178258802Seric case '\\': 178358802Seric bslashmode = TRUE; 178458802Seric break; 178558802Seric 178658802Seric case '(': 178758802Seric cmntcnt++; 178858802Seric break; 178958802Seric 179058802Seric case ')': 179158802Seric if (cmntcnt-- <= 0) 179258802Seric return NULL; 179358802Seric break; 179459089Seric 179559089Seric case ' ': 179659089Seric spacecnt++; 179759089Seric break; 179858802Seric } 179958802Seric 180058802Seric if (cmntcnt > 0) 180158802Seric { 180258802Seric *q++ = c; 180358802Seric continue; 180458802Seric } 180558802Seric 180658802Seric switch (c) 180758802Seric { 180858802Seric case '"': 180958802Seric quotemode = !quotemode; 181058802Seric quotecnt++; 181158802Seric continue; 181258802Seric 181358802Seric case '<': 181458802Seric anglecnt++; 181558802Seric break; 181658802Seric 181758802Seric case '>': 181858802Seric if (anglecnt-- <= 0) 181958802Seric return NULL; 182058802Seric break; 182158802Seric } 182258802Seric *q++ = c; 182358802Seric } 182458802Seric 182558802Seric if (anglecnt != 0 || cmntcnt != 0 || bslashmode || 182659089Seric quotemode || quotecnt <= 0 || spacecnt != 0) 182758802Seric return NULL; 182858802Seric *q++ = '\0'; 182960089Seric return name; 183058802Seric } 1831