122976Smiriam /* 234921Sbostic * Copyright (c) 1983 Eric P. Allman 363589Sbostic * Copyright (c) 1988, 1993 463589Sbostic * The Regents of the University of California. All rights reserved. 533730Sbostic * 642828Sbostic * %sccs.include.redist.c% 733730Sbostic */ 822976Smiriam 922976Smiriam #ifndef lint 10*64740Seric static char sccsid[] = "@(#)parseaddr.c 8.14 (Berkeley) 10/22/93"; 1133730Sbostic #endif /* not lint */ 1222976Smiriam 1356678Seric # include "sendmail.h" 14297Seric 15297Seric /* 169888Seric ** PARSEADDR -- Parse an address 17297Seric ** 18297Seric ** Parses an address and breaks it up into three parts: a 19297Seric ** net to transmit the message on, the host to transmit it 20297Seric ** to, and a user on that host. These are loaded into an 212973Seric ** ADDRESS header with the values squirreled away if necessary. 22297Seric ** The "user" part may not be a real user; the process may 23297Seric ** just reoccur on that machine. For example, on a machine 24297Seric ** with an arpanet connection, the address 25297Seric ** csvax.bill@berkeley 26297Seric ** will break up to a "user" of 'csvax.bill' and a host 27297Seric ** of 'berkeley' -- to be transmitted over the arpanet. 28297Seric ** 29297Seric ** Parameters: 30297Seric ** addr -- the address to parse. 31297Seric ** a -- a pointer to the address descriptor buffer. 32297Seric ** If NULL, a header will be created. 3364284Seric ** flags -- describe detail for parsing. See RF_ definitions 3464284Seric ** in sendmail.h. 3511445Seric ** delim -- the character to terminate the address, passed 3611445Seric ** to prescan. 3758333Seric ** delimptr -- if non-NULL, set to the location of the 3858333Seric ** delim character that was found. 3956678Seric ** e -- the envelope that will contain this address. 40297Seric ** 41297Seric ** Returns: 42297Seric ** A pointer to the address descriptor header (`a' if 43297Seric ** `a' is non-NULL). 44297Seric ** NULL on error. 45297Seric ** 46297Seric ** Side Effects: 47297Seric ** none 48297Seric */ 49297Seric 509374Seric /* following delimiters are inherent to the internal algorithms */ 5159278Seric # define DELIMCHARS "()<>,;\r\n" /* default word delimiters */ 522091Seric 532973Seric ADDRESS * 5464284Seric parseaddr(addr, a, flags, delim, delimptr, e) 55297Seric char *addr; 562973Seric register ADDRESS *a; 5764284Seric int flags; 5859700Seric int delim; 5958333Seric char **delimptr; 6056678Seric register ENVELOPE *e; 61297Seric { 623149Seric register char **pvp; 6358333Seric auto char *delimptrbuf; 6459084Seric bool queueup; 6516914Seric char pvpbuf[PSBUFSIZE]; 6656678Seric extern ADDRESS *buildaddr(); 6757388Seric extern bool invalidaddr(); 68297Seric 69297Seric /* 70297Seric ** Initialize and prescan address. 71297Seric */ 72297Seric 7356678Seric e->e_to = addr; 747675Seric if (tTd(20, 1)) 759888Seric printf("\n--parseaddr(%s)\n", addr); 763188Seric 7758333Seric if (delimptr == NULL) 7858333Seric delimptr = &delimptrbuf; 7958333Seric 8058333Seric pvp = prescan(addr, delim, pvpbuf, delimptr); 813149Seric if (pvp == NULL) 8256729Seric { 8356729Seric if (tTd(20, 1)) 8456729Seric printf("parseaddr-->NULL\n"); 85297Seric return (NULL); 8656729Seric } 87297Seric 8864726Seric if (invalidaddr(addr, delim == '\0' ? NULL : *delimptr)) 8964726Seric { 9064726Seric if (tTd(20, 1)) 9164726Seric printf("parseaddr-->bad address\n"); 9264726Seric return NULL; 9364726Seric } 9464726Seric 95297Seric /* 9664348Seric ** Save addr if we are going to have to. 9764348Seric ** 9864348Seric ** We have to do this early because there is a chance that 9964348Seric ** the map lookups in the rewriting rules could clobber 10064348Seric ** static memory somewhere. 10164348Seric */ 10264348Seric 10364348Seric if (bitset(RF_COPYPADDR, flags) && addr != NULL) 10464348Seric { 10564348Seric char savec = **delimptr; 10664348Seric 10764348Seric if (savec != '\0') 10864348Seric **delimptr = '\0'; 10964348Seric addr = newstr(addr); 11064348Seric if (savec != '\0') 11164348Seric **delimptr = savec; 11264348Seric } 11364348Seric 11464348Seric /* 1153149Seric ** Apply rewriting rules. 1167889Seric ** Ruleset 0 does basic parsing. It must resolve. 117297Seric */ 118297Seric 11959084Seric queueup = FALSE; 12059084Seric if (rewrite(pvp, 3, e) == EX_TEMPFAIL) 12159084Seric queueup = TRUE; 12259084Seric if (rewrite(pvp, 0, e) == EX_TEMPFAIL) 12359084Seric queueup = TRUE; 124297Seric 125297Seric 126297Seric /* 1273149Seric ** Build canonical address from pvp. 128297Seric */ 129297Seric 13064284Seric a = buildaddr(pvp, a, flags, e); 131297Seric 132297Seric /* 1333149Seric ** Make local copies of the host & user and then 1343149Seric ** transport them out. 135297Seric */ 136297Seric 13764348Seric allocaddr(a, flags, addr); 13864306Seric if (bitset(QBADADDR, a->q_flags)) 13964306Seric return a; 14056678Seric 14156678Seric /* 14259084Seric ** If there was a parsing failure, mark it for queueing. 14359084Seric */ 14459084Seric 14559084Seric if (queueup) 14659595Seric { 14759734Seric char *msg = "Transient parse error -- message queued for future delivery"; 14859734Seric 14959595Seric if (tTd(20, 1)) 15059595Seric printf("parseaddr: queuing message\n"); 15159734Seric message(msg); 15259734Seric if (e->e_message == NULL) 15360009Seric e->e_message = newstr(msg); 15459084Seric a->q_flags |= QQUEUEUP; 15559595Seric } 15659084Seric 15759084Seric /* 15856678Seric ** Compute return value. 15956678Seric */ 16056678Seric 16156678Seric if (tTd(20, 1)) 1628078Seric { 16356678Seric printf("parseaddr-->"); 16456678Seric printaddr(a, FALSE); 16556678Seric } 16656678Seric 16756678Seric return (a); 16856678Seric } 16956678Seric /* 17057388Seric ** INVALIDADDR -- check for address containing meta-characters 17157388Seric ** 17257388Seric ** Parameters: 17357388Seric ** addr -- the address to check. 17457388Seric ** 17557388Seric ** Returns: 17657388Seric ** TRUE -- if the address has any "wierd" characters 17757388Seric ** FALSE -- otherwise. 17857388Seric */ 17957388Seric 18057388Seric bool 18164726Seric invalidaddr(addr, delimptr) 18257388Seric register char *addr; 18364726Seric char *delimptr; 18457388Seric { 18564726Seric char savedelim; 18664726Seric 18764726Seric if (delimptr != NULL) 18864726Seric savedelim = *delimptr; 18964726Seric #if 0 19064726Seric /* for testing.... */ 19164726Seric if (strcmp(addr, "INvalidADDR") == 0) 19257388Seric { 19364726Seric usrerr("553 INvalid ADDRess"); 19464726Seric if (delimptr != NULL) 19564726Seric *delimptr = savedelim; 19657388Seric return TRUE; 19757388Seric } 19864726Seric #endif 19964726Seric for (; *addr != '\0'; addr++) 20064726Seric { 20164726Seric if ((*addr & 0340) == 0200) 20264726Seric break; 20364726Seric } 20464726Seric if (delimptr != NULL) 20564726Seric *delimptr = savedelim; 20664726Seric if (*addr == '\0') 20764726Seric return FALSE; 20864726Seric setstat(EX_USAGE); 20964726Seric usrerr("553 Address contained invalid control characters"); 21064726Seric return TRUE; 21157388Seric } 21257388Seric /* 21356678Seric ** ALLOCADDR -- do local allocations of address on demand. 21456678Seric ** 21556678Seric ** Also lowercases the host name if requested. 21656678Seric ** 21756678Seric ** Parameters: 21856678Seric ** a -- the address to reallocate. 21964284Seric ** flags -- the copy flag (see RF_ definitions in sendmail.h 22064284Seric ** for a description). 22156678Seric ** paddr -- the printname of the address. 22256678Seric ** 22356678Seric ** Returns: 22456678Seric ** none. 22556678Seric ** 22656678Seric ** Side Effects: 22756678Seric ** Copies portions of a into local buffers as requested. 22856678Seric */ 22956678Seric 23064348Seric allocaddr(a, flags, paddr) 23156678Seric register ADDRESS *a; 23264284Seric int flags; 23356678Seric char *paddr; 23456678Seric { 23558673Seric if (tTd(24, 4)) 23664284Seric printf("allocaddr(flags=%o, paddr=%s)\n", flags, paddr); 23758673Seric 23864348Seric a->q_paddr = paddr; 2398078Seric 24024944Seric if (a->q_user == NULL) 24124944Seric a->q_user = ""; 24224944Seric if (a->q_host == NULL) 24324944Seric a->q_host = ""; 24424944Seric 24564284Seric if (bitset(RF_COPYPARSE, flags)) 246297Seric { 24724944Seric a->q_host = newstr(a->q_host); 2483149Seric if (a->q_user != a->q_paddr) 2493149Seric a->q_user = newstr(a->q_user); 250297Seric } 251297Seric 25256678Seric if (a->q_paddr == NULL) 25356678Seric a->q_paddr = a->q_user; 254297Seric } 255297Seric /* 256297Seric ** PRESCAN -- Prescan name and make it canonical 257297Seric ** 2589374Seric ** Scans a name and turns it into a set of tokens. This process 2599374Seric ** deletes blanks and comments (in parentheses). 260297Seric ** 261297Seric ** This routine knows about quoted strings and angle brackets. 262297Seric ** 263297Seric ** There are certain subtleties to this routine. The one that 264297Seric ** comes to mind now is that backslashes on the ends of names 265297Seric ** are silently stripped off; this is intentional. The problem 266297Seric ** is that some versions of sndmsg (like at LBL) set the kill 267297Seric ** character to something other than @ when reading addresses; 268297Seric ** so people type "csvax.eric\@berkeley" -- which screws up the 269297Seric ** berknet mailer. 270297Seric ** 271297Seric ** Parameters: 272297Seric ** addr -- the name to chomp. 273297Seric ** delim -- the delimiter for the address, normally 274297Seric ** '\0' or ','; \0 is accepted in any case. 27515284Seric ** If '\t' then we are reading the .cf file. 27616914Seric ** pvpbuf -- place to put the saved text -- note that 27716914Seric ** the pointers are static. 27858333Seric ** delimptr -- if non-NULL, set to the location of the 27958333Seric ** terminating delimiter. 280297Seric ** 281297Seric ** Returns: 2823149Seric ** A pointer to a vector of tokens. 283297Seric ** NULL on error. 284297Seric */ 285297Seric 2868078Seric /* states and character types */ 2878078Seric # define OPR 0 /* operator */ 2888078Seric # define ATM 1 /* atom */ 2898078Seric # define QST 2 /* in quoted string */ 2908078Seric # define SPC 3 /* chewing up spaces */ 2918078Seric # define ONE 4 /* pick up one character */ 2923149Seric 2938078Seric # define NSTATES 5 /* number of states */ 2948078Seric # define TYPE 017 /* mask to select state type */ 2958078Seric 2968078Seric /* meta bits for table */ 2978078Seric # define M 020 /* meta character; don't pass through */ 2988078Seric # define B 040 /* cause a break */ 2998078Seric # define MB M|B /* meta-break */ 3008078Seric 3018078Seric static short StateTab[NSTATES][NSTATES] = 3028078Seric { 3038087Seric /* oldst chtype> OPR ATM QST SPC ONE */ 3049051Seric /*OPR*/ OPR|B, ATM|B, QST|B, SPC|MB, ONE|B, 3059051Seric /*ATM*/ OPR|B, ATM, QST|B, SPC|MB, ONE|B, 3069051Seric /*QST*/ QST, QST, OPR, QST, QST, 3078078Seric /*SPC*/ OPR, ATM, QST, SPC|M, ONE, 3088078Seric /*ONE*/ OPR, OPR, OPR, OPR, OPR, 3098078Seric }; 3108078Seric 3118078Seric # define NOCHAR -1 /* signal nothing in lookahead token */ 3128078Seric 3133149Seric char ** 31458333Seric prescan(addr, delim, pvpbuf, delimptr) 315297Seric char *addr; 316297Seric char delim; 31716914Seric char pvpbuf[]; 31858333Seric char **delimptr; 319297Seric { 320297Seric register char *p; 3218078Seric register char *q; 3229346Seric register int c; 3233149Seric char **avp; 324297Seric bool bslashmode; 325297Seric int cmntcnt; 3268423Seric int anglecnt; 3273149Seric char *tok; 3288078Seric int state; 3298078Seric int newstate; 33059747Seric char *saveto = CurEnv->e_to; 3318078Seric static char *av[MAXATOM+1]; 33256678Seric extern int errno; 333297Seric 33415253Seric /* make sure error messages don't have garbage on them */ 33515253Seric errno = 0; 33615253Seric 33716914Seric q = pvpbuf; 3383149Seric bslashmode = FALSE; 3397800Seric cmntcnt = 0; 3408423Seric anglecnt = 0; 3413149Seric avp = av; 34256678Seric state = ATM; 3438078Seric c = NOCHAR; 3448078Seric p = addr; 34559747Seric CurEnv->e_to = p; 34656764Seric if (tTd(22, 11)) 347297Seric { 3488078Seric printf("prescan: "); 3498078Seric xputs(p); 35023109Seric (void) putchar('\n'); 3518078Seric } 3528078Seric 3538078Seric do 3548078Seric { 3553149Seric /* read a token */ 3563149Seric tok = q; 3578078Seric for (;;) 358297Seric { 3598078Seric /* store away any old lookahead character */ 36059277Seric if (c != NOCHAR && !bslashmode) 3618078Seric { 36215284Seric /* see if there is room */ 36316914Seric if (q >= &pvpbuf[PSBUFSIZE - 5]) 3648078Seric { 36558151Seric usrerr("553 Address too long"); 36658333Seric if (delimptr != NULL) 36758333Seric *delimptr = p; 36859747Seric CurEnv->e_to = saveto; 3698078Seric return (NULL); 3708078Seric } 37115284Seric 37215284Seric /* squirrel it away */ 3738078Seric *q++ = c; 3748078Seric } 3758078Seric 3768078Seric /* read a new input character */ 3778078Seric c = *p++; 37857631Seric if (c == '\0') 37956764Seric { 38056764Seric /* diagnose and patch up bad syntax */ 38156764Seric if (state == QST) 38256764Seric { 38363851Seric usrerr("653 Unbalanced '\"' (fixed)"); 38456764Seric c = '"'; 38556764Seric } 38656764Seric else if (cmntcnt > 0) 38756764Seric { 38863851Seric usrerr("653 Unbalanced '(' (fixed)"); 38956764Seric c = ')'; 39056764Seric } 39156764Seric else if (anglecnt > 0) 39256764Seric { 39356764Seric c = '>'; 39463851Seric usrerr("653 Unbalanced '<' (fixed)"); 39556764Seric } 39656764Seric else 39756764Seric break; 39815284Seric 39956764Seric p--; 40056764Seric } 40157631Seric else if (c == delim && anglecnt <= 0 && 40257631Seric cmntcnt <= 0 && state != QST) 40357631Seric break; 40456764Seric 4058078Seric if (tTd(22, 101)) 4068078Seric printf("c=%c, s=%d; ", c, state); 4078078Seric 4083149Seric /* chew up special characters */ 4093149Seric *q = '\0'; 4103149Seric if (bslashmode) 4113149Seric { 41259105Seric bslashmode = FALSE; 41359105Seric 41424944Seric /* kludge \! for naive users */ 41558061Seric if (cmntcnt > 0) 41659105Seric { 41758061Seric c = NOCHAR; 41859105Seric continue; 41959105Seric } 42059105Seric else if (c != '!' || state == QST) 42159105Seric { 42256678Seric *q++ = '\\'; 42359105Seric continue; 42459105Seric } 4253149Seric } 42656678Seric 42756678Seric if (c == '\\') 4283149Seric { 4293149Seric bslashmode = TRUE; 4303149Seric } 43156678Seric else if (state == QST) 4328514Seric { 4338514Seric /* do nothing, just avoid next clauses */ 4348514Seric } 4358078Seric else if (c == '(') 4364100Seric { 4378078Seric cmntcnt++; 4388078Seric c = NOCHAR; 4394100Seric } 4408078Seric else if (c == ')') 4413149Seric { 4428078Seric if (cmntcnt <= 0) 4433149Seric { 44463851Seric usrerr("653 Unbalanced ')' (fixed)"); 44563844Seric c = NOCHAR; 4463149Seric } 4478078Seric else 4488078Seric cmntcnt--; 4498078Seric } 4508078Seric else if (cmntcnt > 0) 4518078Seric c = NOCHAR; 4528423Seric else if (c == '<') 4538423Seric anglecnt++; 4548423Seric else if (c == '>') 4558423Seric { 4568423Seric if (anglecnt <= 0) 4578423Seric { 45863851Seric usrerr("653 Unbalanced '>' (fixed)"); 45963844Seric c = NOCHAR; 4608423Seric } 46163844Seric else 46263844Seric anglecnt--; 4638423Seric } 46458050Seric else if (delim == ' ' && isascii(c) && isspace(c)) 46511423Seric c = ' '; 4663149Seric 4678078Seric if (c == NOCHAR) 4688078Seric continue; 4693149Seric 4708078Seric /* see if this is end of input */ 47111405Seric if (c == delim && anglecnt <= 0 && state != QST) 4723149Seric break; 4733149Seric 4748078Seric newstate = StateTab[state][toktype(c)]; 4758078Seric if (tTd(22, 101)) 4768078Seric printf("ns=%02o\n", newstate); 4778078Seric state = newstate & TYPE; 4788078Seric if (bitset(M, newstate)) 4798078Seric c = NOCHAR; 4808078Seric if (bitset(B, newstate)) 4814228Seric break; 482297Seric } 4833149Seric 4843149Seric /* new token */ 4858078Seric if (tok != q) 4861378Seric { 4878078Seric *q++ = '\0'; 4888078Seric if (tTd(22, 36)) 489297Seric { 4908078Seric printf("tok="); 4918078Seric xputs(tok); 49223109Seric (void) putchar('\n'); 493297Seric } 4948078Seric if (avp >= &av[MAXATOM]) 495297Seric { 49658151Seric syserr("553 prescan: too many tokens"); 49758333Seric if (delimptr != NULL) 49858333Seric *delimptr = p; 49959747Seric CurEnv->e_to = saveto; 5008078Seric return (NULL); 501297Seric } 5028078Seric *avp++ = tok; 503297Seric } 5048423Seric } while (c != '\0' && (c != delim || anglecnt > 0)); 5053149Seric *avp = NULL; 50658333Seric p--; 50758333Seric if (delimptr != NULL) 50858333Seric *delimptr = p; 50956764Seric if (tTd(22, 12)) 51056764Seric { 51156764Seric printf("prescan==>"); 51256764Seric printav(av); 51356764Seric } 51459747Seric CurEnv->e_to = saveto; 51558546Seric if (av[0] == NULL) 51658546Seric return (NULL); 51758403Seric return (av); 5183149Seric } 5193149Seric /* 5203149Seric ** TOKTYPE -- return token type 5213149Seric ** 5223149Seric ** Parameters: 5233149Seric ** c -- the character in question. 5243149Seric ** 5253149Seric ** Returns: 5263149Seric ** Its type. 5273149Seric ** 5283149Seric ** Side Effects: 5293149Seric ** none. 5303149Seric */ 531297Seric 5323149Seric toktype(c) 53358050Seric register int c; 5343149Seric { 5353380Seric static char buf[50]; 5363382Seric static bool firstime = TRUE; 5373380Seric 5383382Seric if (firstime) 5393380Seric { 5403382Seric firstime = FALSE; 54158050Seric expand("\201o", buf, &buf[sizeof buf - 1], CurEnv); 5427005Seric (void) strcat(buf, DELIMCHARS); 5433380Seric } 54458050Seric c &= 0377; 54556327Seric if (c == MATCHCLASS || c == MATCHREPL || c == MATCHNCLASS) 5468078Seric return (ONE); 54759027Seric if (c == MACRODEXPAND) 54859027Seric return (ONE); 5498078Seric if (c == '"') 5508078Seric return (QST); 55158050Seric if ((c & 0340) == 0200) 55258050Seric return (OPR); 5534100Seric if (!isascii(c)) 5548078Seric return (ATM); 5558078Seric if (isspace(c) || c == ')') 5568078Seric return (SPC); 55758050Seric if (strchr(buf, c) != NULL) 5588078Seric return (OPR); 5598078Seric return (ATM); 5603149Seric } 5613149Seric /* 5623149Seric ** REWRITE -- apply rewrite rules to token vector. 5633149Seric ** 5644476Seric ** This routine is an ordered production system. Each rewrite 5654476Seric ** rule has a LHS (called the pattern) and a RHS (called the 5664476Seric ** rewrite); 'rwr' points the the current rewrite rule. 5674476Seric ** 5684476Seric ** For each rewrite rule, 'avp' points the address vector we 5694476Seric ** are trying to match against, and 'pvp' points to the pattern. 5708058Seric ** If pvp points to a special match value (MATCHZANY, MATCHANY, 5719585Seric ** MATCHONE, MATCHCLASS, MATCHNCLASS) then the address in avp 5729585Seric ** matched is saved away in the match vector (pointed to by 'mvp'). 5734476Seric ** 5744476Seric ** When a match between avp & pvp does not match, we try to 5759585Seric ** back out. If we back up over MATCHONE, MATCHCLASS, or MATCHNCLASS 5764476Seric ** we must also back out the match in mvp. If we reach a 5778058Seric ** MATCHANY or MATCHZANY we just extend the match and start 5788058Seric ** over again. 5794476Seric ** 5804476Seric ** When we finally match, we rewrite the address vector 5814476Seric ** and try over again. 5824476Seric ** 5833149Seric ** Parameters: 5843149Seric ** pvp -- pointer to token vector. 58559027Seric ** ruleset -- the ruleset to use for rewriting. 58659027Seric ** e -- the current envelope. 5873149Seric ** 5883149Seric ** Returns: 58959084Seric ** A status code. If EX_TEMPFAIL, higher level code should 59059084Seric ** attempt recovery. 5913149Seric ** 5923149Seric ** Side Effects: 5933149Seric ** pvp is modified. 5943149Seric */ 5952091Seric 5963149Seric struct match 5973149Seric { 5984468Seric char **first; /* first token matched */ 5994468Seric char **last; /* last token matched */ 60058825Seric char **pattern; /* pointer to pattern */ 6013149Seric }; 6023149Seric 6034468Seric # define MAXMATCH 9 /* max params per rewrite */ 6043149Seric 6053149Seric 60659084Seric int 60759027Seric rewrite(pvp, ruleset, e) 6083149Seric char **pvp; 6094070Seric int ruleset; 61059027Seric register ENVELOPE *e; 6113149Seric { 6123149Seric register char *ap; /* address pointer */ 6133149Seric register char *rp; /* rewrite pointer */ 6143149Seric register char **avp; /* address vector pointer */ 6153149Seric register char **rvp; /* rewrite vector pointer */ 6168058Seric register struct match *mlp; /* cur ptr into mlist */ 6178058Seric register struct rewrite *rwr; /* pointer to current rewrite rule */ 61858866Seric int ruleno; /* current rule number */ 61959084Seric int rstat = EX_OK; /* return status */ 620*64740Seric int loopcount; 62156678Seric struct match mlist[MAXMATCH]; /* stores match on LHS */ 6223149Seric char *npvp[MAXATOM+1]; /* temporary space for rebuild */ 6233149Seric 6249279Seric if (OpMode == MD_TEST || tTd(21, 2)) 6253149Seric { 6268959Seric printf("rewrite: ruleset %2d input:", ruleset); 62756678Seric printav(pvp); 6283149Seric } 62956678Seric if (ruleset < 0 || ruleset >= MAXRWSETS) 63056326Seric { 63158151Seric syserr("554 rewrite: illegal ruleset number %d", ruleset); 63259084Seric return EX_CONFIG; 63356326Seric } 63456678Seric if (pvp == NULL) 63559084Seric return EX_USAGE; 63656326Seric 6373149Seric /* 63856678Seric ** Run through the list of rewrite rules, applying 63956678Seric ** any that match. 6403149Seric */ 6413149Seric 64258866Seric ruleno = 1; 643*64740Seric loopcount = 0; 6444070Seric for (rwr = RewriteRules[ruleset]; rwr != NULL; ) 6453149Seric { 6467675Seric if (tTd(21, 12)) 647297Seric { 6488069Seric printf("-----trying rule:"); 64956678Seric printav(rwr->r_lhs); 6503149Seric } 6513149Seric 6523149Seric /* try to match on this rule */ 6534468Seric mlp = mlist; 6548058Seric rvp = rwr->r_lhs; 6558058Seric avp = pvp; 65658866Seric if (++loopcount > 100) 6573149Seric { 65858866Seric syserr("554 Infinite loop in ruleset %d, rule %d", 65958866Seric ruleset, ruleno); 66058866Seric if (tTd(21, 1)) 66152637Seric { 66256678Seric printf("workspace: "); 66356678Seric printav(pvp); 66452637Seric } 66558866Seric break; 66658866Seric } 66758866Seric 66858866Seric while ((ap = *avp) != NULL || *rvp != NULL) 66958866Seric { 6703149Seric rp = *rvp; 6718058Seric if (tTd(21, 35)) 6728058Seric { 67358825Seric printf("ADVANCE rp="); 67457531Seric xputs(rp); 67557532Seric printf(", ap="); 6768058Seric xputs(ap); 6778069Seric printf("\n"); 6788058Seric } 67956678Seric if (rp == NULL) 68056326Seric { 6813149Seric /* end-of-pattern before end-of-address */ 6828058Seric goto backup; 68356678Seric } 68458173Seric if (ap == NULL && (*rp & 0377) != MATCHZANY && 68558827Seric (*rp & 0377) != MATCHZERO) 68656326Seric { 68758825Seric /* end-of-input with patterns left */ 68858825Seric goto backup; 689297Seric } 69056326Seric 69158050Seric switch (*rp & 0377) 6928058Seric { 69356678Seric register STAB *s; 69458814Seric char buf[MAXLINE]; 69556326Seric 69656678Seric case MATCHCLASS: 69758825Seric /* match any phrase in a class */ 69858825Seric mlp->pattern = rvp; 69958814Seric mlp->first = avp; 70058814Seric extendclass: 70158825Seric ap = *avp; 70258825Seric if (ap == NULL) 70358814Seric goto backup; 70458814Seric mlp->last = avp++; 70558814Seric cataddr(mlp->first, mlp->last, buf, sizeof buf, '\0'); 70658814Seric s = stab(buf, ST_CLASS, ST_FIND); 70756678Seric if (s == NULL || !bitnset(rp[1], s->s_class)) 70856326Seric { 70958825Seric if (tTd(21, 36)) 71058825Seric { 71158825Seric printf("EXTEND rp="); 71258825Seric xputs(rp); 71358825Seric printf(", ap="); 71458825Seric xputs(ap); 71558825Seric printf("\n"); 71658825Seric } 71758825Seric goto extendclass; 71856326Seric } 71958825Seric if (tTd(21, 36)) 72058825Seric printf("CLMATCH\n"); 72158814Seric mlp++; 72258814Seric break; 7234060Seric 72458825Seric case MATCHNCLASS: 72558825Seric /* match any token not in a class */ 72658825Seric s = stab(ap, ST_CLASS, ST_FIND); 72758825Seric if (s != NULL && bitnset(rp[1], s->s_class)) 72858825Seric goto backup; 72958825Seric 73058825Seric /* fall through */ 73158825Seric 73256678Seric case MATCHONE: 73356678Seric case MATCHANY: 73456678Seric /* match exactly one token */ 73558825Seric mlp->pattern = rvp; 73656678Seric mlp->first = avp; 73756678Seric mlp->last = avp++; 7388058Seric mlp++; 73956678Seric break; 7408058Seric 74156678Seric case MATCHZANY: 74256678Seric /* match zero or more tokens */ 74358825Seric mlp->pattern = rvp; 74456678Seric mlp->first = avp; 74556678Seric mlp->last = avp - 1; 74656678Seric mlp++; 74756678Seric break; 74856326Seric 74958827Seric case MATCHZERO: 75058173Seric /* match zero tokens */ 75158173Seric break; 75258173Seric 75359027Seric case MACRODEXPAND: 75459027Seric /* 75559027Seric ** Match against run-time macro. 75659027Seric ** This algorithm is broken for the 75759027Seric ** general case (no recursive macros, 75859027Seric ** improper tokenization) but should 75959027Seric ** work for the usual cases. 76059027Seric */ 76159027Seric 76259027Seric ap = macvalue(rp[1], e); 76359027Seric mlp->first = avp; 76459027Seric if (tTd(21, 2)) 76559027Seric printf("rewrite: LHS $&%c => \"%s\"\n", 76659027Seric rp[1], 76759027Seric ap == NULL ? "(NULL)" : ap); 76859027Seric 76959027Seric if (ap == NULL) 77059027Seric break; 77160502Seric while (*ap != '\0') 77259027Seric { 77359027Seric if (*avp == NULL || 77459027Seric strncasecmp(ap, *avp, strlen(*avp)) != 0) 77559027Seric { 77659027Seric /* no match */ 77759027Seric avp = mlp->first; 77859027Seric goto backup; 77959027Seric } 78059027Seric ap += strlen(*avp++); 78159027Seric } 78259027Seric 78359027Seric /* match */ 78459027Seric break; 78559027Seric 78656678Seric default: 78756678Seric /* must have exact match */ 78856678Seric if (strcasecmp(rp, ap)) 7898058Seric goto backup; 7904468Seric avp++; 79156678Seric break; 7923149Seric } 7933149Seric 79456678Seric /* successful match on this token */ 7953149Seric rvp++; 7963149Seric continue; 7973149Seric 79858825Seric backup: 79956678Seric /* match failed -- back up */ 80058825Seric while (--mlp >= mlist) 8013149Seric { 80258825Seric rvp = mlp->pattern; 80356678Seric rp = *rvp; 80458825Seric avp = mlp->last + 1; 80558825Seric ap = *avp; 80658825Seric 80758825Seric if (tTd(21, 36)) 80858825Seric { 80958825Seric printf("BACKUP rp="); 81058825Seric xputs(rp); 81158825Seric printf(", ap="); 81258825Seric xputs(ap); 81358825Seric printf("\n"); 81458825Seric } 81558825Seric 81658825Seric if (ap == NULL) 81758825Seric { 81858825Seric /* run off the end -- back up again */ 81958825Seric continue; 82058825Seric } 82158050Seric if ((*rp & 0377) == MATCHANY || 82258050Seric (*rp & 0377) == MATCHZANY) 8234468Seric { 82456678Seric /* extend binding and continue */ 82558825Seric mlp->last = avp++; 82656678Seric rvp++; 82758825Seric mlp++; 82856678Seric break; 8294468Seric } 83058825Seric if ((*rp & 0377) == MATCHCLASS) 83156678Seric { 83258814Seric /* extend binding and try again */ 83363397Seric mlp->last = avp; 83458814Seric goto extendclass; 83558814Seric } 8363149Seric } 8373149Seric 83858825Seric if (mlp < mlist) 83956678Seric { 84056678Seric /* total failure to match */ 84156326Seric break; 8423149Seric } 843297Seric } 8443149Seric 8453149Seric /* 84656678Seric ** See if we successfully matched 8473149Seric */ 8483149Seric 84958827Seric if (mlp < mlist || *rvp != NULL) 8503149Seric { 8519374Seric if (tTd(21, 10)) 8529374Seric printf("----- rule fails\n"); 8539374Seric rwr = rwr->r_next; 85458866Seric ruleno++; 855*64740Seric loopcount = 0; 8569374Seric continue; 8579374Seric } 8583149Seric 8599374Seric rvp = rwr->r_rhs; 8609374Seric if (tTd(21, 12)) 8619374Seric { 8629374Seric printf("-----rule matches:"); 86356678Seric printav(rvp); 8649374Seric } 8659374Seric 8669374Seric rp = *rvp; 86758050Seric if ((*rp & 0377) == CANONUSER) 8689374Seric { 8699374Seric rvp++; 8709374Seric rwr = rwr->r_next; 87158866Seric ruleno++; 872*64740Seric loopcount = 0; 8739374Seric } 87458050Seric else if ((*rp & 0377) == CANONHOST) 8759374Seric { 8769374Seric rvp++; 8779374Seric rwr = NULL; 8789374Seric } 87958050Seric else if ((*rp & 0377) == CANONNET) 8809374Seric rwr = NULL; 8819374Seric 8829374Seric /* substitute */ 8839374Seric for (avp = npvp; *rvp != NULL; rvp++) 8849374Seric { 8859374Seric register struct match *m; 8869374Seric register char **pp; 8879374Seric 8888058Seric rp = *rvp; 88958050Seric if ((*rp & 0377) == MATCHREPL) 8908058Seric { 89116914Seric /* substitute from LHS */ 89216914Seric m = &mlist[rp[1] - '1']; 89356678Seric if (m < mlist || m >= mlp) 8949374Seric { 89558151Seric syserr("554 rewrite: ruleset %d: replacement $%c out of bounds", 89656326Seric ruleset, rp[1]); 89759084Seric return EX_CONFIG; 8989374Seric } 89916914Seric if (tTd(21, 15)) 90016914Seric { 90116914Seric printf("$%c:", rp[1]); 90216914Seric pp = m->first; 90356678Seric while (pp <= m->last) 90416914Seric { 90516914Seric printf(" %x=\"", *pp); 90616914Seric (void) fflush(stdout); 90716914Seric printf("%s\"", *pp++); 90816914Seric } 90916914Seric printf("\n"); 91016914Seric } 9119374Seric pp = m->first; 91256678Seric while (pp <= m->last) 9133149Seric { 91416914Seric if (avp >= &npvp[MAXATOM]) 91556678Seric { 91658151Seric syserr("554 rewrite: expansion too long"); 91759084Seric return EX_DATAERR; 91856678Seric } 91916914Seric *avp++ = *pp++; 9203149Seric } 9213149Seric } 92216914Seric else 9238226Seric { 92416914Seric /* vanilla replacement */ 9259374Seric if (avp >= &npvp[MAXATOM]) 92616889Seric { 92756678Seric toolong: 92858151Seric syserr("554 rewrite: expansion too long"); 92959084Seric return EX_DATAERR; 93016889Seric } 93159027Seric if ((*rp & 0377) != MACRODEXPAND) 93259027Seric *avp++ = rp; 93359027Seric else 93459027Seric { 93559027Seric *avp = macvalue(rp[1], e); 93659027Seric if (tTd(21, 2)) 93759027Seric printf("rewrite: RHS $&%c => \"%s\"\n", 93859027Seric rp[1], 93959027Seric *avp == NULL ? "(NULL)" : *avp); 94059027Seric if (*avp != NULL) 94159027Seric avp++; 94259027Seric } 9438226Seric } 9449374Seric } 9459374Seric *avp++ = NULL; 94616914Seric 94716914Seric /* 94856678Seric ** Check for any hostname/keyword lookups. 94916914Seric */ 95016914Seric 95116914Seric for (rvp = npvp; *rvp != NULL; rvp++) 95216914Seric { 95356678Seric char **hbrvp; 95416914Seric char **xpvp; 95516914Seric int trsize; 95656678Seric char *replac; 95756678Seric int endtoken; 95856678Seric STAB *map; 95956678Seric char *mapname; 96056678Seric char **key_rvp; 96156678Seric char **arg_rvp; 96256678Seric char **default_rvp; 96356678Seric char buf[MAXNAME + 1]; 96416914Seric char *pvpb1[MAXATOM + 1]; 96556823Seric char *argvect[10]; 96617174Seric char pvpbuf[PSBUFSIZE]; 96764404Seric char *nullpvp[1]; 96816914Seric 96958050Seric if ((**rvp & 0377) != HOSTBEGIN && 97058050Seric (**rvp & 0377) != LOOKUPBEGIN) 97116914Seric continue; 97216914Seric 97316914Seric /* 97456678Seric ** Got a hostname/keyword lookup. 97516914Seric ** 97616914Seric ** This could be optimized fairly easily. 97716914Seric */ 97816914Seric 97916914Seric hbrvp = rvp; 98058050Seric if ((**rvp & 0377) == HOSTBEGIN) 98156327Seric { 98256678Seric endtoken = HOSTEND; 98356678Seric mapname = "host"; 98456327Seric } 98556326Seric else 98656327Seric { 98756678Seric endtoken = LOOKUPEND; 98856678Seric mapname = *++rvp; 98956327Seric } 99056678Seric map = stab(mapname, ST_MAP, ST_FIND); 99156678Seric if (map == NULL) 99258151Seric syserr("554 rewrite: map %s not found", mapname); 99356678Seric 99456678Seric /* extract the match part */ 99556678Seric key_rvp = ++rvp; 99656823Seric default_rvp = NULL; 99756823Seric arg_rvp = argvect; 99856823Seric xpvp = NULL; 99956823Seric replac = pvpbuf; 100058050Seric while (*rvp != NULL && (**rvp & 0377) != endtoken) 100153654Seric { 100258050Seric int nodetype = **rvp & 0377; 100356823Seric 100456823Seric if (nodetype != CANONHOST && nodetype != CANONUSER) 100556678Seric { 100656823Seric rvp++; 100756823Seric continue; 100856823Seric } 100956823Seric 101056823Seric *rvp++ = NULL; 101156823Seric 101256823Seric if (xpvp != NULL) 101356823Seric { 101458814Seric cataddr(xpvp, NULL, replac, 101558082Seric &pvpbuf[sizeof pvpbuf] - replac, 101658082Seric '\0'); 101756823Seric *++arg_rvp = replac; 101856823Seric replac += strlen(replac) + 1; 101956823Seric xpvp = NULL; 102056823Seric } 102156823Seric switch (nodetype) 102256823Seric { 102356678Seric case CANONHOST: 102456823Seric xpvp = rvp; 102556678Seric break; 102656678Seric 102756678Seric case CANONUSER: 102856678Seric default_rvp = rvp; 102956678Seric break; 103056678Seric } 103153654Seric } 103216914Seric if (*rvp != NULL) 103316914Seric *rvp++ = NULL; 103456823Seric if (xpvp != NULL) 103556823Seric { 103658814Seric cataddr(xpvp, NULL, replac, 103758082Seric &pvpbuf[sizeof pvpbuf] - replac, 103858082Seric '\0'); 103956823Seric *++arg_rvp = replac; 104056823Seric } 104156823Seric *++arg_rvp = NULL; 104216914Seric 104316914Seric /* save the remainder of the input string */ 104416914Seric trsize = (int) (avp - rvp + 1) * sizeof *rvp; 104516914Seric bcopy((char *) rvp, (char *) pvpb1, trsize); 104616914Seric 104756678Seric /* look it up */ 104858814Seric cataddr(key_rvp, NULL, buf, sizeof buf, '\0'); 104956823Seric argvect[0] = buf; 105060538Seric if (map != NULL && bitset(MF_OPEN, map->s_map.map_mflags)) 105156836Seric { 105259084Seric auto int stat = EX_OK; 105356836Seric 105460215Seric /* XXX should try to auto-open the map here */ 105560215Seric 105658796Seric if (tTd(60, 1)) 105758796Seric printf("map_lookup(%s, %s) => ", 105858796Seric mapname, buf); 105956836Seric replac = (*map->s_map.map_class->map_lookup)(&map->s_map, 106060089Seric buf, argvect, &stat); 106158796Seric if (tTd(60, 1)) 106259084Seric printf("%s (%d)\n", 106359084Seric replac ? replac : "NOT FOUND", 106459084Seric stat); 106559084Seric 106659084Seric /* should recover if stat == EX_TEMPFAIL */ 106759084Seric if (stat == EX_TEMPFAIL) 106859084Seric rstat = stat; 106956836Seric } 107053654Seric else 107156678Seric replac = NULL; 107256678Seric 107356678Seric /* if no replacement, use default */ 107456823Seric if (replac == NULL && default_rvp != NULL) 107556823Seric { 107660089Seric /* create the default */ 107760089Seric cataddr(default_rvp, NULL, buf, sizeof buf, '\0'); 107856823Seric replac = buf; 107956823Seric } 108056823Seric 108156678Seric if (replac == NULL) 108251317Seric { 108356823Seric xpvp = key_rvp; 108453654Seric } 108564404Seric else if (*replac == '\0') 108664404Seric { 108764404Seric /* null replacement */ 108864404Seric nullpvp[0] = NULL; 108964404Seric xpvp = nullpvp; 109064404Seric } 109156678Seric else 109253654Seric { 109356678Seric /* scan the new replacement */ 109458333Seric xpvp = prescan(replac, '\0', pvpbuf, NULL); 109553654Seric if (xpvp == NULL) 109651317Seric { 109758403Seric /* prescan already printed error */ 109859084Seric return EX_DATAERR; 109956678Seric } 110051317Seric } 110151317Seric 110216914Seric /* append it to the token list */ 110356678Seric for (avp = hbrvp; *xpvp != NULL; xpvp++) 110456678Seric { 110517174Seric *avp++ = newstr(*xpvp); 110616920Seric if (avp >= &npvp[MAXATOM]) 110716914Seric goto toolong; 110817174Seric } 110916914Seric 111016914Seric /* restore the old trailing information */ 111156678Seric for (xpvp = pvpb1; (*avp++ = *xpvp++) != NULL; ) 111216920Seric if (avp >= &npvp[MAXATOM]) 111316914Seric goto toolong; 111417174Seric 111556678Seric break; 111616914Seric } 111716914Seric 111816914Seric /* 111916914Seric ** Check for subroutine calls. 112016914Seric */ 112116914Seric 112258050Seric if (*npvp != NULL && (**npvp & 0377) == CALLSUBR) 112356678Seric { 112459084Seric int stat; 112559084Seric 112656678Seric bcopy((char *) &npvp[2], (char *) pvp, 112756678Seric (int) (avp - npvp - 2) * sizeof *avp); 112856678Seric if (tTd(21, 3)) 112956678Seric printf("-----callsubr %s\n", npvp[1]); 113059084Seric stat = rewrite(pvp, atoi(npvp[1]), e); 113159084Seric if (rstat == EX_OK || stat == EX_TEMPFAIL) 113259084Seric rstat = stat; 113363581Seric if ((**pvp & 0377) == CANONNET) 113463581Seric rwr = NULL; 113556678Seric } 113656678Seric else 113756678Seric { 113817348Seric bcopy((char *) npvp, (char *) pvp, 113916900Seric (int) (avp - npvp) * sizeof *avp); 114056678Seric } 11419374Seric if (tTd(21, 4)) 11429374Seric { 11439374Seric printf("rewritten as:"); 114456678Seric printav(pvp); 11459374Seric } 1146297Seric } 11478069Seric 11489279Seric if (OpMode == MD_TEST || tTd(21, 2)) 11498069Seric { 11508959Seric printf("rewrite: ruleset %2d returns:", ruleset); 115156678Seric printav(pvp); 11528069Seric } 115359084Seric 115459084Seric return rstat; 11553149Seric } 11563149Seric /* 11573149Seric ** BUILDADDR -- build address from token vector. 11583149Seric ** 11593149Seric ** Parameters: 11603149Seric ** tv -- token vector. 11613149Seric ** a -- pointer to address descriptor to fill. 11623149Seric ** If NULL, one will be allocated. 116364284Seric ** flags -- info regarding whether this is a sender or 116464284Seric ** a recipient. 116558966Seric ** e -- the current envelope. 11663149Seric ** 11673149Seric ** Returns: 11684279Seric ** NULL if there was an error. 11694279Seric ** 'a' otherwise. 11703149Seric ** 11713149Seric ** Side Effects: 11723149Seric ** fills in 'a' 11733149Seric */ 11743149Seric 117557249Seric struct errcodes 117657249Seric { 117757249Seric char *ec_name; /* name of error code */ 117857249Seric int ec_code; /* numeric code */ 117957249Seric } ErrorCodes[] = 118057249Seric { 118157249Seric "usage", EX_USAGE, 118257249Seric "nouser", EX_NOUSER, 118357249Seric "nohost", EX_NOHOST, 118457249Seric "unavailable", EX_UNAVAILABLE, 118557249Seric "software", EX_SOFTWARE, 118657249Seric "tempfail", EX_TEMPFAIL, 118757249Seric "protocol", EX_PROTOCOL, 118857249Seric #ifdef EX_CONFIG 118957249Seric "config", EX_CONFIG, 119057249Seric #endif 119157249Seric NULL, EX_UNAVAILABLE, 119257249Seric }; 119357249Seric 119456678Seric ADDRESS * 119564284Seric buildaddr(tv, a, flags, e) 11963149Seric register char **tv; 11973149Seric register ADDRESS *a; 119864284Seric int flags; 119958966Seric register ENVELOPE *e; 12003149Seric { 12013149Seric struct mailer **mp; 12023149Seric register struct mailer *m; 120358008Seric char *bp; 120458008Seric int spaceleft; 120564306Seric static MAILER errormailer; 120664306Seric static char *errorargv[] = { "ERROR", NULL }; 120757402Seric static char buf[MAXNAME]; 12083149Seric 12093149Seric if (a == NULL) 12103149Seric a = (ADDRESS *) xalloc(sizeof *a); 121116889Seric bzero((char *) a, sizeof *a); 12123149Seric 12133149Seric /* figure out what net/mailer to use */ 121464306Seric if (*tv == NULL || (**tv & 0377) != CANONNET) 12154279Seric { 121658151Seric syserr("554 buildaddr: no net"); 121764306Seric badaddr: 121864306Seric a->q_flags |= QBADADDR; 121964306Seric a->q_mailer = &errormailer; 122064306Seric if (errormailer.m_name == NULL) 122164306Seric { 122264306Seric /* initialize the bogus mailer */ 122364306Seric errormailer.m_name = "*error*"; 122464306Seric errormailer.m_mailer = "ERROR"; 122564306Seric errormailer.m_argv = errorargv; 122664306Seric } 122764306Seric return a; 12284279Seric } 12293149Seric tv++; 123058680Seric if (strcasecmp(*tv, "error") == 0) 12314279Seric { 123258050Seric if ((**++tv & 0377) == CANONHOST) 123310183Seric { 123457249Seric register struct errcodes *ep; 123557249Seric 123658050Seric if (isascii(**++tv) && isdigit(**tv)) 123757249Seric { 123857249Seric setstat(atoi(*tv)); 123957249Seric } 124057249Seric else 124157249Seric { 124257249Seric for (ep = ErrorCodes; ep->ec_name != NULL; ep++) 124357249Seric if (strcasecmp(ep->ec_name, *tv) == 0) 124457249Seric break; 124557249Seric setstat(ep->ec_code); 124657249Seric } 124710183Seric tv++; 124810183Seric } 124958050Seric if ((**tv & 0377) != CANONUSER) 125058151Seric syserr("554 buildaddr: error: no user"); 125158814Seric cataddr(++tv, NULL, buf, sizeof buf, ' '); 125258082Seric stripquotes(buf); 125364659Seric if (isascii(buf[0]) && isdigit(buf[0]) && 125464659Seric isascii(buf[1]) && isdigit(buf[1]) && 125564659Seric isascii(buf[2]) && isdigit(buf[2]) && 125664659Seric buf[3] == ' ') 125764659Seric { 125864659Seric char fmt[10]; 125964659Seric 126064659Seric strncpy(fmt, buf, 3); 126164659Seric strcpy(&fmt[3], " %s"); 126264659Seric usrerr(fmt, buf + 4); 126364659Seric } 126464659Seric else 126564659Seric { 126664659Seric usrerr("%s", buf); 126764659Seric } 126864306Seric goto badaddr; 12694279Seric } 127057402Seric 12714598Seric for (mp = Mailer; (m = *mp++) != NULL; ) 12723149Seric { 127358680Seric if (strcasecmp(m->m_name, *tv) == 0) 12743149Seric break; 12753149Seric } 12763149Seric if (m == NULL) 12774279Seric { 127858151Seric syserr("554 buildaddr: unknown mailer %s", *tv); 127964306Seric goto badaddr; 12804279Seric } 12814598Seric a->q_mailer = m; 12823149Seric 12833149Seric /* figure out what host (if any) */ 128456678Seric tv++; 128558509Seric if ((**tv & 0377) == CANONHOST) 12863149Seric { 128758008Seric bp = buf; 128858008Seric spaceleft = sizeof buf - 1; 128958050Seric while (*++tv != NULL && (**tv & 0377) != CANONUSER) 129058008Seric { 129158008Seric int i = strlen(*tv); 129258008Seric 129358008Seric if (i > spaceleft) 129458008Seric { 129558008Seric /* out of space for this address */ 129658008Seric if (spaceleft >= 0) 129758151Seric syserr("554 buildaddr: host too long (%.40s...)", 129858008Seric buf); 129958008Seric i = spaceleft; 130058008Seric spaceleft = 0; 130158008Seric } 130258008Seric if (i <= 0) 130358008Seric continue; 130458008Seric bcopy(*tv, bp, i); 130558008Seric bp += i; 130658008Seric spaceleft -= i; 130758008Seric } 130858010Seric *bp = '\0'; 13095704Seric a->q_host = newstr(buf); 13103149Seric } 131157249Seric else 131258509Seric { 131358509Seric if (!bitnset(M_LOCALMAILER, m->m_flags)) 131458509Seric { 131558509Seric syserr("554 buildaddr: no host"); 131664306Seric goto badaddr; 131758509Seric } 131857249Seric a->q_host = NULL; 131958509Seric } 13203149Seric 13213149Seric /* figure out the user */ 132258050Seric if (*tv == NULL || (**tv & 0377) != CANONUSER) 13234279Seric { 132458151Seric syserr("554 buildaddr: no user"); 132564306Seric goto badaddr; 13264279Seric } 132757402Seric tv++; 132851317Seric 132957402Seric /* do special mapping for local mailer */ 133057402Seric if (m == LocalMailer && *tv != NULL) 133157402Seric { 133257454Seric register char *p = *tv; 133357454Seric 133457454Seric if (*p == '"') 133557454Seric p++; 133657454Seric if (*p == '|') 133757402Seric a->q_mailer = m = ProgMailer; 133857454Seric else if (*p == '/') 133957402Seric a->q_mailer = m = FileMailer; 134057454Seric else if (*p == ':') 134157402Seric { 134257402Seric /* may be :include: */ 134358814Seric cataddr(tv, NULL, buf, sizeof buf, '\0'); 134458008Seric stripquotes(buf); 134558008Seric if (strncasecmp(buf, ":include:", 9) == 0) 134658008Seric { 134758008Seric /* if :include:, don't need further rewriting */ 134857402Seric a->q_mailer = m = InclMailer; 134958008Seric a->q_user = &buf[9]; 135058008Seric return (a); 135158008Seric } 135257402Seric } 135357402Seric } 135457402Seric 135558008Seric if (m == LocalMailer && *tv != NULL && strcmp(*tv, "@") == 0) 135658008Seric { 135758008Seric tv++; 135858008Seric a->q_flags |= QNOTREMOTE; 135958008Seric } 136058008Seric 136164284Seric /* rewrite according recipient mailer rewriting rules */ 136264284Seric define('h', a->q_host, e); 136364284Seric if (!bitset(RF_SENDERADDR|RF_HEADERADDR, flags)) 136464284Seric { 136564284Seric /* sender addresses done later */ 136664284Seric (void) rewrite(tv, 2, e); 136764284Seric if (m->m_re_rwset > 0) 136864284Seric (void) rewrite(tv, m->m_re_rwset, e); 136964284Seric } 137059084Seric (void) rewrite(tv, 4, e); 137119040Seric 137219040Seric /* save the result for the command line/RCPT argument */ 137358814Seric cataddr(tv, NULL, buf, sizeof buf, '\0'); 13743149Seric a->q_user = buf; 13753149Seric 137658670Seric /* 137758670Seric ** Do mapping to lower case as requested by mailer 137858670Seric */ 137958670Seric 138058670Seric if (a->q_host != NULL && !bitnset(M_HST_UPPER, m->m_flags)) 138158670Seric makelower(a->q_host); 138258670Seric if (!bitnset(M_USR_UPPER, m->m_flags)) 138358670Seric makelower(a->q_user); 138458670Seric 13853149Seric return (a); 13863149Seric } 13873188Seric /* 13884228Seric ** CATADDR -- concatenate pieces of addresses (putting in <LWSP> subs) 13894228Seric ** 13904228Seric ** Parameters: 13914228Seric ** pvp -- parameter vector to rebuild. 139258814Seric ** evp -- last parameter to include. Can be NULL to 139358814Seric ** use entire pvp. 13944228Seric ** buf -- buffer to build the string into. 13954228Seric ** sz -- size of buf. 139658082Seric ** spacesub -- the space separator character; if null, 139758082Seric ** use SpaceSub. 13984228Seric ** 13994228Seric ** Returns: 14004228Seric ** none. 14014228Seric ** 14024228Seric ** Side Effects: 14034228Seric ** Destroys buf. 14044228Seric */ 14054228Seric 140658814Seric cataddr(pvp, evp, buf, sz, spacesub) 14074228Seric char **pvp; 140858814Seric char **evp; 14094228Seric char *buf; 14104228Seric register int sz; 141158082Seric char spacesub; 14124228Seric { 14134228Seric bool oatomtok = FALSE; 141456678Seric bool natomtok = FALSE; 14154228Seric register int i; 14164228Seric register char *p; 14174228Seric 141858082Seric if (spacesub == '\0') 141958082Seric spacesub = SpaceSub; 142058082Seric 14218423Seric if (pvp == NULL) 14228423Seric { 142323109Seric (void) strcpy(buf, ""); 14248423Seric return; 14258423Seric } 14264228Seric p = buf; 142711156Seric sz -= 2; 14284228Seric while (*pvp != NULL && (i = strlen(*pvp)) < sz) 14294228Seric { 14308078Seric natomtok = (toktype(**pvp) == ATM); 14314228Seric if (oatomtok && natomtok) 143258082Seric *p++ = spacesub; 14334228Seric (void) strcpy(p, *pvp); 14344228Seric oatomtok = natomtok; 14354228Seric p += i; 143611156Seric sz -= i + 1; 143758814Seric if (pvp++ == evp) 143858814Seric break; 14394228Seric } 14404228Seric *p = '\0'; 14414228Seric } 14424228Seric /* 14433188Seric ** SAMEADDR -- Determine if two addresses are the same 14443188Seric ** 14453188Seric ** This is not just a straight comparison -- if the mailer doesn't 14463188Seric ** care about the host we just ignore it, etc. 14473188Seric ** 14483188Seric ** Parameters: 14493188Seric ** a, b -- pointers to the internal forms to compare. 14503188Seric ** 14513188Seric ** Returns: 14523188Seric ** TRUE -- they represent the same mailbox. 14533188Seric ** FALSE -- they don't. 14543188Seric ** 14553188Seric ** Side Effects: 14563188Seric ** none. 14573188Seric */ 14583188Seric 14593188Seric bool 14609374Seric sameaddr(a, b) 14613188Seric register ADDRESS *a; 14623188Seric register ADDRESS *b; 14633188Seric { 14643188Seric /* if they don't have the same mailer, forget it */ 14653188Seric if (a->q_mailer != b->q_mailer) 14663188Seric return (FALSE); 14673188Seric 14683188Seric /* if the user isn't the same, we can drop out */ 146956678Seric if (strcmp(a->q_user, b->q_user) != 0) 14703188Seric return (FALSE); 14713188Seric 147258438Seric /* if we have good uids for both but the differ, these are different */ 147358438Seric if (bitset(QGOODUID, a->q_flags & b->q_flags) && a->q_uid != b->q_uid) 147458438Seric return (FALSE); 147558438Seric 147658509Seric /* otherwise compare hosts (but be careful for NULL ptrs) */ 147758509Seric if (a->q_host == b->q_host) 147858509Seric { 147958509Seric /* probably both null pointers */ 14803188Seric return (TRUE); 148158509Seric } 14823188Seric if (a->q_host == NULL || b->q_host == NULL) 148358509Seric { 148458509Seric /* only one is a null pointer */ 14853188Seric return (FALSE); 148658509Seric } 148756678Seric if (strcmp(a->q_host, b->q_host) != 0) 14883188Seric return (FALSE); 14893188Seric 14903188Seric return (TRUE); 14913188Seric } 14923234Seric /* 14933234Seric ** PRINTADDR -- print address (for debugging) 14943234Seric ** 14953234Seric ** Parameters: 14963234Seric ** a -- the address to print 14973234Seric ** follow -- follow the q_next chain. 14983234Seric ** 14993234Seric ** Returns: 15003234Seric ** none. 15013234Seric ** 15023234Seric ** Side Effects: 15033234Seric ** none. 15043234Seric */ 15053234Seric 15063234Seric printaddr(a, follow) 15073234Seric register ADDRESS *a; 15083234Seric bool follow; 15093234Seric { 15105001Seric bool first = TRUE; 151157731Seric register MAILER *m; 151257731Seric MAILER pseudomailer; 15135001Seric 15143234Seric while (a != NULL) 15153234Seric { 15165001Seric first = FALSE; 15174443Seric printf("%x=", a); 15184085Seric (void) fflush(stdout); 151957731Seric 152057731Seric /* find the mailer -- carefully */ 152157731Seric m = a->q_mailer; 152257731Seric if (m == NULL) 152357731Seric { 152457731Seric m = &pseudomailer; 152557731Seric m->m_mno = -1; 152657731Seric m->m_name = "NULL"; 152757731Seric } 152857731Seric 152958680Seric printf("%s:\n\tmailer %d (%s), host `%s', user `%s', ruser `%s'\n", 153057731Seric a->q_paddr, m->m_mno, m->m_name, 153163756Seric a->q_host, a->q_user, 153263756Seric a->q_ruser ? a->q_ruser : "<null>"); 153359269Seric printf("\tnext=%x, flags=%o, alias %x, uid %d, gid %d\n", 153459269Seric a->q_next, a->q_flags, a->q_alias, a->q_uid, a->q_gid); 153559269Seric printf("\towner=%s, home=\"%s\", fullname=\"%s\"\n", 153659269Seric a->q_owner == NULL ? "(none)" : a->q_owner, 153763756Seric a->q_home == NULL ? "(none)" : a->q_home, 153863756Seric a->q_fullname == NULL ? "(none)" : a->q_fullname); 15394996Seric 15403234Seric if (!follow) 15413234Seric return; 15424996Seric a = a->q_next; 15433234Seric } 15445001Seric if (first) 15454443Seric printf("[NULL]\n"); 15463234Seric } 15474317Seric 15487682Seric /* 15497682Seric ** REMOTENAME -- return the name relative to the current mailer 15507682Seric ** 15517682Seric ** Parameters: 15527682Seric ** name -- the name to translate. 15538069Seric ** m -- the mailer that we want to do rewriting relative 15548069Seric ** to. 155559163Seric ** flags -- fine tune operations. 155659163Seric ** pstat -- pointer to status word. 155758020Seric ** e -- the current envelope. 15587682Seric ** 15597682Seric ** Returns: 15607682Seric ** the text string representing this address relative to 15617682Seric ** the receiving mailer. 15627682Seric ** 15637682Seric ** Side Effects: 15647682Seric ** none. 15657682Seric ** 15667682Seric ** Warnings: 15677682Seric ** The text string returned is tucked away locally; 15687682Seric ** copy it if you intend to save it. 15697682Seric */ 15707682Seric 15717682Seric char * 157259163Seric remotename(name, m, flags, pstat, e) 15737682Seric char *name; 157456678Seric struct mailer *m; 157559163Seric int flags; 157659163Seric int *pstat; 157756678Seric register ENVELOPE *e; 15787682Seric { 15798069Seric register char **pvp; 15808069Seric char *fancy; 158156678Seric char *oldg = macvalue('g', e); 158258020Seric int rwset; 15837682Seric static char buf[MAXNAME]; 15847682Seric char lbuf[MAXNAME]; 158516914Seric char pvpbuf[PSBUFSIZE]; 158656678Seric extern char *crackaddr(); 15877682Seric 15887755Seric if (tTd(12, 1)) 15897755Seric printf("remotename(%s)\n", name); 15907755Seric 159110177Seric /* don't do anything if we are tagging it as special */ 159259163Seric if (bitset(RF_SENDERADDR, flags)) 159359163Seric rwset = bitset(RF_HEADERADDR, flags) ? m->m_sh_rwset 159459163Seric : m->m_se_rwset; 159558020Seric else 159659163Seric rwset = bitset(RF_HEADERADDR, flags) ? m->m_rh_rwset 159759163Seric : m->m_re_rwset; 159858020Seric if (rwset < 0) 159910177Seric return (name); 160010177Seric 16017682Seric /* 16028181Seric ** Do a heuristic crack of this name to extract any comment info. 16038181Seric ** This will leave the name as a comment and a $g macro. 16047889Seric */ 16057889Seric 160659163Seric if (bitset(RF_CANONICAL, flags) || bitnset(M_NOCOMMENT, m->m_flags)) 160758050Seric fancy = "\201g"; 160810310Seric else 160910310Seric fancy = crackaddr(name); 16107889Seric 16118181Seric /* 16128181Seric ** Turn the name into canonical form. 16138181Seric ** Normally this will be RFC 822 style, i.e., "user@domain". 16148181Seric ** If this only resolves to "user", and the "C" flag is 16158181Seric ** specified in the sending mailer, then the sender's 16168181Seric ** domain will be appended. 16178181Seric */ 16188181Seric 161958333Seric pvp = prescan(name, '\0', pvpbuf, NULL); 16207889Seric if (pvp == NULL) 16217889Seric return (name); 162259163Seric if (rewrite(pvp, 3, e) == EX_TEMPFAIL) 162359163Seric *pstat = EX_TEMPFAIL; 162459163Seric if (bitset(RF_ADDDOMAIN, flags) && e->e_fromdomain != NULL) 16258181Seric { 16268181Seric /* append from domain to this address */ 16278181Seric register char **pxp = pvp; 16288181Seric 16299594Seric /* see if there is an "@domain" in the current name */ 16308181Seric while (*pxp != NULL && strcmp(*pxp, "@") != 0) 16318181Seric pxp++; 16328181Seric if (*pxp == NULL) 16338181Seric { 16349594Seric /* no.... append the "@domain" from the sender */ 163556678Seric register char **qxq = e->e_fromdomain; 16368181Seric 16379594Seric while ((*pxp++ = *qxq++) != NULL) 16389594Seric continue; 163959163Seric if (rewrite(pvp, 3, e) == EX_TEMPFAIL) 164059163Seric *pstat = EX_TEMPFAIL; 16418181Seric } 16428181Seric } 16438181Seric 16448181Seric /* 16458959Seric ** Do more specific rewriting. 164656678Seric ** Rewrite using ruleset 1 or 2 depending on whether this is 164756678Seric ** a sender address or not. 16488181Seric ** Then run it through any receiving-mailer-specific rulesets. 16498181Seric */ 16508181Seric 165159163Seric if (bitset(RF_SENDERADDR, flags)) 165259541Seric { 165359163Seric if (rewrite(pvp, 1, e) == EX_TEMPFAIL) 165459163Seric *pstat = EX_TEMPFAIL; 165559541Seric } 16568069Seric else 165759541Seric { 165859163Seric if (rewrite(pvp, 2, e) == EX_TEMPFAIL) 165959163Seric *pstat = EX_TEMPFAIL; 166059541Seric } 166158020Seric if (rwset > 0) 166259541Seric { 166359163Seric if (rewrite(pvp, rwset, e) == EX_TEMPFAIL) 166459163Seric *pstat = EX_TEMPFAIL; 166559541Seric } 16667682Seric 16678181Seric /* 16688959Seric ** Do any final sanitation the address may require. 16698959Seric ** This will normally be used to turn internal forms 16708959Seric ** (e.g., user@host.LOCAL) into external form. This 16718959Seric ** may be used as a default to the above rules. 16728959Seric */ 16738959Seric 167459163Seric if (rewrite(pvp, 4, e) == EX_TEMPFAIL) 167559163Seric *pstat = EX_TEMPFAIL; 16768959Seric 16778959Seric /* 16788181Seric ** Now restore the comment information we had at the beginning. 16798181Seric */ 16808181Seric 168158825Seric cataddr(pvp, NULL, lbuf, sizeof lbuf, '\0'); 168256678Seric define('g', lbuf, e); 168356678Seric expand(fancy, buf, &buf[sizeof buf - 1], e); 168456678Seric define('g', oldg, e); 16857682Seric 16867682Seric if (tTd(12, 1)) 16877755Seric printf("remotename => `%s'\n", buf); 16887682Seric return (buf); 16897682Seric } 169051317Seric /* 169156678Seric ** MAPLOCALUSER -- run local username through ruleset 5 for final redirection 169251317Seric ** 169351317Seric ** Parameters: 169456678Seric ** a -- the address to map (but just the user name part). 169556678Seric ** sendq -- the sendq in which to install any replacement 169656678Seric ** addresses. 169751317Seric ** 169851317Seric ** Returns: 169951317Seric ** none. 170051317Seric */ 170151317Seric 170256678Seric maplocaluser(a, sendq, e) 170356678Seric register ADDRESS *a; 170456678Seric ADDRESS **sendq; 170556678Seric ENVELOPE *e; 170651317Seric { 170756678Seric register char **pvp; 170856678Seric register ADDRESS *a1 = NULL; 170958333Seric auto char *delimptr; 171056678Seric char pvpbuf[PSBUFSIZE]; 171151317Seric 171256678Seric if (tTd(29, 1)) 171356678Seric { 171456678Seric printf("maplocaluser: "); 171556678Seric printaddr(a, FALSE); 171656678Seric } 171758333Seric pvp = prescan(a->q_user, '\0', pvpbuf, &delimptr); 171856678Seric if (pvp == NULL) 171956678Seric return; 172051317Seric 172159084Seric (void) rewrite(pvp, 5, e); 172258050Seric if (pvp[0] == NULL || (pvp[0][0] & 0377) != CANONNET) 172356678Seric return; 172451317Seric 172556678Seric /* if non-null, mailer destination specified -- has it changed? */ 172664284Seric a1 = buildaddr(pvp, NULL, 0, e); 172756678Seric if (a1 == NULL || sameaddr(a, a1)) 172856678Seric return; 172951317Seric 173056678Seric /* mark old address as dead; insert new address */ 173156678Seric a->q_flags |= QDONTSEND; 173257731Seric if (tTd(29, 5)) 173357731Seric { 173457731Seric printf("maplocaluser: QDONTSEND "); 173557731Seric printaddr(a, FALSE); 173657731Seric } 173756678Seric a1->q_alias = a; 173864348Seric allocaddr(a1, RF_COPYALL, NULL); 173956678Seric (void) recipient(a1, sendq, e); 174051317Seric } 174158802Seric /* 174258802Seric ** DEQUOTE_INIT -- initialize dequote map 174358802Seric ** 174458802Seric ** This is a no-op. 174558802Seric ** 174658802Seric ** Parameters: 174758802Seric ** map -- the internal map structure. 174858802Seric ** args -- arguments. 174958802Seric ** 175058802Seric ** Returns: 175158802Seric ** TRUE. 175258802Seric */ 175358802Seric 175458802Seric bool 175560219Seric dequote_init(map, args) 175658802Seric MAP *map; 175758802Seric char *args; 175858802Seric { 175958805Seric register char *p = args; 176058805Seric 176158805Seric for (;;) 176258805Seric { 176358805Seric while (isascii(*p) && isspace(*p)) 176458805Seric p++; 176558805Seric if (*p != '-') 176658805Seric break; 176758805Seric switch (*++p) 176858805Seric { 176958805Seric case 'a': 177058805Seric map->map_app = ++p; 177158805Seric break; 177258805Seric } 177358805Seric while (*p != '\0' && !(isascii(*p) && isspace(*p))) 177458805Seric p++; 177558805Seric if (*p != '\0') 177658805Seric *p = '\0'; 177758805Seric } 177858805Seric if (map->map_app != NULL) 177958805Seric map->map_app = newstr(map->map_app); 178058805Seric 178158802Seric return TRUE; 178258802Seric } 178358802Seric /* 178458802Seric ** DEQUOTE_MAP -- unquote an address 178558802Seric ** 178658802Seric ** Parameters: 178758802Seric ** map -- the internal map structure (ignored). 178860089Seric ** name -- the name to dequote. 178958802Seric ** av -- arguments (ignored). 179059084Seric ** statp -- pointer to status out-parameter. 179158802Seric ** 179258802Seric ** Returns: 179358802Seric ** NULL -- if there were no quotes, or if the resulting 179458802Seric ** unquoted buffer would not be acceptable to prescan. 179558802Seric ** else -- The dequoted buffer. 179658802Seric */ 179758802Seric 179858802Seric char * 179960089Seric dequote_map(map, name, av, statp) 180058802Seric MAP *map; 180160089Seric char *name; 180258802Seric char **av; 180359084Seric int *statp; 180458802Seric { 180558802Seric register char *p; 180658802Seric register char *q; 180758802Seric register char c; 180858802Seric int anglecnt; 180958802Seric int cmntcnt; 181058802Seric int quotecnt; 181159089Seric int spacecnt; 181258802Seric bool quotemode; 181358802Seric bool bslashmode; 181458802Seric 181558802Seric anglecnt = 0; 181658802Seric cmntcnt = 0; 181758802Seric quotecnt = 0; 181859089Seric spacecnt = 0; 181958802Seric quotemode = FALSE; 182058802Seric bslashmode = FALSE; 182158802Seric 182260089Seric for (p = q = name; (c = *p++) != '\0'; ) 182358802Seric { 182458802Seric if (bslashmode) 182558802Seric { 182658802Seric bslashmode = FALSE; 182758802Seric *q++ = c; 182858802Seric continue; 182958802Seric } 183058802Seric 183158802Seric switch (c) 183258802Seric { 183358802Seric case '\\': 183458802Seric bslashmode = TRUE; 183558802Seric break; 183658802Seric 183758802Seric case '(': 183858802Seric cmntcnt++; 183958802Seric break; 184058802Seric 184158802Seric case ')': 184258802Seric if (cmntcnt-- <= 0) 184358802Seric return NULL; 184458802Seric break; 184559089Seric 184659089Seric case ' ': 184759089Seric spacecnt++; 184859089Seric break; 184958802Seric } 185058802Seric 185158802Seric if (cmntcnt > 0) 185258802Seric { 185358802Seric *q++ = c; 185458802Seric continue; 185558802Seric } 185658802Seric 185758802Seric switch (c) 185858802Seric { 185958802Seric case '"': 186058802Seric quotemode = !quotemode; 186158802Seric quotecnt++; 186258802Seric continue; 186358802Seric 186458802Seric case '<': 186558802Seric anglecnt++; 186658802Seric break; 186758802Seric 186858802Seric case '>': 186958802Seric if (anglecnt-- <= 0) 187058802Seric return NULL; 187158802Seric break; 187258802Seric } 187358802Seric *q++ = c; 187458802Seric } 187558802Seric 187658802Seric if (anglecnt != 0 || cmntcnt != 0 || bslashmode || 187759089Seric quotemode || quotecnt <= 0 || spacecnt != 0) 187858802Seric return NULL; 187958802Seric *q++ = '\0'; 188060089Seric return name; 188158802Seric } 1882