13312Seric # include "sendmail.h" 2297Seric 3*15284Seric SCCSID(@(#)parseaddr.c 4.3 10/23/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. 3111445Seric ** delim -- the character to terminate the address, passed 3211445Seric ** to prescan. 33297Seric ** 34297Seric ** Returns: 35297Seric ** A pointer to the address descriptor header (`a' if 36297Seric ** `a' is non-NULL). 37297Seric ** NULL on error. 38297Seric ** 39297Seric ** Side Effects: 40297Seric ** none 41297Seric */ 42297Seric 439374Seric /* following delimiters are inherent to the internal algorithms */ 443380Seric # define DELIMCHARS "$()<>,;\\\"\r\n" /* word delimiters */ 452091Seric 462973Seric ADDRESS * 4711445Seric parseaddr(addr, a, copyf, delim) 48297Seric char *addr; 492973Seric register ADDRESS *a; 50297Seric int copyf; 5111445Seric char delim; 52297Seric { 533149Seric register char **pvp; 543149Seric register struct mailer *m; 553149Seric extern char **prescan(); 563149Seric extern ADDRESS *buildaddr(); 57297Seric 58297Seric /* 59297Seric ** Initialize and prescan address. 60297Seric */ 61297Seric 626903Seric CurEnv->e_to = addr; 633188Seric # ifdef DEBUG 647675Seric if (tTd(20, 1)) 659888Seric printf("\n--parseaddr(%s)\n", addr); 663188Seric # endif DEBUG 673188Seric 6811445Seric pvp = prescan(addr, delim); 693149Seric if (pvp == NULL) 70297Seric return (NULL); 71297Seric 72297Seric /* 733149Seric ** Apply rewriting rules. 747889Seric ** Ruleset 0 does basic parsing. It must resolve. 75297Seric */ 76297Seric 778181Seric rewrite(pvp, 3); 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) 1068078Seric { 1078078Seric extern char *DelimChar; 1088078Seric char savec = *DelimChar; 1098078Seric 1108078Seric *DelimChar = '\0'; 1112973Seric a->q_paddr = newstr(addr); 1128078Seric *DelimChar = savec; 1138078Seric } 114297Seric else 115297Seric a->q_paddr = addr; 1163149Seric if (copyf >= 0) 117297Seric { 1183149Seric if (a->q_host != NULL) 1193149Seric a->q_host = newstr(a->q_host); 120297Seric else 1213149Seric a->q_host = ""; 1223149Seric if (a->q_user != a->q_paddr) 1233149Seric a->q_user = newstr(a->q_user); 124297Seric } 125297Seric 126297Seric /* 127297Seric ** Do UPPER->lower case mapping unless inhibited. 128297Seric */ 129297Seric 13010690Seric if (!bitnset(M_HST_UPPER, m->m_flags)) 131297Seric makelower(a->q_host); 13210690Seric if (!bitnset(M_USR_UPPER, m->m_flags)) 133297Seric makelower(a->q_user); 134297Seric 135297Seric /* 136297Seric ** Compute return value. 137297Seric */ 138297Seric 139297Seric # ifdef DEBUG 1407675Seric if (tTd(20, 1)) 1414443Seric { 1429888Seric printf("parseaddr-->"); 1434443Seric printaddr(a, FALSE); 1444443Seric } 145297Seric # endif DEBUG 146297Seric 147297Seric return (a); 148297Seric } 149297Seric /* 150297Seric ** PRESCAN -- Prescan name and make it canonical 151297Seric ** 1529374Seric ** Scans a name and turns it into a set of tokens. This process 1539374Seric ** deletes blanks and comments (in parentheses). 154297Seric ** 155297Seric ** This routine knows about quoted strings and angle brackets. 156297Seric ** 157297Seric ** There are certain subtleties to this routine. The one that 158297Seric ** comes to mind now is that backslashes on the ends of names 159297Seric ** are silently stripped off; this is intentional. The problem 160297Seric ** is that some versions of sndmsg (like at LBL) set the kill 161297Seric ** character to something other than @ when reading addresses; 162297Seric ** so people type "csvax.eric\@berkeley" -- which screws up the 163297Seric ** berknet mailer. 164297Seric ** 165297Seric ** Parameters: 166297Seric ** addr -- the name to chomp. 167297Seric ** delim -- the delimiter for the address, normally 168297Seric ** '\0' or ','; \0 is accepted in any case. 169*15284Seric ** If '\t' then we are reading the .cf file. 170297Seric ** 171297Seric ** Returns: 1723149Seric ** A pointer to a vector of tokens. 173297Seric ** NULL on error. 174297Seric ** 175297Seric ** Side Effects: 1763149Seric ** none. 177297Seric */ 178297Seric 1798078Seric /* states and character types */ 1808078Seric # define OPR 0 /* operator */ 1818078Seric # define ATM 1 /* atom */ 1828078Seric # define QST 2 /* in quoted string */ 1838078Seric # define SPC 3 /* chewing up spaces */ 1848078Seric # define ONE 4 /* pick up one character */ 1853149Seric 1868078Seric # define NSTATES 5 /* number of states */ 1878078Seric # define TYPE 017 /* mask to select state type */ 1888078Seric 1898078Seric /* meta bits for table */ 1908078Seric # define M 020 /* meta character; don't pass through */ 1918078Seric # define B 040 /* cause a break */ 1928078Seric # define MB M|B /* meta-break */ 1938078Seric 1948078Seric static short StateTab[NSTATES][NSTATES] = 1958078Seric { 1968087Seric /* oldst chtype> OPR ATM QST SPC ONE */ 1979051Seric /*OPR*/ OPR|B, ATM|B, QST|B, SPC|MB, ONE|B, 1989051Seric /*ATM*/ OPR|B, ATM, QST|B, SPC|MB, ONE|B, 1999051Seric /*QST*/ QST, QST, OPR, QST, QST, 2008078Seric /*SPC*/ OPR, ATM, QST, SPC|M, ONE, 2018078Seric /*ONE*/ OPR, OPR, OPR, OPR, OPR, 2028078Seric }; 2038078Seric 2048078Seric # define NOCHAR -1 /* signal nothing in lookahead token */ 2058078Seric 2068078Seric char *DelimChar; /* set to point to the delimiter */ 2078078Seric 2083149Seric char ** 2093149Seric prescan(addr, delim) 210297Seric char *addr; 211297Seric char delim; 212297Seric { 213297Seric register char *p; 2148078Seric register char *q; 2159346Seric register int c; 2163149Seric char **avp; 217297Seric bool bslashmode; 218297Seric int cmntcnt; 2198423Seric int anglecnt; 2203149Seric char *tok; 2218078Seric int state; 2228078Seric int newstate; 2238078Seric static char buf[MAXNAME+MAXATOM]; 2248078Seric static char *av[MAXATOM+1]; 22515253Seric extern int errno; 226297Seric 22715253Seric /* make sure error messages don't have garbage on them */ 22815253Seric errno = 0; 22915253Seric 230297Seric q = buf; 2313149Seric bslashmode = FALSE; 2327800Seric cmntcnt = 0; 2338423Seric anglecnt = 0; 2343149Seric avp = av; 2358078Seric state = OPR; 2368078Seric c = NOCHAR; 2378078Seric p = addr; 2388078Seric # ifdef DEBUG 2398078Seric if (tTd(22, 45)) 240297Seric { 2418078Seric printf("prescan: "); 2428078Seric xputs(p); 2438078Seric putchar('\n'); 2448078Seric } 2458078Seric # endif DEBUG 2468078Seric 2478078Seric do 2488078Seric { 2493149Seric /* read a token */ 2503149Seric tok = q; 2518078Seric for (;;) 252297Seric { 2538078Seric /* store away any old lookahead character */ 2548078Seric if (c != NOCHAR) 2558078Seric { 256*15284Seric /* see if there is room */ 2578078Seric if (q >= &buf[sizeof buf - 5]) 2588078Seric { 2598078Seric usrerr("Address too long"); 2608078Seric DelimChar = p; 2618078Seric return (NULL); 2628078Seric } 263*15284Seric 264*15284Seric /* squirrel it away */ 2658078Seric *q++ = c; 2668078Seric } 2678078Seric 2688078Seric /* read a new input character */ 2698078Seric c = *p++; 2708078Seric if (c == '\0') 2718078Seric break; 272*15284Seric c &= ~0200; 273*15284Seric 2748078Seric # ifdef DEBUG 2758078Seric if (tTd(22, 101)) 2768078Seric printf("c=%c, s=%d; ", c, state); 2778078Seric # endif DEBUG 2788078Seric 2793149Seric /* chew up special characters */ 2803149Seric *q = '\0'; 2813149Seric if (bslashmode) 2823149Seric { 2833149Seric c |= 0200; 2843149Seric bslashmode = FALSE; 2853149Seric } 2863149Seric else if (c == '\\') 2873149Seric { 2883149Seric bslashmode = TRUE; 2898078Seric c = NOCHAR; 2903149Seric } 2918514Seric else if (state == QST) 2928514Seric { 2938514Seric /* do nothing, just avoid next clauses */ 2948514Seric } 2958078Seric else if (c == '(') 2964100Seric { 2978078Seric cmntcnt++; 2988078Seric c = NOCHAR; 2994100Seric } 3008078Seric else if (c == ')') 3013149Seric { 3028078Seric if (cmntcnt <= 0) 3033149Seric { 3048078Seric usrerr("Unbalanced ')'"); 3058078Seric DelimChar = p; 3068078Seric return (NULL); 3073149Seric } 3088078Seric else 3098078Seric cmntcnt--; 3108078Seric } 3118078Seric else if (cmntcnt > 0) 3128078Seric c = NOCHAR; 3138423Seric else if (c == '<') 3148423Seric anglecnt++; 3158423Seric else if (c == '>') 3168423Seric { 3178423Seric if (anglecnt <= 0) 3188423Seric { 3198423Seric usrerr("Unbalanced '>'"); 3208423Seric DelimChar = p; 3218423Seric return (NULL); 3228423Seric } 3238423Seric anglecnt--; 3248423Seric } 32511423Seric else if (delim == ' ' && isspace(c)) 32611423Seric c = ' '; 3273149Seric 3288078Seric if (c == NOCHAR) 3298078Seric continue; 3303149Seric 3318078Seric /* see if this is end of input */ 33211405Seric if (c == delim && anglecnt <= 0 && state != QST) 3333149Seric break; 3343149Seric 3358078Seric newstate = StateTab[state][toktype(c)]; 3368078Seric # ifdef DEBUG 3378078Seric if (tTd(22, 101)) 3388078Seric printf("ns=%02o\n", newstate); 3398078Seric # endif DEBUG 3408078Seric state = newstate & TYPE; 3418078Seric if (bitset(M, newstate)) 3428078Seric c = NOCHAR; 3438078Seric if (bitset(B, newstate)) 3444228Seric break; 345297Seric } 3463149Seric 3473149Seric /* new token */ 3488078Seric if (tok != q) 3491378Seric { 3508078Seric *q++ = '\0'; 3518078Seric # ifdef DEBUG 3528078Seric if (tTd(22, 36)) 353297Seric { 3548078Seric printf("tok="); 3558078Seric xputs(tok); 3568078Seric putchar('\n'); 357297Seric } 3588078Seric # endif DEBUG 3598078Seric if (avp >= &av[MAXATOM]) 360297Seric { 3618078Seric syserr("prescan: too many tokens"); 3628078Seric DelimChar = p; 3638078Seric return (NULL); 364297Seric } 3658078Seric *avp++ = tok; 366297Seric } 3678423Seric } while (c != '\0' && (c != delim || anglecnt > 0)); 3683149Seric *avp = NULL; 3698078Seric DelimChar = --p; 3703149Seric if (cmntcnt > 0) 3713149Seric usrerr("Unbalanced '('"); 3728423Seric else if (anglecnt > 0) 3738423Seric usrerr("Unbalanced '<'"); 3748078Seric else if (state == QST) 3753149Seric usrerr("Unbalanced '\"'"); 3763149Seric else if (av[0] != NULL) 3773149Seric return (av); 3783149Seric return (NULL); 3793149Seric } 3803149Seric /* 3813149Seric ** TOKTYPE -- return token type 3823149Seric ** 3833149Seric ** Parameters: 3843149Seric ** c -- the character in question. 3853149Seric ** 3863149Seric ** Returns: 3873149Seric ** Its type. 3883149Seric ** 3893149Seric ** Side Effects: 3903149Seric ** none. 3913149Seric */ 392297Seric 3933149Seric toktype(c) 3943149Seric register char c; 3953149Seric { 3963380Seric static char buf[50]; 3973382Seric static bool firstime = TRUE; 3983380Seric 3993382Seric if (firstime) 4003380Seric { 4013382Seric firstime = FALSE; 4026977Seric expand("$o", buf, &buf[sizeof buf - 1], CurEnv); 4037005Seric (void) strcat(buf, DELIMCHARS); 4043380Seric } 4059585Seric if (c == MATCHCLASS || c == MATCHREPL || c == MATCHNCLASS) 4068078Seric return (ONE); 4078078Seric if (c == '"') 4088078Seric return (QST); 4094100Seric if (!isascii(c)) 4108078Seric return (ATM); 4118078Seric if (isspace(c) || c == ')') 4128078Seric return (SPC); 4133380Seric if (iscntrl(c) || index(buf, c) != NULL) 4148078Seric return (OPR); 4158078Seric return (ATM); 4163149Seric } 4173149Seric /* 4183149Seric ** REWRITE -- apply rewrite rules to token vector. 4193149Seric ** 4204476Seric ** This routine is an ordered production system. Each rewrite 4214476Seric ** rule has a LHS (called the pattern) and a RHS (called the 4224476Seric ** rewrite); 'rwr' points the the current rewrite rule. 4234476Seric ** 4244476Seric ** For each rewrite rule, 'avp' points the address vector we 4254476Seric ** are trying to match against, and 'pvp' points to the pattern. 4268058Seric ** If pvp points to a special match value (MATCHZANY, MATCHANY, 4279585Seric ** MATCHONE, MATCHCLASS, MATCHNCLASS) then the address in avp 4289585Seric ** matched is saved away in the match vector (pointed to by 'mvp'). 4294476Seric ** 4304476Seric ** When a match between avp & pvp does not match, we try to 4319585Seric ** back out. If we back up over MATCHONE, MATCHCLASS, or MATCHNCLASS 4324476Seric ** we must also back out the match in mvp. If we reach a 4338058Seric ** MATCHANY or MATCHZANY we just extend the match and start 4348058Seric ** over again. 4354476Seric ** 4364476Seric ** When we finally match, we rewrite the address vector 4374476Seric ** and try over again. 4384476Seric ** 4393149Seric ** Parameters: 4403149Seric ** pvp -- pointer to token vector. 4413149Seric ** 4423149Seric ** Returns: 4433149Seric ** none. 4443149Seric ** 4453149Seric ** Side Effects: 4463149Seric ** pvp is modified. 4473149Seric */ 4482091Seric 4493149Seric struct match 4503149Seric { 4514468Seric char **first; /* first token matched */ 4524468Seric char **last; /* last token matched */ 4533149Seric }; 4543149Seric 4554468Seric # define MAXMATCH 9 /* max params per rewrite */ 4563149Seric 4573149Seric 4584070Seric rewrite(pvp, ruleset) 4593149Seric char **pvp; 4604070Seric int ruleset; 4613149Seric { 4623149Seric register char *ap; /* address pointer */ 4633149Seric register char *rp; /* rewrite pointer */ 4643149Seric register char **avp; /* address vector pointer */ 4653149Seric register char **rvp; /* rewrite vector pointer */ 4668058Seric register struct match *mlp; /* cur ptr into mlist */ 4678058Seric register struct rewrite *rwr; /* pointer to current rewrite rule */ 4684468Seric struct match mlist[MAXMATCH]; /* stores match on LHS */ 4693149Seric char *npvp[MAXATOM+1]; /* temporary space for rebuild */ 4704060Seric extern bool sameword(); 4713149Seric 4729279Seric if (OpMode == MD_TEST || tTd(21, 2)) 4733149Seric { 4748959Seric printf("rewrite: ruleset %2d input:", ruleset); 4753149Seric printav(pvp); 4763149Seric } 4778423Seric if (pvp == NULL) 4788423Seric return; 4793149Seric 4803149Seric /* 4813149Seric ** Run through the list of rewrite rules, applying 4823149Seric ** any that match. 4833149Seric */ 4843149Seric 4854070Seric for (rwr = RewriteRules[ruleset]; rwr != NULL; ) 4863149Seric { 4874100Seric # ifdef DEBUG 4887675Seric if (tTd(21, 12)) 489297Seric { 4908069Seric printf("-----trying rule:"); 4913149Seric printav(rwr->r_lhs); 4923149Seric } 4934100Seric # endif DEBUG 4943149Seric 4953149Seric /* try to match on this rule */ 4964468Seric mlp = mlist; 4978058Seric rvp = rwr->r_lhs; 4988058Seric avp = pvp; 4998058Seric while ((ap = *avp) != NULL || *rvp != NULL) 5003149Seric { 5013149Seric rp = *rvp; 5028058Seric # ifdef DEBUG 5038058Seric if (tTd(21, 35)) 5048058Seric { 5058069Seric printf("ap="); 5068058Seric xputs(ap); 5078069Seric printf(", rp="); 5088058Seric xputs(rp); 5098069Seric printf("\n"); 5108058Seric } 5118058Seric # endif DEBUG 5123149Seric if (rp == NULL) 513297Seric { 5143149Seric /* end-of-pattern before end-of-address */ 5158058Seric goto backup; 516297Seric } 5178058Seric if (ap == NULL && *rp != MATCHZANY) 5188058Seric { 5198058Seric /* end-of-input */ 5208058Seric break; 5218058Seric } 5223149Seric 5233149Seric switch (*rp) 5243149Seric { 5254060Seric register STAB *s; 5264060Seric 5274060Seric case MATCHCLASS: 5289585Seric case MATCHNCLASS: 5299585Seric /* match any token in (not in) a class */ 5304100Seric s = stab(ap, ST_CLASS, ST_FIND); 53110690Seric if (s == NULL || !bitnset(rp[1], s->s_class)) 5329585Seric { 5339585Seric if (*rp == MATCHCLASS) 5349585Seric goto backup; 5359585Seric } 5369585Seric else if (*rp == MATCHNCLASS) 5378058Seric goto backup; 5384468Seric 5394476Seric /* explicit fall-through */ 5404476Seric 5414476Seric case MATCHONE: 5424476Seric case MATCHANY: 5434476Seric /* match exactly one token */ 5448058Seric mlp->first = avp; 5458058Seric mlp->last = avp++; 5464468Seric mlp++; 5474060Seric break; 5484060Seric 5498058Seric case MATCHZANY: 5508058Seric /* match zero or more tokens */ 5518058Seric mlp->first = avp; 5528058Seric mlp->last = avp - 1; 5538058Seric mlp++; 5548058Seric break; 5558058Seric 5563149Seric default: 5573149Seric /* must have exact match */ 5584060Seric if (!sameword(rp, ap)) 5598058Seric goto backup; 5604468Seric avp++; 5613149Seric break; 5623149Seric } 5633149Seric 5643149Seric /* successful match on this token */ 5653149Seric rvp++; 5663149Seric continue; 5673149Seric 5688058Seric backup: 5693149Seric /* match failed -- back up */ 5703149Seric while (--rvp >= rwr->r_lhs) 5713149Seric { 5723149Seric rp = *rvp; 5738058Seric if (*rp == MATCHANY || *rp == MATCHZANY) 5744468Seric { 5754476Seric /* extend binding and continue */ 5768058Seric avp = ++mlp[-1].last; 5778058Seric avp++; 5784476Seric rvp++; 5793149Seric break; 5804468Seric } 5814476Seric avp--; 5829585Seric if (*rp == MATCHONE || *rp == MATCHCLASS || 5839585Seric *rp == MATCHNCLASS) 5843149Seric { 5854468Seric /* back out binding */ 5864468Seric mlp--; 5873149Seric } 5883149Seric } 5893149Seric 5903149Seric if (rvp < rwr->r_lhs) 5913149Seric { 5923149Seric /* total failure to match */ 5933149Seric break; 5943149Seric } 595297Seric } 5963149Seric 5973149Seric /* 5983149Seric ** See if we successfully matched 5993149Seric */ 6003149Seric 6019374Seric if (rvp < rwr->r_lhs || *rvp != NULL) 6023149Seric { 6034100Seric # ifdef DEBUG 6049374Seric if (tTd(21, 10)) 6059374Seric printf("----- rule fails\n"); 6064100Seric # endif DEBUG 6079374Seric rwr = rwr->r_next; 6089374Seric continue; 6099374Seric } 6103149Seric 6119374Seric rvp = rwr->r_rhs; 6129374Seric # ifdef DEBUG 6139374Seric if (tTd(21, 12)) 6149374Seric { 6159374Seric printf("-----rule matches:"); 6169374Seric printav(rvp); 6179374Seric } 6189374Seric # endif DEBUG 6199374Seric 6209374Seric rp = *rvp; 6219374Seric if (*rp == CANONUSER) 6229374Seric { 6239374Seric rvp++; 6249374Seric rwr = rwr->r_next; 6259374Seric } 6269374Seric else if (*rp == CANONHOST) 6279374Seric { 6289374Seric rvp++; 6299374Seric rwr = NULL; 6309374Seric } 6319374Seric else if (*rp == CANONNET) 6329374Seric rwr = NULL; 6339374Seric 6349374Seric /* substitute */ 6359374Seric for (avp = npvp; *rvp != NULL; rvp++) 6369374Seric { 6379374Seric register struct match *m; 6389374Seric register char **pp; 6399374Seric 6408058Seric rp = *rvp; 6419374Seric if (*rp != MATCHREPL) 6428058Seric { 6439374Seric if (avp >= &npvp[MAXATOM]) 6449374Seric { 6459374Seric syserr("rewrite: expansion too long"); 6469374Seric return; 6479374Seric } 6489374Seric *avp++ = rp; 6499374Seric continue; 6508069Seric } 6518058Seric 6529374Seric /* substitute from LHS */ 6539374Seric m = &mlist[rp[1] - '1']; 6549374Seric # ifdef DEBUG 6559374Seric if (tTd(21, 15)) 6563149Seric { 6579374Seric printf("$%c:", rp[1]); 6589374Seric pp = m->first; 6599374Seric while (pp <= m->last) 6603149Seric { 6619374Seric printf(" %x=\"", *pp); 6629374Seric (void) fflush(stdout); 6639374Seric printf("%s\"", *pp++); 6643149Seric } 6659374Seric printf("\n"); 6663149Seric } 6678226Seric # endif DEBUG 6689374Seric pp = m->first; 6699374Seric while (pp <= m->last) 6708226Seric { 6719374Seric if (avp >= &npvp[MAXATOM]) 6729374Seric { 6739374Seric syserr("rewrite: expansion too long"); 6749374Seric return; 6759374Seric } 6769374Seric *avp++ = *pp++; 6778226Seric } 6789374Seric } 6799374Seric *avp++ = NULL; 6809374Seric if (**npvp == CALLSUBR) 6819374Seric { 6829374Seric bmove((char *) &npvp[2], (char *) pvp, 6839374Seric (avp - npvp - 2) * sizeof *avp); 6848226Seric # ifdef DEBUG 6859374Seric if (tTd(21, 3)) 6869374Seric printf("-----callsubr %s\n", npvp[1]); 6873149Seric # endif DEBUG 6889374Seric rewrite(pvp, atoi(npvp[1])); 6893149Seric } 6903149Seric else 6913149Seric { 6929374Seric bmove((char *) npvp, (char *) pvp, 6939374Seric (avp - npvp) * sizeof *avp); 6949374Seric } 6954100Seric # ifdef DEBUG 6969374Seric if (tTd(21, 4)) 6979374Seric { 6989374Seric printf("rewritten as:"); 6999374Seric printav(pvp); 7009374Seric } 7014100Seric # endif DEBUG 702297Seric } 7038069Seric 7049279Seric if (OpMode == MD_TEST || tTd(21, 2)) 7058069Seric { 7068959Seric printf("rewrite: ruleset %2d returns:", ruleset); 7078069Seric printav(pvp); 7088069Seric } 7093149Seric } 7103149Seric /* 7113149Seric ** BUILDADDR -- build address from token vector. 7123149Seric ** 7133149Seric ** Parameters: 7143149Seric ** tv -- token vector. 7153149Seric ** a -- pointer to address descriptor to fill. 7163149Seric ** If NULL, one will be allocated. 7173149Seric ** 7183149Seric ** Returns: 7194279Seric ** NULL if there was an error. 7204279Seric ** 'a' otherwise. 7213149Seric ** 7223149Seric ** Side Effects: 7233149Seric ** fills in 'a' 7243149Seric */ 7253149Seric 7263149Seric ADDRESS * 7273149Seric buildaddr(tv, a) 7283149Seric register char **tv; 7293149Seric register ADDRESS *a; 7303149Seric { 7313149Seric static char buf[MAXNAME]; 7323149Seric struct mailer **mp; 7333149Seric register struct mailer *m; 7344635Seric extern bool sameword(); 7353149Seric 7363149Seric if (a == NULL) 7373149Seric a = (ADDRESS *) xalloc(sizeof *a); 7384988Seric clear((char *) a, sizeof *a); 7393149Seric 7403149Seric /* figure out what net/mailer to use */ 7413149Seric if (**tv != CANONNET) 7424279Seric { 7433149Seric syserr("buildaddr: no net"); 7444279Seric return (NULL); 7454279Seric } 7463149Seric tv++; 7474635Seric if (sameword(*tv, "error")) 7484279Seric { 74910183Seric if (**++tv == CANONHOST) 75010183Seric { 75110183Seric setstat(atoi(*++tv)); 75210183Seric tv++; 75310183Seric } 75410183Seric if (**tv != CANONUSER) 7554279Seric syserr("buildaddr: error: no user"); 7564279Seric buf[0] = '\0'; 7574279Seric while (*++tv != NULL) 7584279Seric { 7594279Seric if (buf[0] != '\0') 7607005Seric (void) strcat(buf, " "); 7617005Seric (void) strcat(buf, *tv); 7624279Seric } 7634279Seric usrerr(buf); 7644279Seric return (NULL); 7654279Seric } 7664598Seric for (mp = Mailer; (m = *mp++) != NULL; ) 7673149Seric { 7684635Seric if (sameword(m->m_name, *tv)) 7693149Seric break; 7703149Seric } 7713149Seric if (m == NULL) 7724279Seric { 7733149Seric syserr("buildaddr: unknown net %s", *tv); 7744279Seric return (NULL); 7754279Seric } 7764598Seric a->q_mailer = m; 7773149Seric 7783149Seric /* figure out what host (if any) */ 7793149Seric tv++; 78010690Seric if (!bitnset(M_LOCAL, m->m_flags)) 7813149Seric { 7825704Seric if (**tv++ != CANONHOST) 7834279Seric { 7843149Seric syserr("buildaddr: no host"); 7854279Seric return (NULL); 7864279Seric } 7875704Seric buf[0] = '\0'; 7885704Seric while (*tv != NULL && **tv != CANONUSER) 7897005Seric (void) strcat(buf, *tv++); 7905704Seric a->q_host = newstr(buf); 7913149Seric } 7923149Seric else 7933149Seric a->q_host = NULL; 7943149Seric 7953149Seric /* figure out the user */ 7963149Seric if (**tv != CANONUSER) 7974279Seric { 7983149Seric syserr("buildaddr: no user"); 7994279Seric return (NULL); 8004279Seric } 80111278Seric rewrite(++tv, 4); 80211278Seric cataddr(tv, buf, sizeof buf); 8033149Seric a->q_user = buf; 8043149Seric 8053149Seric return (a); 8063149Seric } 8073188Seric /* 8084228Seric ** CATADDR -- concatenate pieces of addresses (putting in <LWSP> subs) 8094228Seric ** 8104228Seric ** Parameters: 8114228Seric ** pvp -- parameter vector to rebuild. 8124228Seric ** buf -- buffer to build the string into. 8134228Seric ** sz -- size of buf. 8144228Seric ** 8154228Seric ** Returns: 8164228Seric ** none. 8174228Seric ** 8184228Seric ** Side Effects: 8194228Seric ** Destroys buf. 8204228Seric */ 8214228Seric 8224228Seric cataddr(pvp, buf, sz) 8234228Seric char **pvp; 8244228Seric char *buf; 8254228Seric register int sz; 8264228Seric { 8274228Seric bool oatomtok = FALSE; 8284228Seric bool natomtok = FALSE; 8294228Seric register int i; 8304228Seric register char *p; 8314228Seric 8328423Seric if (pvp == NULL) 8338423Seric { 8348423Seric strcpy(buf, ""); 8358423Seric return; 8368423Seric } 8374228Seric p = buf; 83811156Seric sz -= 2; 8394228Seric while (*pvp != NULL && (i = strlen(*pvp)) < sz) 8404228Seric { 8418078Seric natomtok = (toktype(**pvp) == ATM); 8424228Seric if (oatomtok && natomtok) 8439042Seric *p++ = SpaceSub; 8444228Seric (void) strcpy(p, *pvp); 8454228Seric oatomtok = natomtok; 8464228Seric p += i; 84711156Seric sz -= i + 1; 8484228Seric pvp++; 8494228Seric } 8504228Seric *p = '\0'; 8514228Seric } 8524228Seric /* 8533188Seric ** SAMEADDR -- Determine if two addresses are the same 8543188Seric ** 8553188Seric ** This is not just a straight comparison -- if the mailer doesn't 8563188Seric ** care about the host we just ignore it, etc. 8573188Seric ** 8583188Seric ** Parameters: 8593188Seric ** a, b -- pointers to the internal forms to compare. 8603188Seric ** 8613188Seric ** Returns: 8623188Seric ** TRUE -- they represent the same mailbox. 8633188Seric ** FALSE -- they don't. 8643188Seric ** 8653188Seric ** Side Effects: 8663188Seric ** none. 8673188Seric */ 8683188Seric 8693188Seric bool 8709374Seric sameaddr(a, b) 8713188Seric register ADDRESS *a; 8723188Seric register ADDRESS *b; 8733188Seric { 8743188Seric /* if they don't have the same mailer, forget it */ 8753188Seric if (a->q_mailer != b->q_mailer) 8763188Seric return (FALSE); 8773188Seric 8783188Seric /* if the user isn't the same, we can drop out */ 8799374Seric if (strcmp(a->q_user, b->q_user) != 0) 8803188Seric return (FALSE); 8813188Seric 8823188Seric /* if the mailer ignores hosts, we have succeeded! */ 88310690Seric if (bitnset(M_LOCAL, a->q_mailer->m_flags)) 8843188Seric return (TRUE); 8853188Seric 8863188Seric /* otherwise compare hosts (but be careful for NULL ptrs) */ 8873188Seric if (a->q_host == NULL || b->q_host == NULL) 8883188Seric return (FALSE); 8893188Seric if (strcmp(a->q_host, b->q_host) != 0) 8903188Seric return (FALSE); 8913188Seric 8923188Seric return (TRUE); 8933188Seric } 8943234Seric /* 8953234Seric ** PRINTADDR -- print address (for debugging) 8963234Seric ** 8973234Seric ** Parameters: 8983234Seric ** a -- the address to print 8993234Seric ** follow -- follow the q_next chain. 9003234Seric ** 9013234Seric ** Returns: 9023234Seric ** none. 9033234Seric ** 9043234Seric ** Side Effects: 9053234Seric ** none. 9063234Seric */ 9073234Seric 9084317Seric # ifdef DEBUG 9094317Seric 9103234Seric printaddr(a, follow) 9113234Seric register ADDRESS *a; 9123234Seric bool follow; 9133234Seric { 9145001Seric bool first = TRUE; 9155001Seric 9163234Seric while (a != NULL) 9173234Seric { 9185001Seric first = FALSE; 9194443Seric printf("%x=", a); 9204085Seric (void) fflush(stdout); 9213234Seric printf("%s: mailer %d (%s), host `%s', user `%s'\n", a->q_paddr, 9228181Seric a->q_mailer->m_mno, a->q_mailer->m_name, a->q_host, 9238181Seric a->q_user); 9248181Seric printf("\tnext=%x, flags=%o, alias %x\n", a->q_next, a->q_flags, 9258181Seric a->q_alias); 9268181Seric printf("\thome=\"%s\", fullname=\"%s\"\n", a->q_home, 9278181Seric a->q_fullname); 9284996Seric 9293234Seric if (!follow) 9303234Seric return; 9314996Seric a = a->q_next; 9323234Seric } 9335001Seric if (first) 9344443Seric printf("[NULL]\n"); 9353234Seric } 9364317Seric 9374317Seric # endif DEBUG 9387682Seric /* 9397682Seric ** REMOTENAME -- return the name relative to the current mailer 9407682Seric ** 9417682Seric ** Parameters: 9427682Seric ** name -- the name to translate. 9438069Seric ** m -- the mailer that we want to do rewriting relative 9448069Seric ** to. 9458069Seric ** senderaddress -- if set, uses the sender rewriting rules 9468069Seric ** rather than the recipient rewriting rules. 94710310Seric ** canonical -- if set, strip out any comment information, 94810310Seric ** etc. 9497682Seric ** 9507682Seric ** Returns: 9517682Seric ** the text string representing this address relative to 9527682Seric ** the receiving mailer. 9537682Seric ** 9547682Seric ** Side Effects: 9557682Seric ** none. 9567682Seric ** 9577682Seric ** Warnings: 9587682Seric ** The text string returned is tucked away locally; 9597682Seric ** copy it if you intend to save it. 9607682Seric */ 9617682Seric 9627682Seric char * 96310310Seric remotename(name, m, senderaddress, canonical) 9647682Seric char *name; 9657682Seric struct mailer *m; 9668069Seric bool senderaddress; 96710310Seric bool canonical; 9687682Seric { 9698069Seric register char **pvp; 9708069Seric char *fancy; 971*15284Seric register char *p; 9728069Seric extern char *macvalue(); 9738181Seric char *oldg = macvalue('g', CurEnv); 9747682Seric static char buf[MAXNAME]; 9757682Seric char lbuf[MAXNAME]; 9767682Seric extern char **prescan(); 9777889Seric extern char *crackaddr(); 9787682Seric 9797755Seric # ifdef DEBUG 9807755Seric if (tTd(12, 1)) 9817755Seric printf("remotename(%s)\n", name); 9827755Seric # endif DEBUG 9837755Seric 98410177Seric /* don't do anything if we are tagging it as special */ 98510177Seric if ((senderaddress ? m->m_s_rwset : m->m_r_rwset) < 0) 98610177Seric return (name); 98710177Seric 9887682Seric /* 9898181Seric ** Do a heuristic crack of this name to extract any comment info. 9908181Seric ** This will leave the name as a comment and a $g macro. 9917889Seric */ 9927889Seric 99310310Seric if (canonical) 99410310Seric fancy = "$g"; 99510310Seric else 99610310Seric fancy = crackaddr(name); 9977889Seric 9988181Seric /* 9998181Seric ** Turn the name into canonical form. 10008181Seric ** Normally this will be RFC 822 style, i.e., "user@domain". 10018181Seric ** If this only resolves to "user", and the "C" flag is 10028181Seric ** specified in the sending mailer, then the sender's 10038181Seric ** domain will be appended. 10048181Seric */ 10058181Seric 10067889Seric pvp = prescan(name, '\0'); 10077889Seric if (pvp == NULL) 10087889Seric return (name); 10098181Seric rewrite(pvp, 3); 10108181Seric if (CurEnv->e_fromdomain != NULL) 10118181Seric { 10128181Seric /* append from domain to this address */ 10138181Seric register char **pxp = pvp; 10148181Seric 10159594Seric /* see if there is an "@domain" in the current name */ 10168181Seric while (*pxp != NULL && strcmp(*pxp, "@") != 0) 10178181Seric pxp++; 10188181Seric if (*pxp == NULL) 10198181Seric { 10209594Seric /* no.... append the "@domain" from the sender */ 10218181Seric register char **qxq = CurEnv->e_fromdomain; 10228181Seric 10239594Seric while ((*pxp++ = *qxq++) != NULL) 10249594Seric continue; 102511726Seric rewrite(pvp, 3); 10268181Seric } 10278181Seric } 10288181Seric 10298181Seric /* 10308959Seric ** Do more specific rewriting. 10318181Seric ** Rewrite using ruleset 1 or 2 depending on whether this is 10328181Seric ** a sender address or not. 10338181Seric ** Then run it through any receiving-mailer-specific rulesets. 10348181Seric */ 10358181Seric 10368069Seric if (senderaddress) 10377755Seric { 10387889Seric rewrite(pvp, 1); 10398069Seric if (m->m_s_rwset > 0) 10408069Seric rewrite(pvp, m->m_s_rwset); 10418069Seric } 10428069Seric else 10438069Seric { 10447889Seric rewrite(pvp, 2); 10458069Seric if (m->m_r_rwset > 0) 10468069Seric rewrite(pvp, m->m_r_rwset); 10477682Seric } 10487682Seric 10498181Seric /* 10508959Seric ** Do any final sanitation the address may require. 10518959Seric ** This will normally be used to turn internal forms 10528959Seric ** (e.g., user@host.LOCAL) into external form. This 10538959Seric ** may be used as a default to the above rules. 10548959Seric */ 10558959Seric 10568959Seric rewrite(pvp, 4); 10578959Seric 10588959Seric /* 10598181Seric ** Now restore the comment information we had at the beginning. 1060*15284Seric ** Make sure that any real '$' characters in the input are 1061*15284Seric ** not accidently interpreted as macro expansions by quoting 1062*15284Seric ** them before expansion. 10638181Seric */ 10648181Seric 10657682Seric cataddr(pvp, lbuf, sizeof lbuf); 1066*15284Seric for (p = lbuf; *p != '\0'; p++) 1067*15284Seric if (*p == '$') 1068*15284Seric *p |= 0200; 10699374Seric define('g', lbuf, CurEnv); 10707889Seric expand(fancy, buf, &buf[sizeof buf - 1], CurEnv); 10719374Seric define('g', oldg, CurEnv); 1072*15284Seric stripquotes(buf, FALSE); 10737682Seric 10747682Seric # ifdef DEBUG 10757682Seric if (tTd(12, 1)) 10767755Seric printf("remotename => `%s'\n", buf); 10777682Seric # endif DEBUG 10787682Seric return (buf); 10797682Seric } 1080