13312Seric # include "sendmail.h" 2297Seric 3*8423Seric SCCSID(@(#)parseaddr.c 3.60 10/09/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 658078Seric pvp = prescan(addr, ','); 663149Seric if (pvp == NULL) 67297Seric return (NULL); 68297Seric 69297Seric /* 703149Seric ** Apply rewriting rules. 717889Seric ** Ruleset 0 does basic parsing. It must resolve. 72297Seric */ 73297Seric 748181Seric rewrite(pvp, 3); 754070Seric rewrite(pvp, 0); 76297Seric 773149Seric /* 783149Seric ** See if we resolved to a real mailer. 793149Seric */ 80297Seric 813149Seric if (pvp[0][0] != CANONNET) 823149Seric { 833149Seric setstat(EX_USAGE); 843149Seric usrerr("cannot resolve name"); 853149Seric return (NULL); 86297Seric } 87297Seric 88297Seric /* 893149Seric ** Build canonical address from pvp. 90297Seric */ 91297Seric 923149Seric a = buildaddr(pvp, a); 934279Seric if (a == NULL) 944279Seric return (NULL); 954598Seric m = a->q_mailer; 96297Seric 97297Seric /* 983149Seric ** Make local copies of the host & user and then 993149Seric ** transport them out. 100297Seric */ 101297Seric 102297Seric if (copyf > 0) 1038078Seric { 1048078Seric extern char *DelimChar; 1058078Seric char savec = *DelimChar; 1068078Seric 1078078Seric *DelimChar = '\0'; 1082973Seric a->q_paddr = newstr(addr); 1098078Seric *DelimChar = savec; 1108078Seric } 111297Seric else 112297Seric a->q_paddr = addr; 1133149Seric if (copyf >= 0) 114297Seric { 1153149Seric if (a->q_host != NULL) 1163149Seric a->q_host = newstr(a->q_host); 117297Seric else 1183149Seric a->q_host = ""; 1193149Seric if (a->q_user != a->q_paddr) 1203149Seric a->q_user = newstr(a->q_user); 121297Seric } 122297Seric 123297Seric /* 124297Seric ** Do UPPER->lower case mapping unless inhibited. 125297Seric */ 126297Seric 1273149Seric if (!bitset(M_HST_UPPER, m->m_flags)) 128297Seric makelower(a->q_host); 1293149Seric if (!bitset(M_USR_UPPER, m->m_flags)) 130297Seric makelower(a->q_user); 131297Seric 132297Seric /* 133297Seric ** Compute return value. 134297Seric */ 135297Seric 136297Seric # ifdef DEBUG 1377675Seric if (tTd(20, 1)) 1384443Seric { 1394443Seric printf("parse-->"); 1404443Seric printaddr(a, FALSE); 1414443Seric } 142297Seric # endif DEBUG 143297Seric 144297Seric return (a); 145297Seric } 146297Seric /* 147297Seric ** PRESCAN -- Prescan name and make it canonical 148297Seric ** 149297Seric ** Scans a name and turns it into canonical form. This involves 150297Seric ** deleting blanks, comments (in parentheses), and turning the 151297Seric ** word "at" into an at-sign ("@"). The name is copied as this 152297Seric ** is done; it is legal to copy a name onto itself, since this 153297Seric ** process can only make things smaller. 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. 169297Seric ** 170297Seric ** Returns: 1713149Seric ** A pointer to a vector of tokens. 172297Seric ** NULL on error. 173297Seric ** 174297Seric ** Side Effects: 1753149Seric ** none. 176297Seric */ 177297Seric 1788078Seric /* states and character types */ 1798078Seric # define OPR 0 /* operator */ 1808078Seric # define ATM 1 /* atom */ 1818078Seric # define QST 2 /* in quoted string */ 1828078Seric # define SPC 3 /* chewing up spaces */ 1838078Seric # define ONE 4 /* pick up one character */ 1843149Seric 1858078Seric # define NSTATES 5 /* number of states */ 1868078Seric # define TYPE 017 /* mask to select state type */ 1878078Seric 1888078Seric /* meta bits for table */ 1898078Seric # define M 020 /* meta character; don't pass through */ 1908078Seric # define B 040 /* cause a break */ 1918078Seric # define MB M|B /* meta-break */ 1928078Seric 1938078Seric static short StateTab[NSTATES][NSTATES] = 1948078Seric { 1958087Seric /* oldst chtype> OPR ATM QST SPC ONE */ 1968078Seric /*OPR*/ OPR|B, ATM|B, QST|MB, SPC|MB, ONE|B, 1978078Seric /*ATM*/ OPR|B, ATM, QST|MB, SPC|MB, ONE|B, 1988087Seric /*QST*/ QST, QST, OPR|MB, QST, QST, 1998078Seric /*SPC*/ OPR, ATM, QST, SPC|M, ONE, 2008078Seric /*ONE*/ OPR, OPR, OPR, OPR, OPR, 2018078Seric }; 2028078Seric 2038078Seric # define NOCHAR -1 /* signal nothing in lookahead token */ 2048078Seric 2058078Seric char *DelimChar; /* set to point to the delimiter */ 2068078Seric 2073149Seric char ** 2083149Seric prescan(addr, delim) 209297Seric char *addr; 210297Seric char delim; 211297Seric { 212297Seric register char *p; 2138078Seric register char *q; 2148078Seric register char c; 2153149Seric char **avp; 216297Seric bool bslashmode; 217297Seric int cmntcnt; 218*8423Seric int anglecnt; 2193149Seric char *tok; 2208078Seric int state; 2218078Seric int newstate; 2228078Seric static char buf[MAXNAME+MAXATOM]; 2238078Seric static char *av[MAXATOM+1]; 224297Seric 225297Seric q = buf; 2263149Seric bslashmode = FALSE; 2277800Seric cmntcnt = 0; 228*8423Seric anglecnt = 0; 2293149Seric avp = av; 2308078Seric state = OPR; 2318078Seric c = NOCHAR; 2328078Seric p = addr; 2338078Seric # ifdef DEBUG 2348078Seric if (tTd(22, 45)) 235297Seric { 2368078Seric printf("prescan: "); 2378078Seric xputs(p); 2388078Seric putchar('\n'); 2398078Seric } 2408078Seric # endif DEBUG 2418078Seric 2428078Seric do 2438078Seric { 2443149Seric /* read a token */ 2453149Seric tok = q; 2468078Seric for (;;) 247297Seric { 2488078Seric /* store away any old lookahead character */ 2498078Seric if (c != NOCHAR) 2508078Seric { 2518078Seric /* squirrel it away */ 2528078Seric if (q >= &buf[sizeof buf - 5]) 2538078Seric { 2548078Seric usrerr("Address too long"); 2558078Seric DelimChar = p; 2568078Seric return (NULL); 2578078Seric } 2588078Seric *q++ = c; 2598078Seric } 2608078Seric 2618078Seric /* read a new input character */ 2628078Seric c = *p++; 2638078Seric if (c == '\0') 2648078Seric break; 2658078Seric # ifdef DEBUG 2668078Seric if (tTd(22, 101)) 2678078Seric printf("c=%c, s=%d; ", c, state); 2688078Seric # endif DEBUG 2698078Seric 2703149Seric /* chew up special characters */ 2714100Seric c &= ~0200; 2723149Seric *q = '\0'; 2733149Seric if (bslashmode) 2743149Seric { 2753149Seric c |= 0200; 2763149Seric bslashmode = FALSE; 2773149Seric } 2783149Seric else if (c == '\\') 2793149Seric { 2803149Seric bslashmode = TRUE; 2818078Seric c = NOCHAR; 2823149Seric } 2838078Seric else if (c == '(') 2844100Seric { 2858078Seric cmntcnt++; 2868078Seric c = NOCHAR; 2874100Seric } 2888078Seric else if (c == ')') 2893149Seric { 2908078Seric if (cmntcnt <= 0) 2913149Seric { 2928078Seric usrerr("Unbalanced ')'"); 2938078Seric DelimChar = p; 2948078Seric return (NULL); 2953149Seric } 2968078Seric else 2978078Seric cmntcnt--; 2988078Seric } 2998078Seric else if (cmntcnt > 0) 3008078Seric c = NOCHAR; 301*8423Seric else if (c == '<') 302*8423Seric anglecnt++; 303*8423Seric else if (c == '>') 304*8423Seric { 305*8423Seric if (anglecnt <= 0) 306*8423Seric { 307*8423Seric usrerr("Unbalanced '>'"); 308*8423Seric DelimChar = p; 309*8423Seric return (NULL); 310*8423Seric } 311*8423Seric anglecnt--; 312*8423Seric } 3133149Seric 3148078Seric if (c == NOCHAR) 3158078Seric continue; 3163149Seric 3178078Seric /* see if this is end of input */ 318*8423Seric if (c == delim && anglecnt <= 0) 3193149Seric break; 3203149Seric 3218078Seric newstate = StateTab[state][toktype(c)]; 3228078Seric # ifdef DEBUG 3238078Seric if (tTd(22, 101)) 3248078Seric printf("ns=%02o\n", newstate); 3258078Seric # endif DEBUG 3268078Seric state = newstate & TYPE; 3278078Seric if (bitset(M, newstate)) 3288078Seric c = NOCHAR; 3298078Seric if (bitset(B, newstate)) 3304228Seric break; 331297Seric } 3323149Seric 3333149Seric /* new token */ 3348078Seric if (tok != q) 3351378Seric { 3368078Seric *q++ = '\0'; 3378078Seric # ifdef DEBUG 3388078Seric if (tTd(22, 36)) 339297Seric { 3408078Seric printf("tok="); 3418078Seric xputs(tok); 3428078Seric putchar('\n'); 343297Seric } 3448078Seric # endif DEBUG 3458078Seric if (avp >= &av[MAXATOM]) 346297Seric { 3478078Seric syserr("prescan: too many tokens"); 3488078Seric DelimChar = p; 3498078Seric return (NULL); 350297Seric } 3518078Seric *avp++ = tok; 352297Seric } 353*8423Seric } while (c != '\0' && (c != delim || anglecnt > 0)); 3543149Seric *avp = NULL; 3558078Seric DelimChar = --p; 3563149Seric if (cmntcnt > 0) 3573149Seric usrerr("Unbalanced '('"); 358*8423Seric else if (anglecnt > 0) 359*8423Seric usrerr("Unbalanced '<'"); 3608078Seric else if (state == QST) 3613149Seric usrerr("Unbalanced '\"'"); 3623149Seric else if (av[0] != NULL) 3633149Seric return (av); 3643149Seric return (NULL); 3653149Seric } 3663149Seric /* 3673149Seric ** TOKTYPE -- return token type 3683149Seric ** 3693149Seric ** Parameters: 3703149Seric ** c -- the character in question. 3713149Seric ** 3723149Seric ** Returns: 3733149Seric ** Its type. 3743149Seric ** 3753149Seric ** Side Effects: 3763149Seric ** none. 3773149Seric */ 378297Seric 3793149Seric toktype(c) 3803149Seric register char c; 3813149Seric { 3823380Seric static char buf[50]; 3833382Seric static bool firstime = TRUE; 3843380Seric 3853382Seric if (firstime) 3863380Seric { 3873382Seric firstime = FALSE; 3886977Seric expand("$o", buf, &buf[sizeof buf - 1], CurEnv); 3897005Seric (void) strcat(buf, DELIMCHARS); 3903380Seric } 3916053Seric if (c == MATCHCLASS || c == MATCHREPL) 3928078Seric return (ONE); 3938078Seric if (c == '"') 3948078Seric return (QST); 3954100Seric if (!isascii(c)) 3968078Seric return (ATM); 3978078Seric if (isspace(c) || c == ')') 3988078Seric return (SPC); 3993380Seric if (iscntrl(c) || index(buf, c) != NULL) 4008078Seric return (OPR); 4018078Seric return (ATM); 4023149Seric } 4033149Seric /* 4043149Seric ** REWRITE -- apply rewrite rules to token vector. 4053149Seric ** 4064476Seric ** This routine is an ordered production system. Each rewrite 4074476Seric ** rule has a LHS (called the pattern) and a RHS (called the 4084476Seric ** rewrite); 'rwr' points the the current rewrite rule. 4094476Seric ** 4104476Seric ** For each rewrite rule, 'avp' points the address vector we 4114476Seric ** are trying to match against, and 'pvp' points to the pattern. 4128058Seric ** If pvp points to a special match value (MATCHZANY, MATCHANY, 4138058Seric ** MATCHONE, MATCHCLASS) then the address in avp matched is 4148058Seric ** saved away in the match vector (pointed to by 'mvp'). 4154476Seric ** 4164476Seric ** When a match between avp & pvp does not match, we try to 4174476Seric ** back out. If we back up over a MATCHONE or a MATCHCLASS 4184476Seric ** we must also back out the match in mvp. If we reach a 4198058Seric ** MATCHANY or MATCHZANY we just extend the match and start 4208058Seric ** over again. 4214476Seric ** 4224476Seric ** When we finally match, we rewrite the address vector 4234476Seric ** and try over again. 4244476Seric ** 4253149Seric ** Parameters: 4263149Seric ** pvp -- pointer to token vector. 4273149Seric ** 4283149Seric ** Returns: 4293149Seric ** none. 4303149Seric ** 4313149Seric ** Side Effects: 4323149Seric ** pvp is modified. 4333149Seric */ 4342091Seric 4353149Seric struct match 4363149Seric { 4374468Seric char **first; /* first token matched */ 4384468Seric char **last; /* last token matched */ 4393149Seric }; 4403149Seric 4414468Seric # define MAXMATCH 9 /* max params per rewrite */ 4423149Seric 4433149Seric 4444070Seric rewrite(pvp, ruleset) 4453149Seric char **pvp; 4464070Seric int ruleset; 4473149Seric { 4483149Seric register char *ap; /* address pointer */ 4493149Seric register char *rp; /* rewrite pointer */ 4503149Seric register char **avp; /* address vector pointer */ 4513149Seric register char **rvp; /* rewrite vector pointer */ 4528058Seric register struct match *mlp; /* cur ptr into mlist */ 4538058Seric register struct rewrite *rwr; /* pointer to current rewrite rule */ 4544468Seric struct match mlist[MAXMATCH]; /* stores match on LHS */ 4553149Seric char *npvp[MAXATOM+1]; /* temporary space for rebuild */ 4564060Seric extern bool sameword(); 4573149Seric 4588335Seric if (Mode == MD_TEST || tTd(21, 2)) 4593149Seric { 4608069Seric printf("rewrite: ruleset %d, original pvp:", ruleset); 4613149Seric printav(pvp); 4623149Seric } 463*8423Seric if (pvp == NULL) 464*8423Seric return; 4653149Seric 4663149Seric /* 4673149Seric ** Run through the list of rewrite rules, applying 4683149Seric ** any that match. 4693149Seric */ 4703149Seric 4714070Seric for (rwr = RewriteRules[ruleset]; rwr != NULL; ) 4723149Seric { 4734100Seric # ifdef DEBUG 4747675Seric if (tTd(21, 12)) 475297Seric { 4768069Seric printf("-----trying rule:"); 4773149Seric printav(rwr->r_lhs); 4783149Seric } 4794100Seric # endif DEBUG 4803149Seric 4813149Seric /* try to match on this rule */ 4824468Seric mlp = mlist; 4838058Seric rvp = rwr->r_lhs; 4848058Seric avp = pvp; 4858058Seric while ((ap = *avp) != NULL || *rvp != NULL) 4863149Seric { 4873149Seric rp = *rvp; 4888058Seric # ifdef DEBUG 4898058Seric if (tTd(21, 35)) 4908058Seric { 4918069Seric printf("ap="); 4928058Seric xputs(ap); 4938069Seric printf(", rp="); 4948058Seric xputs(rp); 4958069Seric printf("\n"); 4968058Seric } 4978058Seric # endif DEBUG 4983149Seric if (rp == NULL) 499297Seric { 5003149Seric /* end-of-pattern before end-of-address */ 5018058Seric goto backup; 502297Seric } 5038058Seric if (ap == NULL && *rp != MATCHZANY) 5048058Seric { 5058058Seric /* end-of-input */ 5068058Seric break; 5078058Seric } 5083149Seric 5093149Seric switch (*rp) 5103149Seric { 5114060Seric register STAB *s; 5124060Seric register int class; 5134060Seric 5144060Seric case MATCHCLASS: 5154060Seric /* match any token in a class */ 5164060Seric class = rp[1]; 5174060Seric if (!isalpha(class)) 5188058Seric goto backup; 5194060Seric if (isupper(class)) 5204060Seric class -= 'A'; 5214060Seric else 5224060Seric class -= 'a'; 5234100Seric s = stab(ap, ST_CLASS, ST_FIND); 5246273Seric if (s == NULL || (s->s_class & (1L << class)) == 0) 5258058Seric goto backup; 5264468Seric 5274476Seric /* explicit fall-through */ 5284476Seric 5294476Seric case MATCHONE: 5304476Seric case MATCHANY: 5314476Seric /* match exactly one token */ 5328058Seric mlp->first = avp; 5338058Seric mlp->last = avp++; 5344468Seric mlp++; 5354060Seric break; 5364060Seric 5378058Seric case MATCHZANY: 5388058Seric /* match zero or more tokens */ 5398058Seric mlp->first = avp; 5408058Seric mlp->last = avp - 1; 5418058Seric mlp++; 5428058Seric break; 5438058Seric 5443149Seric default: 5453149Seric /* must have exact match */ 5464060Seric if (!sameword(rp, ap)) 5478058Seric goto backup; 5484468Seric avp++; 5493149Seric break; 5503149Seric } 5513149Seric 5523149Seric /* successful match on this token */ 5533149Seric rvp++; 5543149Seric continue; 5553149Seric 5568058Seric backup: 5573149Seric /* match failed -- back up */ 5583149Seric while (--rvp >= rwr->r_lhs) 5593149Seric { 5603149Seric rp = *rvp; 5618058Seric if (*rp == MATCHANY || *rp == MATCHZANY) 5624468Seric { 5634476Seric /* extend binding and continue */ 5648058Seric avp = ++mlp[-1].last; 5658058Seric avp++; 5664476Seric rvp++; 5673149Seric break; 5684468Seric } 5694476Seric avp--; 5704476Seric if (*rp == MATCHONE || *rp == MATCHCLASS) 5713149Seric { 5724468Seric /* back out binding */ 5734468Seric mlp--; 5743149Seric } 5753149Seric } 5763149Seric 5773149Seric if (rvp < rwr->r_lhs) 5783149Seric { 5793149Seric /* total failure to match */ 5803149Seric break; 5813149Seric } 582297Seric } 5833149Seric 5843149Seric /* 5853149Seric ** See if we successfully matched 5863149Seric */ 5873149Seric 5883149Seric if (rvp >= rwr->r_lhs && *rvp == NULL) 5893149Seric { 5908058Seric rvp = rwr->r_rhs; 5914100Seric # ifdef DEBUG 5927675Seric if (tTd(21, 12)) 5933149Seric { 5948069Seric printf("-----rule matches:"); 5958058Seric printav(rvp); 5963149Seric } 5974100Seric # endif DEBUG 5983149Seric 5998058Seric rp = *rvp; 6008226Seric if (*rp == CANONUSER) 6018058Seric { 6028069Seric rvp++; 6038069Seric rwr = rwr->r_next; 6048069Seric } 6058069Seric else if (*rp == CANONHOST) 6068069Seric { 6078069Seric rvp++; 6088069Seric rwr = NULL; 6098069Seric } 6108069Seric else if (*rp == CANONNET) 6118069Seric rwr = NULL; 6128058Seric 6133149Seric /* substitute */ 6148069Seric for (avp = npvp; *rvp != NULL; rvp++) 6153149Seric { 6163149Seric rp = *rvp; 6174468Seric if (*rp == MATCHREPL) 6183149Seric { 6193149Seric register struct match *m; 6203149Seric register char **pp; 6213149Seric 6224468Seric m = &mlist[rp[1] - '1']; 6234476Seric # ifdef DEBUG 6247675Seric if (tTd(21, 15)) 6254476Seric { 6264476Seric printf("$%c:", rp[1]); 6274476Seric pp = m->first; 6288058Seric while (pp <= m->last) 6294476Seric { 6304476Seric printf(" %x=\"", *pp); 6314625Seric (void) fflush(stdout); 6328058Seric printf("%s\"", *pp++); 6338058Seric } 6344476Seric printf("\n"); 6354476Seric } 6364476Seric # endif DEBUG 6374468Seric pp = m->first; 6388058Seric while (pp <= m->last) 6393149Seric { 6404468Seric if (avp >= &npvp[MAXATOM]) 6413149Seric { 6424468Seric syserr("rewrite: expansion too long"); 6434468Seric return; 6444468Seric } 6458058Seric *avp++ = *pp++; 6468058Seric } 6473149Seric } 6483149Seric else 6494385Seric { 6504385Seric if (avp >= &npvp[MAXATOM]) 6514385Seric { 6524385Seric syserr("rewrite: expansion too long"); 6534385Seric return; 6544385Seric } 6553149Seric *avp++ = rp; 6564385Seric } 6573149Seric } 6583149Seric *avp++ = NULL; 6598226Seric if (**npvp == CALLSUBR) 6608226Seric { 6618226Seric bmove((char *) &npvp[2], (char *) pvp, 6628226Seric (avp - npvp - 2) * sizeof *avp); 6633149Seric # ifdef DEBUG 6648226Seric if (tTd(21, 3)) 6658226Seric printf("-----callsubr %s\n", npvp[1]); 6668226Seric # endif DEBUG 6678226Seric rewrite(pvp, atoi(npvp[1])); 6688226Seric } 6698226Seric else 6708226Seric { 6718226Seric bmove((char *) npvp, (char *) pvp, 6728226Seric (avp - npvp) * sizeof *avp); 6738226Seric } 6748226Seric # ifdef DEBUG 6757675Seric if (tTd(21, 4)) 6763149Seric { 6778069Seric printf("rewritten as:"); 6788069Seric printav(pvp); 6793149Seric } 6803149Seric # endif DEBUG 6813149Seric } 6823149Seric else 6833149Seric { 6844100Seric # ifdef DEBUG 6857675Seric if (tTd(21, 10)) 6863149Seric printf("----- rule fails\n"); 6874100Seric # endif DEBUG 6883149Seric rwr = rwr->r_next; 6893149Seric } 690297Seric } 6918069Seric 6928335Seric if (Mode == MD_TEST || tTd(21, 2)) 6938069Seric { 6948069Seric printf("rewrite: ruleset %d returns:", ruleset); 6958069Seric printav(pvp); 6968069Seric } 6973149Seric } 6983149Seric /* 6993149Seric ** BUILDADDR -- build address from token vector. 7003149Seric ** 7013149Seric ** Parameters: 7023149Seric ** tv -- token vector. 7033149Seric ** a -- pointer to address descriptor to fill. 7043149Seric ** If NULL, one will be allocated. 7053149Seric ** 7063149Seric ** Returns: 7074279Seric ** NULL if there was an error. 7084279Seric ** 'a' otherwise. 7093149Seric ** 7103149Seric ** Side Effects: 7113149Seric ** fills in 'a' 7123149Seric */ 7133149Seric 7143149Seric ADDRESS * 7153149Seric buildaddr(tv, a) 7163149Seric register char **tv; 7173149Seric register ADDRESS *a; 7183149Seric { 7193149Seric static char buf[MAXNAME]; 7203149Seric struct mailer **mp; 7213149Seric register struct mailer *m; 7224635Seric extern bool sameword(); 7233149Seric 7243149Seric if (a == NULL) 7253149Seric a = (ADDRESS *) xalloc(sizeof *a); 7264988Seric clear((char *) a, sizeof *a); 7273149Seric 7283149Seric /* figure out what net/mailer to use */ 7293149Seric if (**tv != CANONNET) 7304279Seric { 7313149Seric syserr("buildaddr: no net"); 7324279Seric return (NULL); 7334279Seric } 7343149Seric tv++; 7354635Seric if (sameword(*tv, "error")) 7364279Seric { 7374279Seric if (**++tv != CANONUSER) 7384279Seric syserr("buildaddr: error: no user"); 7394279Seric buf[0] = '\0'; 7404279Seric while (*++tv != NULL) 7414279Seric { 7424279Seric if (buf[0] != '\0') 7437005Seric (void) strcat(buf, " "); 7447005Seric (void) strcat(buf, *tv); 7454279Seric } 7464279Seric usrerr(buf); 7474279Seric return (NULL); 7484279Seric } 7494598Seric for (mp = Mailer; (m = *mp++) != NULL; ) 7503149Seric { 7514635Seric if (sameword(m->m_name, *tv)) 7523149Seric break; 7533149Seric } 7543149Seric if (m == NULL) 7554279Seric { 7563149Seric syserr("buildaddr: unknown net %s", *tv); 7574279Seric return (NULL); 7584279Seric } 7594598Seric a->q_mailer = m; 7603149Seric 7613149Seric /* figure out what host (if any) */ 7623149Seric tv++; 7634195Seric if (!bitset(M_LOCAL, m->m_flags)) 7643149Seric { 7655704Seric if (**tv++ != CANONHOST) 7664279Seric { 7673149Seric syserr("buildaddr: no host"); 7684279Seric return (NULL); 7694279Seric } 7705704Seric buf[0] = '\0'; 7715704Seric while (*tv != NULL && **tv != CANONUSER) 7727005Seric (void) strcat(buf, *tv++); 7735704Seric a->q_host = newstr(buf); 7743149Seric } 7753149Seric else 7763149Seric a->q_host = NULL; 7773149Seric 7783149Seric /* figure out the user */ 7793149Seric if (**tv != CANONUSER) 7804279Seric { 7813149Seric syserr("buildaddr: no user"); 7824279Seric return (NULL); 7834279Seric } 7844228Seric cataddr(++tv, buf, sizeof buf); 7853149Seric a->q_user = buf; 7863149Seric 7873149Seric return (a); 7883149Seric } 7893188Seric /* 7904228Seric ** CATADDR -- concatenate pieces of addresses (putting in <LWSP> subs) 7914228Seric ** 7924228Seric ** Parameters: 7934228Seric ** pvp -- parameter vector to rebuild. 7944228Seric ** buf -- buffer to build the string into. 7954228Seric ** sz -- size of buf. 7964228Seric ** 7974228Seric ** Returns: 7984228Seric ** none. 7994228Seric ** 8004228Seric ** Side Effects: 8014228Seric ** Destroys buf. 8024228Seric */ 8034228Seric 8044228Seric cataddr(pvp, buf, sz) 8054228Seric char **pvp; 8064228Seric char *buf; 8074228Seric register int sz; 8084228Seric { 8094228Seric bool oatomtok = FALSE; 8104228Seric bool natomtok = FALSE; 8114228Seric register int i; 8124228Seric register char *p; 8134228Seric 814*8423Seric if (pvp == NULL) 815*8423Seric { 816*8423Seric strcpy(buf, ""); 817*8423Seric return; 818*8423Seric } 8194228Seric p = buf; 8204228Seric sz--; 8214228Seric while (*pvp != NULL && (i = strlen(*pvp)) < sz) 8224228Seric { 8238078Seric natomtok = (toktype(**pvp) == ATM); 8244228Seric if (oatomtok && natomtok) 8254228Seric *p++ = SPACESUB; 8264228Seric (void) strcpy(p, *pvp); 8274228Seric oatomtok = natomtok; 8284228Seric p += i; 8294228Seric sz -= i; 8304228Seric pvp++; 8314228Seric } 8324228Seric *p = '\0'; 8334228Seric } 8344228Seric /* 8353188Seric ** SAMEADDR -- Determine if two addresses are the same 8363188Seric ** 8373188Seric ** This is not just a straight comparison -- if the mailer doesn't 8383188Seric ** care about the host we just ignore it, etc. 8393188Seric ** 8403188Seric ** Parameters: 8413188Seric ** a, b -- pointers to the internal forms to compare. 8423188Seric ** wildflg -- if TRUE, 'a' may have no user specified, 8433188Seric ** in which case it is to match anything. 8443188Seric ** 8453188Seric ** Returns: 8463188Seric ** TRUE -- they represent the same mailbox. 8473188Seric ** FALSE -- they don't. 8483188Seric ** 8493188Seric ** Side Effects: 8503188Seric ** none. 8513188Seric */ 8523188Seric 8533188Seric bool 8543188Seric sameaddr(a, b, wildflg) 8553188Seric register ADDRESS *a; 8563188Seric register ADDRESS *b; 8573188Seric bool wildflg; 8583188Seric { 8593188Seric /* if they don't have the same mailer, forget it */ 8603188Seric if (a->q_mailer != b->q_mailer) 8613188Seric return (FALSE); 8623188Seric 8633188Seric /* if the user isn't the same, we can drop out */ 8643188Seric if ((!wildflg || a->q_user[0] != '\0') && strcmp(a->q_user, b->q_user) != 0) 8653188Seric return (FALSE); 8663188Seric 8673188Seric /* if the mailer ignores hosts, we have succeeded! */ 8684598Seric if (bitset(M_LOCAL, a->q_mailer->m_flags)) 8693188Seric return (TRUE); 8703188Seric 8713188Seric /* otherwise compare hosts (but be careful for NULL ptrs) */ 8723188Seric if (a->q_host == NULL || b->q_host == NULL) 8733188Seric return (FALSE); 8743188Seric if (strcmp(a->q_host, b->q_host) != 0) 8753188Seric return (FALSE); 8763188Seric 8773188Seric return (TRUE); 8783188Seric } 8793234Seric /* 8803234Seric ** PRINTADDR -- print address (for debugging) 8813234Seric ** 8823234Seric ** Parameters: 8833234Seric ** a -- the address to print 8843234Seric ** follow -- follow the q_next chain. 8853234Seric ** 8863234Seric ** Returns: 8873234Seric ** none. 8883234Seric ** 8893234Seric ** Side Effects: 8903234Seric ** none. 8913234Seric */ 8923234Seric 8934317Seric # ifdef DEBUG 8944317Seric 8953234Seric printaddr(a, follow) 8963234Seric register ADDRESS *a; 8973234Seric bool follow; 8983234Seric { 8995001Seric bool first = TRUE; 9005001Seric 9013234Seric while (a != NULL) 9023234Seric { 9035001Seric first = FALSE; 9044443Seric printf("%x=", a); 9054085Seric (void) fflush(stdout); 9063234Seric printf("%s: mailer %d (%s), host `%s', user `%s'\n", a->q_paddr, 9078181Seric a->q_mailer->m_mno, a->q_mailer->m_name, a->q_host, 9088181Seric a->q_user); 9098181Seric printf("\tnext=%x, flags=%o, alias %x\n", a->q_next, a->q_flags, 9108181Seric a->q_alias); 9118181Seric printf("\thome=\"%s\", fullname=\"%s\"\n", a->q_home, 9128181Seric a->q_fullname); 9134996Seric 9143234Seric if (!follow) 9153234Seric return; 9164996Seric a = a->q_next; 9173234Seric } 9185001Seric if (first) 9194443Seric printf("[NULL]\n"); 9203234Seric } 9214317Seric 9224317Seric # endif DEBUG 9237682Seric /* 9247682Seric ** REMOTENAME -- return the name relative to the current mailer 9257682Seric ** 9267682Seric ** Parameters: 9277682Seric ** name -- the name to translate. 9288069Seric ** m -- the mailer that we want to do rewriting relative 9298069Seric ** to. 9308069Seric ** senderaddress -- if set, uses the sender rewriting rules 9318069Seric ** rather than the recipient rewriting rules. 9327682Seric ** 9337682Seric ** Returns: 9347682Seric ** the text string representing this address relative to 9357682Seric ** the receiving mailer. 9367682Seric ** 9377682Seric ** Side Effects: 9387682Seric ** none. 9397682Seric ** 9407682Seric ** Warnings: 9417682Seric ** The text string returned is tucked away locally; 9427682Seric ** copy it if you intend to save it. 9437682Seric */ 9447682Seric 9457682Seric char * 9468069Seric remotename(name, m, senderaddress) 9477682Seric char *name; 9487682Seric struct mailer *m; 9498069Seric bool senderaddress; 9507682Seric { 9518069Seric register char **pvp; 9528069Seric char *fancy; 9538069Seric extern char *macvalue(); 9548181Seric char *oldg = macvalue('g', CurEnv); 9557682Seric static char buf[MAXNAME]; 9567682Seric char lbuf[MAXNAME]; 9577682Seric extern char **prescan(); 9587889Seric extern char *crackaddr(); 9597682Seric 9607755Seric # ifdef DEBUG 9617755Seric if (tTd(12, 1)) 9627755Seric printf("remotename(%s)\n", name); 9637755Seric # endif DEBUG 9647755Seric 9657682Seric /* 9668181Seric ** Do a heuristic crack of this name to extract any comment info. 9678181Seric ** This will leave the name as a comment and a $g macro. 9687889Seric */ 9697889Seric 9707889Seric fancy = crackaddr(name); 9717889Seric 9728181Seric /* 9738181Seric ** Turn the name into canonical form. 9748181Seric ** Normally this will be RFC 822 style, i.e., "user@domain". 9758181Seric ** If this only resolves to "user", and the "C" flag is 9768181Seric ** specified in the sending mailer, then the sender's 9778181Seric ** domain will be appended. 9788181Seric */ 9798181Seric 9807889Seric pvp = prescan(name, '\0'); 9817889Seric if (pvp == NULL) 9827889Seric return (name); 9838181Seric rewrite(pvp, 3); 9848181Seric if (CurEnv->e_fromdomain != NULL) 9858181Seric { 9868181Seric /* append from domain to this address */ 9878181Seric register char **pxp = pvp; 9888181Seric 9898181Seric while (*pxp != NULL && strcmp(*pxp, "@") != 0) 9908181Seric pxp++; 9918181Seric if (*pxp == NULL) 9928181Seric { 9938181Seric register char **qxq = CurEnv->e_fromdomain; 9948181Seric 9958181Seric while (*qxq != NULL) 9968181Seric *pxp++ = *qxq++; 9978181Seric } 9988181Seric } 9998181Seric 10008181Seric /* 10018181Seric ** Now do more specific rewriting. 10028181Seric ** Rewrite using ruleset 1 or 2 depending on whether this is 10038181Seric ** a sender address or not. 10048181Seric ** Then run it through any receiving-mailer-specific rulesets. 10058181Seric */ 10068181Seric 10078069Seric if (senderaddress) 10087755Seric { 10097889Seric rewrite(pvp, 1); 10108069Seric if (m->m_s_rwset > 0) 10118069Seric rewrite(pvp, m->m_s_rwset); 10128069Seric } 10138069Seric else 10148069Seric { 10157889Seric rewrite(pvp, 2); 10168069Seric if (m->m_r_rwset > 0) 10178069Seric rewrite(pvp, m->m_r_rwset); 10187682Seric } 10197682Seric 10208181Seric /* 10218181Seric ** Now restore the comment information we had at the beginning. 10228181Seric */ 10238181Seric 10247682Seric cataddr(pvp, lbuf, sizeof lbuf); 10257682Seric define('g', lbuf); 10267889Seric expand(fancy, buf, &buf[sizeof buf - 1], CurEnv); 10277682Seric define('g', oldg); 10287682Seric 10297682Seric # ifdef DEBUG 10307682Seric if (tTd(12, 1)) 10317755Seric printf("remotename => `%s'\n", buf); 10327682Seric # endif DEBUG 10337682Seric return (buf); 10347682Seric } 10357682Seric /* 10367682Seric ** CANONNAME -- make name canonical 10377682Seric ** 10387682Seric ** This is used for SMTP and misc. printing. Given a print 10398181Seric ** address, it strips out comments, etc. 10407682Seric ** 10417682Seric ** Parameters: 10427682Seric ** name -- the name to make canonical. 10438353Seric ** ruleset -- the canonicalizing ruleset. 10447682Seric ** 10457682Seric ** Returns: 10467682Seric ** pointer to canonical name. 10477682Seric ** 10487682Seric ** Side Effects: 10497682Seric ** none. 10507682Seric ** 10517682Seric ** Warning: 10527682Seric ** result is saved in static buf; future calls will trash it. 10537682Seric */ 10547682Seric 10557682Seric char * 10568353Seric canonname(name, ruleset) 10577682Seric char *name; 10588353Seric int ruleset; 10597682Seric { 10608069Seric static char nbuf[MAXNAME]; 10617940Seric register char **pvp; 10627682Seric 10637940Seric pvp = prescan(name, '\0'); 10648069Seric rewrite(pvp, 3); 10658353Seric rewrite(pvp, ruleset); 10668069Seric cataddr(pvp, nbuf, sizeof nbuf); 10677682Seric return (nbuf); 10687682Seric } 1069