13312Seric # include "sendmail.h" 2297Seric 3*11445Seric SCCSID(@(#)parseaddr.c 3.80 03/08/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. 31*11445Seric ** delim -- the character to terminate the address, passed 32*11445Seric ** 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 * 47*11445Seric parseaddr(addr, a, copyf, delim) 48297Seric char *addr; 492973Seric register ADDRESS *a; 50297Seric int copyf; 51*11445Seric 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 68*11445Seric 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. 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 */ 1969051Seric /*OPR*/ OPR|B, ATM|B, QST|B, SPC|MB, ONE|B, 1979051Seric /*ATM*/ OPR|B, ATM, QST|B, SPC|MB, ONE|B, 1989051Seric /*QST*/ QST, QST, OPR, 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; 2149346Seric register int c; 2153149Seric char **avp; 216297Seric bool bslashmode; 217297Seric int cmntcnt; 2188423Seric 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; 2288423Seric 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 } 2838514Seric else if (state == QST) 2848514Seric { 2858514Seric /* do nothing, just avoid next clauses */ 2868514Seric } 2878078Seric else if (c == '(') 2884100Seric { 2898078Seric cmntcnt++; 2908078Seric c = NOCHAR; 2914100Seric } 2928078Seric else if (c == ')') 2933149Seric { 2948078Seric if (cmntcnt <= 0) 2953149Seric { 2968078Seric usrerr("Unbalanced ')'"); 2978078Seric DelimChar = p; 2988078Seric return (NULL); 2993149Seric } 3008078Seric else 3018078Seric cmntcnt--; 3028078Seric } 3038078Seric else if (cmntcnt > 0) 3048078Seric c = NOCHAR; 3058423Seric else if (c == '<') 3068423Seric anglecnt++; 3078423Seric else if (c == '>') 3088423Seric { 3098423Seric if (anglecnt <= 0) 3108423Seric { 3118423Seric usrerr("Unbalanced '>'"); 3128423Seric DelimChar = p; 3138423Seric return (NULL); 3148423Seric } 3158423Seric anglecnt--; 3168423Seric } 31711423Seric else if (delim == ' ' && isspace(c)) 31811423Seric c = ' '; 3193149Seric 3208078Seric if (c == NOCHAR) 3218078Seric continue; 3223149Seric 3238078Seric /* see if this is end of input */ 32411405Seric if (c == delim && anglecnt <= 0 && state != QST) 3253149Seric break; 3263149Seric 3278078Seric newstate = StateTab[state][toktype(c)]; 3288078Seric # ifdef DEBUG 3298078Seric if (tTd(22, 101)) 3308078Seric printf("ns=%02o\n", newstate); 3318078Seric # endif DEBUG 3328078Seric state = newstate & TYPE; 3338078Seric if (bitset(M, newstate)) 3348078Seric c = NOCHAR; 3358078Seric if (bitset(B, newstate)) 3364228Seric break; 337297Seric } 3383149Seric 3393149Seric /* new token */ 3408078Seric if (tok != q) 3411378Seric { 3428078Seric *q++ = '\0'; 3438078Seric # ifdef DEBUG 3448078Seric if (tTd(22, 36)) 345297Seric { 3468078Seric printf("tok="); 3478078Seric xputs(tok); 3488078Seric putchar('\n'); 349297Seric } 3508078Seric # endif DEBUG 3518078Seric if (avp >= &av[MAXATOM]) 352297Seric { 3538078Seric syserr("prescan: too many tokens"); 3548078Seric DelimChar = p; 3558078Seric return (NULL); 356297Seric } 3578078Seric *avp++ = tok; 358297Seric } 3598423Seric } while (c != '\0' && (c != delim || anglecnt > 0)); 3603149Seric *avp = NULL; 3618078Seric DelimChar = --p; 3623149Seric if (cmntcnt > 0) 3633149Seric usrerr("Unbalanced '('"); 3648423Seric else if (anglecnt > 0) 3658423Seric usrerr("Unbalanced '<'"); 3668078Seric else if (state == QST) 3673149Seric usrerr("Unbalanced '\"'"); 3683149Seric else if (av[0] != NULL) 3693149Seric return (av); 3703149Seric return (NULL); 3713149Seric } 3723149Seric /* 3733149Seric ** TOKTYPE -- return token type 3743149Seric ** 3753149Seric ** Parameters: 3763149Seric ** c -- the character in question. 3773149Seric ** 3783149Seric ** Returns: 3793149Seric ** Its type. 3803149Seric ** 3813149Seric ** Side Effects: 3823149Seric ** none. 3833149Seric */ 384297Seric 3853149Seric toktype(c) 3863149Seric register char c; 3873149Seric { 3883380Seric static char buf[50]; 3893382Seric static bool firstime = TRUE; 3903380Seric 3913382Seric if (firstime) 3923380Seric { 3933382Seric firstime = FALSE; 3946977Seric expand("$o", buf, &buf[sizeof buf - 1], CurEnv); 3957005Seric (void) strcat(buf, DELIMCHARS); 3963380Seric } 3979585Seric if (c == MATCHCLASS || c == MATCHREPL || c == MATCHNCLASS) 3988078Seric return (ONE); 3998078Seric if (c == '"') 4008078Seric return (QST); 4014100Seric if (!isascii(c)) 4028078Seric return (ATM); 4038078Seric if (isspace(c) || c == ')') 4048078Seric return (SPC); 4053380Seric if (iscntrl(c) || index(buf, c) != NULL) 4068078Seric return (OPR); 4078078Seric return (ATM); 4083149Seric } 4093149Seric /* 4103149Seric ** REWRITE -- apply rewrite rules to token vector. 4113149Seric ** 4124476Seric ** This routine is an ordered production system. Each rewrite 4134476Seric ** rule has a LHS (called the pattern) and a RHS (called the 4144476Seric ** rewrite); 'rwr' points the the current rewrite rule. 4154476Seric ** 4164476Seric ** For each rewrite rule, 'avp' points the address vector we 4174476Seric ** are trying to match against, and 'pvp' points to the pattern. 4188058Seric ** If pvp points to a special match value (MATCHZANY, MATCHANY, 4199585Seric ** MATCHONE, MATCHCLASS, MATCHNCLASS) then the address in avp 4209585Seric ** matched is saved away in the match vector (pointed to by 'mvp'). 4214476Seric ** 4224476Seric ** When a match between avp & pvp does not match, we try to 4239585Seric ** back out. If we back up over MATCHONE, MATCHCLASS, or MATCHNCLASS 4244476Seric ** we must also back out the match in mvp. If we reach a 4258058Seric ** MATCHANY or MATCHZANY we just extend the match and start 4268058Seric ** over again. 4274476Seric ** 4284476Seric ** When we finally match, we rewrite the address vector 4294476Seric ** and try over again. 4304476Seric ** 4313149Seric ** Parameters: 4323149Seric ** pvp -- pointer to token vector. 4333149Seric ** 4343149Seric ** Returns: 4353149Seric ** none. 4363149Seric ** 4373149Seric ** Side Effects: 4383149Seric ** pvp is modified. 4393149Seric */ 4402091Seric 4413149Seric struct match 4423149Seric { 4434468Seric char **first; /* first token matched */ 4444468Seric char **last; /* last token matched */ 4453149Seric }; 4463149Seric 4474468Seric # define MAXMATCH 9 /* max params per rewrite */ 4483149Seric 4493149Seric 4504070Seric rewrite(pvp, ruleset) 4513149Seric char **pvp; 4524070Seric int ruleset; 4533149Seric { 4543149Seric register char *ap; /* address pointer */ 4553149Seric register char *rp; /* rewrite pointer */ 4563149Seric register char **avp; /* address vector pointer */ 4573149Seric register char **rvp; /* rewrite vector pointer */ 4588058Seric register struct match *mlp; /* cur ptr into mlist */ 4598058Seric register struct rewrite *rwr; /* pointer to current rewrite rule */ 4604468Seric struct match mlist[MAXMATCH]; /* stores match on LHS */ 4613149Seric char *npvp[MAXATOM+1]; /* temporary space for rebuild */ 4624060Seric extern bool sameword(); 4633149Seric 4649279Seric if (OpMode == MD_TEST || tTd(21, 2)) 4653149Seric { 4668959Seric printf("rewrite: ruleset %2d input:", ruleset); 4673149Seric printav(pvp); 4683149Seric } 4698423Seric if (pvp == NULL) 4708423Seric return; 4713149Seric 4723149Seric /* 4733149Seric ** Run through the list of rewrite rules, applying 4743149Seric ** any that match. 4753149Seric */ 4763149Seric 4774070Seric for (rwr = RewriteRules[ruleset]; rwr != NULL; ) 4783149Seric { 4794100Seric # ifdef DEBUG 4807675Seric if (tTd(21, 12)) 481297Seric { 4828069Seric printf("-----trying rule:"); 4833149Seric printav(rwr->r_lhs); 4843149Seric } 4854100Seric # endif DEBUG 4863149Seric 4873149Seric /* try to match on this rule */ 4884468Seric mlp = mlist; 4898058Seric rvp = rwr->r_lhs; 4908058Seric avp = pvp; 4918058Seric while ((ap = *avp) != NULL || *rvp != NULL) 4923149Seric { 4933149Seric rp = *rvp; 4948058Seric # ifdef DEBUG 4958058Seric if (tTd(21, 35)) 4968058Seric { 4978069Seric printf("ap="); 4988058Seric xputs(ap); 4998069Seric printf(", rp="); 5008058Seric xputs(rp); 5018069Seric printf("\n"); 5028058Seric } 5038058Seric # endif DEBUG 5043149Seric if (rp == NULL) 505297Seric { 5063149Seric /* end-of-pattern before end-of-address */ 5078058Seric goto backup; 508297Seric } 5098058Seric if (ap == NULL && *rp != MATCHZANY) 5108058Seric { 5118058Seric /* end-of-input */ 5128058Seric break; 5138058Seric } 5143149Seric 5153149Seric switch (*rp) 5163149Seric { 5174060Seric register STAB *s; 5184060Seric 5194060Seric case MATCHCLASS: 5209585Seric case MATCHNCLASS: 5219585Seric /* match any token in (not in) a class */ 5224100Seric s = stab(ap, ST_CLASS, ST_FIND); 52310690Seric if (s == NULL || !bitnset(rp[1], s->s_class)) 5249585Seric { 5259585Seric if (*rp == MATCHCLASS) 5269585Seric goto backup; 5279585Seric } 5289585Seric else if (*rp == MATCHNCLASS) 5298058Seric goto backup; 5304468Seric 5314476Seric /* explicit fall-through */ 5324476Seric 5334476Seric case MATCHONE: 5344476Seric case MATCHANY: 5354476Seric /* match exactly one token */ 5368058Seric mlp->first = avp; 5378058Seric mlp->last = avp++; 5384468Seric mlp++; 5394060Seric break; 5404060Seric 5418058Seric case MATCHZANY: 5428058Seric /* match zero or more tokens */ 5438058Seric mlp->first = avp; 5448058Seric mlp->last = avp - 1; 5458058Seric mlp++; 5468058Seric break; 5478058Seric 5483149Seric default: 5493149Seric /* must have exact match */ 5504060Seric if (!sameword(rp, ap)) 5518058Seric goto backup; 5524468Seric avp++; 5533149Seric break; 5543149Seric } 5553149Seric 5563149Seric /* successful match on this token */ 5573149Seric rvp++; 5583149Seric continue; 5593149Seric 5608058Seric backup: 5613149Seric /* match failed -- back up */ 5623149Seric while (--rvp >= rwr->r_lhs) 5633149Seric { 5643149Seric rp = *rvp; 5658058Seric if (*rp == MATCHANY || *rp == MATCHZANY) 5664468Seric { 5674476Seric /* extend binding and continue */ 5688058Seric avp = ++mlp[-1].last; 5698058Seric avp++; 5704476Seric rvp++; 5713149Seric break; 5724468Seric } 5734476Seric avp--; 5749585Seric if (*rp == MATCHONE || *rp == MATCHCLASS || 5759585Seric *rp == MATCHNCLASS) 5763149Seric { 5774468Seric /* back out binding */ 5784468Seric mlp--; 5793149Seric } 5803149Seric } 5813149Seric 5823149Seric if (rvp < rwr->r_lhs) 5833149Seric { 5843149Seric /* total failure to match */ 5853149Seric break; 5863149Seric } 587297Seric } 5883149Seric 5893149Seric /* 5903149Seric ** See if we successfully matched 5913149Seric */ 5923149Seric 5939374Seric if (rvp < rwr->r_lhs || *rvp != NULL) 5943149Seric { 5954100Seric # ifdef DEBUG 5969374Seric if (tTd(21, 10)) 5979374Seric printf("----- rule fails\n"); 5984100Seric # endif DEBUG 5999374Seric rwr = rwr->r_next; 6009374Seric continue; 6019374Seric } 6023149Seric 6039374Seric rvp = rwr->r_rhs; 6049374Seric # ifdef DEBUG 6059374Seric if (tTd(21, 12)) 6069374Seric { 6079374Seric printf("-----rule matches:"); 6089374Seric printav(rvp); 6099374Seric } 6109374Seric # endif DEBUG 6119374Seric 6129374Seric rp = *rvp; 6139374Seric if (*rp == CANONUSER) 6149374Seric { 6159374Seric rvp++; 6169374Seric rwr = rwr->r_next; 6179374Seric } 6189374Seric else if (*rp == CANONHOST) 6199374Seric { 6209374Seric rvp++; 6219374Seric rwr = NULL; 6229374Seric } 6239374Seric else if (*rp == CANONNET) 6249374Seric rwr = NULL; 6259374Seric 6269374Seric /* substitute */ 6279374Seric for (avp = npvp; *rvp != NULL; rvp++) 6289374Seric { 6299374Seric register struct match *m; 6309374Seric register char **pp; 6319374Seric 6328058Seric rp = *rvp; 6339374Seric if (*rp != MATCHREPL) 6348058Seric { 6359374Seric if (avp >= &npvp[MAXATOM]) 6369374Seric { 6379374Seric syserr("rewrite: expansion too long"); 6389374Seric return; 6399374Seric } 6409374Seric *avp++ = rp; 6419374Seric continue; 6428069Seric } 6438058Seric 6449374Seric /* substitute from LHS */ 6459374Seric m = &mlist[rp[1] - '1']; 6469374Seric # ifdef DEBUG 6479374Seric if (tTd(21, 15)) 6483149Seric { 6499374Seric printf("$%c:", rp[1]); 6509374Seric pp = m->first; 6519374Seric while (pp <= m->last) 6523149Seric { 6539374Seric printf(" %x=\"", *pp); 6549374Seric (void) fflush(stdout); 6559374Seric printf("%s\"", *pp++); 6563149Seric } 6579374Seric printf("\n"); 6583149Seric } 6598226Seric # endif DEBUG 6609374Seric pp = m->first; 6619374Seric while (pp <= m->last) 6628226Seric { 6639374Seric if (avp >= &npvp[MAXATOM]) 6649374Seric { 6659374Seric syserr("rewrite: expansion too long"); 6669374Seric return; 6679374Seric } 6689374Seric *avp++ = *pp++; 6698226Seric } 6709374Seric } 6719374Seric *avp++ = NULL; 6729374Seric if (**npvp == CALLSUBR) 6739374Seric { 6749374Seric bmove((char *) &npvp[2], (char *) pvp, 6759374Seric (avp - npvp - 2) * sizeof *avp); 6768226Seric # ifdef DEBUG 6779374Seric if (tTd(21, 3)) 6789374Seric printf("-----callsubr %s\n", npvp[1]); 6793149Seric # endif DEBUG 6809374Seric rewrite(pvp, atoi(npvp[1])); 6813149Seric } 6823149Seric else 6833149Seric { 6849374Seric bmove((char *) npvp, (char *) pvp, 6859374Seric (avp - npvp) * sizeof *avp); 6869374Seric } 6874100Seric # ifdef DEBUG 6889374Seric if (tTd(21, 4)) 6899374Seric { 6909374Seric printf("rewritten as:"); 6919374Seric printav(pvp); 6929374Seric } 6934100Seric # endif DEBUG 694297Seric } 6958069Seric 6969279Seric if (OpMode == MD_TEST || tTd(21, 2)) 6978069Seric { 6988959Seric printf("rewrite: ruleset %2d returns:", ruleset); 6998069Seric printav(pvp); 7008069Seric } 7013149Seric } 7023149Seric /* 7033149Seric ** BUILDADDR -- build address from token vector. 7043149Seric ** 7053149Seric ** Parameters: 7063149Seric ** tv -- token vector. 7073149Seric ** a -- pointer to address descriptor to fill. 7083149Seric ** If NULL, one will be allocated. 7093149Seric ** 7103149Seric ** Returns: 7114279Seric ** NULL if there was an error. 7124279Seric ** 'a' otherwise. 7133149Seric ** 7143149Seric ** Side Effects: 7153149Seric ** fills in 'a' 7163149Seric */ 7173149Seric 7183149Seric ADDRESS * 7193149Seric buildaddr(tv, a) 7203149Seric register char **tv; 7213149Seric register ADDRESS *a; 7223149Seric { 7233149Seric static char buf[MAXNAME]; 7243149Seric struct mailer **mp; 7253149Seric register struct mailer *m; 7264635Seric extern bool sameword(); 7273149Seric 7283149Seric if (a == NULL) 7293149Seric a = (ADDRESS *) xalloc(sizeof *a); 7304988Seric clear((char *) a, sizeof *a); 7313149Seric 7323149Seric /* figure out what net/mailer to use */ 7333149Seric if (**tv != CANONNET) 7344279Seric { 7353149Seric syserr("buildaddr: no net"); 7364279Seric return (NULL); 7374279Seric } 7383149Seric tv++; 7394635Seric if (sameword(*tv, "error")) 7404279Seric { 74110183Seric if (**++tv == CANONHOST) 74210183Seric { 74310183Seric setstat(atoi(*++tv)); 74410183Seric tv++; 74510183Seric } 74610183Seric if (**tv != CANONUSER) 7474279Seric syserr("buildaddr: error: no user"); 7484279Seric buf[0] = '\0'; 7494279Seric while (*++tv != NULL) 7504279Seric { 7514279Seric if (buf[0] != '\0') 7527005Seric (void) strcat(buf, " "); 7537005Seric (void) strcat(buf, *tv); 7544279Seric } 7554279Seric usrerr(buf); 7564279Seric return (NULL); 7574279Seric } 7584598Seric for (mp = Mailer; (m = *mp++) != NULL; ) 7593149Seric { 7604635Seric if (sameword(m->m_name, *tv)) 7613149Seric break; 7623149Seric } 7633149Seric if (m == NULL) 7644279Seric { 7653149Seric syserr("buildaddr: unknown net %s", *tv); 7664279Seric return (NULL); 7674279Seric } 7684598Seric a->q_mailer = m; 7693149Seric 7703149Seric /* figure out what host (if any) */ 7713149Seric tv++; 77210690Seric if (!bitnset(M_LOCAL, m->m_flags)) 7733149Seric { 7745704Seric if (**tv++ != CANONHOST) 7754279Seric { 7763149Seric syserr("buildaddr: no host"); 7774279Seric return (NULL); 7784279Seric } 7795704Seric buf[0] = '\0'; 7805704Seric while (*tv != NULL && **tv != CANONUSER) 7817005Seric (void) strcat(buf, *tv++); 7825704Seric a->q_host = newstr(buf); 7833149Seric } 7843149Seric else 7853149Seric a->q_host = NULL; 7863149Seric 7873149Seric /* figure out the user */ 7883149Seric if (**tv != CANONUSER) 7894279Seric { 7903149Seric syserr("buildaddr: no user"); 7914279Seric return (NULL); 7924279Seric } 79311278Seric rewrite(++tv, 4); 79411278Seric cataddr(tv, buf, sizeof buf); 7953149Seric a->q_user = buf; 7963149Seric 7973149Seric return (a); 7983149Seric } 7993188Seric /* 8004228Seric ** CATADDR -- concatenate pieces of addresses (putting in <LWSP> subs) 8014228Seric ** 8024228Seric ** Parameters: 8034228Seric ** pvp -- parameter vector to rebuild. 8044228Seric ** buf -- buffer to build the string into. 8054228Seric ** sz -- size of buf. 8064228Seric ** 8074228Seric ** Returns: 8084228Seric ** none. 8094228Seric ** 8104228Seric ** Side Effects: 8114228Seric ** Destroys buf. 8124228Seric */ 8134228Seric 8144228Seric cataddr(pvp, buf, sz) 8154228Seric char **pvp; 8164228Seric char *buf; 8174228Seric register int sz; 8184228Seric { 8194228Seric bool oatomtok = FALSE; 8204228Seric bool natomtok = FALSE; 8214228Seric register int i; 8224228Seric register char *p; 8234228Seric 8248423Seric if (pvp == NULL) 8258423Seric { 8268423Seric strcpy(buf, ""); 8278423Seric return; 8288423Seric } 8294228Seric p = buf; 83011156Seric sz -= 2; 8314228Seric while (*pvp != NULL && (i = strlen(*pvp)) < sz) 8324228Seric { 8338078Seric natomtok = (toktype(**pvp) == ATM); 8344228Seric if (oatomtok && natomtok) 8359042Seric *p++ = SpaceSub; 8364228Seric (void) strcpy(p, *pvp); 8374228Seric oatomtok = natomtok; 8384228Seric p += i; 83911156Seric sz -= i + 1; 8404228Seric pvp++; 8414228Seric } 8424228Seric *p = '\0'; 8434228Seric } 8444228Seric /* 8453188Seric ** SAMEADDR -- Determine if two addresses are the same 8463188Seric ** 8473188Seric ** This is not just a straight comparison -- if the mailer doesn't 8483188Seric ** care about the host we just ignore it, etc. 8493188Seric ** 8503188Seric ** Parameters: 8513188Seric ** a, b -- pointers to the internal forms to compare. 8523188Seric ** 8533188Seric ** Returns: 8543188Seric ** TRUE -- they represent the same mailbox. 8553188Seric ** FALSE -- they don't. 8563188Seric ** 8573188Seric ** Side Effects: 8583188Seric ** none. 8593188Seric */ 8603188Seric 8613188Seric bool 8629374Seric sameaddr(a, b) 8633188Seric register ADDRESS *a; 8643188Seric register ADDRESS *b; 8653188Seric { 8663188Seric /* if they don't have the same mailer, forget it */ 8673188Seric if (a->q_mailer != b->q_mailer) 8683188Seric return (FALSE); 8693188Seric 8703188Seric /* if the user isn't the same, we can drop out */ 8719374Seric if (strcmp(a->q_user, b->q_user) != 0) 8723188Seric return (FALSE); 8733188Seric 8743188Seric /* if the mailer ignores hosts, we have succeeded! */ 87510690Seric if (bitnset(M_LOCAL, a->q_mailer->m_flags)) 8763188Seric return (TRUE); 8773188Seric 8783188Seric /* otherwise compare hosts (but be careful for NULL ptrs) */ 8793188Seric if (a->q_host == NULL || b->q_host == NULL) 8803188Seric return (FALSE); 8813188Seric if (strcmp(a->q_host, b->q_host) != 0) 8823188Seric return (FALSE); 8833188Seric 8843188Seric return (TRUE); 8853188Seric } 8863234Seric /* 8873234Seric ** PRINTADDR -- print address (for debugging) 8883234Seric ** 8893234Seric ** Parameters: 8903234Seric ** a -- the address to print 8913234Seric ** follow -- follow the q_next chain. 8923234Seric ** 8933234Seric ** Returns: 8943234Seric ** none. 8953234Seric ** 8963234Seric ** Side Effects: 8973234Seric ** none. 8983234Seric */ 8993234Seric 9004317Seric # ifdef DEBUG 9014317Seric 9023234Seric printaddr(a, follow) 9033234Seric register ADDRESS *a; 9043234Seric bool follow; 9053234Seric { 9065001Seric bool first = TRUE; 9075001Seric 9083234Seric while (a != NULL) 9093234Seric { 9105001Seric first = FALSE; 9114443Seric printf("%x=", a); 9124085Seric (void) fflush(stdout); 9133234Seric printf("%s: mailer %d (%s), host `%s', user `%s'\n", a->q_paddr, 9148181Seric a->q_mailer->m_mno, a->q_mailer->m_name, a->q_host, 9158181Seric a->q_user); 9168181Seric printf("\tnext=%x, flags=%o, alias %x\n", a->q_next, a->q_flags, 9178181Seric a->q_alias); 9188181Seric printf("\thome=\"%s\", fullname=\"%s\"\n", a->q_home, 9198181Seric a->q_fullname); 9204996Seric 9213234Seric if (!follow) 9223234Seric return; 9234996Seric a = a->q_next; 9243234Seric } 9255001Seric if (first) 9264443Seric printf("[NULL]\n"); 9273234Seric } 9284317Seric 9294317Seric # endif DEBUG 9307682Seric /* 9317682Seric ** REMOTENAME -- return the name relative to the current mailer 9327682Seric ** 9337682Seric ** Parameters: 9347682Seric ** name -- the name to translate. 9358069Seric ** m -- the mailer that we want to do rewriting relative 9368069Seric ** to. 9378069Seric ** senderaddress -- if set, uses the sender rewriting rules 9388069Seric ** rather than the recipient rewriting rules. 93910310Seric ** canonical -- if set, strip out any comment information, 94010310Seric ** etc. 9417682Seric ** 9427682Seric ** Returns: 9437682Seric ** the text string representing this address relative to 9447682Seric ** the receiving mailer. 9457682Seric ** 9467682Seric ** Side Effects: 9477682Seric ** none. 9487682Seric ** 9497682Seric ** Warnings: 9507682Seric ** The text string returned is tucked away locally; 9517682Seric ** copy it if you intend to save it. 9527682Seric */ 9537682Seric 9547682Seric char * 95510310Seric remotename(name, m, senderaddress, canonical) 9567682Seric char *name; 9577682Seric struct mailer *m; 9588069Seric bool senderaddress; 95910310Seric bool canonical; 9607682Seric { 9618069Seric register char **pvp; 9628069Seric char *fancy; 9638069Seric extern char *macvalue(); 9648181Seric char *oldg = macvalue('g', CurEnv); 9657682Seric static char buf[MAXNAME]; 9667682Seric char lbuf[MAXNAME]; 9677682Seric extern char **prescan(); 9687889Seric extern char *crackaddr(); 9697682Seric 9707755Seric # ifdef DEBUG 9717755Seric if (tTd(12, 1)) 9727755Seric printf("remotename(%s)\n", name); 9737755Seric # endif DEBUG 9747755Seric 97510177Seric /* don't do anything if we are tagging it as special */ 97610177Seric if ((senderaddress ? m->m_s_rwset : m->m_r_rwset) < 0) 97710177Seric return (name); 97810177Seric 9797682Seric /* 9808181Seric ** Do a heuristic crack of this name to extract any comment info. 9818181Seric ** This will leave the name as a comment and a $g macro. 9827889Seric */ 9837889Seric 98410310Seric if (canonical) 98510310Seric fancy = "$g"; 98610310Seric else 98710310Seric fancy = crackaddr(name); 9887889Seric 9898181Seric /* 9908181Seric ** Turn the name into canonical form. 9918181Seric ** Normally this will be RFC 822 style, i.e., "user@domain". 9928181Seric ** If this only resolves to "user", and the "C" flag is 9938181Seric ** specified in the sending mailer, then the sender's 9948181Seric ** domain will be appended. 9958181Seric */ 9968181Seric 9977889Seric pvp = prescan(name, '\0'); 9987889Seric if (pvp == NULL) 9997889Seric return (name); 10008181Seric rewrite(pvp, 3); 10018181Seric if (CurEnv->e_fromdomain != NULL) 10028181Seric { 10038181Seric /* append from domain to this address */ 10048181Seric register char **pxp = pvp; 10058181Seric 10069594Seric /* see if there is an "@domain" in the current name */ 10078181Seric while (*pxp != NULL && strcmp(*pxp, "@") != 0) 10088181Seric pxp++; 10098181Seric if (*pxp == NULL) 10108181Seric { 10119594Seric /* no.... append the "@domain" from the sender */ 10128181Seric register char **qxq = CurEnv->e_fromdomain; 10138181Seric 10149594Seric while ((*pxp++ = *qxq++) != NULL) 10159594Seric continue; 10168181Seric } 10178181Seric } 10188181Seric 10198181Seric /* 10208959Seric ** Do more specific rewriting. 10218181Seric ** Rewrite using ruleset 1 or 2 depending on whether this is 10228181Seric ** a sender address or not. 10238181Seric ** Then run it through any receiving-mailer-specific rulesets. 10248181Seric */ 10258181Seric 10268069Seric if (senderaddress) 10277755Seric { 10287889Seric rewrite(pvp, 1); 10298069Seric if (m->m_s_rwset > 0) 10308069Seric rewrite(pvp, m->m_s_rwset); 10318069Seric } 10328069Seric else 10338069Seric { 10347889Seric rewrite(pvp, 2); 10358069Seric if (m->m_r_rwset > 0) 10368069Seric rewrite(pvp, m->m_r_rwset); 10377682Seric } 10387682Seric 10398181Seric /* 10408959Seric ** Do any final sanitation the address may require. 10418959Seric ** This will normally be used to turn internal forms 10428959Seric ** (e.g., user@host.LOCAL) into external form. This 10438959Seric ** may be used as a default to the above rules. 10448959Seric */ 10458959Seric 10468959Seric rewrite(pvp, 4); 10478959Seric 10488959Seric /* 10498181Seric ** Now restore the comment information we had at the beginning. 10508181Seric */ 10518181Seric 10527682Seric cataddr(pvp, lbuf, sizeof lbuf); 10539374Seric define('g', lbuf, CurEnv); 10547889Seric expand(fancy, buf, &buf[sizeof buf - 1], CurEnv); 10559374Seric define('g', oldg, CurEnv); 10567682Seric 10577682Seric # ifdef DEBUG 10587682Seric if (tTd(12, 1)) 10597755Seric printf("remotename => `%s'\n", buf); 10607682Seric # endif DEBUG 10617682Seric return (buf); 10627682Seric } 1063