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.15 (Berkeley) 09/22/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 (!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 bool nolock = FALSE; 350 sigfunc_t oldsigint; 351 352 if (!bitset(MCF_REBUILDABLE, map->map_class->map_cflags)) 353 return; 354 355 /* try to lock the source file */ 356 if ((af = fopen(map->map_file, "r+")) == NULL) 357 { 358 if (errno != EACCES || automatic || 359 (af = fopen(map->map_file, "r")) == NULL) 360 { 361 int saveerr = errno; 362 363 if (tTd(27, 1)) 364 printf("Can't open %s: %s\n", 365 map->map_file, errstring(saveerr)); 366 if (!automatic) 367 message("newaliases: cannot open %s: %s", 368 map->map_file, errstring(saveerr)); 369 errno = 0; 370 return; 371 } 372 nolock = TRUE; 373 message("warning: cannot lock %s: %s", 374 map->map_file, errstring(errno)); 375 } 376 377 /* see if someone else is rebuilding the alias file */ 378 if (!nolock && 379 !lockfile(fileno(af), map->map_file, NULL, LOCK_EX|LOCK_NB)) 380 { 381 /* yes, they are -- wait until done */ 382 message("Alias file %s is already being rebuilt", 383 map->map_file); 384 if (OpMode != MD_INITALIAS) 385 { 386 /* wait for other rebuild to complete */ 387 (void) lockfile(fileno(af), map->map_file, NULL, 388 LOCK_EX); 389 } 390 (void) fclose(af); 391 errno = 0; 392 return; 393 } 394 395 oldsigint = setsignal(SIGINT, SIG_IGN); 396 397 if (map->map_class->map_open(map, O_RDWR)) 398 { 399 #ifdef LOG 400 if (LogLevel > 7) 401 { 402 syslog(LOG_NOTICE, "alias database %s %srebuilt by %s", 403 map->map_file, automatic ? "auto" : "", 404 username()); 405 } 406 #endif /* LOG */ 407 map->map_mflags |= MF_OPEN|MF_WRITABLE; 408 readaliases(map, af, automatic); 409 } 410 else 411 { 412 if (tTd(27, 1)) 413 printf("Can't create database for %s: %s\n", 414 map->map_file, errstring(errno)); 415 if (!automatic) 416 syserr("Cannot create database for alias file %s", 417 map->map_file); 418 } 419 420 /* close the file, thus releasing locks */ 421 fclose(af); 422 423 /* add distinguished entries and close the database */ 424 if (bitset(MF_OPEN, map->map_mflags)) 425 map->map_class->map_close(map); 426 427 /* restore the old signal */ 428 (void) setsignal(SIGINT, oldsigint); 429 } 430 /* 431 ** READALIASES -- read and process the alias file. 432 ** 433 ** This routine implements the part of initaliases that occurs 434 ** when we are not going to use the DBM stuff. 435 ** 436 ** Parameters: 437 ** map -- the alias database descriptor. 438 ** af -- file to read the aliases from. 439 ** automatic -- set if this was an automatic rebuild. 440 ** 441 ** Returns: 442 ** none. 443 ** 444 ** Side Effects: 445 ** Reads aliasfile into the symbol table. 446 ** Optionally, builds the .dir & .pag files. 447 */ 448 449 readaliases(map, af, automatic) 450 register MAP *map; 451 FILE *af; 452 int automatic; 453 { 454 register char *p; 455 char *rhs; 456 bool skipping; 457 long naliases, bytes, longest; 458 ADDRESS al, bl; 459 char line[BUFSIZ]; 460 461 /* 462 ** Read and interpret lines 463 */ 464 465 FileName = map->map_file; 466 LineNumber = 0; 467 naliases = bytes = longest = 0; 468 skipping = FALSE; 469 while (fgets(line, sizeof (line), af) != NULL) 470 { 471 int lhssize, rhssize; 472 473 LineNumber++; 474 p = strchr(line, '\n'); 475 if (p != NULL) 476 *p = '\0'; 477 switch (line[0]) 478 { 479 case '#': 480 case '\0': 481 skipping = FALSE; 482 continue; 483 484 case ' ': 485 case '\t': 486 if (!skipping) 487 syserr("554 Non-continuation line starts with space"); 488 skipping = TRUE; 489 continue; 490 } 491 skipping = FALSE; 492 493 /* 494 ** Process the LHS 495 ** Find the colon separator, and parse the address. 496 ** It should resolve to a local name -- this will 497 ** be checked later (we want to optionally do 498 ** parsing of the RHS first to maximize error 499 ** detection). 500 */ 501 502 for (p = line; *p != '\0' && *p != ':' && *p != '\n'; p++) 503 continue; 504 if (*p++ != ':') 505 { 506 syserr("554 missing colon"); 507 continue; 508 } 509 if (parseaddr(line, &al, RF_COPYALL, ':', NULL, CurEnv) == NULL) 510 { 511 syserr("554 %s... illegal alias name", al.q_paddr); 512 continue; 513 } 514 515 /* 516 ** Process the RHS. 517 ** 'al' is the internal form of the LHS address. 518 ** 'p' points to the text of the RHS. 519 */ 520 521 while (isascii(*p) && isspace(*p)) 522 p++; 523 rhs = p; 524 for (;;) 525 { 526 register char c; 527 register char *nlp; 528 529 nlp = &p[strlen(p)]; 530 if (nlp[-1] == '\n') 531 *--nlp = '\0'; 532 533 if (CheckAliases) 534 { 535 /* do parsing & compression of addresses */ 536 while (*p != '\0') 537 { 538 auto char *delimptr; 539 540 while ((isascii(*p) && isspace(*p)) || 541 *p == ',') 542 p++; 543 if (*p == '\0') 544 break; 545 if (parseaddr(p, &bl, RF_COPYNONE, ',', 546 &delimptr, CurEnv) == NULL) 547 usrerr("553 %s... bad address", p); 548 p = delimptr; 549 } 550 } 551 else 552 { 553 p = nlp; 554 } 555 556 /* see if there should be a continuation line */ 557 c = fgetc(af); 558 if (!feof(af)) 559 (void) ungetc(c, af); 560 if (c != ' ' && c != '\t') 561 break; 562 563 /* read continuation line */ 564 if (fgets(p, sizeof line - (p - line), af) == NULL) 565 break; 566 LineNumber++; 567 568 /* check for line overflow */ 569 if (strchr(p, '\n') == NULL) 570 { 571 usrerr("554 alias too long"); 572 break; 573 } 574 } 575 if (al.q_mailer != LocalMailer) 576 { 577 syserr("554 %s... cannot alias non-local names", 578 al.q_paddr); 579 continue; 580 } 581 582 /* 583 ** Insert alias into symbol table or DBM file 584 */ 585 586 if (!bitnset(M_USR_UPPER, al.q_mailer->m_flags)) 587 makelower(al.q_user); 588 589 lhssize = strlen(al.q_user); 590 rhssize = strlen(rhs); 591 map->map_class->map_store(map, al.q_user, rhs); 592 593 if (al.q_paddr != NULL) 594 free(al.q_paddr); 595 if (al.q_host != NULL) 596 free(al.q_host); 597 if (al.q_user != NULL) 598 free(al.q_user); 599 600 /* statistics */ 601 naliases++; 602 bytes += lhssize + rhssize; 603 if (rhssize > longest) 604 longest = rhssize; 605 } 606 607 CurEnv->e_to = NULL; 608 FileName = NULL; 609 if (Verbose || !automatic) 610 message("%s: %d aliases, longest %d bytes, %d bytes total", 611 map->map_file, naliases, longest, bytes); 612 # ifdef LOG 613 if (LogLevel > 7) 614 syslog(LOG_INFO, "%s: %d aliases, longest %d bytes, %d bytes total", 615 map->map_file, naliases, longest, bytes); 616 # endif /* LOG */ 617 } 618 /* 619 ** FORWARD -- Try to forward mail 620 ** 621 ** This is similar but not identical to aliasing. 622 ** 623 ** Parameters: 624 ** user -- the name of the user who's mail we would like 625 ** to forward to. It must have been verified -- 626 ** i.e., the q_home field must have been filled 627 ** in. 628 ** sendq -- a pointer to the head of the send queue to 629 ** put this user's aliases in. 630 ** 631 ** Returns: 632 ** none. 633 ** 634 ** Side Effects: 635 ** New names are added to send queues. 636 */ 637 638 forward(user, sendq, e) 639 ADDRESS *user; 640 ADDRESS **sendq; 641 register ENVELOPE *e; 642 { 643 char *pp; 644 char *ep; 645 646 if (tTd(27, 1)) 647 printf("forward(%s)\n", user->q_paddr); 648 649 if (user->q_mailer != LocalMailer || bitset(QBADADDR, user->q_flags)) 650 return; 651 if (user->q_home == NULL) 652 { 653 syserr("554 forward: no home"); 654 user->q_home = "/nosuchdirectory"; 655 } 656 657 /* good address -- look for .forward file in home */ 658 define('z', user->q_home, e); 659 define('u', user->q_user, e); 660 define('h', user->q_host, e); 661 if (ForwardPath == NULL) 662 ForwardPath = newstr("\201z/.forward"); 663 664 for (pp = ForwardPath; pp != NULL; pp = ep) 665 { 666 int err; 667 char buf[MAXPATHLEN+1]; 668 669 ep = strchr(pp, ':'); 670 if (ep != NULL) 671 *ep = '\0'; 672 expand(pp, buf, &buf[sizeof buf - 1], e); 673 if (ep != NULL) 674 *ep++ = ':'; 675 if (tTd(27, 3)) 676 printf("forward: trying %s\n", buf); 677 678 err = include(buf, TRUE, user, sendq, e); 679 if (err == 0) 680 break; 681 else if (transienterror(err)) 682 { 683 /* we have to suspend this message */ 684 if (tTd(27, 2)) 685 printf("forward: transient error on %s\n", buf); 686 #ifdef LOG 687 if (LogLevel > 2) 688 syslog(LOG_ERR, "%s: forward %s: transient error: %s", 689 e->e_id, buf, errstring(err)); 690 #endif 691 message("%s: %s: message queued", buf, errstring(err)); 692 user->q_flags |= QQUEUEUP; 693 return; 694 } 695 } 696 } 697