122976Smiriam /* 234921Sbostic * Copyright (c) 1983 Eric P. Allman 333730Sbostic * Copyright (c) 1988 Regents of the University of California. 433730Sbostic * All rights reserved. 533730Sbostic * 642828Sbostic * %sccs.include.redist.c% 733730Sbostic */ 822976Smiriam 922976Smiriam #ifndef lint 10*59105Seric static char sccsid[] = "@(#)parseaddr.c 6.41 (Berkeley) 04/16/93"; 1133730Sbostic #endif /* not lint */ 1222976Smiriam 1356678Seric # include "sendmail.h" 14297Seric 15297Seric /* 169888Seric ** PARSEADDR -- Parse an address 17297Seric ** 18297Seric ** Parses an address and breaks it up into three parts: a 19297Seric ** net to transmit the message on, the host to transmit it 20297Seric ** to, and a user on that host. These are loaded into an 212973Seric ** ADDRESS header with the values squirreled away if necessary. 22297Seric ** The "user" part may not be a real user; the process may 23297Seric ** just reoccur on that machine. For example, on a machine 24297Seric ** with an arpanet connection, the address 25297Seric ** csvax.bill@berkeley 26297Seric ** will break up to a "user" of 'csvax.bill' and a host 27297Seric ** of 'berkeley' -- to be transmitted over the arpanet. 28297Seric ** 29297Seric ** Parameters: 30297Seric ** addr -- the address to parse. 31297Seric ** a -- a pointer to the address descriptor buffer. 32297Seric ** If NULL, a header will be created. 33297Seric ** copyf -- determines what shall be copied: 34297Seric ** -1 -- don't copy anything. The printname 35297Seric ** (q_paddr) is just addr, and the 36297Seric ** user & host are allocated internally 37297Seric ** to parse. 38297Seric ** 0 -- copy out the parsed user & host, but 39297Seric ** don't copy the printname. 40297Seric ** +1 -- copy everything. 4111445Seric ** delim -- the character to terminate the address, passed 4211445Seric ** to prescan. 4358333Seric ** delimptr -- if non-NULL, set to the location of the 4458333Seric ** delim character that was found. 4556678Seric ** e -- the envelope that will contain this address. 46297Seric ** 47297Seric ** Returns: 48297Seric ** A pointer to the address descriptor header (`a' if 49297Seric ** `a' is non-NULL). 50297Seric ** NULL on error. 51297Seric ** 52297Seric ** Side Effects: 53297Seric ** none 54297Seric */ 55297Seric 569374Seric /* following delimiters are inherent to the internal algorithms */ 5758050Seric # define DELIMCHARS "\201()<>,;\\\"\r\n" /* word delimiters */ 582091Seric 592973Seric ADDRESS * 6058333Seric parseaddr(addr, a, copyf, delim, delimptr, e) 61297Seric char *addr; 622973Seric register ADDRESS *a; 63297Seric int copyf; 6411445Seric char delim; 6558333Seric char **delimptr; 6656678Seric register ENVELOPE *e; 67297Seric { 683149Seric register char **pvp; 6958333Seric auto char *delimptrbuf; 7059084Seric bool queueup; 7116914Seric char pvpbuf[PSBUFSIZE]; 7256678Seric extern char **prescan(); 7356678Seric extern ADDRESS *buildaddr(); 7457388Seric extern bool invalidaddr(); 75297Seric 76297Seric /* 77297Seric ** Initialize and prescan address. 78297Seric */ 79297Seric 8056678Seric e->e_to = addr; 817675Seric if (tTd(20, 1)) 829888Seric printf("\n--parseaddr(%s)\n", addr); 833188Seric 8457388Seric if (invalidaddr(addr)) 8557388Seric { 8657388Seric if (tTd(20, 1)) 8757388Seric printf("parseaddr-->bad address\n"); 8857388Seric return NULL; 8957388Seric } 9057388Seric 9158333Seric if (delimptr == NULL) 9258333Seric delimptr = &delimptrbuf; 9358333Seric 9458333Seric pvp = prescan(addr, delim, pvpbuf, delimptr); 953149Seric if (pvp == NULL) 9656729Seric { 9756729Seric if (tTd(20, 1)) 9856729Seric printf("parseaddr-->NULL\n"); 99297Seric return (NULL); 10056729Seric } 101297Seric 102297Seric /* 1033149Seric ** Apply rewriting rules. 1047889Seric ** Ruleset 0 does basic parsing. It must resolve. 105297Seric */ 106297Seric 10759084Seric queueup = FALSE; 10859084Seric if (rewrite(pvp, 3, e) == EX_TEMPFAIL) 10959084Seric queueup = TRUE; 11059084Seric if (rewrite(pvp, 0, e) == EX_TEMPFAIL) 11159084Seric queueup = TRUE; 112297Seric 1133149Seric /* 1143149Seric ** See if we resolved to a real mailer. 1153149Seric */ 116297Seric 11759040Seric if (pvp[0] == NULL || (pvp[0][0] & 0377) != CANONNET) 1183149Seric { 1193149Seric setstat(EX_USAGE); 12058151Seric syserr("554 cannot resolve name"); 1213149Seric return (NULL); 122297Seric } 123297Seric 124297Seric /* 1253149Seric ** Build canonical address from pvp. 126297Seric */ 127297Seric 12858966Seric a = buildaddr(pvp, a, e); 1294279Seric if (a == NULL) 1304279Seric return (NULL); 131297Seric 132297Seric /* 1333149Seric ** Make local copies of the host & user and then 1343149Seric ** transport them out. 135297Seric */ 136297Seric 13758333Seric allocaddr(a, copyf, addr, *delimptr); 13856678Seric 13956678Seric /* 14059084Seric ** If there was a parsing failure, mark it for queueing. 14159084Seric */ 14259084Seric 14359084Seric if (queueup) 14459084Seric a->q_flags |= QQUEUEUP; 14559084Seric 14659084Seric /* 14756678Seric ** Compute return value. 14856678Seric */ 14956678Seric 15056678Seric if (tTd(20, 1)) 1518078Seric { 15256678Seric printf("parseaddr-->"); 15356678Seric printaddr(a, FALSE); 15456678Seric } 15556678Seric 15656678Seric return (a); 15756678Seric } 15856678Seric /* 15957388Seric ** INVALIDADDR -- check for address containing meta-characters 16057388Seric ** 16157388Seric ** Parameters: 16257388Seric ** addr -- the address to check. 16357388Seric ** 16457388Seric ** Returns: 16557388Seric ** TRUE -- if the address has any "wierd" characters 16657388Seric ** FALSE -- otherwise. 16757388Seric */ 16857388Seric 16957388Seric bool 17057388Seric invalidaddr(addr) 17157388Seric register char *addr; 17257388Seric { 17357388Seric for (; *addr != '\0'; addr++) 17457388Seric { 17558050Seric if ((*addr & 0340) != 0200) 17657388Seric continue; 17757388Seric setstat(EX_USAGE); 17858151Seric usrerr("553 Address contained invalid control characters"); 17957388Seric return TRUE; 18057388Seric } 18157388Seric return FALSE; 18257388Seric } 18357388Seric /* 18456678Seric ** ALLOCADDR -- do local allocations of address on demand. 18556678Seric ** 18656678Seric ** Also lowercases the host name if requested. 18756678Seric ** 18856678Seric ** Parameters: 18956678Seric ** a -- the address to reallocate. 19056678Seric ** copyf -- the copy flag (see parseaddr for description). 19156678Seric ** paddr -- the printname of the address. 19258333Seric ** delimptr -- a pointer to the address delimiter. Must be set. 19356678Seric ** 19456678Seric ** Returns: 19556678Seric ** none. 19656678Seric ** 19756678Seric ** Side Effects: 19856678Seric ** Copies portions of a into local buffers as requested. 19956678Seric */ 20056678Seric 20158333Seric allocaddr(a, copyf, paddr, delimptr) 20256678Seric register ADDRESS *a; 20356678Seric int copyf; 20456678Seric char *paddr; 20558333Seric char *delimptr; 20656678Seric { 20756678Seric register MAILER *m = a->q_mailer; 20856678Seric 20958673Seric if (tTd(24, 4)) 21058673Seric printf("allocaddr(copyf=%d, paddr=%s)\n", copyf, paddr); 21158673Seric 21256678Seric if (copyf > 0 && paddr != NULL) 21356678Seric { 21458333Seric char savec = *delimptr; 2158078Seric 21658333Seric *delimptr = '\0'; 21756678Seric a->q_paddr = newstr(paddr); 21858333Seric *delimptr = savec; 2198078Seric } 220297Seric else 22156678Seric a->q_paddr = paddr; 22224944Seric 22324944Seric if (a->q_user == NULL) 22424944Seric a->q_user = ""; 22524944Seric if (a->q_host == NULL) 22624944Seric a->q_host = ""; 22724944Seric 2283149Seric if (copyf >= 0) 229297Seric { 23024944Seric a->q_host = newstr(a->q_host); 2313149Seric if (a->q_user != a->q_paddr) 2323149Seric a->q_user = newstr(a->q_user); 233297Seric } 234297Seric 23556678Seric if (a->q_paddr == NULL) 23656678Seric a->q_paddr = a->q_user; 237297Seric } 238297Seric /* 239297Seric ** PRESCAN -- Prescan name and make it canonical 240297Seric ** 2419374Seric ** Scans a name and turns it into a set of tokens. This process 2429374Seric ** deletes blanks and comments (in parentheses). 243297Seric ** 244297Seric ** This routine knows about quoted strings and angle brackets. 245297Seric ** 246297Seric ** There are certain subtleties to this routine. The one that 247297Seric ** comes to mind now is that backslashes on the ends of names 248297Seric ** are silently stripped off; this is intentional. The problem 249297Seric ** is that some versions of sndmsg (like at LBL) set the kill 250297Seric ** character to something other than @ when reading addresses; 251297Seric ** so people type "csvax.eric\@berkeley" -- which screws up the 252297Seric ** berknet mailer. 253297Seric ** 254297Seric ** Parameters: 255297Seric ** addr -- the name to chomp. 256297Seric ** delim -- the delimiter for the address, normally 257297Seric ** '\0' or ','; \0 is accepted in any case. 25815284Seric ** If '\t' then we are reading the .cf file. 25916914Seric ** pvpbuf -- place to put the saved text -- note that 26016914Seric ** the pointers are static. 26158333Seric ** delimptr -- if non-NULL, set to the location of the 26258333Seric ** terminating delimiter. 263297Seric ** 264297Seric ** Returns: 2653149Seric ** A pointer to a vector of tokens. 266297Seric ** NULL on error. 267297Seric */ 268297Seric 2698078Seric /* states and character types */ 2708078Seric # define OPR 0 /* operator */ 2718078Seric # define ATM 1 /* atom */ 2728078Seric # define QST 2 /* in quoted string */ 2738078Seric # define SPC 3 /* chewing up spaces */ 2748078Seric # define ONE 4 /* pick up one character */ 2753149Seric 2768078Seric # define NSTATES 5 /* number of states */ 2778078Seric # define TYPE 017 /* mask to select state type */ 2788078Seric 2798078Seric /* meta bits for table */ 2808078Seric # define M 020 /* meta character; don't pass through */ 2818078Seric # define B 040 /* cause a break */ 2828078Seric # define MB M|B /* meta-break */ 2838078Seric 2848078Seric static short StateTab[NSTATES][NSTATES] = 2858078Seric { 2868087Seric /* oldst chtype> OPR ATM QST SPC ONE */ 2879051Seric /*OPR*/ OPR|B, ATM|B, QST|B, SPC|MB, ONE|B, 2889051Seric /*ATM*/ OPR|B, ATM, QST|B, SPC|MB, ONE|B, 2899051Seric /*QST*/ QST, QST, OPR, QST, QST, 2908078Seric /*SPC*/ OPR, ATM, QST, SPC|M, ONE, 2918078Seric /*ONE*/ OPR, OPR, OPR, OPR, OPR, 2928078Seric }; 2938078Seric 2948078Seric # define NOCHAR -1 /* signal nothing in lookahead token */ 2958078Seric 2963149Seric char ** 29758333Seric prescan(addr, delim, pvpbuf, delimptr) 298297Seric char *addr; 299297Seric char delim; 30016914Seric char pvpbuf[]; 30158333Seric char **delimptr; 302297Seric { 303297Seric register char *p; 3048078Seric register char *q; 3059346Seric register int c; 3063149Seric char **avp; 307297Seric bool bslashmode; 308297Seric int cmntcnt; 3098423Seric int anglecnt; 3103149Seric char *tok; 3118078Seric int state; 3128078Seric int newstate; 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; 32756764Seric if (tTd(22, 11)) 328297Seric { 3298078Seric printf("prescan: "); 3308078Seric xputs(p); 33123109Seric (void) putchar('\n'); 3328078Seric } 3338078Seric 3348078Seric do 3358078Seric { 3363149Seric /* read a token */ 3373149Seric tok = q; 3388078Seric for (;;) 339297Seric { 3408078Seric /* store away any old lookahead character */ 3418078Seric if (c != NOCHAR) 3428078Seric { 34315284Seric /* see if there is room */ 34416914Seric if (q >= &pvpbuf[PSBUFSIZE - 5]) 3458078Seric { 34658151Seric usrerr("553 Address too long"); 34758333Seric if (delimptr != NULL) 34858333Seric *delimptr = p; 3498078Seric return (NULL); 3508078Seric } 35115284Seric 35215284Seric /* squirrel it away */ 3538078Seric *q++ = c; 3548078Seric } 3558078Seric 3568078Seric /* read a new input character */ 3578078Seric c = *p++; 35857631Seric if (c == '\0') 35956764Seric { 36056764Seric /* diagnose and patch up bad syntax */ 36156764Seric if (state == QST) 36256764Seric { 36358151Seric usrerr("553 Unbalanced '\"'"); 36456764Seric c = '"'; 36556764Seric } 36656764Seric else if (cmntcnt > 0) 36756764Seric { 36858151Seric usrerr("553 Unbalanced '('"); 36956764Seric c = ')'; 37056764Seric } 37156764Seric else if (anglecnt > 0) 37256764Seric { 37356764Seric c = '>'; 37458151Seric usrerr("553 Unbalanced '<'"); 37556764Seric } 37656764Seric else 37756764Seric break; 37815284Seric 37956764Seric p--; 38056764Seric } 38157631Seric else if (c == delim && anglecnt <= 0 && 38257631Seric cmntcnt <= 0 && state != QST) 38357631Seric break; 38456764Seric 3858078Seric if (tTd(22, 101)) 3868078Seric printf("c=%c, s=%d; ", c, state); 3878078Seric 3883149Seric /* chew up special characters */ 3893149Seric *q = '\0'; 3903149Seric if (bslashmode) 3913149Seric { 392*59105Seric bslashmode = FALSE; 393*59105Seric 39424944Seric /* kludge \! for naive users */ 39558061Seric if (cmntcnt > 0) 396*59105Seric { 39758061Seric c = NOCHAR; 398*59105Seric continue; 399*59105Seric } 400*59105Seric else if (c != '!' || state == QST) 401*59105Seric { 40256678Seric *q++ = '\\'; 403*59105Seric continue; 404*59105Seric } 4053149Seric } 40656678Seric 40756678Seric if (c == '\\') 4083149Seric { 4093149Seric bslashmode = TRUE; 4108078Seric c = NOCHAR; 41156678Seric continue; 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 { 42658151Seric usrerr("553 Unbalanced ')'"); 42758333Seric if (delimptr != NULL) 42858333Seric *delimptr = p; 4298078Seric return (NULL); 4303149Seric } 4318078Seric else 4328078Seric cmntcnt--; 4338078Seric } 4348078Seric else if (cmntcnt > 0) 4358078Seric c = NOCHAR; 4368423Seric else if (c == '<') 4378423Seric anglecnt++; 4388423Seric else if (c == '>') 4398423Seric { 4408423Seric if (anglecnt <= 0) 4418423Seric { 44258151Seric usrerr("553 Unbalanced '>'"); 44358333Seric if (delimptr != NULL) 44458333Seric *delimptr = p; 4458423Seric return (NULL); 4468423Seric } 4478423Seric anglecnt--; 4488423Seric } 44958050Seric else if (delim == ' ' && isascii(c) && isspace(c)) 45011423Seric c = ' '; 4513149Seric 4528078Seric if (c == NOCHAR) 4538078Seric continue; 4543149Seric 4558078Seric /* see if this is end of input */ 45611405Seric if (c == delim && anglecnt <= 0 && state != QST) 4573149Seric break; 4583149Seric 4598078Seric newstate = StateTab[state][toktype(c)]; 4608078Seric if (tTd(22, 101)) 4618078Seric printf("ns=%02o\n", newstate); 4628078Seric state = newstate & TYPE; 4638078Seric if (bitset(M, newstate)) 4648078Seric c = NOCHAR; 4658078Seric if (bitset(B, newstate)) 4664228Seric break; 467297Seric } 4683149Seric 4693149Seric /* new token */ 4708078Seric if (tok != q) 4711378Seric { 4728078Seric *q++ = '\0'; 4738078Seric if (tTd(22, 36)) 474297Seric { 4758078Seric printf("tok="); 4768078Seric xputs(tok); 47723109Seric (void) putchar('\n'); 478297Seric } 4798078Seric if (avp >= &av[MAXATOM]) 480297Seric { 48158151Seric syserr("553 prescan: too many tokens"); 48258333Seric if (delimptr != NULL) 48358333Seric *delimptr = p; 4848078Seric return (NULL); 485297Seric } 4868078Seric *avp++ = tok; 487297Seric } 4888423Seric } while (c != '\0' && (c != delim || anglecnt > 0)); 4893149Seric *avp = NULL; 49058333Seric p--; 49158333Seric if (delimptr != NULL) 49258333Seric *delimptr = p; 49356764Seric if (tTd(22, 12)) 49456764Seric { 49556764Seric printf("prescan==>"); 49656764Seric printav(av); 49756764Seric } 49858546Seric if (av[0] == NULL) 49958546Seric return (NULL); 50058403Seric return (av); 5013149Seric } 5023149Seric /* 5033149Seric ** TOKTYPE -- return token type 5043149Seric ** 5053149Seric ** Parameters: 5063149Seric ** c -- the character in question. 5073149Seric ** 5083149Seric ** Returns: 5093149Seric ** Its type. 5103149Seric ** 5113149Seric ** Side Effects: 5123149Seric ** none. 5133149Seric */ 514297Seric 5153149Seric toktype(c) 51658050Seric register int c; 5173149Seric { 5183380Seric static char buf[50]; 5193382Seric static bool firstime = TRUE; 5203380Seric 5213382Seric if (firstime) 5223380Seric { 5233382Seric firstime = FALSE; 52458050Seric expand("\201o", buf, &buf[sizeof buf - 1], CurEnv); 5257005Seric (void) strcat(buf, DELIMCHARS); 5263380Seric } 52758050Seric c &= 0377; 52856327Seric if (c == MATCHCLASS || c == MATCHREPL || c == MATCHNCLASS) 5298078Seric return (ONE); 53059027Seric if (c == MACRODEXPAND) 53159027Seric return (ONE); 5328078Seric if (c == '"') 5338078Seric return (QST); 53458050Seric if ((c & 0340) == 0200) 53558050Seric return (OPR); 5364100Seric if (!isascii(c)) 5378078Seric return (ATM); 5388078Seric if (isspace(c) || c == ')') 5398078Seric return (SPC); 54058050Seric if (strchr(buf, c) != NULL) 5418078Seric return (OPR); 5428078Seric return (ATM); 5433149Seric } 5443149Seric /* 5453149Seric ** REWRITE -- apply rewrite rules to token vector. 5463149Seric ** 5474476Seric ** This routine is an ordered production system. Each rewrite 5484476Seric ** rule has a LHS (called the pattern) and a RHS (called the 5494476Seric ** rewrite); 'rwr' points the the current rewrite rule. 5504476Seric ** 5514476Seric ** For each rewrite rule, 'avp' points the address vector we 5524476Seric ** are trying to match against, and 'pvp' points to the pattern. 5538058Seric ** If pvp points to a special match value (MATCHZANY, MATCHANY, 5549585Seric ** MATCHONE, MATCHCLASS, MATCHNCLASS) then the address in avp 5559585Seric ** matched is saved away in the match vector (pointed to by 'mvp'). 5564476Seric ** 5574476Seric ** When a match between avp & pvp does not match, we try to 5589585Seric ** back out. If we back up over MATCHONE, MATCHCLASS, or MATCHNCLASS 5594476Seric ** we must also back out the match in mvp. If we reach a 5608058Seric ** MATCHANY or MATCHZANY we just extend the match and start 5618058Seric ** over again. 5624476Seric ** 5634476Seric ** When we finally match, we rewrite the address vector 5644476Seric ** and try over again. 5654476Seric ** 5663149Seric ** Parameters: 5673149Seric ** pvp -- pointer to token vector. 56859027Seric ** ruleset -- the ruleset to use for rewriting. 56959027Seric ** e -- the current envelope. 5703149Seric ** 5713149Seric ** Returns: 57259084Seric ** A status code. If EX_TEMPFAIL, higher level code should 57359084Seric ** attempt recovery. 5743149Seric ** 5753149Seric ** Side Effects: 5763149Seric ** pvp is modified. 5773149Seric */ 5782091Seric 5793149Seric struct match 5803149Seric { 5814468Seric char **first; /* first token matched */ 5824468Seric char **last; /* last token matched */ 58358825Seric char **pattern; /* pointer to pattern */ 5843149Seric }; 5853149Seric 5864468Seric # define MAXMATCH 9 /* max params per rewrite */ 5873149Seric 5883149Seric 58959084Seric int 59059027Seric rewrite(pvp, ruleset, e) 5913149Seric char **pvp; 5924070Seric int ruleset; 59359027Seric register ENVELOPE *e; 5943149Seric { 5953149Seric register char *ap; /* address pointer */ 5963149Seric register char *rp; /* rewrite pointer */ 5973149Seric register char **avp; /* address vector pointer */ 5983149Seric register char **rvp; /* rewrite vector pointer */ 5998058Seric register struct match *mlp; /* cur ptr into mlist */ 6008058Seric register struct rewrite *rwr; /* pointer to current rewrite rule */ 60158866Seric int ruleno; /* current rule number */ 60259084Seric int rstat = EX_OK; /* return status */ 60356678Seric struct match mlist[MAXMATCH]; /* stores match on LHS */ 6043149Seric char *npvp[MAXATOM+1]; /* temporary space for rebuild */ 60559027Seric extern char *macvalue(); 6063149Seric 6079279Seric if (OpMode == MD_TEST || tTd(21, 2)) 6083149Seric { 6098959Seric printf("rewrite: ruleset %2d input:", ruleset); 61056678Seric printav(pvp); 6113149Seric } 61256678Seric if (ruleset < 0 || ruleset >= MAXRWSETS) 61356326Seric { 61458151Seric syserr("554 rewrite: illegal ruleset number %d", ruleset); 61559084Seric return EX_CONFIG; 61656326Seric } 61756678Seric if (pvp == NULL) 61859084Seric return EX_USAGE; 61956326Seric 6203149Seric /* 62156678Seric ** Run through the list of rewrite rules, applying 62256678Seric ** any that match. 6233149Seric */ 6243149Seric 62558866Seric ruleno = 1; 6264070Seric for (rwr = RewriteRules[ruleset]; rwr != NULL; ) 6273149Seric { 62856678Seric int loopcount = 0; 62956678Seric 6307675Seric if (tTd(21, 12)) 631297Seric { 6328069Seric printf("-----trying rule:"); 63356678Seric printav(rwr->r_lhs); 6343149Seric } 6353149Seric 6363149Seric /* try to match on this rule */ 6374468Seric mlp = mlist; 6388058Seric rvp = rwr->r_lhs; 6398058Seric avp = pvp; 64058866Seric if (++loopcount > 100) 6413149Seric { 64258866Seric syserr("554 Infinite loop in ruleset %d, rule %d", 64358866Seric ruleset, ruleno); 64458866Seric if (tTd(21, 1)) 64552637Seric { 64656678Seric printf("workspace: "); 64756678Seric printav(pvp); 64852637Seric } 64958866Seric break; 65058866Seric } 65158866Seric 65258866Seric while ((ap = *avp) != NULL || *rvp != NULL) 65358866Seric { 6543149Seric rp = *rvp; 6558058Seric if (tTd(21, 35)) 6568058Seric { 65758825Seric printf("ADVANCE rp="); 65857531Seric xputs(rp); 65957532Seric printf(", ap="); 6608058Seric xputs(ap); 6618069Seric printf("\n"); 6628058Seric } 66356678Seric if (rp == NULL) 66456326Seric { 6653149Seric /* end-of-pattern before end-of-address */ 6668058Seric goto backup; 66756678Seric } 66858173Seric if (ap == NULL && (*rp & 0377) != MATCHZANY && 66958827Seric (*rp & 0377) != MATCHZERO) 67056326Seric { 67158825Seric /* end-of-input with patterns left */ 67258825Seric goto backup; 673297Seric } 67456326Seric 67558050Seric switch (*rp & 0377) 6768058Seric { 67756678Seric register STAB *s; 67858814Seric char buf[MAXLINE]; 67956326Seric 68056678Seric case MATCHCLASS: 68158825Seric /* match any phrase in a class */ 68258825Seric mlp->pattern = rvp; 68358814Seric mlp->first = avp; 68458814Seric extendclass: 68558825Seric ap = *avp; 68658825Seric if (ap == NULL) 68758814Seric goto backup; 68858814Seric mlp->last = avp++; 68958814Seric cataddr(mlp->first, mlp->last, buf, sizeof buf, '\0'); 69058814Seric s = stab(buf, ST_CLASS, ST_FIND); 69156678Seric if (s == NULL || !bitnset(rp[1], s->s_class)) 69256326Seric { 69358825Seric if (tTd(21, 36)) 69458825Seric { 69558825Seric printf("EXTEND rp="); 69658825Seric xputs(rp); 69758825Seric printf(", ap="); 69858825Seric xputs(ap); 69958825Seric printf("\n"); 70058825Seric } 70158825Seric goto extendclass; 70256326Seric } 70358825Seric if (tTd(21, 36)) 70458825Seric printf("CLMATCH\n"); 70558814Seric mlp++; 70658814Seric break; 7074060Seric 70858825Seric case MATCHNCLASS: 70958825Seric /* match any token not in a class */ 71058825Seric s = stab(ap, ST_CLASS, ST_FIND); 71158825Seric if (s != NULL && bitnset(rp[1], s->s_class)) 71258825Seric goto backup; 71358825Seric 71458825Seric /* fall through */ 71558825Seric 71656678Seric case MATCHONE: 71756678Seric case MATCHANY: 71856678Seric /* match exactly one token */ 71958825Seric mlp->pattern = rvp; 72056678Seric mlp->first = avp; 72156678Seric mlp->last = avp++; 7228058Seric mlp++; 72356678Seric break; 7248058Seric 72556678Seric case MATCHZANY: 72656678Seric /* match zero or more tokens */ 72758825Seric mlp->pattern = rvp; 72856678Seric mlp->first = avp; 72956678Seric mlp->last = avp - 1; 73056678Seric mlp++; 73156678Seric break; 73256326Seric 73358827Seric case MATCHZERO: 73458173Seric /* match zero tokens */ 73558173Seric break; 73658173Seric 73759027Seric case MACRODEXPAND: 73859027Seric /* 73959027Seric ** Match against run-time macro. 74059027Seric ** This algorithm is broken for the 74159027Seric ** general case (no recursive macros, 74259027Seric ** improper tokenization) but should 74359027Seric ** work for the usual cases. 74459027Seric */ 74559027Seric 74659027Seric ap = macvalue(rp[1], e); 74759027Seric mlp->first = avp; 74859027Seric if (tTd(21, 2)) 74959027Seric printf("rewrite: LHS $&%c => \"%s\"\n", 75059027Seric rp[1], 75159027Seric ap == NULL ? "(NULL)" : ap); 75259027Seric 75359027Seric if (ap == NULL) 75459027Seric break; 75559027Seric while (*ap != NULL) 75659027Seric { 75759027Seric if (*avp == NULL || 75859027Seric strncasecmp(ap, *avp, strlen(*avp)) != 0) 75959027Seric { 76059027Seric /* no match */ 76159027Seric avp = mlp->first; 76259027Seric goto backup; 76359027Seric } 76459027Seric ap += strlen(*avp++); 76559027Seric } 76659027Seric 76759027Seric /* match */ 76859027Seric break; 76959027Seric 77056678Seric default: 77156678Seric /* must have exact match */ 77256678Seric if (strcasecmp(rp, ap)) 7738058Seric goto backup; 7744468Seric avp++; 77556678Seric break; 7763149Seric } 7773149Seric 77856678Seric /* successful match on this token */ 7793149Seric rvp++; 7803149Seric continue; 7813149Seric 78258825Seric backup: 78356678Seric /* match failed -- back up */ 78458825Seric while (--mlp >= mlist) 7853149Seric { 78658825Seric rvp = mlp->pattern; 78756678Seric rp = *rvp; 78858825Seric avp = mlp->last + 1; 78958825Seric ap = *avp; 79058825Seric 79158825Seric if (tTd(21, 36)) 79258825Seric { 79358825Seric printf("BACKUP rp="); 79458825Seric xputs(rp); 79558825Seric printf(", ap="); 79658825Seric xputs(ap); 79758825Seric printf("\n"); 79858825Seric } 79958825Seric 80058825Seric if (ap == NULL) 80158825Seric { 80258825Seric /* run off the end -- back up again */ 80358825Seric continue; 80458825Seric } 80558050Seric if ((*rp & 0377) == MATCHANY || 80658050Seric (*rp & 0377) == MATCHZANY) 8074468Seric { 80856678Seric /* extend binding and continue */ 80958825Seric mlp->last = avp++; 81056678Seric rvp++; 81158825Seric mlp++; 81256678Seric break; 8134468Seric } 81458825Seric if ((*rp & 0377) == MATCHCLASS) 81556678Seric { 81658814Seric /* extend binding and try again */ 81758825Seric mlp->last = avp++; 81858814Seric goto extendclass; 81958814Seric } 8203149Seric } 8213149Seric 82258825Seric if (mlp < mlist) 82356678Seric { 82456678Seric /* total failure to match */ 82556326Seric break; 8263149Seric } 827297Seric } 8283149Seric 8293149Seric /* 83056678Seric ** See if we successfully matched 8313149Seric */ 8323149Seric 83358827Seric if (mlp < mlist || *rvp != NULL) 8343149Seric { 8359374Seric if (tTd(21, 10)) 8369374Seric printf("----- rule fails\n"); 8379374Seric rwr = rwr->r_next; 83858866Seric ruleno++; 8399374Seric continue; 8409374Seric } 8413149Seric 8429374Seric rvp = rwr->r_rhs; 8439374Seric if (tTd(21, 12)) 8449374Seric { 8459374Seric printf("-----rule matches:"); 84656678Seric printav(rvp); 8479374Seric } 8489374Seric 8499374Seric rp = *rvp; 85058050Seric if ((*rp & 0377) == CANONUSER) 8519374Seric { 8529374Seric rvp++; 8539374Seric rwr = rwr->r_next; 85458866Seric ruleno++; 8559374Seric } 85658050Seric else if ((*rp & 0377) == CANONHOST) 8579374Seric { 8589374Seric rvp++; 8599374Seric rwr = NULL; 8609374Seric } 86158050Seric else if ((*rp & 0377) == CANONNET) 8629374Seric rwr = NULL; 8639374Seric 8649374Seric /* substitute */ 8659374Seric for (avp = npvp; *rvp != NULL; rvp++) 8669374Seric { 8679374Seric register struct match *m; 8689374Seric register char **pp; 8699374Seric 8708058Seric rp = *rvp; 87158050Seric if ((*rp & 0377) == MATCHREPL) 8728058Seric { 87316914Seric /* substitute from LHS */ 87416914Seric m = &mlist[rp[1] - '1']; 87556678Seric if (m < mlist || m >= mlp) 8769374Seric { 87758151Seric syserr("554 rewrite: ruleset %d: replacement $%c out of bounds", 87856326Seric ruleset, rp[1]); 87959084Seric return EX_CONFIG; 8809374Seric } 88116914Seric if (tTd(21, 15)) 88216914Seric { 88316914Seric printf("$%c:", rp[1]); 88416914Seric pp = m->first; 88556678Seric while (pp <= m->last) 88616914Seric { 88716914Seric printf(" %x=\"", *pp); 88816914Seric (void) fflush(stdout); 88916914Seric printf("%s\"", *pp++); 89016914Seric } 89116914Seric printf("\n"); 89216914Seric } 8939374Seric pp = m->first; 89456678Seric while (pp <= m->last) 8953149Seric { 89616914Seric if (avp >= &npvp[MAXATOM]) 89756678Seric { 89858151Seric syserr("554 rewrite: expansion too long"); 89959084Seric return EX_DATAERR; 90056678Seric } 90116914Seric *avp++ = *pp++; 9023149Seric } 9033149Seric } 90416914Seric else 9058226Seric { 90616914Seric /* vanilla replacement */ 9079374Seric if (avp >= &npvp[MAXATOM]) 90816889Seric { 90956678Seric toolong: 91058151Seric syserr("554 rewrite: expansion too long"); 91159084Seric return EX_DATAERR; 91216889Seric } 91359027Seric if ((*rp & 0377) != MACRODEXPAND) 91459027Seric *avp++ = rp; 91559027Seric else 91659027Seric { 91759027Seric *avp = macvalue(rp[1], e); 91859027Seric if (tTd(21, 2)) 91959027Seric printf("rewrite: RHS $&%c => \"%s\"\n", 92059027Seric rp[1], 92159027Seric *avp == NULL ? "(NULL)" : *avp); 92259027Seric if (*avp != NULL) 92359027Seric avp++; 92459027Seric } 9258226Seric } 9269374Seric } 9279374Seric *avp++ = NULL; 92816914Seric 92916914Seric /* 93056678Seric ** Check for any hostname/keyword lookups. 93116914Seric */ 93216914Seric 93316914Seric for (rvp = npvp; *rvp != NULL; rvp++) 93416914Seric { 93556678Seric char **hbrvp; 93616914Seric char **xpvp; 93716914Seric int trsize; 93817473Seric char *olddelimchar; 93956678Seric char *replac; 94056678Seric int endtoken; 94156678Seric STAB *map; 94256678Seric char *mapname; 94356678Seric char **key_rvp; 94456678Seric char **arg_rvp; 94556678Seric char **default_rvp; 94656678Seric char buf[MAXNAME + 1]; 94716914Seric char *pvpb1[MAXATOM + 1]; 94856823Seric char *argvect[10]; 94917174Seric char pvpbuf[PSBUFSIZE]; 95016914Seric 95158050Seric if ((**rvp & 0377) != HOSTBEGIN && 95258050Seric (**rvp & 0377) != LOOKUPBEGIN) 95316914Seric continue; 95416914Seric 95516914Seric /* 95656678Seric ** Got a hostname/keyword lookup. 95716914Seric ** 95816914Seric ** This could be optimized fairly easily. 95916914Seric */ 96016914Seric 96116914Seric hbrvp = rvp; 96258050Seric if ((**rvp & 0377) == HOSTBEGIN) 96356327Seric { 96456678Seric endtoken = HOSTEND; 96556678Seric mapname = "host"; 96656327Seric } 96756326Seric else 96856327Seric { 96956678Seric endtoken = LOOKUPEND; 97056678Seric mapname = *++rvp; 97156327Seric } 97256678Seric map = stab(mapname, ST_MAP, ST_FIND); 97356678Seric if (map == NULL) 97458151Seric syserr("554 rewrite: map %s not found", mapname); 97556678Seric 97656678Seric /* extract the match part */ 97756678Seric key_rvp = ++rvp; 97856823Seric default_rvp = NULL; 97956823Seric arg_rvp = argvect; 98056823Seric xpvp = NULL; 98156823Seric replac = pvpbuf; 98258050Seric while (*rvp != NULL && (**rvp & 0377) != endtoken) 98353654Seric { 98458050Seric int nodetype = **rvp & 0377; 98556823Seric 98656823Seric if (nodetype != CANONHOST && nodetype != CANONUSER) 98756678Seric { 98856823Seric rvp++; 98956823Seric continue; 99056823Seric } 99156823Seric 99256823Seric *rvp++ = NULL; 99356823Seric 99456823Seric if (xpvp != NULL) 99556823Seric { 99658814Seric cataddr(xpvp, NULL, replac, 99758082Seric &pvpbuf[sizeof pvpbuf] - replac, 99858082Seric '\0'); 99956823Seric *++arg_rvp = replac; 100056823Seric replac += strlen(replac) + 1; 100156823Seric xpvp = NULL; 100256823Seric } 100356823Seric switch (nodetype) 100456823Seric { 100556678Seric case CANONHOST: 100656823Seric xpvp = rvp; 100756678Seric break; 100856678Seric 100956678Seric case CANONUSER: 101056678Seric default_rvp = rvp; 101156678Seric break; 101256678Seric } 101353654Seric } 101416914Seric if (*rvp != NULL) 101516914Seric *rvp++ = NULL; 101656823Seric if (xpvp != NULL) 101756823Seric { 101858814Seric cataddr(xpvp, NULL, replac, 101958082Seric &pvpbuf[sizeof pvpbuf] - replac, 102058082Seric '\0'); 102156823Seric *++arg_rvp = replac; 102256823Seric } 102356823Seric *++arg_rvp = NULL; 102416914Seric 102516914Seric /* save the remainder of the input string */ 102616914Seric trsize = (int) (avp - rvp + 1) * sizeof *rvp; 102716914Seric bcopy((char *) rvp, (char *) pvpb1, trsize); 102816914Seric 102956678Seric /* look it up */ 103058814Seric cataddr(key_rvp, NULL, buf, sizeof buf, '\0'); 103156823Seric argvect[0] = buf; 103256678Seric if (map != NULL && bitset(MF_VALID, map->s_map.map_flags)) 103356836Seric { 103456836Seric int bsize = sizeof buf - 1; 103559084Seric auto int stat = EX_OK; 103656836Seric 103756836Seric if (map->s_map.map_app != NULL) 103856836Seric bsize -= strlen(map->s_map.map_app); 103958796Seric if (tTd(60, 1)) 104058796Seric printf("map_lookup(%s, %s) => ", 104158796Seric mapname, buf); 104256836Seric replac = (*map->s_map.map_class->map_lookup)(&map->s_map, 104359084Seric buf, sizeof buf - 1, argvect, 104459084Seric &stat); 104556836Seric if (replac != NULL && map->s_map.map_app != NULL) 104656836Seric strcat(replac, map->s_map.map_app); 104758796Seric if (tTd(60, 1)) 104859084Seric printf("%s (%d)\n", 104959084Seric replac ? replac : "NOT FOUND", 105059084Seric stat); 105159084Seric 105259084Seric /* should recover if stat == EX_TEMPFAIL */ 105359084Seric if (stat == EX_TEMPFAIL) 105459084Seric rstat = stat; 105556836Seric } 105653654Seric else 105756678Seric replac = NULL; 105856678Seric 105956678Seric /* if no replacement, use default */ 106056823Seric if (replac == NULL && default_rvp != NULL) 106156823Seric { 106256823Seric char buf2[sizeof buf]; 106356823Seric 106456823Seric /* rewrite the default with % translations */ 106558814Seric cataddr(default_rvp, NULL, buf2, sizeof buf2, '\0'); 106656823Seric map_rewrite(buf2, sizeof buf2, buf, sizeof buf, 106756823Seric argvect); 106856823Seric replac = buf; 106956823Seric } 107056823Seric 107156678Seric if (replac == NULL) 107251317Seric { 107356823Seric xpvp = key_rvp; 107453654Seric } 107556678Seric else 107653654Seric { 107756678Seric /* scan the new replacement */ 107858333Seric xpvp = prescan(replac, '\0', pvpbuf, NULL); 107953654Seric if (xpvp == NULL) 108051317Seric { 108158403Seric /* prescan already printed error */ 108259084Seric return EX_DATAERR; 108356678Seric } 108451317Seric } 108551317Seric 108616914Seric /* append it to the token list */ 108756678Seric for (avp = hbrvp; *xpvp != NULL; xpvp++) 108856678Seric { 108917174Seric *avp++ = newstr(*xpvp); 109016920Seric if (avp >= &npvp[MAXATOM]) 109116914Seric goto toolong; 109217174Seric } 109316914Seric 109416914Seric /* restore the old trailing information */ 109556678Seric for (xpvp = pvpb1; (*avp++ = *xpvp++) != NULL; ) 109616920Seric if (avp >= &npvp[MAXATOM]) 109716914Seric goto toolong; 109817174Seric 109956678Seric break; 110016914Seric } 110116914Seric 110216914Seric /* 110316914Seric ** Check for subroutine calls. 110416914Seric */ 110516914Seric 110658050Seric if (*npvp != NULL && (**npvp & 0377) == CALLSUBR) 110756678Seric { 110859084Seric int stat; 110959084Seric 111056678Seric bcopy((char *) &npvp[2], (char *) pvp, 111156678Seric (int) (avp - npvp - 2) * sizeof *avp); 111256678Seric if (tTd(21, 3)) 111356678Seric printf("-----callsubr %s\n", npvp[1]); 111459084Seric stat = rewrite(pvp, atoi(npvp[1]), e); 111559084Seric if (rstat == EX_OK || stat == EX_TEMPFAIL) 111659084Seric rstat = stat; 111756678Seric } 111856678Seric else 111956678Seric { 112017348Seric bcopy((char *) npvp, (char *) pvp, 112116900Seric (int) (avp - npvp) * sizeof *avp); 112256678Seric } 11239374Seric if (tTd(21, 4)) 11249374Seric { 11259374Seric printf("rewritten as:"); 112656678Seric printav(pvp); 11279374Seric } 1128297Seric } 11298069Seric 11309279Seric if (OpMode == MD_TEST || tTd(21, 2)) 11318069Seric { 11328959Seric printf("rewrite: ruleset %2d returns:", ruleset); 113356678Seric printav(pvp); 11348069Seric } 113559084Seric 113659084Seric return rstat; 11373149Seric } 11383149Seric /* 11393149Seric ** BUILDADDR -- build address from token vector. 11403149Seric ** 11413149Seric ** Parameters: 11423149Seric ** tv -- token vector. 11433149Seric ** a -- pointer to address descriptor to fill. 11443149Seric ** If NULL, one will be allocated. 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 * 117558966Seric buildaddr(tv, a, e) 11763149Seric register char **tv; 11773149Seric register ADDRESS *a; 117858966Seric register ENVELOPE *e; 11793149Seric { 11803149Seric struct mailer **mp; 11813149Seric register struct mailer *m; 118258008Seric char *bp; 118358008Seric int spaceleft; 118457402Seric static char buf[MAXNAME]; 11853149Seric 11863149Seric if (a == NULL) 11873149Seric a = (ADDRESS *) xalloc(sizeof *a); 118816889Seric bzero((char *) a, sizeof *a); 11893149Seric 11903149Seric /* figure out what net/mailer to use */ 119158050Seric if ((**tv & 0377) != CANONNET) 11924279Seric { 119358151Seric syserr("554 buildaddr: no net"); 11944279Seric return (NULL); 11954279Seric } 11963149Seric tv++; 119758680Seric if (strcasecmp(*tv, "error") == 0) 11984279Seric { 119958050Seric if ((**++tv & 0377) == CANONHOST) 120010183Seric { 120157249Seric register struct errcodes *ep; 120257249Seric 120358050Seric if (isascii(**++tv) && isdigit(**tv)) 120457249Seric { 120557249Seric setstat(atoi(*tv)); 120657249Seric } 120757249Seric else 120857249Seric { 120957249Seric for (ep = ErrorCodes; ep->ec_name != NULL; ep++) 121057249Seric if (strcasecmp(ep->ec_name, *tv) == 0) 121157249Seric break; 121257249Seric setstat(ep->ec_code); 121357249Seric } 121410183Seric tv++; 121510183Seric } 121658050Seric if ((**tv & 0377) != CANONUSER) 121758151Seric syserr("554 buildaddr: error: no user"); 121858814Seric cataddr(++tv, NULL, buf, sizeof buf, ' '); 121958082Seric stripquotes(buf); 12204279Seric usrerr(buf); 122158966Seric if (e->e_message == NULL) 122258966Seric e->e_message = newstr(buf); 12234279Seric return (NULL); 12244279Seric } 122557402Seric 12264598Seric for (mp = Mailer; (m = *mp++) != NULL; ) 12273149Seric { 122858680Seric if (strcasecmp(m->m_name, *tv) == 0) 12293149Seric break; 12303149Seric } 12313149Seric if (m == NULL) 12324279Seric { 123358151Seric syserr("554 buildaddr: unknown mailer %s", *tv); 12344279Seric return (NULL); 12354279Seric } 12364598Seric a->q_mailer = m; 12373149Seric 12383149Seric /* figure out what host (if any) */ 123956678Seric tv++; 124058509Seric if ((**tv & 0377) == CANONHOST) 12413149Seric { 124258008Seric bp = buf; 124358008Seric spaceleft = sizeof buf - 1; 124458050Seric while (*++tv != NULL && (**tv & 0377) != CANONUSER) 124558008Seric { 124658008Seric int i = strlen(*tv); 124758008Seric 124858008Seric if (i > spaceleft) 124958008Seric { 125058008Seric /* out of space for this address */ 125158008Seric if (spaceleft >= 0) 125258151Seric syserr("554 buildaddr: host too long (%.40s...)", 125358008Seric buf); 125458008Seric i = spaceleft; 125558008Seric spaceleft = 0; 125658008Seric } 125758008Seric if (i <= 0) 125858008Seric continue; 125958008Seric bcopy(*tv, bp, i); 126058008Seric bp += i; 126158008Seric spaceleft -= i; 126258008Seric } 126358010Seric *bp = '\0'; 12645704Seric a->q_host = newstr(buf); 12653149Seric } 126657249Seric else 126758509Seric { 126858509Seric if (!bitnset(M_LOCALMAILER, m->m_flags)) 126958509Seric { 127058509Seric syserr("554 buildaddr: no host"); 127158509Seric return (NULL); 127258509Seric } 127357249Seric a->q_host = NULL; 127458509Seric } 12753149Seric 12763149Seric /* figure out the user */ 127758050Seric if (*tv == NULL || (**tv & 0377) != CANONUSER) 12784279Seric { 127958151Seric syserr("554 buildaddr: no user"); 12804279Seric return (NULL); 12814279Seric } 128257402Seric tv++; 128351317Seric 128457402Seric /* do special mapping for local mailer */ 128557402Seric if (m == LocalMailer && *tv != NULL) 128657402Seric { 128757454Seric register char *p = *tv; 128857454Seric 128957454Seric if (*p == '"') 129057454Seric p++; 129157454Seric if (*p == '|') 129257402Seric a->q_mailer = m = ProgMailer; 129357454Seric else if (*p == '/') 129457402Seric a->q_mailer = m = FileMailer; 129557454Seric else if (*p == ':') 129657402Seric { 129757402Seric /* may be :include: */ 129858814Seric cataddr(tv, NULL, buf, sizeof buf, '\0'); 129958008Seric stripquotes(buf); 130058008Seric if (strncasecmp(buf, ":include:", 9) == 0) 130158008Seric { 130258008Seric /* if :include:, don't need further rewriting */ 130357402Seric a->q_mailer = m = InclMailer; 130458008Seric a->q_user = &buf[9]; 130558008Seric return (a); 130658008Seric } 130757402Seric } 130857402Seric } 130957402Seric 131058008Seric if (m == LocalMailer && *tv != NULL && strcmp(*tv, "@") == 0) 131158008Seric { 131258008Seric tv++; 131358008Seric a->q_flags |= QNOTREMOTE; 131458008Seric } 131558008Seric 131658673Seric /* do cleanup of final address */ 131759084Seric (void) rewrite(tv, 4, e); 131819040Seric 131919040Seric /* save the result for the command line/RCPT argument */ 132058814Seric cataddr(tv, NULL, buf, sizeof buf, '\0'); 13213149Seric a->q_user = buf; 13223149Seric 132358670Seric /* 132458670Seric ** Do mapping to lower case as requested by mailer 132558670Seric */ 132658670Seric 132758670Seric if (a->q_host != NULL && !bitnset(M_HST_UPPER, m->m_flags)) 132858670Seric makelower(a->q_host); 132958670Seric if (!bitnset(M_USR_UPPER, m->m_flags)) 133058670Seric makelower(a->q_user); 133158670Seric 13323149Seric return (a); 13333149Seric } 13343188Seric /* 13354228Seric ** CATADDR -- concatenate pieces of addresses (putting in <LWSP> subs) 13364228Seric ** 13374228Seric ** Parameters: 13384228Seric ** pvp -- parameter vector to rebuild. 133958814Seric ** evp -- last parameter to include. Can be NULL to 134058814Seric ** use entire pvp. 13414228Seric ** buf -- buffer to build the string into. 13424228Seric ** sz -- size of buf. 134358082Seric ** spacesub -- the space separator character; if null, 134458082Seric ** use SpaceSub. 13454228Seric ** 13464228Seric ** Returns: 13474228Seric ** none. 13484228Seric ** 13494228Seric ** Side Effects: 13504228Seric ** Destroys buf. 13514228Seric */ 13524228Seric 135358814Seric cataddr(pvp, evp, buf, sz, spacesub) 13544228Seric char **pvp; 135558814Seric char **evp; 13564228Seric char *buf; 13574228Seric register int sz; 135858082Seric char spacesub; 13594228Seric { 13604228Seric bool oatomtok = FALSE; 136156678Seric bool natomtok = FALSE; 13624228Seric register int i; 13634228Seric register char *p; 13644228Seric 136558082Seric if (spacesub == '\0') 136658082Seric spacesub = SpaceSub; 136758082Seric 13688423Seric if (pvp == NULL) 13698423Seric { 137023109Seric (void) strcpy(buf, ""); 13718423Seric return; 13728423Seric } 13734228Seric p = buf; 137411156Seric sz -= 2; 13754228Seric while (*pvp != NULL && (i = strlen(*pvp)) < sz) 13764228Seric { 13778078Seric natomtok = (toktype(**pvp) == ATM); 13784228Seric if (oatomtok && natomtok) 137958082Seric *p++ = spacesub; 13804228Seric (void) strcpy(p, *pvp); 13814228Seric oatomtok = natomtok; 13824228Seric p += i; 138311156Seric sz -= i + 1; 138458814Seric if (pvp++ == evp) 138558814Seric break; 13864228Seric } 13874228Seric *p = '\0'; 13884228Seric } 13894228Seric /* 13903188Seric ** SAMEADDR -- Determine if two addresses are the same 13913188Seric ** 13923188Seric ** This is not just a straight comparison -- if the mailer doesn't 13933188Seric ** care about the host we just ignore it, etc. 13943188Seric ** 13953188Seric ** Parameters: 13963188Seric ** a, b -- pointers to the internal forms to compare. 13973188Seric ** 13983188Seric ** Returns: 13993188Seric ** TRUE -- they represent the same mailbox. 14003188Seric ** FALSE -- they don't. 14013188Seric ** 14023188Seric ** Side Effects: 14033188Seric ** none. 14043188Seric */ 14053188Seric 14063188Seric bool 14079374Seric sameaddr(a, b) 14083188Seric register ADDRESS *a; 14093188Seric register ADDRESS *b; 14103188Seric { 14113188Seric /* if they don't have the same mailer, forget it */ 14123188Seric if (a->q_mailer != b->q_mailer) 14133188Seric return (FALSE); 14143188Seric 14153188Seric /* if the user isn't the same, we can drop out */ 141656678Seric if (strcmp(a->q_user, b->q_user) != 0) 14173188Seric return (FALSE); 14183188Seric 141958438Seric /* if we have good uids for both but the differ, these are different */ 142058438Seric if (bitset(QGOODUID, a->q_flags & b->q_flags) && a->q_uid != b->q_uid) 142158438Seric return (FALSE); 142258438Seric 142358509Seric /* otherwise compare hosts (but be careful for NULL ptrs) */ 142458509Seric if (a->q_host == b->q_host) 142558509Seric { 142658509Seric /* probably both null pointers */ 14273188Seric return (TRUE); 142858509Seric } 14293188Seric if (a->q_host == NULL || b->q_host == NULL) 143058509Seric { 143158509Seric /* only one is a null pointer */ 14323188Seric return (FALSE); 143358509Seric } 143456678Seric if (strcmp(a->q_host, b->q_host) != 0) 14353188Seric return (FALSE); 14363188Seric 14373188Seric return (TRUE); 14383188Seric } 14393234Seric /* 14403234Seric ** PRINTADDR -- print address (for debugging) 14413234Seric ** 14423234Seric ** Parameters: 14433234Seric ** a -- the address to print 14443234Seric ** follow -- follow the q_next chain. 14453234Seric ** 14463234Seric ** Returns: 14473234Seric ** none. 14483234Seric ** 14493234Seric ** Side Effects: 14503234Seric ** none. 14513234Seric */ 14523234Seric 14533234Seric printaddr(a, follow) 14543234Seric register ADDRESS *a; 14553234Seric bool follow; 14563234Seric { 14575001Seric bool first = TRUE; 145857731Seric register MAILER *m; 145957731Seric MAILER pseudomailer; 14605001Seric 14613234Seric while (a != NULL) 14623234Seric { 14635001Seric first = FALSE; 14644443Seric printf("%x=", a); 14654085Seric (void) fflush(stdout); 146657731Seric 146757731Seric /* find the mailer -- carefully */ 146857731Seric m = a->q_mailer; 146957731Seric if (m == NULL) 147057731Seric { 147157731Seric m = &pseudomailer; 147257731Seric m->m_mno = -1; 147357731Seric m->m_name = "NULL"; 147457731Seric } 147557731Seric 147658680Seric printf("%s:\n\tmailer %d (%s), host `%s', user `%s', ruser `%s'\n", 147757731Seric a->q_paddr, m->m_mno, m->m_name, 147840973Sbostic a->q_host, a->q_user, a->q_ruser? a->q_ruser: "<null>"); 14798181Seric printf("\tnext=%x, flags=%o, alias %x\n", a->q_next, a->q_flags, 14808181Seric a->q_alias); 14818181Seric printf("\thome=\"%s\", fullname=\"%s\"\n", a->q_home, 14828181Seric a->q_fullname); 14834996Seric 14843234Seric if (!follow) 14853234Seric return; 14864996Seric a = a->q_next; 14873234Seric } 14885001Seric if (first) 14894443Seric printf("[NULL]\n"); 14903234Seric } 14914317Seric 14927682Seric /* 14937682Seric ** REMOTENAME -- return the name relative to the current mailer 14947682Seric ** 14957682Seric ** Parameters: 14967682Seric ** name -- the name to translate. 14978069Seric ** m -- the mailer that we want to do rewriting relative 14988069Seric ** to. 14998069Seric ** senderaddress -- if set, uses the sender rewriting rules 15008069Seric ** rather than the recipient rewriting rules. 150158020Seric ** header -- set if this address is in the header, rather 150258020Seric ** than an envelope header. 150310310Seric ** canonical -- if set, strip out any comment information, 150410310Seric ** etc. 150558509Seric ** adddomain -- if set, OK to do domain extension. 150658020Seric ** e -- the current envelope. 15077682Seric ** 15087682Seric ** Returns: 15097682Seric ** the text string representing this address relative to 15107682Seric ** the receiving mailer. 15117682Seric ** 15127682Seric ** Side Effects: 15137682Seric ** none. 15147682Seric ** 15157682Seric ** Warnings: 15167682Seric ** The text string returned is tucked away locally; 15177682Seric ** copy it if you intend to save it. 15187682Seric */ 15197682Seric 15207682Seric char * 152158509Seric remotename(name, m, senderaddress, header, canonical, adddomain, e) 15227682Seric char *name; 152356678Seric struct mailer *m; 15248069Seric bool senderaddress; 152558020Seric bool header; 152610310Seric bool canonical; 152758509Seric bool adddomain; 152856678Seric register ENVELOPE *e; 15297682Seric { 15308069Seric register char **pvp; 15318069Seric char *fancy; 153256678Seric extern char *macvalue(); 153356678Seric char *oldg = macvalue('g', e); 153458020Seric int rwset; 15357682Seric static char buf[MAXNAME]; 15367682Seric char lbuf[MAXNAME]; 153716914Seric char pvpbuf[PSBUFSIZE]; 153856678Seric extern char **prescan(); 153956678Seric extern char *crackaddr(); 15407682Seric 15417755Seric if (tTd(12, 1)) 15427755Seric printf("remotename(%s)\n", name); 15437755Seric 154410177Seric /* don't do anything if we are tagging it as special */ 154558020Seric if (senderaddress) 154658020Seric rwset = header ? m->m_sh_rwset : m->m_se_rwset; 154758020Seric else 154858020Seric rwset = header ? m->m_rh_rwset : m->m_re_rwset; 154958020Seric if (rwset < 0) 155010177Seric return (name); 155110177Seric 15527682Seric /* 15538181Seric ** Do a heuristic crack of this name to extract any comment info. 15548181Seric ** This will leave the name as a comment and a $g macro. 15557889Seric */ 15567889Seric 155758036Seric if (canonical || bitnset(M_NOCOMMENT, m->m_flags)) 155858050Seric fancy = "\201g"; 155910310Seric else 156010310Seric fancy = crackaddr(name); 15617889Seric 15628181Seric /* 15638181Seric ** Turn the name into canonical form. 15648181Seric ** Normally this will be RFC 822 style, i.e., "user@domain". 15658181Seric ** If this only resolves to "user", and the "C" flag is 15668181Seric ** specified in the sending mailer, then the sender's 15678181Seric ** domain will be appended. 15688181Seric */ 15698181Seric 157058333Seric pvp = prescan(name, '\0', pvpbuf, NULL); 15717889Seric if (pvp == NULL) 15727889Seric return (name); 157359084Seric (void) rewrite(pvp, 3, e); 157458509Seric if (adddomain && e->e_fromdomain != NULL) 15758181Seric { 15768181Seric /* append from domain to this address */ 15778181Seric register char **pxp = pvp; 15788181Seric 15799594Seric /* see if there is an "@domain" in the current name */ 15808181Seric while (*pxp != NULL && strcmp(*pxp, "@") != 0) 15818181Seric pxp++; 15828181Seric if (*pxp == NULL) 15838181Seric { 15849594Seric /* no.... append the "@domain" from the sender */ 158556678Seric register char **qxq = e->e_fromdomain; 15868181Seric 15879594Seric while ((*pxp++ = *qxq++) != NULL) 15889594Seric continue; 158959084Seric (void) rewrite(pvp, 3, e); 15908181Seric } 15918181Seric } 15928181Seric 15938181Seric /* 15948959Seric ** Do more specific rewriting. 159556678Seric ** Rewrite using ruleset 1 or 2 depending on whether this is 159656678Seric ** a sender address or not. 15978181Seric ** Then run it through any receiving-mailer-specific rulesets. 15988181Seric */ 15998181Seric 16008069Seric if (senderaddress) 160159084Seric (void) rewrite(pvp, 1, e); 16028069Seric else 160359084Seric (void) rewrite(pvp, 2, e); 160458020Seric if (rwset > 0) 160559084Seric (void) rewrite(pvp, rwset, e); 16067682Seric 16078181Seric /* 16088959Seric ** Do any final sanitation the address may require. 16098959Seric ** This will normally be used to turn internal forms 16108959Seric ** (e.g., user@host.LOCAL) into external form. This 16118959Seric ** may be used as a default to the above rules. 16128959Seric */ 16138959Seric 161459084Seric (void) rewrite(pvp, 4, e); 16158959Seric 16168959Seric /* 16178181Seric ** Now restore the comment information we had at the beginning. 16188181Seric */ 16198181Seric 162058825Seric cataddr(pvp, NULL, lbuf, sizeof lbuf, '\0'); 162156678Seric define('g', lbuf, e); 162256678Seric expand(fancy, buf, &buf[sizeof buf - 1], e); 162356678Seric define('g', oldg, e); 16247682Seric 16257682Seric if (tTd(12, 1)) 16267755Seric printf("remotename => `%s'\n", buf); 16277682Seric return (buf); 16287682Seric } 162951317Seric /* 163056678Seric ** MAPLOCALUSER -- run local username through ruleset 5 for final redirection 163151317Seric ** 163251317Seric ** Parameters: 163356678Seric ** a -- the address to map (but just the user name part). 163456678Seric ** sendq -- the sendq in which to install any replacement 163556678Seric ** addresses. 163651317Seric ** 163751317Seric ** Returns: 163851317Seric ** none. 163951317Seric */ 164051317Seric 164156678Seric maplocaluser(a, sendq, e) 164256678Seric register ADDRESS *a; 164356678Seric ADDRESS **sendq; 164456678Seric ENVELOPE *e; 164551317Seric { 164656678Seric register char **pvp; 164756678Seric register ADDRESS *a1 = NULL; 164858333Seric auto char *delimptr; 164956678Seric char pvpbuf[PSBUFSIZE]; 165051317Seric 165156678Seric if (tTd(29, 1)) 165256678Seric { 165356678Seric printf("maplocaluser: "); 165456678Seric printaddr(a, FALSE); 165556678Seric } 165658333Seric pvp = prescan(a->q_user, '\0', pvpbuf, &delimptr); 165756678Seric if (pvp == NULL) 165856678Seric return; 165951317Seric 166059084Seric (void) rewrite(pvp, 5, e); 166158050Seric if (pvp[0] == NULL || (pvp[0][0] & 0377) != CANONNET) 166256678Seric return; 166351317Seric 166456678Seric /* if non-null, mailer destination specified -- has it changed? */ 166558966Seric a1 = buildaddr(pvp, NULL, e); 166656678Seric if (a1 == NULL || sameaddr(a, a1)) 166756678Seric return; 166851317Seric 166956678Seric /* mark old address as dead; insert new address */ 167056678Seric a->q_flags |= QDONTSEND; 167157731Seric if (tTd(29, 5)) 167257731Seric { 167357731Seric printf("maplocaluser: QDONTSEND "); 167457731Seric printaddr(a, FALSE); 167557731Seric } 167656678Seric a1->q_alias = a; 167758333Seric allocaddr(a1, 1, NULL, delimptr); 167856678Seric (void) recipient(a1, sendq, e); 167951317Seric } 168058802Seric /* 168158802Seric ** DEQUOTE_INIT -- initialize dequote map 168258802Seric ** 168358802Seric ** This is a no-op. 168458802Seric ** 168558802Seric ** Parameters: 168658802Seric ** map -- the internal map structure. 168758802Seric ** mapname -- the name of the mapl. 168858802Seric ** args -- arguments. 168958802Seric ** 169058802Seric ** Returns: 169158802Seric ** TRUE. 169258802Seric */ 169358802Seric 169458802Seric bool 169558802Seric dequote_init(map, mapname, args) 169658802Seric MAP *map; 169758802Seric char *mapname; 169858802Seric char *args; 169958802Seric { 170058805Seric register char *p = args; 170158805Seric 170258805Seric for (;;) 170358805Seric { 170458805Seric while (isascii(*p) && isspace(*p)) 170558805Seric p++; 170658805Seric if (*p != '-') 170758805Seric break; 170858805Seric switch (*++p) 170958805Seric { 171058805Seric case 'a': 171158805Seric map->map_app = ++p; 171258805Seric break; 171358805Seric } 171458805Seric while (*p != '\0' && !(isascii(*p) && isspace(*p))) 171558805Seric p++; 171658805Seric if (*p != '\0') 171758805Seric *p = '\0'; 171858805Seric } 171958805Seric if (map->map_app != NULL) 172058805Seric map->map_app = newstr(map->map_app); 172158805Seric 172258802Seric return TRUE; 172358802Seric } 172458802Seric /* 172558802Seric ** DEQUOTE_MAP -- unquote an address 172658802Seric ** 172758802Seric ** Parameters: 172858802Seric ** map -- the internal map structure (ignored). 172958802Seric ** buf -- the buffer to dequote. 173058802Seric ** bufsiz -- the size of that buffer. 173158802Seric ** av -- arguments (ignored). 173259084Seric ** statp -- pointer to status out-parameter. 173358802Seric ** 173458802Seric ** Returns: 173558802Seric ** NULL -- if there were no quotes, or if the resulting 173658802Seric ** unquoted buffer would not be acceptable to prescan. 173758802Seric ** else -- The dequoted buffer. 173858802Seric */ 173958802Seric 174058802Seric char * 174159084Seric dequote_map(map, buf, bufsiz, av, statp) 174258802Seric MAP *map; 174358802Seric char buf[]; 174458802Seric int bufsiz; 174558802Seric char **av; 174659084Seric int *statp; 174758802Seric { 174858802Seric register char *p; 174958802Seric register char *q; 175058802Seric register char c; 175158802Seric int anglecnt; 175258802Seric int cmntcnt; 175358802Seric int quotecnt; 175459089Seric int spacecnt; 175558802Seric bool quotemode; 175658802Seric bool bslashmode; 175758802Seric 175858802Seric anglecnt = 0; 175958802Seric cmntcnt = 0; 176058802Seric quotecnt = 0; 176159089Seric spacecnt = 0; 176258802Seric quotemode = FALSE; 176358802Seric bslashmode = FALSE; 176458802Seric 176558802Seric for (p = q = buf; (c = *p++) != '\0'; ) 176658802Seric { 176758802Seric if (bslashmode) 176858802Seric { 176958802Seric bslashmode = FALSE; 177058802Seric *q++ = c; 177158802Seric continue; 177258802Seric } 177358802Seric 177458802Seric switch (c) 177558802Seric { 177658802Seric case '\\': 177758802Seric bslashmode = TRUE; 177858802Seric break; 177958802Seric 178058802Seric case '(': 178158802Seric cmntcnt++; 178258802Seric break; 178358802Seric 178458802Seric case ')': 178558802Seric if (cmntcnt-- <= 0) 178658802Seric return NULL; 178758802Seric break; 178859089Seric 178959089Seric case ' ': 179059089Seric spacecnt++; 179159089Seric break; 179258802Seric } 179358802Seric 179458802Seric if (cmntcnt > 0) 179558802Seric { 179658802Seric *q++ = c; 179758802Seric continue; 179858802Seric } 179958802Seric 180058802Seric switch (c) 180158802Seric { 180258802Seric case '"': 180358802Seric quotemode = !quotemode; 180458802Seric quotecnt++; 180558802Seric continue; 180658802Seric 180758802Seric case '<': 180858802Seric anglecnt++; 180958802Seric break; 181058802Seric 181158802Seric case '>': 181258802Seric if (anglecnt-- <= 0) 181358802Seric return NULL; 181458802Seric break; 181558802Seric } 181658802Seric *q++ = c; 181758802Seric } 181858802Seric 181958802Seric if (anglecnt != 0 || cmntcnt != 0 || bslashmode || 182059089Seric quotemode || quotecnt <= 0 || spacecnt != 0) 182158802Seric return NULL; 182258802Seric *q++ = '\0'; 182358802Seric return buf; 182458802Seric } 1825