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*57454Seric static char sccsid[] = "@(#)parseaddr.c 6.5 (Berkeley) 01/10/93"; 1133730Sbostic #endif /* not lint */ 1222976Smiriam 1356678Seric # include "sendmail.h" 14297Seric 15297Seric /* 169888Seric ** PARSEADDR -- Parse an address 17297Seric ** 18297Seric ** Parses an address and breaks it up into three parts: a 19297Seric ** net to transmit the message on, the host to transmit it 20297Seric ** to, and a user on that host. These are loaded into an 212973Seric ** ADDRESS header with the values squirreled away if necessary. 22297Seric ** The "user" part may not be a real user; the process may 23297Seric ** just reoccur on that machine. For example, on a machine 24297Seric ** with an arpanet connection, the address 25297Seric ** csvax.bill@berkeley 26297Seric ** will break up to a "user" of 'csvax.bill' and a host 27297Seric ** of 'berkeley' -- to be transmitted over the arpanet. 28297Seric ** 29297Seric ** Parameters: 30297Seric ** addr -- the address to parse. 31297Seric ** a -- a pointer to the address descriptor buffer. 32297Seric ** If NULL, a header will be created. 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(); 6957388Seric extern bool invalidaddr(); 70297Seric 71297Seric /* 72297Seric ** Initialize and prescan address. 73297Seric */ 74297Seric 7556678Seric e->e_to = addr; 767675Seric if (tTd(20, 1)) 779888Seric printf("\n--parseaddr(%s)\n", addr); 783188Seric 7957388Seric if (invalidaddr(addr)) 8057388Seric { 8157388Seric if (tTd(20, 1)) 8257388Seric printf("parseaddr-->bad address\n"); 8357388Seric return NULL; 8457388Seric } 8557388Seric 8616914Seric pvp = prescan(addr, delim, pvpbuf); 873149Seric if (pvp == NULL) 8856729Seric { 8956729Seric if (tTd(20, 1)) 9056729Seric printf("parseaddr-->NULL\n"); 91297Seric return (NULL); 9256729Seric } 93297Seric 94297Seric /* 953149Seric ** Apply rewriting rules. 967889Seric ** Ruleset 0 does basic parsing. It must resolve. 97297Seric */ 98297Seric 998181Seric rewrite(pvp, 3); 1004070Seric rewrite(pvp, 0); 101297Seric 1023149Seric /* 1033149Seric ** See if we resolved to a real mailer. 1043149Seric */ 105297Seric 1063149Seric if (pvp[0][0] != CANONNET) 1073149Seric { 1083149Seric setstat(EX_USAGE); 1093149Seric usrerr("cannot resolve name"); 1103149Seric return (NULL); 111297Seric } 112297Seric 113297Seric /* 1143149Seric ** Build canonical address from pvp. 115297Seric */ 116297Seric 1173149Seric a = buildaddr(pvp, a); 1184279Seric if (a == NULL) 1194279Seric return (NULL); 120297Seric 121297Seric /* 1223149Seric ** Make local copies of the host & user and then 1233149Seric ** transport them out. 124297Seric */ 125297Seric 12656678Seric allocaddr(a, copyf, addr); 12756678Seric 12856678Seric /* 12956678Seric ** Compute return value. 13056678Seric */ 13156678Seric 13256678Seric if (tTd(20, 1)) 1338078Seric { 13456678Seric printf("parseaddr-->"); 13556678Seric printaddr(a, FALSE); 13656678Seric } 13756678Seric 13856678Seric return (a); 13956678Seric } 14056678Seric /* 14157388Seric ** INVALIDADDR -- check for address containing meta-characters 14257388Seric ** 14357388Seric ** Parameters: 14457388Seric ** addr -- the address to check. 14557388Seric ** 14657388Seric ** Returns: 14757388Seric ** TRUE -- if the address has any "wierd" characters 14857388Seric ** FALSE -- otherwise. 14957388Seric */ 15057388Seric 15157388Seric bool 15257388Seric invalidaddr(addr) 15357388Seric register char *addr; 15457388Seric { 15557388Seric for (; *addr != '\0'; addr++) 15657388Seric { 157*57454Seric if (!isascii((int) *addr & 0377) || 158*57454Seric !iscntrl(*addr) || isspace(*addr)) 15957388Seric continue; 16057388Seric setstat(EX_USAGE); 16157391Seric usrerr("Address contained invalid control characters"); 16257388Seric return TRUE; 16357388Seric } 16457388Seric return FALSE; 16557388Seric } 16657388Seric /* 16756678Seric ** ALLOCADDR -- do local allocations of address on demand. 16856678Seric ** 16956678Seric ** Also lowercases the host name if requested. 17056678Seric ** 17156678Seric ** Parameters: 17256678Seric ** a -- the address to reallocate. 17356678Seric ** copyf -- the copy flag (see parseaddr for description). 17456678Seric ** paddr -- the printname of the address. 17556678Seric ** 17656678Seric ** Returns: 17756678Seric ** none. 17856678Seric ** 17956678Seric ** Side Effects: 18056678Seric ** Copies portions of a into local buffers as requested. 18156678Seric */ 18256678Seric 18356678Seric allocaddr(a, copyf, paddr) 18456678Seric register ADDRESS *a; 18556678Seric int copyf; 18656678Seric char *paddr; 18756678Seric { 18856678Seric register MAILER *m = a->q_mailer; 18956678Seric 19056678Seric if (copyf > 0 && paddr != NULL) 19156678Seric { 19256678Seric extern char *DelimChar; 1938078Seric char savec = *DelimChar; 1948078Seric 1958078Seric *DelimChar = '\0'; 19656678Seric a->q_paddr = newstr(paddr); 1978078Seric *DelimChar = savec; 1988078Seric } 199297Seric else 20056678Seric a->q_paddr = paddr; 20124944Seric 20224944Seric if (a->q_user == NULL) 20324944Seric a->q_user = ""; 20424944Seric if (a->q_host == NULL) 20524944Seric a->q_host = ""; 20624944Seric 2073149Seric if (copyf >= 0) 208297Seric { 20924944Seric a->q_host = newstr(a->q_host); 2103149Seric if (a->q_user != a->q_paddr) 2113149Seric a->q_user = newstr(a->q_user); 212297Seric } 213297Seric 21456678Seric if (a->q_paddr == NULL) 21556678Seric a->q_paddr = a->q_user; 21656678Seric 217297Seric /* 21816202Seric ** Convert host name to lower case if requested. 21916202Seric ** User name will be done later. 22016202Seric */ 22116202Seric 22216202Seric if (!bitnset(M_HST_UPPER, m->m_flags)) 22316202Seric makelower(a->q_host); 224297Seric } 225297Seric /* 22616162Seric ** LOWERADDR -- map UPPER->lower case on addresses as requested. 22716162Seric ** 22816162Seric ** Parameters: 22916162Seric ** a -- address to be mapped. 23016162Seric ** 23116162Seric ** Returns: 23216162Seric ** none. 23316162Seric ** 23416162Seric ** Side Effects: 23516162Seric ** none. 23616162Seric */ 23716162Seric 23816162Seric loweraddr(a) 23916162Seric register ADDRESS *a; 24016162Seric { 24116162Seric register MAILER *m = a->q_mailer; 24216162Seric 24316162Seric if (!bitnset(M_USR_UPPER, m->m_flags)) 24416162Seric makelower(a->q_user); 24516162Seric } 24616162Seric /* 247297Seric ** PRESCAN -- Prescan name and make it canonical 248297Seric ** 2499374Seric ** Scans a name and turns it into a set of tokens. This process 2509374Seric ** deletes blanks and comments (in parentheses). 251297Seric ** 252297Seric ** This routine knows about quoted strings and angle brackets. 253297Seric ** 254297Seric ** There are certain subtleties to this routine. The one that 255297Seric ** comes to mind now is that backslashes on the ends of names 256297Seric ** are silently stripped off; this is intentional. The problem 257297Seric ** is that some versions of sndmsg (like at LBL) set the kill 258297Seric ** character to something other than @ when reading addresses; 259297Seric ** so people type "csvax.eric\@berkeley" -- which screws up the 260297Seric ** berknet mailer. 261297Seric ** 262297Seric ** Parameters: 263297Seric ** addr -- the name to chomp. 264297Seric ** delim -- the delimiter for the address, normally 265297Seric ** '\0' or ','; \0 is accepted in any case. 26615284Seric ** If '\t' then we are reading the .cf file. 26716914Seric ** pvpbuf -- place to put the saved text -- note that 26816914Seric ** the pointers are static. 269297Seric ** 270297Seric ** Returns: 2713149Seric ** A pointer to a vector of tokens. 272297Seric ** NULL on error. 273297Seric ** 274297Seric ** Side Effects: 27525279Seric ** sets DelimChar to point to the character matching 'delim'. 276297Seric */ 277297Seric 2788078Seric /* states and character types */ 2798078Seric # define OPR 0 /* operator */ 2808078Seric # define ATM 1 /* atom */ 2818078Seric # define QST 2 /* in quoted string */ 2828078Seric # define SPC 3 /* chewing up spaces */ 2838078Seric # define ONE 4 /* pick up one character */ 2843149Seric 2858078Seric # define NSTATES 5 /* number of states */ 2868078Seric # define TYPE 017 /* mask to select state type */ 2878078Seric 2888078Seric /* meta bits for table */ 2898078Seric # define M 020 /* meta character; don't pass through */ 2908078Seric # define B 040 /* cause a break */ 2918078Seric # define MB M|B /* meta-break */ 2928078Seric 2938078Seric static short StateTab[NSTATES][NSTATES] = 2948078Seric { 2958087Seric /* oldst chtype> OPR ATM QST SPC ONE */ 2969051Seric /*OPR*/ OPR|B, ATM|B, QST|B, SPC|MB, ONE|B, 2979051Seric /*ATM*/ OPR|B, ATM, QST|B, SPC|MB, ONE|B, 2989051Seric /*QST*/ QST, QST, OPR, QST, QST, 2998078Seric /*SPC*/ OPR, ATM, QST, SPC|M, ONE, 3008078Seric /*ONE*/ OPR, OPR, OPR, OPR, OPR, 3018078Seric }; 3028078Seric 3038078Seric # define NOCHAR -1 /* signal nothing in lookahead token */ 3048078Seric 30556678Seric char *DelimChar; /* set to point to the delimiter */ 30656678Seric 3073149Seric char ** 30816914Seric prescan(addr, delim, pvpbuf) 309297Seric char *addr; 310297Seric char delim; 31116914Seric char pvpbuf[]; 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; 3238078Seric static char *av[MAXATOM+1]; 32456678Seric extern int errno; 325297Seric 32615253Seric /* make sure error messages don't have garbage on them */ 32715253Seric errno = 0; 32815253Seric 32916914Seric q = pvpbuf; 3303149Seric bslashmode = FALSE; 3317800Seric cmntcnt = 0; 3328423Seric anglecnt = 0; 3333149Seric avp = av; 33456678Seric state = ATM; 3358078Seric c = NOCHAR; 3368078Seric p = addr; 33756764Seric if (tTd(22, 11)) 338297Seric { 3398078Seric printf("prescan: "); 3408078Seric xputs(p); 34123109Seric (void) putchar('\n'); 3428078Seric } 3438078Seric 3448078Seric do 3458078Seric { 3463149Seric /* read a token */ 3473149Seric tok = q; 3488078Seric for (;;) 349297Seric { 3508078Seric /* store away any old lookahead character */ 3518078Seric if (c != NOCHAR) 3528078Seric { 35315284Seric /* see if there is room */ 35416914Seric if (q >= &pvpbuf[PSBUFSIZE - 5]) 3558078Seric { 3568078Seric usrerr("Address too long"); 3578078Seric DelimChar = p; 3588078Seric return (NULL); 3598078Seric } 36015284Seric 36115284Seric /* squirrel it away */ 3628078Seric *q++ = c; 3638078Seric } 3648078Seric 3658078Seric /* read a new input character */ 3668078Seric c = *p++; 36756764Seric if (c == '\0' || (c == delim && anglecnt <= 0)) 36856764Seric { 36956764Seric /* diagnose and patch up bad syntax */ 37056764Seric if (state == QST) 37156764Seric { 37256764Seric usrerr("Unbalanced '\"'"); 37356764Seric c = '"'; 37456764Seric } 37556764Seric else if (cmntcnt > 0) 37656764Seric { 37756764Seric usrerr("Unbalanced '('"); 37856764Seric c = ')'; 37956764Seric } 38056764Seric else if (anglecnt > 0) 38156764Seric { 38256764Seric c = '>'; 38356764Seric usrerr("Unbalanced '<'"); 38456764Seric } 38556764Seric else 38656764Seric break; 38715284Seric 38856764Seric p--; 38956764Seric } 39056764Seric 3918078Seric if (tTd(22, 101)) 3928078Seric printf("c=%c, s=%d; ", c, state); 3938078Seric 3943149Seric /* chew up special characters */ 3953149Seric *q = '\0'; 3963149Seric if (bslashmode) 3973149Seric { 39824944Seric /* kludge \! for naive users */ 39924944Seric if (c != '!') 40056678Seric *q++ = '\\'; 4013149Seric bslashmode = FALSE; 40256678Seric continue; 4033149Seric } 40456678Seric 40556678Seric if (c == '\\') 4063149Seric { 4073149Seric bslashmode = TRUE; 4088078Seric c = NOCHAR; 40956678Seric continue; 4103149Seric } 41156678Seric else if (state == QST) 4128514Seric { 4138514Seric /* do nothing, just avoid next clauses */ 4148514Seric } 4158078Seric else if (c == '(') 4164100Seric { 4178078Seric cmntcnt++; 4188078Seric c = NOCHAR; 4194100Seric } 4208078Seric else if (c == ')') 4213149Seric { 4228078Seric if (cmntcnt <= 0) 4233149Seric { 4248078Seric usrerr("Unbalanced ')'"); 4258078Seric DelimChar = p; 4268078Seric return (NULL); 4273149Seric } 4288078Seric else 4298078Seric cmntcnt--; 4308078Seric } 4318078Seric else if (cmntcnt > 0) 4328078Seric c = NOCHAR; 4338423Seric else if (c == '<') 4348423Seric anglecnt++; 4358423Seric else if (c == '>') 4368423Seric { 4378423Seric if (anglecnt <= 0) 4388423Seric { 4398423Seric usrerr("Unbalanced '>'"); 4408423Seric DelimChar = p; 4418423Seric return (NULL); 4428423Seric } 4438423Seric anglecnt--; 4448423Seric } 44511423Seric else if (delim == ' ' && isspace(c)) 44611423Seric c = ' '; 4473149Seric 4488078Seric if (c == NOCHAR) 4498078Seric continue; 4503149Seric 4518078Seric /* see if this is end of input */ 45211405Seric if (c == delim && anglecnt <= 0 && state != QST) 4533149Seric break; 4543149Seric 4558078Seric newstate = StateTab[state][toktype(c)]; 4568078Seric if (tTd(22, 101)) 4578078Seric printf("ns=%02o\n", newstate); 4588078Seric state = newstate & TYPE; 4598078Seric if (bitset(M, newstate)) 4608078Seric c = NOCHAR; 4618078Seric if (bitset(B, newstate)) 4624228Seric break; 463297Seric } 4643149Seric 4653149Seric /* new token */ 4668078Seric if (tok != q) 4671378Seric { 4688078Seric *q++ = '\0'; 4698078Seric if (tTd(22, 36)) 470297Seric { 4718078Seric printf("tok="); 4728078Seric xputs(tok); 47323109Seric (void) putchar('\n'); 474297Seric } 4758078Seric if (avp >= &av[MAXATOM]) 476297Seric { 4778078Seric syserr("prescan: too many tokens"); 4788078Seric DelimChar = p; 4798078Seric return (NULL); 480297Seric } 4818078Seric *avp++ = tok; 482297Seric } 4838423Seric } while (c != '\0' && (c != delim || anglecnt > 0)); 4843149Seric *avp = NULL; 4858078Seric DelimChar = --p; 48656764Seric if (tTd(22, 12)) 48756764Seric { 48856764Seric printf("prescan==>"); 48956764Seric printav(av); 49056764Seric } 49156764Seric if (av[0] != NULL) 4923149Seric return (av); 4933149Seric return (NULL); 4943149Seric } 4953149Seric /* 4963149Seric ** TOKTYPE -- return token type 4973149Seric ** 4983149Seric ** Parameters: 4993149Seric ** c -- the character in question. 5003149Seric ** 5013149Seric ** Returns: 5023149Seric ** Its type. 5033149Seric ** 5043149Seric ** Side Effects: 5053149Seric ** none. 5063149Seric */ 507297Seric 5083149Seric toktype(c) 5093149Seric register char c; 5103149Seric { 5113380Seric static char buf[50]; 5123382Seric static bool firstime = TRUE; 5133380Seric 5143382Seric if (firstime) 5153380Seric { 5163382Seric firstime = FALSE; 51716155Seric expand("\001o", buf, &buf[sizeof buf - 1], CurEnv); 5187005Seric (void) strcat(buf, DELIMCHARS); 5193380Seric } 52056327Seric if (c == MATCHCLASS || c == MATCHREPL || c == MATCHNCLASS) 5218078Seric return (ONE); 5228078Seric if (c == '"') 5238078Seric return (QST); 5244100Seric if (!isascii(c)) 5258078Seric return (ATM); 5268078Seric if (isspace(c) || c == ')') 5278078Seric return (SPC); 52856795Seric if (iscntrl(c) || strchr(buf, c) != NULL) 5298078Seric return (OPR); 5308078Seric return (ATM); 5313149Seric } 5323149Seric /* 5333149Seric ** REWRITE -- apply rewrite rules to token vector. 5343149Seric ** 5354476Seric ** This routine is an ordered production system. Each rewrite 5364476Seric ** rule has a LHS (called the pattern) and a RHS (called the 5374476Seric ** rewrite); 'rwr' points the the current rewrite rule. 5384476Seric ** 5394476Seric ** For each rewrite rule, 'avp' points the address vector we 5404476Seric ** are trying to match against, and 'pvp' points to the pattern. 5418058Seric ** If pvp points to a special match value (MATCHZANY, MATCHANY, 5429585Seric ** MATCHONE, MATCHCLASS, MATCHNCLASS) then the address in avp 5439585Seric ** matched is saved away in the match vector (pointed to by 'mvp'). 5444476Seric ** 5454476Seric ** When a match between avp & pvp does not match, we try to 5469585Seric ** back out. If we back up over MATCHONE, MATCHCLASS, or MATCHNCLASS 5474476Seric ** we must also back out the match in mvp. If we reach a 5488058Seric ** MATCHANY or MATCHZANY we just extend the match and start 5498058Seric ** over again. 5504476Seric ** 5514476Seric ** When we finally match, we rewrite the address vector 5524476Seric ** and try over again. 5534476Seric ** 5543149Seric ** Parameters: 5553149Seric ** pvp -- pointer to token vector. 5563149Seric ** 5573149Seric ** Returns: 5583149Seric ** none. 5593149Seric ** 5603149Seric ** Side Effects: 5613149Seric ** pvp is modified. 5623149Seric */ 5632091Seric 5643149Seric struct match 5653149Seric { 5664468Seric char **first; /* first token matched */ 5674468Seric char **last; /* last token matched */ 5683149Seric }; 5693149Seric 5704468Seric # define MAXMATCH 9 /* max params per rewrite */ 5713149Seric 5723149Seric 5734070Seric rewrite(pvp, ruleset) 5743149Seric char **pvp; 5754070Seric int ruleset; 5763149Seric { 5773149Seric register char *ap; /* address pointer */ 5783149Seric register char *rp; /* rewrite pointer */ 5793149Seric register char **avp; /* address vector pointer */ 5803149Seric register char **rvp; /* rewrite vector pointer */ 5818058Seric register struct match *mlp; /* cur ptr into mlist */ 5828058Seric register struct rewrite *rwr; /* pointer to current rewrite rule */ 58356678Seric struct match mlist[MAXMATCH]; /* stores match on LHS */ 5843149Seric char *npvp[MAXATOM+1]; /* temporary space for rebuild */ 5853149Seric 5869279Seric if (OpMode == MD_TEST || tTd(21, 2)) 5873149Seric { 5888959Seric printf("rewrite: ruleset %2d input:", ruleset); 58956678Seric printav(pvp); 5903149Seric } 59156678Seric if (ruleset < 0 || ruleset >= MAXRWSETS) 59256326Seric { 59356678Seric syserr("rewrite: illegal ruleset number %d", ruleset); 59456326Seric return; 59556326Seric } 59656678Seric if (pvp == NULL) 59756678Seric return; 59856326Seric 5993149Seric /* 60056678Seric ** Run through the list of rewrite rules, applying 60156678Seric ** any that match. 6023149Seric */ 6033149Seric 6044070Seric for (rwr = RewriteRules[ruleset]; rwr != NULL; ) 6053149Seric { 60656678Seric int loopcount = 0; 60756678Seric 6087675Seric if (tTd(21, 12)) 609297Seric { 6108069Seric printf("-----trying rule:"); 61156678Seric printav(rwr->r_lhs); 6123149Seric } 6133149Seric 6143149Seric /* try to match on this rule */ 6154468Seric mlp = mlist; 6168058Seric rvp = rwr->r_lhs; 6178058Seric avp = pvp; 6188058Seric while ((ap = *avp) != NULL || *rvp != NULL) 6193149Seric { 62056678Seric if (++loopcount > 100) 62152637Seric { 62256678Seric syserr("Infinite loop in ruleset %d", ruleset); 62356678Seric printf("workspace: "); 62456678Seric printav(pvp); 62552637Seric break; 62652637Seric } 6273149Seric rp = *rvp; 6288058Seric if (tTd(21, 35)) 6298058Seric { 63056678Seric printf("operator="); 6318058Seric xputs(ap); 63256678Seric printf(", token="); 6338058Seric xputs(rp); 6348069Seric printf("\n"); 6358058Seric } 63656678Seric if (rp == NULL) 63756326Seric { 6383149Seric /* end-of-pattern before end-of-address */ 6398058Seric goto backup; 64056678Seric } 64156678Seric if (ap == NULL && *rp != MATCHZANY) 64256326Seric { 64356678Seric /* end-of-input */ 64456678Seric break; 645297Seric } 64656326Seric 64756678Seric switch (*rp) 6488058Seric { 64956678Seric register STAB *s; 65056326Seric 65156678Seric case MATCHCLASS: 65256678Seric case MATCHNCLASS: 65356678Seric /* match any token in (not in) a class */ 65456678Seric s = stab(ap, ST_CLASS, ST_FIND); 65556678Seric if (s == NULL || !bitnset(rp[1], s->s_class)) 65656326Seric { 65756678Seric if (*rp == MATCHCLASS) 6589585Seric goto backup; 65956326Seric } 66056678Seric else if (*rp == MATCHNCLASS) 66156678Seric goto backup; 6624060Seric 66356678Seric /* explicit fall-through */ 66456678Seric 66556678Seric case MATCHONE: 66656678Seric case MATCHANY: 66756678Seric /* match exactly one token */ 66856678Seric mlp->first = avp; 66956678Seric mlp->last = avp++; 6708058Seric mlp++; 67156678Seric break; 6728058Seric 67356678Seric case MATCHZANY: 67456678Seric /* match zero or more tokens */ 67556678Seric mlp->first = avp; 67656678Seric mlp->last = avp - 1; 67756678Seric mlp++; 67856678Seric break; 67956326Seric 68056678Seric default: 68156678Seric /* must have exact match */ 68256678Seric if (strcasecmp(rp, ap)) 6838058Seric goto backup; 6844468Seric avp++; 68556678Seric break; 6863149Seric } 6873149Seric 68856678Seric /* successful match on this token */ 6893149Seric rvp++; 6903149Seric continue; 6913149Seric 69256678Seric backup: 69356678Seric /* match failed -- back up */ 69456678Seric while (--rvp >= rwr->r_lhs) 6953149Seric { 69656678Seric rp = *rvp; 69756678Seric if (*rp == MATCHANY || *rp == MATCHZANY) 6984468Seric { 69956678Seric /* extend binding and continue */ 70056678Seric avp = ++mlp[-1].last; 70156678Seric avp++; 70256678Seric rvp++; 70356678Seric break; 7044468Seric } 70556678Seric avp--; 70656678Seric if (*rp == MATCHONE || *rp == MATCHCLASS || 70756678Seric *rp == MATCHNCLASS) 70856678Seric { 70956678Seric /* back out binding */ 71056678Seric mlp--; 71156678Seric } 7123149Seric } 7133149Seric 71456678Seric if (rvp < rwr->r_lhs) 71556678Seric { 71656678Seric /* total failure to match */ 71756326Seric break; 7183149Seric } 719297Seric } 7203149Seric 7213149Seric /* 72256678Seric ** See if we successfully matched 7233149Seric */ 7243149Seric 72556678Seric if (rvp < rwr->r_lhs || *rvp != NULL) 7263149Seric { 7279374Seric if (tTd(21, 10)) 7289374Seric printf("----- rule fails\n"); 7299374Seric rwr = rwr->r_next; 7309374Seric continue; 7319374Seric } 7323149Seric 7339374Seric rvp = rwr->r_rhs; 7349374Seric if (tTd(21, 12)) 7359374Seric { 7369374Seric printf("-----rule matches:"); 73756678Seric printav(rvp); 7389374Seric } 7399374Seric 7409374Seric rp = *rvp; 7419374Seric if (*rp == CANONUSER) 7429374Seric { 7439374Seric rvp++; 7449374Seric rwr = rwr->r_next; 7459374Seric } 7469374Seric else if (*rp == CANONHOST) 7479374Seric { 7489374Seric rvp++; 7499374Seric rwr = NULL; 7509374Seric } 7519374Seric else if (*rp == CANONNET) 7529374Seric rwr = NULL; 7539374Seric 7549374Seric /* substitute */ 7559374Seric for (avp = npvp; *rvp != NULL; rvp++) 7569374Seric { 7579374Seric register struct match *m; 7589374Seric register char **pp; 7599374Seric 7608058Seric rp = *rvp; 76156678Seric if (*rp == MATCHREPL) 7628058Seric { 76316914Seric /* substitute from LHS */ 76416914Seric m = &mlist[rp[1] - '1']; 76556678Seric if (m < mlist || m >= mlp) 7669374Seric { 76756678Seric syserr("rewrite: ruleset %d: replacement $%c out of bounds", 76856326Seric ruleset, rp[1]); 7699374Seric return; 7709374Seric } 77116914Seric if (tTd(21, 15)) 77216914Seric { 77316914Seric printf("$%c:", rp[1]); 77416914Seric pp = m->first; 77556678Seric while (pp <= m->last) 77616914Seric { 77716914Seric printf(" %x=\"", *pp); 77816914Seric (void) fflush(stdout); 77916914Seric printf("%s\"", *pp++); 78016914Seric } 78116914Seric printf("\n"); 78216914Seric } 7839374Seric pp = m->first; 78456678Seric while (pp <= m->last) 7853149Seric { 78616914Seric if (avp >= &npvp[MAXATOM]) 78756678Seric { 78856678Seric syserr("rewrite: expansion too long"); 78956678Seric return; 79056678Seric } 79116914Seric *avp++ = *pp++; 7923149Seric } 7933149Seric } 79416914Seric else 7958226Seric { 79616914Seric /* vanilla replacement */ 7979374Seric if (avp >= &npvp[MAXATOM]) 79816889Seric { 79956678Seric toolong: 80016889Seric syserr("rewrite: expansion too long"); 80116889Seric return; 80216889Seric } 80356678Seric *avp++ = rp; 8048226Seric } 8059374Seric } 8069374Seric *avp++ = NULL; 80716914Seric 80816914Seric /* 80956678Seric ** Check for any hostname/keyword lookups. 81016914Seric */ 81116914Seric 81216914Seric for (rvp = npvp; *rvp != NULL; rvp++) 81316914Seric { 81456678Seric char **hbrvp; 81516914Seric char **xpvp; 81616914Seric int trsize; 81717473Seric char *olddelimchar; 81856678Seric char *replac; 81956678Seric int endtoken; 82056678Seric STAB *map; 82156678Seric char *mapname; 82256678Seric char **key_rvp; 82356678Seric char **arg_rvp; 82456678Seric char **default_rvp; 82556678Seric char buf[MAXNAME + 1]; 82616914Seric char *pvpb1[MAXATOM + 1]; 82756823Seric char *argvect[10]; 82817174Seric char pvpbuf[PSBUFSIZE]; 82956678Seric extern char *DelimChar; 83016914Seric 83156678Seric if (**rvp != HOSTBEGIN && **rvp != LOOKUPBEGIN) 83216914Seric continue; 83316914Seric 83416914Seric /* 83556678Seric ** Got a hostname/keyword lookup. 83616914Seric ** 83716914Seric ** This could be optimized fairly easily. 83816914Seric */ 83916914Seric 84016914Seric hbrvp = rvp; 84156678Seric if (**rvp == HOSTBEGIN) 84256327Seric { 84356678Seric endtoken = HOSTEND; 84456678Seric mapname = "host"; 84556327Seric } 84656326Seric else 84756327Seric { 84856678Seric endtoken = LOOKUPEND; 84956678Seric mapname = *++rvp; 85056327Seric } 85156678Seric map = stab(mapname, ST_MAP, ST_FIND); 85256678Seric if (map == NULL) 85356678Seric syserr("rewrite: map %s not found", mapname); 85456678Seric 85556678Seric /* extract the match part */ 85656678Seric key_rvp = ++rvp; 85756823Seric default_rvp = NULL; 85856823Seric arg_rvp = argvect; 85956823Seric xpvp = NULL; 86056823Seric replac = pvpbuf; 86156678Seric while (*rvp != NULL && **rvp != endtoken) 86253654Seric { 86356823Seric int nodetype = **rvp; 86456823Seric 86556823Seric if (nodetype != CANONHOST && nodetype != CANONUSER) 86656678Seric { 86756823Seric rvp++; 86856823Seric continue; 86956823Seric } 87056823Seric 87156823Seric *rvp++ = NULL; 87256823Seric 87356823Seric if (xpvp != NULL) 87456823Seric { 87556823Seric cataddr(xpvp, replac, 87656823Seric &pvpbuf[sizeof pvpbuf] - replac); 87756823Seric *++arg_rvp = replac; 87856823Seric replac += strlen(replac) + 1; 87956823Seric xpvp = NULL; 88056823Seric } 88156823Seric switch (nodetype) 88256823Seric { 88356678Seric case CANONHOST: 88456823Seric xpvp = rvp; 88556678Seric break; 88656678Seric 88756678Seric case CANONUSER: 88856678Seric default_rvp = rvp; 88956678Seric break; 89056678Seric } 89153654Seric } 89216914Seric if (*rvp != NULL) 89316914Seric *rvp++ = NULL; 89456823Seric if (xpvp != NULL) 89556823Seric { 89656823Seric cataddr(xpvp, replac, 89756823Seric &pvpbuf[sizeof pvpbuf] - replac); 89856823Seric *++arg_rvp = replac; 89956823Seric } 90056823Seric *++arg_rvp = NULL; 90116914Seric 90216914Seric /* save the remainder of the input string */ 90316914Seric trsize = (int) (avp - rvp + 1) * sizeof *rvp; 90416914Seric bcopy((char *) rvp, (char *) pvpb1, trsize); 90516914Seric 90656678Seric /* look it up */ 90756678Seric cataddr(key_rvp, buf, sizeof buf); 90856823Seric argvect[0] = buf; 90956678Seric if (map != NULL && bitset(MF_VALID, map->s_map.map_flags)) 91056836Seric { 91156836Seric int bsize = sizeof buf - 1; 91256836Seric 91356836Seric if (map->s_map.map_app != NULL) 91456836Seric bsize -= strlen(map->s_map.map_app); 91556836Seric replac = (*map->s_map.map_class->map_lookup)(&map->s_map, 91656823Seric buf, sizeof buf - 1, argvect); 91756836Seric if (replac != NULL && map->s_map.map_app != NULL) 91856836Seric strcat(replac, map->s_map.map_app); 91956836Seric } 92053654Seric else 92156678Seric replac = NULL; 92256678Seric 92356678Seric /* if no replacement, use default */ 92456823Seric if (replac == NULL && default_rvp != NULL) 92556823Seric { 92656823Seric char buf2[sizeof buf]; 92756823Seric 92856823Seric /* rewrite the default with % translations */ 92956823Seric cataddr(default_rvp, buf2, sizeof buf2); 93056823Seric map_rewrite(buf2, sizeof buf2, buf, sizeof buf, 93156823Seric argvect); 93256823Seric replac = buf; 93356823Seric } 93456823Seric 93556678Seric if (replac == NULL) 93651317Seric { 93756823Seric xpvp = key_rvp; 93853654Seric } 93956678Seric else 94053654Seric { 94156678Seric /* scan the new replacement */ 94256678Seric olddelimchar = DelimChar; 94356678Seric xpvp = prescan(replac, '\0', pvpbuf); 94456678Seric DelimChar = olddelimchar; 94553654Seric if (xpvp == NULL) 94651317Seric { 94756678Seric syserr("rewrite: cannot prescan map value: %s", replac); 94856678Seric return; 94956678Seric } 95051317Seric } 95151317Seric 95216914Seric /* append it to the token list */ 95356678Seric for (avp = hbrvp; *xpvp != NULL; xpvp++) 95456678Seric { 95517174Seric *avp++ = newstr(*xpvp); 95616920Seric if (avp >= &npvp[MAXATOM]) 95716914Seric goto toolong; 95817174Seric } 95916914Seric 96016914Seric /* restore the old trailing information */ 96156678Seric for (xpvp = pvpb1; (*avp++ = *xpvp++) != NULL; ) 96216920Seric if (avp >= &npvp[MAXATOM]) 96316914Seric goto toolong; 96417174Seric 96556678Seric break; 96616914Seric } 96716914Seric 96816914Seric /* 96916914Seric ** Check for subroutine calls. 97016914Seric */ 97116914Seric 97256678Seric if (*npvp != NULL && **npvp == CALLSUBR) 97356678Seric { 97456678Seric bcopy((char *) &npvp[2], (char *) pvp, 97556678Seric (int) (avp - npvp - 2) * sizeof *avp); 97656678Seric if (tTd(21, 3)) 97756678Seric printf("-----callsubr %s\n", npvp[1]); 97856678Seric rewrite(pvp, atoi(npvp[1])); 97956678Seric } 98056678Seric else 98156678Seric { 98217348Seric bcopy((char *) npvp, (char *) pvp, 98316900Seric (int) (avp - npvp) * sizeof *avp); 98456678Seric } 9859374Seric if (tTd(21, 4)) 9869374Seric { 9879374Seric printf("rewritten as:"); 98856678Seric printav(pvp); 9899374Seric } 990297Seric } 9918069Seric 9929279Seric if (OpMode == MD_TEST || tTd(21, 2)) 9938069Seric { 9948959Seric printf("rewrite: ruleset %2d returns:", ruleset); 99556678Seric printav(pvp); 9968069Seric } 9973149Seric } 9983149Seric /* 9993149Seric ** BUILDADDR -- build address from token vector. 10003149Seric ** 10013149Seric ** Parameters: 10023149Seric ** tv -- token vector. 10033149Seric ** a -- pointer to address descriptor to fill. 10043149Seric ** If NULL, one will be allocated. 10053149Seric ** 10063149Seric ** Returns: 10074279Seric ** NULL if there was an error. 10084279Seric ** 'a' otherwise. 10093149Seric ** 10103149Seric ** Side Effects: 10113149Seric ** fills in 'a' 10123149Seric */ 10133149Seric 101457249Seric struct errcodes 101557249Seric { 101657249Seric char *ec_name; /* name of error code */ 101757249Seric int ec_code; /* numeric code */ 101857249Seric } ErrorCodes[] = 101957249Seric { 102057249Seric "usage", EX_USAGE, 102157249Seric "nouser", EX_NOUSER, 102257249Seric "nohost", EX_NOHOST, 102357249Seric "unavailable", EX_UNAVAILABLE, 102457249Seric "software", EX_SOFTWARE, 102557249Seric "tempfail", EX_TEMPFAIL, 102657249Seric "protocol", EX_PROTOCOL, 102757249Seric #ifdef EX_CONFIG 102857249Seric "config", EX_CONFIG, 102957249Seric #endif 103057249Seric NULL, EX_UNAVAILABLE, 103157249Seric }; 103257249Seric 103356678Seric ADDRESS * 10343149Seric buildaddr(tv, a) 10353149Seric register char **tv; 10363149Seric register ADDRESS *a; 10373149Seric { 10383149Seric struct mailer **mp; 10393149Seric register struct mailer *m; 104057402Seric static char buf[MAXNAME]; 10413149Seric 10423149Seric if (a == NULL) 10433149Seric a = (ADDRESS *) xalloc(sizeof *a); 104416889Seric bzero((char *) a, sizeof *a); 10453149Seric 10463149Seric /* figure out what net/mailer to use */ 104756678Seric if (**tv != CANONNET) 10484279Seric { 10493149Seric syserr("buildaddr: no net"); 10504279Seric return (NULL); 10514279Seric } 10523149Seric tv++; 105333725Sbostic if (!strcasecmp(*tv, "error")) 10544279Seric { 105510183Seric if (**++tv == CANONHOST) 105610183Seric { 105757249Seric register struct errcodes *ep; 105857249Seric 105957249Seric if (isdigit(**++tv)) 106057249Seric { 106157249Seric setstat(atoi(*tv)); 106257249Seric } 106357249Seric else 106457249Seric { 106557249Seric for (ep = ErrorCodes; ep->ec_name != NULL; ep++) 106657249Seric if (strcasecmp(ep->ec_name, *tv) == 0) 106757249Seric break; 106857249Seric setstat(ep->ec_code); 106957249Seric } 107010183Seric tv++; 107110183Seric } 107210183Seric if (**tv != CANONUSER) 10734279Seric syserr("buildaddr: error: no user"); 107456678Seric buf[0] = '\0'; 10754279Seric while (*++tv != NULL) 10764279Seric { 10774279Seric if (buf[0] != '\0') 10787005Seric (void) strcat(buf, " "); 10797005Seric (void) strcat(buf, *tv); 10804279Seric } 10814279Seric usrerr(buf); 10824279Seric return (NULL); 10834279Seric } 108457402Seric 10854598Seric for (mp = Mailer; (m = *mp++) != NULL; ) 10863149Seric { 108733725Sbostic if (!strcasecmp(m->m_name, *tv)) 10883149Seric break; 10893149Seric } 10903149Seric if (m == NULL) 10914279Seric { 109224944Seric syserr("buildaddr: unknown mailer %s", *tv); 10934279Seric return (NULL); 10944279Seric } 10954598Seric a->q_mailer = m; 10963149Seric 10973149Seric /* figure out what host (if any) */ 109856678Seric tv++; 109957249Seric if (!bitnset(M_LOCAL, m->m_flags)) 11003149Seric { 110157249Seric if (**tv++ != CANONHOST) 11024279Seric { 11033149Seric syserr("buildaddr: no host"); 11044279Seric return (NULL); 11054279Seric } 11065704Seric buf[0] = '\0'; 110757249Seric while (*tv != NULL && **tv != CANONUSER) 110857249Seric (void) strcat(buf, *tv++); 11095704Seric a->q_host = newstr(buf); 11103149Seric } 111157249Seric else 111257249Seric a->q_host = NULL; 11133149Seric 11143149Seric /* figure out the user */ 111536615Sbostic if (*tv == NULL || **tv != CANONUSER) 11164279Seric { 11173149Seric syserr("buildaddr: no user"); 11184279Seric return (NULL); 11194279Seric } 112019040Seric 112156678Seric if (m == LocalMailer && tv[1] != NULL && strcmp(tv[1], "@") == 0) 112256678Seric { 112356678Seric tv++; 112456678Seric a->q_flags |= QNOTREMOTE; 112556678Seric } 112657402Seric tv++; 112751317Seric 112857402Seric /* do special mapping for local mailer */ 112957402Seric if (m == LocalMailer && *tv != NULL) 113057402Seric { 1131*57454Seric register char *p = *tv; 1132*57454Seric 1133*57454Seric if (*p == '"') 1134*57454Seric p++; 1135*57454Seric if (*p == '|') 113657402Seric a->q_mailer = m = ProgMailer; 1137*57454Seric else if (*p == '/') 113857402Seric a->q_mailer = m = FileMailer; 1139*57454Seric else if (*p == ':') 114057402Seric { 114157402Seric /* may be :include: */ 114257402Seric cataddr(tv, buf, sizeof buf); 1143*57454Seric p = buf; 1144*57454Seric if (*p == '"') 1145*57454Seric p++; 1146*57454Seric p[9] = '\0'; 1147*57454Seric if (strcasecmp(p, ":include:") == 0) 114857402Seric a->q_mailer = m = InclMailer; 114957402Seric } 115057402Seric } 115157402Seric 115219040Seric /* rewrite according recipient mailer rewriting rules */ 115357402Seric rewrite(tv, 2); 115456327Seric if (m->m_r_rwset > 0) 115556327Seric rewrite(tv, m->m_r_rwset); 115619040Seric rewrite(tv, 4); 115719040Seric 115819040Seric /* save the result for the command line/RCPT argument */ 115911278Seric cataddr(tv, buf, sizeof buf); 11603149Seric a->q_user = buf; 11613149Seric 11623149Seric return (a); 11633149Seric } 11643188Seric /* 11654228Seric ** CATADDR -- concatenate pieces of addresses (putting in <LWSP> subs) 11664228Seric ** 11674228Seric ** Parameters: 11684228Seric ** pvp -- parameter vector to rebuild. 11694228Seric ** buf -- buffer to build the string into. 11704228Seric ** sz -- size of buf. 11714228Seric ** 11724228Seric ** Returns: 11734228Seric ** none. 11744228Seric ** 11754228Seric ** Side Effects: 11764228Seric ** Destroys buf. 11774228Seric */ 11784228Seric 11794228Seric cataddr(pvp, buf, sz) 11804228Seric char **pvp; 11814228Seric char *buf; 11824228Seric register int sz; 11834228Seric { 11844228Seric bool oatomtok = FALSE; 118556678Seric bool natomtok = FALSE; 11864228Seric register int i; 11874228Seric register char *p; 11884228Seric 11898423Seric if (pvp == NULL) 11908423Seric { 119123109Seric (void) strcpy(buf, ""); 11928423Seric return; 11938423Seric } 11944228Seric p = buf; 119511156Seric sz -= 2; 11964228Seric while (*pvp != NULL && (i = strlen(*pvp)) < sz) 11974228Seric { 11988078Seric natomtok = (toktype(**pvp) == ATM); 11994228Seric if (oatomtok && natomtok) 12009042Seric *p++ = SpaceSub; 12014228Seric (void) strcpy(p, *pvp); 12024228Seric oatomtok = natomtok; 12034228Seric p += i; 120411156Seric sz -= i + 1; 12054228Seric pvp++; 12064228Seric } 12074228Seric *p = '\0'; 12084228Seric } 12094228Seric /* 12103188Seric ** SAMEADDR -- Determine if two addresses are the same 12113188Seric ** 12123188Seric ** This is not just a straight comparison -- if the mailer doesn't 12133188Seric ** care about the host we just ignore it, etc. 12143188Seric ** 12153188Seric ** Parameters: 12163188Seric ** a, b -- pointers to the internal forms to compare. 12173188Seric ** 12183188Seric ** Returns: 12193188Seric ** TRUE -- they represent the same mailbox. 12203188Seric ** FALSE -- they don't. 12213188Seric ** 12223188Seric ** Side Effects: 12233188Seric ** none. 12243188Seric */ 12253188Seric 12263188Seric bool 12279374Seric sameaddr(a, b) 12283188Seric register ADDRESS *a; 12293188Seric register ADDRESS *b; 12303188Seric { 12313188Seric /* if they don't have the same mailer, forget it */ 12323188Seric if (a->q_mailer != b->q_mailer) 12333188Seric return (FALSE); 12343188Seric 12353188Seric /* if the user isn't the same, we can drop out */ 123656678Seric if (strcmp(a->q_user, b->q_user) != 0) 12373188Seric return (FALSE); 12383188Seric 12393188Seric /* if the mailer ignores hosts, we have succeeded! */ 124010690Seric if (bitnset(M_LOCAL, a->q_mailer->m_flags)) 12413188Seric return (TRUE); 12423188Seric 12433188Seric /* otherwise compare hosts (but be careful for NULL ptrs) */ 12443188Seric if (a->q_host == NULL || b->q_host == NULL) 12453188Seric return (FALSE); 124656678Seric if (strcmp(a->q_host, b->q_host) != 0) 12473188Seric return (FALSE); 12483188Seric 12493188Seric return (TRUE); 12503188Seric } 12513234Seric /* 12523234Seric ** PRINTADDR -- print address (for debugging) 12533234Seric ** 12543234Seric ** Parameters: 12553234Seric ** a -- the address to print 12563234Seric ** follow -- follow the q_next chain. 12573234Seric ** 12583234Seric ** Returns: 12593234Seric ** none. 12603234Seric ** 12613234Seric ** Side Effects: 12623234Seric ** none. 12633234Seric */ 12643234Seric 12653234Seric printaddr(a, follow) 12663234Seric register ADDRESS *a; 12673234Seric bool follow; 12683234Seric { 12695001Seric bool first = TRUE; 12705001Seric 12713234Seric while (a != NULL) 12723234Seric { 12735001Seric first = FALSE; 12744443Seric printf("%x=", a); 12754085Seric (void) fflush(stdout); 127640973Sbostic printf("%s: mailer %d (%s), host `%s', user `%s', ruser `%s'\n", 127740973Sbostic a->q_paddr, a->q_mailer->m_mno, a->q_mailer->m_name, 127840973Sbostic a->q_host, a->q_user, a->q_ruser? a->q_ruser: "<null>"); 12798181Seric printf("\tnext=%x, flags=%o, alias %x\n", a->q_next, a->q_flags, 12808181Seric a->q_alias); 12818181Seric printf("\thome=\"%s\", fullname=\"%s\"\n", a->q_home, 12828181Seric a->q_fullname); 12834996Seric 12843234Seric if (!follow) 12853234Seric return; 12864996Seric a = a->q_next; 12873234Seric } 12885001Seric if (first) 12894443Seric printf("[NULL]\n"); 12903234Seric } 12914317Seric 12927682Seric /* 12937682Seric ** REMOTENAME -- return the name relative to the current mailer 12947682Seric ** 12957682Seric ** Parameters: 12967682Seric ** name -- the name to translate. 12978069Seric ** m -- the mailer that we want to do rewriting relative 12988069Seric ** to. 12998069Seric ** senderaddress -- if set, uses the sender rewriting rules 13008069Seric ** rather than the recipient rewriting rules. 130110310Seric ** canonical -- if set, strip out any comment information, 130210310Seric ** etc. 13037682Seric ** 13047682Seric ** Returns: 13057682Seric ** the text string representing this address relative to 13067682Seric ** the receiving mailer. 13077682Seric ** 13087682Seric ** Side Effects: 13097682Seric ** none. 13107682Seric ** 13117682Seric ** Warnings: 13127682Seric ** The text string returned is tucked away locally; 13137682Seric ** copy it if you intend to save it. 13147682Seric */ 13157682Seric 13167682Seric char * 131756678Seric remotename(name, m, senderaddress, canonical, e) 13187682Seric char *name; 131956678Seric struct mailer *m; 13208069Seric bool senderaddress; 132110310Seric bool canonical; 132256678Seric register ENVELOPE *e; 13237682Seric { 13248069Seric register char **pvp; 13258069Seric char *fancy; 132656678Seric extern char *macvalue(); 132756678Seric char *oldg = macvalue('g', e); 13287682Seric static char buf[MAXNAME]; 13297682Seric char lbuf[MAXNAME]; 133016914Seric char pvpbuf[PSBUFSIZE]; 133156678Seric extern char **prescan(); 133256678Seric extern char *crackaddr(); 13337682Seric 13347755Seric if (tTd(12, 1)) 13357755Seric printf("remotename(%s)\n", name); 13367755Seric 133710177Seric /* don't do anything if we are tagging it as special */ 133856327Seric if ((senderaddress ? m->m_s_rwset : m->m_r_rwset) < 0) 133910177Seric return (name); 134010177Seric 13417682Seric /* 13428181Seric ** Do a heuristic crack of this name to extract any comment info. 13438181Seric ** This will leave the name as a comment and a $g macro. 13447889Seric */ 13457889Seric 134610310Seric if (canonical) 134716155Seric fancy = "\001g"; 134810310Seric else 134910310Seric fancy = crackaddr(name); 13507889Seric 13518181Seric /* 13528181Seric ** Turn the name into canonical form. 13538181Seric ** Normally this will be RFC 822 style, i.e., "user@domain". 13548181Seric ** If this only resolves to "user", and the "C" flag is 13558181Seric ** specified in the sending mailer, then the sender's 13568181Seric ** domain will be appended. 13578181Seric */ 13588181Seric 135916914Seric pvp = prescan(name, '\0', pvpbuf); 13607889Seric if (pvp == NULL) 13617889Seric return (name); 13628181Seric rewrite(pvp, 3); 136356678Seric if (e->e_fromdomain != NULL) 13648181Seric { 13658181Seric /* append from domain to this address */ 13668181Seric register char **pxp = pvp; 13678181Seric 13689594Seric /* see if there is an "@domain" in the current name */ 13698181Seric while (*pxp != NULL && strcmp(*pxp, "@") != 0) 13708181Seric pxp++; 13718181Seric if (*pxp == NULL) 13728181Seric { 13739594Seric /* no.... append the "@domain" from the sender */ 137456678Seric register char **qxq = e->e_fromdomain; 13758181Seric 13769594Seric while ((*pxp++ = *qxq++) != NULL) 13779594Seric continue; 137811726Seric rewrite(pvp, 3); 13798181Seric } 13808181Seric } 13818181Seric 13828181Seric /* 13838959Seric ** Do more specific rewriting. 138456678Seric ** Rewrite using ruleset 1 or 2 depending on whether this is 138556678Seric ** a sender address or not. 13868181Seric ** Then run it through any receiving-mailer-specific rulesets. 13878181Seric */ 13888181Seric 13898069Seric if (senderaddress) 13907755Seric { 139156327Seric rewrite(pvp, 1); 139256327Seric if (m->m_s_rwset > 0) 139356327Seric rewrite(pvp, m->m_s_rwset); 13948069Seric } 13958069Seric else 13968069Seric { 139756327Seric rewrite(pvp, 2); 139856327Seric if (m->m_r_rwset > 0) 139956327Seric rewrite(pvp, m->m_r_rwset); 14007682Seric } 14017682Seric 14028181Seric /* 14038959Seric ** Do any final sanitation the address may require. 14048959Seric ** This will normally be used to turn internal forms 14058959Seric ** (e.g., user@host.LOCAL) into external form. This 14068959Seric ** may be used as a default to the above rules. 14078959Seric */ 14088959Seric 14098959Seric rewrite(pvp, 4); 14108959Seric 14118959Seric /* 14128181Seric ** Now restore the comment information we had at the beginning. 14138181Seric */ 14148181Seric 14157682Seric cataddr(pvp, lbuf, sizeof lbuf); 141656678Seric define('g', lbuf, e); 141756678Seric expand(fancy, buf, &buf[sizeof buf - 1], e); 141856678Seric define('g', oldg, e); 14197682Seric 14207682Seric if (tTd(12, 1)) 14217755Seric printf("remotename => `%s'\n", buf); 14227682Seric return (buf); 14237682Seric } 142451317Seric /* 142556678Seric ** MAPLOCALUSER -- run local username through ruleset 5 for final redirection 142651317Seric ** 142751317Seric ** Parameters: 142856678Seric ** a -- the address to map (but just the user name part). 142956678Seric ** sendq -- the sendq in which to install any replacement 143056678Seric ** addresses. 143151317Seric ** 143251317Seric ** Returns: 143351317Seric ** none. 143451317Seric */ 143551317Seric 143656678Seric maplocaluser(a, sendq, e) 143756678Seric register ADDRESS *a; 143856678Seric ADDRESS **sendq; 143956678Seric ENVELOPE *e; 144051317Seric { 144156678Seric register char **pvp; 144256678Seric register ADDRESS *a1 = NULL; 144356678Seric char pvpbuf[PSBUFSIZE]; 144451317Seric 144556678Seric if (tTd(29, 1)) 144656678Seric { 144756678Seric printf("maplocaluser: "); 144856678Seric printaddr(a, FALSE); 144956678Seric } 145056678Seric pvp = prescan(a->q_user, '\0', pvpbuf); 145156678Seric if (pvp == NULL) 145256678Seric return; 145351317Seric 145456678Seric rewrite(pvp, 5); 145556678Seric if (pvp[0] == NULL || pvp[0][0] != CANONNET) 145656678Seric return; 145751317Seric 145856678Seric /* if non-null, mailer destination specified -- has it changed? */ 145956678Seric a1 = buildaddr(pvp, NULL); 146056678Seric if (a1 == NULL || sameaddr(a, a1)) 146156678Seric return; 146251317Seric 146356678Seric /* mark old address as dead; insert new address */ 146456678Seric a->q_flags |= QDONTSEND; 146556678Seric a1->q_alias = a; 146656678Seric allocaddr(a1, 1, NULL); 146756678Seric (void) recipient(a1, sendq, e); 146851317Seric } 1469