13312Seric # include "sendmail.h" 2297Seric 3*8058Seric SCCSID(@(#)parseaddr.c 3.51 09/05/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 653149Seric pvp = prescan(addr, '\0'); 663149Seric if (pvp == NULL) 67297Seric return (NULL); 68297Seric 69297Seric /* 703149Seric ** Apply rewriting rules. 717889Seric ** Ruleset 4 rewrites the address into a form that will 727889Seric ** be reflected in the outgoing message. It must 737889Seric ** not resolve. 747889Seric ** Ruleset 0 does basic parsing. It must resolve. 75297Seric */ 76297Seric 777889Seric rewrite(pvp, 4); 784070Seric rewrite(pvp, 0); 79297Seric 803149Seric /* 813149Seric ** See if we resolved to a real mailer. 823149Seric */ 83297Seric 843149Seric if (pvp[0][0] != CANONNET) 853149Seric { 863149Seric setstat(EX_USAGE); 873149Seric usrerr("cannot resolve name"); 883149Seric return (NULL); 89297Seric } 90297Seric 91297Seric /* 923149Seric ** Build canonical address from pvp. 93297Seric */ 94297Seric 953149Seric a = buildaddr(pvp, a); 964279Seric if (a == NULL) 974279Seric return (NULL); 984598Seric m = a->q_mailer; 99297Seric 100297Seric /* 1013149Seric ** Make local copies of the host & user and then 1023149Seric ** transport them out. 103297Seric */ 104297Seric 105297Seric if (copyf > 0) 1062973Seric a->q_paddr = newstr(addr); 107297Seric else 108297Seric a->q_paddr = addr; 1093149Seric if (copyf >= 0) 110297Seric { 1113149Seric if (a->q_host != NULL) 1123149Seric a->q_host = newstr(a->q_host); 113297Seric else 1143149Seric a->q_host = ""; 1153149Seric if (a->q_user != a->q_paddr) 1163149Seric a->q_user = newstr(a->q_user); 117297Seric } 118297Seric 119297Seric /* 120297Seric ** Do UPPER->lower case mapping unless inhibited. 121297Seric */ 122297Seric 1233149Seric if (!bitset(M_HST_UPPER, m->m_flags)) 124297Seric makelower(a->q_host); 1253149Seric if (!bitset(M_USR_UPPER, m->m_flags)) 126297Seric makelower(a->q_user); 127297Seric 128297Seric /* 129297Seric ** Compute return value. 130297Seric */ 131297Seric 132297Seric # ifdef DEBUG 1337675Seric if (tTd(20, 1)) 1344443Seric { 1354443Seric printf("parse-->"); 1364443Seric printaddr(a, FALSE); 1374443Seric } 138297Seric # endif DEBUG 139297Seric 140297Seric return (a); 141297Seric } 142297Seric /* 143297Seric ** PRESCAN -- Prescan name and make it canonical 144297Seric ** 145297Seric ** Scans a name and turns it into canonical form. This involves 146297Seric ** deleting blanks, comments (in parentheses), and turning the 147297Seric ** word "at" into an at-sign ("@"). The name is copied as this 148297Seric ** is done; it is legal to copy a name onto itself, since this 149297Seric ** process can only make things smaller. 150297Seric ** 151297Seric ** This routine knows about quoted strings and angle brackets. 152297Seric ** 153297Seric ** There are certain subtleties to this routine. The one that 154297Seric ** comes to mind now is that backslashes on the ends of names 155297Seric ** are silently stripped off; this is intentional. The problem 156297Seric ** is that some versions of sndmsg (like at LBL) set the kill 157297Seric ** character to something other than @ when reading addresses; 158297Seric ** so people type "csvax.eric\@berkeley" -- which screws up the 159297Seric ** berknet mailer. 160297Seric ** 161297Seric ** Parameters: 162297Seric ** addr -- the name to chomp. 163297Seric ** delim -- the delimiter for the address, normally 164297Seric ** '\0' or ','; \0 is accepted in any case. 165297Seric ** are moving in place; set buflim to high core. 166297Seric ** 167297Seric ** Returns: 1683149Seric ** A pointer to a vector of tokens. 169297Seric ** NULL on error. 170297Seric ** 171297Seric ** Side Effects: 1723149Seric ** none. 173297Seric */ 174297Seric 1753149Seric # define OPER 1 1763149Seric # define ATOM 2 1773149Seric # define EOTOK 3 1783149Seric # define QSTRING 4 1793149Seric # define SPACE 5 1806053Seric # define ONEMORE 6 1813149Seric # define GETONE 7 1824424Seric # define MACRO 8 1833149Seric 1843149Seric char ** 1853149Seric prescan(addr, delim) 186297Seric char *addr; 187297Seric char delim; 188297Seric { 189297Seric register char *p; 1903149Seric static char buf[MAXNAME+MAXATOM]; 1913149Seric static char *av[MAXATOM+1]; 1923149Seric char **avp; 193297Seric bool bslashmode; 194297Seric int cmntcnt; 195297Seric register char c; 1963149Seric char *tok; 197297Seric register char *q; 1983149Seric register int state; 1993149Seric int nstate; 2004085Seric extern char lower(); 201297Seric 202297Seric q = buf; 2033149Seric bslashmode = FALSE; 2047800Seric cmntcnt = 0; 2053149Seric avp = av; 2063149Seric state = OPER; 2073149Seric for (p = addr; *p != '\0' && *p != delim; ) 208297Seric { 2093149Seric /* read a token */ 2103149Seric tok = q; 2113149Seric while ((c = *p++) != '\0' && c != delim) 212297Seric { 2133149Seric /* chew up special characters */ 2144100Seric c &= ~0200; 2153149Seric *q = '\0'; 2163149Seric if (bslashmode) 2173149Seric { 2183149Seric c |= 0200; 2193149Seric bslashmode = FALSE; 2203149Seric } 2213149Seric else if (c == '\\') 2223149Seric { 2233149Seric bslashmode = TRUE; 2243149Seric continue; 2253149Seric } 2264100Seric else if (c == '"') 2274100Seric { 2284100Seric if (state == QSTRING) 2294100Seric state = OPER; 2304100Seric else 2314100Seric state = QSTRING; 2324100Seric break; 2334100Seric } 2343149Seric 2356053Seric nstate = toktype(c); 2363149Seric switch (state) 2373149Seric { 2383149Seric case QSTRING: /* in quoted string */ 2393149Seric break; 2403149Seric 2413149Seric case ATOM: /* regular atom */ 2424228Seric if (nstate != ATOM) 2433149Seric { 2443149Seric state = EOTOK; 2453149Seric p--; 2463149Seric } 2473149Seric break; 2483149Seric 2493149Seric case GETONE: /* grab one character */ 2503149Seric state = OPER; 2513149Seric break; 2523149Seric 2533149Seric case EOTOK: /* after atom or q-string */ 2543149Seric state = nstate; 2553149Seric if (state == SPACE) 2563149Seric continue; 2573149Seric break; 2583149Seric 2593149Seric case SPACE: /* linear white space */ 2603149Seric state = nstate; 2614228Seric break; 2623149Seric 2633149Seric case OPER: /* operator */ 2643149Seric if (nstate == SPACE) 2653149Seric continue; 2663149Seric state = nstate; 2673149Seric break; 2683149Seric 2696053Seric case ONEMORE: /* $- etc. */ 2706053Seric state = GETONE; 2713149Seric break; 2723149Seric 2733149Seric default: 2743149Seric syserr("prescan: unknown state %d", state); 2753149Seric } 2763149Seric 2774228Seric if (state == EOTOK || state == SPACE) 2783149Seric break; 2793149Seric 2803149Seric /* squirrel it away */ 2813149Seric if (q >= &buf[sizeof buf - 5]) 2823149Seric { 2833149Seric usrerr("Address too long"); 2843149Seric return (NULL); 2853149Seric } 2863149Seric *q++ = c; 2873149Seric 2883149Seric /* decide whether this represents end of token */ 2896053Seric if (state == OPER || state == GETONE) 2903149Seric break; 291297Seric } 2923149Seric if (c == '\0' || c == delim) 2933149Seric p--; 2943149Seric 2953149Seric /* new token */ 2963149Seric if (tok == q) 297297Seric continue; 2983149Seric *q++ = '\0'; 2993149Seric 3003149Seric c = tok[0]; 3013149Seric if (c == '(') 3021378Seric { 303297Seric cmntcnt++; 3041378Seric continue; 3051378Seric } 306297Seric else if (c == ')') 307297Seric { 308297Seric if (cmntcnt <= 0) 309297Seric { 310297Seric usrerr("Unbalanced ')'"); 311297Seric return (NULL); 312297Seric } 313297Seric else 314297Seric { 315297Seric cmntcnt--; 316297Seric continue; 317297Seric } 318297Seric } 3193149Seric else if (cmntcnt > 0) 3202091Seric continue; 3213149Seric 3224448Seric if (avp >= &av[MAXATOM]) 3234448Seric { 3244448Seric syserr("prescan: too many tokens"); 3254448Seric return (NULL); 3264448Seric } 3274448Seric *avp++ = tok; 3283149Seric } 3293149Seric *avp = NULL; 3303149Seric if (cmntcnt > 0) 3313149Seric usrerr("Unbalanced '('"); 3323149Seric else if (state == QSTRING) 3333149Seric usrerr("Unbalanced '\"'"); 3343149Seric else if (av[0] != NULL) 3353149Seric return (av); 3363149Seric return (NULL); 3373149Seric } 3383149Seric /* 3393149Seric ** TOKTYPE -- return token type 3403149Seric ** 3413149Seric ** Parameters: 3423149Seric ** c -- the character in question. 3433149Seric ** 3443149Seric ** Returns: 3453149Seric ** Its type. 3463149Seric ** 3473149Seric ** Side Effects: 3483149Seric ** none. 3493149Seric */ 350297Seric 3513149Seric toktype(c) 3523149Seric register char c; 3533149Seric { 3543380Seric static char buf[50]; 3553382Seric static bool firstime = TRUE; 3563380Seric 3573382Seric if (firstime) 3583380Seric { 3593382Seric firstime = FALSE; 3606977Seric expand("$o", buf, &buf[sizeof buf - 1], CurEnv); 3617005Seric (void) strcat(buf, DELIMCHARS); 3623380Seric } 3636053Seric if (c == MATCHCLASS || c == MATCHREPL) 3646053Seric return (ONEMORE); 3654100Seric if (!isascii(c)) 3664100Seric return (ATOM); 3673149Seric if (isspace(c)) 3683149Seric return (SPACE); 3693380Seric if (iscntrl(c) || index(buf, c) != NULL) 3703149Seric return (OPER); 3713149Seric return (ATOM); 3723149Seric } 3733149Seric /* 3743149Seric ** REWRITE -- apply rewrite rules to token vector. 3753149Seric ** 3764476Seric ** This routine is an ordered production system. Each rewrite 3774476Seric ** rule has a LHS (called the pattern) and a RHS (called the 3784476Seric ** rewrite); 'rwr' points the the current rewrite rule. 3794476Seric ** 3804476Seric ** For each rewrite rule, 'avp' points the address vector we 3814476Seric ** are trying to match against, and 'pvp' points to the pattern. 382*8058Seric ** If pvp points to a special match value (MATCHZANY, MATCHANY, 383*8058Seric ** MATCHONE, MATCHCLASS) then the address in avp matched is 384*8058Seric ** saved away in the match vector (pointed to by 'mvp'). 3854476Seric ** 3864476Seric ** When a match between avp & pvp does not match, we try to 3874476Seric ** back out. If we back up over a MATCHONE or a MATCHCLASS 3884476Seric ** we must also back out the match in mvp. If we reach a 389*8058Seric ** MATCHANY or MATCHZANY we just extend the match and start 390*8058Seric ** over again. 3914476Seric ** 3924476Seric ** When we finally match, we rewrite the address vector 3934476Seric ** and try over again. 3944476Seric ** 3953149Seric ** Parameters: 3963149Seric ** pvp -- pointer to token vector. 3973149Seric ** 3983149Seric ** Returns: 3993149Seric ** none. 4003149Seric ** 4013149Seric ** Side Effects: 4023149Seric ** pvp is modified. 4033149Seric */ 4042091Seric 4053149Seric struct match 4063149Seric { 4074468Seric char **first; /* first token matched */ 4084468Seric char **last; /* last token matched */ 4093149Seric }; 4103149Seric 4114468Seric # define MAXMATCH 9 /* max params per rewrite */ 4123149Seric 4133149Seric 4144070Seric rewrite(pvp, ruleset) 4153149Seric char **pvp; 4164070Seric int ruleset; 4173149Seric { 4183149Seric register char *ap; /* address pointer */ 4193149Seric register char *rp; /* rewrite pointer */ 4203149Seric register char **avp; /* address vector pointer */ 4213149Seric register char **rvp; /* rewrite vector pointer */ 422*8058Seric register struct match *mlp; /* cur ptr into mlist */ 423*8058Seric register struct rewrite *rwr; /* pointer to current rewrite rule */ 4244468Seric struct match mlist[MAXMATCH]; /* stores match on LHS */ 4253149Seric char *npvp[MAXATOM+1]; /* temporary space for rebuild */ 4264060Seric extern bool sameword(); 4273149Seric 4284100Seric # ifdef DEBUG 4297675Seric if (tTd(21, 9)) 4303149Seric { 431*8058Seric printf("rewrite: ruleset %d, original pvp:\n", ruleset); 4323149Seric printav(pvp); 4333149Seric } 4344100Seric # endif DEBUG 4353149Seric 4363149Seric /* 4373149Seric ** Run through the list of rewrite rules, applying 4383149Seric ** any that match. 4393149Seric */ 4403149Seric 4414070Seric for (rwr = RewriteRules[ruleset]; rwr != NULL; ) 4423149Seric { 4434100Seric # ifdef DEBUG 4447675Seric if (tTd(21, 12)) 445297Seric { 4463149Seric printf("-----trying rule:\n"); 4473149Seric printav(rwr->r_lhs); 4483149Seric } 4494100Seric # endif DEBUG 4503149Seric 4513149Seric /* try to match on this rule */ 4524468Seric mlp = mlist; 453*8058Seric rvp = rwr->r_lhs; 454*8058Seric avp = pvp; 455*8058Seric while ((ap = *avp) != NULL || *rvp != NULL) 4563149Seric { 4573149Seric rp = *rvp; 458*8058Seric # ifdef DEBUG 459*8058Seric if (tTd(21, 35)) 460*8058Seric { 461*8058Seric printf("ap=\""); 462*8058Seric xputs(ap); 463*8058Seric printf("\", rp=\""); 464*8058Seric xputs(rp); 465*8058Seric printf("\"\n"); 466*8058Seric } 467*8058Seric # endif DEBUG 4683149Seric if (rp == NULL) 469297Seric { 4703149Seric /* end-of-pattern before end-of-address */ 471*8058Seric goto backup; 472297Seric } 473*8058Seric if (ap == NULL && *rp != MATCHZANY) 474*8058Seric { 475*8058Seric /* end-of-input */ 476*8058Seric break; 477*8058Seric } 4783149Seric 4793149Seric switch (*rp) 4803149Seric { 4814060Seric register STAB *s; 4824060Seric register int class; 4834060Seric 4844060Seric case MATCHCLASS: 4854060Seric /* match any token in a class */ 4864060Seric class = rp[1]; 4874060Seric if (!isalpha(class)) 488*8058Seric goto backup; 4894060Seric if (isupper(class)) 4904060Seric class -= 'A'; 4914060Seric else 4924060Seric class -= 'a'; 4934100Seric s = stab(ap, ST_CLASS, ST_FIND); 4946273Seric if (s == NULL || (s->s_class & (1L << class)) == 0) 495*8058Seric goto backup; 4964468Seric 4974476Seric /* explicit fall-through */ 4984476Seric 4994476Seric case MATCHONE: 5004476Seric case MATCHANY: 5014476Seric /* match exactly one token */ 502*8058Seric mlp->first = avp; 503*8058Seric mlp->last = avp++; 5044468Seric mlp++; 5054060Seric break; 5064060Seric 507*8058Seric case MATCHZANY: 508*8058Seric /* match zero or more tokens */ 509*8058Seric mlp->first = avp; 510*8058Seric mlp->last = avp - 1; 511*8058Seric mlp++; 512*8058Seric break; 513*8058Seric 5143149Seric default: 5153149Seric /* must have exact match */ 5164060Seric if (!sameword(rp, ap)) 517*8058Seric goto backup; 5184468Seric avp++; 5193149Seric break; 5203149Seric } 5213149Seric 5223149Seric /* successful match on this token */ 5233149Seric rvp++; 5243149Seric continue; 5253149Seric 526*8058Seric backup: 5273149Seric /* match failed -- back up */ 5283149Seric while (--rvp >= rwr->r_lhs) 5293149Seric { 5303149Seric rp = *rvp; 531*8058Seric if (*rp == MATCHANY || *rp == MATCHZANY) 5324468Seric { 5334476Seric /* extend binding and continue */ 534*8058Seric avp = ++mlp[-1].last; 535*8058Seric avp++; 5364476Seric rvp++; 5373149Seric break; 5384468Seric } 5394476Seric avp--; 5404476Seric if (*rp == MATCHONE || *rp == MATCHCLASS) 5413149Seric { 5424468Seric /* back out binding */ 5434468Seric mlp--; 5443149Seric } 5453149Seric } 5463149Seric 5473149Seric if (rvp < rwr->r_lhs) 5483149Seric { 5493149Seric /* total failure to match */ 5503149Seric break; 5513149Seric } 552297Seric } 5533149Seric 5543149Seric /* 5553149Seric ** See if we successfully matched 5563149Seric */ 5573149Seric 5583149Seric if (rvp >= rwr->r_lhs && *rvp == NULL) 5593149Seric { 560*8058Seric rvp = rwr->r_rhs; 5614100Seric # ifdef DEBUG 5627675Seric if (tTd(21, 12)) 5633149Seric { 5643149Seric printf("-----rule matches:\n"); 565*8058Seric printav(rvp); 5663149Seric } 5674100Seric # endif DEBUG 5683149Seric 569*8058Seric /* see if this is a "subroutine" call */ 570*8058Seric rp = *rvp; 571*8058Seric if (*rp == CALLSUBR) 572*8058Seric { 573*8058Seric rp = *++rvp; 574*8058Seric # ifdef DEBUG 575*8058Seric if (tTd(21, 2)) 576*8058Seric printf("-----callsubr %s\n", rp); 577*8058Seric # endif DEBUG 578*8058Seric rewrite(pvp, atoi(rp)); 579*8058Seric rwr = rwr->r_next; 580*8058Seric continue; 581*8058Seric } 582*8058Seric 5833149Seric /* substitute */ 5843149Seric for (rvp = rwr->r_rhs, avp = npvp; *rvp != NULL; rvp++) 5853149Seric { 5863149Seric rp = *rvp; 5874468Seric if (*rp == MATCHREPL) 5883149Seric { 5893149Seric register struct match *m; 5903149Seric register char **pp; 5913149Seric 5924468Seric m = &mlist[rp[1] - '1']; 5934476Seric # ifdef DEBUG 5947675Seric if (tTd(21, 15)) 5954476Seric { 5964476Seric printf("$%c:", rp[1]); 5974476Seric pp = m->first; 598*8058Seric while (pp <= m->last) 5994476Seric { 6004476Seric printf(" %x=\"", *pp); 6014625Seric (void) fflush(stdout); 602*8058Seric printf("%s\"", *pp++); 603*8058Seric } 6044476Seric printf("\n"); 6054476Seric } 6064476Seric # endif DEBUG 6074468Seric pp = m->first; 608*8058Seric while (pp <= m->last) 6093149Seric { 6104468Seric if (avp >= &npvp[MAXATOM]) 6113149Seric { 6124468Seric syserr("rewrite: expansion too long"); 6134468Seric return; 6144468Seric } 615*8058Seric *avp++ = *pp++; 616*8058Seric } 6173149Seric } 6183149Seric else 6194385Seric { 6204385Seric if (avp >= &npvp[MAXATOM]) 6214385Seric { 6224385Seric syserr("rewrite: expansion too long"); 6234385Seric return; 6244385Seric } 6253149Seric *avp++ = rp; 6264385Seric } 6273149Seric } 6283149Seric *avp++ = NULL; 6294085Seric bmove((char *) npvp, (char *) pvp, (avp - npvp) * sizeof *avp); 6303149Seric # ifdef DEBUG 6317675Seric if (tTd(21, 4)) 6323149Seric { 6333188Seric char **vp; 6343188Seric 6353188Seric printf("rewritten as `"); 6363188Seric for (vp = pvp; *vp != NULL; vp++) 6374228Seric { 6384228Seric if (vp != pvp) 6394228Seric printf("_"); 6403188Seric xputs(*vp); 6414228Seric } 6423188Seric printf("'\n"); 6433149Seric } 6443149Seric # endif DEBUG 6458011Seric if (**pvp == CANONNET || **pvp == CANONUSER) 6463149Seric break; 6473149Seric } 6483149Seric else 6493149Seric { 6504100Seric # ifdef DEBUG 6517675Seric if (tTd(21, 10)) 6523149Seric printf("----- rule fails\n"); 6534100Seric # endif DEBUG 6543149Seric rwr = rwr->r_next; 6553149Seric } 656297Seric } 6573149Seric } 6583149Seric /* 6593149Seric ** BUILDADDR -- build address from token vector. 6603149Seric ** 6613149Seric ** Parameters: 6623149Seric ** tv -- token vector. 6633149Seric ** a -- pointer to address descriptor to fill. 6643149Seric ** If NULL, one will be allocated. 6653149Seric ** 6663149Seric ** Returns: 6674279Seric ** NULL if there was an error. 6684279Seric ** 'a' otherwise. 6693149Seric ** 6703149Seric ** Side Effects: 6713149Seric ** fills in 'a' 6723149Seric */ 6733149Seric 6743149Seric ADDRESS * 6753149Seric buildaddr(tv, a) 6763149Seric register char **tv; 6773149Seric register ADDRESS *a; 6783149Seric { 6793149Seric static char buf[MAXNAME]; 6803149Seric struct mailer **mp; 6813149Seric register struct mailer *m; 6824635Seric extern bool sameword(); 6833149Seric 6843149Seric if (a == NULL) 6853149Seric a = (ADDRESS *) xalloc(sizeof *a); 6864988Seric clear((char *) a, sizeof *a); 6873149Seric 6883149Seric /* figure out what net/mailer to use */ 6893149Seric if (**tv != CANONNET) 6904279Seric { 6913149Seric syserr("buildaddr: no net"); 6924279Seric return (NULL); 6934279Seric } 6943149Seric tv++; 6954635Seric if (sameword(*tv, "error")) 6964279Seric { 6974279Seric if (**++tv != CANONUSER) 6984279Seric syserr("buildaddr: error: no user"); 6994279Seric buf[0] = '\0'; 7004279Seric while (*++tv != NULL) 7014279Seric { 7024279Seric if (buf[0] != '\0') 7037005Seric (void) strcat(buf, " "); 7047005Seric (void) strcat(buf, *tv); 7054279Seric } 7064279Seric usrerr(buf); 7074279Seric return (NULL); 7084279Seric } 7094598Seric for (mp = Mailer; (m = *mp++) != NULL; ) 7103149Seric { 7114635Seric if (sameword(m->m_name, *tv)) 7123149Seric break; 7133149Seric } 7143149Seric if (m == NULL) 7154279Seric { 7163149Seric syserr("buildaddr: unknown net %s", *tv); 7174279Seric return (NULL); 7184279Seric } 7194598Seric a->q_mailer = m; 7203149Seric 7213149Seric /* figure out what host (if any) */ 7223149Seric tv++; 7234195Seric if (!bitset(M_LOCAL, m->m_flags)) 7243149Seric { 7255704Seric if (**tv++ != CANONHOST) 7264279Seric { 7273149Seric syserr("buildaddr: no host"); 7284279Seric return (NULL); 7294279Seric } 7305704Seric buf[0] = '\0'; 7315704Seric while (*tv != NULL && **tv != CANONUSER) 7327005Seric (void) strcat(buf, *tv++); 7335704Seric a->q_host = newstr(buf); 7343149Seric } 7353149Seric else 7363149Seric a->q_host = NULL; 7373149Seric 7383149Seric /* figure out the user */ 7393149Seric if (**tv != CANONUSER) 7404279Seric { 7413149Seric syserr("buildaddr: no user"); 7424279Seric return (NULL); 7434279Seric } 7444228Seric cataddr(++tv, buf, sizeof buf); 7453149Seric a->q_user = buf; 7463149Seric 7473149Seric return (a); 7483149Seric } 7493188Seric /* 7504228Seric ** CATADDR -- concatenate pieces of addresses (putting in <LWSP> subs) 7514228Seric ** 7524228Seric ** Parameters: 7534228Seric ** pvp -- parameter vector to rebuild. 7544228Seric ** buf -- buffer to build the string into. 7554228Seric ** sz -- size of buf. 7564228Seric ** 7574228Seric ** Returns: 7584228Seric ** none. 7594228Seric ** 7604228Seric ** Side Effects: 7614228Seric ** Destroys buf. 7624228Seric */ 7634228Seric 7644228Seric cataddr(pvp, buf, sz) 7654228Seric char **pvp; 7664228Seric char *buf; 7674228Seric register int sz; 7684228Seric { 7694228Seric bool oatomtok = FALSE; 7704228Seric bool natomtok = FALSE; 7714228Seric register int i; 7724228Seric register char *p; 7734228Seric 7744228Seric p = buf; 7754228Seric sz--; 7768011Seric if (*pvp != NULL && **pvp == CANONUSER) 7778011Seric pvp++; 7784228Seric while (*pvp != NULL && (i = strlen(*pvp)) < sz) 7794228Seric { 7804228Seric natomtok = (toktype(**pvp) == ATOM); 7814228Seric if (oatomtok && natomtok) 7824228Seric *p++ = SPACESUB; 7834228Seric (void) strcpy(p, *pvp); 7844228Seric oatomtok = natomtok; 7854228Seric p += i; 7864228Seric sz -= i; 7874228Seric pvp++; 7884228Seric } 7894228Seric *p = '\0'; 7904228Seric } 7914228Seric /* 7923188Seric ** SAMEADDR -- Determine if two addresses are the same 7933188Seric ** 7943188Seric ** This is not just a straight comparison -- if the mailer doesn't 7953188Seric ** care about the host we just ignore it, etc. 7963188Seric ** 7973188Seric ** Parameters: 7983188Seric ** a, b -- pointers to the internal forms to compare. 7993188Seric ** wildflg -- if TRUE, 'a' may have no user specified, 8003188Seric ** in which case it is to match anything. 8013188Seric ** 8023188Seric ** Returns: 8033188Seric ** TRUE -- they represent the same mailbox. 8043188Seric ** FALSE -- they don't. 8053188Seric ** 8063188Seric ** Side Effects: 8073188Seric ** none. 8083188Seric */ 8093188Seric 8103188Seric bool 8113188Seric sameaddr(a, b, wildflg) 8123188Seric register ADDRESS *a; 8133188Seric register ADDRESS *b; 8143188Seric bool wildflg; 8153188Seric { 8163188Seric /* if they don't have the same mailer, forget it */ 8173188Seric if (a->q_mailer != b->q_mailer) 8183188Seric return (FALSE); 8193188Seric 8203188Seric /* if the user isn't the same, we can drop out */ 8213188Seric if ((!wildflg || a->q_user[0] != '\0') && strcmp(a->q_user, b->q_user) != 0) 8223188Seric return (FALSE); 8233188Seric 8243188Seric /* if the mailer ignores hosts, we have succeeded! */ 8254598Seric if (bitset(M_LOCAL, a->q_mailer->m_flags)) 8263188Seric return (TRUE); 8273188Seric 8283188Seric /* otherwise compare hosts (but be careful for NULL ptrs) */ 8293188Seric if (a->q_host == NULL || b->q_host == NULL) 8303188Seric return (FALSE); 8313188Seric if (strcmp(a->q_host, b->q_host) != 0) 8323188Seric return (FALSE); 8333188Seric 8343188Seric return (TRUE); 8353188Seric } 8363234Seric /* 8373234Seric ** PRINTADDR -- print address (for debugging) 8383234Seric ** 8393234Seric ** Parameters: 8403234Seric ** a -- the address to print 8413234Seric ** follow -- follow the q_next chain. 8423234Seric ** 8433234Seric ** Returns: 8443234Seric ** none. 8453234Seric ** 8463234Seric ** Side Effects: 8473234Seric ** none. 8483234Seric */ 8493234Seric 8504317Seric # ifdef DEBUG 8514317Seric 8523234Seric printaddr(a, follow) 8533234Seric register ADDRESS *a; 8543234Seric bool follow; 8553234Seric { 8565001Seric bool first = TRUE; 8575001Seric 8583234Seric while (a != NULL) 8593234Seric { 8605001Seric first = FALSE; 8614443Seric printf("%x=", a); 8624085Seric (void) fflush(stdout); 8633234Seric printf("%s: mailer %d (%s), host `%s', user `%s'\n", a->q_paddr, 8644598Seric a->q_mailer->m_mno, a->q_mailer->m_name, a->q_host, a->q_user); 8655035Seric printf("\tnext=%x, flags=%o, rmailer %d, alias %x\n", a->q_next, 8665035Seric a->q_flags, a->q_rmailer, a->q_alias); 8675001Seric printf("\thome=\"%s\", fullname=\"%s\"\n", a->q_home, a->q_fullname); 8684996Seric 8693234Seric if (!follow) 8703234Seric return; 8714996Seric a = a->q_next; 8723234Seric } 8735001Seric if (first) 8744443Seric printf("[NULL]\n"); 8753234Seric } 8764317Seric 8774317Seric # endif DEBUG 8787682Seric /* 8797682Seric ** REMOTENAME -- return the name relative to the current mailer 8807682Seric ** 8817682Seric ** Parameters: 8827682Seric ** name -- the name to translate. 8837682Seric ** force -- if set, forces rewriting even if the mailer 8847682Seric ** does not request it. Used for rewriting 8857682Seric ** sender addresses. 8867682Seric ** 8877682Seric ** Returns: 8887682Seric ** the text string representing this address relative to 8897682Seric ** the receiving mailer. 8907682Seric ** 8917682Seric ** Side Effects: 8927682Seric ** none. 8937682Seric ** 8947682Seric ** Warnings: 8957682Seric ** The text string returned is tucked away locally; 8967682Seric ** copy it if you intend to save it. 8977682Seric */ 8987682Seric 8997682Seric char * 9007682Seric remotename(name, m, force) 9017682Seric char *name; 9027682Seric struct mailer *m; 9037682Seric bool force; 9047682Seric { 9057682Seric static char buf[MAXNAME]; 9067682Seric char lbuf[MAXNAME]; 9077682Seric extern char *macvalue(); 9087682Seric char *oldf = macvalue('f'); 9097682Seric char *oldg = macvalue('g'); 9107682Seric extern char **prescan(); 9117682Seric register char **pvp; 9127682Seric extern ADDRESS *buildaddr(); 9137889Seric extern char *crackaddr(); 9147889Seric char *fancy; 9157682Seric 9167755Seric # ifdef DEBUG 9177755Seric if (tTd(12, 1)) 9187755Seric printf("remotename(%s)\n", name); 9197755Seric # endif DEBUG 9207755Seric 9217682Seric /* 9227889Seric ** First put this address into canonical form. 9237889Seric ** First turn it into a macro. 9247889Seric ** Then run it through ruleset 4. 925*8058Seric ** If the mailer defines a rewriting set, run it through 926*8058Seric ** there next. 927*8058Seric ** The intent is that ruleset 4 puts the name into a 928*8058Seric ** canonical form; the mailer's ruleset then 929*8058Seric ** does any customization. 9307889Seric */ 9317889Seric 9327889Seric /* save away the extraneous pretty stuff */ 9337889Seric fancy = crackaddr(name); 9347889Seric 9357889Seric /* now run through ruleset four */ 9367889Seric pvp = prescan(name, '\0'); 9377889Seric if (pvp == NULL) 9387889Seric return (name); 9397889Seric rewrite(pvp, 4); 940*8058Seric if (m->m_rwset > 0) 941*8058Seric rewrite(pvp, m->m_rwset); 9427889Seric 9437889Seric /* 9447682Seric ** See if this mailer wants the name to be rewritten. There are 9457682Seric ** many problems here, owing to the standards for doing replies. 9467682Seric ** In general, these names should only be rewritten if we are 9477682Seric ** sending to another host that runs sendmail. 9487682Seric */ 9497682Seric 9507889Seric if (bitset(M_RELRCPT, m->m_flags) && !force) 9517755Seric { 9527889Seric /* 9537889Seric ** Do general rewriting of name. 9547889Seric ** This will also take care of doing global name 9557889Seric ** translation. 9567889Seric */ 9577682Seric 9587889Seric rewrite(pvp, 1); 9597889Seric rewrite(pvp, 3); 9607682Seric 9617889Seric /* make the name relative to the receiving mailer */ 9627889Seric cataddr(pvp, lbuf, sizeof lbuf); 9637889Seric define('f', lbuf); 9647889Seric expand(m->m_from, buf, &buf[sizeof buf - 1], CurEnv); 9657889Seric 9667889Seric /* rewrite to get rid of garbage we added in the expand above */ 9677889Seric pvp = prescan(buf, '\0'); 9687889Seric if (pvp == NULL) 9697889Seric return (name); 9707889Seric rewrite(pvp, 2); 9717682Seric } 9727682Seric 9737889Seric /* now add any comment info we had before back */ 9747682Seric cataddr(pvp, lbuf, sizeof lbuf); 9757682Seric define('g', lbuf); 9767889Seric expand(fancy, buf, &buf[sizeof buf - 1], CurEnv); 9777682Seric 9787682Seric define('f', oldf); 9797682Seric define('g', oldg); 9807682Seric 9817682Seric # ifdef DEBUG 9827682Seric if (tTd(12, 1)) 9837755Seric printf("remotename => `%s'\n", buf); 9847682Seric # endif DEBUG 9857682Seric return (buf); 9867682Seric } 9877682Seric /* 9887682Seric ** CANONNAME -- make name canonical 9897682Seric ** 9907682Seric ** This is used for SMTP and misc. printing. Given a print 9917682Seric ** address, it strips out comments, etc., and puts on exactly 9927682Seric ** one set of brackets. 9937682Seric ** 9947682Seric ** Parameters: 9957682Seric ** name -- the name to make canonical. 9967682Seric ** 9977682Seric ** Returns: 9987682Seric ** pointer to canonical name. 9997682Seric ** 10007682Seric ** Side Effects: 10017682Seric ** none. 10027682Seric ** 10037682Seric ** Warning: 10047682Seric ** result is saved in static buf; future calls will trash it. 10057682Seric */ 10067682Seric 10077682Seric char * 10087682Seric canonname(name) 10097682Seric char *name; 10107682Seric { 10117682Seric static char nbuf[MAXNAME + 2]; 10127940Seric register char **pvp; 10137682Seric 10147940Seric pvp = prescan(name, '\0'); 10157940Seric rewrite(pvp, 4); 10167940Seric cataddr(pvp, nbuf + 1, MAXNAME); 10177940Seric nbuf[0] = '<'; 10187682Seric strcat(nbuf, ">"); 10197682Seric return (nbuf); 10207682Seric } 1021