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*59040Seric static char sccsid[] = "@(#)parseaddr.c 6.38 (Berkeley) 04/12/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 10659027Seric rewrite(pvp, 3, e); 10759027Seric rewrite(pvp, 0, e); 108297Seric 1093149Seric /* 1103149Seric ** See if we resolved to a real mailer. 1113149Seric */ 112297Seric 113*59040Seric if (pvp[0] == NULL || (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 12458966Seric a = buildaddr(pvp, a, e); 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); 51359027Seric if (c == MACRODEXPAND) 51459027Seric return (ONE); 5158078Seric if (c == '"') 5168078Seric return (QST); 51758050Seric if ((c & 0340) == 0200) 51858050Seric return (OPR); 5194100Seric if (!isascii(c)) 5208078Seric return (ATM); 5218078Seric if (isspace(c) || c == ')') 5228078Seric return (SPC); 52358050Seric if (strchr(buf, c) != NULL) 5248078Seric return (OPR); 5258078Seric return (ATM); 5263149Seric } 5273149Seric /* 5283149Seric ** REWRITE -- apply rewrite rules to token vector. 5293149Seric ** 5304476Seric ** This routine is an ordered production system. Each rewrite 5314476Seric ** rule has a LHS (called the pattern) and a RHS (called the 5324476Seric ** rewrite); 'rwr' points the the current rewrite rule. 5334476Seric ** 5344476Seric ** For each rewrite rule, 'avp' points the address vector we 5354476Seric ** are trying to match against, and 'pvp' points to the pattern. 5368058Seric ** If pvp points to a special match value (MATCHZANY, MATCHANY, 5379585Seric ** MATCHONE, MATCHCLASS, MATCHNCLASS) then the address in avp 5389585Seric ** matched is saved away in the match vector (pointed to by 'mvp'). 5394476Seric ** 5404476Seric ** When a match between avp & pvp does not match, we try to 5419585Seric ** back out. If we back up over MATCHONE, MATCHCLASS, or MATCHNCLASS 5424476Seric ** we must also back out the match in mvp. If we reach a 5438058Seric ** MATCHANY or MATCHZANY we just extend the match and start 5448058Seric ** over again. 5454476Seric ** 5464476Seric ** When we finally match, we rewrite the address vector 5474476Seric ** and try over again. 5484476Seric ** 5493149Seric ** Parameters: 5503149Seric ** pvp -- pointer to token vector. 55159027Seric ** ruleset -- the ruleset to use for rewriting. 55259027Seric ** e -- the current envelope. 5533149Seric ** 5543149Seric ** Returns: 5553149Seric ** none. 5563149Seric ** 5573149Seric ** Side Effects: 5583149Seric ** pvp is modified. 5593149Seric */ 5602091Seric 5613149Seric struct match 5623149Seric { 5634468Seric char **first; /* first token matched */ 5644468Seric char **last; /* last token matched */ 56558825Seric char **pattern; /* pointer to pattern */ 5663149Seric }; 5673149Seric 5684468Seric # define MAXMATCH 9 /* max params per rewrite */ 5693149Seric 5703149Seric 57159027Seric rewrite(pvp, ruleset, e) 5723149Seric char **pvp; 5734070Seric int ruleset; 57459027Seric register ENVELOPE *e; 5753149Seric { 5763149Seric register char *ap; /* address pointer */ 5773149Seric register char *rp; /* rewrite pointer */ 5783149Seric register char **avp; /* address vector pointer */ 5793149Seric register char **rvp; /* rewrite vector pointer */ 5808058Seric register struct match *mlp; /* cur ptr into mlist */ 5818058Seric register struct rewrite *rwr; /* pointer to current rewrite rule */ 58258866Seric int ruleno; /* current rule number */ 58356678Seric struct match mlist[MAXMATCH]; /* stores match on LHS */ 5843149Seric char *npvp[MAXATOM+1]; /* temporary space for rebuild */ 58559027Seric extern char *macvalue(); 5863149Seric 5879279Seric if (OpMode == MD_TEST || tTd(21, 2)) 5883149Seric { 5898959Seric printf("rewrite: ruleset %2d input:", ruleset); 59056678Seric printav(pvp); 5913149Seric } 59256678Seric if (ruleset < 0 || ruleset >= MAXRWSETS) 59356326Seric { 59458151Seric syserr("554 rewrite: illegal ruleset number %d", ruleset); 59556326Seric return; 59656326Seric } 59756678Seric if (pvp == NULL) 59856678Seric return; 59956326Seric 6003149Seric /* 60156678Seric ** Run through the list of rewrite rules, applying 60256678Seric ** any that match. 6033149Seric */ 6043149Seric 60558866Seric ruleno = 1; 6064070Seric for (rwr = RewriteRules[ruleset]; rwr != NULL; ) 6073149Seric { 60856678Seric int loopcount = 0; 60956678Seric 6107675Seric if (tTd(21, 12)) 611297Seric { 6128069Seric printf("-----trying rule:"); 61356678Seric printav(rwr->r_lhs); 6143149Seric } 6153149Seric 6163149Seric /* try to match on this rule */ 6174468Seric mlp = mlist; 6188058Seric rvp = rwr->r_lhs; 6198058Seric avp = pvp; 62058866Seric if (++loopcount > 100) 6213149Seric { 62258866Seric syserr("554 Infinite loop in ruleset %d, rule %d", 62358866Seric ruleset, ruleno); 62458866Seric if (tTd(21, 1)) 62552637Seric { 62656678Seric printf("workspace: "); 62756678Seric printav(pvp); 62852637Seric } 62958866Seric break; 63058866Seric } 63158866Seric 63258866Seric while ((ap = *avp) != NULL || *rvp != NULL) 63358866Seric { 6343149Seric rp = *rvp; 6358058Seric if (tTd(21, 35)) 6368058Seric { 63758825Seric printf("ADVANCE 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 } 64858173Seric if (ap == NULL && (*rp & 0377) != MATCHZANY && 64958827Seric (*rp & 0377) != MATCHZERO) 65056326Seric { 65158825Seric /* end-of-input with patterns left */ 65258825Seric goto backup; 653297Seric } 65456326Seric 65558050Seric switch (*rp & 0377) 6568058Seric { 65756678Seric register STAB *s; 65858814Seric char buf[MAXLINE]; 65956326Seric 66056678Seric case MATCHCLASS: 66158825Seric /* match any phrase in a class */ 66258825Seric mlp->pattern = rvp; 66358814Seric mlp->first = avp; 66458814Seric extendclass: 66558825Seric ap = *avp; 66658825Seric if (ap == NULL) 66758814Seric goto backup; 66858814Seric mlp->last = avp++; 66958814Seric cataddr(mlp->first, mlp->last, buf, sizeof buf, '\0'); 67058814Seric s = stab(buf, ST_CLASS, ST_FIND); 67156678Seric if (s == NULL || !bitnset(rp[1], s->s_class)) 67256326Seric { 67358825Seric if (tTd(21, 36)) 67458825Seric { 67558825Seric printf("EXTEND rp="); 67658825Seric xputs(rp); 67758825Seric printf(", ap="); 67858825Seric xputs(ap); 67958825Seric printf("\n"); 68058825Seric } 68158825Seric goto extendclass; 68256326Seric } 68358825Seric if (tTd(21, 36)) 68458825Seric printf("CLMATCH\n"); 68558814Seric mlp++; 68658814Seric break; 6874060Seric 68858825Seric case MATCHNCLASS: 68958825Seric /* match any token not in a class */ 69058825Seric s = stab(ap, ST_CLASS, ST_FIND); 69158825Seric if (s != NULL && bitnset(rp[1], s->s_class)) 69258825Seric goto backup; 69358825Seric 69458825Seric /* fall through */ 69558825Seric 69656678Seric case MATCHONE: 69756678Seric case MATCHANY: 69856678Seric /* match exactly one token */ 69958825Seric mlp->pattern = rvp; 70056678Seric mlp->first = avp; 70156678Seric mlp->last = avp++; 7028058Seric mlp++; 70356678Seric break; 7048058Seric 70556678Seric case MATCHZANY: 70656678Seric /* match zero or more tokens */ 70758825Seric mlp->pattern = rvp; 70856678Seric mlp->first = avp; 70956678Seric mlp->last = avp - 1; 71056678Seric mlp++; 71156678Seric break; 71256326Seric 71358827Seric case MATCHZERO: 71458173Seric /* match zero tokens */ 71558173Seric break; 71658173Seric 71759027Seric case MACRODEXPAND: 71859027Seric /* 71959027Seric ** Match against run-time macro. 72059027Seric ** This algorithm is broken for the 72159027Seric ** general case (no recursive macros, 72259027Seric ** improper tokenization) but should 72359027Seric ** work for the usual cases. 72459027Seric */ 72559027Seric 72659027Seric ap = macvalue(rp[1], e); 72759027Seric mlp->first = avp; 72859027Seric if (tTd(21, 2)) 72959027Seric printf("rewrite: LHS $&%c => \"%s\"\n", 73059027Seric rp[1], 73159027Seric ap == NULL ? "(NULL)" : ap); 73259027Seric 73359027Seric if (ap == NULL) 73459027Seric break; 73559027Seric while (*ap != NULL) 73659027Seric { 73759027Seric if (*avp == NULL || 73859027Seric strncasecmp(ap, *avp, strlen(*avp)) != 0) 73959027Seric { 74059027Seric /* no match */ 74159027Seric avp = mlp->first; 74259027Seric goto backup; 74359027Seric } 74459027Seric ap += strlen(*avp++); 74559027Seric } 74659027Seric 74759027Seric /* match */ 74859027Seric break; 74959027Seric 75056678Seric default: 75156678Seric /* must have exact match */ 75256678Seric if (strcasecmp(rp, ap)) 7538058Seric goto backup; 7544468Seric avp++; 75556678Seric break; 7563149Seric } 7573149Seric 75856678Seric /* successful match on this token */ 7593149Seric rvp++; 7603149Seric continue; 7613149Seric 76258825Seric backup: 76356678Seric /* match failed -- back up */ 76458825Seric while (--mlp >= mlist) 7653149Seric { 76658825Seric rvp = mlp->pattern; 76756678Seric rp = *rvp; 76858825Seric avp = mlp->last + 1; 76958825Seric ap = *avp; 77058825Seric 77158825Seric if (tTd(21, 36)) 77258825Seric { 77358825Seric printf("BACKUP rp="); 77458825Seric xputs(rp); 77558825Seric printf(", ap="); 77658825Seric xputs(ap); 77758825Seric printf("\n"); 77858825Seric } 77958825Seric 78058825Seric if (ap == NULL) 78158825Seric { 78258825Seric /* run off the end -- back up again */ 78358825Seric continue; 78458825Seric } 78558050Seric if ((*rp & 0377) == MATCHANY || 78658050Seric (*rp & 0377) == MATCHZANY) 7874468Seric { 78856678Seric /* extend binding and continue */ 78958825Seric mlp->last = avp++; 79056678Seric rvp++; 79158825Seric mlp++; 79256678Seric break; 7934468Seric } 79458825Seric if ((*rp & 0377) == MATCHCLASS) 79556678Seric { 79658814Seric /* extend binding and try again */ 79758825Seric mlp->last = avp++; 79858814Seric goto extendclass; 79958814Seric } 8003149Seric } 8013149Seric 80258825Seric if (mlp < mlist) 80356678Seric { 80456678Seric /* total failure to match */ 80556326Seric break; 8063149Seric } 807297Seric } 8083149Seric 8093149Seric /* 81056678Seric ** See if we successfully matched 8113149Seric */ 8123149Seric 81358827Seric if (mlp < mlist || *rvp != NULL) 8143149Seric { 8159374Seric if (tTd(21, 10)) 8169374Seric printf("----- rule fails\n"); 8179374Seric rwr = rwr->r_next; 81858866Seric ruleno++; 8199374Seric continue; 8209374Seric } 8213149Seric 8229374Seric rvp = rwr->r_rhs; 8239374Seric if (tTd(21, 12)) 8249374Seric { 8259374Seric printf("-----rule matches:"); 82656678Seric printav(rvp); 8279374Seric } 8289374Seric 8299374Seric rp = *rvp; 83058050Seric if ((*rp & 0377) == CANONUSER) 8319374Seric { 8329374Seric rvp++; 8339374Seric rwr = rwr->r_next; 83458866Seric ruleno++; 8359374Seric } 83658050Seric else if ((*rp & 0377) == CANONHOST) 8379374Seric { 8389374Seric rvp++; 8399374Seric rwr = NULL; 8409374Seric } 84158050Seric else if ((*rp & 0377) == CANONNET) 8429374Seric rwr = NULL; 8439374Seric 8449374Seric /* substitute */ 8459374Seric for (avp = npvp; *rvp != NULL; rvp++) 8469374Seric { 8479374Seric register struct match *m; 8489374Seric register char **pp; 8499374Seric 8508058Seric rp = *rvp; 85158050Seric if ((*rp & 0377) == MATCHREPL) 8528058Seric { 85316914Seric /* substitute from LHS */ 85416914Seric m = &mlist[rp[1] - '1']; 85556678Seric if (m < mlist || m >= mlp) 8569374Seric { 85758151Seric syserr("554 rewrite: ruleset %d: replacement $%c out of bounds", 85856326Seric ruleset, rp[1]); 8599374Seric return; 8609374Seric } 86116914Seric if (tTd(21, 15)) 86216914Seric { 86316914Seric printf("$%c:", rp[1]); 86416914Seric pp = m->first; 86556678Seric while (pp <= m->last) 86616914Seric { 86716914Seric printf(" %x=\"", *pp); 86816914Seric (void) fflush(stdout); 86916914Seric printf("%s\"", *pp++); 87016914Seric } 87116914Seric printf("\n"); 87216914Seric } 8739374Seric pp = m->first; 87456678Seric while (pp <= m->last) 8753149Seric { 87616914Seric if (avp >= &npvp[MAXATOM]) 87756678Seric { 87858151Seric syserr("554 rewrite: expansion too long"); 87956678Seric return; 88056678Seric } 88116914Seric *avp++ = *pp++; 8823149Seric } 8833149Seric } 88416914Seric else 8858226Seric { 88616914Seric /* vanilla replacement */ 8879374Seric if (avp >= &npvp[MAXATOM]) 88816889Seric { 88956678Seric toolong: 89058151Seric syserr("554 rewrite: expansion too long"); 89116889Seric return; 89216889Seric } 89359027Seric if ((*rp & 0377) != MACRODEXPAND) 89459027Seric *avp++ = rp; 89559027Seric else 89659027Seric { 89759027Seric *avp = macvalue(rp[1], e); 89859027Seric if (tTd(21, 2)) 89959027Seric printf("rewrite: RHS $&%c => \"%s\"\n", 90059027Seric rp[1], 90159027Seric *avp == NULL ? "(NULL)" : *avp); 90259027Seric if (*avp != NULL) 90359027Seric avp++; 90459027Seric } 9058226Seric } 9069374Seric } 9079374Seric *avp++ = NULL; 90816914Seric 90916914Seric /* 91056678Seric ** Check for any hostname/keyword lookups. 91116914Seric */ 91216914Seric 91316914Seric for (rvp = npvp; *rvp != NULL; rvp++) 91416914Seric { 91556678Seric char **hbrvp; 91616914Seric char **xpvp; 91716914Seric int trsize; 91817473Seric char *olddelimchar; 91956678Seric char *replac; 92056678Seric int endtoken; 92156678Seric STAB *map; 92256678Seric char *mapname; 92356678Seric char **key_rvp; 92456678Seric char **arg_rvp; 92556678Seric char **default_rvp; 92656678Seric char buf[MAXNAME + 1]; 92716914Seric char *pvpb1[MAXATOM + 1]; 92856823Seric char *argvect[10]; 92917174Seric char pvpbuf[PSBUFSIZE]; 93016914Seric 93158050Seric if ((**rvp & 0377) != HOSTBEGIN && 93258050Seric (**rvp & 0377) != LOOKUPBEGIN) 93316914Seric continue; 93416914Seric 93516914Seric /* 93656678Seric ** Got a hostname/keyword lookup. 93716914Seric ** 93816914Seric ** This could be optimized fairly easily. 93916914Seric */ 94016914Seric 94116914Seric hbrvp = rvp; 94258050Seric if ((**rvp & 0377) == HOSTBEGIN) 94356327Seric { 94456678Seric endtoken = HOSTEND; 94556678Seric mapname = "host"; 94656327Seric } 94756326Seric else 94856327Seric { 94956678Seric endtoken = LOOKUPEND; 95056678Seric mapname = *++rvp; 95156327Seric } 95256678Seric map = stab(mapname, ST_MAP, ST_FIND); 95356678Seric if (map == NULL) 95458151Seric syserr("554 rewrite: map %s not found", mapname); 95556678Seric 95656678Seric /* extract the match part */ 95756678Seric key_rvp = ++rvp; 95856823Seric default_rvp = NULL; 95956823Seric arg_rvp = argvect; 96056823Seric xpvp = NULL; 96156823Seric replac = pvpbuf; 96258050Seric while (*rvp != NULL && (**rvp & 0377) != endtoken) 96353654Seric { 96458050Seric int nodetype = **rvp & 0377; 96556823Seric 96656823Seric if (nodetype != CANONHOST && nodetype != CANONUSER) 96756678Seric { 96856823Seric rvp++; 96956823Seric continue; 97056823Seric } 97156823Seric 97256823Seric *rvp++ = NULL; 97356823Seric 97456823Seric if (xpvp != NULL) 97556823Seric { 97658814Seric cataddr(xpvp, NULL, replac, 97758082Seric &pvpbuf[sizeof pvpbuf] - replac, 97858082Seric '\0'); 97956823Seric *++arg_rvp = replac; 98056823Seric replac += strlen(replac) + 1; 98156823Seric xpvp = NULL; 98256823Seric } 98356823Seric switch (nodetype) 98456823Seric { 98556678Seric case CANONHOST: 98656823Seric xpvp = rvp; 98756678Seric break; 98856678Seric 98956678Seric case CANONUSER: 99056678Seric default_rvp = rvp; 99156678Seric break; 99256678Seric } 99353654Seric } 99416914Seric if (*rvp != NULL) 99516914Seric *rvp++ = NULL; 99656823Seric if (xpvp != NULL) 99756823Seric { 99858814Seric cataddr(xpvp, NULL, replac, 99958082Seric &pvpbuf[sizeof pvpbuf] - replac, 100058082Seric '\0'); 100156823Seric *++arg_rvp = replac; 100256823Seric } 100356823Seric *++arg_rvp = NULL; 100416914Seric 100516914Seric /* save the remainder of the input string */ 100616914Seric trsize = (int) (avp - rvp + 1) * sizeof *rvp; 100716914Seric bcopy((char *) rvp, (char *) pvpb1, trsize); 100816914Seric 100956678Seric /* look it up */ 101058814Seric cataddr(key_rvp, NULL, buf, sizeof buf, '\0'); 101156823Seric argvect[0] = buf; 101256678Seric if (map != NULL && bitset(MF_VALID, map->s_map.map_flags)) 101356836Seric { 101456836Seric int bsize = sizeof buf - 1; 101556836Seric 101656836Seric if (map->s_map.map_app != NULL) 101756836Seric bsize -= strlen(map->s_map.map_app); 101858796Seric if (tTd(60, 1)) 101958796Seric printf("map_lookup(%s, %s) => ", 102058796Seric mapname, buf); 102156836Seric replac = (*map->s_map.map_class->map_lookup)(&map->s_map, 102256823Seric buf, sizeof buf - 1, argvect); 102356836Seric if (replac != NULL && map->s_map.map_app != NULL) 102456836Seric strcat(replac, map->s_map.map_app); 102558796Seric if (tTd(60, 1)) 102658796Seric printf("%s\n", replac ? replac : "NOT FOUND"); 102756836Seric } 102853654Seric else 102956678Seric replac = NULL; 103056678Seric 103156678Seric /* if no replacement, use default */ 103256823Seric if (replac == NULL && default_rvp != NULL) 103356823Seric { 103456823Seric char buf2[sizeof buf]; 103556823Seric 103656823Seric /* rewrite the default with % translations */ 103758814Seric cataddr(default_rvp, NULL, buf2, sizeof buf2, '\0'); 103856823Seric map_rewrite(buf2, sizeof buf2, buf, sizeof buf, 103956823Seric argvect); 104056823Seric replac = buf; 104156823Seric } 104256823Seric 104356678Seric if (replac == NULL) 104451317Seric { 104556823Seric xpvp = key_rvp; 104653654Seric } 104756678Seric else 104853654Seric { 104956678Seric /* scan the new replacement */ 105058333Seric xpvp = prescan(replac, '\0', pvpbuf, NULL); 105153654Seric if (xpvp == NULL) 105251317Seric { 105358403Seric /* prescan already printed error */ 105456678Seric return; 105556678Seric } 105651317Seric } 105751317Seric 105816914Seric /* append it to the token list */ 105956678Seric for (avp = hbrvp; *xpvp != NULL; xpvp++) 106056678Seric { 106117174Seric *avp++ = newstr(*xpvp); 106216920Seric if (avp >= &npvp[MAXATOM]) 106316914Seric goto toolong; 106417174Seric } 106516914Seric 106616914Seric /* restore the old trailing information */ 106756678Seric for (xpvp = pvpb1; (*avp++ = *xpvp++) != NULL; ) 106816920Seric if (avp >= &npvp[MAXATOM]) 106916914Seric goto toolong; 107017174Seric 107156678Seric break; 107216914Seric } 107316914Seric 107416914Seric /* 107516914Seric ** Check for subroutine calls. 107616914Seric */ 107716914Seric 107858050Seric if (*npvp != NULL && (**npvp & 0377) == CALLSUBR) 107956678Seric { 108056678Seric bcopy((char *) &npvp[2], (char *) pvp, 108156678Seric (int) (avp - npvp - 2) * sizeof *avp); 108256678Seric if (tTd(21, 3)) 108356678Seric printf("-----callsubr %s\n", npvp[1]); 108459027Seric rewrite(pvp, atoi(npvp[1]), e); 108556678Seric } 108656678Seric else 108756678Seric { 108817348Seric bcopy((char *) npvp, (char *) pvp, 108916900Seric (int) (avp - npvp) * sizeof *avp); 109056678Seric } 10919374Seric if (tTd(21, 4)) 10929374Seric { 10939374Seric printf("rewritten as:"); 109456678Seric printav(pvp); 10959374Seric } 1096297Seric } 10978069Seric 10989279Seric if (OpMode == MD_TEST || tTd(21, 2)) 10998069Seric { 11008959Seric printf("rewrite: ruleset %2d returns:", ruleset); 110156678Seric printav(pvp); 11028069Seric } 11033149Seric } 11043149Seric /* 11053149Seric ** BUILDADDR -- build address from token vector. 11063149Seric ** 11073149Seric ** Parameters: 11083149Seric ** tv -- token vector. 11093149Seric ** a -- pointer to address descriptor to fill. 11103149Seric ** If NULL, one will be allocated. 111158966Seric ** e -- the current envelope. 11123149Seric ** 11133149Seric ** Returns: 11144279Seric ** NULL if there was an error. 11154279Seric ** 'a' otherwise. 11163149Seric ** 11173149Seric ** Side Effects: 11183149Seric ** fills in 'a' 11193149Seric */ 11203149Seric 112157249Seric struct errcodes 112257249Seric { 112357249Seric char *ec_name; /* name of error code */ 112457249Seric int ec_code; /* numeric code */ 112557249Seric } ErrorCodes[] = 112657249Seric { 112757249Seric "usage", EX_USAGE, 112857249Seric "nouser", EX_NOUSER, 112957249Seric "nohost", EX_NOHOST, 113057249Seric "unavailable", EX_UNAVAILABLE, 113157249Seric "software", EX_SOFTWARE, 113257249Seric "tempfail", EX_TEMPFAIL, 113357249Seric "protocol", EX_PROTOCOL, 113457249Seric #ifdef EX_CONFIG 113557249Seric "config", EX_CONFIG, 113657249Seric #endif 113757249Seric NULL, EX_UNAVAILABLE, 113857249Seric }; 113957249Seric 114056678Seric ADDRESS * 114158966Seric buildaddr(tv, a, e) 11423149Seric register char **tv; 11433149Seric register ADDRESS *a; 114458966Seric register ENVELOPE *e; 11453149Seric { 11463149Seric struct mailer **mp; 11473149Seric register struct mailer *m; 114858008Seric char *bp; 114958008Seric int spaceleft; 115057402Seric static char buf[MAXNAME]; 11513149Seric 11523149Seric if (a == NULL) 11533149Seric a = (ADDRESS *) xalloc(sizeof *a); 115416889Seric bzero((char *) a, sizeof *a); 11553149Seric 11563149Seric /* figure out what net/mailer to use */ 115758050Seric if ((**tv & 0377) != CANONNET) 11584279Seric { 115958151Seric syserr("554 buildaddr: no net"); 11604279Seric return (NULL); 11614279Seric } 11623149Seric tv++; 116358680Seric if (strcasecmp(*tv, "error") == 0) 11644279Seric { 116558050Seric if ((**++tv & 0377) == CANONHOST) 116610183Seric { 116757249Seric register struct errcodes *ep; 116857249Seric 116958050Seric if (isascii(**++tv) && isdigit(**tv)) 117057249Seric { 117157249Seric setstat(atoi(*tv)); 117257249Seric } 117357249Seric else 117457249Seric { 117557249Seric for (ep = ErrorCodes; ep->ec_name != NULL; ep++) 117657249Seric if (strcasecmp(ep->ec_name, *tv) == 0) 117757249Seric break; 117857249Seric setstat(ep->ec_code); 117957249Seric } 118010183Seric tv++; 118110183Seric } 118258050Seric if ((**tv & 0377) != CANONUSER) 118358151Seric syserr("554 buildaddr: error: no user"); 118458814Seric cataddr(++tv, NULL, buf, sizeof buf, ' '); 118558082Seric stripquotes(buf); 11864279Seric usrerr(buf); 118758966Seric if (e->e_message == NULL) 118858966Seric e->e_message = newstr(buf); 11894279Seric return (NULL); 11904279Seric } 119157402Seric 11924598Seric for (mp = Mailer; (m = *mp++) != NULL; ) 11933149Seric { 119458680Seric if (strcasecmp(m->m_name, *tv) == 0) 11953149Seric break; 11963149Seric } 11973149Seric if (m == NULL) 11984279Seric { 119958151Seric syserr("554 buildaddr: unknown mailer %s", *tv); 12004279Seric return (NULL); 12014279Seric } 12024598Seric a->q_mailer = m; 12033149Seric 12043149Seric /* figure out what host (if any) */ 120556678Seric tv++; 120658509Seric if ((**tv & 0377) == CANONHOST) 12073149Seric { 120858008Seric bp = buf; 120958008Seric spaceleft = sizeof buf - 1; 121058050Seric while (*++tv != NULL && (**tv & 0377) != CANONUSER) 121158008Seric { 121258008Seric int i = strlen(*tv); 121358008Seric 121458008Seric if (i > spaceleft) 121558008Seric { 121658008Seric /* out of space for this address */ 121758008Seric if (spaceleft >= 0) 121858151Seric syserr("554 buildaddr: host too long (%.40s...)", 121958008Seric buf); 122058008Seric i = spaceleft; 122158008Seric spaceleft = 0; 122258008Seric } 122358008Seric if (i <= 0) 122458008Seric continue; 122558008Seric bcopy(*tv, bp, i); 122658008Seric bp += i; 122758008Seric spaceleft -= i; 122858008Seric } 122958010Seric *bp = '\0'; 12305704Seric a->q_host = newstr(buf); 12313149Seric } 123257249Seric else 123358509Seric { 123458509Seric if (!bitnset(M_LOCALMAILER, m->m_flags)) 123558509Seric { 123658509Seric syserr("554 buildaddr: no host"); 123758509Seric return (NULL); 123858509Seric } 123957249Seric a->q_host = NULL; 124058509Seric } 12413149Seric 12423149Seric /* figure out the user */ 124358050Seric if (*tv == NULL || (**tv & 0377) != CANONUSER) 12444279Seric { 124558151Seric syserr("554 buildaddr: no user"); 12464279Seric return (NULL); 12474279Seric } 124857402Seric tv++; 124951317Seric 125057402Seric /* do special mapping for local mailer */ 125157402Seric if (m == LocalMailer && *tv != NULL) 125257402Seric { 125357454Seric register char *p = *tv; 125457454Seric 125557454Seric if (*p == '"') 125657454Seric p++; 125757454Seric if (*p == '|') 125857402Seric a->q_mailer = m = ProgMailer; 125957454Seric else if (*p == '/') 126057402Seric a->q_mailer = m = FileMailer; 126157454Seric else if (*p == ':') 126257402Seric { 126357402Seric /* may be :include: */ 126458814Seric cataddr(tv, NULL, buf, sizeof buf, '\0'); 126558008Seric stripquotes(buf); 126658008Seric if (strncasecmp(buf, ":include:", 9) == 0) 126758008Seric { 126858008Seric /* if :include:, don't need further rewriting */ 126957402Seric a->q_mailer = m = InclMailer; 127058008Seric a->q_user = &buf[9]; 127158008Seric return (a); 127258008Seric } 127357402Seric } 127457402Seric } 127557402Seric 127658008Seric if (m == LocalMailer && *tv != NULL && strcmp(*tv, "@") == 0) 127758008Seric { 127858008Seric tv++; 127958008Seric a->q_flags |= QNOTREMOTE; 128058008Seric } 128158008Seric 128258673Seric /* do cleanup of final address */ 128359027Seric rewrite(tv, 4, e); 128419040Seric 128519040Seric /* save the result for the command line/RCPT argument */ 128658814Seric cataddr(tv, NULL, buf, sizeof buf, '\0'); 12873149Seric a->q_user = buf; 12883149Seric 128958670Seric /* 129058670Seric ** Do mapping to lower case as requested by mailer 129158670Seric */ 129258670Seric 129358670Seric if (a->q_host != NULL && !bitnset(M_HST_UPPER, m->m_flags)) 129458670Seric makelower(a->q_host); 129558670Seric if (!bitnset(M_USR_UPPER, m->m_flags)) 129658670Seric makelower(a->q_user); 129758670Seric 12983149Seric return (a); 12993149Seric } 13003188Seric /* 13014228Seric ** CATADDR -- concatenate pieces of addresses (putting in <LWSP> subs) 13024228Seric ** 13034228Seric ** Parameters: 13044228Seric ** pvp -- parameter vector to rebuild. 130558814Seric ** evp -- last parameter to include. Can be NULL to 130658814Seric ** use entire pvp. 13074228Seric ** buf -- buffer to build the string into. 13084228Seric ** sz -- size of buf. 130958082Seric ** spacesub -- the space separator character; if null, 131058082Seric ** use SpaceSub. 13114228Seric ** 13124228Seric ** Returns: 13134228Seric ** none. 13144228Seric ** 13154228Seric ** Side Effects: 13164228Seric ** Destroys buf. 13174228Seric */ 13184228Seric 131958814Seric cataddr(pvp, evp, buf, sz, spacesub) 13204228Seric char **pvp; 132158814Seric char **evp; 13224228Seric char *buf; 13234228Seric register int sz; 132458082Seric char spacesub; 13254228Seric { 13264228Seric bool oatomtok = FALSE; 132756678Seric bool natomtok = FALSE; 13284228Seric register int i; 13294228Seric register char *p; 13304228Seric 133158082Seric if (spacesub == '\0') 133258082Seric spacesub = SpaceSub; 133358082Seric 13348423Seric if (pvp == NULL) 13358423Seric { 133623109Seric (void) strcpy(buf, ""); 13378423Seric return; 13388423Seric } 13394228Seric p = buf; 134011156Seric sz -= 2; 13414228Seric while (*pvp != NULL && (i = strlen(*pvp)) < sz) 13424228Seric { 13438078Seric natomtok = (toktype(**pvp) == ATM); 13444228Seric if (oatomtok && natomtok) 134558082Seric *p++ = spacesub; 13464228Seric (void) strcpy(p, *pvp); 13474228Seric oatomtok = natomtok; 13484228Seric p += i; 134911156Seric sz -= i + 1; 135058814Seric if (pvp++ == evp) 135158814Seric break; 13524228Seric } 13534228Seric *p = '\0'; 13544228Seric } 13554228Seric /* 13563188Seric ** SAMEADDR -- Determine if two addresses are the same 13573188Seric ** 13583188Seric ** This is not just a straight comparison -- if the mailer doesn't 13593188Seric ** care about the host we just ignore it, etc. 13603188Seric ** 13613188Seric ** Parameters: 13623188Seric ** a, b -- pointers to the internal forms to compare. 13633188Seric ** 13643188Seric ** Returns: 13653188Seric ** TRUE -- they represent the same mailbox. 13663188Seric ** FALSE -- they don't. 13673188Seric ** 13683188Seric ** Side Effects: 13693188Seric ** none. 13703188Seric */ 13713188Seric 13723188Seric bool 13739374Seric sameaddr(a, b) 13743188Seric register ADDRESS *a; 13753188Seric register ADDRESS *b; 13763188Seric { 13773188Seric /* if they don't have the same mailer, forget it */ 13783188Seric if (a->q_mailer != b->q_mailer) 13793188Seric return (FALSE); 13803188Seric 13813188Seric /* if the user isn't the same, we can drop out */ 138256678Seric if (strcmp(a->q_user, b->q_user) != 0) 13833188Seric return (FALSE); 13843188Seric 138558438Seric /* if we have good uids for both but the differ, these are different */ 138658438Seric if (bitset(QGOODUID, a->q_flags & b->q_flags) && a->q_uid != b->q_uid) 138758438Seric return (FALSE); 138858438Seric 138958509Seric /* otherwise compare hosts (but be careful for NULL ptrs) */ 139058509Seric if (a->q_host == b->q_host) 139158509Seric { 139258509Seric /* probably both null pointers */ 13933188Seric return (TRUE); 139458509Seric } 13953188Seric if (a->q_host == NULL || b->q_host == NULL) 139658509Seric { 139758509Seric /* only one is a null pointer */ 13983188Seric return (FALSE); 139958509Seric } 140056678Seric if (strcmp(a->q_host, b->q_host) != 0) 14013188Seric return (FALSE); 14023188Seric 14033188Seric return (TRUE); 14043188Seric } 14053234Seric /* 14063234Seric ** PRINTADDR -- print address (for debugging) 14073234Seric ** 14083234Seric ** Parameters: 14093234Seric ** a -- the address to print 14103234Seric ** follow -- follow the q_next chain. 14113234Seric ** 14123234Seric ** Returns: 14133234Seric ** none. 14143234Seric ** 14153234Seric ** Side Effects: 14163234Seric ** none. 14173234Seric */ 14183234Seric 14193234Seric printaddr(a, follow) 14203234Seric register ADDRESS *a; 14213234Seric bool follow; 14223234Seric { 14235001Seric bool first = TRUE; 142457731Seric register MAILER *m; 142557731Seric MAILER pseudomailer; 14265001Seric 14273234Seric while (a != NULL) 14283234Seric { 14295001Seric first = FALSE; 14304443Seric printf("%x=", a); 14314085Seric (void) fflush(stdout); 143257731Seric 143357731Seric /* find the mailer -- carefully */ 143457731Seric m = a->q_mailer; 143557731Seric if (m == NULL) 143657731Seric { 143757731Seric m = &pseudomailer; 143857731Seric m->m_mno = -1; 143957731Seric m->m_name = "NULL"; 144057731Seric } 144157731Seric 144258680Seric printf("%s:\n\tmailer %d (%s), host `%s', user `%s', ruser `%s'\n", 144357731Seric a->q_paddr, m->m_mno, m->m_name, 144440973Sbostic a->q_host, a->q_user, a->q_ruser? a->q_ruser: "<null>"); 14458181Seric printf("\tnext=%x, flags=%o, alias %x\n", a->q_next, a->q_flags, 14468181Seric a->q_alias); 14478181Seric printf("\thome=\"%s\", fullname=\"%s\"\n", a->q_home, 14488181Seric a->q_fullname); 14494996Seric 14503234Seric if (!follow) 14513234Seric return; 14524996Seric a = a->q_next; 14533234Seric } 14545001Seric if (first) 14554443Seric printf("[NULL]\n"); 14563234Seric } 14574317Seric 14587682Seric /* 14597682Seric ** REMOTENAME -- return the name relative to the current mailer 14607682Seric ** 14617682Seric ** Parameters: 14627682Seric ** name -- the name to translate. 14638069Seric ** m -- the mailer that we want to do rewriting relative 14648069Seric ** to. 14658069Seric ** senderaddress -- if set, uses the sender rewriting rules 14668069Seric ** rather than the recipient rewriting rules. 146758020Seric ** header -- set if this address is in the header, rather 146858020Seric ** than an envelope header. 146910310Seric ** canonical -- if set, strip out any comment information, 147010310Seric ** etc. 147158509Seric ** adddomain -- if set, OK to do domain extension. 147258020Seric ** e -- the current envelope. 14737682Seric ** 14747682Seric ** Returns: 14757682Seric ** the text string representing this address relative to 14767682Seric ** the receiving mailer. 14777682Seric ** 14787682Seric ** Side Effects: 14797682Seric ** none. 14807682Seric ** 14817682Seric ** Warnings: 14827682Seric ** The text string returned is tucked away locally; 14837682Seric ** copy it if you intend to save it. 14847682Seric */ 14857682Seric 14867682Seric char * 148758509Seric remotename(name, m, senderaddress, header, canonical, adddomain, e) 14887682Seric char *name; 148956678Seric struct mailer *m; 14908069Seric bool senderaddress; 149158020Seric bool header; 149210310Seric bool canonical; 149358509Seric bool adddomain; 149456678Seric register ENVELOPE *e; 14957682Seric { 14968069Seric register char **pvp; 14978069Seric char *fancy; 149856678Seric extern char *macvalue(); 149956678Seric char *oldg = macvalue('g', e); 150058020Seric int rwset; 15017682Seric static char buf[MAXNAME]; 15027682Seric char lbuf[MAXNAME]; 150316914Seric char pvpbuf[PSBUFSIZE]; 150456678Seric extern char **prescan(); 150556678Seric extern char *crackaddr(); 15067682Seric 15077755Seric if (tTd(12, 1)) 15087755Seric printf("remotename(%s)\n", name); 15097755Seric 151010177Seric /* don't do anything if we are tagging it as special */ 151158020Seric if (senderaddress) 151258020Seric rwset = header ? m->m_sh_rwset : m->m_se_rwset; 151358020Seric else 151458020Seric rwset = header ? m->m_rh_rwset : m->m_re_rwset; 151558020Seric if (rwset < 0) 151610177Seric return (name); 151710177Seric 15187682Seric /* 15198181Seric ** Do a heuristic crack of this name to extract any comment info. 15208181Seric ** This will leave the name as a comment and a $g macro. 15217889Seric */ 15227889Seric 152358036Seric if (canonical || bitnset(M_NOCOMMENT, m->m_flags)) 152458050Seric fancy = "\201g"; 152510310Seric else 152610310Seric fancy = crackaddr(name); 15277889Seric 15288181Seric /* 15298181Seric ** Turn the name into canonical form. 15308181Seric ** Normally this will be RFC 822 style, i.e., "user@domain". 15318181Seric ** If this only resolves to "user", and the "C" flag is 15328181Seric ** specified in the sending mailer, then the sender's 15338181Seric ** domain will be appended. 15348181Seric */ 15358181Seric 153658333Seric pvp = prescan(name, '\0', pvpbuf, NULL); 15377889Seric if (pvp == NULL) 15387889Seric return (name); 153959027Seric rewrite(pvp, 3, e); 154058509Seric if (adddomain && e->e_fromdomain != NULL) 15418181Seric { 15428181Seric /* append from domain to this address */ 15438181Seric register char **pxp = pvp; 15448181Seric 15459594Seric /* see if there is an "@domain" in the current name */ 15468181Seric while (*pxp != NULL && strcmp(*pxp, "@") != 0) 15478181Seric pxp++; 15488181Seric if (*pxp == NULL) 15498181Seric { 15509594Seric /* no.... append the "@domain" from the sender */ 155156678Seric register char **qxq = e->e_fromdomain; 15528181Seric 15539594Seric while ((*pxp++ = *qxq++) != NULL) 15549594Seric continue; 155559027Seric rewrite(pvp, 3, e); 15568181Seric } 15578181Seric } 15588181Seric 15598181Seric /* 15608959Seric ** Do more specific rewriting. 156156678Seric ** Rewrite using ruleset 1 or 2 depending on whether this is 156256678Seric ** a sender address or not. 15638181Seric ** Then run it through any receiving-mailer-specific rulesets. 15648181Seric */ 15658181Seric 15668069Seric if (senderaddress) 156759027Seric rewrite(pvp, 1, e); 15688069Seric else 156959027Seric rewrite(pvp, 2, e); 157058020Seric if (rwset > 0) 157159027Seric rewrite(pvp, rwset, e); 15727682Seric 15738181Seric /* 15748959Seric ** Do any final sanitation the address may require. 15758959Seric ** This will normally be used to turn internal forms 15768959Seric ** (e.g., user@host.LOCAL) into external form. This 15778959Seric ** may be used as a default to the above rules. 15788959Seric */ 15798959Seric 158059027Seric rewrite(pvp, 4, e); 15818959Seric 15828959Seric /* 15838181Seric ** Now restore the comment information we had at the beginning. 15848181Seric */ 15858181Seric 158658825Seric cataddr(pvp, NULL, lbuf, sizeof lbuf, '\0'); 158756678Seric define('g', lbuf, e); 158856678Seric expand(fancy, buf, &buf[sizeof buf - 1], e); 158956678Seric define('g', oldg, e); 15907682Seric 15917682Seric if (tTd(12, 1)) 15927755Seric printf("remotename => `%s'\n", buf); 15937682Seric return (buf); 15947682Seric } 159551317Seric /* 159656678Seric ** MAPLOCALUSER -- run local username through ruleset 5 for final redirection 159751317Seric ** 159851317Seric ** Parameters: 159956678Seric ** a -- the address to map (but just the user name part). 160056678Seric ** sendq -- the sendq in which to install any replacement 160156678Seric ** addresses. 160251317Seric ** 160351317Seric ** Returns: 160451317Seric ** none. 160551317Seric */ 160651317Seric 160756678Seric maplocaluser(a, sendq, e) 160856678Seric register ADDRESS *a; 160956678Seric ADDRESS **sendq; 161056678Seric ENVELOPE *e; 161151317Seric { 161256678Seric register char **pvp; 161356678Seric register ADDRESS *a1 = NULL; 161458333Seric auto char *delimptr; 161556678Seric char pvpbuf[PSBUFSIZE]; 161651317Seric 161756678Seric if (tTd(29, 1)) 161856678Seric { 161956678Seric printf("maplocaluser: "); 162056678Seric printaddr(a, FALSE); 162156678Seric } 162258333Seric pvp = prescan(a->q_user, '\0', pvpbuf, &delimptr); 162356678Seric if (pvp == NULL) 162456678Seric return; 162551317Seric 162659027Seric rewrite(pvp, 5, e); 162758050Seric if (pvp[0] == NULL || (pvp[0][0] & 0377) != CANONNET) 162856678Seric return; 162951317Seric 163056678Seric /* if non-null, mailer destination specified -- has it changed? */ 163158966Seric a1 = buildaddr(pvp, NULL, e); 163256678Seric if (a1 == NULL || sameaddr(a, a1)) 163356678Seric return; 163451317Seric 163556678Seric /* mark old address as dead; insert new address */ 163656678Seric a->q_flags |= QDONTSEND; 163757731Seric if (tTd(29, 5)) 163857731Seric { 163957731Seric printf("maplocaluser: QDONTSEND "); 164057731Seric printaddr(a, FALSE); 164157731Seric } 164256678Seric a1->q_alias = a; 164358333Seric allocaddr(a1, 1, NULL, delimptr); 164456678Seric (void) recipient(a1, sendq, e); 164551317Seric } 164658802Seric /* 164758802Seric ** DEQUOTE_INIT -- initialize dequote map 164858802Seric ** 164958802Seric ** This is a no-op. 165058802Seric ** 165158802Seric ** Parameters: 165258802Seric ** map -- the internal map structure. 165358802Seric ** mapname -- the name of the mapl. 165458802Seric ** args -- arguments. 165558802Seric ** 165658802Seric ** Returns: 165758802Seric ** TRUE. 165858802Seric */ 165958802Seric 166058802Seric bool 166158802Seric dequote_init(map, mapname, args) 166258802Seric MAP *map; 166358802Seric char *mapname; 166458802Seric char *args; 166558802Seric { 166658805Seric register char *p = args; 166758805Seric 166858805Seric for (;;) 166958805Seric { 167058805Seric while (isascii(*p) && isspace(*p)) 167158805Seric p++; 167258805Seric if (*p != '-') 167358805Seric break; 167458805Seric switch (*++p) 167558805Seric { 167658805Seric case 'a': 167758805Seric map->map_app = ++p; 167858805Seric break; 167958805Seric } 168058805Seric while (*p != '\0' && !(isascii(*p) && isspace(*p))) 168158805Seric p++; 168258805Seric if (*p != '\0') 168358805Seric *p = '\0'; 168458805Seric } 168558805Seric if (map->map_app != NULL) 168658805Seric map->map_app = newstr(map->map_app); 168758805Seric 168858802Seric return TRUE; 168958802Seric } 169058802Seric /* 169158802Seric ** DEQUOTE_MAP -- unquote an address 169258802Seric ** 169358802Seric ** Parameters: 169458802Seric ** map -- the internal map structure (ignored). 169558802Seric ** buf -- the buffer to dequote. 169658802Seric ** bufsiz -- the size of that buffer. 169758802Seric ** av -- arguments (ignored). 169858802Seric ** 169958802Seric ** Returns: 170058802Seric ** NULL -- if there were no quotes, or if the resulting 170158802Seric ** unquoted buffer would not be acceptable to prescan. 170258802Seric ** else -- The dequoted buffer. 170358802Seric */ 170458802Seric 170558802Seric char * 170658802Seric dequote_map(map, buf, bufsiz, av) 170758802Seric MAP *map; 170858802Seric char buf[]; 170958802Seric int bufsiz; 171058802Seric char **av; 171158802Seric { 171258802Seric register char *p; 171358802Seric register char *q; 171458802Seric register char c; 171558802Seric int anglecnt; 171658802Seric int cmntcnt; 171758802Seric int quotecnt; 171858802Seric bool quotemode; 171958802Seric bool bslashmode; 172058802Seric 172158802Seric anglecnt = 0; 172258802Seric cmntcnt = 0; 172358802Seric quotecnt = 0; 172458802Seric quotemode = FALSE; 172558802Seric bslashmode = FALSE; 172658802Seric 172758802Seric for (p = q = buf; (c = *p++) != '\0'; ) 172858802Seric { 172958802Seric if (bslashmode) 173058802Seric { 173158802Seric bslashmode = FALSE; 173258802Seric *q++ = c; 173358802Seric continue; 173458802Seric } 173558802Seric 173658802Seric switch (c) 173758802Seric { 173858802Seric case '\\': 173958802Seric bslashmode = TRUE; 174058802Seric break; 174158802Seric 174258802Seric case '(': 174358802Seric cmntcnt++; 174458802Seric break; 174558802Seric 174658802Seric case ')': 174758802Seric if (cmntcnt-- <= 0) 174858802Seric return NULL; 174958802Seric break; 175058802Seric } 175158802Seric 175258802Seric if (cmntcnt > 0) 175358802Seric { 175458802Seric *q++ = c; 175558802Seric continue; 175658802Seric } 175758802Seric 175858802Seric switch (c) 175958802Seric { 176058802Seric case '"': 176158802Seric quotemode = !quotemode; 176258802Seric quotecnt++; 176358802Seric continue; 176458802Seric 176558802Seric case '<': 176658802Seric anglecnt++; 176758802Seric break; 176858802Seric 176958802Seric case '>': 177058802Seric if (anglecnt-- <= 0) 177158802Seric return NULL; 177258802Seric break; 177358802Seric } 177458802Seric *q++ = c; 177558802Seric } 177658802Seric 177758802Seric if (anglecnt != 0 || cmntcnt != 0 || bslashmode || 177858802Seric quotemode || quotecnt <= 0) 177958802Seric return NULL; 178058802Seric *q++ = '\0'; 178158802Seric return buf; 178258802Seric } 1783