122694Sdist /* 222694Sdist ** Sendmail 322694Sdist ** Copyright (c) 1983 Eric P. Allman 422694Sdist ** Berkeley, California 522694Sdist ** 622694Sdist ** Copyright (c) 1983 Regents of the University of California. 722694Sdist ** All rights reserved. The Berkeley software License Agreement 822694Sdist ** specifies the terms and conditions for redistribution. 922694Sdist */ 1022694Sdist 1122694Sdist #ifndef lint 1222903Smiriam # ifdef DBM 13*25522Seric static char SccsId[] = "@(#)alias.c 5.7 (Berkeley) 11/22/85 (with DBM)"; 1422903Smiriam # else DBM 15*25522Seric static char SccsId[] = "@(#)alias.c 5.7 (Berkeley) 11/22/85 (without DBM)"; 1622903Smiriam # endif DBM 1722694Sdist #endif not lint 1822694Sdist 19292Seric # include <pwd.h> 204212Seric # include <sys/types.h> 214212Seric # include <sys/stat.h> 228437Seric # include <signal.h> 2319784Seric # include <errno.h> 243309Seric # include "sendmail.h" 2519784Seric # ifdef FLOCK 2619784Seric # include <sys/file.h> 2719784Seric # endif FLOCK 28292Seric 29402Seric 30292Seric /* 31292Seric ** ALIAS -- Compute aliases. 32292Seric ** 339368Seric ** Scans the alias file for an alias for the given address. 349368Seric ** If found, it arranges to deliver to the alias list instead. 359368Seric ** Uses libdbm database if -DDBM. 36292Seric ** 37292Seric ** Parameters: 384097Seric ** a -- address to alias. 394999Seric ** sendq -- a pointer to the head of the send queue 404999Seric ** to put the aliases in. 41292Seric ** 42292Seric ** Returns: 43292Seric ** none 44292Seric ** 45292Seric ** Side Effects: 463185Seric ** Aliases found are expanded. 47292Seric ** 48292Seric ** Notes: 49292Seric ** If NoAlias (the "-n" flag) is set, no aliasing is 50292Seric ** done. 51292Seric ** 52292Seric ** Deficiencies: 53292Seric ** It should complain about names that are aliased to 54292Seric ** nothing. 55292Seric */ 56292Seric 57292Seric 581503Smark #ifdef DBM 592966Seric typedef struct 602966Seric { 612966Seric char *dptr; 624157Seric int dsize; 634157Seric } DATUM; 644157Seric extern DATUM fetch(); 651503Smark #endif DBM 66292Seric 674999Seric alias(a, sendq) 684097Seric register ADDRESS *a; 694999Seric ADDRESS **sendq; 70292Seric { 714081Seric register char *p; 725701Seric extern char *aliaslookup(); 73292Seric 74292Seric # ifdef DEBUG 757671Seric if (tTd(27, 1)) 764098Seric printf("alias(%s)\n", a->q_paddr); 77292Seric # endif 78292Seric 794098Seric /* don't realias already aliased names */ 804098Seric if (bitset(QDONTSEND, a->q_flags)) 814098Seric return; 824098Seric 836898Seric CurEnv->e_to = a->q_paddr; 844098Seric 854314Seric /* 864314Seric ** Look up this name 874314Seric */ 884314Seric 8924944Seric if (NoAlias) 9024944Seric p = NULL; 9124944Seric else 9224944Seric p = aliaslookup(a->q_user); 934098Seric if (p == NULL) 944098Seric return; 95292Seric 96292Seric /* 974098Seric ** Match on Alias. 984098Seric ** Deliver to the target list. 991515Seric */ 1001515Seric 1014098Seric # ifdef DEBUG 1027671Seric if (tTd(27, 1)) 1034098Seric printf("%s (%s, %s) aliased to %s\n", 1044098Seric a->q_paddr, a->q_host, a->q_user, p); 1054098Seric # endif 1067051Seric message(Arpa_Info, "aliased to %s", p); 1074098Seric AliasLevel++; 1089614Seric sendtolist(p, a, sendq); 1094098Seric AliasLevel--; 1104098Seric } 1114098Seric /* 1125701Seric ** ALIASLOOKUP -- look up a name in the alias file. 1135701Seric ** 1145701Seric ** Parameters: 1155701Seric ** name -- the name to look up. 1165701Seric ** 1175701Seric ** Returns: 1185701Seric ** the value of name. 1195701Seric ** NULL if unknown. 1205701Seric ** 1215701Seric ** Side Effects: 1225701Seric ** none. 1235701Seric ** 1245701Seric ** Warnings: 1255701Seric ** The return value will be trashed across calls. 1265701Seric */ 1275701Seric 1285701Seric char * 1295701Seric aliaslookup(name) 1305701Seric char *name; 1315701Seric { 1325701Seric # ifdef DBM 1335701Seric DATUM rhs, lhs; 1345701Seric 1355701Seric /* create a key for fetch */ 1365701Seric lhs.dptr = name; 1375701Seric lhs.dsize = strlen(name) + 1; 1385701Seric rhs = fetch(lhs); 1395701Seric return (rhs.dptr); 1405701Seric # else DBM 1415701Seric register STAB *s; 1425701Seric 1435701Seric s = stab(name, ST_ALIAS, ST_FIND); 1445701Seric if (s == NULL) 1455701Seric return (NULL); 1465701Seric return (s->s_alias); 1475701Seric # endif DBM 1485701Seric } 1495701Seric /* 1504098Seric ** INITALIASES -- initialize for aliasing 1514098Seric ** 1524098Seric ** Very different depending on whether we are running DBM or not. 1534098Seric ** 1544098Seric ** Parameters: 1554098Seric ** aliasfile -- location of aliases. 1564157Seric ** init -- if set and if DBM, initialize the DBM files. 1574098Seric ** 1584098Seric ** Returns: 1594098Seric ** none. 1604098Seric ** 1614098Seric ** Side Effects: 1624098Seric ** initializes aliases: 1634098Seric ** if DBM: opens the database. 1644098Seric ** if ~DBM: reads the aliases into the symbol table. 1654098Seric */ 1664098Seric 1674157Seric # define DBMMODE 0666 1684157Seric 1694157Seric initaliases(aliasfile, init) 1704098Seric char *aliasfile; 1714157Seric bool init; 1724098Seric { 1739368Seric #ifdef DBM 1748437Seric int atcnt; 175*25522Seric time_t modtime; 176*25522Seric bool automatic = FALSE; 1774322Seric char buf[MAXNAME]; 1789368Seric #endif DBM 1799368Seric struct stat stb; 1804322Seric 18117984Seric if (aliasfile == NULL || stat(aliasfile, &stb) < 0) 1828437Seric { 183*25522Seric if (aliasfile != NULL && init) 184*25522Seric syserr("Cannot open %s", aliasfile); 1858437Seric NoAlias = TRUE; 18611937Seric errno = 0; 1878437Seric return; 1888437Seric } 1898437Seric 1908928Seric # ifdef DBM 1914322Seric /* 1928437Seric ** Check to see that the alias file is complete. 1938437Seric ** If not, we will assume that someone died, and it is up 1948437Seric ** to us to rebuild it. 1958437Seric */ 1968437Seric 1978437Seric dbminit(aliasfile); 19817471Seric atcnt = SafeAlias * 2; 19917471Seric if (atcnt > 0) 20017471Seric { 20117471Seric while (!init && atcnt-- >= 0 && aliaslookup("@") == NULL) 20217471Seric sleep(30); 20317471Seric } 20417471Seric else 20517471Seric atcnt = 1; 2068437Seric 2078437Seric /* 2084322Seric ** See if the DBM version of the file is out of date with 2094322Seric ** the text version. If so, go into 'init' mode automatically. 2104322Seric ** This only happens if our effective userid owns the DBM 2114325Seric ** version or if the mode of the database is 666 -- this 2124322Seric ** is an attempt to avoid protection problems. Note the 2134322Seric ** unpalatable hack to see if the stat succeeded. 2144322Seric */ 2154322Seric 2164322Seric modtime = stb.st_mtime; 2174322Seric (void) strcpy(buf, aliasfile); 2184322Seric (void) strcat(buf, ".pag"); 2194322Seric stb.st_ino = 0; 22019039Seric if (!init && (stat(buf, &stb) < 0 || stb.st_mtime < modtime || atcnt < 0)) 2214322Seric { 22211937Seric errno = 0; 2239150Seric if (AutoRebuild && stb.st_ino != 0 && 2249368Seric ((stb.st_mode & 0777) == 0666 || stb.st_uid == geteuid())) 2254322Seric { 2264322Seric init = TRUE; 227*25522Seric automatic = TRUE; 2287051Seric message(Arpa_Info, "rebuilding alias database"); 22924944Seric #ifdef LOG 23024944Seric if (LogLevel >= 7) 23124944Seric syslog(LOG_INFO, "rebuilding alias database"); 23224944Seric #endif LOG 2334322Seric } 2344322Seric else 2354322Seric { 23619039Seric #ifdef LOG 23724944Seric if (LogLevel >= 7) 23824944Seric syslog(LOG_INFO, "alias database out of date"); 23919039Seric #endif LOG 2404322Seric message(Arpa_Info, "Warning: alias database out of date"); 2414322Seric } 2424322Seric } 2434322Seric 2444322Seric 2454322Seric /* 2468437Seric ** If necessary, load the DBM file. 2474322Seric ** If running without DBM, load the symbol table. 2484322Seric */ 2494322Seric 2504157Seric if (init) 2518437Seric { 252*25522Seric #ifdef LOG 253*25522Seric if (LogLevel >= 6) 254*25522Seric { 255*25522Seric extern char *username(); 256*25522Seric 257*25522Seric syslog(LOG_NOTICE, "alias database %srebuilt by %s", 258*25522Seric automatic ? "auto" : "", username()); 259*25522Seric } 260*25522Seric #endif LOG 2614157Seric readaliases(aliasfile, TRUE); 2628437Seric } 2634098Seric # else DBM 2644157Seric readaliases(aliasfile, init); 2654157Seric # endif DBM 2664157Seric } 2674157Seric /* 2684157Seric ** READALIASES -- read and process the alias file. 2694157Seric ** 2704157Seric ** This routine implements the part of initaliases that occurs 2714157Seric ** when we are not going to use the DBM stuff. 2724157Seric ** 2734157Seric ** Parameters: 2744157Seric ** aliasfile -- the pathname of the alias file master. 2754157Seric ** init -- if set, initialize the DBM stuff. 2764157Seric ** 2774157Seric ** Returns: 2784157Seric ** none. 2794157Seric ** 2804157Seric ** Side Effects: 2814157Seric ** Reads aliasfile into the symbol table. 2824157Seric ** Optionally, builds the .dir & .pag files. 2834157Seric */ 2844157Seric 2854157Seric static 2864157Seric readaliases(aliasfile, init) 2874157Seric char *aliasfile; 2884157Seric bool init; 2894157Seric { 2904098Seric register char *p; 2914098Seric char *p2; 2924098Seric char *rhs; 2934098Seric bool skipping; 2949368Seric int naliases, bytes, longest; 2959368Seric FILE *af; 29619784Seric int (*oldsigint)(); 2974098Seric ADDRESS al, bl; 2984106Seric register STAB *s; 2999368Seric char line[BUFSIZ]; 3004098Seric 3014098Seric if ((af = fopen(aliasfile, "r")) == NULL) 3021515Seric { 3034098Seric # ifdef DEBUG 3047671Seric if (tTd(27, 1)) 3054106Seric printf("Can't open %s\n", aliasfile); 3064098Seric # endif 3074098Seric errno = 0; 3084098Seric NoAlias++; 3094098Seric return; 3104098Seric } 3114314Seric 31219784Seric # ifdef DBM 31319784Seric # ifdef FLOCK 31419784Seric /* see if someone else is rebuilding the alias file already */ 31519784Seric if (flock(fileno(af), LOCK_EX | LOCK_NB) < 0 && errno == EWOULDBLOCK) 31619784Seric { 31719784Seric /* yes, they are -- wait until done and then return */ 31819784Seric message(Arpa_Info, "Alias file is already being rebuilt"); 31919784Seric if (OpMode != MD_INITALIAS) 32019784Seric { 32119784Seric /* wait for other rebuild to complete */ 32219784Seric (void) flock(fileno(af), LOCK_EX); 32319784Seric } 32423108Seric (void) fclose(af); 32519784Seric errno = 0; 32619784Seric return; 32719784Seric } 32819784Seric # endif FLOCK 32919784Seric # endif DBM 33019784Seric 3314314Seric /* 33219784Seric ** If initializing, create the new DBM files. 33319784Seric */ 33419784Seric 33519784Seric if (init) 33619784Seric { 33719784Seric oldsigint = signal(SIGINT, SIG_IGN); 33819784Seric (void) strcpy(line, aliasfile); 33919784Seric (void) strcat(line, ".dir"); 34019784Seric if (close(creat(line, DBMMODE)) < 0) 34119784Seric { 34219784Seric syserr("cannot make %s", line); 34319784Seric (void) signal(SIGINT, oldsigint); 34419784Seric return; 34519784Seric } 34619784Seric (void) strcpy(line, aliasfile); 34719784Seric (void) strcat(line, ".pag"); 34819784Seric if (close(creat(line, DBMMODE)) < 0) 34919784Seric { 35019784Seric syserr("cannot make %s", line); 35119784Seric (void) signal(SIGINT, oldsigint); 35219784Seric return; 35319784Seric } 35419784Seric } 35519784Seric 35619784Seric /* 3574314Seric ** Read and interpret lines 3584314Seric */ 3594314Seric 3609368Seric FileName = aliasfile; 3619368Seric LineNumber = 0; 3624322Seric naliases = bytes = longest = 0; 3634098Seric skipping = FALSE; 3644098Seric while (fgets(line, sizeof (line), af) != NULL) 3654098Seric { 3664322Seric int lhssize, rhssize; 3674322Seric 3689368Seric LineNumber++; 36925278Seric p = index(line, '\n'); 37025278Seric if (p != NULL) 37125278Seric *p = '\0'; 3724098Seric switch (line[0]) 3734098Seric { 3744098Seric case '#': 3754098Seric case '\0': 3764098Seric skipping = FALSE; 3774098Seric continue; 3784065Seric 3794098Seric case ' ': 3804098Seric case '\t': 3814098Seric if (!skipping) 3829368Seric syserr("Non-continuation line starts with space"); 3834098Seric skipping = TRUE; 3844097Seric continue; 3854098Seric } 3864098Seric skipping = FALSE; 3871874Seric 3884314Seric /* 3894314Seric ** Process the LHS 3904314Seric ** Find the final colon, and parse the address. 39116898Seric ** It should resolve to a local name -- this will 39216898Seric ** be checked later (we want to optionally do 39316898Seric ** parsing of the RHS first to maximize error 39416898Seric ** detection). 3954314Seric */ 3964314Seric 3974098Seric for (p = line; *p != '\0' && *p != ':' && *p != '\n'; p++) 3984097Seric continue; 39916898Seric if (*p++ != ':') 4004098Seric { 4019368Seric syserr("missing colon"); 4024097Seric continue; 4034098Seric } 40416898Seric if (parseaddr(line, &al, 1, ':') == NULL) 4054098Seric { 40616898Seric syserr("illegal alias name"); 40716898Seric continue; 4084098Seric } 40916898Seric loweraddr(&al); 4104314Seric 4114314Seric /* 4124314Seric ** Process the RHS. 4134314Seric ** 'al' is the internal form of the LHS address. 4144314Seric ** 'p' points to the text of the RHS. 4154314Seric */ 4164314Seric 4174098Seric rhs = p; 4184098Seric for (;;) 4194098Seric { 4204098Seric register char c; 4211515Seric 4224157Seric if (init) 4234098Seric { 4244157Seric /* do parsing & compression of addresses */ 42525278Seric while (*p != '\0') 4264098Seric { 42725278Seric extern char *DelimChar; 42825278Seric 42925278Seric while (isspace(*p) || *p == ',') 4304157Seric p++; 43125278Seric if (*p == '\0') 43225278Seric break; 43325278Seric if (parseaddr(p, &bl, -1, ',') == NULL) 43425278Seric usrerr("%s... bad address", p); 43525278Seric p = DelimChar; 4364098Seric } 4374098Seric } 4384157Seric else 43915769Seric { 44016898Seric p = &p[strlen(p)]; 44116898Seric if (p[-1] == '\n') 44216898Seric *--p = '\0'; 44315769Seric } 4441515Seric 4454098Seric /* see if there should be a continuation line */ 4464106Seric c = fgetc(af); 4474106Seric if (!feof(af)) 4484314Seric (void) ungetc(c, af); 4494106Seric if (c != ' ' && c != '\t') 4504098Seric break; 4514098Seric 4524098Seric /* read continuation line */ 4534098Seric if (fgets(p, sizeof line - (p - line), af) == NULL) 4544098Seric break; 4559368Seric LineNumber++; 4564098Seric } 45716898Seric if (al.q_mailer != LocalMailer) 45816898Seric { 45916898Seric syserr("cannot alias non-local names"); 46016898Seric continue; 46116898Seric } 4624314Seric 4634314Seric /* 4644314Seric ** Insert alias into symbol table or DBM file 4654314Seric */ 4664314Seric 46716898Seric lhssize = strlen(al.q_user) + 1; 4684322Seric rhssize = strlen(rhs) + 1; 4694322Seric 4704157Seric # ifdef DBM 4714157Seric if (init) 4724157Seric { 4734157Seric DATUM key, content; 4744157Seric 4754322Seric key.dsize = lhssize; 4764157Seric key.dptr = al.q_user; 4774322Seric content.dsize = rhssize; 4784157Seric content.dptr = rhs; 4794157Seric store(key, content); 4804157Seric } 4814157Seric else 4824157Seric # endif DBM 4834157Seric { 4844157Seric s = stab(al.q_user, ST_ALIAS, ST_ENTER); 4854157Seric s->s_alias = newstr(rhs); 4864157Seric } 4874322Seric 4884322Seric /* statistics */ 4894322Seric naliases++; 4904322Seric bytes += lhssize + rhssize; 4914322Seric if (rhssize > longest) 4924322Seric longest = rhssize; 4931515Seric } 49419784Seric 49519784Seric # ifdef DBM 49619784Seric if (init) 49719784Seric { 49819784Seric /* add the distinquished alias "@" */ 49919784Seric DATUM key; 50019784Seric 50119784Seric key.dsize = 2; 50219784Seric key.dptr = "@"; 50319784Seric store(key, key); 50419784Seric 50519784Seric /* restore the old signal */ 50619784Seric (void) signal(SIGINT, oldsigint); 50719784Seric } 50819784Seric # endif DBM 50919784Seric 51019784Seric /* closing the alias file drops the lock */ 5114098Seric (void) fclose(af); 5126898Seric CurEnv->e_to = NULL; 5139368Seric FileName = NULL; 5147051Seric message(Arpa_Info, "%d aliases, longest %d bytes, %d bytes total", 5154322Seric naliases, longest, bytes); 51624944Seric # ifdef LOG 51724944Seric if (LogLevel >= 8) 51824944Seric syslog(LOG_INFO, "%d aliases, longest %d bytes, %d bytes total", 51924944Seric naliases, longest, bytes); 52024944Seric # endif LOG 521292Seric } 522292Seric /* 523292Seric ** FORWARD -- Try to forward mail 524292Seric ** 525292Seric ** This is similar but not identical to aliasing. 526292Seric ** 527292Seric ** Parameters: 5284314Seric ** user -- the name of the user who's mail we would like 5294314Seric ** to forward to. It must have been verified -- 5304314Seric ** i.e., the q_home field must have been filled 5314314Seric ** in. 5324999Seric ** sendq -- a pointer to the head of the send queue to 5334999Seric ** put this user's aliases in. 534292Seric ** 535292Seric ** Returns: 5364098Seric ** none. 537292Seric ** 538292Seric ** Side Effects: 5393185Seric ** New names are added to send queues. 540292Seric */ 541292Seric 5424999Seric forward(user, sendq) 5432966Seric ADDRESS *user; 5444999Seric ADDRESS **sendq; 545292Seric { 5464078Seric char buf[60]; 5474536Seric extern bool safefile(); 5484069Seric 5494098Seric # ifdef DEBUG 5507671Seric if (tTd(27, 1)) 5514098Seric printf("forward(%s)\n", user->q_paddr); 5524098Seric # endif DEBUG 5534098Seric 5544594Seric if (user->q_mailer != LocalMailer || bitset(QBADADDR, user->q_flags)) 5554098Seric return; 5564314Seric # ifdef DEBUG 5574314Seric if (user->q_home == NULL) 5584314Seric syserr("forward: no home"); 5594314Seric # endif DEBUG 5604069Seric 5614069Seric /* good address -- look for .forward file in home */ 5629368Seric define('z', user->q_home, CurEnv); 56316154Seric expand("\001z/.forward", buf, &buf[sizeof buf - 1], CurEnv); 5644536Seric if (!safefile(buf, user->q_uid, S_IREAD)) 5654098Seric return; 5664069Seric 5674069Seric /* we do have an address to forward to -- do it */ 5684999Seric include(buf, "forwarding", user, sendq); 569292Seric } 570