13312Seric # include "sendmail.h" 2297Seric 3*11423Seric SCCSID(@(#)parseaddr.c 3.79 03/07/83); 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. 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 419374Seric /* following delimiters are inherent to the internal algorithms */ 423380Seric # define DELIMCHARS "$()<>,;\\\"\r\n" /* word delimiters */ 432091Seric 442973Seric ADDRESS * 459888Seric parseaddr(addr, a, copyf) 46297Seric char *addr; 472973Seric register ADDRESS *a; 48297Seric int copyf; 49297Seric { 503149Seric register char **pvp; 513149Seric register struct mailer *m; 523149Seric extern char **prescan(); 533149Seric extern ADDRESS *buildaddr(); 54297Seric 55297Seric /* 56297Seric ** Initialize and prescan address. 57297Seric */ 58297Seric 596903Seric CurEnv->e_to = addr; 603188Seric # ifdef DEBUG 617675Seric if (tTd(20, 1)) 629888Seric printf("\n--parseaddr(%s)\n", addr); 633188Seric # endif DEBUG 643188Seric 65*11423Seric pvp = prescan(addr, bitset(EF_OLDSTYLE, CurEnv->e_flags) ? ' ' : ','); 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 12710690Seric if (!bitnset(M_HST_UPPER, m->m_flags)) 128297Seric makelower(a->q_host); 12910690Seric if (!bitnset(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 { 1399888Seric printf("parseaddr-->"); 1404443Seric printaddr(a, FALSE); 1414443Seric } 142297Seric # endif DEBUG 143297Seric 144297Seric return (a); 145297Seric } 146297Seric /* 147297Seric ** PRESCAN -- Prescan name and make it canonical 148297Seric ** 1499374Seric ** Scans a name and turns it into a set of tokens. This process 1509374Seric ** deletes blanks and comments (in parentheses). 151297Seric ** 152297Seric ** This routine knows about quoted strings and angle brackets. 153297Seric ** 154297Seric ** There are certain subtleties to this routine. The one that 155297Seric ** comes to mind now is that backslashes on the ends of names 156297Seric ** are silently stripped off; this is intentional. The problem 157297Seric ** is that some versions of sndmsg (like at LBL) set the kill 158297Seric ** character to something other than @ when reading addresses; 159297Seric ** so people type "csvax.eric\@berkeley" -- which screws up the 160297Seric ** berknet mailer. 161297Seric ** 162297Seric ** Parameters: 163297Seric ** addr -- the name to chomp. 164297Seric ** delim -- the delimiter for the address, normally 165297Seric ** '\0' or ','; \0 is accepted in any case. 166297Seric ** 167297Seric ** Returns: 1683149Seric ** A pointer to a vector of tokens. 169297Seric ** NULL on error. 170297Seric ** 171297Seric ** Side Effects: 1723149Seric ** none. 173297Seric */ 174297Seric 1758078Seric /* states and character types */ 1768078Seric # define OPR 0 /* operator */ 1778078Seric # define ATM 1 /* atom */ 1788078Seric # define QST 2 /* in quoted string */ 1798078Seric # define SPC 3 /* chewing up spaces */ 1808078Seric # define ONE 4 /* pick up one character */ 1813149Seric 1828078Seric # define NSTATES 5 /* number of states */ 1838078Seric # define TYPE 017 /* mask to select state type */ 1848078Seric 1858078Seric /* meta bits for table */ 1868078Seric # define M 020 /* meta character; don't pass through */ 1878078Seric # define B 040 /* cause a break */ 1888078Seric # define MB M|B /* meta-break */ 1898078Seric 1908078Seric static short StateTab[NSTATES][NSTATES] = 1918078Seric { 1928087Seric /* oldst chtype> OPR ATM QST SPC ONE */ 1939051Seric /*OPR*/ OPR|B, ATM|B, QST|B, SPC|MB, ONE|B, 1949051Seric /*ATM*/ OPR|B, ATM, QST|B, SPC|MB, ONE|B, 1959051Seric /*QST*/ QST, QST, OPR, QST, QST, 1968078Seric /*SPC*/ OPR, ATM, QST, SPC|M, ONE, 1978078Seric /*ONE*/ OPR, OPR, OPR, OPR, OPR, 1988078Seric }; 1998078Seric 2008078Seric # define NOCHAR -1 /* signal nothing in lookahead token */ 2018078Seric 2028078Seric char *DelimChar; /* set to point to the delimiter */ 2038078Seric 2043149Seric char ** 2053149Seric prescan(addr, delim) 206297Seric char *addr; 207297Seric char delim; 208297Seric { 209297Seric register char *p; 2108078Seric register char *q; 2119346Seric register int c; 2123149Seric char **avp; 213297Seric bool bslashmode; 214297Seric int cmntcnt; 2158423Seric int anglecnt; 2163149Seric char *tok; 2178078Seric int state; 2188078Seric int newstate; 2198078Seric static char buf[MAXNAME+MAXATOM]; 2208078Seric static char *av[MAXATOM+1]; 221297Seric 222297Seric q = buf; 2233149Seric bslashmode = FALSE; 2247800Seric cmntcnt = 0; 2258423Seric anglecnt = 0; 2263149Seric avp = av; 2278078Seric state = OPR; 2288078Seric c = NOCHAR; 2298078Seric p = addr; 2308078Seric # ifdef DEBUG 2318078Seric if (tTd(22, 45)) 232297Seric { 2338078Seric printf("prescan: "); 2348078Seric xputs(p); 2358078Seric putchar('\n'); 2368078Seric } 2378078Seric # endif DEBUG 2388078Seric 2398078Seric do 2408078Seric { 2413149Seric /* read a token */ 2423149Seric tok = q; 2438078Seric for (;;) 244297Seric { 2458078Seric /* store away any old lookahead character */ 2468078Seric if (c != NOCHAR) 2478078Seric { 2488078Seric /* squirrel it away */ 2498078Seric if (q >= &buf[sizeof buf - 5]) 2508078Seric { 2518078Seric usrerr("Address too long"); 2528078Seric DelimChar = p; 2538078Seric return (NULL); 2548078Seric } 2558078Seric *q++ = c; 2568078Seric } 2578078Seric 2588078Seric /* read a new input character */ 2598078Seric c = *p++; 2608078Seric if (c == '\0') 2618078Seric break; 2628078Seric # ifdef DEBUG 2638078Seric if (tTd(22, 101)) 2648078Seric printf("c=%c, s=%d; ", c, state); 2658078Seric # endif DEBUG 2668078Seric 2673149Seric /* chew up special characters */ 2684100Seric c &= ~0200; 2693149Seric *q = '\0'; 2703149Seric if (bslashmode) 2713149Seric { 2723149Seric c |= 0200; 2733149Seric bslashmode = FALSE; 2743149Seric } 2753149Seric else if (c == '\\') 2763149Seric { 2773149Seric bslashmode = TRUE; 2788078Seric c = NOCHAR; 2793149Seric } 2808514Seric else if (state == QST) 2818514Seric { 2828514Seric /* do nothing, just avoid next clauses */ 2838514Seric } 2848078Seric else if (c == '(') 2854100Seric { 2868078Seric cmntcnt++; 2878078Seric c = NOCHAR; 2884100Seric } 2898078Seric else if (c == ')') 2903149Seric { 2918078Seric if (cmntcnt <= 0) 2923149Seric { 2938078Seric usrerr("Unbalanced ')'"); 2948078Seric DelimChar = p; 2958078Seric return (NULL); 2963149Seric } 2978078Seric else 2988078Seric cmntcnt--; 2998078Seric } 3008078Seric else if (cmntcnt > 0) 3018078Seric c = NOCHAR; 3028423Seric else if (c == '<') 3038423Seric anglecnt++; 3048423Seric else if (c == '>') 3058423Seric { 3068423Seric if (anglecnt <= 0) 3078423Seric { 3088423Seric usrerr("Unbalanced '>'"); 3098423Seric DelimChar = p; 3108423Seric return (NULL); 3118423Seric } 3128423Seric anglecnt--; 3138423Seric } 314*11423Seric else if (delim == ' ' && isspace(c)) 315*11423Seric c = ' '; 3163149Seric 3178078Seric if (c == NOCHAR) 3188078Seric continue; 3193149Seric 3208078Seric /* see if this is end of input */ 32111405Seric if (c == delim && anglecnt <= 0 && state != QST) 3223149Seric break; 3233149Seric 3248078Seric newstate = StateTab[state][toktype(c)]; 3258078Seric # ifdef DEBUG 3268078Seric if (tTd(22, 101)) 3278078Seric printf("ns=%02o\n", newstate); 3288078Seric # endif DEBUG 3298078Seric state = newstate & TYPE; 3308078Seric if (bitset(M, newstate)) 3318078Seric c = NOCHAR; 3328078Seric if (bitset(B, newstate)) 3334228Seric break; 334297Seric } 3353149Seric 3363149Seric /* new token */ 3378078Seric if (tok != q) 3381378Seric { 3398078Seric *q++ = '\0'; 3408078Seric # ifdef DEBUG 3418078Seric if (tTd(22, 36)) 342297Seric { 3438078Seric printf("tok="); 3448078Seric xputs(tok); 3458078Seric putchar('\n'); 346297Seric } 3478078Seric # endif DEBUG 3488078Seric if (avp >= &av[MAXATOM]) 349297Seric { 3508078Seric syserr("prescan: too many tokens"); 3518078Seric DelimChar = p; 3528078Seric return (NULL); 353297Seric } 3548078Seric *avp++ = tok; 355297Seric } 3568423Seric } while (c != '\0' && (c != delim || anglecnt > 0)); 3573149Seric *avp = NULL; 3588078Seric DelimChar = --p; 3593149Seric if (cmntcnt > 0) 3603149Seric usrerr("Unbalanced '('"); 3618423Seric else if (anglecnt > 0) 3628423Seric usrerr("Unbalanced '<'"); 3638078Seric else if (state == QST) 3643149Seric usrerr("Unbalanced '\"'"); 3653149Seric else if (av[0] != NULL) 3663149Seric return (av); 3673149Seric return (NULL); 3683149Seric } 3693149Seric /* 3703149Seric ** TOKTYPE -- return token type 3713149Seric ** 3723149Seric ** Parameters: 3733149Seric ** c -- the character in question. 3743149Seric ** 3753149Seric ** Returns: 3763149Seric ** Its type. 3773149Seric ** 3783149Seric ** Side Effects: 3793149Seric ** none. 3803149Seric */ 381297Seric 3823149Seric toktype(c) 3833149Seric register char c; 3843149Seric { 3853380Seric static char buf[50]; 3863382Seric static bool firstime = TRUE; 3873380Seric 3883382Seric if (firstime) 3893380Seric { 3903382Seric firstime = FALSE; 3916977Seric expand("$o", buf, &buf[sizeof buf - 1], CurEnv); 3927005Seric (void) strcat(buf, DELIMCHARS); 3933380Seric } 3949585Seric if (c == MATCHCLASS || c == MATCHREPL || c == MATCHNCLASS) 3958078Seric return (ONE); 3968078Seric if (c == '"') 3978078Seric return (QST); 3984100Seric if (!isascii(c)) 3998078Seric return (ATM); 4008078Seric if (isspace(c) || c == ')') 4018078Seric return (SPC); 4023380Seric if (iscntrl(c) || index(buf, c) != NULL) 4038078Seric return (OPR); 4048078Seric return (ATM); 4053149Seric } 4063149Seric /* 4073149Seric ** REWRITE -- apply rewrite rules to token vector. 4083149Seric ** 4094476Seric ** This routine is an ordered production system. Each rewrite 4104476Seric ** rule has a LHS (called the pattern) and a RHS (called the 4114476Seric ** rewrite); 'rwr' points the the current rewrite rule. 4124476Seric ** 4134476Seric ** For each rewrite rule, 'avp' points the address vector we 4144476Seric ** are trying to match against, and 'pvp' points to the pattern. 4158058Seric ** If pvp points to a special match value (MATCHZANY, MATCHANY, 4169585Seric ** MATCHONE, MATCHCLASS, MATCHNCLASS) then the address in avp 4179585Seric ** matched is saved away in the match vector (pointed to by 'mvp'). 4184476Seric ** 4194476Seric ** When a match between avp & pvp does not match, we try to 4209585Seric ** back out. If we back up over MATCHONE, MATCHCLASS, or MATCHNCLASS 4214476Seric ** we must also back out the match in mvp. If we reach a 4228058Seric ** MATCHANY or MATCHZANY we just extend the match and start 4238058Seric ** over again. 4244476Seric ** 4254476Seric ** When we finally match, we rewrite the address vector 4264476Seric ** and try over again. 4274476Seric ** 4283149Seric ** Parameters: 4293149Seric ** pvp -- pointer to token vector. 4303149Seric ** 4313149Seric ** Returns: 4323149Seric ** none. 4333149Seric ** 4343149Seric ** Side Effects: 4353149Seric ** pvp is modified. 4363149Seric */ 4372091Seric 4383149Seric struct match 4393149Seric { 4404468Seric char **first; /* first token matched */ 4414468Seric char **last; /* last token matched */ 4423149Seric }; 4433149Seric 4444468Seric # define MAXMATCH 9 /* max params per rewrite */ 4453149Seric 4463149Seric 4474070Seric rewrite(pvp, ruleset) 4483149Seric char **pvp; 4494070Seric int ruleset; 4503149Seric { 4513149Seric register char *ap; /* address pointer */ 4523149Seric register char *rp; /* rewrite pointer */ 4533149Seric register char **avp; /* address vector pointer */ 4543149Seric register char **rvp; /* rewrite vector pointer */ 4558058Seric register struct match *mlp; /* cur ptr into mlist */ 4568058Seric register struct rewrite *rwr; /* pointer to current rewrite rule */ 4574468Seric struct match mlist[MAXMATCH]; /* stores match on LHS */ 4583149Seric char *npvp[MAXATOM+1]; /* temporary space for rebuild */ 4594060Seric extern bool sameword(); 4603149Seric 4619279Seric if (OpMode == MD_TEST || tTd(21, 2)) 4623149Seric { 4638959Seric printf("rewrite: ruleset %2d input:", ruleset); 4643149Seric printav(pvp); 4653149Seric } 4668423Seric if (pvp == NULL) 4678423Seric return; 4683149Seric 4693149Seric /* 4703149Seric ** Run through the list of rewrite rules, applying 4713149Seric ** any that match. 4723149Seric */ 4733149Seric 4744070Seric for (rwr = RewriteRules[ruleset]; rwr != NULL; ) 4753149Seric { 4764100Seric # ifdef DEBUG 4777675Seric if (tTd(21, 12)) 478297Seric { 4798069Seric printf("-----trying rule:"); 4803149Seric printav(rwr->r_lhs); 4813149Seric } 4824100Seric # endif DEBUG 4833149Seric 4843149Seric /* try to match on this rule */ 4854468Seric mlp = mlist; 4868058Seric rvp = rwr->r_lhs; 4878058Seric avp = pvp; 4888058Seric while ((ap = *avp) != NULL || *rvp != NULL) 4893149Seric { 4903149Seric rp = *rvp; 4918058Seric # ifdef DEBUG 4928058Seric if (tTd(21, 35)) 4938058Seric { 4948069Seric printf("ap="); 4958058Seric xputs(ap); 4968069Seric printf(", rp="); 4978058Seric xputs(rp); 4988069Seric printf("\n"); 4998058Seric } 5008058Seric # endif DEBUG 5013149Seric if (rp == NULL) 502297Seric { 5033149Seric /* end-of-pattern before end-of-address */ 5048058Seric goto backup; 505297Seric } 5068058Seric if (ap == NULL && *rp != MATCHZANY) 5078058Seric { 5088058Seric /* end-of-input */ 5098058Seric break; 5108058Seric } 5113149Seric 5123149Seric switch (*rp) 5133149Seric { 5144060Seric register STAB *s; 5154060Seric 5164060Seric case MATCHCLASS: 5179585Seric case MATCHNCLASS: 5189585Seric /* match any token in (not in) a class */ 5194100Seric s = stab(ap, ST_CLASS, ST_FIND); 52010690Seric if (s == NULL || !bitnset(rp[1], s->s_class)) 5219585Seric { 5229585Seric if (*rp == MATCHCLASS) 5239585Seric goto backup; 5249585Seric } 5259585Seric else if (*rp == MATCHNCLASS) 5268058Seric goto backup; 5274468Seric 5284476Seric /* explicit fall-through */ 5294476Seric 5304476Seric case MATCHONE: 5314476Seric case MATCHANY: 5324476Seric /* match exactly one token */ 5338058Seric mlp->first = avp; 5348058Seric mlp->last = avp++; 5354468Seric mlp++; 5364060Seric break; 5374060Seric 5388058Seric case MATCHZANY: 5398058Seric /* match zero or more tokens */ 5408058Seric mlp->first = avp; 5418058Seric mlp->last = avp - 1; 5428058Seric mlp++; 5438058Seric break; 5448058Seric 5453149Seric default: 5463149Seric /* must have exact match */ 5474060Seric if (!sameword(rp, ap)) 5488058Seric goto backup; 5494468Seric avp++; 5503149Seric break; 5513149Seric } 5523149Seric 5533149Seric /* successful match on this token */ 5543149Seric rvp++; 5553149Seric continue; 5563149Seric 5578058Seric backup: 5583149Seric /* match failed -- back up */ 5593149Seric while (--rvp >= rwr->r_lhs) 5603149Seric { 5613149Seric rp = *rvp; 5628058Seric if (*rp == MATCHANY || *rp == MATCHZANY) 5634468Seric { 5644476Seric /* extend binding and continue */ 5658058Seric avp = ++mlp[-1].last; 5668058Seric avp++; 5674476Seric rvp++; 5683149Seric break; 5694468Seric } 5704476Seric avp--; 5719585Seric if (*rp == MATCHONE || *rp == MATCHCLASS || 5729585Seric *rp == MATCHNCLASS) 5733149Seric { 5744468Seric /* back out binding */ 5754468Seric mlp--; 5763149Seric } 5773149Seric } 5783149Seric 5793149Seric if (rvp < rwr->r_lhs) 5803149Seric { 5813149Seric /* total failure to match */ 5823149Seric break; 5833149Seric } 584297Seric } 5853149Seric 5863149Seric /* 5873149Seric ** See if we successfully matched 5883149Seric */ 5893149Seric 5909374Seric if (rvp < rwr->r_lhs || *rvp != NULL) 5913149Seric { 5924100Seric # ifdef DEBUG 5939374Seric if (tTd(21, 10)) 5949374Seric printf("----- rule fails\n"); 5954100Seric # endif DEBUG 5969374Seric rwr = rwr->r_next; 5979374Seric continue; 5989374Seric } 5993149Seric 6009374Seric rvp = rwr->r_rhs; 6019374Seric # ifdef DEBUG 6029374Seric if (tTd(21, 12)) 6039374Seric { 6049374Seric printf("-----rule matches:"); 6059374Seric printav(rvp); 6069374Seric } 6079374Seric # endif DEBUG 6089374Seric 6099374Seric rp = *rvp; 6109374Seric if (*rp == CANONUSER) 6119374Seric { 6129374Seric rvp++; 6139374Seric rwr = rwr->r_next; 6149374Seric } 6159374Seric else if (*rp == CANONHOST) 6169374Seric { 6179374Seric rvp++; 6189374Seric rwr = NULL; 6199374Seric } 6209374Seric else if (*rp == CANONNET) 6219374Seric rwr = NULL; 6229374Seric 6239374Seric /* substitute */ 6249374Seric for (avp = npvp; *rvp != NULL; rvp++) 6259374Seric { 6269374Seric register struct match *m; 6279374Seric register char **pp; 6289374Seric 6298058Seric rp = *rvp; 6309374Seric if (*rp != MATCHREPL) 6318058Seric { 6329374Seric if (avp >= &npvp[MAXATOM]) 6339374Seric { 6349374Seric syserr("rewrite: expansion too long"); 6359374Seric return; 6369374Seric } 6379374Seric *avp++ = rp; 6389374Seric continue; 6398069Seric } 6408058Seric 6419374Seric /* substitute from LHS */ 6429374Seric m = &mlist[rp[1] - '1']; 6439374Seric # ifdef DEBUG 6449374Seric if (tTd(21, 15)) 6453149Seric { 6469374Seric printf("$%c:", rp[1]); 6479374Seric pp = m->first; 6489374Seric while (pp <= m->last) 6493149Seric { 6509374Seric printf(" %x=\"", *pp); 6519374Seric (void) fflush(stdout); 6529374Seric printf("%s\"", *pp++); 6533149Seric } 6549374Seric printf("\n"); 6553149Seric } 6568226Seric # endif DEBUG 6579374Seric pp = m->first; 6589374Seric while (pp <= m->last) 6598226Seric { 6609374Seric if (avp >= &npvp[MAXATOM]) 6619374Seric { 6629374Seric syserr("rewrite: expansion too long"); 6639374Seric return; 6649374Seric } 6659374Seric *avp++ = *pp++; 6668226Seric } 6679374Seric } 6689374Seric *avp++ = NULL; 6699374Seric if (**npvp == CALLSUBR) 6709374Seric { 6719374Seric bmove((char *) &npvp[2], (char *) pvp, 6729374Seric (avp - npvp - 2) * sizeof *avp); 6738226Seric # ifdef DEBUG 6749374Seric if (tTd(21, 3)) 6759374Seric printf("-----callsubr %s\n", npvp[1]); 6763149Seric # endif DEBUG 6779374Seric rewrite(pvp, atoi(npvp[1])); 6783149Seric } 6793149Seric else 6803149Seric { 6819374Seric bmove((char *) npvp, (char *) pvp, 6829374Seric (avp - npvp) * sizeof *avp); 6839374Seric } 6844100Seric # ifdef DEBUG 6859374Seric if (tTd(21, 4)) 6869374Seric { 6879374Seric printf("rewritten as:"); 6889374Seric printav(pvp); 6899374Seric } 6904100Seric # endif DEBUG 691297Seric } 6928069Seric 6939279Seric if (OpMode == MD_TEST || tTd(21, 2)) 6948069Seric { 6958959Seric printf("rewrite: ruleset %2d returns:", ruleset); 6968069Seric printav(pvp); 6978069Seric } 6983149Seric } 6993149Seric /* 7003149Seric ** BUILDADDR -- build address from token vector. 7013149Seric ** 7023149Seric ** Parameters: 7033149Seric ** tv -- token vector. 7043149Seric ** a -- pointer to address descriptor to fill. 7053149Seric ** If NULL, one will be allocated. 7063149Seric ** 7073149Seric ** Returns: 7084279Seric ** NULL if there was an error. 7094279Seric ** 'a' otherwise. 7103149Seric ** 7113149Seric ** Side Effects: 7123149Seric ** fills in 'a' 7133149Seric */ 7143149Seric 7153149Seric ADDRESS * 7163149Seric buildaddr(tv, a) 7173149Seric register char **tv; 7183149Seric register ADDRESS *a; 7193149Seric { 7203149Seric static char buf[MAXNAME]; 7213149Seric struct mailer **mp; 7223149Seric register struct mailer *m; 7234635Seric extern bool sameword(); 7243149Seric 7253149Seric if (a == NULL) 7263149Seric a = (ADDRESS *) xalloc(sizeof *a); 7274988Seric clear((char *) a, sizeof *a); 7283149Seric 7293149Seric /* figure out what net/mailer to use */ 7303149Seric if (**tv != CANONNET) 7314279Seric { 7323149Seric syserr("buildaddr: no net"); 7334279Seric return (NULL); 7344279Seric } 7353149Seric tv++; 7364635Seric if (sameword(*tv, "error")) 7374279Seric { 73810183Seric if (**++tv == CANONHOST) 73910183Seric { 74010183Seric setstat(atoi(*++tv)); 74110183Seric tv++; 74210183Seric } 74310183Seric if (**tv != CANONUSER) 7444279Seric syserr("buildaddr: error: no user"); 7454279Seric buf[0] = '\0'; 7464279Seric while (*++tv != NULL) 7474279Seric { 7484279Seric if (buf[0] != '\0') 7497005Seric (void) strcat(buf, " "); 7507005Seric (void) strcat(buf, *tv); 7514279Seric } 7524279Seric usrerr(buf); 7534279Seric return (NULL); 7544279Seric } 7554598Seric for (mp = Mailer; (m = *mp++) != NULL; ) 7563149Seric { 7574635Seric if (sameword(m->m_name, *tv)) 7583149Seric break; 7593149Seric } 7603149Seric if (m == NULL) 7614279Seric { 7623149Seric syserr("buildaddr: unknown net %s", *tv); 7634279Seric return (NULL); 7644279Seric } 7654598Seric a->q_mailer = m; 7663149Seric 7673149Seric /* figure out what host (if any) */ 7683149Seric tv++; 76910690Seric if (!bitnset(M_LOCAL, m->m_flags)) 7703149Seric { 7715704Seric if (**tv++ != CANONHOST) 7724279Seric { 7733149Seric syserr("buildaddr: no host"); 7744279Seric return (NULL); 7754279Seric } 7765704Seric buf[0] = '\0'; 7775704Seric while (*tv != NULL && **tv != CANONUSER) 7787005Seric (void) strcat(buf, *tv++); 7795704Seric a->q_host = newstr(buf); 7803149Seric } 7813149Seric else 7823149Seric a->q_host = NULL; 7833149Seric 7843149Seric /* figure out the user */ 7853149Seric if (**tv != CANONUSER) 7864279Seric { 7873149Seric syserr("buildaddr: no user"); 7884279Seric return (NULL); 7894279Seric } 79011278Seric rewrite(++tv, 4); 79111278Seric cataddr(tv, buf, sizeof buf); 7923149Seric a->q_user = buf; 7933149Seric 7943149Seric return (a); 7953149Seric } 7963188Seric /* 7974228Seric ** CATADDR -- concatenate pieces of addresses (putting in <LWSP> subs) 7984228Seric ** 7994228Seric ** Parameters: 8004228Seric ** pvp -- parameter vector to rebuild. 8014228Seric ** buf -- buffer to build the string into. 8024228Seric ** sz -- size of buf. 8034228Seric ** 8044228Seric ** Returns: 8054228Seric ** none. 8064228Seric ** 8074228Seric ** Side Effects: 8084228Seric ** Destroys buf. 8094228Seric */ 8104228Seric 8114228Seric cataddr(pvp, buf, sz) 8124228Seric char **pvp; 8134228Seric char *buf; 8144228Seric register int sz; 8154228Seric { 8164228Seric bool oatomtok = FALSE; 8174228Seric bool natomtok = FALSE; 8184228Seric register int i; 8194228Seric register char *p; 8204228Seric 8218423Seric if (pvp == NULL) 8228423Seric { 8238423Seric strcpy(buf, ""); 8248423Seric return; 8258423Seric } 8264228Seric p = buf; 82711156Seric sz -= 2; 8284228Seric while (*pvp != NULL && (i = strlen(*pvp)) < sz) 8294228Seric { 8308078Seric natomtok = (toktype(**pvp) == ATM); 8314228Seric if (oatomtok && natomtok) 8329042Seric *p++ = SpaceSub; 8334228Seric (void) strcpy(p, *pvp); 8344228Seric oatomtok = natomtok; 8354228Seric p += i; 83611156Seric sz -= i + 1; 8374228Seric pvp++; 8384228Seric } 8394228Seric *p = '\0'; 8404228Seric } 8414228Seric /* 8423188Seric ** SAMEADDR -- Determine if two addresses are the same 8433188Seric ** 8443188Seric ** This is not just a straight comparison -- if the mailer doesn't 8453188Seric ** care about the host we just ignore it, etc. 8463188Seric ** 8473188Seric ** Parameters: 8483188Seric ** a, b -- pointers to the internal forms to compare. 8493188Seric ** 8503188Seric ** Returns: 8513188Seric ** TRUE -- they represent the same mailbox. 8523188Seric ** FALSE -- they don't. 8533188Seric ** 8543188Seric ** Side Effects: 8553188Seric ** none. 8563188Seric */ 8573188Seric 8583188Seric bool 8599374Seric sameaddr(a, b) 8603188Seric register ADDRESS *a; 8613188Seric register ADDRESS *b; 8623188Seric { 8633188Seric /* if they don't have the same mailer, forget it */ 8643188Seric if (a->q_mailer != b->q_mailer) 8653188Seric return (FALSE); 8663188Seric 8673188Seric /* if the user isn't the same, we can drop out */ 8689374Seric if (strcmp(a->q_user, b->q_user) != 0) 8693188Seric return (FALSE); 8703188Seric 8713188Seric /* if the mailer ignores hosts, we have succeeded! */ 87210690Seric if (bitnset(M_LOCAL, a->q_mailer->m_flags)) 8733188Seric return (TRUE); 8743188Seric 8753188Seric /* otherwise compare hosts (but be careful for NULL ptrs) */ 8763188Seric if (a->q_host == NULL || b->q_host == NULL) 8773188Seric return (FALSE); 8783188Seric if (strcmp(a->q_host, b->q_host) != 0) 8793188Seric return (FALSE); 8803188Seric 8813188Seric return (TRUE); 8823188Seric } 8833234Seric /* 8843234Seric ** PRINTADDR -- print address (for debugging) 8853234Seric ** 8863234Seric ** Parameters: 8873234Seric ** a -- the address to print 8883234Seric ** follow -- follow the q_next chain. 8893234Seric ** 8903234Seric ** Returns: 8913234Seric ** none. 8923234Seric ** 8933234Seric ** Side Effects: 8943234Seric ** none. 8953234Seric */ 8963234Seric 8974317Seric # ifdef DEBUG 8984317Seric 8993234Seric printaddr(a, follow) 9003234Seric register ADDRESS *a; 9013234Seric bool follow; 9023234Seric { 9035001Seric bool first = TRUE; 9045001Seric 9053234Seric while (a != NULL) 9063234Seric { 9075001Seric first = FALSE; 9084443Seric printf("%x=", a); 9094085Seric (void) fflush(stdout); 9103234Seric printf("%s: mailer %d (%s), host `%s', user `%s'\n", a->q_paddr, 9118181Seric a->q_mailer->m_mno, a->q_mailer->m_name, a->q_host, 9128181Seric a->q_user); 9138181Seric printf("\tnext=%x, flags=%o, alias %x\n", a->q_next, a->q_flags, 9148181Seric a->q_alias); 9158181Seric printf("\thome=\"%s\", fullname=\"%s\"\n", a->q_home, 9168181Seric a->q_fullname); 9174996Seric 9183234Seric if (!follow) 9193234Seric return; 9204996Seric a = a->q_next; 9213234Seric } 9225001Seric if (first) 9234443Seric printf("[NULL]\n"); 9243234Seric } 9254317Seric 9264317Seric # endif DEBUG 9277682Seric /* 9287682Seric ** REMOTENAME -- return the name relative to the current mailer 9297682Seric ** 9307682Seric ** Parameters: 9317682Seric ** name -- the name to translate. 9328069Seric ** m -- the mailer that we want to do rewriting relative 9338069Seric ** to. 9348069Seric ** senderaddress -- if set, uses the sender rewriting rules 9358069Seric ** rather than the recipient rewriting rules. 93610310Seric ** canonical -- if set, strip out any comment information, 93710310Seric ** etc. 9387682Seric ** 9397682Seric ** Returns: 9407682Seric ** the text string representing this address relative to 9417682Seric ** the receiving mailer. 9427682Seric ** 9437682Seric ** Side Effects: 9447682Seric ** none. 9457682Seric ** 9467682Seric ** Warnings: 9477682Seric ** The text string returned is tucked away locally; 9487682Seric ** copy it if you intend to save it. 9497682Seric */ 9507682Seric 9517682Seric char * 95210310Seric remotename(name, m, senderaddress, canonical) 9537682Seric char *name; 9547682Seric struct mailer *m; 9558069Seric bool senderaddress; 95610310Seric bool canonical; 9577682Seric { 9588069Seric register char **pvp; 9598069Seric char *fancy; 9608069Seric extern char *macvalue(); 9618181Seric char *oldg = macvalue('g', CurEnv); 9627682Seric static char buf[MAXNAME]; 9637682Seric char lbuf[MAXNAME]; 9647682Seric extern char **prescan(); 9657889Seric extern char *crackaddr(); 9667682Seric 9677755Seric # ifdef DEBUG 9687755Seric if (tTd(12, 1)) 9697755Seric printf("remotename(%s)\n", name); 9707755Seric # endif DEBUG 9717755Seric 97210177Seric /* don't do anything if we are tagging it as special */ 97310177Seric if ((senderaddress ? m->m_s_rwset : m->m_r_rwset) < 0) 97410177Seric return (name); 97510177Seric 9767682Seric /* 9778181Seric ** Do a heuristic crack of this name to extract any comment info. 9788181Seric ** This will leave the name as a comment and a $g macro. 9797889Seric */ 9807889Seric 98110310Seric if (canonical) 98210310Seric fancy = "$g"; 98310310Seric else 98410310Seric fancy = crackaddr(name); 9857889Seric 9868181Seric /* 9878181Seric ** Turn the name into canonical form. 9888181Seric ** Normally this will be RFC 822 style, i.e., "user@domain". 9898181Seric ** If this only resolves to "user", and the "C" flag is 9908181Seric ** specified in the sending mailer, then the sender's 9918181Seric ** domain will be appended. 9928181Seric */ 9938181Seric 9947889Seric pvp = prescan(name, '\0'); 9957889Seric if (pvp == NULL) 9967889Seric return (name); 9978181Seric rewrite(pvp, 3); 9988181Seric if (CurEnv->e_fromdomain != NULL) 9998181Seric { 10008181Seric /* append from domain to this address */ 10018181Seric register char **pxp = pvp; 10028181Seric 10039594Seric /* see if there is an "@domain" in the current name */ 10048181Seric while (*pxp != NULL && strcmp(*pxp, "@") != 0) 10058181Seric pxp++; 10068181Seric if (*pxp == NULL) 10078181Seric { 10089594Seric /* no.... append the "@domain" from the sender */ 10098181Seric register char **qxq = CurEnv->e_fromdomain; 10108181Seric 10119594Seric while ((*pxp++ = *qxq++) != NULL) 10129594Seric continue; 10138181Seric } 10148181Seric } 10158181Seric 10168181Seric /* 10178959Seric ** Do more specific rewriting. 10188181Seric ** Rewrite using ruleset 1 or 2 depending on whether this is 10198181Seric ** a sender address or not. 10208181Seric ** Then run it through any receiving-mailer-specific rulesets. 10218181Seric */ 10228181Seric 10238069Seric if (senderaddress) 10247755Seric { 10257889Seric rewrite(pvp, 1); 10268069Seric if (m->m_s_rwset > 0) 10278069Seric rewrite(pvp, m->m_s_rwset); 10288069Seric } 10298069Seric else 10308069Seric { 10317889Seric rewrite(pvp, 2); 10328069Seric if (m->m_r_rwset > 0) 10338069Seric rewrite(pvp, m->m_r_rwset); 10347682Seric } 10357682Seric 10368181Seric /* 10378959Seric ** Do any final sanitation the address may require. 10388959Seric ** This will normally be used to turn internal forms 10398959Seric ** (e.g., user@host.LOCAL) into external form. This 10408959Seric ** may be used as a default to the above rules. 10418959Seric */ 10428959Seric 10438959Seric rewrite(pvp, 4); 10448959Seric 10458959Seric /* 10468181Seric ** Now restore the comment information we had at the beginning. 10478181Seric */ 10488181Seric 10497682Seric cataddr(pvp, lbuf, sizeof lbuf); 10509374Seric define('g', lbuf, CurEnv); 10517889Seric expand(fancy, buf, &buf[sizeof buf - 1], CurEnv); 10529374Seric define('g', oldg, CurEnv); 10537682Seric 10547682Seric # ifdef DEBUG 10557682Seric if (tTd(12, 1)) 10567755Seric printf("remotename => `%s'\n", buf); 10577682Seric # endif DEBUG 10587682Seric return (buf); 10597682Seric } 1060