122976Smiriam /* 234921Sbostic * Copyright (c) 1983 Eric P. Allman 363589Sbostic * Copyright (c) 1988, 1993 463589Sbostic * The Regents of the University of California. All rights reserved. 533730Sbostic * 642828Sbostic * %sccs.include.redist.c% 733730Sbostic */ 822976Smiriam 922976Smiriam #ifndef lint 10*63756Seric static char sccsid[] = "@(#)parseaddr.c 8.3 (Berkeley) 07/11/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 ADDRESS *buildaddr(); 7357388Seric extern bool invalidaddr(); 74297Seric 75297Seric /* 76297Seric ** Initialize and prescan address. 77297Seric */ 78297Seric 7956678Seric e->e_to = addr; 807675Seric if (tTd(20, 1)) 819888Seric printf("\n--parseaddr(%s)\n", addr); 823188Seric 8357388Seric if (invalidaddr(addr)) 8457388Seric { 8557388Seric if (tTd(20, 1)) 8657388Seric printf("parseaddr-->bad address\n"); 8757388Seric return NULL; 8857388Seric } 8957388Seric 9058333Seric if (delimptr == NULL) 9158333Seric delimptr = &delimptrbuf; 9258333Seric 9358333Seric pvp = prescan(addr, delim, pvpbuf, delimptr); 943149Seric if (pvp == NULL) 9556729Seric { 9656729Seric if (tTd(20, 1)) 9756729Seric printf("parseaddr-->NULL\n"); 98297Seric return (NULL); 9956729Seric } 100297Seric 101297Seric /* 1023149Seric ** Apply rewriting rules. 1037889Seric ** Ruleset 0 does basic parsing. It must resolve. 104297Seric */ 105297Seric 10659084Seric queueup = FALSE; 10759084Seric if (rewrite(pvp, 3, e) == EX_TEMPFAIL) 10859084Seric queueup = TRUE; 10959084Seric if (rewrite(pvp, 0, e) == EX_TEMPFAIL) 11059084Seric queueup = TRUE; 111297Seric 1123149Seric /* 1133149Seric ** See if we resolved to a real mailer. 1143149Seric */ 115297Seric 11659040Seric if (pvp[0] == NULL || (pvp[0][0] & 0377) != CANONNET) 1173149Seric { 1183149Seric setstat(EX_USAGE); 11963753Seric syserr("554 cannot resolve name %s", addr); 1203149Seric return (NULL); 121297Seric } 122297Seric 123297Seric /* 1243149Seric ** Build canonical address from pvp. 125297Seric */ 126297Seric 12758966Seric a = buildaddr(pvp, a, e); 1284279Seric if (a == NULL) 1294279Seric return (NULL); 130297Seric 131297Seric /* 1323149Seric ** Make local copies of the host & user and then 1333149Seric ** transport them out. 134297Seric */ 135297Seric 13658333Seric allocaddr(a, copyf, addr, *delimptr); 13756678Seric 13856678Seric /* 13959084Seric ** If there was a parsing failure, mark it for queueing. 14059084Seric */ 14159084Seric 14259084Seric if (queueup) 14359595Seric { 14459734Seric char *msg = "Transient parse error -- message queued for future delivery"; 14559734Seric 14659595Seric if (tTd(20, 1)) 14759595Seric printf("parseaddr: queuing message\n"); 14859734Seric message(msg); 14959734Seric if (e->e_message == NULL) 15060009Seric e->e_message = newstr(msg); 15159084Seric a->q_flags |= QQUEUEUP; 15259595Seric } 15359084Seric 15459084Seric /* 15556678Seric ** Compute return value. 15656678Seric */ 15756678Seric 15856678Seric if (tTd(20, 1)) 1598078Seric { 16056678Seric printf("parseaddr-->"); 16156678Seric printaddr(a, FALSE); 16256678Seric } 16356678Seric 16456678Seric return (a); 16556678Seric } 16656678Seric /* 16757388Seric ** INVALIDADDR -- check for address containing meta-characters 16857388Seric ** 16957388Seric ** Parameters: 17057388Seric ** addr -- the address to check. 17157388Seric ** 17257388Seric ** Returns: 17357388Seric ** TRUE -- if the address has any "wierd" characters 17457388Seric ** FALSE -- otherwise. 17557388Seric */ 17657388Seric 17757388Seric bool 17857388Seric invalidaddr(addr) 17957388Seric register char *addr; 18057388Seric { 18157388Seric for (; *addr != '\0'; addr++) 18257388Seric { 18358050Seric if ((*addr & 0340) != 0200) 18457388Seric continue; 18557388Seric setstat(EX_USAGE); 18658151Seric usrerr("553 Address contained invalid control characters"); 18757388Seric return TRUE; 18857388Seric } 18957388Seric return FALSE; 19057388Seric } 19157388Seric /* 19256678Seric ** ALLOCADDR -- do local allocations of address on demand. 19356678Seric ** 19456678Seric ** Also lowercases the host name if requested. 19556678Seric ** 19656678Seric ** Parameters: 19756678Seric ** a -- the address to reallocate. 19856678Seric ** copyf -- the copy flag (see parseaddr for description). 19956678Seric ** paddr -- the printname of the address. 20058333Seric ** delimptr -- a pointer to the address delimiter. Must be set. 20156678Seric ** 20256678Seric ** Returns: 20356678Seric ** none. 20456678Seric ** 20556678Seric ** Side Effects: 20656678Seric ** Copies portions of a into local buffers as requested. 20756678Seric */ 20856678Seric 20958333Seric allocaddr(a, copyf, paddr, delimptr) 21056678Seric register ADDRESS *a; 21156678Seric int copyf; 21256678Seric char *paddr; 21358333Seric char *delimptr; 21456678Seric { 21558673Seric if (tTd(24, 4)) 21658673Seric printf("allocaddr(copyf=%d, paddr=%s)\n", copyf, paddr); 21758673Seric 21856678Seric if (copyf > 0 && paddr != NULL) 21956678Seric { 22058333Seric char savec = *delimptr; 2218078Seric 22259747Seric if (savec != '\0') 22359747Seric *delimptr = '\0'; 22456678Seric a->q_paddr = newstr(paddr); 22559747Seric if (savec != '\0') 22659747Seric *delimptr = savec; 2278078Seric } 228297Seric else 22956678Seric a->q_paddr = paddr; 23024944Seric 23124944Seric if (a->q_user == NULL) 23224944Seric a->q_user = ""; 23324944Seric if (a->q_host == NULL) 23424944Seric a->q_host = ""; 23524944Seric 2363149Seric if (copyf >= 0) 237297Seric { 23824944Seric a->q_host = newstr(a->q_host); 2393149Seric if (a->q_user != a->q_paddr) 2403149Seric a->q_user = newstr(a->q_user); 241297Seric } 242297Seric 24356678Seric if (a->q_paddr == NULL) 24456678Seric a->q_paddr = a->q_user; 245297Seric } 246297Seric /* 247297Seric ** PRESCAN -- Prescan name and make it canonical 248297Seric ** 2499374Seric ** Scans a name and turns it into a set of tokens. This process 2509374Seric ** deletes blanks and comments (in parentheses). 251297Seric ** 252297Seric ** This routine knows about quoted strings and angle brackets. 253297Seric ** 254297Seric ** There are certain subtleties to this routine. The one that 255297Seric ** comes to mind now is that backslashes on the ends of names 256297Seric ** are silently stripped off; this is intentional. The problem 257297Seric ** is that some versions of sndmsg (like at LBL) set the kill 258297Seric ** character to something other than @ when reading addresses; 259297Seric ** so people type "csvax.eric\@berkeley" -- which screws up the 260297Seric ** berknet mailer. 261297Seric ** 262297Seric ** Parameters: 263297Seric ** addr -- the name to chomp. 264297Seric ** delim -- the delimiter for the address, normally 265297Seric ** '\0' or ','; \0 is accepted in any case. 26615284Seric ** If '\t' then we are reading the .cf file. 26716914Seric ** pvpbuf -- place to put the saved text -- note that 26816914Seric ** the pointers are static. 26958333Seric ** delimptr -- if non-NULL, set to the location of the 27058333Seric ** terminating delimiter. 271297Seric ** 272297Seric ** Returns: 2733149Seric ** A pointer to a vector of tokens. 274297Seric ** NULL on error. 275297Seric */ 276297Seric 2778078Seric /* states and character types */ 2788078Seric # define OPR 0 /* operator */ 2798078Seric # define ATM 1 /* atom */ 2808078Seric # define QST 2 /* in quoted string */ 2818078Seric # define SPC 3 /* chewing up spaces */ 2828078Seric # define ONE 4 /* pick up one character */ 2833149Seric 2848078Seric # define NSTATES 5 /* number of states */ 2858078Seric # define TYPE 017 /* mask to select state type */ 2868078Seric 2878078Seric /* meta bits for table */ 2888078Seric # define M 020 /* meta character; don't pass through */ 2898078Seric # define B 040 /* cause a break */ 2908078Seric # define MB M|B /* meta-break */ 2918078Seric 2928078Seric static short StateTab[NSTATES][NSTATES] = 2938078Seric { 2948087Seric /* oldst chtype> OPR ATM QST SPC ONE */ 2959051Seric /*OPR*/ OPR|B, ATM|B, QST|B, SPC|MB, ONE|B, 2969051Seric /*ATM*/ OPR|B, ATM, QST|B, SPC|MB, ONE|B, 2979051Seric /*QST*/ QST, QST, OPR, QST, QST, 2988078Seric /*SPC*/ OPR, ATM, QST, SPC|M, ONE, 2998078Seric /*ONE*/ OPR, OPR, OPR, OPR, OPR, 3008078Seric }; 3018078Seric 3028078Seric # define NOCHAR -1 /* signal nothing in lookahead token */ 3038078Seric 3043149Seric char ** 30558333Seric prescan(addr, delim, pvpbuf, delimptr) 306297Seric char *addr; 307297Seric char delim; 30816914Seric char pvpbuf[]; 30958333Seric char **delimptr; 310297Seric { 311297Seric register char *p; 3128078Seric register char *q; 3139346Seric register int c; 3143149Seric char **avp; 315297Seric bool bslashmode; 316297Seric int cmntcnt; 3178423Seric int anglecnt; 3183149Seric char *tok; 3198078Seric int state; 3208078Seric int newstate; 32159747Seric char *saveto = CurEnv->e_to; 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; 33659747Seric CurEnv->e_to = p; 33756764Seric if (tTd(22, 11)) 338297Seric { 3398078Seric printf("prescan: "); 3408078Seric xputs(p); 34123109Seric (void) putchar('\n'); 3428078Seric } 3438078Seric 3448078Seric do 3458078Seric { 3463149Seric /* read a token */ 3473149Seric tok = q; 3488078Seric for (;;) 349297Seric { 3508078Seric /* store away any old lookahead character */ 35159277Seric if (c != NOCHAR && !bslashmode) 3528078Seric { 35315284Seric /* see if there is room */ 35416914Seric if (q >= &pvpbuf[PSBUFSIZE - 5]) 3558078Seric { 35658151Seric usrerr("553 Address too long"); 35758333Seric if (delimptr != NULL) 35858333Seric *delimptr = p; 35959747Seric CurEnv->e_to = saveto; 3608078Seric return (NULL); 3618078Seric } 36215284Seric 36315284Seric /* squirrel it away */ 3648078Seric *q++ = c; 3658078Seric } 3668078Seric 3678078Seric /* read a new input character */ 3688078Seric c = *p++; 36957631Seric if (c == '\0') 37056764Seric { 37156764Seric /* diagnose and patch up bad syntax */ 37256764Seric if (state == QST) 37356764Seric { 37458151Seric usrerr("553 Unbalanced '\"'"); 37556764Seric c = '"'; 37656764Seric } 37756764Seric else if (cmntcnt > 0) 37856764Seric { 37958151Seric usrerr("553 Unbalanced '('"); 38056764Seric c = ')'; 38156764Seric } 38256764Seric else if (anglecnt > 0) 38356764Seric { 38456764Seric c = '>'; 38558151Seric usrerr("553 Unbalanced '<'"); 38656764Seric } 38756764Seric else 38856764Seric break; 38915284Seric 39056764Seric p--; 39156764Seric } 39257631Seric else if (c == delim && anglecnt <= 0 && 39357631Seric cmntcnt <= 0 && state != QST) 39457631Seric break; 39556764Seric 3968078Seric if (tTd(22, 101)) 3978078Seric printf("c=%c, s=%d; ", c, state); 3988078Seric 3993149Seric /* chew up special characters */ 4003149Seric *q = '\0'; 4013149Seric if (bslashmode) 4023149Seric { 40359105Seric bslashmode = FALSE; 40459105Seric 40524944Seric /* kludge \! for naive users */ 40658061Seric if (cmntcnt > 0) 40759105Seric { 40858061Seric c = NOCHAR; 40959105Seric continue; 41059105Seric } 41159105Seric else if (c != '!' || state == QST) 41259105Seric { 41356678Seric *q++ = '\\'; 41459105Seric continue; 41559105Seric } 4163149Seric } 41756678Seric 41856678Seric if (c == '\\') 4193149Seric { 4203149Seric bslashmode = TRUE; 4213149Seric } 42256678Seric else if (state == QST) 4238514Seric { 4248514Seric /* do nothing, just avoid next clauses */ 4258514Seric } 4268078Seric else if (c == '(') 4274100Seric { 4288078Seric cmntcnt++; 4298078Seric c = NOCHAR; 4304100Seric } 4318078Seric else if (c == ')') 4323149Seric { 4338078Seric if (cmntcnt <= 0) 4343149Seric { 43558151Seric usrerr("553 Unbalanced ')'"); 43658333Seric if (delimptr != NULL) 43758333Seric *delimptr = p; 43859747Seric CurEnv->e_to = saveto; 4398078Seric return (NULL); 4403149Seric } 4418078Seric else 4428078Seric cmntcnt--; 4438078Seric } 4448078Seric else if (cmntcnt > 0) 4458078Seric c = NOCHAR; 4468423Seric else if (c == '<') 4478423Seric anglecnt++; 4488423Seric else if (c == '>') 4498423Seric { 4508423Seric if (anglecnt <= 0) 4518423Seric { 45258151Seric usrerr("553 Unbalanced '>'"); 45358333Seric if (delimptr != NULL) 45458333Seric *delimptr = p; 45559747Seric CurEnv->e_to = saveto; 4568423Seric return (NULL); 4578423Seric } 4588423Seric anglecnt--; 4598423Seric } 46058050Seric else if (delim == ' ' && isascii(c) && isspace(c)) 46111423Seric c = ' '; 4623149Seric 4638078Seric if (c == NOCHAR) 4648078Seric continue; 4653149Seric 4668078Seric /* see if this is end of input */ 46711405Seric if (c == delim && anglecnt <= 0 && state != QST) 4683149Seric break; 4693149Seric 4708078Seric newstate = StateTab[state][toktype(c)]; 4718078Seric if (tTd(22, 101)) 4728078Seric printf("ns=%02o\n", newstate); 4738078Seric state = newstate & TYPE; 4748078Seric if (bitset(M, newstate)) 4758078Seric c = NOCHAR; 4768078Seric if (bitset(B, newstate)) 4774228Seric break; 478297Seric } 4793149Seric 4803149Seric /* new token */ 4818078Seric if (tok != q) 4821378Seric { 4838078Seric *q++ = '\0'; 4848078Seric if (tTd(22, 36)) 485297Seric { 4868078Seric printf("tok="); 4878078Seric xputs(tok); 48823109Seric (void) putchar('\n'); 489297Seric } 4908078Seric if (avp >= &av[MAXATOM]) 491297Seric { 49258151Seric syserr("553 prescan: too many tokens"); 49358333Seric if (delimptr != NULL) 49458333Seric *delimptr = p; 49559747Seric CurEnv->e_to = saveto; 4968078Seric return (NULL); 497297Seric } 4988078Seric *avp++ = tok; 499297Seric } 5008423Seric } while (c != '\0' && (c != delim || anglecnt > 0)); 5013149Seric *avp = NULL; 50258333Seric p--; 50358333Seric if (delimptr != NULL) 50458333Seric *delimptr = p; 50556764Seric if (tTd(22, 12)) 50656764Seric { 50756764Seric printf("prescan==>"); 50856764Seric printav(av); 50956764Seric } 51059747Seric CurEnv->e_to = saveto; 51158546Seric if (av[0] == NULL) 51258546Seric return (NULL); 51358403Seric return (av); 5143149Seric } 5153149Seric /* 5163149Seric ** TOKTYPE -- return token type 5173149Seric ** 5183149Seric ** Parameters: 5193149Seric ** c -- the character in question. 5203149Seric ** 5213149Seric ** Returns: 5223149Seric ** Its type. 5233149Seric ** 5243149Seric ** Side Effects: 5253149Seric ** none. 5263149Seric */ 527297Seric 5283149Seric toktype(c) 52958050Seric register int c; 5303149Seric { 5313380Seric static char buf[50]; 5323382Seric static bool firstime = TRUE; 5333380Seric 5343382Seric if (firstime) 5353380Seric { 5363382Seric firstime = FALSE; 53758050Seric expand("\201o", buf, &buf[sizeof buf - 1], CurEnv); 5387005Seric (void) strcat(buf, DELIMCHARS); 5393380Seric } 54058050Seric c &= 0377; 54156327Seric if (c == MATCHCLASS || c == MATCHREPL || c == MATCHNCLASS) 5428078Seric return (ONE); 54359027Seric if (c == MACRODEXPAND) 54459027Seric return (ONE); 5458078Seric if (c == '"') 5468078Seric return (QST); 54758050Seric if ((c & 0340) == 0200) 54858050Seric return (OPR); 5494100Seric if (!isascii(c)) 5508078Seric return (ATM); 5518078Seric if (isspace(c) || c == ')') 5528078Seric return (SPC); 55358050Seric if (strchr(buf, c) != NULL) 5548078Seric return (OPR); 5558078Seric return (ATM); 5563149Seric } 5573149Seric /* 5583149Seric ** REWRITE -- apply rewrite rules to token vector. 5593149Seric ** 5604476Seric ** This routine is an ordered production system. Each rewrite 5614476Seric ** rule has a LHS (called the pattern) and a RHS (called the 5624476Seric ** rewrite); 'rwr' points the the current rewrite rule. 5634476Seric ** 5644476Seric ** For each rewrite rule, 'avp' points the address vector we 5654476Seric ** are trying to match against, and 'pvp' points to the pattern. 5668058Seric ** If pvp points to a special match value (MATCHZANY, MATCHANY, 5679585Seric ** MATCHONE, MATCHCLASS, MATCHNCLASS) then the address in avp 5689585Seric ** matched is saved away in the match vector (pointed to by 'mvp'). 5694476Seric ** 5704476Seric ** When a match between avp & pvp does not match, we try to 5719585Seric ** back out. If we back up over MATCHONE, MATCHCLASS, or MATCHNCLASS 5724476Seric ** we must also back out the match in mvp. If we reach a 5738058Seric ** MATCHANY or MATCHZANY we just extend the match and start 5748058Seric ** over again. 5754476Seric ** 5764476Seric ** When we finally match, we rewrite the address vector 5774476Seric ** and try over again. 5784476Seric ** 5793149Seric ** Parameters: 5803149Seric ** pvp -- pointer to token vector. 58159027Seric ** ruleset -- the ruleset to use for rewriting. 58259027Seric ** e -- the current envelope. 5833149Seric ** 5843149Seric ** Returns: 58559084Seric ** A status code. If EX_TEMPFAIL, higher level code should 58659084Seric ** attempt recovery. 5873149Seric ** 5883149Seric ** Side Effects: 5893149Seric ** pvp is modified. 5903149Seric */ 5912091Seric 5923149Seric struct match 5933149Seric { 5944468Seric char **first; /* first token matched */ 5954468Seric char **last; /* last token matched */ 59658825Seric char **pattern; /* pointer to pattern */ 5973149Seric }; 5983149Seric 5994468Seric # define MAXMATCH 9 /* max params per rewrite */ 6003149Seric 6013149Seric 60259084Seric int 60359027Seric rewrite(pvp, ruleset, e) 6043149Seric char **pvp; 6054070Seric int ruleset; 60659027Seric register ENVELOPE *e; 6073149Seric { 6083149Seric register char *ap; /* address pointer */ 6093149Seric register char *rp; /* rewrite pointer */ 6103149Seric register char **avp; /* address vector pointer */ 6113149Seric register char **rvp; /* rewrite vector pointer */ 6128058Seric register struct match *mlp; /* cur ptr into mlist */ 6138058Seric register struct rewrite *rwr; /* pointer to current rewrite rule */ 61458866Seric int ruleno; /* current rule number */ 61559084Seric int rstat = EX_OK; /* return status */ 61656678Seric struct match mlist[MAXMATCH]; /* stores match on LHS */ 6173149Seric char *npvp[MAXATOM+1]; /* temporary space for rebuild */ 6183149Seric 6199279Seric if (OpMode == MD_TEST || tTd(21, 2)) 6203149Seric { 6218959Seric printf("rewrite: ruleset %2d input:", ruleset); 62256678Seric printav(pvp); 6233149Seric } 62456678Seric if (ruleset < 0 || ruleset >= MAXRWSETS) 62556326Seric { 62658151Seric syserr("554 rewrite: illegal ruleset number %d", ruleset); 62759084Seric return EX_CONFIG; 62856326Seric } 62956678Seric if (pvp == NULL) 63059084Seric return EX_USAGE; 63156326Seric 6323149Seric /* 63356678Seric ** Run through the list of rewrite rules, applying 63456678Seric ** any that match. 6353149Seric */ 6363149Seric 63758866Seric ruleno = 1; 6384070Seric for (rwr = RewriteRules[ruleset]; rwr != NULL; ) 6393149Seric { 64056678Seric int loopcount = 0; 64156678Seric 6427675Seric if (tTd(21, 12)) 643297Seric { 6448069Seric printf("-----trying rule:"); 64556678Seric printav(rwr->r_lhs); 6463149Seric } 6473149Seric 6483149Seric /* try to match on this rule */ 6494468Seric mlp = mlist; 6508058Seric rvp = rwr->r_lhs; 6518058Seric avp = pvp; 65258866Seric if (++loopcount > 100) 6533149Seric { 65458866Seric syserr("554 Infinite loop in ruleset %d, rule %d", 65558866Seric ruleset, ruleno); 65658866Seric if (tTd(21, 1)) 65752637Seric { 65856678Seric printf("workspace: "); 65956678Seric printav(pvp); 66052637Seric } 66158866Seric break; 66258866Seric } 66358866Seric 66458866Seric while ((ap = *avp) != NULL || *rvp != NULL) 66558866Seric { 6663149Seric rp = *rvp; 6678058Seric if (tTd(21, 35)) 6688058Seric { 66958825Seric printf("ADVANCE rp="); 67057531Seric xputs(rp); 67157532Seric printf(", ap="); 6728058Seric xputs(ap); 6738069Seric printf("\n"); 6748058Seric } 67556678Seric if (rp == NULL) 67656326Seric { 6773149Seric /* end-of-pattern before end-of-address */ 6788058Seric goto backup; 67956678Seric } 68058173Seric if (ap == NULL && (*rp & 0377) != MATCHZANY && 68158827Seric (*rp & 0377) != MATCHZERO) 68256326Seric { 68358825Seric /* end-of-input with patterns left */ 68458825Seric goto backup; 685297Seric } 68656326Seric 68758050Seric switch (*rp & 0377) 6888058Seric { 68956678Seric register STAB *s; 69058814Seric char buf[MAXLINE]; 69156326Seric 69256678Seric case MATCHCLASS: 69358825Seric /* match any phrase in a class */ 69458825Seric mlp->pattern = rvp; 69558814Seric mlp->first = avp; 69658814Seric extendclass: 69758825Seric ap = *avp; 69858825Seric if (ap == NULL) 69958814Seric goto backup; 70058814Seric mlp->last = avp++; 70158814Seric cataddr(mlp->first, mlp->last, buf, sizeof buf, '\0'); 70258814Seric s = stab(buf, ST_CLASS, ST_FIND); 70356678Seric if (s == NULL || !bitnset(rp[1], s->s_class)) 70456326Seric { 70558825Seric if (tTd(21, 36)) 70658825Seric { 70758825Seric printf("EXTEND rp="); 70858825Seric xputs(rp); 70958825Seric printf(", ap="); 71058825Seric xputs(ap); 71158825Seric printf("\n"); 71258825Seric } 71358825Seric goto extendclass; 71456326Seric } 71558825Seric if (tTd(21, 36)) 71658825Seric printf("CLMATCH\n"); 71758814Seric mlp++; 71858814Seric break; 7194060Seric 72058825Seric case MATCHNCLASS: 72158825Seric /* match any token not in a class */ 72258825Seric s = stab(ap, ST_CLASS, ST_FIND); 72358825Seric if (s != NULL && bitnset(rp[1], s->s_class)) 72458825Seric goto backup; 72558825Seric 72658825Seric /* fall through */ 72758825Seric 72856678Seric case MATCHONE: 72956678Seric case MATCHANY: 73056678Seric /* match exactly one token */ 73158825Seric mlp->pattern = rvp; 73256678Seric mlp->first = avp; 73356678Seric mlp->last = avp++; 7348058Seric mlp++; 73556678Seric break; 7368058Seric 73756678Seric case MATCHZANY: 73856678Seric /* match zero or more tokens */ 73958825Seric mlp->pattern = rvp; 74056678Seric mlp->first = avp; 74156678Seric mlp->last = avp - 1; 74256678Seric mlp++; 74356678Seric break; 74456326Seric 74558827Seric case MATCHZERO: 74658173Seric /* match zero tokens */ 74758173Seric break; 74858173Seric 74959027Seric case MACRODEXPAND: 75059027Seric /* 75159027Seric ** Match against run-time macro. 75259027Seric ** This algorithm is broken for the 75359027Seric ** general case (no recursive macros, 75459027Seric ** improper tokenization) but should 75559027Seric ** work for the usual cases. 75659027Seric */ 75759027Seric 75859027Seric ap = macvalue(rp[1], e); 75959027Seric mlp->first = avp; 76059027Seric if (tTd(21, 2)) 76159027Seric printf("rewrite: LHS $&%c => \"%s\"\n", 76259027Seric rp[1], 76359027Seric ap == NULL ? "(NULL)" : ap); 76459027Seric 76559027Seric if (ap == NULL) 76659027Seric break; 76760502Seric while (*ap != '\0') 76859027Seric { 76959027Seric if (*avp == NULL || 77059027Seric strncasecmp(ap, *avp, strlen(*avp)) != 0) 77159027Seric { 77259027Seric /* no match */ 77359027Seric avp = mlp->first; 77459027Seric goto backup; 77559027Seric } 77659027Seric ap += strlen(*avp++); 77759027Seric } 77859027Seric 77959027Seric /* match */ 78059027Seric break; 78159027Seric 78256678Seric default: 78356678Seric /* must have exact match */ 78456678Seric if (strcasecmp(rp, ap)) 7858058Seric goto backup; 7864468Seric avp++; 78756678Seric break; 7883149Seric } 7893149Seric 79056678Seric /* successful match on this token */ 7913149Seric rvp++; 7923149Seric continue; 7933149Seric 79458825Seric backup: 79556678Seric /* match failed -- back up */ 79658825Seric while (--mlp >= mlist) 7973149Seric { 79858825Seric rvp = mlp->pattern; 79956678Seric rp = *rvp; 80058825Seric avp = mlp->last + 1; 80158825Seric ap = *avp; 80258825Seric 80358825Seric if (tTd(21, 36)) 80458825Seric { 80558825Seric printf("BACKUP rp="); 80658825Seric xputs(rp); 80758825Seric printf(", ap="); 80858825Seric xputs(ap); 80958825Seric printf("\n"); 81058825Seric } 81158825Seric 81258825Seric if (ap == NULL) 81358825Seric { 81458825Seric /* run off the end -- back up again */ 81558825Seric continue; 81658825Seric } 81758050Seric if ((*rp & 0377) == MATCHANY || 81858050Seric (*rp & 0377) == MATCHZANY) 8194468Seric { 82056678Seric /* extend binding and continue */ 82158825Seric mlp->last = avp++; 82256678Seric rvp++; 82358825Seric mlp++; 82456678Seric break; 8254468Seric } 82658825Seric if ((*rp & 0377) == MATCHCLASS) 82756678Seric { 82858814Seric /* extend binding and try again */ 82963397Seric mlp->last = avp; 83058814Seric goto extendclass; 83158814Seric } 8323149Seric } 8333149Seric 83458825Seric if (mlp < mlist) 83556678Seric { 83656678Seric /* total failure to match */ 83756326Seric break; 8383149Seric } 839297Seric } 8403149Seric 8413149Seric /* 84256678Seric ** See if we successfully matched 8433149Seric */ 8443149Seric 84558827Seric if (mlp < mlist || *rvp != NULL) 8463149Seric { 8479374Seric if (tTd(21, 10)) 8489374Seric printf("----- rule fails\n"); 8499374Seric rwr = rwr->r_next; 85058866Seric ruleno++; 8519374Seric continue; 8529374Seric } 8533149Seric 8549374Seric rvp = rwr->r_rhs; 8559374Seric if (tTd(21, 12)) 8569374Seric { 8579374Seric printf("-----rule matches:"); 85856678Seric printav(rvp); 8599374Seric } 8609374Seric 8619374Seric rp = *rvp; 86258050Seric if ((*rp & 0377) == CANONUSER) 8639374Seric { 8649374Seric rvp++; 8659374Seric rwr = rwr->r_next; 86658866Seric ruleno++; 8679374Seric } 86858050Seric else if ((*rp & 0377) == CANONHOST) 8699374Seric { 8709374Seric rvp++; 8719374Seric rwr = NULL; 8729374Seric } 87358050Seric else if ((*rp & 0377) == CANONNET) 8749374Seric rwr = NULL; 8759374Seric 8769374Seric /* substitute */ 8779374Seric for (avp = npvp; *rvp != NULL; rvp++) 8789374Seric { 8799374Seric register struct match *m; 8809374Seric register char **pp; 8819374Seric 8828058Seric rp = *rvp; 88358050Seric if ((*rp & 0377) == MATCHREPL) 8848058Seric { 88516914Seric /* substitute from LHS */ 88616914Seric m = &mlist[rp[1] - '1']; 88756678Seric if (m < mlist || m >= mlp) 8889374Seric { 88958151Seric syserr("554 rewrite: ruleset %d: replacement $%c out of bounds", 89056326Seric ruleset, rp[1]); 89159084Seric return EX_CONFIG; 8929374Seric } 89316914Seric if (tTd(21, 15)) 89416914Seric { 89516914Seric printf("$%c:", rp[1]); 89616914Seric pp = m->first; 89756678Seric while (pp <= m->last) 89816914Seric { 89916914Seric printf(" %x=\"", *pp); 90016914Seric (void) fflush(stdout); 90116914Seric printf("%s\"", *pp++); 90216914Seric } 90316914Seric printf("\n"); 90416914Seric } 9059374Seric pp = m->first; 90656678Seric while (pp <= m->last) 9073149Seric { 90816914Seric if (avp >= &npvp[MAXATOM]) 90956678Seric { 91058151Seric syserr("554 rewrite: expansion too long"); 91159084Seric return EX_DATAERR; 91256678Seric } 91316914Seric *avp++ = *pp++; 9143149Seric } 9153149Seric } 91616914Seric else 9178226Seric { 91816914Seric /* vanilla replacement */ 9199374Seric if (avp >= &npvp[MAXATOM]) 92016889Seric { 92156678Seric toolong: 92258151Seric syserr("554 rewrite: expansion too long"); 92359084Seric return EX_DATAERR; 92416889Seric } 92559027Seric if ((*rp & 0377) != MACRODEXPAND) 92659027Seric *avp++ = rp; 92759027Seric else 92859027Seric { 92959027Seric *avp = macvalue(rp[1], e); 93059027Seric if (tTd(21, 2)) 93159027Seric printf("rewrite: RHS $&%c => \"%s\"\n", 93259027Seric rp[1], 93359027Seric *avp == NULL ? "(NULL)" : *avp); 93459027Seric if (*avp != NULL) 93559027Seric avp++; 93659027Seric } 9378226Seric } 9389374Seric } 9399374Seric *avp++ = NULL; 94016914Seric 94116914Seric /* 94256678Seric ** Check for any hostname/keyword lookups. 94316914Seric */ 94416914Seric 94516914Seric for (rvp = npvp; *rvp != NULL; rvp++) 94616914Seric { 94756678Seric char **hbrvp; 94816914Seric char **xpvp; 94916914Seric int trsize; 95056678Seric char *replac; 95156678Seric int endtoken; 95256678Seric STAB *map; 95356678Seric char *mapname; 95456678Seric char **key_rvp; 95556678Seric char **arg_rvp; 95656678Seric char **default_rvp; 95756678Seric char buf[MAXNAME + 1]; 95816914Seric char *pvpb1[MAXATOM + 1]; 95956823Seric char *argvect[10]; 96017174Seric char pvpbuf[PSBUFSIZE]; 96116914Seric 96258050Seric if ((**rvp & 0377) != HOSTBEGIN && 96358050Seric (**rvp & 0377) != LOOKUPBEGIN) 96416914Seric continue; 96516914Seric 96616914Seric /* 96756678Seric ** Got a hostname/keyword lookup. 96816914Seric ** 96916914Seric ** This could be optimized fairly easily. 97016914Seric */ 97116914Seric 97216914Seric hbrvp = rvp; 97358050Seric if ((**rvp & 0377) == HOSTBEGIN) 97456327Seric { 97556678Seric endtoken = HOSTEND; 97656678Seric mapname = "host"; 97756327Seric } 97856326Seric else 97956327Seric { 98056678Seric endtoken = LOOKUPEND; 98156678Seric mapname = *++rvp; 98256327Seric } 98356678Seric map = stab(mapname, ST_MAP, ST_FIND); 98456678Seric if (map == NULL) 98558151Seric syserr("554 rewrite: map %s not found", mapname); 98656678Seric 98756678Seric /* extract the match part */ 98856678Seric key_rvp = ++rvp; 98956823Seric default_rvp = NULL; 99056823Seric arg_rvp = argvect; 99156823Seric xpvp = NULL; 99256823Seric replac = pvpbuf; 99358050Seric while (*rvp != NULL && (**rvp & 0377) != endtoken) 99453654Seric { 99558050Seric int nodetype = **rvp & 0377; 99656823Seric 99756823Seric if (nodetype != CANONHOST && nodetype != CANONUSER) 99856678Seric { 99956823Seric rvp++; 100056823Seric continue; 100156823Seric } 100256823Seric 100356823Seric *rvp++ = NULL; 100456823Seric 100556823Seric if (xpvp != NULL) 100656823Seric { 100758814Seric cataddr(xpvp, NULL, replac, 100858082Seric &pvpbuf[sizeof pvpbuf] - replac, 100958082Seric '\0'); 101056823Seric *++arg_rvp = replac; 101156823Seric replac += strlen(replac) + 1; 101256823Seric xpvp = NULL; 101356823Seric } 101456823Seric switch (nodetype) 101556823Seric { 101656678Seric case CANONHOST: 101756823Seric xpvp = rvp; 101856678Seric break; 101956678Seric 102056678Seric case CANONUSER: 102156678Seric default_rvp = rvp; 102256678Seric break; 102356678Seric } 102453654Seric } 102516914Seric if (*rvp != NULL) 102616914Seric *rvp++ = NULL; 102756823Seric if (xpvp != NULL) 102856823Seric { 102958814Seric cataddr(xpvp, NULL, replac, 103058082Seric &pvpbuf[sizeof pvpbuf] - replac, 103158082Seric '\0'); 103256823Seric *++arg_rvp = replac; 103356823Seric } 103456823Seric *++arg_rvp = NULL; 103516914Seric 103616914Seric /* save the remainder of the input string */ 103716914Seric trsize = (int) (avp - rvp + 1) * sizeof *rvp; 103816914Seric bcopy((char *) rvp, (char *) pvpb1, trsize); 103916914Seric 104056678Seric /* look it up */ 104158814Seric cataddr(key_rvp, NULL, buf, sizeof buf, '\0'); 104256823Seric argvect[0] = buf; 104360538Seric if (map != NULL && bitset(MF_OPEN, map->s_map.map_mflags)) 104456836Seric { 104559084Seric auto int stat = EX_OK; 104656836Seric 104760215Seric /* XXX should try to auto-open the map here */ 104860215Seric 104958796Seric if (tTd(60, 1)) 105058796Seric printf("map_lookup(%s, %s) => ", 105158796Seric mapname, buf); 105256836Seric replac = (*map->s_map.map_class->map_lookup)(&map->s_map, 105360089Seric buf, argvect, &stat); 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 { 106960089Seric /* create the default */ 107060089Seric cataddr(default_rvp, NULL, buf, sizeof buf, '\0'); 107156823Seric replac = buf; 107256823Seric } 107356823Seric 107456678Seric if (replac == NULL) 107551317Seric { 107656823Seric xpvp = key_rvp; 107753654Seric } 107856678Seric else 107953654Seric { 108056678Seric /* scan the new replacement */ 108158333Seric xpvp = prescan(replac, '\0', pvpbuf, NULL); 108253654Seric if (xpvp == NULL) 108351317Seric { 108458403Seric /* prescan already printed error */ 108559084Seric return EX_DATAERR; 108656678Seric } 108751317Seric } 108851317Seric 108916914Seric /* append it to the token list */ 109056678Seric for (avp = hbrvp; *xpvp != NULL; xpvp++) 109156678Seric { 109217174Seric *avp++ = newstr(*xpvp); 109316920Seric if (avp >= &npvp[MAXATOM]) 109416914Seric goto toolong; 109517174Seric } 109616914Seric 109716914Seric /* restore the old trailing information */ 109856678Seric for (xpvp = pvpb1; (*avp++ = *xpvp++) != NULL; ) 109916920Seric if (avp >= &npvp[MAXATOM]) 110016914Seric goto toolong; 110117174Seric 110256678Seric break; 110316914Seric } 110416914Seric 110516914Seric /* 110616914Seric ** Check for subroutine calls. 110716914Seric */ 110816914Seric 110958050Seric if (*npvp != NULL && (**npvp & 0377) == CALLSUBR) 111056678Seric { 111159084Seric int stat; 111259084Seric 111356678Seric bcopy((char *) &npvp[2], (char *) pvp, 111456678Seric (int) (avp - npvp - 2) * sizeof *avp); 111556678Seric if (tTd(21, 3)) 111656678Seric printf("-----callsubr %s\n", npvp[1]); 111759084Seric stat = rewrite(pvp, atoi(npvp[1]), e); 111859084Seric if (rstat == EX_OK || stat == EX_TEMPFAIL) 111959084Seric rstat = stat; 112063581Seric if ((**pvp & 0377) == CANONNET) 112163581Seric rwr = NULL; 112256678Seric } 112356678Seric else 112456678Seric { 112517348Seric bcopy((char *) npvp, (char *) pvp, 112616900Seric (int) (avp - npvp) * sizeof *avp); 112756678Seric } 11289374Seric if (tTd(21, 4)) 11299374Seric { 11309374Seric printf("rewritten as:"); 113156678Seric printav(pvp); 11329374Seric } 1133297Seric } 11348069Seric 11359279Seric if (OpMode == MD_TEST || tTd(21, 2)) 11368069Seric { 11378959Seric printf("rewrite: ruleset %2d returns:", ruleset); 113856678Seric printav(pvp); 11398069Seric } 114059084Seric 114159084Seric return rstat; 11423149Seric } 11433149Seric /* 11443149Seric ** BUILDADDR -- build address from token vector. 11453149Seric ** 11463149Seric ** Parameters: 11473149Seric ** tv -- token vector. 11483149Seric ** a -- pointer to address descriptor to fill. 11493149Seric ** If NULL, one will be allocated. 115058966Seric ** e -- the current envelope. 11513149Seric ** 11523149Seric ** Returns: 11534279Seric ** NULL if there was an error. 11544279Seric ** 'a' otherwise. 11553149Seric ** 11563149Seric ** Side Effects: 11573149Seric ** fills in 'a' 11583149Seric */ 11593149Seric 116057249Seric struct errcodes 116157249Seric { 116257249Seric char *ec_name; /* name of error code */ 116357249Seric int ec_code; /* numeric code */ 116457249Seric } ErrorCodes[] = 116557249Seric { 116657249Seric "usage", EX_USAGE, 116757249Seric "nouser", EX_NOUSER, 116857249Seric "nohost", EX_NOHOST, 116957249Seric "unavailable", EX_UNAVAILABLE, 117057249Seric "software", EX_SOFTWARE, 117157249Seric "tempfail", EX_TEMPFAIL, 117257249Seric "protocol", EX_PROTOCOL, 117357249Seric #ifdef EX_CONFIG 117457249Seric "config", EX_CONFIG, 117557249Seric #endif 117657249Seric NULL, EX_UNAVAILABLE, 117757249Seric }; 117857249Seric 117956678Seric ADDRESS * 118058966Seric buildaddr(tv, a, e) 11813149Seric register char **tv; 11823149Seric register ADDRESS *a; 118358966Seric register ENVELOPE *e; 11843149Seric { 11853149Seric struct mailer **mp; 11863149Seric register struct mailer *m; 118758008Seric char *bp; 118858008Seric int spaceleft; 118957402Seric static char buf[MAXNAME]; 11903149Seric 11913149Seric if (a == NULL) 11923149Seric a = (ADDRESS *) xalloc(sizeof *a); 119316889Seric bzero((char *) a, sizeof *a); 11943149Seric 11953149Seric /* figure out what net/mailer to use */ 119658050Seric if ((**tv & 0377) != CANONNET) 11974279Seric { 119858151Seric syserr("554 buildaddr: no net"); 11994279Seric return (NULL); 12004279Seric } 12013149Seric tv++; 120258680Seric if (strcasecmp(*tv, "error") == 0) 12034279Seric { 120458050Seric if ((**++tv & 0377) == CANONHOST) 120510183Seric { 120657249Seric register struct errcodes *ep; 120757249Seric 120858050Seric if (isascii(**++tv) && isdigit(**tv)) 120957249Seric { 121057249Seric setstat(atoi(*tv)); 121157249Seric } 121257249Seric else 121357249Seric { 121457249Seric for (ep = ErrorCodes; ep->ec_name != NULL; ep++) 121557249Seric if (strcasecmp(ep->ec_name, *tv) == 0) 121657249Seric break; 121757249Seric setstat(ep->ec_code); 121857249Seric } 121910183Seric tv++; 122010183Seric } 122158050Seric if ((**tv & 0377) != CANONUSER) 122258151Seric syserr("554 buildaddr: error: no user"); 122358814Seric cataddr(++tv, NULL, buf, sizeof buf, ' '); 122458082Seric stripquotes(buf); 12254279Seric usrerr(buf); 12264279Seric return (NULL); 12274279Seric } 122857402Seric 12294598Seric for (mp = Mailer; (m = *mp++) != NULL; ) 12303149Seric { 123158680Seric if (strcasecmp(m->m_name, *tv) == 0) 12323149Seric break; 12333149Seric } 12343149Seric if (m == NULL) 12354279Seric { 123658151Seric syserr("554 buildaddr: unknown mailer %s", *tv); 12374279Seric return (NULL); 12384279Seric } 12394598Seric a->q_mailer = m; 12403149Seric 12413149Seric /* figure out what host (if any) */ 124256678Seric tv++; 124358509Seric if ((**tv & 0377) == CANONHOST) 12443149Seric { 124558008Seric bp = buf; 124658008Seric spaceleft = sizeof buf - 1; 124758050Seric while (*++tv != NULL && (**tv & 0377) != CANONUSER) 124858008Seric { 124958008Seric int i = strlen(*tv); 125058008Seric 125158008Seric if (i > spaceleft) 125258008Seric { 125358008Seric /* out of space for this address */ 125458008Seric if (spaceleft >= 0) 125558151Seric syserr("554 buildaddr: host too long (%.40s...)", 125658008Seric buf); 125758008Seric i = spaceleft; 125858008Seric spaceleft = 0; 125958008Seric } 126058008Seric if (i <= 0) 126158008Seric continue; 126258008Seric bcopy(*tv, bp, i); 126358008Seric bp += i; 126458008Seric spaceleft -= i; 126558008Seric } 126658010Seric *bp = '\0'; 12675704Seric a->q_host = newstr(buf); 12683149Seric } 126957249Seric else 127058509Seric { 127158509Seric if (!bitnset(M_LOCALMAILER, m->m_flags)) 127258509Seric { 127358509Seric syserr("554 buildaddr: no host"); 127458509Seric return (NULL); 127558509Seric } 127657249Seric a->q_host = NULL; 127758509Seric } 12783149Seric 12793149Seric /* figure out the user */ 128058050Seric if (*tv == NULL || (**tv & 0377) != CANONUSER) 12814279Seric { 128258151Seric syserr("554 buildaddr: no user"); 12834279Seric return (NULL); 12844279Seric } 128557402Seric tv++; 128651317Seric 128757402Seric /* do special mapping for local mailer */ 128857402Seric if (m == LocalMailer && *tv != NULL) 128957402Seric { 129057454Seric register char *p = *tv; 129157454Seric 129257454Seric if (*p == '"') 129357454Seric p++; 129457454Seric if (*p == '|') 129557402Seric a->q_mailer = m = ProgMailer; 129657454Seric else if (*p == '/') 129757402Seric a->q_mailer = m = FileMailer; 129857454Seric else if (*p == ':') 129957402Seric { 130057402Seric /* may be :include: */ 130158814Seric cataddr(tv, NULL, buf, sizeof buf, '\0'); 130258008Seric stripquotes(buf); 130358008Seric if (strncasecmp(buf, ":include:", 9) == 0) 130458008Seric { 130558008Seric /* if :include:, don't need further rewriting */ 130657402Seric a->q_mailer = m = InclMailer; 130758008Seric a->q_user = &buf[9]; 130858008Seric return (a); 130958008Seric } 131057402Seric } 131157402Seric } 131257402Seric 131358008Seric if (m == LocalMailer && *tv != NULL && strcmp(*tv, "@") == 0) 131458008Seric { 131558008Seric tv++; 131658008Seric a->q_flags |= QNOTREMOTE; 131758008Seric } 131858008Seric 131958673Seric /* do cleanup of final address */ 132059084Seric (void) rewrite(tv, 4, e); 132119040Seric 132219040Seric /* save the result for the command line/RCPT argument */ 132358814Seric cataddr(tv, NULL, buf, sizeof buf, '\0'); 13243149Seric a->q_user = buf; 13253149Seric 132658670Seric /* 132758670Seric ** Do mapping to lower case as requested by mailer 132858670Seric */ 132958670Seric 133058670Seric if (a->q_host != NULL && !bitnset(M_HST_UPPER, m->m_flags)) 133158670Seric makelower(a->q_host); 133258670Seric if (!bitnset(M_USR_UPPER, m->m_flags)) 133358670Seric makelower(a->q_user); 133458670Seric 13353149Seric return (a); 13363149Seric } 13373188Seric /* 13384228Seric ** CATADDR -- concatenate pieces of addresses (putting in <LWSP> subs) 13394228Seric ** 13404228Seric ** Parameters: 13414228Seric ** pvp -- parameter vector to rebuild. 134258814Seric ** evp -- last parameter to include. Can be NULL to 134358814Seric ** use entire pvp. 13444228Seric ** buf -- buffer to build the string into. 13454228Seric ** sz -- size of buf. 134658082Seric ** spacesub -- the space separator character; if null, 134758082Seric ** use SpaceSub. 13484228Seric ** 13494228Seric ** Returns: 13504228Seric ** none. 13514228Seric ** 13524228Seric ** Side Effects: 13534228Seric ** Destroys buf. 13544228Seric */ 13554228Seric 135658814Seric cataddr(pvp, evp, buf, sz, spacesub) 13574228Seric char **pvp; 135858814Seric char **evp; 13594228Seric char *buf; 13604228Seric register int sz; 136158082Seric char spacesub; 13624228Seric { 13634228Seric bool oatomtok = FALSE; 136456678Seric bool natomtok = FALSE; 13654228Seric register int i; 13664228Seric register char *p; 13674228Seric 136858082Seric if (spacesub == '\0') 136958082Seric spacesub = SpaceSub; 137058082Seric 13718423Seric if (pvp == NULL) 13728423Seric { 137323109Seric (void) strcpy(buf, ""); 13748423Seric return; 13758423Seric } 13764228Seric p = buf; 137711156Seric sz -= 2; 13784228Seric while (*pvp != NULL && (i = strlen(*pvp)) < sz) 13794228Seric { 13808078Seric natomtok = (toktype(**pvp) == ATM); 13814228Seric if (oatomtok && natomtok) 138258082Seric *p++ = spacesub; 13834228Seric (void) strcpy(p, *pvp); 13844228Seric oatomtok = natomtok; 13854228Seric p += i; 138611156Seric sz -= i + 1; 138758814Seric if (pvp++ == evp) 138858814Seric break; 13894228Seric } 13904228Seric *p = '\0'; 13914228Seric } 13924228Seric /* 13933188Seric ** SAMEADDR -- Determine if two addresses are the same 13943188Seric ** 13953188Seric ** This is not just a straight comparison -- if the mailer doesn't 13963188Seric ** care about the host we just ignore it, etc. 13973188Seric ** 13983188Seric ** Parameters: 13993188Seric ** a, b -- pointers to the internal forms to compare. 14003188Seric ** 14013188Seric ** Returns: 14023188Seric ** TRUE -- they represent the same mailbox. 14033188Seric ** FALSE -- they don't. 14043188Seric ** 14053188Seric ** Side Effects: 14063188Seric ** none. 14073188Seric */ 14083188Seric 14093188Seric bool 14109374Seric sameaddr(a, b) 14113188Seric register ADDRESS *a; 14123188Seric register ADDRESS *b; 14133188Seric { 14143188Seric /* if they don't have the same mailer, forget it */ 14153188Seric if (a->q_mailer != b->q_mailer) 14163188Seric return (FALSE); 14173188Seric 14183188Seric /* if the user isn't the same, we can drop out */ 141956678Seric if (strcmp(a->q_user, b->q_user) != 0) 14203188Seric return (FALSE); 14213188Seric 142258438Seric /* if we have good uids for both but the differ, these are different */ 142358438Seric if (bitset(QGOODUID, a->q_flags & b->q_flags) && a->q_uid != b->q_uid) 142458438Seric return (FALSE); 142558438Seric 142658509Seric /* otherwise compare hosts (but be careful for NULL ptrs) */ 142758509Seric if (a->q_host == b->q_host) 142858509Seric { 142958509Seric /* probably both null pointers */ 14303188Seric return (TRUE); 143158509Seric } 14323188Seric if (a->q_host == NULL || b->q_host == NULL) 143358509Seric { 143458509Seric /* only one is a null pointer */ 14353188Seric return (FALSE); 143658509Seric } 143756678Seric if (strcmp(a->q_host, b->q_host) != 0) 14383188Seric return (FALSE); 14393188Seric 14403188Seric return (TRUE); 14413188Seric } 14423234Seric /* 14433234Seric ** PRINTADDR -- print address (for debugging) 14443234Seric ** 14453234Seric ** Parameters: 14463234Seric ** a -- the address to print 14473234Seric ** follow -- follow the q_next chain. 14483234Seric ** 14493234Seric ** Returns: 14503234Seric ** none. 14513234Seric ** 14523234Seric ** Side Effects: 14533234Seric ** none. 14543234Seric */ 14553234Seric 14563234Seric printaddr(a, follow) 14573234Seric register ADDRESS *a; 14583234Seric bool follow; 14593234Seric { 14605001Seric bool first = TRUE; 146157731Seric register MAILER *m; 146257731Seric MAILER pseudomailer; 14635001Seric 14643234Seric while (a != NULL) 14653234Seric { 14665001Seric first = FALSE; 14674443Seric printf("%x=", a); 14684085Seric (void) fflush(stdout); 146957731Seric 147057731Seric /* find the mailer -- carefully */ 147157731Seric m = a->q_mailer; 147257731Seric if (m == NULL) 147357731Seric { 147457731Seric m = &pseudomailer; 147557731Seric m->m_mno = -1; 147657731Seric m->m_name = "NULL"; 147757731Seric } 147857731Seric 147958680Seric printf("%s:\n\tmailer %d (%s), host `%s', user `%s', ruser `%s'\n", 148057731Seric a->q_paddr, m->m_mno, m->m_name, 1481*63756Seric a->q_host, a->q_user, 1482*63756Seric a->q_ruser ? a->q_ruser : "<null>"); 148359269Seric printf("\tnext=%x, flags=%o, alias %x, uid %d, gid %d\n", 148459269Seric a->q_next, a->q_flags, a->q_alias, a->q_uid, a->q_gid); 148559269Seric printf("\towner=%s, home=\"%s\", fullname=\"%s\"\n", 148659269Seric a->q_owner == NULL ? "(none)" : a->q_owner, 1487*63756Seric a->q_home == NULL ? "(none)" : a->q_home, 1488*63756Seric a->q_fullname == NULL ? "(none)" : 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 char *oldg = macvalue('g', e); 153258020Seric int rwset; 15337682Seric static char buf[MAXNAME]; 15347682Seric char lbuf[MAXNAME]; 153516914Seric char pvpbuf[PSBUFSIZE]; 153656678Seric extern char *crackaddr(); 15377682Seric 15387755Seric if (tTd(12, 1)) 15397755Seric printf("remotename(%s)\n", name); 15407755Seric 154110177Seric /* don't do anything if we are tagging it as special */ 154259163Seric if (bitset(RF_SENDERADDR, flags)) 154359163Seric rwset = bitset(RF_HEADERADDR, flags) ? m->m_sh_rwset 154459163Seric : m->m_se_rwset; 154558020Seric else 154659163Seric rwset = bitset(RF_HEADERADDR, flags) ? m->m_rh_rwset 154759163Seric : m->m_re_rwset; 154858020Seric if (rwset < 0) 154910177Seric return (name); 155010177Seric 15517682Seric /* 15528181Seric ** Do a heuristic crack of this name to extract any comment info. 15538181Seric ** This will leave the name as a comment and a $g macro. 15547889Seric */ 15557889Seric 155659163Seric if (bitset(RF_CANONICAL, flags) || bitnset(M_NOCOMMENT, m->m_flags)) 155758050Seric fancy = "\201g"; 155810310Seric else 155910310Seric fancy = crackaddr(name); 15607889Seric 15618181Seric /* 15628181Seric ** Turn the name into canonical form. 15638181Seric ** Normally this will be RFC 822 style, i.e., "user@domain". 15648181Seric ** If this only resolves to "user", and the "C" flag is 15658181Seric ** specified in the sending mailer, then the sender's 15668181Seric ** domain will be appended. 15678181Seric */ 15688181Seric 156958333Seric pvp = prescan(name, '\0', pvpbuf, NULL); 15707889Seric if (pvp == NULL) 15717889Seric return (name); 157259163Seric if (rewrite(pvp, 3, e) == EX_TEMPFAIL) 157359163Seric *pstat = EX_TEMPFAIL; 157459163Seric if (bitset(RF_ADDDOMAIN, flags) && 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; 158959163Seric if (rewrite(pvp, 3, e) == EX_TEMPFAIL) 159059163Seric *pstat = EX_TEMPFAIL; 15918181Seric } 15928181Seric } 15938181Seric 15948181Seric /* 15958959Seric ** Do more specific rewriting. 159656678Seric ** Rewrite using ruleset 1 or 2 depending on whether this is 159756678Seric ** a sender address or not. 15988181Seric ** Then run it through any receiving-mailer-specific rulesets. 15998181Seric */ 16008181Seric 160159163Seric if (bitset(RF_SENDERADDR, flags)) 160259541Seric { 160359163Seric if (rewrite(pvp, 1, e) == EX_TEMPFAIL) 160459163Seric *pstat = EX_TEMPFAIL; 160559541Seric } 16068069Seric else 160759541Seric { 160859163Seric if (rewrite(pvp, 2, e) == EX_TEMPFAIL) 160959163Seric *pstat = EX_TEMPFAIL; 161059541Seric } 161158020Seric if (rwset > 0) 161259541Seric { 161359163Seric if (rewrite(pvp, rwset, e) == EX_TEMPFAIL) 161459163Seric *pstat = EX_TEMPFAIL; 161559541Seric } 16167682Seric 16178181Seric /* 16188959Seric ** Do any final sanitation the address may require. 16198959Seric ** This will normally be used to turn internal forms 16208959Seric ** (e.g., user@host.LOCAL) into external form. This 16218959Seric ** may be used as a default to the above rules. 16228959Seric */ 16238959Seric 162459163Seric if (rewrite(pvp, 4, e) == EX_TEMPFAIL) 162559163Seric *pstat = EX_TEMPFAIL; 16268959Seric 16278959Seric /* 16288181Seric ** Now restore the comment information we had at the beginning. 16298181Seric */ 16308181Seric 163158825Seric cataddr(pvp, NULL, lbuf, sizeof lbuf, '\0'); 163256678Seric define('g', lbuf, e); 163356678Seric expand(fancy, buf, &buf[sizeof buf - 1], e); 163456678Seric define('g', oldg, e); 16357682Seric 16367682Seric if (tTd(12, 1)) 16377755Seric printf("remotename => `%s'\n", buf); 16387682Seric return (buf); 16397682Seric } 164051317Seric /* 164156678Seric ** MAPLOCALUSER -- run local username through ruleset 5 for final redirection 164251317Seric ** 164351317Seric ** Parameters: 164456678Seric ** a -- the address to map (but just the user name part). 164556678Seric ** sendq -- the sendq in which to install any replacement 164656678Seric ** addresses. 164751317Seric ** 164851317Seric ** Returns: 164951317Seric ** none. 165051317Seric */ 165151317Seric 165256678Seric maplocaluser(a, sendq, e) 165356678Seric register ADDRESS *a; 165456678Seric ADDRESS **sendq; 165556678Seric ENVELOPE *e; 165651317Seric { 165756678Seric register char **pvp; 165856678Seric register ADDRESS *a1 = NULL; 165958333Seric auto char *delimptr; 166056678Seric char pvpbuf[PSBUFSIZE]; 166151317Seric 166256678Seric if (tTd(29, 1)) 166356678Seric { 166456678Seric printf("maplocaluser: "); 166556678Seric printaddr(a, FALSE); 166656678Seric } 166758333Seric pvp = prescan(a->q_user, '\0', pvpbuf, &delimptr); 166856678Seric if (pvp == NULL) 166956678Seric return; 167051317Seric 167159084Seric (void) rewrite(pvp, 5, e); 167258050Seric if (pvp[0] == NULL || (pvp[0][0] & 0377) != CANONNET) 167356678Seric return; 167451317Seric 167556678Seric /* if non-null, mailer destination specified -- has it changed? */ 167658966Seric a1 = buildaddr(pvp, NULL, e); 167756678Seric if (a1 == NULL || sameaddr(a, a1)) 167856678Seric return; 167951317Seric 168056678Seric /* mark old address as dead; insert new address */ 168156678Seric a->q_flags |= QDONTSEND; 168257731Seric if (tTd(29, 5)) 168357731Seric { 168457731Seric printf("maplocaluser: QDONTSEND "); 168557731Seric printaddr(a, FALSE); 168657731Seric } 168756678Seric a1->q_alias = a; 168858333Seric allocaddr(a1, 1, NULL, delimptr); 168956678Seric (void) recipient(a1, sendq, e); 169051317Seric } 169158802Seric /* 169258802Seric ** DEQUOTE_INIT -- initialize dequote map 169358802Seric ** 169458802Seric ** This is a no-op. 169558802Seric ** 169658802Seric ** Parameters: 169758802Seric ** map -- the internal map structure. 169858802Seric ** args -- arguments. 169958802Seric ** 170058802Seric ** Returns: 170158802Seric ** TRUE. 170258802Seric */ 170358802Seric 170458802Seric bool 170560219Seric dequote_init(map, args) 170658802Seric MAP *map; 170758802Seric char *args; 170858802Seric { 170958805Seric register char *p = args; 171058805Seric 171158805Seric for (;;) 171258805Seric { 171358805Seric while (isascii(*p) && isspace(*p)) 171458805Seric p++; 171558805Seric if (*p != '-') 171658805Seric break; 171758805Seric switch (*++p) 171858805Seric { 171958805Seric case 'a': 172058805Seric map->map_app = ++p; 172158805Seric break; 172258805Seric } 172358805Seric while (*p != '\0' && !(isascii(*p) && isspace(*p))) 172458805Seric p++; 172558805Seric if (*p != '\0') 172658805Seric *p = '\0'; 172758805Seric } 172858805Seric if (map->map_app != NULL) 172958805Seric map->map_app = newstr(map->map_app); 173058805Seric 173158802Seric return TRUE; 173258802Seric } 173358802Seric /* 173458802Seric ** DEQUOTE_MAP -- unquote an address 173558802Seric ** 173658802Seric ** Parameters: 173758802Seric ** map -- the internal map structure (ignored). 173860089Seric ** name -- the name to dequote. 173958802Seric ** av -- arguments (ignored). 174059084Seric ** statp -- pointer to status out-parameter. 174158802Seric ** 174258802Seric ** Returns: 174358802Seric ** NULL -- if there were no quotes, or if the resulting 174458802Seric ** unquoted buffer would not be acceptable to prescan. 174558802Seric ** else -- The dequoted buffer. 174658802Seric */ 174758802Seric 174858802Seric char * 174960089Seric dequote_map(map, name, av, statp) 175058802Seric MAP *map; 175160089Seric char *name; 175258802Seric char **av; 175359084Seric int *statp; 175458802Seric { 175558802Seric register char *p; 175658802Seric register char *q; 175758802Seric register char c; 175858802Seric int anglecnt; 175958802Seric int cmntcnt; 176058802Seric int quotecnt; 176159089Seric int spacecnt; 176258802Seric bool quotemode; 176358802Seric bool bslashmode; 176458802Seric 176558802Seric anglecnt = 0; 176658802Seric cmntcnt = 0; 176758802Seric quotecnt = 0; 176859089Seric spacecnt = 0; 176958802Seric quotemode = FALSE; 177058802Seric bslashmode = FALSE; 177158802Seric 177260089Seric for (p = q = name; (c = *p++) != '\0'; ) 177358802Seric { 177458802Seric if (bslashmode) 177558802Seric { 177658802Seric bslashmode = FALSE; 177758802Seric *q++ = c; 177858802Seric continue; 177958802Seric } 178058802Seric 178158802Seric switch (c) 178258802Seric { 178358802Seric case '\\': 178458802Seric bslashmode = TRUE; 178558802Seric break; 178658802Seric 178758802Seric case '(': 178858802Seric cmntcnt++; 178958802Seric break; 179058802Seric 179158802Seric case ')': 179258802Seric if (cmntcnt-- <= 0) 179358802Seric return NULL; 179458802Seric break; 179559089Seric 179659089Seric case ' ': 179759089Seric spacecnt++; 179859089Seric break; 179958802Seric } 180058802Seric 180158802Seric if (cmntcnt > 0) 180258802Seric { 180358802Seric *q++ = c; 180458802Seric continue; 180558802Seric } 180658802Seric 180758802Seric switch (c) 180858802Seric { 180958802Seric case '"': 181058802Seric quotemode = !quotemode; 181158802Seric quotecnt++; 181258802Seric continue; 181358802Seric 181458802Seric case '<': 181558802Seric anglecnt++; 181658802Seric break; 181758802Seric 181858802Seric case '>': 181958802Seric if (anglecnt-- <= 0) 182058802Seric return NULL; 182158802Seric break; 182258802Seric } 182358802Seric *q++ = c; 182458802Seric } 182558802Seric 182658802Seric if (anglecnt != 0 || cmntcnt != 0 || bslashmode || 182759089Seric quotemode || quotecnt <= 0 || spacecnt != 0) 182858802Seric return NULL; 182958802Seric *q++ = '\0'; 183060089Seric return name; 183158802Seric } 1832