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*58036Seric static char sccsid[] = "@(#)parseaddr.c 6.14 (Berkeley) 02/18/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; 104358008Seric char *bp; 104458008Seric int spaceleft; 104557402Seric static char buf[MAXNAME]; 10463149Seric 10473149Seric if (a == NULL) 10483149Seric a = (ADDRESS *) xalloc(sizeof *a); 104916889Seric bzero((char *) a, sizeof *a); 10503149Seric 10513149Seric /* figure out what net/mailer to use */ 105256678Seric if (**tv != CANONNET) 10534279Seric { 10543149Seric syserr("buildaddr: no net"); 10554279Seric return (NULL); 10564279Seric } 10573149Seric tv++; 105833725Sbostic if (!strcasecmp(*tv, "error")) 10594279Seric { 106010183Seric if (**++tv == CANONHOST) 106110183Seric { 106257249Seric register struct errcodes *ep; 106357249Seric 106457249Seric if (isdigit(**++tv)) 106557249Seric { 106657249Seric setstat(atoi(*tv)); 106757249Seric } 106857249Seric else 106957249Seric { 107057249Seric for (ep = ErrorCodes; ep->ec_name != NULL; ep++) 107157249Seric if (strcasecmp(ep->ec_name, *tv) == 0) 107257249Seric break; 107357249Seric setstat(ep->ec_code); 107457249Seric } 107510183Seric tv++; 107610183Seric } 107710183Seric if (**tv != CANONUSER) 10784279Seric syserr("buildaddr: error: no user"); 107958008Seric bp = buf; 108058008Seric spaceleft = sizeof buf - 2; 10814279Seric while (*++tv != NULL) 10824279Seric { 108358008Seric int i = strlen(*tv); 108458008Seric 108558008Seric if (i > spaceleft) 108658008Seric { 108758008Seric /* out of space for this address */ 108858008Seric if (spaceleft >= 0) 108958008Seric syserr("buildaddr: error message too long (%.40s...)", 109058008Seric buf); 109158008Seric i = spaceleft; 109258008Seric spaceleft = 0; 109358008Seric } 109458008Seric if (i <= 0) 109558008Seric continue; 109658008Seric if (bp != buf) 109758008Seric { 109858008Seric *bp++ = ' '; 109958008Seric spaceleft--; 110058008Seric } 110158008Seric bcopy(*tv, bp, i); 110258008Seric bp += i; 110358008Seric spaceleft -= i; 11044279Seric } 110558010Seric *bp = '\0'; 11064279Seric usrerr(buf); 11074279Seric return (NULL); 11084279Seric } 110957402Seric 11104598Seric for (mp = Mailer; (m = *mp++) != NULL; ) 11113149Seric { 111233725Sbostic if (!strcasecmp(m->m_name, *tv)) 11133149Seric break; 11143149Seric } 11153149Seric if (m == NULL) 11164279Seric { 111724944Seric syserr("buildaddr: unknown mailer %s", *tv); 11184279Seric return (NULL); 11194279Seric } 11204598Seric a->q_mailer = m; 11213149Seric 11223149Seric /* figure out what host (if any) */ 112356678Seric tv++; 112457249Seric if (!bitnset(M_LOCAL, m->m_flags)) 11253149Seric { 112658008Seric if (**tv != CANONHOST) 11274279Seric { 11283149Seric syserr("buildaddr: no host"); 11294279Seric return (NULL); 11304279Seric } 113158008Seric bp = buf; 113258008Seric spaceleft = sizeof buf - 1; 113358008Seric while (*++tv != NULL && **tv != CANONUSER) 113458008Seric { 113558008Seric int i = strlen(*tv); 113658008Seric 113758008Seric if (i > spaceleft) 113858008Seric { 113958008Seric /* out of space for this address */ 114058008Seric if (spaceleft >= 0) 114158008Seric syserr("buildaddr: host too long (%.40s...)", 114258008Seric buf); 114358008Seric i = spaceleft; 114458008Seric spaceleft = 0; 114558008Seric } 114658008Seric if (i <= 0) 114758008Seric continue; 114858008Seric bcopy(*tv, bp, i); 114958008Seric bp += i; 115058008Seric spaceleft -= i; 115158008Seric } 115258010Seric *bp = '\0'; 11535704Seric a->q_host = newstr(buf); 11543149Seric } 115557249Seric else 115657249Seric a->q_host = NULL; 11573149Seric 11583149Seric /* figure out the user */ 115936615Sbostic if (*tv == NULL || **tv != CANONUSER) 11604279Seric { 11613149Seric syserr("buildaddr: no user"); 11624279Seric return (NULL); 11634279Seric } 116457402Seric tv++; 116551317Seric 116657402Seric /* do special mapping for local mailer */ 116757402Seric if (m == LocalMailer && *tv != NULL) 116857402Seric { 116957454Seric register char *p = *tv; 117057454Seric 117157454Seric if (*p == '"') 117257454Seric p++; 117357454Seric if (*p == '|') 117457402Seric a->q_mailer = m = ProgMailer; 117557454Seric else if (*p == '/') 117657402Seric a->q_mailer = m = FileMailer; 117757454Seric else if (*p == ':') 117857402Seric { 117957402Seric /* may be :include: */ 118057402Seric cataddr(tv, buf, sizeof buf); 118158008Seric stripquotes(buf); 118258008Seric if (strncasecmp(buf, ":include:", 9) == 0) 118358008Seric { 118458008Seric /* if :include:, don't need further rewriting */ 118557402Seric a->q_mailer = m = InclMailer; 118658008Seric a->q_user = &buf[9]; 118758008Seric return (a); 118858008Seric } 118957402Seric } 119057402Seric } 119157402Seric 119258008Seric if (m == LocalMailer && *tv != NULL && strcmp(*tv, "@") == 0) 119358008Seric { 119458008Seric tv++; 119558008Seric a->q_flags |= QNOTREMOTE; 119658008Seric } 119758008Seric 119819040Seric /* rewrite according recipient mailer rewriting rules */ 119957402Seric rewrite(tv, 2); 120058020Seric if (m->m_re_rwset > 0) 120158020Seric rewrite(tv, m->m_re_rwset); 120219040Seric rewrite(tv, 4); 120319040Seric 120419040Seric /* save the result for the command line/RCPT argument */ 120511278Seric cataddr(tv, buf, sizeof buf); 12063149Seric a->q_user = buf; 12073149Seric 12083149Seric return (a); 12093149Seric } 12103188Seric /* 12114228Seric ** CATADDR -- concatenate pieces of addresses (putting in <LWSP> subs) 12124228Seric ** 12134228Seric ** Parameters: 12144228Seric ** pvp -- parameter vector to rebuild. 12154228Seric ** buf -- buffer to build the string into. 12164228Seric ** sz -- size of buf. 12174228Seric ** 12184228Seric ** Returns: 12194228Seric ** none. 12204228Seric ** 12214228Seric ** Side Effects: 12224228Seric ** Destroys buf. 12234228Seric */ 12244228Seric 12254228Seric cataddr(pvp, buf, sz) 12264228Seric char **pvp; 12274228Seric char *buf; 12284228Seric register int sz; 12294228Seric { 12304228Seric bool oatomtok = FALSE; 123156678Seric bool natomtok = FALSE; 12324228Seric register int i; 12334228Seric register char *p; 12344228Seric 12358423Seric if (pvp == NULL) 12368423Seric { 123723109Seric (void) strcpy(buf, ""); 12388423Seric return; 12398423Seric } 12404228Seric p = buf; 124111156Seric sz -= 2; 12424228Seric while (*pvp != NULL && (i = strlen(*pvp)) < sz) 12434228Seric { 12448078Seric natomtok = (toktype(**pvp) == ATM); 12454228Seric if (oatomtok && natomtok) 12469042Seric *p++ = SpaceSub; 12474228Seric (void) strcpy(p, *pvp); 12484228Seric oatomtok = natomtok; 12494228Seric p += i; 125011156Seric sz -= i + 1; 12514228Seric pvp++; 12524228Seric } 12534228Seric *p = '\0'; 12544228Seric } 12554228Seric /* 12563188Seric ** SAMEADDR -- Determine if two addresses are the same 12573188Seric ** 12583188Seric ** This is not just a straight comparison -- if the mailer doesn't 12593188Seric ** care about the host we just ignore it, etc. 12603188Seric ** 12613188Seric ** Parameters: 12623188Seric ** a, b -- pointers to the internal forms to compare. 12633188Seric ** 12643188Seric ** Returns: 12653188Seric ** TRUE -- they represent the same mailbox. 12663188Seric ** FALSE -- they don't. 12673188Seric ** 12683188Seric ** Side Effects: 12693188Seric ** none. 12703188Seric */ 12713188Seric 12723188Seric bool 12739374Seric sameaddr(a, b) 12743188Seric register ADDRESS *a; 12753188Seric register ADDRESS *b; 12763188Seric { 12773188Seric /* if they don't have the same mailer, forget it */ 12783188Seric if (a->q_mailer != b->q_mailer) 12793188Seric return (FALSE); 12803188Seric 12813188Seric /* if the user isn't the same, we can drop out */ 128256678Seric if (strcmp(a->q_user, b->q_user) != 0) 12833188Seric return (FALSE); 12843188Seric 12853188Seric /* if the mailer ignores hosts, we have succeeded! */ 128610690Seric if (bitnset(M_LOCAL, a->q_mailer->m_flags)) 12873188Seric return (TRUE); 12883188Seric 12893188Seric /* otherwise compare hosts (but be careful for NULL ptrs) */ 12903188Seric if (a->q_host == NULL || b->q_host == NULL) 12913188Seric return (FALSE); 129256678Seric if (strcmp(a->q_host, b->q_host) != 0) 12933188Seric return (FALSE); 12943188Seric 12953188Seric return (TRUE); 12963188Seric } 12973234Seric /* 12983234Seric ** PRINTADDR -- print address (for debugging) 12993234Seric ** 13003234Seric ** Parameters: 13013234Seric ** a -- the address to print 13023234Seric ** follow -- follow the q_next chain. 13033234Seric ** 13043234Seric ** Returns: 13053234Seric ** none. 13063234Seric ** 13073234Seric ** Side Effects: 13083234Seric ** none. 13093234Seric */ 13103234Seric 13113234Seric printaddr(a, follow) 13123234Seric register ADDRESS *a; 13133234Seric bool follow; 13143234Seric { 13155001Seric bool first = TRUE; 131657731Seric register MAILER *m; 131757731Seric MAILER pseudomailer; 13185001Seric 13193234Seric while (a != NULL) 13203234Seric { 13215001Seric first = FALSE; 13224443Seric printf("%x=", a); 13234085Seric (void) fflush(stdout); 132457731Seric 132557731Seric /* find the mailer -- carefully */ 132657731Seric m = a->q_mailer; 132757731Seric if (m == NULL) 132857731Seric { 132957731Seric m = &pseudomailer; 133057731Seric m->m_mno = -1; 133157731Seric m->m_name = "NULL"; 133257731Seric } 133357731Seric 133440973Sbostic printf("%s: mailer %d (%s), host `%s', user `%s', ruser `%s'\n", 133557731Seric a->q_paddr, m->m_mno, m->m_name, 133640973Sbostic a->q_host, a->q_user, a->q_ruser? a->q_ruser: "<null>"); 13378181Seric printf("\tnext=%x, flags=%o, alias %x\n", a->q_next, a->q_flags, 13388181Seric a->q_alias); 13398181Seric printf("\thome=\"%s\", fullname=\"%s\"\n", a->q_home, 13408181Seric a->q_fullname); 13414996Seric 13423234Seric if (!follow) 13433234Seric return; 13444996Seric a = a->q_next; 13453234Seric } 13465001Seric if (first) 13474443Seric printf("[NULL]\n"); 13483234Seric } 13494317Seric 13507682Seric /* 13517682Seric ** REMOTENAME -- return the name relative to the current mailer 13527682Seric ** 13537682Seric ** Parameters: 13547682Seric ** name -- the name to translate. 13558069Seric ** m -- the mailer that we want to do rewriting relative 13568069Seric ** to. 13578069Seric ** senderaddress -- if set, uses the sender rewriting rules 13588069Seric ** rather than the recipient rewriting rules. 135958020Seric ** header -- set if this address is in the header, rather 136058020Seric ** than an envelope header. 136110310Seric ** canonical -- if set, strip out any comment information, 136210310Seric ** etc. 136358020Seric ** e -- the current envelope. 13647682Seric ** 13657682Seric ** Returns: 13667682Seric ** the text string representing this address relative to 13677682Seric ** the receiving mailer. 13687682Seric ** 13697682Seric ** Side Effects: 13707682Seric ** none. 13717682Seric ** 13727682Seric ** Warnings: 13737682Seric ** The text string returned is tucked away locally; 13747682Seric ** copy it if you intend to save it. 13757682Seric */ 13767682Seric 13777682Seric char * 137858020Seric remotename(name, m, senderaddress, header, canonical, e) 13797682Seric char *name; 138056678Seric struct mailer *m; 13818069Seric bool senderaddress; 138258020Seric bool header; 138310310Seric bool canonical; 138456678Seric register ENVELOPE *e; 13857682Seric { 13868069Seric register char **pvp; 13878069Seric char *fancy; 138856678Seric extern char *macvalue(); 138956678Seric char *oldg = macvalue('g', e); 139058020Seric int rwset; 13917682Seric static char buf[MAXNAME]; 13927682Seric char lbuf[MAXNAME]; 139316914Seric char pvpbuf[PSBUFSIZE]; 139456678Seric extern char **prescan(); 139556678Seric extern char *crackaddr(); 13967682Seric 13977755Seric if (tTd(12, 1)) 13987755Seric printf("remotename(%s)\n", name); 13997755Seric 140010177Seric /* don't do anything if we are tagging it as special */ 140158020Seric if (senderaddress) 140258020Seric rwset = header ? m->m_sh_rwset : m->m_se_rwset; 140358020Seric else 140458020Seric rwset = header ? m->m_rh_rwset : m->m_re_rwset; 140558020Seric if (rwset < 0) 140610177Seric return (name); 140710177Seric 14087682Seric /* 14098181Seric ** Do a heuristic crack of this name to extract any comment info. 14108181Seric ** This will leave the name as a comment and a $g macro. 14117889Seric */ 14127889Seric 1413*58036Seric if (canonical || bitnset(M_NOCOMMENT, m->m_flags)) 141416155Seric fancy = "\001g"; 141510310Seric else 141610310Seric fancy = crackaddr(name); 14177889Seric 14188181Seric /* 14198181Seric ** Turn the name into canonical form. 14208181Seric ** Normally this will be RFC 822 style, i.e., "user@domain". 14218181Seric ** If this only resolves to "user", and the "C" flag is 14228181Seric ** specified in the sending mailer, then the sender's 14238181Seric ** domain will be appended. 14248181Seric */ 14258181Seric 142616914Seric pvp = prescan(name, '\0', pvpbuf); 14277889Seric if (pvp == NULL) 14287889Seric return (name); 14298181Seric rewrite(pvp, 3); 143056678Seric if (e->e_fromdomain != NULL) 14318181Seric { 14328181Seric /* append from domain to this address */ 14338181Seric register char **pxp = pvp; 14348181Seric 14359594Seric /* see if there is an "@domain" in the current name */ 14368181Seric while (*pxp != NULL && strcmp(*pxp, "@") != 0) 14378181Seric pxp++; 14388181Seric if (*pxp == NULL) 14398181Seric { 14409594Seric /* no.... append the "@domain" from the sender */ 144156678Seric register char **qxq = e->e_fromdomain; 14428181Seric 14439594Seric while ((*pxp++ = *qxq++) != NULL) 14449594Seric continue; 144511726Seric rewrite(pvp, 3); 14468181Seric } 14478181Seric } 14488181Seric 14498181Seric /* 14508959Seric ** Do more specific rewriting. 145156678Seric ** Rewrite using ruleset 1 or 2 depending on whether this is 145256678Seric ** a sender address or not. 14538181Seric ** Then run it through any receiving-mailer-specific rulesets. 14548181Seric */ 14558181Seric 14568069Seric if (senderaddress) 145756327Seric rewrite(pvp, 1); 14588069Seric else 145956327Seric rewrite(pvp, 2); 146058020Seric if (rwset > 0) 146158020Seric rewrite(pvp, rwset); 14627682Seric 14638181Seric /* 14648959Seric ** Do any final sanitation the address may require. 14658959Seric ** This will normally be used to turn internal forms 14668959Seric ** (e.g., user@host.LOCAL) into external form. This 14678959Seric ** may be used as a default to the above rules. 14688959Seric */ 14698959Seric 14708959Seric rewrite(pvp, 4); 14718959Seric 14728959Seric /* 14738181Seric ** Now restore the comment information we had at the beginning. 14748181Seric */ 14758181Seric 14767682Seric cataddr(pvp, lbuf, sizeof lbuf); 147756678Seric define('g', lbuf, e); 147856678Seric expand(fancy, buf, &buf[sizeof buf - 1], e); 147956678Seric define('g', oldg, e); 14807682Seric 14817682Seric if (tTd(12, 1)) 14827755Seric printf("remotename => `%s'\n", buf); 14837682Seric return (buf); 14847682Seric } 148551317Seric /* 148656678Seric ** MAPLOCALUSER -- run local username through ruleset 5 for final redirection 148751317Seric ** 148851317Seric ** Parameters: 148956678Seric ** a -- the address to map (but just the user name part). 149056678Seric ** sendq -- the sendq in which to install any replacement 149156678Seric ** addresses. 149251317Seric ** 149351317Seric ** Returns: 149451317Seric ** none. 149551317Seric */ 149651317Seric 149756678Seric maplocaluser(a, sendq, e) 149856678Seric register ADDRESS *a; 149956678Seric ADDRESS **sendq; 150056678Seric ENVELOPE *e; 150151317Seric { 150256678Seric register char **pvp; 150356678Seric register ADDRESS *a1 = NULL; 150456678Seric char pvpbuf[PSBUFSIZE]; 150551317Seric 150656678Seric if (tTd(29, 1)) 150756678Seric { 150856678Seric printf("maplocaluser: "); 150956678Seric printaddr(a, FALSE); 151056678Seric } 151156678Seric pvp = prescan(a->q_user, '\0', pvpbuf); 151256678Seric if (pvp == NULL) 151356678Seric return; 151451317Seric 151556678Seric rewrite(pvp, 5); 151656678Seric if (pvp[0] == NULL || pvp[0][0] != CANONNET) 151756678Seric return; 151851317Seric 151956678Seric /* if non-null, mailer destination specified -- has it changed? */ 152056678Seric a1 = buildaddr(pvp, NULL); 152156678Seric if (a1 == NULL || sameaddr(a, a1)) 152256678Seric return; 152351317Seric 152456678Seric /* mark old address as dead; insert new address */ 152556678Seric a->q_flags |= QDONTSEND; 152657731Seric if (tTd(29, 5)) 152757731Seric { 152857731Seric printf("maplocaluser: QDONTSEND "); 152957731Seric printaddr(a, FALSE); 153057731Seric } 153156678Seric a1->q_alias = a; 153256678Seric allocaddr(a1, 1, NULL); 153356678Seric (void) recipient(a1, sendq, e); 153451317Seric } 1535