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*59595Seric static char sccsid[] = "@(#)parseaddr.c 6.47 (Berkeley) 04/30/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 */ 5759278Seric # define DELIMCHARS "()<>,;\r\n" /* default 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; 7059084Seric bool queueup; 7116914Seric char pvpbuf[PSBUFSIZE]; 7256678Seric extern char **prescan(); 7356678Seric extern ADDRESS *buildaddr(); 7457388Seric extern bool invalidaddr(); 75297Seric 76297Seric /* 77297Seric ** Initialize and prescan address. 78297Seric */ 79297Seric 8056678Seric e->e_to = addr; 817675Seric if (tTd(20, 1)) 829888Seric printf("\n--parseaddr(%s)\n", addr); 833188Seric 8457388Seric if (invalidaddr(addr)) 8557388Seric { 8657388Seric if (tTd(20, 1)) 8757388Seric printf("parseaddr-->bad address\n"); 8857388Seric return NULL; 8957388Seric } 9057388Seric 9158333Seric if (delimptr == NULL) 9258333Seric delimptr = &delimptrbuf; 9358333Seric 9458333Seric pvp = prescan(addr, delim, pvpbuf, delimptr); 953149Seric if (pvp == NULL) 9656729Seric { 9756729Seric if (tTd(20, 1)) 9856729Seric printf("parseaddr-->NULL\n"); 99297Seric return (NULL); 10056729Seric } 101297Seric 102297Seric /* 1033149Seric ** Apply rewriting rules. 1047889Seric ** Ruleset 0 does basic parsing. It must resolve. 105297Seric */ 106297Seric 10759084Seric queueup = FALSE; 10859084Seric if (rewrite(pvp, 3, e) == EX_TEMPFAIL) 10959084Seric queueup = TRUE; 11059084Seric if (rewrite(pvp, 0, e) == EX_TEMPFAIL) 11159084Seric queueup = TRUE; 112297Seric 1133149Seric /* 1143149Seric ** See if we resolved to a real mailer. 1153149Seric */ 116297Seric 11759040Seric if (pvp[0] == NULL || (pvp[0][0] & 0377) != CANONNET) 1183149Seric { 1193149Seric setstat(EX_USAGE); 12058151Seric syserr("554 cannot resolve name"); 1213149Seric return (NULL); 122297Seric } 123297Seric 124297Seric /* 1253149Seric ** Build canonical address from pvp. 126297Seric */ 127297Seric 12858966Seric a = buildaddr(pvp, a, e); 1294279Seric if (a == NULL) 1304279Seric return (NULL); 131297Seric 132297Seric /* 1333149Seric ** Make local copies of the host & user and then 1343149Seric ** transport them out. 135297Seric */ 136297Seric 13758333Seric allocaddr(a, copyf, addr, *delimptr); 13856678Seric 13956678Seric /* 14059084Seric ** If there was a parsing failure, mark it for queueing. 14159084Seric */ 14259084Seric 14359084Seric if (queueup) 144*59595Seric { 145*59595Seric if (tTd(20, 1)) 146*59595Seric printf("parseaddr: queuing message\n"); 147*59595Seric message("Transient parse error -- message queued for future delivery"); 14859084Seric a->q_flags |= QQUEUEUP; 149*59595Seric } 15059084Seric 15159084Seric /* 15256678Seric ** Compute return value. 15356678Seric */ 15456678Seric 15556678Seric if (tTd(20, 1)) 1568078Seric { 15756678Seric printf("parseaddr-->"); 15856678Seric printaddr(a, FALSE); 15956678Seric } 16056678Seric 16156678Seric return (a); 16256678Seric } 16356678Seric /* 16457388Seric ** INVALIDADDR -- check for address containing meta-characters 16557388Seric ** 16657388Seric ** Parameters: 16757388Seric ** addr -- the address to check. 16857388Seric ** 16957388Seric ** Returns: 17057388Seric ** TRUE -- if the address has any "wierd" characters 17157388Seric ** FALSE -- otherwise. 17257388Seric */ 17357388Seric 17457388Seric bool 17557388Seric invalidaddr(addr) 17657388Seric register char *addr; 17757388Seric { 17857388Seric for (; *addr != '\0'; addr++) 17957388Seric { 18058050Seric if ((*addr & 0340) != 0200) 18157388Seric continue; 18257388Seric setstat(EX_USAGE); 18358151Seric usrerr("553 Address contained invalid control characters"); 18457388Seric return TRUE; 18557388Seric } 18657388Seric return FALSE; 18757388Seric } 18857388Seric /* 18956678Seric ** ALLOCADDR -- do local allocations of address on demand. 19056678Seric ** 19156678Seric ** Also lowercases the host name if requested. 19256678Seric ** 19356678Seric ** Parameters: 19456678Seric ** a -- the address to reallocate. 19556678Seric ** copyf -- the copy flag (see parseaddr for description). 19656678Seric ** paddr -- the printname of the address. 19758333Seric ** delimptr -- a pointer to the address delimiter. Must be set. 19856678Seric ** 19956678Seric ** Returns: 20056678Seric ** none. 20156678Seric ** 20256678Seric ** Side Effects: 20356678Seric ** Copies portions of a into local buffers as requested. 20456678Seric */ 20556678Seric 20658333Seric allocaddr(a, copyf, paddr, delimptr) 20756678Seric register ADDRESS *a; 20856678Seric int copyf; 20956678Seric char *paddr; 21058333Seric char *delimptr; 21156678Seric { 21256678Seric register MAILER *m = a->q_mailer; 21356678Seric 21458673Seric if (tTd(24, 4)) 21558673Seric printf("allocaddr(copyf=%d, paddr=%s)\n", copyf, paddr); 21658673Seric 21756678Seric if (copyf > 0 && paddr != NULL) 21856678Seric { 21958333Seric char savec = *delimptr; 2208078Seric 22158333Seric *delimptr = '\0'; 22256678Seric a->q_paddr = newstr(paddr); 22358333Seric *delimptr = savec; 2248078Seric } 225297Seric else 22656678Seric a->q_paddr = paddr; 22724944Seric 22824944Seric if (a->q_user == NULL) 22924944Seric a->q_user = ""; 23024944Seric if (a->q_host == NULL) 23124944Seric a->q_host = ""; 23224944Seric 2333149Seric if (copyf >= 0) 234297Seric { 23524944Seric a->q_host = newstr(a->q_host); 2363149Seric if (a->q_user != a->q_paddr) 2373149Seric a->q_user = newstr(a->q_user); 238297Seric } 239297Seric 24056678Seric if (a->q_paddr == NULL) 24156678Seric a->q_paddr = a->q_user; 242297Seric } 243297Seric /* 244297Seric ** PRESCAN -- Prescan name and make it canonical 245297Seric ** 2469374Seric ** Scans a name and turns it into a set of tokens. This process 2479374Seric ** deletes blanks and comments (in parentheses). 248297Seric ** 249297Seric ** This routine knows about quoted strings and angle brackets. 250297Seric ** 251297Seric ** There are certain subtleties to this routine. The one that 252297Seric ** comes to mind now is that backslashes on the ends of names 253297Seric ** are silently stripped off; this is intentional. The problem 254297Seric ** is that some versions of sndmsg (like at LBL) set the kill 255297Seric ** character to something other than @ when reading addresses; 256297Seric ** so people type "csvax.eric\@berkeley" -- which screws up the 257297Seric ** berknet mailer. 258297Seric ** 259297Seric ** Parameters: 260297Seric ** addr -- the name to chomp. 261297Seric ** delim -- the delimiter for the address, normally 262297Seric ** '\0' or ','; \0 is accepted in any case. 26315284Seric ** If '\t' then we are reading the .cf file. 26416914Seric ** pvpbuf -- place to put the saved text -- note that 26516914Seric ** the pointers are static. 26658333Seric ** delimptr -- if non-NULL, set to the location of the 26758333Seric ** terminating delimiter. 268297Seric ** 269297Seric ** Returns: 2703149Seric ** A pointer to a vector of tokens. 271297Seric ** NULL on error. 272297Seric */ 273297Seric 2748078Seric /* states and character types */ 2758078Seric # define OPR 0 /* operator */ 2768078Seric # define ATM 1 /* atom */ 2778078Seric # define QST 2 /* in quoted string */ 2788078Seric # define SPC 3 /* chewing up spaces */ 2798078Seric # define ONE 4 /* pick up one character */ 2803149Seric 2818078Seric # define NSTATES 5 /* number of states */ 2828078Seric # define TYPE 017 /* mask to select state type */ 2838078Seric 2848078Seric /* meta bits for table */ 2858078Seric # define M 020 /* meta character; don't pass through */ 2868078Seric # define B 040 /* cause a break */ 2878078Seric # define MB M|B /* meta-break */ 2888078Seric 2898078Seric static short StateTab[NSTATES][NSTATES] = 2908078Seric { 2918087Seric /* oldst chtype> OPR ATM QST SPC ONE */ 2929051Seric /*OPR*/ OPR|B, ATM|B, QST|B, SPC|MB, ONE|B, 2939051Seric /*ATM*/ OPR|B, ATM, QST|B, SPC|MB, ONE|B, 2949051Seric /*QST*/ QST, QST, OPR, QST, QST, 2958078Seric /*SPC*/ OPR, ATM, QST, SPC|M, ONE, 2968078Seric /*ONE*/ OPR, OPR, OPR, OPR, OPR, 2978078Seric }; 2988078Seric 2998078Seric # define NOCHAR -1 /* signal nothing in lookahead token */ 3008078Seric 3013149Seric char ** 30258333Seric prescan(addr, delim, pvpbuf, delimptr) 303297Seric char *addr; 304297Seric char delim; 30516914Seric char pvpbuf[]; 30658333Seric char **delimptr; 307297Seric { 308297Seric register char *p; 3098078Seric register char *q; 3109346Seric register int c; 3113149Seric char **avp; 312297Seric bool bslashmode; 313297Seric int cmntcnt; 3148423Seric int anglecnt; 3153149Seric char *tok; 3168078Seric int state; 3178078Seric int newstate; 3188078Seric static char *av[MAXATOM+1]; 31956678Seric extern int errno; 320297Seric 32115253Seric /* make sure error messages don't have garbage on them */ 32215253Seric errno = 0; 32315253Seric 32416914Seric q = pvpbuf; 3253149Seric bslashmode = FALSE; 3267800Seric cmntcnt = 0; 3278423Seric anglecnt = 0; 3283149Seric avp = av; 32956678Seric state = ATM; 3308078Seric c = NOCHAR; 3318078Seric p = addr; 33256764Seric if (tTd(22, 11)) 333297Seric { 3348078Seric printf("prescan: "); 3358078Seric xputs(p); 33623109Seric (void) putchar('\n'); 3378078Seric } 3388078Seric 3398078Seric do 3408078Seric { 3413149Seric /* read a token */ 3423149Seric tok = q; 3438078Seric for (;;) 344297Seric { 3458078Seric /* store away any old lookahead character */ 34659277Seric if (c != NOCHAR && !bslashmode) 3478078Seric { 34815284Seric /* see if there is room */ 34916914Seric if (q >= &pvpbuf[PSBUFSIZE - 5]) 3508078Seric { 35158151Seric usrerr("553 Address too long"); 35258333Seric if (delimptr != NULL) 35358333Seric *delimptr = p; 3548078Seric return (NULL); 3558078Seric } 35615284Seric 35715284Seric /* squirrel it away */ 3588078Seric *q++ = c; 3598078Seric } 3608078Seric 3618078Seric /* read a new input character */ 3628078Seric c = *p++; 36357631Seric if (c == '\0') 36456764Seric { 36556764Seric /* diagnose and patch up bad syntax */ 36656764Seric if (state == QST) 36756764Seric { 36858151Seric usrerr("553 Unbalanced '\"'"); 36956764Seric c = '"'; 37056764Seric } 37156764Seric else if (cmntcnt > 0) 37256764Seric { 37358151Seric usrerr("553 Unbalanced '('"); 37456764Seric c = ')'; 37556764Seric } 37656764Seric else if (anglecnt > 0) 37756764Seric { 37856764Seric c = '>'; 37958151Seric usrerr("553 Unbalanced '<'"); 38056764Seric } 38156764Seric else 38256764Seric break; 38315284Seric 38456764Seric p--; 38556764Seric } 38657631Seric else if (c == delim && anglecnt <= 0 && 38757631Seric cmntcnt <= 0 && state != QST) 38857631Seric break; 38956764Seric 3908078Seric if (tTd(22, 101)) 3918078Seric printf("c=%c, s=%d; ", c, state); 3928078Seric 3933149Seric /* chew up special characters */ 3943149Seric *q = '\0'; 3953149Seric if (bslashmode) 3963149Seric { 39759105Seric bslashmode = FALSE; 39859105Seric 39924944Seric /* kludge \! for naive users */ 40058061Seric if (cmntcnt > 0) 40159105Seric { 40258061Seric c = NOCHAR; 40359105Seric continue; 40459105Seric } 40559105Seric else if (c != '!' || state == QST) 40659105Seric { 40756678Seric *q++ = '\\'; 40859105Seric continue; 40959105Seric } 4103149Seric } 41156678Seric 41256678Seric if (c == '\\') 4133149Seric { 4143149Seric bslashmode = TRUE; 4153149Seric } 41656678Seric else if (state == QST) 4178514Seric { 4188514Seric /* do nothing, just avoid next clauses */ 4198514Seric } 4208078Seric else if (c == '(') 4214100Seric { 4228078Seric cmntcnt++; 4238078Seric c = NOCHAR; 4244100Seric } 4258078Seric else if (c == ')') 4263149Seric { 4278078Seric if (cmntcnt <= 0) 4283149Seric { 42958151Seric usrerr("553 Unbalanced ')'"); 43058333Seric if (delimptr != NULL) 43158333Seric *delimptr = p; 4328078Seric return (NULL); 4333149Seric } 4348078Seric else 4358078Seric cmntcnt--; 4368078Seric } 4378078Seric else if (cmntcnt > 0) 4388078Seric c = NOCHAR; 4398423Seric else if (c == '<') 4408423Seric anglecnt++; 4418423Seric else if (c == '>') 4428423Seric { 4438423Seric if (anglecnt <= 0) 4448423Seric { 44558151Seric usrerr("553 Unbalanced '>'"); 44658333Seric if (delimptr != NULL) 44758333Seric *delimptr = p; 4488423Seric return (NULL); 4498423Seric } 4508423Seric anglecnt--; 4518423Seric } 45258050Seric else if (delim == ' ' && isascii(c) && isspace(c)) 45311423Seric c = ' '; 4543149Seric 4558078Seric if (c == NOCHAR) 4568078Seric continue; 4573149Seric 4588078Seric /* see if this is end of input */ 45911405Seric if (c == delim && anglecnt <= 0 && state != QST) 4603149Seric break; 4613149Seric 4628078Seric newstate = StateTab[state][toktype(c)]; 4638078Seric if (tTd(22, 101)) 4648078Seric printf("ns=%02o\n", newstate); 4658078Seric state = newstate & TYPE; 4668078Seric if (bitset(M, newstate)) 4678078Seric c = NOCHAR; 4688078Seric if (bitset(B, newstate)) 4694228Seric break; 470297Seric } 4713149Seric 4723149Seric /* new token */ 4738078Seric if (tok != q) 4741378Seric { 4758078Seric *q++ = '\0'; 4768078Seric if (tTd(22, 36)) 477297Seric { 4788078Seric printf("tok="); 4798078Seric xputs(tok); 48023109Seric (void) putchar('\n'); 481297Seric } 4828078Seric if (avp >= &av[MAXATOM]) 483297Seric { 48458151Seric syserr("553 prescan: too many tokens"); 48558333Seric if (delimptr != NULL) 48658333Seric *delimptr = p; 4878078Seric return (NULL); 488297Seric } 4898078Seric *avp++ = tok; 490297Seric } 4918423Seric } while (c != '\0' && (c != delim || anglecnt > 0)); 4923149Seric *avp = NULL; 49358333Seric p--; 49458333Seric if (delimptr != NULL) 49558333Seric *delimptr = p; 49656764Seric if (tTd(22, 12)) 49756764Seric { 49856764Seric printf("prescan==>"); 49956764Seric printav(av); 50056764Seric } 50158546Seric if (av[0] == NULL) 50258546Seric return (NULL); 50358403Seric return (av); 5043149Seric } 5053149Seric /* 5063149Seric ** TOKTYPE -- return token type 5073149Seric ** 5083149Seric ** Parameters: 5093149Seric ** c -- the character in question. 5103149Seric ** 5113149Seric ** Returns: 5123149Seric ** Its type. 5133149Seric ** 5143149Seric ** Side Effects: 5153149Seric ** none. 5163149Seric */ 517297Seric 5183149Seric toktype(c) 51958050Seric register int c; 5203149Seric { 5213380Seric static char buf[50]; 5223382Seric static bool firstime = TRUE; 5233380Seric 5243382Seric if (firstime) 5253380Seric { 5263382Seric firstime = FALSE; 52758050Seric expand("\201o", buf, &buf[sizeof buf - 1], CurEnv); 5287005Seric (void) strcat(buf, DELIMCHARS); 5293380Seric } 53058050Seric c &= 0377; 53156327Seric if (c == MATCHCLASS || c == MATCHREPL || c == MATCHNCLASS) 5328078Seric return (ONE); 53359027Seric if (c == MACRODEXPAND) 53459027Seric return (ONE); 5358078Seric if (c == '"') 5368078Seric return (QST); 53758050Seric if ((c & 0340) == 0200) 53858050Seric return (OPR); 5394100Seric if (!isascii(c)) 5408078Seric return (ATM); 5418078Seric if (isspace(c) || c == ')') 5428078Seric return (SPC); 54358050Seric if (strchr(buf, c) != NULL) 5448078Seric return (OPR); 5458078Seric return (ATM); 5463149Seric } 5473149Seric /* 5483149Seric ** REWRITE -- apply rewrite rules to token vector. 5493149Seric ** 5504476Seric ** This routine is an ordered production system. Each rewrite 5514476Seric ** rule has a LHS (called the pattern) and a RHS (called the 5524476Seric ** rewrite); 'rwr' points the the current rewrite rule. 5534476Seric ** 5544476Seric ** For each rewrite rule, 'avp' points the address vector we 5554476Seric ** are trying to match against, and 'pvp' points to the pattern. 5568058Seric ** If pvp points to a special match value (MATCHZANY, MATCHANY, 5579585Seric ** MATCHONE, MATCHCLASS, MATCHNCLASS) then the address in avp 5589585Seric ** matched is saved away in the match vector (pointed to by 'mvp'). 5594476Seric ** 5604476Seric ** When a match between avp & pvp does not match, we try to 5619585Seric ** back out. If we back up over MATCHONE, MATCHCLASS, or MATCHNCLASS 5624476Seric ** we must also back out the match in mvp. If we reach a 5638058Seric ** MATCHANY or MATCHZANY we just extend the match and start 5648058Seric ** over again. 5654476Seric ** 5664476Seric ** When we finally match, we rewrite the address vector 5674476Seric ** and try over again. 5684476Seric ** 5693149Seric ** Parameters: 5703149Seric ** pvp -- pointer to token vector. 57159027Seric ** ruleset -- the ruleset to use for rewriting. 57259027Seric ** e -- the current envelope. 5733149Seric ** 5743149Seric ** Returns: 57559084Seric ** A status code. If EX_TEMPFAIL, higher level code should 57659084Seric ** attempt recovery. 5773149Seric ** 5783149Seric ** Side Effects: 5793149Seric ** pvp is modified. 5803149Seric */ 5812091Seric 5823149Seric struct match 5833149Seric { 5844468Seric char **first; /* first token matched */ 5854468Seric char **last; /* last token matched */ 58658825Seric char **pattern; /* pointer to pattern */ 5873149Seric }; 5883149Seric 5894468Seric # define MAXMATCH 9 /* max params per rewrite */ 5903149Seric 5913149Seric 59259084Seric int 59359027Seric rewrite(pvp, ruleset, e) 5943149Seric char **pvp; 5954070Seric int ruleset; 59659027Seric register ENVELOPE *e; 5973149Seric { 5983149Seric register char *ap; /* address pointer */ 5993149Seric register char *rp; /* rewrite pointer */ 6003149Seric register char **avp; /* address vector pointer */ 6013149Seric register char **rvp; /* rewrite vector pointer */ 6028058Seric register struct match *mlp; /* cur ptr into mlist */ 6038058Seric register struct rewrite *rwr; /* pointer to current rewrite rule */ 60458866Seric int ruleno; /* current rule number */ 60559084Seric int rstat = EX_OK; /* return status */ 60656678Seric struct match mlist[MAXMATCH]; /* stores match on LHS */ 6073149Seric char *npvp[MAXATOM+1]; /* temporary space for rebuild */ 60859027Seric extern char *macvalue(); 6093149Seric 6109279Seric if (OpMode == MD_TEST || tTd(21, 2)) 6113149Seric { 6128959Seric printf("rewrite: ruleset %2d input:", ruleset); 61356678Seric printav(pvp); 6143149Seric } 61556678Seric if (ruleset < 0 || ruleset >= MAXRWSETS) 61656326Seric { 61758151Seric syserr("554 rewrite: illegal ruleset number %d", ruleset); 61859084Seric return EX_CONFIG; 61956326Seric } 62056678Seric if (pvp == NULL) 62159084Seric return EX_USAGE; 62256326Seric 6233149Seric /* 62456678Seric ** Run through the list of rewrite rules, applying 62556678Seric ** any that match. 6263149Seric */ 6273149Seric 62858866Seric ruleno = 1; 6294070Seric for (rwr = RewriteRules[ruleset]; rwr != NULL; ) 6303149Seric { 63156678Seric int loopcount = 0; 63256678Seric 6337675Seric if (tTd(21, 12)) 634297Seric { 6358069Seric printf("-----trying rule:"); 63656678Seric printav(rwr->r_lhs); 6373149Seric } 6383149Seric 6393149Seric /* try to match on this rule */ 6404468Seric mlp = mlist; 6418058Seric rvp = rwr->r_lhs; 6428058Seric avp = pvp; 64358866Seric if (++loopcount > 100) 6443149Seric { 64558866Seric syserr("554 Infinite loop in ruleset %d, rule %d", 64658866Seric ruleset, ruleno); 64758866Seric if (tTd(21, 1)) 64852637Seric { 64956678Seric printf("workspace: "); 65056678Seric printav(pvp); 65152637Seric } 65258866Seric break; 65358866Seric } 65458866Seric 65558866Seric while ((ap = *avp) != NULL || *rvp != NULL) 65658866Seric { 6573149Seric rp = *rvp; 6588058Seric if (tTd(21, 35)) 6598058Seric { 66058825Seric printf("ADVANCE rp="); 66157531Seric xputs(rp); 66257532Seric printf(", ap="); 6638058Seric xputs(ap); 6648069Seric printf("\n"); 6658058Seric } 66656678Seric if (rp == NULL) 66756326Seric { 6683149Seric /* end-of-pattern before end-of-address */ 6698058Seric goto backup; 67056678Seric } 67158173Seric if (ap == NULL && (*rp & 0377) != MATCHZANY && 67258827Seric (*rp & 0377) != MATCHZERO) 67356326Seric { 67458825Seric /* end-of-input with patterns left */ 67558825Seric goto backup; 676297Seric } 67756326Seric 67858050Seric switch (*rp & 0377) 6798058Seric { 68056678Seric register STAB *s; 68158814Seric char buf[MAXLINE]; 68256326Seric 68356678Seric case MATCHCLASS: 68458825Seric /* match any phrase in a class */ 68558825Seric mlp->pattern = rvp; 68658814Seric mlp->first = avp; 68758814Seric extendclass: 68858825Seric ap = *avp; 68958825Seric if (ap == NULL) 69058814Seric goto backup; 69158814Seric mlp->last = avp++; 69258814Seric cataddr(mlp->first, mlp->last, buf, sizeof buf, '\0'); 69358814Seric s = stab(buf, ST_CLASS, ST_FIND); 69456678Seric if (s == NULL || !bitnset(rp[1], s->s_class)) 69556326Seric { 69658825Seric if (tTd(21, 36)) 69758825Seric { 69858825Seric printf("EXTEND rp="); 69958825Seric xputs(rp); 70058825Seric printf(", ap="); 70158825Seric xputs(ap); 70258825Seric printf("\n"); 70358825Seric } 70458825Seric goto extendclass; 70556326Seric } 70658825Seric if (tTd(21, 36)) 70758825Seric printf("CLMATCH\n"); 70858814Seric mlp++; 70958814Seric break; 7104060Seric 71158825Seric case MATCHNCLASS: 71258825Seric /* match any token not in a class */ 71358825Seric s = stab(ap, ST_CLASS, ST_FIND); 71458825Seric if (s != NULL && bitnset(rp[1], s->s_class)) 71558825Seric goto backup; 71658825Seric 71758825Seric /* fall through */ 71858825Seric 71956678Seric case MATCHONE: 72056678Seric case MATCHANY: 72156678Seric /* match exactly one token */ 72258825Seric mlp->pattern = rvp; 72356678Seric mlp->first = avp; 72456678Seric mlp->last = avp++; 7258058Seric mlp++; 72656678Seric break; 7278058Seric 72856678Seric case MATCHZANY: 72956678Seric /* match zero or more tokens */ 73058825Seric mlp->pattern = rvp; 73156678Seric mlp->first = avp; 73256678Seric mlp->last = avp - 1; 73356678Seric mlp++; 73456678Seric break; 73556326Seric 73658827Seric case MATCHZERO: 73758173Seric /* match zero tokens */ 73858173Seric break; 73958173Seric 74059027Seric case MACRODEXPAND: 74159027Seric /* 74259027Seric ** Match against run-time macro. 74359027Seric ** This algorithm is broken for the 74459027Seric ** general case (no recursive macros, 74559027Seric ** improper tokenization) but should 74659027Seric ** work for the usual cases. 74759027Seric */ 74859027Seric 74959027Seric ap = macvalue(rp[1], e); 75059027Seric mlp->first = avp; 75159027Seric if (tTd(21, 2)) 75259027Seric printf("rewrite: LHS $&%c => \"%s\"\n", 75359027Seric rp[1], 75459027Seric ap == NULL ? "(NULL)" : ap); 75559027Seric 75659027Seric if (ap == NULL) 75759027Seric break; 75859027Seric while (*ap != NULL) 75959027Seric { 76059027Seric if (*avp == NULL || 76159027Seric strncasecmp(ap, *avp, strlen(*avp)) != 0) 76259027Seric { 76359027Seric /* no match */ 76459027Seric avp = mlp->first; 76559027Seric goto backup; 76659027Seric } 76759027Seric ap += strlen(*avp++); 76859027Seric } 76959027Seric 77059027Seric /* match */ 77159027Seric break; 77259027Seric 77356678Seric default: 77456678Seric /* must have exact match */ 77556678Seric if (strcasecmp(rp, ap)) 7768058Seric goto backup; 7774468Seric avp++; 77856678Seric break; 7793149Seric } 7803149Seric 78156678Seric /* successful match on this token */ 7823149Seric rvp++; 7833149Seric continue; 7843149Seric 78558825Seric backup: 78656678Seric /* match failed -- back up */ 78758825Seric while (--mlp >= mlist) 7883149Seric { 78958825Seric rvp = mlp->pattern; 79056678Seric rp = *rvp; 79158825Seric avp = mlp->last + 1; 79258825Seric ap = *avp; 79358825Seric 79458825Seric if (tTd(21, 36)) 79558825Seric { 79658825Seric printf("BACKUP rp="); 79758825Seric xputs(rp); 79858825Seric printf(", ap="); 79958825Seric xputs(ap); 80058825Seric printf("\n"); 80158825Seric } 80258825Seric 80358825Seric if (ap == NULL) 80458825Seric { 80558825Seric /* run off the end -- back up again */ 80658825Seric continue; 80758825Seric } 80858050Seric if ((*rp & 0377) == MATCHANY || 80958050Seric (*rp & 0377) == MATCHZANY) 8104468Seric { 81156678Seric /* extend binding and continue */ 81258825Seric mlp->last = avp++; 81356678Seric rvp++; 81458825Seric mlp++; 81556678Seric break; 8164468Seric } 81758825Seric if ((*rp & 0377) == MATCHCLASS) 81856678Seric { 81958814Seric /* extend binding and try again */ 82058825Seric mlp->last = avp++; 82158814Seric goto extendclass; 82258814Seric } 8233149Seric } 8243149Seric 82558825Seric if (mlp < mlist) 82656678Seric { 82756678Seric /* total failure to match */ 82856326Seric break; 8293149Seric } 830297Seric } 8313149Seric 8323149Seric /* 83356678Seric ** See if we successfully matched 8343149Seric */ 8353149Seric 83658827Seric if (mlp < mlist || *rvp != NULL) 8373149Seric { 8389374Seric if (tTd(21, 10)) 8399374Seric printf("----- rule fails\n"); 8409374Seric rwr = rwr->r_next; 84158866Seric ruleno++; 8429374Seric continue; 8439374Seric } 8443149Seric 8459374Seric rvp = rwr->r_rhs; 8469374Seric if (tTd(21, 12)) 8479374Seric { 8489374Seric printf("-----rule matches:"); 84956678Seric printav(rvp); 8509374Seric } 8519374Seric 8529374Seric rp = *rvp; 85358050Seric if ((*rp & 0377) == CANONUSER) 8549374Seric { 8559374Seric rvp++; 8569374Seric rwr = rwr->r_next; 85758866Seric ruleno++; 8589374Seric } 85958050Seric else if ((*rp & 0377) == CANONHOST) 8609374Seric { 8619374Seric rvp++; 8629374Seric rwr = NULL; 8639374Seric } 86458050Seric else if ((*rp & 0377) == CANONNET) 8659374Seric rwr = NULL; 8669374Seric 8679374Seric /* substitute */ 8689374Seric for (avp = npvp; *rvp != NULL; rvp++) 8699374Seric { 8709374Seric register struct match *m; 8719374Seric register char **pp; 8729374Seric 8738058Seric rp = *rvp; 87458050Seric if ((*rp & 0377) == MATCHREPL) 8758058Seric { 87616914Seric /* substitute from LHS */ 87716914Seric m = &mlist[rp[1] - '1']; 87856678Seric if (m < mlist || m >= mlp) 8799374Seric { 88058151Seric syserr("554 rewrite: ruleset %d: replacement $%c out of bounds", 88156326Seric ruleset, rp[1]); 88259084Seric return EX_CONFIG; 8839374Seric } 88416914Seric if (tTd(21, 15)) 88516914Seric { 88616914Seric printf("$%c:", rp[1]); 88716914Seric pp = m->first; 88856678Seric while (pp <= m->last) 88916914Seric { 89016914Seric printf(" %x=\"", *pp); 89116914Seric (void) fflush(stdout); 89216914Seric printf("%s\"", *pp++); 89316914Seric } 89416914Seric printf("\n"); 89516914Seric } 8969374Seric pp = m->first; 89756678Seric while (pp <= m->last) 8983149Seric { 89916914Seric if (avp >= &npvp[MAXATOM]) 90056678Seric { 90158151Seric syserr("554 rewrite: expansion too long"); 90259084Seric return EX_DATAERR; 90356678Seric } 90416914Seric *avp++ = *pp++; 9053149Seric } 9063149Seric } 90716914Seric else 9088226Seric { 90916914Seric /* vanilla replacement */ 9109374Seric if (avp >= &npvp[MAXATOM]) 91116889Seric { 91256678Seric toolong: 91358151Seric syserr("554 rewrite: expansion too long"); 91459084Seric return EX_DATAERR; 91516889Seric } 91659027Seric if ((*rp & 0377) != MACRODEXPAND) 91759027Seric *avp++ = rp; 91859027Seric else 91959027Seric { 92059027Seric *avp = macvalue(rp[1], e); 92159027Seric if (tTd(21, 2)) 92259027Seric printf("rewrite: RHS $&%c => \"%s\"\n", 92359027Seric rp[1], 92459027Seric *avp == NULL ? "(NULL)" : *avp); 92559027Seric if (*avp != NULL) 92659027Seric avp++; 92759027Seric } 9288226Seric } 9299374Seric } 9309374Seric *avp++ = NULL; 93116914Seric 93216914Seric /* 93356678Seric ** Check for any hostname/keyword lookups. 93416914Seric */ 93516914Seric 93616914Seric for (rvp = npvp; *rvp != NULL; rvp++) 93716914Seric { 93856678Seric char **hbrvp; 93916914Seric char **xpvp; 94016914Seric int trsize; 94117473Seric char *olddelimchar; 94256678Seric char *replac; 94356678Seric int endtoken; 94456678Seric STAB *map; 94556678Seric char *mapname; 94656678Seric char **key_rvp; 94756678Seric char **arg_rvp; 94856678Seric char **default_rvp; 94956678Seric char buf[MAXNAME + 1]; 95016914Seric char *pvpb1[MAXATOM + 1]; 95156823Seric char *argvect[10]; 95217174Seric char pvpbuf[PSBUFSIZE]; 95316914Seric 95458050Seric if ((**rvp & 0377) != HOSTBEGIN && 95558050Seric (**rvp & 0377) != LOOKUPBEGIN) 95616914Seric continue; 95716914Seric 95816914Seric /* 95956678Seric ** Got a hostname/keyword lookup. 96016914Seric ** 96116914Seric ** This could be optimized fairly easily. 96216914Seric */ 96316914Seric 96416914Seric hbrvp = rvp; 96558050Seric if ((**rvp & 0377) == HOSTBEGIN) 96656327Seric { 96756678Seric endtoken = HOSTEND; 96856678Seric mapname = "host"; 96956327Seric } 97056326Seric else 97156327Seric { 97256678Seric endtoken = LOOKUPEND; 97356678Seric mapname = *++rvp; 97456327Seric } 97556678Seric map = stab(mapname, ST_MAP, ST_FIND); 97656678Seric if (map == NULL) 97758151Seric syserr("554 rewrite: map %s not found", mapname); 97856678Seric 97956678Seric /* extract the match part */ 98056678Seric key_rvp = ++rvp; 98156823Seric default_rvp = NULL; 98256823Seric arg_rvp = argvect; 98356823Seric xpvp = NULL; 98456823Seric replac = pvpbuf; 98558050Seric while (*rvp != NULL && (**rvp & 0377) != endtoken) 98653654Seric { 98758050Seric int nodetype = **rvp & 0377; 98856823Seric 98956823Seric if (nodetype != CANONHOST && nodetype != CANONUSER) 99056678Seric { 99156823Seric rvp++; 99256823Seric continue; 99356823Seric } 99456823Seric 99556823Seric *rvp++ = NULL; 99656823Seric 99756823Seric if (xpvp != NULL) 99856823Seric { 99958814Seric cataddr(xpvp, NULL, replac, 100058082Seric &pvpbuf[sizeof pvpbuf] - replac, 100158082Seric '\0'); 100256823Seric *++arg_rvp = replac; 100356823Seric replac += strlen(replac) + 1; 100456823Seric xpvp = NULL; 100556823Seric } 100656823Seric switch (nodetype) 100756823Seric { 100856678Seric case CANONHOST: 100956823Seric xpvp = rvp; 101056678Seric break; 101156678Seric 101256678Seric case CANONUSER: 101356678Seric default_rvp = rvp; 101456678Seric break; 101556678Seric } 101653654Seric } 101716914Seric if (*rvp != NULL) 101816914Seric *rvp++ = NULL; 101956823Seric if (xpvp != NULL) 102056823Seric { 102158814Seric cataddr(xpvp, NULL, replac, 102258082Seric &pvpbuf[sizeof pvpbuf] - replac, 102358082Seric '\0'); 102456823Seric *++arg_rvp = replac; 102556823Seric } 102656823Seric *++arg_rvp = NULL; 102716914Seric 102816914Seric /* save the remainder of the input string */ 102916914Seric trsize = (int) (avp - rvp + 1) * sizeof *rvp; 103016914Seric bcopy((char *) rvp, (char *) pvpb1, trsize); 103116914Seric 103256678Seric /* look it up */ 103358814Seric cataddr(key_rvp, NULL, buf, sizeof buf, '\0'); 103456823Seric argvect[0] = buf; 103556678Seric if (map != NULL && bitset(MF_VALID, map->s_map.map_flags)) 103656836Seric { 103756836Seric int bsize = sizeof buf - 1; 103859084Seric auto int stat = EX_OK; 103956836Seric 104056836Seric if (map->s_map.map_app != NULL) 104156836Seric bsize -= strlen(map->s_map.map_app); 104258796Seric if (tTd(60, 1)) 104358796Seric printf("map_lookup(%s, %s) => ", 104458796Seric mapname, buf); 104556836Seric replac = (*map->s_map.map_class->map_lookup)(&map->s_map, 104659084Seric buf, sizeof buf - 1, argvect, 104759084Seric &stat); 104856836Seric if (replac != NULL && map->s_map.map_app != NULL) 104956836Seric strcat(replac, map->s_map.map_app); 105058796Seric if (tTd(60, 1)) 105159084Seric printf("%s (%d)\n", 105259084Seric replac ? replac : "NOT FOUND", 105359084Seric stat); 105459084Seric 105559084Seric /* should recover if stat == EX_TEMPFAIL */ 105659084Seric if (stat == EX_TEMPFAIL) 105759084Seric rstat = stat; 105856836Seric } 105953654Seric else 106056678Seric replac = NULL; 106156678Seric 106256678Seric /* if no replacement, use default */ 106356823Seric if (replac == NULL && default_rvp != NULL) 106456823Seric { 106556823Seric char buf2[sizeof buf]; 106656823Seric 106756823Seric /* rewrite the default with % translations */ 106858814Seric cataddr(default_rvp, NULL, buf2, sizeof buf2, '\0'); 106956823Seric map_rewrite(buf2, sizeof buf2, buf, sizeof buf, 107056823Seric argvect); 107156823Seric replac = buf; 107256823Seric } 107356823Seric 107456678Seric if (replac == NULL) 107551317Seric { 107656823Seric xpvp = key_rvp; 107753654Seric } 107856678Seric else 107953654Seric { 108056678Seric /* scan the new replacement */ 108158333Seric xpvp = prescan(replac, '\0', pvpbuf, NULL); 108253654Seric if (xpvp == NULL) 108351317Seric { 108458403Seric /* prescan already printed error */ 108559084Seric return EX_DATAERR; 108656678Seric } 108751317Seric } 108851317Seric 108916914Seric /* append it to the token list */ 109056678Seric for (avp = hbrvp; *xpvp != NULL; xpvp++) 109156678Seric { 109217174Seric *avp++ = newstr(*xpvp); 109316920Seric if (avp >= &npvp[MAXATOM]) 109416914Seric goto toolong; 109517174Seric } 109616914Seric 109716914Seric /* restore the old trailing information */ 109856678Seric for (xpvp = pvpb1; (*avp++ = *xpvp++) != NULL; ) 109916920Seric if (avp >= &npvp[MAXATOM]) 110016914Seric goto toolong; 110117174Seric 110256678Seric break; 110316914Seric } 110416914Seric 110516914Seric /* 110616914Seric ** Check for subroutine calls. 110716914Seric */ 110816914Seric 110958050Seric if (*npvp != NULL && (**npvp & 0377) == CALLSUBR) 111056678Seric { 111159084Seric int stat; 111259084Seric 111356678Seric bcopy((char *) &npvp[2], (char *) pvp, 111456678Seric (int) (avp - npvp - 2) * sizeof *avp); 111556678Seric if (tTd(21, 3)) 111656678Seric printf("-----callsubr %s\n", npvp[1]); 111759084Seric stat = rewrite(pvp, atoi(npvp[1]), e); 111859084Seric if (rstat == EX_OK || stat == EX_TEMPFAIL) 111959084Seric rstat = stat; 112056678Seric } 112156678Seric else 112256678Seric { 112317348Seric bcopy((char *) npvp, (char *) pvp, 112416900Seric (int) (avp - npvp) * sizeof *avp); 112556678Seric } 11269374Seric if (tTd(21, 4)) 11279374Seric { 11289374Seric printf("rewritten as:"); 112956678Seric printav(pvp); 11309374Seric } 1131297Seric } 11328069Seric 11339279Seric if (OpMode == MD_TEST || tTd(21, 2)) 11348069Seric { 11358959Seric printf("rewrite: ruleset %2d returns:", ruleset); 113656678Seric printav(pvp); 11378069Seric } 113859084Seric 113959084Seric return rstat; 11403149Seric } 11413149Seric /* 11423149Seric ** BUILDADDR -- build address from token vector. 11433149Seric ** 11443149Seric ** Parameters: 11453149Seric ** tv -- token vector. 11463149Seric ** a -- pointer to address descriptor to fill. 11473149Seric ** If NULL, one will be allocated. 114858966Seric ** e -- the current envelope. 11493149Seric ** 11503149Seric ** Returns: 11514279Seric ** NULL if there was an error. 11524279Seric ** 'a' otherwise. 11533149Seric ** 11543149Seric ** Side Effects: 11553149Seric ** fills in 'a' 11563149Seric */ 11573149Seric 115857249Seric struct errcodes 115957249Seric { 116057249Seric char *ec_name; /* name of error code */ 116157249Seric int ec_code; /* numeric code */ 116257249Seric } ErrorCodes[] = 116357249Seric { 116457249Seric "usage", EX_USAGE, 116557249Seric "nouser", EX_NOUSER, 116657249Seric "nohost", EX_NOHOST, 116757249Seric "unavailable", EX_UNAVAILABLE, 116857249Seric "software", EX_SOFTWARE, 116957249Seric "tempfail", EX_TEMPFAIL, 117057249Seric "protocol", EX_PROTOCOL, 117157249Seric #ifdef EX_CONFIG 117257249Seric "config", EX_CONFIG, 117357249Seric #endif 117457249Seric NULL, EX_UNAVAILABLE, 117557249Seric }; 117657249Seric 117756678Seric ADDRESS * 117858966Seric buildaddr(tv, a, e) 11793149Seric register char **tv; 11803149Seric register ADDRESS *a; 118158966Seric register ENVELOPE *e; 11823149Seric { 11833149Seric struct mailer **mp; 11843149Seric register struct mailer *m; 118558008Seric char *bp; 118658008Seric int spaceleft; 118757402Seric static char buf[MAXNAME]; 11883149Seric 11893149Seric if (a == NULL) 11903149Seric a = (ADDRESS *) xalloc(sizeof *a); 119116889Seric bzero((char *) a, sizeof *a); 11923149Seric 11933149Seric /* figure out what net/mailer to use */ 119458050Seric if ((**tv & 0377) != CANONNET) 11954279Seric { 119658151Seric syserr("554 buildaddr: no net"); 11974279Seric return (NULL); 11984279Seric } 11993149Seric tv++; 120058680Seric if (strcasecmp(*tv, "error") == 0) 12014279Seric { 120258050Seric if ((**++tv & 0377) == CANONHOST) 120310183Seric { 120457249Seric register struct errcodes *ep; 120557249Seric 120658050Seric if (isascii(**++tv) && isdigit(**tv)) 120757249Seric { 120857249Seric setstat(atoi(*tv)); 120957249Seric } 121057249Seric else 121157249Seric { 121257249Seric for (ep = ErrorCodes; ep->ec_name != NULL; ep++) 121357249Seric if (strcasecmp(ep->ec_name, *tv) == 0) 121457249Seric break; 121557249Seric setstat(ep->ec_code); 121657249Seric } 121710183Seric tv++; 121810183Seric } 121958050Seric if ((**tv & 0377) != CANONUSER) 122058151Seric syserr("554 buildaddr: error: no user"); 122158814Seric cataddr(++tv, NULL, buf, sizeof buf, ' '); 122258082Seric stripquotes(buf); 12234279Seric usrerr(buf); 12244279Seric return (NULL); 12254279Seric } 122657402Seric 12274598Seric for (mp = Mailer; (m = *mp++) != NULL; ) 12283149Seric { 122958680Seric if (strcasecmp(m->m_name, *tv) == 0) 12303149Seric break; 12313149Seric } 12323149Seric if (m == NULL) 12334279Seric { 123458151Seric syserr("554 buildaddr: unknown mailer %s", *tv); 12354279Seric return (NULL); 12364279Seric } 12374598Seric a->q_mailer = m; 12383149Seric 12393149Seric /* figure out what host (if any) */ 124056678Seric tv++; 124158509Seric if ((**tv & 0377) == CANONHOST) 12423149Seric { 124358008Seric bp = buf; 124458008Seric spaceleft = sizeof buf - 1; 124558050Seric while (*++tv != NULL && (**tv & 0377) != CANONUSER) 124658008Seric { 124758008Seric int i = strlen(*tv); 124858008Seric 124958008Seric if (i > spaceleft) 125058008Seric { 125158008Seric /* out of space for this address */ 125258008Seric if (spaceleft >= 0) 125358151Seric syserr("554 buildaddr: host too long (%.40s...)", 125458008Seric buf); 125558008Seric i = spaceleft; 125658008Seric spaceleft = 0; 125758008Seric } 125858008Seric if (i <= 0) 125958008Seric continue; 126058008Seric bcopy(*tv, bp, i); 126158008Seric bp += i; 126258008Seric spaceleft -= i; 126358008Seric } 126458010Seric *bp = '\0'; 12655704Seric a->q_host = newstr(buf); 12663149Seric } 126757249Seric else 126858509Seric { 126958509Seric if (!bitnset(M_LOCALMAILER, m->m_flags)) 127058509Seric { 127158509Seric syserr("554 buildaddr: no host"); 127258509Seric return (NULL); 127358509Seric } 127457249Seric a->q_host = NULL; 127558509Seric } 12763149Seric 12773149Seric /* figure out the user */ 127858050Seric if (*tv == NULL || (**tv & 0377) != CANONUSER) 12794279Seric { 128058151Seric syserr("554 buildaddr: no user"); 12814279Seric return (NULL); 12824279Seric } 128357402Seric tv++; 128451317Seric 128557402Seric /* do special mapping for local mailer */ 128657402Seric if (m == LocalMailer && *tv != NULL) 128757402Seric { 128857454Seric register char *p = *tv; 128957454Seric 129057454Seric if (*p == '"') 129157454Seric p++; 129257454Seric if (*p == '|') 129357402Seric a->q_mailer = m = ProgMailer; 129457454Seric else if (*p == '/') 129557402Seric a->q_mailer = m = FileMailer; 129657454Seric else if (*p == ':') 129757402Seric { 129857402Seric /* may be :include: */ 129958814Seric cataddr(tv, NULL, buf, sizeof buf, '\0'); 130058008Seric stripquotes(buf); 130158008Seric if (strncasecmp(buf, ":include:", 9) == 0) 130258008Seric { 130358008Seric /* if :include:, don't need further rewriting */ 130457402Seric a->q_mailer = m = InclMailer; 130558008Seric a->q_user = &buf[9]; 130658008Seric return (a); 130758008Seric } 130857402Seric } 130957402Seric } 131057402Seric 131158008Seric if (m == LocalMailer && *tv != NULL && strcmp(*tv, "@") == 0) 131258008Seric { 131358008Seric tv++; 131458008Seric a->q_flags |= QNOTREMOTE; 131558008Seric } 131658008Seric 131758673Seric /* do cleanup of final address */ 131859084Seric (void) rewrite(tv, 4, e); 131919040Seric 132019040Seric /* save the result for the command line/RCPT argument */ 132158814Seric cataddr(tv, NULL, buf, sizeof buf, '\0'); 13223149Seric a->q_user = buf; 13233149Seric 132458670Seric /* 132558670Seric ** Do mapping to lower case as requested by mailer 132658670Seric */ 132758670Seric 132858670Seric if (a->q_host != NULL && !bitnset(M_HST_UPPER, m->m_flags)) 132958670Seric makelower(a->q_host); 133058670Seric if (!bitnset(M_USR_UPPER, m->m_flags)) 133158670Seric makelower(a->q_user); 133258670Seric 13333149Seric return (a); 13343149Seric } 13353188Seric /* 13364228Seric ** CATADDR -- concatenate pieces of addresses (putting in <LWSP> subs) 13374228Seric ** 13384228Seric ** Parameters: 13394228Seric ** pvp -- parameter vector to rebuild. 134058814Seric ** evp -- last parameter to include. Can be NULL to 134158814Seric ** use entire pvp. 13424228Seric ** buf -- buffer to build the string into. 13434228Seric ** sz -- size of buf. 134458082Seric ** spacesub -- the space separator character; if null, 134558082Seric ** use SpaceSub. 13464228Seric ** 13474228Seric ** Returns: 13484228Seric ** none. 13494228Seric ** 13504228Seric ** Side Effects: 13514228Seric ** Destroys buf. 13524228Seric */ 13534228Seric 135458814Seric cataddr(pvp, evp, buf, sz, spacesub) 13554228Seric char **pvp; 135658814Seric char **evp; 13574228Seric char *buf; 13584228Seric register int sz; 135958082Seric char spacesub; 13604228Seric { 13614228Seric bool oatomtok = FALSE; 136256678Seric bool natomtok = FALSE; 13634228Seric register int i; 13644228Seric register char *p; 13654228Seric 136658082Seric if (spacesub == '\0') 136758082Seric spacesub = SpaceSub; 136858082Seric 13698423Seric if (pvp == NULL) 13708423Seric { 137123109Seric (void) strcpy(buf, ""); 13728423Seric return; 13738423Seric } 13744228Seric p = buf; 137511156Seric sz -= 2; 13764228Seric while (*pvp != NULL && (i = strlen(*pvp)) < sz) 13774228Seric { 13788078Seric natomtok = (toktype(**pvp) == ATM); 13794228Seric if (oatomtok && natomtok) 138058082Seric *p++ = spacesub; 13814228Seric (void) strcpy(p, *pvp); 13824228Seric oatomtok = natomtok; 13834228Seric p += i; 138411156Seric sz -= i + 1; 138558814Seric if (pvp++ == evp) 138658814Seric break; 13874228Seric } 13884228Seric *p = '\0'; 13894228Seric } 13904228Seric /* 13913188Seric ** SAMEADDR -- Determine if two addresses are the same 13923188Seric ** 13933188Seric ** This is not just a straight comparison -- if the mailer doesn't 13943188Seric ** care about the host we just ignore it, etc. 13953188Seric ** 13963188Seric ** Parameters: 13973188Seric ** a, b -- pointers to the internal forms to compare. 13983188Seric ** 13993188Seric ** Returns: 14003188Seric ** TRUE -- they represent the same mailbox. 14013188Seric ** FALSE -- they don't. 14023188Seric ** 14033188Seric ** Side Effects: 14043188Seric ** none. 14053188Seric */ 14063188Seric 14073188Seric bool 14089374Seric sameaddr(a, b) 14093188Seric register ADDRESS *a; 14103188Seric register ADDRESS *b; 14113188Seric { 14123188Seric /* if they don't have the same mailer, forget it */ 14133188Seric if (a->q_mailer != b->q_mailer) 14143188Seric return (FALSE); 14153188Seric 14163188Seric /* if the user isn't the same, we can drop out */ 141756678Seric if (strcmp(a->q_user, b->q_user) != 0) 14183188Seric return (FALSE); 14193188Seric 142058438Seric /* if we have good uids for both but the differ, these are different */ 142158438Seric if (bitset(QGOODUID, a->q_flags & b->q_flags) && a->q_uid != b->q_uid) 142258438Seric return (FALSE); 142358438Seric 142458509Seric /* otherwise compare hosts (but be careful for NULL ptrs) */ 142558509Seric if (a->q_host == b->q_host) 142658509Seric { 142758509Seric /* probably both null pointers */ 14283188Seric return (TRUE); 142958509Seric } 14303188Seric if (a->q_host == NULL || b->q_host == NULL) 143158509Seric { 143258509Seric /* only one is a null pointer */ 14333188Seric return (FALSE); 143458509Seric } 143556678Seric if (strcmp(a->q_host, b->q_host) != 0) 14363188Seric return (FALSE); 14373188Seric 14383188Seric return (TRUE); 14393188Seric } 14403234Seric /* 14413234Seric ** PRINTADDR -- print address (for debugging) 14423234Seric ** 14433234Seric ** Parameters: 14443234Seric ** a -- the address to print 14453234Seric ** follow -- follow the q_next chain. 14463234Seric ** 14473234Seric ** Returns: 14483234Seric ** none. 14493234Seric ** 14503234Seric ** Side Effects: 14513234Seric ** none. 14523234Seric */ 14533234Seric 14543234Seric printaddr(a, follow) 14553234Seric register ADDRESS *a; 14563234Seric bool follow; 14573234Seric { 14585001Seric bool first = TRUE; 145957731Seric register MAILER *m; 146057731Seric MAILER pseudomailer; 14615001Seric 14623234Seric while (a != NULL) 14633234Seric { 14645001Seric first = FALSE; 14654443Seric printf("%x=", a); 14664085Seric (void) fflush(stdout); 146757731Seric 146857731Seric /* find the mailer -- carefully */ 146957731Seric m = a->q_mailer; 147057731Seric if (m == NULL) 147157731Seric { 147257731Seric m = &pseudomailer; 147357731Seric m->m_mno = -1; 147457731Seric m->m_name = "NULL"; 147557731Seric } 147657731Seric 147758680Seric printf("%s:\n\tmailer %d (%s), host `%s', user `%s', ruser `%s'\n", 147857731Seric a->q_paddr, m->m_mno, m->m_name, 147940973Sbostic a->q_host, a->q_user, a->q_ruser? a->q_ruser: "<null>"); 148059269Seric printf("\tnext=%x, flags=%o, alias %x, uid %d, gid %d\n", 148159269Seric a->q_next, a->q_flags, a->q_alias, a->q_uid, a->q_gid); 148259269Seric printf("\towner=%s, home=\"%s\", fullname=\"%s\"\n", 148359269Seric a->q_owner == NULL ? "(none)" : a->q_owner, 148459269Seric a->q_home, a->q_fullname); 14854996Seric 14863234Seric if (!follow) 14873234Seric return; 14884996Seric a = a->q_next; 14893234Seric } 14905001Seric if (first) 14914443Seric printf("[NULL]\n"); 14923234Seric } 14934317Seric 14947682Seric /* 14957682Seric ** REMOTENAME -- return the name relative to the current mailer 14967682Seric ** 14977682Seric ** Parameters: 14987682Seric ** name -- the name to translate. 14998069Seric ** m -- the mailer that we want to do rewriting relative 15008069Seric ** to. 150159163Seric ** flags -- fine tune operations. 150259163Seric ** pstat -- pointer to status word. 150358020Seric ** e -- the current envelope. 15047682Seric ** 15057682Seric ** Returns: 15067682Seric ** the text string representing this address relative to 15077682Seric ** the receiving mailer. 15087682Seric ** 15097682Seric ** Side Effects: 15107682Seric ** none. 15117682Seric ** 15127682Seric ** Warnings: 15137682Seric ** The text string returned is tucked away locally; 15147682Seric ** copy it if you intend to save it. 15157682Seric */ 15167682Seric 15177682Seric char * 151859163Seric remotename(name, m, flags, pstat, e) 15197682Seric char *name; 152056678Seric struct mailer *m; 152159163Seric int flags; 152259163Seric int *pstat; 152356678Seric register ENVELOPE *e; 15247682Seric { 15258069Seric register char **pvp; 15268069Seric char *fancy; 152756678Seric extern char *macvalue(); 152856678Seric char *oldg = macvalue('g', e); 152958020Seric int rwset; 15307682Seric static char buf[MAXNAME]; 15317682Seric char lbuf[MAXNAME]; 153216914Seric char pvpbuf[PSBUFSIZE]; 153356678Seric extern char **prescan(); 153456678Seric extern char *crackaddr(); 15357682Seric 15367755Seric if (tTd(12, 1)) 15377755Seric printf("remotename(%s)\n", name); 15387755Seric 153910177Seric /* don't do anything if we are tagging it as special */ 154059163Seric if (bitset(RF_SENDERADDR, flags)) 154159163Seric rwset = bitset(RF_HEADERADDR, flags) ? m->m_sh_rwset 154259163Seric : m->m_se_rwset; 154358020Seric else 154459163Seric rwset = bitset(RF_HEADERADDR, flags) ? m->m_rh_rwset 154559163Seric : m->m_re_rwset; 154658020Seric if (rwset < 0) 154710177Seric return (name); 154810177Seric 15497682Seric /* 15508181Seric ** Do a heuristic crack of this name to extract any comment info. 15518181Seric ** This will leave the name as a comment and a $g macro. 15527889Seric */ 15537889Seric 155459163Seric if (bitset(RF_CANONICAL, flags) || bitnset(M_NOCOMMENT, m->m_flags)) 155558050Seric fancy = "\201g"; 155610310Seric else 155710310Seric fancy = crackaddr(name); 15587889Seric 15598181Seric /* 15608181Seric ** Turn the name into canonical form. 15618181Seric ** Normally this will be RFC 822 style, i.e., "user@domain". 15628181Seric ** If this only resolves to "user", and the "C" flag is 15638181Seric ** specified in the sending mailer, then the sender's 15648181Seric ** domain will be appended. 15658181Seric */ 15668181Seric 156758333Seric pvp = prescan(name, '\0', pvpbuf, NULL); 15687889Seric if (pvp == NULL) 15697889Seric return (name); 157059163Seric if (rewrite(pvp, 3, e) == EX_TEMPFAIL) 157159163Seric *pstat = EX_TEMPFAIL; 157259163Seric if (bitset(RF_ADDDOMAIN, flags) && e->e_fromdomain != NULL) 15738181Seric { 15748181Seric /* append from domain to this address */ 15758181Seric register char **pxp = pvp; 15768181Seric 15779594Seric /* see if there is an "@domain" in the current name */ 15788181Seric while (*pxp != NULL && strcmp(*pxp, "@") != 0) 15798181Seric pxp++; 15808181Seric if (*pxp == NULL) 15818181Seric { 15829594Seric /* no.... append the "@domain" from the sender */ 158356678Seric register char **qxq = e->e_fromdomain; 15848181Seric 15859594Seric while ((*pxp++ = *qxq++) != NULL) 15869594Seric continue; 158759163Seric if (rewrite(pvp, 3, e) == EX_TEMPFAIL) 158859163Seric *pstat = EX_TEMPFAIL; 15898181Seric } 15908181Seric } 15918181Seric 15928181Seric /* 15938959Seric ** Do more specific rewriting. 159456678Seric ** Rewrite using ruleset 1 or 2 depending on whether this is 159556678Seric ** a sender address or not. 15968181Seric ** Then run it through any receiving-mailer-specific rulesets. 15978181Seric */ 15988181Seric 159959163Seric if (bitset(RF_SENDERADDR, flags)) 160059541Seric { 160159163Seric if (rewrite(pvp, 1, e) == EX_TEMPFAIL) 160259163Seric *pstat = EX_TEMPFAIL; 160359541Seric } 16048069Seric else 160559541Seric { 160659163Seric if (rewrite(pvp, 2, e) == EX_TEMPFAIL) 160759163Seric *pstat = EX_TEMPFAIL; 160859541Seric } 160958020Seric if (rwset > 0) 161059541Seric { 161159163Seric if (rewrite(pvp, rwset, e) == EX_TEMPFAIL) 161259163Seric *pstat = EX_TEMPFAIL; 161359541Seric } 16147682Seric 16158181Seric /* 16168959Seric ** Do any final sanitation the address may require. 16178959Seric ** This will normally be used to turn internal forms 16188959Seric ** (e.g., user@host.LOCAL) into external form. This 16198959Seric ** may be used as a default to the above rules. 16208959Seric */ 16218959Seric 162259163Seric if (rewrite(pvp, 4, e) == EX_TEMPFAIL) 162359163Seric *pstat = EX_TEMPFAIL; 16248959Seric 16258959Seric /* 16268181Seric ** Now restore the comment information we had at the beginning. 16278181Seric */ 16288181Seric 162958825Seric cataddr(pvp, NULL, lbuf, sizeof lbuf, '\0'); 163056678Seric define('g', lbuf, e); 163156678Seric expand(fancy, buf, &buf[sizeof buf - 1], e); 163256678Seric define('g', oldg, e); 16337682Seric 16347682Seric if (tTd(12, 1)) 16357755Seric printf("remotename => `%s'\n", buf); 16367682Seric return (buf); 16377682Seric } 163851317Seric /* 163956678Seric ** MAPLOCALUSER -- run local username through ruleset 5 for final redirection 164051317Seric ** 164151317Seric ** Parameters: 164256678Seric ** a -- the address to map (but just the user name part). 164356678Seric ** sendq -- the sendq in which to install any replacement 164456678Seric ** addresses. 164551317Seric ** 164651317Seric ** Returns: 164751317Seric ** none. 164851317Seric */ 164951317Seric 165056678Seric maplocaluser(a, sendq, e) 165156678Seric register ADDRESS *a; 165256678Seric ADDRESS **sendq; 165356678Seric ENVELOPE *e; 165451317Seric { 165556678Seric register char **pvp; 165656678Seric register ADDRESS *a1 = NULL; 165758333Seric auto char *delimptr; 165856678Seric char pvpbuf[PSBUFSIZE]; 165951317Seric 166056678Seric if (tTd(29, 1)) 166156678Seric { 166256678Seric printf("maplocaluser: "); 166356678Seric printaddr(a, FALSE); 166456678Seric } 166558333Seric pvp = prescan(a->q_user, '\0', pvpbuf, &delimptr); 166656678Seric if (pvp == NULL) 166756678Seric return; 166851317Seric 166959084Seric (void) rewrite(pvp, 5, e); 167058050Seric if (pvp[0] == NULL || (pvp[0][0] & 0377) != CANONNET) 167156678Seric return; 167251317Seric 167356678Seric /* if non-null, mailer destination specified -- has it changed? */ 167458966Seric a1 = buildaddr(pvp, NULL, e); 167556678Seric if (a1 == NULL || sameaddr(a, a1)) 167656678Seric return; 167751317Seric 167856678Seric /* mark old address as dead; insert new address */ 167956678Seric a->q_flags |= QDONTSEND; 168057731Seric if (tTd(29, 5)) 168157731Seric { 168257731Seric printf("maplocaluser: QDONTSEND "); 168357731Seric printaddr(a, FALSE); 168457731Seric } 168556678Seric a1->q_alias = a; 168658333Seric allocaddr(a1, 1, NULL, delimptr); 168756678Seric (void) recipient(a1, sendq, e); 168851317Seric } 168958802Seric /* 169058802Seric ** DEQUOTE_INIT -- initialize dequote map 169158802Seric ** 169258802Seric ** This is a no-op. 169358802Seric ** 169458802Seric ** Parameters: 169558802Seric ** map -- the internal map structure. 169658802Seric ** mapname -- the name of the mapl. 169758802Seric ** args -- arguments. 169858802Seric ** 169958802Seric ** Returns: 170058802Seric ** TRUE. 170158802Seric */ 170258802Seric 170358802Seric bool 170458802Seric dequote_init(map, mapname, args) 170558802Seric MAP *map; 170658802Seric char *mapname; 170758802Seric char *args; 170858802Seric { 170958805Seric register char *p = args; 171058805Seric 171158805Seric for (;;) 171258805Seric { 171358805Seric while (isascii(*p) && isspace(*p)) 171458805Seric p++; 171558805Seric if (*p != '-') 171658805Seric break; 171758805Seric switch (*++p) 171858805Seric { 171958805Seric case 'a': 172058805Seric map->map_app = ++p; 172158805Seric break; 172258805Seric } 172358805Seric while (*p != '\0' && !(isascii(*p) && isspace(*p))) 172458805Seric p++; 172558805Seric if (*p != '\0') 172658805Seric *p = '\0'; 172758805Seric } 172858805Seric if (map->map_app != NULL) 172958805Seric map->map_app = newstr(map->map_app); 173058805Seric 173158802Seric return TRUE; 173258802Seric } 173358802Seric /* 173458802Seric ** DEQUOTE_MAP -- unquote an address 173558802Seric ** 173658802Seric ** Parameters: 173758802Seric ** map -- the internal map structure (ignored). 173858802Seric ** buf -- the buffer to dequote. 173958802Seric ** bufsiz -- the size of that buffer. 174058802Seric ** av -- arguments (ignored). 174159084Seric ** statp -- pointer to status out-parameter. 174258802Seric ** 174358802Seric ** Returns: 174458802Seric ** NULL -- if there were no quotes, or if the resulting 174558802Seric ** unquoted buffer would not be acceptable to prescan. 174658802Seric ** else -- The dequoted buffer. 174758802Seric */ 174858802Seric 174958802Seric char * 175059084Seric dequote_map(map, buf, bufsiz, av, statp) 175158802Seric MAP *map; 175258802Seric char buf[]; 175358802Seric int bufsiz; 175458802Seric char **av; 175559084Seric int *statp; 175658802Seric { 175758802Seric register char *p; 175858802Seric register char *q; 175958802Seric register char c; 176058802Seric int anglecnt; 176158802Seric int cmntcnt; 176258802Seric int quotecnt; 176359089Seric int spacecnt; 176458802Seric bool quotemode; 176558802Seric bool bslashmode; 176658802Seric 176758802Seric anglecnt = 0; 176858802Seric cmntcnt = 0; 176958802Seric quotecnt = 0; 177059089Seric spacecnt = 0; 177158802Seric quotemode = FALSE; 177258802Seric bslashmode = FALSE; 177358802Seric 177458802Seric for (p = q = buf; (c = *p++) != '\0'; ) 177558802Seric { 177658802Seric if (bslashmode) 177758802Seric { 177858802Seric bslashmode = FALSE; 177958802Seric *q++ = c; 178058802Seric continue; 178158802Seric } 178258802Seric 178358802Seric switch (c) 178458802Seric { 178558802Seric case '\\': 178658802Seric bslashmode = TRUE; 178758802Seric break; 178858802Seric 178958802Seric case '(': 179058802Seric cmntcnt++; 179158802Seric break; 179258802Seric 179358802Seric case ')': 179458802Seric if (cmntcnt-- <= 0) 179558802Seric return NULL; 179658802Seric break; 179759089Seric 179859089Seric case ' ': 179959089Seric spacecnt++; 180059089Seric break; 180158802Seric } 180258802Seric 180358802Seric if (cmntcnt > 0) 180458802Seric { 180558802Seric *q++ = c; 180658802Seric continue; 180758802Seric } 180858802Seric 180958802Seric switch (c) 181058802Seric { 181158802Seric case '"': 181258802Seric quotemode = !quotemode; 181358802Seric quotecnt++; 181458802Seric continue; 181558802Seric 181658802Seric case '<': 181758802Seric anglecnt++; 181858802Seric break; 181958802Seric 182058802Seric case '>': 182158802Seric if (anglecnt-- <= 0) 182258802Seric return NULL; 182358802Seric break; 182458802Seric } 182558802Seric *q++ = c; 182658802Seric } 182758802Seric 182858802Seric if (anglecnt != 0 || cmntcnt != 0 || bslashmode || 182959089Seric quotemode || quotecnt <= 0 || spacecnt != 0) 183058802Seric return NULL; 183158802Seric *q++ = '\0'; 183258802Seric return buf; 183358802Seric } 1834