13312Seric # include "sendmail.h" 2297Seric 3*16914Seric SCCSID(@(#)parseaddr.c 4.9 08/11/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; 55*16914Seric 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 69*16914Seric 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. 191*16914Seric ** pvpbuf -- place to put the saved text -- note that 192*16914Seric ** 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 ** 232*16914Seric prescan(addr, delim, pvpbuf) 233297Seric char *addr; 234297Seric char delim; 235*16914Seric 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 253*16914Seric 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 */ 280*16914Seric 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; 664*16914Seric if (*rp == MATCHREPL) 6658058Seric { 666*16914Seric /* substitute from LHS */ 667*16914Seric m = &mlist[rp[1] - '1']; 668*16914Seric if (m >= mlp) 6699374Seric { 670*16914Seric syserr("rewrite: ruleset %d: replacement out of bounds", ruleset); 6719374Seric return; 6729374Seric } 6739374Seric # ifdef DEBUG 674*16914Seric if (tTd(21, 15)) 675*16914Seric { 676*16914Seric printf("$%c:", rp[1]); 677*16914Seric pp = m->first; 678*16914Seric while (pp <= m->last) 679*16914Seric { 680*16914Seric printf(" %x=\"", *pp); 681*16914Seric (void) fflush(stdout); 682*16914Seric printf("%s\"", *pp++); 683*16914Seric } 684*16914Seric printf("\n"); 685*16914Seric } 686*16914Seric # endif DEBUG 6879374Seric pp = m->first; 6889374Seric while (pp <= m->last) 6893149Seric { 690*16914Seric if (avp >= &npvp[MAXATOM]) 691*16914Seric { 692*16914Seric syserr("rewrite: expansion too long"); 693*16914Seric return; 694*16914Seric } 695*16914Seric *avp++ = *pp++; 6963149Seric } 6973149Seric } 698*16914Seric else 6998226Seric { 700*16914Seric /* vanilla replacement */ 7019374Seric if (avp >= &npvp[MAXATOM]) 70216889Seric { 703*16914Seric toolong: 70416889Seric syserr("rewrite: expansion too long"); 70516889Seric return; 70616889Seric } 707*16914Seric *avp++ = rp; 7088226Seric } 7099374Seric } 7109374Seric *avp++ = NULL; 711*16914Seric 712*16914Seric /* 713*16914Seric ** Check for any hostname lookups. 714*16914Seric */ 715*16914Seric 716*16914Seric for (rvp = npvp; *rvp != NULL; rvp++) 717*16914Seric { 718*16914Seric char **hbrvp; 719*16914Seric char **xpvp; 720*16914Seric int trsize; 721*16914Seric int i; 722*16914Seric char buf[MAXATOM + 1]; 723*16914Seric char *pvpb1[MAXATOM + 1]; 724*16914Seric static char pvpbuf[PSBUFSIZE]; 725*16914Seric 726*16914Seric if (**rvp != HOSTBEGIN) 727*16914Seric continue; 728*16914Seric 729*16914Seric /* 730*16914Seric ** Got a hostname lookup. 731*16914Seric ** 732*16914Seric ** This could be optimized fairly easily. 733*16914Seric */ 734*16914Seric 735*16914Seric hbrvp = rvp; 736*16914Seric 737*16914Seric /* extract the match part */ 738*16914Seric while (*++rvp != NULL && **rvp != HOSTEND) 739*16914Seric continue; 740*16914Seric if (*rvp != NULL) 741*16914Seric *rvp++ = NULL; 742*16914Seric 743*16914Seric /* save the remainder of the input string */ 744*16914Seric trsize = (int) (avp - rvp + 1) * sizeof *rvp; 745*16914Seric bcopy((char *) rvp, (char *) pvpb1, trsize); 746*16914Seric 747*16914Seric /* look it up */ 748*16914Seric cataddr(++hbrvp, buf, sizeof buf); 749*16914Seric maphostname(buf, sizeof buf); 750*16914Seric 751*16914Seric /* scan the new host name */ 752*16914Seric xpvp = prescan(buf, '\0', pvpbuf); 753*16914Seric if (xpvp == NULL) 754*16914Seric { 755*16914Seric syserr("rewrite: cannot prescan canonical hostname: %s", buf); 756*16914Seric return (NULL); 757*16914Seric } 758*16914Seric 759*16914Seric /* append it to the token list */ 760*16914Seric rvp = --hbrvp; 761*16914Seric while ((*rvp++ = *xpvp++) != NULL) 762*16914Seric if (rvp >= &npvp[MAXATOM]) 763*16914Seric goto toolong; 764*16914Seric 765*16914Seric /* restore the old trailing information */ 766*16914Seric for (xpvp = pvpb1, rvp--; (*rvp++ = *xpvp++) != NULL; ) 767*16914Seric if (rvp >= &npvp[MAXATOM]) 768*16914Seric goto toolong; 769*16914Seric } 770*16914Seric 771*16914Seric /* 772*16914Seric ** Check for subroutine calls. 773*16914Seric */ 774*16914Seric 77516889Seric if (**npvp == CALLSUBR) 7769374Seric { 77716889Seric bcopy((char *) &npvp[2], (char *) pvp, 77816900Seric (int) (avp - npvp - 2) * sizeof *avp); 77916889Seric # ifdef DEBUG 78016889Seric if (tTd(21, 3)) 78116889Seric printf("-----callsubr %s\n", npvp[1]); 78216889Seric # endif DEBUG 78316889Seric rewrite(pvp, atoi(npvp[1])); 7843149Seric } 7853149Seric else 7863149Seric { 787*16914Seric bmove((char *) npvp, (char *) pvp, 78816900Seric (int) (avp - npvp) * sizeof *avp); 7899374Seric } 7904100Seric # ifdef DEBUG 7919374Seric if (tTd(21, 4)) 7929374Seric { 7939374Seric printf("rewritten as:"); 7949374Seric printav(pvp); 7959374Seric } 7964100Seric # endif DEBUG 797297Seric } 7988069Seric 7999279Seric if (OpMode == MD_TEST || tTd(21, 2)) 8008069Seric { 8018959Seric printf("rewrite: ruleset %2d returns:", ruleset); 8028069Seric printav(pvp); 8038069Seric } 8043149Seric } 8053149Seric /* 8063149Seric ** BUILDADDR -- build address from token vector. 8073149Seric ** 8083149Seric ** Parameters: 8093149Seric ** tv -- token vector. 8103149Seric ** a -- pointer to address descriptor to fill. 8113149Seric ** If NULL, one will be allocated. 8123149Seric ** 8133149Seric ** Returns: 8144279Seric ** NULL if there was an error. 8154279Seric ** 'a' otherwise. 8163149Seric ** 8173149Seric ** Side Effects: 8183149Seric ** fills in 'a' 8193149Seric */ 8203149Seric 8213149Seric ADDRESS * 8223149Seric buildaddr(tv, a) 8233149Seric register char **tv; 8243149Seric register ADDRESS *a; 8253149Seric { 8263149Seric static char buf[MAXNAME]; 8273149Seric struct mailer **mp; 8283149Seric register struct mailer *m; 8294635Seric extern bool sameword(); 8303149Seric 8313149Seric if (a == NULL) 8323149Seric a = (ADDRESS *) xalloc(sizeof *a); 83316889Seric bzero((char *) a, sizeof *a); 8343149Seric 8353149Seric /* figure out what net/mailer to use */ 8363149Seric if (**tv != CANONNET) 8374279Seric { 8383149Seric syserr("buildaddr: no net"); 8394279Seric return (NULL); 8404279Seric } 8413149Seric tv++; 8424635Seric if (sameword(*tv, "error")) 8434279Seric { 84410183Seric if (**++tv == CANONHOST) 84510183Seric { 84610183Seric setstat(atoi(*++tv)); 84710183Seric tv++; 84810183Seric } 84910183Seric if (**tv != CANONUSER) 8504279Seric syserr("buildaddr: error: no user"); 8514279Seric buf[0] = '\0'; 8524279Seric while (*++tv != NULL) 8534279Seric { 8544279Seric if (buf[0] != '\0') 8557005Seric (void) strcat(buf, " "); 8567005Seric (void) strcat(buf, *tv); 8574279Seric } 8584279Seric usrerr(buf); 8594279Seric return (NULL); 8604279Seric } 8614598Seric for (mp = Mailer; (m = *mp++) != NULL; ) 8623149Seric { 8634635Seric if (sameword(m->m_name, *tv)) 8643149Seric break; 8653149Seric } 8663149Seric if (m == NULL) 8674279Seric { 8683149Seric syserr("buildaddr: unknown net %s", *tv); 8694279Seric return (NULL); 8704279Seric } 8714598Seric a->q_mailer = m; 8723149Seric 8733149Seric /* figure out what host (if any) */ 8743149Seric tv++; 87510690Seric if (!bitnset(M_LOCAL, m->m_flags)) 8763149Seric { 8775704Seric if (**tv++ != CANONHOST) 8784279Seric { 8793149Seric syserr("buildaddr: no host"); 8804279Seric return (NULL); 8814279Seric } 8825704Seric buf[0] = '\0'; 8835704Seric while (*tv != NULL && **tv != CANONUSER) 8847005Seric (void) strcat(buf, *tv++); 8855704Seric a->q_host = newstr(buf); 8863149Seric } 8873149Seric else 8883149Seric a->q_host = NULL; 8893149Seric 8903149Seric /* figure out the user */ 8913149Seric if (**tv != CANONUSER) 8924279Seric { 8933149Seric syserr("buildaddr: no user"); 8944279Seric return (NULL); 8954279Seric } 89611278Seric rewrite(++tv, 4); 89711278Seric cataddr(tv, buf, sizeof buf); 8983149Seric a->q_user = buf; 8993149Seric 9003149Seric return (a); 9013149Seric } 9023188Seric /* 9034228Seric ** CATADDR -- concatenate pieces of addresses (putting in <LWSP> subs) 9044228Seric ** 9054228Seric ** Parameters: 9064228Seric ** pvp -- parameter vector to rebuild. 9074228Seric ** buf -- buffer to build the string into. 9084228Seric ** sz -- size of buf. 9094228Seric ** 9104228Seric ** Returns: 9114228Seric ** none. 9124228Seric ** 9134228Seric ** Side Effects: 9144228Seric ** Destroys buf. 9154228Seric */ 9164228Seric 9174228Seric cataddr(pvp, buf, sz) 9184228Seric char **pvp; 9194228Seric char *buf; 9204228Seric register int sz; 9214228Seric { 9224228Seric bool oatomtok = FALSE; 9234228Seric bool natomtok = FALSE; 9244228Seric register int i; 9254228Seric register char *p; 9264228Seric 9278423Seric if (pvp == NULL) 9288423Seric { 9298423Seric strcpy(buf, ""); 9308423Seric return; 9318423Seric } 9324228Seric p = buf; 93311156Seric sz -= 2; 9344228Seric while (*pvp != NULL && (i = strlen(*pvp)) < sz) 9354228Seric { 9368078Seric natomtok = (toktype(**pvp) == ATM); 9374228Seric if (oatomtok && natomtok) 9389042Seric *p++ = SpaceSub; 9394228Seric (void) strcpy(p, *pvp); 9404228Seric oatomtok = natomtok; 9414228Seric p += i; 94211156Seric sz -= i + 1; 9434228Seric pvp++; 9444228Seric } 9454228Seric *p = '\0'; 9464228Seric } 9474228Seric /* 9483188Seric ** SAMEADDR -- Determine if two addresses are the same 9493188Seric ** 9503188Seric ** This is not just a straight comparison -- if the mailer doesn't 9513188Seric ** care about the host we just ignore it, etc. 9523188Seric ** 9533188Seric ** Parameters: 9543188Seric ** a, b -- pointers to the internal forms to compare. 9553188Seric ** 9563188Seric ** Returns: 9573188Seric ** TRUE -- they represent the same mailbox. 9583188Seric ** FALSE -- they don't. 9593188Seric ** 9603188Seric ** Side Effects: 9613188Seric ** none. 9623188Seric */ 9633188Seric 9643188Seric bool 9659374Seric sameaddr(a, b) 9663188Seric register ADDRESS *a; 9673188Seric register ADDRESS *b; 9683188Seric { 9693188Seric /* if they don't have the same mailer, forget it */ 9703188Seric if (a->q_mailer != b->q_mailer) 9713188Seric return (FALSE); 9723188Seric 9733188Seric /* if the user isn't the same, we can drop out */ 9749374Seric if (strcmp(a->q_user, b->q_user) != 0) 9753188Seric return (FALSE); 9763188Seric 9773188Seric /* if the mailer ignores hosts, we have succeeded! */ 97810690Seric if (bitnset(M_LOCAL, a->q_mailer->m_flags)) 9793188Seric return (TRUE); 9803188Seric 9813188Seric /* otherwise compare hosts (but be careful for NULL ptrs) */ 9823188Seric if (a->q_host == NULL || b->q_host == NULL) 9833188Seric return (FALSE); 9843188Seric if (strcmp(a->q_host, b->q_host) != 0) 9853188Seric return (FALSE); 9863188Seric 9873188Seric return (TRUE); 9883188Seric } 9893234Seric /* 9903234Seric ** PRINTADDR -- print address (for debugging) 9913234Seric ** 9923234Seric ** Parameters: 9933234Seric ** a -- the address to print 9943234Seric ** follow -- follow the q_next chain. 9953234Seric ** 9963234Seric ** Returns: 9973234Seric ** none. 9983234Seric ** 9993234Seric ** Side Effects: 10003234Seric ** none. 10013234Seric */ 10023234Seric 10034317Seric # ifdef DEBUG 10044317Seric 10053234Seric printaddr(a, follow) 10063234Seric register ADDRESS *a; 10073234Seric bool follow; 10083234Seric { 10095001Seric bool first = TRUE; 10105001Seric 10113234Seric while (a != NULL) 10123234Seric { 10135001Seric first = FALSE; 10144443Seric printf("%x=", a); 10154085Seric (void) fflush(stdout); 10163234Seric printf("%s: mailer %d (%s), host `%s', user `%s'\n", a->q_paddr, 10178181Seric a->q_mailer->m_mno, a->q_mailer->m_name, a->q_host, 10188181Seric a->q_user); 10198181Seric printf("\tnext=%x, flags=%o, alias %x\n", a->q_next, a->q_flags, 10208181Seric a->q_alias); 10218181Seric printf("\thome=\"%s\", fullname=\"%s\"\n", a->q_home, 10228181Seric a->q_fullname); 10234996Seric 10243234Seric if (!follow) 10253234Seric return; 10264996Seric a = a->q_next; 10273234Seric } 10285001Seric if (first) 10294443Seric printf("[NULL]\n"); 10303234Seric } 10314317Seric 10324317Seric # endif DEBUG 10337682Seric /* 10347682Seric ** REMOTENAME -- return the name relative to the current mailer 10357682Seric ** 10367682Seric ** Parameters: 10377682Seric ** name -- the name to translate. 10388069Seric ** m -- the mailer that we want to do rewriting relative 10398069Seric ** to. 10408069Seric ** senderaddress -- if set, uses the sender rewriting rules 10418069Seric ** rather than the recipient rewriting rules. 104210310Seric ** canonical -- if set, strip out any comment information, 104310310Seric ** etc. 10447682Seric ** 10457682Seric ** Returns: 10467682Seric ** the text string representing this address relative to 10477682Seric ** the receiving mailer. 10487682Seric ** 10497682Seric ** Side Effects: 10507682Seric ** none. 10517682Seric ** 10527682Seric ** Warnings: 10537682Seric ** The text string returned is tucked away locally; 10547682Seric ** copy it if you intend to save it. 10557682Seric */ 10567682Seric 10577682Seric char * 105810310Seric remotename(name, m, senderaddress, canonical) 10597682Seric char *name; 10607682Seric struct mailer *m; 10618069Seric bool senderaddress; 106210310Seric bool canonical; 10637682Seric { 10648069Seric register char **pvp; 10658069Seric char *fancy; 106615284Seric register char *p; 10678069Seric extern char *macvalue(); 10688181Seric char *oldg = macvalue('g', CurEnv); 10697682Seric static char buf[MAXNAME]; 10707682Seric char lbuf[MAXNAME]; 1071*16914Seric char pvpbuf[PSBUFSIZE]; 10727682Seric extern char **prescan(); 10737889Seric extern char *crackaddr(); 10747682Seric 10757755Seric # ifdef DEBUG 10767755Seric if (tTd(12, 1)) 10777755Seric printf("remotename(%s)\n", name); 10787755Seric # endif DEBUG 10797755Seric 108010177Seric /* don't do anything if we are tagging it as special */ 108110177Seric if ((senderaddress ? m->m_s_rwset : m->m_r_rwset) < 0) 108210177Seric return (name); 108310177Seric 10847682Seric /* 10858181Seric ** Do a heuristic crack of this name to extract any comment info. 10868181Seric ** This will leave the name as a comment and a $g macro. 10877889Seric */ 10887889Seric 108910310Seric if (canonical) 109016155Seric fancy = "\001g"; 109110310Seric else 109210310Seric fancy = crackaddr(name); 10937889Seric 10948181Seric /* 10958181Seric ** Turn the name into canonical form. 10968181Seric ** Normally this will be RFC 822 style, i.e., "user@domain". 10978181Seric ** If this only resolves to "user", and the "C" flag is 10988181Seric ** specified in the sending mailer, then the sender's 10998181Seric ** domain will be appended. 11008181Seric */ 11018181Seric 1102*16914Seric pvp = prescan(name, '\0', pvpbuf); 11037889Seric if (pvp == NULL) 11047889Seric return (name); 11058181Seric rewrite(pvp, 3); 11068181Seric if (CurEnv->e_fromdomain != NULL) 11078181Seric { 11088181Seric /* append from domain to this address */ 11098181Seric register char **pxp = pvp; 11108181Seric 11119594Seric /* see if there is an "@domain" in the current name */ 11128181Seric while (*pxp != NULL && strcmp(*pxp, "@") != 0) 11138181Seric pxp++; 11148181Seric if (*pxp == NULL) 11158181Seric { 11169594Seric /* no.... append the "@domain" from the sender */ 11178181Seric register char **qxq = CurEnv->e_fromdomain; 11188181Seric 11199594Seric while ((*pxp++ = *qxq++) != NULL) 11209594Seric continue; 112111726Seric rewrite(pvp, 3); 11228181Seric } 11238181Seric } 11248181Seric 11258181Seric /* 11268959Seric ** Do more specific rewriting. 11278181Seric ** Rewrite using ruleset 1 or 2 depending on whether this is 11288181Seric ** a sender address or not. 11298181Seric ** Then run it through any receiving-mailer-specific rulesets. 11308181Seric */ 11318181Seric 11328069Seric if (senderaddress) 11337755Seric { 11347889Seric rewrite(pvp, 1); 11358069Seric if (m->m_s_rwset > 0) 11368069Seric rewrite(pvp, m->m_s_rwset); 11378069Seric } 11388069Seric else 11398069Seric { 11407889Seric rewrite(pvp, 2); 11418069Seric if (m->m_r_rwset > 0) 11428069Seric rewrite(pvp, m->m_r_rwset); 11437682Seric } 11447682Seric 11458181Seric /* 11468959Seric ** Do any final sanitation the address may require. 11478959Seric ** This will normally be used to turn internal forms 11488959Seric ** (e.g., user@host.LOCAL) into external form. This 11498959Seric ** may be used as a default to the above rules. 11508959Seric */ 11518959Seric 11528959Seric rewrite(pvp, 4); 11538959Seric 11548959Seric /* 11558181Seric ** Now restore the comment information we had at the beginning. 11568181Seric */ 11578181Seric 11587682Seric cataddr(pvp, lbuf, sizeof lbuf); 11599374Seric define('g', lbuf, CurEnv); 11607889Seric expand(fancy, buf, &buf[sizeof buf - 1], CurEnv); 11619374Seric define('g', oldg, CurEnv); 11627682Seric 11637682Seric # ifdef DEBUG 11647682Seric if (tTd(12, 1)) 11657755Seric printf("remotename => `%s'\n", buf); 11667682Seric # endif DEBUG 11677682Seric return (buf); 11687682Seric } 1169