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*64404Seric static char sccsid[] = "@(#)parseaddr.c 8.11 (Berkeley) 09/04/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 /* 9664348Seric ** Save addr if we are going to have to. 9764348Seric ** 9864348Seric ** We have to do this early because there is a chance that 9964348Seric ** the map lookups in the rewriting rules could clobber 10064348Seric ** static memory somewhere. 10164348Seric */ 10264348Seric 10364348Seric if (bitset(RF_COPYPADDR, flags) && addr != NULL) 10464348Seric { 10564348Seric char savec = **delimptr; 10664348Seric 10764348Seric if (savec != '\0') 10864348Seric **delimptr = '\0'; 10964348Seric addr = newstr(addr); 11064348Seric if (savec != '\0') 11164348Seric **delimptr = savec; 11264348Seric } 11364348Seric 11464348Seric /* 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 13764348Seric 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 21264348Seric 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 22064348Seric 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]; 947*64404Seric char *nullpvp[1]; 94816914Seric 94958050Seric if ((**rvp & 0377) != HOSTBEGIN && 95058050Seric (**rvp & 0377) != LOOKUPBEGIN) 95116914Seric continue; 95216914Seric 95316914Seric /* 95456678Seric ** Got a hostname/keyword lookup. 95516914Seric ** 95616914Seric ** This could be optimized fairly easily. 95716914Seric */ 95816914Seric 95916914Seric hbrvp = rvp; 96058050Seric if ((**rvp & 0377) == HOSTBEGIN) 96156327Seric { 96256678Seric endtoken = HOSTEND; 96356678Seric mapname = "host"; 96456327Seric } 96556326Seric else 96656327Seric { 96756678Seric endtoken = LOOKUPEND; 96856678Seric mapname = *++rvp; 96956327Seric } 97056678Seric map = stab(mapname, ST_MAP, ST_FIND); 97156678Seric if (map == NULL) 97258151Seric syserr("554 rewrite: map %s not found", mapname); 97356678Seric 97456678Seric /* extract the match part */ 97556678Seric key_rvp = ++rvp; 97656823Seric default_rvp = NULL; 97756823Seric arg_rvp = argvect; 97856823Seric xpvp = NULL; 97956823Seric replac = pvpbuf; 98058050Seric while (*rvp != NULL && (**rvp & 0377) != endtoken) 98153654Seric { 98258050Seric int nodetype = **rvp & 0377; 98356823Seric 98456823Seric if (nodetype != CANONHOST && nodetype != CANONUSER) 98556678Seric { 98656823Seric rvp++; 98756823Seric continue; 98856823Seric } 98956823Seric 99056823Seric *rvp++ = NULL; 99156823Seric 99256823Seric if (xpvp != NULL) 99356823Seric { 99458814Seric cataddr(xpvp, NULL, replac, 99558082Seric &pvpbuf[sizeof pvpbuf] - replac, 99658082Seric '\0'); 99756823Seric *++arg_rvp = replac; 99856823Seric replac += strlen(replac) + 1; 99956823Seric xpvp = NULL; 100056823Seric } 100156823Seric switch (nodetype) 100256823Seric { 100356678Seric case CANONHOST: 100456823Seric xpvp = rvp; 100556678Seric break; 100656678Seric 100756678Seric case CANONUSER: 100856678Seric default_rvp = rvp; 100956678Seric break; 101056678Seric } 101153654Seric } 101216914Seric if (*rvp != NULL) 101316914Seric *rvp++ = NULL; 101456823Seric if (xpvp != NULL) 101556823Seric { 101658814Seric cataddr(xpvp, NULL, replac, 101758082Seric &pvpbuf[sizeof pvpbuf] - replac, 101858082Seric '\0'); 101956823Seric *++arg_rvp = replac; 102056823Seric } 102156823Seric *++arg_rvp = NULL; 102216914Seric 102316914Seric /* save the remainder of the input string */ 102416914Seric trsize = (int) (avp - rvp + 1) * sizeof *rvp; 102516914Seric bcopy((char *) rvp, (char *) pvpb1, trsize); 102616914Seric 102756678Seric /* look it up */ 102858814Seric cataddr(key_rvp, NULL, buf, sizeof buf, '\0'); 102956823Seric argvect[0] = buf; 103060538Seric if (map != NULL && bitset(MF_OPEN, map->s_map.map_mflags)) 103156836Seric { 103259084Seric auto int stat = EX_OK; 103356836Seric 103460215Seric /* XXX should try to auto-open the map here */ 103560215Seric 103658796Seric if (tTd(60, 1)) 103758796Seric printf("map_lookup(%s, %s) => ", 103858796Seric mapname, buf); 103956836Seric replac = (*map->s_map.map_class->map_lookup)(&map->s_map, 104060089Seric buf, argvect, &stat); 104158796Seric if (tTd(60, 1)) 104259084Seric printf("%s (%d)\n", 104359084Seric replac ? replac : "NOT FOUND", 104459084Seric stat); 104559084Seric 104659084Seric /* should recover if stat == EX_TEMPFAIL */ 104759084Seric if (stat == EX_TEMPFAIL) 104859084Seric rstat = stat; 104956836Seric } 105053654Seric else 105156678Seric replac = NULL; 105256678Seric 105356678Seric /* if no replacement, use default */ 105456823Seric if (replac == NULL && default_rvp != NULL) 105556823Seric { 105660089Seric /* create the default */ 105760089Seric cataddr(default_rvp, NULL, buf, sizeof buf, '\0'); 105856823Seric replac = buf; 105956823Seric } 106056823Seric 106156678Seric if (replac == NULL) 106251317Seric { 106356823Seric xpvp = key_rvp; 106453654Seric } 1065*64404Seric else if (*replac == '\0') 1066*64404Seric { 1067*64404Seric /* null replacement */ 1068*64404Seric nullpvp[0] = NULL; 1069*64404Seric xpvp = nullpvp; 1070*64404Seric } 107156678Seric else 107253654Seric { 107356678Seric /* scan the new replacement */ 107458333Seric xpvp = prescan(replac, '\0', pvpbuf, NULL); 107553654Seric if (xpvp == NULL) 107651317Seric { 107758403Seric /* prescan already printed error */ 107859084Seric return EX_DATAERR; 107956678Seric } 108051317Seric } 108151317Seric 108216914Seric /* append it to the token list */ 108356678Seric for (avp = hbrvp; *xpvp != NULL; xpvp++) 108456678Seric { 108517174Seric *avp++ = newstr(*xpvp); 108616920Seric if (avp >= &npvp[MAXATOM]) 108716914Seric goto toolong; 108817174Seric } 108916914Seric 109016914Seric /* restore the old trailing information */ 109156678Seric for (xpvp = pvpb1; (*avp++ = *xpvp++) != NULL; ) 109216920Seric if (avp >= &npvp[MAXATOM]) 109316914Seric goto toolong; 109417174Seric 109556678Seric break; 109616914Seric } 109716914Seric 109816914Seric /* 109916914Seric ** Check for subroutine calls. 110016914Seric */ 110116914Seric 110258050Seric if (*npvp != NULL && (**npvp & 0377) == CALLSUBR) 110356678Seric { 110459084Seric int stat; 110559084Seric 110656678Seric bcopy((char *) &npvp[2], (char *) pvp, 110756678Seric (int) (avp - npvp - 2) * sizeof *avp); 110856678Seric if (tTd(21, 3)) 110956678Seric printf("-----callsubr %s\n", npvp[1]); 111059084Seric stat = rewrite(pvp, atoi(npvp[1]), e); 111159084Seric if (rstat == EX_OK || stat == EX_TEMPFAIL) 111259084Seric rstat = stat; 111363581Seric if ((**pvp & 0377) == CANONNET) 111463581Seric rwr = NULL; 111556678Seric } 111656678Seric else 111756678Seric { 111817348Seric bcopy((char *) npvp, (char *) pvp, 111916900Seric (int) (avp - npvp) * sizeof *avp); 112056678Seric } 11219374Seric if (tTd(21, 4)) 11229374Seric { 11239374Seric printf("rewritten as:"); 112456678Seric printav(pvp); 11259374Seric } 1126297Seric } 11278069Seric 11289279Seric if (OpMode == MD_TEST || tTd(21, 2)) 11298069Seric { 11308959Seric printf("rewrite: ruleset %2d returns:", ruleset); 113156678Seric printav(pvp); 11328069Seric } 113359084Seric 113459084Seric return rstat; 11353149Seric } 11363149Seric /* 11373149Seric ** BUILDADDR -- build address from token vector. 11383149Seric ** 11393149Seric ** Parameters: 11403149Seric ** tv -- token vector. 11413149Seric ** a -- pointer to address descriptor to fill. 11423149Seric ** If NULL, one will be allocated. 114364284Seric ** flags -- info regarding whether this is a sender or 114464284Seric ** a recipient. 114558966Seric ** e -- the current envelope. 11463149Seric ** 11473149Seric ** Returns: 11484279Seric ** NULL if there was an error. 11494279Seric ** 'a' otherwise. 11503149Seric ** 11513149Seric ** Side Effects: 11523149Seric ** fills in 'a' 11533149Seric */ 11543149Seric 115557249Seric struct errcodes 115657249Seric { 115757249Seric char *ec_name; /* name of error code */ 115857249Seric int ec_code; /* numeric code */ 115957249Seric } ErrorCodes[] = 116057249Seric { 116157249Seric "usage", EX_USAGE, 116257249Seric "nouser", EX_NOUSER, 116357249Seric "nohost", EX_NOHOST, 116457249Seric "unavailable", EX_UNAVAILABLE, 116557249Seric "software", EX_SOFTWARE, 116657249Seric "tempfail", EX_TEMPFAIL, 116757249Seric "protocol", EX_PROTOCOL, 116857249Seric #ifdef EX_CONFIG 116957249Seric "config", EX_CONFIG, 117057249Seric #endif 117157249Seric NULL, EX_UNAVAILABLE, 117257249Seric }; 117357249Seric 117456678Seric ADDRESS * 117564284Seric buildaddr(tv, a, flags, e) 11763149Seric register char **tv; 11773149Seric register ADDRESS *a; 117864284Seric int flags; 117958966Seric register ENVELOPE *e; 11803149Seric { 11813149Seric struct mailer **mp; 11823149Seric register struct mailer *m; 118358008Seric char *bp; 118458008Seric int spaceleft; 118564306Seric static MAILER errormailer; 118664306Seric static char *errorargv[] = { "ERROR", NULL }; 118757402Seric static char buf[MAXNAME]; 11883149Seric 11893149Seric if (a == NULL) 11903149Seric a = (ADDRESS *) xalloc(sizeof *a); 119116889Seric bzero((char *) a, sizeof *a); 11923149Seric 11933149Seric /* figure out what net/mailer to use */ 119464306Seric if (*tv == NULL || (**tv & 0377) != CANONNET) 11954279Seric { 119658151Seric syserr("554 buildaddr: no net"); 119764306Seric badaddr: 119864306Seric a->q_flags |= QBADADDR; 119964306Seric a->q_mailer = &errormailer; 120064306Seric if (errormailer.m_name == NULL) 120164306Seric { 120264306Seric /* initialize the bogus mailer */ 120364306Seric errormailer.m_name = "*error*"; 120464306Seric errormailer.m_mailer = "ERROR"; 120564306Seric errormailer.m_argv = errorargv; 120664306Seric } 120764306Seric return a; 12084279Seric } 12093149Seric tv++; 121058680Seric if (strcasecmp(*tv, "error") == 0) 12114279Seric { 121258050Seric if ((**++tv & 0377) == CANONHOST) 121310183Seric { 121457249Seric register struct errcodes *ep; 121557249Seric 121658050Seric if (isascii(**++tv) && isdigit(**tv)) 121757249Seric { 121857249Seric setstat(atoi(*tv)); 121957249Seric } 122057249Seric else 122157249Seric { 122257249Seric for (ep = ErrorCodes; ep->ec_name != NULL; ep++) 122357249Seric if (strcasecmp(ep->ec_name, *tv) == 0) 122457249Seric break; 122557249Seric setstat(ep->ec_code); 122657249Seric } 122710183Seric tv++; 122810183Seric } 122958050Seric if ((**tv & 0377) != CANONUSER) 123058151Seric syserr("554 buildaddr: error: no user"); 123158814Seric cataddr(++tv, NULL, buf, sizeof buf, ' '); 123258082Seric stripquotes(buf); 123364398Seric usrerr("%s", buf); 123464306Seric goto badaddr; 12354279Seric } 123657402Seric 12374598Seric for (mp = Mailer; (m = *mp++) != NULL; ) 12383149Seric { 123958680Seric if (strcasecmp(m->m_name, *tv) == 0) 12403149Seric break; 12413149Seric } 12423149Seric if (m == NULL) 12434279Seric { 124458151Seric syserr("554 buildaddr: unknown mailer %s", *tv); 124564306Seric goto badaddr; 12464279Seric } 12474598Seric a->q_mailer = m; 12483149Seric 12493149Seric /* figure out what host (if any) */ 125056678Seric tv++; 125158509Seric if ((**tv & 0377) == CANONHOST) 12523149Seric { 125358008Seric bp = buf; 125458008Seric spaceleft = sizeof buf - 1; 125558050Seric while (*++tv != NULL && (**tv & 0377) != CANONUSER) 125658008Seric { 125758008Seric int i = strlen(*tv); 125858008Seric 125958008Seric if (i > spaceleft) 126058008Seric { 126158008Seric /* out of space for this address */ 126258008Seric if (spaceleft >= 0) 126358151Seric syserr("554 buildaddr: host too long (%.40s...)", 126458008Seric buf); 126558008Seric i = spaceleft; 126658008Seric spaceleft = 0; 126758008Seric } 126858008Seric if (i <= 0) 126958008Seric continue; 127058008Seric bcopy(*tv, bp, i); 127158008Seric bp += i; 127258008Seric spaceleft -= i; 127358008Seric } 127458010Seric *bp = '\0'; 12755704Seric a->q_host = newstr(buf); 12763149Seric } 127757249Seric else 127858509Seric { 127958509Seric if (!bitnset(M_LOCALMAILER, m->m_flags)) 128058509Seric { 128158509Seric syserr("554 buildaddr: no host"); 128264306Seric goto badaddr; 128358509Seric } 128457249Seric a->q_host = NULL; 128558509Seric } 12863149Seric 12873149Seric /* figure out the user */ 128858050Seric if (*tv == NULL || (**tv & 0377) != CANONUSER) 12894279Seric { 129058151Seric syserr("554 buildaddr: no user"); 129164306Seric goto badaddr; 12924279Seric } 129357402Seric tv++; 129451317Seric 129557402Seric /* do special mapping for local mailer */ 129657402Seric if (m == LocalMailer && *tv != NULL) 129757402Seric { 129857454Seric register char *p = *tv; 129957454Seric 130057454Seric if (*p == '"') 130157454Seric p++; 130257454Seric if (*p == '|') 130357402Seric a->q_mailer = m = ProgMailer; 130457454Seric else if (*p == '/') 130557402Seric a->q_mailer = m = FileMailer; 130657454Seric else if (*p == ':') 130757402Seric { 130857402Seric /* may be :include: */ 130958814Seric cataddr(tv, NULL, buf, sizeof buf, '\0'); 131058008Seric stripquotes(buf); 131158008Seric if (strncasecmp(buf, ":include:", 9) == 0) 131258008Seric { 131358008Seric /* if :include:, don't need further rewriting */ 131457402Seric a->q_mailer = m = InclMailer; 131558008Seric a->q_user = &buf[9]; 131658008Seric return (a); 131758008Seric } 131857402Seric } 131957402Seric } 132057402Seric 132158008Seric if (m == LocalMailer && *tv != NULL && strcmp(*tv, "@") == 0) 132258008Seric { 132358008Seric tv++; 132458008Seric a->q_flags |= QNOTREMOTE; 132558008Seric } 132658008Seric 132764284Seric /* rewrite according recipient mailer rewriting rules */ 132864284Seric define('h', a->q_host, e); 132964284Seric if (!bitset(RF_SENDERADDR|RF_HEADERADDR, flags)) 133064284Seric { 133164284Seric /* sender addresses done later */ 133264284Seric (void) rewrite(tv, 2, e); 133364284Seric if (m->m_re_rwset > 0) 133464284Seric (void) rewrite(tv, m->m_re_rwset, e); 133564284Seric } 133659084Seric (void) rewrite(tv, 4, e); 133719040Seric 133819040Seric /* save the result for the command line/RCPT argument */ 133958814Seric cataddr(tv, NULL, buf, sizeof buf, '\0'); 13403149Seric a->q_user = buf; 13413149Seric 134258670Seric /* 134358670Seric ** Do mapping to lower case as requested by mailer 134458670Seric */ 134558670Seric 134658670Seric if (a->q_host != NULL && !bitnset(M_HST_UPPER, m->m_flags)) 134758670Seric makelower(a->q_host); 134858670Seric if (!bitnset(M_USR_UPPER, m->m_flags)) 134958670Seric makelower(a->q_user); 135058670Seric 13513149Seric return (a); 13523149Seric } 13533188Seric /* 13544228Seric ** CATADDR -- concatenate pieces of addresses (putting in <LWSP> subs) 13554228Seric ** 13564228Seric ** Parameters: 13574228Seric ** pvp -- parameter vector to rebuild. 135858814Seric ** evp -- last parameter to include. Can be NULL to 135958814Seric ** use entire pvp. 13604228Seric ** buf -- buffer to build the string into. 13614228Seric ** sz -- size of buf. 136258082Seric ** spacesub -- the space separator character; if null, 136358082Seric ** use SpaceSub. 13644228Seric ** 13654228Seric ** Returns: 13664228Seric ** none. 13674228Seric ** 13684228Seric ** Side Effects: 13694228Seric ** Destroys buf. 13704228Seric */ 13714228Seric 137258814Seric cataddr(pvp, evp, buf, sz, spacesub) 13734228Seric char **pvp; 137458814Seric char **evp; 13754228Seric char *buf; 13764228Seric register int sz; 137758082Seric char spacesub; 13784228Seric { 13794228Seric bool oatomtok = FALSE; 138056678Seric bool natomtok = FALSE; 13814228Seric register int i; 13824228Seric register char *p; 13834228Seric 138458082Seric if (spacesub == '\0') 138558082Seric spacesub = SpaceSub; 138658082Seric 13878423Seric if (pvp == NULL) 13888423Seric { 138923109Seric (void) strcpy(buf, ""); 13908423Seric return; 13918423Seric } 13924228Seric p = buf; 139311156Seric sz -= 2; 13944228Seric while (*pvp != NULL && (i = strlen(*pvp)) < sz) 13954228Seric { 13968078Seric natomtok = (toktype(**pvp) == ATM); 13974228Seric if (oatomtok && natomtok) 139858082Seric *p++ = spacesub; 13994228Seric (void) strcpy(p, *pvp); 14004228Seric oatomtok = natomtok; 14014228Seric p += i; 140211156Seric sz -= i + 1; 140358814Seric if (pvp++ == evp) 140458814Seric break; 14054228Seric } 14064228Seric *p = '\0'; 14074228Seric } 14084228Seric /* 14093188Seric ** SAMEADDR -- Determine if two addresses are the same 14103188Seric ** 14113188Seric ** This is not just a straight comparison -- if the mailer doesn't 14123188Seric ** care about the host we just ignore it, etc. 14133188Seric ** 14143188Seric ** Parameters: 14153188Seric ** a, b -- pointers to the internal forms to compare. 14163188Seric ** 14173188Seric ** Returns: 14183188Seric ** TRUE -- they represent the same mailbox. 14193188Seric ** FALSE -- they don't. 14203188Seric ** 14213188Seric ** Side Effects: 14223188Seric ** none. 14233188Seric */ 14243188Seric 14253188Seric bool 14269374Seric sameaddr(a, b) 14273188Seric register ADDRESS *a; 14283188Seric register ADDRESS *b; 14293188Seric { 14303188Seric /* if they don't have the same mailer, forget it */ 14313188Seric if (a->q_mailer != b->q_mailer) 14323188Seric return (FALSE); 14333188Seric 14343188Seric /* if the user isn't the same, we can drop out */ 143556678Seric if (strcmp(a->q_user, b->q_user) != 0) 14363188Seric return (FALSE); 14373188Seric 143858438Seric /* if we have good uids for both but the differ, these are different */ 143958438Seric if (bitset(QGOODUID, a->q_flags & b->q_flags) && a->q_uid != b->q_uid) 144058438Seric return (FALSE); 144158438Seric 144258509Seric /* otherwise compare hosts (but be careful for NULL ptrs) */ 144358509Seric if (a->q_host == b->q_host) 144458509Seric { 144558509Seric /* probably both null pointers */ 14463188Seric return (TRUE); 144758509Seric } 14483188Seric if (a->q_host == NULL || b->q_host == NULL) 144958509Seric { 145058509Seric /* only one is a null pointer */ 14513188Seric return (FALSE); 145258509Seric } 145356678Seric if (strcmp(a->q_host, b->q_host) != 0) 14543188Seric return (FALSE); 14553188Seric 14563188Seric return (TRUE); 14573188Seric } 14583234Seric /* 14593234Seric ** PRINTADDR -- print address (for debugging) 14603234Seric ** 14613234Seric ** Parameters: 14623234Seric ** a -- the address to print 14633234Seric ** follow -- follow the q_next chain. 14643234Seric ** 14653234Seric ** Returns: 14663234Seric ** none. 14673234Seric ** 14683234Seric ** Side Effects: 14693234Seric ** none. 14703234Seric */ 14713234Seric 14723234Seric printaddr(a, follow) 14733234Seric register ADDRESS *a; 14743234Seric bool follow; 14753234Seric { 14765001Seric bool first = TRUE; 147757731Seric register MAILER *m; 147857731Seric MAILER pseudomailer; 14795001Seric 14803234Seric while (a != NULL) 14813234Seric { 14825001Seric first = FALSE; 14834443Seric printf("%x=", a); 14844085Seric (void) fflush(stdout); 148557731Seric 148657731Seric /* find the mailer -- carefully */ 148757731Seric m = a->q_mailer; 148857731Seric if (m == NULL) 148957731Seric { 149057731Seric m = &pseudomailer; 149157731Seric m->m_mno = -1; 149257731Seric m->m_name = "NULL"; 149357731Seric } 149457731Seric 149558680Seric printf("%s:\n\tmailer %d (%s), host `%s', user `%s', ruser `%s'\n", 149657731Seric a->q_paddr, m->m_mno, m->m_name, 149763756Seric a->q_host, a->q_user, 149863756Seric a->q_ruser ? a->q_ruser : "<null>"); 149959269Seric printf("\tnext=%x, flags=%o, alias %x, uid %d, gid %d\n", 150059269Seric a->q_next, a->q_flags, a->q_alias, a->q_uid, a->q_gid); 150159269Seric printf("\towner=%s, home=\"%s\", fullname=\"%s\"\n", 150259269Seric a->q_owner == NULL ? "(none)" : a->q_owner, 150363756Seric a->q_home == NULL ? "(none)" : a->q_home, 150463756Seric a->q_fullname == NULL ? "(none)" : a->q_fullname); 15054996Seric 15063234Seric if (!follow) 15073234Seric return; 15084996Seric a = a->q_next; 15093234Seric } 15105001Seric if (first) 15114443Seric printf("[NULL]\n"); 15123234Seric } 15134317Seric 15147682Seric /* 15157682Seric ** REMOTENAME -- return the name relative to the current mailer 15167682Seric ** 15177682Seric ** Parameters: 15187682Seric ** name -- the name to translate. 15198069Seric ** m -- the mailer that we want to do rewriting relative 15208069Seric ** to. 152159163Seric ** flags -- fine tune operations. 152259163Seric ** pstat -- pointer to status word. 152358020Seric ** e -- the current envelope. 15247682Seric ** 15257682Seric ** Returns: 15267682Seric ** the text string representing this address relative to 15277682Seric ** the receiving mailer. 15287682Seric ** 15297682Seric ** Side Effects: 15307682Seric ** none. 15317682Seric ** 15327682Seric ** Warnings: 15337682Seric ** The text string returned is tucked away locally; 15347682Seric ** copy it if you intend to save it. 15357682Seric */ 15367682Seric 15377682Seric char * 153859163Seric remotename(name, m, flags, pstat, e) 15397682Seric char *name; 154056678Seric struct mailer *m; 154159163Seric int flags; 154259163Seric int *pstat; 154356678Seric register ENVELOPE *e; 15447682Seric { 15458069Seric register char **pvp; 15468069Seric char *fancy; 154756678Seric char *oldg = macvalue('g', e); 154858020Seric int rwset; 15497682Seric static char buf[MAXNAME]; 15507682Seric char lbuf[MAXNAME]; 155116914Seric char pvpbuf[PSBUFSIZE]; 155256678Seric extern char *crackaddr(); 15537682Seric 15547755Seric if (tTd(12, 1)) 15557755Seric printf("remotename(%s)\n", name); 15567755Seric 155710177Seric /* don't do anything if we are tagging it as special */ 155859163Seric if (bitset(RF_SENDERADDR, flags)) 155959163Seric rwset = bitset(RF_HEADERADDR, flags) ? m->m_sh_rwset 156059163Seric : m->m_se_rwset; 156158020Seric else 156259163Seric rwset = bitset(RF_HEADERADDR, flags) ? m->m_rh_rwset 156359163Seric : m->m_re_rwset; 156458020Seric if (rwset < 0) 156510177Seric return (name); 156610177Seric 15677682Seric /* 15688181Seric ** Do a heuristic crack of this name to extract any comment info. 15698181Seric ** This will leave the name as a comment and a $g macro. 15707889Seric */ 15717889Seric 157259163Seric if (bitset(RF_CANONICAL, flags) || bitnset(M_NOCOMMENT, m->m_flags)) 157358050Seric fancy = "\201g"; 157410310Seric else 157510310Seric fancy = crackaddr(name); 15767889Seric 15778181Seric /* 15788181Seric ** Turn the name into canonical form. 15798181Seric ** Normally this will be RFC 822 style, i.e., "user@domain". 15808181Seric ** If this only resolves to "user", and the "C" flag is 15818181Seric ** specified in the sending mailer, then the sender's 15828181Seric ** domain will be appended. 15838181Seric */ 15848181Seric 158558333Seric pvp = prescan(name, '\0', pvpbuf, NULL); 15867889Seric if (pvp == NULL) 15877889Seric return (name); 158859163Seric if (rewrite(pvp, 3, e) == EX_TEMPFAIL) 158959163Seric *pstat = EX_TEMPFAIL; 159059163Seric if (bitset(RF_ADDDOMAIN, flags) && e->e_fromdomain != NULL) 15918181Seric { 15928181Seric /* append from domain to this address */ 15938181Seric register char **pxp = pvp; 15948181Seric 15959594Seric /* see if there is an "@domain" in the current name */ 15968181Seric while (*pxp != NULL && strcmp(*pxp, "@") != 0) 15978181Seric pxp++; 15988181Seric if (*pxp == NULL) 15998181Seric { 16009594Seric /* no.... append the "@domain" from the sender */ 160156678Seric register char **qxq = e->e_fromdomain; 16028181Seric 16039594Seric while ((*pxp++ = *qxq++) != NULL) 16049594Seric continue; 160559163Seric if (rewrite(pvp, 3, e) == EX_TEMPFAIL) 160659163Seric *pstat = EX_TEMPFAIL; 16078181Seric } 16088181Seric } 16098181Seric 16108181Seric /* 16118959Seric ** Do more specific rewriting. 161256678Seric ** Rewrite using ruleset 1 or 2 depending on whether this is 161356678Seric ** a sender address or not. 16148181Seric ** Then run it through any receiving-mailer-specific rulesets. 16158181Seric */ 16168181Seric 161759163Seric if (bitset(RF_SENDERADDR, flags)) 161859541Seric { 161959163Seric if (rewrite(pvp, 1, e) == EX_TEMPFAIL) 162059163Seric *pstat = EX_TEMPFAIL; 162159541Seric } 16228069Seric else 162359541Seric { 162459163Seric if (rewrite(pvp, 2, e) == EX_TEMPFAIL) 162559163Seric *pstat = EX_TEMPFAIL; 162659541Seric } 162758020Seric if (rwset > 0) 162859541Seric { 162959163Seric if (rewrite(pvp, rwset, e) == EX_TEMPFAIL) 163059163Seric *pstat = EX_TEMPFAIL; 163159541Seric } 16327682Seric 16338181Seric /* 16348959Seric ** Do any final sanitation the address may require. 16358959Seric ** This will normally be used to turn internal forms 16368959Seric ** (e.g., user@host.LOCAL) into external form. This 16378959Seric ** may be used as a default to the above rules. 16388959Seric */ 16398959Seric 164059163Seric if (rewrite(pvp, 4, e) == EX_TEMPFAIL) 164159163Seric *pstat = EX_TEMPFAIL; 16428959Seric 16438959Seric /* 16448181Seric ** Now restore the comment information we had at the beginning. 16458181Seric */ 16468181Seric 164758825Seric cataddr(pvp, NULL, lbuf, sizeof lbuf, '\0'); 164856678Seric define('g', lbuf, e); 164956678Seric expand(fancy, buf, &buf[sizeof buf - 1], e); 165056678Seric define('g', oldg, e); 16517682Seric 16527682Seric if (tTd(12, 1)) 16537755Seric printf("remotename => `%s'\n", buf); 16547682Seric return (buf); 16557682Seric } 165651317Seric /* 165756678Seric ** MAPLOCALUSER -- run local username through ruleset 5 for final redirection 165851317Seric ** 165951317Seric ** Parameters: 166056678Seric ** a -- the address to map (but just the user name part). 166156678Seric ** sendq -- the sendq in which to install any replacement 166256678Seric ** addresses. 166351317Seric ** 166451317Seric ** Returns: 166551317Seric ** none. 166651317Seric */ 166751317Seric 166856678Seric maplocaluser(a, sendq, e) 166956678Seric register ADDRESS *a; 167056678Seric ADDRESS **sendq; 167156678Seric ENVELOPE *e; 167251317Seric { 167356678Seric register char **pvp; 167456678Seric register ADDRESS *a1 = NULL; 167558333Seric auto char *delimptr; 167656678Seric char pvpbuf[PSBUFSIZE]; 167751317Seric 167856678Seric if (tTd(29, 1)) 167956678Seric { 168056678Seric printf("maplocaluser: "); 168156678Seric printaddr(a, FALSE); 168256678Seric } 168358333Seric pvp = prescan(a->q_user, '\0', pvpbuf, &delimptr); 168456678Seric if (pvp == NULL) 168556678Seric return; 168651317Seric 168759084Seric (void) rewrite(pvp, 5, e); 168858050Seric if (pvp[0] == NULL || (pvp[0][0] & 0377) != CANONNET) 168956678Seric return; 169051317Seric 169156678Seric /* if non-null, mailer destination specified -- has it changed? */ 169264284Seric a1 = buildaddr(pvp, NULL, 0, e); 169356678Seric if (a1 == NULL || sameaddr(a, a1)) 169456678Seric return; 169551317Seric 169656678Seric /* mark old address as dead; insert new address */ 169756678Seric a->q_flags |= QDONTSEND; 169857731Seric if (tTd(29, 5)) 169957731Seric { 170057731Seric printf("maplocaluser: QDONTSEND "); 170157731Seric printaddr(a, FALSE); 170257731Seric } 170356678Seric a1->q_alias = a; 170464348Seric allocaddr(a1, RF_COPYALL, NULL); 170556678Seric (void) recipient(a1, sendq, e); 170651317Seric } 170758802Seric /* 170858802Seric ** DEQUOTE_INIT -- initialize dequote map 170958802Seric ** 171058802Seric ** This is a no-op. 171158802Seric ** 171258802Seric ** Parameters: 171358802Seric ** map -- the internal map structure. 171458802Seric ** args -- arguments. 171558802Seric ** 171658802Seric ** Returns: 171758802Seric ** TRUE. 171858802Seric */ 171958802Seric 172058802Seric bool 172160219Seric dequote_init(map, args) 172258802Seric MAP *map; 172358802Seric char *args; 172458802Seric { 172558805Seric register char *p = args; 172658805Seric 172758805Seric for (;;) 172858805Seric { 172958805Seric while (isascii(*p) && isspace(*p)) 173058805Seric p++; 173158805Seric if (*p != '-') 173258805Seric break; 173358805Seric switch (*++p) 173458805Seric { 173558805Seric case 'a': 173658805Seric map->map_app = ++p; 173758805Seric break; 173858805Seric } 173958805Seric while (*p != '\0' && !(isascii(*p) && isspace(*p))) 174058805Seric p++; 174158805Seric if (*p != '\0') 174258805Seric *p = '\0'; 174358805Seric } 174458805Seric if (map->map_app != NULL) 174558805Seric map->map_app = newstr(map->map_app); 174658805Seric 174758802Seric return TRUE; 174858802Seric } 174958802Seric /* 175058802Seric ** DEQUOTE_MAP -- unquote an address 175158802Seric ** 175258802Seric ** Parameters: 175358802Seric ** map -- the internal map structure (ignored). 175460089Seric ** name -- the name to dequote. 175558802Seric ** av -- arguments (ignored). 175659084Seric ** statp -- pointer to status out-parameter. 175758802Seric ** 175858802Seric ** Returns: 175958802Seric ** NULL -- if there were no quotes, or if the resulting 176058802Seric ** unquoted buffer would not be acceptable to prescan. 176158802Seric ** else -- The dequoted buffer. 176258802Seric */ 176358802Seric 176458802Seric char * 176560089Seric dequote_map(map, name, av, statp) 176658802Seric MAP *map; 176760089Seric char *name; 176858802Seric char **av; 176959084Seric int *statp; 177058802Seric { 177158802Seric register char *p; 177258802Seric register char *q; 177358802Seric register char c; 177458802Seric int anglecnt; 177558802Seric int cmntcnt; 177658802Seric int quotecnt; 177759089Seric int spacecnt; 177858802Seric bool quotemode; 177958802Seric bool bslashmode; 178058802Seric 178158802Seric anglecnt = 0; 178258802Seric cmntcnt = 0; 178358802Seric quotecnt = 0; 178459089Seric spacecnt = 0; 178558802Seric quotemode = FALSE; 178658802Seric bslashmode = FALSE; 178758802Seric 178860089Seric for (p = q = name; (c = *p++) != '\0'; ) 178958802Seric { 179058802Seric if (bslashmode) 179158802Seric { 179258802Seric bslashmode = FALSE; 179358802Seric *q++ = c; 179458802Seric continue; 179558802Seric } 179658802Seric 179758802Seric switch (c) 179858802Seric { 179958802Seric case '\\': 180058802Seric bslashmode = TRUE; 180158802Seric break; 180258802Seric 180358802Seric case '(': 180458802Seric cmntcnt++; 180558802Seric break; 180658802Seric 180758802Seric case ')': 180858802Seric if (cmntcnt-- <= 0) 180958802Seric return NULL; 181058802Seric break; 181159089Seric 181259089Seric case ' ': 181359089Seric spacecnt++; 181459089Seric break; 181558802Seric } 181658802Seric 181758802Seric if (cmntcnt > 0) 181858802Seric { 181958802Seric *q++ = c; 182058802Seric continue; 182158802Seric } 182258802Seric 182358802Seric switch (c) 182458802Seric { 182558802Seric case '"': 182658802Seric quotemode = !quotemode; 182758802Seric quotecnt++; 182858802Seric continue; 182958802Seric 183058802Seric case '<': 183158802Seric anglecnt++; 183258802Seric break; 183358802Seric 183458802Seric case '>': 183558802Seric if (anglecnt-- <= 0) 183658802Seric return NULL; 183758802Seric break; 183858802Seric } 183958802Seric *q++ = c; 184058802Seric } 184158802Seric 184258802Seric if (anglecnt != 0 || cmntcnt != 0 || bslashmode || 184359089Seric quotemode || quotecnt <= 0 || spacecnt != 0) 184458802Seric return NULL; 184558802Seric *q++ = '\0'; 184660089Seric return name; 184758802Seric } 1848