1297Seric # include <stdio.h> 2297Seric # include <ctype.h> 33312Seric # include "sendmail.h" 4297Seric 5*3382Seric static char SccsId[] = "@(#)parseaddr.c 3.12 03/27/81"; 6407Seric 7297Seric /* 8297Seric ** PARSE -- Parse an address 9297Seric ** 10297Seric ** Parses an address and breaks it up into three parts: a 11297Seric ** net to transmit the message on, the host to transmit it 12297Seric ** to, and a user on that host. These are loaded into an 132973Seric ** ADDRESS header with the values squirreled away if necessary. 14297Seric ** The "user" part may not be a real user; the process may 15297Seric ** just reoccur on that machine. For example, on a machine 16297Seric ** with an arpanet connection, the address 17297Seric ** csvax.bill@berkeley 18297Seric ** will break up to a "user" of 'csvax.bill' and a host 19297Seric ** of 'berkeley' -- to be transmitted over the arpanet. 20297Seric ** 21297Seric ** Parameters: 22297Seric ** addr -- the address to parse. 23297Seric ** a -- a pointer to the address descriptor buffer. 24297Seric ** If NULL, a header will be created. 25297Seric ** copyf -- determines what shall be copied: 26297Seric ** -1 -- don't copy anything. The printname 27297Seric ** (q_paddr) is just addr, and the 28297Seric ** user & host are allocated internally 29297Seric ** to parse. 30297Seric ** 0 -- copy out the parsed user & host, but 31297Seric ** don't copy the printname. 32297Seric ** +1 -- copy everything. 33297Seric ** 34297Seric ** Returns: 35297Seric ** A pointer to the address descriptor header (`a' if 36297Seric ** `a' is non-NULL). 37297Seric ** NULL on error. 38297Seric ** 39297Seric ** Side Effects: 40297Seric ** none 41297Seric ** 42297Seric ** Called By: 43297Seric ** main 44297Seric ** sendto 45297Seric ** alias 46297Seric ** savemail 47297Seric */ 48297Seric 493380Seric # define DELIMCHARS "$()<>,;\\\"\r\n" /* word delimiters */ 502091Seric # define SPACESUB ('.'|0200) /* substitution for <lwsp> */ 512091Seric 522973Seric ADDRESS * 53297Seric parse(addr, a, copyf) 54297Seric char *addr; 552973Seric register ADDRESS *a; 56297Seric int copyf; 57297Seric { 583149Seric register char **pvp; 593149Seric register struct mailer *m; 603149Seric extern char **prescan(); 612973Seric extern char *newstr(); 622990Seric extern char *strcpy(); 633149Seric extern ADDRESS *buildaddr(); 64297Seric 65297Seric /* 66297Seric ** Initialize and prescan address. 67297Seric */ 68297Seric 69297Seric To = addr; 703188Seric # ifdef DEBUG 713188Seric if (Debug) 723188Seric printf("\n--parse(%s)\n", addr); 733188Seric # endif DEBUG 743188Seric 753149Seric pvp = prescan(addr, '\0'); 763149Seric if (pvp == NULL) 77297Seric return (NULL); 78297Seric 79297Seric /* 803149Seric ** Apply rewriting rules. 81297Seric */ 82297Seric 833149Seric rewrite(pvp); 84297Seric 853149Seric /* 863149Seric ** See if we resolved to a real mailer. 873149Seric */ 88297Seric 893149Seric if (pvp[0][0] != CANONNET) 903149Seric { 913149Seric setstat(EX_USAGE); 923149Seric usrerr("cannot resolve name"); 933149Seric return (NULL); 94297Seric } 95297Seric 96297Seric /* 973149Seric ** Build canonical address from pvp. 98297Seric */ 99297Seric 1003149Seric a = buildaddr(pvp, a); 1013149Seric m = Mailer[a->q_mailer]; 102297Seric 103297Seric /* 1043149Seric ** Make local copies of the host & user and then 1053149Seric ** transport them out. 106297Seric */ 107297Seric 108297Seric if (copyf > 0) 1092973Seric a->q_paddr = newstr(addr); 110297Seric else 111297Seric a->q_paddr = addr; 112297Seric 1133149Seric if (copyf >= 0) 114297Seric { 1153149Seric if (a->q_host != NULL) 1163149Seric a->q_host = newstr(a->q_host); 117297Seric else 1183149Seric a->q_host = ""; 1193149Seric if (a->q_user != a->q_paddr) 1203149Seric a->q_user = newstr(a->q_user); 121297Seric } 122297Seric 123297Seric /* 124297Seric ** Do UPPER->lower case mapping unless inhibited. 125297Seric */ 126297Seric 1273149Seric if (!bitset(M_HST_UPPER, m->m_flags)) 128297Seric makelower(a->q_host); 1293149Seric if (!bitset(M_USR_UPPER, m->m_flags)) 130297Seric makelower(a->q_user); 131297Seric 132297Seric /* 133297Seric ** Compute return value. 134297Seric */ 135297Seric 136297Seric # ifdef DEBUG 1371583Seric if (Debug) 138297Seric printf("parse(\"%s\"): host \"%s\" user \"%s\" mailer %d\n", 1393149Seric addr, a->q_host, a->q_user, a->q_mailer); 140297Seric # endif DEBUG 141297Seric 142297Seric return (a); 143297Seric } 144297Seric /* 145297Seric ** PRESCAN -- Prescan name and make it canonical 146297Seric ** 147297Seric ** Scans a name and turns it into canonical form. This involves 148297Seric ** deleting blanks, comments (in parentheses), and turning the 149297Seric ** word "at" into an at-sign ("@"). The name is copied as this 150297Seric ** is done; it is legal to copy a name onto itself, since this 151297Seric ** process can only make things smaller. 152297Seric ** 153297Seric ** This routine knows about quoted strings and angle brackets. 154297Seric ** 155297Seric ** There are certain subtleties to this routine. The one that 156297Seric ** comes to mind now is that backslashes on the ends of names 157297Seric ** are silently stripped off; this is intentional. The problem 158297Seric ** is that some versions of sndmsg (like at LBL) set the kill 159297Seric ** character to something other than @ when reading addresses; 160297Seric ** so people type "csvax.eric\@berkeley" -- which screws up the 161297Seric ** berknet mailer. 162297Seric ** 163297Seric ** Parameters: 164297Seric ** addr -- the name to chomp. 165297Seric ** delim -- the delimiter for the address, normally 166297Seric ** '\0' or ','; \0 is accepted in any case. 167297Seric ** are moving in place; set buflim to high core. 168297Seric ** 169297Seric ** Returns: 1703149Seric ** A pointer to a vector of tokens. 171297Seric ** NULL on error. 172297Seric ** 173297Seric ** Side Effects: 1743149Seric ** none. 175297Seric */ 176297Seric 1773149Seric # define OPER 1 1783149Seric # define ATOM 2 1793149Seric # define EOTOK 3 1803149Seric # define QSTRING 4 1813149Seric # define SPACE 5 1823149Seric # define DOLLAR 6 1833149Seric # define GETONE 7 1843149Seric 1853149Seric char ** 1863149Seric prescan(addr, delim) 187297Seric char *addr; 188297Seric char delim; 189297Seric { 190297Seric register char *p; 1913149Seric static char buf[MAXNAME+MAXATOM]; 1923149Seric static char *av[MAXATOM+1]; 1933149Seric char **avp; 194297Seric bool space; 195297Seric bool bslashmode; 196297Seric int cmntcnt; 197297Seric int brccnt; 198297Seric register char c; 1993149Seric char *tok; 200297Seric register char *q; 2012973Seric extern char *index(); 2023149Seric register int state; 2033149Seric int nstate; 204297Seric 2052091Seric space = FALSE; 206297Seric q = buf; 2073149Seric bslashmode = FALSE; 208297Seric cmntcnt = brccnt = 0; 2093149Seric avp = av; 2103149Seric state = OPER; 2113149Seric for (p = addr; *p != '\0' && *p != delim; ) 212297Seric { 2133149Seric /* read a token */ 2143149Seric tok = q; 2153149Seric while ((c = *p++) != '\0' && c != delim) 216297Seric { 2173149Seric /* chew up special characters */ 2183149Seric *q = '\0'; 2193149Seric if (bslashmode) 2203149Seric { 2213149Seric c |= 0200; 2223149Seric bslashmode = FALSE; 2233149Seric } 2243149Seric else if (c == '\\') 2253149Seric { 2263149Seric bslashmode = TRUE; 2273149Seric continue; 2283149Seric } 2293149Seric 2303149Seric nstate = toktype(c); 2313149Seric switch (state) 2323149Seric { 2333149Seric case QSTRING: /* in quoted string */ 2343149Seric if (c == '"') 2353149Seric state = OPER; 2363149Seric break; 2373149Seric 2383149Seric case ATOM: /* regular atom */ 2393149Seric state = nstate; 2403149Seric if (state != ATOM) 2413149Seric { 2423149Seric state = EOTOK; 2433149Seric p--; 2443149Seric } 2453149Seric break; 2463149Seric 2473149Seric case GETONE: /* grab one character */ 2483149Seric state = OPER; 2493149Seric break; 2503149Seric 2513149Seric case EOTOK: /* after atom or q-string */ 2523149Seric state = nstate; 2533149Seric if (state == SPACE) 2543149Seric continue; 2553149Seric break; 2563149Seric 2573149Seric case SPACE: /* linear white space */ 2583149Seric state = nstate; 2593149Seric space = TRUE; 2603149Seric continue; 2613149Seric 2623149Seric case OPER: /* operator */ 2633149Seric if (nstate == SPACE) 2643149Seric continue; 2653149Seric state = nstate; 2663149Seric break; 2673149Seric 2683149Seric case DOLLAR: /* $- etc. */ 2693149Seric state = OPER; 2703149Seric switch (c) 2713149Seric { 2723149Seric case '$': /* literal $ */ 2733149Seric break; 2743149Seric 2753149Seric case '+': /* match anything */ 2763149Seric c = MATCHANY; 2773149Seric state = GETONE; 2783149Seric break; 2793149Seric 2803149Seric case '-': /* match one token */ 2813149Seric c = MATCHONE; 2823149Seric state = GETONE; 2833149Seric break; 2843149Seric 2853149Seric case '#': /* canonical net name */ 2863149Seric c = CANONNET; 2873149Seric break; 2883149Seric 2893149Seric case '@': /* canonical host name */ 2903149Seric c = CANONHOST; 2913149Seric break; 2923149Seric 2933149Seric case ':': /* canonical user name */ 2943149Seric c = CANONUSER; 2953149Seric break; 2963149Seric 2973149Seric default: 2983149Seric c = '$'; 2993149Seric state = OPER; 3003149Seric p--; 3013149Seric break; 3023149Seric } 3033149Seric break; 3043149Seric 3053149Seric default: 3063149Seric syserr("prescan: unknown state %d", state); 3073149Seric } 3083149Seric 3093149Seric if (state == OPER) 3103149Seric space = FALSE; 3113149Seric else if (state == EOTOK) 3123149Seric break; 3133149Seric if (c == '$' && delim == '\t') 3143149Seric { 3153149Seric state = DOLLAR; 3163149Seric continue; 3173149Seric } 3183149Seric 3193149Seric /* squirrel it away */ 3203149Seric if (q >= &buf[sizeof buf - 5]) 3213149Seric { 3223149Seric usrerr("Address too long"); 3233149Seric return (NULL); 3243149Seric } 3253149Seric if (space) 3263149Seric *q++ = SPACESUB; 3273149Seric *q++ = c; 3283149Seric 3293149Seric /* decide whether this represents end of token */ 3303149Seric if (state == OPER) 3313149Seric break; 332297Seric } 3333149Seric if (c == '\0' || c == delim) 3343149Seric p--; 3353149Seric 3363149Seric /* new token */ 3373149Seric if (tok == q) 338297Seric continue; 3393149Seric *q++ = '\0'; 3403149Seric 3413149Seric c = tok[0]; 3423149Seric if (c == '(') 3431378Seric { 344297Seric cmntcnt++; 3451378Seric continue; 3461378Seric } 347297Seric else if (c == ')') 348297Seric { 349297Seric if (cmntcnt <= 0) 350297Seric { 351297Seric usrerr("Unbalanced ')'"); 352297Seric return (NULL); 353297Seric } 354297Seric else 355297Seric { 356297Seric cmntcnt--; 357297Seric continue; 358297Seric } 359297Seric } 3603149Seric else if (cmntcnt > 0) 3612091Seric continue; 3623149Seric 3633149Seric *avp++ = tok; 3643149Seric 3653149Seric /* we prefer <> specs */ 3663149Seric if (c == '<') 367297Seric { 3682092Seric if (brccnt < 0) 3692092Seric { 3702092Seric usrerr("multiple < spec"); 3712092Seric return (NULL); 3722092Seric } 373297Seric brccnt++; 3742091Seric space = FALSE; 375297Seric if (brccnt == 1) 376297Seric { 377297Seric /* we prefer using machine readable name */ 378297Seric q = buf; 379297Seric *q = '\0'; 3803149Seric avp = av; 381297Seric continue; 382297Seric } 383297Seric } 384297Seric else if (c == '>') 385297Seric { 386297Seric if (brccnt <= 0) 387297Seric { 388297Seric usrerr("Unbalanced `>'"); 389297Seric return (NULL); 390297Seric } 391297Seric else 392297Seric brccnt--; 393297Seric if (brccnt <= 0) 3942092Seric { 3952092Seric brccnt = -1; 396297Seric continue; 3972092Seric } 398297Seric } 399297Seric 400297Seric /* 401297Seric ** Turn "at" into "@", 4021378Seric ** but only if "at" is a word. 403297Seric */ 404297Seric 4053149Seric if (lower(tok[0]) == 'a' && lower(tok[1]) == 't' && tok[2] == '\0') 406297Seric { 4073149Seric tok[0] = '@'; 4083149Seric tok[1] = '\0'; 409297Seric } 4103149Seric } 4113149Seric *avp = NULL; 4123149Seric if (cmntcnt > 0) 4133149Seric usrerr("Unbalanced '('"); 4143149Seric else if (brccnt > 0) 4153149Seric usrerr("Unbalanced '<'"); 4163149Seric else if (state == QSTRING) 4173149Seric usrerr("Unbalanced '\"'"); 4183149Seric else if (av[0] != NULL) 4193149Seric return (av); 4203149Seric return (NULL); 4213149Seric } 4223149Seric /* 4233149Seric ** TOKTYPE -- return token type 4243149Seric ** 4253149Seric ** Parameters: 4263149Seric ** c -- the character in question. 4273149Seric ** 4283149Seric ** Returns: 4293149Seric ** Its type. 4303149Seric ** 4313149Seric ** Side Effects: 4323149Seric ** none. 4333149Seric */ 434297Seric 4353149Seric toktype(c) 4363149Seric register char c; 4373149Seric { 4383380Seric static char buf[50]; 439*3382Seric static bool firstime = TRUE; 4403380Seric 441*3382Seric if (firstime) 4423380Seric { 443*3382Seric firstime = FALSE; 4443380Seric expand("$o", buf, &buf[sizeof buf - 1]); 4453380Seric strcat(buf, DELIMCHARS); 4463380Seric } 4473149Seric if (isspace(c)) 4483149Seric return (SPACE); 4493380Seric if (iscntrl(c) || index(buf, c) != NULL) 4503149Seric return (OPER); 4513149Seric return (ATOM); 4523149Seric } 4533149Seric /* 4543149Seric ** REWRITE -- apply rewrite rules to token vector. 4553149Seric ** 4563149Seric ** Parameters: 4573149Seric ** pvp -- pointer to token vector. 4583149Seric ** 4593149Seric ** Returns: 4603149Seric ** none. 4613149Seric ** 4623149Seric ** Side Effects: 4633149Seric ** pvp is modified. 4643149Seric */ 4652091Seric 4663149Seric struct match 4673149Seric { 4683149Seric char **firsttok; /* first token matched */ 4693149Seric char **lasttok; /* last token matched */ 4703149Seric char name; /* name of parameter */ 4713149Seric }; 4723149Seric 4733149Seric # define MAXMATCH 8 /* max params per rewrite */ 4743149Seric 4753149Seric 4763149Seric rewrite(pvp) 4773149Seric char **pvp; 4783149Seric { 4793149Seric register char *ap; /* address pointer */ 4803149Seric register char *rp; /* rewrite pointer */ 4813149Seric register char **avp; /* address vector pointer */ 4823149Seric register char **rvp; /* rewrite vector pointer */ 4833149Seric struct rewrite *rwr; 4843149Seric struct match mlist[MAXMATCH]; 4853149Seric char *npvp[MAXATOM+1]; /* temporary space for rebuild */ 4863149Seric 4873188Seric # ifdef DEBUGX 4883149Seric if (Debug) 4893149Seric { 4903149Seric printf("rewrite: original pvp:\n"); 4913149Seric printav(pvp); 4923149Seric } 4933188Seric # endif DEBUGX 4943149Seric 4953149Seric /* 4963149Seric ** Run through the list of rewrite rules, applying 4973149Seric ** any that match. 4983149Seric */ 4993149Seric 5003149Seric for (rwr = RewriteRules; rwr != NULL; ) 5013149Seric { 5023188Seric # ifdef DEBUGX 5033149Seric if (Debug) 504297Seric { 5053149Seric printf("-----trying rule:\n"); 5063149Seric printav(rwr->r_lhs); 5073149Seric } 5083188Seric # endif DEBUGX 5093149Seric 5103149Seric /* try to match on this rule */ 5113149Seric clrmatch(mlist); 5123149Seric for (rvp = rwr->r_lhs, avp = pvp; *avp != NULL; ) 5133149Seric { 5143149Seric ap = *avp; 5153149Seric rp = *rvp; 5163149Seric 5173149Seric if (rp == NULL) 518297Seric { 5193149Seric /* end-of-pattern before end-of-address */ 5203149Seric goto fail; 521297Seric } 5223149Seric 5233149Seric switch (*rp) 5243149Seric { 5253149Seric case MATCHONE: 5263149Seric /* match exactly one token */ 5273149Seric setmatch(mlist, rp[1], avp, avp); 5283149Seric break; 5293149Seric 5303149Seric case MATCHANY: 5313149Seric /* match any number of tokens */ 5323149Seric setmatch(mlist, rp[1], NULL, avp); 5333149Seric break; 5343149Seric 5353149Seric default: 5363149Seric /* must have exact match */ 5373149Seric /* can scribble rp & ap here safely */ 5383154Seric while (*rp != '\0' || *ap != '\0') 5393149Seric { 5403149Seric if (*rp++ != lower(*ap++)) 5413149Seric goto fail; 5423149Seric } 5433149Seric break; 5443149Seric } 5453149Seric 5463149Seric /* successful match on this token */ 5473149Seric avp++; 5483149Seric rvp++; 5493149Seric continue; 5503149Seric 5513149Seric fail: 5523149Seric /* match failed -- back up */ 5533149Seric while (--rvp >= rwr->r_lhs) 5543149Seric { 5553149Seric rp = *rvp; 5563149Seric if (*rp == MATCHANY) 5573149Seric break; 5583149Seric 5593149Seric /* can't extend match: back up everything */ 5603149Seric avp--; 5613149Seric 5623149Seric if (*rp == MATCHONE) 5633149Seric { 5643149Seric /* undo binding */ 5653149Seric setmatch(mlist, rp[1], NULL, NULL); 5663149Seric } 5673149Seric } 5683149Seric 5693149Seric if (rvp < rwr->r_lhs) 5703149Seric { 5713149Seric /* total failure to match */ 5723149Seric break; 5733149Seric } 574297Seric } 5753149Seric 5763149Seric /* 5773149Seric ** See if we successfully matched 5783149Seric */ 5793149Seric 5803149Seric if (rvp >= rwr->r_lhs && *rvp == NULL) 5813149Seric { 5823188Seric # ifdef DEBUGX 5833149Seric if (Debug) 5843149Seric { 5853149Seric printf("-----rule matches:\n"); 5863149Seric printav(rwr->r_rhs); 5873149Seric } 5883188Seric # endif DEBUGX 5893149Seric 5903149Seric /* substitute */ 5913149Seric for (rvp = rwr->r_rhs, avp = npvp; *rvp != NULL; rvp++) 5923149Seric { 5933149Seric rp = *rvp; 5943149Seric if (*rp == MATCHANY) 5953149Seric { 5963149Seric register struct match *m; 5973149Seric register char **pp; 5983149Seric extern struct match *findmatch(); 5993149Seric 6003149Seric m = findmatch(mlist, rp[1]); 6013149Seric if (m != NULL) 6023149Seric { 6033149Seric pp = m->firsttok; 6043149Seric do 6053149Seric { 6063149Seric *avp++ = *pp; 6073149Seric } while (pp++ != m->lasttok); 6083149Seric } 6093149Seric } 6103149Seric else 6113149Seric *avp++ = rp; 6123149Seric } 6133149Seric *avp++ = NULL; 6143149Seric bmove(npvp, pvp, (avp - npvp) * sizeof *avp); 6153149Seric # ifdef DEBUG 6163149Seric if (Debug) 6173149Seric { 6183188Seric char **vp; 6193188Seric 6203188Seric printf("rewritten as `"); 6213188Seric for (vp = pvp; *vp != NULL; vp++) 6223188Seric xputs(*vp); 6233188Seric printf("'\n"); 6243149Seric } 6253149Seric # endif DEBUG 6263149Seric if (pvp[0][0] == CANONNET) 6273149Seric break; 6283149Seric } 6293149Seric else 6303149Seric { 6313188Seric # ifdef DEBUGX 6323149Seric if (Debug) 6333149Seric printf("----- rule fails\n"); 6343188Seric # endif DEBUGX 6353149Seric rwr = rwr->r_next; 6363149Seric } 637297Seric } 6383149Seric } 6393149Seric /* 6403149Seric ** SETMATCH -- set parameter value in match vector 6413149Seric ** 6423149Seric ** Parameters: 6433149Seric ** mlist -- list of match values. 6443149Seric ** name -- the character name of this parameter. 6453149Seric ** first -- the first location of the replacement. 6463149Seric ** last -- the last location of the replacement. 6473149Seric ** 6483149Seric ** If last == NULL, delete this entry. 6493149Seric ** If first == NULL, extend this entry (or add it if 6503149Seric ** it does not exist). 6513149Seric ** 6523149Seric ** Returns: 6533149Seric ** nothing. 6543149Seric ** 6553149Seric ** Side Effects: 6563149Seric ** munges with mlist. 6573149Seric */ 6583149Seric 6593149Seric setmatch(mlist, name, first, last) 6603149Seric struct match *mlist; 6613149Seric char name; 6623149Seric char **first; 6633149Seric char **last; 6643149Seric { 6653149Seric register struct match *m; 6663149Seric struct match *nullm = NULL; 6673149Seric 6683149Seric for (m = mlist; m < &mlist[MAXMATCH]; m++) 6693149Seric { 6703149Seric if (m->name == name) 6713149Seric break; 6723149Seric if (m->name == '\0') 6733149Seric nullm = m; 6743149Seric } 6753149Seric 6763149Seric if (m >= &mlist[MAXMATCH]) 6773149Seric m = nullm; 6783149Seric 6793149Seric if (last == NULL) 6803149Seric { 6813149Seric m->name = '\0'; 6823149Seric return; 6833149Seric } 6843149Seric 6853149Seric if (m->name == '\0') 6863149Seric { 6873149Seric if (first == NULL) 6883149Seric m->firsttok = last; 6893149Seric else 6903149Seric m->firsttok = first; 6913149Seric } 6923149Seric m->name = name; 6933149Seric m->lasttok = last; 6943149Seric } 6953149Seric /* 6963149Seric ** FINDMATCH -- find match in mlist 6973149Seric ** 6983149Seric ** Parameters: 6993149Seric ** mlist -- list to search. 7003149Seric ** name -- name to find. 7013149Seric ** 7023149Seric ** Returns: 7033149Seric ** pointer to match structure. 7043149Seric ** NULL if no match. 7053149Seric ** 7063149Seric ** Side Effects: 7073149Seric ** none. 7083149Seric */ 7093149Seric 7103149Seric struct match * 7113149Seric findmatch(mlist, name) 7123149Seric struct match *mlist; 7133149Seric char name; 7143149Seric { 7153149Seric register struct match *m; 7163149Seric 7173149Seric for (m = mlist; m < &mlist[MAXMATCH]; m++) 7183149Seric { 7193149Seric if (m->name == name) 7203149Seric return (m); 7213149Seric } 7223149Seric 723297Seric return (NULL); 724297Seric } 7253149Seric /* 7263149Seric ** CLRMATCH -- clear match list 7273149Seric ** 7283149Seric ** Parameters: 7293149Seric ** mlist -- list to clear. 7303149Seric ** 7313149Seric ** Returns: 7323149Seric ** none. 7333149Seric ** 7343149Seric ** Side Effects: 7353149Seric ** mlist is cleared. 7363149Seric */ 7373149Seric 7383149Seric clrmatch(mlist) 7393149Seric struct match *mlist; 7403149Seric { 7413149Seric register struct match *m; 7423149Seric 7433149Seric for (m = mlist; m < &mlist[MAXMATCH]; m++) 7443149Seric m->name = '\0'; 7453149Seric } 7463149Seric /* 7473149Seric ** BUILDADDR -- build address from token vector. 7483149Seric ** 7493149Seric ** Parameters: 7503149Seric ** tv -- token vector. 7513149Seric ** a -- pointer to address descriptor to fill. 7523149Seric ** If NULL, one will be allocated. 7533149Seric ** 7543149Seric ** Returns: 7553149Seric ** 'a' 7563149Seric ** 7573149Seric ** Side Effects: 7583149Seric ** fills in 'a' 7593149Seric */ 7603149Seric 7613149Seric ADDRESS * 7623149Seric buildaddr(tv, a) 7633149Seric register char **tv; 7643149Seric register ADDRESS *a; 7653149Seric { 7663149Seric register int i; 7673149Seric static char buf[MAXNAME]; 7683149Seric struct mailer **mp; 7693149Seric register struct mailer *m; 7703149Seric extern char *xalloc(); 7713149Seric 7723149Seric if (a == NULL) 7733149Seric a = (ADDRESS *) xalloc(sizeof *a); 7743188Seric a->q_flags = 0; 7753149Seric 7763149Seric /* figure out what net/mailer to use */ 7773149Seric if (**tv != CANONNET) 7783149Seric syserr("buildaddr: no net"); 7793149Seric tv++; 7803154Seric for (mp = Mailer, i = 0; (m = *mp++) != NULL; i++) 7813149Seric { 7823149Seric if (strcmp(m->m_name, *tv) == 0) 7833149Seric break; 7843149Seric } 7853149Seric if (m == NULL) 7863149Seric syserr("buildaddr: unknown net %s", *tv); 7873149Seric a->q_mailer = i; 7883149Seric 7893149Seric /* figure out what host (if any) */ 7903149Seric tv++; 7913149Seric if (!bitset(M_NOHOST, m->m_flags)) 7923149Seric { 7933149Seric if (**tv != CANONHOST) 7943149Seric syserr("buildaddr: no host"); 7953149Seric tv++; 7963149Seric a->q_host = *tv; 7973149Seric tv++; 7983149Seric } 7993149Seric else 8003149Seric a->q_host = NULL; 8013149Seric 8023149Seric /* figure out the user */ 8033149Seric if (**tv != CANONUSER) 8043149Seric syserr("buildaddr: no user"); 8053149Seric buf[0] = '\0'; 8063149Seric while (**++tv != NULL) 8073149Seric strcat(buf, *tv); 8083149Seric a->q_user = buf; 8093149Seric 8103149Seric return (a); 8113149Seric } 8123188Seric /* 8133188Seric ** SAMEADDR -- Determine if two addresses are the same 8143188Seric ** 8153188Seric ** This is not just a straight comparison -- if the mailer doesn't 8163188Seric ** care about the host we just ignore it, etc. 8173188Seric ** 8183188Seric ** Parameters: 8193188Seric ** a, b -- pointers to the internal forms to compare. 8203188Seric ** wildflg -- if TRUE, 'a' may have no user specified, 8213188Seric ** in which case it is to match anything. 8223188Seric ** 8233188Seric ** Returns: 8243188Seric ** TRUE -- they represent the same mailbox. 8253188Seric ** FALSE -- they don't. 8263188Seric ** 8273188Seric ** Side Effects: 8283188Seric ** none. 8293188Seric */ 8303188Seric 8313188Seric bool 8323188Seric sameaddr(a, b, wildflg) 8333188Seric register ADDRESS *a; 8343188Seric register ADDRESS *b; 8353188Seric bool wildflg; 8363188Seric { 8373188Seric /* if they don't have the same mailer, forget it */ 8383188Seric if (a->q_mailer != b->q_mailer) 8393188Seric return (FALSE); 8403188Seric 8413188Seric /* if the user isn't the same, we can drop out */ 8423188Seric if ((!wildflg || a->q_user[0] != '\0') && strcmp(a->q_user, b->q_user) != 0) 8433188Seric return (FALSE); 8443188Seric 8453188Seric /* if the mailer ignores hosts, we have succeeded! */ 8463188Seric if (bitset(M_NOHOST, Mailer[a->q_mailer]->m_flags)) 8473188Seric return (TRUE); 8483188Seric 8493188Seric /* otherwise compare hosts (but be careful for NULL ptrs) */ 8503188Seric if (a->q_host == NULL || b->q_host == NULL) 8513188Seric return (FALSE); 8523188Seric if (strcmp(a->q_host, b->q_host) != 0) 8533188Seric return (FALSE); 8543188Seric 8553188Seric return (TRUE); 8563188Seric } 8573234Seric /* 8583234Seric ** PRINTADDR -- print address (for debugging) 8593234Seric ** 8603234Seric ** Parameters: 8613234Seric ** a -- the address to print 8623234Seric ** follow -- follow the q_next chain. 8633234Seric ** 8643234Seric ** Returns: 8653234Seric ** none. 8663234Seric ** 8673234Seric ** Side Effects: 8683234Seric ** none. 8693234Seric */ 8703234Seric 8713234Seric printaddr(a, follow) 8723234Seric register ADDRESS *a; 8733234Seric bool follow; 8743234Seric { 8753234Seric while (a != NULL) 8763234Seric { 8773234Seric printf("addr@%x: ", a); 8783234Seric fflush(stdout); 8793234Seric printf("%s: mailer %d (%s), host `%s', user `%s'\n", a->q_paddr, 8803234Seric a->q_mailer, Mailer[a->q_mailer]->m_name, a->q_host, a->q_user); 8813234Seric printf("\tnext=%x flags=%o, rmailer %d\n", a->q_next, 8823234Seric a->q_flags, a->q_rmailer); 8833234Seric 8843234Seric if (!follow) 8853234Seric return; 8863234Seric a = a->q_next; 8873234Seric } 8883234Seric } 889