1 /* 2 * Copyright (c) 1983 Eric P. Allman 3 * Copyright (c) 1988 Regents of the University of California. 4 * All rights reserved. 5 * 6 * %sccs.include.redist.c% 7 */ 8 9 #ifndef lint 10 static char sccsid[] = "@(#)parseaddr.c 6.32 (Berkeley) 03/25/93"; 11 #endif /* not lint */ 12 13 # include "sendmail.h" 14 15 /* 16 ** PARSEADDR -- Parse an address 17 ** 18 ** Parses an address and breaks it up into three parts: a 19 ** net to transmit the message on, the host to transmit it 20 ** to, and a user on that host. These are loaded into an 21 ** ADDRESS header with the values squirreled away if necessary. 22 ** The "user" part may not be a real user; the process may 23 ** just reoccur on that machine. For example, on a machine 24 ** with an arpanet connection, the address 25 ** csvax.bill@berkeley 26 ** will break up to a "user" of 'csvax.bill' and a host 27 ** of 'berkeley' -- to be transmitted over the arpanet. 28 ** 29 ** Parameters: 30 ** addr -- the address to parse. 31 ** a -- a pointer to the address descriptor buffer. 32 ** If NULL, a header will be created. 33 ** copyf -- determines what shall be copied: 34 ** -1 -- don't copy anything. The printname 35 ** (q_paddr) is just addr, and the 36 ** user & host are allocated internally 37 ** to parse. 38 ** 0 -- copy out the parsed user & host, but 39 ** don't copy the printname. 40 ** +1 -- copy everything. 41 ** delim -- the character to terminate the address, passed 42 ** to prescan. 43 ** delimptr -- if non-NULL, set to the location of the 44 ** delim character that was found. 45 ** e -- the envelope that will contain this address. 46 ** 47 ** Returns: 48 ** A pointer to the address descriptor header (`a' if 49 ** `a' is non-NULL). 50 ** NULL on error. 51 ** 52 ** Side Effects: 53 ** none 54 */ 55 56 /* following delimiters are inherent to the internal algorithms */ 57 # define DELIMCHARS "\201()<>,;\\\"\r\n" /* word delimiters */ 58 59 ADDRESS * 60 parseaddr(addr, a, copyf, delim, delimptr, e) 61 char *addr; 62 register ADDRESS *a; 63 int copyf; 64 char delim; 65 char **delimptr; 66 register ENVELOPE *e; 67 { 68 register char **pvp; 69 auto char *delimptrbuf; 70 char pvpbuf[PSBUFSIZE]; 71 extern char **prescan(); 72 extern ADDRESS *buildaddr(); 73 extern bool invalidaddr(); 74 75 /* 76 ** Initialize and prescan address. 77 */ 78 79 e->e_to = addr; 80 if (tTd(20, 1)) 81 printf("\n--parseaddr(%s)\n", addr); 82 83 if (invalidaddr(addr)) 84 { 85 if (tTd(20, 1)) 86 printf("parseaddr-->bad address\n"); 87 return NULL; 88 } 89 90 if (delimptr == NULL) 91 delimptr = &delimptrbuf; 92 93 pvp = prescan(addr, delim, pvpbuf, delimptr); 94 if (pvp == NULL) 95 { 96 if (tTd(20, 1)) 97 printf("parseaddr-->NULL\n"); 98 return (NULL); 99 } 100 101 /* 102 ** Apply rewriting rules. 103 ** Ruleset 0 does basic parsing. It must resolve. 104 */ 105 106 rewrite(pvp, 3); 107 rewrite(pvp, 0); 108 109 /* 110 ** See if we resolved to a real mailer. 111 */ 112 113 if ((pvp[0][0] & 0377) != CANONNET) 114 { 115 setstat(EX_USAGE); 116 syserr("554 cannot resolve name"); 117 return (NULL); 118 } 119 120 /* 121 ** Build canonical address from pvp. 122 */ 123 124 a = buildaddr(pvp, a); 125 if (a == NULL) 126 return (NULL); 127 128 /* 129 ** Make local copies of the host & user and then 130 ** transport them out. 131 */ 132 133 allocaddr(a, copyf, addr, *delimptr); 134 135 /* 136 ** Compute return value. 137 */ 138 139 if (tTd(20, 1)) 140 { 141 printf("parseaddr-->"); 142 printaddr(a, FALSE); 143 } 144 145 return (a); 146 } 147 /* 148 ** INVALIDADDR -- check for address containing meta-characters 149 ** 150 ** Parameters: 151 ** addr -- the address to check. 152 ** 153 ** Returns: 154 ** TRUE -- if the address has any "wierd" characters 155 ** FALSE -- otherwise. 156 */ 157 158 bool 159 invalidaddr(addr) 160 register char *addr; 161 { 162 for (; *addr != '\0'; addr++) 163 { 164 if ((*addr & 0340) != 0200) 165 continue; 166 setstat(EX_USAGE); 167 usrerr("553 Address contained invalid control characters"); 168 return TRUE; 169 } 170 return FALSE; 171 } 172 /* 173 ** ALLOCADDR -- do local allocations of address on demand. 174 ** 175 ** Also lowercases the host name if requested. 176 ** 177 ** Parameters: 178 ** a -- the address to reallocate. 179 ** copyf -- the copy flag (see parseaddr for description). 180 ** paddr -- the printname of the address. 181 ** delimptr -- a pointer to the address delimiter. Must be set. 182 ** 183 ** Returns: 184 ** none. 185 ** 186 ** Side Effects: 187 ** Copies portions of a into local buffers as requested. 188 */ 189 190 allocaddr(a, copyf, paddr, delimptr) 191 register ADDRESS *a; 192 int copyf; 193 char *paddr; 194 char *delimptr; 195 { 196 register MAILER *m = a->q_mailer; 197 198 if (tTd(24, 4)) 199 printf("allocaddr(copyf=%d, paddr=%s)\n", copyf, paddr); 200 201 if (copyf > 0 && paddr != NULL) 202 { 203 char savec = *delimptr; 204 205 *delimptr = '\0'; 206 a->q_paddr = newstr(paddr); 207 *delimptr = savec; 208 } 209 else 210 a->q_paddr = paddr; 211 212 if (a->q_user == NULL) 213 a->q_user = ""; 214 if (a->q_host == NULL) 215 a->q_host = ""; 216 217 if (copyf >= 0) 218 { 219 a->q_host = newstr(a->q_host); 220 if (a->q_user != a->q_paddr) 221 a->q_user = newstr(a->q_user); 222 } 223 224 if (a->q_paddr == NULL) 225 a->q_paddr = a->q_user; 226 } 227 /* 228 ** PRESCAN -- Prescan name and make it canonical 229 ** 230 ** Scans a name and turns it into a set of tokens. This process 231 ** deletes blanks and comments (in parentheses). 232 ** 233 ** This routine knows about quoted strings and angle brackets. 234 ** 235 ** There are certain subtleties to this routine. The one that 236 ** comes to mind now is that backslashes on the ends of names 237 ** are silently stripped off; this is intentional. The problem 238 ** is that some versions of sndmsg (like at LBL) set the kill 239 ** character to something other than @ when reading addresses; 240 ** so people type "csvax.eric\@berkeley" -- which screws up the 241 ** berknet mailer. 242 ** 243 ** Parameters: 244 ** addr -- the name to chomp. 245 ** delim -- the delimiter for the address, normally 246 ** '\0' or ','; \0 is accepted in any case. 247 ** If '\t' then we are reading the .cf file. 248 ** pvpbuf -- place to put the saved text -- note that 249 ** the pointers are static. 250 ** delimptr -- if non-NULL, set to the location of the 251 ** terminating delimiter. 252 ** 253 ** Returns: 254 ** A pointer to a vector of tokens. 255 ** NULL on error. 256 */ 257 258 /* states and character types */ 259 # define OPR 0 /* operator */ 260 # define ATM 1 /* atom */ 261 # define QST 2 /* in quoted string */ 262 # define SPC 3 /* chewing up spaces */ 263 # define ONE 4 /* pick up one character */ 264 265 # define NSTATES 5 /* number of states */ 266 # define TYPE 017 /* mask to select state type */ 267 268 /* meta bits for table */ 269 # define M 020 /* meta character; don't pass through */ 270 # define B 040 /* cause a break */ 271 # define MB M|B /* meta-break */ 272 273 static short StateTab[NSTATES][NSTATES] = 274 { 275 /* oldst chtype> OPR ATM QST SPC ONE */ 276 /*OPR*/ OPR|B, ATM|B, QST|B, SPC|MB, ONE|B, 277 /*ATM*/ OPR|B, ATM, QST|B, SPC|MB, ONE|B, 278 /*QST*/ QST, QST, OPR, QST, QST, 279 /*SPC*/ OPR, ATM, QST, SPC|M, ONE, 280 /*ONE*/ OPR, OPR, OPR, OPR, OPR, 281 }; 282 283 # define NOCHAR -1 /* signal nothing in lookahead token */ 284 285 char ** 286 prescan(addr, delim, pvpbuf, delimptr) 287 char *addr; 288 char delim; 289 char pvpbuf[]; 290 char **delimptr; 291 { 292 register char *p; 293 register char *q; 294 register int c; 295 char **avp; 296 bool bslashmode; 297 int cmntcnt; 298 int anglecnt; 299 char *tok; 300 int state; 301 int newstate; 302 static char *av[MAXATOM+1]; 303 extern int errno; 304 305 /* make sure error messages don't have garbage on them */ 306 errno = 0; 307 308 q = pvpbuf; 309 bslashmode = FALSE; 310 cmntcnt = 0; 311 anglecnt = 0; 312 avp = av; 313 state = ATM; 314 c = NOCHAR; 315 p = addr; 316 if (tTd(22, 11)) 317 { 318 printf("prescan: "); 319 xputs(p); 320 (void) putchar('\n'); 321 } 322 323 do 324 { 325 /* read a token */ 326 tok = q; 327 for (;;) 328 { 329 /* store away any old lookahead character */ 330 if (c != NOCHAR) 331 { 332 /* see if there is room */ 333 if (q >= &pvpbuf[PSBUFSIZE - 5]) 334 { 335 usrerr("553 Address too long"); 336 if (delimptr != NULL) 337 *delimptr = p; 338 return (NULL); 339 } 340 341 /* squirrel it away */ 342 *q++ = c; 343 } 344 345 /* read a new input character */ 346 c = *p++; 347 if (c == '\0') 348 { 349 /* diagnose and patch up bad syntax */ 350 if (state == QST) 351 { 352 usrerr("553 Unbalanced '\"'"); 353 c = '"'; 354 } 355 else if (cmntcnt > 0) 356 { 357 usrerr("553 Unbalanced '('"); 358 c = ')'; 359 } 360 else if (anglecnt > 0) 361 { 362 c = '>'; 363 usrerr("553 Unbalanced '<'"); 364 } 365 else 366 break; 367 368 p--; 369 } 370 else if (c == delim && anglecnt <= 0 && 371 cmntcnt <= 0 && state != QST) 372 break; 373 374 if (tTd(22, 101)) 375 printf("c=%c, s=%d; ", c, state); 376 377 /* chew up special characters */ 378 *q = '\0'; 379 if (bslashmode) 380 { 381 /* kludge \! for naive users */ 382 if (cmntcnt > 0) 383 c = NOCHAR; 384 else if (c != '!') 385 *q++ = '\\'; 386 bslashmode = FALSE; 387 continue; 388 } 389 390 if (c == '\\') 391 { 392 bslashmode = TRUE; 393 c = NOCHAR; 394 continue; 395 } 396 else if (state == QST) 397 { 398 /* do nothing, just avoid next clauses */ 399 } 400 else if (c == '(') 401 { 402 cmntcnt++; 403 c = NOCHAR; 404 } 405 else if (c == ')') 406 { 407 if (cmntcnt <= 0) 408 { 409 usrerr("553 Unbalanced ')'"); 410 if (delimptr != NULL) 411 *delimptr = p; 412 return (NULL); 413 } 414 else 415 cmntcnt--; 416 } 417 else if (cmntcnt > 0) 418 c = NOCHAR; 419 else if (c == '<') 420 anglecnt++; 421 else if (c == '>') 422 { 423 if (anglecnt <= 0) 424 { 425 usrerr("553 Unbalanced '>'"); 426 if (delimptr != NULL) 427 *delimptr = p; 428 return (NULL); 429 } 430 anglecnt--; 431 } 432 else if (delim == ' ' && isascii(c) && isspace(c)) 433 c = ' '; 434 435 if (c == NOCHAR) 436 continue; 437 438 /* see if this is end of input */ 439 if (c == delim && anglecnt <= 0 && state != QST) 440 break; 441 442 newstate = StateTab[state][toktype(c)]; 443 if (tTd(22, 101)) 444 printf("ns=%02o\n", newstate); 445 state = newstate & TYPE; 446 if (bitset(M, newstate)) 447 c = NOCHAR; 448 if (bitset(B, newstate)) 449 break; 450 } 451 452 /* new token */ 453 if (tok != q) 454 { 455 *q++ = '\0'; 456 if (tTd(22, 36)) 457 { 458 printf("tok="); 459 xputs(tok); 460 (void) putchar('\n'); 461 } 462 if (avp >= &av[MAXATOM]) 463 { 464 syserr("553 prescan: too many tokens"); 465 if (delimptr != NULL) 466 *delimptr = p; 467 return (NULL); 468 } 469 *avp++ = tok; 470 } 471 } while (c != '\0' && (c != delim || anglecnt > 0)); 472 *avp = NULL; 473 p--; 474 if (delimptr != NULL) 475 *delimptr = p; 476 if (tTd(22, 12)) 477 { 478 printf("prescan==>"); 479 printav(av); 480 } 481 if (av[0] == NULL) 482 return (NULL); 483 return (av); 484 } 485 /* 486 ** TOKTYPE -- return token type 487 ** 488 ** Parameters: 489 ** c -- the character in question. 490 ** 491 ** Returns: 492 ** Its type. 493 ** 494 ** Side Effects: 495 ** none. 496 */ 497 498 toktype(c) 499 register int c; 500 { 501 static char buf[50]; 502 static bool firstime = TRUE; 503 504 if (firstime) 505 { 506 firstime = FALSE; 507 expand("\201o", buf, &buf[sizeof buf - 1], CurEnv); 508 (void) strcat(buf, DELIMCHARS); 509 } 510 c &= 0377; 511 if (c == MATCHCLASS || c == MATCHREPL || c == MATCHNCLASS) 512 return (ONE); 513 if (c == '"') 514 return (QST); 515 if ((c & 0340) == 0200) 516 return (OPR); 517 if (!isascii(c)) 518 return (ATM); 519 if (isspace(c) || c == ')') 520 return (SPC); 521 if (strchr(buf, c) != NULL) 522 return (OPR); 523 return (ATM); 524 } 525 /* 526 ** REWRITE -- apply rewrite rules to token vector. 527 ** 528 ** This routine is an ordered production system. Each rewrite 529 ** rule has a LHS (called the pattern) and a RHS (called the 530 ** rewrite); 'rwr' points the the current rewrite rule. 531 ** 532 ** For each rewrite rule, 'avp' points the address vector we 533 ** are trying to match against, and 'pvp' points to the pattern. 534 ** If pvp points to a special match value (MATCHZANY, MATCHANY, 535 ** MATCHONE, MATCHCLASS, MATCHNCLASS) then the address in avp 536 ** matched is saved away in the match vector (pointed to by 'mvp'). 537 ** 538 ** When a match between avp & pvp does not match, we try to 539 ** back out. If we back up over MATCHONE, MATCHCLASS, or MATCHNCLASS 540 ** we must also back out the match in mvp. If we reach a 541 ** MATCHANY or MATCHZANY we just extend the match and start 542 ** over again. 543 ** 544 ** When we finally match, we rewrite the address vector 545 ** and try over again. 546 ** 547 ** Parameters: 548 ** pvp -- pointer to token vector. 549 ** 550 ** Returns: 551 ** none. 552 ** 553 ** Side Effects: 554 ** pvp is modified. 555 */ 556 557 struct match 558 { 559 char **first; /* first token matched */ 560 char **last; /* last token matched */ 561 }; 562 563 # define MAXMATCH 9 /* max params per rewrite */ 564 565 566 rewrite(pvp, ruleset) 567 char **pvp; 568 int ruleset; 569 { 570 register char *ap; /* address pointer */ 571 register char *rp; /* rewrite pointer */ 572 register char **avp; /* address vector pointer */ 573 register char **rvp; /* rewrite vector pointer */ 574 register struct match *mlp; /* cur ptr into mlist */ 575 register struct rewrite *rwr; /* pointer to current rewrite rule */ 576 struct match mlist[MAXMATCH]; /* stores match on LHS */ 577 char *npvp[MAXATOM+1]; /* temporary space for rebuild */ 578 579 if (OpMode == MD_TEST || tTd(21, 2)) 580 { 581 printf("rewrite: ruleset %2d input:", ruleset); 582 printav(pvp); 583 } 584 if (ruleset < 0 || ruleset >= MAXRWSETS) 585 { 586 syserr("554 rewrite: illegal ruleset number %d", ruleset); 587 return; 588 } 589 if (pvp == NULL) 590 return; 591 592 /* 593 ** Run through the list of rewrite rules, applying 594 ** any that match. 595 */ 596 597 for (rwr = RewriteRules[ruleset]; rwr != NULL; ) 598 { 599 int loopcount = 0; 600 601 if (tTd(21, 12)) 602 { 603 printf("-----trying rule:"); 604 printav(rwr->r_lhs); 605 } 606 607 /* try to match on this rule */ 608 mlp = mlist; 609 rvp = rwr->r_lhs; 610 avp = pvp; 611 while ((ap = *avp) != NULL || *rvp != NULL) 612 { 613 if (++loopcount > 100) 614 { 615 syserr("554 Infinite loop in ruleset %d", ruleset); 616 printf("workspace: "); 617 printav(pvp); 618 break; 619 } 620 rp = *rvp; 621 if (tTd(21, 35)) 622 { 623 printf("rp="); 624 xputs(rp); 625 printf(", ap="); 626 xputs(ap); 627 printf("\n"); 628 } 629 if (rp == NULL) 630 { 631 /* end-of-pattern before end-of-address */ 632 goto backup; 633 } 634 if (ap == NULL && (*rp & 0377) != MATCHZANY && 635 (*rp & 0377) != CANONHOST) 636 { 637 /* end-of-input */ 638 break; 639 } 640 641 switch (*rp & 0377) 642 { 643 register STAB *s; 644 char buf[MAXLINE]; 645 646 case MATCHCLASS: 647 case MATCHNCLASS: 648 /* match any token in (not in) a class */ 649 mlp->first = avp; 650 extendclass: 651 if (*avp == NULL) 652 goto backup; 653 mlp->last = avp++; 654 cataddr(mlp->first, mlp->last, buf, sizeof buf, '\0'); 655 s = stab(buf, ST_CLASS, ST_FIND); 656 if (s == NULL || !bitnset(rp[1], s->s_class)) 657 { 658 if ((*rp & 0377) == MATCHCLASS) 659 goto extendclass; 660 } 661 else if ((*rp & 0377) == MATCHNCLASS) 662 goto extendclass; 663 mlp++; 664 break; 665 666 case MATCHONE: 667 case MATCHANY: 668 /* match exactly one token */ 669 mlp->first = avp; 670 mlp->last = avp++; 671 mlp++; 672 break; 673 674 case MATCHZANY: 675 /* match zero or more tokens */ 676 mlp->first = avp; 677 mlp->last = avp - 1; 678 mlp++; 679 break; 680 681 case CANONHOST: 682 /* match zero tokens */ 683 break; 684 685 default: 686 /* must have exact match */ 687 if (strcasecmp(rp, ap)) 688 goto backup; 689 avp++; 690 break; 691 } 692 693 /* successful match on this token */ 694 rvp++; 695 continue; 696 697 backup: 698 /* match failed -- back up */ 699 while (--rvp >= rwr->r_lhs) 700 { 701 rp = *rvp; 702 if ((*rp & 0377) == MATCHANY || 703 (*rp & 0377) == MATCHZANY) 704 { 705 /* extend binding and continue */ 706 avp = ++mlp[-1].last; 707 avp++; 708 rvp++; 709 break; 710 } 711 if ((*rp & 0377) == MATCHCLASS || 712 (*rp & 0377) == MATCHNCLASS) 713 { 714 /* extend binding and try again */ 715 mlp--; 716 avp = ++mlp->last; 717 goto extendclass; 718 } 719 avp--; 720 if ((*rp & 0377) == MATCHONE) 721 { 722 /* back out binding */ 723 mlp--; 724 } 725 } 726 727 if (rvp < rwr->r_lhs) 728 { 729 /* total failure to match */ 730 break; 731 } 732 } 733 734 /* 735 ** See if we successfully matched 736 */ 737 738 if (rvp < rwr->r_lhs || *rvp != NULL) 739 { 740 if (tTd(21, 10)) 741 printf("----- rule fails\n"); 742 rwr = rwr->r_next; 743 continue; 744 } 745 746 rvp = rwr->r_rhs; 747 if (tTd(21, 12)) 748 { 749 printf("-----rule matches:"); 750 printav(rvp); 751 } 752 753 rp = *rvp; 754 if ((*rp & 0377) == CANONUSER) 755 { 756 rvp++; 757 rwr = rwr->r_next; 758 } 759 else if ((*rp & 0377) == CANONHOST) 760 { 761 rvp++; 762 rwr = NULL; 763 } 764 else if ((*rp & 0377) == CANONNET) 765 rwr = NULL; 766 767 /* substitute */ 768 for (avp = npvp; *rvp != NULL; rvp++) 769 { 770 register struct match *m; 771 register char **pp; 772 773 rp = *rvp; 774 if ((*rp & 0377) == MATCHREPL) 775 { 776 /* substitute from LHS */ 777 m = &mlist[rp[1] - '1']; 778 if (m < mlist || m >= mlp) 779 { 780 syserr("554 rewrite: ruleset %d: replacement $%c out of bounds", 781 ruleset, rp[1]); 782 return; 783 } 784 if (tTd(21, 15)) 785 { 786 printf("$%c:", rp[1]); 787 pp = m->first; 788 while (pp <= m->last) 789 { 790 printf(" %x=\"", *pp); 791 (void) fflush(stdout); 792 printf("%s\"", *pp++); 793 } 794 printf("\n"); 795 } 796 pp = m->first; 797 while (pp <= m->last) 798 { 799 if (avp >= &npvp[MAXATOM]) 800 { 801 syserr("554 rewrite: expansion too long"); 802 return; 803 } 804 *avp++ = *pp++; 805 } 806 } 807 else 808 { 809 /* vanilla replacement */ 810 if (avp >= &npvp[MAXATOM]) 811 { 812 toolong: 813 syserr("554 rewrite: expansion too long"); 814 return; 815 } 816 *avp++ = rp; 817 } 818 } 819 *avp++ = NULL; 820 821 /* 822 ** Check for any hostname/keyword lookups. 823 */ 824 825 for (rvp = npvp; *rvp != NULL; rvp++) 826 { 827 char **hbrvp; 828 char **xpvp; 829 int trsize; 830 char *olddelimchar; 831 char *replac; 832 int endtoken; 833 STAB *map; 834 char *mapname; 835 char **key_rvp; 836 char **arg_rvp; 837 char **default_rvp; 838 char buf[MAXNAME + 1]; 839 char *pvpb1[MAXATOM + 1]; 840 char *argvect[10]; 841 char pvpbuf[PSBUFSIZE]; 842 843 if ((**rvp & 0377) != HOSTBEGIN && 844 (**rvp & 0377) != LOOKUPBEGIN) 845 continue; 846 847 /* 848 ** Got a hostname/keyword lookup. 849 ** 850 ** This could be optimized fairly easily. 851 */ 852 853 hbrvp = rvp; 854 if ((**rvp & 0377) == HOSTBEGIN) 855 { 856 endtoken = HOSTEND; 857 mapname = "host"; 858 } 859 else 860 { 861 endtoken = LOOKUPEND; 862 mapname = *++rvp; 863 } 864 map = stab(mapname, ST_MAP, ST_FIND); 865 if (map == NULL) 866 syserr("554 rewrite: map %s not found", mapname); 867 868 /* extract the match part */ 869 key_rvp = ++rvp; 870 default_rvp = NULL; 871 arg_rvp = argvect; 872 xpvp = NULL; 873 replac = pvpbuf; 874 while (*rvp != NULL && (**rvp & 0377) != endtoken) 875 { 876 int nodetype = **rvp & 0377; 877 878 if (nodetype != CANONHOST && nodetype != CANONUSER) 879 { 880 rvp++; 881 continue; 882 } 883 884 *rvp++ = NULL; 885 886 if (xpvp != NULL) 887 { 888 cataddr(xpvp, NULL, replac, 889 &pvpbuf[sizeof pvpbuf] - replac, 890 '\0'); 891 *++arg_rvp = replac; 892 replac += strlen(replac) + 1; 893 xpvp = NULL; 894 } 895 switch (nodetype) 896 { 897 case CANONHOST: 898 xpvp = rvp; 899 break; 900 901 case CANONUSER: 902 default_rvp = rvp; 903 break; 904 } 905 } 906 if (*rvp != NULL) 907 *rvp++ = NULL; 908 if (xpvp != NULL) 909 { 910 cataddr(xpvp, NULL, replac, 911 &pvpbuf[sizeof pvpbuf] - replac, 912 '\0'); 913 *++arg_rvp = replac; 914 } 915 *++arg_rvp = NULL; 916 917 /* save the remainder of the input string */ 918 trsize = (int) (avp - rvp + 1) * sizeof *rvp; 919 bcopy((char *) rvp, (char *) pvpb1, trsize); 920 921 /* look it up */ 922 cataddr(key_rvp, NULL, buf, sizeof buf, '\0'); 923 argvect[0] = buf; 924 if (map != NULL && bitset(MF_VALID, map->s_map.map_flags)) 925 { 926 int bsize = sizeof buf - 1; 927 928 if (map->s_map.map_app != NULL) 929 bsize -= strlen(map->s_map.map_app); 930 if (tTd(60, 1)) 931 printf("map_lookup(%s, %s) => ", 932 mapname, buf); 933 replac = (*map->s_map.map_class->map_lookup)(&map->s_map, 934 buf, sizeof buf - 1, argvect); 935 if (replac != NULL && map->s_map.map_app != NULL) 936 strcat(replac, map->s_map.map_app); 937 if (tTd(60, 1)) 938 printf("%s\n", replac ? replac : "NOT FOUND"); 939 } 940 else 941 replac = NULL; 942 943 /* if no replacement, use default */ 944 if (replac == NULL && default_rvp != NULL) 945 { 946 char buf2[sizeof buf]; 947 948 /* rewrite the default with % translations */ 949 cataddr(default_rvp, NULL, buf2, sizeof buf2, '\0'); 950 map_rewrite(buf2, sizeof buf2, buf, sizeof buf, 951 argvect); 952 replac = buf; 953 } 954 955 if (replac == NULL) 956 { 957 xpvp = key_rvp; 958 } 959 else 960 { 961 /* scan the new replacement */ 962 xpvp = prescan(replac, '\0', pvpbuf, NULL); 963 if (xpvp == NULL) 964 { 965 /* prescan already printed error */ 966 return; 967 } 968 } 969 970 /* append it to the token list */ 971 for (avp = hbrvp; *xpvp != NULL; xpvp++) 972 { 973 *avp++ = newstr(*xpvp); 974 if (avp >= &npvp[MAXATOM]) 975 goto toolong; 976 } 977 978 /* restore the old trailing information */ 979 for (xpvp = pvpb1; (*avp++ = *xpvp++) != NULL; ) 980 if (avp >= &npvp[MAXATOM]) 981 goto toolong; 982 983 break; 984 } 985 986 /* 987 ** Check for subroutine calls. 988 */ 989 990 if (*npvp != NULL && (**npvp & 0377) == CALLSUBR) 991 { 992 bcopy((char *) &npvp[2], (char *) pvp, 993 (int) (avp - npvp - 2) * sizeof *avp); 994 if (tTd(21, 3)) 995 printf("-----callsubr %s\n", npvp[1]); 996 rewrite(pvp, atoi(npvp[1])); 997 } 998 else 999 { 1000 bcopy((char *) npvp, (char *) pvp, 1001 (int) (avp - npvp) * sizeof *avp); 1002 } 1003 if (tTd(21, 4)) 1004 { 1005 printf("rewritten as:"); 1006 printav(pvp); 1007 } 1008 } 1009 1010 if (OpMode == MD_TEST || tTd(21, 2)) 1011 { 1012 printf("rewrite: ruleset %2d returns:", ruleset); 1013 printav(pvp); 1014 } 1015 } 1016 /* 1017 ** BUILDADDR -- build address from token vector. 1018 ** 1019 ** Parameters: 1020 ** tv -- token vector. 1021 ** a -- pointer to address descriptor to fill. 1022 ** If NULL, one will be allocated. 1023 ** 1024 ** Returns: 1025 ** NULL if there was an error. 1026 ** 'a' otherwise. 1027 ** 1028 ** Side Effects: 1029 ** fills in 'a' 1030 */ 1031 1032 struct errcodes 1033 { 1034 char *ec_name; /* name of error code */ 1035 int ec_code; /* numeric code */ 1036 } ErrorCodes[] = 1037 { 1038 "usage", EX_USAGE, 1039 "nouser", EX_NOUSER, 1040 "nohost", EX_NOHOST, 1041 "unavailable", EX_UNAVAILABLE, 1042 "software", EX_SOFTWARE, 1043 "tempfail", EX_TEMPFAIL, 1044 "protocol", EX_PROTOCOL, 1045 #ifdef EX_CONFIG 1046 "config", EX_CONFIG, 1047 #endif 1048 NULL, EX_UNAVAILABLE, 1049 }; 1050 1051 ADDRESS * 1052 buildaddr(tv, a) 1053 register char **tv; 1054 register ADDRESS *a; 1055 { 1056 struct mailer **mp; 1057 register struct mailer *m; 1058 char *bp; 1059 int spaceleft; 1060 static char buf[MAXNAME]; 1061 1062 if (a == NULL) 1063 a = (ADDRESS *) xalloc(sizeof *a); 1064 bzero((char *) a, sizeof *a); 1065 1066 /* figure out what net/mailer to use */ 1067 if ((**tv & 0377) != CANONNET) 1068 { 1069 syserr("554 buildaddr: no net"); 1070 return (NULL); 1071 } 1072 tv++; 1073 if (strcasecmp(*tv, "error") == 0) 1074 { 1075 if ((**++tv & 0377) == CANONHOST) 1076 { 1077 register struct errcodes *ep; 1078 1079 if (isascii(**++tv) && isdigit(**tv)) 1080 { 1081 setstat(atoi(*tv)); 1082 } 1083 else 1084 { 1085 for (ep = ErrorCodes; ep->ec_name != NULL; ep++) 1086 if (strcasecmp(ep->ec_name, *tv) == 0) 1087 break; 1088 setstat(ep->ec_code); 1089 } 1090 tv++; 1091 } 1092 if ((**tv & 0377) != CANONUSER) 1093 syserr("554 buildaddr: error: no user"); 1094 cataddr(++tv, NULL, buf, sizeof buf, ' '); 1095 stripquotes(buf); 1096 usrerr(buf); 1097 return (NULL); 1098 } 1099 1100 for (mp = Mailer; (m = *mp++) != NULL; ) 1101 { 1102 if (strcasecmp(m->m_name, *tv) == 0) 1103 break; 1104 } 1105 if (m == NULL) 1106 { 1107 syserr("554 buildaddr: unknown mailer %s", *tv); 1108 return (NULL); 1109 } 1110 a->q_mailer = m; 1111 1112 /* figure out what host (if any) */ 1113 tv++; 1114 if ((**tv & 0377) == CANONHOST) 1115 { 1116 bp = buf; 1117 spaceleft = sizeof buf - 1; 1118 while (*++tv != NULL && (**tv & 0377) != CANONUSER) 1119 { 1120 int i = strlen(*tv); 1121 1122 if (i > spaceleft) 1123 { 1124 /* out of space for this address */ 1125 if (spaceleft >= 0) 1126 syserr("554 buildaddr: host too long (%.40s...)", 1127 buf); 1128 i = spaceleft; 1129 spaceleft = 0; 1130 } 1131 if (i <= 0) 1132 continue; 1133 bcopy(*tv, bp, i); 1134 bp += i; 1135 spaceleft -= i; 1136 } 1137 *bp = '\0'; 1138 a->q_host = newstr(buf); 1139 } 1140 else 1141 { 1142 if (!bitnset(M_LOCALMAILER, m->m_flags)) 1143 { 1144 syserr("554 buildaddr: no host"); 1145 return (NULL); 1146 } 1147 a->q_host = NULL; 1148 } 1149 1150 /* figure out the user */ 1151 if (*tv == NULL || (**tv & 0377) != CANONUSER) 1152 { 1153 syserr("554 buildaddr: no user"); 1154 return (NULL); 1155 } 1156 tv++; 1157 1158 /* do special mapping for local mailer */ 1159 if (m == LocalMailer && *tv != NULL) 1160 { 1161 register char *p = *tv; 1162 1163 if (*p == '"') 1164 p++; 1165 if (*p == '|') 1166 a->q_mailer = m = ProgMailer; 1167 else if (*p == '/') 1168 a->q_mailer = m = FileMailer; 1169 else if (*p == ':') 1170 { 1171 /* may be :include: */ 1172 cataddr(tv, NULL, buf, sizeof buf, '\0'); 1173 stripquotes(buf); 1174 if (strncasecmp(buf, ":include:", 9) == 0) 1175 { 1176 /* if :include:, don't need further rewriting */ 1177 a->q_mailer = m = InclMailer; 1178 a->q_user = &buf[9]; 1179 return (a); 1180 } 1181 } 1182 } 1183 1184 if (m == LocalMailer && *tv != NULL && strcmp(*tv, "@") == 0) 1185 { 1186 tv++; 1187 a->q_flags |= QNOTREMOTE; 1188 } 1189 1190 /* do cleanup of final address */ 1191 rewrite(tv, 4); 1192 1193 /* save the result for the command line/RCPT argument */ 1194 cataddr(tv, NULL, buf, sizeof buf, '\0'); 1195 a->q_user = buf; 1196 1197 /* 1198 ** Do mapping to lower case as requested by mailer 1199 */ 1200 1201 if (a->q_host != NULL && !bitnset(M_HST_UPPER, m->m_flags)) 1202 makelower(a->q_host); 1203 if (!bitnset(M_USR_UPPER, m->m_flags)) 1204 makelower(a->q_user); 1205 1206 return (a); 1207 } 1208 /* 1209 ** CATADDR -- concatenate pieces of addresses (putting in <LWSP> subs) 1210 ** 1211 ** Parameters: 1212 ** pvp -- parameter vector to rebuild. 1213 ** evp -- last parameter to include. Can be NULL to 1214 ** use entire pvp. 1215 ** buf -- buffer to build the string into. 1216 ** sz -- size of buf. 1217 ** spacesub -- the space separator character; if null, 1218 ** use SpaceSub. 1219 ** 1220 ** Returns: 1221 ** none. 1222 ** 1223 ** Side Effects: 1224 ** Destroys buf. 1225 */ 1226 1227 cataddr(pvp, evp, buf, sz, spacesub) 1228 char **pvp; 1229 char **evp; 1230 char *buf; 1231 register int sz; 1232 char spacesub; 1233 { 1234 bool oatomtok = FALSE; 1235 bool natomtok = FALSE; 1236 register int i; 1237 register char *p; 1238 1239 if (spacesub == '\0') 1240 spacesub = SpaceSub; 1241 1242 if (pvp == NULL) 1243 { 1244 (void) strcpy(buf, ""); 1245 return; 1246 } 1247 p = buf; 1248 sz -= 2; 1249 while (*pvp != NULL && (i = strlen(*pvp)) < sz) 1250 { 1251 natomtok = (toktype(**pvp) == ATM); 1252 if (oatomtok && natomtok) 1253 *p++ = spacesub; 1254 (void) strcpy(p, *pvp); 1255 oatomtok = natomtok; 1256 p += i; 1257 sz -= i + 1; 1258 if (pvp++ == evp) 1259 break; 1260 } 1261 *p = '\0'; 1262 } 1263 /* 1264 ** SAMEADDR -- Determine if two addresses are the same 1265 ** 1266 ** This is not just a straight comparison -- if the mailer doesn't 1267 ** care about the host we just ignore it, etc. 1268 ** 1269 ** Parameters: 1270 ** a, b -- pointers to the internal forms to compare. 1271 ** 1272 ** Returns: 1273 ** TRUE -- they represent the same mailbox. 1274 ** FALSE -- they don't. 1275 ** 1276 ** Side Effects: 1277 ** none. 1278 */ 1279 1280 bool 1281 sameaddr(a, b) 1282 register ADDRESS *a; 1283 register ADDRESS *b; 1284 { 1285 /* if they don't have the same mailer, forget it */ 1286 if (a->q_mailer != b->q_mailer) 1287 return (FALSE); 1288 1289 /* if the user isn't the same, we can drop out */ 1290 if (strcmp(a->q_user, b->q_user) != 0) 1291 return (FALSE); 1292 1293 /* if we have good uids for both but the differ, these are different */ 1294 if (bitset(QGOODUID, a->q_flags & b->q_flags) && a->q_uid != b->q_uid) 1295 return (FALSE); 1296 1297 /* otherwise compare hosts (but be careful for NULL ptrs) */ 1298 if (a->q_host == b->q_host) 1299 { 1300 /* probably both null pointers */ 1301 return (TRUE); 1302 } 1303 if (a->q_host == NULL || b->q_host == NULL) 1304 { 1305 /* only one is a null pointer */ 1306 return (FALSE); 1307 } 1308 if (strcmp(a->q_host, b->q_host) != 0) 1309 return (FALSE); 1310 1311 return (TRUE); 1312 } 1313 /* 1314 ** PRINTADDR -- print address (for debugging) 1315 ** 1316 ** Parameters: 1317 ** a -- the address to print 1318 ** follow -- follow the q_next chain. 1319 ** 1320 ** Returns: 1321 ** none. 1322 ** 1323 ** Side Effects: 1324 ** none. 1325 */ 1326 1327 printaddr(a, follow) 1328 register ADDRESS *a; 1329 bool follow; 1330 { 1331 bool first = TRUE; 1332 register MAILER *m; 1333 MAILER pseudomailer; 1334 1335 while (a != NULL) 1336 { 1337 first = FALSE; 1338 printf("%x=", a); 1339 (void) fflush(stdout); 1340 1341 /* find the mailer -- carefully */ 1342 m = a->q_mailer; 1343 if (m == NULL) 1344 { 1345 m = &pseudomailer; 1346 m->m_mno = -1; 1347 m->m_name = "NULL"; 1348 } 1349 1350 printf("%s:\n\tmailer %d (%s), host `%s', user `%s', ruser `%s'\n", 1351 a->q_paddr, m->m_mno, m->m_name, 1352 a->q_host, a->q_user, a->q_ruser? a->q_ruser: "<null>"); 1353 printf("\tnext=%x, flags=%o, alias %x\n", a->q_next, a->q_flags, 1354 a->q_alias); 1355 printf("\thome=\"%s\", fullname=\"%s\"\n", a->q_home, 1356 a->q_fullname); 1357 1358 if (!follow) 1359 return; 1360 a = a->q_next; 1361 } 1362 if (first) 1363 printf("[NULL]\n"); 1364 } 1365 1366 /* 1367 ** REMOTENAME -- return the name relative to the current mailer 1368 ** 1369 ** Parameters: 1370 ** name -- the name to translate. 1371 ** m -- the mailer that we want to do rewriting relative 1372 ** to. 1373 ** senderaddress -- if set, uses the sender rewriting rules 1374 ** rather than the recipient rewriting rules. 1375 ** header -- set if this address is in the header, rather 1376 ** than an envelope header. 1377 ** canonical -- if set, strip out any comment information, 1378 ** etc. 1379 ** adddomain -- if set, OK to do domain extension. 1380 ** e -- the current envelope. 1381 ** 1382 ** Returns: 1383 ** the text string representing this address relative to 1384 ** the receiving mailer. 1385 ** 1386 ** Side Effects: 1387 ** none. 1388 ** 1389 ** Warnings: 1390 ** The text string returned is tucked away locally; 1391 ** copy it if you intend to save it. 1392 */ 1393 1394 char * 1395 remotename(name, m, senderaddress, header, canonical, adddomain, e) 1396 char *name; 1397 struct mailer *m; 1398 bool senderaddress; 1399 bool header; 1400 bool canonical; 1401 bool adddomain; 1402 register ENVELOPE *e; 1403 { 1404 register char **pvp; 1405 char *fancy; 1406 extern char *macvalue(); 1407 char *oldg = macvalue('g', e); 1408 int rwset; 1409 static char buf[MAXNAME]; 1410 char lbuf[MAXNAME]; 1411 char pvpbuf[PSBUFSIZE]; 1412 extern char **prescan(); 1413 extern char *crackaddr(); 1414 1415 if (tTd(12, 1)) 1416 printf("remotename(%s)\n", name); 1417 1418 /* don't do anything if we are tagging it as special */ 1419 if (senderaddress) 1420 rwset = header ? m->m_sh_rwset : m->m_se_rwset; 1421 else 1422 rwset = header ? m->m_rh_rwset : m->m_re_rwset; 1423 if (rwset < 0) 1424 return (name); 1425 1426 /* 1427 ** Do a heuristic crack of this name to extract any comment info. 1428 ** This will leave the name as a comment and a $g macro. 1429 */ 1430 1431 if (canonical || bitnset(M_NOCOMMENT, m->m_flags)) 1432 fancy = "\201g"; 1433 else 1434 fancy = crackaddr(name); 1435 1436 /* 1437 ** Turn the name into canonical form. 1438 ** Normally this will be RFC 822 style, i.e., "user@domain". 1439 ** If this only resolves to "user", and the "C" flag is 1440 ** specified in the sending mailer, then the sender's 1441 ** domain will be appended. 1442 */ 1443 1444 pvp = prescan(name, '\0', pvpbuf, NULL); 1445 if (pvp == NULL) 1446 return (name); 1447 rewrite(pvp, 3); 1448 if (adddomain && e->e_fromdomain != NULL) 1449 { 1450 /* append from domain to this address */ 1451 register char **pxp = pvp; 1452 1453 /* see if there is an "@domain" in the current name */ 1454 while (*pxp != NULL && strcmp(*pxp, "@") != 0) 1455 pxp++; 1456 if (*pxp == NULL) 1457 { 1458 /* no.... append the "@domain" from the sender */ 1459 register char **qxq = e->e_fromdomain; 1460 1461 while ((*pxp++ = *qxq++) != NULL) 1462 continue; 1463 rewrite(pvp, 3); 1464 } 1465 } 1466 1467 /* 1468 ** Do more specific rewriting. 1469 ** Rewrite using ruleset 1 or 2 depending on whether this is 1470 ** a sender address or not. 1471 ** Then run it through any receiving-mailer-specific rulesets. 1472 */ 1473 1474 if (senderaddress) 1475 rewrite(pvp, 1); 1476 else 1477 rewrite(pvp, 2); 1478 if (rwset > 0) 1479 rewrite(pvp, rwset); 1480 1481 /* 1482 ** Do any final sanitation the address may require. 1483 ** This will normally be used to turn internal forms 1484 ** (e.g., user@host.LOCAL) into external form. This 1485 ** may be used as a default to the above rules. 1486 */ 1487 1488 rewrite(pvp, 4); 1489 1490 /* 1491 ** Now restore the comment information we had at the beginning. 1492 */ 1493 1494 cataddr(pvp, lbuf, sizeof lbuf, '\0'); 1495 define('g', lbuf, e); 1496 expand(fancy, buf, &buf[sizeof buf - 1], e); 1497 define('g', oldg, e); 1498 1499 if (tTd(12, 1)) 1500 printf("remotename => `%s'\n", buf); 1501 return (buf); 1502 } 1503 /* 1504 ** MAPLOCALUSER -- run local username through ruleset 5 for final redirection 1505 ** 1506 ** Parameters: 1507 ** a -- the address to map (but just the user name part). 1508 ** sendq -- the sendq in which to install any replacement 1509 ** addresses. 1510 ** 1511 ** Returns: 1512 ** none. 1513 */ 1514 1515 maplocaluser(a, sendq, e) 1516 register ADDRESS *a; 1517 ADDRESS **sendq; 1518 ENVELOPE *e; 1519 { 1520 register char **pvp; 1521 register ADDRESS *a1 = NULL; 1522 auto char *delimptr; 1523 char pvpbuf[PSBUFSIZE]; 1524 1525 if (tTd(29, 1)) 1526 { 1527 printf("maplocaluser: "); 1528 printaddr(a, FALSE); 1529 } 1530 pvp = prescan(a->q_user, '\0', pvpbuf, &delimptr); 1531 if (pvp == NULL) 1532 return; 1533 1534 rewrite(pvp, 5); 1535 if (pvp[0] == NULL || (pvp[0][0] & 0377) != CANONNET) 1536 return; 1537 1538 /* if non-null, mailer destination specified -- has it changed? */ 1539 a1 = buildaddr(pvp, NULL); 1540 if (a1 == NULL || sameaddr(a, a1)) 1541 return; 1542 1543 /* mark old address as dead; insert new address */ 1544 a->q_flags |= QDONTSEND; 1545 if (tTd(29, 5)) 1546 { 1547 printf("maplocaluser: QDONTSEND "); 1548 printaddr(a, FALSE); 1549 } 1550 a1->q_alias = a; 1551 allocaddr(a1, 1, NULL, delimptr); 1552 (void) recipient(a1, sendq, e); 1553 } 1554 /* 1555 ** DEQUOTE_INIT -- initialize dequote map 1556 ** 1557 ** This is a no-op. 1558 ** 1559 ** Parameters: 1560 ** map -- the internal map structure. 1561 ** mapname -- the name of the mapl. 1562 ** args -- arguments. 1563 ** 1564 ** Returns: 1565 ** TRUE. 1566 */ 1567 1568 bool 1569 dequote_init(map, mapname, args) 1570 MAP *map; 1571 char *mapname; 1572 char *args; 1573 { 1574 register char *p = args; 1575 1576 for (;;) 1577 { 1578 while (isascii(*p) && isspace(*p)) 1579 p++; 1580 if (*p != '-') 1581 break; 1582 switch (*++p) 1583 { 1584 case 'a': 1585 map->map_app = ++p; 1586 break; 1587 } 1588 while (*p != '\0' && !(isascii(*p) && isspace(*p))) 1589 p++; 1590 if (*p != '\0') 1591 *p = '\0'; 1592 } 1593 if (map->map_app != NULL) 1594 map->map_app = newstr(map->map_app); 1595 1596 return TRUE; 1597 } 1598 /* 1599 ** DEQUOTE_MAP -- unquote an address 1600 ** 1601 ** Parameters: 1602 ** map -- the internal map structure (ignored). 1603 ** buf -- the buffer to dequote. 1604 ** bufsiz -- the size of that buffer. 1605 ** av -- arguments (ignored). 1606 ** 1607 ** Returns: 1608 ** NULL -- if there were no quotes, or if the resulting 1609 ** unquoted buffer would not be acceptable to prescan. 1610 ** else -- The dequoted buffer. 1611 */ 1612 1613 char * 1614 dequote_map(map, buf, bufsiz, av) 1615 MAP *map; 1616 char buf[]; 1617 int bufsiz; 1618 char **av; 1619 { 1620 register char *p; 1621 register char *q; 1622 register char c; 1623 int anglecnt; 1624 int cmntcnt; 1625 int quotecnt; 1626 bool quotemode; 1627 bool bslashmode; 1628 1629 anglecnt = 0; 1630 cmntcnt = 0; 1631 quotecnt = 0; 1632 quotemode = FALSE; 1633 bslashmode = FALSE; 1634 1635 for (p = q = buf; (c = *p++) != '\0'; ) 1636 { 1637 if (bslashmode) 1638 { 1639 bslashmode = FALSE; 1640 *q++ = c; 1641 continue; 1642 } 1643 1644 switch (c) 1645 { 1646 case '\\': 1647 bslashmode = TRUE; 1648 break; 1649 1650 case '(': 1651 cmntcnt++; 1652 break; 1653 1654 case ')': 1655 if (cmntcnt-- <= 0) 1656 return NULL; 1657 break; 1658 } 1659 1660 if (cmntcnt > 0) 1661 { 1662 *q++ = c; 1663 continue; 1664 } 1665 1666 switch (c) 1667 { 1668 case '"': 1669 quotemode = !quotemode; 1670 quotecnt++; 1671 continue; 1672 1673 case '<': 1674 anglecnt++; 1675 break; 1676 1677 case '>': 1678 if (anglecnt-- <= 0) 1679 return NULL; 1680 break; 1681 } 1682 *q++ = c; 1683 } 1684 1685 if (anglecnt != 0 || cmntcnt != 0 || bslashmode || 1686 quotemode || quotecnt <= 0) 1687 return NULL; 1688 *q++ = '\0'; 1689 return buf; 1690 } 1691