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*58173Seric static char sccsid[] = "@(#)parseaddr.c 6.20 (Berkeley) 02/24/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 */ 5558050Seric # define DELIMCHARS "\201()<>,;\\\"\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 10658050Seric if ((pvp[0][0] & 0377) != CANONNET) 1073149Seric { 1083149Seric setstat(EX_USAGE); 10958151Seric syserr("554 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 { 15758050Seric if ((*addr & 0340) != 0200) 15857388Seric continue; 15957388Seric setstat(EX_USAGE); 16058151Seric usrerr("553 Address contained invalid control characters"); 16157388Seric return TRUE; 16257388Seric } 16357388Seric return FALSE; 16457388Seric } 16557388Seric /* 16656678Seric ** ALLOCADDR -- do local allocations of address on demand. 16756678Seric ** 16856678Seric ** Also lowercases the host name if requested. 16956678Seric ** 17056678Seric ** Parameters: 17156678Seric ** a -- the address to reallocate. 17256678Seric ** copyf -- the copy flag (see parseaddr for description). 17356678Seric ** paddr -- the printname of the address. 17456678Seric ** 17556678Seric ** Returns: 17656678Seric ** none. 17756678Seric ** 17856678Seric ** Side Effects: 17956678Seric ** Copies portions of a into local buffers as requested. 18056678Seric */ 18156678Seric 18256678Seric allocaddr(a, copyf, paddr) 18356678Seric register ADDRESS *a; 18456678Seric int copyf; 18556678Seric char *paddr; 18656678Seric { 18756678Seric register MAILER *m = a->q_mailer; 18856678Seric 18956678Seric if (copyf > 0 && paddr != NULL) 19056678Seric { 19156678Seric extern char *DelimChar; 1928078Seric char savec = *DelimChar; 1938078Seric 1948078Seric *DelimChar = '\0'; 19556678Seric a->q_paddr = newstr(paddr); 1968078Seric *DelimChar = savec; 1978078Seric } 198297Seric else 19956678Seric a->q_paddr = paddr; 20024944Seric 20124944Seric if (a->q_user == NULL) 20224944Seric a->q_user = ""; 20324944Seric if (a->q_host == NULL) 20424944Seric a->q_host = ""; 20524944Seric 2063149Seric if (copyf >= 0) 207297Seric { 20824944Seric a->q_host = newstr(a->q_host); 2093149Seric if (a->q_user != a->q_paddr) 2103149Seric a->q_user = newstr(a->q_user); 211297Seric } 212297Seric 21356678Seric if (a->q_paddr == NULL) 21456678Seric a->q_paddr = a->q_user; 21556678Seric 216297Seric /* 21716202Seric ** Convert host name to lower case if requested. 21816202Seric ** User name will be done later. 21916202Seric */ 22016202Seric 22116202Seric if (!bitnset(M_HST_UPPER, m->m_flags)) 22216202Seric makelower(a->q_host); 223297Seric } 224297Seric /* 22516162Seric ** LOWERADDR -- map UPPER->lower case on addresses as requested. 22616162Seric ** 22716162Seric ** Parameters: 22816162Seric ** a -- address to be mapped. 22916162Seric ** 23016162Seric ** Returns: 23116162Seric ** none. 23216162Seric ** 23316162Seric ** Side Effects: 23416162Seric ** none. 23516162Seric */ 23616162Seric 23716162Seric loweraddr(a) 23816162Seric register ADDRESS *a; 23916162Seric { 24016162Seric register MAILER *m = a->q_mailer; 24116162Seric 24216162Seric if (!bitnset(M_USR_UPPER, m->m_flags)) 24316162Seric makelower(a->q_user); 24416162Seric } 24516162Seric /* 246297Seric ** PRESCAN -- Prescan name and make it canonical 247297Seric ** 2489374Seric ** Scans a name and turns it into a set of tokens. This process 2499374Seric ** deletes blanks and comments (in parentheses). 250297Seric ** 251297Seric ** This routine knows about quoted strings and angle brackets. 252297Seric ** 253297Seric ** There are certain subtleties to this routine. The one that 254297Seric ** comes to mind now is that backslashes on the ends of names 255297Seric ** are silently stripped off; this is intentional. The problem 256297Seric ** is that some versions of sndmsg (like at LBL) set the kill 257297Seric ** character to something other than @ when reading addresses; 258297Seric ** so people type "csvax.eric\@berkeley" -- which screws up the 259297Seric ** berknet mailer. 260297Seric ** 261297Seric ** Parameters: 262297Seric ** addr -- the name to chomp. 263297Seric ** delim -- the delimiter for the address, normally 264297Seric ** '\0' or ','; \0 is accepted in any case. 26515284Seric ** If '\t' then we are reading the .cf file. 26616914Seric ** pvpbuf -- place to put the saved text -- note that 26716914Seric ** the pointers are static. 268297Seric ** 269297Seric ** Returns: 2703149Seric ** A pointer to a vector of tokens. 271297Seric ** NULL on error. 272297Seric ** 273297Seric ** Side Effects: 27425279Seric ** sets DelimChar to point to the character matching 'delim'. 275297Seric */ 276297Seric 2778078Seric /* states and character types */ 2788078Seric # define OPR 0 /* operator */ 2798078Seric # define ATM 1 /* atom */ 2808078Seric # define QST 2 /* in quoted string */ 2818078Seric # define SPC 3 /* chewing up spaces */ 2828078Seric # define ONE 4 /* pick up one character */ 2833149Seric 2848078Seric # define NSTATES 5 /* number of states */ 2858078Seric # define TYPE 017 /* mask to select state type */ 2868078Seric 2878078Seric /* meta bits for table */ 2888078Seric # define M 020 /* meta character; don't pass through */ 2898078Seric # define B 040 /* cause a break */ 2908078Seric # define MB M|B /* meta-break */ 2918078Seric 2928078Seric static short StateTab[NSTATES][NSTATES] = 2938078Seric { 2948087Seric /* oldst chtype> OPR ATM QST SPC ONE */ 2959051Seric /*OPR*/ OPR|B, ATM|B, QST|B, SPC|MB, ONE|B, 2969051Seric /*ATM*/ OPR|B, ATM, QST|B, SPC|MB, ONE|B, 2979051Seric /*QST*/ QST, QST, OPR, QST, QST, 2988078Seric /*SPC*/ OPR, ATM, QST, SPC|M, ONE, 2998078Seric /*ONE*/ OPR, OPR, OPR, OPR, OPR, 3008078Seric }; 3018078Seric 3028078Seric # define NOCHAR -1 /* signal nothing in lookahead token */ 3038078Seric 30456678Seric char *DelimChar; /* set to point to the delimiter */ 30556678Seric 3063149Seric char ** 30716914Seric prescan(addr, delim, pvpbuf) 308297Seric char *addr; 309297Seric char delim; 31016914Seric char pvpbuf[]; 311297Seric { 312297Seric register char *p; 3138078Seric register char *q; 3149346Seric register int c; 3153149Seric char **avp; 316297Seric bool bslashmode; 317297Seric int cmntcnt; 3188423Seric int anglecnt; 3193149Seric char *tok; 3208078Seric int state; 3218078Seric int newstate; 3228078Seric static char *av[MAXATOM+1]; 32356678Seric extern int errno; 324297Seric 32515253Seric /* make sure error messages don't have garbage on them */ 32615253Seric errno = 0; 32715253Seric 32816914Seric q = pvpbuf; 3293149Seric bslashmode = FALSE; 3307800Seric cmntcnt = 0; 3318423Seric anglecnt = 0; 3323149Seric avp = av; 33356678Seric state = ATM; 3348078Seric c = NOCHAR; 3358078Seric p = addr; 33656764Seric if (tTd(22, 11)) 337297Seric { 3388078Seric printf("prescan: "); 3398078Seric xputs(p); 34023109Seric (void) putchar('\n'); 3418078Seric } 3428078Seric 3438078Seric do 3448078Seric { 3453149Seric /* read a token */ 3463149Seric tok = q; 3478078Seric for (;;) 348297Seric { 3498078Seric /* store away any old lookahead character */ 3508078Seric if (c != NOCHAR) 3518078Seric { 35215284Seric /* see if there is room */ 35316914Seric if (q >= &pvpbuf[PSBUFSIZE - 5]) 3548078Seric { 35558151Seric usrerr("553 Address too long"); 3568078Seric DelimChar = p; 3578078Seric return (NULL); 3588078Seric } 35915284Seric 36015284Seric /* squirrel it away */ 3618078Seric *q++ = c; 3628078Seric } 3638078Seric 3648078Seric /* read a new input character */ 3658078Seric c = *p++; 36657631Seric if (c == '\0') 36756764Seric { 36856764Seric /* diagnose and patch up bad syntax */ 36956764Seric if (state == QST) 37056764Seric { 37158151Seric usrerr("553 Unbalanced '\"'"); 37256764Seric c = '"'; 37356764Seric } 37456764Seric else if (cmntcnt > 0) 37556764Seric { 37658151Seric usrerr("553 Unbalanced '('"); 37756764Seric c = ')'; 37856764Seric } 37956764Seric else if (anglecnt > 0) 38056764Seric { 38156764Seric c = '>'; 38258151Seric usrerr("553 Unbalanced '<'"); 38356764Seric } 38456764Seric else 38556764Seric break; 38615284Seric 38756764Seric p--; 38856764Seric } 38957631Seric else if (c == delim && anglecnt <= 0 && 39057631Seric cmntcnt <= 0 && state != QST) 39157631Seric break; 39256764Seric 3938078Seric if (tTd(22, 101)) 3948078Seric printf("c=%c, s=%d; ", c, state); 3958078Seric 3963149Seric /* chew up special characters */ 3973149Seric *q = '\0'; 3983149Seric if (bslashmode) 3993149Seric { 40024944Seric /* kludge \! for naive users */ 40158061Seric if (cmntcnt > 0) 40258061Seric c = NOCHAR; 40358061Seric else if (c != '!') 40456678Seric *q++ = '\\'; 4053149Seric bslashmode = FALSE; 40656678Seric continue; 4073149Seric } 40856678Seric 40956678Seric if (c == '\\') 4103149Seric { 4113149Seric bslashmode = TRUE; 4128078Seric c = NOCHAR; 41356678Seric continue; 4143149Seric } 41556678Seric else if (state == QST) 4168514Seric { 4178514Seric /* do nothing, just avoid next clauses */ 4188514Seric } 4198078Seric else if (c == '(') 4204100Seric { 4218078Seric cmntcnt++; 4228078Seric c = NOCHAR; 4234100Seric } 4248078Seric else if (c == ')') 4253149Seric { 4268078Seric if (cmntcnt <= 0) 4273149Seric { 42858151Seric usrerr("553 Unbalanced ')'"); 4298078Seric DelimChar = p; 4308078Seric return (NULL); 4313149Seric } 4328078Seric else 4338078Seric cmntcnt--; 4348078Seric } 4358078Seric else if (cmntcnt > 0) 4368078Seric c = NOCHAR; 4378423Seric else if (c == '<') 4388423Seric anglecnt++; 4398423Seric else if (c == '>') 4408423Seric { 4418423Seric if (anglecnt <= 0) 4428423Seric { 44358151Seric usrerr("553 Unbalanced '>'"); 4448423Seric DelimChar = p; 4458423Seric return (NULL); 4468423Seric } 4478423Seric anglecnt--; 4488423Seric } 44958050Seric else if (delim == ' ' && isascii(c) && isspace(c)) 45011423Seric c = ' '; 4513149Seric 4528078Seric if (c == NOCHAR) 4538078Seric continue; 4543149Seric 4558078Seric /* see if this is end of input */ 45611405Seric if (c == delim && anglecnt <= 0 && state != QST) 4573149Seric break; 4583149Seric 4598078Seric newstate = StateTab[state][toktype(c)]; 4608078Seric if (tTd(22, 101)) 4618078Seric printf("ns=%02o\n", newstate); 4628078Seric state = newstate & TYPE; 4638078Seric if (bitset(M, newstate)) 4648078Seric c = NOCHAR; 4658078Seric if (bitset(B, newstate)) 4664228Seric break; 467297Seric } 4683149Seric 4693149Seric /* new token */ 4708078Seric if (tok != q) 4711378Seric { 4728078Seric *q++ = '\0'; 4738078Seric if (tTd(22, 36)) 474297Seric { 4758078Seric printf("tok="); 4768078Seric xputs(tok); 47723109Seric (void) putchar('\n'); 478297Seric } 4798078Seric if (avp >= &av[MAXATOM]) 480297Seric { 48158151Seric syserr("553 prescan: too many tokens"); 4828078Seric DelimChar = p; 4838078Seric return (NULL); 484297Seric } 4858078Seric *avp++ = tok; 486297Seric } 4878423Seric } while (c != '\0' && (c != delim || anglecnt > 0)); 4883149Seric *avp = NULL; 4898078Seric DelimChar = --p; 49056764Seric if (tTd(22, 12)) 49156764Seric { 49256764Seric printf("prescan==>"); 49356764Seric printav(av); 49456764Seric } 49556764Seric if (av[0] != NULL) 4963149Seric return (av); 4973149Seric return (NULL); 4983149Seric } 4993149Seric /* 5003149Seric ** TOKTYPE -- return token type 5013149Seric ** 5023149Seric ** Parameters: 5033149Seric ** c -- the character in question. 5043149Seric ** 5053149Seric ** Returns: 5063149Seric ** Its type. 5073149Seric ** 5083149Seric ** Side Effects: 5093149Seric ** none. 5103149Seric */ 511297Seric 5123149Seric toktype(c) 51358050Seric register int c; 5143149Seric { 5153380Seric static char buf[50]; 5163382Seric static bool firstime = TRUE; 5173380Seric 5183382Seric if (firstime) 5193380Seric { 5203382Seric firstime = FALSE; 52158050Seric expand("\201o", buf, &buf[sizeof buf - 1], CurEnv); 5227005Seric (void) strcat(buf, DELIMCHARS); 5233380Seric } 52458050Seric c &= 0377; 52556327Seric if (c == MATCHCLASS || c == MATCHREPL || c == MATCHNCLASS) 5268078Seric return (ONE); 5278078Seric if (c == '"') 5288078Seric return (QST); 52958050Seric if ((c & 0340) == 0200) 53058050Seric return (OPR); 5314100Seric if (!isascii(c)) 5328078Seric return (ATM); 5338078Seric if (isspace(c) || c == ')') 5348078Seric return (SPC); 53558050Seric if (strchr(buf, c) != NULL) 5368078Seric return (OPR); 5378078Seric return (ATM); 5383149Seric } 5393149Seric /* 5403149Seric ** REWRITE -- apply rewrite rules to token vector. 5413149Seric ** 5424476Seric ** This routine is an ordered production system. Each rewrite 5434476Seric ** rule has a LHS (called the pattern) and a RHS (called the 5444476Seric ** rewrite); 'rwr' points the the current rewrite rule. 5454476Seric ** 5464476Seric ** For each rewrite rule, 'avp' points the address vector we 5474476Seric ** are trying to match against, and 'pvp' points to the pattern. 5488058Seric ** If pvp points to a special match value (MATCHZANY, MATCHANY, 5499585Seric ** MATCHONE, MATCHCLASS, MATCHNCLASS) then the address in avp 5509585Seric ** matched is saved away in the match vector (pointed to by 'mvp'). 5514476Seric ** 5524476Seric ** When a match between avp & pvp does not match, we try to 5539585Seric ** back out. If we back up over MATCHONE, MATCHCLASS, or MATCHNCLASS 5544476Seric ** we must also back out the match in mvp. If we reach a 5558058Seric ** MATCHANY or MATCHZANY we just extend the match and start 5568058Seric ** over again. 5574476Seric ** 5584476Seric ** When we finally match, we rewrite the address vector 5594476Seric ** and try over again. 5604476Seric ** 5613149Seric ** Parameters: 5623149Seric ** pvp -- pointer to token vector. 5633149Seric ** 5643149Seric ** Returns: 5653149Seric ** none. 5663149Seric ** 5673149Seric ** Side Effects: 5683149Seric ** pvp is modified. 5693149Seric */ 5702091Seric 5713149Seric struct match 5723149Seric { 5734468Seric char **first; /* first token matched */ 5744468Seric char **last; /* last token matched */ 5753149Seric }; 5763149Seric 5774468Seric # define MAXMATCH 9 /* max params per rewrite */ 5783149Seric 5793149Seric 5804070Seric rewrite(pvp, ruleset) 5813149Seric char **pvp; 5824070Seric int ruleset; 5833149Seric { 5843149Seric register char *ap; /* address pointer */ 5853149Seric register char *rp; /* rewrite pointer */ 5863149Seric register char **avp; /* address vector pointer */ 5873149Seric register char **rvp; /* rewrite vector pointer */ 5888058Seric register struct match *mlp; /* cur ptr into mlist */ 5898058Seric register struct rewrite *rwr; /* pointer to current rewrite rule */ 59056678Seric struct match mlist[MAXMATCH]; /* stores match on LHS */ 5913149Seric char *npvp[MAXATOM+1]; /* temporary space for rebuild */ 5923149Seric 5939279Seric if (OpMode == MD_TEST || tTd(21, 2)) 5943149Seric { 5958959Seric printf("rewrite: ruleset %2d input:", ruleset); 59656678Seric printav(pvp); 5973149Seric } 59856678Seric if (ruleset < 0 || ruleset >= MAXRWSETS) 59956326Seric { 60058151Seric syserr("554 rewrite: illegal ruleset number %d", ruleset); 60156326Seric return; 60256326Seric } 60356678Seric if (pvp == NULL) 60456678Seric return; 60556326Seric 6063149Seric /* 60756678Seric ** Run through the list of rewrite rules, applying 60856678Seric ** any that match. 6093149Seric */ 6103149Seric 6114070Seric for (rwr = RewriteRules[ruleset]; rwr != NULL; ) 6123149Seric { 61356678Seric int loopcount = 0; 61456678Seric 6157675Seric if (tTd(21, 12)) 616297Seric { 6178069Seric printf("-----trying rule:"); 61856678Seric printav(rwr->r_lhs); 6193149Seric } 6203149Seric 6213149Seric /* try to match on this rule */ 6224468Seric mlp = mlist; 6238058Seric rvp = rwr->r_lhs; 6248058Seric avp = pvp; 6258058Seric while ((ap = *avp) != NULL || *rvp != NULL) 6263149Seric { 62756678Seric if (++loopcount > 100) 62852637Seric { 62958151Seric syserr("554 Infinite loop in ruleset %d", ruleset); 63056678Seric printf("workspace: "); 63156678Seric printav(pvp); 63252637Seric break; 63352637Seric } 6343149Seric rp = *rvp; 6358058Seric if (tTd(21, 35)) 6368058Seric { 63757532Seric printf("rp="); 63857531Seric xputs(rp); 63957532Seric printf(", ap="); 6408058Seric xputs(ap); 6418069Seric printf("\n"); 6428058Seric } 64356678Seric if (rp == NULL) 64456326Seric { 6453149Seric /* end-of-pattern before end-of-address */ 6468058Seric goto backup; 64756678Seric } 648*58173Seric if (ap == NULL && (*rp & 0377) != MATCHZANY && 649*58173Seric (*rp & 0377) != CANONHOST) 65056326Seric { 65156678Seric /* end-of-input */ 65256678Seric break; 653297Seric } 65456326Seric 65558050Seric switch (*rp & 0377) 6568058Seric { 65756678Seric register STAB *s; 65856326Seric 65956678Seric case MATCHCLASS: 66056678Seric case MATCHNCLASS: 66156678Seric /* match any token in (not in) a class */ 66256678Seric s = stab(ap, ST_CLASS, ST_FIND); 66356678Seric if (s == NULL || !bitnset(rp[1], s->s_class)) 66456326Seric { 66558050Seric if ((*rp & 0377) == MATCHCLASS) 6669585Seric goto backup; 66756326Seric } 66858050Seric else if ((*rp & 0377) == MATCHNCLASS) 66956678Seric goto backup; 6704060Seric 67156678Seric /* explicit fall-through */ 67256678Seric 67356678Seric case MATCHONE: 67456678Seric case MATCHANY: 67556678Seric /* match exactly one token */ 67656678Seric mlp->first = avp; 67756678Seric mlp->last = avp++; 6788058Seric mlp++; 67956678Seric break; 6808058Seric 68156678Seric case MATCHZANY: 68256678Seric /* match zero or more tokens */ 68356678Seric mlp->first = avp; 68456678Seric mlp->last = avp - 1; 68556678Seric mlp++; 68656678Seric break; 68756326Seric 688*58173Seric case CANONHOST: 689*58173Seric /* match zero tokens */ 690*58173Seric break; 691*58173Seric 69256678Seric default: 69356678Seric /* must have exact match */ 69456678Seric if (strcasecmp(rp, ap)) 6958058Seric goto backup; 6964468Seric avp++; 69756678Seric break; 6983149Seric } 6993149Seric 70056678Seric /* successful match on this token */ 7013149Seric rvp++; 7023149Seric continue; 7033149Seric 70456678Seric backup: 70556678Seric /* match failed -- back up */ 70656678Seric while (--rvp >= rwr->r_lhs) 7073149Seric { 70856678Seric rp = *rvp; 70958050Seric if ((*rp & 0377) == MATCHANY || 71058050Seric (*rp & 0377) == MATCHZANY) 7114468Seric { 71256678Seric /* extend binding and continue */ 71356678Seric avp = ++mlp[-1].last; 71456678Seric avp++; 71556678Seric rvp++; 71656678Seric break; 7174468Seric } 71856678Seric avp--; 71958050Seric if ((*rp & 0377) == MATCHONE || 72058050Seric (*rp & 0377) == MATCHCLASS || 72158050Seric (*rp & 0377) == MATCHNCLASS) 72256678Seric { 72356678Seric /* back out binding */ 72456678Seric mlp--; 72556678Seric } 7263149Seric } 7273149Seric 72856678Seric if (rvp < rwr->r_lhs) 72956678Seric { 73056678Seric /* total failure to match */ 73156326Seric break; 7323149Seric } 733297Seric } 7343149Seric 7353149Seric /* 73656678Seric ** See if we successfully matched 7373149Seric */ 7383149Seric 73956678Seric if (rvp < rwr->r_lhs || *rvp != NULL) 7403149Seric { 7419374Seric if (tTd(21, 10)) 7429374Seric printf("----- rule fails\n"); 7439374Seric rwr = rwr->r_next; 7449374Seric continue; 7459374Seric } 7463149Seric 7479374Seric rvp = rwr->r_rhs; 7489374Seric if (tTd(21, 12)) 7499374Seric { 7509374Seric printf("-----rule matches:"); 75156678Seric printav(rvp); 7529374Seric } 7539374Seric 7549374Seric rp = *rvp; 75558050Seric if ((*rp & 0377) == CANONUSER) 7569374Seric { 7579374Seric rvp++; 7589374Seric rwr = rwr->r_next; 7599374Seric } 76058050Seric else if ((*rp & 0377) == CANONHOST) 7619374Seric { 7629374Seric rvp++; 7639374Seric rwr = NULL; 7649374Seric } 76558050Seric else if ((*rp & 0377) == CANONNET) 7669374Seric rwr = NULL; 7679374Seric 7689374Seric /* substitute */ 7699374Seric for (avp = npvp; *rvp != NULL; rvp++) 7709374Seric { 7719374Seric register struct match *m; 7729374Seric register char **pp; 7739374Seric 7748058Seric rp = *rvp; 77558050Seric if ((*rp & 0377) == MATCHREPL) 7768058Seric { 77716914Seric /* substitute from LHS */ 77816914Seric m = &mlist[rp[1] - '1']; 77956678Seric if (m < mlist || m >= mlp) 7809374Seric { 78158151Seric syserr("554 rewrite: ruleset %d: replacement $%c out of bounds", 78256326Seric ruleset, rp[1]); 7839374Seric return; 7849374Seric } 78516914Seric if (tTd(21, 15)) 78616914Seric { 78716914Seric printf("$%c:", rp[1]); 78816914Seric pp = m->first; 78956678Seric while (pp <= m->last) 79016914Seric { 79116914Seric printf(" %x=\"", *pp); 79216914Seric (void) fflush(stdout); 79316914Seric printf("%s\"", *pp++); 79416914Seric } 79516914Seric printf("\n"); 79616914Seric } 7979374Seric pp = m->first; 79856678Seric while (pp <= m->last) 7993149Seric { 80016914Seric if (avp >= &npvp[MAXATOM]) 80156678Seric { 80258151Seric syserr("554 rewrite: expansion too long"); 80356678Seric return; 80456678Seric } 80516914Seric *avp++ = *pp++; 8063149Seric } 8073149Seric } 80816914Seric else 8098226Seric { 81016914Seric /* vanilla replacement */ 8119374Seric if (avp >= &npvp[MAXATOM]) 81216889Seric { 81356678Seric toolong: 81458151Seric syserr("554 rewrite: expansion too long"); 81516889Seric return; 81616889Seric } 81756678Seric *avp++ = rp; 8188226Seric } 8199374Seric } 8209374Seric *avp++ = NULL; 82116914Seric 82216914Seric /* 82356678Seric ** Check for any hostname/keyword lookups. 82416914Seric */ 82516914Seric 82616914Seric for (rvp = npvp; *rvp != NULL; rvp++) 82716914Seric { 82856678Seric char **hbrvp; 82916914Seric char **xpvp; 83016914Seric int trsize; 83117473Seric char *olddelimchar; 83256678Seric char *replac; 83356678Seric int endtoken; 83456678Seric STAB *map; 83556678Seric char *mapname; 83656678Seric char **key_rvp; 83756678Seric char **arg_rvp; 83856678Seric char **default_rvp; 83956678Seric char buf[MAXNAME + 1]; 84016914Seric char *pvpb1[MAXATOM + 1]; 84156823Seric char *argvect[10]; 84217174Seric char pvpbuf[PSBUFSIZE]; 84356678Seric extern char *DelimChar; 84416914Seric 84558050Seric if ((**rvp & 0377) != HOSTBEGIN && 84658050Seric (**rvp & 0377) != LOOKUPBEGIN) 84716914Seric continue; 84816914Seric 84916914Seric /* 85056678Seric ** Got a hostname/keyword lookup. 85116914Seric ** 85216914Seric ** This could be optimized fairly easily. 85316914Seric */ 85416914Seric 85516914Seric hbrvp = rvp; 85658050Seric if ((**rvp & 0377) == HOSTBEGIN) 85756327Seric { 85856678Seric endtoken = HOSTEND; 85956678Seric mapname = "host"; 86056327Seric } 86156326Seric else 86256327Seric { 86356678Seric endtoken = LOOKUPEND; 86456678Seric mapname = *++rvp; 86556327Seric } 86656678Seric map = stab(mapname, ST_MAP, ST_FIND); 86756678Seric if (map == NULL) 86858151Seric syserr("554 rewrite: map %s not found", mapname); 86956678Seric 87056678Seric /* extract the match part */ 87156678Seric key_rvp = ++rvp; 87256823Seric default_rvp = NULL; 87356823Seric arg_rvp = argvect; 87456823Seric xpvp = NULL; 87556823Seric replac = pvpbuf; 87658050Seric while (*rvp != NULL && (**rvp & 0377) != endtoken) 87753654Seric { 87858050Seric int nodetype = **rvp & 0377; 87956823Seric 88056823Seric if (nodetype != CANONHOST && nodetype != CANONUSER) 88156678Seric { 88256823Seric rvp++; 88356823Seric continue; 88456823Seric } 88556823Seric 88656823Seric *rvp++ = NULL; 88756823Seric 88856823Seric if (xpvp != NULL) 88956823Seric { 89056823Seric cataddr(xpvp, replac, 89158082Seric &pvpbuf[sizeof pvpbuf] - replac, 89258082Seric '\0'); 89356823Seric *++arg_rvp = replac; 89456823Seric replac += strlen(replac) + 1; 89556823Seric xpvp = NULL; 89656823Seric } 89756823Seric switch (nodetype) 89856823Seric { 89956678Seric case CANONHOST: 90056823Seric xpvp = rvp; 90156678Seric break; 90256678Seric 90356678Seric case CANONUSER: 90456678Seric default_rvp = rvp; 90556678Seric break; 90656678Seric } 90753654Seric } 90816914Seric if (*rvp != NULL) 90916914Seric *rvp++ = NULL; 91056823Seric if (xpvp != NULL) 91156823Seric { 91256823Seric cataddr(xpvp, replac, 91358082Seric &pvpbuf[sizeof pvpbuf] - replac, 91458082Seric '\0'); 91556823Seric *++arg_rvp = replac; 91656823Seric } 91756823Seric *++arg_rvp = NULL; 91816914Seric 91916914Seric /* save the remainder of the input string */ 92016914Seric trsize = (int) (avp - rvp + 1) * sizeof *rvp; 92116914Seric bcopy((char *) rvp, (char *) pvpb1, trsize); 92216914Seric 92356678Seric /* look it up */ 92458082Seric cataddr(key_rvp, buf, sizeof buf, '\0'); 92556823Seric argvect[0] = buf; 92656678Seric if (map != NULL && bitset(MF_VALID, map->s_map.map_flags)) 92756836Seric { 92856836Seric int bsize = sizeof buf - 1; 92956836Seric 93056836Seric if (map->s_map.map_app != NULL) 93156836Seric bsize -= strlen(map->s_map.map_app); 93256836Seric replac = (*map->s_map.map_class->map_lookup)(&map->s_map, 93356823Seric buf, sizeof buf - 1, argvect); 93456836Seric if (replac != NULL && map->s_map.map_app != NULL) 93556836Seric strcat(replac, map->s_map.map_app); 93656836Seric } 93753654Seric else 93856678Seric replac = NULL; 93956678Seric 94056678Seric /* if no replacement, use default */ 94156823Seric if (replac == NULL && default_rvp != NULL) 94256823Seric { 94356823Seric char buf2[sizeof buf]; 94456823Seric 94556823Seric /* rewrite the default with % translations */ 94658082Seric cataddr(default_rvp, buf2, sizeof buf2, '\0'); 94756823Seric map_rewrite(buf2, sizeof buf2, buf, sizeof buf, 94856823Seric argvect); 94956823Seric replac = buf; 95056823Seric } 95156823Seric 95256678Seric if (replac == NULL) 95351317Seric { 95456823Seric xpvp = key_rvp; 95553654Seric } 95656678Seric else 95753654Seric { 95856678Seric /* scan the new replacement */ 95956678Seric olddelimchar = DelimChar; 96056678Seric xpvp = prescan(replac, '\0', pvpbuf); 96156678Seric DelimChar = olddelimchar; 96253654Seric if (xpvp == NULL) 96351317Seric { 96458151Seric syserr("553 rewrite: cannot prescan map value: %s", replac); 96556678Seric return; 96656678Seric } 96751317Seric } 96851317Seric 96916914Seric /* append it to the token list */ 97056678Seric for (avp = hbrvp; *xpvp != NULL; xpvp++) 97156678Seric { 97217174Seric *avp++ = newstr(*xpvp); 97316920Seric if (avp >= &npvp[MAXATOM]) 97416914Seric goto toolong; 97517174Seric } 97616914Seric 97716914Seric /* restore the old trailing information */ 97856678Seric for (xpvp = pvpb1; (*avp++ = *xpvp++) != NULL; ) 97916920Seric if (avp >= &npvp[MAXATOM]) 98016914Seric goto toolong; 98117174Seric 98256678Seric break; 98316914Seric } 98416914Seric 98516914Seric /* 98616914Seric ** Check for subroutine calls. 98716914Seric */ 98816914Seric 98958050Seric if (*npvp != NULL && (**npvp & 0377) == CALLSUBR) 99056678Seric { 99156678Seric bcopy((char *) &npvp[2], (char *) pvp, 99256678Seric (int) (avp - npvp - 2) * sizeof *avp); 99356678Seric if (tTd(21, 3)) 99456678Seric printf("-----callsubr %s\n", npvp[1]); 99556678Seric rewrite(pvp, atoi(npvp[1])); 99656678Seric } 99756678Seric else 99856678Seric { 99917348Seric bcopy((char *) npvp, (char *) pvp, 100016900Seric (int) (avp - npvp) * sizeof *avp); 100156678Seric } 10029374Seric if (tTd(21, 4)) 10039374Seric { 10049374Seric printf("rewritten as:"); 100556678Seric printav(pvp); 10069374Seric } 1007297Seric } 10088069Seric 10099279Seric if (OpMode == MD_TEST || tTd(21, 2)) 10108069Seric { 10118959Seric printf("rewrite: ruleset %2d returns:", ruleset); 101256678Seric printav(pvp); 10138069Seric } 10143149Seric } 10153149Seric /* 10163149Seric ** BUILDADDR -- build address from token vector. 10173149Seric ** 10183149Seric ** Parameters: 10193149Seric ** tv -- token vector. 10203149Seric ** a -- pointer to address descriptor to fill. 10213149Seric ** If NULL, one will be allocated. 10223149Seric ** 10233149Seric ** Returns: 10244279Seric ** NULL if there was an error. 10254279Seric ** 'a' otherwise. 10263149Seric ** 10273149Seric ** Side Effects: 10283149Seric ** fills in 'a' 10293149Seric */ 10303149Seric 103157249Seric struct errcodes 103257249Seric { 103357249Seric char *ec_name; /* name of error code */ 103457249Seric int ec_code; /* numeric code */ 103557249Seric } ErrorCodes[] = 103657249Seric { 103757249Seric "usage", EX_USAGE, 103857249Seric "nouser", EX_NOUSER, 103957249Seric "nohost", EX_NOHOST, 104057249Seric "unavailable", EX_UNAVAILABLE, 104157249Seric "software", EX_SOFTWARE, 104257249Seric "tempfail", EX_TEMPFAIL, 104357249Seric "protocol", EX_PROTOCOL, 104457249Seric #ifdef EX_CONFIG 104557249Seric "config", EX_CONFIG, 104657249Seric #endif 104757249Seric NULL, EX_UNAVAILABLE, 104857249Seric }; 104957249Seric 105056678Seric ADDRESS * 10513149Seric buildaddr(tv, a) 10523149Seric register char **tv; 10533149Seric register ADDRESS *a; 10543149Seric { 10553149Seric struct mailer **mp; 10563149Seric register struct mailer *m; 105758008Seric char *bp; 105858008Seric int spaceleft; 105957402Seric static char buf[MAXNAME]; 10603149Seric 10613149Seric if (a == NULL) 10623149Seric a = (ADDRESS *) xalloc(sizeof *a); 106316889Seric bzero((char *) a, sizeof *a); 10643149Seric 10653149Seric /* figure out what net/mailer to use */ 106658050Seric if ((**tv & 0377) != CANONNET) 10674279Seric { 106858151Seric syserr("554 buildaddr: no net"); 10694279Seric return (NULL); 10704279Seric } 10713149Seric tv++; 107233725Sbostic if (!strcasecmp(*tv, "error")) 10734279Seric { 107458050Seric if ((**++tv & 0377) == CANONHOST) 107510183Seric { 107657249Seric register struct errcodes *ep; 107757249Seric 107858050Seric if (isascii(**++tv) && isdigit(**tv)) 107957249Seric { 108057249Seric setstat(atoi(*tv)); 108157249Seric } 108257249Seric else 108357249Seric { 108457249Seric for (ep = ErrorCodes; ep->ec_name != NULL; ep++) 108557249Seric if (strcasecmp(ep->ec_name, *tv) == 0) 108657249Seric break; 108757249Seric setstat(ep->ec_code); 108857249Seric } 108910183Seric tv++; 109010183Seric } 109158050Seric if ((**tv & 0377) != CANONUSER) 109258151Seric syserr("554 buildaddr: error: no user"); 109358082Seric cataddr(++tv, buf, sizeof buf, ' '); 109458082Seric stripquotes(buf); 10954279Seric usrerr(buf); 10964279Seric return (NULL); 10974279Seric } 109857402Seric 10994598Seric for (mp = Mailer; (m = *mp++) != NULL; ) 11003149Seric { 110133725Sbostic if (!strcasecmp(m->m_name, *tv)) 11023149Seric break; 11033149Seric } 11043149Seric if (m == NULL) 11054279Seric { 110658151Seric syserr("554 buildaddr: unknown mailer %s", *tv); 11074279Seric return (NULL); 11084279Seric } 11094598Seric a->q_mailer = m; 11103149Seric 11113149Seric /* figure out what host (if any) */ 111256678Seric tv++; 111358139Seric if (!bitnset(M_LOCALMAILER, m->m_flags)) 11143149Seric { 111558050Seric if ((**tv & 0377) != CANONHOST) 11164279Seric { 111758151Seric syserr("554 buildaddr: no host"); 11184279Seric return (NULL); 11194279Seric } 112058008Seric bp = buf; 112158008Seric spaceleft = sizeof buf - 1; 112258050Seric while (*++tv != NULL && (**tv & 0377) != CANONUSER) 112358008Seric { 112458008Seric int i = strlen(*tv); 112558008Seric 112658008Seric if (i > spaceleft) 112758008Seric { 112858008Seric /* out of space for this address */ 112958008Seric if (spaceleft >= 0) 113058151Seric syserr("554 buildaddr: host too long (%.40s...)", 113158008Seric buf); 113258008Seric i = spaceleft; 113358008Seric spaceleft = 0; 113458008Seric } 113558008Seric if (i <= 0) 113658008Seric continue; 113758008Seric bcopy(*tv, bp, i); 113858008Seric bp += i; 113958008Seric spaceleft -= i; 114058008Seric } 114158010Seric *bp = '\0'; 11425704Seric a->q_host = newstr(buf); 11433149Seric } 114457249Seric else 114557249Seric a->q_host = NULL; 11463149Seric 11473149Seric /* figure out the user */ 114858050Seric if (*tv == NULL || (**tv & 0377) != CANONUSER) 11494279Seric { 115058151Seric syserr("554 buildaddr: no user"); 11514279Seric return (NULL); 11524279Seric } 115357402Seric tv++; 115451317Seric 115557402Seric /* do special mapping for local mailer */ 115657402Seric if (m == LocalMailer && *tv != NULL) 115757402Seric { 115857454Seric register char *p = *tv; 115957454Seric 116057454Seric if (*p == '"') 116157454Seric p++; 116257454Seric if (*p == '|') 116357402Seric a->q_mailer = m = ProgMailer; 116457454Seric else if (*p == '/') 116557402Seric a->q_mailer = m = FileMailer; 116657454Seric else if (*p == ':') 116757402Seric { 116857402Seric /* may be :include: */ 116958082Seric cataddr(tv, buf, sizeof buf, '\0'); 117058008Seric stripquotes(buf); 117158008Seric if (strncasecmp(buf, ":include:", 9) == 0) 117258008Seric { 117358008Seric /* if :include:, don't need further rewriting */ 117457402Seric a->q_mailer = m = InclMailer; 117558008Seric a->q_user = &buf[9]; 117658008Seric return (a); 117758008Seric } 117857402Seric } 117957402Seric } 118057402Seric 118158008Seric if (m == LocalMailer && *tv != NULL && strcmp(*tv, "@") == 0) 118258008Seric { 118358008Seric tv++; 118458008Seric a->q_flags |= QNOTREMOTE; 118558008Seric } 118658008Seric 118719040Seric /* rewrite according recipient mailer rewriting rules */ 118857402Seric rewrite(tv, 2); 118958020Seric if (m->m_re_rwset > 0) 119058020Seric rewrite(tv, m->m_re_rwset); 119119040Seric rewrite(tv, 4); 119219040Seric 119319040Seric /* save the result for the command line/RCPT argument */ 119458082Seric cataddr(tv, buf, sizeof buf, '\0'); 11953149Seric a->q_user = buf; 11963149Seric 11973149Seric return (a); 11983149Seric } 11993188Seric /* 12004228Seric ** CATADDR -- concatenate pieces of addresses (putting in <LWSP> subs) 12014228Seric ** 12024228Seric ** Parameters: 12034228Seric ** pvp -- parameter vector to rebuild. 12044228Seric ** buf -- buffer to build the string into. 12054228Seric ** sz -- size of buf. 120658082Seric ** spacesub -- the space separator character; if null, 120758082Seric ** use SpaceSub. 12084228Seric ** 12094228Seric ** Returns: 12104228Seric ** none. 12114228Seric ** 12124228Seric ** Side Effects: 12134228Seric ** Destroys buf. 12144228Seric */ 12154228Seric 121658082Seric cataddr(pvp, buf, sz, spacesub) 12174228Seric char **pvp; 12184228Seric char *buf; 12194228Seric register int sz; 122058082Seric char spacesub; 12214228Seric { 12224228Seric bool oatomtok = FALSE; 122356678Seric bool natomtok = FALSE; 12244228Seric register int i; 12254228Seric register char *p; 12264228Seric 122758082Seric if (spacesub == '\0') 122858082Seric spacesub = SpaceSub; 122958082Seric 12308423Seric if (pvp == NULL) 12318423Seric { 123223109Seric (void) strcpy(buf, ""); 12338423Seric return; 12348423Seric } 12354228Seric p = buf; 123611156Seric sz -= 2; 12374228Seric while (*pvp != NULL && (i = strlen(*pvp)) < sz) 12384228Seric { 12398078Seric natomtok = (toktype(**pvp) == ATM); 12404228Seric if (oatomtok && natomtok) 124158082Seric *p++ = spacesub; 12424228Seric (void) strcpy(p, *pvp); 12434228Seric oatomtok = natomtok; 12444228Seric p += i; 124511156Seric sz -= i + 1; 12464228Seric pvp++; 12474228Seric } 12484228Seric *p = '\0'; 12494228Seric } 12504228Seric /* 12513188Seric ** SAMEADDR -- Determine if two addresses are the same 12523188Seric ** 12533188Seric ** This is not just a straight comparison -- if the mailer doesn't 12543188Seric ** care about the host we just ignore it, etc. 12553188Seric ** 12563188Seric ** Parameters: 12573188Seric ** a, b -- pointers to the internal forms to compare. 12583188Seric ** 12593188Seric ** Returns: 12603188Seric ** TRUE -- they represent the same mailbox. 12613188Seric ** FALSE -- they don't. 12623188Seric ** 12633188Seric ** Side Effects: 12643188Seric ** none. 12653188Seric */ 12663188Seric 12673188Seric bool 12689374Seric sameaddr(a, b) 12693188Seric register ADDRESS *a; 12703188Seric register ADDRESS *b; 12713188Seric { 12723188Seric /* if they don't have the same mailer, forget it */ 12733188Seric if (a->q_mailer != b->q_mailer) 12743188Seric return (FALSE); 12753188Seric 12763188Seric /* if the user isn't the same, we can drop out */ 127756678Seric if (strcmp(a->q_user, b->q_user) != 0) 12783188Seric return (FALSE); 12793188Seric 12803188Seric /* if the mailer ignores hosts, we have succeeded! */ 128158139Seric if (bitnset(M_LOCALMAILER, a->q_mailer->m_flags)) 12823188Seric return (TRUE); 12833188Seric 12843188Seric /* otherwise compare hosts (but be careful for NULL ptrs) */ 12853188Seric if (a->q_host == NULL || b->q_host == NULL) 12863188Seric return (FALSE); 128756678Seric if (strcmp(a->q_host, b->q_host) != 0) 12883188Seric return (FALSE); 12893188Seric 12903188Seric return (TRUE); 12913188Seric } 12923234Seric /* 12933234Seric ** PRINTADDR -- print address (for debugging) 12943234Seric ** 12953234Seric ** Parameters: 12963234Seric ** a -- the address to print 12973234Seric ** follow -- follow the q_next chain. 12983234Seric ** 12993234Seric ** Returns: 13003234Seric ** none. 13013234Seric ** 13023234Seric ** Side Effects: 13033234Seric ** none. 13043234Seric */ 13053234Seric 13063234Seric printaddr(a, follow) 13073234Seric register ADDRESS *a; 13083234Seric bool follow; 13093234Seric { 13105001Seric bool first = TRUE; 131157731Seric register MAILER *m; 131257731Seric MAILER pseudomailer; 13135001Seric 13143234Seric while (a != NULL) 13153234Seric { 13165001Seric first = FALSE; 13174443Seric printf("%x=", a); 13184085Seric (void) fflush(stdout); 131957731Seric 132057731Seric /* find the mailer -- carefully */ 132157731Seric m = a->q_mailer; 132257731Seric if (m == NULL) 132357731Seric { 132457731Seric m = &pseudomailer; 132557731Seric m->m_mno = -1; 132657731Seric m->m_name = "NULL"; 132757731Seric } 132857731Seric 132940973Sbostic printf("%s: mailer %d (%s), host `%s', user `%s', ruser `%s'\n", 133057731Seric a->q_paddr, m->m_mno, m->m_name, 133140973Sbostic a->q_host, a->q_user, a->q_ruser? a->q_ruser: "<null>"); 13328181Seric printf("\tnext=%x, flags=%o, alias %x\n", a->q_next, a->q_flags, 13338181Seric a->q_alias); 13348181Seric printf("\thome=\"%s\", fullname=\"%s\"\n", a->q_home, 13358181Seric a->q_fullname); 13364996Seric 13373234Seric if (!follow) 13383234Seric return; 13394996Seric a = a->q_next; 13403234Seric } 13415001Seric if (first) 13424443Seric printf("[NULL]\n"); 13433234Seric } 13444317Seric 13457682Seric /* 13467682Seric ** REMOTENAME -- return the name relative to the current mailer 13477682Seric ** 13487682Seric ** Parameters: 13497682Seric ** name -- the name to translate. 13508069Seric ** m -- the mailer that we want to do rewriting relative 13518069Seric ** to. 13528069Seric ** senderaddress -- if set, uses the sender rewriting rules 13538069Seric ** rather than the recipient rewriting rules. 135458020Seric ** header -- set if this address is in the header, rather 135558020Seric ** than an envelope header. 135610310Seric ** canonical -- if set, strip out any comment information, 135710310Seric ** etc. 135858020Seric ** e -- the current envelope. 13597682Seric ** 13607682Seric ** Returns: 13617682Seric ** the text string representing this address relative to 13627682Seric ** the receiving mailer. 13637682Seric ** 13647682Seric ** Side Effects: 13657682Seric ** none. 13667682Seric ** 13677682Seric ** Warnings: 13687682Seric ** The text string returned is tucked away locally; 13697682Seric ** copy it if you intend to save it. 13707682Seric */ 13717682Seric 13727682Seric char * 137358020Seric remotename(name, m, senderaddress, header, canonical, e) 13747682Seric char *name; 137556678Seric struct mailer *m; 13768069Seric bool senderaddress; 137758020Seric bool header; 137810310Seric bool canonical; 137956678Seric register ENVELOPE *e; 13807682Seric { 13818069Seric register char **pvp; 13828069Seric char *fancy; 138356678Seric extern char *macvalue(); 138456678Seric char *oldg = macvalue('g', e); 138558020Seric int rwset; 13867682Seric static char buf[MAXNAME]; 13877682Seric char lbuf[MAXNAME]; 138816914Seric char pvpbuf[PSBUFSIZE]; 138956678Seric extern char **prescan(); 139056678Seric extern char *crackaddr(); 13917682Seric 13927755Seric if (tTd(12, 1)) 13937755Seric printf("remotename(%s)\n", name); 13947755Seric 139510177Seric /* don't do anything if we are tagging it as special */ 139658020Seric if (senderaddress) 139758020Seric rwset = header ? m->m_sh_rwset : m->m_se_rwset; 139858020Seric else 139958020Seric rwset = header ? m->m_rh_rwset : m->m_re_rwset; 140058020Seric if (rwset < 0) 140110177Seric return (name); 140210177Seric 14037682Seric /* 14048181Seric ** Do a heuristic crack of this name to extract any comment info. 14058181Seric ** This will leave the name as a comment and a $g macro. 14067889Seric */ 14077889Seric 140858036Seric if (canonical || bitnset(M_NOCOMMENT, m->m_flags)) 140958050Seric fancy = "\201g"; 141010310Seric else 141110310Seric fancy = crackaddr(name); 14127889Seric 14138181Seric /* 14148181Seric ** Turn the name into canonical form. 14158181Seric ** Normally this will be RFC 822 style, i.e., "user@domain". 14168181Seric ** If this only resolves to "user", and the "C" flag is 14178181Seric ** specified in the sending mailer, then the sender's 14188181Seric ** domain will be appended. 14198181Seric */ 14208181Seric 142116914Seric pvp = prescan(name, '\0', pvpbuf); 14227889Seric if (pvp == NULL) 14237889Seric return (name); 14248181Seric rewrite(pvp, 3); 142556678Seric if (e->e_fromdomain != NULL) 14268181Seric { 14278181Seric /* append from domain to this address */ 14288181Seric register char **pxp = pvp; 14298181Seric 14309594Seric /* see if there is an "@domain" in the current name */ 14318181Seric while (*pxp != NULL && strcmp(*pxp, "@") != 0) 14328181Seric pxp++; 14338181Seric if (*pxp == NULL) 14348181Seric { 14359594Seric /* no.... append the "@domain" from the sender */ 143656678Seric register char **qxq = e->e_fromdomain; 14378181Seric 14389594Seric while ((*pxp++ = *qxq++) != NULL) 14399594Seric continue; 144011726Seric rewrite(pvp, 3); 14418181Seric } 14428181Seric } 14438181Seric 14448181Seric /* 14458959Seric ** Do more specific rewriting. 144656678Seric ** Rewrite using ruleset 1 or 2 depending on whether this is 144756678Seric ** a sender address or not. 14488181Seric ** Then run it through any receiving-mailer-specific rulesets. 14498181Seric */ 14508181Seric 14518069Seric if (senderaddress) 145256327Seric rewrite(pvp, 1); 14538069Seric else 145456327Seric rewrite(pvp, 2); 145558020Seric if (rwset > 0) 145658020Seric rewrite(pvp, rwset); 14577682Seric 14588181Seric /* 14598959Seric ** Do any final sanitation the address may require. 14608959Seric ** This will normally be used to turn internal forms 14618959Seric ** (e.g., user@host.LOCAL) into external form. This 14628959Seric ** may be used as a default to the above rules. 14638959Seric */ 14648959Seric 14658959Seric rewrite(pvp, 4); 14668959Seric 14678959Seric /* 14688181Seric ** Now restore the comment information we had at the beginning. 14698181Seric */ 14708181Seric 147158082Seric cataddr(pvp, lbuf, sizeof lbuf, '\0'); 147256678Seric define('g', lbuf, e); 147356678Seric expand(fancy, buf, &buf[sizeof buf - 1], e); 147456678Seric define('g', oldg, e); 14757682Seric 14767682Seric if (tTd(12, 1)) 14777755Seric printf("remotename => `%s'\n", buf); 14787682Seric return (buf); 14797682Seric } 148051317Seric /* 148156678Seric ** MAPLOCALUSER -- run local username through ruleset 5 for final redirection 148251317Seric ** 148351317Seric ** Parameters: 148456678Seric ** a -- the address to map (but just the user name part). 148556678Seric ** sendq -- the sendq in which to install any replacement 148656678Seric ** addresses. 148751317Seric ** 148851317Seric ** Returns: 148951317Seric ** none. 149051317Seric */ 149151317Seric 149256678Seric maplocaluser(a, sendq, e) 149356678Seric register ADDRESS *a; 149456678Seric ADDRESS **sendq; 149556678Seric ENVELOPE *e; 149651317Seric { 149756678Seric register char **pvp; 149856678Seric register ADDRESS *a1 = NULL; 149956678Seric char pvpbuf[PSBUFSIZE]; 150051317Seric 150156678Seric if (tTd(29, 1)) 150256678Seric { 150356678Seric printf("maplocaluser: "); 150456678Seric printaddr(a, FALSE); 150556678Seric } 150656678Seric pvp = prescan(a->q_user, '\0', pvpbuf); 150756678Seric if (pvp == NULL) 150856678Seric return; 150951317Seric 151056678Seric rewrite(pvp, 5); 151158050Seric if (pvp[0] == NULL || (pvp[0][0] & 0377) != CANONNET) 151256678Seric return; 151351317Seric 151456678Seric /* if non-null, mailer destination specified -- has it changed? */ 151556678Seric a1 = buildaddr(pvp, NULL); 151656678Seric if (a1 == NULL || sameaddr(a, a1)) 151756678Seric return; 151851317Seric 151956678Seric /* mark old address as dead; insert new address */ 152056678Seric a->q_flags |= QDONTSEND; 152157731Seric if (tTd(29, 5)) 152257731Seric { 152357731Seric printf("maplocaluser: QDONTSEND "); 152457731Seric printaddr(a, FALSE); 152557731Seric } 152656678Seric a1->q_alias = a; 152756678Seric allocaddr(a1, 1, NULL); 152856678Seric (void) recipient(a1, sendq, e); 152951317Seric } 1530