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*59734Seric static char sccsid[] = "@(#)parseaddr.c 6.49 (Berkeley) 05/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. 33297Seric ** copyf -- determines what shall be copied: 34297Seric ** -1 -- don't copy anything. The printname 35297Seric ** (q_paddr) is just addr, and the 36297Seric ** user & host are allocated internally 37297Seric ** to parse. 38297Seric ** 0 -- copy out the parsed user & host, but 39297Seric ** don't copy the printname. 40297Seric ** +1 -- copy everything. 4111445Seric ** delim -- the character to terminate the address, passed 4211445Seric ** to prescan. 4358333Seric ** delimptr -- if non-NULL, set to the location of the 4458333Seric ** delim character that was found. 4556678Seric ** e -- the envelope that will contain this address. 46297Seric ** 47297Seric ** Returns: 48297Seric ** A pointer to the address descriptor header (`a' if 49297Seric ** `a' is non-NULL). 50297Seric ** NULL on error. 51297Seric ** 52297Seric ** Side Effects: 53297Seric ** none 54297Seric */ 55297Seric 569374Seric /* following delimiters are inherent to the internal algorithms */ 5759278Seric # define DELIMCHARS "()<>,;\r\n" /* default word delimiters */ 582091Seric 592973Seric ADDRESS * 6058333Seric parseaddr(addr, a, copyf, delim, delimptr, e) 61297Seric char *addr; 622973Seric register ADDRESS *a; 63297Seric int copyf; 6459700Seric int 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) 14459595Seric { 145*59734Seric char *msg = "Transient parse error -- message queued for future delivery"; 146*59734Seric 14759595Seric if (tTd(20, 1)) 14859595Seric printf("parseaddr: queuing message\n"); 149*59734Seric message(msg); 150*59734Seric if (e->e_message == NULL) 151*59734Seric e->e_message = msg; 15259084Seric a->q_flags |= QQUEUEUP; 15359595Seric } 15459084Seric 15559084Seric /* 15656678Seric ** Compute return value. 15756678Seric */ 15856678Seric 15956678Seric if (tTd(20, 1)) 1608078Seric { 16156678Seric printf("parseaddr-->"); 16256678Seric printaddr(a, FALSE); 16356678Seric } 16456678Seric 16556678Seric return (a); 16656678Seric } 16756678Seric /* 16857388Seric ** INVALIDADDR -- check for address containing meta-characters 16957388Seric ** 17057388Seric ** Parameters: 17157388Seric ** addr -- the address to check. 17257388Seric ** 17357388Seric ** Returns: 17457388Seric ** TRUE -- if the address has any "wierd" characters 17557388Seric ** FALSE -- otherwise. 17657388Seric */ 17757388Seric 17857388Seric bool 17957388Seric invalidaddr(addr) 18057388Seric register char *addr; 18157388Seric { 18257388Seric for (; *addr != '\0'; addr++) 18357388Seric { 18458050Seric if ((*addr & 0340) != 0200) 18557388Seric continue; 18657388Seric setstat(EX_USAGE); 18758151Seric usrerr("553 Address contained invalid control characters"); 18857388Seric return TRUE; 18957388Seric } 19057388Seric return FALSE; 19157388Seric } 19257388Seric /* 19356678Seric ** ALLOCADDR -- do local allocations of address on demand. 19456678Seric ** 19556678Seric ** Also lowercases the host name if requested. 19656678Seric ** 19756678Seric ** Parameters: 19856678Seric ** a -- the address to reallocate. 19956678Seric ** copyf -- the copy flag (see parseaddr for description). 20056678Seric ** paddr -- the printname of the address. 20158333Seric ** delimptr -- a pointer to the address delimiter. Must be set. 20256678Seric ** 20356678Seric ** Returns: 20456678Seric ** none. 20556678Seric ** 20656678Seric ** Side Effects: 20756678Seric ** Copies portions of a into local buffers as requested. 20856678Seric */ 20956678Seric 21058333Seric allocaddr(a, copyf, paddr, delimptr) 21156678Seric register ADDRESS *a; 21256678Seric int copyf; 21356678Seric char *paddr; 21458333Seric char *delimptr; 21556678Seric { 21656678Seric register MAILER *m = a->q_mailer; 21756678Seric 21858673Seric if (tTd(24, 4)) 21958673Seric printf("allocaddr(copyf=%d, paddr=%s)\n", copyf, paddr); 22058673Seric 22156678Seric if (copyf > 0 && paddr != NULL) 22256678Seric { 22358333Seric char savec = *delimptr; 2248078Seric 22558333Seric *delimptr = '\0'; 22656678Seric a->q_paddr = newstr(paddr); 22758333Seric *delimptr = savec; 2288078Seric } 229297Seric else 23056678Seric a->q_paddr = paddr; 23124944Seric 23224944Seric if (a->q_user == NULL) 23324944Seric a->q_user = ""; 23424944Seric if (a->q_host == NULL) 23524944Seric a->q_host = ""; 23624944Seric 2373149Seric if (copyf >= 0) 238297Seric { 23924944Seric a->q_host = newstr(a->q_host); 2403149Seric if (a->q_user != a->q_paddr) 2413149Seric a->q_user = newstr(a->q_user); 242297Seric } 243297Seric 24456678Seric if (a->q_paddr == NULL) 24556678Seric a->q_paddr = a->q_user; 246297Seric } 247297Seric /* 248297Seric ** PRESCAN -- Prescan name and make it canonical 249297Seric ** 2509374Seric ** Scans a name and turns it into a set of tokens. This process 2519374Seric ** deletes blanks and comments (in parentheses). 252297Seric ** 253297Seric ** This routine knows about quoted strings and angle brackets. 254297Seric ** 255297Seric ** There are certain subtleties to this routine. The one that 256297Seric ** comes to mind now is that backslashes on the ends of names 257297Seric ** are silently stripped off; this is intentional. The problem 258297Seric ** is that some versions of sndmsg (like at LBL) set the kill 259297Seric ** character to something other than @ when reading addresses; 260297Seric ** so people type "csvax.eric\@berkeley" -- which screws up the 261297Seric ** berknet mailer. 262297Seric ** 263297Seric ** Parameters: 264297Seric ** addr -- the name to chomp. 265297Seric ** delim -- the delimiter for the address, normally 266297Seric ** '\0' or ','; \0 is accepted in any case. 26715284Seric ** If '\t' then we are reading the .cf file. 26816914Seric ** pvpbuf -- place to put the saved text -- note that 26916914Seric ** the pointers are static. 27058333Seric ** delimptr -- if non-NULL, set to the location of the 27158333Seric ** terminating delimiter. 272297Seric ** 273297Seric ** Returns: 2743149Seric ** A pointer to a vector of tokens. 275297Seric ** NULL on error. 276297Seric */ 277297Seric 2788078Seric /* states and character types */ 2798078Seric # define OPR 0 /* operator */ 2808078Seric # define ATM 1 /* atom */ 2818078Seric # define QST 2 /* in quoted string */ 2828078Seric # define SPC 3 /* chewing up spaces */ 2838078Seric # define ONE 4 /* pick up one character */ 2843149Seric 2858078Seric # define NSTATES 5 /* number of states */ 2868078Seric # define TYPE 017 /* mask to select state type */ 2878078Seric 2888078Seric /* meta bits for table */ 2898078Seric # define M 020 /* meta character; don't pass through */ 2908078Seric # define B 040 /* cause a break */ 2918078Seric # define MB M|B /* meta-break */ 2928078Seric 2938078Seric static short StateTab[NSTATES][NSTATES] = 2948078Seric { 2958087Seric /* oldst chtype> OPR ATM QST SPC ONE */ 2969051Seric /*OPR*/ OPR|B, ATM|B, QST|B, SPC|MB, ONE|B, 2979051Seric /*ATM*/ OPR|B, ATM, QST|B, SPC|MB, ONE|B, 2989051Seric /*QST*/ QST, QST, OPR, QST, QST, 2998078Seric /*SPC*/ OPR, ATM, QST, SPC|M, ONE, 3008078Seric /*ONE*/ OPR, OPR, OPR, OPR, OPR, 3018078Seric }; 3028078Seric 3038078Seric # define NOCHAR -1 /* signal nothing in lookahead token */ 3048078Seric 3053149Seric char ** 30658333Seric prescan(addr, delim, pvpbuf, delimptr) 307297Seric char *addr; 308297Seric char delim; 30916914Seric char pvpbuf[]; 31058333Seric char **delimptr; 311297Seric { 312297Seric register char *p; 3138078Seric register char *q; 3149346Seric register int c; 3153149Seric char **avp; 316297Seric bool bslashmode; 317297Seric int cmntcnt; 3188423Seric int anglecnt; 3193149Seric char *tok; 3208078Seric int state; 3218078Seric int newstate; 3228078Seric static char *av[MAXATOM+1]; 32356678Seric extern int errno; 324297Seric 32515253Seric /* make sure error messages don't have garbage on them */ 32615253Seric errno = 0; 32715253Seric 32816914Seric q = pvpbuf; 3293149Seric bslashmode = FALSE; 3307800Seric cmntcnt = 0; 3318423Seric anglecnt = 0; 3323149Seric avp = av; 33356678Seric state = ATM; 3348078Seric c = NOCHAR; 3358078Seric p = addr; 33656764Seric if (tTd(22, 11)) 337297Seric { 3388078Seric printf("prescan: "); 3398078Seric xputs(p); 34023109Seric (void) putchar('\n'); 3418078Seric } 3428078Seric 3438078Seric do 3448078Seric { 3453149Seric /* read a token */ 3463149Seric tok = q; 3478078Seric for (;;) 348297Seric { 3498078Seric /* store away any old lookahead character */ 35059277Seric if (c != NOCHAR && !bslashmode) 3518078Seric { 35215284Seric /* see if there is room */ 35316914Seric if (q >= &pvpbuf[PSBUFSIZE - 5]) 3548078Seric { 35558151Seric usrerr("553 Address too long"); 35658333Seric if (delimptr != NULL) 35758333Seric *delimptr = p; 3588078Seric return (NULL); 3598078Seric } 36015284Seric 36115284Seric /* squirrel it away */ 3628078Seric *q++ = c; 3638078Seric } 3648078Seric 3658078Seric /* read a new input character */ 3668078Seric c = *p++; 36757631Seric if (c == '\0') 36856764Seric { 36956764Seric /* diagnose and patch up bad syntax */ 37056764Seric if (state == QST) 37156764Seric { 37258151Seric usrerr("553 Unbalanced '\"'"); 37356764Seric c = '"'; 37456764Seric } 37556764Seric else if (cmntcnt > 0) 37656764Seric { 37758151Seric usrerr("553 Unbalanced '('"); 37856764Seric c = ')'; 37956764Seric } 38056764Seric else if (anglecnt > 0) 38156764Seric { 38256764Seric c = '>'; 38358151Seric usrerr("553 Unbalanced '<'"); 38456764Seric } 38556764Seric else 38656764Seric break; 38715284Seric 38856764Seric p--; 38956764Seric } 39057631Seric else if (c == delim && anglecnt <= 0 && 39157631Seric cmntcnt <= 0 && state != QST) 39257631Seric break; 39356764Seric 3948078Seric if (tTd(22, 101)) 3958078Seric printf("c=%c, s=%d; ", c, state); 3968078Seric 3973149Seric /* chew up special characters */ 3983149Seric *q = '\0'; 3993149Seric if (bslashmode) 4003149Seric { 40159105Seric bslashmode = FALSE; 40259105Seric 40324944Seric /* kludge \! for naive users */ 40458061Seric if (cmntcnt > 0) 40559105Seric { 40658061Seric c = NOCHAR; 40759105Seric continue; 40859105Seric } 40959105Seric else if (c != '!' || state == QST) 41059105Seric { 41156678Seric *q++ = '\\'; 41259105Seric continue; 41359105Seric } 4143149Seric } 41556678Seric 41656678Seric if (c == '\\') 4173149Seric { 4183149Seric bslashmode = TRUE; 4193149Seric } 42056678Seric else if (state == QST) 4218514Seric { 4228514Seric /* do nothing, just avoid next clauses */ 4238514Seric } 4248078Seric else if (c == '(') 4254100Seric { 4268078Seric cmntcnt++; 4278078Seric c = NOCHAR; 4284100Seric } 4298078Seric else if (c == ')') 4303149Seric { 4318078Seric if (cmntcnt <= 0) 4323149Seric { 43358151Seric usrerr("553 Unbalanced ')'"); 43458333Seric if (delimptr != NULL) 43558333Seric *delimptr = p; 4368078Seric return (NULL); 4373149Seric } 4388078Seric else 4398078Seric cmntcnt--; 4408078Seric } 4418078Seric else if (cmntcnt > 0) 4428078Seric c = NOCHAR; 4438423Seric else if (c == '<') 4448423Seric anglecnt++; 4458423Seric else if (c == '>') 4468423Seric { 4478423Seric if (anglecnt <= 0) 4488423Seric { 44958151Seric usrerr("553 Unbalanced '>'"); 45058333Seric if (delimptr != NULL) 45158333Seric *delimptr = p; 4528423Seric return (NULL); 4538423Seric } 4548423Seric anglecnt--; 4558423Seric } 45658050Seric else if (delim == ' ' && isascii(c) && isspace(c)) 45711423Seric c = ' '; 4583149Seric 4598078Seric if (c == NOCHAR) 4608078Seric continue; 4613149Seric 4628078Seric /* see if this is end of input */ 46311405Seric if (c == delim && anglecnt <= 0 && state != QST) 4643149Seric break; 4653149Seric 4668078Seric newstate = StateTab[state][toktype(c)]; 4678078Seric if (tTd(22, 101)) 4688078Seric printf("ns=%02o\n", newstate); 4698078Seric state = newstate & TYPE; 4708078Seric if (bitset(M, newstate)) 4718078Seric c = NOCHAR; 4728078Seric if (bitset(B, newstate)) 4734228Seric break; 474297Seric } 4753149Seric 4763149Seric /* new token */ 4778078Seric if (tok != q) 4781378Seric { 4798078Seric *q++ = '\0'; 4808078Seric if (tTd(22, 36)) 481297Seric { 4828078Seric printf("tok="); 4838078Seric xputs(tok); 48423109Seric (void) putchar('\n'); 485297Seric } 4868078Seric if (avp >= &av[MAXATOM]) 487297Seric { 48858151Seric syserr("553 prescan: too many tokens"); 48958333Seric if (delimptr != NULL) 49058333Seric *delimptr = p; 4918078Seric return (NULL); 492297Seric } 4938078Seric *avp++ = tok; 494297Seric } 4958423Seric } while (c != '\0' && (c != delim || anglecnt > 0)); 4963149Seric *avp = NULL; 49758333Seric p--; 49858333Seric if (delimptr != NULL) 49958333Seric *delimptr = p; 50056764Seric if (tTd(22, 12)) 50156764Seric { 50256764Seric printf("prescan==>"); 50356764Seric printav(av); 50456764Seric } 50558546Seric if (av[0] == NULL) 50658546Seric return (NULL); 50758403Seric return (av); 5083149Seric } 5093149Seric /* 5103149Seric ** TOKTYPE -- return token type 5113149Seric ** 5123149Seric ** Parameters: 5133149Seric ** c -- the character in question. 5143149Seric ** 5153149Seric ** Returns: 5163149Seric ** Its type. 5173149Seric ** 5183149Seric ** Side Effects: 5193149Seric ** none. 5203149Seric */ 521297Seric 5223149Seric toktype(c) 52358050Seric register int c; 5243149Seric { 5253380Seric static char buf[50]; 5263382Seric static bool firstime = TRUE; 5273380Seric 5283382Seric if (firstime) 5293380Seric { 5303382Seric firstime = FALSE; 53158050Seric expand("\201o", buf, &buf[sizeof buf - 1], CurEnv); 5327005Seric (void) strcat(buf, DELIMCHARS); 5333380Seric } 53458050Seric c &= 0377; 53556327Seric if (c == MATCHCLASS || c == MATCHREPL || c == MATCHNCLASS) 5368078Seric return (ONE); 53759027Seric if (c == MACRODEXPAND) 53859027Seric return (ONE); 5398078Seric if (c == '"') 5408078Seric return (QST); 54158050Seric if ((c & 0340) == 0200) 54258050Seric return (OPR); 5434100Seric if (!isascii(c)) 5448078Seric return (ATM); 5458078Seric if (isspace(c) || c == ')') 5468078Seric return (SPC); 54758050Seric if (strchr(buf, c) != NULL) 5488078Seric return (OPR); 5498078Seric return (ATM); 5503149Seric } 5513149Seric /* 5523149Seric ** REWRITE -- apply rewrite rules to token vector. 5533149Seric ** 5544476Seric ** This routine is an ordered production system. Each rewrite 5554476Seric ** rule has a LHS (called the pattern) and a RHS (called the 5564476Seric ** rewrite); 'rwr' points the the current rewrite rule. 5574476Seric ** 5584476Seric ** For each rewrite rule, 'avp' points the address vector we 5594476Seric ** are trying to match against, and 'pvp' points to the pattern. 5608058Seric ** If pvp points to a special match value (MATCHZANY, MATCHANY, 5619585Seric ** MATCHONE, MATCHCLASS, MATCHNCLASS) then the address in avp 5629585Seric ** matched is saved away in the match vector (pointed to by 'mvp'). 5634476Seric ** 5644476Seric ** When a match between avp & pvp does not match, we try to 5659585Seric ** back out. If we back up over MATCHONE, MATCHCLASS, or MATCHNCLASS 5664476Seric ** we must also back out the match in mvp. If we reach a 5678058Seric ** MATCHANY or MATCHZANY we just extend the match and start 5688058Seric ** over again. 5694476Seric ** 5704476Seric ** When we finally match, we rewrite the address vector 5714476Seric ** and try over again. 5724476Seric ** 5733149Seric ** Parameters: 5743149Seric ** pvp -- pointer to token vector. 57559027Seric ** ruleset -- the ruleset to use for rewriting. 57659027Seric ** e -- the current envelope. 5773149Seric ** 5783149Seric ** Returns: 57959084Seric ** A status code. If EX_TEMPFAIL, higher level code should 58059084Seric ** attempt recovery. 5813149Seric ** 5823149Seric ** Side Effects: 5833149Seric ** pvp is modified. 5843149Seric */ 5852091Seric 5863149Seric struct match 5873149Seric { 5884468Seric char **first; /* first token matched */ 5894468Seric char **last; /* last token matched */ 59058825Seric char **pattern; /* pointer to pattern */ 5913149Seric }; 5923149Seric 5934468Seric # define MAXMATCH 9 /* max params per rewrite */ 5943149Seric 5953149Seric 59659084Seric int 59759027Seric rewrite(pvp, ruleset, e) 5983149Seric char **pvp; 5994070Seric int ruleset; 60059027Seric register ENVELOPE *e; 6013149Seric { 6023149Seric register char *ap; /* address pointer */ 6033149Seric register char *rp; /* rewrite pointer */ 6043149Seric register char **avp; /* address vector pointer */ 6053149Seric register char **rvp; /* rewrite vector pointer */ 6068058Seric register struct match *mlp; /* cur ptr into mlist */ 6078058Seric register struct rewrite *rwr; /* pointer to current rewrite rule */ 60858866Seric int ruleno; /* current rule number */ 60959084Seric int rstat = EX_OK; /* return status */ 61056678Seric struct match mlist[MAXMATCH]; /* stores match on LHS */ 6113149Seric char *npvp[MAXATOM+1]; /* temporary space for rebuild */ 61259027Seric extern char *macvalue(); 6133149Seric 6149279Seric if (OpMode == MD_TEST || tTd(21, 2)) 6153149Seric { 6168959Seric printf("rewrite: ruleset %2d input:", ruleset); 61756678Seric printav(pvp); 6183149Seric } 61956678Seric if (ruleset < 0 || ruleset >= MAXRWSETS) 62056326Seric { 62158151Seric syserr("554 rewrite: illegal ruleset number %d", ruleset); 62259084Seric return EX_CONFIG; 62356326Seric } 62456678Seric if (pvp == NULL) 62559084Seric return EX_USAGE; 62656326Seric 6273149Seric /* 62856678Seric ** Run through the list of rewrite rules, applying 62956678Seric ** any that match. 6303149Seric */ 6313149Seric 63258866Seric ruleno = 1; 6334070Seric for (rwr = RewriteRules[ruleset]; rwr != NULL; ) 6343149Seric { 63556678Seric int loopcount = 0; 63656678Seric 6377675Seric if (tTd(21, 12)) 638297Seric { 6398069Seric printf("-----trying rule:"); 64056678Seric printav(rwr->r_lhs); 6413149Seric } 6423149Seric 6433149Seric /* try to match on this rule */ 6444468Seric mlp = mlist; 6458058Seric rvp = rwr->r_lhs; 6468058Seric avp = pvp; 64758866Seric if (++loopcount > 100) 6483149Seric { 64958866Seric syserr("554 Infinite loop in ruleset %d, rule %d", 65058866Seric ruleset, ruleno); 65158866Seric if (tTd(21, 1)) 65252637Seric { 65356678Seric printf("workspace: "); 65456678Seric printav(pvp); 65552637Seric } 65658866Seric break; 65758866Seric } 65858866Seric 65958866Seric while ((ap = *avp) != NULL || *rvp != NULL) 66058866Seric { 6613149Seric rp = *rvp; 6628058Seric if (tTd(21, 35)) 6638058Seric { 66458825Seric printf("ADVANCE rp="); 66557531Seric xputs(rp); 66657532Seric printf(", ap="); 6678058Seric xputs(ap); 6688069Seric printf("\n"); 6698058Seric } 67056678Seric if (rp == NULL) 67156326Seric { 6723149Seric /* end-of-pattern before end-of-address */ 6738058Seric goto backup; 67456678Seric } 67558173Seric if (ap == NULL && (*rp & 0377) != MATCHZANY && 67658827Seric (*rp & 0377) != MATCHZERO) 67756326Seric { 67858825Seric /* end-of-input with patterns left */ 67958825Seric goto backup; 680297Seric } 68156326Seric 68258050Seric switch (*rp & 0377) 6838058Seric { 68456678Seric register STAB *s; 68558814Seric char buf[MAXLINE]; 68656326Seric 68756678Seric case MATCHCLASS: 68858825Seric /* match any phrase in a class */ 68958825Seric mlp->pattern = rvp; 69058814Seric mlp->first = avp; 69158814Seric extendclass: 69258825Seric ap = *avp; 69358825Seric if (ap == NULL) 69458814Seric goto backup; 69558814Seric mlp->last = avp++; 69658814Seric cataddr(mlp->first, mlp->last, buf, sizeof buf, '\0'); 69758814Seric s = stab(buf, ST_CLASS, ST_FIND); 69856678Seric if (s == NULL || !bitnset(rp[1], s->s_class)) 69956326Seric { 70058825Seric if (tTd(21, 36)) 70158825Seric { 70258825Seric printf("EXTEND rp="); 70358825Seric xputs(rp); 70458825Seric printf(", ap="); 70558825Seric xputs(ap); 70658825Seric printf("\n"); 70758825Seric } 70858825Seric goto extendclass; 70956326Seric } 71058825Seric if (tTd(21, 36)) 71158825Seric printf("CLMATCH\n"); 71258814Seric mlp++; 71358814Seric break; 7144060Seric 71558825Seric case MATCHNCLASS: 71658825Seric /* match any token not in a class */ 71758825Seric s = stab(ap, ST_CLASS, ST_FIND); 71858825Seric if (s != NULL && bitnset(rp[1], s->s_class)) 71958825Seric goto backup; 72058825Seric 72158825Seric /* fall through */ 72258825Seric 72356678Seric case MATCHONE: 72456678Seric case MATCHANY: 72556678Seric /* match exactly one token */ 72658825Seric mlp->pattern = rvp; 72756678Seric mlp->first = avp; 72856678Seric mlp->last = avp++; 7298058Seric mlp++; 73056678Seric break; 7318058Seric 73256678Seric case MATCHZANY: 73356678Seric /* match zero or more tokens */ 73458825Seric mlp->pattern = rvp; 73556678Seric mlp->first = avp; 73656678Seric mlp->last = avp - 1; 73756678Seric mlp++; 73856678Seric break; 73956326Seric 74058827Seric case MATCHZERO: 74158173Seric /* match zero tokens */ 74258173Seric break; 74358173Seric 74459027Seric case MACRODEXPAND: 74559027Seric /* 74659027Seric ** Match against run-time macro. 74759027Seric ** This algorithm is broken for the 74859027Seric ** general case (no recursive macros, 74959027Seric ** improper tokenization) but should 75059027Seric ** work for the usual cases. 75159027Seric */ 75259027Seric 75359027Seric ap = macvalue(rp[1], e); 75459027Seric mlp->first = avp; 75559027Seric if (tTd(21, 2)) 75659027Seric printf("rewrite: LHS $&%c => \"%s\"\n", 75759027Seric rp[1], 75859027Seric ap == NULL ? "(NULL)" : ap); 75959027Seric 76059027Seric if (ap == NULL) 76159027Seric break; 76259027Seric while (*ap != NULL) 76359027Seric { 76459027Seric if (*avp == NULL || 76559027Seric strncasecmp(ap, *avp, strlen(*avp)) != 0) 76659027Seric { 76759027Seric /* no match */ 76859027Seric avp = mlp->first; 76959027Seric goto backup; 77059027Seric } 77159027Seric ap += strlen(*avp++); 77259027Seric } 77359027Seric 77459027Seric /* match */ 77559027Seric break; 77659027Seric 77756678Seric default: 77856678Seric /* must have exact match */ 77956678Seric if (strcasecmp(rp, ap)) 7808058Seric goto backup; 7814468Seric avp++; 78256678Seric break; 7833149Seric } 7843149Seric 78556678Seric /* successful match on this token */ 7863149Seric rvp++; 7873149Seric continue; 7883149Seric 78958825Seric backup: 79056678Seric /* match failed -- back up */ 79158825Seric while (--mlp >= mlist) 7923149Seric { 79358825Seric rvp = mlp->pattern; 79456678Seric rp = *rvp; 79558825Seric avp = mlp->last + 1; 79658825Seric ap = *avp; 79758825Seric 79858825Seric if (tTd(21, 36)) 79958825Seric { 80058825Seric printf("BACKUP rp="); 80158825Seric xputs(rp); 80258825Seric printf(", ap="); 80358825Seric xputs(ap); 80458825Seric printf("\n"); 80558825Seric } 80658825Seric 80758825Seric if (ap == NULL) 80858825Seric { 80958825Seric /* run off the end -- back up again */ 81058825Seric continue; 81158825Seric } 81258050Seric if ((*rp & 0377) == MATCHANY || 81358050Seric (*rp & 0377) == MATCHZANY) 8144468Seric { 81556678Seric /* extend binding and continue */ 81658825Seric mlp->last = avp++; 81756678Seric rvp++; 81858825Seric mlp++; 81956678Seric break; 8204468Seric } 82158825Seric if ((*rp & 0377) == MATCHCLASS) 82256678Seric { 82358814Seric /* extend binding and try again */ 82458825Seric mlp->last = avp++; 82558814Seric goto extendclass; 82658814Seric } 8273149Seric } 8283149Seric 82958825Seric if (mlp < mlist) 83056678Seric { 83156678Seric /* total failure to match */ 83256326Seric break; 8333149Seric } 834297Seric } 8353149Seric 8363149Seric /* 83756678Seric ** See if we successfully matched 8383149Seric */ 8393149Seric 84058827Seric if (mlp < mlist || *rvp != NULL) 8413149Seric { 8429374Seric if (tTd(21, 10)) 8439374Seric printf("----- rule fails\n"); 8449374Seric rwr = rwr->r_next; 84558866Seric ruleno++; 8469374Seric continue; 8479374Seric } 8483149Seric 8499374Seric rvp = rwr->r_rhs; 8509374Seric if (tTd(21, 12)) 8519374Seric { 8529374Seric printf("-----rule matches:"); 85356678Seric printav(rvp); 8549374Seric } 8559374Seric 8569374Seric rp = *rvp; 85758050Seric if ((*rp & 0377) == CANONUSER) 8589374Seric { 8599374Seric rvp++; 8609374Seric rwr = rwr->r_next; 86158866Seric ruleno++; 8629374Seric } 86358050Seric else if ((*rp & 0377) == CANONHOST) 8649374Seric { 8659374Seric rvp++; 8669374Seric rwr = NULL; 8679374Seric } 86858050Seric else if ((*rp & 0377) == CANONNET) 8699374Seric rwr = NULL; 8709374Seric 8719374Seric /* substitute */ 8729374Seric for (avp = npvp; *rvp != NULL; rvp++) 8739374Seric { 8749374Seric register struct match *m; 8759374Seric register char **pp; 8769374Seric 8778058Seric rp = *rvp; 87858050Seric if ((*rp & 0377) == MATCHREPL) 8798058Seric { 88016914Seric /* substitute from LHS */ 88116914Seric m = &mlist[rp[1] - '1']; 88256678Seric if (m < mlist || m >= mlp) 8839374Seric { 88458151Seric syserr("554 rewrite: ruleset %d: replacement $%c out of bounds", 88556326Seric ruleset, rp[1]); 88659084Seric return EX_CONFIG; 8879374Seric } 88816914Seric if (tTd(21, 15)) 88916914Seric { 89016914Seric printf("$%c:", rp[1]); 89116914Seric pp = m->first; 89256678Seric while (pp <= m->last) 89316914Seric { 89416914Seric printf(" %x=\"", *pp); 89516914Seric (void) fflush(stdout); 89616914Seric printf("%s\"", *pp++); 89716914Seric } 89816914Seric printf("\n"); 89916914Seric } 9009374Seric pp = m->first; 90156678Seric while (pp <= m->last) 9023149Seric { 90316914Seric if (avp >= &npvp[MAXATOM]) 90456678Seric { 90558151Seric syserr("554 rewrite: expansion too long"); 90659084Seric return EX_DATAERR; 90756678Seric } 90816914Seric *avp++ = *pp++; 9093149Seric } 9103149Seric } 91116914Seric else 9128226Seric { 91316914Seric /* vanilla replacement */ 9149374Seric if (avp >= &npvp[MAXATOM]) 91516889Seric { 91656678Seric toolong: 91758151Seric syserr("554 rewrite: expansion too long"); 91859084Seric return EX_DATAERR; 91916889Seric } 92059027Seric if ((*rp & 0377) != MACRODEXPAND) 92159027Seric *avp++ = rp; 92259027Seric else 92359027Seric { 92459027Seric *avp = macvalue(rp[1], e); 92559027Seric if (tTd(21, 2)) 92659027Seric printf("rewrite: RHS $&%c => \"%s\"\n", 92759027Seric rp[1], 92859027Seric *avp == NULL ? "(NULL)" : *avp); 92959027Seric if (*avp != NULL) 93059027Seric avp++; 93159027Seric } 9328226Seric } 9339374Seric } 9349374Seric *avp++ = NULL; 93516914Seric 93616914Seric /* 93756678Seric ** Check for any hostname/keyword lookups. 93816914Seric */ 93916914Seric 94016914Seric for (rvp = npvp; *rvp != NULL; rvp++) 94116914Seric { 94256678Seric char **hbrvp; 94316914Seric char **xpvp; 94416914Seric int trsize; 94517473Seric char *olddelimchar; 94656678Seric char *replac; 94756678Seric int endtoken; 94856678Seric STAB *map; 94956678Seric char *mapname; 95056678Seric char **key_rvp; 95156678Seric char **arg_rvp; 95256678Seric char **default_rvp; 95356678Seric char buf[MAXNAME + 1]; 95416914Seric char *pvpb1[MAXATOM + 1]; 95556823Seric char *argvect[10]; 95617174Seric char pvpbuf[PSBUFSIZE]; 95716914Seric 95858050Seric if ((**rvp & 0377) != HOSTBEGIN && 95958050Seric (**rvp & 0377) != LOOKUPBEGIN) 96016914Seric continue; 96116914Seric 96216914Seric /* 96356678Seric ** Got a hostname/keyword lookup. 96416914Seric ** 96516914Seric ** This could be optimized fairly easily. 96616914Seric */ 96716914Seric 96816914Seric hbrvp = rvp; 96958050Seric if ((**rvp & 0377) == HOSTBEGIN) 97056327Seric { 97156678Seric endtoken = HOSTEND; 97256678Seric mapname = "host"; 97356327Seric } 97456326Seric else 97556327Seric { 97656678Seric endtoken = LOOKUPEND; 97756678Seric mapname = *++rvp; 97856327Seric } 97956678Seric map = stab(mapname, ST_MAP, ST_FIND); 98056678Seric if (map == NULL) 98158151Seric syserr("554 rewrite: map %s not found", mapname); 98256678Seric 98356678Seric /* extract the match part */ 98456678Seric key_rvp = ++rvp; 98556823Seric default_rvp = NULL; 98656823Seric arg_rvp = argvect; 98756823Seric xpvp = NULL; 98856823Seric replac = pvpbuf; 98958050Seric while (*rvp != NULL && (**rvp & 0377) != endtoken) 99053654Seric { 99158050Seric int nodetype = **rvp & 0377; 99256823Seric 99356823Seric if (nodetype != CANONHOST && nodetype != CANONUSER) 99456678Seric { 99556823Seric rvp++; 99656823Seric continue; 99756823Seric } 99856823Seric 99956823Seric *rvp++ = NULL; 100056823Seric 100156823Seric if (xpvp != NULL) 100256823Seric { 100358814Seric cataddr(xpvp, NULL, replac, 100458082Seric &pvpbuf[sizeof pvpbuf] - replac, 100558082Seric '\0'); 100656823Seric *++arg_rvp = replac; 100756823Seric replac += strlen(replac) + 1; 100856823Seric xpvp = NULL; 100956823Seric } 101056823Seric switch (nodetype) 101156823Seric { 101256678Seric case CANONHOST: 101356823Seric xpvp = rvp; 101456678Seric break; 101556678Seric 101656678Seric case CANONUSER: 101756678Seric default_rvp = rvp; 101856678Seric break; 101956678Seric } 102053654Seric } 102116914Seric if (*rvp != NULL) 102216914Seric *rvp++ = NULL; 102356823Seric if (xpvp != NULL) 102456823Seric { 102558814Seric cataddr(xpvp, NULL, replac, 102658082Seric &pvpbuf[sizeof pvpbuf] - replac, 102758082Seric '\0'); 102856823Seric *++arg_rvp = replac; 102956823Seric } 103056823Seric *++arg_rvp = NULL; 103116914Seric 103216914Seric /* save the remainder of the input string */ 103316914Seric trsize = (int) (avp - rvp + 1) * sizeof *rvp; 103416914Seric bcopy((char *) rvp, (char *) pvpb1, trsize); 103516914Seric 103656678Seric /* look it up */ 103758814Seric cataddr(key_rvp, NULL, buf, sizeof buf, '\0'); 103856823Seric argvect[0] = buf; 103956678Seric if (map != NULL && bitset(MF_VALID, map->s_map.map_flags)) 104056836Seric { 104156836Seric int bsize = sizeof buf - 1; 104259084Seric auto int stat = EX_OK; 104356836Seric 104456836Seric if (map->s_map.map_app != NULL) 104556836Seric bsize -= strlen(map->s_map.map_app); 104658796Seric if (tTd(60, 1)) 104758796Seric printf("map_lookup(%s, %s) => ", 104858796Seric mapname, buf); 104956836Seric replac = (*map->s_map.map_class->map_lookup)(&map->s_map, 105059084Seric buf, sizeof buf - 1, argvect, 105159084Seric &stat); 105256836Seric if (replac != NULL && map->s_map.map_app != NULL) 105356836Seric strcat(replac, map->s_map.map_app); 105458796Seric if (tTd(60, 1)) 105559084Seric printf("%s (%d)\n", 105659084Seric replac ? replac : "NOT FOUND", 105759084Seric stat); 105859084Seric 105959084Seric /* should recover if stat == EX_TEMPFAIL */ 106059084Seric if (stat == EX_TEMPFAIL) 106159084Seric rstat = stat; 106256836Seric } 106353654Seric else 106456678Seric replac = NULL; 106556678Seric 106656678Seric /* if no replacement, use default */ 106756823Seric if (replac == NULL && default_rvp != NULL) 106856823Seric { 106956823Seric char buf2[sizeof buf]; 107056823Seric 107156823Seric /* rewrite the default with % translations */ 107258814Seric cataddr(default_rvp, NULL, buf2, sizeof buf2, '\0'); 107356823Seric map_rewrite(buf2, sizeof buf2, buf, sizeof buf, 107456823Seric argvect); 107556823Seric replac = buf; 107656823Seric } 107756823Seric 107856678Seric if (replac == NULL) 107951317Seric { 108056823Seric xpvp = key_rvp; 108153654Seric } 108256678Seric else 108353654Seric { 108456678Seric /* scan the new replacement */ 108558333Seric xpvp = prescan(replac, '\0', pvpbuf, NULL); 108653654Seric if (xpvp == NULL) 108751317Seric { 108858403Seric /* prescan already printed error */ 108959084Seric return EX_DATAERR; 109056678Seric } 109151317Seric } 109251317Seric 109316914Seric /* append it to the token list */ 109456678Seric for (avp = hbrvp; *xpvp != NULL; xpvp++) 109556678Seric { 109617174Seric *avp++ = newstr(*xpvp); 109716920Seric if (avp >= &npvp[MAXATOM]) 109816914Seric goto toolong; 109917174Seric } 110016914Seric 110116914Seric /* restore the old trailing information */ 110256678Seric for (xpvp = pvpb1; (*avp++ = *xpvp++) != NULL; ) 110316920Seric if (avp >= &npvp[MAXATOM]) 110416914Seric goto toolong; 110517174Seric 110656678Seric break; 110716914Seric } 110816914Seric 110916914Seric /* 111016914Seric ** Check for subroutine calls. 111116914Seric */ 111216914Seric 111358050Seric if (*npvp != NULL && (**npvp & 0377) == CALLSUBR) 111456678Seric { 111559084Seric int stat; 111659084Seric 111756678Seric bcopy((char *) &npvp[2], (char *) pvp, 111856678Seric (int) (avp - npvp - 2) * sizeof *avp); 111956678Seric if (tTd(21, 3)) 112056678Seric printf("-----callsubr %s\n", npvp[1]); 112159084Seric stat = rewrite(pvp, atoi(npvp[1]), e); 112259084Seric if (rstat == EX_OK || stat == EX_TEMPFAIL) 112359084Seric rstat = stat; 112456678Seric } 112556678Seric else 112656678Seric { 112717348Seric bcopy((char *) npvp, (char *) pvp, 112816900Seric (int) (avp - npvp) * sizeof *avp); 112956678Seric } 11309374Seric if (tTd(21, 4)) 11319374Seric { 11329374Seric printf("rewritten as:"); 113356678Seric printav(pvp); 11349374Seric } 1135297Seric } 11368069Seric 11379279Seric if (OpMode == MD_TEST || tTd(21, 2)) 11388069Seric { 11398959Seric printf("rewrite: ruleset %2d returns:", ruleset); 114056678Seric printav(pvp); 11418069Seric } 114259084Seric 114359084Seric return rstat; 11443149Seric } 11453149Seric /* 11463149Seric ** BUILDADDR -- build address from token vector. 11473149Seric ** 11483149Seric ** Parameters: 11493149Seric ** tv -- token vector. 11503149Seric ** a -- pointer to address descriptor to fill. 11513149Seric ** If NULL, one will be allocated. 115258966Seric ** e -- the current envelope. 11533149Seric ** 11543149Seric ** Returns: 11554279Seric ** NULL if there was an error. 11564279Seric ** 'a' otherwise. 11573149Seric ** 11583149Seric ** Side Effects: 11593149Seric ** fills in 'a' 11603149Seric */ 11613149Seric 116257249Seric struct errcodes 116357249Seric { 116457249Seric char *ec_name; /* name of error code */ 116557249Seric int ec_code; /* numeric code */ 116657249Seric } ErrorCodes[] = 116757249Seric { 116857249Seric "usage", EX_USAGE, 116957249Seric "nouser", EX_NOUSER, 117057249Seric "nohost", EX_NOHOST, 117157249Seric "unavailable", EX_UNAVAILABLE, 117257249Seric "software", EX_SOFTWARE, 117357249Seric "tempfail", EX_TEMPFAIL, 117457249Seric "protocol", EX_PROTOCOL, 117557249Seric #ifdef EX_CONFIG 117657249Seric "config", EX_CONFIG, 117757249Seric #endif 117857249Seric NULL, EX_UNAVAILABLE, 117957249Seric }; 118057249Seric 118156678Seric ADDRESS * 118258966Seric buildaddr(tv, a, e) 11833149Seric register char **tv; 11843149Seric register ADDRESS *a; 118558966Seric register ENVELOPE *e; 11863149Seric { 11873149Seric struct mailer **mp; 11883149Seric register struct mailer *m; 118958008Seric char *bp; 119058008Seric int spaceleft; 119157402Seric static char buf[MAXNAME]; 11923149Seric 11933149Seric if (a == NULL) 11943149Seric a = (ADDRESS *) xalloc(sizeof *a); 119516889Seric bzero((char *) a, sizeof *a); 11963149Seric 11973149Seric /* figure out what net/mailer to use */ 119858050Seric if ((**tv & 0377) != CANONNET) 11994279Seric { 120058151Seric syserr("554 buildaddr: no net"); 12014279Seric return (NULL); 12024279Seric } 12033149Seric tv++; 120458680Seric if (strcasecmp(*tv, "error") == 0) 12054279Seric { 120658050Seric if ((**++tv & 0377) == CANONHOST) 120710183Seric { 120857249Seric register struct errcodes *ep; 120957249Seric 121058050Seric if (isascii(**++tv) && isdigit(**tv)) 121157249Seric { 121257249Seric setstat(atoi(*tv)); 121357249Seric } 121457249Seric else 121557249Seric { 121657249Seric for (ep = ErrorCodes; ep->ec_name != NULL; ep++) 121757249Seric if (strcasecmp(ep->ec_name, *tv) == 0) 121857249Seric break; 121957249Seric setstat(ep->ec_code); 122057249Seric } 122110183Seric tv++; 122210183Seric } 122358050Seric if ((**tv & 0377) != CANONUSER) 122458151Seric syserr("554 buildaddr: error: no user"); 122558814Seric cataddr(++tv, NULL, buf, sizeof buf, ' '); 122658082Seric stripquotes(buf); 12274279Seric usrerr(buf); 12284279Seric return (NULL); 12294279Seric } 123057402Seric 12314598Seric for (mp = Mailer; (m = *mp++) != NULL; ) 12323149Seric { 123358680Seric if (strcasecmp(m->m_name, *tv) == 0) 12343149Seric break; 12353149Seric } 12363149Seric if (m == NULL) 12374279Seric { 123858151Seric syserr("554 buildaddr: unknown mailer %s", *tv); 12394279Seric return (NULL); 12404279Seric } 12414598Seric a->q_mailer = m; 12423149Seric 12433149Seric /* figure out what host (if any) */ 124456678Seric tv++; 124558509Seric if ((**tv & 0377) == CANONHOST) 12463149Seric { 124758008Seric bp = buf; 124858008Seric spaceleft = sizeof buf - 1; 124958050Seric while (*++tv != NULL && (**tv & 0377) != CANONUSER) 125058008Seric { 125158008Seric int i = strlen(*tv); 125258008Seric 125358008Seric if (i > spaceleft) 125458008Seric { 125558008Seric /* out of space for this address */ 125658008Seric if (spaceleft >= 0) 125758151Seric syserr("554 buildaddr: host too long (%.40s...)", 125858008Seric buf); 125958008Seric i = spaceleft; 126058008Seric spaceleft = 0; 126158008Seric } 126258008Seric if (i <= 0) 126358008Seric continue; 126458008Seric bcopy(*tv, bp, i); 126558008Seric bp += i; 126658008Seric spaceleft -= i; 126758008Seric } 126858010Seric *bp = '\0'; 12695704Seric a->q_host = newstr(buf); 12703149Seric } 127157249Seric else 127258509Seric { 127358509Seric if (!bitnset(M_LOCALMAILER, m->m_flags)) 127458509Seric { 127558509Seric syserr("554 buildaddr: no host"); 127658509Seric return (NULL); 127758509Seric } 127857249Seric a->q_host = NULL; 127958509Seric } 12803149Seric 12813149Seric /* figure out the user */ 128258050Seric if (*tv == NULL || (**tv & 0377) != CANONUSER) 12834279Seric { 128458151Seric syserr("554 buildaddr: no user"); 12854279Seric return (NULL); 12864279Seric } 128757402Seric tv++; 128851317Seric 128957402Seric /* do special mapping for local mailer */ 129057402Seric if (m == LocalMailer && *tv != NULL) 129157402Seric { 129257454Seric register char *p = *tv; 129357454Seric 129457454Seric if (*p == '"') 129557454Seric p++; 129657454Seric if (*p == '|') 129757402Seric a->q_mailer = m = ProgMailer; 129857454Seric else if (*p == '/') 129957402Seric a->q_mailer = m = FileMailer; 130057454Seric else if (*p == ':') 130157402Seric { 130257402Seric /* may be :include: */ 130358814Seric cataddr(tv, NULL, buf, sizeof buf, '\0'); 130458008Seric stripquotes(buf); 130558008Seric if (strncasecmp(buf, ":include:", 9) == 0) 130658008Seric { 130758008Seric /* if :include:, don't need further rewriting */ 130857402Seric a->q_mailer = m = InclMailer; 130958008Seric a->q_user = &buf[9]; 131058008Seric return (a); 131158008Seric } 131257402Seric } 131357402Seric } 131457402Seric 131558008Seric if (m == LocalMailer && *tv != NULL && strcmp(*tv, "@") == 0) 131658008Seric { 131758008Seric tv++; 131858008Seric a->q_flags |= QNOTREMOTE; 131958008Seric } 132058008Seric 132158673Seric /* do cleanup of final address */ 132259084Seric (void) rewrite(tv, 4, e); 132319040Seric 132419040Seric /* save the result for the command line/RCPT argument */ 132558814Seric cataddr(tv, NULL, buf, sizeof buf, '\0'); 13263149Seric a->q_user = buf; 13273149Seric 132858670Seric /* 132958670Seric ** Do mapping to lower case as requested by mailer 133058670Seric */ 133158670Seric 133258670Seric if (a->q_host != NULL && !bitnset(M_HST_UPPER, m->m_flags)) 133358670Seric makelower(a->q_host); 133458670Seric if (!bitnset(M_USR_UPPER, m->m_flags)) 133558670Seric makelower(a->q_user); 133658670Seric 13373149Seric return (a); 13383149Seric } 13393188Seric /* 13404228Seric ** CATADDR -- concatenate pieces of addresses (putting in <LWSP> subs) 13414228Seric ** 13424228Seric ** Parameters: 13434228Seric ** pvp -- parameter vector to rebuild. 134458814Seric ** evp -- last parameter to include. Can be NULL to 134558814Seric ** use entire pvp. 13464228Seric ** buf -- buffer to build the string into. 13474228Seric ** sz -- size of buf. 134858082Seric ** spacesub -- the space separator character; if null, 134958082Seric ** use SpaceSub. 13504228Seric ** 13514228Seric ** Returns: 13524228Seric ** none. 13534228Seric ** 13544228Seric ** Side Effects: 13554228Seric ** Destroys buf. 13564228Seric */ 13574228Seric 135858814Seric cataddr(pvp, evp, buf, sz, spacesub) 13594228Seric char **pvp; 136058814Seric char **evp; 13614228Seric char *buf; 13624228Seric register int sz; 136358082Seric char spacesub; 13644228Seric { 13654228Seric bool oatomtok = FALSE; 136656678Seric bool natomtok = FALSE; 13674228Seric register int i; 13684228Seric register char *p; 13694228Seric 137058082Seric if (spacesub == '\0') 137158082Seric spacesub = SpaceSub; 137258082Seric 13738423Seric if (pvp == NULL) 13748423Seric { 137523109Seric (void) strcpy(buf, ""); 13768423Seric return; 13778423Seric } 13784228Seric p = buf; 137911156Seric sz -= 2; 13804228Seric while (*pvp != NULL && (i = strlen(*pvp)) < sz) 13814228Seric { 13828078Seric natomtok = (toktype(**pvp) == ATM); 13834228Seric if (oatomtok && natomtok) 138458082Seric *p++ = spacesub; 13854228Seric (void) strcpy(p, *pvp); 13864228Seric oatomtok = natomtok; 13874228Seric p += i; 138811156Seric sz -= i + 1; 138958814Seric if (pvp++ == evp) 139058814Seric break; 13914228Seric } 13924228Seric *p = '\0'; 13934228Seric } 13944228Seric /* 13953188Seric ** SAMEADDR -- Determine if two addresses are the same 13963188Seric ** 13973188Seric ** This is not just a straight comparison -- if the mailer doesn't 13983188Seric ** care about the host we just ignore it, etc. 13993188Seric ** 14003188Seric ** Parameters: 14013188Seric ** a, b -- pointers to the internal forms to compare. 14023188Seric ** 14033188Seric ** Returns: 14043188Seric ** TRUE -- they represent the same mailbox. 14053188Seric ** FALSE -- they don't. 14063188Seric ** 14073188Seric ** Side Effects: 14083188Seric ** none. 14093188Seric */ 14103188Seric 14113188Seric bool 14129374Seric sameaddr(a, b) 14133188Seric register ADDRESS *a; 14143188Seric register ADDRESS *b; 14153188Seric { 14163188Seric /* if they don't have the same mailer, forget it */ 14173188Seric if (a->q_mailer != b->q_mailer) 14183188Seric return (FALSE); 14193188Seric 14203188Seric /* if the user isn't the same, we can drop out */ 142156678Seric if (strcmp(a->q_user, b->q_user) != 0) 14223188Seric return (FALSE); 14233188Seric 142458438Seric /* if we have good uids for both but the differ, these are different */ 142558438Seric if (bitset(QGOODUID, a->q_flags & b->q_flags) && a->q_uid != b->q_uid) 142658438Seric return (FALSE); 142758438Seric 142858509Seric /* otherwise compare hosts (but be careful for NULL ptrs) */ 142958509Seric if (a->q_host == b->q_host) 143058509Seric { 143158509Seric /* probably both null pointers */ 14323188Seric return (TRUE); 143358509Seric } 14343188Seric if (a->q_host == NULL || b->q_host == NULL) 143558509Seric { 143658509Seric /* only one is a null pointer */ 14373188Seric return (FALSE); 143858509Seric } 143956678Seric if (strcmp(a->q_host, b->q_host) != 0) 14403188Seric return (FALSE); 14413188Seric 14423188Seric return (TRUE); 14433188Seric } 14443234Seric /* 14453234Seric ** PRINTADDR -- print address (for debugging) 14463234Seric ** 14473234Seric ** Parameters: 14483234Seric ** a -- the address to print 14493234Seric ** follow -- follow the q_next chain. 14503234Seric ** 14513234Seric ** Returns: 14523234Seric ** none. 14533234Seric ** 14543234Seric ** Side Effects: 14553234Seric ** none. 14563234Seric */ 14573234Seric 14583234Seric printaddr(a, follow) 14593234Seric register ADDRESS *a; 14603234Seric bool follow; 14613234Seric { 14625001Seric bool first = TRUE; 146357731Seric register MAILER *m; 146457731Seric MAILER pseudomailer; 14655001Seric 14663234Seric while (a != NULL) 14673234Seric { 14685001Seric first = FALSE; 14694443Seric printf("%x=", a); 14704085Seric (void) fflush(stdout); 147157731Seric 147257731Seric /* find the mailer -- carefully */ 147357731Seric m = a->q_mailer; 147457731Seric if (m == NULL) 147557731Seric { 147657731Seric m = &pseudomailer; 147757731Seric m->m_mno = -1; 147857731Seric m->m_name = "NULL"; 147957731Seric } 148057731Seric 148158680Seric printf("%s:\n\tmailer %d (%s), host `%s', user `%s', ruser `%s'\n", 148257731Seric a->q_paddr, m->m_mno, m->m_name, 148340973Sbostic a->q_host, a->q_user, a->q_ruser? a->q_ruser: "<null>"); 148459269Seric printf("\tnext=%x, flags=%o, alias %x, uid %d, gid %d\n", 148559269Seric a->q_next, a->q_flags, a->q_alias, a->q_uid, a->q_gid); 148659269Seric printf("\towner=%s, home=\"%s\", fullname=\"%s\"\n", 148759269Seric a->q_owner == NULL ? "(none)" : a->q_owner, 148859269Seric a->q_home, a->q_fullname); 14894996Seric 14903234Seric if (!follow) 14913234Seric return; 14924996Seric a = a->q_next; 14933234Seric } 14945001Seric if (first) 14954443Seric printf("[NULL]\n"); 14963234Seric } 14974317Seric 14987682Seric /* 14997682Seric ** REMOTENAME -- return the name relative to the current mailer 15007682Seric ** 15017682Seric ** Parameters: 15027682Seric ** name -- the name to translate. 15038069Seric ** m -- the mailer that we want to do rewriting relative 15048069Seric ** to. 150559163Seric ** flags -- fine tune operations. 150659163Seric ** pstat -- pointer to status word. 150758020Seric ** e -- the current envelope. 15087682Seric ** 15097682Seric ** Returns: 15107682Seric ** the text string representing this address relative to 15117682Seric ** the receiving mailer. 15127682Seric ** 15137682Seric ** Side Effects: 15147682Seric ** none. 15157682Seric ** 15167682Seric ** Warnings: 15177682Seric ** The text string returned is tucked away locally; 15187682Seric ** copy it if you intend to save it. 15197682Seric */ 15207682Seric 15217682Seric char * 152259163Seric remotename(name, m, flags, pstat, e) 15237682Seric char *name; 152456678Seric struct mailer *m; 152559163Seric int flags; 152659163Seric int *pstat; 152756678Seric register ENVELOPE *e; 15287682Seric { 15298069Seric register char **pvp; 15308069Seric char *fancy; 153156678Seric extern char *macvalue(); 153256678Seric char *oldg = macvalue('g', e); 153358020Seric int rwset; 15347682Seric static char buf[MAXNAME]; 15357682Seric char lbuf[MAXNAME]; 153616914Seric char pvpbuf[PSBUFSIZE]; 153756678Seric extern char **prescan(); 153856678Seric extern char *crackaddr(); 15397682Seric 15407755Seric if (tTd(12, 1)) 15417755Seric printf("remotename(%s)\n", name); 15427755Seric 154310177Seric /* don't do anything if we are tagging it as special */ 154459163Seric if (bitset(RF_SENDERADDR, flags)) 154559163Seric rwset = bitset(RF_HEADERADDR, flags) ? m->m_sh_rwset 154659163Seric : m->m_se_rwset; 154758020Seric else 154859163Seric rwset = bitset(RF_HEADERADDR, flags) ? m->m_rh_rwset 154959163Seric : m->m_re_rwset; 155058020Seric if (rwset < 0) 155110177Seric return (name); 155210177Seric 15537682Seric /* 15548181Seric ** Do a heuristic crack of this name to extract any comment info. 15558181Seric ** This will leave the name as a comment and a $g macro. 15567889Seric */ 15577889Seric 155859163Seric if (bitset(RF_CANONICAL, flags) || bitnset(M_NOCOMMENT, m->m_flags)) 155958050Seric fancy = "\201g"; 156010310Seric else 156110310Seric fancy = crackaddr(name); 15627889Seric 15638181Seric /* 15648181Seric ** Turn the name into canonical form. 15658181Seric ** Normally this will be RFC 822 style, i.e., "user@domain". 15668181Seric ** If this only resolves to "user", and the "C" flag is 15678181Seric ** specified in the sending mailer, then the sender's 15688181Seric ** domain will be appended. 15698181Seric */ 15708181Seric 157158333Seric pvp = prescan(name, '\0', pvpbuf, NULL); 15727889Seric if (pvp == NULL) 15737889Seric return (name); 157459163Seric if (rewrite(pvp, 3, e) == EX_TEMPFAIL) 157559163Seric *pstat = EX_TEMPFAIL; 157659163Seric if (bitset(RF_ADDDOMAIN, flags) && e->e_fromdomain != NULL) 15778181Seric { 15788181Seric /* append from domain to this address */ 15798181Seric register char **pxp = pvp; 15808181Seric 15819594Seric /* see if there is an "@domain" in the current name */ 15828181Seric while (*pxp != NULL && strcmp(*pxp, "@") != 0) 15838181Seric pxp++; 15848181Seric if (*pxp == NULL) 15858181Seric { 15869594Seric /* no.... append the "@domain" from the sender */ 158756678Seric register char **qxq = e->e_fromdomain; 15888181Seric 15899594Seric while ((*pxp++ = *qxq++) != NULL) 15909594Seric continue; 159159163Seric if (rewrite(pvp, 3, e) == EX_TEMPFAIL) 159259163Seric *pstat = EX_TEMPFAIL; 15938181Seric } 15948181Seric } 15958181Seric 15968181Seric /* 15978959Seric ** Do more specific rewriting. 159856678Seric ** Rewrite using ruleset 1 or 2 depending on whether this is 159956678Seric ** a sender address or not. 16008181Seric ** Then run it through any receiving-mailer-specific rulesets. 16018181Seric */ 16028181Seric 160359163Seric if (bitset(RF_SENDERADDR, flags)) 160459541Seric { 160559163Seric if (rewrite(pvp, 1, e) == EX_TEMPFAIL) 160659163Seric *pstat = EX_TEMPFAIL; 160759541Seric } 16088069Seric else 160959541Seric { 161059163Seric if (rewrite(pvp, 2, e) == EX_TEMPFAIL) 161159163Seric *pstat = EX_TEMPFAIL; 161259541Seric } 161358020Seric if (rwset > 0) 161459541Seric { 161559163Seric if (rewrite(pvp, rwset, e) == EX_TEMPFAIL) 161659163Seric *pstat = EX_TEMPFAIL; 161759541Seric } 16187682Seric 16198181Seric /* 16208959Seric ** Do any final sanitation the address may require. 16218959Seric ** This will normally be used to turn internal forms 16228959Seric ** (e.g., user@host.LOCAL) into external form. This 16238959Seric ** may be used as a default to the above rules. 16248959Seric */ 16258959Seric 162659163Seric if (rewrite(pvp, 4, e) == EX_TEMPFAIL) 162759163Seric *pstat = EX_TEMPFAIL; 16288959Seric 16298959Seric /* 16308181Seric ** Now restore the comment information we had at the beginning. 16318181Seric */ 16328181Seric 163358825Seric cataddr(pvp, NULL, lbuf, sizeof lbuf, '\0'); 163456678Seric define('g', lbuf, e); 163556678Seric expand(fancy, buf, &buf[sizeof buf - 1], e); 163656678Seric define('g', oldg, e); 16377682Seric 16387682Seric if (tTd(12, 1)) 16397755Seric printf("remotename => `%s'\n", buf); 16407682Seric return (buf); 16417682Seric } 164251317Seric /* 164356678Seric ** MAPLOCALUSER -- run local username through ruleset 5 for final redirection 164451317Seric ** 164551317Seric ** Parameters: 164656678Seric ** a -- the address to map (but just the user name part). 164756678Seric ** sendq -- the sendq in which to install any replacement 164856678Seric ** addresses. 164951317Seric ** 165051317Seric ** Returns: 165151317Seric ** none. 165251317Seric */ 165351317Seric 165456678Seric maplocaluser(a, sendq, e) 165556678Seric register ADDRESS *a; 165656678Seric ADDRESS **sendq; 165756678Seric ENVELOPE *e; 165851317Seric { 165956678Seric register char **pvp; 166056678Seric register ADDRESS *a1 = NULL; 166158333Seric auto char *delimptr; 166256678Seric char pvpbuf[PSBUFSIZE]; 166351317Seric 166456678Seric if (tTd(29, 1)) 166556678Seric { 166656678Seric printf("maplocaluser: "); 166756678Seric printaddr(a, FALSE); 166856678Seric } 166958333Seric pvp = prescan(a->q_user, '\0', pvpbuf, &delimptr); 167056678Seric if (pvp == NULL) 167156678Seric return; 167251317Seric 167359084Seric (void) rewrite(pvp, 5, e); 167458050Seric if (pvp[0] == NULL || (pvp[0][0] & 0377) != CANONNET) 167556678Seric return; 167651317Seric 167756678Seric /* if non-null, mailer destination specified -- has it changed? */ 167858966Seric a1 = buildaddr(pvp, NULL, e); 167956678Seric if (a1 == NULL || sameaddr(a, a1)) 168056678Seric return; 168151317Seric 168256678Seric /* mark old address as dead; insert new address */ 168356678Seric a->q_flags |= QDONTSEND; 168457731Seric if (tTd(29, 5)) 168557731Seric { 168657731Seric printf("maplocaluser: QDONTSEND "); 168757731Seric printaddr(a, FALSE); 168857731Seric } 168956678Seric a1->q_alias = a; 169058333Seric allocaddr(a1, 1, NULL, delimptr); 169156678Seric (void) recipient(a1, sendq, e); 169251317Seric } 169358802Seric /* 169458802Seric ** DEQUOTE_INIT -- initialize dequote map 169558802Seric ** 169658802Seric ** This is a no-op. 169758802Seric ** 169858802Seric ** Parameters: 169958802Seric ** map -- the internal map structure. 170058802Seric ** mapname -- the name of the mapl. 170158802Seric ** args -- arguments. 170258802Seric ** 170358802Seric ** Returns: 170458802Seric ** TRUE. 170558802Seric */ 170658802Seric 170758802Seric bool 170858802Seric dequote_init(map, mapname, args) 170958802Seric MAP *map; 171058802Seric char *mapname; 171158802Seric char *args; 171258802Seric { 171358805Seric register char *p = args; 171458805Seric 171558805Seric for (;;) 171658805Seric { 171758805Seric while (isascii(*p) && isspace(*p)) 171858805Seric p++; 171958805Seric if (*p != '-') 172058805Seric break; 172158805Seric switch (*++p) 172258805Seric { 172358805Seric case 'a': 172458805Seric map->map_app = ++p; 172558805Seric break; 172658805Seric } 172758805Seric while (*p != '\0' && !(isascii(*p) && isspace(*p))) 172858805Seric p++; 172958805Seric if (*p != '\0') 173058805Seric *p = '\0'; 173158805Seric } 173258805Seric if (map->map_app != NULL) 173358805Seric map->map_app = newstr(map->map_app); 173458805Seric 173558802Seric return TRUE; 173658802Seric } 173758802Seric /* 173858802Seric ** DEQUOTE_MAP -- unquote an address 173958802Seric ** 174058802Seric ** Parameters: 174158802Seric ** map -- the internal map structure (ignored). 174258802Seric ** buf -- the buffer to dequote. 174358802Seric ** bufsiz -- the size of that buffer. 174458802Seric ** av -- arguments (ignored). 174559084Seric ** statp -- pointer to status out-parameter. 174658802Seric ** 174758802Seric ** Returns: 174858802Seric ** NULL -- if there were no quotes, or if the resulting 174958802Seric ** unquoted buffer would not be acceptable to prescan. 175058802Seric ** else -- The dequoted buffer. 175158802Seric */ 175258802Seric 175358802Seric char * 175459084Seric dequote_map(map, buf, bufsiz, av, statp) 175558802Seric MAP *map; 175658802Seric char buf[]; 175758802Seric int bufsiz; 175858802Seric char **av; 175959084Seric int *statp; 176058802Seric { 176158802Seric register char *p; 176258802Seric register char *q; 176358802Seric register char c; 176458802Seric int anglecnt; 176558802Seric int cmntcnt; 176658802Seric int quotecnt; 176759089Seric int spacecnt; 176858802Seric bool quotemode; 176958802Seric bool bslashmode; 177058802Seric 177158802Seric anglecnt = 0; 177258802Seric cmntcnt = 0; 177358802Seric quotecnt = 0; 177459089Seric spacecnt = 0; 177558802Seric quotemode = FALSE; 177658802Seric bslashmode = FALSE; 177758802Seric 177858802Seric for (p = q = buf; (c = *p++) != '\0'; ) 177958802Seric { 178058802Seric if (bslashmode) 178158802Seric { 178258802Seric bslashmode = FALSE; 178358802Seric *q++ = c; 178458802Seric continue; 178558802Seric } 178658802Seric 178758802Seric switch (c) 178858802Seric { 178958802Seric case '\\': 179058802Seric bslashmode = TRUE; 179158802Seric break; 179258802Seric 179358802Seric case '(': 179458802Seric cmntcnt++; 179558802Seric break; 179658802Seric 179758802Seric case ')': 179858802Seric if (cmntcnt-- <= 0) 179958802Seric return NULL; 180058802Seric break; 180159089Seric 180259089Seric case ' ': 180359089Seric spacecnt++; 180459089Seric break; 180558802Seric } 180658802Seric 180758802Seric if (cmntcnt > 0) 180858802Seric { 180958802Seric *q++ = c; 181058802Seric continue; 181158802Seric } 181258802Seric 181358802Seric switch (c) 181458802Seric { 181558802Seric case '"': 181658802Seric quotemode = !quotemode; 181758802Seric quotecnt++; 181858802Seric continue; 181958802Seric 182058802Seric case '<': 182158802Seric anglecnt++; 182258802Seric break; 182358802Seric 182458802Seric case '>': 182558802Seric if (anglecnt-- <= 0) 182658802Seric return NULL; 182758802Seric break; 182858802Seric } 182958802Seric *q++ = c; 183058802Seric } 183158802Seric 183258802Seric if (anglecnt != 0 || cmntcnt != 0 || bslashmode || 183359089Seric quotemode || quotecnt <= 0 || spacecnt != 0) 183458802Seric return NULL; 183558802Seric *q++ = '\0'; 183658802Seric return buf; 183758802Seric } 1838