13312Seric # include "sendmail.h" 2297Seric 3*9374Seric SCCSID(@(#)parseaddr.c 3.67 11/28/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 41*9374Seric /* following delimiters are inherent to the internal algorithms */ 423380Seric # define DELIMCHARS "$()<>,;\\\"\r\n" /* word delimiters */ 432091Seric 442973Seric ADDRESS * 45297Seric parse(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)) 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 ** 149*9374Seric ** Scans a name and turns it into a set of tokens. This process 150*9374Seric ** 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 } 3143149Seric 3158078Seric if (c == NOCHAR) 3168078Seric continue; 3173149Seric 3188078Seric /* see if this is end of input */ 3198423Seric if (c == delim && anglecnt <= 0) 3203149Seric break; 3213149Seric 3228078Seric newstate = StateTab[state][toktype(c)]; 3238078Seric # ifdef DEBUG 3248078Seric if (tTd(22, 101)) 3258078Seric printf("ns=%02o\n", newstate); 3268078Seric # endif DEBUG 3278078Seric state = newstate & TYPE; 3288078Seric if (bitset(M, newstate)) 3298078Seric c = NOCHAR; 3308078Seric if (bitset(B, newstate)) 3314228Seric break; 332297Seric } 3333149Seric 3343149Seric /* new token */ 3358078Seric if (tok != q) 3361378Seric { 3378078Seric *q++ = '\0'; 3388078Seric # ifdef DEBUG 3398078Seric if (tTd(22, 36)) 340297Seric { 3418078Seric printf("tok="); 3428078Seric xputs(tok); 3438078Seric putchar('\n'); 344297Seric } 3458078Seric # endif DEBUG 3468078Seric if (avp >= &av[MAXATOM]) 347297Seric { 3488078Seric syserr("prescan: too many tokens"); 3498078Seric DelimChar = p; 3508078Seric return (NULL); 351297Seric } 3528078Seric *avp++ = tok; 353297Seric } 3548423Seric } while (c != '\0' && (c != delim || anglecnt > 0)); 3553149Seric *avp = NULL; 3568078Seric DelimChar = --p; 3573149Seric if (cmntcnt > 0) 3583149Seric usrerr("Unbalanced '('"); 3598423Seric else if (anglecnt > 0) 3608423Seric usrerr("Unbalanced '<'"); 3618078Seric else if (state == QST) 3623149Seric usrerr("Unbalanced '\"'"); 3633149Seric else if (av[0] != NULL) 3643149Seric return (av); 3653149Seric return (NULL); 3663149Seric } 3673149Seric /* 3683149Seric ** TOKTYPE -- return token type 3693149Seric ** 3703149Seric ** Parameters: 3713149Seric ** c -- the character in question. 3723149Seric ** 3733149Seric ** Returns: 3743149Seric ** Its type. 3753149Seric ** 3763149Seric ** Side Effects: 3773149Seric ** none. 3783149Seric */ 379297Seric 3803149Seric toktype(c) 3813149Seric register char c; 3823149Seric { 3833380Seric static char buf[50]; 3843382Seric static bool firstime = TRUE; 3853380Seric 3863382Seric if (firstime) 3873380Seric { 3883382Seric firstime = FALSE; 3896977Seric expand("$o", buf, &buf[sizeof buf - 1], CurEnv); 3907005Seric (void) strcat(buf, DELIMCHARS); 3913380Seric } 3926053Seric if (c == MATCHCLASS || c == MATCHREPL) 3938078Seric return (ONE); 3948078Seric if (c == '"') 3958078Seric return (QST); 3964100Seric if (!isascii(c)) 3978078Seric return (ATM); 3988078Seric if (isspace(c) || c == ')') 3998078Seric return (SPC); 4003380Seric if (iscntrl(c) || index(buf, c) != NULL) 4018078Seric return (OPR); 4028078Seric return (ATM); 4033149Seric } 4043149Seric /* 4053149Seric ** REWRITE -- apply rewrite rules to token vector. 4063149Seric ** 4074476Seric ** This routine is an ordered production system. Each rewrite 4084476Seric ** rule has a LHS (called the pattern) and a RHS (called the 4094476Seric ** rewrite); 'rwr' points the the current rewrite rule. 4104476Seric ** 4114476Seric ** For each rewrite rule, 'avp' points the address vector we 4124476Seric ** are trying to match against, and 'pvp' points to the pattern. 4138058Seric ** If pvp points to a special match value (MATCHZANY, MATCHANY, 4148058Seric ** MATCHONE, MATCHCLASS) then the address in avp matched is 4158058Seric ** saved away in the match vector (pointed to by 'mvp'). 4164476Seric ** 4174476Seric ** When a match between avp & pvp does not match, we try to 4184476Seric ** back out. If we back up over a MATCHONE or a MATCHCLASS 4194476Seric ** we must also back out the match in mvp. If we reach a 4208058Seric ** MATCHANY or MATCHZANY we just extend the match and start 4218058Seric ** over again. 4224476Seric ** 4234476Seric ** When we finally match, we rewrite the address vector 4244476Seric ** and try over again. 4254476Seric ** 4263149Seric ** Parameters: 4273149Seric ** pvp -- pointer to token vector. 4283149Seric ** 4293149Seric ** Returns: 4303149Seric ** none. 4313149Seric ** 4323149Seric ** Side Effects: 4333149Seric ** pvp is modified. 4343149Seric */ 4352091Seric 4363149Seric struct match 4373149Seric { 4384468Seric char **first; /* first token matched */ 4394468Seric char **last; /* last token matched */ 4403149Seric }; 4413149Seric 4424468Seric # define MAXMATCH 9 /* max params per rewrite */ 4433149Seric 4443149Seric 4454070Seric rewrite(pvp, ruleset) 4463149Seric char **pvp; 4474070Seric int ruleset; 4483149Seric { 4493149Seric register char *ap; /* address pointer */ 4503149Seric register char *rp; /* rewrite pointer */ 4513149Seric register char **avp; /* address vector pointer */ 4523149Seric register char **rvp; /* rewrite vector pointer */ 4538058Seric register struct match *mlp; /* cur ptr into mlist */ 4548058Seric register struct rewrite *rwr; /* pointer to current rewrite rule */ 4554468Seric struct match mlist[MAXMATCH]; /* stores match on LHS */ 4563149Seric char *npvp[MAXATOM+1]; /* temporary space for rebuild */ 4574060Seric extern bool sameword(); 4583149Seric 4599279Seric if (OpMode == MD_TEST || tTd(21, 2)) 4603149Seric { 4618959Seric printf("rewrite: ruleset %2d input:", ruleset); 4623149Seric printav(pvp); 4633149Seric } 4648423Seric if (pvp == NULL) 4658423Seric return; 4663149Seric 4673149Seric /* 4683149Seric ** Run through the list of rewrite rules, applying 4693149Seric ** any that match. 4703149Seric */ 4713149Seric 4724070Seric for (rwr = RewriteRules[ruleset]; rwr != NULL; ) 4733149Seric { 4744100Seric # ifdef DEBUG 4757675Seric if (tTd(21, 12)) 476297Seric { 4778069Seric printf("-----trying rule:"); 4783149Seric printav(rwr->r_lhs); 4793149Seric } 4804100Seric # endif DEBUG 4813149Seric 4823149Seric /* try to match on this rule */ 4834468Seric mlp = mlist; 4848058Seric rvp = rwr->r_lhs; 4858058Seric avp = pvp; 4868058Seric while ((ap = *avp) != NULL || *rvp != NULL) 4873149Seric { 4883149Seric rp = *rvp; 4898058Seric # ifdef DEBUG 4908058Seric if (tTd(21, 35)) 4918058Seric { 4928069Seric printf("ap="); 4938058Seric xputs(ap); 4948069Seric printf(", rp="); 4958058Seric xputs(rp); 4968069Seric printf("\n"); 4978058Seric } 4988058Seric # endif DEBUG 4993149Seric if (rp == NULL) 500297Seric { 5013149Seric /* end-of-pattern before end-of-address */ 5028058Seric goto backup; 503297Seric } 5048058Seric if (ap == NULL && *rp != MATCHZANY) 5058058Seric { 5068058Seric /* end-of-input */ 5078058Seric break; 5088058Seric } 5093149Seric 5103149Seric switch (*rp) 5113149Seric { 5124060Seric register STAB *s; 5134060Seric register int class; 5144060Seric 5154060Seric case MATCHCLASS: 5164060Seric /* match any token in a class */ 5174060Seric class = rp[1]; 5184060Seric if (!isalpha(class)) 5198058Seric goto backup; 5204060Seric if (isupper(class)) 5214060Seric class -= 'A'; 5224060Seric else 5234060Seric class -= 'a'; 5244100Seric s = stab(ap, ST_CLASS, ST_FIND); 5256273Seric if (s == NULL || (s->s_class & (1L << class)) == 0) 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--; 5714476Seric if (*rp == MATCHONE || *rp == MATCHCLASS) 5723149Seric { 5734468Seric /* back out binding */ 5744468Seric mlp--; 5753149Seric } 5763149Seric } 5773149Seric 5783149Seric if (rvp < rwr->r_lhs) 5793149Seric { 5803149Seric /* total failure to match */ 5813149Seric break; 5823149Seric } 583297Seric } 5843149Seric 5853149Seric /* 5863149Seric ** See if we successfully matched 5873149Seric */ 5883149Seric 589*9374Seric if (rvp < rwr->r_lhs || *rvp != NULL) 5903149Seric { 5914100Seric # ifdef DEBUG 592*9374Seric if (tTd(21, 10)) 593*9374Seric printf("----- rule fails\n"); 5944100Seric # endif DEBUG 595*9374Seric rwr = rwr->r_next; 596*9374Seric continue; 597*9374Seric } 5983149Seric 599*9374Seric rvp = rwr->r_rhs; 600*9374Seric # ifdef DEBUG 601*9374Seric if (tTd(21, 12)) 602*9374Seric { 603*9374Seric printf("-----rule matches:"); 604*9374Seric printav(rvp); 605*9374Seric } 606*9374Seric # endif DEBUG 607*9374Seric 608*9374Seric rp = *rvp; 609*9374Seric if (*rp == CANONUSER) 610*9374Seric { 611*9374Seric rvp++; 612*9374Seric rwr = rwr->r_next; 613*9374Seric } 614*9374Seric else if (*rp == CANONHOST) 615*9374Seric { 616*9374Seric rvp++; 617*9374Seric rwr = NULL; 618*9374Seric } 619*9374Seric else if (*rp == CANONNET) 620*9374Seric rwr = NULL; 621*9374Seric 622*9374Seric /* substitute */ 623*9374Seric for (avp = npvp; *rvp != NULL; rvp++) 624*9374Seric { 625*9374Seric register struct match *m; 626*9374Seric register char **pp; 627*9374Seric 6288058Seric rp = *rvp; 629*9374Seric if (*rp != MATCHREPL) 6308058Seric { 631*9374Seric if (avp >= &npvp[MAXATOM]) 632*9374Seric { 633*9374Seric syserr("rewrite: expansion too long"); 634*9374Seric return; 635*9374Seric } 636*9374Seric *avp++ = rp; 637*9374Seric continue; 6388069Seric } 6398058Seric 640*9374Seric /* substitute from LHS */ 641*9374Seric m = &mlist[rp[1] - '1']; 642*9374Seric # ifdef DEBUG 643*9374Seric if (tTd(21, 15)) 6443149Seric { 645*9374Seric printf("$%c:", rp[1]); 646*9374Seric pp = m->first; 647*9374Seric while (pp <= m->last) 6483149Seric { 649*9374Seric printf(" %x=\"", *pp); 650*9374Seric (void) fflush(stdout); 651*9374Seric printf("%s\"", *pp++); 6523149Seric } 653*9374Seric printf("\n"); 6543149Seric } 6558226Seric # endif DEBUG 656*9374Seric pp = m->first; 657*9374Seric while (pp <= m->last) 6588226Seric { 659*9374Seric if (avp >= &npvp[MAXATOM]) 660*9374Seric { 661*9374Seric syserr("rewrite: expansion too long"); 662*9374Seric return; 663*9374Seric } 664*9374Seric *avp++ = *pp++; 6658226Seric } 666*9374Seric } 667*9374Seric *avp++ = NULL; 668*9374Seric if (**npvp == CALLSUBR) 669*9374Seric { 670*9374Seric bmove((char *) &npvp[2], (char *) pvp, 671*9374Seric (avp - npvp - 2) * sizeof *avp); 6728226Seric # ifdef DEBUG 673*9374Seric if (tTd(21, 3)) 674*9374Seric printf("-----callsubr %s\n", npvp[1]); 6753149Seric # endif DEBUG 676*9374Seric rewrite(pvp, atoi(npvp[1])); 6773149Seric } 6783149Seric else 6793149Seric { 680*9374Seric bmove((char *) npvp, (char *) pvp, 681*9374Seric (avp - npvp) * sizeof *avp); 682*9374Seric } 6834100Seric # ifdef DEBUG 684*9374Seric if (tTd(21, 4)) 685*9374Seric { 686*9374Seric printf("rewritten as:"); 687*9374Seric printav(pvp); 688*9374Seric } 6894100Seric # endif DEBUG 690297Seric } 6918069Seric 6929279Seric if (OpMode == MD_TEST || tTd(21, 2)) 6938069Seric { 6948959Seric printf("rewrite: ruleset %2d returns:", ruleset); 6958069Seric printav(pvp); 6968069Seric } 6973149Seric } 6983149Seric /* 6993149Seric ** BUILDADDR -- build address from token vector. 7003149Seric ** 7013149Seric ** Parameters: 7023149Seric ** tv -- token vector. 7033149Seric ** a -- pointer to address descriptor to fill. 7043149Seric ** If NULL, one will be allocated. 7053149Seric ** 7063149Seric ** Returns: 7074279Seric ** NULL if there was an error. 7084279Seric ** 'a' otherwise. 7093149Seric ** 7103149Seric ** Side Effects: 7113149Seric ** fills in 'a' 7123149Seric */ 7133149Seric 7143149Seric ADDRESS * 7153149Seric buildaddr(tv, a) 7163149Seric register char **tv; 7173149Seric register ADDRESS *a; 7183149Seric { 7193149Seric static char buf[MAXNAME]; 7203149Seric struct mailer **mp; 7213149Seric register struct mailer *m; 7224635Seric extern bool sameword(); 7233149Seric 7243149Seric if (a == NULL) 7253149Seric a = (ADDRESS *) xalloc(sizeof *a); 7264988Seric clear((char *) a, sizeof *a); 7273149Seric 7283149Seric /* figure out what net/mailer to use */ 7293149Seric if (**tv != CANONNET) 7304279Seric { 7313149Seric syserr("buildaddr: no net"); 7324279Seric return (NULL); 7334279Seric } 7343149Seric tv++; 7354635Seric if (sameword(*tv, "error")) 7364279Seric { 7374279Seric if (**++tv != CANONUSER) 7384279Seric syserr("buildaddr: error: no user"); 7394279Seric buf[0] = '\0'; 7404279Seric while (*++tv != NULL) 7414279Seric { 7424279Seric if (buf[0] != '\0') 7437005Seric (void) strcat(buf, " "); 7447005Seric (void) strcat(buf, *tv); 7454279Seric } 7464279Seric usrerr(buf); 7474279Seric return (NULL); 7484279Seric } 7494598Seric for (mp = Mailer; (m = *mp++) != NULL; ) 7503149Seric { 7514635Seric if (sameword(m->m_name, *tv)) 7523149Seric break; 7533149Seric } 7543149Seric if (m == NULL) 7554279Seric { 7563149Seric syserr("buildaddr: unknown net %s", *tv); 7574279Seric return (NULL); 7584279Seric } 7594598Seric a->q_mailer = m; 7603149Seric 7613149Seric /* figure out what host (if any) */ 7623149Seric tv++; 7634195Seric if (!bitset(M_LOCAL, m->m_flags)) 7643149Seric { 7655704Seric if (**tv++ != CANONHOST) 7664279Seric { 7673149Seric syserr("buildaddr: no host"); 7684279Seric return (NULL); 7694279Seric } 7705704Seric buf[0] = '\0'; 7715704Seric while (*tv != NULL && **tv != CANONUSER) 7727005Seric (void) strcat(buf, *tv++); 7735704Seric a->q_host = newstr(buf); 7743149Seric } 7753149Seric else 7763149Seric a->q_host = NULL; 7773149Seric 7783149Seric /* figure out the user */ 7793149Seric if (**tv != CANONUSER) 7804279Seric { 7813149Seric syserr("buildaddr: no user"); 7824279Seric return (NULL); 7834279Seric } 7844228Seric cataddr(++tv, buf, sizeof buf); 7853149Seric a->q_user = buf; 7863149Seric 7873149Seric return (a); 7883149Seric } 7893188Seric /* 7904228Seric ** CATADDR -- concatenate pieces of addresses (putting in <LWSP> subs) 7914228Seric ** 7924228Seric ** Parameters: 7934228Seric ** pvp -- parameter vector to rebuild. 7944228Seric ** buf -- buffer to build the string into. 7954228Seric ** sz -- size of buf. 7964228Seric ** 7974228Seric ** Returns: 7984228Seric ** none. 7994228Seric ** 8004228Seric ** Side Effects: 8014228Seric ** Destroys buf. 8024228Seric */ 8034228Seric 8044228Seric cataddr(pvp, buf, sz) 8054228Seric char **pvp; 8064228Seric char *buf; 8074228Seric register int sz; 8084228Seric { 8094228Seric bool oatomtok = FALSE; 8104228Seric bool natomtok = FALSE; 8114228Seric register int i; 8124228Seric register char *p; 8134228Seric 8148423Seric if (pvp == NULL) 8158423Seric { 8168423Seric strcpy(buf, ""); 8178423Seric return; 8188423Seric } 8194228Seric p = buf; 8204228Seric sz--; 8214228Seric while (*pvp != NULL && (i = strlen(*pvp)) < sz) 8224228Seric { 8238078Seric natomtok = (toktype(**pvp) == ATM); 8244228Seric if (oatomtok && natomtok) 8259042Seric *p++ = SpaceSub; 8264228Seric (void) strcpy(p, *pvp); 8274228Seric oatomtok = natomtok; 8284228Seric p += i; 8294228Seric sz -= i; 8304228Seric pvp++; 8314228Seric } 8324228Seric *p = '\0'; 8334228Seric } 8344228Seric /* 8353188Seric ** SAMEADDR -- Determine if two addresses are the same 8363188Seric ** 8373188Seric ** This is not just a straight comparison -- if the mailer doesn't 8383188Seric ** care about the host we just ignore it, etc. 8393188Seric ** 8403188Seric ** Parameters: 8413188Seric ** a, b -- pointers to the internal forms to compare. 8423188Seric ** 8433188Seric ** Returns: 8443188Seric ** TRUE -- they represent the same mailbox. 8453188Seric ** FALSE -- they don't. 8463188Seric ** 8473188Seric ** Side Effects: 8483188Seric ** none. 8493188Seric */ 8503188Seric 8513188Seric bool 852*9374Seric sameaddr(a, b) 8533188Seric register ADDRESS *a; 8543188Seric register ADDRESS *b; 8553188Seric { 8563188Seric /* if they don't have the same mailer, forget it */ 8573188Seric if (a->q_mailer != b->q_mailer) 8583188Seric return (FALSE); 8593188Seric 8603188Seric /* if the user isn't the same, we can drop out */ 861*9374Seric if (strcmp(a->q_user, b->q_user) != 0) 8623188Seric return (FALSE); 8633188Seric 8643188Seric /* if the mailer ignores hosts, we have succeeded! */ 8654598Seric if (bitset(M_LOCAL, a->q_mailer->m_flags)) 8663188Seric return (TRUE); 8673188Seric 8683188Seric /* otherwise compare hosts (but be careful for NULL ptrs) */ 8693188Seric if (a->q_host == NULL || b->q_host == NULL) 8703188Seric return (FALSE); 8713188Seric if (strcmp(a->q_host, b->q_host) != 0) 8723188Seric return (FALSE); 8733188Seric 8743188Seric return (TRUE); 8753188Seric } 8763234Seric /* 8773234Seric ** PRINTADDR -- print address (for debugging) 8783234Seric ** 8793234Seric ** Parameters: 8803234Seric ** a -- the address to print 8813234Seric ** follow -- follow the q_next chain. 8823234Seric ** 8833234Seric ** Returns: 8843234Seric ** none. 8853234Seric ** 8863234Seric ** Side Effects: 8873234Seric ** none. 8883234Seric */ 8893234Seric 8904317Seric # ifdef DEBUG 8914317Seric 8923234Seric printaddr(a, follow) 8933234Seric register ADDRESS *a; 8943234Seric bool follow; 8953234Seric { 8965001Seric bool first = TRUE; 8975001Seric 8983234Seric while (a != NULL) 8993234Seric { 9005001Seric first = FALSE; 9014443Seric printf("%x=", a); 9024085Seric (void) fflush(stdout); 9033234Seric printf("%s: mailer %d (%s), host `%s', user `%s'\n", a->q_paddr, 9048181Seric a->q_mailer->m_mno, a->q_mailer->m_name, a->q_host, 9058181Seric a->q_user); 9068181Seric printf("\tnext=%x, flags=%o, alias %x\n", a->q_next, a->q_flags, 9078181Seric a->q_alias); 9088181Seric printf("\thome=\"%s\", fullname=\"%s\"\n", a->q_home, 9098181Seric a->q_fullname); 9104996Seric 9113234Seric if (!follow) 9123234Seric return; 9134996Seric a = a->q_next; 9143234Seric } 9155001Seric if (first) 9164443Seric printf("[NULL]\n"); 9173234Seric } 9184317Seric 9194317Seric # endif DEBUG 9207682Seric /* 9217682Seric ** REMOTENAME -- return the name relative to the current mailer 9227682Seric ** 9237682Seric ** Parameters: 9247682Seric ** name -- the name to translate. 9258069Seric ** m -- the mailer that we want to do rewriting relative 9268069Seric ** to. 9278069Seric ** senderaddress -- if set, uses the sender rewriting rules 9288069Seric ** rather than the recipient rewriting rules. 9297682Seric ** 9307682Seric ** Returns: 9317682Seric ** the text string representing this address relative to 9327682Seric ** the receiving mailer. 9337682Seric ** 9347682Seric ** Side Effects: 9357682Seric ** none. 9367682Seric ** 9377682Seric ** Warnings: 9387682Seric ** The text string returned is tucked away locally; 9397682Seric ** copy it if you intend to save it. 9407682Seric */ 9417682Seric 9427682Seric char * 9438069Seric remotename(name, m, senderaddress) 9447682Seric char *name; 9457682Seric struct mailer *m; 9468069Seric bool senderaddress; 9477682Seric { 9488069Seric register char **pvp; 9498069Seric char *fancy; 9508069Seric extern char *macvalue(); 9518181Seric char *oldg = macvalue('g', CurEnv); 9527682Seric static char buf[MAXNAME]; 9537682Seric char lbuf[MAXNAME]; 9547682Seric extern char **prescan(); 9557889Seric extern char *crackaddr(); 9567682Seric 9577755Seric # ifdef DEBUG 9587755Seric if (tTd(12, 1)) 9597755Seric printf("remotename(%s)\n", name); 9607755Seric # endif DEBUG 9617755Seric 9627682Seric /* 9638181Seric ** Do a heuristic crack of this name to extract any comment info. 9648181Seric ** This will leave the name as a comment and a $g macro. 9657889Seric */ 9667889Seric 9677889Seric fancy = crackaddr(name); 9687889Seric 9698181Seric /* 9708181Seric ** Turn the name into canonical form. 9718181Seric ** Normally this will be RFC 822 style, i.e., "user@domain". 9728181Seric ** If this only resolves to "user", and the "C" flag is 9738181Seric ** specified in the sending mailer, then the sender's 9748181Seric ** domain will be appended. 9758181Seric */ 9768181Seric 9777889Seric pvp = prescan(name, '\0'); 9787889Seric if (pvp == NULL) 9797889Seric return (name); 9808181Seric rewrite(pvp, 3); 9818181Seric if (CurEnv->e_fromdomain != NULL) 9828181Seric { 9838181Seric /* append from domain to this address */ 9848181Seric register char **pxp = pvp; 9858181Seric 9868181Seric while (*pxp != NULL && strcmp(*pxp, "@") != 0) 9878181Seric pxp++; 9888181Seric if (*pxp == NULL) 9898181Seric { 9908181Seric register char **qxq = CurEnv->e_fromdomain; 9918181Seric 9928181Seric while (*qxq != NULL) 9938181Seric *pxp++ = *qxq++; 9948181Seric } 9958181Seric } 9968181Seric 9978181Seric /* 9988959Seric ** Do more specific rewriting. 9998181Seric ** Rewrite using ruleset 1 or 2 depending on whether this is 10008181Seric ** a sender address or not. 10018181Seric ** Then run it through any receiving-mailer-specific rulesets. 10028181Seric */ 10038181Seric 10048069Seric if (senderaddress) 10057755Seric { 10067889Seric rewrite(pvp, 1); 10078069Seric if (m->m_s_rwset > 0) 10088069Seric rewrite(pvp, m->m_s_rwset); 10098069Seric } 10108069Seric else 10118069Seric { 10127889Seric rewrite(pvp, 2); 10138069Seric if (m->m_r_rwset > 0) 10148069Seric rewrite(pvp, m->m_r_rwset); 10157682Seric } 10167682Seric 10178181Seric /* 10188959Seric ** Do any final sanitation the address may require. 10198959Seric ** This will normally be used to turn internal forms 10208959Seric ** (e.g., user@host.LOCAL) into external form. This 10218959Seric ** may be used as a default to the above rules. 10228959Seric */ 10238959Seric 10248959Seric rewrite(pvp, 4); 10258959Seric 10268959Seric /* 10278181Seric ** Now restore the comment information we had at the beginning. 10288181Seric */ 10298181Seric 10307682Seric cataddr(pvp, lbuf, sizeof lbuf); 1031*9374Seric define('g', lbuf, CurEnv); 10327889Seric expand(fancy, buf, &buf[sizeof buf - 1], CurEnv); 1033*9374Seric define('g', oldg, CurEnv); 10347682Seric 10357682Seric # ifdef DEBUG 10367682Seric if (tTd(12, 1)) 10377755Seric printf("remotename => `%s'\n", buf); 10387682Seric # endif DEBUG 10397682Seric return (buf); 10407682Seric } 10417682Seric /* 10427682Seric ** CANONNAME -- make name canonical 10437682Seric ** 10447682Seric ** This is used for SMTP and misc. printing. Given a print 10458181Seric ** address, it strips out comments, etc. 10467682Seric ** 10477682Seric ** Parameters: 10487682Seric ** name -- the name to make canonical. 10498353Seric ** ruleset -- the canonicalizing ruleset. 10507682Seric ** 10517682Seric ** Returns: 10527682Seric ** pointer to canonical name. 10537682Seric ** 10547682Seric ** Side Effects: 10557682Seric ** none. 10567682Seric ** 10577682Seric ** Warning: 10587682Seric ** result is saved in static buf; future calls will trash it. 10597682Seric */ 10607682Seric 10617682Seric char * 10628353Seric canonname(name, ruleset) 10637682Seric char *name; 10648353Seric int ruleset; 10657682Seric { 10668069Seric static char nbuf[MAXNAME]; 10677940Seric register char **pvp; 10687682Seric 10697940Seric pvp = prescan(name, '\0'); 10708069Seric rewrite(pvp, 3); 10718353Seric rewrite(pvp, ruleset); 10728069Seric cataddr(pvp, nbuf, sizeof nbuf); 10737682Seric return (nbuf); 10747682Seric } 1075