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*56836Seric static char sccsid[] = "@(#)parseaddr.c 5.27 (Berkeley) 11/15/92"; 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. 4356678Seric ** e -- the envelope that will contain this address. 44297Seric ** 45297Seric ** Returns: 46297Seric ** A pointer to the address descriptor header (`a' if 47297Seric ** `a' is non-NULL). 48297Seric ** NULL on error. 49297Seric ** 50297Seric ** Side Effects: 51297Seric ** none 52297Seric */ 53297Seric 549374Seric /* following delimiters are inherent to the internal algorithms */ 5516155Seric # define DELIMCHARS "\001()<>,;\\\"\r\n" /* word delimiters */ 562091Seric 572973Seric ADDRESS * 5856678Seric parseaddr(addr, a, copyf, delim, e) 59297Seric char *addr; 602973Seric register ADDRESS *a; 61297Seric int copyf; 6211445Seric char delim; 6356678Seric register ENVELOPE *e; 64297Seric { 653149Seric register char **pvp; 6616914Seric char pvpbuf[PSBUFSIZE]; 6756678Seric extern char **prescan(); 6856678Seric extern ADDRESS *buildaddr(); 69297Seric 70297Seric /* 71297Seric ** Initialize and prescan address. 72297Seric */ 73297Seric 7456678Seric e->e_to = addr; 757675Seric if (tTd(20, 1)) 769888Seric printf("\n--parseaddr(%s)\n", addr); 773188Seric 7816914Seric pvp = prescan(addr, delim, pvpbuf); 793149Seric if (pvp == NULL) 8056729Seric { 8156729Seric if (tTd(20, 1)) 8256729Seric printf("parseaddr-->NULL\n"); 83297Seric return (NULL); 8456729Seric } 85297Seric 86297Seric /* 873149Seric ** Apply rewriting rules. 887889Seric ** Ruleset 0 does basic parsing. It must resolve. 89297Seric */ 90297Seric 918181Seric rewrite(pvp, 3); 924070Seric rewrite(pvp, 0); 93297Seric 943149Seric /* 953149Seric ** See if we resolved to a real mailer. 963149Seric */ 97297Seric 983149Seric if (pvp[0][0] != CANONNET) 993149Seric { 1003149Seric setstat(EX_USAGE); 1013149Seric usrerr("cannot resolve name"); 1023149Seric return (NULL); 103297Seric } 104297Seric 105297Seric /* 1063149Seric ** Build canonical address from pvp. 107297Seric */ 108297Seric 1093149Seric a = buildaddr(pvp, a); 1104279Seric if (a == NULL) 1114279Seric return (NULL); 112297Seric 113297Seric /* 1143149Seric ** Make local copies of the host & user and then 1153149Seric ** transport them out. 116297Seric */ 117297Seric 11856678Seric allocaddr(a, copyf, addr); 11956678Seric 12056678Seric /* 12156678Seric ** Compute return value. 12256678Seric */ 12356678Seric 12456678Seric if (tTd(20, 1)) 1258078Seric { 12656678Seric printf("parseaddr-->"); 12756678Seric printaddr(a, FALSE); 12856678Seric } 12956678Seric 13056678Seric return (a); 13156678Seric } 13256678Seric /* 13356678Seric ** ALLOCADDR -- do local allocations of address on demand. 13456678Seric ** 13556678Seric ** Also lowercases the host name if requested. 13656678Seric ** 13756678Seric ** Parameters: 13856678Seric ** a -- the address to reallocate. 13956678Seric ** copyf -- the copy flag (see parseaddr for description). 14056678Seric ** paddr -- the printname of the address. 14156678Seric ** 14256678Seric ** Returns: 14356678Seric ** none. 14456678Seric ** 14556678Seric ** Side Effects: 14656678Seric ** Copies portions of a into local buffers as requested. 14756678Seric */ 14856678Seric 14956678Seric allocaddr(a, copyf, paddr) 15056678Seric register ADDRESS *a; 15156678Seric int copyf; 15256678Seric char *paddr; 15356678Seric { 15456678Seric register MAILER *m = a->q_mailer; 15556678Seric 15656678Seric if (copyf > 0 && paddr != NULL) 15756678Seric { 15856678Seric extern char *DelimChar; 1598078Seric char savec = *DelimChar; 1608078Seric 1618078Seric *DelimChar = '\0'; 16256678Seric a->q_paddr = newstr(paddr); 1638078Seric *DelimChar = savec; 1648078Seric } 165297Seric else 16656678Seric a->q_paddr = paddr; 16724944Seric 16824944Seric if (a->q_user == NULL) 16924944Seric a->q_user = ""; 17024944Seric if (a->q_host == NULL) 17124944Seric a->q_host = ""; 17224944Seric 1733149Seric if (copyf >= 0) 174297Seric { 17524944Seric a->q_host = newstr(a->q_host); 1763149Seric if (a->q_user != a->q_paddr) 1773149Seric a->q_user = newstr(a->q_user); 178297Seric } 179297Seric 18056678Seric if (a->q_paddr == NULL) 18156678Seric a->q_paddr = a->q_user; 18256678Seric 183297Seric /* 18416202Seric ** Convert host name to lower case if requested. 18516202Seric ** User name will be done later. 18616202Seric */ 18716202Seric 18816202Seric if (!bitnset(M_HST_UPPER, m->m_flags)) 18916202Seric makelower(a->q_host); 190297Seric } 191297Seric /* 19216162Seric ** LOWERADDR -- map UPPER->lower case on addresses as requested. 19316162Seric ** 19416162Seric ** Parameters: 19516162Seric ** a -- address to be mapped. 19616162Seric ** 19716162Seric ** Returns: 19816162Seric ** none. 19916162Seric ** 20016162Seric ** Side Effects: 20116162Seric ** none. 20216162Seric */ 20316162Seric 20416162Seric loweraddr(a) 20516162Seric register ADDRESS *a; 20616162Seric { 20716162Seric register MAILER *m = a->q_mailer; 20816162Seric 20916162Seric if (!bitnset(M_USR_UPPER, m->m_flags)) 21016162Seric makelower(a->q_user); 21116162Seric } 21216162Seric /* 213297Seric ** PRESCAN -- Prescan name and make it canonical 214297Seric ** 2159374Seric ** Scans a name and turns it into a set of tokens. This process 2169374Seric ** deletes blanks and comments (in parentheses). 217297Seric ** 218297Seric ** This routine knows about quoted strings and angle brackets. 219297Seric ** 220297Seric ** There are certain subtleties to this routine. The one that 221297Seric ** comes to mind now is that backslashes on the ends of names 222297Seric ** are silently stripped off; this is intentional. The problem 223297Seric ** is that some versions of sndmsg (like at LBL) set the kill 224297Seric ** character to something other than @ when reading addresses; 225297Seric ** so people type "csvax.eric\@berkeley" -- which screws up the 226297Seric ** berknet mailer. 227297Seric ** 228297Seric ** Parameters: 229297Seric ** addr -- the name to chomp. 230297Seric ** delim -- the delimiter for the address, normally 231297Seric ** '\0' or ','; \0 is accepted in any case. 23215284Seric ** If '\t' then we are reading the .cf file. 23316914Seric ** pvpbuf -- place to put the saved text -- note that 23416914Seric ** the pointers are static. 235297Seric ** 236297Seric ** Returns: 2373149Seric ** A pointer to a vector of tokens. 238297Seric ** NULL on error. 239297Seric ** 240297Seric ** Side Effects: 24125279Seric ** sets DelimChar to point to the character matching 'delim'. 242297Seric */ 243297Seric 2448078Seric /* states and character types */ 2458078Seric # define OPR 0 /* operator */ 2468078Seric # define ATM 1 /* atom */ 2478078Seric # define QST 2 /* in quoted string */ 2488078Seric # define SPC 3 /* chewing up spaces */ 2498078Seric # define ONE 4 /* pick up one character */ 2503149Seric 2518078Seric # define NSTATES 5 /* number of states */ 2528078Seric # define TYPE 017 /* mask to select state type */ 2538078Seric 2548078Seric /* meta bits for table */ 2558078Seric # define M 020 /* meta character; don't pass through */ 2568078Seric # define B 040 /* cause a break */ 2578078Seric # define MB M|B /* meta-break */ 2588078Seric 2598078Seric static short StateTab[NSTATES][NSTATES] = 2608078Seric { 2618087Seric /* oldst chtype> OPR ATM QST SPC ONE */ 2629051Seric /*OPR*/ OPR|B, ATM|B, QST|B, SPC|MB, ONE|B, 2639051Seric /*ATM*/ OPR|B, ATM, QST|B, SPC|MB, ONE|B, 2649051Seric /*QST*/ QST, QST, OPR, QST, QST, 2658078Seric /*SPC*/ OPR, ATM, QST, SPC|M, ONE, 2668078Seric /*ONE*/ OPR, OPR, OPR, OPR, OPR, 2678078Seric }; 2688078Seric 2698078Seric # define NOCHAR -1 /* signal nothing in lookahead token */ 2708078Seric 27156678Seric char *DelimChar; /* set to point to the delimiter */ 27256678Seric 2733149Seric char ** 27416914Seric prescan(addr, delim, pvpbuf) 275297Seric char *addr; 276297Seric char delim; 27716914Seric char pvpbuf[]; 278297Seric { 279297Seric register char *p; 2808078Seric register char *q; 2819346Seric register int c; 2823149Seric char **avp; 283297Seric bool bslashmode; 284297Seric int cmntcnt; 2858423Seric int anglecnt; 2863149Seric char *tok; 2878078Seric int state; 2888078Seric int newstate; 2898078Seric static char *av[MAXATOM+1]; 29056678Seric extern int errno; 291297Seric 29215253Seric /* make sure error messages don't have garbage on them */ 29315253Seric errno = 0; 29415253Seric 29516914Seric q = pvpbuf; 2963149Seric bslashmode = FALSE; 2977800Seric cmntcnt = 0; 2988423Seric anglecnt = 0; 2993149Seric avp = av; 30056678Seric state = ATM; 3018078Seric c = NOCHAR; 3028078Seric p = addr; 30356764Seric if (tTd(22, 11)) 304297Seric { 3058078Seric printf("prescan: "); 3068078Seric xputs(p); 30723109Seric (void) putchar('\n'); 3088078Seric } 3098078Seric 3108078Seric do 3118078Seric { 3123149Seric /* read a token */ 3133149Seric tok = q; 3148078Seric for (;;) 315297Seric { 3168078Seric /* store away any old lookahead character */ 3178078Seric if (c != NOCHAR) 3188078Seric { 31915284Seric /* see if there is room */ 32016914Seric if (q >= &pvpbuf[PSBUFSIZE - 5]) 3218078Seric { 3228078Seric usrerr("Address too long"); 3238078Seric DelimChar = p; 3248078Seric return (NULL); 3258078Seric } 32615284Seric 32715284Seric /* squirrel it away */ 3288078Seric *q++ = c; 3298078Seric } 3308078Seric 3318078Seric /* read a new input character */ 3328078Seric c = *p++; 33356764Seric if (c == '\0' || (c == delim && anglecnt <= 0)) 33456764Seric { 33556764Seric /* diagnose and patch up bad syntax */ 33656764Seric if (state == QST) 33756764Seric { 33856764Seric usrerr("Unbalanced '\"'"); 33956764Seric c = '"'; 34056764Seric } 34156764Seric else if (cmntcnt > 0) 34256764Seric { 34356764Seric usrerr("Unbalanced '('"); 34456764Seric c = ')'; 34556764Seric } 34656764Seric else if (anglecnt > 0) 34756764Seric { 34856764Seric c = '>'; 34956764Seric usrerr("Unbalanced '<'"); 35056764Seric } 35156764Seric else 35256764Seric break; 35315284Seric 35456764Seric p--; 35556764Seric } 35656764Seric 3578078Seric if (tTd(22, 101)) 3588078Seric printf("c=%c, s=%d; ", c, state); 3598078Seric 3603149Seric /* chew up special characters */ 3613149Seric *q = '\0'; 3623149Seric if (bslashmode) 3633149Seric { 36424944Seric /* kludge \! for naive users */ 36524944Seric if (c != '!') 36656678Seric *q++ = '\\'; 3673149Seric bslashmode = FALSE; 36856678Seric continue; 3693149Seric } 37056678Seric 37156678Seric if (c == '\\') 3723149Seric { 3733149Seric bslashmode = TRUE; 3748078Seric c = NOCHAR; 37556678Seric continue; 3763149Seric } 37756678Seric else if (state == QST) 3788514Seric { 3798514Seric /* do nothing, just avoid next clauses */ 3808514Seric } 3818078Seric else if (c == '(') 3824100Seric { 3838078Seric cmntcnt++; 3848078Seric c = NOCHAR; 3854100Seric } 3868078Seric else if (c == ')') 3873149Seric { 3888078Seric if (cmntcnt <= 0) 3893149Seric { 3908078Seric usrerr("Unbalanced ')'"); 3918078Seric DelimChar = p; 3928078Seric return (NULL); 3933149Seric } 3948078Seric else 3958078Seric cmntcnt--; 3968078Seric } 3978078Seric else if (cmntcnt > 0) 3988078Seric c = NOCHAR; 3998423Seric else if (c == '<') 4008423Seric anglecnt++; 4018423Seric else if (c == '>') 4028423Seric { 4038423Seric if (anglecnt <= 0) 4048423Seric { 4058423Seric usrerr("Unbalanced '>'"); 4068423Seric DelimChar = p; 4078423Seric return (NULL); 4088423Seric } 4098423Seric anglecnt--; 4108423Seric } 41111423Seric else if (delim == ' ' && isspace(c)) 41211423Seric c = ' '; 4133149Seric 4148078Seric if (c == NOCHAR) 4158078Seric continue; 4163149Seric 4178078Seric /* see if this is end of input */ 41811405Seric if (c == delim && anglecnt <= 0 && state != QST) 4193149Seric break; 4203149Seric 4218078Seric newstate = StateTab[state][toktype(c)]; 4228078Seric if (tTd(22, 101)) 4238078Seric printf("ns=%02o\n", newstate); 4248078Seric state = newstate & TYPE; 4258078Seric if (bitset(M, newstate)) 4268078Seric c = NOCHAR; 4278078Seric if (bitset(B, newstate)) 4284228Seric break; 429297Seric } 4303149Seric 4313149Seric /* new token */ 4328078Seric if (tok != q) 4331378Seric { 4348078Seric *q++ = '\0'; 4358078Seric if (tTd(22, 36)) 436297Seric { 4378078Seric printf("tok="); 4388078Seric xputs(tok); 43923109Seric (void) putchar('\n'); 440297Seric } 4418078Seric if (avp >= &av[MAXATOM]) 442297Seric { 4438078Seric syserr("prescan: too many tokens"); 4448078Seric DelimChar = p; 4458078Seric return (NULL); 446297Seric } 4478078Seric *avp++ = tok; 448297Seric } 4498423Seric } while (c != '\0' && (c != delim || anglecnt > 0)); 4503149Seric *avp = NULL; 4518078Seric DelimChar = --p; 45256764Seric if (tTd(22, 12)) 45356764Seric { 45456764Seric printf("prescan==>"); 45556764Seric printav(av); 45656764Seric } 45756764Seric if (av[0] != NULL) 4583149Seric return (av); 4593149Seric return (NULL); 4603149Seric } 4613149Seric /* 4623149Seric ** TOKTYPE -- return token type 4633149Seric ** 4643149Seric ** Parameters: 4653149Seric ** c -- the character in question. 4663149Seric ** 4673149Seric ** Returns: 4683149Seric ** Its type. 4693149Seric ** 4703149Seric ** Side Effects: 4713149Seric ** none. 4723149Seric */ 473297Seric 4743149Seric toktype(c) 4753149Seric register char c; 4763149Seric { 4773380Seric static char buf[50]; 4783382Seric static bool firstime = TRUE; 4793380Seric 4803382Seric if (firstime) 4813380Seric { 4823382Seric firstime = FALSE; 48316155Seric expand("\001o", buf, &buf[sizeof buf - 1], CurEnv); 4847005Seric (void) strcat(buf, DELIMCHARS); 4853380Seric } 48656327Seric if (c == MATCHCLASS || c == MATCHREPL || c == MATCHNCLASS) 4878078Seric return (ONE); 4888078Seric if (c == '"') 4898078Seric return (QST); 4904100Seric if (!isascii(c)) 4918078Seric return (ATM); 4928078Seric if (isspace(c) || c == ')') 4938078Seric return (SPC); 49456795Seric if (iscntrl(c) || strchr(buf, c) != NULL) 4958078Seric return (OPR); 4968078Seric return (ATM); 4973149Seric } 4983149Seric /* 4993149Seric ** REWRITE -- apply rewrite rules to token vector. 5003149Seric ** 5014476Seric ** This routine is an ordered production system. Each rewrite 5024476Seric ** rule has a LHS (called the pattern) and a RHS (called the 5034476Seric ** rewrite); 'rwr' points the the current rewrite rule. 5044476Seric ** 5054476Seric ** For each rewrite rule, 'avp' points the address vector we 5064476Seric ** are trying to match against, and 'pvp' points to the pattern. 5078058Seric ** If pvp points to a special match value (MATCHZANY, MATCHANY, 5089585Seric ** MATCHONE, MATCHCLASS, MATCHNCLASS) then the address in avp 5099585Seric ** matched is saved away in the match vector (pointed to by 'mvp'). 5104476Seric ** 5114476Seric ** When a match between avp & pvp does not match, we try to 5129585Seric ** back out. If we back up over MATCHONE, MATCHCLASS, or MATCHNCLASS 5134476Seric ** we must also back out the match in mvp. If we reach a 5148058Seric ** MATCHANY or MATCHZANY we just extend the match and start 5158058Seric ** over again. 5164476Seric ** 5174476Seric ** When we finally match, we rewrite the address vector 5184476Seric ** and try over again. 5194476Seric ** 5203149Seric ** Parameters: 5213149Seric ** pvp -- pointer to token vector. 5223149Seric ** 5233149Seric ** Returns: 5243149Seric ** none. 5253149Seric ** 5263149Seric ** Side Effects: 5273149Seric ** pvp is modified. 5283149Seric */ 5292091Seric 5303149Seric struct match 5313149Seric { 5324468Seric char **first; /* first token matched */ 5334468Seric char **last; /* last token matched */ 5343149Seric }; 5353149Seric 5364468Seric # define MAXMATCH 9 /* max params per rewrite */ 5373149Seric 5383149Seric 5394070Seric rewrite(pvp, ruleset) 5403149Seric char **pvp; 5414070Seric int ruleset; 5423149Seric { 5433149Seric register char *ap; /* address pointer */ 5443149Seric register char *rp; /* rewrite pointer */ 5453149Seric register char **avp; /* address vector pointer */ 5463149Seric register char **rvp; /* rewrite vector pointer */ 5478058Seric register struct match *mlp; /* cur ptr into mlist */ 5488058Seric register struct rewrite *rwr; /* pointer to current rewrite rule */ 54956678Seric struct match mlist[MAXMATCH]; /* stores match on LHS */ 5503149Seric char *npvp[MAXATOM+1]; /* temporary space for rebuild */ 5513149Seric 5529279Seric if (OpMode == MD_TEST || tTd(21, 2)) 5533149Seric { 5548959Seric printf("rewrite: ruleset %2d input:", ruleset); 55556678Seric printav(pvp); 5563149Seric } 55756678Seric if (ruleset < 0 || ruleset >= MAXRWSETS) 55856326Seric { 55956678Seric syserr("rewrite: illegal ruleset number %d", ruleset); 56056326Seric return; 56156326Seric } 56256678Seric if (pvp == NULL) 56356678Seric return; 56456326Seric 5653149Seric /* 56656678Seric ** Run through the list of rewrite rules, applying 56756678Seric ** any that match. 5683149Seric */ 5693149Seric 5704070Seric for (rwr = RewriteRules[ruleset]; rwr != NULL; ) 5713149Seric { 57256678Seric int loopcount = 0; 57356678Seric 5747675Seric if (tTd(21, 12)) 575297Seric { 5768069Seric printf("-----trying rule:"); 57756678Seric printav(rwr->r_lhs); 5783149Seric } 5793149Seric 5803149Seric /* try to match on this rule */ 5814468Seric mlp = mlist; 5828058Seric rvp = rwr->r_lhs; 5838058Seric avp = pvp; 5848058Seric while ((ap = *avp) != NULL || *rvp != NULL) 5853149Seric { 58656678Seric if (++loopcount > 100) 58752637Seric { 58856678Seric syserr("Infinite loop in ruleset %d", ruleset); 58956678Seric printf("workspace: "); 59056678Seric printav(pvp); 59152637Seric break; 59252637Seric } 5933149Seric rp = *rvp; 5948058Seric if (tTd(21, 35)) 5958058Seric { 59656678Seric printf("operator="); 5978058Seric xputs(ap); 59856678Seric printf(", token="); 5998058Seric xputs(rp); 6008069Seric printf("\n"); 6018058Seric } 60256678Seric if (rp == NULL) 60356326Seric { 6043149Seric /* end-of-pattern before end-of-address */ 6058058Seric goto backup; 60656678Seric } 60756678Seric if (ap == NULL && *rp != MATCHZANY) 60856326Seric { 60956678Seric /* end-of-input */ 61056678Seric break; 611297Seric } 61256326Seric 61356678Seric switch (*rp) 6148058Seric { 61556678Seric register STAB *s; 61656326Seric 61756678Seric case MATCHCLASS: 61856678Seric case MATCHNCLASS: 61956678Seric /* match any token in (not in) a class */ 62056678Seric s = stab(ap, ST_CLASS, ST_FIND); 62156678Seric if (s == NULL || !bitnset(rp[1], s->s_class)) 62256326Seric { 62356678Seric if (*rp == MATCHCLASS) 6249585Seric goto backup; 62556326Seric } 62656678Seric else if (*rp == MATCHNCLASS) 62756678Seric goto backup; 6284060Seric 62956678Seric /* explicit fall-through */ 63056678Seric 63156678Seric case MATCHONE: 63256678Seric case MATCHANY: 63356678Seric /* match exactly one token */ 63456678Seric mlp->first = avp; 63556678Seric mlp->last = avp++; 6368058Seric mlp++; 63756678Seric break; 6388058Seric 63956678Seric case MATCHZANY: 64056678Seric /* match zero or more tokens */ 64156678Seric mlp->first = avp; 64256678Seric mlp->last = avp - 1; 64356678Seric mlp++; 64456678Seric break; 64556326Seric 64656678Seric default: 64756678Seric /* must have exact match */ 64856678Seric if (strcasecmp(rp, ap)) 6498058Seric goto backup; 6504468Seric avp++; 65156678Seric break; 6523149Seric } 6533149Seric 65456678Seric /* successful match on this token */ 6553149Seric rvp++; 6563149Seric continue; 6573149Seric 65856678Seric backup: 65956678Seric /* match failed -- back up */ 66056678Seric while (--rvp >= rwr->r_lhs) 6613149Seric { 66256678Seric rp = *rvp; 66356678Seric if (*rp == MATCHANY || *rp == MATCHZANY) 6644468Seric { 66556678Seric /* extend binding and continue */ 66656678Seric avp = ++mlp[-1].last; 66756678Seric avp++; 66856678Seric rvp++; 66956678Seric break; 6704468Seric } 67156678Seric avp--; 67256678Seric if (*rp == MATCHONE || *rp == MATCHCLASS || 67356678Seric *rp == MATCHNCLASS) 67456678Seric { 67556678Seric /* back out binding */ 67656678Seric mlp--; 67756678Seric } 6783149Seric } 6793149Seric 68056678Seric if (rvp < rwr->r_lhs) 68156678Seric { 68256678Seric /* total failure to match */ 68356326Seric break; 6843149Seric } 685297Seric } 6863149Seric 6873149Seric /* 68856678Seric ** See if we successfully matched 6893149Seric */ 6903149Seric 69156678Seric if (rvp < rwr->r_lhs || *rvp != NULL) 6923149Seric { 6939374Seric if (tTd(21, 10)) 6949374Seric printf("----- rule fails\n"); 6959374Seric rwr = rwr->r_next; 6969374Seric continue; 6979374Seric } 6983149Seric 6999374Seric rvp = rwr->r_rhs; 7009374Seric if (tTd(21, 12)) 7019374Seric { 7029374Seric printf("-----rule matches:"); 70356678Seric printav(rvp); 7049374Seric } 7059374Seric 7069374Seric rp = *rvp; 7079374Seric if (*rp == CANONUSER) 7089374Seric { 7099374Seric rvp++; 7109374Seric rwr = rwr->r_next; 7119374Seric } 7129374Seric else if (*rp == CANONHOST) 7139374Seric { 7149374Seric rvp++; 7159374Seric rwr = NULL; 7169374Seric } 7179374Seric else if (*rp == CANONNET) 7189374Seric rwr = NULL; 7199374Seric 7209374Seric /* substitute */ 7219374Seric for (avp = npvp; *rvp != NULL; rvp++) 7229374Seric { 7239374Seric register struct match *m; 7249374Seric register char **pp; 7259374Seric 7268058Seric rp = *rvp; 72756678Seric if (*rp == MATCHREPL) 7288058Seric { 72916914Seric /* substitute from LHS */ 73016914Seric m = &mlist[rp[1] - '1']; 73156678Seric if (m < mlist || m >= mlp) 7329374Seric { 73356678Seric syserr("rewrite: ruleset %d: replacement $%c out of bounds", 73456326Seric ruleset, rp[1]); 7359374Seric return; 7369374Seric } 73716914Seric if (tTd(21, 15)) 73816914Seric { 73916914Seric printf("$%c:", rp[1]); 74016914Seric pp = m->first; 74156678Seric while (pp <= m->last) 74216914Seric { 74316914Seric printf(" %x=\"", *pp); 74416914Seric (void) fflush(stdout); 74516914Seric printf("%s\"", *pp++); 74616914Seric } 74716914Seric printf("\n"); 74816914Seric } 7499374Seric pp = m->first; 75056678Seric while (pp <= m->last) 7513149Seric { 75216914Seric if (avp >= &npvp[MAXATOM]) 75356678Seric { 75456678Seric syserr("rewrite: expansion too long"); 75556678Seric return; 75656678Seric } 75716914Seric *avp++ = *pp++; 7583149Seric } 7593149Seric } 76016914Seric else 7618226Seric { 76216914Seric /* vanilla replacement */ 7639374Seric if (avp >= &npvp[MAXATOM]) 76416889Seric { 76556678Seric toolong: 76616889Seric syserr("rewrite: expansion too long"); 76716889Seric return; 76816889Seric } 76956678Seric *avp++ = rp; 7708226Seric } 7719374Seric } 7729374Seric *avp++ = NULL; 77316914Seric 77416914Seric /* 77556678Seric ** Check for any hostname/keyword lookups. 77616914Seric */ 77716914Seric 77816914Seric for (rvp = npvp; *rvp != NULL; rvp++) 77916914Seric { 78056678Seric char **hbrvp; 78116914Seric char **xpvp; 78216914Seric int trsize; 78317473Seric char *olddelimchar; 78456678Seric char *replac; 78556678Seric int endtoken; 78656678Seric STAB *map; 78756678Seric char *mapname; 78856678Seric char **key_rvp; 78956678Seric char **arg_rvp; 79056678Seric char **default_rvp; 79156678Seric char buf[MAXNAME + 1]; 79216914Seric char *pvpb1[MAXATOM + 1]; 79356823Seric char *argvect[10]; 79417174Seric char pvpbuf[PSBUFSIZE]; 79556678Seric extern char *DelimChar; 79616914Seric 79756678Seric if (**rvp != HOSTBEGIN && **rvp != LOOKUPBEGIN) 79816914Seric continue; 79916914Seric 80016914Seric /* 80156678Seric ** Got a hostname/keyword lookup. 80216914Seric ** 80316914Seric ** This could be optimized fairly easily. 80416914Seric */ 80516914Seric 80616914Seric hbrvp = rvp; 80756678Seric if (**rvp == HOSTBEGIN) 80856327Seric { 80956678Seric endtoken = HOSTEND; 81056678Seric mapname = "host"; 81156327Seric } 81256326Seric else 81356327Seric { 81456678Seric endtoken = LOOKUPEND; 81556678Seric mapname = *++rvp; 81656327Seric } 81756678Seric map = stab(mapname, ST_MAP, ST_FIND); 81856678Seric if (map == NULL) 81956678Seric syserr("rewrite: map %s not found", mapname); 82056678Seric 82156678Seric /* extract the match part */ 82256678Seric key_rvp = ++rvp; 82356823Seric default_rvp = NULL; 82456823Seric arg_rvp = argvect; 82556823Seric xpvp = NULL; 82656823Seric replac = pvpbuf; 82756678Seric while (*rvp != NULL && **rvp != endtoken) 82853654Seric { 82956823Seric int nodetype = **rvp; 83056823Seric 83156823Seric if (nodetype != CANONHOST && nodetype != CANONUSER) 83256678Seric { 83356823Seric rvp++; 83456823Seric continue; 83556823Seric } 83656823Seric 83756823Seric *rvp++ = NULL; 83856823Seric 83956823Seric if (xpvp != NULL) 84056823Seric { 84156823Seric cataddr(xpvp, replac, 84256823Seric &pvpbuf[sizeof pvpbuf] - replac); 84356823Seric *++arg_rvp = replac; 84456823Seric replac += strlen(replac) + 1; 84556823Seric xpvp = NULL; 84656823Seric } 84756823Seric switch (nodetype) 84856823Seric { 84956678Seric case CANONHOST: 85056823Seric xpvp = rvp; 85156678Seric break; 85256678Seric 85356678Seric case CANONUSER: 85456678Seric default_rvp = rvp; 85556678Seric break; 85656678Seric } 85753654Seric } 85816914Seric if (*rvp != NULL) 85916914Seric *rvp++ = NULL; 86056823Seric if (xpvp != NULL) 86156823Seric { 86256823Seric cataddr(xpvp, replac, 86356823Seric &pvpbuf[sizeof pvpbuf] - replac); 86456823Seric *++arg_rvp = replac; 86556823Seric } 86656823Seric *++arg_rvp = NULL; 86716914Seric 86816914Seric /* save the remainder of the input string */ 86916914Seric trsize = (int) (avp - rvp + 1) * sizeof *rvp; 87016914Seric bcopy((char *) rvp, (char *) pvpb1, trsize); 87116914Seric 87256678Seric /* look it up */ 87356678Seric cataddr(key_rvp, buf, sizeof buf); 87456823Seric argvect[0] = buf; 87556678Seric if (map != NULL && bitset(MF_VALID, map->s_map.map_flags)) 876*56836Seric { 877*56836Seric int bsize = sizeof buf - 1; 878*56836Seric 879*56836Seric if (map->s_map.map_app != NULL) 880*56836Seric bsize -= strlen(map->s_map.map_app); 881*56836Seric replac = (*map->s_map.map_class->map_lookup)(&map->s_map, 88256823Seric buf, sizeof buf - 1, argvect); 883*56836Seric if (replac != NULL && map->s_map.map_app != NULL) 884*56836Seric strcat(replac, map->s_map.map_app); 885*56836Seric } 88653654Seric else 88756678Seric replac = NULL; 88856678Seric 88956678Seric /* if no replacement, use default */ 89056823Seric if (replac == NULL && default_rvp != NULL) 89156823Seric { 89256823Seric char buf2[sizeof buf]; 89356823Seric 89456823Seric /* rewrite the default with % translations */ 89556823Seric cataddr(default_rvp, buf2, sizeof buf2); 89656823Seric map_rewrite(buf2, sizeof buf2, buf, sizeof buf, 89756823Seric argvect); 89856823Seric replac = buf; 89956823Seric } 90056823Seric 90156678Seric if (replac == NULL) 90251317Seric { 90356823Seric xpvp = key_rvp; 90453654Seric } 90556678Seric else 90653654Seric { 90756678Seric /* scan the new replacement */ 90856678Seric olddelimchar = DelimChar; 90956678Seric xpvp = prescan(replac, '\0', pvpbuf); 91056678Seric DelimChar = olddelimchar; 91153654Seric if (xpvp == NULL) 91251317Seric { 91356678Seric syserr("rewrite: cannot prescan map value: %s", replac); 91456678Seric return; 91556678Seric } 91651317Seric } 91751317Seric 91816914Seric /* append it to the token list */ 91956678Seric for (avp = hbrvp; *xpvp != NULL; xpvp++) 92056678Seric { 92117174Seric *avp++ = newstr(*xpvp); 92216920Seric if (avp >= &npvp[MAXATOM]) 92316914Seric goto toolong; 92417174Seric } 92516914Seric 92616914Seric /* restore the old trailing information */ 92756678Seric for (xpvp = pvpb1; (*avp++ = *xpvp++) != NULL; ) 92816920Seric if (avp >= &npvp[MAXATOM]) 92916914Seric goto toolong; 93017174Seric 93156678Seric break; 93216914Seric } 93316914Seric 93416914Seric /* 93516914Seric ** Check for subroutine calls. 93616914Seric */ 93716914Seric 93856678Seric if (*npvp != NULL && **npvp == CALLSUBR) 93956678Seric { 94056678Seric bcopy((char *) &npvp[2], (char *) pvp, 94156678Seric (int) (avp - npvp - 2) * sizeof *avp); 94256678Seric if (tTd(21, 3)) 94356678Seric printf("-----callsubr %s\n", npvp[1]); 94456678Seric rewrite(pvp, atoi(npvp[1])); 94556678Seric } 94656678Seric else 94756678Seric { 94817348Seric bcopy((char *) npvp, (char *) pvp, 94916900Seric (int) (avp - npvp) * sizeof *avp); 95056678Seric } 9519374Seric if (tTd(21, 4)) 9529374Seric { 9539374Seric printf("rewritten as:"); 95456678Seric printav(pvp); 9559374Seric } 956297Seric } 9578069Seric 9589279Seric if (OpMode == MD_TEST || tTd(21, 2)) 9598069Seric { 9608959Seric printf("rewrite: ruleset %2d returns:", ruleset); 96156678Seric printav(pvp); 9628069Seric } 9633149Seric } 9643149Seric /* 9653149Seric ** BUILDADDR -- build address from token vector. 9663149Seric ** 9673149Seric ** Parameters: 9683149Seric ** tv -- token vector. 9693149Seric ** a -- pointer to address descriptor to fill. 9703149Seric ** If NULL, one will be allocated. 9713149Seric ** 9723149Seric ** Returns: 9734279Seric ** NULL if there was an error. 9744279Seric ** 'a' otherwise. 9753149Seric ** 9763149Seric ** Side Effects: 9773149Seric ** fills in 'a' 9783149Seric */ 9793149Seric 98056678Seric ADDRESS * 9813149Seric buildaddr(tv, a) 9823149Seric register char **tv; 9833149Seric register ADDRESS *a; 9843149Seric { 9853149Seric static char buf[MAXNAME]; 9863149Seric struct mailer **mp; 9873149Seric register struct mailer *m; 9883149Seric 9893149Seric if (a == NULL) 9903149Seric a = (ADDRESS *) xalloc(sizeof *a); 99116889Seric bzero((char *) a, sizeof *a); 9923149Seric 9933149Seric /* figure out what net/mailer to use */ 99456678Seric if (**tv != CANONNET) 9954279Seric { 9963149Seric syserr("buildaddr: no net"); 9974279Seric return (NULL); 9984279Seric } 9993149Seric tv++; 100033725Sbostic if (!strcasecmp(*tv, "error")) 10014279Seric { 100210183Seric if (**++tv == CANONHOST) 100310183Seric { 100410183Seric setstat(atoi(*++tv)); 100510183Seric tv++; 100610183Seric } 100710183Seric if (**tv != CANONUSER) 10084279Seric syserr("buildaddr: error: no user"); 100956678Seric buf[0] = '\0'; 10104279Seric while (*++tv != NULL) 10114279Seric { 10124279Seric if (buf[0] != '\0') 10137005Seric (void) strcat(buf, " "); 10147005Seric (void) strcat(buf, *tv); 10154279Seric } 10164279Seric usrerr(buf); 10174279Seric return (NULL); 10184279Seric } 10194598Seric for (mp = Mailer; (m = *mp++) != NULL; ) 10203149Seric { 102133725Sbostic if (!strcasecmp(m->m_name, *tv)) 10223149Seric break; 10233149Seric } 10243149Seric if (m == NULL) 10254279Seric { 102624944Seric syserr("buildaddr: unknown mailer %s", *tv); 10274279Seric return (NULL); 10284279Seric } 10294598Seric a->q_mailer = m; 10303149Seric 10313149Seric /* figure out what host (if any) */ 103256678Seric tv++; 103356678Seric if (!bitnset(M_LOCAL, m->m_flags)) 10343149Seric { 103556678Seric if (**tv++ != CANONHOST) 10364279Seric { 10373149Seric syserr("buildaddr: no host"); 10384279Seric return (NULL); 10394279Seric } 10405704Seric buf[0] = '\0'; 104156678Seric while (*tv != NULL && **tv != CANONUSER) 104256678Seric (void) strcat(buf, *tv++); 10435704Seric a->q_host = newstr(buf); 10443149Seric } 104556678Seric else 104656678Seric a->q_host = NULL; 10473149Seric 10483149Seric /* figure out the user */ 104936615Sbostic if (*tv == NULL || **tv != CANONUSER) 10504279Seric { 10513149Seric syserr("buildaddr: no user"); 10524279Seric return (NULL); 10534279Seric } 105419040Seric 105556678Seric if (m == LocalMailer && tv[1] != NULL && strcmp(tv[1], "@") == 0) 105656678Seric { 105756678Seric tv++; 105856678Seric a->q_flags |= QNOTREMOTE; 105956678Seric } 106051317Seric 106119040Seric /* rewrite according recipient mailer rewriting rules */ 106219040Seric rewrite(++tv, 2); 106356327Seric if (m->m_r_rwset > 0) 106456327Seric rewrite(tv, m->m_r_rwset); 106519040Seric rewrite(tv, 4); 106619040Seric 106719040Seric /* save the result for the command line/RCPT argument */ 106811278Seric cataddr(tv, buf, sizeof buf); 10693149Seric a->q_user = buf; 10703149Seric 10713149Seric return (a); 10723149Seric } 10733188Seric /* 10744228Seric ** CATADDR -- concatenate pieces of addresses (putting in <LWSP> subs) 10754228Seric ** 10764228Seric ** Parameters: 10774228Seric ** pvp -- parameter vector to rebuild. 10784228Seric ** buf -- buffer to build the string into. 10794228Seric ** sz -- size of buf. 10804228Seric ** 10814228Seric ** Returns: 10824228Seric ** none. 10834228Seric ** 10844228Seric ** Side Effects: 10854228Seric ** Destroys buf. 10864228Seric */ 10874228Seric 10884228Seric cataddr(pvp, buf, sz) 10894228Seric char **pvp; 10904228Seric char *buf; 10914228Seric register int sz; 10924228Seric { 10934228Seric bool oatomtok = FALSE; 109456678Seric bool natomtok = FALSE; 10954228Seric register int i; 10964228Seric register char *p; 10974228Seric 10988423Seric if (pvp == NULL) 10998423Seric { 110023109Seric (void) strcpy(buf, ""); 11018423Seric return; 11028423Seric } 11034228Seric p = buf; 110411156Seric sz -= 2; 11054228Seric while (*pvp != NULL && (i = strlen(*pvp)) < sz) 11064228Seric { 11078078Seric natomtok = (toktype(**pvp) == ATM); 11084228Seric if (oatomtok && natomtok) 11099042Seric *p++ = SpaceSub; 11104228Seric (void) strcpy(p, *pvp); 11114228Seric oatomtok = natomtok; 11124228Seric p += i; 111311156Seric sz -= i + 1; 11144228Seric pvp++; 11154228Seric } 11164228Seric *p = '\0'; 11174228Seric } 11184228Seric /* 11193188Seric ** SAMEADDR -- Determine if two addresses are the same 11203188Seric ** 11213188Seric ** This is not just a straight comparison -- if the mailer doesn't 11223188Seric ** care about the host we just ignore it, etc. 11233188Seric ** 11243188Seric ** Parameters: 11253188Seric ** a, b -- pointers to the internal forms to compare. 11263188Seric ** 11273188Seric ** Returns: 11283188Seric ** TRUE -- they represent the same mailbox. 11293188Seric ** FALSE -- they don't. 11303188Seric ** 11313188Seric ** Side Effects: 11323188Seric ** none. 11333188Seric */ 11343188Seric 11353188Seric bool 11369374Seric sameaddr(a, b) 11373188Seric register ADDRESS *a; 11383188Seric register ADDRESS *b; 11393188Seric { 11403188Seric /* if they don't have the same mailer, forget it */ 11413188Seric if (a->q_mailer != b->q_mailer) 11423188Seric return (FALSE); 11433188Seric 11443188Seric /* if the user isn't the same, we can drop out */ 114556678Seric if (strcmp(a->q_user, b->q_user) != 0) 11463188Seric return (FALSE); 11473188Seric 11483188Seric /* if the mailer ignores hosts, we have succeeded! */ 114910690Seric if (bitnset(M_LOCAL, a->q_mailer->m_flags)) 11503188Seric return (TRUE); 11513188Seric 11523188Seric /* otherwise compare hosts (but be careful for NULL ptrs) */ 11533188Seric if (a->q_host == NULL || b->q_host == NULL) 11543188Seric return (FALSE); 115556678Seric if (strcmp(a->q_host, b->q_host) != 0) 11563188Seric return (FALSE); 11573188Seric 11583188Seric return (TRUE); 11593188Seric } 11603234Seric /* 11613234Seric ** PRINTADDR -- print address (for debugging) 11623234Seric ** 11633234Seric ** Parameters: 11643234Seric ** a -- the address to print 11653234Seric ** follow -- follow the q_next chain. 11663234Seric ** 11673234Seric ** Returns: 11683234Seric ** none. 11693234Seric ** 11703234Seric ** Side Effects: 11713234Seric ** none. 11723234Seric */ 11733234Seric 11743234Seric printaddr(a, follow) 11753234Seric register ADDRESS *a; 11763234Seric bool follow; 11773234Seric { 11785001Seric bool first = TRUE; 11795001Seric 11803234Seric while (a != NULL) 11813234Seric { 11825001Seric first = FALSE; 11834443Seric printf("%x=", a); 11844085Seric (void) fflush(stdout); 118540973Sbostic printf("%s: mailer %d (%s), host `%s', user `%s', ruser `%s'\n", 118640973Sbostic a->q_paddr, a->q_mailer->m_mno, a->q_mailer->m_name, 118740973Sbostic a->q_host, a->q_user, a->q_ruser? a->q_ruser: "<null>"); 11888181Seric printf("\tnext=%x, flags=%o, alias %x\n", a->q_next, a->q_flags, 11898181Seric a->q_alias); 11908181Seric printf("\thome=\"%s\", fullname=\"%s\"\n", a->q_home, 11918181Seric a->q_fullname); 11924996Seric 11933234Seric if (!follow) 11943234Seric return; 11954996Seric a = a->q_next; 11963234Seric } 11975001Seric if (first) 11984443Seric printf("[NULL]\n"); 11993234Seric } 12004317Seric 12017682Seric /* 12027682Seric ** REMOTENAME -- return the name relative to the current mailer 12037682Seric ** 12047682Seric ** Parameters: 12057682Seric ** name -- the name to translate. 12068069Seric ** m -- the mailer that we want to do rewriting relative 12078069Seric ** to. 12088069Seric ** senderaddress -- if set, uses the sender rewriting rules 12098069Seric ** rather than the recipient rewriting rules. 121010310Seric ** canonical -- if set, strip out any comment information, 121110310Seric ** etc. 12127682Seric ** 12137682Seric ** Returns: 12147682Seric ** the text string representing this address relative to 12157682Seric ** the receiving mailer. 12167682Seric ** 12177682Seric ** Side Effects: 12187682Seric ** none. 12197682Seric ** 12207682Seric ** Warnings: 12217682Seric ** The text string returned is tucked away locally; 12227682Seric ** copy it if you intend to save it. 12237682Seric */ 12247682Seric 12257682Seric char * 122656678Seric remotename(name, m, senderaddress, canonical, e) 12277682Seric char *name; 122856678Seric struct mailer *m; 12298069Seric bool senderaddress; 123010310Seric bool canonical; 123156678Seric register ENVELOPE *e; 12327682Seric { 12338069Seric register char **pvp; 12348069Seric char *fancy; 123556678Seric extern char *macvalue(); 123656678Seric char *oldg = macvalue('g', e); 12377682Seric static char buf[MAXNAME]; 12387682Seric char lbuf[MAXNAME]; 123916914Seric char pvpbuf[PSBUFSIZE]; 124056678Seric extern char **prescan(); 124156678Seric extern char *crackaddr(); 12427682Seric 12437755Seric if (tTd(12, 1)) 12447755Seric printf("remotename(%s)\n", name); 12457755Seric 124610177Seric /* don't do anything if we are tagging it as special */ 124756327Seric if ((senderaddress ? m->m_s_rwset : m->m_r_rwset) < 0) 124810177Seric return (name); 124910177Seric 12507682Seric /* 12518181Seric ** Do a heuristic crack of this name to extract any comment info. 12528181Seric ** This will leave the name as a comment and a $g macro. 12537889Seric */ 12547889Seric 125510310Seric if (canonical) 125616155Seric fancy = "\001g"; 125710310Seric else 125810310Seric fancy = crackaddr(name); 12597889Seric 12608181Seric /* 12618181Seric ** Turn the name into canonical form. 12628181Seric ** Normally this will be RFC 822 style, i.e., "user@domain". 12638181Seric ** If this only resolves to "user", and the "C" flag is 12648181Seric ** specified in the sending mailer, then the sender's 12658181Seric ** domain will be appended. 12668181Seric */ 12678181Seric 126816914Seric pvp = prescan(name, '\0', pvpbuf); 12697889Seric if (pvp == NULL) 12707889Seric return (name); 12718181Seric rewrite(pvp, 3); 127256678Seric if (e->e_fromdomain != NULL) 12738181Seric { 12748181Seric /* append from domain to this address */ 12758181Seric register char **pxp = pvp; 12768181Seric 12779594Seric /* see if there is an "@domain" in the current name */ 12788181Seric while (*pxp != NULL && strcmp(*pxp, "@") != 0) 12798181Seric pxp++; 12808181Seric if (*pxp == NULL) 12818181Seric { 12829594Seric /* no.... append the "@domain" from the sender */ 128356678Seric register char **qxq = e->e_fromdomain; 12848181Seric 12859594Seric while ((*pxp++ = *qxq++) != NULL) 12869594Seric continue; 128711726Seric rewrite(pvp, 3); 12888181Seric } 12898181Seric } 12908181Seric 12918181Seric /* 12928959Seric ** Do more specific rewriting. 129356678Seric ** Rewrite using ruleset 1 or 2 depending on whether this is 129456678Seric ** a sender address or not. 12958181Seric ** Then run it through any receiving-mailer-specific rulesets. 12968181Seric */ 12978181Seric 12988069Seric if (senderaddress) 12997755Seric { 130056327Seric rewrite(pvp, 1); 130156327Seric if (m->m_s_rwset > 0) 130256327Seric rewrite(pvp, m->m_s_rwset); 13038069Seric } 13048069Seric else 13058069Seric { 130656327Seric rewrite(pvp, 2); 130756327Seric if (m->m_r_rwset > 0) 130856327Seric rewrite(pvp, m->m_r_rwset); 13097682Seric } 13107682Seric 13118181Seric /* 13128959Seric ** Do any final sanitation the address may require. 13138959Seric ** This will normally be used to turn internal forms 13148959Seric ** (e.g., user@host.LOCAL) into external form. This 13158959Seric ** may be used as a default to the above rules. 13168959Seric */ 13178959Seric 13188959Seric rewrite(pvp, 4); 13198959Seric 13208959Seric /* 13218181Seric ** Now restore the comment information we had at the beginning. 13228181Seric */ 13238181Seric 13247682Seric cataddr(pvp, lbuf, sizeof lbuf); 132556678Seric define('g', lbuf, e); 132656678Seric expand(fancy, buf, &buf[sizeof buf - 1], e); 132756678Seric define('g', oldg, e); 13287682Seric 13297682Seric if (tTd(12, 1)) 13307755Seric printf("remotename => `%s'\n", buf); 13317682Seric return (buf); 13327682Seric } 133351317Seric /* 133456678Seric ** MAPLOCALUSER -- run local username through ruleset 5 for final redirection 133551317Seric ** 133651317Seric ** Parameters: 133756678Seric ** a -- the address to map (but just the user name part). 133856678Seric ** sendq -- the sendq in which to install any replacement 133956678Seric ** addresses. 134051317Seric ** 134151317Seric ** Returns: 134251317Seric ** none. 134351317Seric */ 134451317Seric 134556678Seric maplocaluser(a, sendq, e) 134656678Seric register ADDRESS *a; 134756678Seric ADDRESS **sendq; 134856678Seric ENVELOPE *e; 134951317Seric { 135056678Seric register char **pvp; 135156678Seric register ADDRESS *a1 = NULL; 135256678Seric char pvpbuf[PSBUFSIZE]; 135351317Seric 135456678Seric if (tTd(29, 1)) 135556678Seric { 135656678Seric printf("maplocaluser: "); 135756678Seric printaddr(a, FALSE); 135856678Seric } 135956678Seric pvp = prescan(a->q_user, '\0', pvpbuf); 136056678Seric if (pvp == NULL) 136156678Seric return; 136251317Seric 136356678Seric rewrite(pvp, 5); 136456678Seric if (pvp[0] == NULL || pvp[0][0] != CANONNET) 136556678Seric return; 136651317Seric 136756678Seric /* if non-null, mailer destination specified -- has it changed? */ 136856678Seric a1 = buildaddr(pvp, NULL); 136956678Seric if (a1 == NULL || sameaddr(a, a1)) 137056678Seric return; 137151317Seric 137256678Seric /* mark old address as dead; insert new address */ 137356678Seric a->q_flags |= QDONTSEND; 137456678Seric a1->q_alias = a; 137556678Seric allocaddr(a1, 1, NULL); 137656678Seric (void) recipient(a1, sendq, e); 137751317Seric } 1378