13312Seric # include "sendmail.h" 2297Seric 3*16202Seric SCCSID(@(#)parseaddr.c 4.6 03/17/84); 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 */ 4416155Seric # define DELIMCHARS "\001()<>,;\\\"\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 /* 127*16202Seric ** Convert host name to lower case if requested. 128*16202Seric ** User name will be done later. 129*16202Seric */ 130*16202Seric 131*16202Seric if (!bitnset(M_HST_UPPER, m->m_flags)) 132*16202Seric makelower(a->q_host); 133*16202Seric 134*16202Seric /* 135297Seric ** Compute return value. 136297Seric */ 137297Seric 138297Seric # ifdef DEBUG 1397675Seric if (tTd(20, 1)) 1404443Seric { 1419888Seric printf("parseaddr-->"); 1424443Seric printaddr(a, FALSE); 1434443Seric } 144297Seric # endif DEBUG 145297Seric 146297Seric return (a); 147297Seric } 148297Seric /* 14916162Seric ** LOWERADDR -- map UPPER->lower case on addresses as requested. 15016162Seric ** 15116162Seric ** Parameters: 15216162Seric ** a -- address to be mapped. 15316162Seric ** 15416162Seric ** Returns: 15516162Seric ** none. 15616162Seric ** 15716162Seric ** Side Effects: 15816162Seric ** none. 15916162Seric */ 16016162Seric 16116162Seric loweraddr(a) 16216162Seric register ADDRESS *a; 16316162Seric { 16416162Seric register MAILER *m = a->q_mailer; 16516162Seric 16616162Seric if (!bitnset(M_USR_UPPER, m->m_flags)) 16716162Seric makelower(a->q_user); 16816162Seric } 16916162Seric /* 170297Seric ** PRESCAN -- Prescan name and make it canonical 171297Seric ** 1729374Seric ** Scans a name and turns it into a set of tokens. This process 1739374Seric ** deletes blanks and comments (in parentheses). 174297Seric ** 175297Seric ** This routine knows about quoted strings and angle brackets. 176297Seric ** 177297Seric ** There are certain subtleties to this routine. The one that 178297Seric ** comes to mind now is that backslashes on the ends of names 179297Seric ** are silently stripped off; this is intentional. The problem 180297Seric ** is that some versions of sndmsg (like at LBL) set the kill 181297Seric ** character to something other than @ when reading addresses; 182297Seric ** so people type "csvax.eric\@berkeley" -- which screws up the 183297Seric ** berknet mailer. 184297Seric ** 185297Seric ** Parameters: 186297Seric ** addr -- the name to chomp. 187297Seric ** delim -- the delimiter for the address, normally 188297Seric ** '\0' or ','; \0 is accepted in any case. 18915284Seric ** If '\t' then we are reading the .cf file. 190297Seric ** 191297Seric ** Returns: 1923149Seric ** A pointer to a vector of tokens. 193297Seric ** NULL on error. 194297Seric ** 195297Seric ** Side Effects: 1963149Seric ** none. 197297Seric */ 198297Seric 1998078Seric /* states and character types */ 2008078Seric # define OPR 0 /* operator */ 2018078Seric # define ATM 1 /* atom */ 2028078Seric # define QST 2 /* in quoted string */ 2038078Seric # define SPC 3 /* chewing up spaces */ 2048078Seric # define ONE 4 /* pick up one character */ 2053149Seric 2068078Seric # define NSTATES 5 /* number of states */ 2078078Seric # define TYPE 017 /* mask to select state type */ 2088078Seric 2098078Seric /* meta bits for table */ 2108078Seric # define M 020 /* meta character; don't pass through */ 2118078Seric # define B 040 /* cause a break */ 2128078Seric # define MB M|B /* meta-break */ 2138078Seric 2148078Seric static short StateTab[NSTATES][NSTATES] = 2158078Seric { 2168087Seric /* oldst chtype> OPR ATM QST SPC ONE */ 2179051Seric /*OPR*/ OPR|B, ATM|B, QST|B, SPC|MB, ONE|B, 2189051Seric /*ATM*/ OPR|B, ATM, QST|B, SPC|MB, ONE|B, 2199051Seric /*QST*/ QST, QST, OPR, QST, QST, 2208078Seric /*SPC*/ OPR, ATM, QST, SPC|M, ONE, 2218078Seric /*ONE*/ OPR, OPR, OPR, OPR, OPR, 2228078Seric }; 2238078Seric 2248078Seric # define NOCHAR -1 /* signal nothing in lookahead token */ 2258078Seric 2268078Seric char *DelimChar; /* set to point to the delimiter */ 2278078Seric 2283149Seric char ** 2293149Seric prescan(addr, delim) 230297Seric char *addr; 231297Seric char delim; 232297Seric { 233297Seric register char *p; 2348078Seric register char *q; 2359346Seric register int c; 2363149Seric char **avp; 237297Seric bool bslashmode; 238297Seric int cmntcnt; 2398423Seric int anglecnt; 2403149Seric char *tok; 2418078Seric int state; 2428078Seric int newstate; 2438078Seric static char buf[MAXNAME+MAXATOM]; 2448078Seric static char *av[MAXATOM+1]; 24515253Seric extern int errno; 246297Seric 24715253Seric /* make sure error messages don't have garbage on them */ 24815253Seric errno = 0; 24915253Seric 250297Seric q = buf; 2513149Seric bslashmode = FALSE; 2527800Seric cmntcnt = 0; 2538423Seric anglecnt = 0; 2543149Seric avp = av; 2558078Seric state = OPR; 2568078Seric c = NOCHAR; 2578078Seric p = addr; 2588078Seric # ifdef DEBUG 2598078Seric if (tTd(22, 45)) 260297Seric { 2618078Seric printf("prescan: "); 2628078Seric xputs(p); 2638078Seric putchar('\n'); 2648078Seric } 2658078Seric # endif DEBUG 2668078Seric 2678078Seric do 2688078Seric { 2693149Seric /* read a token */ 2703149Seric tok = q; 2718078Seric for (;;) 272297Seric { 2738078Seric /* store away any old lookahead character */ 2748078Seric if (c != NOCHAR) 2758078Seric { 27615284Seric /* see if there is room */ 2778078Seric if (q >= &buf[sizeof buf - 5]) 2788078Seric { 2798078Seric usrerr("Address too long"); 2808078Seric DelimChar = p; 2818078Seric return (NULL); 2828078Seric } 28315284Seric 28415284Seric /* squirrel it away */ 2858078Seric *q++ = c; 2868078Seric } 2878078Seric 2888078Seric /* read a new input character */ 2898078Seric c = *p++; 2908078Seric if (c == '\0') 2918078Seric break; 29215284Seric c &= ~0200; 29315284Seric 2948078Seric # ifdef DEBUG 2958078Seric if (tTd(22, 101)) 2968078Seric printf("c=%c, s=%d; ", c, state); 2978078Seric # endif DEBUG 2988078Seric 2993149Seric /* chew up special characters */ 3003149Seric *q = '\0'; 3013149Seric if (bslashmode) 3023149Seric { 3033149Seric c |= 0200; 3043149Seric bslashmode = FALSE; 3053149Seric } 3063149Seric else if (c == '\\') 3073149Seric { 3083149Seric bslashmode = TRUE; 3098078Seric c = NOCHAR; 3103149Seric } 3118514Seric else if (state == QST) 3128514Seric { 3138514Seric /* do nothing, just avoid next clauses */ 3148514Seric } 3158078Seric else if (c == '(') 3164100Seric { 3178078Seric cmntcnt++; 3188078Seric c = NOCHAR; 3194100Seric } 3208078Seric else if (c == ')') 3213149Seric { 3228078Seric if (cmntcnt <= 0) 3233149Seric { 3248078Seric usrerr("Unbalanced ')'"); 3258078Seric DelimChar = p; 3268078Seric return (NULL); 3273149Seric } 3288078Seric else 3298078Seric cmntcnt--; 3308078Seric } 3318078Seric else if (cmntcnt > 0) 3328078Seric c = NOCHAR; 3338423Seric else if (c == '<') 3348423Seric anglecnt++; 3358423Seric else if (c == '>') 3368423Seric { 3378423Seric if (anglecnt <= 0) 3388423Seric { 3398423Seric usrerr("Unbalanced '>'"); 3408423Seric DelimChar = p; 3418423Seric return (NULL); 3428423Seric } 3438423Seric anglecnt--; 3448423Seric } 34511423Seric else if (delim == ' ' && isspace(c)) 34611423Seric c = ' '; 3473149Seric 3488078Seric if (c == NOCHAR) 3498078Seric continue; 3503149Seric 3518078Seric /* see if this is end of input */ 35211405Seric if (c == delim && anglecnt <= 0 && state != QST) 3533149Seric break; 3543149Seric 3558078Seric newstate = StateTab[state][toktype(c)]; 3568078Seric # ifdef DEBUG 3578078Seric if (tTd(22, 101)) 3588078Seric printf("ns=%02o\n", newstate); 3598078Seric # endif DEBUG 3608078Seric state = newstate & TYPE; 3618078Seric if (bitset(M, newstate)) 3628078Seric c = NOCHAR; 3638078Seric if (bitset(B, newstate)) 3644228Seric break; 365297Seric } 3663149Seric 3673149Seric /* new token */ 3688078Seric if (tok != q) 3691378Seric { 3708078Seric *q++ = '\0'; 3718078Seric # ifdef DEBUG 3728078Seric if (tTd(22, 36)) 373297Seric { 3748078Seric printf("tok="); 3758078Seric xputs(tok); 3768078Seric putchar('\n'); 377297Seric } 3788078Seric # endif DEBUG 3798078Seric if (avp >= &av[MAXATOM]) 380297Seric { 3818078Seric syserr("prescan: too many tokens"); 3828078Seric DelimChar = p; 3838078Seric return (NULL); 384297Seric } 3858078Seric *avp++ = tok; 386297Seric } 3878423Seric } while (c != '\0' && (c != delim || anglecnt > 0)); 3883149Seric *avp = NULL; 3898078Seric DelimChar = --p; 3903149Seric if (cmntcnt > 0) 3913149Seric usrerr("Unbalanced '('"); 3928423Seric else if (anglecnt > 0) 3938423Seric usrerr("Unbalanced '<'"); 3948078Seric else if (state == QST) 3953149Seric usrerr("Unbalanced '\"'"); 3963149Seric else if (av[0] != NULL) 3973149Seric return (av); 3983149Seric return (NULL); 3993149Seric } 4003149Seric /* 4013149Seric ** TOKTYPE -- return token type 4023149Seric ** 4033149Seric ** Parameters: 4043149Seric ** c -- the character in question. 4053149Seric ** 4063149Seric ** Returns: 4073149Seric ** Its type. 4083149Seric ** 4093149Seric ** Side Effects: 4103149Seric ** none. 4113149Seric */ 412297Seric 4133149Seric toktype(c) 4143149Seric register char c; 4153149Seric { 4163380Seric static char buf[50]; 4173382Seric static bool firstime = TRUE; 4183380Seric 4193382Seric if (firstime) 4203380Seric { 4213382Seric firstime = FALSE; 42216155Seric expand("\001o", buf, &buf[sizeof buf - 1], CurEnv); 4237005Seric (void) strcat(buf, DELIMCHARS); 4243380Seric } 4259585Seric if (c == MATCHCLASS || c == MATCHREPL || c == MATCHNCLASS) 4268078Seric return (ONE); 4278078Seric if (c == '"') 4288078Seric return (QST); 4294100Seric if (!isascii(c)) 4308078Seric return (ATM); 4318078Seric if (isspace(c) || c == ')') 4328078Seric return (SPC); 4333380Seric if (iscntrl(c) || index(buf, c) != NULL) 4348078Seric return (OPR); 4358078Seric return (ATM); 4363149Seric } 4373149Seric /* 4383149Seric ** REWRITE -- apply rewrite rules to token vector. 4393149Seric ** 4404476Seric ** This routine is an ordered production system. Each rewrite 4414476Seric ** rule has a LHS (called the pattern) and a RHS (called the 4424476Seric ** rewrite); 'rwr' points the the current rewrite rule. 4434476Seric ** 4444476Seric ** For each rewrite rule, 'avp' points the address vector we 4454476Seric ** are trying to match against, and 'pvp' points to the pattern. 4468058Seric ** If pvp points to a special match value (MATCHZANY, MATCHANY, 4479585Seric ** MATCHONE, MATCHCLASS, MATCHNCLASS) then the address in avp 4489585Seric ** matched is saved away in the match vector (pointed to by 'mvp'). 4494476Seric ** 4504476Seric ** When a match between avp & pvp does not match, we try to 4519585Seric ** back out. If we back up over MATCHONE, MATCHCLASS, or MATCHNCLASS 4524476Seric ** we must also back out the match in mvp. If we reach a 4538058Seric ** MATCHANY or MATCHZANY we just extend the match and start 4548058Seric ** over again. 4554476Seric ** 4564476Seric ** When we finally match, we rewrite the address vector 4574476Seric ** and try over again. 4584476Seric ** 4593149Seric ** Parameters: 4603149Seric ** pvp -- pointer to token vector. 4613149Seric ** 4623149Seric ** Returns: 4633149Seric ** none. 4643149Seric ** 4653149Seric ** Side Effects: 4663149Seric ** pvp is modified. 4673149Seric */ 4682091Seric 4693149Seric struct match 4703149Seric { 4714468Seric char **first; /* first token matched */ 4724468Seric char **last; /* last token matched */ 4733149Seric }; 4743149Seric 4754468Seric # define MAXMATCH 9 /* max params per rewrite */ 4763149Seric 4773149Seric 4784070Seric rewrite(pvp, ruleset) 4793149Seric char **pvp; 4804070Seric int ruleset; 4813149Seric { 4823149Seric register char *ap; /* address pointer */ 4833149Seric register char *rp; /* rewrite pointer */ 4843149Seric register char **avp; /* address vector pointer */ 4853149Seric register char **rvp; /* rewrite vector pointer */ 4868058Seric register struct match *mlp; /* cur ptr into mlist */ 4878058Seric register struct rewrite *rwr; /* pointer to current rewrite rule */ 4884468Seric struct match mlist[MAXMATCH]; /* stores match on LHS */ 4893149Seric char *npvp[MAXATOM+1]; /* temporary space for rebuild */ 4904060Seric extern bool sameword(); 4913149Seric 4929279Seric if (OpMode == MD_TEST || tTd(21, 2)) 4933149Seric { 4948959Seric printf("rewrite: ruleset %2d input:", ruleset); 4953149Seric printav(pvp); 4963149Seric } 4978423Seric if (pvp == NULL) 4988423Seric return; 4993149Seric 5003149Seric /* 5013149Seric ** Run through the list of rewrite rules, applying 5023149Seric ** any that match. 5033149Seric */ 5043149Seric 5054070Seric for (rwr = RewriteRules[ruleset]; rwr != NULL; ) 5063149Seric { 5074100Seric # ifdef DEBUG 5087675Seric if (tTd(21, 12)) 509297Seric { 5108069Seric printf("-----trying rule:"); 5113149Seric printav(rwr->r_lhs); 5123149Seric } 5134100Seric # endif DEBUG 5143149Seric 5153149Seric /* try to match on this rule */ 5164468Seric mlp = mlist; 5178058Seric rvp = rwr->r_lhs; 5188058Seric avp = pvp; 5198058Seric while ((ap = *avp) != NULL || *rvp != NULL) 5203149Seric { 5213149Seric rp = *rvp; 5228058Seric # ifdef DEBUG 5238058Seric if (tTd(21, 35)) 5248058Seric { 5258069Seric printf("ap="); 5268058Seric xputs(ap); 5278069Seric printf(", rp="); 5288058Seric xputs(rp); 5298069Seric printf("\n"); 5308058Seric } 5318058Seric # endif DEBUG 5323149Seric if (rp == NULL) 533297Seric { 5343149Seric /* end-of-pattern before end-of-address */ 5358058Seric goto backup; 536297Seric } 5378058Seric if (ap == NULL && *rp != MATCHZANY) 5388058Seric { 5398058Seric /* end-of-input */ 5408058Seric break; 5418058Seric } 5423149Seric 5433149Seric switch (*rp) 5443149Seric { 5454060Seric register STAB *s; 5464060Seric 5474060Seric case MATCHCLASS: 5489585Seric case MATCHNCLASS: 5499585Seric /* match any token in (not in) a class */ 5504100Seric s = stab(ap, ST_CLASS, ST_FIND); 55110690Seric if (s == NULL || !bitnset(rp[1], s->s_class)) 5529585Seric { 5539585Seric if (*rp == MATCHCLASS) 5549585Seric goto backup; 5559585Seric } 5569585Seric else if (*rp == MATCHNCLASS) 5578058Seric goto backup; 5584468Seric 5594476Seric /* explicit fall-through */ 5604476Seric 5614476Seric case MATCHONE: 5624476Seric case MATCHANY: 5634476Seric /* match exactly one token */ 5648058Seric mlp->first = avp; 5658058Seric mlp->last = avp++; 5664468Seric mlp++; 5674060Seric break; 5684060Seric 5698058Seric case MATCHZANY: 5708058Seric /* match zero or more tokens */ 5718058Seric mlp->first = avp; 5728058Seric mlp->last = avp - 1; 5738058Seric mlp++; 5748058Seric break; 5758058Seric 5763149Seric default: 5773149Seric /* must have exact match */ 5784060Seric if (!sameword(rp, ap)) 5798058Seric goto backup; 5804468Seric avp++; 5813149Seric break; 5823149Seric } 5833149Seric 5843149Seric /* successful match on this token */ 5853149Seric rvp++; 5863149Seric continue; 5873149Seric 5888058Seric backup: 5893149Seric /* match failed -- back up */ 5903149Seric while (--rvp >= rwr->r_lhs) 5913149Seric { 5923149Seric rp = *rvp; 5938058Seric if (*rp == MATCHANY || *rp == MATCHZANY) 5944468Seric { 5954476Seric /* extend binding and continue */ 5968058Seric avp = ++mlp[-1].last; 5978058Seric avp++; 5984476Seric rvp++; 5993149Seric break; 6004468Seric } 6014476Seric avp--; 6029585Seric if (*rp == MATCHONE || *rp == MATCHCLASS || 6039585Seric *rp == MATCHNCLASS) 6043149Seric { 6054468Seric /* back out binding */ 6064468Seric mlp--; 6073149Seric } 6083149Seric } 6093149Seric 6103149Seric if (rvp < rwr->r_lhs) 6113149Seric { 6123149Seric /* total failure to match */ 6133149Seric break; 6143149Seric } 615297Seric } 6163149Seric 6173149Seric /* 6183149Seric ** See if we successfully matched 6193149Seric */ 6203149Seric 6219374Seric if (rvp < rwr->r_lhs || *rvp != NULL) 6223149Seric { 6234100Seric # ifdef DEBUG 6249374Seric if (tTd(21, 10)) 6259374Seric printf("----- rule fails\n"); 6264100Seric # endif DEBUG 6279374Seric rwr = rwr->r_next; 6289374Seric continue; 6299374Seric } 6303149Seric 6319374Seric rvp = rwr->r_rhs; 6329374Seric # ifdef DEBUG 6339374Seric if (tTd(21, 12)) 6349374Seric { 6359374Seric printf("-----rule matches:"); 6369374Seric printav(rvp); 6379374Seric } 6389374Seric # endif DEBUG 6399374Seric 6409374Seric rp = *rvp; 6419374Seric if (*rp == CANONUSER) 6429374Seric { 6439374Seric rvp++; 6449374Seric rwr = rwr->r_next; 6459374Seric } 6469374Seric else if (*rp == CANONHOST) 6479374Seric { 6489374Seric rvp++; 6499374Seric rwr = NULL; 6509374Seric } 6519374Seric else if (*rp == CANONNET) 6529374Seric rwr = NULL; 6539374Seric 6549374Seric /* substitute */ 6559374Seric for (avp = npvp; *rvp != NULL; rvp++) 6569374Seric { 6579374Seric register struct match *m; 6589374Seric register char **pp; 6599374Seric 6608058Seric rp = *rvp; 6619374Seric if (*rp != MATCHREPL) 6628058Seric { 6639374Seric if (avp >= &npvp[MAXATOM]) 6649374Seric { 6659374Seric syserr("rewrite: expansion too long"); 6669374Seric return; 6679374Seric } 6689374Seric *avp++ = rp; 6699374Seric continue; 6708069Seric } 6718058Seric 6729374Seric /* substitute from LHS */ 6739374Seric m = &mlist[rp[1] - '1']; 6749374Seric # ifdef DEBUG 6759374Seric if (tTd(21, 15)) 6763149Seric { 6779374Seric printf("$%c:", rp[1]); 6789374Seric pp = m->first; 6799374Seric while (pp <= m->last) 6803149Seric { 6819374Seric printf(" %x=\"", *pp); 6829374Seric (void) fflush(stdout); 6839374Seric printf("%s\"", *pp++); 6843149Seric } 6859374Seric printf("\n"); 6863149Seric } 6878226Seric # endif DEBUG 6889374Seric pp = m->first; 6899374Seric while (pp <= m->last) 6908226Seric { 6919374Seric if (avp >= &npvp[MAXATOM]) 6929374Seric { 6939374Seric syserr("rewrite: expansion too long"); 6949374Seric return; 6959374Seric } 6969374Seric *avp++ = *pp++; 6978226Seric } 6989374Seric } 6999374Seric *avp++ = NULL; 7009374Seric if (**npvp == CALLSUBR) 7019374Seric { 7029374Seric bmove((char *) &npvp[2], (char *) pvp, 7039374Seric (avp - npvp - 2) * sizeof *avp); 7048226Seric # ifdef DEBUG 7059374Seric if (tTd(21, 3)) 7069374Seric printf("-----callsubr %s\n", npvp[1]); 7073149Seric # endif DEBUG 7089374Seric rewrite(pvp, atoi(npvp[1])); 7093149Seric } 7103149Seric else 7113149Seric { 7129374Seric bmove((char *) npvp, (char *) pvp, 7139374Seric (avp - npvp) * sizeof *avp); 7149374Seric } 7154100Seric # ifdef DEBUG 7169374Seric if (tTd(21, 4)) 7179374Seric { 7189374Seric printf("rewritten as:"); 7199374Seric printav(pvp); 7209374Seric } 7214100Seric # endif DEBUG 722297Seric } 7238069Seric 7249279Seric if (OpMode == MD_TEST || tTd(21, 2)) 7258069Seric { 7268959Seric printf("rewrite: ruleset %2d returns:", ruleset); 7278069Seric printav(pvp); 7288069Seric } 7293149Seric } 7303149Seric /* 7313149Seric ** BUILDADDR -- build address from token vector. 7323149Seric ** 7333149Seric ** Parameters: 7343149Seric ** tv -- token vector. 7353149Seric ** a -- pointer to address descriptor to fill. 7363149Seric ** If NULL, one will be allocated. 7373149Seric ** 7383149Seric ** Returns: 7394279Seric ** NULL if there was an error. 7404279Seric ** 'a' otherwise. 7413149Seric ** 7423149Seric ** Side Effects: 7433149Seric ** fills in 'a' 7443149Seric */ 7453149Seric 7463149Seric ADDRESS * 7473149Seric buildaddr(tv, a) 7483149Seric register char **tv; 7493149Seric register ADDRESS *a; 7503149Seric { 7513149Seric static char buf[MAXNAME]; 7523149Seric struct mailer **mp; 7533149Seric register struct mailer *m; 7544635Seric extern bool sameword(); 7553149Seric 7563149Seric if (a == NULL) 7573149Seric a = (ADDRESS *) xalloc(sizeof *a); 7584988Seric clear((char *) a, sizeof *a); 7593149Seric 7603149Seric /* figure out what net/mailer to use */ 7613149Seric if (**tv != CANONNET) 7624279Seric { 7633149Seric syserr("buildaddr: no net"); 7644279Seric return (NULL); 7654279Seric } 7663149Seric tv++; 7674635Seric if (sameword(*tv, "error")) 7684279Seric { 76910183Seric if (**++tv == CANONHOST) 77010183Seric { 77110183Seric setstat(atoi(*++tv)); 77210183Seric tv++; 77310183Seric } 77410183Seric if (**tv != CANONUSER) 7754279Seric syserr("buildaddr: error: no user"); 7764279Seric buf[0] = '\0'; 7774279Seric while (*++tv != NULL) 7784279Seric { 7794279Seric if (buf[0] != '\0') 7807005Seric (void) strcat(buf, " "); 7817005Seric (void) strcat(buf, *tv); 7824279Seric } 7834279Seric usrerr(buf); 7844279Seric return (NULL); 7854279Seric } 7864598Seric for (mp = Mailer; (m = *mp++) != NULL; ) 7873149Seric { 7884635Seric if (sameword(m->m_name, *tv)) 7893149Seric break; 7903149Seric } 7913149Seric if (m == NULL) 7924279Seric { 7933149Seric syserr("buildaddr: unknown net %s", *tv); 7944279Seric return (NULL); 7954279Seric } 7964598Seric a->q_mailer = m; 7973149Seric 7983149Seric /* figure out what host (if any) */ 7993149Seric tv++; 80010690Seric if (!bitnset(M_LOCAL, m->m_flags)) 8013149Seric { 8025704Seric if (**tv++ != CANONHOST) 8034279Seric { 8043149Seric syserr("buildaddr: no host"); 8054279Seric return (NULL); 8064279Seric } 8075704Seric buf[0] = '\0'; 8085704Seric while (*tv != NULL && **tv != CANONUSER) 8097005Seric (void) strcat(buf, *tv++); 8105704Seric a->q_host = newstr(buf); 8113149Seric } 8123149Seric else 8133149Seric a->q_host = NULL; 8143149Seric 8153149Seric /* figure out the user */ 8163149Seric if (**tv != CANONUSER) 8174279Seric { 8183149Seric syserr("buildaddr: no user"); 8194279Seric return (NULL); 8204279Seric } 82111278Seric rewrite(++tv, 4); 82211278Seric cataddr(tv, buf, sizeof buf); 8233149Seric a->q_user = buf; 8243149Seric 8253149Seric return (a); 8263149Seric } 8273188Seric /* 8284228Seric ** CATADDR -- concatenate pieces of addresses (putting in <LWSP> subs) 8294228Seric ** 8304228Seric ** Parameters: 8314228Seric ** pvp -- parameter vector to rebuild. 8324228Seric ** buf -- buffer to build the string into. 8334228Seric ** sz -- size of buf. 8344228Seric ** 8354228Seric ** Returns: 8364228Seric ** none. 8374228Seric ** 8384228Seric ** Side Effects: 8394228Seric ** Destroys buf. 8404228Seric */ 8414228Seric 8424228Seric cataddr(pvp, buf, sz) 8434228Seric char **pvp; 8444228Seric char *buf; 8454228Seric register int sz; 8464228Seric { 8474228Seric bool oatomtok = FALSE; 8484228Seric bool natomtok = FALSE; 8494228Seric register int i; 8504228Seric register char *p; 8514228Seric 8528423Seric if (pvp == NULL) 8538423Seric { 8548423Seric strcpy(buf, ""); 8558423Seric return; 8568423Seric } 8574228Seric p = buf; 85811156Seric sz -= 2; 8594228Seric while (*pvp != NULL && (i = strlen(*pvp)) < sz) 8604228Seric { 8618078Seric natomtok = (toktype(**pvp) == ATM); 8624228Seric if (oatomtok && natomtok) 8639042Seric *p++ = SpaceSub; 8644228Seric (void) strcpy(p, *pvp); 8654228Seric oatomtok = natomtok; 8664228Seric p += i; 86711156Seric sz -= i + 1; 8684228Seric pvp++; 8694228Seric } 8704228Seric *p = '\0'; 8714228Seric } 8724228Seric /* 8733188Seric ** SAMEADDR -- Determine if two addresses are the same 8743188Seric ** 8753188Seric ** This is not just a straight comparison -- if the mailer doesn't 8763188Seric ** care about the host we just ignore it, etc. 8773188Seric ** 8783188Seric ** Parameters: 8793188Seric ** a, b -- pointers to the internal forms to compare. 8803188Seric ** 8813188Seric ** Returns: 8823188Seric ** TRUE -- they represent the same mailbox. 8833188Seric ** FALSE -- they don't. 8843188Seric ** 8853188Seric ** Side Effects: 8863188Seric ** none. 8873188Seric */ 8883188Seric 8893188Seric bool 8909374Seric sameaddr(a, b) 8913188Seric register ADDRESS *a; 8923188Seric register ADDRESS *b; 8933188Seric { 8943188Seric /* if they don't have the same mailer, forget it */ 8953188Seric if (a->q_mailer != b->q_mailer) 8963188Seric return (FALSE); 8973188Seric 8983188Seric /* if the user isn't the same, we can drop out */ 8999374Seric if (strcmp(a->q_user, b->q_user) != 0) 9003188Seric return (FALSE); 9013188Seric 9023188Seric /* if the mailer ignores hosts, we have succeeded! */ 90310690Seric if (bitnset(M_LOCAL, a->q_mailer->m_flags)) 9043188Seric return (TRUE); 9053188Seric 9063188Seric /* otherwise compare hosts (but be careful for NULL ptrs) */ 9073188Seric if (a->q_host == NULL || b->q_host == NULL) 9083188Seric return (FALSE); 9093188Seric if (strcmp(a->q_host, b->q_host) != 0) 9103188Seric return (FALSE); 9113188Seric 9123188Seric return (TRUE); 9133188Seric } 9143234Seric /* 9153234Seric ** PRINTADDR -- print address (for debugging) 9163234Seric ** 9173234Seric ** Parameters: 9183234Seric ** a -- the address to print 9193234Seric ** follow -- follow the q_next chain. 9203234Seric ** 9213234Seric ** Returns: 9223234Seric ** none. 9233234Seric ** 9243234Seric ** Side Effects: 9253234Seric ** none. 9263234Seric */ 9273234Seric 9284317Seric # ifdef DEBUG 9294317Seric 9303234Seric printaddr(a, follow) 9313234Seric register ADDRESS *a; 9323234Seric bool follow; 9333234Seric { 9345001Seric bool first = TRUE; 9355001Seric 9363234Seric while (a != NULL) 9373234Seric { 9385001Seric first = FALSE; 9394443Seric printf("%x=", a); 9404085Seric (void) fflush(stdout); 9413234Seric printf("%s: mailer %d (%s), host `%s', user `%s'\n", a->q_paddr, 9428181Seric a->q_mailer->m_mno, a->q_mailer->m_name, a->q_host, 9438181Seric a->q_user); 9448181Seric printf("\tnext=%x, flags=%o, alias %x\n", a->q_next, a->q_flags, 9458181Seric a->q_alias); 9468181Seric printf("\thome=\"%s\", fullname=\"%s\"\n", a->q_home, 9478181Seric a->q_fullname); 9484996Seric 9493234Seric if (!follow) 9503234Seric return; 9514996Seric a = a->q_next; 9523234Seric } 9535001Seric if (first) 9544443Seric printf("[NULL]\n"); 9553234Seric } 9564317Seric 9574317Seric # endif DEBUG 9587682Seric /* 9597682Seric ** REMOTENAME -- return the name relative to the current mailer 9607682Seric ** 9617682Seric ** Parameters: 9627682Seric ** name -- the name to translate. 9638069Seric ** m -- the mailer that we want to do rewriting relative 9648069Seric ** to. 9658069Seric ** senderaddress -- if set, uses the sender rewriting rules 9668069Seric ** rather than the recipient rewriting rules. 96710310Seric ** canonical -- if set, strip out any comment information, 96810310Seric ** etc. 9697682Seric ** 9707682Seric ** Returns: 9717682Seric ** the text string representing this address relative to 9727682Seric ** the receiving mailer. 9737682Seric ** 9747682Seric ** Side Effects: 9757682Seric ** none. 9767682Seric ** 9777682Seric ** Warnings: 9787682Seric ** The text string returned is tucked away locally; 9797682Seric ** copy it if you intend to save it. 9807682Seric */ 9817682Seric 9827682Seric char * 98310310Seric remotename(name, m, senderaddress, canonical) 9847682Seric char *name; 9857682Seric struct mailer *m; 9868069Seric bool senderaddress; 98710310Seric bool canonical; 9887682Seric { 9898069Seric register char **pvp; 9908069Seric char *fancy; 99115284Seric register char *p; 9928069Seric extern char *macvalue(); 9938181Seric char *oldg = macvalue('g', CurEnv); 9947682Seric static char buf[MAXNAME]; 9957682Seric char lbuf[MAXNAME]; 9967682Seric extern char **prescan(); 9977889Seric extern char *crackaddr(); 9987682Seric 9997755Seric # ifdef DEBUG 10007755Seric if (tTd(12, 1)) 10017755Seric printf("remotename(%s)\n", name); 10027755Seric # endif DEBUG 10037755Seric 100410177Seric /* don't do anything if we are tagging it as special */ 100510177Seric if ((senderaddress ? m->m_s_rwset : m->m_r_rwset) < 0) 100610177Seric return (name); 100710177Seric 10087682Seric /* 10098181Seric ** Do a heuristic crack of this name to extract any comment info. 10108181Seric ** This will leave the name as a comment and a $g macro. 10117889Seric */ 10127889Seric 101310310Seric if (canonical) 101416155Seric fancy = "\001g"; 101510310Seric else 101610310Seric fancy = crackaddr(name); 10177889Seric 10188181Seric /* 10198181Seric ** Turn the name into canonical form. 10208181Seric ** Normally this will be RFC 822 style, i.e., "user@domain". 10218181Seric ** If this only resolves to "user", and the "C" flag is 10228181Seric ** specified in the sending mailer, then the sender's 10238181Seric ** domain will be appended. 10248181Seric */ 10258181Seric 10267889Seric pvp = prescan(name, '\0'); 10277889Seric if (pvp == NULL) 10287889Seric return (name); 10298181Seric rewrite(pvp, 3); 10308181Seric if (CurEnv->e_fromdomain != NULL) 10318181Seric { 10328181Seric /* append from domain to this address */ 10338181Seric register char **pxp = pvp; 10348181Seric 10359594Seric /* see if there is an "@domain" in the current name */ 10368181Seric while (*pxp != NULL && strcmp(*pxp, "@") != 0) 10378181Seric pxp++; 10388181Seric if (*pxp == NULL) 10398181Seric { 10409594Seric /* no.... append the "@domain" from the sender */ 10418181Seric register char **qxq = CurEnv->e_fromdomain; 10428181Seric 10439594Seric while ((*pxp++ = *qxq++) != NULL) 10449594Seric continue; 104511726Seric rewrite(pvp, 3); 10468181Seric } 10478181Seric } 10488181Seric 10498181Seric /* 10508959Seric ** Do more specific rewriting. 10518181Seric ** Rewrite using ruleset 1 or 2 depending on whether this is 10528181Seric ** a sender address or not. 10538181Seric ** Then run it through any receiving-mailer-specific rulesets. 10548181Seric */ 10558181Seric 10568069Seric if (senderaddress) 10577755Seric { 10587889Seric rewrite(pvp, 1); 10598069Seric if (m->m_s_rwset > 0) 10608069Seric rewrite(pvp, m->m_s_rwset); 10618069Seric } 10628069Seric else 10638069Seric { 10647889Seric rewrite(pvp, 2); 10658069Seric if (m->m_r_rwset > 0) 10668069Seric rewrite(pvp, m->m_r_rwset); 10677682Seric } 10687682Seric 10698181Seric /* 10708959Seric ** Do any final sanitation the address may require. 10718959Seric ** This will normally be used to turn internal forms 10728959Seric ** (e.g., user@host.LOCAL) into external form. This 10738959Seric ** may be used as a default to the above rules. 10748959Seric */ 10758959Seric 10768959Seric rewrite(pvp, 4); 10778959Seric 10788959Seric /* 10798181Seric ** Now restore the comment information we had at the beginning. 10808181Seric */ 10818181Seric 10827682Seric cataddr(pvp, lbuf, sizeof lbuf); 10839374Seric define('g', lbuf, CurEnv); 10847889Seric expand(fancy, buf, &buf[sizeof buf - 1], CurEnv); 10859374Seric define('g', oldg, CurEnv); 10867682Seric 10877682Seric # ifdef DEBUG 10887682Seric if (tTd(12, 1)) 10897755Seric printf("remotename => `%s'\n", buf); 10907682Seric # endif DEBUG 10917682Seric return (buf); 10927682Seric } 1093