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*58827Seric static char sccsid[] = "@(#)parseaddr.c 6.34 (Berkeley) 03/26/93"; 1133730Sbostic #endif /* not lint */ 1222976Smiriam 1356678Seric # include "sendmail.h" 14297Seric 15297Seric /* 169888Seric ** PARSEADDR -- Parse an address 17297Seric ** 18297Seric ** Parses an address and breaks it up into three parts: a 19297Seric ** net to transmit the message on, the host to transmit it 20297Seric ** to, and a user on that host. These are loaded into an 212973Seric ** ADDRESS header with the values squirreled away if necessary. 22297Seric ** The "user" part may not be a real user; the process may 23297Seric ** just reoccur on that machine. For example, on a machine 24297Seric ** with an arpanet connection, the address 25297Seric ** csvax.bill@berkeley 26297Seric ** will break up to a "user" of 'csvax.bill' and a host 27297Seric ** of 'berkeley' -- to be transmitted over the arpanet. 28297Seric ** 29297Seric ** Parameters: 30297Seric ** addr -- the address to parse. 31297Seric ** a -- a pointer to the address descriptor buffer. 32297Seric ** If NULL, a header will be created. 33297Seric ** copyf -- determines what shall be copied: 34297Seric ** -1 -- don't copy anything. The printname 35297Seric ** (q_paddr) is just addr, and the 36297Seric ** user & host are allocated internally 37297Seric ** to parse. 38297Seric ** 0 -- copy out the parsed user & host, but 39297Seric ** don't copy the printname. 40297Seric ** +1 -- copy everything. 4111445Seric ** delim -- the character to terminate the address, passed 4211445Seric ** to prescan. 4358333Seric ** delimptr -- if non-NULL, set to the location of the 4458333Seric ** delim character that was found. 4556678Seric ** e -- the envelope that will contain this address. 46297Seric ** 47297Seric ** Returns: 48297Seric ** A pointer to the address descriptor header (`a' if 49297Seric ** `a' is non-NULL). 50297Seric ** NULL on error. 51297Seric ** 52297Seric ** Side Effects: 53297Seric ** none 54297Seric */ 55297Seric 569374Seric /* following delimiters are inherent to the internal algorithms */ 5758050Seric # define DELIMCHARS "\201()<>,;\\\"\r\n" /* word delimiters */ 582091Seric 592973Seric ADDRESS * 6058333Seric parseaddr(addr, a, copyf, delim, delimptr, e) 61297Seric char *addr; 622973Seric register ADDRESS *a; 63297Seric int copyf; 6411445Seric char delim; 6558333Seric char **delimptr; 6656678Seric register ENVELOPE *e; 67297Seric { 683149Seric register char **pvp; 6958333Seric auto char *delimptrbuf; 7016914Seric char pvpbuf[PSBUFSIZE]; 7156678Seric extern char **prescan(); 7256678Seric extern ADDRESS *buildaddr(); 7357388Seric extern bool invalidaddr(); 74297Seric 75297Seric /* 76297Seric ** Initialize and prescan address. 77297Seric */ 78297Seric 7956678Seric e->e_to = addr; 807675Seric if (tTd(20, 1)) 819888Seric printf("\n--parseaddr(%s)\n", addr); 823188Seric 8357388Seric if (invalidaddr(addr)) 8457388Seric { 8557388Seric if (tTd(20, 1)) 8657388Seric printf("parseaddr-->bad address\n"); 8757388Seric return NULL; 8857388Seric } 8957388Seric 9058333Seric if (delimptr == NULL) 9158333Seric delimptr = &delimptrbuf; 9258333Seric 9358333Seric pvp = prescan(addr, delim, pvpbuf, delimptr); 943149Seric if (pvp == NULL) 9556729Seric { 9656729Seric if (tTd(20, 1)) 9756729Seric printf("parseaddr-->NULL\n"); 98297Seric return (NULL); 9956729Seric } 100297Seric 101297Seric /* 1023149Seric ** Apply rewriting rules. 1037889Seric ** Ruleset 0 does basic parsing. It must resolve. 104297Seric */ 105297Seric 1068181Seric rewrite(pvp, 3); 1074070Seric rewrite(pvp, 0); 108297Seric 1093149Seric /* 1103149Seric ** See if we resolved to a real mailer. 1113149Seric */ 112297Seric 11358050Seric if ((pvp[0][0] & 0377) != CANONNET) 1143149Seric { 1153149Seric setstat(EX_USAGE); 11658151Seric syserr("554 cannot resolve name"); 1173149Seric return (NULL); 118297Seric } 119297Seric 120297Seric /* 1213149Seric ** Build canonical address from pvp. 122297Seric */ 123297Seric 1243149Seric a = buildaddr(pvp, a); 1254279Seric if (a == NULL) 1264279Seric return (NULL); 127297Seric 128297Seric /* 1293149Seric ** Make local copies of the host & user and then 1303149Seric ** transport them out. 131297Seric */ 132297Seric 13358333Seric allocaddr(a, copyf, addr, *delimptr); 13456678Seric 13556678Seric /* 13656678Seric ** Compute return value. 13756678Seric */ 13856678Seric 13956678Seric if (tTd(20, 1)) 1408078Seric { 14156678Seric printf("parseaddr-->"); 14256678Seric printaddr(a, FALSE); 14356678Seric } 14456678Seric 14556678Seric return (a); 14656678Seric } 14756678Seric /* 14857388Seric ** INVALIDADDR -- check for address containing meta-characters 14957388Seric ** 15057388Seric ** Parameters: 15157388Seric ** addr -- the address to check. 15257388Seric ** 15357388Seric ** Returns: 15457388Seric ** TRUE -- if the address has any "wierd" characters 15557388Seric ** FALSE -- otherwise. 15657388Seric */ 15757388Seric 15857388Seric bool 15957388Seric invalidaddr(addr) 16057388Seric register char *addr; 16157388Seric { 16257388Seric for (; *addr != '\0'; addr++) 16357388Seric { 16458050Seric if ((*addr & 0340) != 0200) 16557388Seric continue; 16657388Seric setstat(EX_USAGE); 16758151Seric usrerr("553 Address contained invalid control characters"); 16857388Seric return TRUE; 16957388Seric } 17057388Seric return FALSE; 17157388Seric } 17257388Seric /* 17356678Seric ** ALLOCADDR -- do local allocations of address on demand. 17456678Seric ** 17556678Seric ** Also lowercases the host name if requested. 17656678Seric ** 17756678Seric ** Parameters: 17856678Seric ** a -- the address to reallocate. 17956678Seric ** copyf -- the copy flag (see parseaddr for description). 18056678Seric ** paddr -- the printname of the address. 18158333Seric ** delimptr -- a pointer to the address delimiter. Must be set. 18256678Seric ** 18356678Seric ** Returns: 18456678Seric ** none. 18556678Seric ** 18656678Seric ** Side Effects: 18756678Seric ** Copies portions of a into local buffers as requested. 18856678Seric */ 18956678Seric 19058333Seric allocaddr(a, copyf, paddr, delimptr) 19156678Seric register ADDRESS *a; 19256678Seric int copyf; 19356678Seric char *paddr; 19458333Seric char *delimptr; 19556678Seric { 19656678Seric register MAILER *m = a->q_mailer; 19756678Seric 19858673Seric if (tTd(24, 4)) 19958673Seric printf("allocaddr(copyf=%d, paddr=%s)\n", copyf, paddr); 20058673Seric 20156678Seric if (copyf > 0 && paddr != NULL) 20256678Seric { 20358333Seric char savec = *delimptr; 2048078Seric 20558333Seric *delimptr = '\0'; 20656678Seric a->q_paddr = newstr(paddr); 20758333Seric *delimptr = savec; 2088078Seric } 209297Seric else 21056678Seric a->q_paddr = paddr; 21124944Seric 21224944Seric if (a->q_user == NULL) 21324944Seric a->q_user = ""; 21424944Seric if (a->q_host == NULL) 21524944Seric a->q_host = ""; 21624944Seric 2173149Seric if (copyf >= 0) 218297Seric { 21924944Seric a->q_host = newstr(a->q_host); 2203149Seric if (a->q_user != a->q_paddr) 2213149Seric a->q_user = newstr(a->q_user); 222297Seric } 223297Seric 22456678Seric if (a->q_paddr == NULL) 22556678Seric a->q_paddr = a->q_user; 226297Seric } 227297Seric /* 228297Seric ** PRESCAN -- Prescan name and make it canonical 229297Seric ** 2309374Seric ** Scans a name and turns it into a set of tokens. This process 2319374Seric ** deletes blanks and comments (in parentheses). 232297Seric ** 233297Seric ** This routine knows about quoted strings and angle brackets. 234297Seric ** 235297Seric ** There are certain subtleties to this routine. The one that 236297Seric ** comes to mind now is that backslashes on the ends of names 237297Seric ** are silently stripped off; this is intentional. The problem 238297Seric ** is that some versions of sndmsg (like at LBL) set the kill 239297Seric ** character to something other than @ when reading addresses; 240297Seric ** so people type "csvax.eric\@berkeley" -- which screws up the 241297Seric ** berknet mailer. 242297Seric ** 243297Seric ** Parameters: 244297Seric ** addr -- the name to chomp. 245297Seric ** delim -- the delimiter for the address, normally 246297Seric ** '\0' or ','; \0 is accepted in any case. 24715284Seric ** If '\t' then we are reading the .cf file. 24816914Seric ** pvpbuf -- place to put the saved text -- note that 24916914Seric ** the pointers are static. 25058333Seric ** delimptr -- if non-NULL, set to the location of the 25158333Seric ** terminating delimiter. 252297Seric ** 253297Seric ** Returns: 2543149Seric ** A pointer to a vector of tokens. 255297Seric ** NULL on error. 256297Seric */ 257297Seric 2588078Seric /* states and character types */ 2598078Seric # define OPR 0 /* operator */ 2608078Seric # define ATM 1 /* atom */ 2618078Seric # define QST 2 /* in quoted string */ 2628078Seric # define SPC 3 /* chewing up spaces */ 2638078Seric # define ONE 4 /* pick up one character */ 2643149Seric 2658078Seric # define NSTATES 5 /* number of states */ 2668078Seric # define TYPE 017 /* mask to select state type */ 2678078Seric 2688078Seric /* meta bits for table */ 2698078Seric # define M 020 /* meta character; don't pass through */ 2708078Seric # define B 040 /* cause a break */ 2718078Seric # define MB M|B /* meta-break */ 2728078Seric 2738078Seric static short StateTab[NSTATES][NSTATES] = 2748078Seric { 2758087Seric /* oldst chtype> OPR ATM QST SPC ONE */ 2769051Seric /*OPR*/ OPR|B, ATM|B, QST|B, SPC|MB, ONE|B, 2779051Seric /*ATM*/ OPR|B, ATM, QST|B, SPC|MB, ONE|B, 2789051Seric /*QST*/ QST, QST, OPR, QST, QST, 2798078Seric /*SPC*/ OPR, ATM, QST, SPC|M, ONE, 2808078Seric /*ONE*/ OPR, OPR, OPR, OPR, OPR, 2818078Seric }; 2828078Seric 2838078Seric # define NOCHAR -1 /* signal nothing in lookahead token */ 2848078Seric 2853149Seric char ** 28658333Seric prescan(addr, delim, pvpbuf, delimptr) 287297Seric char *addr; 288297Seric char delim; 28916914Seric char pvpbuf[]; 29058333Seric char **delimptr; 291297Seric { 292297Seric register char *p; 2938078Seric register char *q; 2949346Seric register int c; 2953149Seric char **avp; 296297Seric bool bslashmode; 297297Seric int cmntcnt; 2988423Seric int anglecnt; 2993149Seric char *tok; 3008078Seric int state; 3018078Seric int newstate; 3028078Seric static char *av[MAXATOM+1]; 30356678Seric extern int errno; 304297Seric 30515253Seric /* make sure error messages don't have garbage on them */ 30615253Seric errno = 0; 30715253Seric 30816914Seric q = pvpbuf; 3093149Seric bslashmode = FALSE; 3107800Seric cmntcnt = 0; 3118423Seric anglecnt = 0; 3123149Seric avp = av; 31356678Seric state = ATM; 3148078Seric c = NOCHAR; 3158078Seric p = addr; 31656764Seric if (tTd(22, 11)) 317297Seric { 3188078Seric printf("prescan: "); 3198078Seric xputs(p); 32023109Seric (void) putchar('\n'); 3218078Seric } 3228078Seric 3238078Seric do 3248078Seric { 3253149Seric /* read a token */ 3263149Seric tok = q; 3278078Seric for (;;) 328297Seric { 3298078Seric /* store away any old lookahead character */ 3308078Seric if (c != NOCHAR) 3318078Seric { 33215284Seric /* see if there is room */ 33316914Seric if (q >= &pvpbuf[PSBUFSIZE - 5]) 3348078Seric { 33558151Seric usrerr("553 Address too long"); 33658333Seric if (delimptr != NULL) 33758333Seric *delimptr = p; 3388078Seric return (NULL); 3398078Seric } 34015284Seric 34115284Seric /* squirrel it away */ 3428078Seric *q++ = c; 3438078Seric } 3448078Seric 3458078Seric /* read a new input character */ 3468078Seric c = *p++; 34757631Seric if (c == '\0') 34856764Seric { 34956764Seric /* diagnose and patch up bad syntax */ 35056764Seric if (state == QST) 35156764Seric { 35258151Seric usrerr("553 Unbalanced '\"'"); 35356764Seric c = '"'; 35456764Seric } 35556764Seric else if (cmntcnt > 0) 35656764Seric { 35758151Seric usrerr("553 Unbalanced '('"); 35856764Seric c = ')'; 35956764Seric } 36056764Seric else if (anglecnt > 0) 36156764Seric { 36256764Seric c = '>'; 36358151Seric usrerr("553 Unbalanced '<'"); 36456764Seric } 36556764Seric else 36656764Seric break; 36715284Seric 36856764Seric p--; 36956764Seric } 37057631Seric else if (c == delim && anglecnt <= 0 && 37157631Seric cmntcnt <= 0 && state != QST) 37257631Seric break; 37356764Seric 3748078Seric if (tTd(22, 101)) 3758078Seric printf("c=%c, s=%d; ", c, state); 3768078Seric 3773149Seric /* chew up special characters */ 3783149Seric *q = '\0'; 3793149Seric if (bslashmode) 3803149Seric { 38124944Seric /* kludge \! for naive users */ 38258061Seric if (cmntcnt > 0) 38358061Seric c = NOCHAR; 38458061Seric else if (c != '!') 38556678Seric *q++ = '\\'; 3863149Seric bslashmode = FALSE; 38756678Seric continue; 3883149Seric } 38956678Seric 39056678Seric if (c == '\\') 3913149Seric { 3923149Seric bslashmode = TRUE; 3938078Seric c = NOCHAR; 39456678Seric continue; 3953149Seric } 39656678Seric else if (state == QST) 3978514Seric { 3988514Seric /* do nothing, just avoid next clauses */ 3998514Seric } 4008078Seric else if (c == '(') 4014100Seric { 4028078Seric cmntcnt++; 4038078Seric c = NOCHAR; 4044100Seric } 4058078Seric else if (c == ')') 4063149Seric { 4078078Seric if (cmntcnt <= 0) 4083149Seric { 40958151Seric usrerr("553 Unbalanced ')'"); 41058333Seric if (delimptr != NULL) 41158333Seric *delimptr = p; 4128078Seric return (NULL); 4133149Seric } 4148078Seric else 4158078Seric cmntcnt--; 4168078Seric } 4178078Seric else if (cmntcnt > 0) 4188078Seric c = NOCHAR; 4198423Seric else if (c == '<') 4208423Seric anglecnt++; 4218423Seric else if (c == '>') 4228423Seric { 4238423Seric if (anglecnt <= 0) 4248423Seric { 42558151Seric usrerr("553 Unbalanced '>'"); 42658333Seric if (delimptr != NULL) 42758333Seric *delimptr = p; 4288423Seric return (NULL); 4298423Seric } 4308423Seric anglecnt--; 4318423Seric } 43258050Seric else if (delim == ' ' && isascii(c) && isspace(c)) 43311423Seric c = ' '; 4343149Seric 4358078Seric if (c == NOCHAR) 4368078Seric continue; 4373149Seric 4388078Seric /* see if this is end of input */ 43911405Seric if (c == delim && anglecnt <= 0 && state != QST) 4403149Seric break; 4413149Seric 4428078Seric newstate = StateTab[state][toktype(c)]; 4438078Seric if (tTd(22, 101)) 4448078Seric printf("ns=%02o\n", newstate); 4458078Seric state = newstate & TYPE; 4468078Seric if (bitset(M, newstate)) 4478078Seric c = NOCHAR; 4488078Seric if (bitset(B, newstate)) 4494228Seric break; 450297Seric } 4513149Seric 4523149Seric /* new token */ 4538078Seric if (tok != q) 4541378Seric { 4558078Seric *q++ = '\0'; 4568078Seric if (tTd(22, 36)) 457297Seric { 4588078Seric printf("tok="); 4598078Seric xputs(tok); 46023109Seric (void) putchar('\n'); 461297Seric } 4628078Seric if (avp >= &av[MAXATOM]) 463297Seric { 46458151Seric syserr("553 prescan: too many tokens"); 46558333Seric if (delimptr != NULL) 46658333Seric *delimptr = p; 4678078Seric return (NULL); 468297Seric } 4698078Seric *avp++ = tok; 470297Seric } 4718423Seric } while (c != '\0' && (c != delim || anglecnt > 0)); 4723149Seric *avp = NULL; 47358333Seric p--; 47458333Seric if (delimptr != NULL) 47558333Seric *delimptr = p; 47656764Seric if (tTd(22, 12)) 47756764Seric { 47856764Seric printf("prescan==>"); 47956764Seric printav(av); 48056764Seric } 48158546Seric if (av[0] == NULL) 48258546Seric return (NULL); 48358403Seric return (av); 4843149Seric } 4853149Seric /* 4863149Seric ** TOKTYPE -- return token type 4873149Seric ** 4883149Seric ** Parameters: 4893149Seric ** c -- the character in question. 4903149Seric ** 4913149Seric ** Returns: 4923149Seric ** Its type. 4933149Seric ** 4943149Seric ** Side Effects: 4953149Seric ** none. 4963149Seric */ 497297Seric 4983149Seric toktype(c) 49958050Seric register int c; 5003149Seric { 5013380Seric static char buf[50]; 5023382Seric static bool firstime = TRUE; 5033380Seric 5043382Seric if (firstime) 5053380Seric { 5063382Seric firstime = FALSE; 50758050Seric expand("\201o", buf, &buf[sizeof buf - 1], CurEnv); 5087005Seric (void) strcat(buf, DELIMCHARS); 5093380Seric } 51058050Seric c &= 0377; 51156327Seric if (c == MATCHCLASS || c == MATCHREPL || c == MATCHNCLASS) 5128078Seric return (ONE); 5138078Seric if (c == '"') 5148078Seric return (QST); 51558050Seric if ((c & 0340) == 0200) 51658050Seric return (OPR); 5174100Seric if (!isascii(c)) 5188078Seric return (ATM); 5198078Seric if (isspace(c) || c == ')') 5208078Seric return (SPC); 52158050Seric if (strchr(buf, c) != NULL) 5228078Seric return (OPR); 5238078Seric return (ATM); 5243149Seric } 5253149Seric /* 5263149Seric ** REWRITE -- apply rewrite rules to token vector. 5273149Seric ** 5284476Seric ** This routine is an ordered production system. Each rewrite 5294476Seric ** rule has a LHS (called the pattern) and a RHS (called the 5304476Seric ** rewrite); 'rwr' points the the current rewrite rule. 5314476Seric ** 5324476Seric ** For each rewrite rule, 'avp' points the address vector we 5334476Seric ** are trying to match against, and 'pvp' points to the pattern. 5348058Seric ** If pvp points to a special match value (MATCHZANY, MATCHANY, 5359585Seric ** MATCHONE, MATCHCLASS, MATCHNCLASS) then the address in avp 5369585Seric ** matched is saved away in the match vector (pointed to by 'mvp'). 5374476Seric ** 5384476Seric ** When a match between avp & pvp does not match, we try to 5399585Seric ** back out. If we back up over MATCHONE, MATCHCLASS, or MATCHNCLASS 5404476Seric ** we must also back out the match in mvp. If we reach a 5418058Seric ** MATCHANY or MATCHZANY we just extend the match and start 5428058Seric ** over again. 5434476Seric ** 5444476Seric ** When we finally match, we rewrite the address vector 5454476Seric ** and try over again. 5464476Seric ** 5473149Seric ** Parameters: 5483149Seric ** pvp -- pointer to token vector. 5493149Seric ** 5503149Seric ** Returns: 5513149Seric ** none. 5523149Seric ** 5533149Seric ** Side Effects: 5543149Seric ** pvp is modified. 5553149Seric */ 5562091Seric 5573149Seric struct match 5583149Seric { 5594468Seric char **first; /* first token matched */ 5604468Seric char **last; /* last token matched */ 56158825Seric char **pattern; /* pointer to pattern */ 5623149Seric }; 5633149Seric 5644468Seric # define MAXMATCH 9 /* max params per rewrite */ 5653149Seric 5663149Seric 5674070Seric rewrite(pvp, ruleset) 5683149Seric char **pvp; 5694070Seric int ruleset; 5703149Seric { 5713149Seric register char *ap; /* address pointer */ 5723149Seric register char *rp; /* rewrite pointer */ 5733149Seric register char **avp; /* address vector pointer */ 5743149Seric register char **rvp; /* rewrite vector pointer */ 5758058Seric register struct match *mlp; /* cur ptr into mlist */ 5768058Seric register struct rewrite *rwr; /* pointer to current rewrite rule */ 57756678Seric struct match mlist[MAXMATCH]; /* stores match on LHS */ 5783149Seric char *npvp[MAXATOM+1]; /* temporary space for rebuild */ 5793149Seric 5809279Seric if (OpMode == MD_TEST || tTd(21, 2)) 5813149Seric { 5828959Seric printf("rewrite: ruleset %2d input:", ruleset); 58356678Seric printav(pvp); 5843149Seric } 58556678Seric if (ruleset < 0 || ruleset >= MAXRWSETS) 58656326Seric { 58758151Seric syserr("554 rewrite: illegal ruleset number %d", ruleset); 58856326Seric return; 58956326Seric } 59056678Seric if (pvp == NULL) 59156678Seric return; 59256326Seric 5933149Seric /* 59456678Seric ** Run through the list of rewrite rules, applying 59556678Seric ** any that match. 5963149Seric */ 5973149Seric 5984070Seric for (rwr = RewriteRules[ruleset]; rwr != NULL; ) 5993149Seric { 60056678Seric int loopcount = 0; 60156678Seric 6027675Seric if (tTd(21, 12)) 603297Seric { 6048069Seric printf("-----trying rule:"); 60556678Seric printav(rwr->r_lhs); 6063149Seric } 6073149Seric 6083149Seric /* try to match on this rule */ 6094468Seric mlp = mlist; 6108058Seric rvp = rwr->r_lhs; 6118058Seric avp = pvp; 6128058Seric while ((ap = *avp) != NULL || *rvp != NULL) 6133149Seric { 61456678Seric if (++loopcount > 100) 61552637Seric { 61658151Seric syserr("554 Infinite loop in ruleset %d", ruleset); 61756678Seric printf("workspace: "); 61856678Seric printav(pvp); 61952637Seric break; 62052637Seric } 6213149Seric rp = *rvp; 6228058Seric if (tTd(21, 35)) 6238058Seric { 62458825Seric printf("ADVANCE rp="); 62557531Seric xputs(rp); 62657532Seric printf(", ap="); 6278058Seric xputs(ap); 6288069Seric printf("\n"); 6298058Seric } 63056678Seric if (rp == NULL) 63156326Seric { 6323149Seric /* end-of-pattern before end-of-address */ 6338058Seric goto backup; 63456678Seric } 63558173Seric if (ap == NULL && (*rp & 0377) != MATCHZANY && 636*58827Seric (*rp & 0377) != MATCHZERO) 63756326Seric { 63858825Seric /* end-of-input with patterns left */ 63958825Seric goto backup; 640297Seric } 64156326Seric 64258050Seric switch (*rp & 0377) 6438058Seric { 64456678Seric register STAB *s; 64558814Seric char buf[MAXLINE]; 64656326Seric 64756678Seric case MATCHCLASS: 64858825Seric /* match any phrase in a class */ 64958825Seric mlp->pattern = rvp; 65058814Seric mlp->first = avp; 65158814Seric extendclass: 65258825Seric ap = *avp; 65358825Seric if (ap == NULL) 65458814Seric goto backup; 65558814Seric mlp->last = avp++; 65658814Seric cataddr(mlp->first, mlp->last, buf, sizeof buf, '\0'); 65758814Seric s = stab(buf, ST_CLASS, ST_FIND); 65856678Seric if (s == NULL || !bitnset(rp[1], s->s_class)) 65956326Seric { 66058825Seric if (tTd(21, 36)) 66158825Seric { 66258825Seric printf("EXTEND rp="); 66358825Seric xputs(rp); 66458825Seric printf(", ap="); 66558825Seric xputs(ap); 66658825Seric printf("\n"); 66758825Seric } 66858825Seric goto extendclass; 66956326Seric } 67058825Seric if (tTd(21, 36)) 67158825Seric printf("CLMATCH\n"); 67258814Seric mlp++; 67358814Seric break; 6744060Seric 67558825Seric case MATCHNCLASS: 67658825Seric /* match any token not in a class */ 67758825Seric s = stab(ap, ST_CLASS, ST_FIND); 67858825Seric if (s != NULL && bitnset(rp[1], s->s_class)) 67958825Seric goto backup; 68058825Seric 68158825Seric /* fall through */ 68258825Seric 68356678Seric case MATCHONE: 68456678Seric case MATCHANY: 68556678Seric /* match exactly one token */ 68658825Seric mlp->pattern = rvp; 68756678Seric mlp->first = avp; 68856678Seric mlp->last = avp++; 6898058Seric mlp++; 69056678Seric break; 6918058Seric 69256678Seric case MATCHZANY: 69356678Seric /* match zero or more tokens */ 69458825Seric mlp->pattern = rvp; 69556678Seric mlp->first = avp; 69656678Seric mlp->last = avp - 1; 69756678Seric mlp++; 69856678Seric break; 69956326Seric 700*58827Seric case MATCHZERO: 70158173Seric /* match zero tokens */ 70258173Seric break; 70358173Seric 70456678Seric default: 70556678Seric /* must have exact match */ 70656678Seric if (strcasecmp(rp, ap)) 7078058Seric goto backup; 7084468Seric avp++; 70956678Seric break; 7103149Seric } 7113149Seric 71256678Seric /* successful match on this token */ 7133149Seric rvp++; 7143149Seric continue; 7153149Seric 71658825Seric backup: 71756678Seric /* match failed -- back up */ 71858825Seric while (--mlp >= mlist) 7193149Seric { 72058825Seric rvp = mlp->pattern; 72156678Seric rp = *rvp; 72258825Seric avp = mlp->last + 1; 72358825Seric ap = *avp; 72458825Seric 72558825Seric if (tTd(21, 36)) 72658825Seric { 72758825Seric printf("BACKUP rp="); 72858825Seric xputs(rp); 72958825Seric printf(", ap="); 73058825Seric xputs(ap); 73158825Seric printf("\n"); 73258825Seric } 73358825Seric 73458825Seric if (ap == NULL) 73558825Seric { 73658825Seric /* run off the end -- back up again */ 73758825Seric continue; 73858825Seric } 73958050Seric if ((*rp & 0377) == MATCHANY || 74058050Seric (*rp & 0377) == MATCHZANY) 7414468Seric { 74256678Seric /* extend binding and continue */ 74358825Seric mlp->last = avp++; 74456678Seric rvp++; 74558825Seric mlp++; 74656678Seric break; 7474468Seric } 74858825Seric if ((*rp & 0377) == MATCHCLASS) 74956678Seric { 75058814Seric /* extend binding and try again */ 75158825Seric mlp->last = avp++; 75258814Seric goto extendclass; 75358814Seric } 7543149Seric } 7553149Seric 75658825Seric if (mlp < mlist) 75756678Seric { 75856678Seric /* total failure to match */ 75956326Seric break; 7603149Seric } 761297Seric } 7623149Seric 7633149Seric /* 76456678Seric ** See if we successfully matched 7653149Seric */ 7663149Seric 767*58827Seric if (mlp < mlist || *rvp != NULL) 7683149Seric { 7699374Seric if (tTd(21, 10)) 7709374Seric printf("----- rule fails\n"); 7719374Seric rwr = rwr->r_next; 7729374Seric continue; 7739374Seric } 7743149Seric 7759374Seric rvp = rwr->r_rhs; 7769374Seric if (tTd(21, 12)) 7779374Seric { 7789374Seric printf("-----rule matches:"); 77956678Seric printav(rvp); 7809374Seric } 7819374Seric 7829374Seric rp = *rvp; 78358050Seric if ((*rp & 0377) == CANONUSER) 7849374Seric { 7859374Seric rvp++; 7869374Seric rwr = rwr->r_next; 7879374Seric } 78858050Seric else if ((*rp & 0377) == CANONHOST) 7899374Seric { 7909374Seric rvp++; 7919374Seric rwr = NULL; 7929374Seric } 79358050Seric else if ((*rp & 0377) == CANONNET) 7949374Seric rwr = NULL; 7959374Seric 7969374Seric /* substitute */ 7979374Seric for (avp = npvp; *rvp != NULL; rvp++) 7989374Seric { 7999374Seric register struct match *m; 8009374Seric register char **pp; 8019374Seric 8028058Seric rp = *rvp; 80358050Seric if ((*rp & 0377) == MATCHREPL) 8048058Seric { 80516914Seric /* substitute from LHS */ 80616914Seric m = &mlist[rp[1] - '1']; 80756678Seric if (m < mlist || m >= mlp) 8089374Seric { 80958151Seric syserr("554 rewrite: ruleset %d: replacement $%c out of bounds", 81056326Seric ruleset, rp[1]); 8119374Seric return; 8129374Seric } 81316914Seric if (tTd(21, 15)) 81416914Seric { 81516914Seric printf("$%c:", rp[1]); 81616914Seric pp = m->first; 81756678Seric while (pp <= m->last) 81816914Seric { 81916914Seric printf(" %x=\"", *pp); 82016914Seric (void) fflush(stdout); 82116914Seric printf("%s\"", *pp++); 82216914Seric } 82316914Seric printf("\n"); 82416914Seric } 8259374Seric pp = m->first; 82656678Seric while (pp <= m->last) 8273149Seric { 82816914Seric if (avp >= &npvp[MAXATOM]) 82956678Seric { 83058151Seric syserr("554 rewrite: expansion too long"); 83156678Seric return; 83256678Seric } 83316914Seric *avp++ = *pp++; 8343149Seric } 8353149Seric } 83616914Seric else 8378226Seric { 83816914Seric /* vanilla replacement */ 8399374Seric if (avp >= &npvp[MAXATOM]) 84016889Seric { 84156678Seric toolong: 84258151Seric syserr("554 rewrite: expansion too long"); 84316889Seric return; 84416889Seric } 84556678Seric *avp++ = rp; 8468226Seric } 8479374Seric } 8489374Seric *avp++ = NULL; 84916914Seric 85016914Seric /* 85156678Seric ** Check for any hostname/keyword lookups. 85216914Seric */ 85316914Seric 85416914Seric for (rvp = npvp; *rvp != NULL; rvp++) 85516914Seric { 85656678Seric char **hbrvp; 85716914Seric char **xpvp; 85816914Seric int trsize; 85917473Seric char *olddelimchar; 86056678Seric char *replac; 86156678Seric int endtoken; 86256678Seric STAB *map; 86356678Seric char *mapname; 86456678Seric char **key_rvp; 86556678Seric char **arg_rvp; 86656678Seric char **default_rvp; 86756678Seric char buf[MAXNAME + 1]; 86816914Seric char *pvpb1[MAXATOM + 1]; 86956823Seric char *argvect[10]; 87017174Seric char pvpbuf[PSBUFSIZE]; 87116914Seric 87258050Seric if ((**rvp & 0377) != HOSTBEGIN && 87358050Seric (**rvp & 0377) != LOOKUPBEGIN) 87416914Seric continue; 87516914Seric 87616914Seric /* 87756678Seric ** Got a hostname/keyword lookup. 87816914Seric ** 87916914Seric ** This could be optimized fairly easily. 88016914Seric */ 88116914Seric 88216914Seric hbrvp = rvp; 88358050Seric if ((**rvp & 0377) == HOSTBEGIN) 88456327Seric { 88556678Seric endtoken = HOSTEND; 88656678Seric mapname = "host"; 88756327Seric } 88856326Seric else 88956327Seric { 89056678Seric endtoken = LOOKUPEND; 89156678Seric mapname = *++rvp; 89256327Seric } 89356678Seric map = stab(mapname, ST_MAP, ST_FIND); 89456678Seric if (map == NULL) 89558151Seric syserr("554 rewrite: map %s not found", mapname); 89656678Seric 89756678Seric /* extract the match part */ 89856678Seric key_rvp = ++rvp; 89956823Seric default_rvp = NULL; 90056823Seric arg_rvp = argvect; 90156823Seric xpvp = NULL; 90256823Seric replac = pvpbuf; 90358050Seric while (*rvp != NULL && (**rvp & 0377) != endtoken) 90453654Seric { 90558050Seric int nodetype = **rvp & 0377; 90656823Seric 90756823Seric if (nodetype != CANONHOST && nodetype != CANONUSER) 90856678Seric { 90956823Seric rvp++; 91056823Seric continue; 91156823Seric } 91256823Seric 91356823Seric *rvp++ = NULL; 91456823Seric 91556823Seric if (xpvp != NULL) 91656823Seric { 91758814Seric cataddr(xpvp, NULL, replac, 91858082Seric &pvpbuf[sizeof pvpbuf] - replac, 91958082Seric '\0'); 92056823Seric *++arg_rvp = replac; 92156823Seric replac += strlen(replac) + 1; 92256823Seric xpvp = NULL; 92356823Seric } 92456823Seric switch (nodetype) 92556823Seric { 92656678Seric case CANONHOST: 92756823Seric xpvp = rvp; 92856678Seric break; 92956678Seric 93056678Seric case CANONUSER: 93156678Seric default_rvp = rvp; 93256678Seric break; 93356678Seric } 93453654Seric } 93516914Seric if (*rvp != NULL) 93616914Seric *rvp++ = NULL; 93756823Seric if (xpvp != NULL) 93856823Seric { 93958814Seric cataddr(xpvp, NULL, replac, 94058082Seric &pvpbuf[sizeof pvpbuf] - replac, 94158082Seric '\0'); 94256823Seric *++arg_rvp = replac; 94356823Seric } 94456823Seric *++arg_rvp = NULL; 94516914Seric 94616914Seric /* save the remainder of the input string */ 94716914Seric trsize = (int) (avp - rvp + 1) * sizeof *rvp; 94816914Seric bcopy((char *) rvp, (char *) pvpb1, trsize); 94916914Seric 95056678Seric /* look it up */ 95158814Seric cataddr(key_rvp, NULL, buf, sizeof buf, '\0'); 95256823Seric argvect[0] = buf; 95356678Seric if (map != NULL && bitset(MF_VALID, map->s_map.map_flags)) 95456836Seric { 95556836Seric int bsize = sizeof buf - 1; 95656836Seric 95756836Seric if (map->s_map.map_app != NULL) 95856836Seric bsize -= strlen(map->s_map.map_app); 95958796Seric if (tTd(60, 1)) 96058796Seric printf("map_lookup(%s, %s) => ", 96158796Seric mapname, buf); 96256836Seric replac = (*map->s_map.map_class->map_lookup)(&map->s_map, 96356823Seric buf, sizeof buf - 1, argvect); 96456836Seric if (replac != NULL && map->s_map.map_app != NULL) 96556836Seric strcat(replac, map->s_map.map_app); 96658796Seric if (tTd(60, 1)) 96758796Seric printf("%s\n", replac ? replac : "NOT FOUND"); 96856836Seric } 96953654Seric else 97056678Seric replac = NULL; 97156678Seric 97256678Seric /* if no replacement, use default */ 97356823Seric if (replac == NULL && default_rvp != NULL) 97456823Seric { 97556823Seric char buf2[sizeof buf]; 97656823Seric 97756823Seric /* rewrite the default with % translations */ 97858814Seric cataddr(default_rvp, NULL, buf2, sizeof buf2, '\0'); 97956823Seric map_rewrite(buf2, sizeof buf2, buf, sizeof buf, 98056823Seric argvect); 98156823Seric replac = buf; 98256823Seric } 98356823Seric 98456678Seric if (replac == NULL) 98551317Seric { 98656823Seric xpvp = key_rvp; 98753654Seric } 98856678Seric else 98953654Seric { 99056678Seric /* scan the new replacement */ 99158333Seric xpvp = prescan(replac, '\0', pvpbuf, NULL); 99253654Seric if (xpvp == NULL) 99351317Seric { 99458403Seric /* prescan already printed error */ 99556678Seric return; 99656678Seric } 99751317Seric } 99851317Seric 99916914Seric /* append it to the token list */ 100056678Seric for (avp = hbrvp; *xpvp != NULL; xpvp++) 100156678Seric { 100217174Seric *avp++ = newstr(*xpvp); 100316920Seric if (avp >= &npvp[MAXATOM]) 100416914Seric goto toolong; 100517174Seric } 100616914Seric 100716914Seric /* restore the old trailing information */ 100856678Seric for (xpvp = pvpb1; (*avp++ = *xpvp++) != NULL; ) 100916920Seric if (avp >= &npvp[MAXATOM]) 101016914Seric goto toolong; 101117174Seric 101256678Seric break; 101316914Seric } 101416914Seric 101516914Seric /* 101616914Seric ** Check for subroutine calls. 101716914Seric */ 101816914Seric 101958050Seric if (*npvp != NULL && (**npvp & 0377) == CALLSUBR) 102056678Seric { 102156678Seric bcopy((char *) &npvp[2], (char *) pvp, 102256678Seric (int) (avp - npvp - 2) * sizeof *avp); 102356678Seric if (tTd(21, 3)) 102456678Seric printf("-----callsubr %s\n", npvp[1]); 102556678Seric rewrite(pvp, atoi(npvp[1])); 102656678Seric } 102756678Seric else 102856678Seric { 102917348Seric bcopy((char *) npvp, (char *) pvp, 103016900Seric (int) (avp - npvp) * sizeof *avp); 103156678Seric } 10329374Seric if (tTd(21, 4)) 10339374Seric { 10349374Seric printf("rewritten as:"); 103556678Seric printav(pvp); 10369374Seric } 1037297Seric } 10388069Seric 10399279Seric if (OpMode == MD_TEST || tTd(21, 2)) 10408069Seric { 10418959Seric printf("rewrite: ruleset %2d returns:", ruleset); 104256678Seric printav(pvp); 10438069Seric } 10443149Seric } 10453149Seric /* 10463149Seric ** BUILDADDR -- build address from token vector. 10473149Seric ** 10483149Seric ** Parameters: 10493149Seric ** tv -- token vector. 10503149Seric ** a -- pointer to address descriptor to fill. 10513149Seric ** If NULL, one will be allocated. 10523149Seric ** 10533149Seric ** Returns: 10544279Seric ** NULL if there was an error. 10554279Seric ** 'a' otherwise. 10563149Seric ** 10573149Seric ** Side Effects: 10583149Seric ** fills in 'a' 10593149Seric */ 10603149Seric 106157249Seric struct errcodes 106257249Seric { 106357249Seric char *ec_name; /* name of error code */ 106457249Seric int ec_code; /* numeric code */ 106557249Seric } ErrorCodes[] = 106657249Seric { 106757249Seric "usage", EX_USAGE, 106857249Seric "nouser", EX_NOUSER, 106957249Seric "nohost", EX_NOHOST, 107057249Seric "unavailable", EX_UNAVAILABLE, 107157249Seric "software", EX_SOFTWARE, 107257249Seric "tempfail", EX_TEMPFAIL, 107357249Seric "protocol", EX_PROTOCOL, 107457249Seric #ifdef EX_CONFIG 107557249Seric "config", EX_CONFIG, 107657249Seric #endif 107757249Seric NULL, EX_UNAVAILABLE, 107857249Seric }; 107957249Seric 108056678Seric ADDRESS * 10813149Seric buildaddr(tv, a) 10823149Seric register char **tv; 10833149Seric register ADDRESS *a; 10843149Seric { 10853149Seric struct mailer **mp; 10863149Seric register struct mailer *m; 108758008Seric char *bp; 108858008Seric int spaceleft; 108957402Seric static char buf[MAXNAME]; 10903149Seric 10913149Seric if (a == NULL) 10923149Seric a = (ADDRESS *) xalloc(sizeof *a); 109316889Seric bzero((char *) a, sizeof *a); 10943149Seric 10953149Seric /* figure out what net/mailer to use */ 109658050Seric if ((**tv & 0377) != CANONNET) 10974279Seric { 109858151Seric syserr("554 buildaddr: no net"); 10994279Seric return (NULL); 11004279Seric } 11013149Seric tv++; 110258680Seric if (strcasecmp(*tv, "error") == 0) 11034279Seric { 110458050Seric if ((**++tv & 0377) == CANONHOST) 110510183Seric { 110657249Seric register struct errcodes *ep; 110757249Seric 110858050Seric if (isascii(**++tv) && isdigit(**tv)) 110957249Seric { 111057249Seric setstat(atoi(*tv)); 111157249Seric } 111257249Seric else 111357249Seric { 111457249Seric for (ep = ErrorCodes; ep->ec_name != NULL; ep++) 111557249Seric if (strcasecmp(ep->ec_name, *tv) == 0) 111657249Seric break; 111757249Seric setstat(ep->ec_code); 111857249Seric } 111910183Seric tv++; 112010183Seric } 112158050Seric if ((**tv & 0377) != CANONUSER) 112258151Seric syserr("554 buildaddr: error: no user"); 112358814Seric cataddr(++tv, NULL, buf, sizeof buf, ' '); 112458082Seric stripquotes(buf); 11254279Seric usrerr(buf); 11264279Seric return (NULL); 11274279Seric } 112857402Seric 11294598Seric for (mp = Mailer; (m = *mp++) != NULL; ) 11303149Seric { 113158680Seric if (strcasecmp(m->m_name, *tv) == 0) 11323149Seric break; 11333149Seric } 11343149Seric if (m == NULL) 11354279Seric { 113658151Seric syserr("554 buildaddr: unknown mailer %s", *tv); 11374279Seric return (NULL); 11384279Seric } 11394598Seric a->q_mailer = m; 11403149Seric 11413149Seric /* figure out what host (if any) */ 114256678Seric tv++; 114358509Seric if ((**tv & 0377) == CANONHOST) 11443149Seric { 114558008Seric bp = buf; 114658008Seric spaceleft = sizeof buf - 1; 114758050Seric while (*++tv != NULL && (**tv & 0377) != CANONUSER) 114858008Seric { 114958008Seric int i = strlen(*tv); 115058008Seric 115158008Seric if (i > spaceleft) 115258008Seric { 115358008Seric /* out of space for this address */ 115458008Seric if (spaceleft >= 0) 115558151Seric syserr("554 buildaddr: host too long (%.40s...)", 115658008Seric buf); 115758008Seric i = spaceleft; 115858008Seric spaceleft = 0; 115958008Seric } 116058008Seric if (i <= 0) 116158008Seric continue; 116258008Seric bcopy(*tv, bp, i); 116358008Seric bp += i; 116458008Seric spaceleft -= i; 116558008Seric } 116658010Seric *bp = '\0'; 11675704Seric a->q_host = newstr(buf); 11683149Seric } 116957249Seric else 117058509Seric { 117158509Seric if (!bitnset(M_LOCALMAILER, m->m_flags)) 117258509Seric { 117358509Seric syserr("554 buildaddr: no host"); 117458509Seric return (NULL); 117558509Seric } 117657249Seric a->q_host = NULL; 117758509Seric } 11783149Seric 11793149Seric /* figure out the user */ 118058050Seric if (*tv == NULL || (**tv & 0377) != CANONUSER) 11814279Seric { 118258151Seric syserr("554 buildaddr: no user"); 11834279Seric return (NULL); 11844279Seric } 118557402Seric tv++; 118651317Seric 118757402Seric /* do special mapping for local mailer */ 118857402Seric if (m == LocalMailer && *tv != NULL) 118957402Seric { 119057454Seric register char *p = *tv; 119157454Seric 119257454Seric if (*p == '"') 119357454Seric p++; 119457454Seric if (*p == '|') 119557402Seric a->q_mailer = m = ProgMailer; 119657454Seric else if (*p == '/') 119757402Seric a->q_mailer = m = FileMailer; 119857454Seric else if (*p == ':') 119957402Seric { 120057402Seric /* may be :include: */ 120158814Seric cataddr(tv, NULL, buf, sizeof buf, '\0'); 120258008Seric stripquotes(buf); 120358008Seric if (strncasecmp(buf, ":include:", 9) == 0) 120458008Seric { 120558008Seric /* if :include:, don't need further rewriting */ 120657402Seric a->q_mailer = m = InclMailer; 120758008Seric a->q_user = &buf[9]; 120858008Seric return (a); 120958008Seric } 121057402Seric } 121157402Seric } 121257402Seric 121358008Seric if (m == LocalMailer && *tv != NULL && strcmp(*tv, "@") == 0) 121458008Seric { 121558008Seric tv++; 121658008Seric a->q_flags |= QNOTREMOTE; 121758008Seric } 121858008Seric 121958673Seric /* do cleanup of final address */ 122019040Seric rewrite(tv, 4); 122119040Seric 122219040Seric /* save the result for the command line/RCPT argument */ 122358814Seric cataddr(tv, NULL, buf, sizeof buf, '\0'); 12243149Seric a->q_user = buf; 12253149Seric 122658670Seric /* 122758670Seric ** Do mapping to lower case as requested by mailer 122858670Seric */ 122958670Seric 123058670Seric if (a->q_host != NULL && !bitnset(M_HST_UPPER, m->m_flags)) 123158670Seric makelower(a->q_host); 123258670Seric if (!bitnset(M_USR_UPPER, m->m_flags)) 123358670Seric makelower(a->q_user); 123458670Seric 12353149Seric return (a); 12363149Seric } 12373188Seric /* 12384228Seric ** CATADDR -- concatenate pieces of addresses (putting in <LWSP> subs) 12394228Seric ** 12404228Seric ** Parameters: 12414228Seric ** pvp -- parameter vector to rebuild. 124258814Seric ** evp -- last parameter to include. Can be NULL to 124358814Seric ** use entire pvp. 12444228Seric ** buf -- buffer to build the string into. 12454228Seric ** sz -- size of buf. 124658082Seric ** spacesub -- the space separator character; if null, 124758082Seric ** use SpaceSub. 12484228Seric ** 12494228Seric ** Returns: 12504228Seric ** none. 12514228Seric ** 12524228Seric ** Side Effects: 12534228Seric ** Destroys buf. 12544228Seric */ 12554228Seric 125658814Seric cataddr(pvp, evp, buf, sz, spacesub) 12574228Seric char **pvp; 125858814Seric char **evp; 12594228Seric char *buf; 12604228Seric register int sz; 126158082Seric char spacesub; 12624228Seric { 12634228Seric bool oatomtok = FALSE; 126456678Seric bool natomtok = FALSE; 12654228Seric register int i; 12664228Seric register char *p; 12674228Seric 126858082Seric if (spacesub == '\0') 126958082Seric spacesub = SpaceSub; 127058082Seric 12718423Seric if (pvp == NULL) 12728423Seric { 127323109Seric (void) strcpy(buf, ""); 12748423Seric return; 12758423Seric } 12764228Seric p = buf; 127711156Seric sz -= 2; 12784228Seric while (*pvp != NULL && (i = strlen(*pvp)) < sz) 12794228Seric { 12808078Seric natomtok = (toktype(**pvp) == ATM); 12814228Seric if (oatomtok && natomtok) 128258082Seric *p++ = spacesub; 12834228Seric (void) strcpy(p, *pvp); 12844228Seric oatomtok = natomtok; 12854228Seric p += i; 128611156Seric sz -= i + 1; 128758814Seric if (pvp++ == evp) 128858814Seric break; 12894228Seric } 12904228Seric *p = '\0'; 12914228Seric } 12924228Seric /* 12933188Seric ** SAMEADDR -- Determine if two addresses are the same 12943188Seric ** 12953188Seric ** This is not just a straight comparison -- if the mailer doesn't 12963188Seric ** care about the host we just ignore it, etc. 12973188Seric ** 12983188Seric ** Parameters: 12993188Seric ** a, b -- pointers to the internal forms to compare. 13003188Seric ** 13013188Seric ** Returns: 13023188Seric ** TRUE -- they represent the same mailbox. 13033188Seric ** FALSE -- they don't. 13043188Seric ** 13053188Seric ** Side Effects: 13063188Seric ** none. 13073188Seric */ 13083188Seric 13093188Seric bool 13109374Seric sameaddr(a, b) 13113188Seric register ADDRESS *a; 13123188Seric register ADDRESS *b; 13133188Seric { 13143188Seric /* if they don't have the same mailer, forget it */ 13153188Seric if (a->q_mailer != b->q_mailer) 13163188Seric return (FALSE); 13173188Seric 13183188Seric /* if the user isn't the same, we can drop out */ 131956678Seric if (strcmp(a->q_user, b->q_user) != 0) 13203188Seric return (FALSE); 13213188Seric 132258438Seric /* if we have good uids for both but the differ, these are different */ 132358438Seric if (bitset(QGOODUID, a->q_flags & b->q_flags) && a->q_uid != b->q_uid) 132458438Seric return (FALSE); 132558438Seric 132658509Seric /* otherwise compare hosts (but be careful for NULL ptrs) */ 132758509Seric if (a->q_host == b->q_host) 132858509Seric { 132958509Seric /* probably both null pointers */ 13303188Seric return (TRUE); 133158509Seric } 13323188Seric if (a->q_host == NULL || b->q_host == NULL) 133358509Seric { 133458509Seric /* only one is a null pointer */ 13353188Seric return (FALSE); 133658509Seric } 133756678Seric if (strcmp(a->q_host, b->q_host) != 0) 13383188Seric return (FALSE); 13393188Seric 13403188Seric return (TRUE); 13413188Seric } 13423234Seric /* 13433234Seric ** PRINTADDR -- print address (for debugging) 13443234Seric ** 13453234Seric ** Parameters: 13463234Seric ** a -- the address to print 13473234Seric ** follow -- follow the q_next chain. 13483234Seric ** 13493234Seric ** Returns: 13503234Seric ** none. 13513234Seric ** 13523234Seric ** Side Effects: 13533234Seric ** none. 13543234Seric */ 13553234Seric 13563234Seric printaddr(a, follow) 13573234Seric register ADDRESS *a; 13583234Seric bool follow; 13593234Seric { 13605001Seric bool first = TRUE; 136157731Seric register MAILER *m; 136257731Seric MAILER pseudomailer; 13635001Seric 13643234Seric while (a != NULL) 13653234Seric { 13665001Seric first = FALSE; 13674443Seric printf("%x=", a); 13684085Seric (void) fflush(stdout); 136957731Seric 137057731Seric /* find the mailer -- carefully */ 137157731Seric m = a->q_mailer; 137257731Seric if (m == NULL) 137357731Seric { 137457731Seric m = &pseudomailer; 137557731Seric m->m_mno = -1; 137657731Seric m->m_name = "NULL"; 137757731Seric } 137857731Seric 137958680Seric printf("%s:\n\tmailer %d (%s), host `%s', user `%s', ruser `%s'\n", 138057731Seric a->q_paddr, m->m_mno, m->m_name, 138140973Sbostic a->q_host, a->q_user, a->q_ruser? a->q_ruser: "<null>"); 13828181Seric printf("\tnext=%x, flags=%o, alias %x\n", a->q_next, a->q_flags, 13838181Seric a->q_alias); 13848181Seric printf("\thome=\"%s\", fullname=\"%s\"\n", a->q_home, 13858181Seric a->q_fullname); 13864996Seric 13873234Seric if (!follow) 13883234Seric return; 13894996Seric a = a->q_next; 13903234Seric } 13915001Seric if (first) 13924443Seric printf("[NULL]\n"); 13933234Seric } 13944317Seric 13957682Seric /* 13967682Seric ** REMOTENAME -- return the name relative to the current mailer 13977682Seric ** 13987682Seric ** Parameters: 13997682Seric ** name -- the name to translate. 14008069Seric ** m -- the mailer that we want to do rewriting relative 14018069Seric ** to. 14028069Seric ** senderaddress -- if set, uses the sender rewriting rules 14038069Seric ** rather than the recipient rewriting rules. 140458020Seric ** header -- set if this address is in the header, rather 140558020Seric ** than an envelope header. 140610310Seric ** canonical -- if set, strip out any comment information, 140710310Seric ** etc. 140858509Seric ** adddomain -- if set, OK to do domain extension. 140958020Seric ** e -- the current envelope. 14107682Seric ** 14117682Seric ** Returns: 14127682Seric ** the text string representing this address relative to 14137682Seric ** the receiving mailer. 14147682Seric ** 14157682Seric ** Side Effects: 14167682Seric ** none. 14177682Seric ** 14187682Seric ** Warnings: 14197682Seric ** The text string returned is tucked away locally; 14207682Seric ** copy it if you intend to save it. 14217682Seric */ 14227682Seric 14237682Seric char * 142458509Seric remotename(name, m, senderaddress, header, canonical, adddomain, e) 14257682Seric char *name; 142656678Seric struct mailer *m; 14278069Seric bool senderaddress; 142858020Seric bool header; 142910310Seric bool canonical; 143058509Seric bool adddomain; 143156678Seric register ENVELOPE *e; 14327682Seric { 14338069Seric register char **pvp; 14348069Seric char *fancy; 143556678Seric extern char *macvalue(); 143656678Seric char *oldg = macvalue('g', e); 143758020Seric int rwset; 14387682Seric static char buf[MAXNAME]; 14397682Seric char lbuf[MAXNAME]; 144016914Seric char pvpbuf[PSBUFSIZE]; 144156678Seric extern char **prescan(); 144256678Seric extern char *crackaddr(); 14437682Seric 14447755Seric if (tTd(12, 1)) 14457755Seric printf("remotename(%s)\n", name); 14467755Seric 144710177Seric /* don't do anything if we are tagging it as special */ 144858020Seric if (senderaddress) 144958020Seric rwset = header ? m->m_sh_rwset : m->m_se_rwset; 145058020Seric else 145158020Seric rwset = header ? m->m_rh_rwset : m->m_re_rwset; 145258020Seric if (rwset < 0) 145310177Seric return (name); 145410177Seric 14557682Seric /* 14568181Seric ** Do a heuristic crack of this name to extract any comment info. 14578181Seric ** This will leave the name as a comment and a $g macro. 14587889Seric */ 14597889Seric 146058036Seric if (canonical || bitnset(M_NOCOMMENT, m->m_flags)) 146158050Seric fancy = "\201g"; 146210310Seric else 146310310Seric fancy = crackaddr(name); 14647889Seric 14658181Seric /* 14668181Seric ** Turn the name into canonical form. 14678181Seric ** Normally this will be RFC 822 style, i.e., "user@domain". 14688181Seric ** If this only resolves to "user", and the "C" flag is 14698181Seric ** specified in the sending mailer, then the sender's 14708181Seric ** domain will be appended. 14718181Seric */ 14728181Seric 147358333Seric pvp = prescan(name, '\0', pvpbuf, NULL); 14747889Seric if (pvp == NULL) 14757889Seric return (name); 14768181Seric rewrite(pvp, 3); 147758509Seric if (adddomain && e->e_fromdomain != NULL) 14788181Seric { 14798181Seric /* append from domain to this address */ 14808181Seric register char **pxp = pvp; 14818181Seric 14829594Seric /* see if there is an "@domain" in the current name */ 14838181Seric while (*pxp != NULL && strcmp(*pxp, "@") != 0) 14848181Seric pxp++; 14858181Seric if (*pxp == NULL) 14868181Seric { 14879594Seric /* no.... append the "@domain" from the sender */ 148856678Seric register char **qxq = e->e_fromdomain; 14898181Seric 14909594Seric while ((*pxp++ = *qxq++) != NULL) 14919594Seric continue; 149211726Seric rewrite(pvp, 3); 14938181Seric } 14948181Seric } 14958181Seric 14968181Seric /* 14978959Seric ** Do more specific rewriting. 149856678Seric ** Rewrite using ruleset 1 or 2 depending on whether this is 149956678Seric ** a sender address or not. 15008181Seric ** Then run it through any receiving-mailer-specific rulesets. 15018181Seric */ 15028181Seric 15038069Seric if (senderaddress) 150456327Seric rewrite(pvp, 1); 15058069Seric else 150656327Seric rewrite(pvp, 2); 150758020Seric if (rwset > 0) 150858020Seric rewrite(pvp, rwset); 15097682Seric 15108181Seric /* 15118959Seric ** Do any final sanitation the address may require. 15128959Seric ** This will normally be used to turn internal forms 15138959Seric ** (e.g., user@host.LOCAL) into external form. This 15148959Seric ** may be used as a default to the above rules. 15158959Seric */ 15168959Seric 15178959Seric rewrite(pvp, 4); 15188959Seric 15198959Seric /* 15208181Seric ** Now restore the comment information we had at the beginning. 15218181Seric */ 15228181Seric 152358825Seric cataddr(pvp, NULL, lbuf, sizeof lbuf, '\0'); 152456678Seric define('g', lbuf, e); 152556678Seric expand(fancy, buf, &buf[sizeof buf - 1], e); 152656678Seric define('g', oldg, e); 15277682Seric 15287682Seric if (tTd(12, 1)) 15297755Seric printf("remotename => `%s'\n", buf); 15307682Seric return (buf); 15317682Seric } 153251317Seric /* 153356678Seric ** MAPLOCALUSER -- run local username through ruleset 5 for final redirection 153451317Seric ** 153551317Seric ** Parameters: 153656678Seric ** a -- the address to map (but just the user name part). 153756678Seric ** sendq -- the sendq in which to install any replacement 153856678Seric ** addresses. 153951317Seric ** 154051317Seric ** Returns: 154151317Seric ** none. 154251317Seric */ 154351317Seric 154456678Seric maplocaluser(a, sendq, e) 154556678Seric register ADDRESS *a; 154656678Seric ADDRESS **sendq; 154756678Seric ENVELOPE *e; 154851317Seric { 154956678Seric register char **pvp; 155056678Seric register ADDRESS *a1 = NULL; 155158333Seric auto char *delimptr; 155256678Seric char pvpbuf[PSBUFSIZE]; 155351317Seric 155456678Seric if (tTd(29, 1)) 155556678Seric { 155656678Seric printf("maplocaluser: "); 155756678Seric printaddr(a, FALSE); 155856678Seric } 155958333Seric pvp = prescan(a->q_user, '\0', pvpbuf, &delimptr); 156056678Seric if (pvp == NULL) 156156678Seric return; 156251317Seric 156356678Seric rewrite(pvp, 5); 156458050Seric if (pvp[0] == NULL || (pvp[0][0] & 0377) != CANONNET) 156556678Seric return; 156651317Seric 156756678Seric /* if non-null, mailer destination specified -- has it changed? */ 156856678Seric a1 = buildaddr(pvp, NULL); 156956678Seric if (a1 == NULL || sameaddr(a, a1)) 157056678Seric return; 157151317Seric 157256678Seric /* mark old address as dead; insert new address */ 157356678Seric a->q_flags |= QDONTSEND; 157457731Seric if (tTd(29, 5)) 157557731Seric { 157657731Seric printf("maplocaluser: QDONTSEND "); 157757731Seric printaddr(a, FALSE); 157857731Seric } 157956678Seric a1->q_alias = a; 158058333Seric allocaddr(a1, 1, NULL, delimptr); 158156678Seric (void) recipient(a1, sendq, e); 158251317Seric } 158358802Seric /* 158458802Seric ** DEQUOTE_INIT -- initialize dequote map 158558802Seric ** 158658802Seric ** This is a no-op. 158758802Seric ** 158858802Seric ** Parameters: 158958802Seric ** map -- the internal map structure. 159058802Seric ** mapname -- the name of the mapl. 159158802Seric ** args -- arguments. 159258802Seric ** 159358802Seric ** Returns: 159458802Seric ** TRUE. 159558802Seric */ 159658802Seric 159758802Seric bool 159858802Seric dequote_init(map, mapname, args) 159958802Seric MAP *map; 160058802Seric char *mapname; 160158802Seric char *args; 160258802Seric { 160358805Seric register char *p = args; 160458805Seric 160558805Seric for (;;) 160658805Seric { 160758805Seric while (isascii(*p) && isspace(*p)) 160858805Seric p++; 160958805Seric if (*p != '-') 161058805Seric break; 161158805Seric switch (*++p) 161258805Seric { 161358805Seric case 'a': 161458805Seric map->map_app = ++p; 161558805Seric break; 161658805Seric } 161758805Seric while (*p != '\0' && !(isascii(*p) && isspace(*p))) 161858805Seric p++; 161958805Seric if (*p != '\0') 162058805Seric *p = '\0'; 162158805Seric } 162258805Seric if (map->map_app != NULL) 162358805Seric map->map_app = newstr(map->map_app); 162458805Seric 162558802Seric return TRUE; 162658802Seric } 162758802Seric /* 162858802Seric ** DEQUOTE_MAP -- unquote an address 162958802Seric ** 163058802Seric ** Parameters: 163158802Seric ** map -- the internal map structure (ignored). 163258802Seric ** buf -- the buffer to dequote. 163358802Seric ** bufsiz -- the size of that buffer. 163458802Seric ** av -- arguments (ignored). 163558802Seric ** 163658802Seric ** Returns: 163758802Seric ** NULL -- if there were no quotes, or if the resulting 163858802Seric ** unquoted buffer would not be acceptable to prescan. 163958802Seric ** else -- The dequoted buffer. 164058802Seric */ 164158802Seric 164258802Seric char * 164358802Seric dequote_map(map, buf, bufsiz, av) 164458802Seric MAP *map; 164558802Seric char buf[]; 164658802Seric int bufsiz; 164758802Seric char **av; 164858802Seric { 164958802Seric register char *p; 165058802Seric register char *q; 165158802Seric register char c; 165258802Seric int anglecnt; 165358802Seric int cmntcnt; 165458802Seric int quotecnt; 165558802Seric bool quotemode; 165658802Seric bool bslashmode; 165758802Seric 165858802Seric anglecnt = 0; 165958802Seric cmntcnt = 0; 166058802Seric quotecnt = 0; 166158802Seric quotemode = FALSE; 166258802Seric bslashmode = FALSE; 166358802Seric 166458802Seric for (p = q = buf; (c = *p++) != '\0'; ) 166558802Seric { 166658802Seric if (bslashmode) 166758802Seric { 166858802Seric bslashmode = FALSE; 166958802Seric *q++ = c; 167058802Seric continue; 167158802Seric } 167258802Seric 167358802Seric switch (c) 167458802Seric { 167558802Seric case '\\': 167658802Seric bslashmode = TRUE; 167758802Seric break; 167858802Seric 167958802Seric case '(': 168058802Seric cmntcnt++; 168158802Seric break; 168258802Seric 168358802Seric case ')': 168458802Seric if (cmntcnt-- <= 0) 168558802Seric return NULL; 168658802Seric break; 168758802Seric } 168858802Seric 168958802Seric if (cmntcnt > 0) 169058802Seric { 169158802Seric *q++ = c; 169258802Seric continue; 169358802Seric } 169458802Seric 169558802Seric switch (c) 169658802Seric { 169758802Seric case '"': 169858802Seric quotemode = !quotemode; 169958802Seric quotecnt++; 170058802Seric continue; 170158802Seric 170258802Seric case '<': 170358802Seric anglecnt++; 170458802Seric break; 170558802Seric 170658802Seric case '>': 170758802Seric if (anglecnt-- <= 0) 170858802Seric return NULL; 170958802Seric break; 171058802Seric } 171158802Seric *q++ = c; 171258802Seric } 171358802Seric 171458802Seric if (anglecnt != 0 || cmntcnt != 0 || bslashmode || 171558802Seric quotemode || quotecnt <= 0) 171658802Seric return NULL; 171758802Seric *q++ = '\0'; 171858802Seric return buf; 171958802Seric } 1720