1292Seric # include <pwd.h> 24212Seric # include <sys/types.h> 34212Seric # include <sys/stat.h> 48437Seric # include <signal.h> 53309Seric # include "sendmail.h" 6292Seric 74106Seric # ifdef DBM 8*9368Seric SCCSID(@(#)alias.c 3.42 11/28/82 (with DBM)); 94106Seric # else DBM 10*9368Seric SCCSID(@(#)alias.c 3.42 11/28/82 (without DBM)); 114106Seric # endif DBM 12402Seric 13292Seric /* 14292Seric ** ALIAS -- Compute aliases. 15292Seric ** 16*9368Seric ** Scans the alias file for an alias for the given address. 17*9368Seric ** If found, it arranges to deliver to the alias list instead. 18*9368Seric ** Uses libdbm database if -DDBM. 19292Seric ** 20292Seric ** Parameters: 214097Seric ** a -- address to alias. 224999Seric ** sendq -- a pointer to the head of the send queue 234999Seric ** to put the aliases in. 24292Seric ** 25292Seric ** Returns: 26292Seric ** none 27292Seric ** 28292Seric ** Side Effects: 293185Seric ** Aliases found are expanded. 30292Seric ** 31292Seric ** Notes: 32292Seric ** If NoAlias (the "-n" flag) is set, no aliasing is 33292Seric ** done. 34292Seric ** 35292Seric ** Deficiencies: 36292Seric ** It should complain about names that are aliased to 37292Seric ** nothing. 38292Seric */ 39292Seric 40292Seric 411503Smark #ifdef DBM 422966Seric typedef struct 432966Seric { 442966Seric char *dptr; 454157Seric int dsize; 464157Seric } DATUM; 474157Seric extern DATUM fetch(); 481503Smark #endif DBM 49292Seric 504999Seric alias(a, sendq) 514097Seric register ADDRESS *a; 524999Seric ADDRESS **sendq; 53292Seric { 544081Seric register char *p; 555701Seric extern char *aliaslookup(); 56292Seric 57292Seric if (NoAlias) 58292Seric return; 59292Seric # ifdef DEBUG 607671Seric if (tTd(27, 1)) 614098Seric printf("alias(%s)\n", a->q_paddr); 62292Seric # endif 63292Seric 644098Seric /* don't realias already aliased names */ 654098Seric if (bitset(QDONTSEND, a->q_flags)) 664098Seric return; 674098Seric 686898Seric CurEnv->e_to = a->q_paddr; 694098Seric 704314Seric /* 714314Seric ** Look up this name 724314Seric */ 734314Seric 745701Seric p = aliaslookup(a->q_user); 754098Seric if (p == NULL) 764098Seric return; 77292Seric 78292Seric /* 794098Seric ** Match on Alias. 804098Seric ** Deliver to the target list. 811515Seric */ 821515Seric 834098Seric # ifdef DEBUG 847671Seric if (tTd(27, 1)) 854098Seric printf("%s (%s, %s) aliased to %s\n", 864098Seric a->q_paddr, a->q_host, a->q_user, p); 874098Seric # endif 887051Seric message(Arpa_Info, "aliased to %s", p); 894098Seric AliasLevel++; 908076Seric sendto(p, a, sendq); 914098Seric AliasLevel--; 924098Seric } 934098Seric /* 945701Seric ** ALIASLOOKUP -- look up a name in the alias file. 955701Seric ** 965701Seric ** Parameters: 975701Seric ** name -- the name to look up. 985701Seric ** 995701Seric ** Returns: 1005701Seric ** the value of name. 1015701Seric ** NULL if unknown. 1025701Seric ** 1035701Seric ** Side Effects: 1045701Seric ** none. 1055701Seric ** 1065701Seric ** Warnings: 1075701Seric ** The return value will be trashed across calls. 1085701Seric */ 1095701Seric 1105701Seric char * 1115701Seric aliaslookup(name) 1125701Seric char *name; 1135701Seric { 1145701Seric # ifdef DBM 1155701Seric DATUM rhs, lhs; 1165701Seric 1175701Seric /* create a key for fetch */ 1185701Seric lhs.dptr = name; 1195701Seric lhs.dsize = strlen(name) + 1; 1205701Seric rhs = fetch(lhs); 1215701Seric return (rhs.dptr); 1225701Seric # else DBM 1235701Seric register STAB *s; 1245701Seric 1255701Seric s = stab(name, ST_ALIAS, ST_FIND); 1265701Seric if (s == NULL) 1275701Seric return (NULL); 1285701Seric return (s->s_alias); 1295701Seric # endif DBM 1305701Seric } 1315701Seric /* 1324098Seric ** INITALIASES -- initialize for aliasing 1334098Seric ** 1344098Seric ** Very different depending on whether we are running DBM or not. 1354098Seric ** 1364098Seric ** Parameters: 1374098Seric ** aliasfile -- location of aliases. 1384157Seric ** init -- if set and if DBM, initialize the DBM files. 1394098Seric ** 1404098Seric ** Returns: 1414098Seric ** none. 1424098Seric ** 1434098Seric ** Side Effects: 1444098Seric ** initializes aliases: 1454098Seric ** if DBM: opens the database. 1464098Seric ** if ~DBM: reads the aliases into the symbol table. 1474098Seric */ 1484098Seric 1494157Seric # define DBMMODE 0666 1504157Seric 1514157Seric initaliases(aliasfile, init) 1524098Seric char *aliasfile; 1534157Seric bool init; 1544098Seric { 155*9368Seric #ifdef DBM 1568437Seric int atcnt; 1574322Seric char buf[MAXNAME]; 1584322Seric time_t modtime; 1598437Seric int (*oldsigint)(); 160*9368Seric #endif DBM 161*9368Seric struct stat stb; 1624322Seric 1638437Seric if (stat(aliasfile, &stb) < 0) 1648437Seric { 1658437Seric NoAlias = TRUE; 1668437Seric return; 1678437Seric } 1688437Seric 1698928Seric # ifdef DBM 1704322Seric /* 1718437Seric ** Check to see that the alias file is complete. 1728437Seric ** If not, we will assume that someone died, and it is up 1738437Seric ** to us to rebuild it. 1748437Seric */ 1758437Seric 1768437Seric dbminit(aliasfile); 1778928Seric atcnt = 10; 1788928Seric while (SafeAlias && !init && atcnt-- >= 0 && aliaslookup("@") == NULL) 1798437Seric sleep(30); 1808437Seric 1818437Seric /* 1824322Seric ** See if the DBM version of the file is out of date with 1834322Seric ** the text version. If so, go into 'init' mode automatically. 1844322Seric ** This only happens if our effective userid owns the DBM 1854325Seric ** version or if the mode of the database is 666 -- this 1864322Seric ** is an attempt to avoid protection problems. Note the 1874322Seric ** unpalatable hack to see if the stat succeeded. 1884322Seric */ 1894322Seric 1904322Seric modtime = stb.st_mtime; 1914322Seric (void) strcpy(buf, aliasfile); 1924322Seric (void) strcat(buf, ".pag"); 1934322Seric stb.st_ino = 0; 1949150Seric if (!init && (atcnt < 0 || stat(buf, &stb) < 0 || stb.st_mtime < modtime)) 1954322Seric { 1969150Seric if (AutoRebuild && stb.st_ino != 0 && 197*9368Seric ((stb.st_mode & 0777) == 0666 || stb.st_uid == geteuid())) 1984322Seric { 1994322Seric init = TRUE; 2007051Seric message(Arpa_Info, "rebuilding alias database"); 2014322Seric } 2024322Seric else 2034322Seric { 2047051Seric bool oldverb = Verbose; 2057051Seric 2067051Seric Verbose = TRUE; 2074322Seric message(Arpa_Info, "Warning: alias database out of date"); 2087051Seric Verbose = oldverb; 2094322Seric } 2104322Seric } 2114322Seric 2124322Seric /* 2134322Seric ** If initializing, create the new files. 2144322Seric ** We should lock the alias file here to prevent other 2154322Seric ** instantiations of sendmail from reading an incomplete 2164322Seric ** file -- or worse yet, doing a concurrent initialize. 2174322Seric */ 2184322Seric 2194157Seric if (init) 2204157Seric { 2218437Seric oldsigint = signal(SIGINT, SIG_IGN); 2224157Seric (void) strcpy(buf, aliasfile); 2234157Seric (void) strcat(buf, ".dir"); 2244157Seric if (close(creat(buf, DBMMODE)) < 0) 2254157Seric { 2264157Seric syserr("cannot make %s", buf); 227*9368Seric (void) signal(SIGINT, oldsigint); 2284157Seric return; 2294157Seric } 2304157Seric (void) strcpy(buf, aliasfile); 2314157Seric (void) strcat(buf, ".pag"); 2324157Seric if (close(creat(buf, DBMMODE)) < 0) 2334157Seric { 2344157Seric syserr("cannot make %s", buf); 235*9368Seric (void) signal(SIGINT, oldsigint); 2364157Seric return; 2374157Seric } 2384157Seric } 2394322Seric 2404322Seric /* 2418437Seric ** If necessary, load the DBM file. 2424322Seric ** If running without DBM, load the symbol table. 2438437Seric ** After loading the DBM file, add the distinquished alias "@". 2444322Seric */ 2454322Seric 2464157Seric if (init) 2478437Seric { 2488437Seric DATUM key; 2498437Seric 2504157Seric readaliases(aliasfile, TRUE); 2518437Seric key.dsize = 2; 2528437Seric key.dptr = "@"; 2538437Seric store(key, key); 2548437Seric (void) signal(SIGINT, oldsigint); 2558437Seric } 2564098Seric # else DBM 2574157Seric readaliases(aliasfile, init); 2584157Seric # endif DBM 2594157Seric } 2604157Seric /* 2614157Seric ** READALIASES -- read and process the alias file. 2624157Seric ** 2634157Seric ** This routine implements the part of initaliases that occurs 2644157Seric ** when we are not going to use the DBM stuff. 2654157Seric ** 2664157Seric ** Parameters: 2674157Seric ** aliasfile -- the pathname of the alias file master. 2684157Seric ** init -- if set, initialize the DBM stuff. 2694157Seric ** 2704157Seric ** Returns: 2714157Seric ** none. 2724157Seric ** 2734157Seric ** Side Effects: 2744157Seric ** Reads aliasfile into the symbol table. 2754157Seric ** Optionally, builds the .dir & .pag files. 2764157Seric */ 2774157Seric 2784157Seric static 2794157Seric readaliases(aliasfile, init) 2804157Seric char *aliasfile; 2814157Seric bool init; 2824157Seric { 2834098Seric register char *p; 2844098Seric char *p2; 2854098Seric char *rhs; 2864098Seric bool skipping; 287*9368Seric int naliases, bytes, longest; 288*9368Seric FILE *af; 2894098Seric ADDRESS al, bl; 2904106Seric register STAB *s; 291*9368Seric char line[BUFSIZ]; 2924098Seric 2934098Seric if ((af = fopen(aliasfile, "r")) == NULL) 2941515Seric { 2954098Seric # ifdef DEBUG 2967671Seric if (tTd(27, 1)) 2974106Seric printf("Can't open %s\n", aliasfile); 2984098Seric # endif 2994098Seric errno = 0; 3004098Seric NoAlias++; 3014098Seric return; 3024098Seric } 3034314Seric 3044314Seric /* 3054314Seric ** Read and interpret lines 3064314Seric */ 3074314Seric 308*9368Seric FileName = aliasfile; 309*9368Seric LineNumber = 0; 3104322Seric naliases = bytes = longest = 0; 3114098Seric skipping = FALSE; 3124098Seric while (fgets(line, sizeof (line), af) != NULL) 3134098Seric { 3144322Seric int lhssize, rhssize; 3154322Seric 316*9368Seric LineNumber++; 3174098Seric switch (line[0]) 3184098Seric { 3194098Seric case '#': 3204098Seric case '\n': 3214098Seric case '\0': 3224098Seric skipping = FALSE; 3234098Seric continue; 3244065Seric 3254098Seric case ' ': 3264098Seric case '\t': 3274098Seric if (!skipping) 328*9368Seric syserr("Non-continuation line starts with space"); 3294098Seric skipping = TRUE; 3304097Seric continue; 3314098Seric } 3324098Seric skipping = FALSE; 3331874Seric 3344314Seric /* 3354314Seric ** Process the LHS 3364314Seric ** Find the final colon, and parse the address. 3374314Seric ** It should resolve to a local name -- this will 3384314Seric ** be checked later (we want to optionally do 3394314Seric ** parsing of the RHS first to maximize error 3404314Seric ** detection). 3414314Seric */ 3424314Seric 3434098Seric for (p = line; *p != '\0' && *p != ':' && *p != '\n'; p++) 3444097Seric continue; 3454098Seric if (*p == '\0' || *p == '\n') 3464098Seric { 347*9368Seric syserr("missing colon"); 3484097Seric continue; 3494098Seric } 3504098Seric *p++ = '\0'; 3514098Seric if (parse(line, &al, 1) == NULL) 3524098Seric { 3534098Seric *--p = ':'; 354*9368Seric syserr("illegal alias name"); 355*9368Seric continue; 3564098Seric } 3574314Seric 3584314Seric /* 3594314Seric ** Process the RHS. 3604314Seric ** 'al' is the internal form of the LHS address. 3614314Seric ** 'p' points to the text of the RHS. 3624314Seric */ 3634314Seric 3644098Seric rhs = p; 3654098Seric for (;;) 3664098Seric { 3674098Seric register char c; 3681515Seric 3694157Seric if (init) 3704098Seric { 3714157Seric /* do parsing & compression of addresses */ 3724098Seric c = *p; 3734157Seric while (c != '\0') 3744098Seric { 3754157Seric p2 = p; 3764157Seric while (*p != '\n' && *p != ',' && *p != '\0') 3774157Seric p++; 3784157Seric c = *p; 3794157Seric *p++ = '\0'; 3804322Seric if (c == '\n') 3814322Seric c = '\0'; 3824157Seric if (*p2 == '\0') 3834157Seric { 3844157Seric p[-1] = c; 3854157Seric continue; 3864157Seric } 3874314Seric (void) parse(p2, &bl, -1); 3884098Seric p[-1] = c; 3894157Seric while (isspace(*p)) 3904157Seric p++; 3914098Seric } 3924098Seric } 3934157Seric else 3944157Seric p = &p[strlen(p)]; 3951515Seric 3964098Seric /* see if there should be a continuation line */ 3974106Seric c = fgetc(af); 3984106Seric if (!feof(af)) 3994314Seric (void) ungetc(c, af); 4004106Seric if (c != ' ' && c != '\t') 4014098Seric break; 4024098Seric 4034098Seric /* read continuation line */ 4044098Seric p--; 4054098Seric if (fgets(p, sizeof line - (p - line), af) == NULL) 4064098Seric break; 407*9368Seric LineNumber++; 4084098Seric } 4094594Seric if (al.q_mailer != LocalMailer) 4104098Seric { 411*9368Seric syserr("cannot alias non-local names"); 4124098Seric continue; 4134098Seric } 4144314Seric 4154314Seric /* 4164314Seric ** Insert alias into symbol table or DBM file 4174314Seric */ 4184314Seric 4194322Seric lhssize = strlen(al.q_user) + 1; 4204322Seric rhssize = strlen(rhs) + 1; 4214322Seric 4224157Seric # ifdef DBM 4234157Seric if (init) 4244157Seric { 4254157Seric DATUM key, content; 4264157Seric 4274322Seric key.dsize = lhssize; 4284157Seric key.dptr = al.q_user; 4294322Seric content.dsize = rhssize; 4304157Seric content.dptr = rhs; 4314157Seric store(key, content); 4324157Seric } 4334157Seric else 4344157Seric # endif DBM 4354157Seric { 4364157Seric s = stab(al.q_user, ST_ALIAS, ST_ENTER); 4374157Seric s->s_alias = newstr(rhs); 4384157Seric } 4394322Seric 4404322Seric /* statistics */ 4414322Seric naliases++; 4424322Seric bytes += lhssize + rhssize; 4434322Seric if (rhssize > longest) 4444322Seric longest = rhssize; 4451515Seric } 4464098Seric (void) fclose(af); 4476898Seric CurEnv->e_to = NULL; 448*9368Seric FileName = NULL; 4497051Seric message(Arpa_Info, "%d aliases, longest %d bytes, %d bytes total", 4504322Seric naliases, longest, bytes); 451292Seric } 452292Seric /* 453292Seric ** FORWARD -- Try to forward mail 454292Seric ** 455292Seric ** This is similar but not identical to aliasing. 456292Seric ** 457292Seric ** Parameters: 4584314Seric ** user -- the name of the user who's mail we would like 4594314Seric ** to forward to. It must have been verified -- 4604314Seric ** i.e., the q_home field must have been filled 4614314Seric ** in. 4624999Seric ** sendq -- a pointer to the head of the send queue to 4634999Seric ** put this user's aliases in. 464292Seric ** 465292Seric ** Returns: 4664098Seric ** none. 467292Seric ** 468292Seric ** Side Effects: 4693185Seric ** New names are added to send queues. 470292Seric */ 471292Seric 4724999Seric forward(user, sendq) 4732966Seric ADDRESS *user; 4744999Seric ADDRESS **sendq; 475292Seric { 4764078Seric char buf[60]; 4774536Seric extern bool safefile(); 4784069Seric 4794098Seric # ifdef DEBUG 4807671Seric if (tTd(27, 1)) 4814098Seric printf("forward(%s)\n", user->q_paddr); 4824098Seric # endif DEBUG 4834098Seric 4844594Seric if (user->q_mailer != LocalMailer || bitset(QBADADDR, user->q_flags)) 4854098Seric return; 4864314Seric # ifdef DEBUG 4874314Seric if (user->q_home == NULL) 4884314Seric syserr("forward: no home"); 4894314Seric # endif DEBUG 4904069Seric 4914069Seric /* good address -- look for .forward file in home */ 492*9368Seric define('z', user->q_home, CurEnv); 4936973Seric expand("$z/.forward", buf, &buf[sizeof buf - 1], CurEnv); 4944536Seric if (!safefile(buf, user->q_uid, S_IREAD)) 4954098Seric return; 4964069Seric 4974069Seric /* we do have an address to forward to -- do it */ 4984999Seric include(buf, "forwarding", user, sendq); 499292Seric } 500