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*67939Seric static char sccsid[] = "@(#)parseaddr.c 8.40 (Berkeley) 11/20/94"; 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'; 10966794Seric e->e_to = 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; 12065071Seric if (rewrite(pvp, 3, 0, e) == EX_TEMPFAIL) 12159084Seric queueup = TRUE; 12265071Seric 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)) 24367693Seric printf("allocaddr(flags=%x, 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 31965092Seric /* token type table -- it gets modified with $o characters */ 32065092Seric static TokTypeTab[256] = 32165092Seric { 32265092Seric ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,SPC,SPC,SPC,SPC,SPC,ATM,ATM, 32365092Seric ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, 32465092Seric SPC,ATM,QST,ATM,ATM,ATM,ATM,ATM,ATM,SPC,ATM,ATM,ATM,ATM,ATM,ATM, 32565092Seric ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, 32665092Seric ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, 32765092Seric ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, 32865092Seric ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, 32965092Seric ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, 33065092Seric OPR,OPR,ONE,OPR,OPR,OPR,OPR,OPR,OPR,OPR,OPR,OPR,OPR,OPR,OPR,OPR, 33165092Seric OPR,OPR,OPR,ONE,ONE,ONE,OPR,OPR,OPR,OPR,OPR,OPR,OPR,OPR,OPR,OPR, 33265092Seric ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, 33365092Seric ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, 33465092Seric ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, 33565092Seric ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, 33665092Seric ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, 33765092Seric ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, 33865092Seric }; 33965092Seric 34065092Seric #define toktype(c) ((int) TokTypeTab[(c) & 0xff]) 34165092Seric 34265092Seric 3438078Seric # define NOCHAR -1 /* signal nothing in lookahead token */ 3448078Seric 3453149Seric char ** 34665066Seric prescan(addr, delim, pvpbuf, pvpbsize, delimptr) 347297Seric char *addr; 348297Seric char delim; 34916914Seric char pvpbuf[]; 35058333Seric char **delimptr; 351297Seric { 352297Seric register char *p; 3538078Seric register char *q; 3549346Seric register int c; 3553149Seric char **avp; 356297Seric bool bslashmode; 357297Seric int cmntcnt; 3588423Seric int anglecnt; 3593149Seric char *tok; 3608078Seric int state; 3618078Seric int newstate; 36259747Seric char *saveto = CurEnv->e_to; 3638078Seric static char *av[MAXATOM+1]; 36465092Seric static char firsttime = TRUE; 36556678Seric extern int errno; 366297Seric 36765092Seric if (firsttime) 36865092Seric { 36965092Seric /* initialize the token type table */ 37065092Seric char obuf[50]; 37165092Seric 37265092Seric firsttime = FALSE; 37365092Seric expand("\201o", obuf, &obuf[sizeof obuf - sizeof DELIMCHARS], CurEnv); 37465092Seric strcat(obuf, DELIMCHARS); 37565092Seric for (p = obuf; *p != '\0'; p++) 37665092Seric { 37765092Seric if (TokTypeTab[*p & 0xff] == ATM) 37865092Seric TokTypeTab[*p & 0xff] = OPR; 37965092Seric } 38065092Seric } 38165092Seric 38215253Seric /* make sure error messages don't have garbage on them */ 38315253Seric errno = 0; 38415253Seric 38516914Seric q = pvpbuf; 3863149Seric bslashmode = FALSE; 3877800Seric cmntcnt = 0; 3888423Seric anglecnt = 0; 3893149Seric avp = av; 39056678Seric state = ATM; 3918078Seric c = NOCHAR; 3928078Seric p = addr; 39359747Seric CurEnv->e_to = p; 39456764Seric if (tTd(22, 11)) 395297Seric { 3968078Seric printf("prescan: "); 3978078Seric xputs(p); 39823109Seric (void) putchar('\n'); 3998078Seric } 4008078Seric 4018078Seric do 4028078Seric { 4033149Seric /* read a token */ 4043149Seric tok = q; 4058078Seric for (;;) 406297Seric { 4078078Seric /* store away any old lookahead character */ 40859277Seric if (c != NOCHAR && !bslashmode) 4098078Seric { 41015284Seric /* see if there is room */ 41165066Seric if (q >= &pvpbuf[pvpbsize - 5]) 4128078Seric { 41358151Seric usrerr("553 Address too long"); 41465015Seric returnnull: 41558333Seric if (delimptr != NULL) 41658333Seric *delimptr = p; 41759747Seric CurEnv->e_to = saveto; 4188078Seric return (NULL); 4198078Seric } 42015284Seric 42115284Seric /* squirrel it away */ 4228078Seric *q++ = c; 4238078Seric } 4248078Seric 4258078Seric /* read a new input character */ 4268078Seric c = *p++; 42757631Seric if (c == '\0') 42856764Seric { 42956764Seric /* diagnose and patch up bad syntax */ 43056764Seric if (state == QST) 43156764Seric { 43264767Seric usrerr("653 Unbalanced '\"'"); 43356764Seric c = '"'; 43456764Seric } 43556764Seric else if (cmntcnt > 0) 43656764Seric { 43764767Seric usrerr("653 Unbalanced '('"); 43856764Seric c = ')'; 43956764Seric } 44056764Seric else if (anglecnt > 0) 44156764Seric { 44256764Seric c = '>'; 44364767Seric usrerr("653 Unbalanced '<'"); 44456764Seric } 44556764Seric else 44656764Seric break; 44715284Seric 44856764Seric p--; 44956764Seric } 45057631Seric else if (c == delim && anglecnt <= 0 && 45157631Seric cmntcnt <= 0 && state != QST) 45257631Seric break; 45356764Seric 4548078Seric if (tTd(22, 101)) 4558078Seric printf("c=%c, s=%d; ", c, state); 4568078Seric 4573149Seric /* chew up special characters */ 4583149Seric *q = '\0'; 4593149Seric if (bslashmode) 4603149Seric { 46159105Seric bslashmode = FALSE; 46259105Seric 46324944Seric /* kludge \! for naive users */ 46458061Seric if (cmntcnt > 0) 46559105Seric { 46658061Seric c = NOCHAR; 46759105Seric continue; 46859105Seric } 46959105Seric else if (c != '!' || state == QST) 47059105Seric { 47156678Seric *q++ = '\\'; 47259105Seric continue; 47359105Seric } 4743149Seric } 47556678Seric 47656678Seric if (c == '\\') 4773149Seric { 4783149Seric bslashmode = TRUE; 4793149Seric } 48056678Seric else if (state == QST) 4818514Seric { 4828514Seric /* do nothing, just avoid next clauses */ 4838514Seric } 4848078Seric else if (c == '(') 4854100Seric { 4868078Seric cmntcnt++; 4878078Seric c = NOCHAR; 4884100Seric } 4898078Seric else if (c == ')') 4903149Seric { 4918078Seric if (cmntcnt <= 0) 4923149Seric { 49364767Seric usrerr("653 Unbalanced ')'"); 49463844Seric c = NOCHAR; 4953149Seric } 4968078Seric else 4978078Seric cmntcnt--; 4988078Seric } 4998078Seric else if (cmntcnt > 0) 5008078Seric c = NOCHAR; 5018423Seric else if (c == '<') 5028423Seric anglecnt++; 5038423Seric else if (c == '>') 5048423Seric { 5058423Seric if (anglecnt <= 0) 5068423Seric { 50764767Seric usrerr("653 Unbalanced '>'"); 50863844Seric c = NOCHAR; 5098423Seric } 51063844Seric else 51163844Seric anglecnt--; 5128423Seric } 51358050Seric else if (delim == ' ' && isascii(c) && isspace(c)) 51411423Seric c = ' '; 5153149Seric 5168078Seric if (c == NOCHAR) 5178078Seric continue; 5183149Seric 5198078Seric /* see if this is end of input */ 52011405Seric if (c == delim && anglecnt <= 0 && state != QST) 5213149Seric break; 5223149Seric 5238078Seric newstate = StateTab[state][toktype(c)]; 5248078Seric if (tTd(22, 101)) 5258078Seric printf("ns=%02o\n", newstate); 5268078Seric state = newstate & TYPE; 5278078Seric if (bitset(M, newstate)) 5288078Seric c = NOCHAR; 5298078Seric if (bitset(B, newstate)) 5304228Seric break; 531297Seric } 5323149Seric 5333149Seric /* new token */ 5348078Seric if (tok != q) 5351378Seric { 5368078Seric *q++ = '\0'; 5378078Seric if (tTd(22, 36)) 538297Seric { 5398078Seric printf("tok="); 5408078Seric xputs(tok); 54123109Seric (void) putchar('\n'); 542297Seric } 5438078Seric if (avp >= &av[MAXATOM]) 544297Seric { 54558151Seric syserr("553 prescan: too many tokens"); 54665015Seric goto returnnull; 547297Seric } 54865015Seric if (q - tok > MAXNAME) 54965015Seric { 55065015Seric syserr("553 prescan: token too long"); 55165015Seric goto returnnull; 55265015Seric } 5538078Seric *avp++ = tok; 554297Seric } 5558423Seric } while (c != '\0' && (c != delim || anglecnt > 0)); 5563149Seric *avp = NULL; 55758333Seric p--; 55858333Seric if (delimptr != NULL) 55958333Seric *delimptr = p; 56056764Seric if (tTd(22, 12)) 56156764Seric { 56256764Seric printf("prescan==>"); 56356764Seric printav(av); 56456764Seric } 56559747Seric CurEnv->e_to = saveto; 56658546Seric if (av[0] == NULL) 56764966Seric { 56864966Seric if (tTd(22, 1)) 56964966Seric printf("prescan: null leading token\n"); 57058546Seric return (NULL); 57164966Seric } 57258403Seric return (av); 5733149Seric } 5743149Seric /* 5753149Seric ** REWRITE -- apply rewrite rules to token vector. 5763149Seric ** 5774476Seric ** This routine is an ordered production system. Each rewrite 5784476Seric ** rule has a LHS (called the pattern) and a RHS (called the 5794476Seric ** rewrite); 'rwr' points the the current rewrite rule. 5804476Seric ** 5814476Seric ** For each rewrite rule, 'avp' points the address vector we 5824476Seric ** are trying to match against, and 'pvp' points to the pattern. 5838058Seric ** If pvp points to a special match value (MATCHZANY, MATCHANY, 5849585Seric ** MATCHONE, MATCHCLASS, MATCHNCLASS) then the address in avp 5859585Seric ** matched is saved away in the match vector (pointed to by 'mvp'). 5864476Seric ** 5874476Seric ** When a match between avp & pvp does not match, we try to 5889585Seric ** back out. If we back up over MATCHONE, MATCHCLASS, or MATCHNCLASS 5894476Seric ** we must also back out the match in mvp. If we reach a 5908058Seric ** MATCHANY or MATCHZANY we just extend the match and start 5918058Seric ** over again. 5924476Seric ** 5934476Seric ** When we finally match, we rewrite the address vector 5944476Seric ** and try over again. 5954476Seric ** 5963149Seric ** Parameters: 5973149Seric ** pvp -- pointer to token vector. 59859027Seric ** ruleset -- the ruleset to use for rewriting. 59965071Seric ** reclevel -- recursion level (to catch loops). 60059027Seric ** e -- the current envelope. 6013149Seric ** 6023149Seric ** Returns: 60359084Seric ** A status code. If EX_TEMPFAIL, higher level code should 60459084Seric ** attempt recovery. 6053149Seric ** 6063149Seric ** Side Effects: 6073149Seric ** pvp is modified. 6083149Seric */ 6092091Seric 6103149Seric struct match 6113149Seric { 6124468Seric char **first; /* first token matched */ 6134468Seric char **last; /* last token matched */ 61458825Seric char **pattern; /* pointer to pattern */ 6153149Seric }; 6163149Seric 6174468Seric # define MAXMATCH 9 /* max params per rewrite */ 6183149Seric 61965136Seric # ifndef MAXRULERECURSION 62065136Seric # define MAXRULERECURSION 50 /* max recursion depth */ 62165136Seric # endif 6223149Seric 62365136Seric 62459084Seric int 62565071Seric rewrite(pvp, ruleset, reclevel, e) 6263149Seric char **pvp; 6274070Seric int ruleset; 62865071Seric int reclevel; 62959027Seric register ENVELOPE *e; 6303149Seric { 6313149Seric register char *ap; /* address pointer */ 6323149Seric register char *rp; /* rewrite pointer */ 6333149Seric register char **avp; /* address vector pointer */ 6343149Seric register char **rvp; /* rewrite vector pointer */ 6358058Seric register struct match *mlp; /* cur ptr into mlist */ 6368058Seric register struct rewrite *rwr; /* pointer to current rewrite rule */ 63758866Seric int ruleno; /* current rule number */ 63859084Seric int rstat = EX_OK; /* return status */ 63964740Seric int loopcount; 64056678Seric struct match mlist[MAXMATCH]; /* stores match on LHS */ 6413149Seric char *npvp[MAXATOM+1]; /* temporary space for rebuild */ 6423149Seric 64367262Seric if (OpMode == MD_TEST || tTd(21, 1)) 6443149Seric { 6458959Seric printf("rewrite: ruleset %2d input:", ruleset); 64656678Seric printav(pvp); 6473149Seric } 64856678Seric if (ruleset < 0 || ruleset >= MAXRWSETS) 64956326Seric { 65058151Seric syserr("554 rewrite: illegal ruleset number %d", ruleset); 65159084Seric return EX_CONFIG; 65256326Seric } 65365136Seric if (reclevel++ > MAXRULERECURSION) 65465071Seric { 65565071Seric syserr("rewrite: infinite recursion, ruleset %d", ruleset); 65665071Seric return EX_CONFIG; 65765071Seric } 65856678Seric if (pvp == NULL) 65959084Seric return EX_USAGE; 66056326Seric 6613149Seric /* 66256678Seric ** Run through the list of rewrite rules, applying 66356678Seric ** any that match. 6643149Seric */ 6653149Seric 66658866Seric ruleno = 1; 66764740Seric loopcount = 0; 6684070Seric for (rwr = RewriteRules[ruleset]; rwr != NULL; ) 6693149Seric { 6707675Seric if (tTd(21, 12)) 671297Seric { 6728069Seric printf("-----trying rule:"); 67356678Seric printav(rwr->r_lhs); 6743149Seric } 6753149Seric 6763149Seric /* try to match on this rule */ 6774468Seric mlp = mlist; 6788058Seric rvp = rwr->r_lhs; 6798058Seric avp = pvp; 68058866Seric if (++loopcount > 100) 6813149Seric { 68258866Seric syserr("554 Infinite loop in ruleset %d, rule %d", 68358866Seric ruleset, ruleno); 68458866Seric if (tTd(21, 1)) 68552637Seric { 68656678Seric printf("workspace: "); 68756678Seric printav(pvp); 68852637Seric } 68958866Seric break; 69058866Seric } 69158866Seric 69258866Seric while ((ap = *avp) != NULL || *rvp != NULL) 69358866Seric { 6943149Seric rp = *rvp; 6958058Seric if (tTd(21, 35)) 6968058Seric { 69758825Seric printf("ADVANCE rp="); 69857531Seric xputs(rp); 69957532Seric printf(", ap="); 7008058Seric xputs(ap); 7018069Seric printf("\n"); 7028058Seric } 70356678Seric if (rp == NULL) 70456326Seric { 7053149Seric /* end-of-pattern before end-of-address */ 7068058Seric goto backup; 70756678Seric } 70858173Seric if (ap == NULL && (*rp & 0377) != MATCHZANY && 70958827Seric (*rp & 0377) != MATCHZERO) 71056326Seric { 71158825Seric /* end-of-input with patterns left */ 71258825Seric goto backup; 713297Seric } 71456326Seric 71558050Seric switch (*rp & 0377) 7168058Seric { 71756678Seric register STAB *s; 71858814Seric char buf[MAXLINE]; 71956326Seric 72056678Seric case MATCHCLASS: 72158825Seric /* match any phrase in a class */ 72258825Seric mlp->pattern = rvp; 72358814Seric mlp->first = avp; 72458814Seric extendclass: 72558825Seric ap = *avp; 72658825Seric if (ap == NULL) 72758814Seric goto backup; 72858814Seric mlp->last = avp++; 72958814Seric cataddr(mlp->first, mlp->last, buf, sizeof buf, '\0'); 73058814Seric s = stab(buf, ST_CLASS, ST_FIND); 73156678Seric if (s == NULL || !bitnset(rp[1], s->s_class)) 73256326Seric { 73358825Seric if (tTd(21, 36)) 73458825Seric { 73558825Seric printf("EXTEND rp="); 73658825Seric xputs(rp); 73758825Seric printf(", ap="); 73858825Seric xputs(ap); 73958825Seric printf("\n"); 74058825Seric } 74158825Seric goto extendclass; 74256326Seric } 74358825Seric if (tTd(21, 36)) 74458825Seric printf("CLMATCH\n"); 74558814Seric mlp++; 74658814Seric break; 7474060Seric 74858825Seric case MATCHNCLASS: 74958825Seric /* match any token not in a class */ 75058825Seric s = stab(ap, ST_CLASS, ST_FIND); 75158825Seric if (s != NULL && bitnset(rp[1], s->s_class)) 75258825Seric goto backup; 75358825Seric 75458825Seric /* fall through */ 75558825Seric 75656678Seric case MATCHONE: 75756678Seric case MATCHANY: 75856678Seric /* match exactly one token */ 75958825Seric mlp->pattern = rvp; 76056678Seric mlp->first = avp; 76156678Seric mlp->last = avp++; 7628058Seric mlp++; 76356678Seric break; 7648058Seric 76556678Seric case MATCHZANY: 76656678Seric /* match zero or more tokens */ 76758825Seric mlp->pattern = rvp; 76856678Seric mlp->first = avp; 76956678Seric mlp->last = avp - 1; 77056678Seric mlp++; 77156678Seric break; 77256326Seric 77358827Seric case MATCHZERO: 77458173Seric /* match zero tokens */ 77558173Seric break; 77658173Seric 77759027Seric case MACRODEXPAND: 77859027Seric /* 77959027Seric ** Match against run-time macro. 78059027Seric ** This algorithm is broken for the 78159027Seric ** general case (no recursive macros, 78259027Seric ** improper tokenization) but should 78359027Seric ** work for the usual cases. 78459027Seric */ 78559027Seric 78659027Seric ap = macvalue(rp[1], e); 78759027Seric mlp->first = avp; 78859027Seric if (tTd(21, 2)) 78959027Seric printf("rewrite: LHS $&%c => \"%s\"\n", 79059027Seric rp[1], 79159027Seric ap == NULL ? "(NULL)" : ap); 79259027Seric 79359027Seric if (ap == NULL) 79459027Seric break; 79560502Seric while (*ap != '\0') 79659027Seric { 79759027Seric if (*avp == NULL || 79859027Seric strncasecmp(ap, *avp, strlen(*avp)) != 0) 79959027Seric { 80059027Seric /* no match */ 80159027Seric avp = mlp->first; 80259027Seric goto backup; 80359027Seric } 80459027Seric ap += strlen(*avp++); 80559027Seric } 80659027Seric 80759027Seric /* match */ 80859027Seric break; 80959027Seric 81056678Seric default: 81156678Seric /* must have exact match */ 81256678Seric if (strcasecmp(rp, ap)) 8138058Seric goto backup; 8144468Seric avp++; 81556678Seric break; 8163149Seric } 8173149Seric 81856678Seric /* successful match on this token */ 8193149Seric rvp++; 8203149Seric continue; 8213149Seric 82258825Seric backup: 82356678Seric /* match failed -- back up */ 82458825Seric while (--mlp >= mlist) 8253149Seric { 82658825Seric rvp = mlp->pattern; 82756678Seric rp = *rvp; 82858825Seric avp = mlp->last + 1; 82958825Seric ap = *avp; 83058825Seric 83158825Seric if (tTd(21, 36)) 83258825Seric { 83358825Seric printf("BACKUP rp="); 83458825Seric xputs(rp); 83558825Seric printf(", ap="); 83658825Seric xputs(ap); 83758825Seric printf("\n"); 83858825Seric } 83958825Seric 84058825Seric if (ap == NULL) 84158825Seric { 84258825Seric /* run off the end -- back up again */ 84358825Seric continue; 84458825Seric } 84558050Seric if ((*rp & 0377) == MATCHANY || 84658050Seric (*rp & 0377) == MATCHZANY) 8474468Seric { 84856678Seric /* extend binding and continue */ 84958825Seric mlp->last = avp++; 85056678Seric rvp++; 85158825Seric mlp++; 85256678Seric break; 8534468Seric } 85458825Seric if ((*rp & 0377) == MATCHCLASS) 85556678Seric { 85658814Seric /* extend binding and try again */ 85763397Seric mlp->last = avp; 85858814Seric goto extendclass; 85958814Seric } 8603149Seric } 8613149Seric 86258825Seric if (mlp < mlist) 86356678Seric { 86456678Seric /* total failure to match */ 86556326Seric break; 8663149Seric } 867297Seric } 8683149Seric 8693149Seric /* 87056678Seric ** See if we successfully matched 8713149Seric */ 8723149Seric 87358827Seric if (mlp < mlist || *rvp != NULL) 8743149Seric { 8759374Seric if (tTd(21, 10)) 8769374Seric printf("----- rule fails\n"); 8779374Seric rwr = rwr->r_next; 87858866Seric ruleno++; 87964740Seric loopcount = 0; 8809374Seric continue; 8819374Seric } 8823149Seric 8839374Seric rvp = rwr->r_rhs; 8849374Seric if (tTd(21, 12)) 8859374Seric { 8869374Seric printf("-----rule matches:"); 88756678Seric printav(rvp); 8889374Seric } 8899374Seric 8909374Seric rp = *rvp; 89158050Seric if ((*rp & 0377) == CANONUSER) 8929374Seric { 8939374Seric rvp++; 8949374Seric rwr = rwr->r_next; 89558866Seric ruleno++; 89664740Seric loopcount = 0; 8979374Seric } 89858050Seric else if ((*rp & 0377) == CANONHOST) 8999374Seric { 9009374Seric rvp++; 9019374Seric rwr = NULL; 9029374Seric } 90358050Seric else if ((*rp & 0377) == CANONNET) 9049374Seric rwr = NULL; 9059374Seric 9069374Seric /* substitute */ 9079374Seric for (avp = npvp; *rvp != NULL; rvp++) 9089374Seric { 9099374Seric register struct match *m; 9109374Seric register char **pp; 9119374Seric 9128058Seric rp = *rvp; 91358050Seric if ((*rp & 0377) == MATCHREPL) 9148058Seric { 91516914Seric /* substitute from LHS */ 91616914Seric m = &mlist[rp[1] - '1']; 91756678Seric if (m < mlist || m >= mlp) 9189374Seric { 91958151Seric syserr("554 rewrite: ruleset %d: replacement $%c out of bounds", 92056326Seric ruleset, rp[1]); 92159084Seric return EX_CONFIG; 9229374Seric } 92316914Seric if (tTd(21, 15)) 92416914Seric { 92516914Seric printf("$%c:", rp[1]); 92616914Seric pp = m->first; 92756678Seric while (pp <= m->last) 92816914Seric { 92916914Seric printf(" %x=\"", *pp); 93016914Seric (void) fflush(stdout); 93116914Seric printf("%s\"", *pp++); 93216914Seric } 93316914Seric printf("\n"); 93416914Seric } 9359374Seric pp = m->first; 93656678Seric while (pp <= m->last) 9373149Seric { 93816914Seric if (avp >= &npvp[MAXATOM]) 93956678Seric { 94058151Seric syserr("554 rewrite: expansion too long"); 94159084Seric return EX_DATAERR; 94256678Seric } 94316914Seric *avp++ = *pp++; 9443149Seric } 9453149Seric } 94616914Seric else 9478226Seric { 94816914Seric /* vanilla replacement */ 9499374Seric if (avp >= &npvp[MAXATOM]) 95016889Seric { 95156678Seric toolong: 95258151Seric syserr("554 rewrite: expansion too long"); 95359084Seric return EX_DATAERR; 95416889Seric } 95559027Seric if ((*rp & 0377) != MACRODEXPAND) 95659027Seric *avp++ = rp; 95759027Seric else 95859027Seric { 95959027Seric *avp = macvalue(rp[1], e); 96059027Seric if (tTd(21, 2)) 96159027Seric printf("rewrite: RHS $&%c => \"%s\"\n", 96259027Seric rp[1], 96359027Seric *avp == NULL ? "(NULL)" : *avp); 96459027Seric if (*avp != NULL) 96559027Seric avp++; 96659027Seric } 9678226Seric } 9689374Seric } 9699374Seric *avp++ = NULL; 97016914Seric 97116914Seric /* 97256678Seric ** Check for any hostname/keyword lookups. 97316914Seric */ 97416914Seric 97516914Seric for (rvp = npvp; *rvp != NULL; rvp++) 97616914Seric { 97756678Seric char **hbrvp; 97816914Seric char **xpvp; 97916914Seric int trsize; 98056678Seric char *replac; 98156678Seric int endtoken; 98256678Seric STAB *map; 98356678Seric char *mapname; 98456678Seric char **key_rvp; 98556678Seric char **arg_rvp; 98656678Seric char **default_rvp; 98756678Seric char buf[MAXNAME + 1]; 98816914Seric char *pvpb1[MAXATOM + 1]; 98956823Seric char *argvect[10]; 99017174Seric char pvpbuf[PSBUFSIZE]; 99164404Seric char *nullpvp[1]; 99216914Seric 99358050Seric if ((**rvp & 0377) != HOSTBEGIN && 99458050Seric (**rvp & 0377) != LOOKUPBEGIN) 99516914Seric continue; 99616914Seric 99716914Seric /* 99856678Seric ** Got a hostname/keyword lookup. 99916914Seric ** 100016914Seric ** This could be optimized fairly easily. 100116914Seric */ 100216914Seric 100316914Seric hbrvp = rvp; 100458050Seric if ((**rvp & 0377) == HOSTBEGIN) 100556327Seric { 100656678Seric endtoken = HOSTEND; 100756678Seric mapname = "host"; 100856327Seric } 100956326Seric else 101056327Seric { 101156678Seric endtoken = LOOKUPEND; 101256678Seric mapname = *++rvp; 101356327Seric } 101456678Seric map = stab(mapname, ST_MAP, ST_FIND); 101556678Seric if (map == NULL) 101658151Seric syserr("554 rewrite: map %s not found", mapname); 101756678Seric 101856678Seric /* extract the match part */ 101956678Seric key_rvp = ++rvp; 102056823Seric default_rvp = NULL; 102156823Seric arg_rvp = argvect; 102256823Seric xpvp = NULL; 102356823Seric replac = pvpbuf; 102458050Seric while (*rvp != NULL && (**rvp & 0377) != endtoken) 102553654Seric { 102658050Seric int nodetype = **rvp & 0377; 102756823Seric 102856823Seric if (nodetype != CANONHOST && nodetype != CANONUSER) 102956678Seric { 103056823Seric rvp++; 103156823Seric continue; 103256823Seric } 103356823Seric 103456823Seric *rvp++ = NULL; 103556823Seric 103656823Seric if (xpvp != NULL) 103756823Seric { 103858814Seric cataddr(xpvp, NULL, replac, 103958082Seric &pvpbuf[sizeof pvpbuf] - replac, 104058082Seric '\0'); 104156823Seric *++arg_rvp = replac; 104256823Seric replac += strlen(replac) + 1; 104356823Seric xpvp = NULL; 104456823Seric } 104556823Seric switch (nodetype) 104656823Seric { 104756678Seric case CANONHOST: 104856823Seric xpvp = rvp; 104956678Seric break; 105056678Seric 105156678Seric case CANONUSER: 105256678Seric default_rvp = rvp; 105356678Seric break; 105456678Seric } 105553654Seric } 105616914Seric if (*rvp != NULL) 105716914Seric *rvp++ = NULL; 105856823Seric if (xpvp != NULL) 105956823Seric { 106058814Seric cataddr(xpvp, NULL, replac, 106158082Seric &pvpbuf[sizeof pvpbuf] - replac, 106258082Seric '\0'); 106356823Seric *++arg_rvp = replac; 106456823Seric } 106556823Seric *++arg_rvp = NULL; 106616914Seric 106716914Seric /* save the remainder of the input string */ 106816914Seric trsize = (int) (avp - rvp + 1) * sizeof *rvp; 106916914Seric bcopy((char *) rvp, (char *) pvpb1, trsize); 107016914Seric 107156678Seric /* look it up */ 107258814Seric cataddr(key_rvp, NULL, buf, sizeof buf, '\0'); 107356823Seric argvect[0] = buf; 107460538Seric if (map != NULL && bitset(MF_OPEN, map->s_map.map_mflags)) 107556836Seric { 107659084Seric auto int stat = EX_OK; 107756836Seric 107860215Seric /* XXX should try to auto-open the map here */ 107960215Seric 108058796Seric if (tTd(60, 1)) 108158796Seric printf("map_lookup(%s, %s) => ", 108258796Seric mapname, buf); 108356836Seric replac = (*map->s_map.map_class->map_lookup)(&map->s_map, 108460089Seric buf, argvect, &stat); 108558796Seric if (tTd(60, 1)) 108659084Seric printf("%s (%d)\n", 108759084Seric replac ? replac : "NOT FOUND", 108859084Seric stat); 108959084Seric 109059084Seric /* should recover if stat == EX_TEMPFAIL */ 109159084Seric if (stat == EX_TEMPFAIL) 109259084Seric rstat = stat; 109356836Seric } 109453654Seric else 109556678Seric replac = NULL; 109656678Seric 109756678Seric /* if no replacement, use default */ 109856823Seric if (replac == NULL && default_rvp != NULL) 109956823Seric { 110060089Seric /* create the default */ 110160089Seric cataddr(default_rvp, NULL, buf, sizeof buf, '\0'); 110256823Seric replac = buf; 110356823Seric } 110456823Seric 110556678Seric if (replac == NULL) 110651317Seric { 110756823Seric xpvp = key_rvp; 110853654Seric } 110964404Seric else if (*replac == '\0') 111064404Seric { 111164404Seric /* null replacement */ 111264404Seric nullpvp[0] = NULL; 111364404Seric xpvp = nullpvp; 111464404Seric } 111556678Seric else 111653654Seric { 111756678Seric /* scan the new replacement */ 111865066Seric xpvp = prescan(replac, '\0', pvpbuf, 111965066Seric sizeof pvpbuf, NULL); 112053654Seric if (xpvp == NULL) 112151317Seric { 112258403Seric /* prescan already printed error */ 112359084Seric return EX_DATAERR; 112456678Seric } 112551317Seric } 112651317Seric 112716914Seric /* append it to the token list */ 112856678Seric for (avp = hbrvp; *xpvp != NULL; xpvp++) 112956678Seric { 113017174Seric *avp++ = newstr(*xpvp); 113116920Seric if (avp >= &npvp[MAXATOM]) 113216914Seric goto toolong; 113317174Seric } 113416914Seric 113516914Seric /* restore the old trailing information */ 113656678Seric for (xpvp = pvpb1; (*avp++ = *xpvp++) != NULL; ) 113716920Seric if (avp >= &npvp[MAXATOM]) 113816914Seric goto toolong; 113917174Seric 114056678Seric break; 114116914Seric } 114216914Seric 114316914Seric /* 114416914Seric ** Check for subroutine calls. 114516914Seric */ 114616914Seric 114758050Seric if (*npvp != NULL && (**npvp & 0377) == CALLSUBR) 114856678Seric { 114959084Seric int stat; 115059084Seric 115165379Seric if (npvp[1] == NULL) 115265379Seric { 115365379Seric syserr("parseaddr: NULL subroutine call in ruleset %d, rule %d", 115465379Seric ruleset, ruleno); 115565379Seric *pvp = NULL; 115665379Seric } 115765379Seric else 115865379Seric { 115965379Seric bcopy((char *) &npvp[2], (char *) pvp, 116065379Seric (int) (avp - npvp - 2) * sizeof *avp); 116165379Seric if (tTd(21, 3)) 116265379Seric printf("-----callsubr %s\n", npvp[1]); 116365379Seric stat = rewrite(pvp, atoi(npvp[1]), reclevel, e); 116465379Seric if (rstat == EX_OK || stat == EX_TEMPFAIL) 116565379Seric rstat = stat; 116665379Seric if (*pvp != NULL && (**pvp & 0377) == CANONNET) 116763581Seric rwr = NULL; 116865379Seric } 116956678Seric } 117056678Seric else 117156678Seric { 117217348Seric bcopy((char *) npvp, (char *) pvp, 117316900Seric (int) (avp - npvp) * sizeof *avp); 117456678Seric } 11759374Seric if (tTd(21, 4)) 11769374Seric { 11779374Seric printf("rewritten as:"); 117856678Seric printav(pvp); 11799374Seric } 1180297Seric } 11818069Seric 118267262Seric if (OpMode == MD_TEST || tTd(21, 1)) 11838069Seric { 11848959Seric printf("rewrite: ruleset %2d returns:", ruleset); 118556678Seric printav(pvp); 11868069Seric } 118759084Seric 118859084Seric return rstat; 11893149Seric } 11903149Seric /* 11913149Seric ** BUILDADDR -- build address from token vector. 11923149Seric ** 11933149Seric ** Parameters: 11943149Seric ** tv -- token vector. 11953149Seric ** a -- pointer to address descriptor to fill. 11963149Seric ** If NULL, one will be allocated. 119764284Seric ** flags -- info regarding whether this is a sender or 119864284Seric ** a recipient. 119958966Seric ** e -- the current envelope. 12003149Seric ** 12013149Seric ** Returns: 12024279Seric ** NULL if there was an error. 12034279Seric ** 'a' otherwise. 12043149Seric ** 12053149Seric ** Side Effects: 12063149Seric ** fills in 'a' 12073149Seric */ 12083149Seric 120957249Seric struct errcodes 121057249Seric { 121157249Seric char *ec_name; /* name of error code */ 121257249Seric int ec_code; /* numeric code */ 121357249Seric } ErrorCodes[] = 121457249Seric { 121557249Seric "usage", EX_USAGE, 121657249Seric "nouser", EX_NOUSER, 121757249Seric "nohost", EX_NOHOST, 121857249Seric "unavailable", EX_UNAVAILABLE, 121957249Seric "software", EX_SOFTWARE, 122057249Seric "tempfail", EX_TEMPFAIL, 122157249Seric "protocol", EX_PROTOCOL, 122257249Seric #ifdef EX_CONFIG 122357249Seric "config", EX_CONFIG, 122457249Seric #endif 122557249Seric NULL, EX_UNAVAILABLE, 122657249Seric }; 122757249Seric 122856678Seric ADDRESS * 122964284Seric buildaddr(tv, a, flags, e) 12303149Seric register char **tv; 12313149Seric register ADDRESS *a; 123264284Seric int flags; 123358966Seric register ENVELOPE *e; 12343149Seric { 12353149Seric struct mailer **mp; 12363149Seric register struct mailer *m; 123758008Seric char *bp; 123858008Seric int spaceleft; 123964306Seric static MAILER errormailer; 124064306Seric static char *errorargv[] = { "ERROR", NULL }; 124157402Seric static char buf[MAXNAME]; 12423149Seric 124364791Seric if (tTd(24, 5)) 124464791Seric { 124567693Seric printf("buildaddr, flags=%x, tv=", flags); 124664791Seric printav(tv); 124764791Seric } 124864791Seric 12493149Seric if (a == NULL) 12503149Seric a = (ADDRESS *) xalloc(sizeof *a); 125116889Seric bzero((char *) a, sizeof *a); 12523149Seric 125367880Seric /* set up default error return flags */ 125467880Seric a->q_flags |= QPINGONFAILURE; 125567880Seric 12563149Seric /* figure out what net/mailer to use */ 125764306Seric if (*tv == NULL || (**tv & 0377) != CANONNET) 12584279Seric { 125958151Seric syserr("554 buildaddr: no net"); 126064306Seric badaddr: 126164306Seric a->q_flags |= QBADADDR; 126264306Seric a->q_mailer = &errormailer; 126364306Seric if (errormailer.m_name == NULL) 126464306Seric { 126564306Seric /* initialize the bogus mailer */ 126664306Seric errormailer.m_name = "*error*"; 126764306Seric errormailer.m_mailer = "ERROR"; 126864306Seric errormailer.m_argv = errorargv; 126964306Seric } 127064306Seric return a; 12714279Seric } 12723149Seric tv++; 127358680Seric if (strcasecmp(*tv, "error") == 0) 12744279Seric { 127558050Seric if ((**++tv & 0377) == CANONHOST) 127610183Seric { 127757249Seric register struct errcodes *ep; 127857249Seric 127958050Seric if (isascii(**++tv) && isdigit(**tv)) 128057249Seric { 128157249Seric setstat(atoi(*tv)); 128257249Seric } 128357249Seric else 128457249Seric { 128557249Seric for (ep = ErrorCodes; ep->ec_name != NULL; ep++) 128657249Seric if (strcasecmp(ep->ec_name, *tv) == 0) 128757249Seric break; 128857249Seric setstat(ep->ec_code); 128957249Seric } 129010183Seric tv++; 129110183Seric } 129264928Seric else 129364928Seric setstat(EX_UNAVAILABLE); 129458050Seric if ((**tv & 0377) != CANONUSER) 129558151Seric syserr("554 buildaddr: error: no user"); 129658814Seric cataddr(++tv, NULL, buf, sizeof buf, ' '); 129758082Seric stripquotes(buf); 129864659Seric if (isascii(buf[0]) && isdigit(buf[0]) && 129964659Seric isascii(buf[1]) && isdigit(buf[1]) && 130064659Seric isascii(buf[2]) && isdigit(buf[2]) && 130164659Seric buf[3] == ' ') 130264659Seric { 130364659Seric char fmt[10]; 130464659Seric 130564659Seric strncpy(fmt, buf, 3); 130664659Seric strcpy(&fmt[3], " %s"); 130764659Seric usrerr(fmt, buf + 4); 130867786Seric 130967786Seric /* 131067786Seric ** If this is a 4xx code and we aren't running 131167786Seric ** SMTP on our input, bounce this message; 131267786Seric ** otherwise it disappears without a trace. 131367786Seric */ 131467786Seric 131567786Seric if (fmt[0] == '4' && OpMode != MD_SMTP && 131667786Seric OpMode != MD_DAEMON) 131767786Seric { 131867786Seric e->e_flags |= EF_FATALERRS; 131967786Seric } 132064659Seric } 132164659Seric else 132264659Seric { 132366039Seric usrerr("553 %s", buf); 132464659Seric } 132564306Seric goto badaddr; 13264279Seric } 132757402Seric 13284598Seric for (mp = Mailer; (m = *mp++) != NULL; ) 13293149Seric { 133058680Seric if (strcasecmp(m->m_name, *tv) == 0) 13313149Seric break; 13323149Seric } 13333149Seric if (m == NULL) 13344279Seric { 133558151Seric syserr("554 buildaddr: unknown mailer %s", *tv); 133664306Seric goto badaddr; 13374279Seric } 13384598Seric a->q_mailer = m; 13393149Seric 13403149Seric /* figure out what host (if any) */ 134156678Seric tv++; 134258509Seric if ((**tv & 0377) == CANONHOST) 13433149Seric { 134458008Seric bp = buf; 134558008Seric spaceleft = sizeof buf - 1; 134658050Seric while (*++tv != NULL && (**tv & 0377) != CANONUSER) 134758008Seric { 134858008Seric int i = strlen(*tv); 134958008Seric 135058008Seric if (i > spaceleft) 135158008Seric { 135258008Seric /* out of space for this address */ 135358008Seric if (spaceleft >= 0) 135458151Seric syserr("554 buildaddr: host too long (%.40s...)", 135558008Seric buf); 135658008Seric i = spaceleft; 135758008Seric spaceleft = 0; 135858008Seric } 135958008Seric if (i <= 0) 136058008Seric continue; 136158008Seric bcopy(*tv, bp, i); 136258008Seric bp += i; 136358008Seric spaceleft -= i; 136458008Seric } 136558010Seric *bp = '\0'; 13665704Seric a->q_host = newstr(buf); 13673149Seric } 136857249Seric else 136958509Seric { 137058509Seric if (!bitnset(M_LOCALMAILER, m->m_flags)) 137158509Seric { 137258509Seric syserr("554 buildaddr: no host"); 137364306Seric goto badaddr; 137458509Seric } 137557249Seric a->q_host = NULL; 137658509Seric } 13773149Seric 13783149Seric /* figure out the user */ 137958050Seric if (*tv == NULL || (**tv & 0377) != CANONUSER) 13804279Seric { 138158151Seric syserr("554 buildaddr: no user"); 138264306Seric goto badaddr; 13834279Seric } 138457402Seric tv++; 138551317Seric 138657402Seric /* do special mapping for local mailer */ 138767472Seric if (*tv != NULL) 138857402Seric { 138957454Seric register char *p = *tv; 139057454Seric 139157454Seric if (*p == '"') 139257454Seric p++; 139367472Seric if (*p == '|' && bitnset(M_CHECKPROG, m->m_flags)) 139457402Seric a->q_mailer = m = ProgMailer; 139567472Seric else if (*p == '/' && bitnset(M_CHECKFILE, m->m_flags)) 139657402Seric a->q_mailer = m = FileMailer; 139767472Seric else if (*p == ':' && bitnset(M_CHECKINCLUDE, m->m_flags)) 139857402Seric { 139957402Seric /* may be :include: */ 140058814Seric cataddr(tv, NULL, buf, sizeof buf, '\0'); 140158008Seric stripquotes(buf); 140258008Seric if (strncasecmp(buf, ":include:", 9) == 0) 140358008Seric { 140458008Seric /* if :include:, don't need further rewriting */ 140557402Seric a->q_mailer = m = InclMailer; 140658008Seric a->q_user = &buf[9]; 140758008Seric return (a); 140858008Seric } 140957402Seric } 141057402Seric } 141157402Seric 141267472Seric if (bitnset(M_CHECKUDB, m->m_flags) && *tv != NULL && 141367472Seric strcmp(*tv, "@") == 0) 141458008Seric { 141558008Seric tv++; 141658008Seric a->q_flags |= QNOTREMOTE; 141758008Seric } 141858008Seric 141964284Seric /* rewrite according recipient mailer rewriting rules */ 142064284Seric define('h', a->q_host, e); 142164284Seric if (!bitset(RF_SENDERADDR|RF_HEADERADDR, flags)) 142264284Seric { 142364284Seric /* sender addresses done later */ 142465071Seric (void) rewrite(tv, 2, 0, e); 142564284Seric if (m->m_re_rwset > 0) 142665071Seric (void) rewrite(tv, m->m_re_rwset, 0, e); 142764284Seric } 142865071Seric (void) rewrite(tv, 4, 0, e); 142919040Seric 143019040Seric /* save the result for the command line/RCPT argument */ 143158814Seric cataddr(tv, NULL, buf, sizeof buf, '\0'); 14323149Seric a->q_user = buf; 14333149Seric 143458670Seric /* 143558670Seric ** Do mapping to lower case as requested by mailer 143658670Seric */ 143758670Seric 143858670Seric if (a->q_host != NULL && !bitnset(M_HST_UPPER, m->m_flags)) 143958670Seric makelower(a->q_host); 144058670Seric if (!bitnset(M_USR_UPPER, m->m_flags)) 144158670Seric makelower(a->q_user); 144258670Seric 14433149Seric return (a); 14443149Seric } 14453188Seric /* 14464228Seric ** CATADDR -- concatenate pieces of addresses (putting in <LWSP> subs) 14474228Seric ** 14484228Seric ** Parameters: 14494228Seric ** pvp -- parameter vector to rebuild. 145058814Seric ** evp -- last parameter to include. Can be NULL to 145158814Seric ** use entire pvp. 14524228Seric ** buf -- buffer to build the string into. 14534228Seric ** sz -- size of buf. 145458082Seric ** spacesub -- the space separator character; if null, 145558082Seric ** use SpaceSub. 14564228Seric ** 14574228Seric ** Returns: 14584228Seric ** none. 14594228Seric ** 14604228Seric ** Side Effects: 14614228Seric ** Destroys buf. 14624228Seric */ 14634228Seric 146458814Seric cataddr(pvp, evp, buf, sz, spacesub) 14654228Seric char **pvp; 146658814Seric char **evp; 14674228Seric char *buf; 14684228Seric register int sz; 146958082Seric char spacesub; 14704228Seric { 14714228Seric bool oatomtok = FALSE; 147256678Seric bool natomtok = FALSE; 14734228Seric register int i; 14744228Seric register char *p; 14754228Seric 147658082Seric if (spacesub == '\0') 147758082Seric spacesub = SpaceSub; 147858082Seric 14798423Seric if (pvp == NULL) 14808423Seric { 148123109Seric (void) strcpy(buf, ""); 14828423Seric return; 14838423Seric } 14844228Seric p = buf; 148511156Seric sz -= 2; 14864228Seric while (*pvp != NULL && (i = strlen(*pvp)) < sz) 14874228Seric { 14888078Seric natomtok = (toktype(**pvp) == ATM); 14894228Seric if (oatomtok && natomtok) 149058082Seric *p++ = spacesub; 14914228Seric (void) strcpy(p, *pvp); 14924228Seric oatomtok = natomtok; 14934228Seric p += i; 149411156Seric sz -= i + 1; 149558814Seric if (pvp++ == evp) 149658814Seric break; 14974228Seric } 14984228Seric *p = '\0'; 14994228Seric } 15004228Seric /* 15013188Seric ** SAMEADDR -- Determine if two addresses are the same 15023188Seric ** 15033188Seric ** This is not just a straight comparison -- if the mailer doesn't 15043188Seric ** care about the host we just ignore it, etc. 15053188Seric ** 15063188Seric ** Parameters: 15073188Seric ** a, b -- pointers to the internal forms to compare. 15083188Seric ** 15093188Seric ** Returns: 15103188Seric ** TRUE -- they represent the same mailbox. 15113188Seric ** FALSE -- they don't. 15123188Seric ** 15133188Seric ** Side Effects: 15143188Seric ** none. 15153188Seric */ 15163188Seric 15173188Seric bool 15189374Seric sameaddr(a, b) 15193188Seric register ADDRESS *a; 15203188Seric register ADDRESS *b; 15213188Seric { 152265093Seric register ADDRESS *ca, *cb; 152365093Seric 15243188Seric /* if they don't have the same mailer, forget it */ 15253188Seric if (a->q_mailer != b->q_mailer) 15263188Seric return (FALSE); 15273188Seric 15283188Seric /* if the user isn't the same, we can drop out */ 152956678Seric if (strcmp(a->q_user, b->q_user) != 0) 15303188Seric return (FALSE); 15313188Seric 153265093Seric /* if we have good uids for both but they differ, these are different */ 153365379Seric if (a->q_mailer == ProgMailer) 153465379Seric { 153565379Seric ca = getctladdr(a); 153665379Seric cb = getctladdr(b); 153765379Seric if (ca != NULL && cb != NULL && 153865379Seric bitset(QGOODUID, ca->q_flags & cb->q_flags) && 153965379Seric ca->q_uid != cb->q_uid) 154065379Seric return (FALSE); 154165379Seric } 154258438Seric 154358509Seric /* otherwise compare hosts (but be careful for NULL ptrs) */ 154458509Seric if (a->q_host == b->q_host) 154558509Seric { 154658509Seric /* probably both null pointers */ 15473188Seric return (TRUE); 154858509Seric } 15493188Seric if (a->q_host == NULL || b->q_host == NULL) 155058509Seric { 155158509Seric /* only one is a null pointer */ 15523188Seric return (FALSE); 155358509Seric } 155456678Seric if (strcmp(a->q_host, b->q_host) != 0) 15553188Seric return (FALSE); 15563188Seric 15573188Seric return (TRUE); 15583188Seric } 15593234Seric /* 15603234Seric ** PRINTADDR -- print address (for debugging) 15613234Seric ** 15623234Seric ** Parameters: 15633234Seric ** a -- the address to print 15643234Seric ** follow -- follow the q_next chain. 15653234Seric ** 15663234Seric ** Returns: 15673234Seric ** none. 15683234Seric ** 15693234Seric ** Side Effects: 15703234Seric ** none. 15713234Seric */ 15723234Seric 15733234Seric printaddr(a, follow) 15743234Seric register ADDRESS *a; 15753234Seric bool follow; 15763234Seric { 15775001Seric bool first = TRUE; 157857731Seric register MAILER *m; 157957731Seric MAILER pseudomailer; 15805001Seric 15813234Seric while (a != NULL) 15823234Seric { 15835001Seric first = FALSE; 15844443Seric printf("%x=", a); 15854085Seric (void) fflush(stdout); 158657731Seric 158757731Seric /* find the mailer -- carefully */ 158857731Seric m = a->q_mailer; 158957731Seric if (m == NULL) 159057731Seric { 159157731Seric m = &pseudomailer; 159257731Seric m->m_mno = -1; 159357731Seric m->m_name = "NULL"; 159457731Seric } 159557731Seric 159658680Seric printf("%s:\n\tmailer %d (%s), host `%s', user `%s', ruser `%s'\n", 159757731Seric a->q_paddr, m->m_mno, m->m_name, 159867172Seric a->q_host == NULL ? "<null>" : a->q_host, a->q_user, 159967172Seric a->q_ruser == NULL ? "<null>" : a->q_ruser); 160067693Seric printf("\tnext=%x, flags=%x, alias %x, uid %d, gid %d\n", 160159269Seric a->q_next, a->q_flags, a->q_alias, a->q_uid, a->q_gid); 160259269Seric printf("\towner=%s, home=\"%s\", fullname=\"%s\"\n", 160359269Seric a->q_owner == NULL ? "(none)" : a->q_owner, 160463756Seric a->q_home == NULL ? "(none)" : a->q_home, 160563756Seric a->q_fullname == NULL ? "(none)" : a->q_fullname); 16064996Seric 16073234Seric if (!follow) 16083234Seric return; 16094996Seric a = a->q_next; 16103234Seric } 16115001Seric if (first) 16124443Seric printf("[NULL]\n"); 16133234Seric } 1614*67939Seric /* 1615*67939Seric ** EMPTYADDR -- return TRUE if this address is empty (``<>'') 1616*67939Seric ** 1617*67939Seric ** Parameters: 1618*67939Seric ** a -- pointer to the address 1619*67939Seric ** 1620*67939Seric ** Returns: 1621*67939Seric ** TRUE -- if this address is "empty" (i.e., no one should 1622*67939Seric ** ever generate replies to it. 1623*67939Seric ** FALSE -- if it is a "regular" (read: replyable) address. 1624*67939Seric */ 16254317Seric 1626*67939Seric bool 1627*67939Seric emptyaddr(a) 1628*67939Seric register ADDRESS *a; 1629*67939Seric { 1630*67939Seric return strcmp(a->q_paddr, "<>") == 0 || strcmp(a->q_user, "<>") == 0; 1631*67939Seric } 16327682Seric /* 16337682Seric ** REMOTENAME -- return the name relative to the current mailer 16347682Seric ** 16357682Seric ** Parameters: 16367682Seric ** name -- the name to translate. 16378069Seric ** m -- the mailer that we want to do rewriting relative 16388069Seric ** to. 163959163Seric ** flags -- fine tune operations. 164059163Seric ** pstat -- pointer to status word. 164158020Seric ** e -- the current envelope. 16427682Seric ** 16437682Seric ** Returns: 16447682Seric ** the text string representing this address relative to 16457682Seric ** the receiving mailer. 16467682Seric ** 16477682Seric ** Side Effects: 16487682Seric ** none. 16497682Seric ** 16507682Seric ** Warnings: 16517682Seric ** The text string returned is tucked away locally; 16527682Seric ** copy it if you intend to save it. 16537682Seric */ 16547682Seric 16557682Seric char * 165659163Seric remotename(name, m, flags, pstat, e) 16577682Seric char *name; 165856678Seric struct mailer *m; 165959163Seric int flags; 166059163Seric int *pstat; 166156678Seric register ENVELOPE *e; 16627682Seric { 16638069Seric register char **pvp; 16648069Seric char *fancy; 166556678Seric char *oldg = macvalue('g', e); 166658020Seric int rwset; 16677682Seric static char buf[MAXNAME]; 16687682Seric char lbuf[MAXNAME]; 166916914Seric char pvpbuf[PSBUFSIZE]; 167056678Seric extern char *crackaddr(); 16717682Seric 16727755Seric if (tTd(12, 1)) 16737755Seric printf("remotename(%s)\n", name); 16747755Seric 167510177Seric /* don't do anything if we are tagging it as special */ 167659163Seric if (bitset(RF_SENDERADDR, flags)) 167759163Seric rwset = bitset(RF_HEADERADDR, flags) ? m->m_sh_rwset 167859163Seric : m->m_se_rwset; 167958020Seric else 168059163Seric rwset = bitset(RF_HEADERADDR, flags) ? m->m_rh_rwset 168159163Seric : m->m_re_rwset; 168258020Seric if (rwset < 0) 168310177Seric return (name); 168410177Seric 16857682Seric /* 16868181Seric ** Do a heuristic crack of this name to extract any comment info. 16878181Seric ** This will leave the name as a comment and a $g macro. 16887889Seric */ 16897889Seric 169059163Seric if (bitset(RF_CANONICAL, flags) || bitnset(M_NOCOMMENT, m->m_flags)) 169158050Seric fancy = "\201g"; 169210310Seric else 169310310Seric fancy = crackaddr(name); 16947889Seric 16958181Seric /* 16968181Seric ** Turn the name into canonical form. 16978181Seric ** Normally this will be RFC 822 style, i.e., "user@domain". 16988181Seric ** If this only resolves to "user", and the "C" flag is 16998181Seric ** specified in the sending mailer, then the sender's 17008181Seric ** domain will be appended. 17018181Seric */ 17028181Seric 170365066Seric pvp = prescan(name, '\0', pvpbuf, sizeof pvpbuf, NULL); 17047889Seric if (pvp == NULL) 17057889Seric return (name); 170665071Seric if (rewrite(pvp, 3, 0, e) == EX_TEMPFAIL) 170759163Seric *pstat = EX_TEMPFAIL; 170859163Seric if (bitset(RF_ADDDOMAIN, flags) && e->e_fromdomain != NULL) 17098181Seric { 17108181Seric /* append from domain to this address */ 17118181Seric register char **pxp = pvp; 17128181Seric 17139594Seric /* see if there is an "@domain" in the current name */ 17148181Seric while (*pxp != NULL && strcmp(*pxp, "@") != 0) 17158181Seric pxp++; 17168181Seric if (*pxp == NULL) 17178181Seric { 17189594Seric /* no.... append the "@domain" from the sender */ 171956678Seric register char **qxq = e->e_fromdomain; 17208181Seric 17219594Seric while ((*pxp++ = *qxq++) != NULL) 17229594Seric continue; 172365071Seric if (rewrite(pvp, 3, 0, e) == EX_TEMPFAIL) 172459163Seric *pstat = EX_TEMPFAIL; 17258181Seric } 17268181Seric } 17278181Seric 17288181Seric /* 17298959Seric ** Do more specific rewriting. 173056678Seric ** Rewrite using ruleset 1 or 2 depending on whether this is 173156678Seric ** a sender address or not. 17328181Seric ** Then run it through any receiving-mailer-specific rulesets. 17338181Seric */ 17348181Seric 173559163Seric if (bitset(RF_SENDERADDR, flags)) 173659541Seric { 173765071Seric if (rewrite(pvp, 1, 0, e) == EX_TEMPFAIL) 173859163Seric *pstat = EX_TEMPFAIL; 173959541Seric } 17408069Seric else 174159541Seric { 174265071Seric if (rewrite(pvp, 2, 0, e) == EX_TEMPFAIL) 174359163Seric *pstat = EX_TEMPFAIL; 174459541Seric } 174558020Seric if (rwset > 0) 174659541Seric { 174765071Seric if (rewrite(pvp, rwset, 0, e) == EX_TEMPFAIL) 174859163Seric *pstat = EX_TEMPFAIL; 174959541Seric } 17507682Seric 17518181Seric /* 17528959Seric ** Do any final sanitation the address may require. 17538959Seric ** This will normally be used to turn internal forms 17548959Seric ** (e.g., user@host.LOCAL) into external form. This 17558959Seric ** may be used as a default to the above rules. 17568959Seric */ 17578959Seric 175865071Seric if (rewrite(pvp, 4, 0, e) == EX_TEMPFAIL) 175959163Seric *pstat = EX_TEMPFAIL; 17608959Seric 17618959Seric /* 17628181Seric ** Now restore the comment information we had at the beginning. 17638181Seric */ 17648181Seric 176558825Seric cataddr(pvp, NULL, lbuf, sizeof lbuf, '\0'); 176656678Seric define('g', lbuf, e); 176765494Seric 176865494Seric /* need to make sure route-addrs have <angle brackets> */ 176965494Seric if (bitset(RF_CANONICAL, flags) && lbuf[0] == '@') 177065494Seric expand("<\201g>", buf, &buf[sizeof buf - 1], e); 177165494Seric else 177265494Seric expand(fancy, buf, &buf[sizeof buf - 1], e); 177365494Seric 177456678Seric define('g', oldg, e); 17757682Seric 17767682Seric if (tTd(12, 1)) 17777755Seric printf("remotename => `%s'\n", buf); 17787682Seric return (buf); 17797682Seric } 178051317Seric /* 178156678Seric ** MAPLOCALUSER -- run local username through ruleset 5 for final redirection 178251317Seric ** 178351317Seric ** Parameters: 178456678Seric ** a -- the address to map (but just the user name part). 178556678Seric ** sendq -- the sendq in which to install any replacement 178656678Seric ** addresses. 178751317Seric ** 178851317Seric ** Returns: 178951317Seric ** none. 179051317Seric */ 179151317Seric 179256678Seric maplocaluser(a, sendq, e) 179356678Seric register ADDRESS *a; 179456678Seric ADDRESS **sendq; 179556678Seric ENVELOPE *e; 179651317Seric { 179756678Seric register char **pvp; 179856678Seric register ADDRESS *a1 = NULL; 179958333Seric auto char *delimptr; 180056678Seric char pvpbuf[PSBUFSIZE]; 180151317Seric 180256678Seric if (tTd(29, 1)) 180356678Seric { 180456678Seric printf("maplocaluser: "); 180556678Seric printaddr(a, FALSE); 180656678Seric } 180765066Seric pvp = prescan(a->q_user, '\0', pvpbuf, sizeof pvpbuf, &delimptr); 180856678Seric if (pvp == NULL) 180956678Seric return; 181051317Seric 181165071Seric (void) rewrite(pvp, 5, 0, e); 181258050Seric if (pvp[0] == NULL || (pvp[0][0] & 0377) != CANONNET) 181356678Seric return; 181451317Seric 181556678Seric /* if non-null, mailer destination specified -- has it changed? */ 181664284Seric a1 = buildaddr(pvp, NULL, 0, e); 181756678Seric if (a1 == NULL || sameaddr(a, a1)) 181856678Seric return; 181951317Seric 182056678Seric /* mark old address as dead; insert new address */ 182156678Seric a->q_flags |= QDONTSEND; 182257731Seric if (tTd(29, 5)) 182357731Seric { 182457731Seric printf("maplocaluser: QDONTSEND "); 182557731Seric printaddr(a, FALSE); 182657731Seric } 182756678Seric a1->q_alias = a; 182864348Seric allocaddr(a1, RF_COPYALL, NULL); 182956678Seric (void) recipient(a1, sendq, e); 183051317Seric } 183158802Seric /* 183258802Seric ** DEQUOTE_INIT -- initialize dequote map 183358802Seric ** 183458802Seric ** This is a no-op. 183558802Seric ** 183658802Seric ** Parameters: 183758802Seric ** map -- the internal map structure. 183858802Seric ** args -- arguments. 183958802Seric ** 184058802Seric ** Returns: 184158802Seric ** TRUE. 184258802Seric */ 184358802Seric 184458802Seric bool 184560219Seric dequote_init(map, args) 184658802Seric MAP *map; 184758802Seric char *args; 184858802Seric { 184958805Seric register char *p = args; 185058805Seric 185158805Seric for (;;) 185258805Seric { 185358805Seric while (isascii(*p) && isspace(*p)) 185458805Seric p++; 185558805Seric if (*p != '-') 185658805Seric break; 185758805Seric switch (*++p) 185858805Seric { 185958805Seric case 'a': 186058805Seric map->map_app = ++p; 186158805Seric break; 186267824Seric 186367824Seric case 's': 186467824Seric map->map_coldelim = *++p; 186567824Seric break; 186658805Seric } 186758805Seric while (*p != '\0' && !(isascii(*p) && isspace(*p))) 186858805Seric p++; 186958805Seric if (*p != '\0') 187058805Seric *p = '\0'; 187158805Seric } 187258805Seric if (map->map_app != NULL) 187358805Seric map->map_app = newstr(map->map_app); 187458805Seric 187558802Seric return TRUE; 187658802Seric } 187758802Seric /* 187858802Seric ** DEQUOTE_MAP -- unquote an address 187958802Seric ** 188058802Seric ** Parameters: 188158802Seric ** map -- the internal map structure (ignored). 188260089Seric ** name -- the name to dequote. 188358802Seric ** av -- arguments (ignored). 188459084Seric ** statp -- pointer to status out-parameter. 188558802Seric ** 188658802Seric ** Returns: 188758802Seric ** NULL -- if there were no quotes, or if the resulting 188858802Seric ** unquoted buffer would not be acceptable to prescan. 188958802Seric ** else -- The dequoted buffer. 189058802Seric */ 189158802Seric 189258802Seric char * 189360089Seric dequote_map(map, name, av, statp) 189458802Seric MAP *map; 189560089Seric char *name; 189658802Seric char **av; 189759084Seric int *statp; 189858802Seric { 189958802Seric register char *p; 190058802Seric register char *q; 190158802Seric register char c; 190267824Seric int anglecnt = 0; 190367824Seric int cmntcnt = 0; 190467824Seric int quotecnt = 0; 190567824Seric int spacecnt = 0; 190667824Seric bool quotemode = FALSE; 190767824Seric bool bslashmode = FALSE; 190867824Seric char spacesub = map->map_coldelim; 190958802Seric 191060089Seric for (p = q = name; (c = *p++) != '\0'; ) 191158802Seric { 191258802Seric if (bslashmode) 191358802Seric { 191458802Seric bslashmode = FALSE; 191558802Seric *q++ = c; 191658802Seric continue; 191758802Seric } 191858802Seric 191967824Seric if (c == ' ' && spacesub != '\0') 192067824Seric c = spacesub; 192167764Seric 192258802Seric switch (c) 192358802Seric { 192458802Seric case '\\': 192558802Seric bslashmode = TRUE; 192658802Seric break; 192758802Seric 192858802Seric case '(': 192958802Seric cmntcnt++; 193058802Seric break; 193158802Seric 193258802Seric case ')': 193358802Seric if (cmntcnt-- <= 0) 193458802Seric return NULL; 193558802Seric break; 193659089Seric 193759089Seric case ' ': 193859089Seric spacecnt++; 193959089Seric break; 194058802Seric } 194158802Seric 194258802Seric if (cmntcnt > 0) 194358802Seric { 194458802Seric *q++ = c; 194558802Seric continue; 194658802Seric } 194758802Seric 194858802Seric switch (c) 194958802Seric { 195058802Seric case '"': 195158802Seric quotemode = !quotemode; 195258802Seric quotecnt++; 195358802Seric continue; 195458802Seric 195558802Seric case '<': 195658802Seric anglecnt++; 195758802Seric break; 195858802Seric 195958802Seric case '>': 196058802Seric if (anglecnt-- <= 0) 196158802Seric return NULL; 196258802Seric break; 196358802Seric } 196458802Seric *q++ = c; 196558802Seric } 196658802Seric 196758802Seric if (anglecnt != 0 || cmntcnt != 0 || bslashmode || 196859089Seric quotemode || quotecnt <= 0 || spacecnt != 0) 196958802Seric return NULL; 197058802Seric *q++ = '\0'; 197160089Seric return name; 197258802Seric } 1973