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