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*65071Seric static char sccsid[] = "@(#)parseaddr.c 8.23 (Berkeley) 12/10/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 8065066Seric pvp = prescan(addr, delim, pvpbuf, sizeof 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; 120*65071Seric if (rewrite(pvp, 3, 0, e) == EX_TEMPFAIL) 12159084Seric queueup = TRUE; 122*65071Seric if (rewrite(pvp, 0, 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) 18864764Seric { 18964726Seric savedelim = *delimptr; 19064764Seric if (savedelim != '\0') 19164764Seric *delimptr = '\0'; 19264764Seric } 19364726Seric #if 0 19464726Seric /* for testing.... */ 19564726Seric if (strcmp(addr, "INvalidADDR") == 0) 19657388Seric { 19764726Seric usrerr("553 INvalid ADDRess"); 19864764Seric goto addrfailure; 19957388Seric } 20064726Seric #endif 20164726Seric for (; *addr != '\0'; addr++) 20264726Seric { 20364726Seric if ((*addr & 0340) == 0200) 20464726Seric break; 20564726Seric } 20664726Seric if (*addr == '\0') 20764764Seric { 20864764Seric if (savedelim != '\0' && delimptr != NULL) 20964764Seric *delimptr = savedelim; 21064726Seric return FALSE; 21164764Seric } 21264726Seric setstat(EX_USAGE); 21364726Seric usrerr("553 Address contained invalid control characters"); 21464764Seric addrfailure: 21564764Seric if (savedelim != '\0' && delimptr != NULL) 21664764Seric *delimptr = savedelim; 21764726Seric return TRUE; 21857388Seric } 21957388Seric /* 22056678Seric ** ALLOCADDR -- do local allocations of address on demand. 22156678Seric ** 22256678Seric ** Also lowercases the host name if requested. 22356678Seric ** 22456678Seric ** Parameters: 22556678Seric ** a -- the address to reallocate. 22664284Seric ** flags -- the copy flag (see RF_ definitions in sendmail.h 22764284Seric ** for a description). 22856678Seric ** paddr -- the printname of the address. 22956678Seric ** 23056678Seric ** Returns: 23156678Seric ** none. 23256678Seric ** 23356678Seric ** Side Effects: 23456678Seric ** Copies portions of a into local buffers as requested. 23556678Seric */ 23656678Seric 23764348Seric allocaddr(a, flags, paddr) 23856678Seric register ADDRESS *a; 23964284Seric int flags; 24056678Seric char *paddr; 24156678Seric { 24258673Seric if (tTd(24, 4)) 24364284Seric printf("allocaddr(flags=%o, paddr=%s)\n", flags, paddr); 24458673Seric 24564348Seric a->q_paddr = paddr; 2468078Seric 24724944Seric if (a->q_user == NULL) 24824944Seric a->q_user = ""; 24924944Seric if (a->q_host == NULL) 25024944Seric a->q_host = ""; 25124944Seric 25264284Seric if (bitset(RF_COPYPARSE, flags)) 253297Seric { 25424944Seric a->q_host = newstr(a->q_host); 2553149Seric if (a->q_user != a->q_paddr) 2563149Seric a->q_user = newstr(a->q_user); 257297Seric } 258297Seric 25956678Seric if (a->q_paddr == NULL) 26056678Seric a->q_paddr = a->q_user; 261297Seric } 262297Seric /* 263297Seric ** PRESCAN -- Prescan name and make it canonical 264297Seric ** 2659374Seric ** Scans a name and turns it into a set of tokens. This process 2669374Seric ** deletes blanks and comments (in parentheses). 267297Seric ** 268297Seric ** This routine knows about quoted strings and angle brackets. 269297Seric ** 270297Seric ** There are certain subtleties to this routine. The one that 271297Seric ** comes to mind now is that backslashes on the ends of names 272297Seric ** are silently stripped off; this is intentional. The problem 273297Seric ** is that some versions of sndmsg (like at LBL) set the kill 274297Seric ** character to something other than @ when reading addresses; 275297Seric ** so people type "csvax.eric\@berkeley" -- which screws up the 276297Seric ** berknet mailer. 277297Seric ** 278297Seric ** Parameters: 279297Seric ** addr -- the name to chomp. 280297Seric ** delim -- the delimiter for the address, normally 281297Seric ** '\0' or ','; \0 is accepted in any case. 28215284Seric ** If '\t' then we are reading the .cf file. 28316914Seric ** pvpbuf -- place to put the saved text -- note that 28416914Seric ** the pointers are static. 28565066Seric ** pvpbsize -- size of pvpbuf. 28658333Seric ** delimptr -- if non-NULL, set to the location of the 28758333Seric ** terminating delimiter. 288297Seric ** 289297Seric ** Returns: 2903149Seric ** A pointer to a vector of tokens. 291297Seric ** NULL on error. 292297Seric */ 293297Seric 2948078Seric /* states and character types */ 2958078Seric # define OPR 0 /* operator */ 2968078Seric # define ATM 1 /* atom */ 2978078Seric # define QST 2 /* in quoted string */ 2988078Seric # define SPC 3 /* chewing up spaces */ 2998078Seric # define ONE 4 /* pick up one character */ 3003149Seric 3018078Seric # define NSTATES 5 /* number of states */ 3028078Seric # define TYPE 017 /* mask to select state type */ 3038078Seric 3048078Seric /* meta bits for table */ 3058078Seric # define M 020 /* meta character; don't pass through */ 3068078Seric # define B 040 /* cause a break */ 3078078Seric # define MB M|B /* meta-break */ 3088078Seric 3098078Seric static short StateTab[NSTATES][NSTATES] = 3108078Seric { 3118087Seric /* oldst chtype> OPR ATM QST SPC ONE */ 3129051Seric /*OPR*/ OPR|B, ATM|B, QST|B, SPC|MB, ONE|B, 3139051Seric /*ATM*/ OPR|B, ATM, QST|B, SPC|MB, ONE|B, 3149051Seric /*QST*/ QST, QST, OPR, QST, QST, 3158078Seric /*SPC*/ OPR, ATM, QST, SPC|M, ONE, 3168078Seric /*ONE*/ OPR, OPR, OPR, OPR, OPR, 3178078Seric }; 3188078Seric 3198078Seric # define NOCHAR -1 /* signal nothing in lookahead token */ 3208078Seric 3213149Seric char ** 32265066Seric prescan(addr, delim, pvpbuf, pvpbsize, delimptr) 323297Seric char *addr; 324297Seric char delim; 32516914Seric char pvpbuf[]; 32658333Seric char **delimptr; 327297Seric { 328297Seric register char *p; 3298078Seric register char *q; 3309346Seric register int c; 3313149Seric char **avp; 332297Seric bool bslashmode; 333297Seric int cmntcnt; 3348423Seric int anglecnt; 3353149Seric char *tok; 3368078Seric int state; 3378078Seric int newstate; 33859747Seric char *saveto = CurEnv->e_to; 3398078Seric static char *av[MAXATOM+1]; 34056678Seric extern int errno; 341297Seric 34215253Seric /* make sure error messages don't have garbage on them */ 34315253Seric errno = 0; 34415253Seric 34516914Seric q = pvpbuf; 3463149Seric bslashmode = FALSE; 3477800Seric cmntcnt = 0; 3488423Seric anglecnt = 0; 3493149Seric avp = av; 35056678Seric state = ATM; 3518078Seric c = NOCHAR; 3528078Seric p = addr; 35359747Seric CurEnv->e_to = p; 35456764Seric if (tTd(22, 11)) 355297Seric { 3568078Seric printf("prescan: "); 3578078Seric xputs(p); 35823109Seric (void) putchar('\n'); 3598078Seric } 3608078Seric 3618078Seric do 3628078Seric { 3633149Seric /* read a token */ 3643149Seric tok = q; 3658078Seric for (;;) 366297Seric { 3678078Seric /* store away any old lookahead character */ 36859277Seric if (c != NOCHAR && !bslashmode) 3698078Seric { 37015284Seric /* see if there is room */ 37165066Seric if (q >= &pvpbuf[pvpbsize - 5]) 3728078Seric { 37358151Seric usrerr("553 Address too long"); 37465015Seric returnnull: 37558333Seric if (delimptr != NULL) 37658333Seric *delimptr = p; 37759747Seric CurEnv->e_to = saveto; 3788078Seric return (NULL); 3798078Seric } 38015284Seric 38115284Seric /* squirrel it away */ 3828078Seric *q++ = c; 3838078Seric } 3848078Seric 3858078Seric /* read a new input character */ 3868078Seric c = *p++; 38757631Seric if (c == '\0') 38856764Seric { 38956764Seric /* diagnose and patch up bad syntax */ 39056764Seric if (state == QST) 39156764Seric { 39264767Seric usrerr("653 Unbalanced '\"'"); 39356764Seric c = '"'; 39456764Seric } 39556764Seric else if (cmntcnt > 0) 39656764Seric { 39764767Seric usrerr("653 Unbalanced '('"); 39856764Seric c = ')'; 39956764Seric } 40056764Seric else if (anglecnt > 0) 40156764Seric { 40256764Seric c = '>'; 40364767Seric usrerr("653 Unbalanced '<'"); 40456764Seric } 40556764Seric else 40656764Seric break; 40715284Seric 40856764Seric p--; 40956764Seric } 41057631Seric else if (c == delim && anglecnt <= 0 && 41157631Seric cmntcnt <= 0 && state != QST) 41257631Seric break; 41356764Seric 4148078Seric if (tTd(22, 101)) 4158078Seric printf("c=%c, s=%d; ", c, state); 4168078Seric 4173149Seric /* chew up special characters */ 4183149Seric *q = '\0'; 4193149Seric if (bslashmode) 4203149Seric { 42159105Seric bslashmode = FALSE; 42259105Seric 42324944Seric /* kludge \! for naive users */ 42458061Seric if (cmntcnt > 0) 42559105Seric { 42658061Seric c = NOCHAR; 42759105Seric continue; 42859105Seric } 42959105Seric else if (c != '!' || state == QST) 43059105Seric { 43156678Seric *q++ = '\\'; 43259105Seric continue; 43359105Seric } 4343149Seric } 43556678Seric 43656678Seric if (c == '\\') 4373149Seric { 4383149Seric bslashmode = TRUE; 4393149Seric } 44056678Seric else if (state == QST) 4418514Seric { 4428514Seric /* do nothing, just avoid next clauses */ 4438514Seric } 4448078Seric else if (c == '(') 4454100Seric { 4468078Seric cmntcnt++; 4478078Seric c = NOCHAR; 4484100Seric } 4498078Seric else if (c == ')') 4503149Seric { 4518078Seric if (cmntcnt <= 0) 4523149Seric { 45364767Seric usrerr("653 Unbalanced ')'"); 45463844Seric c = NOCHAR; 4553149Seric } 4568078Seric else 4578078Seric cmntcnt--; 4588078Seric } 4598078Seric else if (cmntcnt > 0) 4608078Seric c = NOCHAR; 4618423Seric else if (c == '<') 4628423Seric anglecnt++; 4638423Seric else if (c == '>') 4648423Seric { 4658423Seric if (anglecnt <= 0) 4668423Seric { 46764767Seric usrerr("653 Unbalanced '>'"); 46863844Seric c = NOCHAR; 4698423Seric } 47063844Seric else 47163844Seric anglecnt--; 4728423Seric } 47358050Seric else if (delim == ' ' && isascii(c) && isspace(c)) 47411423Seric c = ' '; 4753149Seric 4768078Seric if (c == NOCHAR) 4778078Seric continue; 4783149Seric 4798078Seric /* see if this is end of input */ 48011405Seric if (c == delim && anglecnt <= 0 && state != QST) 4813149Seric break; 4823149Seric 4838078Seric newstate = StateTab[state][toktype(c)]; 4848078Seric if (tTd(22, 101)) 4858078Seric printf("ns=%02o\n", newstate); 4868078Seric state = newstate & TYPE; 4878078Seric if (bitset(M, newstate)) 4888078Seric c = NOCHAR; 4898078Seric if (bitset(B, newstate)) 4904228Seric break; 491297Seric } 4923149Seric 4933149Seric /* new token */ 4948078Seric if (tok != q) 4951378Seric { 4968078Seric *q++ = '\0'; 4978078Seric if (tTd(22, 36)) 498297Seric { 4998078Seric printf("tok="); 5008078Seric xputs(tok); 50123109Seric (void) putchar('\n'); 502297Seric } 5038078Seric if (avp >= &av[MAXATOM]) 504297Seric { 50558151Seric syserr("553 prescan: too many tokens"); 50665015Seric goto returnnull; 507297Seric } 50865015Seric if (q - tok > MAXNAME) 50965015Seric { 51065015Seric syserr("553 prescan: token too long"); 51165015Seric goto returnnull; 51265015Seric } 5138078Seric *avp++ = tok; 514297Seric } 5158423Seric } while (c != '\0' && (c != delim || anglecnt > 0)); 5163149Seric *avp = NULL; 51758333Seric p--; 51858333Seric if (delimptr != NULL) 51958333Seric *delimptr = p; 52056764Seric if (tTd(22, 12)) 52156764Seric { 52256764Seric printf("prescan==>"); 52356764Seric printav(av); 52456764Seric } 52559747Seric CurEnv->e_to = saveto; 52658546Seric if (av[0] == NULL) 52764966Seric { 52864966Seric if (tTd(22, 1)) 52964966Seric printf("prescan: null leading token\n"); 53058546Seric return (NULL); 53164966Seric } 53258403Seric return (av); 5333149Seric } 5343149Seric /* 5353149Seric ** TOKTYPE -- return token type 5363149Seric ** 5373149Seric ** Parameters: 5383149Seric ** c -- the character in question. 5393149Seric ** 5403149Seric ** Returns: 5413149Seric ** Its type. 5423149Seric ** 5433149Seric ** Side Effects: 5443149Seric ** none. 5453149Seric */ 546297Seric 5473149Seric toktype(c) 54858050Seric register int c; 5493149Seric { 5503380Seric static char buf[50]; 5513382Seric static bool firstime = TRUE; 5523380Seric 5533382Seric if (firstime) 5543380Seric { 5553382Seric firstime = FALSE; 55658050Seric expand("\201o", buf, &buf[sizeof buf - 1], CurEnv); 5577005Seric (void) strcat(buf, DELIMCHARS); 5583380Seric } 55958050Seric c &= 0377; 56056327Seric if (c == MATCHCLASS || c == MATCHREPL || c == MATCHNCLASS) 5618078Seric return (ONE); 56259027Seric if (c == MACRODEXPAND) 56359027Seric return (ONE); 5648078Seric if (c == '"') 5658078Seric return (QST); 56658050Seric if ((c & 0340) == 0200) 56758050Seric return (OPR); 5684100Seric if (!isascii(c)) 5698078Seric return (ATM); 5708078Seric if (isspace(c) || c == ')') 5718078Seric return (SPC); 57258050Seric if (strchr(buf, c) != NULL) 5738078Seric return (OPR); 5748078Seric return (ATM); 5753149Seric } 5763149Seric /* 5773149Seric ** REWRITE -- apply rewrite rules to token vector. 5783149Seric ** 5794476Seric ** This routine is an ordered production system. Each rewrite 5804476Seric ** rule has a LHS (called the pattern) and a RHS (called the 5814476Seric ** rewrite); 'rwr' points the the current rewrite rule. 5824476Seric ** 5834476Seric ** For each rewrite rule, 'avp' points the address vector we 5844476Seric ** are trying to match against, and 'pvp' points to the pattern. 5858058Seric ** If pvp points to a special match value (MATCHZANY, MATCHANY, 5869585Seric ** MATCHONE, MATCHCLASS, MATCHNCLASS) then the address in avp 5879585Seric ** matched is saved away in the match vector (pointed to by 'mvp'). 5884476Seric ** 5894476Seric ** When a match between avp & pvp does not match, we try to 5909585Seric ** back out. If we back up over MATCHONE, MATCHCLASS, or MATCHNCLASS 5914476Seric ** we must also back out the match in mvp. If we reach a 5928058Seric ** MATCHANY or MATCHZANY we just extend the match and start 5938058Seric ** over again. 5944476Seric ** 5954476Seric ** When we finally match, we rewrite the address vector 5964476Seric ** and try over again. 5974476Seric ** 5983149Seric ** Parameters: 5993149Seric ** pvp -- pointer to token vector. 60059027Seric ** ruleset -- the ruleset to use for rewriting. 601*65071Seric ** reclevel -- recursion level (to catch loops). 60259027Seric ** e -- the current envelope. 6033149Seric ** 6043149Seric ** Returns: 60559084Seric ** A status code. If EX_TEMPFAIL, higher level code should 60659084Seric ** attempt recovery. 6073149Seric ** 6083149Seric ** Side Effects: 6093149Seric ** pvp is modified. 6103149Seric */ 6112091Seric 6123149Seric struct match 6133149Seric { 6144468Seric char **first; /* first token matched */ 6154468Seric char **last; /* last token matched */ 61658825Seric char **pattern; /* pointer to pattern */ 6173149Seric }; 6183149Seric 6194468Seric # define MAXMATCH 9 /* max params per rewrite */ 6203149Seric 6213149Seric 62259084Seric int 623*65071Seric rewrite(pvp, ruleset, reclevel, e) 6243149Seric char **pvp; 6254070Seric int ruleset; 626*65071Seric int reclevel; 62759027Seric register ENVELOPE *e; 6283149Seric { 6293149Seric register char *ap; /* address pointer */ 6303149Seric register char *rp; /* rewrite pointer */ 6313149Seric register char **avp; /* address vector pointer */ 6323149Seric register char **rvp; /* rewrite vector pointer */ 6338058Seric register struct match *mlp; /* cur ptr into mlist */ 6348058Seric register struct rewrite *rwr; /* pointer to current rewrite rule */ 63558866Seric int ruleno; /* current rule number */ 63659084Seric int rstat = EX_OK; /* return status */ 63764740Seric int loopcount; 63856678Seric struct match mlist[MAXMATCH]; /* stores match on LHS */ 6393149Seric char *npvp[MAXATOM+1]; /* temporary space for rebuild */ 6403149Seric 6419279Seric if (OpMode == MD_TEST || tTd(21, 2)) 6423149Seric { 6438959Seric printf("rewrite: ruleset %2d input:", ruleset); 64456678Seric printav(pvp); 6453149Seric } 64656678Seric if (ruleset < 0 || ruleset >= MAXRWSETS) 64756326Seric { 64858151Seric syserr("554 rewrite: illegal ruleset number %d", ruleset); 64959084Seric return EX_CONFIG; 65056326Seric } 651*65071Seric if (reclevel++ > 50) 652*65071Seric { 653*65071Seric syserr("rewrite: infinite recursion, ruleset %d", ruleset); 654*65071Seric return EX_CONFIG; 655*65071Seric } 65656678Seric if (pvp == NULL) 65759084Seric return EX_USAGE; 65856326Seric 6593149Seric /* 66056678Seric ** Run through the list of rewrite rules, applying 66156678Seric ** any that match. 6623149Seric */ 6633149Seric 66458866Seric ruleno = 1; 66564740Seric loopcount = 0; 6664070Seric for (rwr = RewriteRules[ruleset]; rwr != NULL; ) 6673149Seric { 6687675Seric if (tTd(21, 12)) 669297Seric { 6708069Seric printf("-----trying rule:"); 67156678Seric printav(rwr->r_lhs); 6723149Seric } 6733149Seric 6743149Seric /* try to match on this rule */ 6754468Seric mlp = mlist; 6768058Seric rvp = rwr->r_lhs; 6778058Seric avp = pvp; 67858866Seric if (++loopcount > 100) 6793149Seric { 68058866Seric syserr("554 Infinite loop in ruleset %d, rule %d", 68158866Seric ruleset, ruleno); 68258866Seric if (tTd(21, 1)) 68352637Seric { 68456678Seric printf("workspace: "); 68556678Seric printav(pvp); 68652637Seric } 68758866Seric break; 68858866Seric } 68958866Seric 69058866Seric while ((ap = *avp) != NULL || *rvp != NULL) 69158866Seric { 6923149Seric rp = *rvp; 6938058Seric if (tTd(21, 35)) 6948058Seric { 69558825Seric printf("ADVANCE rp="); 69657531Seric xputs(rp); 69757532Seric printf(", ap="); 6988058Seric xputs(ap); 6998069Seric printf("\n"); 7008058Seric } 70156678Seric if (rp == NULL) 70256326Seric { 7033149Seric /* end-of-pattern before end-of-address */ 7048058Seric goto backup; 70556678Seric } 70658173Seric if (ap == NULL && (*rp & 0377) != MATCHZANY && 70758827Seric (*rp & 0377) != MATCHZERO) 70856326Seric { 70958825Seric /* end-of-input with patterns left */ 71058825Seric goto backup; 711297Seric } 71256326Seric 71358050Seric switch (*rp & 0377) 7148058Seric { 71556678Seric register STAB *s; 71658814Seric char buf[MAXLINE]; 71756326Seric 71856678Seric case MATCHCLASS: 71958825Seric /* match any phrase in a class */ 72058825Seric mlp->pattern = rvp; 72158814Seric mlp->first = avp; 72258814Seric extendclass: 72358825Seric ap = *avp; 72458825Seric if (ap == NULL) 72558814Seric goto backup; 72658814Seric mlp->last = avp++; 72758814Seric cataddr(mlp->first, mlp->last, buf, sizeof buf, '\0'); 72858814Seric s = stab(buf, ST_CLASS, ST_FIND); 72956678Seric if (s == NULL || !bitnset(rp[1], s->s_class)) 73056326Seric { 73158825Seric if (tTd(21, 36)) 73258825Seric { 73358825Seric printf("EXTEND rp="); 73458825Seric xputs(rp); 73558825Seric printf(", ap="); 73658825Seric xputs(ap); 73758825Seric printf("\n"); 73858825Seric } 73958825Seric goto extendclass; 74056326Seric } 74158825Seric if (tTd(21, 36)) 74258825Seric printf("CLMATCH\n"); 74358814Seric mlp++; 74458814Seric break; 7454060Seric 74658825Seric case MATCHNCLASS: 74758825Seric /* match any token not in a class */ 74858825Seric s = stab(ap, ST_CLASS, ST_FIND); 74958825Seric if (s != NULL && bitnset(rp[1], s->s_class)) 75058825Seric goto backup; 75158825Seric 75258825Seric /* fall through */ 75358825Seric 75456678Seric case MATCHONE: 75556678Seric case MATCHANY: 75656678Seric /* match exactly one token */ 75758825Seric mlp->pattern = rvp; 75856678Seric mlp->first = avp; 75956678Seric mlp->last = avp++; 7608058Seric mlp++; 76156678Seric break; 7628058Seric 76356678Seric case MATCHZANY: 76456678Seric /* match zero or more tokens */ 76558825Seric mlp->pattern = rvp; 76656678Seric mlp->first = avp; 76756678Seric mlp->last = avp - 1; 76856678Seric mlp++; 76956678Seric break; 77056326Seric 77158827Seric case MATCHZERO: 77258173Seric /* match zero tokens */ 77358173Seric break; 77458173Seric 77559027Seric case MACRODEXPAND: 77659027Seric /* 77759027Seric ** Match against run-time macro. 77859027Seric ** This algorithm is broken for the 77959027Seric ** general case (no recursive macros, 78059027Seric ** improper tokenization) but should 78159027Seric ** work for the usual cases. 78259027Seric */ 78359027Seric 78459027Seric ap = macvalue(rp[1], e); 78559027Seric mlp->first = avp; 78659027Seric if (tTd(21, 2)) 78759027Seric printf("rewrite: LHS $&%c => \"%s\"\n", 78859027Seric rp[1], 78959027Seric ap == NULL ? "(NULL)" : ap); 79059027Seric 79159027Seric if (ap == NULL) 79259027Seric break; 79360502Seric while (*ap != '\0') 79459027Seric { 79559027Seric if (*avp == NULL || 79659027Seric strncasecmp(ap, *avp, strlen(*avp)) != 0) 79759027Seric { 79859027Seric /* no match */ 79959027Seric avp = mlp->first; 80059027Seric goto backup; 80159027Seric } 80259027Seric ap += strlen(*avp++); 80359027Seric } 80459027Seric 80559027Seric /* match */ 80659027Seric break; 80759027Seric 80856678Seric default: 80956678Seric /* must have exact match */ 81056678Seric if (strcasecmp(rp, ap)) 8118058Seric goto backup; 8124468Seric avp++; 81356678Seric break; 8143149Seric } 8153149Seric 81656678Seric /* successful match on this token */ 8173149Seric rvp++; 8183149Seric continue; 8193149Seric 82058825Seric backup: 82156678Seric /* match failed -- back up */ 82258825Seric while (--mlp >= mlist) 8233149Seric { 82458825Seric rvp = mlp->pattern; 82556678Seric rp = *rvp; 82658825Seric avp = mlp->last + 1; 82758825Seric ap = *avp; 82858825Seric 82958825Seric if (tTd(21, 36)) 83058825Seric { 83158825Seric printf("BACKUP rp="); 83258825Seric xputs(rp); 83358825Seric printf(", ap="); 83458825Seric xputs(ap); 83558825Seric printf("\n"); 83658825Seric } 83758825Seric 83858825Seric if (ap == NULL) 83958825Seric { 84058825Seric /* run off the end -- back up again */ 84158825Seric continue; 84258825Seric } 84358050Seric if ((*rp & 0377) == MATCHANY || 84458050Seric (*rp & 0377) == MATCHZANY) 8454468Seric { 84656678Seric /* extend binding and continue */ 84758825Seric mlp->last = avp++; 84856678Seric rvp++; 84958825Seric mlp++; 85056678Seric break; 8514468Seric } 85258825Seric if ((*rp & 0377) == MATCHCLASS) 85356678Seric { 85458814Seric /* extend binding and try again */ 85563397Seric mlp->last = avp; 85658814Seric goto extendclass; 85758814Seric } 8583149Seric } 8593149Seric 86058825Seric if (mlp < mlist) 86156678Seric { 86256678Seric /* total failure to match */ 86356326Seric break; 8643149Seric } 865297Seric } 8663149Seric 8673149Seric /* 86856678Seric ** See if we successfully matched 8693149Seric */ 8703149Seric 87158827Seric if (mlp < mlist || *rvp != NULL) 8723149Seric { 8739374Seric if (tTd(21, 10)) 8749374Seric printf("----- rule fails\n"); 8759374Seric rwr = rwr->r_next; 87658866Seric ruleno++; 87764740Seric loopcount = 0; 8789374Seric continue; 8799374Seric } 8803149Seric 8819374Seric rvp = rwr->r_rhs; 8829374Seric if (tTd(21, 12)) 8839374Seric { 8849374Seric printf("-----rule matches:"); 88556678Seric printav(rvp); 8869374Seric } 8879374Seric 8889374Seric rp = *rvp; 88958050Seric if ((*rp & 0377) == CANONUSER) 8909374Seric { 8919374Seric rvp++; 8929374Seric rwr = rwr->r_next; 89358866Seric ruleno++; 89464740Seric loopcount = 0; 8959374Seric } 89658050Seric else if ((*rp & 0377) == CANONHOST) 8979374Seric { 8989374Seric rvp++; 8999374Seric rwr = NULL; 9009374Seric } 90158050Seric else if ((*rp & 0377) == CANONNET) 9029374Seric rwr = NULL; 9039374Seric 9049374Seric /* substitute */ 9059374Seric for (avp = npvp; *rvp != NULL; rvp++) 9069374Seric { 9079374Seric register struct match *m; 9089374Seric register char **pp; 9099374Seric 9108058Seric rp = *rvp; 91158050Seric if ((*rp & 0377) == MATCHREPL) 9128058Seric { 91316914Seric /* substitute from LHS */ 91416914Seric m = &mlist[rp[1] - '1']; 91556678Seric if (m < mlist || m >= mlp) 9169374Seric { 91758151Seric syserr("554 rewrite: ruleset %d: replacement $%c out of bounds", 91856326Seric ruleset, rp[1]); 91959084Seric return EX_CONFIG; 9209374Seric } 92116914Seric if (tTd(21, 15)) 92216914Seric { 92316914Seric printf("$%c:", rp[1]); 92416914Seric pp = m->first; 92556678Seric while (pp <= m->last) 92616914Seric { 92716914Seric printf(" %x=\"", *pp); 92816914Seric (void) fflush(stdout); 92916914Seric printf("%s\"", *pp++); 93016914Seric } 93116914Seric printf("\n"); 93216914Seric } 9339374Seric pp = m->first; 93456678Seric while (pp <= m->last) 9353149Seric { 93616914Seric if (avp >= &npvp[MAXATOM]) 93756678Seric { 93858151Seric syserr("554 rewrite: expansion too long"); 93959084Seric return EX_DATAERR; 94056678Seric } 94116914Seric *avp++ = *pp++; 9423149Seric } 9433149Seric } 94416914Seric else 9458226Seric { 94616914Seric /* vanilla replacement */ 9479374Seric if (avp >= &npvp[MAXATOM]) 94816889Seric { 94956678Seric toolong: 95058151Seric syserr("554 rewrite: expansion too long"); 95159084Seric return EX_DATAERR; 95216889Seric } 95359027Seric if ((*rp & 0377) != MACRODEXPAND) 95459027Seric *avp++ = rp; 95559027Seric else 95659027Seric { 95759027Seric *avp = macvalue(rp[1], e); 95859027Seric if (tTd(21, 2)) 95959027Seric printf("rewrite: RHS $&%c => \"%s\"\n", 96059027Seric rp[1], 96159027Seric *avp == NULL ? "(NULL)" : *avp); 96259027Seric if (*avp != NULL) 96359027Seric avp++; 96459027Seric } 9658226Seric } 9669374Seric } 9679374Seric *avp++ = NULL; 96816914Seric 96916914Seric /* 97056678Seric ** Check for any hostname/keyword lookups. 97116914Seric */ 97216914Seric 97316914Seric for (rvp = npvp; *rvp != NULL; rvp++) 97416914Seric { 97556678Seric char **hbrvp; 97616914Seric char **xpvp; 97716914Seric int trsize; 97856678Seric char *replac; 97956678Seric int endtoken; 98056678Seric STAB *map; 98156678Seric char *mapname; 98256678Seric char **key_rvp; 98356678Seric char **arg_rvp; 98456678Seric char **default_rvp; 98556678Seric char buf[MAXNAME + 1]; 98616914Seric char *pvpb1[MAXATOM + 1]; 98756823Seric char *argvect[10]; 98817174Seric char pvpbuf[PSBUFSIZE]; 98964404Seric char *nullpvp[1]; 99016914Seric 99158050Seric if ((**rvp & 0377) != HOSTBEGIN && 99258050Seric (**rvp & 0377) != LOOKUPBEGIN) 99316914Seric continue; 99416914Seric 99516914Seric /* 99656678Seric ** Got a hostname/keyword lookup. 99716914Seric ** 99816914Seric ** This could be optimized fairly easily. 99916914Seric */ 100016914Seric 100116914Seric hbrvp = rvp; 100258050Seric if ((**rvp & 0377) == HOSTBEGIN) 100356327Seric { 100456678Seric endtoken = HOSTEND; 100556678Seric mapname = "host"; 100656327Seric } 100756326Seric else 100856327Seric { 100956678Seric endtoken = LOOKUPEND; 101056678Seric mapname = *++rvp; 101156327Seric } 101256678Seric map = stab(mapname, ST_MAP, ST_FIND); 101356678Seric if (map == NULL) 101458151Seric syserr("554 rewrite: map %s not found", mapname); 101556678Seric 101656678Seric /* extract the match part */ 101756678Seric key_rvp = ++rvp; 101856823Seric default_rvp = NULL; 101956823Seric arg_rvp = argvect; 102056823Seric xpvp = NULL; 102156823Seric replac = pvpbuf; 102258050Seric while (*rvp != NULL && (**rvp & 0377) != endtoken) 102353654Seric { 102458050Seric int nodetype = **rvp & 0377; 102556823Seric 102656823Seric if (nodetype != CANONHOST && nodetype != CANONUSER) 102756678Seric { 102856823Seric rvp++; 102956823Seric continue; 103056823Seric } 103156823Seric 103256823Seric *rvp++ = NULL; 103356823Seric 103456823Seric if (xpvp != NULL) 103556823Seric { 103658814Seric cataddr(xpvp, NULL, replac, 103758082Seric &pvpbuf[sizeof pvpbuf] - replac, 103858082Seric '\0'); 103956823Seric *++arg_rvp = replac; 104056823Seric replac += strlen(replac) + 1; 104156823Seric xpvp = NULL; 104256823Seric } 104356823Seric switch (nodetype) 104456823Seric { 104556678Seric case CANONHOST: 104656823Seric xpvp = rvp; 104756678Seric break; 104856678Seric 104956678Seric case CANONUSER: 105056678Seric default_rvp = rvp; 105156678Seric break; 105256678Seric } 105353654Seric } 105416914Seric if (*rvp != NULL) 105516914Seric *rvp++ = NULL; 105656823Seric if (xpvp != NULL) 105756823Seric { 105858814Seric cataddr(xpvp, NULL, replac, 105958082Seric &pvpbuf[sizeof pvpbuf] - replac, 106058082Seric '\0'); 106156823Seric *++arg_rvp = replac; 106256823Seric } 106356823Seric *++arg_rvp = NULL; 106416914Seric 106516914Seric /* save the remainder of the input string */ 106616914Seric trsize = (int) (avp - rvp + 1) * sizeof *rvp; 106716914Seric bcopy((char *) rvp, (char *) pvpb1, trsize); 106816914Seric 106956678Seric /* look it up */ 107058814Seric cataddr(key_rvp, NULL, buf, sizeof buf, '\0'); 107156823Seric argvect[0] = buf; 107260538Seric if (map != NULL && bitset(MF_OPEN, map->s_map.map_mflags)) 107356836Seric { 107459084Seric auto int stat = EX_OK; 107556836Seric 107660215Seric /* XXX should try to auto-open the map here */ 107760215Seric 107858796Seric if (tTd(60, 1)) 107958796Seric printf("map_lookup(%s, %s) => ", 108058796Seric mapname, buf); 108156836Seric replac = (*map->s_map.map_class->map_lookup)(&map->s_map, 108260089Seric buf, argvect, &stat); 108358796Seric if (tTd(60, 1)) 108459084Seric printf("%s (%d)\n", 108559084Seric replac ? replac : "NOT FOUND", 108659084Seric stat); 108759084Seric 108859084Seric /* should recover if stat == EX_TEMPFAIL */ 108959084Seric if (stat == EX_TEMPFAIL) 109059084Seric rstat = stat; 109156836Seric } 109253654Seric else 109356678Seric replac = NULL; 109456678Seric 109556678Seric /* if no replacement, use default */ 109656823Seric if (replac == NULL && default_rvp != NULL) 109756823Seric { 109860089Seric /* create the default */ 109960089Seric cataddr(default_rvp, NULL, buf, sizeof buf, '\0'); 110056823Seric replac = buf; 110156823Seric } 110256823Seric 110356678Seric if (replac == NULL) 110451317Seric { 110556823Seric xpvp = key_rvp; 110653654Seric } 110764404Seric else if (*replac == '\0') 110864404Seric { 110964404Seric /* null replacement */ 111064404Seric nullpvp[0] = NULL; 111164404Seric xpvp = nullpvp; 111264404Seric } 111356678Seric else 111453654Seric { 111556678Seric /* scan the new replacement */ 111665066Seric xpvp = prescan(replac, '\0', pvpbuf, 111765066Seric sizeof pvpbuf, NULL); 111853654Seric if (xpvp == NULL) 111951317Seric { 112058403Seric /* prescan already printed error */ 112159084Seric return EX_DATAERR; 112256678Seric } 112351317Seric } 112451317Seric 112516914Seric /* append it to the token list */ 112656678Seric for (avp = hbrvp; *xpvp != NULL; xpvp++) 112756678Seric { 112817174Seric *avp++ = newstr(*xpvp); 112916920Seric if (avp >= &npvp[MAXATOM]) 113016914Seric goto toolong; 113117174Seric } 113216914Seric 113316914Seric /* restore the old trailing information */ 113456678Seric for (xpvp = pvpb1; (*avp++ = *xpvp++) != NULL; ) 113516920Seric if (avp >= &npvp[MAXATOM]) 113616914Seric goto toolong; 113717174Seric 113856678Seric break; 113916914Seric } 114016914Seric 114116914Seric /* 114216914Seric ** Check for subroutine calls. 114316914Seric */ 114416914Seric 114558050Seric if (*npvp != NULL && (**npvp & 0377) == CALLSUBR) 114656678Seric { 114759084Seric int stat; 114859084Seric 114956678Seric bcopy((char *) &npvp[2], (char *) pvp, 115056678Seric (int) (avp - npvp - 2) * sizeof *avp); 115156678Seric if (tTd(21, 3)) 115256678Seric printf("-----callsubr %s\n", npvp[1]); 1153*65071Seric stat = rewrite(pvp, atoi(npvp[1]), reclevel, e); 115459084Seric if (rstat == EX_OK || stat == EX_TEMPFAIL) 115559084Seric rstat = stat; 115665056Seric if (*pvp != NULL && (**pvp & 0377) == CANONNET) 115763581Seric rwr = NULL; 115856678Seric } 115956678Seric else 116056678Seric { 116117348Seric bcopy((char *) npvp, (char *) pvp, 116216900Seric (int) (avp - npvp) * sizeof *avp); 116356678Seric } 11649374Seric if (tTd(21, 4)) 11659374Seric { 11669374Seric printf("rewritten as:"); 116756678Seric printav(pvp); 11689374Seric } 1169297Seric } 11708069Seric 11719279Seric if (OpMode == MD_TEST || tTd(21, 2)) 11728069Seric { 11738959Seric printf("rewrite: ruleset %2d returns:", ruleset); 117456678Seric printav(pvp); 11758069Seric } 117659084Seric 117759084Seric return rstat; 11783149Seric } 11793149Seric /* 11803149Seric ** BUILDADDR -- build address from token vector. 11813149Seric ** 11823149Seric ** Parameters: 11833149Seric ** tv -- token vector. 11843149Seric ** a -- pointer to address descriptor to fill. 11853149Seric ** If NULL, one will be allocated. 118664284Seric ** flags -- info regarding whether this is a sender or 118764284Seric ** a recipient. 118858966Seric ** e -- the current envelope. 11893149Seric ** 11903149Seric ** Returns: 11914279Seric ** NULL if there was an error. 11924279Seric ** 'a' otherwise. 11933149Seric ** 11943149Seric ** Side Effects: 11953149Seric ** fills in 'a' 11963149Seric */ 11973149Seric 119857249Seric struct errcodes 119957249Seric { 120057249Seric char *ec_name; /* name of error code */ 120157249Seric int ec_code; /* numeric code */ 120257249Seric } ErrorCodes[] = 120357249Seric { 120457249Seric "usage", EX_USAGE, 120557249Seric "nouser", EX_NOUSER, 120657249Seric "nohost", EX_NOHOST, 120757249Seric "unavailable", EX_UNAVAILABLE, 120857249Seric "software", EX_SOFTWARE, 120957249Seric "tempfail", EX_TEMPFAIL, 121057249Seric "protocol", EX_PROTOCOL, 121157249Seric #ifdef EX_CONFIG 121257249Seric "config", EX_CONFIG, 121357249Seric #endif 121457249Seric NULL, EX_UNAVAILABLE, 121557249Seric }; 121657249Seric 121756678Seric ADDRESS * 121864284Seric buildaddr(tv, a, flags, e) 12193149Seric register char **tv; 12203149Seric register ADDRESS *a; 122164284Seric int flags; 122258966Seric register ENVELOPE *e; 12233149Seric { 12243149Seric struct mailer **mp; 12253149Seric register struct mailer *m; 122658008Seric char *bp; 122758008Seric int spaceleft; 122864306Seric static MAILER errormailer; 122964306Seric static char *errorargv[] = { "ERROR", NULL }; 123057402Seric static char buf[MAXNAME]; 12313149Seric 123264791Seric if (tTd(24, 5)) 123364791Seric { 123464791Seric printf("buildaddr, flags=%o, tv=", flags); 123564791Seric printav(tv); 123664791Seric } 123764791Seric 12383149Seric if (a == NULL) 12393149Seric a = (ADDRESS *) xalloc(sizeof *a); 124016889Seric bzero((char *) a, sizeof *a); 12413149Seric 12423149Seric /* figure out what net/mailer to use */ 124364306Seric if (*tv == NULL || (**tv & 0377) != CANONNET) 12444279Seric { 124558151Seric syserr("554 buildaddr: no net"); 124664306Seric badaddr: 124764306Seric a->q_flags |= QBADADDR; 124864306Seric a->q_mailer = &errormailer; 124964306Seric if (errormailer.m_name == NULL) 125064306Seric { 125164306Seric /* initialize the bogus mailer */ 125264306Seric errormailer.m_name = "*error*"; 125364306Seric errormailer.m_mailer = "ERROR"; 125464306Seric errormailer.m_argv = errorargv; 125564306Seric } 125664306Seric return a; 12574279Seric } 12583149Seric tv++; 125958680Seric if (strcasecmp(*tv, "error") == 0) 12604279Seric { 126158050Seric if ((**++tv & 0377) == CANONHOST) 126210183Seric { 126357249Seric register struct errcodes *ep; 126457249Seric 126558050Seric if (isascii(**++tv) && isdigit(**tv)) 126657249Seric { 126757249Seric setstat(atoi(*tv)); 126857249Seric } 126957249Seric else 127057249Seric { 127157249Seric for (ep = ErrorCodes; ep->ec_name != NULL; ep++) 127257249Seric if (strcasecmp(ep->ec_name, *tv) == 0) 127357249Seric break; 127457249Seric setstat(ep->ec_code); 127557249Seric } 127610183Seric tv++; 127710183Seric } 127864928Seric else 127964928Seric setstat(EX_UNAVAILABLE); 128058050Seric if ((**tv & 0377) != CANONUSER) 128158151Seric syserr("554 buildaddr: error: no user"); 128258814Seric cataddr(++tv, NULL, buf, sizeof buf, ' '); 128358082Seric stripquotes(buf); 128464659Seric if (isascii(buf[0]) && isdigit(buf[0]) && 128564659Seric isascii(buf[1]) && isdigit(buf[1]) && 128664659Seric isascii(buf[2]) && isdigit(buf[2]) && 128764659Seric buf[3] == ' ') 128864659Seric { 128964659Seric char fmt[10]; 129064659Seric 129164659Seric strncpy(fmt, buf, 3); 129264659Seric strcpy(&fmt[3], " %s"); 129364659Seric usrerr(fmt, buf + 4); 129464659Seric } 129564659Seric else 129664659Seric { 129764659Seric usrerr("%s", buf); 129864659Seric } 129964306Seric goto badaddr; 13004279Seric } 130157402Seric 13024598Seric for (mp = Mailer; (m = *mp++) != NULL; ) 13033149Seric { 130458680Seric if (strcasecmp(m->m_name, *tv) == 0) 13053149Seric break; 13063149Seric } 13073149Seric if (m == NULL) 13084279Seric { 130958151Seric syserr("554 buildaddr: unknown mailer %s", *tv); 131064306Seric goto badaddr; 13114279Seric } 13124598Seric a->q_mailer = m; 13133149Seric 13143149Seric /* figure out what host (if any) */ 131556678Seric tv++; 131658509Seric if ((**tv & 0377) == CANONHOST) 13173149Seric { 131858008Seric bp = buf; 131958008Seric spaceleft = sizeof buf - 1; 132058050Seric while (*++tv != NULL && (**tv & 0377) != CANONUSER) 132158008Seric { 132258008Seric int i = strlen(*tv); 132358008Seric 132458008Seric if (i > spaceleft) 132558008Seric { 132658008Seric /* out of space for this address */ 132758008Seric if (spaceleft >= 0) 132858151Seric syserr("554 buildaddr: host too long (%.40s...)", 132958008Seric buf); 133058008Seric i = spaceleft; 133158008Seric spaceleft = 0; 133258008Seric } 133358008Seric if (i <= 0) 133458008Seric continue; 133558008Seric bcopy(*tv, bp, i); 133658008Seric bp += i; 133758008Seric spaceleft -= i; 133858008Seric } 133958010Seric *bp = '\0'; 13405704Seric a->q_host = newstr(buf); 13413149Seric } 134257249Seric else 134358509Seric { 134458509Seric if (!bitnset(M_LOCALMAILER, m->m_flags)) 134558509Seric { 134658509Seric syserr("554 buildaddr: no host"); 134764306Seric goto badaddr; 134858509Seric } 134957249Seric a->q_host = NULL; 135058509Seric } 13513149Seric 13523149Seric /* figure out the user */ 135358050Seric if (*tv == NULL || (**tv & 0377) != CANONUSER) 13544279Seric { 135558151Seric syserr("554 buildaddr: no user"); 135664306Seric goto badaddr; 13574279Seric } 135857402Seric tv++; 135951317Seric 136057402Seric /* do special mapping for local mailer */ 136157402Seric if (m == LocalMailer && *tv != NULL) 136257402Seric { 136357454Seric register char *p = *tv; 136457454Seric 136557454Seric if (*p == '"') 136657454Seric p++; 136757454Seric if (*p == '|') 136857402Seric a->q_mailer = m = ProgMailer; 136957454Seric else if (*p == '/') 137057402Seric a->q_mailer = m = FileMailer; 137157454Seric else if (*p == ':') 137257402Seric { 137357402Seric /* may be :include: */ 137458814Seric cataddr(tv, NULL, buf, sizeof buf, '\0'); 137558008Seric stripquotes(buf); 137658008Seric if (strncasecmp(buf, ":include:", 9) == 0) 137758008Seric { 137858008Seric /* if :include:, don't need further rewriting */ 137957402Seric a->q_mailer = m = InclMailer; 138058008Seric a->q_user = &buf[9]; 138158008Seric return (a); 138258008Seric } 138357402Seric } 138457402Seric } 138557402Seric 138658008Seric if (m == LocalMailer && *tv != NULL && strcmp(*tv, "@") == 0) 138758008Seric { 138858008Seric tv++; 138958008Seric a->q_flags |= QNOTREMOTE; 139058008Seric } 139158008Seric 139264284Seric /* rewrite according recipient mailer rewriting rules */ 139364284Seric define('h', a->q_host, e); 139464284Seric if (!bitset(RF_SENDERADDR|RF_HEADERADDR, flags)) 139564284Seric { 139664284Seric /* sender addresses done later */ 1397*65071Seric (void) rewrite(tv, 2, 0, e); 139864284Seric if (m->m_re_rwset > 0) 1399*65071Seric (void) rewrite(tv, m->m_re_rwset, 0, e); 140064284Seric } 1401*65071Seric (void) rewrite(tv, 4, 0, e); 140219040Seric 140319040Seric /* save the result for the command line/RCPT argument */ 140458814Seric cataddr(tv, NULL, buf, sizeof buf, '\0'); 14053149Seric a->q_user = buf; 14063149Seric 140758670Seric /* 140858670Seric ** Do mapping to lower case as requested by mailer 140958670Seric */ 141058670Seric 141158670Seric if (a->q_host != NULL && !bitnset(M_HST_UPPER, m->m_flags)) 141258670Seric makelower(a->q_host); 141358670Seric if (!bitnset(M_USR_UPPER, m->m_flags)) 141458670Seric makelower(a->q_user); 141558670Seric 14163149Seric return (a); 14173149Seric } 14183188Seric /* 14194228Seric ** CATADDR -- concatenate pieces of addresses (putting in <LWSP> subs) 14204228Seric ** 14214228Seric ** Parameters: 14224228Seric ** pvp -- parameter vector to rebuild. 142358814Seric ** evp -- last parameter to include. Can be NULL to 142458814Seric ** use entire pvp. 14254228Seric ** buf -- buffer to build the string into. 14264228Seric ** sz -- size of buf. 142758082Seric ** spacesub -- the space separator character; if null, 142858082Seric ** use SpaceSub. 14294228Seric ** 14304228Seric ** Returns: 14314228Seric ** none. 14324228Seric ** 14334228Seric ** Side Effects: 14344228Seric ** Destroys buf. 14354228Seric */ 14364228Seric 143758814Seric cataddr(pvp, evp, buf, sz, spacesub) 14384228Seric char **pvp; 143958814Seric char **evp; 14404228Seric char *buf; 14414228Seric register int sz; 144258082Seric char spacesub; 14434228Seric { 14444228Seric bool oatomtok = FALSE; 144556678Seric bool natomtok = FALSE; 14464228Seric register int i; 14474228Seric register char *p; 14484228Seric 144958082Seric if (spacesub == '\0') 145058082Seric spacesub = SpaceSub; 145158082Seric 14528423Seric if (pvp == NULL) 14538423Seric { 145423109Seric (void) strcpy(buf, ""); 14558423Seric return; 14568423Seric } 14574228Seric p = buf; 145811156Seric sz -= 2; 14594228Seric while (*pvp != NULL && (i = strlen(*pvp)) < sz) 14604228Seric { 14618078Seric natomtok = (toktype(**pvp) == ATM); 14624228Seric if (oatomtok && natomtok) 146358082Seric *p++ = spacesub; 14644228Seric (void) strcpy(p, *pvp); 14654228Seric oatomtok = natomtok; 14664228Seric p += i; 146711156Seric sz -= i + 1; 146858814Seric if (pvp++ == evp) 146958814Seric break; 14704228Seric } 14714228Seric *p = '\0'; 14724228Seric } 14734228Seric /* 14743188Seric ** SAMEADDR -- Determine if two addresses are the same 14753188Seric ** 14763188Seric ** This is not just a straight comparison -- if the mailer doesn't 14773188Seric ** care about the host we just ignore it, etc. 14783188Seric ** 14793188Seric ** Parameters: 14803188Seric ** a, b -- pointers to the internal forms to compare. 14813188Seric ** 14823188Seric ** Returns: 14833188Seric ** TRUE -- they represent the same mailbox. 14843188Seric ** FALSE -- they don't. 14853188Seric ** 14863188Seric ** Side Effects: 14873188Seric ** none. 14883188Seric */ 14893188Seric 14903188Seric bool 14919374Seric sameaddr(a, b) 14923188Seric register ADDRESS *a; 14933188Seric register ADDRESS *b; 14943188Seric { 14953188Seric /* if they don't have the same mailer, forget it */ 14963188Seric if (a->q_mailer != b->q_mailer) 14973188Seric return (FALSE); 14983188Seric 14993188Seric /* if the user isn't the same, we can drop out */ 150056678Seric if (strcmp(a->q_user, b->q_user) != 0) 15013188Seric return (FALSE); 15023188Seric 150358438Seric /* if we have good uids for both but the differ, these are different */ 150458438Seric if (bitset(QGOODUID, a->q_flags & b->q_flags) && a->q_uid != b->q_uid) 150558438Seric return (FALSE); 150658438Seric 150758509Seric /* otherwise compare hosts (but be careful for NULL ptrs) */ 150858509Seric if (a->q_host == b->q_host) 150958509Seric { 151058509Seric /* probably both null pointers */ 15113188Seric return (TRUE); 151258509Seric } 15133188Seric if (a->q_host == NULL || b->q_host == NULL) 151458509Seric { 151558509Seric /* only one is a null pointer */ 15163188Seric return (FALSE); 151758509Seric } 151856678Seric if (strcmp(a->q_host, b->q_host) != 0) 15193188Seric return (FALSE); 15203188Seric 15213188Seric return (TRUE); 15223188Seric } 15233234Seric /* 15243234Seric ** PRINTADDR -- print address (for debugging) 15253234Seric ** 15263234Seric ** Parameters: 15273234Seric ** a -- the address to print 15283234Seric ** follow -- follow the q_next chain. 15293234Seric ** 15303234Seric ** Returns: 15313234Seric ** none. 15323234Seric ** 15333234Seric ** Side Effects: 15343234Seric ** none. 15353234Seric */ 15363234Seric 15373234Seric printaddr(a, follow) 15383234Seric register ADDRESS *a; 15393234Seric bool follow; 15403234Seric { 15415001Seric bool first = TRUE; 154257731Seric register MAILER *m; 154357731Seric MAILER pseudomailer; 15445001Seric 15453234Seric while (a != NULL) 15463234Seric { 15475001Seric first = FALSE; 15484443Seric printf("%x=", a); 15494085Seric (void) fflush(stdout); 155057731Seric 155157731Seric /* find the mailer -- carefully */ 155257731Seric m = a->q_mailer; 155357731Seric if (m == NULL) 155457731Seric { 155557731Seric m = &pseudomailer; 155657731Seric m->m_mno = -1; 155757731Seric m->m_name = "NULL"; 155857731Seric } 155957731Seric 156058680Seric printf("%s:\n\tmailer %d (%s), host `%s', user `%s', ruser `%s'\n", 156157731Seric a->q_paddr, m->m_mno, m->m_name, 156263756Seric a->q_host, a->q_user, 156363756Seric a->q_ruser ? a->q_ruser : "<null>"); 156459269Seric printf("\tnext=%x, flags=%o, alias %x, uid %d, gid %d\n", 156559269Seric a->q_next, a->q_flags, a->q_alias, a->q_uid, a->q_gid); 156659269Seric printf("\towner=%s, home=\"%s\", fullname=\"%s\"\n", 156759269Seric a->q_owner == NULL ? "(none)" : a->q_owner, 156863756Seric a->q_home == NULL ? "(none)" : a->q_home, 156963756Seric a->q_fullname == NULL ? "(none)" : a->q_fullname); 15704996Seric 15713234Seric if (!follow) 15723234Seric return; 15734996Seric a = a->q_next; 15743234Seric } 15755001Seric if (first) 15764443Seric printf("[NULL]\n"); 15773234Seric } 15784317Seric 15797682Seric /* 15807682Seric ** REMOTENAME -- return the name relative to the current mailer 15817682Seric ** 15827682Seric ** Parameters: 15837682Seric ** name -- the name to translate. 15848069Seric ** m -- the mailer that we want to do rewriting relative 15858069Seric ** to. 158659163Seric ** flags -- fine tune operations. 158759163Seric ** pstat -- pointer to status word. 158858020Seric ** e -- the current envelope. 15897682Seric ** 15907682Seric ** Returns: 15917682Seric ** the text string representing this address relative to 15927682Seric ** the receiving mailer. 15937682Seric ** 15947682Seric ** Side Effects: 15957682Seric ** none. 15967682Seric ** 15977682Seric ** Warnings: 15987682Seric ** The text string returned is tucked away locally; 15997682Seric ** copy it if you intend to save it. 16007682Seric */ 16017682Seric 16027682Seric char * 160359163Seric remotename(name, m, flags, pstat, e) 16047682Seric char *name; 160556678Seric struct mailer *m; 160659163Seric int flags; 160759163Seric int *pstat; 160856678Seric register ENVELOPE *e; 16097682Seric { 16108069Seric register char **pvp; 16118069Seric char *fancy; 161256678Seric char *oldg = macvalue('g', e); 161358020Seric int rwset; 16147682Seric static char buf[MAXNAME]; 16157682Seric char lbuf[MAXNAME]; 161616914Seric char pvpbuf[PSBUFSIZE]; 161756678Seric extern char *crackaddr(); 16187682Seric 16197755Seric if (tTd(12, 1)) 16207755Seric printf("remotename(%s)\n", name); 16217755Seric 162210177Seric /* don't do anything if we are tagging it as special */ 162359163Seric if (bitset(RF_SENDERADDR, flags)) 162459163Seric rwset = bitset(RF_HEADERADDR, flags) ? m->m_sh_rwset 162559163Seric : m->m_se_rwset; 162658020Seric else 162759163Seric rwset = bitset(RF_HEADERADDR, flags) ? m->m_rh_rwset 162859163Seric : m->m_re_rwset; 162958020Seric if (rwset < 0) 163010177Seric return (name); 163110177Seric 16327682Seric /* 16338181Seric ** Do a heuristic crack of this name to extract any comment info. 16348181Seric ** This will leave the name as a comment and a $g macro. 16357889Seric */ 16367889Seric 163759163Seric if (bitset(RF_CANONICAL, flags) || bitnset(M_NOCOMMENT, m->m_flags)) 163858050Seric fancy = "\201g"; 163910310Seric else 164010310Seric fancy = crackaddr(name); 16417889Seric 16428181Seric /* 16438181Seric ** Turn the name into canonical form. 16448181Seric ** Normally this will be RFC 822 style, i.e., "user@domain". 16458181Seric ** If this only resolves to "user", and the "C" flag is 16468181Seric ** specified in the sending mailer, then the sender's 16478181Seric ** domain will be appended. 16488181Seric */ 16498181Seric 165065066Seric pvp = prescan(name, '\0', pvpbuf, sizeof pvpbuf, NULL); 16517889Seric if (pvp == NULL) 16527889Seric return (name); 1653*65071Seric if (rewrite(pvp, 3, 0, e) == EX_TEMPFAIL) 165459163Seric *pstat = EX_TEMPFAIL; 165559163Seric if (bitset(RF_ADDDOMAIN, flags) && e->e_fromdomain != NULL) 16568181Seric { 16578181Seric /* append from domain to this address */ 16588181Seric register char **pxp = pvp; 16598181Seric 16609594Seric /* see if there is an "@domain" in the current name */ 16618181Seric while (*pxp != NULL && strcmp(*pxp, "@") != 0) 16628181Seric pxp++; 16638181Seric if (*pxp == NULL) 16648181Seric { 16659594Seric /* no.... append the "@domain" from the sender */ 166656678Seric register char **qxq = e->e_fromdomain; 16678181Seric 16689594Seric while ((*pxp++ = *qxq++) != NULL) 16699594Seric continue; 1670*65071Seric if (rewrite(pvp, 3, 0, e) == EX_TEMPFAIL) 167159163Seric *pstat = EX_TEMPFAIL; 16728181Seric } 16738181Seric } 16748181Seric 16758181Seric /* 16768959Seric ** Do more specific rewriting. 167756678Seric ** Rewrite using ruleset 1 or 2 depending on whether this is 167856678Seric ** a sender address or not. 16798181Seric ** Then run it through any receiving-mailer-specific rulesets. 16808181Seric */ 16818181Seric 168259163Seric if (bitset(RF_SENDERADDR, flags)) 168359541Seric { 1684*65071Seric if (rewrite(pvp, 1, 0, e) == EX_TEMPFAIL) 168559163Seric *pstat = EX_TEMPFAIL; 168659541Seric } 16878069Seric else 168859541Seric { 1689*65071Seric if (rewrite(pvp, 2, 0, e) == EX_TEMPFAIL) 169059163Seric *pstat = EX_TEMPFAIL; 169159541Seric } 169258020Seric if (rwset > 0) 169359541Seric { 1694*65071Seric if (rewrite(pvp, rwset, 0, e) == EX_TEMPFAIL) 169559163Seric *pstat = EX_TEMPFAIL; 169659541Seric } 16977682Seric 16988181Seric /* 16998959Seric ** Do any final sanitation the address may require. 17008959Seric ** This will normally be used to turn internal forms 17018959Seric ** (e.g., user@host.LOCAL) into external form. This 17028959Seric ** may be used as a default to the above rules. 17038959Seric */ 17048959Seric 1705*65071Seric if (rewrite(pvp, 4, 0, e) == EX_TEMPFAIL) 170659163Seric *pstat = EX_TEMPFAIL; 17078959Seric 17088959Seric /* 17098181Seric ** Now restore the comment information we had at the beginning. 17108181Seric */ 17118181Seric 171258825Seric cataddr(pvp, NULL, lbuf, sizeof lbuf, '\0'); 171356678Seric define('g', lbuf, e); 171456678Seric expand(fancy, buf, &buf[sizeof buf - 1], e); 171556678Seric define('g', oldg, e); 17167682Seric 17177682Seric if (tTd(12, 1)) 17187755Seric printf("remotename => `%s'\n", buf); 17197682Seric return (buf); 17207682Seric } 172151317Seric /* 172256678Seric ** MAPLOCALUSER -- run local username through ruleset 5 for final redirection 172351317Seric ** 172451317Seric ** Parameters: 172556678Seric ** a -- the address to map (but just the user name part). 172656678Seric ** sendq -- the sendq in which to install any replacement 172756678Seric ** addresses. 172851317Seric ** 172951317Seric ** Returns: 173051317Seric ** none. 173151317Seric */ 173251317Seric 173356678Seric maplocaluser(a, sendq, e) 173456678Seric register ADDRESS *a; 173556678Seric ADDRESS **sendq; 173656678Seric ENVELOPE *e; 173751317Seric { 173856678Seric register char **pvp; 173956678Seric register ADDRESS *a1 = NULL; 174058333Seric auto char *delimptr; 174156678Seric char pvpbuf[PSBUFSIZE]; 174251317Seric 174356678Seric if (tTd(29, 1)) 174456678Seric { 174556678Seric printf("maplocaluser: "); 174656678Seric printaddr(a, FALSE); 174756678Seric } 174865066Seric pvp = prescan(a->q_user, '\0', pvpbuf, sizeof pvpbuf, &delimptr); 174956678Seric if (pvp == NULL) 175056678Seric return; 175151317Seric 1752*65071Seric (void) rewrite(pvp, 5, 0, e); 175358050Seric if (pvp[0] == NULL || (pvp[0][0] & 0377) != CANONNET) 175456678Seric return; 175551317Seric 175656678Seric /* if non-null, mailer destination specified -- has it changed? */ 175764284Seric a1 = buildaddr(pvp, NULL, 0, e); 175856678Seric if (a1 == NULL || sameaddr(a, a1)) 175956678Seric return; 176051317Seric 176156678Seric /* mark old address as dead; insert new address */ 176256678Seric a->q_flags |= QDONTSEND; 176357731Seric if (tTd(29, 5)) 176457731Seric { 176557731Seric printf("maplocaluser: QDONTSEND "); 176657731Seric printaddr(a, FALSE); 176757731Seric } 176856678Seric a1->q_alias = a; 176964348Seric allocaddr(a1, RF_COPYALL, NULL); 177056678Seric (void) recipient(a1, sendq, e); 177151317Seric } 177258802Seric /* 177358802Seric ** DEQUOTE_INIT -- initialize dequote map 177458802Seric ** 177558802Seric ** This is a no-op. 177658802Seric ** 177758802Seric ** Parameters: 177858802Seric ** map -- the internal map structure. 177958802Seric ** args -- arguments. 178058802Seric ** 178158802Seric ** Returns: 178258802Seric ** TRUE. 178358802Seric */ 178458802Seric 178558802Seric bool 178660219Seric dequote_init(map, args) 178758802Seric MAP *map; 178858802Seric char *args; 178958802Seric { 179058805Seric register char *p = args; 179158805Seric 179258805Seric for (;;) 179358805Seric { 179458805Seric while (isascii(*p) && isspace(*p)) 179558805Seric p++; 179658805Seric if (*p != '-') 179758805Seric break; 179858805Seric switch (*++p) 179958805Seric { 180058805Seric case 'a': 180158805Seric map->map_app = ++p; 180258805Seric break; 180358805Seric } 180458805Seric while (*p != '\0' && !(isascii(*p) && isspace(*p))) 180558805Seric p++; 180658805Seric if (*p != '\0') 180758805Seric *p = '\0'; 180858805Seric } 180958805Seric if (map->map_app != NULL) 181058805Seric map->map_app = newstr(map->map_app); 181158805Seric 181258802Seric return TRUE; 181358802Seric } 181458802Seric /* 181558802Seric ** DEQUOTE_MAP -- unquote an address 181658802Seric ** 181758802Seric ** Parameters: 181858802Seric ** map -- the internal map structure (ignored). 181960089Seric ** name -- the name to dequote. 182058802Seric ** av -- arguments (ignored). 182159084Seric ** statp -- pointer to status out-parameter. 182258802Seric ** 182358802Seric ** Returns: 182458802Seric ** NULL -- if there were no quotes, or if the resulting 182558802Seric ** unquoted buffer would not be acceptable to prescan. 182658802Seric ** else -- The dequoted buffer. 182758802Seric */ 182858802Seric 182958802Seric char * 183060089Seric dequote_map(map, name, av, statp) 183158802Seric MAP *map; 183260089Seric char *name; 183358802Seric char **av; 183459084Seric int *statp; 183558802Seric { 183658802Seric register char *p; 183758802Seric register char *q; 183858802Seric register char c; 183958802Seric int anglecnt; 184058802Seric int cmntcnt; 184158802Seric int quotecnt; 184259089Seric int spacecnt; 184358802Seric bool quotemode; 184458802Seric bool bslashmode; 184558802Seric 184658802Seric anglecnt = 0; 184758802Seric cmntcnt = 0; 184858802Seric quotecnt = 0; 184959089Seric spacecnt = 0; 185058802Seric quotemode = FALSE; 185158802Seric bslashmode = FALSE; 185258802Seric 185360089Seric for (p = q = name; (c = *p++) != '\0'; ) 185458802Seric { 185558802Seric if (bslashmode) 185658802Seric { 185758802Seric bslashmode = FALSE; 185858802Seric *q++ = c; 185958802Seric continue; 186058802Seric } 186158802Seric 186258802Seric switch (c) 186358802Seric { 186458802Seric case '\\': 186558802Seric bslashmode = TRUE; 186658802Seric break; 186758802Seric 186858802Seric case '(': 186958802Seric cmntcnt++; 187058802Seric break; 187158802Seric 187258802Seric case ')': 187358802Seric if (cmntcnt-- <= 0) 187458802Seric return NULL; 187558802Seric break; 187659089Seric 187759089Seric case ' ': 187859089Seric spacecnt++; 187959089Seric break; 188058802Seric } 188158802Seric 188258802Seric if (cmntcnt > 0) 188358802Seric { 188458802Seric *q++ = c; 188558802Seric continue; 188658802Seric } 188758802Seric 188858802Seric switch (c) 188958802Seric { 189058802Seric case '"': 189158802Seric quotemode = !quotemode; 189258802Seric quotecnt++; 189358802Seric continue; 189458802Seric 189558802Seric case '<': 189658802Seric anglecnt++; 189758802Seric break; 189858802Seric 189958802Seric case '>': 190058802Seric if (anglecnt-- <= 0) 190158802Seric return NULL; 190258802Seric break; 190358802Seric } 190458802Seric *q++ = c; 190558802Seric } 190658802Seric 190758802Seric if (anglecnt != 0 || cmntcnt != 0 || bslashmode || 190859089Seric quotemode || quotecnt <= 0 || spacecnt != 0) 190958802Seric return NULL; 191058802Seric *q++ = '\0'; 191160089Seric return name; 191258802Seric } 1913