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*65066Seric static char sccsid[] = "@(#)parseaddr.c 8.22 (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 80*65066Seric 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; 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) 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. 285*65066Seric ** 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 ** 322*65066Seric 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 */ 371*65066Seric 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. 60159027Seric ** e -- the current envelope. 6023149Seric ** 6033149Seric ** Returns: 60459084Seric ** A status code. If EX_TEMPFAIL, higher level code should 60559084Seric ** attempt recovery. 6063149Seric ** 6073149Seric ** Side Effects: 6083149Seric ** pvp is modified. 6093149Seric */ 6102091Seric 6113149Seric struct match 6123149Seric { 6134468Seric char **first; /* first token matched */ 6144468Seric char **last; /* last token matched */ 61558825Seric char **pattern; /* pointer to pattern */ 6163149Seric }; 6173149Seric 6184468Seric # define MAXMATCH 9 /* max params per rewrite */ 6193149Seric 6203149Seric 62159084Seric int 62259027Seric rewrite(pvp, ruleset, e) 6233149Seric char **pvp; 6244070Seric int ruleset; 62559027Seric register ENVELOPE *e; 6263149Seric { 6273149Seric register char *ap; /* address pointer */ 6283149Seric register char *rp; /* rewrite pointer */ 6293149Seric register char **avp; /* address vector pointer */ 6303149Seric register char **rvp; /* rewrite vector pointer */ 6318058Seric register struct match *mlp; /* cur ptr into mlist */ 6328058Seric register struct rewrite *rwr; /* pointer to current rewrite rule */ 63358866Seric int ruleno; /* current rule number */ 63459084Seric int rstat = EX_OK; /* return status */ 63564740Seric int loopcount; 63656678Seric struct match mlist[MAXMATCH]; /* stores match on LHS */ 6373149Seric char *npvp[MAXATOM+1]; /* temporary space for rebuild */ 6383149Seric 6399279Seric if (OpMode == MD_TEST || tTd(21, 2)) 6403149Seric { 6418959Seric printf("rewrite: ruleset %2d input:", ruleset); 64256678Seric printav(pvp); 6433149Seric } 64456678Seric if (ruleset < 0 || ruleset >= MAXRWSETS) 64556326Seric { 64658151Seric syserr("554 rewrite: illegal ruleset number %d", ruleset); 64759084Seric return EX_CONFIG; 64856326Seric } 64956678Seric if (pvp == NULL) 65059084Seric return EX_USAGE; 65156326Seric 6523149Seric /* 65356678Seric ** Run through the list of rewrite rules, applying 65456678Seric ** any that match. 6553149Seric */ 6563149Seric 65758866Seric ruleno = 1; 65864740Seric loopcount = 0; 6594070Seric for (rwr = RewriteRules[ruleset]; rwr != NULL; ) 6603149Seric { 6617675Seric if (tTd(21, 12)) 662297Seric { 6638069Seric printf("-----trying rule:"); 66456678Seric printav(rwr->r_lhs); 6653149Seric } 6663149Seric 6673149Seric /* try to match on this rule */ 6684468Seric mlp = mlist; 6698058Seric rvp = rwr->r_lhs; 6708058Seric avp = pvp; 67158866Seric if (++loopcount > 100) 6723149Seric { 67358866Seric syserr("554 Infinite loop in ruleset %d, rule %d", 67458866Seric ruleset, ruleno); 67558866Seric if (tTd(21, 1)) 67652637Seric { 67756678Seric printf("workspace: "); 67856678Seric printav(pvp); 67952637Seric } 68058866Seric break; 68158866Seric } 68258866Seric 68358866Seric while ((ap = *avp) != NULL || *rvp != NULL) 68458866Seric { 6853149Seric rp = *rvp; 6868058Seric if (tTd(21, 35)) 6878058Seric { 68858825Seric printf("ADVANCE rp="); 68957531Seric xputs(rp); 69057532Seric printf(", ap="); 6918058Seric xputs(ap); 6928069Seric printf("\n"); 6938058Seric } 69456678Seric if (rp == NULL) 69556326Seric { 6963149Seric /* end-of-pattern before end-of-address */ 6978058Seric goto backup; 69856678Seric } 69958173Seric if (ap == NULL && (*rp & 0377) != MATCHZANY && 70058827Seric (*rp & 0377) != MATCHZERO) 70156326Seric { 70258825Seric /* end-of-input with patterns left */ 70358825Seric goto backup; 704297Seric } 70556326Seric 70658050Seric switch (*rp & 0377) 7078058Seric { 70856678Seric register STAB *s; 70958814Seric char buf[MAXLINE]; 71056326Seric 71156678Seric case MATCHCLASS: 71258825Seric /* match any phrase in a class */ 71358825Seric mlp->pattern = rvp; 71458814Seric mlp->first = avp; 71558814Seric extendclass: 71658825Seric ap = *avp; 71758825Seric if (ap == NULL) 71858814Seric goto backup; 71958814Seric mlp->last = avp++; 72058814Seric cataddr(mlp->first, mlp->last, buf, sizeof buf, '\0'); 72158814Seric s = stab(buf, ST_CLASS, ST_FIND); 72256678Seric if (s == NULL || !bitnset(rp[1], s->s_class)) 72356326Seric { 72458825Seric if (tTd(21, 36)) 72558825Seric { 72658825Seric printf("EXTEND rp="); 72758825Seric xputs(rp); 72858825Seric printf(", ap="); 72958825Seric xputs(ap); 73058825Seric printf("\n"); 73158825Seric } 73258825Seric goto extendclass; 73356326Seric } 73458825Seric if (tTd(21, 36)) 73558825Seric printf("CLMATCH\n"); 73658814Seric mlp++; 73758814Seric break; 7384060Seric 73958825Seric case MATCHNCLASS: 74058825Seric /* match any token not in a class */ 74158825Seric s = stab(ap, ST_CLASS, ST_FIND); 74258825Seric if (s != NULL && bitnset(rp[1], s->s_class)) 74358825Seric goto backup; 74458825Seric 74558825Seric /* fall through */ 74658825Seric 74756678Seric case MATCHONE: 74856678Seric case MATCHANY: 74956678Seric /* match exactly one token */ 75058825Seric mlp->pattern = rvp; 75156678Seric mlp->first = avp; 75256678Seric mlp->last = avp++; 7538058Seric mlp++; 75456678Seric break; 7558058Seric 75656678Seric case MATCHZANY: 75756678Seric /* match zero or more tokens */ 75858825Seric mlp->pattern = rvp; 75956678Seric mlp->first = avp; 76056678Seric mlp->last = avp - 1; 76156678Seric mlp++; 76256678Seric break; 76356326Seric 76458827Seric case MATCHZERO: 76558173Seric /* match zero tokens */ 76658173Seric break; 76758173Seric 76859027Seric case MACRODEXPAND: 76959027Seric /* 77059027Seric ** Match against run-time macro. 77159027Seric ** This algorithm is broken for the 77259027Seric ** general case (no recursive macros, 77359027Seric ** improper tokenization) but should 77459027Seric ** work for the usual cases. 77559027Seric */ 77659027Seric 77759027Seric ap = macvalue(rp[1], e); 77859027Seric mlp->first = avp; 77959027Seric if (tTd(21, 2)) 78059027Seric printf("rewrite: LHS $&%c => \"%s\"\n", 78159027Seric rp[1], 78259027Seric ap == NULL ? "(NULL)" : ap); 78359027Seric 78459027Seric if (ap == NULL) 78559027Seric break; 78660502Seric while (*ap != '\0') 78759027Seric { 78859027Seric if (*avp == NULL || 78959027Seric strncasecmp(ap, *avp, strlen(*avp)) != 0) 79059027Seric { 79159027Seric /* no match */ 79259027Seric avp = mlp->first; 79359027Seric goto backup; 79459027Seric } 79559027Seric ap += strlen(*avp++); 79659027Seric } 79759027Seric 79859027Seric /* match */ 79959027Seric break; 80059027Seric 80156678Seric default: 80256678Seric /* must have exact match */ 80356678Seric if (strcasecmp(rp, ap)) 8048058Seric goto backup; 8054468Seric avp++; 80656678Seric break; 8073149Seric } 8083149Seric 80956678Seric /* successful match on this token */ 8103149Seric rvp++; 8113149Seric continue; 8123149Seric 81358825Seric backup: 81456678Seric /* match failed -- back up */ 81558825Seric while (--mlp >= mlist) 8163149Seric { 81758825Seric rvp = mlp->pattern; 81856678Seric rp = *rvp; 81958825Seric avp = mlp->last + 1; 82058825Seric ap = *avp; 82158825Seric 82258825Seric if (tTd(21, 36)) 82358825Seric { 82458825Seric printf("BACKUP rp="); 82558825Seric xputs(rp); 82658825Seric printf(", ap="); 82758825Seric xputs(ap); 82858825Seric printf("\n"); 82958825Seric } 83058825Seric 83158825Seric if (ap == NULL) 83258825Seric { 83358825Seric /* run off the end -- back up again */ 83458825Seric continue; 83558825Seric } 83658050Seric if ((*rp & 0377) == MATCHANY || 83758050Seric (*rp & 0377) == MATCHZANY) 8384468Seric { 83956678Seric /* extend binding and continue */ 84058825Seric mlp->last = avp++; 84156678Seric rvp++; 84258825Seric mlp++; 84356678Seric break; 8444468Seric } 84558825Seric if ((*rp & 0377) == MATCHCLASS) 84656678Seric { 84758814Seric /* extend binding and try again */ 84863397Seric mlp->last = avp; 84958814Seric goto extendclass; 85058814Seric } 8513149Seric } 8523149Seric 85358825Seric if (mlp < mlist) 85456678Seric { 85556678Seric /* total failure to match */ 85656326Seric break; 8573149Seric } 858297Seric } 8593149Seric 8603149Seric /* 86156678Seric ** See if we successfully matched 8623149Seric */ 8633149Seric 86458827Seric if (mlp < mlist || *rvp != NULL) 8653149Seric { 8669374Seric if (tTd(21, 10)) 8679374Seric printf("----- rule fails\n"); 8689374Seric rwr = rwr->r_next; 86958866Seric ruleno++; 87064740Seric loopcount = 0; 8719374Seric continue; 8729374Seric } 8733149Seric 8749374Seric rvp = rwr->r_rhs; 8759374Seric if (tTd(21, 12)) 8769374Seric { 8779374Seric printf("-----rule matches:"); 87856678Seric printav(rvp); 8799374Seric } 8809374Seric 8819374Seric rp = *rvp; 88258050Seric if ((*rp & 0377) == CANONUSER) 8839374Seric { 8849374Seric rvp++; 8859374Seric rwr = rwr->r_next; 88658866Seric ruleno++; 88764740Seric loopcount = 0; 8889374Seric } 88958050Seric else if ((*rp & 0377) == CANONHOST) 8909374Seric { 8919374Seric rvp++; 8929374Seric rwr = NULL; 8939374Seric } 89458050Seric else if ((*rp & 0377) == CANONNET) 8959374Seric rwr = NULL; 8969374Seric 8979374Seric /* substitute */ 8989374Seric for (avp = npvp; *rvp != NULL; rvp++) 8999374Seric { 9009374Seric register struct match *m; 9019374Seric register char **pp; 9029374Seric 9038058Seric rp = *rvp; 90458050Seric if ((*rp & 0377) == MATCHREPL) 9058058Seric { 90616914Seric /* substitute from LHS */ 90716914Seric m = &mlist[rp[1] - '1']; 90856678Seric if (m < mlist || m >= mlp) 9099374Seric { 91058151Seric syserr("554 rewrite: ruleset %d: replacement $%c out of bounds", 91156326Seric ruleset, rp[1]); 91259084Seric return EX_CONFIG; 9139374Seric } 91416914Seric if (tTd(21, 15)) 91516914Seric { 91616914Seric printf("$%c:", rp[1]); 91716914Seric pp = m->first; 91856678Seric while (pp <= m->last) 91916914Seric { 92016914Seric printf(" %x=\"", *pp); 92116914Seric (void) fflush(stdout); 92216914Seric printf("%s\"", *pp++); 92316914Seric } 92416914Seric printf("\n"); 92516914Seric } 9269374Seric pp = m->first; 92756678Seric while (pp <= m->last) 9283149Seric { 92916914Seric if (avp >= &npvp[MAXATOM]) 93056678Seric { 93158151Seric syserr("554 rewrite: expansion too long"); 93259084Seric return EX_DATAERR; 93356678Seric } 93416914Seric *avp++ = *pp++; 9353149Seric } 9363149Seric } 93716914Seric else 9388226Seric { 93916914Seric /* vanilla replacement */ 9409374Seric if (avp >= &npvp[MAXATOM]) 94116889Seric { 94256678Seric toolong: 94358151Seric syserr("554 rewrite: expansion too long"); 94459084Seric return EX_DATAERR; 94516889Seric } 94659027Seric if ((*rp & 0377) != MACRODEXPAND) 94759027Seric *avp++ = rp; 94859027Seric else 94959027Seric { 95059027Seric *avp = macvalue(rp[1], e); 95159027Seric if (tTd(21, 2)) 95259027Seric printf("rewrite: RHS $&%c => \"%s\"\n", 95359027Seric rp[1], 95459027Seric *avp == NULL ? "(NULL)" : *avp); 95559027Seric if (*avp != NULL) 95659027Seric avp++; 95759027Seric } 9588226Seric } 9599374Seric } 9609374Seric *avp++ = NULL; 96116914Seric 96216914Seric /* 96356678Seric ** Check for any hostname/keyword lookups. 96416914Seric */ 96516914Seric 96616914Seric for (rvp = npvp; *rvp != NULL; rvp++) 96716914Seric { 96856678Seric char **hbrvp; 96916914Seric char **xpvp; 97016914Seric int trsize; 97156678Seric char *replac; 97256678Seric int endtoken; 97356678Seric STAB *map; 97456678Seric char *mapname; 97556678Seric char **key_rvp; 97656678Seric char **arg_rvp; 97756678Seric char **default_rvp; 97856678Seric char buf[MAXNAME + 1]; 97916914Seric char *pvpb1[MAXATOM + 1]; 98056823Seric char *argvect[10]; 98117174Seric char pvpbuf[PSBUFSIZE]; 98264404Seric char *nullpvp[1]; 98316914Seric 98458050Seric if ((**rvp & 0377) != HOSTBEGIN && 98558050Seric (**rvp & 0377) != LOOKUPBEGIN) 98616914Seric continue; 98716914Seric 98816914Seric /* 98956678Seric ** Got a hostname/keyword lookup. 99016914Seric ** 99116914Seric ** This could be optimized fairly easily. 99216914Seric */ 99316914Seric 99416914Seric hbrvp = rvp; 99558050Seric if ((**rvp & 0377) == HOSTBEGIN) 99656327Seric { 99756678Seric endtoken = HOSTEND; 99856678Seric mapname = "host"; 99956327Seric } 100056326Seric else 100156327Seric { 100256678Seric endtoken = LOOKUPEND; 100356678Seric mapname = *++rvp; 100456327Seric } 100556678Seric map = stab(mapname, ST_MAP, ST_FIND); 100656678Seric if (map == NULL) 100758151Seric syserr("554 rewrite: map %s not found", mapname); 100856678Seric 100956678Seric /* extract the match part */ 101056678Seric key_rvp = ++rvp; 101156823Seric default_rvp = NULL; 101256823Seric arg_rvp = argvect; 101356823Seric xpvp = NULL; 101456823Seric replac = pvpbuf; 101558050Seric while (*rvp != NULL && (**rvp & 0377) != endtoken) 101653654Seric { 101758050Seric int nodetype = **rvp & 0377; 101856823Seric 101956823Seric if (nodetype != CANONHOST && nodetype != CANONUSER) 102056678Seric { 102156823Seric rvp++; 102256823Seric continue; 102356823Seric } 102456823Seric 102556823Seric *rvp++ = NULL; 102656823Seric 102756823Seric if (xpvp != NULL) 102856823Seric { 102958814Seric cataddr(xpvp, NULL, replac, 103058082Seric &pvpbuf[sizeof pvpbuf] - replac, 103158082Seric '\0'); 103256823Seric *++arg_rvp = replac; 103356823Seric replac += strlen(replac) + 1; 103456823Seric xpvp = NULL; 103556823Seric } 103656823Seric switch (nodetype) 103756823Seric { 103856678Seric case CANONHOST: 103956823Seric xpvp = rvp; 104056678Seric break; 104156678Seric 104256678Seric case CANONUSER: 104356678Seric default_rvp = rvp; 104456678Seric break; 104556678Seric } 104653654Seric } 104716914Seric if (*rvp != NULL) 104816914Seric *rvp++ = NULL; 104956823Seric if (xpvp != NULL) 105056823Seric { 105158814Seric cataddr(xpvp, NULL, replac, 105258082Seric &pvpbuf[sizeof pvpbuf] - replac, 105358082Seric '\0'); 105456823Seric *++arg_rvp = replac; 105556823Seric } 105656823Seric *++arg_rvp = NULL; 105716914Seric 105816914Seric /* save the remainder of the input string */ 105916914Seric trsize = (int) (avp - rvp + 1) * sizeof *rvp; 106016914Seric bcopy((char *) rvp, (char *) pvpb1, trsize); 106116914Seric 106256678Seric /* look it up */ 106358814Seric cataddr(key_rvp, NULL, buf, sizeof buf, '\0'); 106456823Seric argvect[0] = buf; 106560538Seric if (map != NULL && bitset(MF_OPEN, map->s_map.map_mflags)) 106656836Seric { 106759084Seric auto int stat = EX_OK; 106856836Seric 106960215Seric /* XXX should try to auto-open the map here */ 107060215Seric 107158796Seric if (tTd(60, 1)) 107258796Seric printf("map_lookup(%s, %s) => ", 107358796Seric mapname, buf); 107456836Seric replac = (*map->s_map.map_class->map_lookup)(&map->s_map, 107560089Seric buf, argvect, &stat); 107658796Seric if (tTd(60, 1)) 107759084Seric printf("%s (%d)\n", 107859084Seric replac ? replac : "NOT FOUND", 107959084Seric stat); 108059084Seric 108159084Seric /* should recover if stat == EX_TEMPFAIL */ 108259084Seric if (stat == EX_TEMPFAIL) 108359084Seric rstat = stat; 108456836Seric } 108553654Seric else 108656678Seric replac = NULL; 108756678Seric 108856678Seric /* if no replacement, use default */ 108956823Seric if (replac == NULL && default_rvp != NULL) 109056823Seric { 109160089Seric /* create the default */ 109260089Seric cataddr(default_rvp, NULL, buf, sizeof buf, '\0'); 109356823Seric replac = buf; 109456823Seric } 109556823Seric 109656678Seric if (replac == NULL) 109751317Seric { 109856823Seric xpvp = key_rvp; 109953654Seric } 110064404Seric else if (*replac == '\0') 110164404Seric { 110264404Seric /* null replacement */ 110364404Seric nullpvp[0] = NULL; 110464404Seric xpvp = nullpvp; 110564404Seric } 110656678Seric else 110753654Seric { 110856678Seric /* scan the new replacement */ 1109*65066Seric xpvp = prescan(replac, '\0', pvpbuf, 1110*65066Seric sizeof pvpbuf, NULL); 111153654Seric if (xpvp == NULL) 111251317Seric { 111358403Seric /* prescan already printed error */ 111459084Seric return EX_DATAERR; 111556678Seric } 111651317Seric } 111751317Seric 111816914Seric /* append it to the token list */ 111956678Seric for (avp = hbrvp; *xpvp != NULL; xpvp++) 112056678Seric { 112117174Seric *avp++ = newstr(*xpvp); 112216920Seric if (avp >= &npvp[MAXATOM]) 112316914Seric goto toolong; 112417174Seric } 112516914Seric 112616914Seric /* restore the old trailing information */ 112756678Seric for (xpvp = pvpb1; (*avp++ = *xpvp++) != NULL; ) 112816920Seric if (avp >= &npvp[MAXATOM]) 112916914Seric goto toolong; 113017174Seric 113156678Seric break; 113216914Seric } 113316914Seric 113416914Seric /* 113516914Seric ** Check for subroutine calls. 113616914Seric */ 113716914Seric 113858050Seric if (*npvp != NULL && (**npvp & 0377) == CALLSUBR) 113956678Seric { 114059084Seric int stat; 114159084Seric 114256678Seric bcopy((char *) &npvp[2], (char *) pvp, 114356678Seric (int) (avp - npvp - 2) * sizeof *avp); 114456678Seric if (tTd(21, 3)) 114556678Seric printf("-----callsubr %s\n", npvp[1]); 114659084Seric stat = rewrite(pvp, atoi(npvp[1]), e); 114759084Seric if (rstat == EX_OK || stat == EX_TEMPFAIL) 114859084Seric rstat = stat; 114965056Seric if (*pvp != NULL && (**pvp & 0377) == CANONNET) 115063581Seric rwr = NULL; 115156678Seric } 115256678Seric else 115356678Seric { 115417348Seric bcopy((char *) npvp, (char *) pvp, 115516900Seric (int) (avp - npvp) * sizeof *avp); 115656678Seric } 11579374Seric if (tTd(21, 4)) 11589374Seric { 11599374Seric printf("rewritten as:"); 116056678Seric printav(pvp); 11619374Seric } 1162297Seric } 11638069Seric 11649279Seric if (OpMode == MD_TEST || tTd(21, 2)) 11658069Seric { 11668959Seric printf("rewrite: ruleset %2d returns:", ruleset); 116756678Seric printav(pvp); 11688069Seric } 116959084Seric 117059084Seric return rstat; 11713149Seric } 11723149Seric /* 11733149Seric ** BUILDADDR -- build address from token vector. 11743149Seric ** 11753149Seric ** Parameters: 11763149Seric ** tv -- token vector. 11773149Seric ** a -- pointer to address descriptor to fill. 11783149Seric ** If NULL, one will be allocated. 117964284Seric ** flags -- info regarding whether this is a sender or 118064284Seric ** a recipient. 118158966Seric ** e -- the current envelope. 11823149Seric ** 11833149Seric ** Returns: 11844279Seric ** NULL if there was an error. 11854279Seric ** 'a' otherwise. 11863149Seric ** 11873149Seric ** Side Effects: 11883149Seric ** fills in 'a' 11893149Seric */ 11903149Seric 119157249Seric struct errcodes 119257249Seric { 119357249Seric char *ec_name; /* name of error code */ 119457249Seric int ec_code; /* numeric code */ 119557249Seric } ErrorCodes[] = 119657249Seric { 119757249Seric "usage", EX_USAGE, 119857249Seric "nouser", EX_NOUSER, 119957249Seric "nohost", EX_NOHOST, 120057249Seric "unavailable", EX_UNAVAILABLE, 120157249Seric "software", EX_SOFTWARE, 120257249Seric "tempfail", EX_TEMPFAIL, 120357249Seric "protocol", EX_PROTOCOL, 120457249Seric #ifdef EX_CONFIG 120557249Seric "config", EX_CONFIG, 120657249Seric #endif 120757249Seric NULL, EX_UNAVAILABLE, 120857249Seric }; 120957249Seric 121056678Seric ADDRESS * 121164284Seric buildaddr(tv, a, flags, e) 12123149Seric register char **tv; 12133149Seric register ADDRESS *a; 121464284Seric int flags; 121558966Seric register ENVELOPE *e; 12163149Seric { 12173149Seric struct mailer **mp; 12183149Seric register struct mailer *m; 121958008Seric char *bp; 122058008Seric int spaceleft; 122164306Seric static MAILER errormailer; 122264306Seric static char *errorargv[] = { "ERROR", NULL }; 122357402Seric static char buf[MAXNAME]; 12243149Seric 122564791Seric if (tTd(24, 5)) 122664791Seric { 122764791Seric printf("buildaddr, flags=%o, tv=", flags); 122864791Seric printav(tv); 122964791Seric } 123064791Seric 12313149Seric if (a == NULL) 12323149Seric a = (ADDRESS *) xalloc(sizeof *a); 123316889Seric bzero((char *) a, sizeof *a); 12343149Seric 12353149Seric /* figure out what net/mailer to use */ 123664306Seric if (*tv == NULL || (**tv & 0377) != CANONNET) 12374279Seric { 123858151Seric syserr("554 buildaddr: no net"); 123964306Seric badaddr: 124064306Seric a->q_flags |= QBADADDR; 124164306Seric a->q_mailer = &errormailer; 124264306Seric if (errormailer.m_name == NULL) 124364306Seric { 124464306Seric /* initialize the bogus mailer */ 124564306Seric errormailer.m_name = "*error*"; 124664306Seric errormailer.m_mailer = "ERROR"; 124764306Seric errormailer.m_argv = errorargv; 124864306Seric } 124964306Seric return a; 12504279Seric } 12513149Seric tv++; 125258680Seric if (strcasecmp(*tv, "error") == 0) 12534279Seric { 125458050Seric if ((**++tv & 0377) == CANONHOST) 125510183Seric { 125657249Seric register struct errcodes *ep; 125757249Seric 125858050Seric if (isascii(**++tv) && isdigit(**tv)) 125957249Seric { 126057249Seric setstat(atoi(*tv)); 126157249Seric } 126257249Seric else 126357249Seric { 126457249Seric for (ep = ErrorCodes; ep->ec_name != NULL; ep++) 126557249Seric if (strcasecmp(ep->ec_name, *tv) == 0) 126657249Seric break; 126757249Seric setstat(ep->ec_code); 126857249Seric } 126910183Seric tv++; 127010183Seric } 127164928Seric else 127264928Seric setstat(EX_UNAVAILABLE); 127358050Seric if ((**tv & 0377) != CANONUSER) 127458151Seric syserr("554 buildaddr: error: no user"); 127558814Seric cataddr(++tv, NULL, buf, sizeof buf, ' '); 127658082Seric stripquotes(buf); 127764659Seric if (isascii(buf[0]) && isdigit(buf[0]) && 127864659Seric isascii(buf[1]) && isdigit(buf[1]) && 127964659Seric isascii(buf[2]) && isdigit(buf[2]) && 128064659Seric buf[3] == ' ') 128164659Seric { 128264659Seric char fmt[10]; 128364659Seric 128464659Seric strncpy(fmt, buf, 3); 128564659Seric strcpy(&fmt[3], " %s"); 128664659Seric usrerr(fmt, buf + 4); 128764659Seric } 128864659Seric else 128964659Seric { 129064659Seric usrerr("%s", buf); 129164659Seric } 129264306Seric goto badaddr; 12934279Seric } 129457402Seric 12954598Seric for (mp = Mailer; (m = *mp++) != NULL; ) 12963149Seric { 129758680Seric if (strcasecmp(m->m_name, *tv) == 0) 12983149Seric break; 12993149Seric } 13003149Seric if (m == NULL) 13014279Seric { 130258151Seric syserr("554 buildaddr: unknown mailer %s", *tv); 130364306Seric goto badaddr; 13044279Seric } 13054598Seric a->q_mailer = m; 13063149Seric 13073149Seric /* figure out what host (if any) */ 130856678Seric tv++; 130958509Seric if ((**tv & 0377) == CANONHOST) 13103149Seric { 131158008Seric bp = buf; 131258008Seric spaceleft = sizeof buf - 1; 131358050Seric while (*++tv != NULL && (**tv & 0377) != CANONUSER) 131458008Seric { 131558008Seric int i = strlen(*tv); 131658008Seric 131758008Seric if (i > spaceleft) 131858008Seric { 131958008Seric /* out of space for this address */ 132058008Seric if (spaceleft >= 0) 132158151Seric syserr("554 buildaddr: host too long (%.40s...)", 132258008Seric buf); 132358008Seric i = spaceleft; 132458008Seric spaceleft = 0; 132558008Seric } 132658008Seric if (i <= 0) 132758008Seric continue; 132858008Seric bcopy(*tv, bp, i); 132958008Seric bp += i; 133058008Seric spaceleft -= i; 133158008Seric } 133258010Seric *bp = '\0'; 13335704Seric a->q_host = newstr(buf); 13343149Seric } 133557249Seric else 133658509Seric { 133758509Seric if (!bitnset(M_LOCALMAILER, m->m_flags)) 133858509Seric { 133958509Seric syserr("554 buildaddr: no host"); 134064306Seric goto badaddr; 134158509Seric } 134257249Seric a->q_host = NULL; 134358509Seric } 13443149Seric 13453149Seric /* figure out the user */ 134658050Seric if (*tv == NULL || (**tv & 0377) != CANONUSER) 13474279Seric { 134858151Seric syserr("554 buildaddr: no user"); 134964306Seric goto badaddr; 13504279Seric } 135157402Seric tv++; 135251317Seric 135357402Seric /* do special mapping for local mailer */ 135457402Seric if (m == LocalMailer && *tv != NULL) 135557402Seric { 135657454Seric register char *p = *tv; 135757454Seric 135857454Seric if (*p == '"') 135957454Seric p++; 136057454Seric if (*p == '|') 136157402Seric a->q_mailer = m = ProgMailer; 136257454Seric else if (*p == '/') 136357402Seric a->q_mailer = m = FileMailer; 136457454Seric else if (*p == ':') 136557402Seric { 136657402Seric /* may be :include: */ 136758814Seric cataddr(tv, NULL, buf, sizeof buf, '\0'); 136858008Seric stripquotes(buf); 136958008Seric if (strncasecmp(buf, ":include:", 9) == 0) 137058008Seric { 137158008Seric /* if :include:, don't need further rewriting */ 137257402Seric a->q_mailer = m = InclMailer; 137358008Seric a->q_user = &buf[9]; 137458008Seric return (a); 137558008Seric } 137657402Seric } 137757402Seric } 137857402Seric 137958008Seric if (m == LocalMailer && *tv != NULL && strcmp(*tv, "@") == 0) 138058008Seric { 138158008Seric tv++; 138258008Seric a->q_flags |= QNOTREMOTE; 138358008Seric } 138458008Seric 138564284Seric /* rewrite according recipient mailer rewriting rules */ 138664284Seric define('h', a->q_host, e); 138764284Seric if (!bitset(RF_SENDERADDR|RF_HEADERADDR, flags)) 138864284Seric { 138964284Seric /* sender addresses done later */ 139064284Seric (void) rewrite(tv, 2, e); 139164284Seric if (m->m_re_rwset > 0) 139264284Seric (void) rewrite(tv, m->m_re_rwset, e); 139364284Seric } 139459084Seric (void) rewrite(tv, 4, e); 139519040Seric 139619040Seric /* save the result for the command line/RCPT argument */ 139758814Seric cataddr(tv, NULL, buf, sizeof buf, '\0'); 13983149Seric a->q_user = buf; 13993149Seric 140058670Seric /* 140158670Seric ** Do mapping to lower case as requested by mailer 140258670Seric */ 140358670Seric 140458670Seric if (a->q_host != NULL && !bitnset(M_HST_UPPER, m->m_flags)) 140558670Seric makelower(a->q_host); 140658670Seric if (!bitnset(M_USR_UPPER, m->m_flags)) 140758670Seric makelower(a->q_user); 140858670Seric 14093149Seric return (a); 14103149Seric } 14113188Seric /* 14124228Seric ** CATADDR -- concatenate pieces of addresses (putting in <LWSP> subs) 14134228Seric ** 14144228Seric ** Parameters: 14154228Seric ** pvp -- parameter vector to rebuild. 141658814Seric ** evp -- last parameter to include. Can be NULL to 141758814Seric ** use entire pvp. 14184228Seric ** buf -- buffer to build the string into. 14194228Seric ** sz -- size of buf. 142058082Seric ** spacesub -- the space separator character; if null, 142158082Seric ** use SpaceSub. 14224228Seric ** 14234228Seric ** Returns: 14244228Seric ** none. 14254228Seric ** 14264228Seric ** Side Effects: 14274228Seric ** Destroys buf. 14284228Seric */ 14294228Seric 143058814Seric cataddr(pvp, evp, buf, sz, spacesub) 14314228Seric char **pvp; 143258814Seric char **evp; 14334228Seric char *buf; 14344228Seric register int sz; 143558082Seric char spacesub; 14364228Seric { 14374228Seric bool oatomtok = FALSE; 143856678Seric bool natomtok = FALSE; 14394228Seric register int i; 14404228Seric register char *p; 14414228Seric 144258082Seric if (spacesub == '\0') 144358082Seric spacesub = SpaceSub; 144458082Seric 14458423Seric if (pvp == NULL) 14468423Seric { 144723109Seric (void) strcpy(buf, ""); 14488423Seric return; 14498423Seric } 14504228Seric p = buf; 145111156Seric sz -= 2; 14524228Seric while (*pvp != NULL && (i = strlen(*pvp)) < sz) 14534228Seric { 14548078Seric natomtok = (toktype(**pvp) == ATM); 14554228Seric if (oatomtok && natomtok) 145658082Seric *p++ = spacesub; 14574228Seric (void) strcpy(p, *pvp); 14584228Seric oatomtok = natomtok; 14594228Seric p += i; 146011156Seric sz -= i + 1; 146158814Seric if (pvp++ == evp) 146258814Seric break; 14634228Seric } 14644228Seric *p = '\0'; 14654228Seric } 14664228Seric /* 14673188Seric ** SAMEADDR -- Determine if two addresses are the same 14683188Seric ** 14693188Seric ** This is not just a straight comparison -- if the mailer doesn't 14703188Seric ** care about the host we just ignore it, etc. 14713188Seric ** 14723188Seric ** Parameters: 14733188Seric ** a, b -- pointers to the internal forms to compare. 14743188Seric ** 14753188Seric ** Returns: 14763188Seric ** TRUE -- they represent the same mailbox. 14773188Seric ** FALSE -- they don't. 14783188Seric ** 14793188Seric ** Side Effects: 14803188Seric ** none. 14813188Seric */ 14823188Seric 14833188Seric bool 14849374Seric sameaddr(a, b) 14853188Seric register ADDRESS *a; 14863188Seric register ADDRESS *b; 14873188Seric { 14883188Seric /* if they don't have the same mailer, forget it */ 14893188Seric if (a->q_mailer != b->q_mailer) 14903188Seric return (FALSE); 14913188Seric 14923188Seric /* if the user isn't the same, we can drop out */ 149356678Seric if (strcmp(a->q_user, b->q_user) != 0) 14943188Seric return (FALSE); 14953188Seric 149658438Seric /* if we have good uids for both but the differ, these are different */ 149758438Seric if (bitset(QGOODUID, a->q_flags & b->q_flags) && a->q_uid != b->q_uid) 149858438Seric return (FALSE); 149958438Seric 150058509Seric /* otherwise compare hosts (but be careful for NULL ptrs) */ 150158509Seric if (a->q_host == b->q_host) 150258509Seric { 150358509Seric /* probably both null pointers */ 15043188Seric return (TRUE); 150558509Seric } 15063188Seric if (a->q_host == NULL || b->q_host == NULL) 150758509Seric { 150858509Seric /* only one is a null pointer */ 15093188Seric return (FALSE); 151058509Seric } 151156678Seric if (strcmp(a->q_host, b->q_host) != 0) 15123188Seric return (FALSE); 15133188Seric 15143188Seric return (TRUE); 15153188Seric } 15163234Seric /* 15173234Seric ** PRINTADDR -- print address (for debugging) 15183234Seric ** 15193234Seric ** Parameters: 15203234Seric ** a -- the address to print 15213234Seric ** follow -- follow the q_next chain. 15223234Seric ** 15233234Seric ** Returns: 15243234Seric ** none. 15253234Seric ** 15263234Seric ** Side Effects: 15273234Seric ** none. 15283234Seric */ 15293234Seric 15303234Seric printaddr(a, follow) 15313234Seric register ADDRESS *a; 15323234Seric bool follow; 15333234Seric { 15345001Seric bool first = TRUE; 153557731Seric register MAILER *m; 153657731Seric MAILER pseudomailer; 15375001Seric 15383234Seric while (a != NULL) 15393234Seric { 15405001Seric first = FALSE; 15414443Seric printf("%x=", a); 15424085Seric (void) fflush(stdout); 154357731Seric 154457731Seric /* find the mailer -- carefully */ 154557731Seric m = a->q_mailer; 154657731Seric if (m == NULL) 154757731Seric { 154857731Seric m = &pseudomailer; 154957731Seric m->m_mno = -1; 155057731Seric m->m_name = "NULL"; 155157731Seric } 155257731Seric 155358680Seric printf("%s:\n\tmailer %d (%s), host `%s', user `%s', ruser `%s'\n", 155457731Seric a->q_paddr, m->m_mno, m->m_name, 155563756Seric a->q_host, a->q_user, 155663756Seric a->q_ruser ? a->q_ruser : "<null>"); 155759269Seric printf("\tnext=%x, flags=%o, alias %x, uid %d, gid %d\n", 155859269Seric a->q_next, a->q_flags, a->q_alias, a->q_uid, a->q_gid); 155959269Seric printf("\towner=%s, home=\"%s\", fullname=\"%s\"\n", 156059269Seric a->q_owner == NULL ? "(none)" : a->q_owner, 156163756Seric a->q_home == NULL ? "(none)" : a->q_home, 156263756Seric a->q_fullname == NULL ? "(none)" : a->q_fullname); 15634996Seric 15643234Seric if (!follow) 15653234Seric return; 15664996Seric a = a->q_next; 15673234Seric } 15685001Seric if (first) 15694443Seric printf("[NULL]\n"); 15703234Seric } 15714317Seric 15727682Seric /* 15737682Seric ** REMOTENAME -- return the name relative to the current mailer 15747682Seric ** 15757682Seric ** Parameters: 15767682Seric ** name -- the name to translate. 15778069Seric ** m -- the mailer that we want to do rewriting relative 15788069Seric ** to. 157959163Seric ** flags -- fine tune operations. 158059163Seric ** pstat -- pointer to status word. 158158020Seric ** e -- the current envelope. 15827682Seric ** 15837682Seric ** Returns: 15847682Seric ** the text string representing this address relative to 15857682Seric ** the receiving mailer. 15867682Seric ** 15877682Seric ** Side Effects: 15887682Seric ** none. 15897682Seric ** 15907682Seric ** Warnings: 15917682Seric ** The text string returned is tucked away locally; 15927682Seric ** copy it if you intend to save it. 15937682Seric */ 15947682Seric 15957682Seric char * 159659163Seric remotename(name, m, flags, pstat, e) 15977682Seric char *name; 159856678Seric struct mailer *m; 159959163Seric int flags; 160059163Seric int *pstat; 160156678Seric register ENVELOPE *e; 16027682Seric { 16038069Seric register char **pvp; 16048069Seric char *fancy; 160556678Seric char *oldg = macvalue('g', e); 160658020Seric int rwset; 16077682Seric static char buf[MAXNAME]; 16087682Seric char lbuf[MAXNAME]; 160916914Seric char pvpbuf[PSBUFSIZE]; 161056678Seric extern char *crackaddr(); 16117682Seric 16127755Seric if (tTd(12, 1)) 16137755Seric printf("remotename(%s)\n", name); 16147755Seric 161510177Seric /* don't do anything if we are tagging it as special */ 161659163Seric if (bitset(RF_SENDERADDR, flags)) 161759163Seric rwset = bitset(RF_HEADERADDR, flags) ? m->m_sh_rwset 161859163Seric : m->m_se_rwset; 161958020Seric else 162059163Seric rwset = bitset(RF_HEADERADDR, flags) ? m->m_rh_rwset 162159163Seric : m->m_re_rwset; 162258020Seric if (rwset < 0) 162310177Seric return (name); 162410177Seric 16257682Seric /* 16268181Seric ** Do a heuristic crack of this name to extract any comment info. 16278181Seric ** This will leave the name as a comment and a $g macro. 16287889Seric */ 16297889Seric 163059163Seric if (bitset(RF_CANONICAL, flags) || bitnset(M_NOCOMMENT, m->m_flags)) 163158050Seric fancy = "\201g"; 163210310Seric else 163310310Seric fancy = crackaddr(name); 16347889Seric 16358181Seric /* 16368181Seric ** Turn the name into canonical form. 16378181Seric ** Normally this will be RFC 822 style, i.e., "user@domain". 16388181Seric ** If this only resolves to "user", and the "C" flag is 16398181Seric ** specified in the sending mailer, then the sender's 16408181Seric ** domain will be appended. 16418181Seric */ 16428181Seric 1643*65066Seric pvp = prescan(name, '\0', pvpbuf, sizeof pvpbuf, NULL); 16447889Seric if (pvp == NULL) 16457889Seric return (name); 164659163Seric if (rewrite(pvp, 3, e) == EX_TEMPFAIL) 164759163Seric *pstat = EX_TEMPFAIL; 164859163Seric if (bitset(RF_ADDDOMAIN, flags) && e->e_fromdomain != NULL) 16498181Seric { 16508181Seric /* append from domain to this address */ 16518181Seric register char **pxp = pvp; 16528181Seric 16539594Seric /* see if there is an "@domain" in the current name */ 16548181Seric while (*pxp != NULL && strcmp(*pxp, "@") != 0) 16558181Seric pxp++; 16568181Seric if (*pxp == NULL) 16578181Seric { 16589594Seric /* no.... append the "@domain" from the sender */ 165956678Seric register char **qxq = e->e_fromdomain; 16608181Seric 16619594Seric while ((*pxp++ = *qxq++) != NULL) 16629594Seric continue; 166359163Seric if (rewrite(pvp, 3, e) == EX_TEMPFAIL) 166459163Seric *pstat = EX_TEMPFAIL; 16658181Seric } 16668181Seric } 16678181Seric 16688181Seric /* 16698959Seric ** Do more specific rewriting. 167056678Seric ** Rewrite using ruleset 1 or 2 depending on whether this is 167156678Seric ** a sender address or not. 16728181Seric ** Then run it through any receiving-mailer-specific rulesets. 16738181Seric */ 16748181Seric 167559163Seric if (bitset(RF_SENDERADDR, flags)) 167659541Seric { 167759163Seric if (rewrite(pvp, 1, e) == EX_TEMPFAIL) 167859163Seric *pstat = EX_TEMPFAIL; 167959541Seric } 16808069Seric else 168159541Seric { 168259163Seric if (rewrite(pvp, 2, e) == EX_TEMPFAIL) 168359163Seric *pstat = EX_TEMPFAIL; 168459541Seric } 168558020Seric if (rwset > 0) 168659541Seric { 168759163Seric if (rewrite(pvp, rwset, e) == EX_TEMPFAIL) 168859163Seric *pstat = EX_TEMPFAIL; 168959541Seric } 16907682Seric 16918181Seric /* 16928959Seric ** Do any final sanitation the address may require. 16938959Seric ** This will normally be used to turn internal forms 16948959Seric ** (e.g., user@host.LOCAL) into external form. This 16958959Seric ** may be used as a default to the above rules. 16968959Seric */ 16978959Seric 169859163Seric if (rewrite(pvp, 4, e) == EX_TEMPFAIL) 169959163Seric *pstat = EX_TEMPFAIL; 17008959Seric 17018959Seric /* 17028181Seric ** Now restore the comment information we had at the beginning. 17038181Seric */ 17048181Seric 170558825Seric cataddr(pvp, NULL, lbuf, sizeof lbuf, '\0'); 170656678Seric define('g', lbuf, e); 170756678Seric expand(fancy, buf, &buf[sizeof buf - 1], e); 170856678Seric define('g', oldg, e); 17097682Seric 17107682Seric if (tTd(12, 1)) 17117755Seric printf("remotename => `%s'\n", buf); 17127682Seric return (buf); 17137682Seric } 171451317Seric /* 171556678Seric ** MAPLOCALUSER -- run local username through ruleset 5 for final redirection 171651317Seric ** 171751317Seric ** Parameters: 171856678Seric ** a -- the address to map (but just the user name part). 171956678Seric ** sendq -- the sendq in which to install any replacement 172056678Seric ** addresses. 172151317Seric ** 172251317Seric ** Returns: 172351317Seric ** none. 172451317Seric */ 172551317Seric 172656678Seric maplocaluser(a, sendq, e) 172756678Seric register ADDRESS *a; 172856678Seric ADDRESS **sendq; 172956678Seric ENVELOPE *e; 173051317Seric { 173156678Seric register char **pvp; 173256678Seric register ADDRESS *a1 = NULL; 173358333Seric auto char *delimptr; 173456678Seric char pvpbuf[PSBUFSIZE]; 173551317Seric 173656678Seric if (tTd(29, 1)) 173756678Seric { 173856678Seric printf("maplocaluser: "); 173956678Seric printaddr(a, FALSE); 174056678Seric } 1741*65066Seric pvp = prescan(a->q_user, '\0', pvpbuf, sizeof pvpbuf, &delimptr); 174256678Seric if (pvp == NULL) 174356678Seric return; 174451317Seric 174559084Seric (void) rewrite(pvp, 5, e); 174658050Seric if (pvp[0] == NULL || (pvp[0][0] & 0377) != CANONNET) 174756678Seric return; 174851317Seric 174956678Seric /* if non-null, mailer destination specified -- has it changed? */ 175064284Seric a1 = buildaddr(pvp, NULL, 0, e); 175156678Seric if (a1 == NULL || sameaddr(a, a1)) 175256678Seric return; 175351317Seric 175456678Seric /* mark old address as dead; insert new address */ 175556678Seric a->q_flags |= QDONTSEND; 175657731Seric if (tTd(29, 5)) 175757731Seric { 175857731Seric printf("maplocaluser: QDONTSEND "); 175957731Seric printaddr(a, FALSE); 176057731Seric } 176156678Seric a1->q_alias = a; 176264348Seric allocaddr(a1, RF_COPYALL, NULL); 176356678Seric (void) recipient(a1, sendq, e); 176451317Seric } 176558802Seric /* 176658802Seric ** DEQUOTE_INIT -- initialize dequote map 176758802Seric ** 176858802Seric ** This is a no-op. 176958802Seric ** 177058802Seric ** Parameters: 177158802Seric ** map -- the internal map structure. 177258802Seric ** args -- arguments. 177358802Seric ** 177458802Seric ** Returns: 177558802Seric ** TRUE. 177658802Seric */ 177758802Seric 177858802Seric bool 177960219Seric dequote_init(map, args) 178058802Seric MAP *map; 178158802Seric char *args; 178258802Seric { 178358805Seric register char *p = args; 178458805Seric 178558805Seric for (;;) 178658805Seric { 178758805Seric while (isascii(*p) && isspace(*p)) 178858805Seric p++; 178958805Seric if (*p != '-') 179058805Seric break; 179158805Seric switch (*++p) 179258805Seric { 179358805Seric case 'a': 179458805Seric map->map_app = ++p; 179558805Seric break; 179658805Seric } 179758805Seric while (*p != '\0' && !(isascii(*p) && isspace(*p))) 179858805Seric p++; 179958805Seric if (*p != '\0') 180058805Seric *p = '\0'; 180158805Seric } 180258805Seric if (map->map_app != NULL) 180358805Seric map->map_app = newstr(map->map_app); 180458805Seric 180558802Seric return TRUE; 180658802Seric } 180758802Seric /* 180858802Seric ** DEQUOTE_MAP -- unquote an address 180958802Seric ** 181058802Seric ** Parameters: 181158802Seric ** map -- the internal map structure (ignored). 181260089Seric ** name -- the name to dequote. 181358802Seric ** av -- arguments (ignored). 181459084Seric ** statp -- pointer to status out-parameter. 181558802Seric ** 181658802Seric ** Returns: 181758802Seric ** NULL -- if there were no quotes, or if the resulting 181858802Seric ** unquoted buffer would not be acceptable to prescan. 181958802Seric ** else -- The dequoted buffer. 182058802Seric */ 182158802Seric 182258802Seric char * 182360089Seric dequote_map(map, name, av, statp) 182458802Seric MAP *map; 182560089Seric char *name; 182658802Seric char **av; 182759084Seric int *statp; 182858802Seric { 182958802Seric register char *p; 183058802Seric register char *q; 183158802Seric register char c; 183258802Seric int anglecnt; 183358802Seric int cmntcnt; 183458802Seric int quotecnt; 183559089Seric int spacecnt; 183658802Seric bool quotemode; 183758802Seric bool bslashmode; 183858802Seric 183958802Seric anglecnt = 0; 184058802Seric cmntcnt = 0; 184158802Seric quotecnt = 0; 184259089Seric spacecnt = 0; 184358802Seric quotemode = FALSE; 184458802Seric bslashmode = FALSE; 184558802Seric 184660089Seric for (p = q = name; (c = *p++) != '\0'; ) 184758802Seric { 184858802Seric if (bslashmode) 184958802Seric { 185058802Seric bslashmode = FALSE; 185158802Seric *q++ = c; 185258802Seric continue; 185358802Seric } 185458802Seric 185558802Seric switch (c) 185658802Seric { 185758802Seric case '\\': 185858802Seric bslashmode = TRUE; 185958802Seric break; 186058802Seric 186158802Seric case '(': 186258802Seric cmntcnt++; 186358802Seric break; 186458802Seric 186558802Seric case ')': 186658802Seric if (cmntcnt-- <= 0) 186758802Seric return NULL; 186858802Seric break; 186959089Seric 187059089Seric case ' ': 187159089Seric spacecnt++; 187259089Seric break; 187358802Seric } 187458802Seric 187558802Seric if (cmntcnt > 0) 187658802Seric { 187758802Seric *q++ = c; 187858802Seric continue; 187958802Seric } 188058802Seric 188158802Seric switch (c) 188258802Seric { 188358802Seric case '"': 188458802Seric quotemode = !quotemode; 188558802Seric quotecnt++; 188658802Seric continue; 188758802Seric 188858802Seric case '<': 188958802Seric anglecnt++; 189058802Seric break; 189158802Seric 189258802Seric case '>': 189358802Seric if (anglecnt-- <= 0) 189458802Seric return NULL; 189558802Seric break; 189658802Seric } 189758802Seric *q++ = c; 189858802Seric } 189958802Seric 190058802Seric if (anglecnt != 0 || cmntcnt != 0 || bslashmode || 190159089Seric quotemode || quotecnt <= 0 || spacecnt != 0) 190258802Seric return NULL; 190358802Seric *q++ = '\0'; 190460089Seric return name; 190558802Seric } 1906