122976Smiriam /* 2*34921Sbostic * Copyright (c) 1983 Eric P. Allman 333730Sbostic * Copyright (c) 1988 Regents of the University of California. 433730Sbostic * All rights reserved. 533730Sbostic * 633730Sbostic * Redistribution and use in source and binary forms are permitted 7*34921Sbostic * provided that the above copyright notice and this paragraph are 8*34921Sbostic * duplicated in all such forms and that any documentation, 9*34921Sbostic * advertising materials, and other materials related to such 10*34921Sbostic * distribution and use acknowledge that the software was developed 11*34921Sbostic * by the University of California, Berkeley. The name of the 12*34921Sbostic * University may not be used to endorse or promote products derived 13*34921Sbostic * from this software without specific prior written permission. 14*34921Sbostic * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 15*34921Sbostic * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 16*34921Sbostic * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 1733730Sbostic */ 1822976Smiriam 1922976Smiriam #ifndef lint 20*34921Sbostic static char sccsid[] = "@(#)parseaddr.c 5.9 (Berkeley) 06/30/88"; 2133730Sbostic #endif /* not lint */ 2222976Smiriam 233312Seric # include "sendmail.h" 24297Seric 25297Seric /* 269888Seric ** PARSEADDR -- Parse an address 27297Seric ** 28297Seric ** Parses an address and breaks it up into three parts: a 29297Seric ** net to transmit the message on, the host to transmit it 30297Seric ** to, and a user on that host. These are loaded into an 312973Seric ** ADDRESS header with the values squirreled away if necessary. 32297Seric ** The "user" part may not be a real user; the process may 33297Seric ** just reoccur on that machine. For example, on a machine 34297Seric ** with an arpanet connection, the address 35297Seric ** csvax.bill@berkeley 36297Seric ** will break up to a "user" of 'csvax.bill' and a host 37297Seric ** of 'berkeley' -- to be transmitted over the arpanet. 38297Seric ** 39297Seric ** Parameters: 40297Seric ** addr -- the address to parse. 41297Seric ** a -- a pointer to the address descriptor buffer. 42297Seric ** If NULL, a header will be created. 43297Seric ** copyf -- determines what shall be copied: 44297Seric ** -1 -- don't copy anything. The printname 45297Seric ** (q_paddr) is just addr, and the 46297Seric ** user & host are allocated internally 47297Seric ** to parse. 48297Seric ** 0 -- copy out the parsed user & host, but 49297Seric ** don't copy the printname. 50297Seric ** +1 -- copy everything. 5111445Seric ** delim -- the character to terminate the address, passed 5211445Seric ** to prescan. 53297Seric ** 54297Seric ** Returns: 55297Seric ** A pointer to the address descriptor header (`a' if 56297Seric ** `a' is non-NULL). 57297Seric ** NULL on error. 58297Seric ** 59297Seric ** Side Effects: 60297Seric ** none 61297Seric */ 62297Seric 639374Seric /* following delimiters are inherent to the internal algorithms */ 6416155Seric # define DELIMCHARS "\001()<>,;\\\"\r\n" /* word delimiters */ 652091Seric 662973Seric ADDRESS * 6711445Seric parseaddr(addr, a, copyf, delim) 68297Seric char *addr; 692973Seric register ADDRESS *a; 70297Seric int copyf; 7111445Seric char delim; 72297Seric { 733149Seric register char **pvp; 743149Seric register struct mailer *m; 7516914Seric char pvpbuf[PSBUFSIZE]; 763149Seric extern char **prescan(); 773149Seric extern ADDRESS *buildaddr(); 78297Seric 79297Seric /* 80297Seric ** Initialize and prescan address. 81297Seric */ 82297Seric 836903Seric CurEnv->e_to = addr; 843188Seric # ifdef DEBUG 857675Seric if (tTd(20, 1)) 869888Seric printf("\n--parseaddr(%s)\n", addr); 873188Seric # endif DEBUG 883188Seric 8916914Seric pvp = prescan(addr, delim, pvpbuf); 903149Seric if (pvp == NULL) 91297Seric return (NULL); 92297Seric 93297Seric /* 943149Seric ** Apply rewriting rules. 957889Seric ** Ruleset 0 does basic parsing. It must resolve. 96297Seric */ 97297Seric 988181Seric rewrite(pvp, 3); 994070Seric rewrite(pvp, 0); 100297Seric 1013149Seric /* 1023149Seric ** See if we resolved to a real mailer. 1033149Seric */ 104297Seric 1053149Seric if (pvp[0][0] != CANONNET) 1063149Seric { 1073149Seric setstat(EX_USAGE); 1083149Seric usrerr("cannot resolve name"); 1093149Seric return (NULL); 110297Seric } 111297Seric 112297Seric /* 1133149Seric ** Build canonical address from pvp. 114297Seric */ 115297Seric 1163149Seric a = buildaddr(pvp, a); 1174279Seric if (a == NULL) 1184279Seric return (NULL); 1194598Seric m = a->q_mailer; 120297Seric 121297Seric /* 1223149Seric ** Make local copies of the host & user and then 1233149Seric ** transport them out. 124297Seric */ 125297Seric 126297Seric if (copyf > 0) 1278078Seric { 1288078Seric extern char *DelimChar; 1298078Seric char savec = *DelimChar; 1308078Seric 1318078Seric *DelimChar = '\0'; 1322973Seric a->q_paddr = newstr(addr); 1338078Seric *DelimChar = savec; 1348078Seric } 135297Seric else 136297Seric a->q_paddr = addr; 13724944Seric 13824944Seric if (a->q_user == NULL) 13924944Seric a->q_user = ""; 14024944Seric if (a->q_host == NULL) 14124944Seric a->q_host = ""; 14224944Seric 1433149Seric if (copyf >= 0) 144297Seric { 14524944Seric a->q_host = newstr(a->q_host); 1463149Seric if (a->q_user != a->q_paddr) 1473149Seric a->q_user = newstr(a->q_user); 148297Seric } 149297Seric 150297Seric /* 15116202Seric ** Convert host name to lower case if requested. 15216202Seric ** User name will be done later. 15316202Seric */ 15416202Seric 15516202Seric if (!bitnset(M_HST_UPPER, m->m_flags)) 15616202Seric makelower(a->q_host); 15716202Seric 15816202Seric /* 159297Seric ** Compute return value. 160297Seric */ 161297Seric 162297Seric # ifdef DEBUG 1637675Seric if (tTd(20, 1)) 1644443Seric { 1659888Seric printf("parseaddr-->"); 1664443Seric printaddr(a, FALSE); 1674443Seric } 168297Seric # endif DEBUG 169297Seric 170297Seric return (a); 171297Seric } 172297Seric /* 17316162Seric ** LOWERADDR -- map UPPER->lower case on addresses as requested. 17416162Seric ** 17516162Seric ** Parameters: 17616162Seric ** a -- address to be mapped. 17716162Seric ** 17816162Seric ** Returns: 17916162Seric ** none. 18016162Seric ** 18116162Seric ** Side Effects: 18216162Seric ** none. 18316162Seric */ 18416162Seric 18516162Seric loweraddr(a) 18616162Seric register ADDRESS *a; 18716162Seric { 18816162Seric register MAILER *m = a->q_mailer; 18916162Seric 19016162Seric if (!bitnset(M_USR_UPPER, m->m_flags)) 19116162Seric makelower(a->q_user); 19216162Seric } 19316162Seric /* 194297Seric ** PRESCAN -- Prescan name and make it canonical 195297Seric ** 1969374Seric ** Scans a name and turns it into a set of tokens. This process 1979374Seric ** deletes blanks and comments (in parentheses). 198297Seric ** 199297Seric ** This routine knows about quoted strings and angle brackets. 200297Seric ** 201297Seric ** There are certain subtleties to this routine. The one that 202297Seric ** comes to mind now is that backslashes on the ends of names 203297Seric ** are silently stripped off; this is intentional. The problem 204297Seric ** is that some versions of sndmsg (like at LBL) set the kill 205297Seric ** character to something other than @ when reading addresses; 206297Seric ** so people type "csvax.eric\@berkeley" -- which screws up the 207297Seric ** berknet mailer. 208297Seric ** 209297Seric ** Parameters: 210297Seric ** addr -- the name to chomp. 211297Seric ** delim -- the delimiter for the address, normally 212297Seric ** '\0' or ','; \0 is accepted in any case. 21315284Seric ** If '\t' then we are reading the .cf file. 21416914Seric ** pvpbuf -- place to put the saved text -- note that 21516914Seric ** the pointers are static. 216297Seric ** 217297Seric ** Returns: 2183149Seric ** A pointer to a vector of tokens. 219297Seric ** NULL on error. 220297Seric ** 221297Seric ** Side Effects: 22225279Seric ** sets DelimChar to point to the character matching 'delim'. 223297Seric */ 224297Seric 2258078Seric /* states and character types */ 2268078Seric # define OPR 0 /* operator */ 2278078Seric # define ATM 1 /* atom */ 2288078Seric # define QST 2 /* in quoted string */ 2298078Seric # define SPC 3 /* chewing up spaces */ 2308078Seric # define ONE 4 /* pick up one character */ 2313149Seric 2328078Seric # define NSTATES 5 /* number of states */ 2338078Seric # define TYPE 017 /* mask to select state type */ 2348078Seric 2358078Seric /* meta bits for table */ 2368078Seric # define M 020 /* meta character; don't pass through */ 2378078Seric # define B 040 /* cause a break */ 2388078Seric # define MB M|B /* meta-break */ 2398078Seric 2408078Seric static short StateTab[NSTATES][NSTATES] = 2418078Seric { 2428087Seric /* oldst chtype> OPR ATM QST SPC ONE */ 2439051Seric /*OPR*/ OPR|B, ATM|B, QST|B, SPC|MB, ONE|B, 2449051Seric /*ATM*/ OPR|B, ATM, QST|B, SPC|MB, ONE|B, 2459051Seric /*QST*/ QST, QST, OPR, QST, QST, 2468078Seric /*SPC*/ OPR, ATM, QST, SPC|M, ONE, 2478078Seric /*ONE*/ OPR, OPR, OPR, OPR, OPR, 2488078Seric }; 2498078Seric 2508078Seric # define NOCHAR -1 /* signal nothing in lookahead token */ 2518078Seric 2528078Seric char *DelimChar; /* set to point to the delimiter */ 2538078Seric 2543149Seric char ** 25516914Seric prescan(addr, delim, pvpbuf) 256297Seric char *addr; 257297Seric char delim; 25816914Seric char pvpbuf[]; 259297Seric { 260297Seric register char *p; 2618078Seric register char *q; 2629346Seric register int c; 2633149Seric char **avp; 264297Seric bool bslashmode; 265297Seric int cmntcnt; 2668423Seric int anglecnt; 2673149Seric char *tok; 2688078Seric int state; 2698078Seric int newstate; 2708078Seric static char *av[MAXATOM+1]; 27115253Seric extern int errno; 272297Seric 27315253Seric /* make sure error messages don't have garbage on them */ 27415253Seric errno = 0; 27515253Seric 27616914Seric q = pvpbuf; 2773149Seric bslashmode = FALSE; 2787800Seric cmntcnt = 0; 2798423Seric anglecnt = 0; 2803149Seric avp = av; 2818078Seric state = OPR; 2828078Seric c = NOCHAR; 2838078Seric p = addr; 2848078Seric # ifdef DEBUG 2858078Seric if (tTd(22, 45)) 286297Seric { 2878078Seric printf("prescan: "); 2888078Seric xputs(p); 28923109Seric (void) putchar('\n'); 2908078Seric } 2918078Seric # endif DEBUG 2928078Seric 2938078Seric do 2948078Seric { 2953149Seric /* read a token */ 2963149Seric tok = q; 2978078Seric for (;;) 298297Seric { 2998078Seric /* store away any old lookahead character */ 3008078Seric if (c != NOCHAR) 3018078Seric { 30215284Seric /* see if there is room */ 30316914Seric if (q >= &pvpbuf[PSBUFSIZE - 5]) 3048078Seric { 3058078Seric usrerr("Address too long"); 3068078Seric DelimChar = p; 3078078Seric return (NULL); 3088078Seric } 30915284Seric 31015284Seric /* squirrel it away */ 3118078Seric *q++ = c; 3128078Seric } 3138078Seric 3148078Seric /* read a new input character */ 3158078Seric c = *p++; 3168078Seric if (c == '\0') 3178078Seric break; 31815284Seric c &= ~0200; 31915284Seric 3208078Seric # ifdef DEBUG 3218078Seric if (tTd(22, 101)) 3228078Seric printf("c=%c, s=%d; ", c, state); 3238078Seric # endif DEBUG 3248078Seric 3253149Seric /* chew up special characters */ 3263149Seric *q = '\0'; 3273149Seric if (bslashmode) 3283149Seric { 32924944Seric /* kludge \! for naive users */ 33024944Seric if (c != '!') 33124944Seric c |= 0200; 3323149Seric bslashmode = FALSE; 3333149Seric } 3343149Seric else if (c == '\\') 3353149Seric { 3363149Seric bslashmode = TRUE; 3378078Seric c = NOCHAR; 3383149Seric } 3398514Seric else if (state == QST) 3408514Seric { 3418514Seric /* do nothing, just avoid next clauses */ 3428514Seric } 3438078Seric else if (c == '(') 3444100Seric { 3458078Seric cmntcnt++; 3468078Seric c = NOCHAR; 3474100Seric } 3488078Seric else if (c == ')') 3493149Seric { 3508078Seric if (cmntcnt <= 0) 3513149Seric { 3528078Seric usrerr("Unbalanced ')'"); 3538078Seric DelimChar = p; 3548078Seric return (NULL); 3553149Seric } 3568078Seric else 3578078Seric cmntcnt--; 3588078Seric } 3598078Seric else if (cmntcnt > 0) 3608078Seric c = NOCHAR; 3618423Seric else if (c == '<') 3628423Seric anglecnt++; 3638423Seric else if (c == '>') 3648423Seric { 3658423Seric if (anglecnt <= 0) 3668423Seric { 3678423Seric usrerr("Unbalanced '>'"); 3688423Seric DelimChar = p; 3698423Seric return (NULL); 3708423Seric } 3718423Seric anglecnt--; 3728423Seric } 37311423Seric else if (delim == ' ' && isspace(c)) 37411423Seric c = ' '; 3753149Seric 3768078Seric if (c == NOCHAR) 3778078Seric continue; 3783149Seric 3798078Seric /* see if this is end of input */ 38011405Seric if (c == delim && anglecnt <= 0 && state != QST) 3813149Seric break; 3823149Seric 3838078Seric newstate = StateTab[state][toktype(c)]; 3848078Seric # ifdef DEBUG 3858078Seric if (tTd(22, 101)) 3868078Seric printf("ns=%02o\n", newstate); 3878078Seric # endif DEBUG 3888078Seric state = newstate & TYPE; 3898078Seric if (bitset(M, newstate)) 3908078Seric c = NOCHAR; 3918078Seric if (bitset(B, newstate)) 3924228Seric break; 393297Seric } 3943149Seric 3953149Seric /* new token */ 3968078Seric if (tok != q) 3971378Seric { 3988078Seric *q++ = '\0'; 3998078Seric # ifdef DEBUG 4008078Seric if (tTd(22, 36)) 401297Seric { 4028078Seric printf("tok="); 4038078Seric xputs(tok); 40423109Seric (void) putchar('\n'); 405297Seric } 4068078Seric # endif DEBUG 4078078Seric if (avp >= &av[MAXATOM]) 408297Seric { 4098078Seric syserr("prescan: too many tokens"); 4108078Seric DelimChar = p; 4118078Seric return (NULL); 412297Seric } 4138078Seric *avp++ = tok; 414297Seric } 4158423Seric } while (c != '\0' && (c != delim || anglecnt > 0)); 4163149Seric *avp = NULL; 4178078Seric DelimChar = --p; 4183149Seric if (cmntcnt > 0) 4193149Seric usrerr("Unbalanced '('"); 4208423Seric else if (anglecnt > 0) 4218423Seric usrerr("Unbalanced '<'"); 4228078Seric else if (state == QST) 4233149Seric usrerr("Unbalanced '\"'"); 4243149Seric else if (av[0] != NULL) 4253149Seric return (av); 4263149Seric return (NULL); 4273149Seric } 4283149Seric /* 4293149Seric ** TOKTYPE -- return token type 4303149Seric ** 4313149Seric ** Parameters: 4323149Seric ** c -- the character in question. 4333149Seric ** 4343149Seric ** Returns: 4353149Seric ** Its type. 4363149Seric ** 4373149Seric ** Side Effects: 4383149Seric ** none. 4393149Seric */ 440297Seric 4413149Seric toktype(c) 4423149Seric register char c; 4433149Seric { 4443380Seric static char buf[50]; 4453382Seric static bool firstime = TRUE; 4463380Seric 4473382Seric if (firstime) 4483380Seric { 4493382Seric firstime = FALSE; 45016155Seric expand("\001o", buf, &buf[sizeof buf - 1], CurEnv); 4517005Seric (void) strcat(buf, DELIMCHARS); 4523380Seric } 4539585Seric if (c == MATCHCLASS || c == MATCHREPL || c == MATCHNCLASS) 4548078Seric return (ONE); 4558078Seric if (c == '"') 4568078Seric return (QST); 4574100Seric if (!isascii(c)) 4588078Seric return (ATM); 4598078Seric if (isspace(c) || c == ')') 4608078Seric return (SPC); 4613380Seric if (iscntrl(c) || index(buf, c) != NULL) 4628078Seric return (OPR); 4638078Seric return (ATM); 4643149Seric } 4653149Seric /* 4663149Seric ** REWRITE -- apply rewrite rules to token vector. 4673149Seric ** 4684476Seric ** This routine is an ordered production system. Each rewrite 4694476Seric ** rule has a LHS (called the pattern) and a RHS (called the 4704476Seric ** rewrite); 'rwr' points the the current rewrite rule. 4714476Seric ** 4724476Seric ** For each rewrite rule, 'avp' points the address vector we 4734476Seric ** are trying to match against, and 'pvp' points to the pattern. 4748058Seric ** If pvp points to a special match value (MATCHZANY, MATCHANY, 4759585Seric ** MATCHONE, MATCHCLASS, MATCHNCLASS) then the address in avp 4769585Seric ** matched is saved away in the match vector (pointed to by 'mvp'). 4774476Seric ** 4784476Seric ** When a match between avp & pvp does not match, we try to 4799585Seric ** back out. If we back up over MATCHONE, MATCHCLASS, or MATCHNCLASS 4804476Seric ** we must also back out the match in mvp. If we reach a 4818058Seric ** MATCHANY or MATCHZANY we just extend the match and start 4828058Seric ** over again. 4834476Seric ** 4844476Seric ** When we finally match, we rewrite the address vector 4854476Seric ** and try over again. 4864476Seric ** 4873149Seric ** Parameters: 4883149Seric ** pvp -- pointer to token vector. 4893149Seric ** 4903149Seric ** Returns: 4913149Seric ** none. 4923149Seric ** 4933149Seric ** Side Effects: 4943149Seric ** pvp is modified. 4953149Seric */ 4962091Seric 4973149Seric struct match 4983149Seric { 4994468Seric char **first; /* first token matched */ 5004468Seric char **last; /* last token matched */ 5013149Seric }; 5023149Seric 5034468Seric # define MAXMATCH 9 /* max params per rewrite */ 5043149Seric 5053149Seric 5064070Seric rewrite(pvp, ruleset) 5073149Seric char **pvp; 5084070Seric int ruleset; 5093149Seric { 5103149Seric register char *ap; /* address pointer */ 5113149Seric register char *rp; /* rewrite pointer */ 5123149Seric register char **avp; /* address vector pointer */ 5133149Seric register char **rvp; /* rewrite vector pointer */ 5148058Seric register struct match *mlp; /* cur ptr into mlist */ 5158058Seric register struct rewrite *rwr; /* pointer to current rewrite rule */ 5164468Seric struct match mlist[MAXMATCH]; /* stores match on LHS */ 5173149Seric char *npvp[MAXATOM+1]; /* temporary space for rebuild */ 5183149Seric 5199279Seric if (OpMode == MD_TEST || tTd(21, 2)) 5203149Seric { 5218959Seric printf("rewrite: ruleset %2d input:", ruleset); 5223149Seric printav(pvp); 5233149Seric } 5248423Seric if (pvp == NULL) 5258423Seric return; 5263149Seric 5273149Seric /* 5283149Seric ** Run through the list of rewrite rules, applying 5293149Seric ** any that match. 5303149Seric */ 5313149Seric 5324070Seric for (rwr = RewriteRules[ruleset]; rwr != NULL; ) 5333149Seric { 5344100Seric # ifdef DEBUG 5357675Seric if (tTd(21, 12)) 536297Seric { 5378069Seric printf("-----trying rule:"); 5383149Seric printav(rwr->r_lhs); 5393149Seric } 5404100Seric # endif DEBUG 5413149Seric 5423149Seric /* try to match on this rule */ 5434468Seric mlp = mlist; 5448058Seric rvp = rwr->r_lhs; 5458058Seric avp = pvp; 5468058Seric while ((ap = *avp) != NULL || *rvp != NULL) 5473149Seric { 5483149Seric rp = *rvp; 5498058Seric # ifdef DEBUG 5508058Seric if (tTd(21, 35)) 5518058Seric { 5528069Seric printf("ap="); 5538058Seric xputs(ap); 5548069Seric printf(", rp="); 5558058Seric xputs(rp); 5568069Seric printf("\n"); 5578058Seric } 5588058Seric # endif DEBUG 5593149Seric if (rp == NULL) 560297Seric { 5613149Seric /* end-of-pattern before end-of-address */ 5628058Seric goto backup; 563297Seric } 5648058Seric if (ap == NULL && *rp != MATCHZANY) 5658058Seric { 5668058Seric /* end-of-input */ 5678058Seric break; 5688058Seric } 5693149Seric 5703149Seric switch (*rp) 5713149Seric { 5724060Seric register STAB *s; 5734060Seric 5744060Seric case MATCHCLASS: 5759585Seric case MATCHNCLASS: 5769585Seric /* match any token in (not in) a class */ 5774100Seric s = stab(ap, ST_CLASS, ST_FIND); 57810690Seric if (s == NULL || !bitnset(rp[1], s->s_class)) 5799585Seric { 5809585Seric if (*rp == MATCHCLASS) 5819585Seric goto backup; 5829585Seric } 5839585Seric else if (*rp == MATCHNCLASS) 5848058Seric goto backup; 5854468Seric 5864476Seric /* explicit fall-through */ 5874476Seric 5884476Seric case MATCHONE: 5894476Seric case MATCHANY: 5904476Seric /* match exactly one token */ 5918058Seric mlp->first = avp; 5928058Seric mlp->last = avp++; 5934468Seric mlp++; 5944060Seric break; 5954060Seric 5968058Seric case MATCHZANY: 5978058Seric /* match zero or more tokens */ 5988058Seric mlp->first = avp; 5998058Seric mlp->last = avp - 1; 6008058Seric mlp++; 6018058Seric break; 6028058Seric 6033149Seric default: 6043149Seric /* must have exact match */ 60533725Sbostic if (strcasecmp(rp, ap)) 6068058Seric goto backup; 6074468Seric avp++; 6083149Seric break; 6093149Seric } 6103149Seric 6113149Seric /* successful match on this token */ 6123149Seric rvp++; 6133149Seric continue; 6143149Seric 6158058Seric backup: 6163149Seric /* match failed -- back up */ 6173149Seric while (--rvp >= rwr->r_lhs) 6183149Seric { 6193149Seric rp = *rvp; 6208058Seric if (*rp == MATCHANY || *rp == MATCHZANY) 6214468Seric { 6224476Seric /* extend binding and continue */ 6238058Seric avp = ++mlp[-1].last; 6248058Seric avp++; 6254476Seric rvp++; 6263149Seric break; 6274468Seric } 6284476Seric avp--; 6299585Seric if (*rp == MATCHONE || *rp == MATCHCLASS || 6309585Seric *rp == MATCHNCLASS) 6313149Seric { 6324468Seric /* back out binding */ 6334468Seric mlp--; 6343149Seric } 6353149Seric } 6363149Seric 6373149Seric if (rvp < rwr->r_lhs) 6383149Seric { 6393149Seric /* total failure to match */ 6403149Seric break; 6413149Seric } 642297Seric } 6433149Seric 6443149Seric /* 6453149Seric ** See if we successfully matched 6463149Seric */ 6473149Seric 6489374Seric if (rvp < rwr->r_lhs || *rvp != NULL) 6493149Seric { 6504100Seric # ifdef DEBUG 6519374Seric if (tTd(21, 10)) 6529374Seric printf("----- rule fails\n"); 6534100Seric # endif DEBUG 6549374Seric rwr = rwr->r_next; 6559374Seric continue; 6569374Seric } 6573149Seric 6589374Seric rvp = rwr->r_rhs; 6599374Seric # ifdef DEBUG 6609374Seric if (tTd(21, 12)) 6619374Seric { 6629374Seric printf("-----rule matches:"); 6639374Seric printav(rvp); 6649374Seric } 6659374Seric # endif DEBUG 6669374Seric 6679374Seric rp = *rvp; 6689374Seric if (*rp == CANONUSER) 6699374Seric { 6709374Seric rvp++; 6719374Seric rwr = rwr->r_next; 6729374Seric } 6739374Seric else if (*rp == CANONHOST) 6749374Seric { 6759374Seric rvp++; 6769374Seric rwr = NULL; 6779374Seric } 6789374Seric else if (*rp == CANONNET) 6799374Seric rwr = NULL; 6809374Seric 6819374Seric /* substitute */ 6829374Seric for (avp = npvp; *rvp != NULL; rvp++) 6839374Seric { 6849374Seric register struct match *m; 6859374Seric register char **pp; 6869374Seric 6878058Seric rp = *rvp; 68816914Seric if (*rp == MATCHREPL) 6898058Seric { 69016914Seric /* substitute from LHS */ 69116914Seric m = &mlist[rp[1] - '1']; 69216914Seric if (m >= mlp) 6939374Seric { 69416914Seric syserr("rewrite: ruleset %d: replacement out of bounds", ruleset); 6959374Seric return; 6969374Seric } 6979374Seric # ifdef DEBUG 69816914Seric if (tTd(21, 15)) 69916914Seric { 70016914Seric printf("$%c:", rp[1]); 70116914Seric pp = m->first; 70216914Seric while (pp <= m->last) 70316914Seric { 70416914Seric printf(" %x=\"", *pp); 70516914Seric (void) fflush(stdout); 70616914Seric printf("%s\"", *pp++); 70716914Seric } 70816914Seric printf("\n"); 70916914Seric } 71016914Seric # endif DEBUG 7119374Seric pp = m->first; 7129374Seric while (pp <= m->last) 7133149Seric { 71416914Seric if (avp >= &npvp[MAXATOM]) 71516914Seric { 71616914Seric syserr("rewrite: expansion too long"); 71716914Seric return; 71816914Seric } 71916914Seric *avp++ = *pp++; 7203149Seric } 7213149Seric } 72216914Seric else 7238226Seric { 72416914Seric /* vanilla replacement */ 7259374Seric if (avp >= &npvp[MAXATOM]) 72616889Seric { 72716914Seric toolong: 72816889Seric syserr("rewrite: expansion too long"); 72916889Seric return; 73016889Seric } 73116914Seric *avp++ = rp; 7328226Seric } 7339374Seric } 7349374Seric *avp++ = NULL; 73516914Seric 73616914Seric /* 73716914Seric ** Check for any hostname lookups. 73816914Seric */ 73916914Seric 74016914Seric for (rvp = npvp; *rvp != NULL; rvp++) 74116914Seric { 74216914Seric char **hbrvp; 74316914Seric char **xpvp; 74416914Seric int trsize; 74517473Seric char *olddelimchar; 74616920Seric char buf[MAXNAME + 1]; 74716914Seric char *pvpb1[MAXATOM + 1]; 74817174Seric char pvpbuf[PSBUFSIZE]; 74917473Seric extern char *DelimChar; 75016914Seric 75116914Seric if (**rvp != HOSTBEGIN) 75216914Seric continue; 75316914Seric 75416914Seric /* 75516914Seric ** Got a hostname lookup. 75616914Seric ** 75716914Seric ** This could be optimized fairly easily. 75816914Seric */ 75916914Seric 76016914Seric hbrvp = rvp; 76116914Seric 76216914Seric /* extract the match part */ 76316914Seric while (*++rvp != NULL && **rvp != HOSTEND) 76416914Seric continue; 76516914Seric if (*rvp != NULL) 76616914Seric *rvp++ = NULL; 76716914Seric 76816914Seric /* save the remainder of the input string */ 76916914Seric trsize = (int) (avp - rvp + 1) * sizeof *rvp; 77016914Seric bcopy((char *) rvp, (char *) pvpb1, trsize); 77116914Seric 77216914Seric /* look it up */ 77316914Seric cataddr(++hbrvp, buf, sizeof buf); 77416914Seric maphostname(buf, sizeof buf); 77516914Seric 77616914Seric /* scan the new host name */ 77717473Seric olddelimchar = DelimChar; 77816914Seric xpvp = prescan(buf, '\0', pvpbuf); 77917473Seric DelimChar = olddelimchar; 78016914Seric if (xpvp == NULL) 78116914Seric { 78216914Seric syserr("rewrite: cannot prescan canonical hostname: %s", buf); 78322976Smiriam return; 78416914Seric } 78516914Seric 78616914Seric /* append it to the token list */ 78717174Seric for (avp = --hbrvp; *xpvp != NULL; xpvp++) 78817174Seric { 78917174Seric *avp++ = newstr(*xpvp); 79016920Seric if (avp >= &npvp[MAXATOM]) 79116914Seric goto toolong; 79217174Seric } 79316914Seric 79416914Seric /* restore the old trailing information */ 79517177Seric for (xpvp = pvpb1; (*avp++ = *xpvp++) != NULL; ) 79616920Seric if (avp >= &npvp[MAXATOM]) 79716914Seric goto toolong; 79817174Seric 79917174Seric break; 80016914Seric } 80116914Seric 80216914Seric /* 80316914Seric ** Check for subroutine calls. 80416914Seric */ 80516914Seric 80624944Seric if (*npvp != NULL && **npvp == CALLSUBR) 8079374Seric { 80816889Seric bcopy((char *) &npvp[2], (char *) pvp, 80916900Seric (int) (avp - npvp - 2) * sizeof *avp); 81016889Seric # ifdef DEBUG 81116889Seric if (tTd(21, 3)) 81216889Seric printf("-----callsubr %s\n", npvp[1]); 81316889Seric # endif DEBUG 81416889Seric rewrite(pvp, atoi(npvp[1])); 8153149Seric } 8163149Seric else 8173149Seric { 81817348Seric bcopy((char *) npvp, (char *) pvp, 81916900Seric (int) (avp - npvp) * sizeof *avp); 8209374Seric } 8214100Seric # ifdef DEBUG 8229374Seric if (tTd(21, 4)) 8239374Seric { 8249374Seric printf("rewritten as:"); 8259374Seric printav(pvp); 8269374Seric } 8274100Seric # endif DEBUG 828297Seric } 8298069Seric 8309279Seric if (OpMode == MD_TEST || tTd(21, 2)) 8318069Seric { 8328959Seric printf("rewrite: ruleset %2d returns:", ruleset); 8338069Seric printav(pvp); 8348069Seric } 8353149Seric } 8363149Seric /* 8373149Seric ** BUILDADDR -- build address from token vector. 8383149Seric ** 8393149Seric ** Parameters: 8403149Seric ** tv -- token vector. 8413149Seric ** a -- pointer to address descriptor to fill. 8423149Seric ** If NULL, one will be allocated. 8433149Seric ** 8443149Seric ** Returns: 8454279Seric ** NULL if there was an error. 8464279Seric ** 'a' otherwise. 8473149Seric ** 8483149Seric ** Side Effects: 8493149Seric ** fills in 'a' 8503149Seric */ 8513149Seric 8523149Seric ADDRESS * 8533149Seric buildaddr(tv, a) 8543149Seric register char **tv; 8553149Seric register ADDRESS *a; 8563149Seric { 8573149Seric static char buf[MAXNAME]; 8583149Seric struct mailer **mp; 8593149Seric register struct mailer *m; 8603149Seric 8613149Seric if (a == NULL) 8623149Seric a = (ADDRESS *) xalloc(sizeof *a); 86316889Seric bzero((char *) a, sizeof *a); 8643149Seric 8653149Seric /* figure out what net/mailer to use */ 8663149Seric if (**tv != CANONNET) 8674279Seric { 8683149Seric syserr("buildaddr: no net"); 8694279Seric return (NULL); 8704279Seric } 8713149Seric tv++; 87233725Sbostic if (!strcasecmp(*tv, "error")) 8734279Seric { 87410183Seric if (**++tv == CANONHOST) 87510183Seric { 87610183Seric setstat(atoi(*++tv)); 87710183Seric tv++; 87810183Seric } 87910183Seric if (**tv != CANONUSER) 8804279Seric syserr("buildaddr: error: no user"); 8814279Seric buf[0] = '\0'; 8824279Seric while (*++tv != NULL) 8834279Seric { 8844279Seric if (buf[0] != '\0') 8857005Seric (void) strcat(buf, " "); 8867005Seric (void) strcat(buf, *tv); 8874279Seric } 8884279Seric usrerr(buf); 8894279Seric return (NULL); 8904279Seric } 8914598Seric for (mp = Mailer; (m = *mp++) != NULL; ) 8923149Seric { 89333725Sbostic if (!strcasecmp(m->m_name, *tv)) 8943149Seric break; 8953149Seric } 8963149Seric if (m == NULL) 8974279Seric { 89824944Seric syserr("buildaddr: unknown mailer %s", *tv); 8994279Seric return (NULL); 9004279Seric } 9014598Seric a->q_mailer = m; 9023149Seric 9033149Seric /* figure out what host (if any) */ 9043149Seric tv++; 90510690Seric if (!bitnset(M_LOCAL, m->m_flags)) 9063149Seric { 9075704Seric if (**tv++ != CANONHOST) 9084279Seric { 9093149Seric syserr("buildaddr: no host"); 9104279Seric return (NULL); 9114279Seric } 9125704Seric buf[0] = '\0'; 9135704Seric while (*tv != NULL && **tv != CANONUSER) 9147005Seric (void) strcat(buf, *tv++); 9155704Seric a->q_host = newstr(buf); 9163149Seric } 9173149Seric else 9183149Seric a->q_host = NULL; 9193149Seric 9203149Seric /* figure out the user */ 9213149Seric if (**tv != CANONUSER) 9224279Seric { 9233149Seric syserr("buildaddr: no user"); 9244279Seric return (NULL); 9254279Seric } 92619040Seric 92719040Seric /* rewrite according recipient mailer rewriting rules */ 92819040Seric rewrite(++tv, 2); 92919040Seric if (m->m_r_rwset > 0) 93019040Seric rewrite(tv, m->m_r_rwset); 93119040Seric rewrite(tv, 4); 93219040Seric 93319040Seric /* save the result for the command line/RCPT argument */ 93411278Seric cataddr(tv, buf, sizeof buf); 9353149Seric a->q_user = buf; 9363149Seric 9373149Seric return (a); 9383149Seric } 9393188Seric /* 9404228Seric ** CATADDR -- concatenate pieces of addresses (putting in <LWSP> subs) 9414228Seric ** 9424228Seric ** Parameters: 9434228Seric ** pvp -- parameter vector to rebuild. 9444228Seric ** buf -- buffer to build the string into. 9454228Seric ** sz -- size of buf. 9464228Seric ** 9474228Seric ** Returns: 9484228Seric ** none. 9494228Seric ** 9504228Seric ** Side Effects: 9514228Seric ** Destroys buf. 9524228Seric */ 9534228Seric 9544228Seric cataddr(pvp, buf, sz) 9554228Seric char **pvp; 9564228Seric char *buf; 9574228Seric register int sz; 9584228Seric { 9594228Seric bool oatomtok = FALSE; 9604228Seric bool natomtok = FALSE; 9614228Seric register int i; 9624228Seric register char *p; 9634228Seric 9648423Seric if (pvp == NULL) 9658423Seric { 96623109Seric (void) strcpy(buf, ""); 9678423Seric return; 9688423Seric } 9694228Seric p = buf; 97011156Seric sz -= 2; 9714228Seric while (*pvp != NULL && (i = strlen(*pvp)) < sz) 9724228Seric { 9738078Seric natomtok = (toktype(**pvp) == ATM); 9744228Seric if (oatomtok && natomtok) 9759042Seric *p++ = SpaceSub; 9764228Seric (void) strcpy(p, *pvp); 9774228Seric oatomtok = natomtok; 9784228Seric p += i; 97911156Seric sz -= i + 1; 9804228Seric pvp++; 9814228Seric } 9824228Seric *p = '\0'; 9834228Seric } 9844228Seric /* 9853188Seric ** SAMEADDR -- Determine if two addresses are the same 9863188Seric ** 9873188Seric ** This is not just a straight comparison -- if the mailer doesn't 9883188Seric ** care about the host we just ignore it, etc. 9893188Seric ** 9903188Seric ** Parameters: 9913188Seric ** a, b -- pointers to the internal forms to compare. 9923188Seric ** 9933188Seric ** Returns: 9943188Seric ** TRUE -- they represent the same mailbox. 9953188Seric ** FALSE -- they don't. 9963188Seric ** 9973188Seric ** Side Effects: 9983188Seric ** none. 9993188Seric */ 10003188Seric 10013188Seric bool 10029374Seric sameaddr(a, b) 10033188Seric register ADDRESS *a; 10043188Seric register ADDRESS *b; 10053188Seric { 10063188Seric /* if they don't have the same mailer, forget it */ 10073188Seric if (a->q_mailer != b->q_mailer) 10083188Seric return (FALSE); 10093188Seric 10103188Seric /* if the user isn't the same, we can drop out */ 10119374Seric if (strcmp(a->q_user, b->q_user) != 0) 10123188Seric return (FALSE); 10133188Seric 10143188Seric /* if the mailer ignores hosts, we have succeeded! */ 101510690Seric if (bitnset(M_LOCAL, a->q_mailer->m_flags)) 10163188Seric return (TRUE); 10173188Seric 10183188Seric /* otherwise compare hosts (but be careful for NULL ptrs) */ 10193188Seric if (a->q_host == NULL || b->q_host == NULL) 10203188Seric return (FALSE); 10213188Seric if (strcmp(a->q_host, b->q_host) != 0) 10223188Seric return (FALSE); 10233188Seric 10243188Seric return (TRUE); 10253188Seric } 10263234Seric /* 10273234Seric ** PRINTADDR -- print address (for debugging) 10283234Seric ** 10293234Seric ** Parameters: 10303234Seric ** a -- the address to print 10313234Seric ** follow -- follow the q_next chain. 10323234Seric ** 10333234Seric ** Returns: 10343234Seric ** none. 10353234Seric ** 10363234Seric ** Side Effects: 10373234Seric ** none. 10383234Seric */ 10393234Seric 10404317Seric # ifdef DEBUG 10414317Seric 10423234Seric printaddr(a, follow) 10433234Seric register ADDRESS *a; 10443234Seric bool follow; 10453234Seric { 10465001Seric bool first = TRUE; 10475001Seric 10483234Seric while (a != NULL) 10493234Seric { 10505001Seric first = FALSE; 10514443Seric printf("%x=", a); 10524085Seric (void) fflush(stdout); 10533234Seric printf("%s: mailer %d (%s), host `%s', user `%s'\n", a->q_paddr, 10548181Seric a->q_mailer->m_mno, a->q_mailer->m_name, a->q_host, 10558181Seric a->q_user); 10568181Seric printf("\tnext=%x, flags=%o, alias %x\n", a->q_next, a->q_flags, 10578181Seric a->q_alias); 10588181Seric printf("\thome=\"%s\", fullname=\"%s\"\n", a->q_home, 10598181Seric a->q_fullname); 10604996Seric 10613234Seric if (!follow) 10623234Seric return; 10634996Seric a = a->q_next; 10643234Seric } 10655001Seric if (first) 10664443Seric printf("[NULL]\n"); 10673234Seric } 10684317Seric 10694317Seric # endif DEBUG 10707682Seric /* 10717682Seric ** REMOTENAME -- return the name relative to the current mailer 10727682Seric ** 10737682Seric ** Parameters: 10747682Seric ** name -- the name to translate. 10758069Seric ** m -- the mailer that we want to do rewriting relative 10768069Seric ** to. 10778069Seric ** senderaddress -- if set, uses the sender rewriting rules 10788069Seric ** rather than the recipient rewriting rules. 107910310Seric ** canonical -- if set, strip out any comment information, 108010310Seric ** etc. 10817682Seric ** 10827682Seric ** Returns: 10837682Seric ** the text string representing this address relative to 10847682Seric ** the receiving mailer. 10857682Seric ** 10867682Seric ** Side Effects: 10877682Seric ** none. 10887682Seric ** 10897682Seric ** Warnings: 10907682Seric ** The text string returned is tucked away locally; 10917682Seric ** copy it if you intend to save it. 10927682Seric */ 10937682Seric 10947682Seric char * 109510310Seric remotename(name, m, senderaddress, canonical) 10967682Seric char *name; 10977682Seric struct mailer *m; 10988069Seric bool senderaddress; 109910310Seric bool canonical; 11007682Seric { 11018069Seric register char **pvp; 11028069Seric char *fancy; 11038069Seric extern char *macvalue(); 11048181Seric char *oldg = macvalue('g', CurEnv); 11057682Seric static char buf[MAXNAME]; 11067682Seric char lbuf[MAXNAME]; 110716914Seric char pvpbuf[PSBUFSIZE]; 11087682Seric extern char **prescan(); 11097889Seric extern char *crackaddr(); 11107682Seric 11117755Seric # ifdef DEBUG 11127755Seric if (tTd(12, 1)) 11137755Seric printf("remotename(%s)\n", name); 11147755Seric # endif DEBUG 11157755Seric 111610177Seric /* don't do anything if we are tagging it as special */ 111710177Seric if ((senderaddress ? m->m_s_rwset : m->m_r_rwset) < 0) 111810177Seric return (name); 111910177Seric 11207682Seric /* 11218181Seric ** Do a heuristic crack of this name to extract any comment info. 11228181Seric ** This will leave the name as a comment and a $g macro. 11237889Seric */ 11247889Seric 112510310Seric if (canonical) 112616155Seric fancy = "\001g"; 112710310Seric else 112810310Seric fancy = crackaddr(name); 11297889Seric 11308181Seric /* 11318181Seric ** Turn the name into canonical form. 11328181Seric ** Normally this will be RFC 822 style, i.e., "user@domain". 11338181Seric ** If this only resolves to "user", and the "C" flag is 11348181Seric ** specified in the sending mailer, then the sender's 11358181Seric ** domain will be appended. 11368181Seric */ 11378181Seric 113816914Seric pvp = prescan(name, '\0', pvpbuf); 11397889Seric if (pvp == NULL) 11407889Seric return (name); 11418181Seric rewrite(pvp, 3); 11428181Seric if (CurEnv->e_fromdomain != NULL) 11438181Seric { 11448181Seric /* append from domain to this address */ 11458181Seric register char **pxp = pvp; 11468181Seric 11479594Seric /* see if there is an "@domain" in the current name */ 11488181Seric while (*pxp != NULL && strcmp(*pxp, "@") != 0) 11498181Seric pxp++; 11508181Seric if (*pxp == NULL) 11518181Seric { 11529594Seric /* no.... append the "@domain" from the sender */ 11538181Seric register char **qxq = CurEnv->e_fromdomain; 11548181Seric 11559594Seric while ((*pxp++ = *qxq++) != NULL) 11569594Seric continue; 115711726Seric rewrite(pvp, 3); 11588181Seric } 11598181Seric } 11608181Seric 11618181Seric /* 11628959Seric ** Do more specific rewriting. 11638181Seric ** Rewrite using ruleset 1 or 2 depending on whether this is 11648181Seric ** a sender address or not. 11658181Seric ** Then run it through any receiving-mailer-specific rulesets. 11668181Seric */ 11678181Seric 11688069Seric if (senderaddress) 11697755Seric { 11707889Seric rewrite(pvp, 1); 11718069Seric if (m->m_s_rwset > 0) 11728069Seric rewrite(pvp, m->m_s_rwset); 11738069Seric } 11748069Seric else 11758069Seric { 11767889Seric rewrite(pvp, 2); 11778069Seric if (m->m_r_rwset > 0) 11788069Seric rewrite(pvp, m->m_r_rwset); 11797682Seric } 11807682Seric 11818181Seric /* 11828959Seric ** Do any final sanitation the address may require. 11838959Seric ** This will normally be used to turn internal forms 11848959Seric ** (e.g., user@host.LOCAL) into external form. This 11858959Seric ** may be used as a default to the above rules. 11868959Seric */ 11878959Seric 11888959Seric rewrite(pvp, 4); 11898959Seric 11908959Seric /* 11918181Seric ** Now restore the comment information we had at the beginning. 11928181Seric */ 11938181Seric 11947682Seric cataddr(pvp, lbuf, sizeof lbuf); 11959374Seric define('g', lbuf, CurEnv); 11967889Seric expand(fancy, buf, &buf[sizeof buf - 1], CurEnv); 11979374Seric define('g', oldg, CurEnv); 11987682Seric 11997682Seric # ifdef DEBUG 12007682Seric if (tTd(12, 1)) 12017755Seric printf("remotename => `%s'\n", buf); 12027682Seric # endif DEBUG 12037682Seric return (buf); 12047682Seric } 1205