13312Seric # include "sendmail.h" 2297Seric 3*17473Seric SCCSID(@(#)parseaddr.c 4.14 12/05/84); 4407Seric 5297Seric /* 69888Seric ** PARSEADDR -- Parse an address 7297Seric ** 8297Seric ** Parses an address and breaks it up into three parts: a 9297Seric ** net to transmit the message on, the host to transmit it 10297Seric ** to, and a user on that host. These are loaded into an 112973Seric ** ADDRESS header with the values squirreled away if necessary. 12297Seric ** The "user" part may not be a real user; the process may 13297Seric ** just reoccur on that machine. For example, on a machine 14297Seric ** with an arpanet connection, the address 15297Seric ** csvax.bill@berkeley 16297Seric ** will break up to a "user" of 'csvax.bill' and a host 17297Seric ** of 'berkeley' -- to be transmitted over the arpanet. 18297Seric ** 19297Seric ** Parameters: 20297Seric ** addr -- the address to parse. 21297Seric ** a -- a pointer to the address descriptor buffer. 22297Seric ** If NULL, a header will be created. 23297Seric ** copyf -- determines what shall be copied: 24297Seric ** -1 -- don't copy anything. The printname 25297Seric ** (q_paddr) is just addr, and the 26297Seric ** user & host are allocated internally 27297Seric ** to parse. 28297Seric ** 0 -- copy out the parsed user & host, but 29297Seric ** don't copy the printname. 30297Seric ** +1 -- copy everything. 3111445Seric ** delim -- the character to terminate the address, passed 3211445Seric ** to prescan. 33297Seric ** 34297Seric ** Returns: 35297Seric ** A pointer to the address descriptor header (`a' if 36297Seric ** `a' is non-NULL). 37297Seric ** NULL on error. 38297Seric ** 39297Seric ** Side Effects: 40297Seric ** none 41297Seric */ 42297Seric 439374Seric /* following delimiters are inherent to the internal algorithms */ 4416155Seric # define DELIMCHARS "\001()<>,;\\\"\r\n" /* word delimiters */ 452091Seric 462973Seric ADDRESS * 4711445Seric parseaddr(addr, a, copyf, delim) 48297Seric char *addr; 492973Seric register ADDRESS *a; 50297Seric int copyf; 5111445Seric char delim; 52297Seric { 533149Seric register char **pvp; 543149Seric register struct mailer *m; 5516914Seric char pvpbuf[PSBUFSIZE]; 563149Seric extern char **prescan(); 573149Seric extern ADDRESS *buildaddr(); 58297Seric 59297Seric /* 60297Seric ** Initialize and prescan address. 61297Seric */ 62297Seric 636903Seric CurEnv->e_to = addr; 643188Seric # ifdef DEBUG 657675Seric if (tTd(20, 1)) 669888Seric printf("\n--parseaddr(%s)\n", addr); 673188Seric # endif DEBUG 683188Seric 6916914Seric pvp = prescan(addr, delim, pvpbuf); 703149Seric if (pvp == NULL) 71297Seric return (NULL); 72297Seric 73297Seric /* 743149Seric ** Apply rewriting rules. 757889Seric ** Ruleset 0 does basic parsing. It must resolve. 76297Seric */ 77297Seric 788181Seric rewrite(pvp, 3); 794070Seric rewrite(pvp, 0); 80297Seric 813149Seric /* 823149Seric ** See if we resolved to a real mailer. 833149Seric */ 84297Seric 853149Seric if (pvp[0][0] != CANONNET) 863149Seric { 873149Seric setstat(EX_USAGE); 883149Seric usrerr("cannot resolve name"); 893149Seric return (NULL); 90297Seric } 91297Seric 92297Seric /* 933149Seric ** Build canonical address from pvp. 94297Seric */ 95297Seric 963149Seric a = buildaddr(pvp, a); 974279Seric if (a == NULL) 984279Seric return (NULL); 994598Seric m = a->q_mailer; 100297Seric 101297Seric /* 1023149Seric ** Make local copies of the host & user and then 1033149Seric ** transport them out. 104297Seric */ 105297Seric 106297Seric if (copyf > 0) 1078078Seric { 1088078Seric extern char *DelimChar; 1098078Seric char savec = *DelimChar; 1108078Seric 1118078Seric *DelimChar = '\0'; 1122973Seric a->q_paddr = newstr(addr); 1138078Seric *DelimChar = savec; 1148078Seric } 115297Seric else 116297Seric a->q_paddr = addr; 1173149Seric if (copyf >= 0) 118297Seric { 1193149Seric if (a->q_host != NULL) 1203149Seric a->q_host = newstr(a->q_host); 121297Seric else 1223149Seric a->q_host = ""; 1233149Seric if (a->q_user != a->q_paddr) 1243149Seric a->q_user = newstr(a->q_user); 125297Seric } 126297Seric 127297Seric /* 12816202Seric ** Convert host name to lower case if requested. 12916202Seric ** User name will be done later. 13016202Seric */ 13116202Seric 13216202Seric if (!bitnset(M_HST_UPPER, m->m_flags)) 13316202Seric makelower(a->q_host); 13416202Seric 13516202Seric /* 136297Seric ** Compute return value. 137297Seric */ 138297Seric 139297Seric # ifdef DEBUG 1407675Seric if (tTd(20, 1)) 1414443Seric { 1429888Seric printf("parseaddr-->"); 1434443Seric printaddr(a, FALSE); 1444443Seric } 145297Seric # endif DEBUG 146297Seric 147297Seric return (a); 148297Seric } 149297Seric /* 15016162Seric ** LOWERADDR -- map UPPER->lower case on addresses as requested. 15116162Seric ** 15216162Seric ** Parameters: 15316162Seric ** a -- address to be mapped. 15416162Seric ** 15516162Seric ** Returns: 15616162Seric ** none. 15716162Seric ** 15816162Seric ** Side Effects: 15916162Seric ** none. 16016162Seric */ 16116162Seric 16216162Seric loweraddr(a) 16316162Seric register ADDRESS *a; 16416162Seric { 16516162Seric register MAILER *m = a->q_mailer; 16616162Seric 16716162Seric if (!bitnset(M_USR_UPPER, m->m_flags)) 16816162Seric makelower(a->q_user); 16916162Seric } 17016162Seric /* 171297Seric ** PRESCAN -- Prescan name and make it canonical 172297Seric ** 1739374Seric ** Scans a name and turns it into a set of tokens. This process 1749374Seric ** deletes blanks and comments (in parentheses). 175297Seric ** 176297Seric ** This routine knows about quoted strings and angle brackets. 177297Seric ** 178297Seric ** There are certain subtleties to this routine. The one that 179297Seric ** comes to mind now is that backslashes on the ends of names 180297Seric ** are silently stripped off; this is intentional. The problem 181297Seric ** is that some versions of sndmsg (like at LBL) set the kill 182297Seric ** character to something other than @ when reading addresses; 183297Seric ** so people type "csvax.eric\@berkeley" -- which screws up the 184297Seric ** berknet mailer. 185297Seric ** 186297Seric ** Parameters: 187297Seric ** addr -- the name to chomp. 188297Seric ** delim -- the delimiter for the address, normally 189297Seric ** '\0' or ','; \0 is accepted in any case. 19015284Seric ** If '\t' then we are reading the .cf file. 19116914Seric ** pvpbuf -- place to put the saved text -- note that 19216914Seric ** the pointers are static. 193297Seric ** 194297Seric ** Returns: 1953149Seric ** A pointer to a vector of tokens. 196297Seric ** NULL on error. 197297Seric ** 198297Seric ** Side Effects: 1993149Seric ** none. 200297Seric */ 201297Seric 2028078Seric /* states and character types */ 2038078Seric # define OPR 0 /* operator */ 2048078Seric # define ATM 1 /* atom */ 2058078Seric # define QST 2 /* in quoted string */ 2068078Seric # define SPC 3 /* chewing up spaces */ 2078078Seric # define ONE 4 /* pick up one character */ 2083149Seric 2098078Seric # define NSTATES 5 /* number of states */ 2108078Seric # define TYPE 017 /* mask to select state type */ 2118078Seric 2128078Seric /* meta bits for table */ 2138078Seric # define M 020 /* meta character; don't pass through */ 2148078Seric # define B 040 /* cause a break */ 2158078Seric # define MB M|B /* meta-break */ 2168078Seric 2178078Seric static short StateTab[NSTATES][NSTATES] = 2188078Seric { 2198087Seric /* oldst chtype> OPR ATM QST SPC ONE */ 2209051Seric /*OPR*/ OPR|B, ATM|B, QST|B, SPC|MB, ONE|B, 2219051Seric /*ATM*/ OPR|B, ATM, QST|B, SPC|MB, ONE|B, 2229051Seric /*QST*/ QST, QST, OPR, QST, QST, 2238078Seric /*SPC*/ OPR, ATM, QST, SPC|M, ONE, 2248078Seric /*ONE*/ OPR, OPR, OPR, OPR, OPR, 2258078Seric }; 2268078Seric 2278078Seric # define NOCHAR -1 /* signal nothing in lookahead token */ 2288078Seric 2298078Seric char *DelimChar; /* set to point to the delimiter */ 2308078Seric 2313149Seric char ** 23216914Seric prescan(addr, delim, pvpbuf) 233297Seric char *addr; 234297Seric char delim; 23516914Seric char pvpbuf[]; 236297Seric { 237297Seric register char *p; 2388078Seric register char *q; 2399346Seric register int c; 2403149Seric char **avp; 241297Seric bool bslashmode; 242297Seric int cmntcnt; 2438423Seric int anglecnt; 2443149Seric char *tok; 2458078Seric int state; 2468078Seric int newstate; 2478078Seric static char *av[MAXATOM+1]; 24815253Seric extern int errno; 249297Seric 25015253Seric /* make sure error messages don't have garbage on them */ 25115253Seric errno = 0; 25215253Seric 25316914Seric q = pvpbuf; 2543149Seric bslashmode = FALSE; 2557800Seric cmntcnt = 0; 2568423Seric anglecnt = 0; 2573149Seric avp = av; 2588078Seric state = OPR; 2598078Seric c = NOCHAR; 2608078Seric p = addr; 2618078Seric # ifdef DEBUG 2628078Seric if (tTd(22, 45)) 263297Seric { 2648078Seric printf("prescan: "); 2658078Seric xputs(p); 2668078Seric putchar('\n'); 2678078Seric } 2688078Seric # endif DEBUG 2698078Seric 2708078Seric do 2718078Seric { 2723149Seric /* read a token */ 2733149Seric tok = q; 2748078Seric for (;;) 275297Seric { 2768078Seric /* store away any old lookahead character */ 2778078Seric if (c != NOCHAR) 2788078Seric { 27915284Seric /* see if there is room */ 28016914Seric if (q >= &pvpbuf[PSBUFSIZE - 5]) 2818078Seric { 2828078Seric usrerr("Address too long"); 2838078Seric DelimChar = p; 2848078Seric return (NULL); 2858078Seric } 28615284Seric 28715284Seric /* squirrel it away */ 2888078Seric *q++ = c; 2898078Seric } 2908078Seric 2918078Seric /* read a new input character */ 2928078Seric c = *p++; 2938078Seric if (c == '\0') 2948078Seric break; 29515284Seric c &= ~0200; 29615284Seric 2978078Seric # ifdef DEBUG 2988078Seric if (tTd(22, 101)) 2998078Seric printf("c=%c, s=%d; ", c, state); 3008078Seric # endif DEBUG 3018078Seric 3023149Seric /* chew up special characters */ 3033149Seric *q = '\0'; 3043149Seric if (bslashmode) 3053149Seric { 3063149Seric c |= 0200; 3073149Seric bslashmode = FALSE; 3083149Seric } 3093149Seric else if (c == '\\') 3103149Seric { 3113149Seric bslashmode = TRUE; 3128078Seric c = NOCHAR; 3133149Seric } 3148514Seric else if (state == QST) 3158514Seric { 3168514Seric /* do nothing, just avoid next clauses */ 3178514Seric } 3188078Seric else if (c == '(') 3194100Seric { 3208078Seric cmntcnt++; 3218078Seric c = NOCHAR; 3224100Seric } 3238078Seric else if (c == ')') 3243149Seric { 3258078Seric if (cmntcnt <= 0) 3263149Seric { 3278078Seric usrerr("Unbalanced ')'"); 3288078Seric DelimChar = p; 3298078Seric return (NULL); 3303149Seric } 3318078Seric else 3328078Seric cmntcnt--; 3338078Seric } 3348078Seric else if (cmntcnt > 0) 3358078Seric c = NOCHAR; 3368423Seric else if (c == '<') 3378423Seric anglecnt++; 3388423Seric else if (c == '>') 3398423Seric { 3408423Seric if (anglecnt <= 0) 3418423Seric { 3428423Seric usrerr("Unbalanced '>'"); 3438423Seric DelimChar = p; 3448423Seric return (NULL); 3458423Seric } 3468423Seric anglecnt--; 3478423Seric } 34811423Seric else if (delim == ' ' && isspace(c)) 34911423Seric c = ' '; 3503149Seric 3518078Seric if (c == NOCHAR) 3528078Seric continue; 3533149Seric 3548078Seric /* see if this is end of input */ 35511405Seric if (c == delim && anglecnt <= 0 && state != QST) 3563149Seric break; 3573149Seric 3588078Seric newstate = StateTab[state][toktype(c)]; 3598078Seric # ifdef DEBUG 3608078Seric if (tTd(22, 101)) 3618078Seric printf("ns=%02o\n", newstate); 3628078Seric # endif DEBUG 3638078Seric state = newstate & TYPE; 3648078Seric if (bitset(M, newstate)) 3658078Seric c = NOCHAR; 3668078Seric if (bitset(B, newstate)) 3674228Seric break; 368297Seric } 3693149Seric 3703149Seric /* new token */ 3718078Seric if (tok != q) 3721378Seric { 3738078Seric *q++ = '\0'; 3748078Seric # ifdef DEBUG 3758078Seric if (tTd(22, 36)) 376297Seric { 3778078Seric printf("tok="); 3788078Seric xputs(tok); 3798078Seric putchar('\n'); 380297Seric } 3818078Seric # endif DEBUG 3828078Seric if (avp >= &av[MAXATOM]) 383297Seric { 3848078Seric syserr("prescan: too many tokens"); 3858078Seric DelimChar = p; 3868078Seric return (NULL); 387297Seric } 3888078Seric *avp++ = tok; 389297Seric } 3908423Seric } while (c != '\0' && (c != delim || anglecnt > 0)); 3913149Seric *avp = NULL; 3928078Seric DelimChar = --p; 3933149Seric if (cmntcnt > 0) 3943149Seric usrerr("Unbalanced '('"); 3958423Seric else if (anglecnt > 0) 3968423Seric usrerr("Unbalanced '<'"); 3978078Seric else if (state == QST) 3983149Seric usrerr("Unbalanced '\"'"); 3993149Seric else if (av[0] != NULL) 4003149Seric return (av); 4013149Seric return (NULL); 4023149Seric } 4033149Seric /* 4043149Seric ** TOKTYPE -- return token type 4053149Seric ** 4063149Seric ** Parameters: 4073149Seric ** c -- the character in question. 4083149Seric ** 4093149Seric ** Returns: 4103149Seric ** Its type. 4113149Seric ** 4123149Seric ** Side Effects: 4133149Seric ** none. 4143149Seric */ 415297Seric 4163149Seric toktype(c) 4173149Seric register char c; 4183149Seric { 4193380Seric static char buf[50]; 4203382Seric static bool firstime = TRUE; 4213380Seric 4223382Seric if (firstime) 4233380Seric { 4243382Seric firstime = FALSE; 42516155Seric expand("\001o", buf, &buf[sizeof buf - 1], CurEnv); 4267005Seric (void) strcat(buf, DELIMCHARS); 4273380Seric } 4289585Seric if (c == MATCHCLASS || c == MATCHREPL || c == MATCHNCLASS) 4298078Seric return (ONE); 4308078Seric if (c == '"') 4318078Seric return (QST); 4324100Seric if (!isascii(c)) 4338078Seric return (ATM); 4348078Seric if (isspace(c) || c == ')') 4358078Seric return (SPC); 4363380Seric if (iscntrl(c) || index(buf, c) != NULL) 4378078Seric return (OPR); 4388078Seric return (ATM); 4393149Seric } 4403149Seric /* 4413149Seric ** REWRITE -- apply rewrite rules to token vector. 4423149Seric ** 4434476Seric ** This routine is an ordered production system. Each rewrite 4444476Seric ** rule has a LHS (called the pattern) and a RHS (called the 4454476Seric ** rewrite); 'rwr' points the the current rewrite rule. 4464476Seric ** 4474476Seric ** For each rewrite rule, 'avp' points the address vector we 4484476Seric ** are trying to match against, and 'pvp' points to the pattern. 4498058Seric ** If pvp points to a special match value (MATCHZANY, MATCHANY, 4509585Seric ** MATCHONE, MATCHCLASS, MATCHNCLASS) then the address in avp 4519585Seric ** matched is saved away in the match vector (pointed to by 'mvp'). 4524476Seric ** 4534476Seric ** When a match between avp & pvp does not match, we try to 4549585Seric ** back out. If we back up over MATCHONE, MATCHCLASS, or MATCHNCLASS 4554476Seric ** we must also back out the match in mvp. If we reach a 4568058Seric ** MATCHANY or MATCHZANY we just extend the match and start 4578058Seric ** over again. 4584476Seric ** 4594476Seric ** When we finally match, we rewrite the address vector 4604476Seric ** and try over again. 4614476Seric ** 4623149Seric ** Parameters: 4633149Seric ** pvp -- pointer to token vector. 4643149Seric ** 4653149Seric ** Returns: 4663149Seric ** none. 4673149Seric ** 4683149Seric ** Side Effects: 4693149Seric ** pvp is modified. 4703149Seric */ 4712091Seric 4723149Seric struct match 4733149Seric { 4744468Seric char **first; /* first token matched */ 4754468Seric char **last; /* last token matched */ 4763149Seric }; 4773149Seric 4784468Seric # define MAXMATCH 9 /* max params per rewrite */ 4793149Seric 4803149Seric 4814070Seric rewrite(pvp, ruleset) 4823149Seric char **pvp; 4834070Seric int ruleset; 4843149Seric { 4853149Seric register char *ap; /* address pointer */ 4863149Seric register char *rp; /* rewrite pointer */ 4873149Seric register char **avp; /* address vector pointer */ 4883149Seric register char **rvp; /* rewrite vector pointer */ 4898058Seric register struct match *mlp; /* cur ptr into mlist */ 4908058Seric register struct rewrite *rwr; /* pointer to current rewrite rule */ 4914468Seric struct match mlist[MAXMATCH]; /* stores match on LHS */ 4923149Seric char *npvp[MAXATOM+1]; /* temporary space for rebuild */ 4934060Seric extern bool sameword(); 4943149Seric 4959279Seric if (OpMode == MD_TEST || tTd(21, 2)) 4963149Seric { 4978959Seric printf("rewrite: ruleset %2d input:", ruleset); 4983149Seric printav(pvp); 4993149Seric } 5008423Seric if (pvp == NULL) 5018423Seric return; 5023149Seric 5033149Seric /* 5043149Seric ** Run through the list of rewrite rules, applying 5053149Seric ** any that match. 5063149Seric */ 5073149Seric 5084070Seric for (rwr = RewriteRules[ruleset]; rwr != NULL; ) 5093149Seric { 5104100Seric # ifdef DEBUG 5117675Seric if (tTd(21, 12)) 512297Seric { 5138069Seric printf("-----trying rule:"); 5143149Seric printav(rwr->r_lhs); 5153149Seric } 5164100Seric # endif DEBUG 5173149Seric 5183149Seric /* try to match on this rule */ 5194468Seric mlp = mlist; 5208058Seric rvp = rwr->r_lhs; 5218058Seric avp = pvp; 5228058Seric while ((ap = *avp) != NULL || *rvp != NULL) 5233149Seric { 5243149Seric rp = *rvp; 5258058Seric # ifdef DEBUG 5268058Seric if (tTd(21, 35)) 5278058Seric { 5288069Seric printf("ap="); 5298058Seric xputs(ap); 5308069Seric printf(", rp="); 5318058Seric xputs(rp); 5328069Seric printf("\n"); 5338058Seric } 5348058Seric # endif DEBUG 5353149Seric if (rp == NULL) 536297Seric { 5373149Seric /* end-of-pattern before end-of-address */ 5388058Seric goto backup; 539297Seric } 5408058Seric if (ap == NULL && *rp != MATCHZANY) 5418058Seric { 5428058Seric /* end-of-input */ 5438058Seric break; 5448058Seric } 5453149Seric 5463149Seric switch (*rp) 5473149Seric { 5484060Seric register STAB *s; 5494060Seric 5504060Seric case MATCHCLASS: 5519585Seric case MATCHNCLASS: 5529585Seric /* match any token in (not in) a class */ 5534100Seric s = stab(ap, ST_CLASS, ST_FIND); 55410690Seric if (s == NULL || !bitnset(rp[1], s->s_class)) 5559585Seric { 5569585Seric if (*rp == MATCHCLASS) 5579585Seric goto backup; 5589585Seric } 5599585Seric else if (*rp == MATCHNCLASS) 5608058Seric goto backup; 5614468Seric 5624476Seric /* explicit fall-through */ 5634476Seric 5644476Seric case MATCHONE: 5654476Seric case MATCHANY: 5664476Seric /* match exactly one token */ 5678058Seric mlp->first = avp; 5688058Seric mlp->last = avp++; 5694468Seric mlp++; 5704060Seric break; 5714060Seric 5728058Seric case MATCHZANY: 5738058Seric /* match zero or more tokens */ 5748058Seric mlp->first = avp; 5758058Seric mlp->last = avp - 1; 5768058Seric mlp++; 5778058Seric break; 5788058Seric 5793149Seric default: 5803149Seric /* must have exact match */ 5814060Seric if (!sameword(rp, ap)) 5828058Seric goto backup; 5834468Seric avp++; 5843149Seric break; 5853149Seric } 5863149Seric 5873149Seric /* successful match on this token */ 5883149Seric rvp++; 5893149Seric continue; 5903149Seric 5918058Seric backup: 5923149Seric /* match failed -- back up */ 5933149Seric while (--rvp >= rwr->r_lhs) 5943149Seric { 5953149Seric rp = *rvp; 5968058Seric if (*rp == MATCHANY || *rp == MATCHZANY) 5974468Seric { 5984476Seric /* extend binding and continue */ 5998058Seric avp = ++mlp[-1].last; 6008058Seric avp++; 6014476Seric rvp++; 6023149Seric break; 6034468Seric } 6044476Seric avp--; 6059585Seric if (*rp == MATCHONE || *rp == MATCHCLASS || 6069585Seric *rp == MATCHNCLASS) 6073149Seric { 6084468Seric /* back out binding */ 6094468Seric mlp--; 6103149Seric } 6113149Seric } 6123149Seric 6133149Seric if (rvp < rwr->r_lhs) 6143149Seric { 6153149Seric /* total failure to match */ 6163149Seric break; 6173149Seric } 618297Seric } 6193149Seric 6203149Seric /* 6213149Seric ** See if we successfully matched 6223149Seric */ 6233149Seric 6249374Seric if (rvp < rwr->r_lhs || *rvp != NULL) 6253149Seric { 6264100Seric # ifdef DEBUG 6279374Seric if (tTd(21, 10)) 6289374Seric printf("----- rule fails\n"); 6294100Seric # endif DEBUG 6309374Seric rwr = rwr->r_next; 6319374Seric continue; 6329374Seric } 6333149Seric 6349374Seric rvp = rwr->r_rhs; 6359374Seric # ifdef DEBUG 6369374Seric if (tTd(21, 12)) 6379374Seric { 6389374Seric printf("-----rule matches:"); 6399374Seric printav(rvp); 6409374Seric } 6419374Seric # endif DEBUG 6429374Seric 6439374Seric rp = *rvp; 6449374Seric if (*rp == CANONUSER) 6459374Seric { 6469374Seric rvp++; 6479374Seric rwr = rwr->r_next; 6489374Seric } 6499374Seric else if (*rp == CANONHOST) 6509374Seric { 6519374Seric rvp++; 6529374Seric rwr = NULL; 6539374Seric } 6549374Seric else if (*rp == CANONNET) 6559374Seric rwr = NULL; 6569374Seric 6579374Seric /* substitute */ 6589374Seric for (avp = npvp; *rvp != NULL; rvp++) 6599374Seric { 6609374Seric register struct match *m; 6619374Seric register char **pp; 6629374Seric 6638058Seric rp = *rvp; 66416914Seric if (*rp == MATCHREPL) 6658058Seric { 66616914Seric /* substitute from LHS */ 66716914Seric m = &mlist[rp[1] - '1']; 66816914Seric if (m >= mlp) 6699374Seric { 67016914Seric syserr("rewrite: ruleset %d: replacement out of bounds", ruleset); 6719374Seric return; 6729374Seric } 6739374Seric # ifdef DEBUG 67416914Seric if (tTd(21, 15)) 67516914Seric { 67616914Seric printf("$%c:", rp[1]); 67716914Seric pp = m->first; 67816914Seric while (pp <= m->last) 67916914Seric { 68016914Seric printf(" %x=\"", *pp); 68116914Seric (void) fflush(stdout); 68216914Seric printf("%s\"", *pp++); 68316914Seric } 68416914Seric printf("\n"); 68516914Seric } 68616914Seric # endif DEBUG 6879374Seric pp = m->first; 6889374Seric while (pp <= m->last) 6893149Seric { 69016914Seric if (avp >= &npvp[MAXATOM]) 69116914Seric { 69216914Seric syserr("rewrite: expansion too long"); 69316914Seric return; 69416914Seric } 69516914Seric *avp++ = *pp++; 6963149Seric } 6973149Seric } 69816914Seric else 6998226Seric { 70016914Seric /* vanilla replacement */ 7019374Seric if (avp >= &npvp[MAXATOM]) 70216889Seric { 70316914Seric toolong: 70416889Seric syserr("rewrite: expansion too long"); 70516889Seric return; 70616889Seric } 70716914Seric *avp++ = rp; 7088226Seric } 7099374Seric } 7109374Seric *avp++ = NULL; 71116914Seric 71216914Seric /* 71316914Seric ** Check for any hostname lookups. 71416914Seric */ 71516914Seric 71616914Seric for (rvp = npvp; *rvp != NULL; rvp++) 71716914Seric { 71816914Seric char **hbrvp; 71916914Seric char **xpvp; 72016914Seric int trsize; 72116914Seric int i; 722*17473Seric char *olddelimchar; 72316920Seric char buf[MAXNAME + 1]; 72416914Seric char *pvpb1[MAXATOM + 1]; 72517174Seric char pvpbuf[PSBUFSIZE]; 726*17473Seric extern char *DelimChar; 72716914Seric 72816914Seric if (**rvp != HOSTBEGIN) 72916914Seric continue; 73016914Seric 73116914Seric /* 73216914Seric ** Got a hostname lookup. 73316914Seric ** 73416914Seric ** This could be optimized fairly easily. 73516914Seric */ 73616914Seric 73716914Seric hbrvp = rvp; 73816914Seric 73916914Seric /* extract the match part */ 74016914Seric while (*++rvp != NULL && **rvp != HOSTEND) 74116914Seric continue; 74216914Seric if (*rvp != NULL) 74316914Seric *rvp++ = NULL; 74416914Seric 74516914Seric /* save the remainder of the input string */ 74616914Seric trsize = (int) (avp - rvp + 1) * sizeof *rvp; 74716914Seric bcopy((char *) rvp, (char *) pvpb1, trsize); 74816914Seric 74916914Seric /* look it up */ 75016914Seric cataddr(++hbrvp, buf, sizeof buf); 75116914Seric maphostname(buf, sizeof buf); 75216914Seric 75316914Seric /* scan the new host name */ 754*17473Seric olddelimchar = DelimChar; 75516914Seric xpvp = prescan(buf, '\0', pvpbuf); 756*17473Seric DelimChar = olddelimchar; 75716914Seric if (xpvp == NULL) 75816914Seric { 75916914Seric syserr("rewrite: cannot prescan canonical hostname: %s", buf); 76016914Seric return (NULL); 76116914Seric } 76216914Seric 76316914Seric /* append it to the token list */ 76417174Seric for (avp = --hbrvp; *xpvp != NULL; xpvp++) 76517174Seric { 76617174Seric *avp++ = newstr(*xpvp); 76716920Seric if (avp >= &npvp[MAXATOM]) 76816914Seric goto toolong; 76917174Seric } 77016914Seric 77116914Seric /* restore the old trailing information */ 77217177Seric for (xpvp = pvpb1; (*avp++ = *xpvp++) != NULL; ) 77316920Seric if (avp >= &npvp[MAXATOM]) 77416914Seric goto toolong; 77517174Seric 77617174Seric break; 77716914Seric } 77816914Seric 77916914Seric /* 78016914Seric ** Check for subroutine calls. 78116914Seric */ 78216914Seric 78316889Seric if (**npvp == CALLSUBR) 7849374Seric { 78516889Seric bcopy((char *) &npvp[2], (char *) pvp, 78616900Seric (int) (avp - npvp - 2) * sizeof *avp); 78716889Seric # ifdef DEBUG 78816889Seric if (tTd(21, 3)) 78916889Seric printf("-----callsubr %s\n", npvp[1]); 79016889Seric # endif DEBUG 79116889Seric rewrite(pvp, atoi(npvp[1])); 7923149Seric } 7933149Seric else 7943149Seric { 79517348Seric bcopy((char *) npvp, (char *) pvp, 79616900Seric (int) (avp - npvp) * sizeof *avp); 7979374Seric } 7984100Seric # ifdef DEBUG 7999374Seric if (tTd(21, 4)) 8009374Seric { 8019374Seric printf("rewritten as:"); 8029374Seric printav(pvp); 8039374Seric } 8044100Seric # endif DEBUG 805297Seric } 8068069Seric 8079279Seric if (OpMode == MD_TEST || tTd(21, 2)) 8088069Seric { 8098959Seric printf("rewrite: ruleset %2d returns:", ruleset); 8108069Seric printav(pvp); 8118069Seric } 8123149Seric } 8133149Seric /* 8143149Seric ** BUILDADDR -- build address from token vector. 8153149Seric ** 8163149Seric ** Parameters: 8173149Seric ** tv -- token vector. 8183149Seric ** a -- pointer to address descriptor to fill. 8193149Seric ** If NULL, one will be allocated. 8203149Seric ** 8213149Seric ** Returns: 8224279Seric ** NULL if there was an error. 8234279Seric ** 'a' otherwise. 8243149Seric ** 8253149Seric ** Side Effects: 8263149Seric ** fills in 'a' 8273149Seric */ 8283149Seric 8293149Seric ADDRESS * 8303149Seric buildaddr(tv, a) 8313149Seric register char **tv; 8323149Seric register ADDRESS *a; 8333149Seric { 8343149Seric static char buf[MAXNAME]; 8353149Seric struct mailer **mp; 8363149Seric register struct mailer *m; 8374635Seric extern bool sameword(); 8383149Seric 8393149Seric if (a == NULL) 8403149Seric a = (ADDRESS *) xalloc(sizeof *a); 84116889Seric bzero((char *) a, sizeof *a); 8423149Seric 8433149Seric /* figure out what net/mailer to use */ 8443149Seric if (**tv != CANONNET) 8454279Seric { 8463149Seric syserr("buildaddr: no net"); 8474279Seric return (NULL); 8484279Seric } 8493149Seric tv++; 8504635Seric if (sameword(*tv, "error")) 8514279Seric { 85210183Seric if (**++tv == CANONHOST) 85310183Seric { 85410183Seric setstat(atoi(*++tv)); 85510183Seric tv++; 85610183Seric } 85710183Seric if (**tv != CANONUSER) 8584279Seric syserr("buildaddr: error: no user"); 8594279Seric buf[0] = '\0'; 8604279Seric while (*++tv != NULL) 8614279Seric { 8624279Seric if (buf[0] != '\0') 8637005Seric (void) strcat(buf, " "); 8647005Seric (void) strcat(buf, *tv); 8654279Seric } 8664279Seric usrerr(buf); 8674279Seric return (NULL); 8684279Seric } 8694598Seric for (mp = Mailer; (m = *mp++) != NULL; ) 8703149Seric { 8714635Seric if (sameword(m->m_name, *tv)) 8723149Seric break; 8733149Seric } 8743149Seric if (m == NULL) 8754279Seric { 8763149Seric syserr("buildaddr: unknown net %s", *tv); 8774279Seric return (NULL); 8784279Seric } 8794598Seric a->q_mailer = m; 8803149Seric 8813149Seric /* figure out what host (if any) */ 8823149Seric tv++; 88310690Seric if (!bitnset(M_LOCAL, m->m_flags)) 8843149Seric { 8855704Seric if (**tv++ != CANONHOST) 8864279Seric { 8873149Seric syserr("buildaddr: no host"); 8884279Seric return (NULL); 8894279Seric } 8905704Seric buf[0] = '\0'; 8915704Seric while (*tv != NULL && **tv != CANONUSER) 8927005Seric (void) strcat(buf, *tv++); 8935704Seric a->q_host = newstr(buf); 8943149Seric } 8953149Seric else 8963149Seric a->q_host = NULL; 8973149Seric 8983149Seric /* figure out the user */ 8993149Seric if (**tv != CANONUSER) 9004279Seric { 9013149Seric syserr("buildaddr: no user"); 9024279Seric return (NULL); 9034279Seric } 90411278Seric rewrite(++tv, 4); 90511278Seric cataddr(tv, buf, sizeof buf); 9063149Seric a->q_user = buf; 9073149Seric 9083149Seric return (a); 9093149Seric } 9103188Seric /* 9114228Seric ** CATADDR -- concatenate pieces of addresses (putting in <LWSP> subs) 9124228Seric ** 9134228Seric ** Parameters: 9144228Seric ** pvp -- parameter vector to rebuild. 9154228Seric ** buf -- buffer to build the string into. 9164228Seric ** sz -- size of buf. 9174228Seric ** 9184228Seric ** Returns: 9194228Seric ** none. 9204228Seric ** 9214228Seric ** Side Effects: 9224228Seric ** Destroys buf. 9234228Seric */ 9244228Seric 9254228Seric cataddr(pvp, buf, sz) 9264228Seric char **pvp; 9274228Seric char *buf; 9284228Seric register int sz; 9294228Seric { 9304228Seric bool oatomtok = FALSE; 9314228Seric bool natomtok = FALSE; 9324228Seric register int i; 9334228Seric register char *p; 9344228Seric 9358423Seric if (pvp == NULL) 9368423Seric { 9378423Seric strcpy(buf, ""); 9388423Seric return; 9398423Seric } 9404228Seric p = buf; 94111156Seric sz -= 2; 9424228Seric while (*pvp != NULL && (i = strlen(*pvp)) < sz) 9434228Seric { 9448078Seric natomtok = (toktype(**pvp) == ATM); 9454228Seric if (oatomtok && natomtok) 9469042Seric *p++ = SpaceSub; 9474228Seric (void) strcpy(p, *pvp); 9484228Seric oatomtok = natomtok; 9494228Seric p += i; 95011156Seric sz -= i + 1; 9514228Seric pvp++; 9524228Seric } 9534228Seric *p = '\0'; 9544228Seric } 9554228Seric /* 9563188Seric ** SAMEADDR -- Determine if two addresses are the same 9573188Seric ** 9583188Seric ** This is not just a straight comparison -- if the mailer doesn't 9593188Seric ** care about the host we just ignore it, etc. 9603188Seric ** 9613188Seric ** Parameters: 9623188Seric ** a, b -- pointers to the internal forms to compare. 9633188Seric ** 9643188Seric ** Returns: 9653188Seric ** TRUE -- they represent the same mailbox. 9663188Seric ** FALSE -- they don't. 9673188Seric ** 9683188Seric ** Side Effects: 9693188Seric ** none. 9703188Seric */ 9713188Seric 9723188Seric bool 9739374Seric sameaddr(a, b) 9743188Seric register ADDRESS *a; 9753188Seric register ADDRESS *b; 9763188Seric { 9773188Seric /* if they don't have the same mailer, forget it */ 9783188Seric if (a->q_mailer != b->q_mailer) 9793188Seric return (FALSE); 9803188Seric 9813188Seric /* if the user isn't the same, we can drop out */ 9829374Seric if (strcmp(a->q_user, b->q_user) != 0) 9833188Seric return (FALSE); 9843188Seric 9853188Seric /* if the mailer ignores hosts, we have succeeded! */ 98610690Seric if (bitnset(M_LOCAL, a->q_mailer->m_flags)) 9873188Seric return (TRUE); 9883188Seric 9893188Seric /* otherwise compare hosts (but be careful for NULL ptrs) */ 9903188Seric if (a->q_host == NULL || b->q_host == NULL) 9913188Seric return (FALSE); 9923188Seric if (strcmp(a->q_host, b->q_host) != 0) 9933188Seric return (FALSE); 9943188Seric 9953188Seric return (TRUE); 9963188Seric } 9973234Seric /* 9983234Seric ** PRINTADDR -- print address (for debugging) 9993234Seric ** 10003234Seric ** Parameters: 10013234Seric ** a -- the address to print 10023234Seric ** follow -- follow the q_next chain. 10033234Seric ** 10043234Seric ** Returns: 10053234Seric ** none. 10063234Seric ** 10073234Seric ** Side Effects: 10083234Seric ** none. 10093234Seric */ 10103234Seric 10114317Seric # ifdef DEBUG 10124317Seric 10133234Seric printaddr(a, follow) 10143234Seric register ADDRESS *a; 10153234Seric bool follow; 10163234Seric { 10175001Seric bool first = TRUE; 10185001Seric 10193234Seric while (a != NULL) 10203234Seric { 10215001Seric first = FALSE; 10224443Seric printf("%x=", a); 10234085Seric (void) fflush(stdout); 10243234Seric printf("%s: mailer %d (%s), host `%s', user `%s'\n", a->q_paddr, 10258181Seric a->q_mailer->m_mno, a->q_mailer->m_name, a->q_host, 10268181Seric a->q_user); 10278181Seric printf("\tnext=%x, flags=%o, alias %x\n", a->q_next, a->q_flags, 10288181Seric a->q_alias); 10298181Seric printf("\thome=\"%s\", fullname=\"%s\"\n", a->q_home, 10308181Seric a->q_fullname); 10314996Seric 10323234Seric if (!follow) 10333234Seric return; 10344996Seric a = a->q_next; 10353234Seric } 10365001Seric if (first) 10374443Seric printf("[NULL]\n"); 10383234Seric } 10394317Seric 10404317Seric # endif DEBUG 10417682Seric /* 10427682Seric ** REMOTENAME -- return the name relative to the current mailer 10437682Seric ** 10447682Seric ** Parameters: 10457682Seric ** name -- the name to translate. 10468069Seric ** m -- the mailer that we want to do rewriting relative 10478069Seric ** to. 10488069Seric ** senderaddress -- if set, uses the sender rewriting rules 10498069Seric ** rather than the recipient rewriting rules. 105010310Seric ** canonical -- if set, strip out any comment information, 105110310Seric ** etc. 10527682Seric ** 10537682Seric ** Returns: 10547682Seric ** the text string representing this address relative to 10557682Seric ** the receiving mailer. 10567682Seric ** 10577682Seric ** Side Effects: 10587682Seric ** none. 10597682Seric ** 10607682Seric ** Warnings: 10617682Seric ** The text string returned is tucked away locally; 10627682Seric ** copy it if you intend to save it. 10637682Seric */ 10647682Seric 10657682Seric char * 106610310Seric remotename(name, m, senderaddress, canonical) 10677682Seric char *name; 10687682Seric struct mailer *m; 10698069Seric bool senderaddress; 107010310Seric bool canonical; 10717682Seric { 10728069Seric register char **pvp; 10738069Seric char *fancy; 107415284Seric register char *p; 10758069Seric extern char *macvalue(); 10768181Seric char *oldg = macvalue('g', CurEnv); 10777682Seric static char buf[MAXNAME]; 10787682Seric char lbuf[MAXNAME]; 107916914Seric char pvpbuf[PSBUFSIZE]; 10807682Seric extern char **prescan(); 10817889Seric extern char *crackaddr(); 10827682Seric 10837755Seric # ifdef DEBUG 10847755Seric if (tTd(12, 1)) 10857755Seric printf("remotename(%s)\n", name); 10867755Seric # endif DEBUG 10877755Seric 108810177Seric /* don't do anything if we are tagging it as special */ 108910177Seric if ((senderaddress ? m->m_s_rwset : m->m_r_rwset) < 0) 109010177Seric return (name); 109110177Seric 10927682Seric /* 10938181Seric ** Do a heuristic crack of this name to extract any comment info. 10948181Seric ** This will leave the name as a comment and a $g macro. 10957889Seric */ 10967889Seric 109710310Seric if (canonical) 109816155Seric fancy = "\001g"; 109910310Seric else 110010310Seric fancy = crackaddr(name); 11017889Seric 11028181Seric /* 11038181Seric ** Turn the name into canonical form. 11048181Seric ** Normally this will be RFC 822 style, i.e., "user@domain". 11058181Seric ** If this only resolves to "user", and the "C" flag is 11068181Seric ** specified in the sending mailer, then the sender's 11078181Seric ** domain will be appended. 11088181Seric */ 11098181Seric 111016914Seric pvp = prescan(name, '\0', pvpbuf); 11117889Seric if (pvp == NULL) 11127889Seric return (name); 11138181Seric rewrite(pvp, 3); 11148181Seric if (CurEnv->e_fromdomain != NULL) 11158181Seric { 11168181Seric /* append from domain to this address */ 11178181Seric register char **pxp = pvp; 11188181Seric 11199594Seric /* see if there is an "@domain" in the current name */ 11208181Seric while (*pxp != NULL && strcmp(*pxp, "@") != 0) 11218181Seric pxp++; 11228181Seric if (*pxp == NULL) 11238181Seric { 11249594Seric /* no.... append the "@domain" from the sender */ 11258181Seric register char **qxq = CurEnv->e_fromdomain; 11268181Seric 11279594Seric while ((*pxp++ = *qxq++) != NULL) 11289594Seric continue; 112911726Seric rewrite(pvp, 3); 11308181Seric } 11318181Seric } 11328181Seric 11338181Seric /* 11348959Seric ** Do more specific rewriting. 11358181Seric ** Rewrite using ruleset 1 or 2 depending on whether this is 11368181Seric ** a sender address or not. 11378181Seric ** Then run it through any receiving-mailer-specific rulesets. 11388181Seric */ 11398181Seric 11408069Seric if (senderaddress) 11417755Seric { 11427889Seric rewrite(pvp, 1); 11438069Seric if (m->m_s_rwset > 0) 11448069Seric rewrite(pvp, m->m_s_rwset); 11458069Seric } 11468069Seric else 11478069Seric { 11487889Seric rewrite(pvp, 2); 11498069Seric if (m->m_r_rwset > 0) 11508069Seric rewrite(pvp, m->m_r_rwset); 11517682Seric } 11527682Seric 11538181Seric /* 11548959Seric ** Do any final sanitation the address may require. 11558959Seric ** This will normally be used to turn internal forms 11568959Seric ** (e.g., user@host.LOCAL) into external form. This 11578959Seric ** may be used as a default to the above rules. 11588959Seric */ 11598959Seric 11608959Seric rewrite(pvp, 4); 11618959Seric 11628959Seric /* 11638181Seric ** Now restore the comment information we had at the beginning. 11648181Seric */ 11658181Seric 11667682Seric cataddr(pvp, lbuf, sizeof lbuf); 11679374Seric define('g', lbuf, CurEnv); 11687889Seric expand(fancy, buf, &buf[sizeof buf - 1], CurEnv); 11699374Seric define('g', oldg, CurEnv); 11707682Seric 11717682Seric # ifdef DEBUG 11727682Seric if (tTd(12, 1)) 11737755Seric printf("remotename => `%s'\n", buf); 11747682Seric # endif DEBUG 11757682Seric return (buf); 11767682Seric } 1177