122976Smiriam /* 234921Sbostic * Copyright (c) 1983 Eric P. Allman 363589Sbostic * Copyright (c) 1988, 1993 463589Sbostic * The Regents of the University of California. All rights reserved. 533730Sbostic * 642828Sbostic * %sccs.include.redist.c% 733730Sbostic */ 822976Smiriam 922976Smiriam #ifndef lint 10*64284Seric static char sccsid[] = "@(#)parseaddr.c 8.7 (Berkeley) 08/17/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. 33*64284Seric ** flags -- describe detail for parsing. See RF_ definitions 34*64284Seric ** in sendmail.h. 3511445Seric ** delim -- the character to terminate the address, passed 3611445Seric ** to prescan. 3758333Seric ** delimptr -- if non-NULL, set to the location of the 3858333Seric ** delim character that was found. 3956678Seric ** e -- the envelope that will contain this address. 40297Seric ** 41297Seric ** Returns: 42297Seric ** A pointer to the address descriptor header (`a' if 43297Seric ** `a' is non-NULL). 44297Seric ** NULL on error. 45297Seric ** 46297Seric ** Side Effects: 47297Seric ** none 48297Seric */ 49297Seric 509374Seric /* following delimiters are inherent to the internal algorithms */ 5159278Seric # define DELIMCHARS "()<>,;\r\n" /* default word delimiters */ 522091Seric 532973Seric ADDRESS * 54*64284Seric parseaddr(addr, a, flags, delim, delimptr, e) 55297Seric char *addr; 562973Seric register ADDRESS *a; 57*64284Seric int flags; 5859700Seric int delim; 5958333Seric char **delimptr; 6056678Seric register ENVELOPE *e; 61297Seric { 623149Seric register char **pvp; 6358333Seric auto char *delimptrbuf; 6459084Seric bool queueup; 6516914Seric char pvpbuf[PSBUFSIZE]; 6656678Seric extern ADDRESS *buildaddr(); 6757388Seric extern bool invalidaddr(); 68297Seric 69297Seric /* 70297Seric ** Initialize and prescan address. 71297Seric */ 72297Seric 7356678Seric e->e_to = addr; 747675Seric if (tTd(20, 1)) 759888Seric printf("\n--parseaddr(%s)\n", addr); 763188Seric 7757388Seric if (invalidaddr(addr)) 7857388Seric { 7957388Seric if (tTd(20, 1)) 8057388Seric printf("parseaddr-->bad address\n"); 8157388Seric return NULL; 8257388Seric } 8357388Seric 8458333Seric if (delimptr == NULL) 8558333Seric delimptr = &delimptrbuf; 8658333Seric 8758333Seric pvp = prescan(addr, delim, pvpbuf, delimptr); 883149Seric if (pvp == NULL) 8956729Seric { 9056729Seric if (tTd(20, 1)) 9156729Seric printf("parseaddr-->NULL\n"); 92297Seric return (NULL); 9356729Seric } 94297Seric 95297Seric /* 963149Seric ** Apply rewriting rules. 977889Seric ** Ruleset 0 does basic parsing. It must resolve. 98297Seric */ 99297Seric 10059084Seric queueup = FALSE; 10159084Seric if (rewrite(pvp, 3, e) == EX_TEMPFAIL) 10259084Seric queueup = TRUE; 10359084Seric if (rewrite(pvp, 0, e) == EX_TEMPFAIL) 10459084Seric queueup = TRUE; 105297Seric 1063149Seric /* 1073149Seric ** See if we resolved to a real mailer. 1083149Seric */ 109297Seric 11059040Seric if (pvp[0] == NULL || (pvp[0][0] & 0377) != CANONNET) 1113149Seric { 1123149Seric setstat(EX_USAGE); 11363753Seric syserr("554 cannot resolve name %s", addr); 1143149Seric return (NULL); 115297Seric } 116297Seric 117297Seric /* 1183149Seric ** Build canonical address from pvp. 119297Seric */ 120297Seric 121*64284Seric a = buildaddr(pvp, a, flags, e); 1224279Seric if (a == NULL) 1234279Seric return (NULL); 124297Seric 125297Seric /* 1263149Seric ** Make local copies of the host & user and then 1273149Seric ** transport them out. 128297Seric */ 129297Seric 130*64284Seric allocaddr(a, flags, addr, *delimptr); 13156678Seric 13256678Seric /* 13359084Seric ** If there was a parsing failure, mark it for queueing. 13459084Seric */ 13559084Seric 13659084Seric if (queueup) 13759595Seric { 13859734Seric char *msg = "Transient parse error -- message queued for future delivery"; 13959734Seric 14059595Seric if (tTd(20, 1)) 14159595Seric printf("parseaddr: queuing message\n"); 14259734Seric message(msg); 14359734Seric if (e->e_message == NULL) 14460009Seric e->e_message = newstr(msg); 14559084Seric a->q_flags |= QQUEUEUP; 14659595Seric } 14759084Seric 14859084Seric /* 14956678Seric ** Compute return value. 15056678Seric */ 15156678Seric 15256678Seric if (tTd(20, 1)) 1538078Seric { 15456678Seric printf("parseaddr-->"); 15556678Seric printaddr(a, FALSE); 15656678Seric } 15756678Seric 15856678Seric return (a); 15956678Seric } 16056678Seric /* 16157388Seric ** INVALIDADDR -- check for address containing meta-characters 16257388Seric ** 16357388Seric ** Parameters: 16457388Seric ** addr -- the address to check. 16557388Seric ** 16657388Seric ** Returns: 16757388Seric ** TRUE -- if the address has any "wierd" characters 16857388Seric ** FALSE -- otherwise. 16957388Seric */ 17057388Seric 17157388Seric bool 17257388Seric invalidaddr(addr) 17357388Seric register char *addr; 17457388Seric { 17557388Seric for (; *addr != '\0'; addr++) 17657388Seric { 17758050Seric if ((*addr & 0340) != 0200) 17857388Seric continue; 17957388Seric setstat(EX_USAGE); 18058151Seric usrerr("553 Address contained invalid control characters"); 18157388Seric return TRUE; 18257388Seric } 18357388Seric return FALSE; 18457388Seric } 18557388Seric /* 18656678Seric ** ALLOCADDR -- do local allocations of address on demand. 18756678Seric ** 18856678Seric ** Also lowercases the host name if requested. 18956678Seric ** 19056678Seric ** Parameters: 19156678Seric ** a -- the address to reallocate. 192*64284Seric ** flags -- the copy flag (see RF_ definitions in sendmail.h 193*64284Seric ** for a description). 19456678Seric ** paddr -- the printname of the address. 19558333Seric ** delimptr -- a pointer to the address delimiter. Must be set. 19656678Seric ** 19756678Seric ** Returns: 19856678Seric ** none. 19956678Seric ** 20056678Seric ** Side Effects: 20156678Seric ** Copies portions of a into local buffers as requested. 20256678Seric */ 20356678Seric 204*64284Seric allocaddr(a, flags, paddr, delimptr) 20556678Seric register ADDRESS *a; 206*64284Seric int flags; 20756678Seric char *paddr; 20858333Seric char *delimptr; 20956678Seric { 21058673Seric if (tTd(24, 4)) 211*64284Seric printf("allocaddr(flags=%o, paddr=%s)\n", flags, paddr); 21258673Seric 213*64284Seric if (bitset(RF_COPYPADDR, flags) && paddr != NULL) 21456678Seric { 21558333Seric char savec = *delimptr; 2168078Seric 21759747Seric if (savec != '\0') 21859747Seric *delimptr = '\0'; 21956678Seric a->q_paddr = newstr(paddr); 22059747Seric if (savec != '\0') 22159747Seric *delimptr = savec; 2228078Seric } 223297Seric else 22456678Seric a->q_paddr = paddr; 22524944Seric 22624944Seric if (a->q_user == NULL) 22724944Seric a->q_user = ""; 22824944Seric if (a->q_host == NULL) 22924944Seric a->q_host = ""; 23024944Seric 231*64284Seric if (bitset(RF_COPYPARSE, flags)) 232297Seric { 23324944Seric a->q_host = newstr(a->q_host); 2343149Seric if (a->q_user != a->q_paddr) 2353149Seric a->q_user = newstr(a->q_user); 236297Seric } 237297Seric 23856678Seric if (a->q_paddr == NULL) 23956678Seric a->q_paddr = a->q_user; 240297Seric } 241297Seric /* 242297Seric ** PRESCAN -- Prescan name and make it canonical 243297Seric ** 2449374Seric ** Scans a name and turns it into a set of tokens. This process 2459374Seric ** deletes blanks and comments (in parentheses). 246297Seric ** 247297Seric ** This routine knows about quoted strings and angle brackets. 248297Seric ** 249297Seric ** There are certain subtleties to this routine. The one that 250297Seric ** comes to mind now is that backslashes on the ends of names 251297Seric ** are silently stripped off; this is intentional. The problem 252297Seric ** is that some versions of sndmsg (like at LBL) set the kill 253297Seric ** character to something other than @ when reading addresses; 254297Seric ** so people type "csvax.eric\@berkeley" -- which screws up the 255297Seric ** berknet mailer. 256297Seric ** 257297Seric ** Parameters: 258297Seric ** addr -- the name to chomp. 259297Seric ** delim -- the delimiter for the address, normally 260297Seric ** '\0' or ','; \0 is accepted in any case. 26115284Seric ** If '\t' then we are reading the .cf file. 26216914Seric ** pvpbuf -- place to put the saved text -- note that 26316914Seric ** the pointers are static. 26458333Seric ** delimptr -- if non-NULL, set to the location of the 26558333Seric ** terminating delimiter. 266297Seric ** 267297Seric ** Returns: 2683149Seric ** A pointer to a vector of tokens. 269297Seric ** NULL on error. 270297Seric */ 271297Seric 2728078Seric /* states and character types */ 2738078Seric # define OPR 0 /* operator */ 2748078Seric # define ATM 1 /* atom */ 2758078Seric # define QST 2 /* in quoted string */ 2768078Seric # define SPC 3 /* chewing up spaces */ 2778078Seric # define ONE 4 /* pick up one character */ 2783149Seric 2798078Seric # define NSTATES 5 /* number of states */ 2808078Seric # define TYPE 017 /* mask to select state type */ 2818078Seric 2828078Seric /* meta bits for table */ 2838078Seric # define M 020 /* meta character; don't pass through */ 2848078Seric # define B 040 /* cause a break */ 2858078Seric # define MB M|B /* meta-break */ 2868078Seric 2878078Seric static short StateTab[NSTATES][NSTATES] = 2888078Seric { 2898087Seric /* oldst chtype> OPR ATM QST SPC ONE */ 2909051Seric /*OPR*/ OPR|B, ATM|B, QST|B, SPC|MB, ONE|B, 2919051Seric /*ATM*/ OPR|B, ATM, QST|B, SPC|MB, ONE|B, 2929051Seric /*QST*/ QST, QST, OPR, QST, QST, 2938078Seric /*SPC*/ OPR, ATM, QST, SPC|M, ONE, 2948078Seric /*ONE*/ OPR, OPR, OPR, OPR, OPR, 2958078Seric }; 2968078Seric 2978078Seric # define NOCHAR -1 /* signal nothing in lookahead token */ 2988078Seric 2993149Seric char ** 30058333Seric prescan(addr, delim, pvpbuf, delimptr) 301297Seric char *addr; 302297Seric char delim; 30316914Seric char pvpbuf[]; 30458333Seric char **delimptr; 305297Seric { 306297Seric register char *p; 3078078Seric register char *q; 3089346Seric register int c; 3093149Seric char **avp; 310297Seric bool bslashmode; 311297Seric int cmntcnt; 3128423Seric int anglecnt; 3133149Seric char *tok; 3148078Seric int state; 3158078Seric int newstate; 31659747Seric char *saveto = CurEnv->e_to; 3178078Seric static char *av[MAXATOM+1]; 31856678Seric extern int errno; 319297Seric 32015253Seric /* make sure error messages don't have garbage on them */ 32115253Seric errno = 0; 32215253Seric 32316914Seric q = pvpbuf; 3243149Seric bslashmode = FALSE; 3257800Seric cmntcnt = 0; 3268423Seric anglecnt = 0; 3273149Seric avp = av; 32856678Seric state = ATM; 3298078Seric c = NOCHAR; 3308078Seric p = addr; 33159747Seric CurEnv->e_to = p; 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; 35459747Seric CurEnv->e_to = saveto; 3558078Seric return (NULL); 3568078Seric } 35715284Seric 35815284Seric /* squirrel it away */ 3598078Seric *q++ = c; 3608078Seric } 3618078Seric 3628078Seric /* read a new input character */ 3638078Seric c = *p++; 36457631Seric if (c == '\0') 36556764Seric { 36656764Seric /* diagnose and patch up bad syntax */ 36756764Seric if (state == QST) 36856764Seric { 36963851Seric usrerr("653 Unbalanced '\"' (fixed)"); 37056764Seric c = '"'; 37156764Seric } 37256764Seric else if (cmntcnt > 0) 37356764Seric { 37463851Seric usrerr("653 Unbalanced '(' (fixed)"); 37556764Seric c = ')'; 37656764Seric } 37756764Seric else if (anglecnt > 0) 37856764Seric { 37956764Seric c = '>'; 38063851Seric usrerr("653 Unbalanced '<' (fixed)"); 38156764Seric } 38256764Seric else 38356764Seric break; 38415284Seric 38556764Seric p--; 38656764Seric } 38757631Seric else if (c == delim && anglecnt <= 0 && 38857631Seric cmntcnt <= 0 && state != QST) 38957631Seric break; 39056764Seric 3918078Seric if (tTd(22, 101)) 3928078Seric printf("c=%c, s=%d; ", c, state); 3938078Seric 3943149Seric /* chew up special characters */ 3953149Seric *q = '\0'; 3963149Seric if (bslashmode) 3973149Seric { 39859105Seric bslashmode = FALSE; 39959105Seric 40024944Seric /* kludge \! for naive users */ 40158061Seric if (cmntcnt > 0) 40259105Seric { 40358061Seric c = NOCHAR; 40459105Seric continue; 40559105Seric } 40659105Seric else if (c != '!' || state == QST) 40759105Seric { 40856678Seric *q++ = '\\'; 40959105Seric continue; 41059105Seric } 4113149Seric } 41256678Seric 41356678Seric if (c == '\\') 4143149Seric { 4153149Seric bslashmode = TRUE; 4163149Seric } 41756678Seric else if (state == QST) 4188514Seric { 4198514Seric /* do nothing, just avoid next clauses */ 4208514Seric } 4218078Seric else if (c == '(') 4224100Seric { 4238078Seric cmntcnt++; 4248078Seric c = NOCHAR; 4254100Seric } 4268078Seric else if (c == ')') 4273149Seric { 4288078Seric if (cmntcnt <= 0) 4293149Seric { 43063851Seric usrerr("653 Unbalanced ')' (fixed)"); 43163844Seric c = NOCHAR; 4323149Seric } 4338078Seric else 4348078Seric cmntcnt--; 4358078Seric } 4368078Seric else if (cmntcnt > 0) 4378078Seric c = NOCHAR; 4388423Seric else if (c == '<') 4398423Seric anglecnt++; 4408423Seric else if (c == '>') 4418423Seric { 4428423Seric if (anglecnt <= 0) 4438423Seric { 44463851Seric usrerr("653 Unbalanced '>' (fixed)"); 44563844Seric c = NOCHAR; 4468423Seric } 44763844Seric else 44863844Seric anglecnt--; 4498423Seric } 45058050Seric else if (delim == ' ' && isascii(c) && isspace(c)) 45111423Seric c = ' '; 4523149Seric 4538078Seric if (c == NOCHAR) 4548078Seric continue; 4553149Seric 4568078Seric /* see if this is end of input */ 45711405Seric if (c == delim && anglecnt <= 0 && state != QST) 4583149Seric break; 4593149Seric 4608078Seric newstate = StateTab[state][toktype(c)]; 4618078Seric if (tTd(22, 101)) 4628078Seric printf("ns=%02o\n", newstate); 4638078Seric state = newstate & TYPE; 4648078Seric if (bitset(M, newstate)) 4658078Seric c = NOCHAR; 4668078Seric if (bitset(B, newstate)) 4674228Seric break; 468297Seric } 4693149Seric 4703149Seric /* new token */ 4718078Seric if (tok != q) 4721378Seric { 4738078Seric *q++ = '\0'; 4748078Seric if (tTd(22, 36)) 475297Seric { 4768078Seric printf("tok="); 4778078Seric xputs(tok); 47823109Seric (void) putchar('\n'); 479297Seric } 4808078Seric if (avp >= &av[MAXATOM]) 481297Seric { 48258151Seric syserr("553 prescan: too many tokens"); 48358333Seric if (delimptr != NULL) 48458333Seric *delimptr = p; 48559747Seric CurEnv->e_to = saveto; 4868078Seric return (NULL); 487297Seric } 4888078Seric *avp++ = tok; 489297Seric } 4908423Seric } while (c != '\0' && (c != delim || anglecnt > 0)); 4913149Seric *avp = NULL; 49258333Seric p--; 49358333Seric if (delimptr != NULL) 49458333Seric *delimptr = p; 49556764Seric if (tTd(22, 12)) 49656764Seric { 49756764Seric printf("prescan==>"); 49856764Seric printav(av); 49956764Seric } 50059747Seric CurEnv->e_to = saveto; 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 */ 6083149Seric 6099279Seric if (OpMode == MD_TEST || tTd(21, 2)) 6103149Seric { 6118959Seric printf("rewrite: ruleset %2d input:", ruleset); 61256678Seric printav(pvp); 6133149Seric } 61456678Seric if (ruleset < 0 || ruleset >= MAXRWSETS) 61556326Seric { 61658151Seric syserr("554 rewrite: illegal ruleset number %d", ruleset); 61759084Seric return EX_CONFIG; 61856326Seric } 61956678Seric if (pvp == NULL) 62059084Seric return EX_USAGE; 62156326Seric 6223149Seric /* 62356678Seric ** Run through the list of rewrite rules, applying 62456678Seric ** any that match. 6253149Seric */ 6263149Seric 62758866Seric ruleno = 1; 6284070Seric for (rwr = RewriteRules[ruleset]; rwr != NULL; ) 6293149Seric { 63056678Seric int loopcount = 0; 63156678Seric 6327675Seric if (tTd(21, 12)) 633297Seric { 6348069Seric printf("-----trying rule:"); 63556678Seric printav(rwr->r_lhs); 6363149Seric } 6373149Seric 6383149Seric /* try to match on this rule */ 6394468Seric mlp = mlist; 6408058Seric rvp = rwr->r_lhs; 6418058Seric avp = pvp; 64258866Seric if (++loopcount > 100) 6433149Seric { 64458866Seric syserr("554 Infinite loop in ruleset %d, rule %d", 64558866Seric ruleset, ruleno); 64658866Seric if (tTd(21, 1)) 64752637Seric { 64856678Seric printf("workspace: "); 64956678Seric printav(pvp); 65052637Seric } 65158866Seric break; 65258866Seric } 65358866Seric 65458866Seric while ((ap = *avp) != NULL || *rvp != NULL) 65558866Seric { 6563149Seric rp = *rvp; 6578058Seric if (tTd(21, 35)) 6588058Seric { 65958825Seric printf("ADVANCE rp="); 66057531Seric xputs(rp); 66157532Seric printf(", ap="); 6628058Seric xputs(ap); 6638069Seric printf("\n"); 6648058Seric } 66556678Seric if (rp == NULL) 66656326Seric { 6673149Seric /* end-of-pattern before end-of-address */ 6688058Seric goto backup; 66956678Seric } 67058173Seric if (ap == NULL && (*rp & 0377) != MATCHZANY && 67158827Seric (*rp & 0377) != MATCHZERO) 67256326Seric { 67358825Seric /* end-of-input with patterns left */ 67458825Seric goto backup; 675297Seric } 67656326Seric 67758050Seric switch (*rp & 0377) 6788058Seric { 67956678Seric register STAB *s; 68058814Seric char buf[MAXLINE]; 68156326Seric 68256678Seric case MATCHCLASS: 68358825Seric /* match any phrase in a class */ 68458825Seric mlp->pattern = rvp; 68558814Seric mlp->first = avp; 68658814Seric extendclass: 68758825Seric ap = *avp; 68858825Seric if (ap == NULL) 68958814Seric goto backup; 69058814Seric mlp->last = avp++; 69158814Seric cataddr(mlp->first, mlp->last, buf, sizeof buf, '\0'); 69258814Seric s = stab(buf, ST_CLASS, ST_FIND); 69356678Seric if (s == NULL || !bitnset(rp[1], s->s_class)) 69456326Seric { 69558825Seric if (tTd(21, 36)) 69658825Seric { 69758825Seric printf("EXTEND rp="); 69858825Seric xputs(rp); 69958825Seric printf(", ap="); 70058825Seric xputs(ap); 70158825Seric printf("\n"); 70258825Seric } 70358825Seric goto extendclass; 70456326Seric } 70558825Seric if (tTd(21, 36)) 70658825Seric printf("CLMATCH\n"); 70758814Seric mlp++; 70858814Seric break; 7094060Seric 71058825Seric case MATCHNCLASS: 71158825Seric /* match any token not in a class */ 71258825Seric s = stab(ap, ST_CLASS, ST_FIND); 71358825Seric if (s != NULL && bitnset(rp[1], s->s_class)) 71458825Seric goto backup; 71558825Seric 71658825Seric /* fall through */ 71758825Seric 71856678Seric case MATCHONE: 71956678Seric case MATCHANY: 72056678Seric /* match exactly one token */ 72158825Seric mlp->pattern = rvp; 72256678Seric mlp->first = avp; 72356678Seric mlp->last = avp++; 7248058Seric mlp++; 72556678Seric break; 7268058Seric 72756678Seric case MATCHZANY: 72856678Seric /* match zero or more tokens */ 72958825Seric mlp->pattern = rvp; 73056678Seric mlp->first = avp; 73156678Seric mlp->last = avp - 1; 73256678Seric mlp++; 73356678Seric break; 73456326Seric 73558827Seric case MATCHZERO: 73658173Seric /* match zero tokens */ 73758173Seric break; 73858173Seric 73959027Seric case MACRODEXPAND: 74059027Seric /* 74159027Seric ** Match against run-time macro. 74259027Seric ** This algorithm is broken for the 74359027Seric ** general case (no recursive macros, 74459027Seric ** improper tokenization) but should 74559027Seric ** work for the usual cases. 74659027Seric */ 74759027Seric 74859027Seric ap = macvalue(rp[1], e); 74959027Seric mlp->first = avp; 75059027Seric if (tTd(21, 2)) 75159027Seric printf("rewrite: LHS $&%c => \"%s\"\n", 75259027Seric rp[1], 75359027Seric ap == NULL ? "(NULL)" : ap); 75459027Seric 75559027Seric if (ap == NULL) 75659027Seric break; 75760502Seric while (*ap != '\0') 75859027Seric { 75959027Seric if (*avp == NULL || 76059027Seric strncasecmp(ap, *avp, strlen(*avp)) != 0) 76159027Seric { 76259027Seric /* no match */ 76359027Seric avp = mlp->first; 76459027Seric goto backup; 76559027Seric } 76659027Seric ap += strlen(*avp++); 76759027Seric } 76859027Seric 76959027Seric /* match */ 77059027Seric break; 77159027Seric 77256678Seric default: 77356678Seric /* must have exact match */ 77456678Seric if (strcasecmp(rp, ap)) 7758058Seric goto backup; 7764468Seric avp++; 77756678Seric break; 7783149Seric } 7793149Seric 78056678Seric /* successful match on this token */ 7813149Seric rvp++; 7823149Seric continue; 7833149Seric 78458825Seric backup: 78556678Seric /* match failed -- back up */ 78658825Seric while (--mlp >= mlist) 7873149Seric { 78858825Seric rvp = mlp->pattern; 78956678Seric rp = *rvp; 79058825Seric avp = mlp->last + 1; 79158825Seric ap = *avp; 79258825Seric 79358825Seric if (tTd(21, 36)) 79458825Seric { 79558825Seric printf("BACKUP rp="); 79658825Seric xputs(rp); 79758825Seric printf(", ap="); 79858825Seric xputs(ap); 79958825Seric printf("\n"); 80058825Seric } 80158825Seric 80258825Seric if (ap == NULL) 80358825Seric { 80458825Seric /* run off the end -- back up again */ 80558825Seric continue; 80658825Seric } 80758050Seric if ((*rp & 0377) == MATCHANY || 80858050Seric (*rp & 0377) == MATCHZANY) 8094468Seric { 81056678Seric /* extend binding and continue */ 81158825Seric mlp->last = avp++; 81256678Seric rvp++; 81358825Seric mlp++; 81456678Seric break; 8154468Seric } 81658825Seric if ((*rp & 0377) == MATCHCLASS) 81756678Seric { 81858814Seric /* extend binding and try again */ 81963397Seric mlp->last = avp; 82058814Seric goto extendclass; 82158814Seric } 8223149Seric } 8233149Seric 82458825Seric if (mlp < mlist) 82556678Seric { 82656678Seric /* total failure to match */ 82756326Seric break; 8283149Seric } 829297Seric } 8303149Seric 8313149Seric /* 83256678Seric ** See if we successfully matched 8333149Seric */ 8343149Seric 83558827Seric if (mlp < mlist || *rvp != NULL) 8363149Seric { 8379374Seric if (tTd(21, 10)) 8389374Seric printf("----- rule fails\n"); 8399374Seric rwr = rwr->r_next; 84058866Seric ruleno++; 8419374Seric continue; 8429374Seric } 8433149Seric 8449374Seric rvp = rwr->r_rhs; 8459374Seric if (tTd(21, 12)) 8469374Seric { 8479374Seric printf("-----rule matches:"); 84856678Seric printav(rvp); 8499374Seric } 8509374Seric 8519374Seric rp = *rvp; 85258050Seric if ((*rp & 0377) == CANONUSER) 8539374Seric { 8549374Seric rvp++; 8559374Seric rwr = rwr->r_next; 85658866Seric ruleno++; 8579374Seric } 85858050Seric else if ((*rp & 0377) == CANONHOST) 8599374Seric { 8609374Seric rvp++; 8619374Seric rwr = NULL; 8629374Seric } 86358050Seric else if ((*rp & 0377) == CANONNET) 8649374Seric rwr = NULL; 8659374Seric 8669374Seric /* substitute */ 8679374Seric for (avp = npvp; *rvp != NULL; rvp++) 8689374Seric { 8699374Seric register struct match *m; 8709374Seric register char **pp; 8719374Seric 8728058Seric rp = *rvp; 87358050Seric if ((*rp & 0377) == MATCHREPL) 8748058Seric { 87516914Seric /* substitute from LHS */ 87616914Seric m = &mlist[rp[1] - '1']; 87756678Seric if (m < mlist || m >= mlp) 8789374Seric { 87958151Seric syserr("554 rewrite: ruleset %d: replacement $%c out of bounds", 88056326Seric ruleset, rp[1]); 88159084Seric return EX_CONFIG; 8829374Seric } 88316914Seric if (tTd(21, 15)) 88416914Seric { 88516914Seric printf("$%c:", rp[1]); 88616914Seric pp = m->first; 88756678Seric while (pp <= m->last) 88816914Seric { 88916914Seric printf(" %x=\"", *pp); 89016914Seric (void) fflush(stdout); 89116914Seric printf("%s\"", *pp++); 89216914Seric } 89316914Seric printf("\n"); 89416914Seric } 8959374Seric pp = m->first; 89656678Seric while (pp <= m->last) 8973149Seric { 89816914Seric if (avp >= &npvp[MAXATOM]) 89956678Seric { 90058151Seric syserr("554 rewrite: expansion too long"); 90159084Seric return EX_DATAERR; 90256678Seric } 90316914Seric *avp++ = *pp++; 9043149Seric } 9053149Seric } 90616914Seric else 9078226Seric { 90816914Seric /* vanilla replacement */ 9099374Seric if (avp >= &npvp[MAXATOM]) 91016889Seric { 91156678Seric toolong: 91258151Seric syserr("554 rewrite: expansion too long"); 91359084Seric return EX_DATAERR; 91416889Seric } 91559027Seric if ((*rp & 0377) != MACRODEXPAND) 91659027Seric *avp++ = rp; 91759027Seric else 91859027Seric { 91959027Seric *avp = macvalue(rp[1], e); 92059027Seric if (tTd(21, 2)) 92159027Seric printf("rewrite: RHS $&%c => \"%s\"\n", 92259027Seric rp[1], 92359027Seric *avp == NULL ? "(NULL)" : *avp); 92459027Seric if (*avp != NULL) 92559027Seric avp++; 92659027Seric } 9278226Seric } 9289374Seric } 9299374Seric *avp++ = NULL; 93016914Seric 93116914Seric /* 93256678Seric ** Check for any hostname/keyword lookups. 93316914Seric */ 93416914Seric 93516914Seric for (rvp = npvp; *rvp != NULL; rvp++) 93616914Seric { 93756678Seric char **hbrvp; 93816914Seric char **xpvp; 93916914Seric int trsize; 94056678Seric char *replac; 94156678Seric int endtoken; 94256678Seric STAB *map; 94356678Seric char *mapname; 94456678Seric char **key_rvp; 94556678Seric char **arg_rvp; 94656678Seric char **default_rvp; 94756678Seric char buf[MAXNAME + 1]; 94816914Seric char *pvpb1[MAXATOM + 1]; 94956823Seric char *argvect[10]; 95017174Seric char pvpbuf[PSBUFSIZE]; 95116914Seric 95258050Seric if ((**rvp & 0377) != HOSTBEGIN && 95358050Seric (**rvp & 0377) != LOOKUPBEGIN) 95416914Seric continue; 95516914Seric 95616914Seric /* 95756678Seric ** Got a hostname/keyword lookup. 95816914Seric ** 95916914Seric ** This could be optimized fairly easily. 96016914Seric */ 96116914Seric 96216914Seric hbrvp = rvp; 96358050Seric if ((**rvp & 0377) == HOSTBEGIN) 96456327Seric { 96556678Seric endtoken = HOSTEND; 96656678Seric mapname = "host"; 96756327Seric } 96856326Seric else 96956327Seric { 97056678Seric endtoken = LOOKUPEND; 97156678Seric mapname = *++rvp; 97256327Seric } 97356678Seric map = stab(mapname, ST_MAP, ST_FIND); 97456678Seric if (map == NULL) 97558151Seric syserr("554 rewrite: map %s not found", mapname); 97656678Seric 97756678Seric /* extract the match part */ 97856678Seric key_rvp = ++rvp; 97956823Seric default_rvp = NULL; 98056823Seric arg_rvp = argvect; 98156823Seric xpvp = NULL; 98256823Seric replac = pvpbuf; 98358050Seric while (*rvp != NULL && (**rvp & 0377) != endtoken) 98453654Seric { 98558050Seric int nodetype = **rvp & 0377; 98656823Seric 98756823Seric if (nodetype != CANONHOST && nodetype != CANONUSER) 98856678Seric { 98956823Seric rvp++; 99056823Seric continue; 99156823Seric } 99256823Seric 99356823Seric *rvp++ = NULL; 99456823Seric 99556823Seric if (xpvp != NULL) 99656823Seric { 99758814Seric cataddr(xpvp, NULL, replac, 99858082Seric &pvpbuf[sizeof pvpbuf] - replac, 99958082Seric '\0'); 100056823Seric *++arg_rvp = replac; 100156823Seric replac += strlen(replac) + 1; 100256823Seric xpvp = NULL; 100356823Seric } 100456823Seric switch (nodetype) 100556823Seric { 100656678Seric case CANONHOST: 100756823Seric xpvp = rvp; 100856678Seric break; 100956678Seric 101056678Seric case CANONUSER: 101156678Seric default_rvp = rvp; 101256678Seric break; 101356678Seric } 101453654Seric } 101516914Seric if (*rvp != NULL) 101616914Seric *rvp++ = NULL; 101756823Seric if (xpvp != NULL) 101856823Seric { 101958814Seric cataddr(xpvp, NULL, replac, 102058082Seric &pvpbuf[sizeof pvpbuf] - replac, 102158082Seric '\0'); 102256823Seric *++arg_rvp = replac; 102356823Seric } 102456823Seric *++arg_rvp = NULL; 102516914Seric 102616914Seric /* save the remainder of the input string */ 102716914Seric trsize = (int) (avp - rvp + 1) * sizeof *rvp; 102816914Seric bcopy((char *) rvp, (char *) pvpb1, trsize); 102916914Seric 103056678Seric /* look it up */ 103158814Seric cataddr(key_rvp, NULL, buf, sizeof buf, '\0'); 103256823Seric argvect[0] = buf; 103360538Seric if (map != NULL && bitset(MF_OPEN, map->s_map.map_mflags)) 103456836Seric { 103559084Seric auto int stat = EX_OK; 103656836Seric 103760215Seric /* XXX should try to auto-open the map here */ 103860215Seric 103958796Seric if (tTd(60, 1)) 104058796Seric printf("map_lookup(%s, %s) => ", 104158796Seric mapname, buf); 104256836Seric replac = (*map->s_map.map_class->map_lookup)(&map->s_map, 104360089Seric buf, argvect, &stat); 104458796Seric if (tTd(60, 1)) 104559084Seric printf("%s (%d)\n", 104659084Seric replac ? replac : "NOT FOUND", 104759084Seric stat); 104859084Seric 104959084Seric /* should recover if stat == EX_TEMPFAIL */ 105059084Seric if (stat == EX_TEMPFAIL) 105159084Seric rstat = stat; 105256836Seric } 105353654Seric else 105456678Seric replac = NULL; 105556678Seric 105656678Seric /* if no replacement, use default */ 105756823Seric if (replac == NULL && default_rvp != NULL) 105856823Seric { 105960089Seric /* create the default */ 106060089Seric cataddr(default_rvp, NULL, buf, sizeof buf, '\0'); 106156823Seric replac = buf; 106256823Seric } 106356823Seric 106456678Seric if (replac == NULL) 106551317Seric { 106656823Seric xpvp = key_rvp; 106753654Seric } 106856678Seric else 106953654Seric { 107056678Seric /* scan the new replacement */ 107158333Seric xpvp = prescan(replac, '\0', pvpbuf, NULL); 107253654Seric if (xpvp == NULL) 107351317Seric { 107458403Seric /* prescan already printed error */ 107559084Seric return EX_DATAERR; 107656678Seric } 107751317Seric } 107851317Seric 107916914Seric /* append it to the token list */ 108056678Seric for (avp = hbrvp; *xpvp != NULL; xpvp++) 108156678Seric { 108217174Seric *avp++ = newstr(*xpvp); 108316920Seric if (avp >= &npvp[MAXATOM]) 108416914Seric goto toolong; 108517174Seric } 108616914Seric 108716914Seric /* restore the old trailing information */ 108856678Seric for (xpvp = pvpb1; (*avp++ = *xpvp++) != NULL; ) 108916920Seric if (avp >= &npvp[MAXATOM]) 109016914Seric goto toolong; 109117174Seric 109256678Seric break; 109316914Seric } 109416914Seric 109516914Seric /* 109616914Seric ** Check for subroutine calls. 109716914Seric */ 109816914Seric 109958050Seric if (*npvp != NULL && (**npvp & 0377) == CALLSUBR) 110056678Seric { 110159084Seric int stat; 110259084Seric 110356678Seric bcopy((char *) &npvp[2], (char *) pvp, 110456678Seric (int) (avp - npvp - 2) * sizeof *avp); 110556678Seric if (tTd(21, 3)) 110656678Seric printf("-----callsubr %s\n", npvp[1]); 110759084Seric stat = rewrite(pvp, atoi(npvp[1]), e); 110859084Seric if (rstat == EX_OK || stat == EX_TEMPFAIL) 110959084Seric rstat = stat; 111063581Seric if ((**pvp & 0377) == CANONNET) 111163581Seric rwr = NULL; 111256678Seric } 111356678Seric else 111456678Seric { 111517348Seric bcopy((char *) npvp, (char *) pvp, 111616900Seric (int) (avp - npvp) * sizeof *avp); 111756678Seric } 11189374Seric if (tTd(21, 4)) 11199374Seric { 11209374Seric printf("rewritten as:"); 112156678Seric printav(pvp); 11229374Seric } 1123297Seric } 11248069Seric 11259279Seric if (OpMode == MD_TEST || tTd(21, 2)) 11268069Seric { 11278959Seric printf("rewrite: ruleset %2d returns:", ruleset); 112856678Seric printav(pvp); 11298069Seric } 113059084Seric 113159084Seric return rstat; 11323149Seric } 11333149Seric /* 11343149Seric ** BUILDADDR -- build address from token vector. 11353149Seric ** 11363149Seric ** Parameters: 11373149Seric ** tv -- token vector. 11383149Seric ** a -- pointer to address descriptor to fill. 11393149Seric ** If NULL, one will be allocated. 1140*64284Seric ** flags -- info regarding whether this is a sender or 1141*64284Seric ** a recipient. 114258966Seric ** e -- the current envelope. 11433149Seric ** 11443149Seric ** Returns: 11454279Seric ** NULL if there was an error. 11464279Seric ** 'a' otherwise. 11473149Seric ** 11483149Seric ** Side Effects: 11493149Seric ** fills in 'a' 11503149Seric */ 11513149Seric 115257249Seric struct errcodes 115357249Seric { 115457249Seric char *ec_name; /* name of error code */ 115557249Seric int ec_code; /* numeric code */ 115657249Seric } ErrorCodes[] = 115757249Seric { 115857249Seric "usage", EX_USAGE, 115957249Seric "nouser", EX_NOUSER, 116057249Seric "nohost", EX_NOHOST, 116157249Seric "unavailable", EX_UNAVAILABLE, 116257249Seric "software", EX_SOFTWARE, 116357249Seric "tempfail", EX_TEMPFAIL, 116457249Seric "protocol", EX_PROTOCOL, 116557249Seric #ifdef EX_CONFIG 116657249Seric "config", EX_CONFIG, 116757249Seric #endif 116857249Seric NULL, EX_UNAVAILABLE, 116957249Seric }; 117057249Seric 117156678Seric ADDRESS * 1172*64284Seric buildaddr(tv, a, flags, e) 11733149Seric register char **tv; 11743149Seric register ADDRESS *a; 1175*64284Seric int flags; 117658966Seric register ENVELOPE *e; 11773149Seric { 11783149Seric struct mailer **mp; 11793149Seric register struct mailer *m; 118058008Seric char *bp; 118158008Seric int spaceleft; 118257402Seric static char buf[MAXNAME]; 11833149Seric 11843149Seric if (a == NULL) 11853149Seric a = (ADDRESS *) xalloc(sizeof *a); 118616889Seric bzero((char *) a, sizeof *a); 11873149Seric 11883149Seric /* figure out what net/mailer to use */ 118958050Seric if ((**tv & 0377) != CANONNET) 11904279Seric { 119158151Seric syserr("554 buildaddr: no net"); 11924279Seric return (NULL); 11934279Seric } 11943149Seric tv++; 119558680Seric if (strcasecmp(*tv, "error") == 0) 11964279Seric { 119758050Seric if ((**++tv & 0377) == CANONHOST) 119810183Seric { 119957249Seric register struct errcodes *ep; 120057249Seric 120158050Seric if (isascii(**++tv) && isdigit(**tv)) 120257249Seric { 120357249Seric setstat(atoi(*tv)); 120457249Seric } 120557249Seric else 120657249Seric { 120757249Seric for (ep = ErrorCodes; ep->ec_name != NULL; ep++) 120857249Seric if (strcasecmp(ep->ec_name, *tv) == 0) 120957249Seric break; 121057249Seric setstat(ep->ec_code); 121157249Seric } 121210183Seric tv++; 121310183Seric } 121458050Seric if ((**tv & 0377) != CANONUSER) 121558151Seric syserr("554 buildaddr: error: no user"); 121658814Seric cataddr(++tv, NULL, buf, sizeof buf, ' '); 121758082Seric stripquotes(buf); 12184279Seric usrerr(buf); 12194279Seric return (NULL); 12204279Seric } 122157402Seric 12224598Seric for (mp = Mailer; (m = *mp++) != NULL; ) 12233149Seric { 122458680Seric if (strcasecmp(m->m_name, *tv) == 0) 12253149Seric break; 12263149Seric } 12273149Seric if (m == NULL) 12284279Seric { 122958151Seric syserr("554 buildaddr: unknown mailer %s", *tv); 12304279Seric return (NULL); 12314279Seric } 12324598Seric a->q_mailer = m; 12333149Seric 12343149Seric /* figure out what host (if any) */ 123556678Seric tv++; 123658509Seric if ((**tv & 0377) == CANONHOST) 12373149Seric { 123858008Seric bp = buf; 123958008Seric spaceleft = sizeof buf - 1; 124058050Seric while (*++tv != NULL && (**tv & 0377) != CANONUSER) 124158008Seric { 124258008Seric int i = strlen(*tv); 124358008Seric 124458008Seric if (i > spaceleft) 124558008Seric { 124658008Seric /* out of space for this address */ 124758008Seric if (spaceleft >= 0) 124858151Seric syserr("554 buildaddr: host too long (%.40s...)", 124958008Seric buf); 125058008Seric i = spaceleft; 125158008Seric spaceleft = 0; 125258008Seric } 125358008Seric if (i <= 0) 125458008Seric continue; 125558008Seric bcopy(*tv, bp, i); 125658008Seric bp += i; 125758008Seric spaceleft -= i; 125858008Seric } 125958010Seric *bp = '\0'; 12605704Seric a->q_host = newstr(buf); 12613149Seric } 126257249Seric else 126358509Seric { 126458509Seric if (!bitnset(M_LOCALMAILER, m->m_flags)) 126558509Seric { 126658509Seric syserr("554 buildaddr: no host"); 126758509Seric return (NULL); 126858509Seric } 126957249Seric a->q_host = NULL; 127058509Seric } 12713149Seric 12723149Seric /* figure out the user */ 127358050Seric if (*tv == NULL || (**tv & 0377) != CANONUSER) 12744279Seric { 127558151Seric syserr("554 buildaddr: no user"); 12764279Seric return (NULL); 12774279Seric } 127857402Seric tv++; 127951317Seric 128057402Seric /* do special mapping for local mailer */ 128157402Seric if (m == LocalMailer && *tv != NULL) 128257402Seric { 128357454Seric register char *p = *tv; 128457454Seric 128557454Seric if (*p == '"') 128657454Seric p++; 128757454Seric if (*p == '|') 128857402Seric a->q_mailer = m = ProgMailer; 128957454Seric else if (*p == '/') 129057402Seric a->q_mailer = m = FileMailer; 129157454Seric else if (*p == ':') 129257402Seric { 129357402Seric /* may be :include: */ 129458814Seric cataddr(tv, NULL, buf, sizeof buf, '\0'); 129558008Seric stripquotes(buf); 129658008Seric if (strncasecmp(buf, ":include:", 9) == 0) 129758008Seric { 129858008Seric /* if :include:, don't need further rewriting */ 129957402Seric a->q_mailer = m = InclMailer; 130058008Seric a->q_user = &buf[9]; 130158008Seric return (a); 130258008Seric } 130357402Seric } 130457402Seric } 130557402Seric 130658008Seric if (m == LocalMailer && *tv != NULL && strcmp(*tv, "@") == 0) 130758008Seric { 130858008Seric tv++; 130958008Seric a->q_flags |= QNOTREMOTE; 131058008Seric } 131158008Seric 1312*64284Seric /* rewrite according recipient mailer rewriting rules */ 1313*64284Seric define('h', a->q_host, e); 1314*64284Seric if (!bitset(RF_SENDERADDR|RF_HEADERADDR, flags)) 1315*64284Seric { 1316*64284Seric /* sender addresses done later */ 1317*64284Seric (void) rewrite(tv, 2, e); 1318*64284Seric if (m->m_re_rwset > 0) 1319*64284Seric (void) rewrite(tv, m->m_re_rwset, e); 1320*64284Seric } 132159084Seric (void) rewrite(tv, 4, e); 132219040Seric 132319040Seric /* save the result for the command line/RCPT argument */ 132458814Seric cataddr(tv, NULL, buf, sizeof buf, '\0'); 13253149Seric a->q_user = buf; 13263149Seric 132758670Seric /* 132858670Seric ** Do mapping to lower case as requested by mailer 132958670Seric */ 133058670Seric 133158670Seric if (a->q_host != NULL && !bitnset(M_HST_UPPER, m->m_flags)) 133258670Seric makelower(a->q_host); 133358670Seric if (!bitnset(M_USR_UPPER, m->m_flags)) 133458670Seric makelower(a->q_user); 133558670Seric 13363149Seric return (a); 13373149Seric } 13383188Seric /* 13394228Seric ** CATADDR -- concatenate pieces of addresses (putting in <LWSP> subs) 13404228Seric ** 13414228Seric ** Parameters: 13424228Seric ** pvp -- parameter vector to rebuild. 134358814Seric ** evp -- last parameter to include. Can be NULL to 134458814Seric ** use entire pvp. 13454228Seric ** buf -- buffer to build the string into. 13464228Seric ** sz -- size of buf. 134758082Seric ** spacesub -- the space separator character; if null, 134858082Seric ** use SpaceSub. 13494228Seric ** 13504228Seric ** Returns: 13514228Seric ** none. 13524228Seric ** 13534228Seric ** Side Effects: 13544228Seric ** Destroys buf. 13554228Seric */ 13564228Seric 135758814Seric cataddr(pvp, evp, buf, sz, spacesub) 13584228Seric char **pvp; 135958814Seric char **evp; 13604228Seric char *buf; 13614228Seric register int sz; 136258082Seric char spacesub; 13634228Seric { 13644228Seric bool oatomtok = FALSE; 136556678Seric bool natomtok = FALSE; 13664228Seric register int i; 13674228Seric register char *p; 13684228Seric 136958082Seric if (spacesub == '\0') 137058082Seric spacesub = SpaceSub; 137158082Seric 13728423Seric if (pvp == NULL) 13738423Seric { 137423109Seric (void) strcpy(buf, ""); 13758423Seric return; 13768423Seric } 13774228Seric p = buf; 137811156Seric sz -= 2; 13794228Seric while (*pvp != NULL && (i = strlen(*pvp)) < sz) 13804228Seric { 13818078Seric natomtok = (toktype(**pvp) == ATM); 13824228Seric if (oatomtok && natomtok) 138358082Seric *p++ = spacesub; 13844228Seric (void) strcpy(p, *pvp); 13854228Seric oatomtok = natomtok; 13864228Seric p += i; 138711156Seric sz -= i + 1; 138858814Seric if (pvp++ == evp) 138958814Seric break; 13904228Seric } 13914228Seric *p = '\0'; 13924228Seric } 13934228Seric /* 13943188Seric ** SAMEADDR -- Determine if two addresses are the same 13953188Seric ** 13963188Seric ** This is not just a straight comparison -- if the mailer doesn't 13973188Seric ** care about the host we just ignore it, etc. 13983188Seric ** 13993188Seric ** Parameters: 14003188Seric ** a, b -- pointers to the internal forms to compare. 14013188Seric ** 14023188Seric ** Returns: 14033188Seric ** TRUE -- they represent the same mailbox. 14043188Seric ** FALSE -- they don't. 14053188Seric ** 14063188Seric ** Side Effects: 14073188Seric ** none. 14083188Seric */ 14093188Seric 14103188Seric bool 14119374Seric sameaddr(a, b) 14123188Seric register ADDRESS *a; 14133188Seric register ADDRESS *b; 14143188Seric { 14153188Seric /* if they don't have the same mailer, forget it */ 14163188Seric if (a->q_mailer != b->q_mailer) 14173188Seric return (FALSE); 14183188Seric 14193188Seric /* if the user isn't the same, we can drop out */ 142056678Seric if (strcmp(a->q_user, b->q_user) != 0) 14213188Seric return (FALSE); 14223188Seric 142358438Seric /* if we have good uids for both but the differ, these are different */ 142458438Seric if (bitset(QGOODUID, a->q_flags & b->q_flags) && a->q_uid != b->q_uid) 142558438Seric return (FALSE); 142658438Seric 142758509Seric /* otherwise compare hosts (but be careful for NULL ptrs) */ 142858509Seric if (a->q_host == b->q_host) 142958509Seric { 143058509Seric /* probably both null pointers */ 14313188Seric return (TRUE); 143258509Seric } 14333188Seric if (a->q_host == NULL || b->q_host == NULL) 143458509Seric { 143558509Seric /* only one is a null pointer */ 14363188Seric return (FALSE); 143758509Seric } 143856678Seric if (strcmp(a->q_host, b->q_host) != 0) 14393188Seric return (FALSE); 14403188Seric 14413188Seric return (TRUE); 14423188Seric } 14433234Seric /* 14443234Seric ** PRINTADDR -- print address (for debugging) 14453234Seric ** 14463234Seric ** Parameters: 14473234Seric ** a -- the address to print 14483234Seric ** follow -- follow the q_next chain. 14493234Seric ** 14503234Seric ** Returns: 14513234Seric ** none. 14523234Seric ** 14533234Seric ** Side Effects: 14543234Seric ** none. 14553234Seric */ 14563234Seric 14573234Seric printaddr(a, follow) 14583234Seric register ADDRESS *a; 14593234Seric bool follow; 14603234Seric { 14615001Seric bool first = TRUE; 146257731Seric register MAILER *m; 146357731Seric MAILER pseudomailer; 14645001Seric 14653234Seric while (a != NULL) 14663234Seric { 14675001Seric first = FALSE; 14684443Seric printf("%x=", a); 14694085Seric (void) fflush(stdout); 147057731Seric 147157731Seric /* find the mailer -- carefully */ 147257731Seric m = a->q_mailer; 147357731Seric if (m == NULL) 147457731Seric { 147557731Seric m = &pseudomailer; 147657731Seric m->m_mno = -1; 147757731Seric m->m_name = "NULL"; 147857731Seric } 147957731Seric 148058680Seric printf("%s:\n\tmailer %d (%s), host `%s', user `%s', ruser `%s'\n", 148157731Seric a->q_paddr, m->m_mno, m->m_name, 148263756Seric a->q_host, a->q_user, 148363756Seric a->q_ruser ? a->q_ruser : "<null>"); 148459269Seric printf("\tnext=%x, flags=%o, alias %x, uid %d, gid %d\n", 148559269Seric a->q_next, a->q_flags, a->q_alias, a->q_uid, a->q_gid); 148659269Seric printf("\towner=%s, home=\"%s\", fullname=\"%s\"\n", 148759269Seric a->q_owner == NULL ? "(none)" : a->q_owner, 148863756Seric a->q_home == NULL ? "(none)" : a->q_home, 148963756Seric a->q_fullname == NULL ? "(none)" : a->q_fullname); 14904996Seric 14913234Seric if (!follow) 14923234Seric return; 14934996Seric a = a->q_next; 14943234Seric } 14955001Seric if (first) 14964443Seric printf("[NULL]\n"); 14973234Seric } 14984317Seric 14997682Seric /* 15007682Seric ** REMOTENAME -- return the name relative to the current mailer 15017682Seric ** 15027682Seric ** Parameters: 15037682Seric ** name -- the name to translate. 15048069Seric ** m -- the mailer that we want to do rewriting relative 15058069Seric ** to. 150659163Seric ** flags -- fine tune operations. 150759163Seric ** pstat -- pointer to status word. 150858020Seric ** e -- the current envelope. 15097682Seric ** 15107682Seric ** Returns: 15117682Seric ** the text string representing this address relative to 15127682Seric ** the receiving mailer. 15137682Seric ** 15147682Seric ** Side Effects: 15157682Seric ** none. 15167682Seric ** 15177682Seric ** Warnings: 15187682Seric ** The text string returned is tucked away locally; 15197682Seric ** copy it if you intend to save it. 15207682Seric */ 15217682Seric 15227682Seric char * 152359163Seric remotename(name, m, flags, pstat, e) 15247682Seric char *name; 152556678Seric struct mailer *m; 152659163Seric int flags; 152759163Seric int *pstat; 152856678Seric register ENVELOPE *e; 15297682Seric { 15308069Seric register char **pvp; 15318069Seric char *fancy; 153256678Seric char *oldg = macvalue('g', e); 153358020Seric int rwset; 15347682Seric static char buf[MAXNAME]; 15357682Seric char lbuf[MAXNAME]; 153616914Seric char pvpbuf[PSBUFSIZE]; 153756678Seric extern char *crackaddr(); 15387682Seric 15397755Seric if (tTd(12, 1)) 15407755Seric printf("remotename(%s)\n", name); 15417755Seric 154210177Seric /* don't do anything if we are tagging it as special */ 154359163Seric if (bitset(RF_SENDERADDR, flags)) 154459163Seric rwset = bitset(RF_HEADERADDR, flags) ? m->m_sh_rwset 154559163Seric : m->m_se_rwset; 154658020Seric else 154759163Seric rwset = bitset(RF_HEADERADDR, flags) ? m->m_rh_rwset 154859163Seric : m->m_re_rwset; 154958020Seric if (rwset < 0) 155010177Seric return (name); 155110177Seric 15527682Seric /* 15538181Seric ** Do a heuristic crack of this name to extract any comment info. 15548181Seric ** This will leave the name as a comment and a $g macro. 15557889Seric */ 15567889Seric 155759163Seric if (bitset(RF_CANONICAL, flags) || bitnset(M_NOCOMMENT, m->m_flags)) 155858050Seric fancy = "\201g"; 155910310Seric else 156010310Seric fancy = crackaddr(name); 15617889Seric 15628181Seric /* 15638181Seric ** Turn the name into canonical form. 15648181Seric ** Normally this will be RFC 822 style, i.e., "user@domain". 15658181Seric ** If this only resolves to "user", and the "C" flag is 15668181Seric ** specified in the sending mailer, then the sender's 15678181Seric ** domain will be appended. 15688181Seric */ 15698181Seric 157058333Seric pvp = prescan(name, '\0', pvpbuf, NULL); 15717889Seric if (pvp == NULL) 15727889Seric return (name); 157359163Seric if (rewrite(pvp, 3, e) == EX_TEMPFAIL) 157459163Seric *pstat = EX_TEMPFAIL; 157559163Seric if (bitset(RF_ADDDOMAIN, flags) && e->e_fromdomain != NULL) 15768181Seric { 15778181Seric /* append from domain to this address */ 15788181Seric register char **pxp = pvp; 15798181Seric 15809594Seric /* see if there is an "@domain" in the current name */ 15818181Seric while (*pxp != NULL && strcmp(*pxp, "@") != 0) 15828181Seric pxp++; 15838181Seric if (*pxp == NULL) 15848181Seric { 15859594Seric /* no.... append the "@domain" from the sender */ 158656678Seric register char **qxq = e->e_fromdomain; 15878181Seric 15889594Seric while ((*pxp++ = *qxq++) != NULL) 15899594Seric continue; 159059163Seric if (rewrite(pvp, 3, e) == EX_TEMPFAIL) 159159163Seric *pstat = EX_TEMPFAIL; 15928181Seric } 15938181Seric } 15948181Seric 15958181Seric /* 15968959Seric ** Do more specific rewriting. 159756678Seric ** Rewrite using ruleset 1 or 2 depending on whether this is 159856678Seric ** a sender address or not. 15998181Seric ** Then run it through any receiving-mailer-specific rulesets. 16008181Seric */ 16018181Seric 160259163Seric if (bitset(RF_SENDERADDR, flags)) 160359541Seric { 160459163Seric if (rewrite(pvp, 1, e) == EX_TEMPFAIL) 160559163Seric *pstat = EX_TEMPFAIL; 160659541Seric } 16078069Seric else 160859541Seric { 160959163Seric if (rewrite(pvp, 2, e) == EX_TEMPFAIL) 161059163Seric *pstat = EX_TEMPFAIL; 161159541Seric } 161258020Seric if (rwset > 0) 161359541Seric { 161459163Seric if (rewrite(pvp, rwset, e) == EX_TEMPFAIL) 161559163Seric *pstat = EX_TEMPFAIL; 161659541Seric } 16177682Seric 16188181Seric /* 16198959Seric ** Do any final sanitation the address may require. 16208959Seric ** This will normally be used to turn internal forms 16218959Seric ** (e.g., user@host.LOCAL) into external form. This 16228959Seric ** may be used as a default to the above rules. 16238959Seric */ 16248959Seric 162559163Seric if (rewrite(pvp, 4, e) == EX_TEMPFAIL) 162659163Seric *pstat = EX_TEMPFAIL; 16278959Seric 16288959Seric /* 16298181Seric ** Now restore the comment information we had at the beginning. 16308181Seric */ 16318181Seric 163258825Seric cataddr(pvp, NULL, lbuf, sizeof lbuf, '\0'); 163356678Seric define('g', lbuf, e); 163456678Seric expand(fancy, buf, &buf[sizeof buf - 1], e); 163556678Seric define('g', oldg, e); 16367682Seric 16377682Seric if (tTd(12, 1)) 16387755Seric printf("remotename => `%s'\n", buf); 16397682Seric return (buf); 16407682Seric } 164151317Seric /* 164256678Seric ** MAPLOCALUSER -- run local username through ruleset 5 for final redirection 164351317Seric ** 164451317Seric ** Parameters: 164556678Seric ** a -- the address to map (but just the user name part). 164656678Seric ** sendq -- the sendq in which to install any replacement 164756678Seric ** addresses. 164851317Seric ** 164951317Seric ** Returns: 165051317Seric ** none. 165151317Seric */ 165251317Seric 165356678Seric maplocaluser(a, sendq, e) 165456678Seric register ADDRESS *a; 165556678Seric ADDRESS **sendq; 165656678Seric ENVELOPE *e; 165751317Seric { 165856678Seric register char **pvp; 165956678Seric register ADDRESS *a1 = NULL; 166058333Seric auto char *delimptr; 166156678Seric char pvpbuf[PSBUFSIZE]; 166251317Seric 166356678Seric if (tTd(29, 1)) 166456678Seric { 166556678Seric printf("maplocaluser: "); 166656678Seric printaddr(a, FALSE); 166756678Seric } 166858333Seric pvp = prescan(a->q_user, '\0', pvpbuf, &delimptr); 166956678Seric if (pvp == NULL) 167056678Seric return; 167151317Seric 167259084Seric (void) rewrite(pvp, 5, e); 167358050Seric if (pvp[0] == NULL || (pvp[0][0] & 0377) != CANONNET) 167456678Seric return; 167551317Seric 167656678Seric /* if non-null, mailer destination specified -- has it changed? */ 1677*64284Seric a1 = buildaddr(pvp, NULL, 0, e); 167856678Seric if (a1 == NULL || sameaddr(a, a1)) 167956678Seric return; 168051317Seric 168156678Seric /* mark old address as dead; insert new address */ 168256678Seric a->q_flags |= QDONTSEND; 168357731Seric if (tTd(29, 5)) 168457731Seric { 168557731Seric printf("maplocaluser: QDONTSEND "); 168657731Seric printaddr(a, FALSE); 168757731Seric } 168856678Seric a1->q_alias = a; 1689*64284Seric allocaddr(a1, RF_COPYALL, NULL, delimptr); 169056678Seric (void) recipient(a1, sendq, e); 169151317Seric } 169258802Seric /* 169358802Seric ** DEQUOTE_INIT -- initialize dequote map 169458802Seric ** 169558802Seric ** This is a no-op. 169658802Seric ** 169758802Seric ** Parameters: 169858802Seric ** map -- the internal map structure. 169958802Seric ** args -- arguments. 170058802Seric ** 170158802Seric ** Returns: 170258802Seric ** TRUE. 170358802Seric */ 170458802Seric 170558802Seric bool 170660219Seric dequote_init(map, args) 170758802Seric MAP *map; 170858802Seric char *args; 170958802Seric { 171058805Seric register char *p = args; 171158805Seric 171258805Seric for (;;) 171358805Seric { 171458805Seric while (isascii(*p) && isspace(*p)) 171558805Seric p++; 171658805Seric if (*p != '-') 171758805Seric break; 171858805Seric switch (*++p) 171958805Seric { 172058805Seric case 'a': 172158805Seric map->map_app = ++p; 172258805Seric break; 172358805Seric } 172458805Seric while (*p != '\0' && !(isascii(*p) && isspace(*p))) 172558805Seric p++; 172658805Seric if (*p != '\0') 172758805Seric *p = '\0'; 172858805Seric } 172958805Seric if (map->map_app != NULL) 173058805Seric map->map_app = newstr(map->map_app); 173158805Seric 173258802Seric return TRUE; 173358802Seric } 173458802Seric /* 173558802Seric ** DEQUOTE_MAP -- unquote an address 173658802Seric ** 173758802Seric ** Parameters: 173858802Seric ** map -- the internal map structure (ignored). 173960089Seric ** name -- the name to dequote. 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 * 175060089Seric dequote_map(map, name, av, statp) 175158802Seric MAP *map; 175260089Seric char *name; 175358802Seric char **av; 175459084Seric int *statp; 175558802Seric { 175658802Seric register char *p; 175758802Seric register char *q; 175858802Seric register char c; 175958802Seric int anglecnt; 176058802Seric int cmntcnt; 176158802Seric int quotecnt; 176259089Seric int spacecnt; 176358802Seric bool quotemode; 176458802Seric bool bslashmode; 176558802Seric 176658802Seric anglecnt = 0; 176758802Seric cmntcnt = 0; 176858802Seric quotecnt = 0; 176959089Seric spacecnt = 0; 177058802Seric quotemode = FALSE; 177158802Seric bslashmode = FALSE; 177258802Seric 177360089Seric for (p = q = name; (c = *p++) != '\0'; ) 177458802Seric { 177558802Seric if (bslashmode) 177658802Seric { 177758802Seric bslashmode = FALSE; 177858802Seric *q++ = c; 177958802Seric continue; 178058802Seric } 178158802Seric 178258802Seric switch (c) 178358802Seric { 178458802Seric case '\\': 178558802Seric bslashmode = TRUE; 178658802Seric break; 178758802Seric 178858802Seric case '(': 178958802Seric cmntcnt++; 179058802Seric break; 179158802Seric 179258802Seric case ')': 179358802Seric if (cmntcnt-- <= 0) 179458802Seric return NULL; 179558802Seric break; 179659089Seric 179759089Seric case ' ': 179859089Seric spacecnt++; 179959089Seric break; 180058802Seric } 180158802Seric 180258802Seric if (cmntcnt > 0) 180358802Seric { 180458802Seric *q++ = c; 180558802Seric continue; 180658802Seric } 180758802Seric 180858802Seric switch (c) 180958802Seric { 181058802Seric case '"': 181158802Seric quotemode = !quotemode; 181258802Seric quotecnt++; 181358802Seric continue; 181458802Seric 181558802Seric case '<': 181658802Seric anglecnt++; 181758802Seric break; 181858802Seric 181958802Seric case '>': 182058802Seric if (anglecnt-- <= 0) 182158802Seric return NULL; 182258802Seric break; 182358802Seric } 182458802Seric *q++ = c; 182558802Seric } 182658802Seric 182758802Seric if (anglecnt != 0 || cmntcnt != 0 || bslashmode || 182859089Seric quotemode || quotecnt <= 0 || spacecnt != 0) 182958802Seric return NULL; 183058802Seric *q++ = '\0'; 183160089Seric return name; 183258802Seric } 1833