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*57731Seric static char sccsid[] = "@(#)parseaddr.c 6.10 (Berkeley) 01/26/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 { 15757454Seric if (!isascii((int) *addr & 0377) || 15857454Seric !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++; 36757631Seric if (c == '\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 } 39057631Seric else if (c == delim && anglecnt <= 0 && 39157631Seric cmntcnt <= 0 && state != QST) 39257631Seric break; 39356764Seric 3948078Seric if (tTd(22, 101)) 3958078Seric printf("c=%c, s=%d; ", c, state); 3968078Seric 3973149Seric /* chew up special characters */ 3983149Seric *q = '\0'; 3993149Seric if (bslashmode) 4003149Seric { 40124944Seric /* kludge \! for naive users */ 40224944Seric if (c != '!') 40356678Seric *q++ = '\\'; 4043149Seric bslashmode = FALSE; 40556678Seric continue; 4063149Seric } 40756678Seric 40856678Seric if (c == '\\') 4093149Seric { 4103149Seric bslashmode = TRUE; 4118078Seric c = NOCHAR; 41256678Seric continue; 4133149Seric } 41456678Seric else if (state == QST) 4158514Seric { 4168514Seric /* do nothing, just avoid next clauses */ 4178514Seric } 4188078Seric else if (c == '(') 4194100Seric { 4208078Seric cmntcnt++; 4218078Seric c = NOCHAR; 4224100Seric } 4238078Seric else if (c == ')') 4243149Seric { 4258078Seric if (cmntcnt <= 0) 4263149Seric { 4278078Seric usrerr("Unbalanced ')'"); 4288078Seric DelimChar = p; 4298078Seric return (NULL); 4303149Seric } 4318078Seric else 4328078Seric cmntcnt--; 4338078Seric } 4348078Seric else if (cmntcnt > 0) 4358078Seric c = NOCHAR; 4368423Seric else if (c == '<') 4378423Seric anglecnt++; 4388423Seric else if (c == '>') 4398423Seric { 4408423Seric if (anglecnt <= 0) 4418423Seric { 4428423Seric usrerr("Unbalanced '>'"); 4438423Seric DelimChar = p; 4448423Seric return (NULL); 4458423Seric } 4468423Seric anglecnt--; 4478423Seric } 44811423Seric else if (delim == ' ' && isspace(c)) 44911423Seric c = ' '; 4503149Seric 4518078Seric if (c == NOCHAR) 4528078Seric continue; 4533149Seric 4548078Seric /* see if this is end of input */ 45511405Seric if (c == delim && anglecnt <= 0 && state != QST) 4563149Seric break; 4573149Seric 4588078Seric newstate = StateTab[state][toktype(c)]; 4598078Seric if (tTd(22, 101)) 4608078Seric printf("ns=%02o\n", newstate); 4618078Seric state = newstate & TYPE; 4628078Seric if (bitset(M, newstate)) 4638078Seric c = NOCHAR; 4648078Seric if (bitset(B, newstate)) 4654228Seric break; 466297Seric } 4673149Seric 4683149Seric /* new token */ 4698078Seric if (tok != q) 4701378Seric { 4718078Seric *q++ = '\0'; 4728078Seric if (tTd(22, 36)) 473297Seric { 4748078Seric printf("tok="); 4758078Seric xputs(tok); 47623109Seric (void) putchar('\n'); 477297Seric } 4788078Seric if (avp >= &av[MAXATOM]) 479297Seric { 4808078Seric syserr("prescan: too many tokens"); 4818078Seric DelimChar = p; 4828078Seric return (NULL); 483297Seric } 4848078Seric *avp++ = tok; 485297Seric } 4868423Seric } while (c != '\0' && (c != delim || anglecnt > 0)); 4873149Seric *avp = NULL; 4888078Seric DelimChar = --p; 48956764Seric if (tTd(22, 12)) 49056764Seric { 49156764Seric printf("prescan==>"); 49256764Seric printav(av); 49356764Seric } 49456764Seric if (av[0] != NULL) 4953149Seric return (av); 4963149Seric return (NULL); 4973149Seric } 4983149Seric /* 4993149Seric ** TOKTYPE -- return token type 5003149Seric ** 5013149Seric ** Parameters: 5023149Seric ** c -- the character in question. 5033149Seric ** 5043149Seric ** Returns: 5053149Seric ** Its type. 5063149Seric ** 5073149Seric ** Side Effects: 5083149Seric ** none. 5093149Seric */ 510297Seric 5113149Seric toktype(c) 5123149Seric register char c; 5133149Seric { 5143380Seric static char buf[50]; 5153382Seric static bool firstime = TRUE; 5163380Seric 5173382Seric if (firstime) 5183380Seric { 5193382Seric firstime = FALSE; 52016155Seric expand("\001o", buf, &buf[sizeof buf - 1], CurEnv); 5217005Seric (void) strcat(buf, DELIMCHARS); 5223380Seric } 52356327Seric if (c == MATCHCLASS || c == MATCHREPL || c == MATCHNCLASS) 5248078Seric return (ONE); 5258078Seric if (c == '"') 5268078Seric return (QST); 5274100Seric if (!isascii(c)) 5288078Seric return (ATM); 5298078Seric if (isspace(c) || c == ')') 5308078Seric return (SPC); 53156795Seric if (iscntrl(c) || strchr(buf, c) != NULL) 5328078Seric return (OPR); 5338078Seric return (ATM); 5343149Seric } 5353149Seric /* 5363149Seric ** REWRITE -- apply rewrite rules to token vector. 5373149Seric ** 5384476Seric ** This routine is an ordered production system. Each rewrite 5394476Seric ** rule has a LHS (called the pattern) and a RHS (called the 5404476Seric ** rewrite); 'rwr' points the the current rewrite rule. 5414476Seric ** 5424476Seric ** For each rewrite rule, 'avp' points the address vector we 5434476Seric ** are trying to match against, and 'pvp' points to the pattern. 5448058Seric ** If pvp points to a special match value (MATCHZANY, MATCHANY, 5459585Seric ** MATCHONE, MATCHCLASS, MATCHNCLASS) then the address in avp 5469585Seric ** matched is saved away in the match vector (pointed to by 'mvp'). 5474476Seric ** 5484476Seric ** When a match between avp & pvp does not match, we try to 5499585Seric ** back out. If we back up over MATCHONE, MATCHCLASS, or MATCHNCLASS 5504476Seric ** we must also back out the match in mvp. If we reach a 5518058Seric ** MATCHANY or MATCHZANY we just extend the match and start 5528058Seric ** over again. 5534476Seric ** 5544476Seric ** When we finally match, we rewrite the address vector 5554476Seric ** and try over again. 5564476Seric ** 5573149Seric ** Parameters: 5583149Seric ** pvp -- pointer to token vector. 5593149Seric ** 5603149Seric ** Returns: 5613149Seric ** none. 5623149Seric ** 5633149Seric ** Side Effects: 5643149Seric ** pvp is modified. 5653149Seric */ 5662091Seric 5673149Seric struct match 5683149Seric { 5694468Seric char **first; /* first token matched */ 5704468Seric char **last; /* last token matched */ 5713149Seric }; 5723149Seric 5734468Seric # define MAXMATCH 9 /* max params per rewrite */ 5743149Seric 5753149Seric 5764070Seric rewrite(pvp, ruleset) 5773149Seric char **pvp; 5784070Seric int ruleset; 5793149Seric { 5803149Seric register char *ap; /* address pointer */ 5813149Seric register char *rp; /* rewrite pointer */ 5823149Seric register char **avp; /* address vector pointer */ 5833149Seric register char **rvp; /* rewrite vector pointer */ 5848058Seric register struct match *mlp; /* cur ptr into mlist */ 5858058Seric register struct rewrite *rwr; /* pointer to current rewrite rule */ 58656678Seric struct match mlist[MAXMATCH]; /* stores match on LHS */ 5873149Seric char *npvp[MAXATOM+1]; /* temporary space for rebuild */ 5883149Seric 5899279Seric if (OpMode == MD_TEST || tTd(21, 2)) 5903149Seric { 5918959Seric printf("rewrite: ruleset %2d input:", ruleset); 59256678Seric printav(pvp); 5933149Seric } 59456678Seric if (ruleset < 0 || ruleset >= MAXRWSETS) 59556326Seric { 59656678Seric syserr("rewrite: illegal ruleset number %d", ruleset); 59756326Seric return; 59856326Seric } 59956678Seric if (pvp == NULL) 60056678Seric return; 60156326Seric 6023149Seric /* 60356678Seric ** Run through the list of rewrite rules, applying 60456678Seric ** any that match. 6053149Seric */ 6063149Seric 6074070Seric for (rwr = RewriteRules[ruleset]; rwr != NULL; ) 6083149Seric { 60956678Seric int loopcount = 0; 61056678Seric 6117675Seric if (tTd(21, 12)) 612297Seric { 6138069Seric printf("-----trying rule:"); 61456678Seric printav(rwr->r_lhs); 6153149Seric } 6163149Seric 6173149Seric /* try to match on this rule */ 6184468Seric mlp = mlist; 6198058Seric rvp = rwr->r_lhs; 6208058Seric avp = pvp; 6218058Seric while ((ap = *avp) != NULL || *rvp != NULL) 6223149Seric { 62356678Seric if (++loopcount > 100) 62452637Seric { 62556678Seric syserr("Infinite loop in ruleset %d", ruleset); 62656678Seric printf("workspace: "); 62756678Seric printav(pvp); 62852637Seric break; 62952637Seric } 6303149Seric rp = *rvp; 6318058Seric if (tTd(21, 35)) 6328058Seric { 63357532Seric printf("rp="); 63457531Seric xputs(rp); 63557532Seric printf(", ap="); 6368058Seric xputs(ap); 6378069Seric printf("\n"); 6388058Seric } 63956678Seric if (rp == NULL) 64056326Seric { 6413149Seric /* end-of-pattern before end-of-address */ 6428058Seric goto backup; 64356678Seric } 64456678Seric if (ap == NULL && *rp != MATCHZANY) 64556326Seric { 64656678Seric /* end-of-input */ 64756678Seric break; 648297Seric } 64956326Seric 65056678Seric switch (*rp) 6518058Seric { 65256678Seric register STAB *s; 65356326Seric 65456678Seric case MATCHCLASS: 65556678Seric case MATCHNCLASS: 65656678Seric /* match any token in (not in) a class */ 65756678Seric s = stab(ap, ST_CLASS, ST_FIND); 65856678Seric if (s == NULL || !bitnset(rp[1], s->s_class)) 65956326Seric { 66056678Seric if (*rp == MATCHCLASS) 6619585Seric goto backup; 66256326Seric } 66356678Seric else if (*rp == MATCHNCLASS) 66456678Seric goto backup; 6654060Seric 66656678Seric /* explicit fall-through */ 66756678Seric 66856678Seric case MATCHONE: 66956678Seric case MATCHANY: 67056678Seric /* match exactly one token */ 67156678Seric mlp->first = avp; 67256678Seric mlp->last = avp++; 6738058Seric mlp++; 67456678Seric break; 6758058Seric 67656678Seric case MATCHZANY: 67756678Seric /* match zero or more tokens */ 67856678Seric mlp->first = avp; 67956678Seric mlp->last = avp - 1; 68056678Seric mlp++; 68156678Seric break; 68256326Seric 68356678Seric default: 68456678Seric /* must have exact match */ 68556678Seric if (strcasecmp(rp, ap)) 6868058Seric goto backup; 6874468Seric avp++; 68856678Seric break; 6893149Seric } 6903149Seric 69156678Seric /* successful match on this token */ 6923149Seric rvp++; 6933149Seric continue; 6943149Seric 69556678Seric backup: 69656678Seric /* match failed -- back up */ 69756678Seric while (--rvp >= rwr->r_lhs) 6983149Seric { 69956678Seric rp = *rvp; 70056678Seric if (*rp == MATCHANY || *rp == MATCHZANY) 7014468Seric { 70256678Seric /* extend binding and continue */ 70356678Seric avp = ++mlp[-1].last; 70456678Seric avp++; 70556678Seric rvp++; 70656678Seric break; 7074468Seric } 70856678Seric avp--; 70956678Seric if (*rp == MATCHONE || *rp == MATCHCLASS || 71056678Seric *rp == MATCHNCLASS) 71156678Seric { 71256678Seric /* back out binding */ 71356678Seric mlp--; 71456678Seric } 7153149Seric } 7163149Seric 71756678Seric if (rvp < rwr->r_lhs) 71856678Seric { 71956678Seric /* total failure to match */ 72056326Seric break; 7213149Seric } 722297Seric } 7233149Seric 7243149Seric /* 72556678Seric ** See if we successfully matched 7263149Seric */ 7273149Seric 72856678Seric if (rvp < rwr->r_lhs || *rvp != NULL) 7293149Seric { 7309374Seric if (tTd(21, 10)) 7319374Seric printf("----- rule fails\n"); 7329374Seric rwr = rwr->r_next; 7339374Seric continue; 7349374Seric } 7353149Seric 7369374Seric rvp = rwr->r_rhs; 7379374Seric if (tTd(21, 12)) 7389374Seric { 7399374Seric printf("-----rule matches:"); 74056678Seric printav(rvp); 7419374Seric } 7429374Seric 7439374Seric rp = *rvp; 7449374Seric if (*rp == CANONUSER) 7459374Seric { 7469374Seric rvp++; 7479374Seric rwr = rwr->r_next; 7489374Seric } 7499374Seric else if (*rp == CANONHOST) 7509374Seric { 7519374Seric rvp++; 7529374Seric rwr = NULL; 7539374Seric } 7549374Seric else if (*rp == CANONNET) 7559374Seric rwr = NULL; 7569374Seric 7579374Seric /* substitute */ 7589374Seric for (avp = npvp; *rvp != NULL; rvp++) 7599374Seric { 7609374Seric register struct match *m; 7619374Seric register char **pp; 7629374Seric 7638058Seric rp = *rvp; 76456678Seric if (*rp == MATCHREPL) 7658058Seric { 76616914Seric /* substitute from LHS */ 76716914Seric m = &mlist[rp[1] - '1']; 76856678Seric if (m < mlist || m >= mlp) 7699374Seric { 77056678Seric syserr("rewrite: ruleset %d: replacement $%c out of bounds", 77156326Seric ruleset, rp[1]); 7729374Seric return; 7739374Seric } 77416914Seric if (tTd(21, 15)) 77516914Seric { 77616914Seric printf("$%c:", rp[1]); 77716914Seric pp = m->first; 77856678Seric while (pp <= m->last) 77916914Seric { 78016914Seric printf(" %x=\"", *pp); 78116914Seric (void) fflush(stdout); 78216914Seric printf("%s\"", *pp++); 78316914Seric } 78416914Seric printf("\n"); 78516914Seric } 7869374Seric pp = m->first; 78756678Seric while (pp <= m->last) 7883149Seric { 78916914Seric if (avp >= &npvp[MAXATOM]) 79056678Seric { 79156678Seric syserr("rewrite: expansion too long"); 79256678Seric return; 79356678Seric } 79416914Seric *avp++ = *pp++; 7953149Seric } 7963149Seric } 79716914Seric else 7988226Seric { 79916914Seric /* vanilla replacement */ 8009374Seric if (avp >= &npvp[MAXATOM]) 80116889Seric { 80256678Seric toolong: 80316889Seric syserr("rewrite: expansion too long"); 80416889Seric return; 80516889Seric } 80656678Seric *avp++ = rp; 8078226Seric } 8089374Seric } 8099374Seric *avp++ = NULL; 81016914Seric 81116914Seric /* 81256678Seric ** Check for any hostname/keyword lookups. 81316914Seric */ 81416914Seric 81516914Seric for (rvp = npvp; *rvp != NULL; rvp++) 81616914Seric { 81756678Seric char **hbrvp; 81816914Seric char **xpvp; 81916914Seric int trsize; 82017473Seric char *olddelimchar; 82156678Seric char *replac; 82256678Seric int endtoken; 82356678Seric STAB *map; 82456678Seric char *mapname; 82556678Seric char **key_rvp; 82656678Seric char **arg_rvp; 82756678Seric char **default_rvp; 82856678Seric char buf[MAXNAME + 1]; 82916914Seric char *pvpb1[MAXATOM + 1]; 83056823Seric char *argvect[10]; 83117174Seric char pvpbuf[PSBUFSIZE]; 83256678Seric extern char *DelimChar; 83316914Seric 83456678Seric if (**rvp != HOSTBEGIN && **rvp != LOOKUPBEGIN) 83516914Seric continue; 83616914Seric 83716914Seric /* 83856678Seric ** Got a hostname/keyword lookup. 83916914Seric ** 84016914Seric ** This could be optimized fairly easily. 84116914Seric */ 84216914Seric 84316914Seric hbrvp = rvp; 84456678Seric if (**rvp == HOSTBEGIN) 84556327Seric { 84656678Seric endtoken = HOSTEND; 84756678Seric mapname = "host"; 84856327Seric } 84956326Seric else 85056327Seric { 85156678Seric endtoken = LOOKUPEND; 85256678Seric mapname = *++rvp; 85356327Seric } 85456678Seric map = stab(mapname, ST_MAP, ST_FIND); 85556678Seric if (map == NULL) 85656678Seric syserr("rewrite: map %s not found", mapname); 85756678Seric 85856678Seric /* extract the match part */ 85956678Seric key_rvp = ++rvp; 86056823Seric default_rvp = NULL; 86156823Seric arg_rvp = argvect; 86256823Seric xpvp = NULL; 86356823Seric replac = pvpbuf; 86456678Seric while (*rvp != NULL && **rvp != endtoken) 86553654Seric { 86656823Seric int nodetype = **rvp; 86756823Seric 86856823Seric if (nodetype != CANONHOST && nodetype != CANONUSER) 86956678Seric { 87056823Seric rvp++; 87156823Seric continue; 87256823Seric } 87356823Seric 87456823Seric *rvp++ = NULL; 87556823Seric 87656823Seric if (xpvp != NULL) 87756823Seric { 87856823Seric cataddr(xpvp, replac, 87956823Seric &pvpbuf[sizeof pvpbuf] - replac); 88056823Seric *++arg_rvp = replac; 88156823Seric replac += strlen(replac) + 1; 88256823Seric xpvp = NULL; 88356823Seric } 88456823Seric switch (nodetype) 88556823Seric { 88656678Seric case CANONHOST: 88756823Seric xpvp = rvp; 88856678Seric break; 88956678Seric 89056678Seric case CANONUSER: 89156678Seric default_rvp = rvp; 89256678Seric break; 89356678Seric } 89453654Seric } 89516914Seric if (*rvp != NULL) 89616914Seric *rvp++ = NULL; 89756823Seric if (xpvp != NULL) 89856823Seric { 89956823Seric cataddr(xpvp, replac, 90056823Seric &pvpbuf[sizeof pvpbuf] - replac); 90156823Seric *++arg_rvp = replac; 90256823Seric } 90356823Seric *++arg_rvp = NULL; 90416914Seric 90516914Seric /* save the remainder of the input string */ 90616914Seric trsize = (int) (avp - rvp + 1) * sizeof *rvp; 90716914Seric bcopy((char *) rvp, (char *) pvpb1, trsize); 90816914Seric 90956678Seric /* look it up */ 91056678Seric cataddr(key_rvp, buf, sizeof buf); 91156823Seric argvect[0] = buf; 91256678Seric if (map != NULL && bitset(MF_VALID, map->s_map.map_flags)) 91356836Seric { 91456836Seric int bsize = sizeof buf - 1; 91556836Seric 91656836Seric if (map->s_map.map_app != NULL) 91756836Seric bsize -= strlen(map->s_map.map_app); 91856836Seric replac = (*map->s_map.map_class->map_lookup)(&map->s_map, 91956823Seric buf, sizeof buf - 1, argvect); 92056836Seric if (replac != NULL && map->s_map.map_app != NULL) 92156836Seric strcat(replac, map->s_map.map_app); 92256836Seric } 92353654Seric else 92456678Seric replac = NULL; 92556678Seric 92656678Seric /* if no replacement, use default */ 92756823Seric if (replac == NULL && default_rvp != NULL) 92856823Seric { 92956823Seric char buf2[sizeof buf]; 93056823Seric 93156823Seric /* rewrite the default with % translations */ 93256823Seric cataddr(default_rvp, buf2, sizeof buf2); 93356823Seric map_rewrite(buf2, sizeof buf2, buf, sizeof buf, 93456823Seric argvect); 93556823Seric replac = buf; 93656823Seric } 93756823Seric 93856678Seric if (replac == NULL) 93951317Seric { 94056823Seric xpvp = key_rvp; 94153654Seric } 94256678Seric else 94353654Seric { 94456678Seric /* scan the new replacement */ 94556678Seric olddelimchar = DelimChar; 94656678Seric xpvp = prescan(replac, '\0', pvpbuf); 94756678Seric DelimChar = olddelimchar; 94853654Seric if (xpvp == NULL) 94951317Seric { 95056678Seric syserr("rewrite: cannot prescan map value: %s", replac); 95156678Seric return; 95256678Seric } 95351317Seric } 95451317Seric 95516914Seric /* append it to the token list */ 95656678Seric for (avp = hbrvp; *xpvp != NULL; xpvp++) 95756678Seric { 95817174Seric *avp++ = newstr(*xpvp); 95916920Seric if (avp >= &npvp[MAXATOM]) 96016914Seric goto toolong; 96117174Seric } 96216914Seric 96316914Seric /* restore the old trailing information */ 96456678Seric for (xpvp = pvpb1; (*avp++ = *xpvp++) != NULL; ) 96516920Seric if (avp >= &npvp[MAXATOM]) 96616914Seric goto toolong; 96717174Seric 96856678Seric break; 96916914Seric } 97016914Seric 97116914Seric /* 97216914Seric ** Check for subroutine calls. 97316914Seric */ 97416914Seric 97556678Seric if (*npvp != NULL && **npvp == CALLSUBR) 97656678Seric { 97756678Seric bcopy((char *) &npvp[2], (char *) pvp, 97856678Seric (int) (avp - npvp - 2) * sizeof *avp); 97956678Seric if (tTd(21, 3)) 98056678Seric printf("-----callsubr %s\n", npvp[1]); 98156678Seric rewrite(pvp, atoi(npvp[1])); 98256678Seric } 98356678Seric else 98456678Seric { 98517348Seric bcopy((char *) npvp, (char *) pvp, 98616900Seric (int) (avp - npvp) * sizeof *avp); 98756678Seric } 9889374Seric if (tTd(21, 4)) 9899374Seric { 9909374Seric printf("rewritten as:"); 99156678Seric printav(pvp); 9929374Seric } 993297Seric } 9948069Seric 9959279Seric if (OpMode == MD_TEST || tTd(21, 2)) 9968069Seric { 9978959Seric printf("rewrite: ruleset %2d returns:", ruleset); 99856678Seric printav(pvp); 9998069Seric } 10003149Seric } 10013149Seric /* 10023149Seric ** BUILDADDR -- build address from token vector. 10033149Seric ** 10043149Seric ** Parameters: 10053149Seric ** tv -- token vector. 10063149Seric ** a -- pointer to address descriptor to fill. 10073149Seric ** If NULL, one will be allocated. 10083149Seric ** 10093149Seric ** Returns: 10104279Seric ** NULL if there was an error. 10114279Seric ** 'a' otherwise. 10123149Seric ** 10133149Seric ** Side Effects: 10143149Seric ** fills in 'a' 10153149Seric */ 10163149Seric 101757249Seric struct errcodes 101857249Seric { 101957249Seric char *ec_name; /* name of error code */ 102057249Seric int ec_code; /* numeric code */ 102157249Seric } ErrorCodes[] = 102257249Seric { 102357249Seric "usage", EX_USAGE, 102457249Seric "nouser", EX_NOUSER, 102557249Seric "nohost", EX_NOHOST, 102657249Seric "unavailable", EX_UNAVAILABLE, 102757249Seric "software", EX_SOFTWARE, 102857249Seric "tempfail", EX_TEMPFAIL, 102957249Seric "protocol", EX_PROTOCOL, 103057249Seric #ifdef EX_CONFIG 103157249Seric "config", EX_CONFIG, 103257249Seric #endif 103357249Seric NULL, EX_UNAVAILABLE, 103457249Seric }; 103557249Seric 103656678Seric ADDRESS * 10373149Seric buildaddr(tv, a) 10383149Seric register char **tv; 10393149Seric register ADDRESS *a; 10403149Seric { 10413149Seric struct mailer **mp; 10423149Seric register struct mailer *m; 104357402Seric static char buf[MAXNAME]; 10443149Seric 10453149Seric if (a == NULL) 10463149Seric a = (ADDRESS *) xalloc(sizeof *a); 104716889Seric bzero((char *) a, sizeof *a); 10483149Seric 10493149Seric /* figure out what net/mailer to use */ 105056678Seric if (**tv != CANONNET) 10514279Seric { 10523149Seric syserr("buildaddr: no net"); 10534279Seric return (NULL); 10544279Seric } 10553149Seric tv++; 105633725Sbostic if (!strcasecmp(*tv, "error")) 10574279Seric { 105810183Seric if (**++tv == CANONHOST) 105910183Seric { 106057249Seric register struct errcodes *ep; 106157249Seric 106257249Seric if (isdigit(**++tv)) 106357249Seric { 106457249Seric setstat(atoi(*tv)); 106557249Seric } 106657249Seric else 106757249Seric { 106857249Seric for (ep = ErrorCodes; ep->ec_name != NULL; ep++) 106957249Seric if (strcasecmp(ep->ec_name, *tv) == 0) 107057249Seric break; 107157249Seric setstat(ep->ec_code); 107257249Seric } 107310183Seric tv++; 107410183Seric } 107510183Seric if (**tv != CANONUSER) 10764279Seric syserr("buildaddr: error: no user"); 107756678Seric buf[0] = '\0'; 10784279Seric while (*++tv != NULL) 10794279Seric { 10804279Seric if (buf[0] != '\0') 10817005Seric (void) strcat(buf, " "); 10827005Seric (void) strcat(buf, *tv); 10834279Seric } 10844279Seric usrerr(buf); 10854279Seric return (NULL); 10864279Seric } 108757402Seric 10884598Seric for (mp = Mailer; (m = *mp++) != NULL; ) 10893149Seric { 109033725Sbostic if (!strcasecmp(m->m_name, *tv)) 10913149Seric break; 10923149Seric } 10933149Seric if (m == NULL) 10944279Seric { 109524944Seric syserr("buildaddr: unknown mailer %s", *tv); 10964279Seric return (NULL); 10974279Seric } 10984598Seric a->q_mailer = m; 10993149Seric 11003149Seric /* figure out what host (if any) */ 110156678Seric tv++; 110257249Seric if (!bitnset(M_LOCAL, m->m_flags)) 11033149Seric { 110457249Seric if (**tv++ != CANONHOST) 11054279Seric { 11063149Seric syserr("buildaddr: no host"); 11074279Seric return (NULL); 11084279Seric } 11095704Seric buf[0] = '\0'; 111057249Seric while (*tv != NULL && **tv != CANONUSER) 111157249Seric (void) strcat(buf, *tv++); 11125704Seric a->q_host = newstr(buf); 11133149Seric } 111457249Seric else 111557249Seric a->q_host = NULL; 11163149Seric 11173149Seric /* figure out the user */ 111836615Sbostic if (*tv == NULL || **tv != CANONUSER) 11194279Seric { 11203149Seric syserr("buildaddr: no user"); 11214279Seric return (NULL); 11224279Seric } 112319040Seric 112456678Seric if (m == LocalMailer && tv[1] != NULL && strcmp(tv[1], "@") == 0) 112556678Seric { 112656678Seric tv++; 112756678Seric a->q_flags |= QNOTREMOTE; 112856678Seric } 112957402Seric tv++; 113051317Seric 113157402Seric /* do special mapping for local mailer */ 113257402Seric if (m == LocalMailer && *tv != NULL) 113357402Seric { 113457454Seric register char *p = *tv; 113557454Seric 113657454Seric if (*p == '"') 113757454Seric p++; 113857454Seric if (*p == '|') 113957402Seric a->q_mailer = m = ProgMailer; 114057454Seric else if (*p == '/') 114157402Seric a->q_mailer = m = FileMailer; 114257454Seric else if (*p == ':') 114357402Seric { 114457402Seric /* may be :include: */ 114557402Seric cataddr(tv, buf, sizeof buf); 114657454Seric p = buf; 114757454Seric if (*p == '"') 114857454Seric p++; 114957454Seric p[9] = '\0'; 115057454Seric if (strcasecmp(p, ":include:") == 0) 115157402Seric a->q_mailer = m = InclMailer; 115257402Seric } 115357402Seric } 115457402Seric 115519040Seric /* rewrite according recipient mailer rewriting rules */ 115657402Seric rewrite(tv, 2); 115756327Seric if (m->m_r_rwset > 0) 115856327Seric rewrite(tv, m->m_r_rwset); 115919040Seric rewrite(tv, 4); 116019040Seric 116119040Seric /* save the result for the command line/RCPT argument */ 116211278Seric cataddr(tv, buf, sizeof buf); 11633149Seric a->q_user = buf; 11643149Seric 11653149Seric return (a); 11663149Seric } 11673188Seric /* 11684228Seric ** CATADDR -- concatenate pieces of addresses (putting in <LWSP> subs) 11694228Seric ** 11704228Seric ** Parameters: 11714228Seric ** pvp -- parameter vector to rebuild. 11724228Seric ** buf -- buffer to build the string into. 11734228Seric ** sz -- size of buf. 11744228Seric ** 11754228Seric ** Returns: 11764228Seric ** none. 11774228Seric ** 11784228Seric ** Side Effects: 11794228Seric ** Destroys buf. 11804228Seric */ 11814228Seric 11824228Seric cataddr(pvp, buf, sz) 11834228Seric char **pvp; 11844228Seric char *buf; 11854228Seric register int sz; 11864228Seric { 11874228Seric bool oatomtok = FALSE; 118856678Seric bool natomtok = FALSE; 11894228Seric register int i; 11904228Seric register char *p; 11914228Seric 11928423Seric if (pvp == NULL) 11938423Seric { 119423109Seric (void) strcpy(buf, ""); 11958423Seric return; 11968423Seric } 11974228Seric p = buf; 119811156Seric sz -= 2; 11994228Seric while (*pvp != NULL && (i = strlen(*pvp)) < sz) 12004228Seric { 12018078Seric natomtok = (toktype(**pvp) == ATM); 12024228Seric if (oatomtok && natomtok) 12039042Seric *p++ = SpaceSub; 12044228Seric (void) strcpy(p, *pvp); 12054228Seric oatomtok = natomtok; 12064228Seric p += i; 120711156Seric sz -= i + 1; 12084228Seric pvp++; 12094228Seric } 12104228Seric *p = '\0'; 12114228Seric } 12124228Seric /* 12133188Seric ** SAMEADDR -- Determine if two addresses are the same 12143188Seric ** 12153188Seric ** This is not just a straight comparison -- if the mailer doesn't 12163188Seric ** care about the host we just ignore it, etc. 12173188Seric ** 12183188Seric ** Parameters: 12193188Seric ** a, b -- pointers to the internal forms to compare. 12203188Seric ** 12213188Seric ** Returns: 12223188Seric ** TRUE -- they represent the same mailbox. 12233188Seric ** FALSE -- they don't. 12243188Seric ** 12253188Seric ** Side Effects: 12263188Seric ** none. 12273188Seric */ 12283188Seric 12293188Seric bool 12309374Seric sameaddr(a, b) 12313188Seric register ADDRESS *a; 12323188Seric register ADDRESS *b; 12333188Seric { 12343188Seric /* if they don't have the same mailer, forget it */ 12353188Seric if (a->q_mailer != b->q_mailer) 12363188Seric return (FALSE); 12373188Seric 12383188Seric /* if the user isn't the same, we can drop out */ 123956678Seric if (strcmp(a->q_user, b->q_user) != 0) 12403188Seric return (FALSE); 12413188Seric 12423188Seric /* if the mailer ignores hosts, we have succeeded! */ 124310690Seric if (bitnset(M_LOCAL, a->q_mailer->m_flags)) 12443188Seric return (TRUE); 12453188Seric 12463188Seric /* otherwise compare hosts (but be careful for NULL ptrs) */ 12473188Seric if (a->q_host == NULL || b->q_host == NULL) 12483188Seric return (FALSE); 124956678Seric if (strcmp(a->q_host, b->q_host) != 0) 12503188Seric return (FALSE); 12513188Seric 12523188Seric return (TRUE); 12533188Seric } 12543234Seric /* 12553234Seric ** PRINTADDR -- print address (for debugging) 12563234Seric ** 12573234Seric ** Parameters: 12583234Seric ** a -- the address to print 12593234Seric ** follow -- follow the q_next chain. 12603234Seric ** 12613234Seric ** Returns: 12623234Seric ** none. 12633234Seric ** 12643234Seric ** Side Effects: 12653234Seric ** none. 12663234Seric */ 12673234Seric 12683234Seric printaddr(a, follow) 12693234Seric register ADDRESS *a; 12703234Seric bool follow; 12713234Seric { 12725001Seric bool first = TRUE; 1273*57731Seric register MAILER *m; 1274*57731Seric MAILER pseudomailer; 12755001Seric 12763234Seric while (a != NULL) 12773234Seric { 12785001Seric first = FALSE; 12794443Seric printf("%x=", a); 12804085Seric (void) fflush(stdout); 1281*57731Seric 1282*57731Seric /* find the mailer -- carefully */ 1283*57731Seric m = a->q_mailer; 1284*57731Seric if (m == NULL) 1285*57731Seric { 1286*57731Seric m = &pseudomailer; 1287*57731Seric m->m_mno = -1; 1288*57731Seric m->m_name = "NULL"; 1289*57731Seric } 1290*57731Seric 129140973Sbostic printf("%s: mailer %d (%s), host `%s', user `%s', ruser `%s'\n", 1292*57731Seric a->q_paddr, m->m_mno, m->m_name, 129340973Sbostic a->q_host, a->q_user, a->q_ruser? a->q_ruser: "<null>"); 12948181Seric printf("\tnext=%x, flags=%o, alias %x\n", a->q_next, a->q_flags, 12958181Seric a->q_alias); 12968181Seric printf("\thome=\"%s\", fullname=\"%s\"\n", a->q_home, 12978181Seric a->q_fullname); 12984996Seric 12993234Seric if (!follow) 13003234Seric return; 13014996Seric a = a->q_next; 13023234Seric } 13035001Seric if (first) 13044443Seric printf("[NULL]\n"); 13053234Seric } 13064317Seric 13077682Seric /* 13087682Seric ** REMOTENAME -- return the name relative to the current mailer 13097682Seric ** 13107682Seric ** Parameters: 13117682Seric ** name -- the name to translate. 13128069Seric ** m -- the mailer that we want to do rewriting relative 13138069Seric ** to. 13148069Seric ** senderaddress -- if set, uses the sender rewriting rules 13158069Seric ** rather than the recipient rewriting rules. 131610310Seric ** canonical -- if set, strip out any comment information, 131710310Seric ** etc. 13187682Seric ** 13197682Seric ** Returns: 13207682Seric ** the text string representing this address relative to 13217682Seric ** the receiving mailer. 13227682Seric ** 13237682Seric ** Side Effects: 13247682Seric ** none. 13257682Seric ** 13267682Seric ** Warnings: 13277682Seric ** The text string returned is tucked away locally; 13287682Seric ** copy it if you intend to save it. 13297682Seric */ 13307682Seric 13317682Seric char * 133256678Seric remotename(name, m, senderaddress, canonical, e) 13337682Seric char *name; 133456678Seric struct mailer *m; 13358069Seric bool senderaddress; 133610310Seric bool canonical; 133756678Seric register ENVELOPE *e; 13387682Seric { 13398069Seric register char **pvp; 13408069Seric char *fancy; 134156678Seric extern char *macvalue(); 134256678Seric char *oldg = macvalue('g', e); 13437682Seric static char buf[MAXNAME]; 13447682Seric char lbuf[MAXNAME]; 134516914Seric char pvpbuf[PSBUFSIZE]; 134656678Seric extern char **prescan(); 134756678Seric extern char *crackaddr(); 13487682Seric 13497755Seric if (tTd(12, 1)) 13507755Seric printf("remotename(%s)\n", name); 13517755Seric 135210177Seric /* don't do anything if we are tagging it as special */ 135356327Seric if ((senderaddress ? m->m_s_rwset : m->m_r_rwset) < 0) 135410177Seric return (name); 135510177Seric 13567682Seric /* 13578181Seric ** Do a heuristic crack of this name to extract any comment info. 13588181Seric ** This will leave the name as a comment and a $g macro. 13597889Seric */ 13607889Seric 136110310Seric if (canonical) 136216155Seric fancy = "\001g"; 136310310Seric else 136410310Seric fancy = crackaddr(name); 13657889Seric 13668181Seric /* 13678181Seric ** Turn the name into canonical form. 13688181Seric ** Normally this will be RFC 822 style, i.e., "user@domain". 13698181Seric ** If this only resolves to "user", and the "C" flag is 13708181Seric ** specified in the sending mailer, then the sender's 13718181Seric ** domain will be appended. 13728181Seric */ 13738181Seric 137416914Seric pvp = prescan(name, '\0', pvpbuf); 13757889Seric if (pvp == NULL) 13767889Seric return (name); 13778181Seric rewrite(pvp, 3); 137856678Seric if (e->e_fromdomain != NULL) 13798181Seric { 13808181Seric /* append from domain to this address */ 13818181Seric register char **pxp = pvp; 13828181Seric 13839594Seric /* see if there is an "@domain" in the current name */ 13848181Seric while (*pxp != NULL && strcmp(*pxp, "@") != 0) 13858181Seric pxp++; 13868181Seric if (*pxp == NULL) 13878181Seric { 13889594Seric /* no.... append the "@domain" from the sender */ 138956678Seric register char **qxq = e->e_fromdomain; 13908181Seric 13919594Seric while ((*pxp++ = *qxq++) != NULL) 13929594Seric continue; 139311726Seric rewrite(pvp, 3); 13948181Seric } 13958181Seric } 13968181Seric 13978181Seric /* 13988959Seric ** Do more specific rewriting. 139956678Seric ** Rewrite using ruleset 1 or 2 depending on whether this is 140056678Seric ** a sender address or not. 14018181Seric ** Then run it through any receiving-mailer-specific rulesets. 14028181Seric */ 14038181Seric 14048069Seric if (senderaddress) 14057755Seric { 140656327Seric rewrite(pvp, 1); 140756327Seric if (m->m_s_rwset > 0) 140856327Seric rewrite(pvp, m->m_s_rwset); 14098069Seric } 14108069Seric else 14118069Seric { 141256327Seric rewrite(pvp, 2); 141356327Seric if (m->m_r_rwset > 0) 141456327Seric rewrite(pvp, m->m_r_rwset); 14157682Seric } 14167682Seric 14178181Seric /* 14188959Seric ** Do any final sanitation the address may require. 14198959Seric ** This will normally be used to turn internal forms 14208959Seric ** (e.g., user@host.LOCAL) into external form. This 14218959Seric ** may be used as a default to the above rules. 14228959Seric */ 14238959Seric 14248959Seric rewrite(pvp, 4); 14258959Seric 14268959Seric /* 14278181Seric ** Now restore the comment information we had at the beginning. 14288181Seric */ 14298181Seric 14307682Seric cataddr(pvp, lbuf, sizeof lbuf); 143156678Seric define('g', lbuf, e); 143256678Seric expand(fancy, buf, &buf[sizeof buf - 1], e); 143356678Seric define('g', oldg, e); 14347682Seric 14357682Seric if (tTd(12, 1)) 14367755Seric printf("remotename => `%s'\n", buf); 14377682Seric return (buf); 14387682Seric } 143951317Seric /* 144056678Seric ** MAPLOCALUSER -- run local username through ruleset 5 for final redirection 144151317Seric ** 144251317Seric ** Parameters: 144356678Seric ** a -- the address to map (but just the user name part). 144456678Seric ** sendq -- the sendq in which to install any replacement 144556678Seric ** addresses. 144651317Seric ** 144751317Seric ** Returns: 144851317Seric ** none. 144951317Seric */ 145051317Seric 145156678Seric maplocaluser(a, sendq, e) 145256678Seric register ADDRESS *a; 145356678Seric ADDRESS **sendq; 145456678Seric ENVELOPE *e; 145551317Seric { 145656678Seric register char **pvp; 145756678Seric register ADDRESS *a1 = NULL; 145856678Seric char pvpbuf[PSBUFSIZE]; 145951317Seric 146056678Seric if (tTd(29, 1)) 146156678Seric { 146256678Seric printf("maplocaluser: "); 146356678Seric printaddr(a, FALSE); 146456678Seric } 146556678Seric pvp = prescan(a->q_user, '\0', pvpbuf); 146656678Seric if (pvp == NULL) 146756678Seric return; 146851317Seric 146956678Seric rewrite(pvp, 5); 147056678Seric if (pvp[0] == NULL || pvp[0][0] != CANONNET) 147156678Seric return; 147251317Seric 147356678Seric /* if non-null, mailer destination specified -- has it changed? */ 147456678Seric a1 = buildaddr(pvp, NULL); 147556678Seric if (a1 == NULL || sameaddr(a, a1)) 147656678Seric return; 147751317Seric 147856678Seric /* mark old address as dead; insert new address */ 147956678Seric a->q_flags |= QDONTSEND; 1480*57731Seric if (tTd(29, 5)) 1481*57731Seric { 1482*57731Seric printf("maplocaluser: QDONTSEND "); 1483*57731Seric printaddr(a, FALSE); 1484*57731Seric } 148556678Seric a1->q_alias = a; 148656678Seric allocaddr(a1, 1, NULL); 148756678Seric (void) recipient(a1, sendq, e); 148851317Seric } 1489