13312Seric # include "sendmail.h" 2297Seric 3*9042Seric SCCSID(@(#)parseaddr.c 3.63 11/03/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; 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 } 3173149Seric 3188078Seric if (c == NOCHAR) 3198078Seric continue; 3203149Seric 3218078Seric /* see if this is end of input */ 3228423Seric if (c == delim && anglecnt <= 0) 3233149Seric break; 3243149Seric 3258078Seric newstate = StateTab[state][toktype(c)]; 3268078Seric # ifdef DEBUG 3278078Seric if (tTd(22, 101)) 3288078Seric printf("ns=%02o\n", newstate); 3298078Seric # endif DEBUG 3308078Seric state = newstate & TYPE; 3318078Seric if (bitset(M, newstate)) 3328078Seric c = NOCHAR; 3338078Seric if (bitset(B, newstate)) 3344228Seric break; 335297Seric } 3363149Seric 3373149Seric /* new token */ 3388078Seric if (tok != q) 3391378Seric { 3408078Seric *q++ = '\0'; 3418078Seric # ifdef DEBUG 3428078Seric if (tTd(22, 36)) 343297Seric { 3448078Seric printf("tok="); 3458078Seric xputs(tok); 3468078Seric putchar('\n'); 347297Seric } 3488078Seric # endif DEBUG 3498078Seric if (avp >= &av[MAXATOM]) 350297Seric { 3518078Seric syserr("prescan: too many tokens"); 3528078Seric DelimChar = p; 3538078Seric return (NULL); 354297Seric } 3558078Seric *avp++ = tok; 356297Seric } 3578423Seric } while (c != '\0' && (c != delim || anglecnt > 0)); 3583149Seric *avp = NULL; 3598078Seric DelimChar = --p; 3603149Seric if (cmntcnt > 0) 3613149Seric usrerr("Unbalanced '('"); 3628423Seric else if (anglecnt > 0) 3638423Seric usrerr("Unbalanced '<'"); 3648078Seric else if (state == QST) 3653149Seric usrerr("Unbalanced '\"'"); 3663149Seric else if (av[0] != NULL) 3673149Seric return (av); 3683149Seric return (NULL); 3693149Seric } 3703149Seric /* 3713149Seric ** TOKTYPE -- return token type 3723149Seric ** 3733149Seric ** Parameters: 3743149Seric ** c -- the character in question. 3753149Seric ** 3763149Seric ** Returns: 3773149Seric ** Its type. 3783149Seric ** 3793149Seric ** Side Effects: 3803149Seric ** none. 3813149Seric */ 382297Seric 3833149Seric toktype(c) 3843149Seric register char c; 3853149Seric { 3863380Seric static char buf[50]; 3873382Seric static bool firstime = TRUE; 3883380Seric 3893382Seric if (firstime) 3903380Seric { 3913382Seric firstime = FALSE; 3926977Seric expand("$o", buf, &buf[sizeof buf - 1], CurEnv); 3937005Seric (void) strcat(buf, DELIMCHARS); 3943380Seric } 3956053Seric if (c == MATCHCLASS || c == MATCHREPL) 3968078Seric return (ONE); 3978078Seric if (c == '"') 3988078Seric return (QST); 3994100Seric if (!isascii(c)) 4008078Seric return (ATM); 4018078Seric if (isspace(c) || c == ')') 4028078Seric return (SPC); 4033380Seric if (iscntrl(c) || index(buf, c) != NULL) 4048078Seric return (OPR); 4058078Seric return (ATM); 4063149Seric } 4073149Seric /* 4083149Seric ** REWRITE -- apply rewrite rules to token vector. 4093149Seric ** 4104476Seric ** This routine is an ordered production system. Each rewrite 4114476Seric ** rule has a LHS (called the pattern) and a RHS (called the 4124476Seric ** rewrite); 'rwr' points the the current rewrite rule. 4134476Seric ** 4144476Seric ** For each rewrite rule, 'avp' points the address vector we 4154476Seric ** are trying to match against, and 'pvp' points to the pattern. 4168058Seric ** If pvp points to a special match value (MATCHZANY, MATCHANY, 4178058Seric ** MATCHONE, MATCHCLASS) then the address in avp matched is 4188058Seric ** saved away in the match vector (pointed to by 'mvp'). 4194476Seric ** 4204476Seric ** When a match between avp & pvp does not match, we try to 4214476Seric ** back out. If we back up over a MATCHONE or a MATCHCLASS 4224476Seric ** we must also back out the match in mvp. If we reach a 4238058Seric ** MATCHANY or MATCHZANY we just extend the match and start 4248058Seric ** over again. 4254476Seric ** 4264476Seric ** When we finally match, we rewrite the address vector 4274476Seric ** and try over again. 4284476Seric ** 4293149Seric ** Parameters: 4303149Seric ** pvp -- pointer to token vector. 4313149Seric ** 4323149Seric ** Returns: 4333149Seric ** none. 4343149Seric ** 4353149Seric ** Side Effects: 4363149Seric ** pvp is modified. 4373149Seric */ 4382091Seric 4393149Seric struct match 4403149Seric { 4414468Seric char **first; /* first token matched */ 4424468Seric char **last; /* last token matched */ 4433149Seric }; 4443149Seric 4454468Seric # define MAXMATCH 9 /* max params per rewrite */ 4463149Seric 4473149Seric 4484070Seric rewrite(pvp, ruleset) 4493149Seric char **pvp; 4504070Seric int ruleset; 4513149Seric { 4523149Seric register char *ap; /* address pointer */ 4533149Seric register char *rp; /* rewrite pointer */ 4543149Seric register char **avp; /* address vector pointer */ 4553149Seric register char **rvp; /* rewrite vector pointer */ 4568058Seric register struct match *mlp; /* cur ptr into mlist */ 4578058Seric register struct rewrite *rwr; /* pointer to current rewrite rule */ 4584468Seric struct match mlist[MAXMATCH]; /* stores match on LHS */ 4593149Seric char *npvp[MAXATOM+1]; /* temporary space for rebuild */ 4604060Seric extern bool sameword(); 4613149Seric 4628335Seric if (Mode == MD_TEST || tTd(21, 2)) 4633149Seric { 4648959Seric printf("rewrite: ruleset %2d input:", ruleset); 4653149Seric printav(pvp); 4663149Seric } 4678423Seric if (pvp == NULL) 4688423Seric return; 4693149Seric 4703149Seric /* 4713149Seric ** Run through the list of rewrite rules, applying 4723149Seric ** any that match. 4733149Seric */ 4743149Seric 4754070Seric for (rwr = RewriteRules[ruleset]; rwr != NULL; ) 4763149Seric { 4774100Seric # ifdef DEBUG 4787675Seric if (tTd(21, 12)) 479297Seric { 4808069Seric printf("-----trying rule:"); 4813149Seric printav(rwr->r_lhs); 4823149Seric } 4834100Seric # endif DEBUG 4843149Seric 4853149Seric /* try to match on this rule */ 4864468Seric mlp = mlist; 4878058Seric rvp = rwr->r_lhs; 4888058Seric avp = pvp; 4898058Seric while ((ap = *avp) != NULL || *rvp != NULL) 4903149Seric { 4913149Seric rp = *rvp; 4928058Seric # ifdef DEBUG 4938058Seric if (tTd(21, 35)) 4948058Seric { 4958069Seric printf("ap="); 4968058Seric xputs(ap); 4978069Seric printf(", rp="); 4988058Seric xputs(rp); 4998069Seric printf("\n"); 5008058Seric } 5018058Seric # endif DEBUG 5023149Seric if (rp == NULL) 503297Seric { 5043149Seric /* end-of-pattern before end-of-address */ 5058058Seric goto backup; 506297Seric } 5078058Seric if (ap == NULL && *rp != MATCHZANY) 5088058Seric { 5098058Seric /* end-of-input */ 5108058Seric break; 5118058Seric } 5123149Seric 5133149Seric switch (*rp) 5143149Seric { 5154060Seric register STAB *s; 5164060Seric register int class; 5174060Seric 5184060Seric case MATCHCLASS: 5194060Seric /* match any token in a class */ 5204060Seric class = rp[1]; 5214060Seric if (!isalpha(class)) 5228058Seric goto backup; 5234060Seric if (isupper(class)) 5244060Seric class -= 'A'; 5254060Seric else 5264060Seric class -= 'a'; 5274100Seric s = stab(ap, ST_CLASS, ST_FIND); 5286273Seric if (s == NULL || (s->s_class & (1L << class)) == 0) 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--; 5744476Seric if (*rp == MATCHONE || *rp == MATCHCLASS) 5753149Seric { 5764468Seric /* back out binding */ 5774468Seric mlp--; 5783149Seric } 5793149Seric } 5803149Seric 5813149Seric if (rvp < rwr->r_lhs) 5823149Seric { 5833149Seric /* total failure to match */ 5843149Seric break; 5853149Seric } 586297Seric } 5873149Seric 5883149Seric /* 5893149Seric ** See if we successfully matched 5903149Seric */ 5913149Seric 5923149Seric if (rvp >= rwr->r_lhs && *rvp == NULL) 5933149Seric { 5948058Seric rvp = rwr->r_rhs; 5954100Seric # ifdef DEBUG 5967675Seric if (tTd(21, 12)) 5973149Seric { 5988069Seric printf("-----rule matches:"); 5998058Seric printav(rvp); 6003149Seric } 6014100Seric # endif DEBUG 6023149Seric 6038058Seric rp = *rvp; 6048226Seric if (*rp == CANONUSER) 6058058Seric { 6068069Seric rvp++; 6078069Seric rwr = rwr->r_next; 6088069Seric } 6098069Seric else if (*rp == CANONHOST) 6108069Seric { 6118069Seric rvp++; 6128069Seric rwr = NULL; 6138069Seric } 6148069Seric else if (*rp == CANONNET) 6158069Seric rwr = NULL; 6168058Seric 6173149Seric /* substitute */ 6188069Seric for (avp = npvp; *rvp != NULL; rvp++) 6193149Seric { 6203149Seric rp = *rvp; 6214468Seric if (*rp == MATCHREPL) 6223149Seric { 6233149Seric register struct match *m; 6243149Seric register char **pp; 6253149Seric 6264468Seric m = &mlist[rp[1] - '1']; 6274476Seric # ifdef DEBUG 6287675Seric if (tTd(21, 15)) 6294476Seric { 6304476Seric printf("$%c:", rp[1]); 6314476Seric pp = m->first; 6328058Seric while (pp <= m->last) 6334476Seric { 6344476Seric printf(" %x=\"", *pp); 6354625Seric (void) fflush(stdout); 6368058Seric printf("%s\"", *pp++); 6378058Seric } 6384476Seric printf("\n"); 6394476Seric } 6404476Seric # endif DEBUG 6414468Seric pp = m->first; 6428058Seric while (pp <= m->last) 6433149Seric { 6444468Seric if (avp >= &npvp[MAXATOM]) 6453149Seric { 6464468Seric syserr("rewrite: expansion too long"); 6474468Seric return; 6484468Seric } 6498058Seric *avp++ = *pp++; 6508058Seric } 6513149Seric } 6523149Seric else 6534385Seric { 6544385Seric if (avp >= &npvp[MAXATOM]) 6554385Seric { 6564385Seric syserr("rewrite: expansion too long"); 6574385Seric return; 6584385Seric } 6593149Seric *avp++ = rp; 6604385Seric } 6613149Seric } 6623149Seric *avp++ = NULL; 6638226Seric if (**npvp == CALLSUBR) 6648226Seric { 6658226Seric bmove((char *) &npvp[2], (char *) pvp, 6668226Seric (avp - npvp - 2) * sizeof *avp); 6673149Seric # ifdef DEBUG 6688226Seric if (tTd(21, 3)) 6698226Seric printf("-----callsubr %s\n", npvp[1]); 6708226Seric # endif DEBUG 6718226Seric rewrite(pvp, atoi(npvp[1])); 6728226Seric } 6738226Seric else 6748226Seric { 6758226Seric bmove((char *) npvp, (char *) pvp, 6768226Seric (avp - npvp) * sizeof *avp); 6778226Seric } 6788226Seric # ifdef DEBUG 6797675Seric if (tTd(21, 4)) 6803149Seric { 6818069Seric printf("rewritten as:"); 6828069Seric printav(pvp); 6833149Seric } 6843149Seric # endif DEBUG 6853149Seric } 6863149Seric else 6873149Seric { 6884100Seric # ifdef DEBUG 6897675Seric if (tTd(21, 10)) 6903149Seric printf("----- rule fails\n"); 6914100Seric # endif DEBUG 6923149Seric rwr = rwr->r_next; 6933149Seric } 694297Seric } 6958069Seric 6968335Seric if (Mode == 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 { 7414279Seric if (**++tv != CANONUSER) 7424279Seric syserr("buildaddr: error: no user"); 7434279Seric buf[0] = '\0'; 7444279Seric while (*++tv != NULL) 7454279Seric { 7464279Seric if (buf[0] != '\0') 7477005Seric (void) strcat(buf, " "); 7487005Seric (void) strcat(buf, *tv); 7494279Seric } 7504279Seric usrerr(buf); 7514279Seric return (NULL); 7524279Seric } 7534598Seric for (mp = Mailer; (m = *mp++) != NULL; ) 7543149Seric { 7554635Seric if (sameword(m->m_name, *tv)) 7563149Seric break; 7573149Seric } 7583149Seric if (m == NULL) 7594279Seric { 7603149Seric syserr("buildaddr: unknown net %s", *tv); 7614279Seric return (NULL); 7624279Seric } 7634598Seric a->q_mailer = m; 7643149Seric 7653149Seric /* figure out what host (if any) */ 7663149Seric tv++; 7674195Seric if (!bitset(M_LOCAL, m->m_flags)) 7683149Seric { 7695704Seric if (**tv++ != CANONHOST) 7704279Seric { 7713149Seric syserr("buildaddr: no host"); 7724279Seric return (NULL); 7734279Seric } 7745704Seric buf[0] = '\0'; 7755704Seric while (*tv != NULL && **tv != CANONUSER) 7767005Seric (void) strcat(buf, *tv++); 7775704Seric a->q_host = newstr(buf); 7783149Seric } 7793149Seric else 7803149Seric a->q_host = NULL; 7813149Seric 7823149Seric /* figure out the user */ 7833149Seric if (**tv != CANONUSER) 7844279Seric { 7853149Seric syserr("buildaddr: no user"); 7864279Seric return (NULL); 7874279Seric } 7884228Seric cataddr(++tv, buf, sizeof buf); 7893149Seric a->q_user = buf; 7903149Seric 7913149Seric return (a); 7923149Seric } 7933188Seric /* 7944228Seric ** CATADDR -- concatenate pieces of addresses (putting in <LWSP> subs) 7954228Seric ** 7964228Seric ** Parameters: 7974228Seric ** pvp -- parameter vector to rebuild. 7984228Seric ** buf -- buffer to build the string into. 7994228Seric ** sz -- size of buf. 8004228Seric ** 8014228Seric ** Returns: 8024228Seric ** none. 8034228Seric ** 8044228Seric ** Side Effects: 8054228Seric ** Destroys buf. 8064228Seric */ 8074228Seric 8084228Seric cataddr(pvp, buf, sz) 8094228Seric char **pvp; 8104228Seric char *buf; 8114228Seric register int sz; 8124228Seric { 8134228Seric bool oatomtok = FALSE; 8144228Seric bool natomtok = FALSE; 8154228Seric register int i; 8164228Seric register char *p; 8174228Seric 8188423Seric if (pvp == NULL) 8198423Seric { 8208423Seric strcpy(buf, ""); 8218423Seric return; 8228423Seric } 8234228Seric p = buf; 8244228Seric sz--; 8254228Seric while (*pvp != NULL && (i = strlen(*pvp)) < sz) 8264228Seric { 8278078Seric natomtok = (toktype(**pvp) == ATM); 8284228Seric if (oatomtok && natomtok) 829*9042Seric *p++ = SpaceSub; 8304228Seric (void) strcpy(p, *pvp); 8314228Seric oatomtok = natomtok; 8324228Seric p += i; 8334228Seric sz -= i; 8344228Seric pvp++; 8354228Seric } 8364228Seric *p = '\0'; 8374228Seric } 8384228Seric /* 8393188Seric ** SAMEADDR -- Determine if two addresses are the same 8403188Seric ** 8413188Seric ** This is not just a straight comparison -- if the mailer doesn't 8423188Seric ** care about the host we just ignore it, etc. 8433188Seric ** 8443188Seric ** Parameters: 8453188Seric ** a, b -- pointers to the internal forms to compare. 8463188Seric ** wildflg -- if TRUE, 'a' may have no user specified, 8473188Seric ** in which case it is to match anything. 8483188Seric ** 8493188Seric ** Returns: 8503188Seric ** TRUE -- they represent the same mailbox. 8513188Seric ** FALSE -- they don't. 8523188Seric ** 8533188Seric ** Side Effects: 8543188Seric ** none. 8553188Seric */ 8563188Seric 8573188Seric bool 8583188Seric sameaddr(a, b, wildflg) 8593188Seric register ADDRESS *a; 8603188Seric register ADDRESS *b; 8613188Seric bool wildflg; 8623188Seric { 8633188Seric /* if they don't have the same mailer, forget it */ 8643188Seric if (a->q_mailer != b->q_mailer) 8653188Seric return (FALSE); 8663188Seric 8673188Seric /* if the user isn't the same, we can drop out */ 8683188Seric if ((!wildflg || a->q_user[0] != '\0') && strcmp(a->q_user, b->q_user) != 0) 8693188Seric return (FALSE); 8703188Seric 8713188Seric /* if the mailer ignores hosts, we have succeeded! */ 8724598Seric if (bitset(M_LOCAL, a->q_mailer->m_flags)) 8733188Seric return (TRUE); 8743188Seric 8753188Seric /* otherwise compare hosts (but be careful for NULL ptrs) */ 8763188Seric if (a->q_host == NULL || b->q_host == NULL) 8773188Seric return (FALSE); 8783188Seric if (strcmp(a->q_host, b->q_host) != 0) 8793188Seric return (FALSE); 8803188Seric 8813188Seric return (TRUE); 8823188Seric } 8833234Seric /* 8843234Seric ** PRINTADDR -- print address (for debugging) 8853234Seric ** 8863234Seric ** Parameters: 8873234Seric ** a -- the address to print 8883234Seric ** follow -- follow the q_next chain. 8893234Seric ** 8903234Seric ** Returns: 8913234Seric ** none. 8923234Seric ** 8933234Seric ** Side Effects: 8943234Seric ** none. 8953234Seric */ 8963234Seric 8974317Seric # ifdef DEBUG 8984317Seric 8993234Seric printaddr(a, follow) 9003234Seric register ADDRESS *a; 9013234Seric bool follow; 9023234Seric { 9035001Seric bool first = TRUE; 9045001Seric 9053234Seric while (a != NULL) 9063234Seric { 9075001Seric first = FALSE; 9084443Seric printf("%x=", a); 9094085Seric (void) fflush(stdout); 9103234Seric printf("%s: mailer %d (%s), host `%s', user `%s'\n", a->q_paddr, 9118181Seric a->q_mailer->m_mno, a->q_mailer->m_name, a->q_host, 9128181Seric a->q_user); 9138181Seric printf("\tnext=%x, flags=%o, alias %x\n", a->q_next, a->q_flags, 9148181Seric a->q_alias); 9158181Seric printf("\thome=\"%s\", fullname=\"%s\"\n", a->q_home, 9168181Seric a->q_fullname); 9174996Seric 9183234Seric if (!follow) 9193234Seric return; 9204996Seric a = a->q_next; 9213234Seric } 9225001Seric if (first) 9234443Seric printf("[NULL]\n"); 9243234Seric } 9254317Seric 9264317Seric # endif DEBUG 9277682Seric /* 9287682Seric ** REMOTENAME -- return the name relative to the current mailer 9297682Seric ** 9307682Seric ** Parameters: 9317682Seric ** name -- the name to translate. 9328069Seric ** m -- the mailer that we want to do rewriting relative 9338069Seric ** to. 9348069Seric ** senderaddress -- if set, uses the sender rewriting rules 9358069Seric ** rather than the recipient rewriting rules. 9367682Seric ** 9377682Seric ** Returns: 9387682Seric ** the text string representing this address relative to 9397682Seric ** the receiving mailer. 9407682Seric ** 9417682Seric ** Side Effects: 9427682Seric ** none. 9437682Seric ** 9447682Seric ** Warnings: 9457682Seric ** The text string returned is tucked away locally; 9467682Seric ** copy it if you intend to save it. 9477682Seric */ 9487682Seric 9497682Seric char * 9508069Seric remotename(name, m, senderaddress) 9517682Seric char *name; 9527682Seric struct mailer *m; 9538069Seric bool senderaddress; 9547682Seric { 9558069Seric register char **pvp; 9568069Seric char *fancy; 9578069Seric extern char *macvalue(); 9588181Seric char *oldg = macvalue('g', CurEnv); 9597682Seric static char buf[MAXNAME]; 9607682Seric char lbuf[MAXNAME]; 9617682Seric extern char **prescan(); 9627889Seric extern char *crackaddr(); 9637682Seric 9647755Seric # ifdef DEBUG 9657755Seric if (tTd(12, 1)) 9667755Seric printf("remotename(%s)\n", name); 9677755Seric # endif DEBUG 9687755Seric 9697682Seric /* 9708181Seric ** Do a heuristic crack of this name to extract any comment info. 9718181Seric ** This will leave the name as a comment and a $g macro. 9727889Seric */ 9737889Seric 9747889Seric fancy = crackaddr(name); 9757889Seric 9768181Seric /* 9778181Seric ** Turn the name into canonical form. 9788181Seric ** Normally this will be RFC 822 style, i.e., "user@domain". 9798181Seric ** If this only resolves to "user", and the "C" flag is 9808181Seric ** specified in the sending mailer, then the sender's 9818181Seric ** domain will be appended. 9828181Seric */ 9838181Seric 9847889Seric pvp = prescan(name, '\0'); 9857889Seric if (pvp == NULL) 9867889Seric return (name); 9878181Seric rewrite(pvp, 3); 9888181Seric if (CurEnv->e_fromdomain != NULL) 9898181Seric { 9908181Seric /* append from domain to this address */ 9918181Seric register char **pxp = pvp; 9928181Seric 9938181Seric while (*pxp != NULL && strcmp(*pxp, "@") != 0) 9948181Seric pxp++; 9958181Seric if (*pxp == NULL) 9968181Seric { 9978181Seric register char **qxq = CurEnv->e_fromdomain; 9988181Seric 9998181Seric while (*qxq != NULL) 10008181Seric *pxp++ = *qxq++; 10018181Seric } 10028181Seric } 10038181Seric 10048181Seric /* 10058959Seric ** Do more specific rewriting. 10068181Seric ** Rewrite using ruleset 1 or 2 depending on whether this is 10078181Seric ** a sender address or not. 10088181Seric ** Then run it through any receiving-mailer-specific rulesets. 10098181Seric */ 10108181Seric 10118069Seric if (senderaddress) 10127755Seric { 10137889Seric rewrite(pvp, 1); 10148069Seric if (m->m_s_rwset > 0) 10158069Seric rewrite(pvp, m->m_s_rwset); 10168069Seric } 10178069Seric else 10188069Seric { 10197889Seric rewrite(pvp, 2); 10208069Seric if (m->m_r_rwset > 0) 10218069Seric rewrite(pvp, m->m_r_rwset); 10227682Seric } 10237682Seric 10248181Seric /* 10258959Seric ** Do any final sanitation the address may require. 10268959Seric ** This will normally be used to turn internal forms 10278959Seric ** (e.g., user@host.LOCAL) into external form. This 10288959Seric ** may be used as a default to the above rules. 10298959Seric */ 10308959Seric 10318959Seric rewrite(pvp, 4); 10328959Seric 10338959Seric /* 10348181Seric ** Now restore the comment information we had at the beginning. 10358181Seric */ 10368181Seric 10377682Seric cataddr(pvp, lbuf, sizeof lbuf); 10387682Seric define('g', lbuf); 10397889Seric expand(fancy, buf, &buf[sizeof buf - 1], CurEnv); 10407682Seric define('g', oldg); 10417682Seric 10427682Seric # ifdef DEBUG 10437682Seric if (tTd(12, 1)) 10447755Seric printf("remotename => `%s'\n", buf); 10457682Seric # endif DEBUG 10467682Seric return (buf); 10477682Seric } 10487682Seric /* 10497682Seric ** CANONNAME -- make name canonical 10507682Seric ** 10517682Seric ** This is used for SMTP and misc. printing. Given a print 10528181Seric ** address, it strips out comments, etc. 10537682Seric ** 10547682Seric ** Parameters: 10557682Seric ** name -- the name to make canonical. 10568353Seric ** ruleset -- the canonicalizing ruleset. 10577682Seric ** 10587682Seric ** Returns: 10597682Seric ** pointer to canonical name. 10607682Seric ** 10617682Seric ** Side Effects: 10627682Seric ** none. 10637682Seric ** 10647682Seric ** Warning: 10657682Seric ** result is saved in static buf; future calls will trash it. 10667682Seric */ 10677682Seric 10687682Seric char * 10698353Seric canonname(name, ruleset) 10707682Seric char *name; 10718353Seric int ruleset; 10727682Seric { 10738069Seric static char nbuf[MAXNAME]; 10747940Seric register char **pvp; 10757682Seric 10767940Seric pvp = prescan(name, '\0'); 10778069Seric rewrite(pvp, 3); 10788353Seric rewrite(pvp, ruleset); 10798069Seric cataddr(pvp, nbuf, sizeof nbuf); 10807682Seric return (nbuf); 10817682Seric } 1082