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*59541Seric static char sccsid[] = "@(#)parseaddr.c 6.46 (Berkeley) 04/29/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; 6411445Seric char delim; 6558333Seric char **delimptr; 6656678Seric register ENVELOPE *e; 67297Seric { 683149Seric register char **pvp; 6958333Seric auto char *delimptrbuf; 7059084Seric bool queueup; 7116914Seric char pvpbuf[PSBUFSIZE]; 7256678Seric extern char **prescan(); 7356678Seric extern ADDRESS *buildaddr(); 7457388Seric extern bool invalidaddr(); 75297Seric 76297Seric /* 77297Seric ** Initialize and prescan address. 78297Seric */ 79297Seric 8056678Seric e->e_to = addr; 817675Seric if (tTd(20, 1)) 829888Seric printf("\n--parseaddr(%s)\n", addr); 833188Seric 8457388Seric if (invalidaddr(addr)) 8557388Seric { 8657388Seric if (tTd(20, 1)) 8757388Seric printf("parseaddr-->bad address\n"); 8857388Seric return NULL; 8957388Seric } 9057388Seric 9158333Seric if (delimptr == NULL) 9258333Seric delimptr = &delimptrbuf; 9358333Seric 9458333Seric pvp = prescan(addr, delim, pvpbuf, delimptr); 953149Seric if (pvp == NULL) 9656729Seric { 9756729Seric if (tTd(20, 1)) 9856729Seric printf("parseaddr-->NULL\n"); 99297Seric return (NULL); 10056729Seric } 101297Seric 102297Seric /* 1033149Seric ** Apply rewriting rules. 1047889Seric ** Ruleset 0 does basic parsing. It must resolve. 105297Seric */ 106297Seric 10759084Seric queueup = FALSE; 10859084Seric if (rewrite(pvp, 3, e) == EX_TEMPFAIL) 10959084Seric queueup = TRUE; 11059084Seric if (rewrite(pvp, 0, e) == EX_TEMPFAIL) 11159084Seric queueup = TRUE; 112297Seric 1133149Seric /* 1143149Seric ** See if we resolved to a real mailer. 1153149Seric */ 116297Seric 11759040Seric if (pvp[0] == NULL || (pvp[0][0] & 0377) != CANONNET) 1183149Seric { 1193149Seric setstat(EX_USAGE); 12058151Seric syserr("554 cannot resolve name"); 1213149Seric return (NULL); 122297Seric } 123297Seric 124297Seric /* 1253149Seric ** Build canonical address from pvp. 126297Seric */ 127297Seric 12858966Seric a = buildaddr(pvp, a, e); 1294279Seric if (a == NULL) 1304279Seric return (NULL); 131297Seric 132297Seric /* 1333149Seric ** Make local copies of the host & user and then 1343149Seric ** transport them out. 135297Seric */ 136297Seric 13758333Seric allocaddr(a, copyf, addr, *delimptr); 13856678Seric 13956678Seric /* 14059084Seric ** If there was a parsing failure, mark it for queueing. 14159084Seric */ 14259084Seric 14359084Seric if (queueup) 14459084Seric a->q_flags |= QQUEUEUP; 14559084Seric 14659084Seric /* 14756678Seric ** Compute return value. 14856678Seric */ 14956678Seric 15056678Seric if (tTd(20, 1)) 1518078Seric { 15256678Seric printf("parseaddr-->"); 15356678Seric printaddr(a, FALSE); 15456678Seric } 15556678Seric 15656678Seric return (a); 15756678Seric } 15856678Seric /* 15957388Seric ** INVALIDADDR -- check for address containing meta-characters 16057388Seric ** 16157388Seric ** Parameters: 16257388Seric ** addr -- the address to check. 16357388Seric ** 16457388Seric ** Returns: 16557388Seric ** TRUE -- if the address has any "wierd" characters 16657388Seric ** FALSE -- otherwise. 16757388Seric */ 16857388Seric 16957388Seric bool 17057388Seric invalidaddr(addr) 17157388Seric register char *addr; 17257388Seric { 17357388Seric for (; *addr != '\0'; addr++) 17457388Seric { 17558050Seric if ((*addr & 0340) != 0200) 17657388Seric continue; 17757388Seric setstat(EX_USAGE); 17858151Seric usrerr("553 Address contained invalid control characters"); 17957388Seric return TRUE; 18057388Seric } 18157388Seric return FALSE; 18257388Seric } 18357388Seric /* 18456678Seric ** ALLOCADDR -- do local allocations of address on demand. 18556678Seric ** 18656678Seric ** Also lowercases the host name if requested. 18756678Seric ** 18856678Seric ** Parameters: 18956678Seric ** a -- the address to reallocate. 19056678Seric ** copyf -- the copy flag (see parseaddr for description). 19156678Seric ** paddr -- the printname of the address. 19258333Seric ** delimptr -- a pointer to the address delimiter. Must be set. 19356678Seric ** 19456678Seric ** Returns: 19556678Seric ** none. 19656678Seric ** 19756678Seric ** Side Effects: 19856678Seric ** Copies portions of a into local buffers as requested. 19956678Seric */ 20056678Seric 20158333Seric allocaddr(a, copyf, paddr, delimptr) 20256678Seric register ADDRESS *a; 20356678Seric int copyf; 20456678Seric char *paddr; 20558333Seric char *delimptr; 20656678Seric { 20756678Seric register MAILER *m = a->q_mailer; 20856678Seric 20958673Seric if (tTd(24, 4)) 21058673Seric printf("allocaddr(copyf=%d, paddr=%s)\n", copyf, paddr); 21158673Seric 21256678Seric if (copyf > 0 && paddr != NULL) 21356678Seric { 21458333Seric char savec = *delimptr; 2158078Seric 21658333Seric *delimptr = '\0'; 21756678Seric a->q_paddr = newstr(paddr); 21858333Seric *delimptr = savec; 2198078Seric } 220297Seric else 22156678Seric a->q_paddr = paddr; 22224944Seric 22324944Seric if (a->q_user == NULL) 22424944Seric a->q_user = ""; 22524944Seric if (a->q_host == NULL) 22624944Seric a->q_host = ""; 22724944Seric 2283149Seric if (copyf >= 0) 229297Seric { 23024944Seric a->q_host = newstr(a->q_host); 2313149Seric if (a->q_user != a->q_paddr) 2323149Seric a->q_user = newstr(a->q_user); 233297Seric } 234297Seric 23556678Seric if (a->q_paddr == NULL) 23656678Seric a->q_paddr = a->q_user; 237297Seric } 238297Seric /* 239297Seric ** PRESCAN -- Prescan name and make it canonical 240297Seric ** 2419374Seric ** Scans a name and turns it into a set of tokens. This process 2429374Seric ** deletes blanks and comments (in parentheses). 243297Seric ** 244297Seric ** This routine knows about quoted strings and angle brackets. 245297Seric ** 246297Seric ** There are certain subtleties to this routine. The one that 247297Seric ** comes to mind now is that backslashes on the ends of names 248297Seric ** are silently stripped off; this is intentional. The problem 249297Seric ** is that some versions of sndmsg (like at LBL) set the kill 250297Seric ** character to something other than @ when reading addresses; 251297Seric ** so people type "csvax.eric\@berkeley" -- which screws up the 252297Seric ** berknet mailer. 253297Seric ** 254297Seric ** Parameters: 255297Seric ** addr -- the name to chomp. 256297Seric ** delim -- the delimiter for the address, normally 257297Seric ** '\0' or ','; \0 is accepted in any case. 25815284Seric ** If '\t' then we are reading the .cf file. 25916914Seric ** pvpbuf -- place to put the saved text -- note that 26016914Seric ** the pointers are static. 26158333Seric ** delimptr -- if non-NULL, set to the location of the 26258333Seric ** terminating delimiter. 263297Seric ** 264297Seric ** Returns: 2653149Seric ** A pointer to a vector of tokens. 266297Seric ** NULL on error. 267297Seric */ 268297Seric 2698078Seric /* states and character types */ 2708078Seric # define OPR 0 /* operator */ 2718078Seric # define ATM 1 /* atom */ 2728078Seric # define QST 2 /* in quoted string */ 2738078Seric # define SPC 3 /* chewing up spaces */ 2748078Seric # define ONE 4 /* pick up one character */ 2753149Seric 2768078Seric # define NSTATES 5 /* number of states */ 2778078Seric # define TYPE 017 /* mask to select state type */ 2788078Seric 2798078Seric /* meta bits for table */ 2808078Seric # define M 020 /* meta character; don't pass through */ 2818078Seric # define B 040 /* cause a break */ 2828078Seric # define MB M|B /* meta-break */ 2838078Seric 2848078Seric static short StateTab[NSTATES][NSTATES] = 2858078Seric { 2868087Seric /* oldst chtype> OPR ATM QST SPC ONE */ 2879051Seric /*OPR*/ OPR|B, ATM|B, QST|B, SPC|MB, ONE|B, 2889051Seric /*ATM*/ OPR|B, ATM, QST|B, SPC|MB, ONE|B, 2899051Seric /*QST*/ QST, QST, OPR, QST, QST, 2908078Seric /*SPC*/ OPR, ATM, QST, SPC|M, ONE, 2918078Seric /*ONE*/ OPR, OPR, OPR, OPR, OPR, 2928078Seric }; 2938078Seric 2948078Seric # define NOCHAR -1 /* signal nothing in lookahead token */ 2958078Seric 2963149Seric char ** 29758333Seric prescan(addr, delim, pvpbuf, delimptr) 298297Seric char *addr; 299297Seric char delim; 30016914Seric char pvpbuf[]; 30158333Seric char **delimptr; 302297Seric { 303297Seric register char *p; 3048078Seric register char *q; 3059346Seric register int c; 3063149Seric char **avp; 307297Seric bool bslashmode; 308297Seric int cmntcnt; 3098423Seric int anglecnt; 3103149Seric char *tok; 3118078Seric int state; 3128078Seric int newstate; 3138078Seric static char *av[MAXATOM+1]; 31456678Seric extern int errno; 315297Seric 31615253Seric /* make sure error messages don't have garbage on them */ 31715253Seric errno = 0; 31815253Seric 31916914Seric q = pvpbuf; 3203149Seric bslashmode = FALSE; 3217800Seric cmntcnt = 0; 3228423Seric anglecnt = 0; 3233149Seric avp = av; 32456678Seric state = ATM; 3258078Seric c = NOCHAR; 3268078Seric p = addr; 32756764Seric if (tTd(22, 11)) 328297Seric { 3298078Seric printf("prescan: "); 3308078Seric xputs(p); 33123109Seric (void) putchar('\n'); 3328078Seric } 3338078Seric 3348078Seric do 3358078Seric { 3363149Seric /* read a token */ 3373149Seric tok = q; 3388078Seric for (;;) 339297Seric { 3408078Seric /* store away any old lookahead character */ 34159277Seric if (c != NOCHAR && !bslashmode) 3428078Seric { 34315284Seric /* see if there is room */ 34416914Seric if (q >= &pvpbuf[PSBUFSIZE - 5]) 3458078Seric { 34658151Seric usrerr("553 Address too long"); 34758333Seric if (delimptr != NULL) 34858333Seric *delimptr = p; 3498078Seric return (NULL); 3508078Seric } 35115284Seric 35215284Seric /* squirrel it away */ 3538078Seric *q++ = c; 3548078Seric } 3558078Seric 3568078Seric /* read a new input character */ 3578078Seric c = *p++; 35857631Seric if (c == '\0') 35956764Seric { 36056764Seric /* diagnose and patch up bad syntax */ 36156764Seric if (state == QST) 36256764Seric { 36358151Seric usrerr("553 Unbalanced '\"'"); 36456764Seric c = '"'; 36556764Seric } 36656764Seric else if (cmntcnt > 0) 36756764Seric { 36858151Seric usrerr("553 Unbalanced '('"); 36956764Seric c = ')'; 37056764Seric } 37156764Seric else if (anglecnt > 0) 37256764Seric { 37356764Seric c = '>'; 37458151Seric usrerr("553 Unbalanced '<'"); 37556764Seric } 37656764Seric else 37756764Seric break; 37815284Seric 37956764Seric p--; 38056764Seric } 38157631Seric else if (c == delim && anglecnt <= 0 && 38257631Seric cmntcnt <= 0 && state != QST) 38357631Seric break; 38456764Seric 3858078Seric if (tTd(22, 101)) 3868078Seric printf("c=%c, s=%d; ", c, state); 3878078Seric 3883149Seric /* chew up special characters */ 3893149Seric *q = '\0'; 3903149Seric if (bslashmode) 3913149Seric { 39259105Seric bslashmode = FALSE; 39359105Seric 39424944Seric /* kludge \! for naive users */ 39558061Seric if (cmntcnt > 0) 39659105Seric { 39758061Seric c = NOCHAR; 39859105Seric continue; 39959105Seric } 40059105Seric else if (c != '!' || state == QST) 40159105Seric { 40256678Seric *q++ = '\\'; 40359105Seric continue; 40459105Seric } 4053149Seric } 40656678Seric 40756678Seric if (c == '\\') 4083149Seric { 4093149Seric bslashmode = TRUE; 4103149Seric } 41156678Seric else if (state == QST) 4128514Seric { 4138514Seric /* do nothing, just avoid next clauses */ 4148514Seric } 4158078Seric else if (c == '(') 4164100Seric { 4178078Seric cmntcnt++; 4188078Seric c = NOCHAR; 4194100Seric } 4208078Seric else if (c == ')') 4213149Seric { 4228078Seric if (cmntcnt <= 0) 4233149Seric { 42458151Seric usrerr("553 Unbalanced ')'"); 42558333Seric if (delimptr != NULL) 42658333Seric *delimptr = p; 4278078Seric return (NULL); 4283149Seric } 4298078Seric else 4308078Seric cmntcnt--; 4318078Seric } 4328078Seric else if (cmntcnt > 0) 4338078Seric c = NOCHAR; 4348423Seric else if (c == '<') 4358423Seric anglecnt++; 4368423Seric else if (c == '>') 4378423Seric { 4388423Seric if (anglecnt <= 0) 4398423Seric { 44058151Seric usrerr("553 Unbalanced '>'"); 44158333Seric if (delimptr != NULL) 44258333Seric *delimptr = p; 4438423Seric return (NULL); 4448423Seric } 4458423Seric anglecnt--; 4468423Seric } 44758050Seric else if (delim == ' ' && isascii(c) && isspace(c)) 44811423Seric c = ' '; 4493149Seric 4508078Seric if (c == NOCHAR) 4518078Seric continue; 4523149Seric 4538078Seric /* see if this is end of input */ 45411405Seric if (c == delim && anglecnt <= 0 && state != QST) 4553149Seric break; 4563149Seric 4578078Seric newstate = StateTab[state][toktype(c)]; 4588078Seric if (tTd(22, 101)) 4598078Seric printf("ns=%02o\n", newstate); 4608078Seric state = newstate & TYPE; 4618078Seric if (bitset(M, newstate)) 4628078Seric c = NOCHAR; 4638078Seric if (bitset(B, newstate)) 4644228Seric break; 465297Seric } 4663149Seric 4673149Seric /* new token */ 4688078Seric if (tok != q) 4691378Seric { 4708078Seric *q++ = '\0'; 4718078Seric if (tTd(22, 36)) 472297Seric { 4738078Seric printf("tok="); 4748078Seric xputs(tok); 47523109Seric (void) putchar('\n'); 476297Seric } 4778078Seric if (avp >= &av[MAXATOM]) 478297Seric { 47958151Seric syserr("553 prescan: too many tokens"); 48058333Seric if (delimptr != NULL) 48158333Seric *delimptr = p; 4828078Seric return (NULL); 483297Seric } 4848078Seric *avp++ = tok; 485297Seric } 4868423Seric } while (c != '\0' && (c != delim || anglecnt > 0)); 4873149Seric *avp = NULL; 48858333Seric p--; 48958333Seric if (delimptr != NULL) 49058333Seric *delimptr = p; 49156764Seric if (tTd(22, 12)) 49256764Seric { 49356764Seric printf("prescan==>"); 49456764Seric printav(av); 49556764Seric } 49658546Seric if (av[0] == NULL) 49758546Seric return (NULL); 49858403Seric return (av); 4993149Seric } 5003149Seric /* 5013149Seric ** TOKTYPE -- return token type 5023149Seric ** 5033149Seric ** Parameters: 5043149Seric ** c -- the character in question. 5053149Seric ** 5063149Seric ** Returns: 5073149Seric ** Its type. 5083149Seric ** 5093149Seric ** Side Effects: 5103149Seric ** none. 5113149Seric */ 512297Seric 5133149Seric toktype(c) 51458050Seric register int c; 5153149Seric { 5163380Seric static char buf[50]; 5173382Seric static bool firstime = TRUE; 5183380Seric 5193382Seric if (firstime) 5203380Seric { 5213382Seric firstime = FALSE; 52258050Seric expand("\201o", buf, &buf[sizeof buf - 1], CurEnv); 5237005Seric (void) strcat(buf, DELIMCHARS); 5243380Seric } 52558050Seric c &= 0377; 52656327Seric if (c == MATCHCLASS || c == MATCHREPL || c == MATCHNCLASS) 5278078Seric return (ONE); 52859027Seric if (c == MACRODEXPAND) 52959027Seric return (ONE); 5308078Seric if (c == '"') 5318078Seric return (QST); 53258050Seric if ((c & 0340) == 0200) 53358050Seric return (OPR); 5344100Seric if (!isascii(c)) 5358078Seric return (ATM); 5368078Seric if (isspace(c) || c == ')') 5378078Seric return (SPC); 53858050Seric if (strchr(buf, c) != NULL) 5398078Seric return (OPR); 5408078Seric return (ATM); 5413149Seric } 5423149Seric /* 5433149Seric ** REWRITE -- apply rewrite rules to token vector. 5443149Seric ** 5454476Seric ** This routine is an ordered production system. Each rewrite 5464476Seric ** rule has a LHS (called the pattern) and a RHS (called the 5474476Seric ** rewrite); 'rwr' points the the current rewrite rule. 5484476Seric ** 5494476Seric ** For each rewrite rule, 'avp' points the address vector we 5504476Seric ** are trying to match against, and 'pvp' points to the pattern. 5518058Seric ** If pvp points to a special match value (MATCHZANY, MATCHANY, 5529585Seric ** MATCHONE, MATCHCLASS, MATCHNCLASS) then the address in avp 5539585Seric ** matched is saved away in the match vector (pointed to by 'mvp'). 5544476Seric ** 5554476Seric ** When a match between avp & pvp does not match, we try to 5569585Seric ** back out. If we back up over MATCHONE, MATCHCLASS, or MATCHNCLASS 5574476Seric ** we must also back out the match in mvp. If we reach a 5588058Seric ** MATCHANY or MATCHZANY we just extend the match and start 5598058Seric ** over again. 5604476Seric ** 5614476Seric ** When we finally match, we rewrite the address vector 5624476Seric ** and try over again. 5634476Seric ** 5643149Seric ** Parameters: 5653149Seric ** pvp -- pointer to token vector. 56659027Seric ** ruleset -- the ruleset to use for rewriting. 56759027Seric ** e -- the current envelope. 5683149Seric ** 5693149Seric ** Returns: 57059084Seric ** A status code. If EX_TEMPFAIL, higher level code should 57159084Seric ** attempt recovery. 5723149Seric ** 5733149Seric ** Side Effects: 5743149Seric ** pvp is modified. 5753149Seric */ 5762091Seric 5773149Seric struct match 5783149Seric { 5794468Seric char **first; /* first token matched */ 5804468Seric char **last; /* last token matched */ 58158825Seric char **pattern; /* pointer to pattern */ 5823149Seric }; 5833149Seric 5844468Seric # define MAXMATCH 9 /* max params per rewrite */ 5853149Seric 5863149Seric 58759084Seric int 58859027Seric rewrite(pvp, ruleset, e) 5893149Seric char **pvp; 5904070Seric int ruleset; 59159027Seric register ENVELOPE *e; 5923149Seric { 5933149Seric register char *ap; /* address pointer */ 5943149Seric register char *rp; /* rewrite pointer */ 5953149Seric register char **avp; /* address vector pointer */ 5963149Seric register char **rvp; /* rewrite vector pointer */ 5978058Seric register struct match *mlp; /* cur ptr into mlist */ 5988058Seric register struct rewrite *rwr; /* pointer to current rewrite rule */ 59958866Seric int ruleno; /* current rule number */ 60059084Seric int rstat = EX_OK; /* return status */ 60156678Seric struct match mlist[MAXMATCH]; /* stores match on LHS */ 6023149Seric char *npvp[MAXATOM+1]; /* temporary space for rebuild */ 60359027Seric extern char *macvalue(); 6043149Seric 6059279Seric if (OpMode == MD_TEST || tTd(21, 2)) 6063149Seric { 6078959Seric printf("rewrite: ruleset %2d input:", ruleset); 60856678Seric printav(pvp); 6093149Seric } 61056678Seric if (ruleset < 0 || ruleset >= MAXRWSETS) 61156326Seric { 61258151Seric syserr("554 rewrite: illegal ruleset number %d", ruleset); 61359084Seric return EX_CONFIG; 61456326Seric } 61556678Seric if (pvp == NULL) 61659084Seric return EX_USAGE; 61756326Seric 6183149Seric /* 61956678Seric ** Run through the list of rewrite rules, applying 62056678Seric ** any that match. 6213149Seric */ 6223149Seric 62358866Seric ruleno = 1; 6244070Seric for (rwr = RewriteRules[ruleset]; rwr != NULL; ) 6253149Seric { 62656678Seric int loopcount = 0; 62756678Seric 6287675Seric if (tTd(21, 12)) 629297Seric { 6308069Seric printf("-----trying rule:"); 63156678Seric printav(rwr->r_lhs); 6323149Seric } 6333149Seric 6343149Seric /* try to match on this rule */ 6354468Seric mlp = mlist; 6368058Seric rvp = rwr->r_lhs; 6378058Seric avp = pvp; 63858866Seric if (++loopcount > 100) 6393149Seric { 64058866Seric syserr("554 Infinite loop in ruleset %d, rule %d", 64158866Seric ruleset, ruleno); 64258866Seric if (tTd(21, 1)) 64352637Seric { 64456678Seric printf("workspace: "); 64556678Seric printav(pvp); 64652637Seric } 64758866Seric break; 64858866Seric } 64958866Seric 65058866Seric while ((ap = *avp) != NULL || *rvp != NULL) 65158866Seric { 6523149Seric rp = *rvp; 6538058Seric if (tTd(21, 35)) 6548058Seric { 65558825Seric printf("ADVANCE rp="); 65657531Seric xputs(rp); 65757532Seric printf(", ap="); 6588058Seric xputs(ap); 6598069Seric printf("\n"); 6608058Seric } 66156678Seric if (rp == NULL) 66256326Seric { 6633149Seric /* end-of-pattern before end-of-address */ 6648058Seric goto backup; 66556678Seric } 66658173Seric if (ap == NULL && (*rp & 0377) != MATCHZANY && 66758827Seric (*rp & 0377) != MATCHZERO) 66856326Seric { 66958825Seric /* end-of-input with patterns left */ 67058825Seric goto backup; 671297Seric } 67256326Seric 67358050Seric switch (*rp & 0377) 6748058Seric { 67556678Seric register STAB *s; 67658814Seric char buf[MAXLINE]; 67756326Seric 67856678Seric case MATCHCLASS: 67958825Seric /* match any phrase in a class */ 68058825Seric mlp->pattern = rvp; 68158814Seric mlp->first = avp; 68258814Seric extendclass: 68358825Seric ap = *avp; 68458825Seric if (ap == NULL) 68558814Seric goto backup; 68658814Seric mlp->last = avp++; 68758814Seric cataddr(mlp->first, mlp->last, buf, sizeof buf, '\0'); 68858814Seric s = stab(buf, ST_CLASS, ST_FIND); 68956678Seric if (s == NULL || !bitnset(rp[1], s->s_class)) 69056326Seric { 69158825Seric if (tTd(21, 36)) 69258825Seric { 69358825Seric printf("EXTEND rp="); 69458825Seric xputs(rp); 69558825Seric printf(", ap="); 69658825Seric xputs(ap); 69758825Seric printf("\n"); 69858825Seric } 69958825Seric goto extendclass; 70056326Seric } 70158825Seric if (tTd(21, 36)) 70258825Seric printf("CLMATCH\n"); 70358814Seric mlp++; 70458814Seric break; 7054060Seric 70658825Seric case MATCHNCLASS: 70758825Seric /* match any token not in a class */ 70858825Seric s = stab(ap, ST_CLASS, ST_FIND); 70958825Seric if (s != NULL && bitnset(rp[1], s->s_class)) 71058825Seric goto backup; 71158825Seric 71258825Seric /* fall through */ 71358825Seric 71456678Seric case MATCHONE: 71556678Seric case MATCHANY: 71656678Seric /* match exactly one token */ 71758825Seric mlp->pattern = rvp; 71856678Seric mlp->first = avp; 71956678Seric mlp->last = avp++; 7208058Seric mlp++; 72156678Seric break; 7228058Seric 72356678Seric case MATCHZANY: 72456678Seric /* match zero or more tokens */ 72558825Seric mlp->pattern = rvp; 72656678Seric mlp->first = avp; 72756678Seric mlp->last = avp - 1; 72856678Seric mlp++; 72956678Seric break; 73056326Seric 73158827Seric case MATCHZERO: 73258173Seric /* match zero tokens */ 73358173Seric break; 73458173Seric 73559027Seric case MACRODEXPAND: 73659027Seric /* 73759027Seric ** Match against run-time macro. 73859027Seric ** This algorithm is broken for the 73959027Seric ** general case (no recursive macros, 74059027Seric ** improper tokenization) but should 74159027Seric ** work for the usual cases. 74259027Seric */ 74359027Seric 74459027Seric ap = macvalue(rp[1], e); 74559027Seric mlp->first = avp; 74659027Seric if (tTd(21, 2)) 74759027Seric printf("rewrite: LHS $&%c => \"%s\"\n", 74859027Seric rp[1], 74959027Seric ap == NULL ? "(NULL)" : ap); 75059027Seric 75159027Seric if (ap == NULL) 75259027Seric break; 75359027Seric while (*ap != NULL) 75459027Seric { 75559027Seric if (*avp == NULL || 75659027Seric strncasecmp(ap, *avp, strlen(*avp)) != 0) 75759027Seric { 75859027Seric /* no match */ 75959027Seric avp = mlp->first; 76059027Seric goto backup; 76159027Seric } 76259027Seric ap += strlen(*avp++); 76359027Seric } 76459027Seric 76559027Seric /* match */ 76659027Seric break; 76759027Seric 76856678Seric default: 76956678Seric /* must have exact match */ 77056678Seric if (strcasecmp(rp, ap)) 7718058Seric goto backup; 7724468Seric avp++; 77356678Seric break; 7743149Seric } 7753149Seric 77656678Seric /* successful match on this token */ 7773149Seric rvp++; 7783149Seric continue; 7793149Seric 78058825Seric backup: 78156678Seric /* match failed -- back up */ 78258825Seric while (--mlp >= mlist) 7833149Seric { 78458825Seric rvp = mlp->pattern; 78556678Seric rp = *rvp; 78658825Seric avp = mlp->last + 1; 78758825Seric ap = *avp; 78858825Seric 78958825Seric if (tTd(21, 36)) 79058825Seric { 79158825Seric printf("BACKUP rp="); 79258825Seric xputs(rp); 79358825Seric printf(", ap="); 79458825Seric xputs(ap); 79558825Seric printf("\n"); 79658825Seric } 79758825Seric 79858825Seric if (ap == NULL) 79958825Seric { 80058825Seric /* run off the end -- back up again */ 80158825Seric continue; 80258825Seric } 80358050Seric if ((*rp & 0377) == MATCHANY || 80458050Seric (*rp & 0377) == MATCHZANY) 8054468Seric { 80656678Seric /* extend binding and continue */ 80758825Seric mlp->last = avp++; 80856678Seric rvp++; 80958825Seric mlp++; 81056678Seric break; 8114468Seric } 81258825Seric if ((*rp & 0377) == MATCHCLASS) 81356678Seric { 81458814Seric /* extend binding and try again */ 81558825Seric mlp->last = avp++; 81658814Seric goto extendclass; 81758814Seric } 8183149Seric } 8193149Seric 82058825Seric if (mlp < mlist) 82156678Seric { 82256678Seric /* total failure to match */ 82356326Seric break; 8243149Seric } 825297Seric } 8263149Seric 8273149Seric /* 82856678Seric ** See if we successfully matched 8293149Seric */ 8303149Seric 83158827Seric if (mlp < mlist || *rvp != NULL) 8323149Seric { 8339374Seric if (tTd(21, 10)) 8349374Seric printf("----- rule fails\n"); 8359374Seric rwr = rwr->r_next; 83658866Seric ruleno++; 8379374Seric continue; 8389374Seric } 8393149Seric 8409374Seric rvp = rwr->r_rhs; 8419374Seric if (tTd(21, 12)) 8429374Seric { 8439374Seric printf("-----rule matches:"); 84456678Seric printav(rvp); 8459374Seric } 8469374Seric 8479374Seric rp = *rvp; 84858050Seric if ((*rp & 0377) == CANONUSER) 8499374Seric { 8509374Seric rvp++; 8519374Seric rwr = rwr->r_next; 85258866Seric ruleno++; 8539374Seric } 85458050Seric else if ((*rp & 0377) == CANONHOST) 8559374Seric { 8569374Seric rvp++; 8579374Seric rwr = NULL; 8589374Seric } 85958050Seric else if ((*rp & 0377) == CANONNET) 8609374Seric rwr = NULL; 8619374Seric 8629374Seric /* substitute */ 8639374Seric for (avp = npvp; *rvp != NULL; rvp++) 8649374Seric { 8659374Seric register struct match *m; 8669374Seric register char **pp; 8679374Seric 8688058Seric rp = *rvp; 86958050Seric if ((*rp & 0377) == MATCHREPL) 8708058Seric { 87116914Seric /* substitute from LHS */ 87216914Seric m = &mlist[rp[1] - '1']; 87356678Seric if (m < mlist || m >= mlp) 8749374Seric { 87558151Seric syserr("554 rewrite: ruleset %d: replacement $%c out of bounds", 87656326Seric ruleset, rp[1]); 87759084Seric return EX_CONFIG; 8789374Seric } 87916914Seric if (tTd(21, 15)) 88016914Seric { 88116914Seric printf("$%c:", rp[1]); 88216914Seric pp = m->first; 88356678Seric while (pp <= m->last) 88416914Seric { 88516914Seric printf(" %x=\"", *pp); 88616914Seric (void) fflush(stdout); 88716914Seric printf("%s\"", *pp++); 88816914Seric } 88916914Seric printf("\n"); 89016914Seric } 8919374Seric pp = m->first; 89256678Seric while (pp <= m->last) 8933149Seric { 89416914Seric if (avp >= &npvp[MAXATOM]) 89556678Seric { 89658151Seric syserr("554 rewrite: expansion too long"); 89759084Seric return EX_DATAERR; 89856678Seric } 89916914Seric *avp++ = *pp++; 9003149Seric } 9013149Seric } 90216914Seric else 9038226Seric { 90416914Seric /* vanilla replacement */ 9059374Seric if (avp >= &npvp[MAXATOM]) 90616889Seric { 90756678Seric toolong: 90858151Seric syserr("554 rewrite: expansion too long"); 90959084Seric return EX_DATAERR; 91016889Seric } 91159027Seric if ((*rp & 0377) != MACRODEXPAND) 91259027Seric *avp++ = rp; 91359027Seric else 91459027Seric { 91559027Seric *avp = macvalue(rp[1], e); 91659027Seric if (tTd(21, 2)) 91759027Seric printf("rewrite: RHS $&%c => \"%s\"\n", 91859027Seric rp[1], 91959027Seric *avp == NULL ? "(NULL)" : *avp); 92059027Seric if (*avp != NULL) 92159027Seric avp++; 92259027Seric } 9238226Seric } 9249374Seric } 9259374Seric *avp++ = NULL; 92616914Seric 92716914Seric /* 92856678Seric ** Check for any hostname/keyword lookups. 92916914Seric */ 93016914Seric 93116914Seric for (rvp = npvp; *rvp != NULL; rvp++) 93216914Seric { 93356678Seric char **hbrvp; 93416914Seric char **xpvp; 93516914Seric int trsize; 93617473Seric char *olddelimchar; 93756678Seric char *replac; 93856678Seric int endtoken; 93956678Seric STAB *map; 94056678Seric char *mapname; 94156678Seric char **key_rvp; 94256678Seric char **arg_rvp; 94356678Seric char **default_rvp; 94456678Seric char buf[MAXNAME + 1]; 94516914Seric char *pvpb1[MAXATOM + 1]; 94656823Seric char *argvect[10]; 94717174Seric char pvpbuf[PSBUFSIZE]; 94816914Seric 94958050Seric if ((**rvp & 0377) != HOSTBEGIN && 95058050Seric (**rvp & 0377) != LOOKUPBEGIN) 95116914Seric continue; 95216914Seric 95316914Seric /* 95456678Seric ** Got a hostname/keyword lookup. 95516914Seric ** 95616914Seric ** This could be optimized fairly easily. 95716914Seric */ 95816914Seric 95916914Seric hbrvp = rvp; 96058050Seric if ((**rvp & 0377) == HOSTBEGIN) 96156327Seric { 96256678Seric endtoken = HOSTEND; 96356678Seric mapname = "host"; 96456327Seric } 96556326Seric else 96656327Seric { 96756678Seric endtoken = LOOKUPEND; 96856678Seric mapname = *++rvp; 96956327Seric } 97056678Seric map = stab(mapname, ST_MAP, ST_FIND); 97156678Seric if (map == NULL) 97258151Seric syserr("554 rewrite: map %s not found", mapname); 97356678Seric 97456678Seric /* extract the match part */ 97556678Seric key_rvp = ++rvp; 97656823Seric default_rvp = NULL; 97756823Seric arg_rvp = argvect; 97856823Seric xpvp = NULL; 97956823Seric replac = pvpbuf; 98058050Seric while (*rvp != NULL && (**rvp & 0377) != endtoken) 98153654Seric { 98258050Seric int nodetype = **rvp & 0377; 98356823Seric 98456823Seric if (nodetype != CANONHOST && nodetype != CANONUSER) 98556678Seric { 98656823Seric rvp++; 98756823Seric continue; 98856823Seric } 98956823Seric 99056823Seric *rvp++ = NULL; 99156823Seric 99256823Seric if (xpvp != NULL) 99356823Seric { 99458814Seric cataddr(xpvp, NULL, replac, 99558082Seric &pvpbuf[sizeof pvpbuf] - replac, 99658082Seric '\0'); 99756823Seric *++arg_rvp = replac; 99856823Seric replac += strlen(replac) + 1; 99956823Seric xpvp = NULL; 100056823Seric } 100156823Seric switch (nodetype) 100256823Seric { 100356678Seric case CANONHOST: 100456823Seric xpvp = rvp; 100556678Seric break; 100656678Seric 100756678Seric case CANONUSER: 100856678Seric default_rvp = rvp; 100956678Seric break; 101056678Seric } 101153654Seric } 101216914Seric if (*rvp != NULL) 101316914Seric *rvp++ = NULL; 101456823Seric if (xpvp != NULL) 101556823Seric { 101658814Seric cataddr(xpvp, NULL, replac, 101758082Seric &pvpbuf[sizeof pvpbuf] - replac, 101858082Seric '\0'); 101956823Seric *++arg_rvp = replac; 102056823Seric } 102156823Seric *++arg_rvp = NULL; 102216914Seric 102316914Seric /* save the remainder of the input string */ 102416914Seric trsize = (int) (avp - rvp + 1) * sizeof *rvp; 102516914Seric bcopy((char *) rvp, (char *) pvpb1, trsize); 102616914Seric 102756678Seric /* look it up */ 102858814Seric cataddr(key_rvp, NULL, buf, sizeof buf, '\0'); 102956823Seric argvect[0] = buf; 103056678Seric if (map != NULL && bitset(MF_VALID, map->s_map.map_flags)) 103156836Seric { 103256836Seric int bsize = sizeof buf - 1; 103359084Seric auto int stat = EX_OK; 103456836Seric 103556836Seric if (map->s_map.map_app != NULL) 103656836Seric bsize -= strlen(map->s_map.map_app); 103758796Seric if (tTd(60, 1)) 103858796Seric printf("map_lookup(%s, %s) => ", 103958796Seric mapname, buf); 104056836Seric replac = (*map->s_map.map_class->map_lookup)(&map->s_map, 104159084Seric buf, sizeof buf - 1, argvect, 104259084Seric &stat); 104356836Seric if (replac != NULL && map->s_map.map_app != NULL) 104456836Seric strcat(replac, map->s_map.map_app); 104558796Seric if (tTd(60, 1)) 104659084Seric printf("%s (%d)\n", 104759084Seric replac ? replac : "NOT FOUND", 104859084Seric stat); 104959084Seric 105059084Seric /* should recover if stat == EX_TEMPFAIL */ 105159084Seric if (stat == EX_TEMPFAIL) 105259084Seric rstat = stat; 105356836Seric } 105453654Seric else 105556678Seric replac = NULL; 105656678Seric 105756678Seric /* if no replacement, use default */ 105856823Seric if (replac == NULL && default_rvp != NULL) 105956823Seric { 106056823Seric char buf2[sizeof buf]; 106156823Seric 106256823Seric /* rewrite the default with % translations */ 106358814Seric cataddr(default_rvp, NULL, buf2, sizeof buf2, '\0'); 106456823Seric map_rewrite(buf2, sizeof buf2, buf, sizeof buf, 106556823Seric argvect); 106656823Seric replac = buf; 106756823Seric } 106856823Seric 106956678Seric if (replac == NULL) 107051317Seric { 107156823Seric xpvp = key_rvp; 107253654Seric } 107356678Seric else 107453654Seric { 107556678Seric /* scan the new replacement */ 107658333Seric xpvp = prescan(replac, '\0', pvpbuf, NULL); 107753654Seric if (xpvp == NULL) 107851317Seric { 107958403Seric /* prescan already printed error */ 108059084Seric return EX_DATAERR; 108156678Seric } 108251317Seric } 108351317Seric 108416914Seric /* append it to the token list */ 108556678Seric for (avp = hbrvp; *xpvp != NULL; xpvp++) 108656678Seric { 108717174Seric *avp++ = newstr(*xpvp); 108816920Seric if (avp >= &npvp[MAXATOM]) 108916914Seric goto toolong; 109017174Seric } 109116914Seric 109216914Seric /* restore the old trailing information */ 109356678Seric for (xpvp = pvpb1; (*avp++ = *xpvp++) != NULL; ) 109416920Seric if (avp >= &npvp[MAXATOM]) 109516914Seric goto toolong; 109617174Seric 109756678Seric break; 109816914Seric } 109916914Seric 110016914Seric /* 110116914Seric ** Check for subroutine calls. 110216914Seric */ 110316914Seric 110458050Seric if (*npvp != NULL && (**npvp & 0377) == CALLSUBR) 110556678Seric { 110659084Seric int stat; 110759084Seric 110856678Seric bcopy((char *) &npvp[2], (char *) pvp, 110956678Seric (int) (avp - npvp - 2) * sizeof *avp); 111056678Seric if (tTd(21, 3)) 111156678Seric printf("-----callsubr %s\n", npvp[1]); 111259084Seric stat = rewrite(pvp, atoi(npvp[1]), e); 111359084Seric if (rstat == EX_OK || stat == EX_TEMPFAIL) 111459084Seric rstat = stat; 111556678Seric } 111656678Seric else 111756678Seric { 111817348Seric bcopy((char *) npvp, (char *) pvp, 111916900Seric (int) (avp - npvp) * sizeof *avp); 112056678Seric } 11219374Seric if (tTd(21, 4)) 11229374Seric { 11239374Seric printf("rewritten as:"); 112456678Seric printav(pvp); 11259374Seric } 1126297Seric } 11278069Seric 11289279Seric if (OpMode == MD_TEST || tTd(21, 2)) 11298069Seric { 11308959Seric printf("rewrite: ruleset %2d returns:", ruleset); 113156678Seric printav(pvp); 11328069Seric } 113359084Seric 113459084Seric return rstat; 11353149Seric } 11363149Seric /* 11373149Seric ** BUILDADDR -- build address from token vector. 11383149Seric ** 11393149Seric ** Parameters: 11403149Seric ** tv -- token vector. 11413149Seric ** a -- pointer to address descriptor to fill. 11423149Seric ** If NULL, one will be allocated. 114358966Seric ** e -- the current envelope. 11443149Seric ** 11453149Seric ** Returns: 11464279Seric ** NULL if there was an error. 11474279Seric ** 'a' otherwise. 11483149Seric ** 11493149Seric ** Side Effects: 11503149Seric ** fills in 'a' 11513149Seric */ 11523149Seric 115357249Seric struct errcodes 115457249Seric { 115557249Seric char *ec_name; /* name of error code */ 115657249Seric int ec_code; /* numeric code */ 115757249Seric } ErrorCodes[] = 115857249Seric { 115957249Seric "usage", EX_USAGE, 116057249Seric "nouser", EX_NOUSER, 116157249Seric "nohost", EX_NOHOST, 116257249Seric "unavailable", EX_UNAVAILABLE, 116357249Seric "software", EX_SOFTWARE, 116457249Seric "tempfail", EX_TEMPFAIL, 116557249Seric "protocol", EX_PROTOCOL, 116657249Seric #ifdef EX_CONFIG 116757249Seric "config", EX_CONFIG, 116857249Seric #endif 116957249Seric NULL, EX_UNAVAILABLE, 117057249Seric }; 117157249Seric 117256678Seric ADDRESS * 117358966Seric buildaddr(tv, a, e) 11743149Seric register char **tv; 11753149Seric register ADDRESS *a; 117658966Seric register ENVELOPE *e; 11773149Seric { 11783149Seric struct mailer **mp; 11793149Seric register struct mailer *m; 118058008Seric char *bp; 118158008Seric int spaceleft; 118257402Seric static char buf[MAXNAME]; 11833149Seric 11843149Seric if (a == NULL) 11853149Seric a = (ADDRESS *) xalloc(sizeof *a); 118616889Seric bzero((char *) a, sizeof *a); 11873149Seric 11883149Seric /* figure out what net/mailer to use */ 118958050Seric if ((**tv & 0377) != CANONNET) 11904279Seric { 119158151Seric syserr("554 buildaddr: no net"); 11924279Seric return (NULL); 11934279Seric } 11943149Seric tv++; 119558680Seric if (strcasecmp(*tv, "error") == 0) 11964279Seric { 119758050Seric if ((**++tv & 0377) == CANONHOST) 119810183Seric { 119957249Seric register struct errcodes *ep; 120057249Seric 120158050Seric if (isascii(**++tv) && isdigit(**tv)) 120257249Seric { 120357249Seric setstat(atoi(*tv)); 120457249Seric } 120557249Seric else 120657249Seric { 120757249Seric for (ep = ErrorCodes; ep->ec_name != NULL; ep++) 120857249Seric if (strcasecmp(ep->ec_name, *tv) == 0) 120957249Seric break; 121057249Seric setstat(ep->ec_code); 121157249Seric } 121210183Seric tv++; 121310183Seric } 121458050Seric if ((**tv & 0377) != CANONUSER) 121558151Seric syserr("554 buildaddr: error: no user"); 121658814Seric cataddr(++tv, NULL, buf, sizeof buf, ' '); 121758082Seric stripquotes(buf); 12184279Seric usrerr(buf); 121958966Seric if (e->e_message == NULL) 122058966Seric e->e_message = newstr(buf); 12214279Seric return (NULL); 12224279Seric } 122357402Seric 12244598Seric for (mp = Mailer; (m = *mp++) != NULL; ) 12253149Seric { 122658680Seric if (strcasecmp(m->m_name, *tv) == 0) 12273149Seric break; 12283149Seric } 12293149Seric if (m == NULL) 12304279Seric { 123158151Seric syserr("554 buildaddr: unknown mailer %s", *tv); 12324279Seric return (NULL); 12334279Seric } 12344598Seric a->q_mailer = m; 12353149Seric 12363149Seric /* figure out what host (if any) */ 123756678Seric tv++; 123858509Seric if ((**tv & 0377) == CANONHOST) 12393149Seric { 124058008Seric bp = buf; 124158008Seric spaceleft = sizeof buf - 1; 124258050Seric while (*++tv != NULL && (**tv & 0377) != CANONUSER) 124358008Seric { 124458008Seric int i = strlen(*tv); 124558008Seric 124658008Seric if (i > spaceleft) 124758008Seric { 124858008Seric /* out of space for this address */ 124958008Seric if (spaceleft >= 0) 125058151Seric syserr("554 buildaddr: host too long (%.40s...)", 125158008Seric buf); 125258008Seric i = spaceleft; 125358008Seric spaceleft = 0; 125458008Seric } 125558008Seric if (i <= 0) 125658008Seric continue; 125758008Seric bcopy(*tv, bp, i); 125858008Seric bp += i; 125958008Seric spaceleft -= i; 126058008Seric } 126158010Seric *bp = '\0'; 12625704Seric a->q_host = newstr(buf); 12633149Seric } 126457249Seric else 126558509Seric { 126658509Seric if (!bitnset(M_LOCALMAILER, m->m_flags)) 126758509Seric { 126858509Seric syserr("554 buildaddr: no host"); 126958509Seric return (NULL); 127058509Seric } 127157249Seric a->q_host = NULL; 127258509Seric } 12733149Seric 12743149Seric /* figure out the user */ 127558050Seric if (*tv == NULL || (**tv & 0377) != CANONUSER) 12764279Seric { 127758151Seric syserr("554 buildaddr: no user"); 12784279Seric return (NULL); 12794279Seric } 128057402Seric tv++; 128151317Seric 128257402Seric /* do special mapping for local mailer */ 128357402Seric if (m == LocalMailer && *tv != NULL) 128457402Seric { 128557454Seric register char *p = *tv; 128657454Seric 128757454Seric if (*p == '"') 128857454Seric p++; 128957454Seric if (*p == '|') 129057402Seric a->q_mailer = m = ProgMailer; 129157454Seric else if (*p == '/') 129257402Seric a->q_mailer = m = FileMailer; 129357454Seric else if (*p == ':') 129457402Seric { 129557402Seric /* may be :include: */ 129658814Seric cataddr(tv, NULL, buf, sizeof buf, '\0'); 129758008Seric stripquotes(buf); 129858008Seric if (strncasecmp(buf, ":include:", 9) == 0) 129958008Seric { 130058008Seric /* if :include:, don't need further rewriting */ 130157402Seric a->q_mailer = m = InclMailer; 130258008Seric a->q_user = &buf[9]; 130358008Seric return (a); 130458008Seric } 130557402Seric } 130657402Seric } 130757402Seric 130858008Seric if (m == LocalMailer && *tv != NULL && strcmp(*tv, "@") == 0) 130958008Seric { 131058008Seric tv++; 131158008Seric a->q_flags |= QNOTREMOTE; 131258008Seric } 131358008Seric 131458673Seric /* do cleanup of final address */ 131559084Seric (void) rewrite(tv, 4, e); 131619040Seric 131719040Seric /* save the result for the command line/RCPT argument */ 131858814Seric cataddr(tv, NULL, buf, sizeof buf, '\0'); 13193149Seric a->q_user = buf; 13203149Seric 132158670Seric /* 132258670Seric ** Do mapping to lower case as requested by mailer 132358670Seric */ 132458670Seric 132558670Seric if (a->q_host != NULL && !bitnset(M_HST_UPPER, m->m_flags)) 132658670Seric makelower(a->q_host); 132758670Seric if (!bitnset(M_USR_UPPER, m->m_flags)) 132858670Seric makelower(a->q_user); 132958670Seric 13303149Seric return (a); 13313149Seric } 13323188Seric /* 13334228Seric ** CATADDR -- concatenate pieces of addresses (putting in <LWSP> subs) 13344228Seric ** 13354228Seric ** Parameters: 13364228Seric ** pvp -- parameter vector to rebuild. 133758814Seric ** evp -- last parameter to include. Can be NULL to 133858814Seric ** use entire pvp. 13394228Seric ** buf -- buffer to build the string into. 13404228Seric ** sz -- size of buf. 134158082Seric ** spacesub -- the space separator character; if null, 134258082Seric ** use SpaceSub. 13434228Seric ** 13444228Seric ** Returns: 13454228Seric ** none. 13464228Seric ** 13474228Seric ** Side Effects: 13484228Seric ** Destroys buf. 13494228Seric */ 13504228Seric 135158814Seric cataddr(pvp, evp, buf, sz, spacesub) 13524228Seric char **pvp; 135358814Seric char **evp; 13544228Seric char *buf; 13554228Seric register int sz; 135658082Seric char spacesub; 13574228Seric { 13584228Seric bool oatomtok = FALSE; 135956678Seric bool natomtok = FALSE; 13604228Seric register int i; 13614228Seric register char *p; 13624228Seric 136358082Seric if (spacesub == '\0') 136458082Seric spacesub = SpaceSub; 136558082Seric 13668423Seric if (pvp == NULL) 13678423Seric { 136823109Seric (void) strcpy(buf, ""); 13698423Seric return; 13708423Seric } 13714228Seric p = buf; 137211156Seric sz -= 2; 13734228Seric while (*pvp != NULL && (i = strlen(*pvp)) < sz) 13744228Seric { 13758078Seric natomtok = (toktype(**pvp) == ATM); 13764228Seric if (oatomtok && natomtok) 137758082Seric *p++ = spacesub; 13784228Seric (void) strcpy(p, *pvp); 13794228Seric oatomtok = natomtok; 13804228Seric p += i; 138111156Seric sz -= i + 1; 138258814Seric if (pvp++ == evp) 138358814Seric break; 13844228Seric } 13854228Seric *p = '\0'; 13864228Seric } 13874228Seric /* 13883188Seric ** SAMEADDR -- Determine if two addresses are the same 13893188Seric ** 13903188Seric ** This is not just a straight comparison -- if the mailer doesn't 13913188Seric ** care about the host we just ignore it, etc. 13923188Seric ** 13933188Seric ** Parameters: 13943188Seric ** a, b -- pointers to the internal forms to compare. 13953188Seric ** 13963188Seric ** Returns: 13973188Seric ** TRUE -- they represent the same mailbox. 13983188Seric ** FALSE -- they don't. 13993188Seric ** 14003188Seric ** Side Effects: 14013188Seric ** none. 14023188Seric */ 14033188Seric 14043188Seric bool 14059374Seric sameaddr(a, b) 14063188Seric register ADDRESS *a; 14073188Seric register ADDRESS *b; 14083188Seric { 14093188Seric /* if they don't have the same mailer, forget it */ 14103188Seric if (a->q_mailer != b->q_mailer) 14113188Seric return (FALSE); 14123188Seric 14133188Seric /* if the user isn't the same, we can drop out */ 141456678Seric if (strcmp(a->q_user, b->q_user) != 0) 14153188Seric return (FALSE); 14163188Seric 141758438Seric /* if we have good uids for both but the differ, these are different */ 141858438Seric if (bitset(QGOODUID, a->q_flags & b->q_flags) && a->q_uid != b->q_uid) 141958438Seric return (FALSE); 142058438Seric 142158509Seric /* otherwise compare hosts (but be careful for NULL ptrs) */ 142258509Seric if (a->q_host == b->q_host) 142358509Seric { 142458509Seric /* probably both null pointers */ 14253188Seric return (TRUE); 142658509Seric } 14273188Seric if (a->q_host == NULL || b->q_host == NULL) 142858509Seric { 142958509Seric /* only one is a null pointer */ 14303188Seric return (FALSE); 143158509Seric } 143256678Seric if (strcmp(a->q_host, b->q_host) != 0) 14333188Seric return (FALSE); 14343188Seric 14353188Seric return (TRUE); 14363188Seric } 14373234Seric /* 14383234Seric ** PRINTADDR -- print address (for debugging) 14393234Seric ** 14403234Seric ** Parameters: 14413234Seric ** a -- the address to print 14423234Seric ** follow -- follow the q_next chain. 14433234Seric ** 14443234Seric ** Returns: 14453234Seric ** none. 14463234Seric ** 14473234Seric ** Side Effects: 14483234Seric ** none. 14493234Seric */ 14503234Seric 14513234Seric printaddr(a, follow) 14523234Seric register ADDRESS *a; 14533234Seric bool follow; 14543234Seric { 14555001Seric bool first = TRUE; 145657731Seric register MAILER *m; 145757731Seric MAILER pseudomailer; 14585001Seric 14593234Seric while (a != NULL) 14603234Seric { 14615001Seric first = FALSE; 14624443Seric printf("%x=", a); 14634085Seric (void) fflush(stdout); 146457731Seric 146557731Seric /* find the mailer -- carefully */ 146657731Seric m = a->q_mailer; 146757731Seric if (m == NULL) 146857731Seric { 146957731Seric m = &pseudomailer; 147057731Seric m->m_mno = -1; 147157731Seric m->m_name = "NULL"; 147257731Seric } 147357731Seric 147458680Seric printf("%s:\n\tmailer %d (%s), host `%s', user `%s', ruser `%s'\n", 147557731Seric a->q_paddr, m->m_mno, m->m_name, 147640973Sbostic a->q_host, a->q_user, a->q_ruser? a->q_ruser: "<null>"); 147759269Seric printf("\tnext=%x, flags=%o, alias %x, uid %d, gid %d\n", 147859269Seric a->q_next, a->q_flags, a->q_alias, a->q_uid, a->q_gid); 147959269Seric printf("\towner=%s, home=\"%s\", fullname=\"%s\"\n", 148059269Seric a->q_owner == NULL ? "(none)" : a->q_owner, 148159269Seric a->q_home, a->q_fullname); 14824996Seric 14833234Seric if (!follow) 14843234Seric return; 14854996Seric a = a->q_next; 14863234Seric } 14875001Seric if (first) 14884443Seric printf("[NULL]\n"); 14893234Seric } 14904317Seric 14917682Seric /* 14927682Seric ** REMOTENAME -- return the name relative to the current mailer 14937682Seric ** 14947682Seric ** Parameters: 14957682Seric ** name -- the name to translate. 14968069Seric ** m -- the mailer that we want to do rewriting relative 14978069Seric ** to. 149859163Seric ** flags -- fine tune operations. 149959163Seric ** pstat -- pointer to status word. 150058020Seric ** e -- the current envelope. 15017682Seric ** 15027682Seric ** Returns: 15037682Seric ** the text string representing this address relative to 15047682Seric ** the receiving mailer. 15057682Seric ** 15067682Seric ** Side Effects: 15077682Seric ** none. 15087682Seric ** 15097682Seric ** Warnings: 15107682Seric ** The text string returned is tucked away locally; 15117682Seric ** copy it if you intend to save it. 15127682Seric */ 15137682Seric 15147682Seric char * 151559163Seric remotename(name, m, flags, pstat, e) 15167682Seric char *name; 151756678Seric struct mailer *m; 151859163Seric int flags; 151959163Seric int *pstat; 152056678Seric register ENVELOPE *e; 15217682Seric { 15228069Seric register char **pvp; 15238069Seric char *fancy; 152456678Seric extern char *macvalue(); 152556678Seric char *oldg = macvalue('g', e); 152658020Seric int rwset; 15277682Seric static char buf[MAXNAME]; 15287682Seric char lbuf[MAXNAME]; 152916914Seric char pvpbuf[PSBUFSIZE]; 153056678Seric extern char **prescan(); 153156678Seric extern char *crackaddr(); 15327682Seric 15337755Seric if (tTd(12, 1)) 15347755Seric printf("remotename(%s)\n", name); 15357755Seric 153610177Seric /* don't do anything if we are tagging it as special */ 153759163Seric if (bitset(RF_SENDERADDR, flags)) 153859163Seric rwset = bitset(RF_HEADERADDR, flags) ? m->m_sh_rwset 153959163Seric : m->m_se_rwset; 154058020Seric else 154159163Seric rwset = bitset(RF_HEADERADDR, flags) ? m->m_rh_rwset 154259163Seric : m->m_re_rwset; 154358020Seric if (rwset < 0) 154410177Seric return (name); 154510177Seric 15467682Seric /* 15478181Seric ** Do a heuristic crack of this name to extract any comment info. 15488181Seric ** This will leave the name as a comment and a $g macro. 15497889Seric */ 15507889Seric 155159163Seric if (bitset(RF_CANONICAL, flags) || bitnset(M_NOCOMMENT, m->m_flags)) 155258050Seric fancy = "\201g"; 155310310Seric else 155410310Seric fancy = crackaddr(name); 15557889Seric 15568181Seric /* 15578181Seric ** Turn the name into canonical form. 15588181Seric ** Normally this will be RFC 822 style, i.e., "user@domain". 15598181Seric ** If this only resolves to "user", and the "C" flag is 15608181Seric ** specified in the sending mailer, then the sender's 15618181Seric ** domain will be appended. 15628181Seric */ 15638181Seric 156458333Seric pvp = prescan(name, '\0', pvpbuf, NULL); 15657889Seric if (pvp == NULL) 15667889Seric return (name); 156759163Seric if (rewrite(pvp, 3, e) == EX_TEMPFAIL) 156859163Seric *pstat = EX_TEMPFAIL; 156959163Seric if (bitset(RF_ADDDOMAIN, flags) && e->e_fromdomain != NULL) 15708181Seric { 15718181Seric /* append from domain to this address */ 15728181Seric register char **pxp = pvp; 15738181Seric 15749594Seric /* see if there is an "@domain" in the current name */ 15758181Seric while (*pxp != NULL && strcmp(*pxp, "@") != 0) 15768181Seric pxp++; 15778181Seric if (*pxp == NULL) 15788181Seric { 15799594Seric /* no.... append the "@domain" from the sender */ 158056678Seric register char **qxq = e->e_fromdomain; 15818181Seric 15829594Seric while ((*pxp++ = *qxq++) != NULL) 15839594Seric continue; 158459163Seric if (rewrite(pvp, 3, e) == EX_TEMPFAIL) 158559163Seric *pstat = EX_TEMPFAIL; 15868181Seric } 15878181Seric } 15888181Seric 15898181Seric /* 15908959Seric ** Do more specific rewriting. 159156678Seric ** Rewrite using ruleset 1 or 2 depending on whether this is 159256678Seric ** a sender address or not. 15938181Seric ** Then run it through any receiving-mailer-specific rulesets. 15948181Seric */ 15958181Seric 159659163Seric if (bitset(RF_SENDERADDR, flags)) 1597*59541Seric { 159859163Seric if (rewrite(pvp, 1, e) == EX_TEMPFAIL) 159959163Seric *pstat = EX_TEMPFAIL; 1600*59541Seric } 16018069Seric else 1602*59541Seric { 160359163Seric if (rewrite(pvp, 2, e) == EX_TEMPFAIL) 160459163Seric *pstat = EX_TEMPFAIL; 1605*59541Seric } 160658020Seric if (rwset > 0) 1607*59541Seric { 160859163Seric if (rewrite(pvp, rwset, e) == EX_TEMPFAIL) 160959163Seric *pstat = EX_TEMPFAIL; 1610*59541Seric } 16117682Seric 16128181Seric /* 16138959Seric ** Do any final sanitation the address may require. 16148959Seric ** This will normally be used to turn internal forms 16158959Seric ** (e.g., user@host.LOCAL) into external form. This 16168959Seric ** may be used as a default to the above rules. 16178959Seric */ 16188959Seric 161959163Seric if (rewrite(pvp, 4, e) == EX_TEMPFAIL) 162059163Seric *pstat = EX_TEMPFAIL; 16218959Seric 16228959Seric /* 16238181Seric ** Now restore the comment information we had at the beginning. 16248181Seric */ 16258181Seric 162658825Seric cataddr(pvp, NULL, lbuf, sizeof lbuf, '\0'); 162756678Seric define('g', lbuf, e); 162856678Seric expand(fancy, buf, &buf[sizeof buf - 1], e); 162956678Seric define('g', oldg, e); 16307682Seric 16317682Seric if (tTd(12, 1)) 16327755Seric printf("remotename => `%s'\n", buf); 16337682Seric return (buf); 16347682Seric } 163551317Seric /* 163656678Seric ** MAPLOCALUSER -- run local username through ruleset 5 for final redirection 163751317Seric ** 163851317Seric ** Parameters: 163956678Seric ** a -- the address to map (but just the user name part). 164056678Seric ** sendq -- the sendq in which to install any replacement 164156678Seric ** addresses. 164251317Seric ** 164351317Seric ** Returns: 164451317Seric ** none. 164551317Seric */ 164651317Seric 164756678Seric maplocaluser(a, sendq, e) 164856678Seric register ADDRESS *a; 164956678Seric ADDRESS **sendq; 165056678Seric ENVELOPE *e; 165151317Seric { 165256678Seric register char **pvp; 165356678Seric register ADDRESS *a1 = NULL; 165458333Seric auto char *delimptr; 165556678Seric char pvpbuf[PSBUFSIZE]; 165651317Seric 165756678Seric if (tTd(29, 1)) 165856678Seric { 165956678Seric printf("maplocaluser: "); 166056678Seric printaddr(a, FALSE); 166156678Seric } 166258333Seric pvp = prescan(a->q_user, '\0', pvpbuf, &delimptr); 166356678Seric if (pvp == NULL) 166456678Seric return; 166551317Seric 166659084Seric (void) rewrite(pvp, 5, e); 166758050Seric if (pvp[0] == NULL || (pvp[0][0] & 0377) != CANONNET) 166856678Seric return; 166951317Seric 167056678Seric /* if non-null, mailer destination specified -- has it changed? */ 167158966Seric a1 = buildaddr(pvp, NULL, e); 167256678Seric if (a1 == NULL || sameaddr(a, a1)) 167356678Seric return; 167451317Seric 167556678Seric /* mark old address as dead; insert new address */ 167656678Seric a->q_flags |= QDONTSEND; 167757731Seric if (tTd(29, 5)) 167857731Seric { 167957731Seric printf("maplocaluser: QDONTSEND "); 168057731Seric printaddr(a, FALSE); 168157731Seric } 168256678Seric a1->q_alias = a; 168358333Seric allocaddr(a1, 1, NULL, delimptr); 168456678Seric (void) recipient(a1, sendq, e); 168551317Seric } 168658802Seric /* 168758802Seric ** DEQUOTE_INIT -- initialize dequote map 168858802Seric ** 168958802Seric ** This is a no-op. 169058802Seric ** 169158802Seric ** Parameters: 169258802Seric ** map -- the internal map structure. 169358802Seric ** mapname -- the name of the mapl. 169458802Seric ** args -- arguments. 169558802Seric ** 169658802Seric ** Returns: 169758802Seric ** TRUE. 169858802Seric */ 169958802Seric 170058802Seric bool 170158802Seric dequote_init(map, mapname, args) 170258802Seric MAP *map; 170358802Seric char *mapname; 170458802Seric char *args; 170558802Seric { 170658805Seric register char *p = args; 170758805Seric 170858805Seric for (;;) 170958805Seric { 171058805Seric while (isascii(*p) && isspace(*p)) 171158805Seric p++; 171258805Seric if (*p != '-') 171358805Seric break; 171458805Seric switch (*++p) 171558805Seric { 171658805Seric case 'a': 171758805Seric map->map_app = ++p; 171858805Seric break; 171958805Seric } 172058805Seric while (*p != '\0' && !(isascii(*p) && isspace(*p))) 172158805Seric p++; 172258805Seric if (*p != '\0') 172358805Seric *p = '\0'; 172458805Seric } 172558805Seric if (map->map_app != NULL) 172658805Seric map->map_app = newstr(map->map_app); 172758805Seric 172858802Seric return TRUE; 172958802Seric } 173058802Seric /* 173158802Seric ** DEQUOTE_MAP -- unquote an address 173258802Seric ** 173358802Seric ** Parameters: 173458802Seric ** map -- the internal map structure (ignored). 173558802Seric ** buf -- the buffer to dequote. 173658802Seric ** bufsiz -- the size of that buffer. 173758802Seric ** av -- arguments (ignored). 173859084Seric ** statp -- pointer to status out-parameter. 173958802Seric ** 174058802Seric ** Returns: 174158802Seric ** NULL -- if there were no quotes, or if the resulting 174258802Seric ** unquoted buffer would not be acceptable to prescan. 174358802Seric ** else -- The dequoted buffer. 174458802Seric */ 174558802Seric 174658802Seric char * 174759084Seric dequote_map(map, buf, bufsiz, av, statp) 174858802Seric MAP *map; 174958802Seric char buf[]; 175058802Seric int bufsiz; 175158802Seric char **av; 175259084Seric int *statp; 175358802Seric { 175458802Seric register char *p; 175558802Seric register char *q; 175658802Seric register char c; 175758802Seric int anglecnt; 175858802Seric int cmntcnt; 175958802Seric int quotecnt; 176059089Seric int spacecnt; 176158802Seric bool quotemode; 176258802Seric bool bslashmode; 176358802Seric 176458802Seric anglecnt = 0; 176558802Seric cmntcnt = 0; 176658802Seric quotecnt = 0; 176759089Seric spacecnt = 0; 176858802Seric quotemode = FALSE; 176958802Seric bslashmode = FALSE; 177058802Seric 177158802Seric for (p = q = buf; (c = *p++) != '\0'; ) 177258802Seric { 177358802Seric if (bslashmode) 177458802Seric { 177558802Seric bslashmode = FALSE; 177658802Seric *q++ = c; 177758802Seric continue; 177858802Seric } 177958802Seric 178058802Seric switch (c) 178158802Seric { 178258802Seric case '\\': 178358802Seric bslashmode = TRUE; 178458802Seric break; 178558802Seric 178658802Seric case '(': 178758802Seric cmntcnt++; 178858802Seric break; 178958802Seric 179058802Seric case ')': 179158802Seric if (cmntcnt-- <= 0) 179258802Seric return NULL; 179358802Seric break; 179459089Seric 179559089Seric case ' ': 179659089Seric spacecnt++; 179759089Seric break; 179858802Seric } 179958802Seric 180058802Seric if (cmntcnt > 0) 180158802Seric { 180258802Seric *q++ = c; 180358802Seric continue; 180458802Seric } 180558802Seric 180658802Seric switch (c) 180758802Seric { 180858802Seric case '"': 180958802Seric quotemode = !quotemode; 181058802Seric quotecnt++; 181158802Seric continue; 181258802Seric 181358802Seric case '<': 181458802Seric anglecnt++; 181558802Seric break; 181658802Seric 181758802Seric case '>': 181858802Seric if (anglecnt-- <= 0) 181958802Seric return NULL; 182058802Seric break; 182158802Seric } 182258802Seric *q++ = c; 182358802Seric } 182458802Seric 182558802Seric if (anglecnt != 0 || cmntcnt != 0 || bslashmode || 182659089Seric quotemode || quotecnt <= 0 || spacecnt != 0) 182758802Seric return NULL; 182858802Seric *q++ = '\0'; 182958802Seric return buf; 183058802Seric } 1831