1 /* 2 * Copyright (c) 1983 Eric P. Allman 3 * Copyright (c) 1988, 1993 4 * The Regents of the University of California. All rights reserved. 5 * 6 * %sccs.include.redist.c% 7 */ 8 9 # include "sendmail.h" 10 11 #ifndef lint 12 static char sccsid[] = "@(#)alias.c 8.42 (Berkeley) 03/31/95"; 13 #endif /* not lint */ 14 15 16 MAP *AliasDB[MAXALIASDB + 1]; /* actual database list */ 17 int NAliasDBs; /* number of alias databases */ 18 /* 19 ** ALIAS -- Compute aliases. 20 ** 21 ** Scans the alias file for an alias for the given address. 22 ** If found, it arranges to deliver to the alias list instead. 23 ** Uses libdbm database if -DDBM. 24 ** 25 ** Parameters: 26 ** a -- address to alias. 27 ** sendq -- a pointer to the head of the send queue 28 ** to put the aliases in. 29 ** aliaslevel -- the current alias nesting depth. 30 ** e -- the current envelope. 31 ** 32 ** Returns: 33 ** none 34 ** 35 ** Side Effects: 36 ** Aliases found are expanded. 37 ** 38 ** Deficiencies: 39 ** It should complain about names that are aliased to 40 ** nothing. 41 */ 42 43 void 44 alias(a, sendq, aliaslevel, e) 45 register ADDRESS *a; 46 ADDRESS **sendq; 47 int aliaslevel; 48 register ENVELOPE *e; 49 { 50 register char *p; 51 int naliases; 52 char *owner; 53 auto int stat = EX_OK; 54 char obuf[MAXNAME + 6]; 55 extern char *aliaslookup(); 56 57 if (tTd(27, 1)) 58 printf("alias(%s)\n", a->q_user); 59 60 /* don't realias already aliased names */ 61 if (bitset(QDONTSEND|QBADADDR|QVERIFIED, a->q_flags)) 62 return; 63 64 if (NoAlias) 65 return; 66 67 e->e_to = a->q_paddr; 68 69 /* 70 ** Look up this name. 71 ** 72 ** If the map was unavailable, we will queue this message 73 ** until the map becomes available; otherwise, we could 74 ** bounce messages inappropriately. 75 */ 76 77 p = aliaslookup(a->q_user, &stat, e); 78 if (stat == EX_TEMPFAIL || stat == EX_UNAVAILABLE) 79 { 80 a->q_flags |= QQUEUEUP; 81 if (e->e_message == NULL) 82 e->e_message = "alias database unavailable"; 83 return; 84 } 85 if (p == NULL) 86 return; 87 88 /* 89 ** Match on Alias. 90 ** Deliver to the target list. 91 */ 92 93 if (tTd(27, 1)) 94 printf("%s (%s, %s) aliased to %s\n", 95 a->q_paddr, a->q_host, a->q_user, p); 96 if (bitset(EF_VRFYONLY, e->e_flags)) 97 { 98 a->q_flags |= QVERIFIED; 99 e->e_nrcpts++; 100 return; 101 } 102 message("aliased to %s", p); 103 #ifdef LOG 104 if (LogLevel > 9) 105 syslog(LOG_INFO, "%s: alias %s => %s", 106 e->e_id == NULL ? "NOQUEUE" : e->e_id, 107 a->q_paddr, p); 108 #endif 109 a->q_flags &= ~QSELFREF; 110 if (tTd(27, 5)) 111 { 112 printf("alias: QDONTSEND "); 113 printaddr(a, FALSE); 114 } 115 a->q_flags |= QDONTSEND; 116 naliases = sendtolist(p, a, sendq, aliaslevel + 1, e); 117 if (bitset(QSELFREF, a->q_flags)) 118 a->q_flags &= ~QDONTSEND; 119 120 /* 121 ** Look for owner of alias 122 */ 123 124 (void) strcpy(obuf, "owner-"); 125 if (strncmp(a->q_user, "owner-", 6) == 0) 126 (void) strcat(obuf, "owner"); 127 else 128 (void) strcat(obuf, a->q_user); 129 if (!bitnset(M_USR_UPPER, a->q_mailer->m_flags)) 130 makelower(obuf); 131 owner = aliaslookup(obuf, &stat, e); 132 if (owner == NULL) 133 return; 134 135 /* reflect owner into envelope sender */ 136 if (strpbrk(owner, ",:/|\"") != NULL) 137 owner = obuf; 138 a->q_owner = newstr(owner); 139 140 /* announce delivery to this alias; NORECEIPT bit set later */ 141 if (e->e_xfp != NULL) 142 fprintf(e->e_xfp, "Message delivered to mailing list %s\n", 143 a->q_paddr); 144 e->e_flags |= EF_SENDRECEIPT; 145 a->q_flags |= QREPORT|QEXPLODED; 146 } 147 /* 148 ** ALIASLOOKUP -- look up a name in the alias file. 149 ** 150 ** Parameters: 151 ** name -- the name to look up. 152 ** pstat -- a pointer to a place to put the status. 153 ** e -- the current envelope. 154 ** 155 ** Returns: 156 ** the value of name. 157 ** NULL if unknown. 158 ** 159 ** Side Effects: 160 ** none. 161 ** 162 ** Warnings: 163 ** The return value will be trashed across calls. 164 */ 165 166 char * 167 aliaslookup(name, pstat, e) 168 char *name; 169 int *pstat; 170 ENVELOPE *e; 171 { 172 register int dbno; 173 register MAP *map; 174 register char *p; 175 176 for (dbno = 0; dbno < NAliasDBs; dbno++) 177 { 178 auto int stat; 179 180 map = AliasDB[dbno]; 181 if (!bitset(MF_OPEN, map->map_mflags)) 182 continue; 183 p = (*map->map_class->map_lookup)(map, name, NULL, pstat); 184 if (p != NULL) 185 return p; 186 } 187 return NULL; 188 } 189 /* 190 ** SETALIAS -- set up an alias map 191 ** 192 ** Called when reading configuration file. 193 ** 194 ** Parameters: 195 ** spec -- the alias specification 196 ** 197 ** Returns: 198 ** none. 199 */ 200 201 void 202 setalias(spec) 203 char *spec; 204 { 205 register char *p; 206 register MAP *map; 207 char *class; 208 STAB *s; 209 static bool first_unqual = TRUE; 210 211 if (tTd(27, 8)) 212 printf("setalias(%s)\n", spec); 213 214 for (p = spec; p != NULL; ) 215 { 216 while (isspace(*p)) 217 p++; 218 if (*p == '\0') 219 break; 220 spec = p; 221 222 /* 223 ** Treat simple filename specially -- this is the file name 224 ** for the files implementation, not necessarily in order. 225 */ 226 227 if (spec[0] == '/' && first_unqual) 228 { 229 s = stab("aliases.files", ST_MAP, ST_ENTER); 230 map = &s->s_map; 231 first_unqual = FALSE; 232 } 233 else 234 { 235 char aname[50]; 236 237 if (NAliasDBs >= MAXALIASDB) 238 { 239 syserr("Too many alias databases defined, %d max", 240 MAXALIASDB); 241 return; 242 } 243 (void) sprintf(aname, "Alias%d", NAliasDBs); 244 s = stab(aname, ST_MAP, ST_ENTER); 245 map = &s->s_map; 246 AliasDB[NAliasDBs] = map; 247 } 248 bzero(map, sizeof *map); 249 map->map_mname = s->s_name; 250 251 p = strpbrk(p, " ,/:"); 252 if (p != NULL && *p == ':') 253 { 254 /* map name */ 255 *p++ = '\0'; 256 class = spec; 257 spec = p; 258 } 259 else 260 { 261 class = "implicit"; 262 map->map_mflags = MF_OPTIONAL|MF_INCLNULL; 263 } 264 265 /* find end of spec */ 266 if (p != NULL) 267 p = strchr(p, ','); 268 if (p != NULL) 269 *p++ = '\0'; 270 271 if (tTd(27, 20)) 272 printf(" map %s:%s %s\n", class, s->s_name, spec); 273 274 /* look up class */ 275 s = stab(class, ST_MAPCLASS, ST_FIND); 276 if (s == NULL) 277 { 278 if (tTd(27, 1)) 279 printf("Unknown alias class %s\n", class); 280 } 281 else if (!bitset(MCF_ALIASOK, s->s_mapclass.map_cflags)) 282 { 283 syserr("setalias: map class %s can't handle aliases", 284 class); 285 } 286 else 287 { 288 map->map_class = &s->s_mapclass; 289 if (map->map_class->map_parse(map, spec)) 290 { 291 map->map_mflags |= MF_VALID|MF_ALIAS; 292 if (AliasDB[NAliasDBs] == map) 293 NAliasDBs++; 294 } 295 } 296 } 297 } 298 /* 299 ** ALIASWAIT -- wait for distinguished @:@ token to appear. 300 ** 301 ** This can decide to reopen or rebuild the alias file 302 ** 303 ** Parameters: 304 ** map -- a pointer to the map descriptor for this alias file. 305 ** ext -- the filename extension (e.g., ".db") for the 306 ** database file. 307 ** isopen -- if set, the database is already open, and we 308 ** should check for validity; otherwise, we are 309 ** just checking to see if it should be created. 310 ** 311 ** Returns: 312 ** TRUE -- if the database is open when we return. 313 ** FALSE -- if the database is closed when we return. 314 */ 315 316 bool 317 aliaswait(map, ext, isopen) 318 MAP *map; 319 char *ext; 320 int isopen; 321 { 322 bool attimeout = FALSE; 323 time_t mtime; 324 struct stat stb; 325 char buf[MAXNAME + 1]; 326 327 if (tTd(27, 3)) 328 printf("aliaswait(%s:%s)\n", 329 map->map_class->map_cname, map->map_file); 330 if (bitset(MF_ALIASWAIT, map->map_mflags)) 331 return isopen; 332 map->map_mflags |= MF_ALIASWAIT; 333 334 if (SafeAlias > 0) 335 { 336 auto int st; 337 time_t toolong = curtime() + SafeAlias; 338 unsigned int sleeptime = 2; 339 340 while (isopen && 341 map->map_class->map_lookup(map, "@", NULL, &st) == NULL) 342 { 343 if (curtime() > toolong) 344 { 345 /* we timed out */ 346 attimeout = TRUE; 347 break; 348 } 349 350 /* 351 ** Close and re-open the alias database in case 352 ** the one is mv'ed instead of cp'ed in. 353 */ 354 355 if (tTd(27, 2)) 356 printf("aliaswait: sleeping for %d seconds\n", 357 sleeptime); 358 359 map->map_class->map_close(map); 360 sleep(sleeptime); 361 sleeptime *= 2; 362 if (sleeptime > 60) 363 sleeptime = 60; 364 isopen = map->map_class->map_open(map, O_RDONLY); 365 } 366 } 367 368 /* see if we need to go into auto-rebuild mode */ 369 if (!bitset(MCF_REBUILDABLE, map->map_class->map_cflags)) 370 { 371 if (tTd(27, 3)) 372 printf("aliaswait: not rebuildable\n"); 373 map->map_mflags &= ~MF_ALIASWAIT; 374 return isopen; 375 } 376 if (stat(map->map_file, &stb) < 0) 377 { 378 if (tTd(27, 3)) 379 printf("aliaswait: no source file\n"); 380 map->map_mflags &= ~MF_ALIASWAIT; 381 return isopen; 382 } 383 mtime = stb.st_mtime; 384 (void) strcpy(buf, map->map_file); 385 if (ext != NULL) 386 (void) strcat(buf, ext); 387 if (stat(buf, &stb) < 0 || stb.st_mtime < mtime || attimeout) 388 { 389 /* database is out of date */ 390 if (AutoRebuild && stb.st_ino != 0 && stb.st_uid == geteuid()) 391 { 392 bool oldSuprErrs; 393 394 message("auto-rebuilding alias database %s", buf); 395 oldSuprErrs = SuprErrs; 396 SuprErrs = TRUE; 397 if (isopen) 398 map->map_class->map_close(map); 399 rebuildaliases(map, TRUE); 400 isopen = map->map_class->map_open(map, O_RDONLY); 401 SuprErrs = oldSuprErrs; 402 } 403 else 404 { 405 #ifdef LOG 406 if (LogLevel > 3) 407 syslog(LOG_INFO, "alias database %s out of date", 408 buf); 409 #endif /* LOG */ 410 message("Warning: alias database %s out of date", buf); 411 } 412 } 413 map->map_mflags &= ~MF_ALIASWAIT; 414 return isopen; 415 } 416 /* 417 ** REBUILDALIASES -- rebuild the alias database. 418 ** 419 ** Parameters: 420 ** map -- the database to rebuild. 421 ** automatic -- set if this was automatically generated. 422 ** 423 ** Returns: 424 ** none. 425 ** 426 ** Side Effects: 427 ** Reads the text version of the database, builds the 428 ** DBM or DB version. 429 */ 430 431 void 432 rebuildaliases(map, automatic) 433 register MAP *map; 434 bool automatic; 435 { 436 FILE *af; 437 bool nolock = FALSE; 438 sigfunc_t oldsigint, oldsigquit; 439 #ifdef SIGTSTP 440 sigfunc_t oldsigtstp; 441 #endif 442 443 if (!bitset(MCF_REBUILDABLE, map->map_class->map_cflags)) 444 return; 445 446 /* try to lock the source file */ 447 if ((af = fopen(map->map_file, "r+")) == NULL) 448 { 449 if ((errno != EACCES && errno != EROFS) || automatic || 450 (af = fopen(map->map_file, "r")) == NULL) 451 { 452 int saveerr = errno; 453 454 if (tTd(27, 1)) 455 printf("Can't open %s: %s\n", 456 map->map_file, errstring(saveerr)); 457 if (!automatic && !bitset(MF_OPTIONAL, map->map_mflags)) 458 message("newaliases: cannot open %s: %s", 459 map->map_file, errstring(saveerr)); 460 errno = 0; 461 return; 462 } 463 nolock = TRUE; 464 message("warning: cannot lock %s: %s", 465 map->map_file, errstring(errno)); 466 } 467 468 /* see if someone else is rebuilding the alias file */ 469 if (!nolock && 470 !lockfile(fileno(af), map->map_file, NULL, LOCK_EX|LOCK_NB)) 471 { 472 /* yes, they are -- wait until done */ 473 message("Alias file %s is already being rebuilt", 474 map->map_file); 475 if (OpMode != MD_INITALIAS) 476 { 477 /* wait for other rebuild to complete */ 478 (void) lockfile(fileno(af), map->map_file, NULL, 479 LOCK_EX); 480 } 481 (void) xfclose(af, "rebuildaliases1", map->map_file); 482 errno = 0; 483 return; 484 } 485 486 /* avoid denial-of-service attacks */ 487 resetlimits(); 488 oldsigint = setsignal(SIGINT, SIG_IGN); 489 oldsigquit = setsignal(SIGQUIT, SIG_IGN); 490 #ifdef SIGTSTP 491 oldsigtstp = setsignal(SIGTSTP, SIG_IGN); 492 #endif 493 494 if (map->map_class->map_open(map, O_RDWR)) 495 { 496 #ifdef LOG 497 if (LogLevel > 7) 498 { 499 syslog(LOG_NOTICE, "alias database %s %srebuilt by %s", 500 map->map_file, automatic ? "auto" : "", 501 username()); 502 } 503 #endif /* LOG */ 504 map->map_mflags |= MF_OPEN|MF_WRITABLE; 505 readaliases(map, af, !automatic, TRUE); 506 } 507 else 508 { 509 if (tTd(27, 1)) 510 printf("Can't create database for %s: %s\n", 511 map->map_file, errstring(errno)); 512 if (!automatic) 513 syserr("Cannot create database for alias file %s", 514 map->map_file); 515 } 516 517 /* close the file, thus releasing locks */ 518 xfclose(af, "rebuildaliases2", map->map_file); 519 520 /* add distinguished entries and close the database */ 521 if (bitset(MF_OPEN, map->map_mflags)) 522 map->map_class->map_close(map); 523 524 /* restore the old signals */ 525 (void) setsignal(SIGINT, oldsigint); 526 (void) setsignal(SIGQUIT, oldsigquit); 527 #ifdef SIGTSTP 528 (void) setsignal(SIGTSTP, oldsigtstp); 529 #endif 530 } 531 /* 532 ** READALIASES -- read and process the alias file. 533 ** 534 ** This routine implements the part of initaliases that occurs 535 ** when we are not going to use the DBM stuff. 536 ** 537 ** Parameters: 538 ** map -- the alias database descriptor. 539 ** af -- file to read the aliases from. 540 ** announcestats -- anounce statistics regarding number of 541 ** aliases, longest alias, etc. 542 ** logstats -- lot the same info. 543 ** 544 ** Returns: 545 ** none. 546 ** 547 ** Side Effects: 548 ** Reads aliasfile into the symbol table. 549 ** Optionally, builds the .dir & .pag files. 550 */ 551 552 void 553 readaliases(map, af, announcestats, logstats) 554 register MAP *map; 555 FILE *af; 556 bool announcestats; 557 bool logstats; 558 { 559 register char *p; 560 char *rhs; 561 bool skipping; 562 long naliases, bytes, longest; 563 ADDRESS al, bl; 564 char line[BUFSIZ]; 565 566 /* 567 ** Read and interpret lines 568 */ 569 570 FileName = map->map_file; 571 LineNumber = 0; 572 naliases = bytes = longest = 0; 573 skipping = FALSE; 574 while (fgets(line, sizeof (line), af) != NULL) 575 { 576 int lhssize, rhssize; 577 578 LineNumber++; 579 p = strchr(line, '\n'); 580 if (p != NULL) 581 *p = '\0'; 582 switch (line[0]) 583 { 584 case '#': 585 case '\0': 586 skipping = FALSE; 587 continue; 588 589 case ' ': 590 case '\t': 591 if (!skipping) 592 syserr("554 Non-continuation line starts with space"); 593 skipping = TRUE; 594 continue; 595 } 596 skipping = FALSE; 597 598 /* 599 ** Process the LHS 600 ** Find the colon separator, and parse the address. 601 ** It should resolve to a local name -- this will 602 ** be checked later (we want to optionally do 603 ** parsing of the RHS first to maximize error 604 ** detection). 605 */ 606 607 for (p = line; *p != '\0' && *p != ':' && *p != '\n'; p++) 608 continue; 609 if (*p++ != ':') 610 { 611 syserr("554 missing colon"); 612 continue; 613 } 614 if (parseaddr(line, &al, RF_COPYALL, ':', NULL, CurEnv) == NULL) 615 { 616 syserr("554 %.40s... illegal alias name", line); 617 continue; 618 } 619 620 /* 621 ** Process the RHS. 622 ** 'al' is the internal form of the LHS address. 623 ** 'p' points to the text of the RHS. 624 */ 625 626 while (isascii(*p) && isspace(*p)) 627 p++; 628 rhs = p; 629 for (;;) 630 { 631 register char c; 632 register char *nlp; 633 634 nlp = &p[strlen(p)]; 635 if (nlp[-1] == '\n') 636 *--nlp = '\0'; 637 638 if (CheckAliases) 639 { 640 /* do parsing & compression of addresses */ 641 while (*p != '\0') 642 { 643 auto char *delimptr; 644 645 while ((isascii(*p) && isspace(*p)) || 646 *p == ',') 647 p++; 648 if (*p == '\0') 649 break; 650 if (parseaddr(p, &bl, RF_COPYNONE, ',', 651 &delimptr, CurEnv) == NULL) 652 usrerr("553 %s... bad address", p); 653 p = delimptr; 654 } 655 } 656 else 657 { 658 p = nlp; 659 } 660 661 /* see if there should be a continuation line */ 662 c = fgetc(af); 663 if (!feof(af)) 664 (void) ungetc(c, af); 665 if (c != ' ' && c != '\t') 666 break; 667 668 /* read continuation line */ 669 if (fgets(p, sizeof line - (p - line), af) == NULL) 670 break; 671 LineNumber++; 672 673 /* check for line overflow */ 674 if (strchr(p, '\n') == NULL) 675 { 676 usrerr("554 alias too long"); 677 break; 678 } 679 } 680 if (!bitnset(M_ALIASABLE, al.q_mailer->m_flags)) 681 { 682 syserr("554 %s... cannot alias non-local names", 683 al.q_paddr); 684 continue; 685 } 686 687 /* 688 ** Insert alias into symbol table or DBM file 689 */ 690 691 if (!bitnset(M_USR_UPPER, al.q_mailer->m_flags)) 692 makelower(al.q_user); 693 694 lhssize = strlen(al.q_user); 695 rhssize = strlen(rhs); 696 map->map_class->map_store(map, al.q_user, rhs); 697 698 if (al.q_paddr != NULL) 699 free(al.q_paddr); 700 if (al.q_host != NULL) 701 free(al.q_host); 702 if (al.q_user != NULL) 703 free(al.q_user); 704 705 /* statistics */ 706 naliases++; 707 bytes += lhssize + rhssize; 708 if (rhssize > longest) 709 longest = rhssize; 710 } 711 712 CurEnv->e_to = NULL; 713 FileName = NULL; 714 if (Verbose || announcestats) 715 message("%s: %d aliases, longest %d bytes, %d bytes total", 716 map->map_file, naliases, longest, bytes); 717 # ifdef LOG 718 if (LogLevel > 7 && logstats) 719 syslog(LOG_INFO, "%s: %d aliases, longest %d bytes, %d bytes total", 720 map->map_file, naliases, longest, bytes); 721 # endif /* LOG */ 722 } 723 /* 724 ** FORWARD -- Try to forward mail 725 ** 726 ** This is similar but not identical to aliasing. 727 ** 728 ** Parameters: 729 ** user -- the name of the user who's mail we would like 730 ** to forward to. It must have been verified -- 731 ** i.e., the q_home field must have been filled 732 ** in. 733 ** sendq -- a pointer to the head of the send queue to 734 ** put this user's aliases in. 735 ** aliaslevel -- the current alias nesting depth. 736 ** e -- the current envelope. 737 ** 738 ** Returns: 739 ** none. 740 ** 741 ** Side Effects: 742 ** New names are added to send queues. 743 */ 744 745 void 746 forward(user, sendq, aliaslevel, e) 747 ADDRESS *user; 748 ADDRESS **sendq; 749 int aliaslevel; 750 register ENVELOPE *e; 751 { 752 char *pp; 753 char *ep; 754 755 if (tTd(27, 1)) 756 printf("forward(%s)\n", user->q_paddr); 757 758 if (!bitnset(M_HASPWENT, user->q_mailer->m_flags) || 759 bitset(QBADADDR, user->q_flags)) 760 return; 761 if (user->q_home == NULL) 762 { 763 syserr("554 forward: no home"); 764 user->q_home = "/nosuchdirectory"; 765 } 766 767 /* good address -- look for .forward file in home */ 768 define('z', user->q_home, e); 769 define('u', user->q_user, e); 770 define('h', user->q_host, e); 771 if (ForwardPath == NULL) 772 ForwardPath = newstr("\201z/.forward"); 773 774 for (pp = ForwardPath; pp != NULL; pp = ep) 775 { 776 int err; 777 char buf[MAXPATHLEN+1]; 778 extern int include(); 779 780 ep = strchr(pp, ':'); 781 if (ep != NULL) 782 *ep = '\0'; 783 expand(pp, buf, sizeof buf, e); 784 if (ep != NULL) 785 *ep++ = ':'; 786 if (tTd(27, 3)) 787 printf("forward: trying %s\n", buf); 788 789 err = include(buf, TRUE, user, sendq, aliaslevel, e); 790 if (err == 0) 791 break; 792 else if (transienterror(err)) 793 { 794 /* we have to suspend this message */ 795 if (tTd(27, 2)) 796 printf("forward: transient error on %s\n", buf); 797 #ifdef LOG 798 if (LogLevel > 2) 799 syslog(LOG_ERR, "%s: forward %s: transient error: %s", 800 e->e_id == NULL ? "NOQUEUE" : e->e_id, 801 buf, errstring(err)); 802 #endif 803 message("%s: %s: message queued", buf, errstring(err)); 804 user->q_flags |= QQUEUEUP; 805 return; 806 } 807 } 808 } 809