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*51381Seric static char sccsid[] = "@(#)parseaddr.c 5.15 (Berkeley) 10/20/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 11351317Seric allocaddr(a, copyf, addr); 11451317Seric 11551317Seric /* 11651317Seric ** Compute return value. 11751317Seric */ 11851317Seric 11951317Seric if (tTd(20, 1)) 1208078Seric { 12151317Seric printf("parseaddr-->"); 12251317Seric printaddr(a, FALSE); 12351317Seric } 12451317Seric 12551317Seric return (a); 12651317Seric } 12751317Seric /* 12851317Seric ** ALLOCADDR -- do local allocations of address on demand. 12951317Seric ** 13051317Seric ** Also lowercases the host name if requested. 13151317Seric ** 13251317Seric ** Parameters: 13351317Seric ** a -- the address to reallocate. 13451317Seric ** copyf -- the copy flag (see parseaddr for description). 13551317Seric ** paddr -- the printname of the address. 13651317Seric ** 13751317Seric ** Returns: 13851317Seric ** none. 13951317Seric ** 14051317Seric ** Side Effects: 14151317Seric ** Copies portions of a into local buffers as requested. 14251317Seric */ 14351317Seric 14451317Seric allocaddr(a, copyf, paddr) 14551317Seric register ADDRESS *a; 14651317Seric int copyf; 14751317Seric char *paddr; 14851317Seric { 14951317Seric register MAILER *m = a->q_mailer; 15051317Seric 15151317Seric if (copyf > 0 && paddr != NULL) 15251317Seric { 1538078Seric extern char *DelimChar; 1548078Seric char savec = *DelimChar; 1558078Seric 1568078Seric *DelimChar = '\0'; 15751317Seric a->q_paddr = newstr(paddr); 1588078Seric *DelimChar = savec; 1598078Seric } 160297Seric else 16151317Seric 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 17551317Seric if (a->q_paddr == NULL) 17651317Seric a->q_paddr = a->q_user; 17751317Seric 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 } 344*51381Seric 345*51381Seric if (c == '\\') 3463149Seric { 3473149Seric bslashmode = TRUE; 3488078Seric c = NOCHAR; 3493149Seric } 3508514Seric else if (state == QST) 3518514Seric { 3528514Seric /* do nothing, just avoid next clauses */ 3538514Seric } 3548078Seric else if (c == '(') 3554100Seric { 3568078Seric cmntcnt++; 3578078Seric c = NOCHAR; 3584100Seric } 3598078Seric else if (c == ')') 3603149Seric { 3618078Seric if (cmntcnt <= 0) 3623149Seric { 3638078Seric usrerr("Unbalanced ')'"); 3648078Seric DelimChar = p; 3658078Seric return (NULL); 3663149Seric } 3678078Seric else 3688078Seric cmntcnt--; 3698078Seric } 3708078Seric else if (cmntcnt > 0) 3718078Seric c = NOCHAR; 3728423Seric else if (c == '<') 3738423Seric anglecnt++; 3748423Seric else if (c == '>') 3758423Seric { 3768423Seric if (anglecnt <= 0) 3778423Seric { 3788423Seric usrerr("Unbalanced '>'"); 3798423Seric DelimChar = p; 3808423Seric return (NULL); 3818423Seric } 3828423Seric anglecnt--; 3838423Seric } 38411423Seric else if (delim == ' ' && isspace(c)) 38511423Seric c = ' '; 3863149Seric 3878078Seric if (c == NOCHAR) 3888078Seric continue; 3893149Seric 3908078Seric /* see if this is end of input */ 39111405Seric if (c == delim && anglecnt <= 0 && state != QST) 3923149Seric break; 3933149Seric 3948078Seric newstate = StateTab[state][toktype(c)]; 3958078Seric if (tTd(22, 101)) 3968078Seric printf("ns=%02o\n", newstate); 3978078Seric state = newstate & TYPE; 3988078Seric if (bitset(M, newstate)) 3998078Seric c = NOCHAR; 4008078Seric if (bitset(B, newstate)) 4014228Seric break; 402297Seric } 4033149Seric 4043149Seric /* new token */ 4058078Seric if (tok != q) 4061378Seric { 4078078Seric *q++ = '\0'; 4088078Seric if (tTd(22, 36)) 409297Seric { 4108078Seric printf("tok="); 4118078Seric xputs(tok); 41223109Seric (void) putchar('\n'); 413297Seric } 4148078Seric if (avp >= &av[MAXATOM]) 415297Seric { 4168078Seric syserr("prescan: too many tokens"); 4178078Seric DelimChar = p; 4188078Seric return (NULL); 419297Seric } 4208078Seric *avp++ = tok; 421297Seric } 4228423Seric } while (c != '\0' && (c != delim || anglecnt > 0)); 4233149Seric *avp = NULL; 4248078Seric DelimChar = --p; 4253149Seric if (cmntcnt > 0) 4263149Seric usrerr("Unbalanced '('"); 4278423Seric else if (anglecnt > 0) 4288423Seric usrerr("Unbalanced '<'"); 4298078Seric else if (state == QST) 4303149Seric usrerr("Unbalanced '\"'"); 4313149Seric else if (av[0] != NULL) 4323149Seric return (av); 4333149Seric return (NULL); 4343149Seric } 4353149Seric /* 4363149Seric ** TOKTYPE -- return token type 4373149Seric ** 4383149Seric ** Parameters: 4393149Seric ** c -- the character in question. 4403149Seric ** 4413149Seric ** Returns: 4423149Seric ** Its type. 4433149Seric ** 4443149Seric ** Side Effects: 4453149Seric ** none. 4463149Seric */ 447297Seric 4483149Seric toktype(c) 4493149Seric register char c; 4503149Seric { 4513380Seric static char buf[50]; 4523382Seric static bool firstime = TRUE; 4533380Seric 4543382Seric if (firstime) 4553380Seric { 4563382Seric firstime = FALSE; 45716155Seric expand("\001o", buf, &buf[sizeof buf - 1], CurEnv); 4587005Seric (void) strcat(buf, DELIMCHARS); 4593380Seric } 4609585Seric if (c == MATCHCLASS || c == MATCHREPL || c == MATCHNCLASS) 4618078Seric return (ONE); 4628078Seric if (c == '"') 4638078Seric return (QST); 4644100Seric if (!isascii(c)) 4658078Seric return (ATM); 4668078Seric if (isspace(c) || c == ')') 4678078Seric return (SPC); 4683380Seric if (iscntrl(c) || index(buf, c) != NULL) 4698078Seric return (OPR); 4708078Seric return (ATM); 4713149Seric } 4723149Seric /* 4733149Seric ** REWRITE -- apply rewrite rules to token vector. 4743149Seric ** 4754476Seric ** This routine is an ordered production system. Each rewrite 4764476Seric ** rule has a LHS (called the pattern) and a RHS (called the 4774476Seric ** rewrite); 'rwr' points the the current rewrite rule. 4784476Seric ** 4794476Seric ** For each rewrite rule, 'avp' points the address vector we 4804476Seric ** are trying to match against, and 'pvp' points to the pattern. 4818058Seric ** If pvp points to a special match value (MATCHZANY, MATCHANY, 4829585Seric ** MATCHONE, MATCHCLASS, MATCHNCLASS) then the address in avp 4839585Seric ** matched is saved away in the match vector (pointed to by 'mvp'). 4844476Seric ** 4854476Seric ** When a match between avp & pvp does not match, we try to 4869585Seric ** back out. If we back up over MATCHONE, MATCHCLASS, or MATCHNCLASS 4874476Seric ** we must also back out the match in mvp. If we reach a 4888058Seric ** MATCHANY or MATCHZANY we just extend the match and start 4898058Seric ** over again. 4904476Seric ** 4914476Seric ** When we finally match, we rewrite the address vector 4924476Seric ** and try over again. 4934476Seric ** 4943149Seric ** Parameters: 4953149Seric ** pvp -- pointer to token vector. 4963149Seric ** 4973149Seric ** Returns: 4983149Seric ** none. 4993149Seric ** 5003149Seric ** Side Effects: 5013149Seric ** pvp is modified. 5023149Seric */ 5032091Seric 5043149Seric struct match 5053149Seric { 5064468Seric char **first; /* first token matched */ 5074468Seric char **last; /* last token matched */ 5083149Seric }; 5093149Seric 5104468Seric # define MAXMATCH 9 /* max params per rewrite */ 5113149Seric 5123149Seric 5134070Seric rewrite(pvp, ruleset) 5143149Seric char **pvp; 5154070Seric int ruleset; 5163149Seric { 5173149Seric register char *ap; /* address pointer */ 5183149Seric register char *rp; /* rewrite pointer */ 5193149Seric register char **avp; /* address vector pointer */ 5203149Seric register char **rvp; /* rewrite vector pointer */ 5218058Seric register struct match *mlp; /* cur ptr into mlist */ 5228058Seric register struct rewrite *rwr; /* pointer to current rewrite rule */ 5234468Seric struct match mlist[MAXMATCH]; /* stores match on LHS */ 5243149Seric char *npvp[MAXATOM+1]; /* temporary space for rebuild */ 5253149Seric 5269279Seric if (OpMode == MD_TEST || tTd(21, 2)) 5273149Seric { 5288959Seric printf("rewrite: ruleset %2d input:", ruleset); 5293149Seric printav(pvp); 5303149Seric } 5318423Seric if (pvp == NULL) 5328423Seric return; 5333149Seric 5343149Seric /* 5353149Seric ** Run through the list of rewrite rules, applying 5363149Seric ** any that match. 5373149Seric */ 5383149Seric 5394070Seric for (rwr = RewriteRules[ruleset]; rwr != NULL; ) 5403149Seric { 5417675Seric if (tTd(21, 12)) 542297Seric { 5438069Seric printf("-----trying rule:"); 5443149Seric printav(rwr->r_lhs); 5453149Seric } 5463149Seric 5473149Seric /* try to match on this rule */ 5484468Seric mlp = mlist; 5498058Seric rvp = rwr->r_lhs; 5508058Seric avp = pvp; 5518058Seric while ((ap = *avp) != NULL || *rvp != NULL) 5523149Seric { 5533149Seric rp = *rvp; 5548058Seric if (tTd(21, 35)) 5558058Seric { 5568069Seric printf("ap="); 5578058Seric xputs(ap); 5588069Seric printf(", rp="); 5598058Seric xputs(rp); 5608069Seric printf("\n"); 5618058Seric } 5623149Seric if (rp == NULL) 563297Seric { 5643149Seric /* end-of-pattern before end-of-address */ 5658058Seric goto backup; 566297Seric } 5678058Seric if (ap == NULL && *rp != MATCHZANY) 5688058Seric { 5698058Seric /* end-of-input */ 5708058Seric break; 5718058Seric } 5723149Seric 5733149Seric switch (*rp) 5743149Seric { 5754060Seric register STAB *s; 5764060Seric 5774060Seric case MATCHCLASS: 5789585Seric case MATCHNCLASS: 5799585Seric /* match any token in (not in) a class */ 5804100Seric s = stab(ap, ST_CLASS, ST_FIND); 58110690Seric if (s == NULL || !bitnset(rp[1], s->s_class)) 5829585Seric { 5839585Seric if (*rp == MATCHCLASS) 5849585Seric goto backup; 5859585Seric } 5869585Seric else if (*rp == MATCHNCLASS) 5878058Seric goto backup; 5884468Seric 5894476Seric /* explicit fall-through */ 5904476Seric 5914476Seric case MATCHONE: 5924476Seric case MATCHANY: 5934476Seric /* match exactly one token */ 5948058Seric mlp->first = avp; 5958058Seric mlp->last = avp++; 5964468Seric mlp++; 5974060Seric break; 5984060Seric 5998058Seric case MATCHZANY: 6008058Seric /* match zero or more tokens */ 6018058Seric mlp->first = avp; 6028058Seric mlp->last = avp - 1; 6038058Seric mlp++; 6048058Seric break; 6058058Seric 6063149Seric default: 6073149Seric /* must have exact match */ 60833725Sbostic if (strcasecmp(rp, ap)) 6098058Seric goto backup; 6104468Seric avp++; 6113149Seric break; 6123149Seric } 6133149Seric 6143149Seric /* successful match on this token */ 6153149Seric rvp++; 6163149Seric continue; 6173149Seric 6188058Seric backup: 6193149Seric /* match failed -- back up */ 6203149Seric while (--rvp >= rwr->r_lhs) 6213149Seric { 6223149Seric rp = *rvp; 6238058Seric if (*rp == MATCHANY || *rp == MATCHZANY) 6244468Seric { 6254476Seric /* extend binding and continue */ 6268058Seric avp = ++mlp[-1].last; 6278058Seric avp++; 6284476Seric rvp++; 6293149Seric break; 6304468Seric } 6314476Seric avp--; 6329585Seric if (*rp == MATCHONE || *rp == MATCHCLASS || 6339585Seric *rp == MATCHNCLASS) 6343149Seric { 6354468Seric /* back out binding */ 6364468Seric mlp--; 6373149Seric } 6383149Seric } 6393149Seric 6403149Seric if (rvp < rwr->r_lhs) 6413149Seric { 6423149Seric /* total failure to match */ 6433149Seric break; 6443149Seric } 645297Seric } 6463149Seric 6473149Seric /* 6483149Seric ** See if we successfully matched 6493149Seric */ 6503149Seric 6519374Seric if (rvp < rwr->r_lhs || *rvp != NULL) 6523149Seric { 6539374Seric if (tTd(21, 10)) 6549374Seric printf("----- rule fails\n"); 6559374Seric rwr = rwr->r_next; 6569374Seric continue; 6579374Seric } 6583149Seric 6599374Seric rvp = rwr->r_rhs; 6609374Seric if (tTd(21, 12)) 6619374Seric { 6629374Seric printf("-----rule matches:"); 6639374Seric printav(rvp); 6649374Seric } 6659374Seric 6669374Seric rp = *rvp; 6679374Seric if (*rp == CANONUSER) 6689374Seric { 6699374Seric rvp++; 6709374Seric rwr = rwr->r_next; 6719374Seric } 6729374Seric else if (*rp == CANONHOST) 6739374Seric { 6749374Seric rvp++; 6759374Seric rwr = NULL; 6769374Seric } 6779374Seric else if (*rp == CANONNET) 6789374Seric rwr = NULL; 6799374Seric 6809374Seric /* substitute */ 6819374Seric for (avp = npvp; *rvp != NULL; rvp++) 6829374Seric { 6839374Seric register struct match *m; 6849374Seric register char **pp; 6859374Seric 6868058Seric rp = *rvp; 68716914Seric if (*rp == MATCHREPL) 6888058Seric { 68916914Seric /* substitute from LHS */ 69016914Seric m = &mlist[rp[1] - '1']; 69116914Seric if (m >= mlp) 6929374Seric { 69316914Seric syserr("rewrite: ruleset %d: replacement out of bounds", ruleset); 6949374Seric return; 6959374Seric } 69616914Seric if (tTd(21, 15)) 69716914Seric { 69816914Seric printf("$%c:", rp[1]); 69916914Seric pp = m->first; 70016914Seric while (pp <= m->last) 70116914Seric { 70216914Seric printf(" %x=\"", *pp); 70316914Seric (void) fflush(stdout); 70416914Seric printf("%s\"", *pp++); 70516914Seric } 70616914Seric printf("\n"); 70716914Seric } 7089374Seric pp = m->first; 7099374Seric while (pp <= m->last) 7103149Seric { 71116914Seric if (avp >= &npvp[MAXATOM]) 71216914Seric { 71316914Seric syserr("rewrite: expansion too long"); 71416914Seric return; 71516914Seric } 71616914Seric *avp++ = *pp++; 7173149Seric } 7183149Seric } 71916914Seric else 7208226Seric { 72116914Seric /* vanilla replacement */ 7229374Seric if (avp >= &npvp[MAXATOM]) 72316889Seric { 72416914Seric toolong: 72516889Seric syserr("rewrite: expansion too long"); 72616889Seric return; 72716889Seric } 72816914Seric *avp++ = rp; 7298226Seric } 7309374Seric } 7319374Seric *avp++ = NULL; 73216914Seric 73316914Seric /* 73416914Seric ** Check for any hostname lookups. 73516914Seric */ 73616914Seric 73716914Seric for (rvp = npvp; *rvp != NULL; rvp++) 73816914Seric { 73916914Seric char **hbrvp; 74016914Seric char **xpvp; 74116914Seric int trsize; 74217473Seric char *olddelimchar; 74316920Seric char buf[MAXNAME + 1]; 74416914Seric char *pvpb1[MAXATOM + 1]; 74517174Seric char pvpbuf[PSBUFSIZE]; 74617473Seric extern char *DelimChar; 74716914Seric 74816914Seric if (**rvp != HOSTBEGIN) 74916914Seric continue; 75016914Seric 75116914Seric /* 75216914Seric ** Got a hostname lookup. 75316914Seric ** 75416914Seric ** This could be optimized fairly easily. 75516914Seric */ 75616914Seric 75716914Seric hbrvp = rvp; 75816914Seric 75916914Seric /* extract the match part */ 76016914Seric while (*++rvp != NULL && **rvp != HOSTEND) 76116914Seric continue; 76216914Seric if (*rvp != NULL) 76316914Seric *rvp++ = NULL; 76416914Seric 76516914Seric /* save the remainder of the input string */ 76616914Seric trsize = (int) (avp - rvp + 1) * sizeof *rvp; 76716914Seric bcopy((char *) rvp, (char *) pvpb1, trsize); 76816914Seric 76916914Seric /* look it up */ 77016914Seric cataddr(++hbrvp, buf, sizeof buf); 77151317Seric if (maphostname(buf, sizeof buf - 1) && ConfigLevel >= 2) 77251317Seric { 77351317Seric register int i; 77416914Seric 77551317Seric /* it mapped -- mark it with a trailing dot */ 77651317Seric i = strlen(buf); 77751317Seric if (i > 0 && buf[i - 1] != '.') 77851317Seric { 77951317Seric buf[i++] = '.'; 78051317Seric buf[i] = '\0'; 78151317Seric } 78251317Seric } 78351317Seric 78416914Seric /* scan the new host name */ 78517473Seric olddelimchar = DelimChar; 78616914Seric xpvp = prescan(buf, '\0', pvpbuf); 78717473Seric DelimChar = olddelimchar; 78816914Seric if (xpvp == NULL) 78916914Seric { 79016914Seric syserr("rewrite: cannot prescan canonical hostname: %s", buf); 79122976Smiriam return; 79216914Seric } 79316914Seric 79416914Seric /* append it to the token list */ 79517174Seric for (avp = --hbrvp; *xpvp != NULL; xpvp++) 79617174Seric { 79717174Seric *avp++ = newstr(*xpvp); 79816920Seric if (avp >= &npvp[MAXATOM]) 79916914Seric goto toolong; 80017174Seric } 80116914Seric 80216914Seric /* restore the old trailing information */ 80317177Seric for (xpvp = pvpb1; (*avp++ = *xpvp++) != NULL; ) 80416920Seric if (avp >= &npvp[MAXATOM]) 80516914Seric goto toolong; 80617174Seric 80717174Seric break; 80816914Seric } 80916914Seric 81016914Seric /* 81116914Seric ** Check for subroutine calls. 81216914Seric */ 81316914Seric 81424944Seric if (*npvp != NULL && **npvp == CALLSUBR) 8159374Seric { 81616889Seric bcopy((char *) &npvp[2], (char *) pvp, 81716900Seric (int) (avp - npvp - 2) * sizeof *avp); 81816889Seric if (tTd(21, 3)) 81916889Seric printf("-----callsubr %s\n", npvp[1]); 82016889Seric rewrite(pvp, atoi(npvp[1])); 8213149Seric } 8223149Seric else 8233149Seric { 82417348Seric bcopy((char *) npvp, (char *) pvp, 82516900Seric (int) (avp - npvp) * sizeof *avp); 8269374Seric } 8279374Seric if (tTd(21, 4)) 8289374Seric { 8299374Seric printf("rewritten as:"); 8309374Seric printav(pvp); 8319374Seric } 832297Seric } 8338069Seric 8349279Seric if (OpMode == MD_TEST || tTd(21, 2)) 8358069Seric { 8368959Seric printf("rewrite: ruleset %2d returns:", ruleset); 8378069Seric printav(pvp); 8388069Seric } 8393149Seric } 8403149Seric /* 8413149Seric ** BUILDADDR -- build address from token vector. 8423149Seric ** 8433149Seric ** Parameters: 8443149Seric ** tv -- token vector. 8453149Seric ** a -- pointer to address descriptor to fill. 8463149Seric ** If NULL, one will be allocated. 8473149Seric ** 8483149Seric ** Returns: 8494279Seric ** NULL if there was an error. 8504279Seric ** 'a' otherwise. 8513149Seric ** 8523149Seric ** Side Effects: 8533149Seric ** fills in 'a' 8543149Seric */ 8553149Seric 8563149Seric ADDRESS * 8573149Seric buildaddr(tv, a) 8583149Seric register char **tv; 8593149Seric register ADDRESS *a; 8603149Seric { 8613149Seric static char buf[MAXNAME]; 8623149Seric struct mailer **mp; 8633149Seric register struct mailer *m; 8643149Seric 8653149Seric if (a == NULL) 8663149Seric a = (ADDRESS *) xalloc(sizeof *a); 86716889Seric bzero((char *) a, sizeof *a); 8683149Seric 8693149Seric /* figure out what net/mailer to use */ 8703149Seric if (**tv != CANONNET) 8714279Seric { 8723149Seric syserr("buildaddr: no net"); 8734279Seric return (NULL); 8744279Seric } 8753149Seric tv++; 87633725Sbostic if (!strcasecmp(*tv, "error")) 8774279Seric { 87810183Seric if (**++tv == CANONHOST) 87910183Seric { 88010183Seric setstat(atoi(*++tv)); 88110183Seric tv++; 88210183Seric } 88310183Seric if (**tv != CANONUSER) 8844279Seric syserr("buildaddr: error: no user"); 8854279Seric buf[0] = '\0'; 8864279Seric while (*++tv != NULL) 8874279Seric { 8884279Seric if (buf[0] != '\0') 8897005Seric (void) strcat(buf, " "); 8907005Seric (void) strcat(buf, *tv); 8914279Seric } 8924279Seric usrerr(buf); 8934279Seric return (NULL); 8944279Seric } 8954598Seric for (mp = Mailer; (m = *mp++) != NULL; ) 8963149Seric { 89733725Sbostic if (!strcasecmp(m->m_name, *tv)) 8983149Seric break; 8993149Seric } 9003149Seric if (m == NULL) 9014279Seric { 90224944Seric syserr("buildaddr: unknown mailer %s", *tv); 9034279Seric return (NULL); 9044279Seric } 9054598Seric a->q_mailer = m; 9063149Seric 9073149Seric /* figure out what host (if any) */ 9083149Seric tv++; 90910690Seric if (!bitnset(M_LOCAL, m->m_flags)) 9103149Seric { 9115704Seric if (**tv++ != CANONHOST) 9124279Seric { 9133149Seric syserr("buildaddr: no host"); 9144279Seric return (NULL); 9154279Seric } 9165704Seric buf[0] = '\0'; 9175704Seric while (*tv != NULL && **tv != CANONUSER) 9187005Seric (void) strcat(buf, *tv++); 9195704Seric a->q_host = newstr(buf); 9203149Seric } 9213149Seric else 9223149Seric a->q_host = NULL; 9233149Seric 9243149Seric /* figure out the user */ 92536615Sbostic if (*tv == NULL || **tv != CANONUSER) 9264279Seric { 9273149Seric syserr("buildaddr: no user"); 9284279Seric return (NULL); 9294279Seric } 93019040Seric 93151317Seric if (m == LocalMailer && tv[1] != NULL && strcmp(tv[1], ":") == 0) 93251317Seric { 93351317Seric tv++; 93451317Seric a->q_flags |= QNOTREMOTE; 93551317Seric } 93651317Seric 93719040Seric /* rewrite according recipient mailer rewriting rules */ 93819040Seric rewrite(++tv, 2); 93919040Seric if (m->m_r_rwset > 0) 94019040Seric rewrite(tv, m->m_r_rwset); 94119040Seric rewrite(tv, 4); 94219040Seric 94319040Seric /* save the result for the command line/RCPT argument */ 94411278Seric cataddr(tv, buf, sizeof buf); 9453149Seric a->q_user = buf; 9463149Seric 9473149Seric return (a); 9483149Seric } 9493188Seric /* 9504228Seric ** CATADDR -- concatenate pieces of addresses (putting in <LWSP> subs) 9514228Seric ** 9524228Seric ** Parameters: 9534228Seric ** pvp -- parameter vector to rebuild. 9544228Seric ** buf -- buffer to build the string into. 9554228Seric ** sz -- size of buf. 9564228Seric ** 9574228Seric ** Returns: 9584228Seric ** none. 9594228Seric ** 9604228Seric ** Side Effects: 9614228Seric ** Destroys buf. 9624228Seric */ 9634228Seric 9644228Seric cataddr(pvp, buf, sz) 9654228Seric char **pvp; 9664228Seric char *buf; 9674228Seric register int sz; 9684228Seric { 9694228Seric bool oatomtok = FALSE; 9704228Seric bool natomtok = FALSE; 9714228Seric register int i; 9724228Seric register char *p; 9734228Seric 9748423Seric if (pvp == NULL) 9758423Seric { 97623109Seric (void) strcpy(buf, ""); 9778423Seric return; 9788423Seric } 9794228Seric p = buf; 98011156Seric sz -= 2; 9814228Seric while (*pvp != NULL && (i = strlen(*pvp)) < sz) 9824228Seric { 9838078Seric natomtok = (toktype(**pvp) == ATM); 9844228Seric if (oatomtok && natomtok) 9859042Seric *p++ = SpaceSub; 9864228Seric (void) strcpy(p, *pvp); 9874228Seric oatomtok = natomtok; 9884228Seric p += i; 98911156Seric sz -= i + 1; 9904228Seric pvp++; 9914228Seric } 9924228Seric *p = '\0'; 9934228Seric } 9944228Seric /* 9953188Seric ** SAMEADDR -- Determine if two addresses are the same 9963188Seric ** 9973188Seric ** This is not just a straight comparison -- if the mailer doesn't 9983188Seric ** care about the host we just ignore it, etc. 9993188Seric ** 10003188Seric ** Parameters: 10013188Seric ** a, b -- pointers to the internal forms to compare. 10023188Seric ** 10033188Seric ** Returns: 10043188Seric ** TRUE -- they represent the same mailbox. 10053188Seric ** FALSE -- they don't. 10063188Seric ** 10073188Seric ** Side Effects: 10083188Seric ** none. 10093188Seric */ 10103188Seric 10113188Seric bool 10129374Seric sameaddr(a, b) 10133188Seric register ADDRESS *a; 10143188Seric register ADDRESS *b; 10153188Seric { 10163188Seric /* if they don't have the same mailer, forget it */ 10173188Seric if (a->q_mailer != b->q_mailer) 10183188Seric return (FALSE); 10193188Seric 10203188Seric /* if the user isn't the same, we can drop out */ 10219374Seric if (strcmp(a->q_user, b->q_user) != 0) 10223188Seric return (FALSE); 10233188Seric 10243188Seric /* if the mailer ignores hosts, we have succeeded! */ 102510690Seric if (bitnset(M_LOCAL, a->q_mailer->m_flags)) 10263188Seric return (TRUE); 10273188Seric 10283188Seric /* otherwise compare hosts (but be careful for NULL ptrs) */ 10293188Seric if (a->q_host == NULL || b->q_host == NULL) 10303188Seric return (FALSE); 10313188Seric if (strcmp(a->q_host, b->q_host) != 0) 10323188Seric return (FALSE); 10333188Seric 10343188Seric return (TRUE); 10353188Seric } 10363234Seric /* 10373234Seric ** PRINTADDR -- print address (for debugging) 10383234Seric ** 10393234Seric ** Parameters: 10403234Seric ** a -- the address to print 10413234Seric ** follow -- follow the q_next chain. 10423234Seric ** 10433234Seric ** Returns: 10443234Seric ** none. 10453234Seric ** 10463234Seric ** Side Effects: 10473234Seric ** none. 10483234Seric */ 10493234Seric 10503234Seric printaddr(a, follow) 10513234Seric register ADDRESS *a; 10523234Seric bool follow; 10533234Seric { 10545001Seric bool first = TRUE; 10555001Seric 10563234Seric while (a != NULL) 10573234Seric { 10585001Seric first = FALSE; 10594443Seric printf("%x=", a); 10604085Seric (void) fflush(stdout); 106140973Sbostic printf("%s: mailer %d (%s), host `%s', user `%s', ruser `%s'\n", 106240973Sbostic a->q_paddr, a->q_mailer->m_mno, a->q_mailer->m_name, 106340973Sbostic a->q_host, a->q_user, a->q_ruser? a->q_ruser: "<null>"); 10648181Seric printf("\tnext=%x, flags=%o, alias %x\n", a->q_next, a->q_flags, 10658181Seric a->q_alias); 10668181Seric printf("\thome=\"%s\", fullname=\"%s\"\n", a->q_home, 10678181Seric a->q_fullname); 10684996Seric 10693234Seric if (!follow) 10703234Seric return; 10714996Seric a = a->q_next; 10723234Seric } 10735001Seric if (first) 10744443Seric printf("[NULL]\n"); 10753234Seric } 10764317Seric 10777682Seric /* 10787682Seric ** REMOTENAME -- return the name relative to the current mailer 10797682Seric ** 10807682Seric ** Parameters: 10817682Seric ** name -- the name to translate. 10828069Seric ** m -- the mailer that we want to do rewriting relative 10838069Seric ** to. 10848069Seric ** senderaddress -- if set, uses the sender rewriting rules 10858069Seric ** rather than the recipient rewriting rules. 108610310Seric ** canonical -- if set, strip out any comment information, 108710310Seric ** etc. 10887682Seric ** 10897682Seric ** Returns: 10907682Seric ** the text string representing this address relative to 10917682Seric ** the receiving mailer. 10927682Seric ** 10937682Seric ** Side Effects: 10947682Seric ** none. 10957682Seric ** 10967682Seric ** Warnings: 10977682Seric ** The text string returned is tucked away locally; 10987682Seric ** copy it if you intend to save it. 10997682Seric */ 11007682Seric 11017682Seric char * 110210310Seric remotename(name, m, senderaddress, canonical) 11037682Seric char *name; 11047682Seric struct mailer *m; 11058069Seric bool senderaddress; 110610310Seric bool canonical; 11077682Seric { 11088069Seric register char **pvp; 11098069Seric char *fancy; 11108069Seric extern char *macvalue(); 11118181Seric char *oldg = macvalue('g', CurEnv); 11127682Seric static char buf[MAXNAME]; 11137682Seric char lbuf[MAXNAME]; 111416914Seric char pvpbuf[PSBUFSIZE]; 11157682Seric extern char **prescan(); 11167889Seric extern char *crackaddr(); 11177682Seric 11187755Seric if (tTd(12, 1)) 11197755Seric printf("remotename(%s)\n", name); 11207755Seric 112110177Seric /* don't do anything if we are tagging it as special */ 112210177Seric if ((senderaddress ? m->m_s_rwset : m->m_r_rwset) < 0) 112310177Seric return (name); 112410177Seric 11257682Seric /* 11268181Seric ** Do a heuristic crack of this name to extract any comment info. 11278181Seric ** This will leave the name as a comment and a $g macro. 11287889Seric */ 11297889Seric 113010310Seric if (canonical) 113116155Seric fancy = "\001g"; 113210310Seric else 113310310Seric fancy = crackaddr(name); 11347889Seric 11358181Seric /* 11368181Seric ** Turn the name into canonical form. 11378181Seric ** Normally this will be RFC 822 style, i.e., "user@domain". 11388181Seric ** If this only resolves to "user", and the "C" flag is 11398181Seric ** specified in the sending mailer, then the sender's 11408181Seric ** domain will be appended. 11418181Seric */ 11428181Seric 114316914Seric pvp = prescan(name, '\0', pvpbuf); 11447889Seric if (pvp == NULL) 11457889Seric return (name); 11468181Seric rewrite(pvp, 3); 11478181Seric if (CurEnv->e_fromdomain != NULL) 11488181Seric { 11498181Seric /* append from domain to this address */ 11508181Seric register char **pxp = pvp; 11518181Seric 11529594Seric /* see if there is an "@domain" in the current name */ 11538181Seric while (*pxp != NULL && strcmp(*pxp, "@") != 0) 11548181Seric pxp++; 11558181Seric if (*pxp == NULL) 11568181Seric { 11579594Seric /* no.... append the "@domain" from the sender */ 11588181Seric register char **qxq = CurEnv->e_fromdomain; 11598181Seric 11609594Seric while ((*pxp++ = *qxq++) != NULL) 11619594Seric continue; 116211726Seric rewrite(pvp, 3); 11638181Seric } 11648181Seric } 11658181Seric 11668181Seric /* 11678959Seric ** Do more specific rewriting. 11688181Seric ** Rewrite using ruleset 1 or 2 depending on whether this is 11698181Seric ** a sender address or not. 11708181Seric ** Then run it through any receiving-mailer-specific rulesets. 11718181Seric */ 11728181Seric 11738069Seric if (senderaddress) 11747755Seric { 11757889Seric rewrite(pvp, 1); 11768069Seric if (m->m_s_rwset > 0) 11778069Seric rewrite(pvp, m->m_s_rwset); 11788069Seric } 11798069Seric else 11808069Seric { 11817889Seric rewrite(pvp, 2); 11828069Seric if (m->m_r_rwset > 0) 11838069Seric rewrite(pvp, m->m_r_rwset); 11847682Seric } 11857682Seric 11868181Seric /* 11878959Seric ** Do any final sanitation the address may require. 11888959Seric ** This will normally be used to turn internal forms 11898959Seric ** (e.g., user@host.LOCAL) into external form. This 11908959Seric ** may be used as a default to the above rules. 11918959Seric */ 11928959Seric 11938959Seric rewrite(pvp, 4); 11948959Seric 11958959Seric /* 11968181Seric ** Now restore the comment information we had at the beginning. 11978181Seric */ 11988181Seric 11997682Seric cataddr(pvp, lbuf, sizeof lbuf); 12009374Seric define('g', lbuf, CurEnv); 12017889Seric expand(fancy, buf, &buf[sizeof buf - 1], CurEnv); 12029374Seric define('g', oldg, CurEnv); 12037682Seric 12047682Seric if (tTd(12, 1)) 12057755Seric printf("remotename => `%s'\n", buf); 12067682Seric return (buf); 12077682Seric } 120851317Seric /* 120951317Seric ** MAPLOCALUSER -- run local username through ruleset 5 for final redirection 121051317Seric ** 121151317Seric ** Parameters: 121251317Seric ** a -- the address to map (but just the user name part). 121351317Seric ** sendq -- the sendq in which to install any replacement 121451317Seric ** addresses. 121551317Seric ** 121651317Seric ** Returns: 121751317Seric ** none. 121851317Seric */ 121951317Seric 122051317Seric maplocaluser(a, sendq) 122151317Seric register ADDRESS *a; 122251317Seric ADDRESS **sendq; 122351317Seric { 122451317Seric register char **pvp; 122551317Seric register ADDRESS *a1 = NULL; 122651317Seric char pvpbuf[PSBUFSIZE]; 122751317Seric 122851317Seric if (tTd(29, 1)) 122951317Seric { 123051317Seric printf("maplocaluser: "); 123151317Seric printaddr(a, FALSE); 123251317Seric } 123351317Seric pvp = prescan(a->q_user, '\0', pvpbuf); 123451317Seric if (pvp == NULL) 123551317Seric return; 123651317Seric 123751317Seric rewrite(pvp, 5); 123851317Seric if (pvp[0] == NULL || pvp[0][0] != CANONNET) 123951317Seric return; 124051317Seric 124151317Seric /* if non-null, mailer destination specified -- has it changed? */ 124251317Seric a1 = buildaddr(pvp, NULL); 124351317Seric if (a1 == NULL || sameaddr(a, a1)) 124451317Seric return; 124551317Seric 124651317Seric /* mark old address as dead; insert new address */ 124751317Seric a->q_flags |= QDONTSEND; 124851317Seric a1->q_alias = a; 124951317Seric allocaddr(a1, 1, NULL); 125051317Seric (void) recipient(a1, sendq); 125151317Seric } 1252