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*51317Seric static char sccsid[] = "@(#)parseaddr.c 5.14 (Berkeley) 10/05/91"; 1133730Sbostic #endif /* not lint */ 1222976Smiriam 133312Seric # 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. 43297Seric ** 44297Seric ** Returns: 45297Seric ** A pointer to the address descriptor header (`a' if 46297Seric ** `a' is non-NULL). 47297Seric ** NULL on error. 48297Seric ** 49297Seric ** Side Effects: 50297Seric ** none 51297Seric */ 52297Seric 539374Seric /* following delimiters are inherent to the internal algorithms */ 5416155Seric # define DELIMCHARS "\001()<>,;\\\"\r\n" /* word delimiters */ 552091Seric 562973Seric ADDRESS * 5711445Seric parseaddr(addr, a, copyf, delim) 58297Seric char *addr; 592973Seric register ADDRESS *a; 60297Seric int copyf; 6111445Seric char delim; 62297Seric { 633149Seric register char **pvp; 643149Seric register struct mailer *m; 6516914Seric char pvpbuf[PSBUFSIZE]; 663149Seric extern char **prescan(); 673149Seric extern ADDRESS *buildaddr(); 68297Seric 69297Seric /* 70297Seric ** Initialize and prescan address. 71297Seric */ 72297Seric 736903Seric CurEnv->e_to = addr; 747675Seric if (tTd(20, 1)) 759888Seric printf("\n--parseaddr(%s)\n", addr); 763188Seric 7716914Seric pvp = prescan(addr, delim, pvpbuf); 783149Seric if (pvp == NULL) 79297Seric return (NULL); 80297Seric 81297Seric /* 823149Seric ** Apply rewriting rules. 837889Seric ** Ruleset 0 does basic parsing. It must resolve. 84297Seric */ 85297Seric 868181Seric rewrite(pvp, 3); 874070Seric rewrite(pvp, 0); 88297Seric 893149Seric /* 903149Seric ** See if we resolved to a real mailer. 913149Seric */ 92297Seric 933149Seric if (pvp[0][0] != CANONNET) 943149Seric { 953149Seric setstat(EX_USAGE); 963149Seric usrerr("cannot resolve name"); 973149Seric return (NULL); 98297Seric } 99297Seric 100297Seric /* 1013149Seric ** Build canonical address from pvp. 102297Seric */ 103297Seric 1043149Seric a = buildaddr(pvp, a); 1054279Seric if (a == NULL) 1064279Seric return (NULL); 107297Seric 108297Seric /* 1093149Seric ** Make local copies of the host & user and then 1103149Seric ** transport them out. 111297Seric */ 112297Seric 113*51317Seric allocaddr(a, copyf, addr); 114*51317Seric 115*51317Seric /* 116*51317Seric ** Compute return value. 117*51317Seric */ 118*51317Seric 119*51317Seric if (tTd(20, 1)) 1208078Seric { 121*51317Seric printf("parseaddr-->"); 122*51317Seric printaddr(a, FALSE); 123*51317Seric } 124*51317Seric 125*51317Seric return (a); 126*51317Seric } 127*51317Seric /* 128*51317Seric ** ALLOCADDR -- do local allocations of address on demand. 129*51317Seric ** 130*51317Seric ** Also lowercases the host name if requested. 131*51317Seric ** 132*51317Seric ** Parameters: 133*51317Seric ** a -- the address to reallocate. 134*51317Seric ** copyf -- the copy flag (see parseaddr for description). 135*51317Seric ** paddr -- the printname of the address. 136*51317Seric ** 137*51317Seric ** Returns: 138*51317Seric ** none. 139*51317Seric ** 140*51317Seric ** Side Effects: 141*51317Seric ** Copies portions of a into local buffers as requested. 142*51317Seric */ 143*51317Seric 144*51317Seric allocaddr(a, copyf, paddr) 145*51317Seric register ADDRESS *a; 146*51317Seric int copyf; 147*51317Seric char *paddr; 148*51317Seric { 149*51317Seric register MAILER *m = a->q_mailer; 150*51317Seric 151*51317Seric if (copyf > 0 && paddr != NULL) 152*51317Seric { 1538078Seric extern char *DelimChar; 1548078Seric char savec = *DelimChar; 1558078Seric 1568078Seric *DelimChar = '\0'; 157*51317Seric a->q_paddr = newstr(paddr); 1588078Seric *DelimChar = savec; 1598078Seric } 160297Seric else 161*51317Seric a->q_paddr = paddr; 16224944Seric 16324944Seric if (a->q_user == NULL) 16424944Seric a->q_user = ""; 16524944Seric if (a->q_host == NULL) 16624944Seric a->q_host = ""; 16724944Seric 1683149Seric if (copyf >= 0) 169297Seric { 17024944Seric a->q_host = newstr(a->q_host); 1713149Seric if (a->q_user != a->q_paddr) 1723149Seric a->q_user = newstr(a->q_user); 173297Seric } 174297Seric 175*51317Seric if (a->q_paddr == NULL) 176*51317Seric a->q_paddr = a->q_user; 177*51317Seric 178297Seric /* 17916202Seric ** Convert host name to lower case if requested. 18016202Seric ** User name will be done later. 18116202Seric */ 18216202Seric 18316202Seric if (!bitnset(M_HST_UPPER, m->m_flags)) 18416202Seric makelower(a->q_host); 185297Seric } 186297Seric /* 18716162Seric ** LOWERADDR -- map UPPER->lower case on addresses as requested. 18816162Seric ** 18916162Seric ** Parameters: 19016162Seric ** a -- address to be mapped. 19116162Seric ** 19216162Seric ** Returns: 19316162Seric ** none. 19416162Seric ** 19516162Seric ** Side Effects: 19616162Seric ** none. 19716162Seric */ 19816162Seric 19916162Seric loweraddr(a) 20016162Seric register ADDRESS *a; 20116162Seric { 20216162Seric register MAILER *m = a->q_mailer; 20316162Seric 20416162Seric if (!bitnset(M_USR_UPPER, m->m_flags)) 20516162Seric makelower(a->q_user); 20616162Seric } 20716162Seric /* 208297Seric ** PRESCAN -- Prescan name and make it canonical 209297Seric ** 2109374Seric ** Scans a name and turns it into a set of tokens. This process 2119374Seric ** deletes blanks and comments (in parentheses). 212297Seric ** 213297Seric ** This routine knows about quoted strings and angle brackets. 214297Seric ** 215297Seric ** There are certain subtleties to this routine. The one that 216297Seric ** comes to mind now is that backslashes on the ends of names 217297Seric ** are silently stripped off; this is intentional. The problem 218297Seric ** is that some versions of sndmsg (like at LBL) set the kill 219297Seric ** character to something other than @ when reading addresses; 220297Seric ** so people type "csvax.eric\@berkeley" -- which screws up the 221297Seric ** berknet mailer. 222297Seric ** 223297Seric ** Parameters: 224297Seric ** addr -- the name to chomp. 225297Seric ** delim -- the delimiter for the address, normally 226297Seric ** '\0' or ','; \0 is accepted in any case. 22715284Seric ** If '\t' then we are reading the .cf file. 22816914Seric ** pvpbuf -- place to put the saved text -- note that 22916914Seric ** the pointers are static. 230297Seric ** 231297Seric ** Returns: 2323149Seric ** A pointer to a vector of tokens. 233297Seric ** NULL on error. 234297Seric ** 235297Seric ** Side Effects: 23625279Seric ** sets DelimChar to point to the character matching 'delim'. 237297Seric */ 238297Seric 2398078Seric /* states and character types */ 2408078Seric # define OPR 0 /* operator */ 2418078Seric # define ATM 1 /* atom */ 2428078Seric # define QST 2 /* in quoted string */ 2438078Seric # define SPC 3 /* chewing up spaces */ 2448078Seric # define ONE 4 /* pick up one character */ 2453149Seric 2468078Seric # define NSTATES 5 /* number of states */ 2478078Seric # define TYPE 017 /* mask to select state type */ 2488078Seric 2498078Seric /* meta bits for table */ 2508078Seric # define M 020 /* meta character; don't pass through */ 2518078Seric # define B 040 /* cause a break */ 2528078Seric # define MB M|B /* meta-break */ 2538078Seric 2548078Seric static short StateTab[NSTATES][NSTATES] = 2558078Seric { 2568087Seric /* oldst chtype> OPR ATM QST SPC ONE */ 2579051Seric /*OPR*/ OPR|B, ATM|B, QST|B, SPC|MB, ONE|B, 2589051Seric /*ATM*/ OPR|B, ATM, QST|B, SPC|MB, ONE|B, 2599051Seric /*QST*/ QST, QST, OPR, QST, QST, 2608078Seric /*SPC*/ OPR, ATM, QST, SPC|M, ONE, 2618078Seric /*ONE*/ OPR, OPR, OPR, OPR, OPR, 2628078Seric }; 2638078Seric 2648078Seric # define NOCHAR -1 /* signal nothing in lookahead token */ 2658078Seric 2668078Seric char *DelimChar; /* set to point to the delimiter */ 2678078Seric 2683149Seric char ** 26916914Seric prescan(addr, delim, pvpbuf) 270297Seric char *addr; 271297Seric char delim; 27216914Seric char pvpbuf[]; 273297Seric { 274297Seric register char *p; 2758078Seric register char *q; 2769346Seric register int c; 2773149Seric char **avp; 278297Seric bool bslashmode; 279297Seric int cmntcnt; 2808423Seric int anglecnt; 2813149Seric char *tok; 2828078Seric int state; 2838078Seric int newstate; 2848078Seric static char *av[MAXATOM+1]; 28515253Seric extern int errno; 286297Seric 28715253Seric /* make sure error messages don't have garbage on them */ 28815253Seric errno = 0; 28915253Seric 29016914Seric q = pvpbuf; 2913149Seric bslashmode = FALSE; 2927800Seric cmntcnt = 0; 2938423Seric anglecnt = 0; 2943149Seric avp = av; 2958078Seric state = OPR; 2968078Seric c = NOCHAR; 2978078Seric p = addr; 2988078Seric if (tTd(22, 45)) 299297Seric { 3008078Seric printf("prescan: "); 3018078Seric xputs(p); 30223109Seric (void) putchar('\n'); 3038078Seric } 3048078Seric 3058078Seric do 3068078Seric { 3073149Seric /* read a token */ 3083149Seric tok = q; 3098078Seric for (;;) 310297Seric { 3118078Seric /* store away any old lookahead character */ 3128078Seric if (c != NOCHAR) 3138078Seric { 31415284Seric /* see if there is room */ 31516914Seric if (q >= &pvpbuf[PSBUFSIZE - 5]) 3168078Seric { 3178078Seric usrerr("Address too long"); 3188078Seric DelimChar = p; 3198078Seric return (NULL); 3208078Seric } 32115284Seric 32215284Seric /* squirrel it away */ 3238078Seric *q++ = c; 3248078Seric } 3258078Seric 3268078Seric /* read a new input character */ 3278078Seric c = *p++; 3288078Seric if (c == '\0') 3298078Seric break; 33015284Seric c &= ~0200; 33115284Seric 3328078Seric if (tTd(22, 101)) 3338078Seric printf("c=%c, s=%d; ", c, state); 3348078Seric 3353149Seric /* chew up special characters */ 3363149Seric *q = '\0'; 3373149Seric if (bslashmode) 3383149Seric { 33924944Seric /* kludge \! for naive users */ 34024944Seric if (c != '!') 34124944Seric c |= 0200; 3423149Seric bslashmode = FALSE; 3433149Seric } 3443149Seric else if (c == '\\') 3453149Seric { 3463149Seric bslashmode = TRUE; 3478078Seric c = NOCHAR; 3483149Seric } 3498514Seric else if (state == QST) 3508514Seric { 3518514Seric /* do nothing, just avoid next clauses */ 3528514Seric } 3538078Seric else if (c == '(') 3544100Seric { 3558078Seric cmntcnt++; 3568078Seric c = NOCHAR; 3574100Seric } 3588078Seric else if (c == ')') 3593149Seric { 3608078Seric if (cmntcnt <= 0) 3613149Seric { 3628078Seric usrerr("Unbalanced ')'"); 3638078Seric DelimChar = p; 3648078Seric return (NULL); 3653149Seric } 3668078Seric else 3678078Seric cmntcnt--; 3688078Seric } 3698078Seric else if (cmntcnt > 0) 3708078Seric c = NOCHAR; 3718423Seric else if (c == '<') 3728423Seric anglecnt++; 3738423Seric else if (c == '>') 3748423Seric { 3758423Seric if (anglecnt <= 0) 3768423Seric { 3778423Seric usrerr("Unbalanced '>'"); 3788423Seric DelimChar = p; 3798423Seric return (NULL); 3808423Seric } 3818423Seric anglecnt--; 3828423Seric } 38311423Seric else if (delim == ' ' && isspace(c)) 38411423Seric c = ' '; 3853149Seric 3868078Seric if (c == NOCHAR) 3878078Seric continue; 3883149Seric 3898078Seric /* see if this is end of input */ 39011405Seric if (c == delim && anglecnt <= 0 && state != QST) 3913149Seric break; 3923149Seric 3938078Seric newstate = StateTab[state][toktype(c)]; 3948078Seric if (tTd(22, 101)) 3958078Seric printf("ns=%02o\n", newstate); 3968078Seric state = newstate & TYPE; 3978078Seric if (bitset(M, newstate)) 3988078Seric c = NOCHAR; 3998078Seric if (bitset(B, newstate)) 4004228Seric break; 401297Seric } 4023149Seric 4033149Seric /* new token */ 4048078Seric if (tok != q) 4051378Seric { 4068078Seric *q++ = '\0'; 4078078Seric if (tTd(22, 36)) 408297Seric { 4098078Seric printf("tok="); 4108078Seric xputs(tok); 41123109Seric (void) putchar('\n'); 412297Seric } 4138078Seric if (avp >= &av[MAXATOM]) 414297Seric { 4158078Seric syserr("prescan: too many tokens"); 4168078Seric DelimChar = p; 4178078Seric return (NULL); 418297Seric } 4198078Seric *avp++ = tok; 420297Seric } 4218423Seric } while (c != '\0' && (c != delim || anglecnt > 0)); 4223149Seric *avp = NULL; 4238078Seric DelimChar = --p; 4243149Seric if (cmntcnt > 0) 4253149Seric usrerr("Unbalanced '('"); 4268423Seric else if (anglecnt > 0) 4278423Seric usrerr("Unbalanced '<'"); 4288078Seric else if (state == QST) 4293149Seric usrerr("Unbalanced '\"'"); 4303149Seric else if (av[0] != NULL) 4313149Seric return (av); 4323149Seric return (NULL); 4333149Seric } 4343149Seric /* 4353149Seric ** TOKTYPE -- return token type 4363149Seric ** 4373149Seric ** Parameters: 4383149Seric ** c -- the character in question. 4393149Seric ** 4403149Seric ** Returns: 4413149Seric ** Its type. 4423149Seric ** 4433149Seric ** Side Effects: 4443149Seric ** none. 4453149Seric */ 446297Seric 4473149Seric toktype(c) 4483149Seric register char c; 4493149Seric { 4503380Seric static char buf[50]; 4513382Seric static bool firstime = TRUE; 4523380Seric 4533382Seric if (firstime) 4543380Seric { 4553382Seric firstime = FALSE; 45616155Seric expand("\001o", buf, &buf[sizeof buf - 1], CurEnv); 4577005Seric (void) strcat(buf, DELIMCHARS); 4583380Seric } 4599585Seric if (c == MATCHCLASS || c == MATCHREPL || c == MATCHNCLASS) 4608078Seric return (ONE); 4618078Seric if (c == '"') 4628078Seric return (QST); 4634100Seric if (!isascii(c)) 4648078Seric return (ATM); 4658078Seric if (isspace(c) || c == ')') 4668078Seric return (SPC); 4673380Seric if (iscntrl(c) || index(buf, c) != NULL) 4688078Seric return (OPR); 4698078Seric return (ATM); 4703149Seric } 4713149Seric /* 4723149Seric ** REWRITE -- apply rewrite rules to token vector. 4733149Seric ** 4744476Seric ** This routine is an ordered production system. Each rewrite 4754476Seric ** rule has a LHS (called the pattern) and a RHS (called the 4764476Seric ** rewrite); 'rwr' points the the current rewrite rule. 4774476Seric ** 4784476Seric ** For each rewrite rule, 'avp' points the address vector we 4794476Seric ** are trying to match against, and 'pvp' points to the pattern. 4808058Seric ** If pvp points to a special match value (MATCHZANY, MATCHANY, 4819585Seric ** MATCHONE, MATCHCLASS, MATCHNCLASS) then the address in avp 4829585Seric ** matched is saved away in the match vector (pointed to by 'mvp'). 4834476Seric ** 4844476Seric ** When a match between avp & pvp does not match, we try to 4859585Seric ** back out. If we back up over MATCHONE, MATCHCLASS, or MATCHNCLASS 4864476Seric ** we must also back out the match in mvp. If we reach a 4878058Seric ** MATCHANY or MATCHZANY we just extend the match and start 4888058Seric ** over again. 4894476Seric ** 4904476Seric ** When we finally match, we rewrite the address vector 4914476Seric ** and try over again. 4924476Seric ** 4933149Seric ** Parameters: 4943149Seric ** pvp -- pointer to token vector. 4953149Seric ** 4963149Seric ** Returns: 4973149Seric ** none. 4983149Seric ** 4993149Seric ** Side Effects: 5003149Seric ** pvp is modified. 5013149Seric */ 5022091Seric 5033149Seric struct match 5043149Seric { 5054468Seric char **first; /* first token matched */ 5064468Seric char **last; /* last token matched */ 5073149Seric }; 5083149Seric 5094468Seric # define MAXMATCH 9 /* max params per rewrite */ 5103149Seric 5113149Seric 5124070Seric rewrite(pvp, ruleset) 5133149Seric char **pvp; 5144070Seric int ruleset; 5153149Seric { 5163149Seric register char *ap; /* address pointer */ 5173149Seric register char *rp; /* rewrite pointer */ 5183149Seric register char **avp; /* address vector pointer */ 5193149Seric register char **rvp; /* rewrite vector pointer */ 5208058Seric register struct match *mlp; /* cur ptr into mlist */ 5218058Seric register struct rewrite *rwr; /* pointer to current rewrite rule */ 5224468Seric struct match mlist[MAXMATCH]; /* stores match on LHS */ 5233149Seric char *npvp[MAXATOM+1]; /* temporary space for rebuild */ 5243149Seric 5259279Seric if (OpMode == MD_TEST || tTd(21, 2)) 5263149Seric { 5278959Seric printf("rewrite: ruleset %2d input:", ruleset); 5283149Seric printav(pvp); 5293149Seric } 5308423Seric if (pvp == NULL) 5318423Seric return; 5323149Seric 5333149Seric /* 5343149Seric ** Run through the list of rewrite rules, applying 5353149Seric ** any that match. 5363149Seric */ 5373149Seric 5384070Seric for (rwr = RewriteRules[ruleset]; rwr != NULL; ) 5393149Seric { 5407675Seric if (tTd(21, 12)) 541297Seric { 5428069Seric printf("-----trying rule:"); 5433149Seric printav(rwr->r_lhs); 5443149Seric } 5453149Seric 5463149Seric /* try to match on this rule */ 5474468Seric mlp = mlist; 5488058Seric rvp = rwr->r_lhs; 5498058Seric avp = pvp; 5508058Seric while ((ap = *avp) != NULL || *rvp != NULL) 5513149Seric { 5523149Seric rp = *rvp; 5538058Seric if (tTd(21, 35)) 5548058Seric { 5558069Seric printf("ap="); 5568058Seric xputs(ap); 5578069Seric printf(", rp="); 5588058Seric xputs(rp); 5598069Seric printf("\n"); 5608058Seric } 5613149Seric if (rp == NULL) 562297Seric { 5633149Seric /* end-of-pattern before end-of-address */ 5648058Seric goto backup; 565297Seric } 5668058Seric if (ap == NULL && *rp != MATCHZANY) 5678058Seric { 5688058Seric /* end-of-input */ 5698058Seric break; 5708058Seric } 5713149Seric 5723149Seric switch (*rp) 5733149Seric { 5744060Seric register STAB *s; 5754060Seric 5764060Seric case MATCHCLASS: 5779585Seric case MATCHNCLASS: 5789585Seric /* match any token in (not in) a class */ 5794100Seric s = stab(ap, ST_CLASS, ST_FIND); 58010690Seric if (s == NULL || !bitnset(rp[1], s->s_class)) 5819585Seric { 5829585Seric if (*rp == MATCHCLASS) 5839585Seric goto backup; 5849585Seric } 5859585Seric else if (*rp == MATCHNCLASS) 5868058Seric goto backup; 5874468Seric 5884476Seric /* explicit fall-through */ 5894476Seric 5904476Seric case MATCHONE: 5914476Seric case MATCHANY: 5924476Seric /* match exactly one token */ 5938058Seric mlp->first = avp; 5948058Seric mlp->last = avp++; 5954468Seric mlp++; 5964060Seric break; 5974060Seric 5988058Seric case MATCHZANY: 5998058Seric /* match zero or more tokens */ 6008058Seric mlp->first = avp; 6018058Seric mlp->last = avp - 1; 6028058Seric mlp++; 6038058Seric break; 6048058Seric 6053149Seric default: 6063149Seric /* must have exact match */ 60733725Sbostic if (strcasecmp(rp, ap)) 6088058Seric goto backup; 6094468Seric avp++; 6103149Seric break; 6113149Seric } 6123149Seric 6133149Seric /* successful match on this token */ 6143149Seric rvp++; 6153149Seric continue; 6163149Seric 6178058Seric backup: 6183149Seric /* match failed -- back up */ 6193149Seric while (--rvp >= rwr->r_lhs) 6203149Seric { 6213149Seric rp = *rvp; 6228058Seric if (*rp == MATCHANY || *rp == MATCHZANY) 6234468Seric { 6244476Seric /* extend binding and continue */ 6258058Seric avp = ++mlp[-1].last; 6268058Seric avp++; 6274476Seric rvp++; 6283149Seric break; 6294468Seric } 6304476Seric avp--; 6319585Seric if (*rp == MATCHONE || *rp == MATCHCLASS || 6329585Seric *rp == MATCHNCLASS) 6333149Seric { 6344468Seric /* back out binding */ 6354468Seric mlp--; 6363149Seric } 6373149Seric } 6383149Seric 6393149Seric if (rvp < rwr->r_lhs) 6403149Seric { 6413149Seric /* total failure to match */ 6423149Seric break; 6433149Seric } 644297Seric } 6453149Seric 6463149Seric /* 6473149Seric ** See if we successfully matched 6483149Seric */ 6493149Seric 6509374Seric if (rvp < rwr->r_lhs || *rvp != NULL) 6513149Seric { 6529374Seric if (tTd(21, 10)) 6539374Seric printf("----- rule fails\n"); 6549374Seric rwr = rwr->r_next; 6559374Seric continue; 6569374Seric } 6573149Seric 6589374Seric rvp = rwr->r_rhs; 6599374Seric if (tTd(21, 12)) 6609374Seric { 6619374Seric printf("-----rule matches:"); 6629374Seric printav(rvp); 6639374Seric } 6649374Seric 6659374Seric rp = *rvp; 6669374Seric if (*rp == CANONUSER) 6679374Seric { 6689374Seric rvp++; 6699374Seric rwr = rwr->r_next; 6709374Seric } 6719374Seric else if (*rp == CANONHOST) 6729374Seric { 6739374Seric rvp++; 6749374Seric rwr = NULL; 6759374Seric } 6769374Seric else if (*rp == CANONNET) 6779374Seric rwr = NULL; 6789374Seric 6799374Seric /* substitute */ 6809374Seric for (avp = npvp; *rvp != NULL; rvp++) 6819374Seric { 6829374Seric register struct match *m; 6839374Seric register char **pp; 6849374Seric 6858058Seric rp = *rvp; 68616914Seric if (*rp == MATCHREPL) 6878058Seric { 68816914Seric /* substitute from LHS */ 68916914Seric m = &mlist[rp[1] - '1']; 69016914Seric if (m >= mlp) 6919374Seric { 69216914Seric syserr("rewrite: ruleset %d: replacement out of bounds", ruleset); 6939374Seric return; 6949374Seric } 69516914Seric if (tTd(21, 15)) 69616914Seric { 69716914Seric printf("$%c:", rp[1]); 69816914Seric pp = m->first; 69916914Seric while (pp <= m->last) 70016914Seric { 70116914Seric printf(" %x=\"", *pp); 70216914Seric (void) fflush(stdout); 70316914Seric printf("%s\"", *pp++); 70416914Seric } 70516914Seric printf("\n"); 70616914Seric } 7079374Seric pp = m->first; 7089374Seric while (pp <= m->last) 7093149Seric { 71016914Seric if (avp >= &npvp[MAXATOM]) 71116914Seric { 71216914Seric syserr("rewrite: expansion too long"); 71316914Seric return; 71416914Seric } 71516914Seric *avp++ = *pp++; 7163149Seric } 7173149Seric } 71816914Seric else 7198226Seric { 72016914Seric /* vanilla replacement */ 7219374Seric if (avp >= &npvp[MAXATOM]) 72216889Seric { 72316914Seric toolong: 72416889Seric syserr("rewrite: expansion too long"); 72516889Seric return; 72616889Seric } 72716914Seric *avp++ = rp; 7288226Seric } 7299374Seric } 7309374Seric *avp++ = NULL; 73116914Seric 73216914Seric /* 73316914Seric ** Check for any hostname lookups. 73416914Seric */ 73516914Seric 73616914Seric for (rvp = npvp; *rvp != NULL; rvp++) 73716914Seric { 73816914Seric char **hbrvp; 73916914Seric char **xpvp; 74016914Seric int trsize; 74117473Seric char *olddelimchar; 74216920Seric char buf[MAXNAME + 1]; 74316914Seric char *pvpb1[MAXATOM + 1]; 74417174Seric char pvpbuf[PSBUFSIZE]; 74517473Seric extern char *DelimChar; 74616914Seric 74716914Seric if (**rvp != HOSTBEGIN) 74816914Seric continue; 74916914Seric 75016914Seric /* 75116914Seric ** Got a hostname lookup. 75216914Seric ** 75316914Seric ** This could be optimized fairly easily. 75416914Seric */ 75516914Seric 75616914Seric hbrvp = rvp; 75716914Seric 75816914Seric /* extract the match part */ 75916914Seric while (*++rvp != NULL && **rvp != HOSTEND) 76016914Seric continue; 76116914Seric if (*rvp != NULL) 76216914Seric *rvp++ = NULL; 76316914Seric 76416914Seric /* save the remainder of the input string */ 76516914Seric trsize = (int) (avp - rvp + 1) * sizeof *rvp; 76616914Seric bcopy((char *) rvp, (char *) pvpb1, trsize); 76716914Seric 76816914Seric /* look it up */ 76916914Seric cataddr(++hbrvp, buf, sizeof buf); 770*51317Seric if (maphostname(buf, sizeof buf - 1) && ConfigLevel >= 2) 771*51317Seric { 772*51317Seric register int i; 77316914Seric 774*51317Seric /* it mapped -- mark it with a trailing dot */ 775*51317Seric i = strlen(buf); 776*51317Seric if (i > 0 && buf[i - 1] != '.') 777*51317Seric { 778*51317Seric buf[i++] = '.'; 779*51317Seric buf[i] = '\0'; 780*51317Seric } 781*51317Seric } 782*51317Seric 78316914Seric /* scan the new host name */ 78417473Seric olddelimchar = DelimChar; 78516914Seric xpvp = prescan(buf, '\0', pvpbuf); 78617473Seric DelimChar = olddelimchar; 78716914Seric if (xpvp == NULL) 78816914Seric { 78916914Seric syserr("rewrite: cannot prescan canonical hostname: %s", buf); 79022976Smiriam return; 79116914Seric } 79216914Seric 79316914Seric /* append it to the token list */ 79417174Seric for (avp = --hbrvp; *xpvp != NULL; xpvp++) 79517174Seric { 79617174Seric *avp++ = newstr(*xpvp); 79716920Seric if (avp >= &npvp[MAXATOM]) 79816914Seric goto toolong; 79917174Seric } 80016914Seric 80116914Seric /* restore the old trailing information */ 80217177Seric for (xpvp = pvpb1; (*avp++ = *xpvp++) != NULL; ) 80316920Seric if (avp >= &npvp[MAXATOM]) 80416914Seric goto toolong; 80517174Seric 80617174Seric break; 80716914Seric } 80816914Seric 80916914Seric /* 81016914Seric ** Check for subroutine calls. 81116914Seric */ 81216914Seric 81324944Seric if (*npvp != NULL && **npvp == CALLSUBR) 8149374Seric { 81516889Seric bcopy((char *) &npvp[2], (char *) pvp, 81616900Seric (int) (avp - npvp - 2) * sizeof *avp); 81716889Seric if (tTd(21, 3)) 81816889Seric printf("-----callsubr %s\n", npvp[1]); 81916889Seric rewrite(pvp, atoi(npvp[1])); 8203149Seric } 8213149Seric else 8223149Seric { 82317348Seric bcopy((char *) npvp, (char *) pvp, 82416900Seric (int) (avp - npvp) * sizeof *avp); 8259374Seric } 8269374Seric if (tTd(21, 4)) 8279374Seric { 8289374Seric printf("rewritten as:"); 8299374Seric printav(pvp); 8309374Seric } 831297Seric } 8328069Seric 8339279Seric if (OpMode == MD_TEST || tTd(21, 2)) 8348069Seric { 8358959Seric printf("rewrite: ruleset %2d returns:", ruleset); 8368069Seric printav(pvp); 8378069Seric } 8383149Seric } 8393149Seric /* 8403149Seric ** BUILDADDR -- build address from token vector. 8413149Seric ** 8423149Seric ** Parameters: 8433149Seric ** tv -- token vector. 8443149Seric ** a -- pointer to address descriptor to fill. 8453149Seric ** If NULL, one will be allocated. 8463149Seric ** 8473149Seric ** Returns: 8484279Seric ** NULL if there was an error. 8494279Seric ** 'a' otherwise. 8503149Seric ** 8513149Seric ** Side Effects: 8523149Seric ** fills in 'a' 8533149Seric */ 8543149Seric 8553149Seric ADDRESS * 8563149Seric buildaddr(tv, a) 8573149Seric register char **tv; 8583149Seric register ADDRESS *a; 8593149Seric { 8603149Seric static char buf[MAXNAME]; 8613149Seric struct mailer **mp; 8623149Seric register struct mailer *m; 8633149Seric 8643149Seric if (a == NULL) 8653149Seric a = (ADDRESS *) xalloc(sizeof *a); 86616889Seric bzero((char *) a, sizeof *a); 8673149Seric 8683149Seric /* figure out what net/mailer to use */ 8693149Seric if (**tv != CANONNET) 8704279Seric { 8713149Seric syserr("buildaddr: no net"); 8724279Seric return (NULL); 8734279Seric } 8743149Seric tv++; 87533725Sbostic if (!strcasecmp(*tv, "error")) 8764279Seric { 87710183Seric if (**++tv == CANONHOST) 87810183Seric { 87910183Seric setstat(atoi(*++tv)); 88010183Seric tv++; 88110183Seric } 88210183Seric if (**tv != CANONUSER) 8834279Seric syserr("buildaddr: error: no user"); 8844279Seric buf[0] = '\0'; 8854279Seric while (*++tv != NULL) 8864279Seric { 8874279Seric if (buf[0] != '\0') 8887005Seric (void) strcat(buf, " "); 8897005Seric (void) strcat(buf, *tv); 8904279Seric } 8914279Seric usrerr(buf); 8924279Seric return (NULL); 8934279Seric } 8944598Seric for (mp = Mailer; (m = *mp++) != NULL; ) 8953149Seric { 89633725Sbostic if (!strcasecmp(m->m_name, *tv)) 8973149Seric break; 8983149Seric } 8993149Seric if (m == NULL) 9004279Seric { 90124944Seric syserr("buildaddr: unknown mailer %s", *tv); 9024279Seric return (NULL); 9034279Seric } 9044598Seric a->q_mailer = m; 9053149Seric 9063149Seric /* figure out what host (if any) */ 9073149Seric tv++; 90810690Seric if (!bitnset(M_LOCAL, m->m_flags)) 9093149Seric { 9105704Seric if (**tv++ != CANONHOST) 9114279Seric { 9123149Seric syserr("buildaddr: no host"); 9134279Seric return (NULL); 9144279Seric } 9155704Seric buf[0] = '\0'; 9165704Seric while (*tv != NULL && **tv != CANONUSER) 9177005Seric (void) strcat(buf, *tv++); 9185704Seric a->q_host = newstr(buf); 9193149Seric } 9203149Seric else 9213149Seric a->q_host = NULL; 9223149Seric 9233149Seric /* figure out the user */ 92436615Sbostic if (*tv == NULL || **tv != CANONUSER) 9254279Seric { 9263149Seric syserr("buildaddr: no user"); 9274279Seric return (NULL); 9284279Seric } 92919040Seric 930*51317Seric if (m == LocalMailer && tv[1] != NULL && strcmp(tv[1], ":") == 0) 931*51317Seric { 932*51317Seric tv++; 933*51317Seric a->q_flags |= QNOTREMOTE; 934*51317Seric } 935*51317Seric 93619040Seric /* rewrite according recipient mailer rewriting rules */ 93719040Seric rewrite(++tv, 2); 93819040Seric if (m->m_r_rwset > 0) 93919040Seric rewrite(tv, m->m_r_rwset); 94019040Seric rewrite(tv, 4); 94119040Seric 94219040Seric /* save the result for the command line/RCPT argument */ 94311278Seric cataddr(tv, buf, sizeof buf); 9443149Seric a->q_user = buf; 9453149Seric 9463149Seric return (a); 9473149Seric } 9483188Seric /* 9494228Seric ** CATADDR -- concatenate pieces of addresses (putting in <LWSP> subs) 9504228Seric ** 9514228Seric ** Parameters: 9524228Seric ** pvp -- parameter vector to rebuild. 9534228Seric ** buf -- buffer to build the string into. 9544228Seric ** sz -- size of buf. 9554228Seric ** 9564228Seric ** Returns: 9574228Seric ** none. 9584228Seric ** 9594228Seric ** Side Effects: 9604228Seric ** Destroys buf. 9614228Seric */ 9624228Seric 9634228Seric cataddr(pvp, buf, sz) 9644228Seric char **pvp; 9654228Seric char *buf; 9664228Seric register int sz; 9674228Seric { 9684228Seric bool oatomtok = FALSE; 9694228Seric bool natomtok = FALSE; 9704228Seric register int i; 9714228Seric register char *p; 9724228Seric 9738423Seric if (pvp == NULL) 9748423Seric { 97523109Seric (void) strcpy(buf, ""); 9768423Seric return; 9778423Seric } 9784228Seric p = buf; 97911156Seric sz -= 2; 9804228Seric while (*pvp != NULL && (i = strlen(*pvp)) < sz) 9814228Seric { 9828078Seric natomtok = (toktype(**pvp) == ATM); 9834228Seric if (oatomtok && natomtok) 9849042Seric *p++ = SpaceSub; 9854228Seric (void) strcpy(p, *pvp); 9864228Seric oatomtok = natomtok; 9874228Seric p += i; 98811156Seric sz -= i + 1; 9894228Seric pvp++; 9904228Seric } 9914228Seric *p = '\0'; 9924228Seric } 9934228Seric /* 9943188Seric ** SAMEADDR -- Determine if two addresses are the same 9953188Seric ** 9963188Seric ** This is not just a straight comparison -- if the mailer doesn't 9973188Seric ** care about the host we just ignore it, etc. 9983188Seric ** 9993188Seric ** Parameters: 10003188Seric ** a, b -- pointers to the internal forms to compare. 10013188Seric ** 10023188Seric ** Returns: 10033188Seric ** TRUE -- they represent the same mailbox. 10043188Seric ** FALSE -- they don't. 10053188Seric ** 10063188Seric ** Side Effects: 10073188Seric ** none. 10083188Seric */ 10093188Seric 10103188Seric bool 10119374Seric sameaddr(a, b) 10123188Seric register ADDRESS *a; 10133188Seric register ADDRESS *b; 10143188Seric { 10153188Seric /* if they don't have the same mailer, forget it */ 10163188Seric if (a->q_mailer != b->q_mailer) 10173188Seric return (FALSE); 10183188Seric 10193188Seric /* if the user isn't the same, we can drop out */ 10209374Seric if (strcmp(a->q_user, b->q_user) != 0) 10213188Seric return (FALSE); 10223188Seric 10233188Seric /* if the mailer ignores hosts, we have succeeded! */ 102410690Seric if (bitnset(M_LOCAL, a->q_mailer->m_flags)) 10253188Seric return (TRUE); 10263188Seric 10273188Seric /* otherwise compare hosts (but be careful for NULL ptrs) */ 10283188Seric if (a->q_host == NULL || b->q_host == NULL) 10293188Seric return (FALSE); 10303188Seric if (strcmp(a->q_host, b->q_host) != 0) 10313188Seric return (FALSE); 10323188Seric 10333188Seric return (TRUE); 10343188Seric } 10353234Seric /* 10363234Seric ** PRINTADDR -- print address (for debugging) 10373234Seric ** 10383234Seric ** Parameters: 10393234Seric ** a -- the address to print 10403234Seric ** follow -- follow the q_next chain. 10413234Seric ** 10423234Seric ** Returns: 10433234Seric ** none. 10443234Seric ** 10453234Seric ** Side Effects: 10463234Seric ** none. 10473234Seric */ 10483234Seric 10493234Seric printaddr(a, follow) 10503234Seric register ADDRESS *a; 10513234Seric bool follow; 10523234Seric { 10535001Seric bool first = TRUE; 10545001Seric 10553234Seric while (a != NULL) 10563234Seric { 10575001Seric first = FALSE; 10584443Seric printf("%x=", a); 10594085Seric (void) fflush(stdout); 106040973Sbostic printf("%s: mailer %d (%s), host `%s', user `%s', ruser `%s'\n", 106140973Sbostic a->q_paddr, a->q_mailer->m_mno, a->q_mailer->m_name, 106240973Sbostic a->q_host, a->q_user, a->q_ruser? a->q_ruser: "<null>"); 10638181Seric printf("\tnext=%x, flags=%o, alias %x\n", a->q_next, a->q_flags, 10648181Seric a->q_alias); 10658181Seric printf("\thome=\"%s\", fullname=\"%s\"\n", a->q_home, 10668181Seric a->q_fullname); 10674996Seric 10683234Seric if (!follow) 10693234Seric return; 10704996Seric a = a->q_next; 10713234Seric } 10725001Seric if (first) 10734443Seric printf("[NULL]\n"); 10743234Seric } 10754317Seric 10767682Seric /* 10777682Seric ** REMOTENAME -- return the name relative to the current mailer 10787682Seric ** 10797682Seric ** Parameters: 10807682Seric ** name -- the name to translate. 10818069Seric ** m -- the mailer that we want to do rewriting relative 10828069Seric ** to. 10838069Seric ** senderaddress -- if set, uses the sender rewriting rules 10848069Seric ** rather than the recipient rewriting rules. 108510310Seric ** canonical -- if set, strip out any comment information, 108610310Seric ** etc. 10877682Seric ** 10887682Seric ** Returns: 10897682Seric ** the text string representing this address relative to 10907682Seric ** the receiving mailer. 10917682Seric ** 10927682Seric ** Side Effects: 10937682Seric ** none. 10947682Seric ** 10957682Seric ** Warnings: 10967682Seric ** The text string returned is tucked away locally; 10977682Seric ** copy it if you intend to save it. 10987682Seric */ 10997682Seric 11007682Seric char * 110110310Seric remotename(name, m, senderaddress, canonical) 11027682Seric char *name; 11037682Seric struct mailer *m; 11048069Seric bool senderaddress; 110510310Seric bool canonical; 11067682Seric { 11078069Seric register char **pvp; 11088069Seric char *fancy; 11098069Seric extern char *macvalue(); 11108181Seric char *oldg = macvalue('g', CurEnv); 11117682Seric static char buf[MAXNAME]; 11127682Seric char lbuf[MAXNAME]; 111316914Seric char pvpbuf[PSBUFSIZE]; 11147682Seric extern char **prescan(); 11157889Seric extern char *crackaddr(); 11167682Seric 11177755Seric if (tTd(12, 1)) 11187755Seric printf("remotename(%s)\n", name); 11197755Seric 112010177Seric /* don't do anything if we are tagging it as special */ 112110177Seric if ((senderaddress ? m->m_s_rwset : m->m_r_rwset) < 0) 112210177Seric return (name); 112310177Seric 11247682Seric /* 11258181Seric ** Do a heuristic crack of this name to extract any comment info. 11268181Seric ** This will leave the name as a comment and a $g macro. 11277889Seric */ 11287889Seric 112910310Seric if (canonical) 113016155Seric fancy = "\001g"; 113110310Seric else 113210310Seric fancy = crackaddr(name); 11337889Seric 11348181Seric /* 11358181Seric ** Turn the name into canonical form. 11368181Seric ** Normally this will be RFC 822 style, i.e., "user@domain". 11378181Seric ** If this only resolves to "user", and the "C" flag is 11388181Seric ** specified in the sending mailer, then the sender's 11398181Seric ** domain will be appended. 11408181Seric */ 11418181Seric 114216914Seric pvp = prescan(name, '\0', pvpbuf); 11437889Seric if (pvp == NULL) 11447889Seric return (name); 11458181Seric rewrite(pvp, 3); 11468181Seric if (CurEnv->e_fromdomain != NULL) 11478181Seric { 11488181Seric /* append from domain to this address */ 11498181Seric register char **pxp = pvp; 11508181Seric 11519594Seric /* see if there is an "@domain" in the current name */ 11528181Seric while (*pxp != NULL && strcmp(*pxp, "@") != 0) 11538181Seric pxp++; 11548181Seric if (*pxp == NULL) 11558181Seric { 11569594Seric /* no.... append the "@domain" from the sender */ 11578181Seric register char **qxq = CurEnv->e_fromdomain; 11588181Seric 11599594Seric while ((*pxp++ = *qxq++) != NULL) 11609594Seric continue; 116111726Seric rewrite(pvp, 3); 11628181Seric } 11638181Seric } 11648181Seric 11658181Seric /* 11668959Seric ** Do more specific rewriting. 11678181Seric ** Rewrite using ruleset 1 or 2 depending on whether this is 11688181Seric ** a sender address or not. 11698181Seric ** Then run it through any receiving-mailer-specific rulesets. 11708181Seric */ 11718181Seric 11728069Seric if (senderaddress) 11737755Seric { 11747889Seric rewrite(pvp, 1); 11758069Seric if (m->m_s_rwset > 0) 11768069Seric rewrite(pvp, m->m_s_rwset); 11778069Seric } 11788069Seric else 11798069Seric { 11807889Seric rewrite(pvp, 2); 11818069Seric if (m->m_r_rwset > 0) 11828069Seric rewrite(pvp, m->m_r_rwset); 11837682Seric } 11847682Seric 11858181Seric /* 11868959Seric ** Do any final sanitation the address may require. 11878959Seric ** This will normally be used to turn internal forms 11888959Seric ** (e.g., user@host.LOCAL) into external form. This 11898959Seric ** may be used as a default to the above rules. 11908959Seric */ 11918959Seric 11928959Seric rewrite(pvp, 4); 11938959Seric 11948959Seric /* 11958181Seric ** Now restore the comment information we had at the beginning. 11968181Seric */ 11978181Seric 11987682Seric cataddr(pvp, lbuf, sizeof lbuf); 11999374Seric define('g', lbuf, CurEnv); 12007889Seric expand(fancy, buf, &buf[sizeof buf - 1], CurEnv); 12019374Seric define('g', oldg, CurEnv); 12027682Seric 12037682Seric if (tTd(12, 1)) 12047755Seric printf("remotename => `%s'\n", buf); 12057682Seric return (buf); 12067682Seric } 1207*51317Seric /* 1208*51317Seric ** MAPLOCALUSER -- run local username through ruleset 5 for final redirection 1209*51317Seric ** 1210*51317Seric ** Parameters: 1211*51317Seric ** a -- the address to map (but just the user name part). 1212*51317Seric ** sendq -- the sendq in which to install any replacement 1213*51317Seric ** addresses. 1214*51317Seric ** 1215*51317Seric ** Returns: 1216*51317Seric ** none. 1217*51317Seric */ 1218*51317Seric 1219*51317Seric maplocaluser(a, sendq) 1220*51317Seric register ADDRESS *a; 1221*51317Seric ADDRESS **sendq; 1222*51317Seric { 1223*51317Seric register char **pvp; 1224*51317Seric register ADDRESS *a1 = NULL; 1225*51317Seric char pvpbuf[PSBUFSIZE]; 1226*51317Seric 1227*51317Seric if (tTd(29, 1)) 1228*51317Seric { 1229*51317Seric printf("maplocaluser: "); 1230*51317Seric printaddr(a, FALSE); 1231*51317Seric } 1232*51317Seric pvp = prescan(a->q_user, '\0', pvpbuf); 1233*51317Seric if (pvp == NULL) 1234*51317Seric return; 1235*51317Seric 1236*51317Seric rewrite(pvp, 5); 1237*51317Seric if (pvp[0] == NULL || pvp[0][0] != CANONNET) 1238*51317Seric return; 1239*51317Seric 1240*51317Seric /* if non-null, mailer destination specified -- has it changed? */ 1241*51317Seric a1 = buildaddr(pvp, NULL); 1242*51317Seric if (a1 == NULL || sameaddr(a, a1)) 1243*51317Seric return; 1244*51317Seric 1245*51317Seric /* mark old address as dead; insert new address */ 1246*51317Seric a->q_flags |= QDONTSEND; 1247*51317Seric a1->q_alias = a; 1248*51317Seric allocaddr(a1, 1, NULL); 1249*51317Seric (void) recipient(a1, sendq); 1250*51317Seric } 1251