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*64348Seric static char sccsid[] = "@(#)parseaddr.c 8.9 (Berkeley) 08/24/93"; 1133730Sbostic #endif /* not lint */ 1222976Smiriam 1356678Seric # include "sendmail.h" 14297Seric 15297Seric /* 169888Seric ** PARSEADDR -- Parse an address 17297Seric ** 18297Seric ** Parses an address and breaks it up into three parts: a 19297Seric ** net to transmit the message on, the host to transmit it 20297Seric ** to, and a user on that host. These are loaded into an 212973Seric ** ADDRESS header with the values squirreled away if necessary. 22297Seric ** The "user" part may not be a real user; the process may 23297Seric ** just reoccur on that machine. For example, on a machine 24297Seric ** with an arpanet connection, the address 25297Seric ** csvax.bill@berkeley 26297Seric ** will break up to a "user" of 'csvax.bill' and a host 27297Seric ** of 'berkeley' -- to be transmitted over the arpanet. 28297Seric ** 29297Seric ** Parameters: 30297Seric ** addr -- the address to parse. 31297Seric ** a -- a pointer to the address descriptor buffer. 32297Seric ** If NULL, a header will be created. 3364284Seric ** flags -- describe detail for parsing. See RF_ definitions 3464284Seric ** 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 * 5464284Seric parseaddr(addr, a, flags, delim, delimptr, e) 55297Seric char *addr; 562973Seric register ADDRESS *a; 5764284Seric 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 /* 96*64348Seric ** Save addr if we are going to have to. 97*64348Seric ** 98*64348Seric ** We have to do this early because there is a chance that 99*64348Seric ** the map lookups in the rewriting rules could clobber 100*64348Seric ** static memory somewhere. 101*64348Seric */ 102*64348Seric 103*64348Seric if (bitset(RF_COPYPADDR, flags) && addr != NULL) 104*64348Seric { 105*64348Seric char savec = **delimptr; 106*64348Seric 107*64348Seric if (savec != '\0') 108*64348Seric **delimptr = '\0'; 109*64348Seric addr = newstr(addr); 110*64348Seric if (savec != '\0') 111*64348Seric **delimptr = savec; 112*64348Seric } 113*64348Seric 114*64348Seric /* 1153149Seric ** Apply rewriting rules. 1167889Seric ** Ruleset 0 does basic parsing. It must resolve. 117297Seric */ 118297Seric 11959084Seric queueup = FALSE; 12059084Seric if (rewrite(pvp, 3, e) == EX_TEMPFAIL) 12159084Seric queueup = TRUE; 12259084Seric if (rewrite(pvp, 0, e) == EX_TEMPFAIL) 12359084Seric queueup = TRUE; 124297Seric 125297Seric 126297Seric /* 1273149Seric ** Build canonical address from pvp. 128297Seric */ 129297Seric 13064284Seric a = buildaddr(pvp, a, flags, e); 131297Seric 132297Seric /* 1333149Seric ** Make local copies of the host & user and then 1343149Seric ** transport them out. 135297Seric */ 136297Seric 137*64348Seric allocaddr(a, flags, addr); 13864306Seric if (bitset(QBADADDR, a->q_flags)) 13964306Seric return a; 14056678Seric 14156678Seric /* 14259084Seric ** If there was a parsing failure, mark it for queueing. 14359084Seric */ 14459084Seric 14559084Seric if (queueup) 14659595Seric { 14759734Seric char *msg = "Transient parse error -- message queued for future delivery"; 14859734Seric 14959595Seric if (tTd(20, 1)) 15059595Seric printf("parseaddr: queuing message\n"); 15159734Seric message(msg); 15259734Seric if (e->e_message == NULL) 15360009Seric e->e_message = newstr(msg); 15459084Seric a->q_flags |= QQUEUEUP; 15559595Seric } 15659084Seric 15759084Seric /* 15856678Seric ** Compute return value. 15956678Seric */ 16056678Seric 16156678Seric if (tTd(20, 1)) 1628078Seric { 16356678Seric printf("parseaddr-->"); 16456678Seric printaddr(a, FALSE); 16556678Seric } 16656678Seric 16756678Seric return (a); 16856678Seric } 16956678Seric /* 17057388Seric ** INVALIDADDR -- check for address containing meta-characters 17157388Seric ** 17257388Seric ** Parameters: 17357388Seric ** addr -- the address to check. 17457388Seric ** 17557388Seric ** Returns: 17657388Seric ** TRUE -- if the address has any "wierd" characters 17757388Seric ** FALSE -- otherwise. 17857388Seric */ 17957388Seric 18057388Seric bool 18157388Seric invalidaddr(addr) 18257388Seric register char *addr; 18357388Seric { 18457388Seric for (; *addr != '\0'; addr++) 18557388Seric { 18658050Seric if ((*addr & 0340) != 0200) 18757388Seric continue; 18857388Seric setstat(EX_USAGE); 18958151Seric usrerr("553 Address contained invalid control characters"); 19057388Seric return TRUE; 19157388Seric } 19257388Seric return FALSE; 19357388Seric } 19457388Seric /* 19556678Seric ** ALLOCADDR -- do local allocations of address on demand. 19656678Seric ** 19756678Seric ** Also lowercases the host name if requested. 19856678Seric ** 19956678Seric ** Parameters: 20056678Seric ** a -- the address to reallocate. 20164284Seric ** flags -- the copy flag (see RF_ definitions in sendmail.h 20264284Seric ** for a description). 20356678Seric ** paddr -- the printname of the address. 20456678Seric ** 20556678Seric ** Returns: 20656678Seric ** none. 20756678Seric ** 20856678Seric ** Side Effects: 20956678Seric ** Copies portions of a into local buffers as requested. 21056678Seric */ 21156678Seric 212*64348Seric allocaddr(a, flags, paddr) 21356678Seric register ADDRESS *a; 21464284Seric int flags; 21556678Seric char *paddr; 21656678Seric { 21758673Seric if (tTd(24, 4)) 21864284Seric printf("allocaddr(flags=%o, paddr=%s)\n", flags, paddr); 21958673Seric 220*64348Seric a->q_paddr = paddr; 2218078Seric 22224944Seric if (a->q_user == NULL) 22324944Seric a->q_user = ""; 22424944Seric if (a->q_host == NULL) 22524944Seric a->q_host = ""; 22624944Seric 22764284Seric if (bitset(RF_COPYPARSE, flags)) 228297Seric { 22924944Seric a->q_host = newstr(a->q_host); 2303149Seric if (a->q_user != a->q_paddr) 2313149Seric a->q_user = newstr(a->q_user); 232297Seric } 233297Seric 23456678Seric if (a->q_paddr == NULL) 23556678Seric a->q_paddr = a->q_user; 236297Seric } 237297Seric /* 238297Seric ** PRESCAN -- Prescan name and make it canonical 239297Seric ** 2409374Seric ** Scans a name and turns it into a set of tokens. This process 2419374Seric ** deletes blanks and comments (in parentheses). 242297Seric ** 243297Seric ** This routine knows about quoted strings and angle brackets. 244297Seric ** 245297Seric ** There are certain subtleties to this routine. The one that 246297Seric ** comes to mind now is that backslashes on the ends of names 247297Seric ** are silently stripped off; this is intentional. The problem 248297Seric ** is that some versions of sndmsg (like at LBL) set the kill 249297Seric ** character to something other than @ when reading addresses; 250297Seric ** so people type "csvax.eric\@berkeley" -- which screws up the 251297Seric ** berknet mailer. 252297Seric ** 253297Seric ** Parameters: 254297Seric ** addr -- the name to chomp. 255297Seric ** delim -- the delimiter for the address, normally 256297Seric ** '\0' or ','; \0 is accepted in any case. 25715284Seric ** If '\t' then we are reading the .cf file. 25816914Seric ** pvpbuf -- place to put the saved text -- note that 25916914Seric ** the pointers are static. 26058333Seric ** delimptr -- if non-NULL, set to the location of the 26158333Seric ** terminating delimiter. 262297Seric ** 263297Seric ** Returns: 2643149Seric ** A pointer to a vector of tokens. 265297Seric ** NULL on error. 266297Seric */ 267297Seric 2688078Seric /* states and character types */ 2698078Seric # define OPR 0 /* operator */ 2708078Seric # define ATM 1 /* atom */ 2718078Seric # define QST 2 /* in quoted string */ 2728078Seric # define SPC 3 /* chewing up spaces */ 2738078Seric # define ONE 4 /* pick up one character */ 2743149Seric 2758078Seric # define NSTATES 5 /* number of states */ 2768078Seric # define TYPE 017 /* mask to select state type */ 2778078Seric 2788078Seric /* meta bits for table */ 2798078Seric # define M 020 /* meta character; don't pass through */ 2808078Seric # define B 040 /* cause a break */ 2818078Seric # define MB M|B /* meta-break */ 2828078Seric 2838078Seric static short StateTab[NSTATES][NSTATES] = 2848078Seric { 2858087Seric /* oldst chtype> OPR ATM QST SPC ONE */ 2869051Seric /*OPR*/ OPR|B, ATM|B, QST|B, SPC|MB, ONE|B, 2879051Seric /*ATM*/ OPR|B, ATM, QST|B, SPC|MB, ONE|B, 2889051Seric /*QST*/ QST, QST, OPR, QST, QST, 2898078Seric /*SPC*/ OPR, ATM, QST, SPC|M, ONE, 2908078Seric /*ONE*/ OPR, OPR, OPR, OPR, OPR, 2918078Seric }; 2928078Seric 2938078Seric # define NOCHAR -1 /* signal nothing in lookahead token */ 2948078Seric 2953149Seric char ** 29658333Seric prescan(addr, delim, pvpbuf, delimptr) 297297Seric char *addr; 298297Seric char delim; 29916914Seric char pvpbuf[]; 30058333Seric char **delimptr; 301297Seric { 302297Seric register char *p; 3038078Seric register char *q; 3049346Seric register int c; 3053149Seric char **avp; 306297Seric bool bslashmode; 307297Seric int cmntcnt; 3088423Seric int anglecnt; 3093149Seric char *tok; 3108078Seric int state; 3118078Seric int newstate; 31259747Seric char *saveto = CurEnv->e_to; 3138078Seric static char *av[MAXATOM+1]; 31456678Seric extern int errno; 315297Seric 31615253Seric /* make sure error messages don't have garbage on them */ 31715253Seric errno = 0; 31815253Seric 31916914Seric q = pvpbuf; 3203149Seric bslashmode = FALSE; 3217800Seric cmntcnt = 0; 3228423Seric anglecnt = 0; 3233149Seric avp = av; 32456678Seric state = ATM; 3258078Seric c = NOCHAR; 3268078Seric p = addr; 32759747Seric CurEnv->e_to = p; 32856764Seric if (tTd(22, 11)) 329297Seric { 3308078Seric printf("prescan: "); 3318078Seric xputs(p); 33223109Seric (void) putchar('\n'); 3338078Seric } 3348078Seric 3358078Seric do 3368078Seric { 3373149Seric /* read a token */ 3383149Seric tok = q; 3398078Seric for (;;) 340297Seric { 3418078Seric /* store away any old lookahead character */ 34259277Seric if (c != NOCHAR && !bslashmode) 3438078Seric { 34415284Seric /* see if there is room */ 34516914Seric if (q >= &pvpbuf[PSBUFSIZE - 5]) 3468078Seric { 34758151Seric usrerr("553 Address too long"); 34858333Seric if (delimptr != NULL) 34958333Seric *delimptr = p; 35059747Seric CurEnv->e_to = saveto; 3518078Seric return (NULL); 3528078Seric } 35315284Seric 35415284Seric /* squirrel it away */ 3558078Seric *q++ = c; 3568078Seric } 3578078Seric 3588078Seric /* read a new input character */ 3598078Seric c = *p++; 36057631Seric if (c == '\0') 36156764Seric { 36256764Seric /* diagnose and patch up bad syntax */ 36356764Seric if (state == QST) 36456764Seric { 36563851Seric usrerr("653 Unbalanced '\"' (fixed)"); 36656764Seric c = '"'; 36756764Seric } 36856764Seric else if (cmntcnt > 0) 36956764Seric { 37063851Seric usrerr("653 Unbalanced '(' (fixed)"); 37156764Seric c = ')'; 37256764Seric } 37356764Seric else if (anglecnt > 0) 37456764Seric { 37556764Seric c = '>'; 37663851Seric usrerr("653 Unbalanced '<' (fixed)"); 37756764Seric } 37856764Seric else 37956764Seric break; 38015284Seric 38156764Seric p--; 38256764Seric } 38357631Seric else if (c == delim && anglecnt <= 0 && 38457631Seric cmntcnt <= 0 && state != QST) 38557631Seric break; 38656764Seric 3878078Seric if (tTd(22, 101)) 3888078Seric printf("c=%c, s=%d; ", c, state); 3898078Seric 3903149Seric /* chew up special characters */ 3913149Seric *q = '\0'; 3923149Seric if (bslashmode) 3933149Seric { 39459105Seric bslashmode = FALSE; 39559105Seric 39624944Seric /* kludge \! for naive users */ 39758061Seric if (cmntcnt > 0) 39859105Seric { 39958061Seric c = NOCHAR; 40059105Seric continue; 40159105Seric } 40259105Seric else if (c != '!' || state == QST) 40359105Seric { 40456678Seric *q++ = '\\'; 40559105Seric continue; 40659105Seric } 4073149Seric } 40856678Seric 40956678Seric if (c == '\\') 4103149Seric { 4113149Seric bslashmode = TRUE; 4123149Seric } 41356678Seric else if (state == QST) 4148514Seric { 4158514Seric /* do nothing, just avoid next clauses */ 4168514Seric } 4178078Seric else if (c == '(') 4184100Seric { 4198078Seric cmntcnt++; 4208078Seric c = NOCHAR; 4214100Seric } 4228078Seric else if (c == ')') 4233149Seric { 4248078Seric if (cmntcnt <= 0) 4253149Seric { 42663851Seric usrerr("653 Unbalanced ')' (fixed)"); 42763844Seric c = NOCHAR; 4283149Seric } 4298078Seric else 4308078Seric cmntcnt--; 4318078Seric } 4328078Seric else if (cmntcnt > 0) 4338078Seric c = NOCHAR; 4348423Seric else if (c == '<') 4358423Seric anglecnt++; 4368423Seric else if (c == '>') 4378423Seric { 4388423Seric if (anglecnt <= 0) 4398423Seric { 44063851Seric usrerr("653 Unbalanced '>' (fixed)"); 44163844Seric c = NOCHAR; 4428423Seric } 44363844Seric else 44463844Seric anglecnt--; 4458423Seric } 44658050Seric else if (delim == ' ' && isascii(c) && isspace(c)) 44711423Seric c = ' '; 4483149Seric 4498078Seric if (c == NOCHAR) 4508078Seric continue; 4513149Seric 4528078Seric /* see if this is end of input */ 45311405Seric if (c == delim && anglecnt <= 0 && state != QST) 4543149Seric break; 4553149Seric 4568078Seric newstate = StateTab[state][toktype(c)]; 4578078Seric if (tTd(22, 101)) 4588078Seric printf("ns=%02o\n", newstate); 4598078Seric state = newstate & TYPE; 4608078Seric if (bitset(M, newstate)) 4618078Seric c = NOCHAR; 4628078Seric if (bitset(B, newstate)) 4634228Seric break; 464297Seric } 4653149Seric 4663149Seric /* new token */ 4678078Seric if (tok != q) 4681378Seric { 4698078Seric *q++ = '\0'; 4708078Seric if (tTd(22, 36)) 471297Seric { 4728078Seric printf("tok="); 4738078Seric xputs(tok); 47423109Seric (void) putchar('\n'); 475297Seric } 4768078Seric if (avp >= &av[MAXATOM]) 477297Seric { 47858151Seric syserr("553 prescan: too many tokens"); 47958333Seric if (delimptr != NULL) 48058333Seric *delimptr = p; 48159747Seric CurEnv->e_to = saveto; 4828078Seric return (NULL); 483297Seric } 4848078Seric *avp++ = tok; 485297Seric } 4868423Seric } while (c != '\0' && (c != delim || anglecnt > 0)); 4873149Seric *avp = NULL; 48858333Seric p--; 48958333Seric if (delimptr != NULL) 49058333Seric *delimptr = p; 49156764Seric if (tTd(22, 12)) 49256764Seric { 49356764Seric printf("prescan==>"); 49456764Seric printav(av); 49556764Seric } 49659747Seric CurEnv->e_to = saveto; 49758546Seric if (av[0] == NULL) 49858546Seric return (NULL); 49958403Seric return (av); 5003149Seric } 5013149Seric /* 5023149Seric ** TOKTYPE -- return token type 5033149Seric ** 5043149Seric ** Parameters: 5053149Seric ** c -- the character in question. 5063149Seric ** 5073149Seric ** Returns: 5083149Seric ** Its type. 5093149Seric ** 5103149Seric ** Side Effects: 5113149Seric ** none. 5123149Seric */ 513297Seric 5143149Seric toktype(c) 51558050Seric register int c; 5163149Seric { 5173380Seric static char buf[50]; 5183382Seric static bool firstime = TRUE; 5193380Seric 5203382Seric if (firstime) 5213380Seric { 5223382Seric firstime = FALSE; 52358050Seric expand("\201o", buf, &buf[sizeof buf - 1], CurEnv); 5247005Seric (void) strcat(buf, DELIMCHARS); 5253380Seric } 52658050Seric c &= 0377; 52756327Seric if (c == MATCHCLASS || c == MATCHREPL || c == MATCHNCLASS) 5288078Seric return (ONE); 52959027Seric if (c == MACRODEXPAND) 53059027Seric return (ONE); 5318078Seric if (c == '"') 5328078Seric return (QST); 53358050Seric if ((c & 0340) == 0200) 53458050Seric return (OPR); 5354100Seric if (!isascii(c)) 5368078Seric return (ATM); 5378078Seric if (isspace(c) || c == ')') 5388078Seric return (SPC); 53958050Seric if (strchr(buf, c) != NULL) 5408078Seric return (OPR); 5418078Seric return (ATM); 5423149Seric } 5433149Seric /* 5443149Seric ** REWRITE -- apply rewrite rules to token vector. 5453149Seric ** 5464476Seric ** This routine is an ordered production system. Each rewrite 5474476Seric ** rule has a LHS (called the pattern) and a RHS (called the 5484476Seric ** rewrite); 'rwr' points the the current rewrite rule. 5494476Seric ** 5504476Seric ** For each rewrite rule, 'avp' points the address vector we 5514476Seric ** are trying to match against, and 'pvp' points to the pattern. 5528058Seric ** If pvp points to a special match value (MATCHZANY, MATCHANY, 5539585Seric ** MATCHONE, MATCHCLASS, MATCHNCLASS) then the address in avp 5549585Seric ** matched is saved away in the match vector (pointed to by 'mvp'). 5554476Seric ** 5564476Seric ** When a match between avp & pvp does not match, we try to 5579585Seric ** back out. If we back up over MATCHONE, MATCHCLASS, or MATCHNCLASS 5584476Seric ** we must also back out the match in mvp. If we reach a 5598058Seric ** MATCHANY or MATCHZANY we just extend the match and start 5608058Seric ** over again. 5614476Seric ** 5624476Seric ** When we finally match, we rewrite the address vector 5634476Seric ** and try over again. 5644476Seric ** 5653149Seric ** Parameters: 5663149Seric ** pvp -- pointer to token vector. 56759027Seric ** ruleset -- the ruleset to use for rewriting. 56859027Seric ** e -- the current envelope. 5693149Seric ** 5703149Seric ** Returns: 57159084Seric ** A status code. If EX_TEMPFAIL, higher level code should 57259084Seric ** attempt recovery. 5733149Seric ** 5743149Seric ** Side Effects: 5753149Seric ** pvp is modified. 5763149Seric */ 5772091Seric 5783149Seric struct match 5793149Seric { 5804468Seric char **first; /* first token matched */ 5814468Seric char **last; /* last token matched */ 58258825Seric char **pattern; /* pointer to pattern */ 5833149Seric }; 5843149Seric 5854468Seric # define MAXMATCH 9 /* max params per rewrite */ 5863149Seric 5873149Seric 58859084Seric int 58959027Seric rewrite(pvp, ruleset, e) 5903149Seric char **pvp; 5914070Seric int ruleset; 59259027Seric register ENVELOPE *e; 5933149Seric { 5943149Seric register char *ap; /* address pointer */ 5953149Seric register char *rp; /* rewrite pointer */ 5963149Seric register char **avp; /* address vector pointer */ 5973149Seric register char **rvp; /* rewrite vector pointer */ 5988058Seric register struct match *mlp; /* cur ptr into mlist */ 5998058Seric register struct rewrite *rwr; /* pointer to current rewrite rule */ 60058866Seric int ruleno; /* current rule number */ 60159084Seric int rstat = EX_OK; /* return status */ 60256678Seric struct match mlist[MAXMATCH]; /* stores match on LHS */ 6033149Seric char *npvp[MAXATOM+1]; /* temporary space for rebuild */ 6043149Seric 6059279Seric if (OpMode == MD_TEST || tTd(21, 2)) 6063149Seric { 6078959Seric printf("rewrite: ruleset %2d input:", ruleset); 60856678Seric printav(pvp); 6093149Seric } 61056678Seric if (ruleset < 0 || ruleset >= MAXRWSETS) 61156326Seric { 61258151Seric syserr("554 rewrite: illegal ruleset number %d", ruleset); 61359084Seric return EX_CONFIG; 61456326Seric } 61556678Seric if (pvp == NULL) 61659084Seric return EX_USAGE; 61756326Seric 6183149Seric /* 61956678Seric ** Run through the list of rewrite rules, applying 62056678Seric ** any that match. 6213149Seric */ 6223149Seric 62358866Seric ruleno = 1; 6244070Seric for (rwr = RewriteRules[ruleset]; rwr != NULL; ) 6253149Seric { 62656678Seric int loopcount = 0; 62756678Seric 6287675Seric if (tTd(21, 12)) 629297Seric { 6308069Seric printf("-----trying rule:"); 63156678Seric printav(rwr->r_lhs); 6323149Seric } 6333149Seric 6343149Seric /* try to match on this rule */ 6354468Seric mlp = mlist; 6368058Seric rvp = rwr->r_lhs; 6378058Seric avp = pvp; 63858866Seric if (++loopcount > 100) 6393149Seric { 64058866Seric syserr("554 Infinite loop in ruleset %d, rule %d", 64158866Seric ruleset, ruleno); 64258866Seric if (tTd(21, 1)) 64352637Seric { 64456678Seric printf("workspace: "); 64556678Seric printav(pvp); 64652637Seric } 64758866Seric break; 64858866Seric } 64958866Seric 65058866Seric while ((ap = *avp) != NULL || *rvp != NULL) 65158866Seric { 6523149Seric rp = *rvp; 6538058Seric if (tTd(21, 35)) 6548058Seric { 65558825Seric printf("ADVANCE rp="); 65657531Seric xputs(rp); 65757532Seric printf(", ap="); 6588058Seric xputs(ap); 6598069Seric printf("\n"); 6608058Seric } 66156678Seric if (rp == NULL) 66256326Seric { 6633149Seric /* end-of-pattern before end-of-address */ 6648058Seric goto backup; 66556678Seric } 66658173Seric if (ap == NULL && (*rp & 0377) != MATCHZANY && 66758827Seric (*rp & 0377) != MATCHZERO) 66856326Seric { 66958825Seric /* end-of-input with patterns left */ 67058825Seric goto backup; 671297Seric } 67256326Seric 67358050Seric switch (*rp & 0377) 6748058Seric { 67556678Seric register STAB *s; 67658814Seric char buf[MAXLINE]; 67756326Seric 67856678Seric case MATCHCLASS: 67958825Seric /* match any phrase in a class */ 68058825Seric mlp->pattern = rvp; 68158814Seric mlp->first = avp; 68258814Seric extendclass: 68358825Seric ap = *avp; 68458825Seric if (ap == NULL) 68558814Seric goto backup; 68658814Seric mlp->last = avp++; 68758814Seric cataddr(mlp->first, mlp->last, buf, sizeof buf, '\0'); 68858814Seric s = stab(buf, ST_CLASS, ST_FIND); 68956678Seric if (s == NULL || !bitnset(rp[1], s->s_class)) 69056326Seric { 69158825Seric if (tTd(21, 36)) 69258825Seric { 69358825Seric printf("EXTEND rp="); 69458825Seric xputs(rp); 69558825Seric printf(", ap="); 69658825Seric xputs(ap); 69758825Seric printf("\n"); 69858825Seric } 69958825Seric goto extendclass; 70056326Seric } 70158825Seric if (tTd(21, 36)) 70258825Seric printf("CLMATCH\n"); 70358814Seric mlp++; 70458814Seric break; 7054060Seric 70658825Seric case MATCHNCLASS: 70758825Seric /* match any token not in a class */ 70858825Seric s = stab(ap, ST_CLASS, ST_FIND); 70958825Seric if (s != NULL && bitnset(rp[1], s->s_class)) 71058825Seric goto backup; 71158825Seric 71258825Seric /* fall through */ 71358825Seric 71456678Seric case MATCHONE: 71556678Seric case MATCHANY: 71656678Seric /* match exactly one token */ 71758825Seric mlp->pattern = rvp; 71856678Seric mlp->first = avp; 71956678Seric mlp->last = avp++; 7208058Seric mlp++; 72156678Seric break; 7228058Seric 72356678Seric case MATCHZANY: 72456678Seric /* match zero or more tokens */ 72558825Seric mlp->pattern = rvp; 72656678Seric mlp->first = avp; 72756678Seric mlp->last = avp - 1; 72856678Seric mlp++; 72956678Seric break; 73056326Seric 73158827Seric case MATCHZERO: 73258173Seric /* match zero tokens */ 73358173Seric break; 73458173Seric 73559027Seric case MACRODEXPAND: 73659027Seric /* 73759027Seric ** Match against run-time macro. 73859027Seric ** This algorithm is broken for the 73959027Seric ** general case (no recursive macros, 74059027Seric ** improper tokenization) but should 74159027Seric ** work for the usual cases. 74259027Seric */ 74359027Seric 74459027Seric ap = macvalue(rp[1], e); 74559027Seric mlp->first = avp; 74659027Seric if (tTd(21, 2)) 74759027Seric printf("rewrite: LHS $&%c => \"%s\"\n", 74859027Seric rp[1], 74959027Seric ap == NULL ? "(NULL)" : ap); 75059027Seric 75159027Seric if (ap == NULL) 75259027Seric break; 75360502Seric while (*ap != '\0') 75459027Seric { 75559027Seric if (*avp == NULL || 75659027Seric strncasecmp(ap, *avp, strlen(*avp)) != 0) 75759027Seric { 75859027Seric /* no match */ 75959027Seric avp = mlp->first; 76059027Seric goto backup; 76159027Seric } 76259027Seric ap += strlen(*avp++); 76359027Seric } 76459027Seric 76559027Seric /* match */ 76659027Seric break; 76759027Seric 76856678Seric default: 76956678Seric /* must have exact match */ 77056678Seric if (strcasecmp(rp, ap)) 7718058Seric goto backup; 7724468Seric avp++; 77356678Seric break; 7743149Seric } 7753149Seric 77656678Seric /* successful match on this token */ 7773149Seric rvp++; 7783149Seric continue; 7793149Seric 78058825Seric backup: 78156678Seric /* match failed -- back up */ 78258825Seric while (--mlp >= mlist) 7833149Seric { 78458825Seric rvp = mlp->pattern; 78556678Seric rp = *rvp; 78658825Seric avp = mlp->last + 1; 78758825Seric ap = *avp; 78858825Seric 78958825Seric if (tTd(21, 36)) 79058825Seric { 79158825Seric printf("BACKUP rp="); 79258825Seric xputs(rp); 79358825Seric printf(", ap="); 79458825Seric xputs(ap); 79558825Seric printf("\n"); 79658825Seric } 79758825Seric 79858825Seric if (ap == NULL) 79958825Seric { 80058825Seric /* run off the end -- back up again */ 80158825Seric continue; 80258825Seric } 80358050Seric if ((*rp & 0377) == MATCHANY || 80458050Seric (*rp & 0377) == MATCHZANY) 8054468Seric { 80656678Seric /* extend binding and continue */ 80758825Seric mlp->last = avp++; 80856678Seric rvp++; 80958825Seric mlp++; 81056678Seric break; 8114468Seric } 81258825Seric if ((*rp & 0377) == MATCHCLASS) 81356678Seric { 81458814Seric /* extend binding and try again */ 81563397Seric mlp->last = avp; 81658814Seric goto extendclass; 81758814Seric } 8183149Seric } 8193149Seric 82058825Seric if (mlp < mlist) 82156678Seric { 82256678Seric /* total failure to match */ 82356326Seric break; 8243149Seric } 825297Seric } 8263149Seric 8273149Seric /* 82856678Seric ** See if we successfully matched 8293149Seric */ 8303149Seric 83158827Seric if (mlp < mlist || *rvp != NULL) 8323149Seric { 8339374Seric if (tTd(21, 10)) 8349374Seric printf("----- rule fails\n"); 8359374Seric rwr = rwr->r_next; 83658866Seric ruleno++; 8379374Seric continue; 8389374Seric } 8393149Seric 8409374Seric rvp = rwr->r_rhs; 8419374Seric if (tTd(21, 12)) 8429374Seric { 8439374Seric printf("-----rule matches:"); 84456678Seric printav(rvp); 8459374Seric } 8469374Seric 8479374Seric rp = *rvp; 84858050Seric if ((*rp & 0377) == CANONUSER) 8499374Seric { 8509374Seric rvp++; 8519374Seric rwr = rwr->r_next; 85258866Seric ruleno++; 8539374Seric } 85458050Seric else if ((*rp & 0377) == CANONHOST) 8559374Seric { 8569374Seric rvp++; 8579374Seric rwr = NULL; 8589374Seric } 85958050Seric else if ((*rp & 0377) == CANONNET) 8609374Seric rwr = NULL; 8619374Seric 8629374Seric /* substitute */ 8639374Seric for (avp = npvp; *rvp != NULL; rvp++) 8649374Seric { 8659374Seric register struct match *m; 8669374Seric register char **pp; 8679374Seric 8688058Seric rp = *rvp; 86958050Seric if ((*rp & 0377) == MATCHREPL) 8708058Seric { 87116914Seric /* substitute from LHS */ 87216914Seric m = &mlist[rp[1] - '1']; 87356678Seric if (m < mlist || m >= mlp) 8749374Seric { 87558151Seric syserr("554 rewrite: ruleset %d: replacement $%c out of bounds", 87656326Seric ruleset, rp[1]); 87759084Seric return EX_CONFIG; 8789374Seric } 87916914Seric if (tTd(21, 15)) 88016914Seric { 88116914Seric printf("$%c:", rp[1]); 88216914Seric pp = m->first; 88356678Seric while (pp <= m->last) 88416914Seric { 88516914Seric printf(" %x=\"", *pp); 88616914Seric (void) fflush(stdout); 88716914Seric printf("%s\"", *pp++); 88816914Seric } 88916914Seric printf("\n"); 89016914Seric } 8919374Seric pp = m->first; 89256678Seric while (pp <= m->last) 8933149Seric { 89416914Seric if (avp >= &npvp[MAXATOM]) 89556678Seric { 89658151Seric syserr("554 rewrite: expansion too long"); 89759084Seric return EX_DATAERR; 89856678Seric } 89916914Seric *avp++ = *pp++; 9003149Seric } 9013149Seric } 90216914Seric else 9038226Seric { 90416914Seric /* vanilla replacement */ 9059374Seric if (avp >= &npvp[MAXATOM]) 90616889Seric { 90756678Seric toolong: 90858151Seric syserr("554 rewrite: expansion too long"); 90959084Seric return EX_DATAERR; 91016889Seric } 91159027Seric if ((*rp & 0377) != MACRODEXPAND) 91259027Seric *avp++ = rp; 91359027Seric else 91459027Seric { 91559027Seric *avp = macvalue(rp[1], e); 91659027Seric if (tTd(21, 2)) 91759027Seric printf("rewrite: RHS $&%c => \"%s\"\n", 91859027Seric rp[1], 91959027Seric *avp == NULL ? "(NULL)" : *avp); 92059027Seric if (*avp != NULL) 92159027Seric avp++; 92259027Seric } 9238226Seric } 9249374Seric } 9259374Seric *avp++ = NULL; 92616914Seric 92716914Seric /* 92856678Seric ** Check for any hostname/keyword lookups. 92916914Seric */ 93016914Seric 93116914Seric for (rvp = npvp; *rvp != NULL; rvp++) 93216914Seric { 93356678Seric char **hbrvp; 93416914Seric char **xpvp; 93516914Seric int trsize; 93656678Seric char *replac; 93756678Seric int endtoken; 93856678Seric STAB *map; 93956678Seric char *mapname; 94056678Seric char **key_rvp; 94156678Seric char **arg_rvp; 94256678Seric char **default_rvp; 94356678Seric char buf[MAXNAME + 1]; 94416914Seric char *pvpb1[MAXATOM + 1]; 94556823Seric char *argvect[10]; 94617174Seric char pvpbuf[PSBUFSIZE]; 94716914Seric 94858050Seric if ((**rvp & 0377) != HOSTBEGIN && 94958050Seric (**rvp & 0377) != LOOKUPBEGIN) 95016914Seric continue; 95116914Seric 95216914Seric /* 95356678Seric ** Got a hostname/keyword lookup. 95416914Seric ** 95516914Seric ** This could be optimized fairly easily. 95616914Seric */ 95716914Seric 95816914Seric hbrvp = rvp; 95958050Seric if ((**rvp & 0377) == HOSTBEGIN) 96056327Seric { 96156678Seric endtoken = HOSTEND; 96256678Seric mapname = "host"; 96356327Seric } 96456326Seric else 96556327Seric { 96656678Seric endtoken = LOOKUPEND; 96756678Seric mapname = *++rvp; 96856327Seric } 96956678Seric map = stab(mapname, ST_MAP, ST_FIND); 97056678Seric if (map == NULL) 97158151Seric syserr("554 rewrite: map %s not found", mapname); 97256678Seric 97356678Seric /* extract the match part */ 97456678Seric key_rvp = ++rvp; 97556823Seric default_rvp = NULL; 97656823Seric arg_rvp = argvect; 97756823Seric xpvp = NULL; 97856823Seric replac = pvpbuf; 97958050Seric while (*rvp != NULL && (**rvp & 0377) != endtoken) 98053654Seric { 98158050Seric int nodetype = **rvp & 0377; 98256823Seric 98356823Seric if (nodetype != CANONHOST && nodetype != CANONUSER) 98456678Seric { 98556823Seric rvp++; 98656823Seric continue; 98756823Seric } 98856823Seric 98956823Seric *rvp++ = NULL; 99056823Seric 99156823Seric if (xpvp != NULL) 99256823Seric { 99358814Seric cataddr(xpvp, NULL, replac, 99458082Seric &pvpbuf[sizeof pvpbuf] - replac, 99558082Seric '\0'); 99656823Seric *++arg_rvp = replac; 99756823Seric replac += strlen(replac) + 1; 99856823Seric xpvp = NULL; 99956823Seric } 100056823Seric switch (nodetype) 100156823Seric { 100256678Seric case CANONHOST: 100356823Seric xpvp = rvp; 100456678Seric break; 100556678Seric 100656678Seric case CANONUSER: 100756678Seric default_rvp = rvp; 100856678Seric break; 100956678Seric } 101053654Seric } 101116914Seric if (*rvp != NULL) 101216914Seric *rvp++ = NULL; 101356823Seric if (xpvp != NULL) 101456823Seric { 101558814Seric cataddr(xpvp, NULL, replac, 101658082Seric &pvpbuf[sizeof pvpbuf] - replac, 101758082Seric '\0'); 101856823Seric *++arg_rvp = replac; 101956823Seric } 102056823Seric *++arg_rvp = NULL; 102116914Seric 102216914Seric /* save the remainder of the input string */ 102316914Seric trsize = (int) (avp - rvp + 1) * sizeof *rvp; 102416914Seric bcopy((char *) rvp, (char *) pvpb1, trsize); 102516914Seric 102656678Seric /* look it up */ 102758814Seric cataddr(key_rvp, NULL, buf, sizeof buf, '\0'); 102856823Seric argvect[0] = buf; 102960538Seric if (map != NULL && bitset(MF_OPEN, map->s_map.map_mflags)) 103056836Seric { 103159084Seric auto int stat = EX_OK; 103256836Seric 103360215Seric /* XXX should try to auto-open the map here */ 103460215Seric 103558796Seric if (tTd(60, 1)) 103658796Seric printf("map_lookup(%s, %s) => ", 103758796Seric mapname, buf); 103856836Seric replac = (*map->s_map.map_class->map_lookup)(&map->s_map, 103960089Seric buf, argvect, &stat); 104058796Seric if (tTd(60, 1)) 104159084Seric printf("%s (%d)\n", 104259084Seric replac ? replac : "NOT FOUND", 104359084Seric stat); 104459084Seric 104559084Seric /* should recover if stat == EX_TEMPFAIL */ 104659084Seric if (stat == EX_TEMPFAIL) 104759084Seric rstat = stat; 104856836Seric } 104953654Seric else 105056678Seric replac = NULL; 105156678Seric 105256678Seric /* if no replacement, use default */ 105356823Seric if (replac == NULL && default_rvp != NULL) 105456823Seric { 105560089Seric /* create the default */ 105660089Seric cataddr(default_rvp, NULL, buf, sizeof buf, '\0'); 105756823Seric replac = buf; 105856823Seric } 105956823Seric 106056678Seric if (replac == NULL) 106151317Seric { 106256823Seric xpvp = key_rvp; 106353654Seric } 106456678Seric else 106553654Seric { 106656678Seric /* scan the new replacement */ 106758333Seric xpvp = prescan(replac, '\0', pvpbuf, NULL); 106853654Seric if (xpvp == NULL) 106951317Seric { 107058403Seric /* prescan already printed error */ 107159084Seric return EX_DATAERR; 107256678Seric } 107351317Seric } 107451317Seric 107516914Seric /* append it to the token list */ 107656678Seric for (avp = hbrvp; *xpvp != NULL; xpvp++) 107756678Seric { 107817174Seric *avp++ = newstr(*xpvp); 107916920Seric if (avp >= &npvp[MAXATOM]) 108016914Seric goto toolong; 108117174Seric } 108216914Seric 108316914Seric /* restore the old trailing information */ 108456678Seric for (xpvp = pvpb1; (*avp++ = *xpvp++) != NULL; ) 108516920Seric if (avp >= &npvp[MAXATOM]) 108616914Seric goto toolong; 108717174Seric 108856678Seric break; 108916914Seric } 109016914Seric 109116914Seric /* 109216914Seric ** Check for subroutine calls. 109316914Seric */ 109416914Seric 109558050Seric if (*npvp != NULL && (**npvp & 0377) == CALLSUBR) 109656678Seric { 109759084Seric int stat; 109859084Seric 109956678Seric bcopy((char *) &npvp[2], (char *) pvp, 110056678Seric (int) (avp - npvp - 2) * sizeof *avp); 110156678Seric if (tTd(21, 3)) 110256678Seric printf("-----callsubr %s\n", npvp[1]); 110359084Seric stat = rewrite(pvp, atoi(npvp[1]), e); 110459084Seric if (rstat == EX_OK || stat == EX_TEMPFAIL) 110559084Seric rstat = stat; 110663581Seric if ((**pvp & 0377) == CANONNET) 110763581Seric rwr = NULL; 110856678Seric } 110956678Seric else 111056678Seric { 111117348Seric bcopy((char *) npvp, (char *) pvp, 111216900Seric (int) (avp - npvp) * sizeof *avp); 111356678Seric } 11149374Seric if (tTd(21, 4)) 11159374Seric { 11169374Seric printf("rewritten as:"); 111756678Seric printav(pvp); 11189374Seric } 1119297Seric } 11208069Seric 11219279Seric if (OpMode == MD_TEST || tTd(21, 2)) 11228069Seric { 11238959Seric printf("rewrite: ruleset %2d returns:", ruleset); 112456678Seric printav(pvp); 11258069Seric } 112659084Seric 112759084Seric return rstat; 11283149Seric } 11293149Seric /* 11303149Seric ** BUILDADDR -- build address from token vector. 11313149Seric ** 11323149Seric ** Parameters: 11333149Seric ** tv -- token vector. 11343149Seric ** a -- pointer to address descriptor to fill. 11353149Seric ** If NULL, one will be allocated. 113664284Seric ** flags -- info regarding whether this is a sender or 113764284Seric ** a recipient. 113858966Seric ** e -- the current envelope. 11393149Seric ** 11403149Seric ** Returns: 11414279Seric ** NULL if there was an error. 11424279Seric ** 'a' otherwise. 11433149Seric ** 11443149Seric ** Side Effects: 11453149Seric ** fills in 'a' 11463149Seric */ 11473149Seric 114857249Seric struct errcodes 114957249Seric { 115057249Seric char *ec_name; /* name of error code */ 115157249Seric int ec_code; /* numeric code */ 115257249Seric } ErrorCodes[] = 115357249Seric { 115457249Seric "usage", EX_USAGE, 115557249Seric "nouser", EX_NOUSER, 115657249Seric "nohost", EX_NOHOST, 115757249Seric "unavailable", EX_UNAVAILABLE, 115857249Seric "software", EX_SOFTWARE, 115957249Seric "tempfail", EX_TEMPFAIL, 116057249Seric "protocol", EX_PROTOCOL, 116157249Seric #ifdef EX_CONFIG 116257249Seric "config", EX_CONFIG, 116357249Seric #endif 116457249Seric NULL, EX_UNAVAILABLE, 116557249Seric }; 116657249Seric 116756678Seric ADDRESS * 116864284Seric buildaddr(tv, a, flags, e) 11693149Seric register char **tv; 11703149Seric register ADDRESS *a; 117164284Seric int flags; 117258966Seric register ENVELOPE *e; 11733149Seric { 11743149Seric struct mailer **mp; 11753149Seric register struct mailer *m; 117658008Seric char *bp; 117758008Seric int spaceleft; 117864306Seric static MAILER errormailer; 117964306Seric static char *errorargv[] = { "ERROR", NULL }; 118057402Seric static char buf[MAXNAME]; 11813149Seric 11823149Seric if (a == NULL) 11833149Seric a = (ADDRESS *) xalloc(sizeof *a); 118416889Seric bzero((char *) a, sizeof *a); 11853149Seric 11863149Seric /* figure out what net/mailer to use */ 118764306Seric if (*tv == NULL || (**tv & 0377) != CANONNET) 11884279Seric { 118958151Seric syserr("554 buildaddr: no net"); 119064306Seric badaddr: 119164306Seric a->q_flags |= QBADADDR; 119264306Seric a->q_mailer = &errormailer; 119364306Seric if (errormailer.m_name == NULL) 119464306Seric { 119564306Seric /* initialize the bogus mailer */ 119664306Seric errormailer.m_name = "*error*"; 119764306Seric errormailer.m_mailer = "ERROR"; 119864306Seric errormailer.m_argv = errorargv; 119964306Seric } 120064306Seric return a; 12014279Seric } 12023149Seric tv++; 120358680Seric if (strcasecmp(*tv, "error") == 0) 12044279Seric { 120558050Seric if ((**++tv & 0377) == CANONHOST) 120610183Seric { 120757249Seric register struct errcodes *ep; 120857249Seric 120958050Seric if (isascii(**++tv) && isdigit(**tv)) 121057249Seric { 121157249Seric setstat(atoi(*tv)); 121257249Seric } 121357249Seric else 121457249Seric { 121557249Seric for (ep = ErrorCodes; ep->ec_name != NULL; ep++) 121657249Seric if (strcasecmp(ep->ec_name, *tv) == 0) 121757249Seric break; 121857249Seric setstat(ep->ec_code); 121957249Seric } 122010183Seric tv++; 122110183Seric } 122258050Seric if ((**tv & 0377) != CANONUSER) 122358151Seric syserr("554 buildaddr: error: no user"); 122458814Seric cataddr(++tv, NULL, buf, sizeof buf, ' '); 122558082Seric stripquotes(buf); 12264279Seric usrerr(buf); 122764306Seric goto badaddr; 12284279Seric } 122957402Seric 12304598Seric for (mp = Mailer; (m = *mp++) != NULL; ) 12313149Seric { 123258680Seric if (strcasecmp(m->m_name, *tv) == 0) 12333149Seric break; 12343149Seric } 12353149Seric if (m == NULL) 12364279Seric { 123758151Seric syserr("554 buildaddr: unknown mailer %s", *tv); 123864306Seric goto badaddr; 12394279Seric } 12404598Seric a->q_mailer = m; 12413149Seric 12423149Seric /* figure out what host (if any) */ 124356678Seric tv++; 124458509Seric if ((**tv & 0377) == CANONHOST) 12453149Seric { 124658008Seric bp = buf; 124758008Seric spaceleft = sizeof buf - 1; 124858050Seric while (*++tv != NULL && (**tv & 0377) != CANONUSER) 124958008Seric { 125058008Seric int i = strlen(*tv); 125158008Seric 125258008Seric if (i > spaceleft) 125358008Seric { 125458008Seric /* out of space for this address */ 125558008Seric if (spaceleft >= 0) 125658151Seric syserr("554 buildaddr: host too long (%.40s...)", 125758008Seric buf); 125858008Seric i = spaceleft; 125958008Seric spaceleft = 0; 126058008Seric } 126158008Seric if (i <= 0) 126258008Seric continue; 126358008Seric bcopy(*tv, bp, i); 126458008Seric bp += i; 126558008Seric spaceleft -= i; 126658008Seric } 126758010Seric *bp = '\0'; 12685704Seric a->q_host = newstr(buf); 12693149Seric } 127057249Seric else 127158509Seric { 127258509Seric if (!bitnset(M_LOCALMAILER, m->m_flags)) 127358509Seric { 127458509Seric syserr("554 buildaddr: no host"); 127564306Seric goto badaddr; 127658509Seric } 127757249Seric a->q_host = NULL; 127858509Seric } 12793149Seric 12803149Seric /* figure out the user */ 128158050Seric if (*tv == NULL || (**tv & 0377) != CANONUSER) 12824279Seric { 128358151Seric syserr("554 buildaddr: no user"); 128464306Seric goto badaddr; 12854279Seric } 128657402Seric tv++; 128751317Seric 128857402Seric /* do special mapping for local mailer */ 128957402Seric if (m == LocalMailer && *tv != NULL) 129057402Seric { 129157454Seric register char *p = *tv; 129257454Seric 129357454Seric if (*p == '"') 129457454Seric p++; 129557454Seric if (*p == '|') 129657402Seric a->q_mailer = m = ProgMailer; 129757454Seric else if (*p == '/') 129857402Seric a->q_mailer = m = FileMailer; 129957454Seric else if (*p == ':') 130057402Seric { 130157402Seric /* may be :include: */ 130258814Seric cataddr(tv, NULL, buf, sizeof buf, '\0'); 130358008Seric stripquotes(buf); 130458008Seric if (strncasecmp(buf, ":include:", 9) == 0) 130558008Seric { 130658008Seric /* if :include:, don't need further rewriting */ 130757402Seric a->q_mailer = m = InclMailer; 130858008Seric a->q_user = &buf[9]; 130958008Seric return (a); 131058008Seric } 131157402Seric } 131257402Seric } 131357402Seric 131458008Seric if (m == LocalMailer && *tv != NULL && strcmp(*tv, "@") == 0) 131558008Seric { 131658008Seric tv++; 131758008Seric a->q_flags |= QNOTREMOTE; 131858008Seric } 131958008Seric 132064284Seric /* rewrite according recipient mailer rewriting rules */ 132164284Seric define('h', a->q_host, e); 132264284Seric if (!bitset(RF_SENDERADDR|RF_HEADERADDR, flags)) 132364284Seric { 132464284Seric /* sender addresses done later */ 132564284Seric (void) rewrite(tv, 2, e); 132664284Seric if (m->m_re_rwset > 0) 132764284Seric (void) rewrite(tv, m->m_re_rwset, e); 132864284Seric } 132959084Seric (void) rewrite(tv, 4, e); 133019040Seric 133119040Seric /* save the result for the command line/RCPT argument */ 133258814Seric cataddr(tv, NULL, buf, sizeof buf, '\0'); 13333149Seric a->q_user = buf; 13343149Seric 133558670Seric /* 133658670Seric ** Do mapping to lower case as requested by mailer 133758670Seric */ 133858670Seric 133958670Seric if (a->q_host != NULL && !bitnset(M_HST_UPPER, m->m_flags)) 134058670Seric makelower(a->q_host); 134158670Seric if (!bitnset(M_USR_UPPER, m->m_flags)) 134258670Seric makelower(a->q_user); 134358670Seric 13443149Seric return (a); 13453149Seric } 13463188Seric /* 13474228Seric ** CATADDR -- concatenate pieces of addresses (putting in <LWSP> subs) 13484228Seric ** 13494228Seric ** Parameters: 13504228Seric ** pvp -- parameter vector to rebuild. 135158814Seric ** evp -- last parameter to include. Can be NULL to 135258814Seric ** use entire pvp. 13534228Seric ** buf -- buffer to build the string into. 13544228Seric ** sz -- size of buf. 135558082Seric ** spacesub -- the space separator character; if null, 135658082Seric ** use SpaceSub. 13574228Seric ** 13584228Seric ** Returns: 13594228Seric ** none. 13604228Seric ** 13614228Seric ** Side Effects: 13624228Seric ** Destroys buf. 13634228Seric */ 13644228Seric 136558814Seric cataddr(pvp, evp, buf, sz, spacesub) 13664228Seric char **pvp; 136758814Seric char **evp; 13684228Seric char *buf; 13694228Seric register int sz; 137058082Seric char spacesub; 13714228Seric { 13724228Seric bool oatomtok = FALSE; 137356678Seric bool natomtok = FALSE; 13744228Seric register int i; 13754228Seric register char *p; 13764228Seric 137758082Seric if (spacesub == '\0') 137858082Seric spacesub = SpaceSub; 137958082Seric 13808423Seric if (pvp == NULL) 13818423Seric { 138223109Seric (void) strcpy(buf, ""); 13838423Seric return; 13848423Seric } 13854228Seric p = buf; 138611156Seric sz -= 2; 13874228Seric while (*pvp != NULL && (i = strlen(*pvp)) < sz) 13884228Seric { 13898078Seric natomtok = (toktype(**pvp) == ATM); 13904228Seric if (oatomtok && natomtok) 139158082Seric *p++ = spacesub; 13924228Seric (void) strcpy(p, *pvp); 13934228Seric oatomtok = natomtok; 13944228Seric p += i; 139511156Seric sz -= i + 1; 139658814Seric if (pvp++ == evp) 139758814Seric break; 13984228Seric } 13994228Seric *p = '\0'; 14004228Seric } 14014228Seric /* 14023188Seric ** SAMEADDR -- Determine if two addresses are the same 14033188Seric ** 14043188Seric ** This is not just a straight comparison -- if the mailer doesn't 14053188Seric ** care about the host we just ignore it, etc. 14063188Seric ** 14073188Seric ** Parameters: 14083188Seric ** a, b -- pointers to the internal forms to compare. 14093188Seric ** 14103188Seric ** Returns: 14113188Seric ** TRUE -- they represent the same mailbox. 14123188Seric ** FALSE -- they don't. 14133188Seric ** 14143188Seric ** Side Effects: 14153188Seric ** none. 14163188Seric */ 14173188Seric 14183188Seric bool 14199374Seric sameaddr(a, b) 14203188Seric register ADDRESS *a; 14213188Seric register ADDRESS *b; 14223188Seric { 14233188Seric /* if they don't have the same mailer, forget it */ 14243188Seric if (a->q_mailer != b->q_mailer) 14253188Seric return (FALSE); 14263188Seric 14273188Seric /* if the user isn't the same, we can drop out */ 142856678Seric if (strcmp(a->q_user, b->q_user) != 0) 14293188Seric return (FALSE); 14303188Seric 143158438Seric /* if we have good uids for both but the differ, these are different */ 143258438Seric if (bitset(QGOODUID, a->q_flags & b->q_flags) && a->q_uid != b->q_uid) 143358438Seric return (FALSE); 143458438Seric 143558509Seric /* otherwise compare hosts (but be careful for NULL ptrs) */ 143658509Seric if (a->q_host == b->q_host) 143758509Seric { 143858509Seric /* probably both null pointers */ 14393188Seric return (TRUE); 144058509Seric } 14413188Seric if (a->q_host == NULL || b->q_host == NULL) 144258509Seric { 144358509Seric /* only one is a null pointer */ 14443188Seric return (FALSE); 144558509Seric } 144656678Seric if (strcmp(a->q_host, b->q_host) != 0) 14473188Seric return (FALSE); 14483188Seric 14493188Seric return (TRUE); 14503188Seric } 14513234Seric /* 14523234Seric ** PRINTADDR -- print address (for debugging) 14533234Seric ** 14543234Seric ** Parameters: 14553234Seric ** a -- the address to print 14563234Seric ** follow -- follow the q_next chain. 14573234Seric ** 14583234Seric ** Returns: 14593234Seric ** none. 14603234Seric ** 14613234Seric ** Side Effects: 14623234Seric ** none. 14633234Seric */ 14643234Seric 14653234Seric printaddr(a, follow) 14663234Seric register ADDRESS *a; 14673234Seric bool follow; 14683234Seric { 14695001Seric bool first = TRUE; 147057731Seric register MAILER *m; 147157731Seric MAILER pseudomailer; 14725001Seric 14733234Seric while (a != NULL) 14743234Seric { 14755001Seric first = FALSE; 14764443Seric printf("%x=", a); 14774085Seric (void) fflush(stdout); 147857731Seric 147957731Seric /* find the mailer -- carefully */ 148057731Seric m = a->q_mailer; 148157731Seric if (m == NULL) 148257731Seric { 148357731Seric m = &pseudomailer; 148457731Seric m->m_mno = -1; 148557731Seric m->m_name = "NULL"; 148657731Seric } 148757731Seric 148858680Seric printf("%s:\n\tmailer %d (%s), host `%s', user `%s', ruser `%s'\n", 148957731Seric a->q_paddr, m->m_mno, m->m_name, 149063756Seric a->q_host, a->q_user, 149163756Seric a->q_ruser ? a->q_ruser : "<null>"); 149259269Seric printf("\tnext=%x, flags=%o, alias %x, uid %d, gid %d\n", 149359269Seric a->q_next, a->q_flags, a->q_alias, a->q_uid, a->q_gid); 149459269Seric printf("\towner=%s, home=\"%s\", fullname=\"%s\"\n", 149559269Seric a->q_owner == NULL ? "(none)" : a->q_owner, 149663756Seric a->q_home == NULL ? "(none)" : a->q_home, 149763756Seric a->q_fullname == NULL ? "(none)" : a->q_fullname); 14984996Seric 14993234Seric if (!follow) 15003234Seric return; 15014996Seric a = a->q_next; 15023234Seric } 15035001Seric if (first) 15044443Seric printf("[NULL]\n"); 15053234Seric } 15064317Seric 15077682Seric /* 15087682Seric ** REMOTENAME -- return the name relative to the current mailer 15097682Seric ** 15107682Seric ** Parameters: 15117682Seric ** name -- the name to translate. 15128069Seric ** m -- the mailer that we want to do rewriting relative 15138069Seric ** to. 151459163Seric ** flags -- fine tune operations. 151559163Seric ** pstat -- pointer to status word. 151658020Seric ** e -- the current envelope. 15177682Seric ** 15187682Seric ** Returns: 15197682Seric ** the text string representing this address relative to 15207682Seric ** the receiving mailer. 15217682Seric ** 15227682Seric ** Side Effects: 15237682Seric ** none. 15247682Seric ** 15257682Seric ** Warnings: 15267682Seric ** The text string returned is tucked away locally; 15277682Seric ** copy it if you intend to save it. 15287682Seric */ 15297682Seric 15307682Seric char * 153159163Seric remotename(name, m, flags, pstat, e) 15327682Seric char *name; 153356678Seric struct mailer *m; 153459163Seric int flags; 153559163Seric int *pstat; 153656678Seric register ENVELOPE *e; 15377682Seric { 15388069Seric register char **pvp; 15398069Seric char *fancy; 154056678Seric char *oldg = macvalue('g', e); 154158020Seric int rwset; 15427682Seric static char buf[MAXNAME]; 15437682Seric char lbuf[MAXNAME]; 154416914Seric char pvpbuf[PSBUFSIZE]; 154556678Seric extern char *crackaddr(); 15467682Seric 15477755Seric if (tTd(12, 1)) 15487755Seric printf("remotename(%s)\n", name); 15497755Seric 155010177Seric /* don't do anything if we are tagging it as special */ 155159163Seric if (bitset(RF_SENDERADDR, flags)) 155259163Seric rwset = bitset(RF_HEADERADDR, flags) ? m->m_sh_rwset 155359163Seric : m->m_se_rwset; 155458020Seric else 155559163Seric rwset = bitset(RF_HEADERADDR, flags) ? m->m_rh_rwset 155659163Seric : m->m_re_rwset; 155758020Seric if (rwset < 0) 155810177Seric return (name); 155910177Seric 15607682Seric /* 15618181Seric ** Do a heuristic crack of this name to extract any comment info. 15628181Seric ** This will leave the name as a comment and a $g macro. 15637889Seric */ 15647889Seric 156559163Seric if (bitset(RF_CANONICAL, flags) || bitnset(M_NOCOMMENT, m->m_flags)) 156658050Seric fancy = "\201g"; 156710310Seric else 156810310Seric fancy = crackaddr(name); 15697889Seric 15708181Seric /* 15718181Seric ** Turn the name into canonical form. 15728181Seric ** Normally this will be RFC 822 style, i.e., "user@domain". 15738181Seric ** If this only resolves to "user", and the "C" flag is 15748181Seric ** specified in the sending mailer, then the sender's 15758181Seric ** domain will be appended. 15768181Seric */ 15778181Seric 157858333Seric pvp = prescan(name, '\0', pvpbuf, NULL); 15797889Seric if (pvp == NULL) 15807889Seric return (name); 158159163Seric if (rewrite(pvp, 3, e) == EX_TEMPFAIL) 158259163Seric *pstat = EX_TEMPFAIL; 158359163Seric if (bitset(RF_ADDDOMAIN, flags) && e->e_fromdomain != NULL) 15848181Seric { 15858181Seric /* append from domain to this address */ 15868181Seric register char **pxp = pvp; 15878181Seric 15889594Seric /* see if there is an "@domain" in the current name */ 15898181Seric while (*pxp != NULL && strcmp(*pxp, "@") != 0) 15908181Seric pxp++; 15918181Seric if (*pxp == NULL) 15928181Seric { 15939594Seric /* no.... append the "@domain" from the sender */ 159456678Seric register char **qxq = e->e_fromdomain; 15958181Seric 15969594Seric while ((*pxp++ = *qxq++) != NULL) 15979594Seric continue; 159859163Seric if (rewrite(pvp, 3, e) == EX_TEMPFAIL) 159959163Seric *pstat = EX_TEMPFAIL; 16008181Seric } 16018181Seric } 16028181Seric 16038181Seric /* 16048959Seric ** Do more specific rewriting. 160556678Seric ** Rewrite using ruleset 1 or 2 depending on whether this is 160656678Seric ** a sender address or not. 16078181Seric ** Then run it through any receiving-mailer-specific rulesets. 16088181Seric */ 16098181Seric 161059163Seric if (bitset(RF_SENDERADDR, flags)) 161159541Seric { 161259163Seric if (rewrite(pvp, 1, e) == EX_TEMPFAIL) 161359163Seric *pstat = EX_TEMPFAIL; 161459541Seric } 16158069Seric else 161659541Seric { 161759163Seric if (rewrite(pvp, 2, e) == EX_TEMPFAIL) 161859163Seric *pstat = EX_TEMPFAIL; 161959541Seric } 162058020Seric if (rwset > 0) 162159541Seric { 162259163Seric if (rewrite(pvp, rwset, e) == EX_TEMPFAIL) 162359163Seric *pstat = EX_TEMPFAIL; 162459541Seric } 16257682Seric 16268181Seric /* 16278959Seric ** Do any final sanitation the address may require. 16288959Seric ** This will normally be used to turn internal forms 16298959Seric ** (e.g., user@host.LOCAL) into external form. This 16308959Seric ** may be used as a default to the above rules. 16318959Seric */ 16328959Seric 163359163Seric if (rewrite(pvp, 4, e) == EX_TEMPFAIL) 163459163Seric *pstat = EX_TEMPFAIL; 16358959Seric 16368959Seric /* 16378181Seric ** Now restore the comment information we had at the beginning. 16388181Seric */ 16398181Seric 164058825Seric cataddr(pvp, NULL, lbuf, sizeof lbuf, '\0'); 164156678Seric define('g', lbuf, e); 164256678Seric expand(fancy, buf, &buf[sizeof buf - 1], e); 164356678Seric define('g', oldg, e); 16447682Seric 16457682Seric if (tTd(12, 1)) 16467755Seric printf("remotename => `%s'\n", buf); 16477682Seric return (buf); 16487682Seric } 164951317Seric /* 165056678Seric ** MAPLOCALUSER -- run local username through ruleset 5 for final redirection 165151317Seric ** 165251317Seric ** Parameters: 165356678Seric ** a -- the address to map (but just the user name part). 165456678Seric ** sendq -- the sendq in which to install any replacement 165556678Seric ** addresses. 165651317Seric ** 165751317Seric ** Returns: 165851317Seric ** none. 165951317Seric */ 166051317Seric 166156678Seric maplocaluser(a, sendq, e) 166256678Seric register ADDRESS *a; 166356678Seric ADDRESS **sendq; 166456678Seric ENVELOPE *e; 166551317Seric { 166656678Seric register char **pvp; 166756678Seric register ADDRESS *a1 = NULL; 166858333Seric auto char *delimptr; 166956678Seric char pvpbuf[PSBUFSIZE]; 167051317Seric 167156678Seric if (tTd(29, 1)) 167256678Seric { 167356678Seric printf("maplocaluser: "); 167456678Seric printaddr(a, FALSE); 167556678Seric } 167658333Seric pvp = prescan(a->q_user, '\0', pvpbuf, &delimptr); 167756678Seric if (pvp == NULL) 167856678Seric return; 167951317Seric 168059084Seric (void) rewrite(pvp, 5, e); 168158050Seric if (pvp[0] == NULL || (pvp[0][0] & 0377) != CANONNET) 168256678Seric return; 168351317Seric 168456678Seric /* if non-null, mailer destination specified -- has it changed? */ 168564284Seric a1 = buildaddr(pvp, NULL, 0, e); 168656678Seric if (a1 == NULL || sameaddr(a, a1)) 168756678Seric return; 168851317Seric 168956678Seric /* mark old address as dead; insert new address */ 169056678Seric a->q_flags |= QDONTSEND; 169157731Seric if (tTd(29, 5)) 169257731Seric { 169357731Seric printf("maplocaluser: QDONTSEND "); 169457731Seric printaddr(a, FALSE); 169557731Seric } 169656678Seric a1->q_alias = a; 1697*64348Seric allocaddr(a1, RF_COPYALL, NULL); 169856678Seric (void) recipient(a1, sendq, e); 169951317Seric } 170058802Seric /* 170158802Seric ** DEQUOTE_INIT -- initialize dequote map 170258802Seric ** 170358802Seric ** This is a no-op. 170458802Seric ** 170558802Seric ** Parameters: 170658802Seric ** map -- the internal map structure. 170758802Seric ** args -- arguments. 170858802Seric ** 170958802Seric ** Returns: 171058802Seric ** TRUE. 171158802Seric */ 171258802Seric 171358802Seric bool 171460219Seric dequote_init(map, args) 171558802Seric MAP *map; 171658802Seric char *args; 171758802Seric { 171858805Seric register char *p = args; 171958805Seric 172058805Seric for (;;) 172158805Seric { 172258805Seric while (isascii(*p) && isspace(*p)) 172358805Seric p++; 172458805Seric if (*p != '-') 172558805Seric break; 172658805Seric switch (*++p) 172758805Seric { 172858805Seric case 'a': 172958805Seric map->map_app = ++p; 173058805Seric break; 173158805Seric } 173258805Seric while (*p != '\0' && !(isascii(*p) && isspace(*p))) 173358805Seric p++; 173458805Seric if (*p != '\0') 173558805Seric *p = '\0'; 173658805Seric } 173758805Seric if (map->map_app != NULL) 173858805Seric map->map_app = newstr(map->map_app); 173958805Seric 174058802Seric return TRUE; 174158802Seric } 174258802Seric /* 174358802Seric ** DEQUOTE_MAP -- unquote an address 174458802Seric ** 174558802Seric ** Parameters: 174658802Seric ** map -- the internal map structure (ignored). 174760089Seric ** name -- the name to dequote. 174858802Seric ** av -- arguments (ignored). 174959084Seric ** statp -- pointer to status out-parameter. 175058802Seric ** 175158802Seric ** Returns: 175258802Seric ** NULL -- if there were no quotes, or if the resulting 175358802Seric ** unquoted buffer would not be acceptable to prescan. 175458802Seric ** else -- The dequoted buffer. 175558802Seric */ 175658802Seric 175758802Seric char * 175860089Seric dequote_map(map, name, av, statp) 175958802Seric MAP *map; 176060089Seric char *name; 176158802Seric char **av; 176259084Seric int *statp; 176358802Seric { 176458802Seric register char *p; 176558802Seric register char *q; 176658802Seric register char c; 176758802Seric int anglecnt; 176858802Seric int cmntcnt; 176958802Seric int quotecnt; 177059089Seric int spacecnt; 177158802Seric bool quotemode; 177258802Seric bool bslashmode; 177358802Seric 177458802Seric anglecnt = 0; 177558802Seric cmntcnt = 0; 177658802Seric quotecnt = 0; 177759089Seric spacecnt = 0; 177858802Seric quotemode = FALSE; 177958802Seric bslashmode = FALSE; 178058802Seric 178160089Seric for (p = q = name; (c = *p++) != '\0'; ) 178258802Seric { 178358802Seric if (bslashmode) 178458802Seric { 178558802Seric bslashmode = FALSE; 178658802Seric *q++ = c; 178758802Seric continue; 178858802Seric } 178958802Seric 179058802Seric switch (c) 179158802Seric { 179258802Seric case '\\': 179358802Seric bslashmode = TRUE; 179458802Seric break; 179558802Seric 179658802Seric case '(': 179758802Seric cmntcnt++; 179858802Seric break; 179958802Seric 180058802Seric case ')': 180158802Seric if (cmntcnt-- <= 0) 180258802Seric return NULL; 180358802Seric break; 180459089Seric 180559089Seric case ' ': 180659089Seric spacecnt++; 180759089Seric break; 180858802Seric } 180958802Seric 181058802Seric if (cmntcnt > 0) 181158802Seric { 181258802Seric *q++ = c; 181358802Seric continue; 181458802Seric } 181558802Seric 181658802Seric switch (c) 181758802Seric { 181858802Seric case '"': 181958802Seric quotemode = !quotemode; 182058802Seric quotecnt++; 182158802Seric continue; 182258802Seric 182358802Seric case '<': 182458802Seric anglecnt++; 182558802Seric break; 182658802Seric 182758802Seric case '>': 182858802Seric if (anglecnt-- <= 0) 182958802Seric return NULL; 183058802Seric break; 183158802Seric } 183258802Seric *q++ = c; 183358802Seric } 183458802Seric 183558802Seric if (anglecnt != 0 || cmntcnt != 0 || bslashmode || 183659089Seric quotemode || quotecnt <= 0 || spacecnt != 0) 183758802Seric return NULL; 183858802Seric *q++ = '\0'; 183960089Seric return name; 184058802Seric } 1841