1*22976Smiriam 2*22976Smiriam /* 3*22976Smiriam ** Sendmail 4*22976Smiriam ** Copyright (c) 1983 Eric P. Allman 5*22976Smiriam ** Berkeley, California 6*22976Smiriam ** 7*22976Smiriam ** Copyright (c) 1983 Regents of the University of California. 8*22976Smiriam ** All rights reserved. The Berkeley software License Agreement 9*22976Smiriam ** specifies the terms and conditions for redistribution. 10*22976Smiriam */ 11*22976Smiriam 12*22976Smiriam #ifndef lint 13*22976Smiriam static char SccsId[] = "@(#)parseaddr.c 4.16 (Berkeley) 06/07/85"; 14*22976Smiriam #endif not lint 15*22976Smiriam 163312Seric # include "sendmail.h" 17297Seric 18297Seric /* 199888Seric ** PARSEADDR -- Parse an address 20297Seric ** 21297Seric ** Parses an address and breaks it up into three parts: a 22297Seric ** net to transmit the message on, the host to transmit it 23297Seric ** to, and a user on that host. These are loaded into an 242973Seric ** ADDRESS header with the values squirreled away if necessary. 25297Seric ** The "user" part may not be a real user; the process may 26297Seric ** just reoccur on that machine. For example, on a machine 27297Seric ** with an arpanet connection, the address 28297Seric ** csvax.bill@berkeley 29297Seric ** will break up to a "user" of 'csvax.bill' and a host 30297Seric ** of 'berkeley' -- to be transmitted over the arpanet. 31297Seric ** 32297Seric ** Parameters: 33297Seric ** addr -- the address to parse. 34297Seric ** a -- a pointer to the address descriptor buffer. 35297Seric ** If NULL, a header will be created. 36297Seric ** copyf -- determines what shall be copied: 37297Seric ** -1 -- don't copy anything. The printname 38297Seric ** (q_paddr) is just addr, and the 39297Seric ** user & host are allocated internally 40297Seric ** to parse. 41297Seric ** 0 -- copy out the parsed user & host, but 42297Seric ** don't copy the printname. 43297Seric ** +1 -- copy everything. 4411445Seric ** delim -- the character to terminate the address, passed 4511445Seric ** to prescan. 46297Seric ** 47297Seric ** Returns: 48297Seric ** A pointer to the address descriptor header (`a' if 49297Seric ** `a' is non-NULL). 50297Seric ** NULL on error. 51297Seric ** 52297Seric ** Side Effects: 53297Seric ** none 54297Seric */ 55297Seric 569374Seric /* following delimiters are inherent to the internal algorithms */ 5716155Seric # define DELIMCHARS "\001()<>,;\\\"\r\n" /* word delimiters */ 582091Seric 592973Seric ADDRESS * 6011445Seric parseaddr(addr, a, copyf, delim) 61297Seric char *addr; 622973Seric register ADDRESS *a; 63297Seric int copyf; 6411445Seric char delim; 65297Seric { 663149Seric register char **pvp; 673149Seric register struct mailer *m; 6816914Seric char pvpbuf[PSBUFSIZE]; 693149Seric extern char **prescan(); 703149Seric extern ADDRESS *buildaddr(); 71297Seric 72297Seric /* 73297Seric ** Initialize and prescan address. 74297Seric */ 75297Seric 766903Seric CurEnv->e_to = addr; 773188Seric # ifdef DEBUG 787675Seric if (tTd(20, 1)) 799888Seric printf("\n--parseaddr(%s)\n", addr); 803188Seric # endif DEBUG 813188Seric 8216914Seric pvp = prescan(addr, delim, pvpbuf); 833149Seric if (pvp == NULL) 84297Seric return (NULL); 85297Seric 86297Seric /* 873149Seric ** Apply rewriting rules. 887889Seric ** Ruleset 0 does basic parsing. It must resolve. 89297Seric */ 90297Seric 918181Seric rewrite(pvp, 3); 924070Seric rewrite(pvp, 0); 93297Seric 943149Seric /* 953149Seric ** See if we resolved to a real mailer. 963149Seric */ 97297Seric 983149Seric if (pvp[0][0] != CANONNET) 993149Seric { 1003149Seric setstat(EX_USAGE); 1013149Seric usrerr("cannot resolve name"); 1023149Seric return (NULL); 103297Seric } 104297Seric 105297Seric /* 1063149Seric ** Build canonical address from pvp. 107297Seric */ 108297Seric 1093149Seric a = buildaddr(pvp, a); 1104279Seric if (a == NULL) 1114279Seric return (NULL); 1124598Seric m = a->q_mailer; 113297Seric 114297Seric /* 1153149Seric ** Make local copies of the host & user and then 1163149Seric ** transport them out. 117297Seric */ 118297Seric 119297Seric if (copyf > 0) 1208078Seric { 1218078Seric extern char *DelimChar; 1228078Seric char savec = *DelimChar; 1238078Seric 1248078Seric *DelimChar = '\0'; 1252973Seric a->q_paddr = newstr(addr); 1268078Seric *DelimChar = savec; 1278078Seric } 128297Seric else 129297Seric a->q_paddr = addr; 1303149Seric if (copyf >= 0) 131297Seric { 1323149Seric if (a->q_host != NULL) 1333149Seric a->q_host = newstr(a->q_host); 134297Seric else 1353149Seric a->q_host = ""; 1363149Seric if (a->q_user != a->q_paddr) 1373149Seric a->q_user = newstr(a->q_user); 138297Seric } 139297Seric 140297Seric /* 14116202Seric ** Convert host name to lower case if requested. 14216202Seric ** User name will be done later. 14316202Seric */ 14416202Seric 14516202Seric if (!bitnset(M_HST_UPPER, m->m_flags)) 14616202Seric makelower(a->q_host); 14716202Seric 14816202Seric /* 149297Seric ** Compute return value. 150297Seric */ 151297Seric 152297Seric # ifdef DEBUG 1537675Seric if (tTd(20, 1)) 1544443Seric { 1559888Seric printf("parseaddr-->"); 1564443Seric printaddr(a, FALSE); 1574443Seric } 158297Seric # endif DEBUG 159297Seric 160297Seric return (a); 161297Seric } 162297Seric /* 16316162Seric ** LOWERADDR -- map UPPER->lower case on addresses as requested. 16416162Seric ** 16516162Seric ** Parameters: 16616162Seric ** a -- address to be mapped. 16716162Seric ** 16816162Seric ** Returns: 16916162Seric ** none. 17016162Seric ** 17116162Seric ** Side Effects: 17216162Seric ** none. 17316162Seric */ 17416162Seric 17516162Seric loweraddr(a) 17616162Seric register ADDRESS *a; 17716162Seric { 17816162Seric register MAILER *m = a->q_mailer; 17916162Seric 18016162Seric if (!bitnset(M_USR_UPPER, m->m_flags)) 18116162Seric makelower(a->q_user); 18216162Seric } 18316162Seric /* 184297Seric ** PRESCAN -- Prescan name and make it canonical 185297Seric ** 1869374Seric ** Scans a name and turns it into a set of tokens. This process 1879374Seric ** deletes blanks and comments (in parentheses). 188297Seric ** 189297Seric ** This routine knows about quoted strings and angle brackets. 190297Seric ** 191297Seric ** There are certain subtleties to this routine. The one that 192297Seric ** comes to mind now is that backslashes on the ends of names 193297Seric ** are silently stripped off; this is intentional. The problem 194297Seric ** is that some versions of sndmsg (like at LBL) set the kill 195297Seric ** character to something other than @ when reading addresses; 196297Seric ** so people type "csvax.eric\@berkeley" -- which screws up the 197297Seric ** berknet mailer. 198297Seric ** 199297Seric ** Parameters: 200297Seric ** addr -- the name to chomp. 201297Seric ** delim -- the delimiter for the address, normally 202297Seric ** '\0' or ','; \0 is accepted in any case. 20315284Seric ** If '\t' then we are reading the .cf file. 20416914Seric ** pvpbuf -- place to put the saved text -- note that 20516914Seric ** the pointers are static. 206297Seric ** 207297Seric ** Returns: 2083149Seric ** A pointer to a vector of tokens. 209297Seric ** NULL on error. 210297Seric ** 211297Seric ** Side Effects: 2123149Seric ** none. 213297Seric */ 214297Seric 2158078Seric /* states and character types */ 2168078Seric # define OPR 0 /* operator */ 2178078Seric # define ATM 1 /* atom */ 2188078Seric # define QST 2 /* in quoted string */ 2198078Seric # define SPC 3 /* chewing up spaces */ 2208078Seric # define ONE 4 /* pick up one character */ 2213149Seric 2228078Seric # define NSTATES 5 /* number of states */ 2238078Seric # define TYPE 017 /* mask to select state type */ 2248078Seric 2258078Seric /* meta bits for table */ 2268078Seric # define M 020 /* meta character; don't pass through */ 2278078Seric # define B 040 /* cause a break */ 2288078Seric # define MB M|B /* meta-break */ 2298078Seric 2308078Seric static short StateTab[NSTATES][NSTATES] = 2318078Seric { 2328087Seric /* oldst chtype> OPR ATM QST SPC ONE */ 2339051Seric /*OPR*/ OPR|B, ATM|B, QST|B, SPC|MB, ONE|B, 2349051Seric /*ATM*/ OPR|B, ATM, QST|B, SPC|MB, ONE|B, 2359051Seric /*QST*/ QST, QST, OPR, QST, QST, 2368078Seric /*SPC*/ OPR, ATM, QST, SPC|M, ONE, 2378078Seric /*ONE*/ OPR, OPR, OPR, OPR, OPR, 2388078Seric }; 2398078Seric 2408078Seric # define NOCHAR -1 /* signal nothing in lookahead token */ 2418078Seric 2428078Seric char *DelimChar; /* set to point to the delimiter */ 2438078Seric 2443149Seric char ** 24516914Seric prescan(addr, delim, pvpbuf) 246297Seric char *addr; 247297Seric char delim; 24816914Seric char pvpbuf[]; 249297Seric { 250297Seric register char *p; 2518078Seric register char *q; 2529346Seric register int c; 2533149Seric char **avp; 254297Seric bool bslashmode; 255297Seric int cmntcnt; 2568423Seric int anglecnt; 2573149Seric char *tok; 2588078Seric int state; 2598078Seric int newstate; 2608078Seric static char *av[MAXATOM+1]; 26115253Seric extern int errno; 262297Seric 26315253Seric /* make sure error messages don't have garbage on them */ 26415253Seric errno = 0; 26515253Seric 26616914Seric q = pvpbuf; 2673149Seric bslashmode = FALSE; 2687800Seric cmntcnt = 0; 2698423Seric anglecnt = 0; 2703149Seric avp = av; 2718078Seric state = OPR; 2728078Seric c = NOCHAR; 2738078Seric p = addr; 2748078Seric # ifdef DEBUG 2758078Seric if (tTd(22, 45)) 276297Seric { 2778078Seric printf("prescan: "); 2788078Seric xputs(p); 2798078Seric putchar('\n'); 2808078Seric } 2818078Seric # endif DEBUG 2828078Seric 2838078Seric do 2848078Seric { 2853149Seric /* read a token */ 2863149Seric tok = q; 2878078Seric for (;;) 288297Seric { 2898078Seric /* store away any old lookahead character */ 2908078Seric if (c != NOCHAR) 2918078Seric { 29215284Seric /* see if there is room */ 29316914Seric if (q >= &pvpbuf[PSBUFSIZE - 5]) 2948078Seric { 2958078Seric usrerr("Address too long"); 2968078Seric DelimChar = p; 2978078Seric return (NULL); 2988078Seric } 29915284Seric 30015284Seric /* squirrel it away */ 3018078Seric *q++ = c; 3028078Seric } 3038078Seric 3048078Seric /* read a new input character */ 3058078Seric c = *p++; 3068078Seric if (c == '\0') 3078078Seric break; 30815284Seric c &= ~0200; 30915284Seric 3108078Seric # ifdef DEBUG 3118078Seric if (tTd(22, 101)) 3128078Seric printf("c=%c, s=%d; ", c, state); 3138078Seric # endif DEBUG 3148078Seric 3153149Seric /* chew up special characters */ 3163149Seric *q = '\0'; 3173149Seric if (bslashmode) 3183149Seric { 3193149Seric c |= 0200; 3203149Seric bslashmode = FALSE; 3213149Seric } 3223149Seric else if (c == '\\') 3233149Seric { 3243149Seric bslashmode = TRUE; 3258078Seric c = NOCHAR; 3263149Seric } 3278514Seric else if (state == QST) 3288514Seric { 3298514Seric /* do nothing, just avoid next clauses */ 3308514Seric } 3318078Seric else if (c == '(') 3324100Seric { 3338078Seric cmntcnt++; 3348078Seric c = NOCHAR; 3354100Seric } 3368078Seric else if (c == ')') 3373149Seric { 3388078Seric if (cmntcnt <= 0) 3393149Seric { 3408078Seric usrerr("Unbalanced ')'"); 3418078Seric DelimChar = p; 3428078Seric return (NULL); 3433149Seric } 3448078Seric else 3458078Seric cmntcnt--; 3468078Seric } 3478078Seric else if (cmntcnt > 0) 3488078Seric c = NOCHAR; 3498423Seric else if (c == '<') 3508423Seric anglecnt++; 3518423Seric else if (c == '>') 3528423Seric { 3538423Seric if (anglecnt <= 0) 3548423Seric { 3558423Seric usrerr("Unbalanced '>'"); 3568423Seric DelimChar = p; 3578423Seric return (NULL); 3588423Seric } 3598423Seric anglecnt--; 3608423Seric } 36111423Seric else if (delim == ' ' && isspace(c)) 36211423Seric c = ' '; 3633149Seric 3648078Seric if (c == NOCHAR) 3658078Seric continue; 3663149Seric 3678078Seric /* see if this is end of input */ 36811405Seric if (c == delim && anglecnt <= 0 && state != QST) 3693149Seric break; 3703149Seric 3718078Seric newstate = StateTab[state][toktype(c)]; 3728078Seric # ifdef DEBUG 3738078Seric if (tTd(22, 101)) 3748078Seric printf("ns=%02o\n", newstate); 3758078Seric # endif DEBUG 3768078Seric state = newstate & TYPE; 3778078Seric if (bitset(M, newstate)) 3788078Seric c = NOCHAR; 3798078Seric if (bitset(B, newstate)) 3804228Seric break; 381297Seric } 3823149Seric 3833149Seric /* new token */ 3848078Seric if (tok != q) 3851378Seric { 3868078Seric *q++ = '\0'; 3878078Seric # ifdef DEBUG 3888078Seric if (tTd(22, 36)) 389297Seric { 3908078Seric printf("tok="); 3918078Seric xputs(tok); 3928078Seric putchar('\n'); 393297Seric } 3948078Seric # endif DEBUG 3958078Seric if (avp >= &av[MAXATOM]) 396297Seric { 3978078Seric syserr("prescan: too many tokens"); 3988078Seric DelimChar = p; 3998078Seric return (NULL); 400297Seric } 4018078Seric *avp++ = tok; 402297Seric } 4038423Seric } while (c != '\0' && (c != delim || anglecnt > 0)); 4043149Seric *avp = NULL; 4058078Seric DelimChar = --p; 4063149Seric if (cmntcnt > 0) 4073149Seric usrerr("Unbalanced '('"); 4088423Seric else if (anglecnt > 0) 4098423Seric usrerr("Unbalanced '<'"); 4108078Seric else if (state == QST) 4113149Seric usrerr("Unbalanced '\"'"); 4123149Seric else if (av[0] != NULL) 4133149Seric return (av); 4143149Seric return (NULL); 4153149Seric } 4163149Seric /* 4173149Seric ** TOKTYPE -- return token type 4183149Seric ** 4193149Seric ** Parameters: 4203149Seric ** c -- the character in question. 4213149Seric ** 4223149Seric ** Returns: 4233149Seric ** Its type. 4243149Seric ** 4253149Seric ** Side Effects: 4263149Seric ** none. 4273149Seric */ 428297Seric 4293149Seric toktype(c) 4303149Seric register char c; 4313149Seric { 4323380Seric static char buf[50]; 4333382Seric static bool firstime = TRUE; 4343380Seric 4353382Seric if (firstime) 4363380Seric { 4373382Seric firstime = FALSE; 43816155Seric expand("\001o", buf, &buf[sizeof buf - 1], CurEnv); 4397005Seric (void) strcat(buf, DELIMCHARS); 4403380Seric } 4419585Seric if (c == MATCHCLASS || c == MATCHREPL || c == MATCHNCLASS) 4428078Seric return (ONE); 4438078Seric if (c == '"') 4448078Seric return (QST); 4454100Seric if (!isascii(c)) 4468078Seric return (ATM); 4478078Seric if (isspace(c) || c == ')') 4488078Seric return (SPC); 4493380Seric if (iscntrl(c) || index(buf, c) != NULL) 4508078Seric return (OPR); 4518078Seric return (ATM); 4523149Seric } 4533149Seric /* 4543149Seric ** REWRITE -- apply rewrite rules to token vector. 4553149Seric ** 4564476Seric ** This routine is an ordered production system. Each rewrite 4574476Seric ** rule has a LHS (called the pattern) and a RHS (called the 4584476Seric ** rewrite); 'rwr' points the the current rewrite rule. 4594476Seric ** 4604476Seric ** For each rewrite rule, 'avp' points the address vector we 4614476Seric ** are trying to match against, and 'pvp' points to the pattern. 4628058Seric ** If pvp points to a special match value (MATCHZANY, MATCHANY, 4639585Seric ** MATCHONE, MATCHCLASS, MATCHNCLASS) then the address in avp 4649585Seric ** matched is saved away in the match vector (pointed to by 'mvp'). 4654476Seric ** 4664476Seric ** When a match between avp & pvp does not match, we try to 4679585Seric ** back out. If we back up over MATCHONE, MATCHCLASS, or MATCHNCLASS 4684476Seric ** we must also back out the match in mvp. If we reach a 4698058Seric ** MATCHANY or MATCHZANY we just extend the match and start 4708058Seric ** over again. 4714476Seric ** 4724476Seric ** When we finally match, we rewrite the address vector 4734476Seric ** and try over again. 4744476Seric ** 4753149Seric ** Parameters: 4763149Seric ** pvp -- pointer to token vector. 4773149Seric ** 4783149Seric ** Returns: 4793149Seric ** none. 4803149Seric ** 4813149Seric ** Side Effects: 4823149Seric ** pvp is modified. 4833149Seric */ 4842091Seric 4853149Seric struct match 4863149Seric { 4874468Seric char **first; /* first token matched */ 4884468Seric char **last; /* last token matched */ 4893149Seric }; 4903149Seric 4914468Seric # define MAXMATCH 9 /* max params per rewrite */ 4923149Seric 4933149Seric 4944070Seric rewrite(pvp, ruleset) 4953149Seric char **pvp; 4964070Seric int ruleset; 4973149Seric { 4983149Seric register char *ap; /* address pointer */ 4993149Seric register char *rp; /* rewrite pointer */ 5003149Seric register char **avp; /* address vector pointer */ 5013149Seric register char **rvp; /* rewrite vector pointer */ 5028058Seric register struct match *mlp; /* cur ptr into mlist */ 5038058Seric register struct rewrite *rwr; /* pointer to current rewrite rule */ 5044468Seric struct match mlist[MAXMATCH]; /* stores match on LHS */ 5053149Seric char *npvp[MAXATOM+1]; /* temporary space for rebuild */ 5064060Seric extern bool sameword(); 5073149Seric 5089279Seric if (OpMode == MD_TEST || tTd(21, 2)) 5093149Seric { 5108959Seric printf("rewrite: ruleset %2d input:", ruleset); 5113149Seric printav(pvp); 5123149Seric } 5138423Seric if (pvp == NULL) 5148423Seric return; 5153149Seric 5163149Seric /* 5173149Seric ** Run through the list of rewrite rules, applying 5183149Seric ** any that match. 5193149Seric */ 5203149Seric 5214070Seric for (rwr = RewriteRules[ruleset]; rwr != NULL; ) 5223149Seric { 5234100Seric # ifdef DEBUG 5247675Seric if (tTd(21, 12)) 525297Seric { 5268069Seric printf("-----trying rule:"); 5273149Seric printav(rwr->r_lhs); 5283149Seric } 5294100Seric # endif DEBUG 5303149Seric 5313149Seric /* try to match on this rule */ 5324468Seric mlp = mlist; 5338058Seric rvp = rwr->r_lhs; 5348058Seric avp = pvp; 5358058Seric while ((ap = *avp) != NULL || *rvp != NULL) 5363149Seric { 5373149Seric rp = *rvp; 5388058Seric # ifdef DEBUG 5398058Seric if (tTd(21, 35)) 5408058Seric { 5418069Seric printf("ap="); 5428058Seric xputs(ap); 5438069Seric printf(", rp="); 5448058Seric xputs(rp); 5458069Seric printf("\n"); 5468058Seric } 5478058Seric # endif DEBUG 5483149Seric if (rp == NULL) 549297Seric { 5503149Seric /* end-of-pattern before end-of-address */ 5518058Seric goto backup; 552297Seric } 5538058Seric if (ap == NULL && *rp != MATCHZANY) 5548058Seric { 5558058Seric /* end-of-input */ 5568058Seric break; 5578058Seric } 5583149Seric 5593149Seric switch (*rp) 5603149Seric { 5614060Seric register STAB *s; 5624060Seric 5634060Seric case MATCHCLASS: 5649585Seric case MATCHNCLASS: 5659585Seric /* match any token in (not in) a class */ 5664100Seric s = stab(ap, ST_CLASS, ST_FIND); 56710690Seric if (s == NULL || !bitnset(rp[1], s->s_class)) 5689585Seric { 5699585Seric if (*rp == MATCHCLASS) 5709585Seric goto backup; 5719585Seric } 5729585Seric else if (*rp == MATCHNCLASS) 5738058Seric goto backup; 5744468Seric 5754476Seric /* explicit fall-through */ 5764476Seric 5774476Seric case MATCHONE: 5784476Seric case MATCHANY: 5794476Seric /* match exactly one token */ 5808058Seric mlp->first = avp; 5818058Seric mlp->last = avp++; 5824468Seric mlp++; 5834060Seric break; 5844060Seric 5858058Seric case MATCHZANY: 5868058Seric /* match zero or more tokens */ 5878058Seric mlp->first = avp; 5888058Seric mlp->last = avp - 1; 5898058Seric mlp++; 5908058Seric break; 5918058Seric 5923149Seric default: 5933149Seric /* must have exact match */ 5944060Seric if (!sameword(rp, ap)) 5958058Seric goto backup; 5964468Seric avp++; 5973149Seric break; 5983149Seric } 5993149Seric 6003149Seric /* successful match on this token */ 6013149Seric rvp++; 6023149Seric continue; 6033149Seric 6048058Seric backup: 6053149Seric /* match failed -- back up */ 6063149Seric while (--rvp >= rwr->r_lhs) 6073149Seric { 6083149Seric rp = *rvp; 6098058Seric if (*rp == MATCHANY || *rp == MATCHZANY) 6104468Seric { 6114476Seric /* extend binding and continue */ 6128058Seric avp = ++mlp[-1].last; 6138058Seric avp++; 6144476Seric rvp++; 6153149Seric break; 6164468Seric } 6174476Seric avp--; 6189585Seric if (*rp == MATCHONE || *rp == MATCHCLASS || 6199585Seric *rp == MATCHNCLASS) 6203149Seric { 6214468Seric /* back out binding */ 6224468Seric mlp--; 6233149Seric } 6243149Seric } 6253149Seric 6263149Seric if (rvp < rwr->r_lhs) 6273149Seric { 6283149Seric /* total failure to match */ 6293149Seric break; 6303149Seric } 631297Seric } 6323149Seric 6333149Seric /* 6343149Seric ** See if we successfully matched 6353149Seric */ 6363149Seric 6379374Seric if (rvp < rwr->r_lhs || *rvp != NULL) 6383149Seric { 6394100Seric # ifdef DEBUG 6409374Seric if (tTd(21, 10)) 6419374Seric printf("----- rule fails\n"); 6424100Seric # endif DEBUG 6439374Seric rwr = rwr->r_next; 6449374Seric continue; 6459374Seric } 6463149Seric 6479374Seric rvp = rwr->r_rhs; 6489374Seric # ifdef DEBUG 6499374Seric if (tTd(21, 12)) 6509374Seric { 6519374Seric printf("-----rule matches:"); 6529374Seric printav(rvp); 6539374Seric } 6549374Seric # endif DEBUG 6559374Seric 6569374Seric rp = *rvp; 6579374Seric if (*rp == CANONUSER) 6589374Seric { 6599374Seric rvp++; 6609374Seric rwr = rwr->r_next; 6619374Seric } 6629374Seric else if (*rp == CANONHOST) 6639374Seric { 6649374Seric rvp++; 6659374Seric rwr = NULL; 6669374Seric } 6679374Seric else if (*rp == CANONNET) 6689374Seric rwr = NULL; 6699374Seric 6709374Seric /* substitute */ 6719374Seric for (avp = npvp; *rvp != NULL; rvp++) 6729374Seric { 6739374Seric register struct match *m; 6749374Seric register char **pp; 6759374Seric 6768058Seric rp = *rvp; 67716914Seric if (*rp == MATCHREPL) 6788058Seric { 67916914Seric /* substitute from LHS */ 68016914Seric m = &mlist[rp[1] - '1']; 68116914Seric if (m >= mlp) 6829374Seric { 68316914Seric syserr("rewrite: ruleset %d: replacement out of bounds", ruleset); 6849374Seric return; 6859374Seric } 6869374Seric # ifdef DEBUG 68716914Seric if (tTd(21, 15)) 68816914Seric { 68916914Seric printf("$%c:", rp[1]); 69016914Seric pp = m->first; 69116914Seric while (pp <= m->last) 69216914Seric { 69316914Seric printf(" %x=\"", *pp); 69416914Seric (void) fflush(stdout); 69516914Seric printf("%s\"", *pp++); 69616914Seric } 69716914Seric printf("\n"); 69816914Seric } 69916914Seric # endif DEBUG 7009374Seric pp = m->first; 7019374Seric while (pp <= m->last) 7023149Seric { 70316914Seric if (avp >= &npvp[MAXATOM]) 70416914Seric { 70516914Seric syserr("rewrite: expansion too long"); 70616914Seric return; 70716914Seric } 70816914Seric *avp++ = *pp++; 7093149Seric } 7103149Seric } 71116914Seric else 7128226Seric { 71316914Seric /* vanilla replacement */ 7149374Seric if (avp >= &npvp[MAXATOM]) 71516889Seric { 71616914Seric toolong: 71716889Seric syserr("rewrite: expansion too long"); 71816889Seric return; 71916889Seric } 72016914Seric *avp++ = rp; 7218226Seric } 7229374Seric } 7239374Seric *avp++ = NULL; 72416914Seric 72516914Seric /* 72616914Seric ** Check for any hostname lookups. 72716914Seric */ 72816914Seric 72916914Seric for (rvp = npvp; *rvp != NULL; rvp++) 73016914Seric { 73116914Seric char **hbrvp; 73216914Seric char **xpvp; 73316914Seric int trsize; 73417473Seric char *olddelimchar; 73516920Seric char buf[MAXNAME + 1]; 73616914Seric char *pvpb1[MAXATOM + 1]; 73717174Seric char pvpbuf[PSBUFSIZE]; 73817473Seric extern char *DelimChar; 73916914Seric 74016914Seric if (**rvp != HOSTBEGIN) 74116914Seric continue; 74216914Seric 74316914Seric /* 74416914Seric ** Got a hostname lookup. 74516914Seric ** 74616914Seric ** This could be optimized fairly easily. 74716914Seric */ 74816914Seric 74916914Seric hbrvp = rvp; 75016914Seric 75116914Seric /* extract the match part */ 75216914Seric while (*++rvp != NULL && **rvp != HOSTEND) 75316914Seric continue; 75416914Seric if (*rvp != NULL) 75516914Seric *rvp++ = NULL; 75616914Seric 75716914Seric /* save the remainder of the input string */ 75816914Seric trsize = (int) (avp - rvp + 1) * sizeof *rvp; 75916914Seric bcopy((char *) rvp, (char *) pvpb1, trsize); 76016914Seric 76116914Seric /* look it up */ 76216914Seric cataddr(++hbrvp, buf, sizeof buf); 76316914Seric maphostname(buf, sizeof buf); 76416914Seric 76516914Seric /* scan the new host name */ 76617473Seric olddelimchar = DelimChar; 76716914Seric xpvp = prescan(buf, '\0', pvpbuf); 76817473Seric DelimChar = olddelimchar; 76916914Seric if (xpvp == NULL) 77016914Seric { 77116914Seric syserr("rewrite: cannot prescan canonical hostname: %s", buf); 772*22976Smiriam return; 77316914Seric } 77416914Seric 77516914Seric /* append it to the token list */ 77617174Seric for (avp = --hbrvp; *xpvp != NULL; xpvp++) 77717174Seric { 77817174Seric *avp++ = newstr(*xpvp); 77916920Seric if (avp >= &npvp[MAXATOM]) 78016914Seric goto toolong; 78117174Seric } 78216914Seric 78316914Seric /* restore the old trailing information */ 78417177Seric for (xpvp = pvpb1; (*avp++ = *xpvp++) != NULL; ) 78516920Seric if (avp >= &npvp[MAXATOM]) 78616914Seric goto toolong; 78717174Seric 78817174Seric break; 78916914Seric } 79016914Seric 79116914Seric /* 79216914Seric ** Check for subroutine calls. 79316914Seric */ 79416914Seric 79516889Seric if (**npvp == CALLSUBR) 7969374Seric { 79716889Seric bcopy((char *) &npvp[2], (char *) pvp, 79816900Seric (int) (avp - npvp - 2) * sizeof *avp); 79916889Seric # ifdef DEBUG 80016889Seric if (tTd(21, 3)) 80116889Seric printf("-----callsubr %s\n", npvp[1]); 80216889Seric # endif DEBUG 80316889Seric rewrite(pvp, atoi(npvp[1])); 8043149Seric } 8053149Seric else 8063149Seric { 80717348Seric bcopy((char *) npvp, (char *) pvp, 80816900Seric (int) (avp - npvp) * sizeof *avp); 8099374Seric } 8104100Seric # ifdef DEBUG 8119374Seric if (tTd(21, 4)) 8129374Seric { 8139374Seric printf("rewritten as:"); 8149374Seric printav(pvp); 8159374Seric } 8164100Seric # endif DEBUG 817297Seric } 8188069Seric 8199279Seric if (OpMode == MD_TEST || tTd(21, 2)) 8208069Seric { 8218959Seric printf("rewrite: ruleset %2d returns:", ruleset); 8228069Seric printav(pvp); 8238069Seric } 8243149Seric } 8253149Seric /* 8263149Seric ** BUILDADDR -- build address from token vector. 8273149Seric ** 8283149Seric ** Parameters: 8293149Seric ** tv -- token vector. 8303149Seric ** a -- pointer to address descriptor to fill. 8313149Seric ** If NULL, one will be allocated. 8323149Seric ** 8333149Seric ** Returns: 8344279Seric ** NULL if there was an error. 8354279Seric ** 'a' otherwise. 8363149Seric ** 8373149Seric ** Side Effects: 8383149Seric ** fills in 'a' 8393149Seric */ 8403149Seric 8413149Seric ADDRESS * 8423149Seric buildaddr(tv, a) 8433149Seric register char **tv; 8443149Seric register ADDRESS *a; 8453149Seric { 8463149Seric static char buf[MAXNAME]; 8473149Seric struct mailer **mp; 8483149Seric register struct mailer *m; 8494635Seric extern bool sameword(); 8503149Seric 8513149Seric if (a == NULL) 8523149Seric a = (ADDRESS *) xalloc(sizeof *a); 85316889Seric bzero((char *) a, sizeof *a); 8543149Seric 8553149Seric /* figure out what net/mailer to use */ 8563149Seric if (**tv != CANONNET) 8574279Seric { 8583149Seric syserr("buildaddr: no net"); 8594279Seric return (NULL); 8604279Seric } 8613149Seric tv++; 8624635Seric if (sameword(*tv, "error")) 8634279Seric { 86410183Seric if (**++tv == CANONHOST) 86510183Seric { 86610183Seric setstat(atoi(*++tv)); 86710183Seric tv++; 86810183Seric } 86910183Seric if (**tv != CANONUSER) 8704279Seric syserr("buildaddr: error: no user"); 8714279Seric buf[0] = '\0'; 8724279Seric while (*++tv != NULL) 8734279Seric { 8744279Seric if (buf[0] != '\0') 8757005Seric (void) strcat(buf, " "); 8767005Seric (void) strcat(buf, *tv); 8774279Seric } 8784279Seric usrerr(buf); 8794279Seric return (NULL); 8804279Seric } 8814598Seric for (mp = Mailer; (m = *mp++) != NULL; ) 8823149Seric { 8834635Seric if (sameword(m->m_name, *tv)) 8843149Seric break; 8853149Seric } 8863149Seric if (m == NULL) 8874279Seric { 8883149Seric syserr("buildaddr: unknown net %s", *tv); 8894279Seric return (NULL); 8904279Seric } 8914598Seric a->q_mailer = m; 8923149Seric 8933149Seric /* figure out what host (if any) */ 8943149Seric tv++; 89510690Seric if (!bitnset(M_LOCAL, m->m_flags)) 8963149Seric { 8975704Seric if (**tv++ != CANONHOST) 8984279Seric { 8993149Seric syserr("buildaddr: no host"); 9004279Seric return (NULL); 9014279Seric } 9025704Seric buf[0] = '\0'; 9035704Seric while (*tv != NULL && **tv != CANONUSER) 9047005Seric (void) strcat(buf, *tv++); 9055704Seric a->q_host = newstr(buf); 9063149Seric } 9073149Seric else 9083149Seric a->q_host = NULL; 9093149Seric 9103149Seric /* figure out the user */ 9113149Seric if (**tv != CANONUSER) 9124279Seric { 9133149Seric syserr("buildaddr: no user"); 9144279Seric return (NULL); 9154279Seric } 91619040Seric 91719040Seric /* rewrite according recipient mailer rewriting rules */ 91819040Seric rewrite(++tv, 2); 91919040Seric if (m->m_r_rwset > 0) 92019040Seric rewrite(tv, m->m_r_rwset); 92119040Seric rewrite(tv, 4); 92219040Seric 92319040Seric /* save the result for the command line/RCPT argument */ 92411278Seric cataddr(tv, buf, sizeof buf); 9253149Seric a->q_user = buf; 9263149Seric 9273149Seric return (a); 9283149Seric } 9293188Seric /* 9304228Seric ** CATADDR -- concatenate pieces of addresses (putting in <LWSP> subs) 9314228Seric ** 9324228Seric ** Parameters: 9334228Seric ** pvp -- parameter vector to rebuild. 9344228Seric ** buf -- buffer to build the string into. 9354228Seric ** sz -- size of buf. 9364228Seric ** 9374228Seric ** Returns: 9384228Seric ** none. 9394228Seric ** 9404228Seric ** Side Effects: 9414228Seric ** Destroys buf. 9424228Seric */ 9434228Seric 9444228Seric cataddr(pvp, buf, sz) 9454228Seric char **pvp; 9464228Seric char *buf; 9474228Seric register int sz; 9484228Seric { 9494228Seric bool oatomtok = FALSE; 9504228Seric bool natomtok = FALSE; 9514228Seric register int i; 9524228Seric register char *p; 9534228Seric 9548423Seric if (pvp == NULL) 9558423Seric { 9568423Seric strcpy(buf, ""); 9578423Seric return; 9588423Seric } 9594228Seric p = buf; 96011156Seric sz -= 2; 9614228Seric while (*pvp != NULL && (i = strlen(*pvp)) < sz) 9624228Seric { 9638078Seric natomtok = (toktype(**pvp) == ATM); 9644228Seric if (oatomtok && natomtok) 9659042Seric *p++ = SpaceSub; 9664228Seric (void) strcpy(p, *pvp); 9674228Seric oatomtok = natomtok; 9684228Seric p += i; 96911156Seric sz -= i + 1; 9704228Seric pvp++; 9714228Seric } 9724228Seric *p = '\0'; 9734228Seric } 9744228Seric /* 9753188Seric ** SAMEADDR -- Determine if two addresses are the same 9763188Seric ** 9773188Seric ** This is not just a straight comparison -- if the mailer doesn't 9783188Seric ** care about the host we just ignore it, etc. 9793188Seric ** 9803188Seric ** Parameters: 9813188Seric ** a, b -- pointers to the internal forms to compare. 9823188Seric ** 9833188Seric ** Returns: 9843188Seric ** TRUE -- they represent the same mailbox. 9853188Seric ** FALSE -- they don't. 9863188Seric ** 9873188Seric ** Side Effects: 9883188Seric ** none. 9893188Seric */ 9903188Seric 9913188Seric bool 9929374Seric sameaddr(a, b) 9933188Seric register ADDRESS *a; 9943188Seric register ADDRESS *b; 9953188Seric { 9963188Seric /* if they don't have the same mailer, forget it */ 9973188Seric if (a->q_mailer != b->q_mailer) 9983188Seric return (FALSE); 9993188Seric 10003188Seric /* if the user isn't the same, we can drop out */ 10019374Seric if (strcmp(a->q_user, b->q_user) != 0) 10023188Seric return (FALSE); 10033188Seric 10043188Seric /* if the mailer ignores hosts, we have succeeded! */ 100510690Seric if (bitnset(M_LOCAL, a->q_mailer->m_flags)) 10063188Seric return (TRUE); 10073188Seric 10083188Seric /* otherwise compare hosts (but be careful for NULL ptrs) */ 10093188Seric if (a->q_host == NULL || b->q_host == NULL) 10103188Seric return (FALSE); 10113188Seric if (strcmp(a->q_host, b->q_host) != 0) 10123188Seric return (FALSE); 10133188Seric 10143188Seric return (TRUE); 10153188Seric } 10163234Seric /* 10173234Seric ** PRINTADDR -- print address (for debugging) 10183234Seric ** 10193234Seric ** Parameters: 10203234Seric ** a -- the address to print 10213234Seric ** follow -- follow the q_next chain. 10223234Seric ** 10233234Seric ** Returns: 10243234Seric ** none. 10253234Seric ** 10263234Seric ** Side Effects: 10273234Seric ** none. 10283234Seric */ 10293234Seric 10304317Seric # ifdef DEBUG 10314317Seric 10323234Seric printaddr(a, follow) 10333234Seric register ADDRESS *a; 10343234Seric bool follow; 10353234Seric { 10365001Seric bool first = TRUE; 10375001Seric 10383234Seric while (a != NULL) 10393234Seric { 10405001Seric first = FALSE; 10414443Seric printf("%x=", a); 10424085Seric (void) fflush(stdout); 10433234Seric printf("%s: mailer %d (%s), host `%s', user `%s'\n", a->q_paddr, 10448181Seric a->q_mailer->m_mno, a->q_mailer->m_name, a->q_host, 10458181Seric a->q_user); 10468181Seric printf("\tnext=%x, flags=%o, alias %x\n", a->q_next, a->q_flags, 10478181Seric a->q_alias); 10488181Seric printf("\thome=\"%s\", fullname=\"%s\"\n", a->q_home, 10498181Seric a->q_fullname); 10504996Seric 10513234Seric if (!follow) 10523234Seric return; 10534996Seric a = a->q_next; 10543234Seric } 10555001Seric if (first) 10564443Seric printf("[NULL]\n"); 10573234Seric } 10584317Seric 10594317Seric # endif DEBUG 10607682Seric /* 10617682Seric ** REMOTENAME -- return the name relative to the current mailer 10627682Seric ** 10637682Seric ** Parameters: 10647682Seric ** name -- the name to translate. 10658069Seric ** m -- the mailer that we want to do rewriting relative 10668069Seric ** to. 10678069Seric ** senderaddress -- if set, uses the sender rewriting rules 10688069Seric ** rather than the recipient rewriting rules. 106910310Seric ** canonical -- if set, strip out any comment information, 107010310Seric ** etc. 10717682Seric ** 10727682Seric ** Returns: 10737682Seric ** the text string representing this address relative to 10747682Seric ** the receiving mailer. 10757682Seric ** 10767682Seric ** Side Effects: 10777682Seric ** none. 10787682Seric ** 10797682Seric ** Warnings: 10807682Seric ** The text string returned is tucked away locally; 10817682Seric ** copy it if you intend to save it. 10827682Seric */ 10837682Seric 10847682Seric char * 108510310Seric remotename(name, m, senderaddress, canonical) 10867682Seric char *name; 10877682Seric struct mailer *m; 10888069Seric bool senderaddress; 108910310Seric bool canonical; 10907682Seric { 10918069Seric register char **pvp; 10928069Seric char *fancy; 10938069Seric extern char *macvalue(); 10948181Seric char *oldg = macvalue('g', CurEnv); 10957682Seric static char buf[MAXNAME]; 10967682Seric char lbuf[MAXNAME]; 109716914Seric char pvpbuf[PSBUFSIZE]; 10987682Seric extern char **prescan(); 10997889Seric extern char *crackaddr(); 11007682Seric 11017755Seric # ifdef DEBUG 11027755Seric if (tTd(12, 1)) 11037755Seric printf("remotename(%s)\n", name); 11047755Seric # endif DEBUG 11057755Seric 110610177Seric /* don't do anything if we are tagging it as special */ 110710177Seric if ((senderaddress ? m->m_s_rwset : m->m_r_rwset) < 0) 110810177Seric return (name); 110910177Seric 11107682Seric /* 11118181Seric ** Do a heuristic crack of this name to extract any comment info. 11128181Seric ** This will leave the name as a comment and a $g macro. 11137889Seric */ 11147889Seric 111510310Seric if (canonical) 111616155Seric fancy = "\001g"; 111710310Seric else 111810310Seric fancy = crackaddr(name); 11197889Seric 11208181Seric /* 11218181Seric ** Turn the name into canonical form. 11228181Seric ** Normally this will be RFC 822 style, i.e., "user@domain". 11238181Seric ** If this only resolves to "user", and the "C" flag is 11248181Seric ** specified in the sending mailer, then the sender's 11258181Seric ** domain will be appended. 11268181Seric */ 11278181Seric 112816914Seric pvp = prescan(name, '\0', pvpbuf); 11297889Seric if (pvp == NULL) 11307889Seric return (name); 11318181Seric rewrite(pvp, 3); 11328181Seric if (CurEnv->e_fromdomain != NULL) 11338181Seric { 11348181Seric /* append from domain to this address */ 11358181Seric register char **pxp = pvp; 11368181Seric 11379594Seric /* see if there is an "@domain" in the current name */ 11388181Seric while (*pxp != NULL && strcmp(*pxp, "@") != 0) 11398181Seric pxp++; 11408181Seric if (*pxp == NULL) 11418181Seric { 11429594Seric /* no.... append the "@domain" from the sender */ 11438181Seric register char **qxq = CurEnv->e_fromdomain; 11448181Seric 11459594Seric while ((*pxp++ = *qxq++) != NULL) 11469594Seric continue; 114711726Seric rewrite(pvp, 3); 11488181Seric } 11498181Seric } 11508181Seric 11518181Seric /* 11528959Seric ** Do more specific rewriting. 11538181Seric ** Rewrite using ruleset 1 or 2 depending on whether this is 11548181Seric ** a sender address or not. 11558181Seric ** Then run it through any receiving-mailer-specific rulesets. 11568181Seric */ 11578181Seric 11588069Seric if (senderaddress) 11597755Seric { 11607889Seric rewrite(pvp, 1); 11618069Seric if (m->m_s_rwset > 0) 11628069Seric rewrite(pvp, m->m_s_rwset); 11638069Seric } 11648069Seric else 11658069Seric { 11667889Seric rewrite(pvp, 2); 11678069Seric if (m->m_r_rwset > 0) 11688069Seric rewrite(pvp, m->m_r_rwset); 11697682Seric } 11707682Seric 11718181Seric /* 11728959Seric ** Do any final sanitation the address may require. 11738959Seric ** This will normally be used to turn internal forms 11748959Seric ** (e.g., user@host.LOCAL) into external form. This 11758959Seric ** may be used as a default to the above rules. 11768959Seric */ 11778959Seric 11788959Seric rewrite(pvp, 4); 11798959Seric 11808959Seric /* 11818181Seric ** Now restore the comment information we had at the beginning. 11828181Seric */ 11838181Seric 11847682Seric cataddr(pvp, lbuf, sizeof lbuf); 11859374Seric define('g', lbuf, CurEnv); 11867889Seric expand(fancy, buf, &buf[sizeof buf - 1], CurEnv); 11879374Seric define('g', oldg, CurEnv); 11887682Seric 11897682Seric # ifdef DEBUG 11907682Seric if (tTd(12, 1)) 11917755Seric printf("remotename => `%s'\n", buf); 11927682Seric # endif DEBUG 11937682Seric return (buf); 11947682Seric } 1195