13312Seric # include "sendmail.h" 2297Seric 3*8226Seric SCCSID(@(#)parseaddr.c 3.57 09/16/82); 4407Seric 5297Seric /* 6297Seric ** PARSE -- 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. 31297Seric ** 32297Seric ** Returns: 33297Seric ** A pointer to the address descriptor header (`a' if 34297Seric ** `a' is non-NULL). 35297Seric ** NULL on error. 36297Seric ** 37297Seric ** Side Effects: 38297Seric ** none 39297Seric */ 40297Seric 413380Seric # define DELIMCHARS "$()<>,;\\\"\r\n" /* word delimiters */ 422091Seric 432973Seric ADDRESS * 44297Seric parse(addr, a, copyf) 45297Seric char *addr; 462973Seric register ADDRESS *a; 47297Seric int copyf; 48297Seric { 493149Seric register char **pvp; 503149Seric register struct mailer *m; 513149Seric extern char **prescan(); 523149Seric extern ADDRESS *buildaddr(); 537889Seric static char nbuf[MAXNAME]; 54297Seric 55297Seric /* 56297Seric ** Initialize and prescan address. 57297Seric */ 58297Seric 596903Seric CurEnv->e_to = addr; 603188Seric # ifdef DEBUG 617675Seric if (tTd(20, 1)) 623188Seric printf("\n--parse(%s)\n", addr); 633188Seric # endif DEBUG 643188Seric 658078Seric pvp = prescan(addr, ','); 663149Seric if (pvp == NULL) 67297Seric return (NULL); 68297Seric 69297Seric /* 703149Seric ** Apply rewriting rules. 717889Seric ** Ruleset 0 does basic parsing. It must resolve. 72297Seric */ 73297Seric 748181Seric rewrite(pvp, 3); 754070Seric rewrite(pvp, 0); 76297Seric 773149Seric /* 783149Seric ** See if we resolved to a real mailer. 793149Seric */ 80297Seric 813149Seric if (pvp[0][0] != CANONNET) 823149Seric { 833149Seric setstat(EX_USAGE); 843149Seric usrerr("cannot resolve name"); 853149Seric return (NULL); 86297Seric } 87297Seric 88297Seric /* 893149Seric ** Build canonical address from pvp. 90297Seric */ 91297Seric 923149Seric a = buildaddr(pvp, a); 934279Seric if (a == NULL) 944279Seric return (NULL); 954598Seric m = a->q_mailer; 96297Seric 97297Seric /* 983149Seric ** Make local copies of the host & user and then 993149Seric ** transport them out. 100297Seric */ 101297Seric 102297Seric if (copyf > 0) 1038078Seric { 1048078Seric extern char *DelimChar; 1058078Seric char savec = *DelimChar; 1068078Seric 1078078Seric *DelimChar = '\0'; 1082973Seric a->q_paddr = newstr(addr); 1098078Seric *DelimChar = savec; 1108078Seric } 111297Seric else 112297Seric a->q_paddr = addr; 1133149Seric if (copyf >= 0) 114297Seric { 1153149Seric if (a->q_host != NULL) 1163149Seric a->q_host = newstr(a->q_host); 117297Seric else 1183149Seric a->q_host = ""; 1193149Seric if (a->q_user != a->q_paddr) 1203149Seric a->q_user = newstr(a->q_user); 121297Seric } 122297Seric 123297Seric /* 124297Seric ** Do UPPER->lower case mapping unless inhibited. 125297Seric */ 126297Seric 1273149Seric if (!bitset(M_HST_UPPER, m->m_flags)) 128297Seric makelower(a->q_host); 1293149Seric if (!bitset(M_USR_UPPER, m->m_flags)) 130297Seric makelower(a->q_user); 131297Seric 132297Seric /* 133297Seric ** Compute return value. 134297Seric */ 135297Seric 136297Seric # ifdef DEBUG 1377675Seric if (tTd(20, 1)) 1384443Seric { 1394443Seric printf("parse-->"); 1404443Seric printaddr(a, FALSE); 1414443Seric } 142297Seric # endif DEBUG 143297Seric 144297Seric return (a); 145297Seric } 146297Seric /* 147297Seric ** PRESCAN -- Prescan name and make it canonical 148297Seric ** 149297Seric ** Scans a name and turns it into canonical form. This involves 150297Seric ** deleting blanks, comments (in parentheses), and turning the 151297Seric ** word "at" into an at-sign ("@"). The name is copied as this 152297Seric ** is done; it is legal to copy a name onto itself, since this 153297Seric ** process can only make things smaller. 154297Seric ** 155297Seric ** This routine knows about quoted strings and angle brackets. 156297Seric ** 157297Seric ** There are certain subtleties to this routine. The one that 158297Seric ** comes to mind now is that backslashes on the ends of names 159297Seric ** are silently stripped off; this is intentional. The problem 160297Seric ** is that some versions of sndmsg (like at LBL) set the kill 161297Seric ** character to something other than @ when reading addresses; 162297Seric ** so people type "csvax.eric\@berkeley" -- which screws up the 163297Seric ** berknet mailer. 164297Seric ** 165297Seric ** Parameters: 166297Seric ** addr -- the name to chomp. 167297Seric ** delim -- the delimiter for the address, normally 168297Seric ** '\0' or ','; \0 is accepted in any case. 169297Seric ** 170297Seric ** Returns: 1713149Seric ** A pointer to a vector of tokens. 172297Seric ** NULL on error. 173297Seric ** 174297Seric ** Side Effects: 1753149Seric ** none. 176297Seric */ 177297Seric 1788078Seric /* states and character types */ 1798078Seric # define OPR 0 /* operator */ 1808078Seric # define ATM 1 /* atom */ 1818078Seric # define QST 2 /* in quoted string */ 1828078Seric # define SPC 3 /* chewing up spaces */ 1838078Seric # define ONE 4 /* pick up one character */ 1843149Seric 1858078Seric # define NSTATES 5 /* number of states */ 1868078Seric # define TYPE 017 /* mask to select state type */ 1878078Seric 1888078Seric /* meta bits for table */ 1898078Seric # define M 020 /* meta character; don't pass through */ 1908078Seric # define B 040 /* cause a break */ 1918078Seric # define MB M|B /* meta-break */ 1928078Seric 1938078Seric static short StateTab[NSTATES][NSTATES] = 1948078Seric { 1958087Seric /* oldst chtype> OPR ATM QST SPC ONE */ 1968078Seric /*OPR*/ OPR|B, ATM|B, QST|MB, SPC|MB, ONE|B, 1978078Seric /*ATM*/ OPR|B, ATM, QST|MB, SPC|MB, ONE|B, 1988087Seric /*QST*/ QST, QST, OPR|MB, QST, QST, 1998078Seric /*SPC*/ OPR, ATM, QST, SPC|M, ONE, 2008078Seric /*ONE*/ OPR, OPR, OPR, OPR, OPR, 2018078Seric }; 2028078Seric 2038078Seric # define NOCHAR -1 /* signal nothing in lookahead token */ 2048078Seric 2058078Seric char *DelimChar; /* set to point to the delimiter */ 2068078Seric 2073149Seric char ** 2083149Seric prescan(addr, delim) 209297Seric char *addr; 210297Seric char delim; 211297Seric { 212297Seric register char *p; 2138078Seric register char *q; 2148078Seric register char c; 2153149Seric char **avp; 216297Seric bool bslashmode; 217297Seric int cmntcnt; 2183149Seric char *tok; 2198078Seric int state; 2208078Seric int newstate; 2218078Seric static char buf[MAXNAME+MAXATOM]; 2228078Seric static char *av[MAXATOM+1]; 223297Seric 224297Seric q = buf; 2253149Seric bslashmode = FALSE; 2267800Seric cmntcnt = 0; 2273149Seric avp = av; 2288078Seric state = OPR; 2298078Seric c = NOCHAR; 2308078Seric p = addr; 2318078Seric # ifdef DEBUG 2328078Seric if (tTd(22, 45)) 233297Seric { 2348078Seric printf("prescan: "); 2358078Seric xputs(p); 2368078Seric putchar('\n'); 2378078Seric } 2388078Seric # endif DEBUG 2398078Seric 2408078Seric do 2418078Seric { 2423149Seric /* read a token */ 2433149Seric tok = q; 2448078Seric for (;;) 245297Seric { 2468078Seric /* store away any old lookahead character */ 2478078Seric if (c != NOCHAR) 2488078Seric { 2498078Seric /* squirrel it away */ 2508078Seric if (q >= &buf[sizeof buf - 5]) 2518078Seric { 2528078Seric usrerr("Address too long"); 2538078Seric DelimChar = p; 2548078Seric return (NULL); 2558078Seric } 2568078Seric *q++ = c; 2578078Seric } 2588078Seric 2598078Seric /* read a new input character */ 2608078Seric c = *p++; 2618078Seric if (c == '\0') 2628078Seric break; 2638078Seric # ifdef DEBUG 2648078Seric if (tTd(22, 101)) 2658078Seric printf("c=%c, s=%d; ", c, state); 2668078Seric # endif DEBUG 2678078Seric 2683149Seric /* chew up special characters */ 2694100Seric c &= ~0200; 2703149Seric *q = '\0'; 2713149Seric if (bslashmode) 2723149Seric { 2733149Seric c |= 0200; 2743149Seric bslashmode = FALSE; 2753149Seric } 2763149Seric else if (c == '\\') 2773149Seric { 2783149Seric bslashmode = TRUE; 2798078Seric c = NOCHAR; 2803149Seric } 2818078Seric else if (c == '(') 2824100Seric { 2838078Seric cmntcnt++; 2848078Seric c = NOCHAR; 2854100Seric } 2868078Seric else if (c == ')') 2873149Seric { 2888078Seric if (cmntcnt <= 0) 2893149Seric { 2908078Seric usrerr("Unbalanced ')'"); 2918078Seric DelimChar = p; 2928078Seric return (NULL); 2933149Seric } 2948078Seric else 2958078Seric cmntcnt--; 2968078Seric } 2978078Seric else if (cmntcnt > 0) 2988078Seric c = NOCHAR; 2993149Seric 3008078Seric if (c == NOCHAR) 3018078Seric continue; 3023149Seric 3038078Seric /* see if this is end of input */ 3048078Seric if (c == delim) 3053149Seric break; 3063149Seric 3078078Seric newstate = StateTab[state][toktype(c)]; 3088078Seric # ifdef DEBUG 3098078Seric if (tTd(22, 101)) 3108078Seric printf("ns=%02o\n", newstate); 3118078Seric # endif DEBUG 3128078Seric state = newstate & TYPE; 3138078Seric if (bitset(M, newstate)) 3148078Seric c = NOCHAR; 3158078Seric if (bitset(B, newstate)) 3164228Seric break; 317297Seric } 3183149Seric 3193149Seric /* new token */ 3208078Seric if (tok != q) 3211378Seric { 3228078Seric *q++ = '\0'; 3238078Seric # ifdef DEBUG 3248078Seric if (tTd(22, 36)) 325297Seric { 3268078Seric printf("tok="); 3278078Seric xputs(tok); 3288078Seric putchar('\n'); 329297Seric } 3308078Seric # endif DEBUG 3318078Seric if (avp >= &av[MAXATOM]) 332297Seric { 3338078Seric syserr("prescan: too many tokens"); 3348078Seric DelimChar = p; 3358078Seric return (NULL); 336297Seric } 3378078Seric *avp++ = tok; 338297Seric } 3398078Seric } while (c != '\0' && c != delim); 3403149Seric *avp = NULL; 3418078Seric DelimChar = --p; 3423149Seric if (cmntcnt > 0) 3433149Seric usrerr("Unbalanced '('"); 3448078Seric else if (state == QST) 3453149Seric usrerr("Unbalanced '\"'"); 3463149Seric else if (av[0] != NULL) 3473149Seric return (av); 3483149Seric return (NULL); 3493149Seric } 3503149Seric /* 3513149Seric ** TOKTYPE -- return token type 3523149Seric ** 3533149Seric ** Parameters: 3543149Seric ** c -- the character in question. 3553149Seric ** 3563149Seric ** Returns: 3573149Seric ** Its type. 3583149Seric ** 3593149Seric ** Side Effects: 3603149Seric ** none. 3613149Seric */ 362297Seric 3633149Seric toktype(c) 3643149Seric register char c; 3653149Seric { 3663380Seric static char buf[50]; 3673382Seric static bool firstime = TRUE; 3683380Seric 3693382Seric if (firstime) 3703380Seric { 3713382Seric firstime = FALSE; 3726977Seric expand("$o", buf, &buf[sizeof buf - 1], CurEnv); 3737005Seric (void) strcat(buf, DELIMCHARS); 3743380Seric } 3756053Seric if (c == MATCHCLASS || c == MATCHREPL) 3768078Seric return (ONE); 3778078Seric if (c == '"') 3788078Seric return (QST); 3794100Seric if (!isascii(c)) 3808078Seric return (ATM); 3818078Seric if (isspace(c) || c == ')') 3828078Seric return (SPC); 3833380Seric if (iscntrl(c) || index(buf, c) != NULL) 3848078Seric return (OPR); 3858078Seric return (ATM); 3863149Seric } 3873149Seric /* 3883149Seric ** REWRITE -- apply rewrite rules to token vector. 3893149Seric ** 3904476Seric ** This routine is an ordered production system. Each rewrite 3914476Seric ** rule has a LHS (called the pattern) and a RHS (called the 3924476Seric ** rewrite); 'rwr' points the the current rewrite rule. 3934476Seric ** 3944476Seric ** For each rewrite rule, 'avp' points the address vector we 3954476Seric ** are trying to match against, and 'pvp' points to the pattern. 3968058Seric ** If pvp points to a special match value (MATCHZANY, MATCHANY, 3978058Seric ** MATCHONE, MATCHCLASS) then the address in avp matched is 3988058Seric ** saved away in the match vector (pointed to by 'mvp'). 3994476Seric ** 4004476Seric ** When a match between avp & pvp does not match, we try to 4014476Seric ** back out. If we back up over a MATCHONE or a MATCHCLASS 4024476Seric ** we must also back out the match in mvp. If we reach a 4038058Seric ** MATCHANY or MATCHZANY we just extend the match and start 4048058Seric ** over again. 4054476Seric ** 4064476Seric ** When we finally match, we rewrite the address vector 4074476Seric ** and try over again. 4084476Seric ** 4093149Seric ** Parameters: 4103149Seric ** pvp -- pointer to token vector. 4113149Seric ** 4123149Seric ** Returns: 4133149Seric ** none. 4143149Seric ** 4153149Seric ** Side Effects: 4163149Seric ** pvp is modified. 4173149Seric */ 4182091Seric 4193149Seric struct match 4203149Seric { 4214468Seric char **first; /* first token matched */ 4224468Seric char **last; /* last token matched */ 4233149Seric }; 4243149Seric 4254468Seric # define MAXMATCH 9 /* max params per rewrite */ 4263149Seric 4273149Seric 4284070Seric rewrite(pvp, ruleset) 4293149Seric char **pvp; 4304070Seric int ruleset; 4313149Seric { 4323149Seric register char *ap; /* address pointer */ 4333149Seric register char *rp; /* rewrite pointer */ 4343149Seric register char **avp; /* address vector pointer */ 4353149Seric register char **rvp; /* rewrite vector pointer */ 4368058Seric register struct match *mlp; /* cur ptr into mlist */ 4378058Seric register struct rewrite *rwr; /* pointer to current rewrite rule */ 4384468Seric struct match mlist[MAXMATCH]; /* stores match on LHS */ 4393149Seric char *npvp[MAXATOM+1]; /* temporary space for rebuild */ 4404060Seric extern bool sameword(); 4413149Seric 4424100Seric # ifdef DEBUG 4438069Seric if (tTd(21, 2)) 4443149Seric { 4458069Seric printf("rewrite: ruleset %d, original pvp:", ruleset); 4463149Seric printav(pvp); 4473149Seric } 4484100Seric # endif DEBUG 4493149Seric 4503149Seric /* 4513149Seric ** Run through the list of rewrite rules, applying 4523149Seric ** any that match. 4533149Seric */ 4543149Seric 4554070Seric for (rwr = RewriteRules[ruleset]; rwr != NULL; ) 4563149Seric { 4574100Seric # ifdef DEBUG 4587675Seric if (tTd(21, 12)) 459297Seric { 4608069Seric printf("-----trying rule:"); 4613149Seric printav(rwr->r_lhs); 4623149Seric } 4634100Seric # endif DEBUG 4643149Seric 4653149Seric /* try to match on this rule */ 4664468Seric mlp = mlist; 4678058Seric rvp = rwr->r_lhs; 4688058Seric avp = pvp; 4698058Seric while ((ap = *avp) != NULL || *rvp != NULL) 4703149Seric { 4713149Seric rp = *rvp; 4728058Seric # ifdef DEBUG 4738058Seric if (tTd(21, 35)) 4748058Seric { 4758069Seric printf("ap="); 4768058Seric xputs(ap); 4778069Seric printf(", rp="); 4788058Seric xputs(rp); 4798069Seric printf("\n"); 4808058Seric } 4818058Seric # endif DEBUG 4823149Seric if (rp == NULL) 483297Seric { 4843149Seric /* end-of-pattern before end-of-address */ 4858058Seric goto backup; 486297Seric } 4878058Seric if (ap == NULL && *rp != MATCHZANY) 4888058Seric { 4898058Seric /* end-of-input */ 4908058Seric break; 4918058Seric } 4923149Seric 4933149Seric switch (*rp) 4943149Seric { 4954060Seric register STAB *s; 4964060Seric register int class; 4974060Seric 4984060Seric case MATCHCLASS: 4994060Seric /* match any token in a class */ 5004060Seric class = rp[1]; 5014060Seric if (!isalpha(class)) 5028058Seric goto backup; 5034060Seric if (isupper(class)) 5044060Seric class -= 'A'; 5054060Seric else 5064060Seric class -= 'a'; 5074100Seric s = stab(ap, ST_CLASS, ST_FIND); 5086273Seric if (s == NULL || (s->s_class & (1L << class)) == 0) 5098058Seric goto backup; 5104468Seric 5114476Seric /* explicit fall-through */ 5124476Seric 5134476Seric case MATCHONE: 5144476Seric case MATCHANY: 5154476Seric /* match exactly one token */ 5168058Seric mlp->first = avp; 5178058Seric mlp->last = avp++; 5184468Seric mlp++; 5194060Seric break; 5204060Seric 5218058Seric case MATCHZANY: 5228058Seric /* match zero or more tokens */ 5238058Seric mlp->first = avp; 5248058Seric mlp->last = avp - 1; 5258058Seric mlp++; 5268058Seric break; 5278058Seric 5283149Seric default: 5293149Seric /* must have exact match */ 5304060Seric if (!sameword(rp, ap)) 5318058Seric goto backup; 5324468Seric avp++; 5333149Seric break; 5343149Seric } 5353149Seric 5363149Seric /* successful match on this token */ 5373149Seric rvp++; 5383149Seric continue; 5393149Seric 5408058Seric backup: 5413149Seric /* match failed -- back up */ 5423149Seric while (--rvp >= rwr->r_lhs) 5433149Seric { 5443149Seric rp = *rvp; 5458058Seric if (*rp == MATCHANY || *rp == MATCHZANY) 5464468Seric { 5474476Seric /* extend binding and continue */ 5488058Seric avp = ++mlp[-1].last; 5498058Seric avp++; 5504476Seric rvp++; 5513149Seric break; 5524468Seric } 5534476Seric avp--; 5544476Seric if (*rp == MATCHONE || *rp == MATCHCLASS) 5553149Seric { 5564468Seric /* back out binding */ 5574468Seric mlp--; 5583149Seric } 5593149Seric } 5603149Seric 5613149Seric if (rvp < rwr->r_lhs) 5623149Seric { 5633149Seric /* total failure to match */ 5643149Seric break; 5653149Seric } 566297Seric } 5673149Seric 5683149Seric /* 5693149Seric ** See if we successfully matched 5703149Seric */ 5713149Seric 5723149Seric if (rvp >= rwr->r_lhs && *rvp == NULL) 5733149Seric { 5748058Seric rvp = rwr->r_rhs; 5754100Seric # ifdef DEBUG 5767675Seric if (tTd(21, 12)) 5773149Seric { 5788069Seric printf("-----rule matches:"); 5798058Seric printav(rvp); 5803149Seric } 5814100Seric # endif DEBUG 5823149Seric 5838058Seric rp = *rvp; 584*8226Seric if (*rp == CANONUSER) 5858058Seric { 5868069Seric rvp++; 5878069Seric rwr = rwr->r_next; 5888069Seric } 5898069Seric else if (*rp == CANONHOST) 5908069Seric { 5918069Seric rvp++; 5928069Seric rwr = NULL; 5938069Seric } 5948069Seric else if (*rp == CANONNET) 5958069Seric rwr = NULL; 5968058Seric 5973149Seric /* substitute */ 5988069Seric for (avp = npvp; *rvp != NULL; rvp++) 5993149Seric { 6003149Seric rp = *rvp; 6014468Seric if (*rp == MATCHREPL) 6023149Seric { 6033149Seric register struct match *m; 6043149Seric register char **pp; 6053149Seric 6064468Seric m = &mlist[rp[1] - '1']; 6074476Seric # ifdef DEBUG 6087675Seric if (tTd(21, 15)) 6094476Seric { 6104476Seric printf("$%c:", rp[1]); 6114476Seric pp = m->first; 6128058Seric while (pp <= m->last) 6134476Seric { 6144476Seric printf(" %x=\"", *pp); 6154625Seric (void) fflush(stdout); 6168058Seric printf("%s\"", *pp++); 6178058Seric } 6184476Seric printf("\n"); 6194476Seric } 6204476Seric # endif DEBUG 6214468Seric pp = m->first; 6228058Seric while (pp <= m->last) 6233149Seric { 6244468Seric if (avp >= &npvp[MAXATOM]) 6253149Seric { 6264468Seric syserr("rewrite: expansion too long"); 6274468Seric return; 6284468Seric } 6298058Seric *avp++ = *pp++; 6308058Seric } 6313149Seric } 6323149Seric else 6334385Seric { 6344385Seric if (avp >= &npvp[MAXATOM]) 6354385Seric { 6364385Seric syserr("rewrite: expansion too long"); 6374385Seric return; 6384385Seric } 6393149Seric *avp++ = rp; 6404385Seric } 6413149Seric } 6423149Seric *avp++ = NULL; 643*8226Seric if (**npvp == CALLSUBR) 644*8226Seric { 645*8226Seric bmove((char *) &npvp[2], (char *) pvp, 646*8226Seric (avp - npvp - 2) * sizeof *avp); 6473149Seric # ifdef DEBUG 648*8226Seric if (tTd(21, 3)) 649*8226Seric printf("-----callsubr %s\n", npvp[1]); 650*8226Seric # endif DEBUG 651*8226Seric rewrite(pvp, atoi(npvp[1])); 652*8226Seric } 653*8226Seric else 654*8226Seric { 655*8226Seric bmove((char *) npvp, (char *) pvp, 656*8226Seric (avp - npvp) * sizeof *avp); 657*8226Seric } 658*8226Seric # ifdef DEBUG 6597675Seric if (tTd(21, 4)) 6603149Seric { 6618069Seric printf("rewritten as:"); 6628069Seric printav(pvp); 6633149Seric } 6643149Seric # endif DEBUG 6653149Seric } 6663149Seric else 6673149Seric { 6684100Seric # ifdef DEBUG 6697675Seric if (tTd(21, 10)) 6703149Seric printf("----- rule fails\n"); 6714100Seric # endif DEBUG 6723149Seric rwr = rwr->r_next; 6733149Seric } 674297Seric } 6758069Seric 6768069Seric # ifdef DEBUG 6778069Seric if (tTd(21, 2)) 6788069Seric { 6798069Seric printf("rewrite: ruleset %d returns:", ruleset); 6808069Seric printav(pvp); 6818069Seric } 6828069Seric # endif DEBUG 6833149Seric } 6843149Seric /* 6853149Seric ** BUILDADDR -- build address from token vector. 6863149Seric ** 6873149Seric ** Parameters: 6883149Seric ** tv -- token vector. 6893149Seric ** a -- pointer to address descriptor to fill. 6903149Seric ** If NULL, one will be allocated. 6913149Seric ** 6923149Seric ** Returns: 6934279Seric ** NULL if there was an error. 6944279Seric ** 'a' otherwise. 6953149Seric ** 6963149Seric ** Side Effects: 6973149Seric ** fills in 'a' 6983149Seric */ 6993149Seric 7003149Seric ADDRESS * 7013149Seric buildaddr(tv, a) 7023149Seric register char **tv; 7033149Seric register ADDRESS *a; 7043149Seric { 7053149Seric static char buf[MAXNAME]; 7063149Seric struct mailer **mp; 7073149Seric register struct mailer *m; 7084635Seric extern bool sameword(); 7093149Seric 7103149Seric if (a == NULL) 7113149Seric a = (ADDRESS *) xalloc(sizeof *a); 7124988Seric clear((char *) a, sizeof *a); 7133149Seric 7143149Seric /* figure out what net/mailer to use */ 7153149Seric if (**tv != CANONNET) 7164279Seric { 7173149Seric syserr("buildaddr: no net"); 7184279Seric return (NULL); 7194279Seric } 7203149Seric tv++; 7214635Seric if (sameword(*tv, "error")) 7224279Seric { 7234279Seric if (**++tv != CANONUSER) 7244279Seric syserr("buildaddr: error: no user"); 7254279Seric buf[0] = '\0'; 7264279Seric while (*++tv != NULL) 7274279Seric { 7284279Seric if (buf[0] != '\0') 7297005Seric (void) strcat(buf, " "); 7307005Seric (void) strcat(buf, *tv); 7314279Seric } 7324279Seric usrerr(buf); 7334279Seric return (NULL); 7344279Seric } 7354598Seric for (mp = Mailer; (m = *mp++) != NULL; ) 7363149Seric { 7374635Seric if (sameword(m->m_name, *tv)) 7383149Seric break; 7393149Seric } 7403149Seric if (m == NULL) 7414279Seric { 7423149Seric syserr("buildaddr: unknown net %s", *tv); 7434279Seric return (NULL); 7444279Seric } 7454598Seric a->q_mailer = m; 7463149Seric 7473149Seric /* figure out what host (if any) */ 7483149Seric tv++; 7494195Seric if (!bitset(M_LOCAL, m->m_flags)) 7503149Seric { 7515704Seric if (**tv++ != CANONHOST) 7524279Seric { 7533149Seric syserr("buildaddr: no host"); 7544279Seric return (NULL); 7554279Seric } 7565704Seric buf[0] = '\0'; 7575704Seric while (*tv != NULL && **tv != CANONUSER) 7587005Seric (void) strcat(buf, *tv++); 7595704Seric a->q_host = newstr(buf); 7603149Seric } 7613149Seric else 7623149Seric a->q_host = NULL; 7633149Seric 7643149Seric /* figure out the user */ 7653149Seric if (**tv != CANONUSER) 7664279Seric { 7673149Seric syserr("buildaddr: no user"); 7684279Seric return (NULL); 7694279Seric } 7704228Seric cataddr(++tv, buf, sizeof buf); 7713149Seric a->q_user = buf; 7723149Seric 7733149Seric return (a); 7743149Seric } 7753188Seric /* 7764228Seric ** CATADDR -- concatenate pieces of addresses (putting in <LWSP> subs) 7774228Seric ** 7784228Seric ** Parameters: 7794228Seric ** pvp -- parameter vector to rebuild. 7804228Seric ** buf -- buffer to build the string into. 7814228Seric ** sz -- size of buf. 7824228Seric ** 7834228Seric ** Returns: 7844228Seric ** none. 7854228Seric ** 7864228Seric ** Side Effects: 7874228Seric ** Destroys buf. 7884228Seric */ 7894228Seric 7904228Seric cataddr(pvp, buf, sz) 7914228Seric char **pvp; 7924228Seric char *buf; 7934228Seric register int sz; 7944228Seric { 7954228Seric bool oatomtok = FALSE; 7964228Seric bool natomtok = FALSE; 7974228Seric register int i; 7984228Seric register char *p; 7994228Seric 8004228Seric p = buf; 8014228Seric sz--; 8024228Seric while (*pvp != NULL && (i = strlen(*pvp)) < sz) 8034228Seric { 8048078Seric natomtok = (toktype(**pvp) == ATM); 8054228Seric if (oatomtok && natomtok) 8064228Seric *p++ = SPACESUB; 8074228Seric (void) strcpy(p, *pvp); 8084228Seric oatomtok = natomtok; 8094228Seric p += i; 8104228Seric sz -= i; 8114228Seric pvp++; 8124228Seric } 8134228Seric *p = '\0'; 8144228Seric } 8154228Seric /* 8163188Seric ** SAMEADDR -- Determine if two addresses are the same 8173188Seric ** 8183188Seric ** This is not just a straight comparison -- if the mailer doesn't 8193188Seric ** care about the host we just ignore it, etc. 8203188Seric ** 8213188Seric ** Parameters: 8223188Seric ** a, b -- pointers to the internal forms to compare. 8233188Seric ** wildflg -- if TRUE, 'a' may have no user specified, 8243188Seric ** in which case it is to match anything. 8253188Seric ** 8263188Seric ** Returns: 8273188Seric ** TRUE -- they represent the same mailbox. 8283188Seric ** FALSE -- they don't. 8293188Seric ** 8303188Seric ** Side Effects: 8313188Seric ** none. 8323188Seric */ 8333188Seric 8343188Seric bool 8353188Seric sameaddr(a, b, wildflg) 8363188Seric register ADDRESS *a; 8373188Seric register ADDRESS *b; 8383188Seric bool wildflg; 8393188Seric { 8403188Seric /* if they don't have the same mailer, forget it */ 8413188Seric if (a->q_mailer != b->q_mailer) 8423188Seric return (FALSE); 8433188Seric 8443188Seric /* if the user isn't the same, we can drop out */ 8453188Seric if ((!wildflg || a->q_user[0] != '\0') && strcmp(a->q_user, b->q_user) != 0) 8463188Seric return (FALSE); 8473188Seric 8483188Seric /* if the mailer ignores hosts, we have succeeded! */ 8494598Seric if (bitset(M_LOCAL, a->q_mailer->m_flags)) 8503188Seric return (TRUE); 8513188Seric 8523188Seric /* otherwise compare hosts (but be careful for NULL ptrs) */ 8533188Seric if (a->q_host == NULL || b->q_host == NULL) 8543188Seric return (FALSE); 8553188Seric if (strcmp(a->q_host, b->q_host) != 0) 8563188Seric return (FALSE); 8573188Seric 8583188Seric return (TRUE); 8593188Seric } 8603234Seric /* 8613234Seric ** PRINTADDR -- print address (for debugging) 8623234Seric ** 8633234Seric ** Parameters: 8643234Seric ** a -- the address to print 8653234Seric ** follow -- follow the q_next chain. 8663234Seric ** 8673234Seric ** Returns: 8683234Seric ** none. 8693234Seric ** 8703234Seric ** Side Effects: 8713234Seric ** none. 8723234Seric */ 8733234Seric 8744317Seric # ifdef DEBUG 8754317Seric 8763234Seric printaddr(a, follow) 8773234Seric register ADDRESS *a; 8783234Seric bool follow; 8793234Seric { 8805001Seric bool first = TRUE; 8815001Seric 8823234Seric while (a != NULL) 8833234Seric { 8845001Seric first = FALSE; 8854443Seric printf("%x=", a); 8864085Seric (void) fflush(stdout); 8873234Seric printf("%s: mailer %d (%s), host `%s', user `%s'\n", a->q_paddr, 8888181Seric a->q_mailer->m_mno, a->q_mailer->m_name, a->q_host, 8898181Seric a->q_user); 8908181Seric printf("\tnext=%x, flags=%o, alias %x\n", a->q_next, a->q_flags, 8918181Seric a->q_alias); 8928181Seric printf("\thome=\"%s\", fullname=\"%s\"\n", a->q_home, 8938181Seric a->q_fullname); 8944996Seric 8953234Seric if (!follow) 8963234Seric return; 8974996Seric a = a->q_next; 8983234Seric } 8995001Seric if (first) 9004443Seric printf("[NULL]\n"); 9013234Seric } 9024317Seric 9034317Seric # endif DEBUG 9047682Seric /* 9057682Seric ** REMOTENAME -- return the name relative to the current mailer 9067682Seric ** 9077682Seric ** Parameters: 9087682Seric ** name -- the name to translate. 9098069Seric ** m -- the mailer that we want to do rewriting relative 9108069Seric ** to. 9118069Seric ** senderaddress -- if set, uses the sender rewriting rules 9128069Seric ** rather than the recipient rewriting rules. 9137682Seric ** 9147682Seric ** Returns: 9157682Seric ** the text string representing this address relative to 9167682Seric ** the receiving mailer. 9177682Seric ** 9187682Seric ** Side Effects: 9197682Seric ** none. 9207682Seric ** 9217682Seric ** Warnings: 9227682Seric ** The text string returned is tucked away locally; 9237682Seric ** copy it if you intend to save it. 9247682Seric */ 9257682Seric 9267682Seric char * 9278069Seric remotename(name, m, senderaddress) 9287682Seric char *name; 9297682Seric struct mailer *m; 9308069Seric bool senderaddress; 9317682Seric { 9328069Seric register char **pvp; 9338069Seric char *fancy; 9348069Seric extern char *macvalue(); 9358181Seric char *oldg = macvalue('g', CurEnv); 9367682Seric static char buf[MAXNAME]; 9377682Seric char lbuf[MAXNAME]; 9387682Seric extern char **prescan(); 9397889Seric extern char *crackaddr(); 9407682Seric 9417755Seric # ifdef DEBUG 9427755Seric if (tTd(12, 1)) 9437755Seric printf("remotename(%s)\n", name); 9447755Seric # endif DEBUG 9457755Seric 9467682Seric /* 9478181Seric ** Do a heuristic crack of this name to extract any comment info. 9488181Seric ** This will leave the name as a comment and a $g macro. 9497889Seric */ 9507889Seric 9517889Seric fancy = crackaddr(name); 9527889Seric 9538181Seric /* 9548181Seric ** Turn the name into canonical form. 9558181Seric ** Normally this will be RFC 822 style, i.e., "user@domain". 9568181Seric ** If this only resolves to "user", and the "C" flag is 9578181Seric ** specified in the sending mailer, then the sender's 9588181Seric ** domain will be appended. 9598181Seric */ 9608181Seric 9617889Seric pvp = prescan(name, '\0'); 9627889Seric if (pvp == NULL) 9637889Seric return (name); 9648181Seric rewrite(pvp, 3); 9658181Seric if (CurEnv->e_fromdomain != NULL) 9668181Seric { 9678181Seric /* append from domain to this address */ 9688181Seric register char **pxp = pvp; 9698181Seric 9708181Seric while (*pxp != NULL && strcmp(*pxp, "@") != 0) 9718181Seric pxp++; 9728181Seric if (*pxp == NULL) 9738181Seric { 9748181Seric register char **qxq = CurEnv->e_fromdomain; 9758181Seric 9768181Seric while (*qxq != NULL) 9778181Seric *pxp++ = *qxq++; 9788181Seric } 9798181Seric } 9808181Seric 9818181Seric /* 9828181Seric ** Now do more specific rewriting. 9838181Seric ** Rewrite using ruleset 1 or 2 depending on whether this is 9848181Seric ** a sender address or not. 9858181Seric ** Then run it through any receiving-mailer-specific rulesets. 9868181Seric */ 9878181Seric 9888069Seric if (senderaddress) 9897755Seric { 9907889Seric rewrite(pvp, 1); 9918069Seric if (m->m_s_rwset > 0) 9928069Seric rewrite(pvp, m->m_s_rwset); 9938069Seric } 9948069Seric else 9958069Seric { 9967889Seric rewrite(pvp, 2); 9978069Seric if (m->m_r_rwset > 0) 9988069Seric rewrite(pvp, m->m_r_rwset); 9997682Seric } 10007682Seric 10018181Seric /* 10028181Seric ** Now restore the comment information we had at the beginning. 10038181Seric */ 10048181Seric 10057682Seric cataddr(pvp, lbuf, sizeof lbuf); 10067682Seric define('g', lbuf); 10077889Seric expand(fancy, buf, &buf[sizeof buf - 1], CurEnv); 10087682Seric define('g', oldg); 10097682Seric 10107682Seric # ifdef DEBUG 10117682Seric if (tTd(12, 1)) 10127755Seric printf("remotename => `%s'\n", buf); 10137682Seric # endif DEBUG 10147682Seric return (buf); 10157682Seric } 10167682Seric /* 10177682Seric ** CANONNAME -- make name canonical 10187682Seric ** 10197682Seric ** This is used for SMTP and misc. printing. Given a print 10208181Seric ** address, it strips out comments, etc. 10217682Seric ** 10227682Seric ** Parameters: 10237682Seric ** name -- the name to make canonical. 10247682Seric ** 10257682Seric ** Returns: 10267682Seric ** pointer to canonical name. 10277682Seric ** 10287682Seric ** Side Effects: 10297682Seric ** none. 10307682Seric ** 10317682Seric ** Warning: 10327682Seric ** result is saved in static buf; future calls will trash it. 10337682Seric */ 10347682Seric 10357682Seric char * 10367682Seric canonname(name) 10377682Seric char *name; 10387682Seric { 10398069Seric static char nbuf[MAXNAME]; 10407940Seric register char **pvp; 10417682Seric 10427940Seric pvp = prescan(name, '\0'); 10438069Seric rewrite(pvp, 3); 10448069Seric cataddr(pvp, nbuf, sizeof nbuf); 10457682Seric return (nbuf); 10467682Seric } 1047