156822Seric /* 256822Seric * Copyright (c) 1992 Eric P. Allman. 356822Seric * Copyright (c) 1992 Regents of the University of California. 456822Seric * All rights reserved. 556822Seric * 656822Seric * %sccs.include.redist.c% 756822Seric */ 856822Seric 956822Seric #ifndef lint 10*60585Seric static char sccsid[] = "@(#)map.c 6.25 (Berkeley) 05/29/93"; 1156822Seric #endif /* not lint */ 1256822Seric 1356822Seric #include "sendmail.h" 1456822Seric 1560089Seric #ifdef NDBM 1656822Seric #include <ndbm.h> 1756822Seric #endif 1860089Seric #ifdef NEWDB 1956822Seric #include <db.h> 2056822Seric #endif 2160089Seric #ifdef NIS 2257208Seric #include <rpcsvc/ypclnt.h> 2357208Seric #endif 2456822Seric 2556822Seric /* 2660089Seric ** MAP.C -- implementations for various map classes. 2756822Seric ** 2860089Seric ** Each map class implements a series of functions: 2960089Seric ** 3060089Seric ** bool map_parse(MAP *map, char *args) 3160089Seric ** Parse the arguments from the config file. Return TRUE 3260089Seric ** if they were ok, FALSE otherwise. Fill in map with the 3360089Seric ** values. 3460089Seric ** 3560222Seric ** char *map_lookup(MAP *map, char *key, char **args, int *pstat) 3660222Seric ** Look up the key in the given map. If found, do any 3760222Seric ** rewriting the map wants (including "args" if desired) 3860089Seric ** and return the value. Set *pstat to the appropriate status 3960222Seric ** on error and return NULL. Args will be NULL if called 4060222Seric ** from the alias routines, although this should probably 4160222Seric ** not be relied upon. It is suggested you call map_rewrite 4260222Seric ** to return the results -- it takes care of null termination 4360222Seric ** and uses a dynamically expanded buffer as needed. 4460089Seric ** 4560089Seric ** void map_store(MAP *map, char *key, char *value) 4660089Seric ** Store the key:value pair in the map. 4760089Seric ** 4860089Seric ** bool map_open(MAP *map, int mode) 4960222Seric ** Open the map for the indicated mode. Mode should 5060222Seric ** be either O_RDONLY or O_RDWR. Return TRUE if it 5160222Seric ** was opened successfully, FALSE otherwise. If the open 5260222Seric ** failed an the MF_OPTIONAL flag is not set, it should 5360222Seric ** also print an error. If the MF_ALIAS bit is set 5460222Seric ** and this map class understands the @:@ convention, it 5560222Seric ** should call aliaswait() before returning. 5660089Seric ** 5760089Seric ** void map_close(MAP *map) 5860089Seric ** Close the map. 5960089Seric */ 6060089Seric 6160089Seric #define DBMMODE 0644 6260089Seric /* 6360089Seric ** MAP_PARSEARGS -- parse config line arguments for database lookup 6460089Seric ** 6560089Seric ** This is a generic version of the map_parse method. 6660089Seric ** 6756822Seric ** Parameters: 6860089Seric ** map -- the map being initialized. 6960089Seric ** ap -- a pointer to the args on the config line. 7056822Seric ** 7156822Seric ** Returns: 7260089Seric ** TRUE -- if everything parsed OK. 7356822Seric ** FALSE -- otherwise. 7456822Seric ** 7556822Seric ** Side Effects: 7660089Seric ** null terminates the filename; stores it in map 7756822Seric */ 7856822Seric 7956822Seric bool 8060089Seric map_parseargs(map, ap) 8156822Seric MAP *map; 8260089Seric char *ap; 8356822Seric { 8460089Seric register char *p = ap; 8556822Seric 8660089Seric for (;;) 8760089Seric { 8860089Seric while (isascii(*p) && isspace(*p)) 8960089Seric p++; 9060089Seric if (*p != '-') 9160089Seric break; 9260089Seric switch (*++p) 9360089Seric { 9460089Seric case 'N': 9560207Seric map->map_mflags |= MF_INCLNULL; 9660089Seric break; 9760089Seric 9860089Seric case 'o': 9960207Seric map->map_mflags |= MF_OPTIONAL; 10060089Seric break; 10160089Seric 10260089Seric case 'f': 10360207Seric map->map_mflags |= MF_NOFOLDCASE; 10460089Seric break; 10560089Seric 10660089Seric case 'm': 10760207Seric map->map_mflags |= MF_MATCHONLY; 10860089Seric break; 10960089Seric 11060089Seric case 'a': 11160089Seric map->map_app = ++p; 11260089Seric break; 11360089Seric } 11460089Seric while (*p != '\0' && !(isascii(*p) && isspace(*p))) 11560089Seric p++; 11660089Seric if (*p != '\0') 11760089Seric *p++ = '\0'; 11860089Seric } 11960089Seric if (map->map_app != NULL) 12060089Seric map->map_app = newstr(map->map_app); 12160089Seric 12260089Seric if (*p != '\0') 12360089Seric { 12460089Seric map->map_file = p; 12560089Seric while (*p != '\0' && !(isascii(*p) && isspace(*p))) 12660089Seric p++; 12760089Seric if (*p != '\0') 12860089Seric *p++ = '\0'; 12960089Seric map->map_file = newstr(map->map_file); 13060089Seric } 13160089Seric 13260089Seric while (*p != '\0' && isascii(*p) && isspace(*p)) 13360089Seric p++; 13460089Seric if (*p != '\0') 13560089Seric map->map_rebuild = newstr(p); 13660089Seric 13756822Seric if (map->map_file == NULL) 13857208Seric { 13960089Seric syserr("No file name for %s map %s", 14060089Seric map->map_class->map_cname, map->map_mname); 14156822Seric return FALSE; 14257208Seric } 14360089Seric return TRUE; 14460089Seric } 14560089Seric /* 14660089Seric ** MAP_REWRITE -- rewrite a database key, interpolating %n indications. 14760089Seric ** 14860089Seric ** It also adds the map_app string. It can be used as a utility 14960089Seric ** in the map_lookup method. 15060089Seric ** 15160089Seric ** Parameters: 15260089Seric ** map -- the map that causes this. 15360089Seric ** s -- the string to rewrite, NOT necessarily null terminated. 15460089Seric ** slen -- the length of s. 15560089Seric ** av -- arguments to interpolate into buf. 15660089Seric ** 15760089Seric ** Returns: 15860089Seric ** Pointer to rewritten result. 15960089Seric ** 16060089Seric ** Side Effects: 16160089Seric ** none. 16260089Seric */ 16360089Seric 16460492Seric struct rwbuf 16560492Seric { 16660492Seric int rwb_len; /* size of buffer */ 16760492Seric char *rwb_buf; /* ptr to buffer */ 16860492Seric }; 16960492Seric 17060492Seric struct rwbuf RwBufs[2]; /* buffers for rewriting output */ 17160492Seric 17260089Seric char * 17360089Seric map_rewrite(map, s, slen, av) 17460089Seric register MAP *map; 17560089Seric register char *s; 17660089Seric int slen; 17760089Seric char **av; 17860089Seric { 17960089Seric register char *bp; 18060089Seric register char c; 18160089Seric char **avp; 18260089Seric register char *ap; 18360492Seric register struct rwbuf *rwb; 18460089Seric int i; 18560089Seric int len; 18660089Seric 18760537Seric if (tTd(39, 1)) 18860089Seric { 18960256Seric printf("map_rewrite(%.*s), av =", slen, s); 19060256Seric if (av == NULL) 19160256Seric printf(" (nullv)"); 19260256Seric else 19360256Seric { 19460256Seric for (avp = av; *avp != NULL; avp++) 19560256Seric printf("\n\t%s", *avp); 19660256Seric } 19760256Seric printf("\n"); 19860089Seric } 19960089Seric 20060492Seric rwb = RwBufs; 20160492Seric if (av == NULL) 20260492Seric rwb++; 20360492Seric 20460089Seric /* count expected size of output (can safely overestimate) */ 20560089Seric i = len = slen; 20660089Seric if (av != NULL) 20760089Seric { 20860089Seric bp = s; 20960089Seric for (i = slen; --i >= 0 && (c = *bp++) != 0; ) 21060089Seric { 21160089Seric if (c != '%') 21260089Seric continue; 21360089Seric if (--i < 0) 21460089Seric break; 21560089Seric c = *bp++; 21660089Seric if (!(isascii(c) && isdigit(c))) 21760089Seric continue; 21860089Seric c -= 0; 21960089Seric for (avp = av; --c >= 0 && *avp != NULL; avp++) 22060089Seric continue; 22160089Seric if (*avp == NULL) 22260089Seric continue; 22360089Seric len += strlen(*avp); 22460089Seric } 22560089Seric } 22660089Seric if (map->map_app != NULL) 22760089Seric len += strlen(map->map_app); 22860492Seric if (rwb->rwb_len < ++len) 22960089Seric { 23060089Seric /* need to malloc additional space */ 23160492Seric rwb->rwb_len = len; 23260492Seric if (rwb->rwb_buf != NULL) 23360492Seric free(rwb->rwb_buf); 23460492Seric rwb->rwb_buf = xalloc(rwb->rwb_len); 23560089Seric } 23660089Seric 23760492Seric bp = rwb->rwb_buf; 23860089Seric if (av == NULL) 23960089Seric { 24060089Seric bcopy(s, bp, slen); 24160089Seric bp += slen; 24260089Seric } 24360089Seric else 24460089Seric { 24560089Seric while (--slen >= 0 && (c = *s++) != '\0') 24660089Seric { 24760089Seric if (c != '%') 24860089Seric { 24960089Seric pushc: 25060089Seric *bp++ = c; 25160089Seric continue; 25260089Seric } 25360089Seric if (--slen < 0 || (c = *s++) == '\0') 25460089Seric c = '%'; 25560089Seric if (c == '%') 25660089Seric goto pushc; 25760089Seric if (!(isascii(c) && isdigit(c))) 25860089Seric { 25960089Seric *bp++ = '%'; 26060089Seric goto pushc; 26160089Seric } 26260089Seric c -= '0'; 26360089Seric for (avp = av; --c >= 0 && *avp != NULL; avp++) 26460089Seric continue; 26560089Seric if (*avp == NULL) 26660089Seric continue; 26760089Seric 26860089Seric /* transliterate argument into output string */ 26960089Seric for (ap = *avp; (c = *ap++) != '\0'; ) 27060089Seric *bp++ = c; 27160089Seric } 27260089Seric } 27360089Seric if (map->map_app != NULL) 27460089Seric strcpy(bp, map->map_app); 27560089Seric else 27660089Seric *bp = '\0'; 27760537Seric if (tTd(39, 1)) 27860492Seric printf("map_rewrite => %s\n", rwb->rwb_buf); 27960492Seric return rwb->rwb_buf; 28060089Seric } 28160089Seric /* 28260537Seric ** INITMAPS -- initialize for aliasing 28360537Seric ** 28460537Seric ** Parameters: 28560537Seric ** rebuild -- if TRUE, this rebuilds the cached versions. 28660537Seric ** e -- current envelope. 28760537Seric ** 28860537Seric ** Returns: 28960537Seric ** none. 29060537Seric ** 29160537Seric ** Side Effects: 29260537Seric ** initializes aliases: 29360537Seric ** if NDBM: opens the database. 29460537Seric ** if ~NDBM: reads the aliases into the symbol table. 29560537Seric */ 29660537Seric 29760537Seric initmaps(rebuild, e) 29860537Seric bool rebuild; 29960537Seric register ENVELOPE *e; 30060537Seric { 30160537Seric extern void map_init(); 30260537Seric 30360537Seric CurEnv = e; 30460537Seric stabapply(map_init, rebuild); 30560537Seric } 30660537Seric 30760537Seric void 30860537Seric map_init(s, rebuild) 30960537Seric register STAB *s; 31060537Seric int rebuild; 31160537Seric { 31260537Seric register MAP *map; 31360537Seric 31460537Seric /* has to be a map */ 31560537Seric if (s->s_type != ST_MAP) 31660537Seric return; 31760537Seric 31860537Seric map = &s->s_map; 31960537Seric if (!bitset(MF_VALID, map->map_mflags)) 32060537Seric return; 32160537Seric 32260537Seric if (tTd(38, 2)) 32360537Seric printf("map_init(%s:%s)\n", 32460537Seric map->map_class->map_cname, map->map_file); 32560537Seric 32660537Seric /* if already open, close it (for nested open) */ 32760537Seric if (bitset(MF_OPEN, map->map_mflags)) 32860537Seric { 32960537Seric map->map_class->map_close(map); 33060537Seric map->map_mflags &= ~(MF_OPEN|MF_WRITABLE); 33160537Seric } 33260537Seric 33360537Seric if (rebuild) 33460537Seric { 33560537Seric if (bitset(MCF_REBUILDABLE, map->map_class->map_cflags)) 33660537Seric rebuildaliases(map, FALSE); 33760537Seric } 33860537Seric else 33960537Seric { 34060537Seric if (map->map_class->map_open(map, O_RDONLY)) 34160537Seric { 34260537Seric if (tTd(38, 4)) 34360537Seric printf("%s:%s: valid\n", 34460537Seric map->map_class->map_cname, 34560537Seric map->map_file); 34660537Seric map->map_mflags |= MF_OPEN; 34760537Seric } 34860537Seric else if (tTd(38, 4)) 34960537Seric printf("%s:%s: invalid: %s\n", 35060537Seric map->map_class->map_cname, 35160537Seric map->map_file, 35260537Seric errstring(errno)); 35360537Seric } 35460537Seric } 35560537Seric /* 35660089Seric ** NDBM modules 35760089Seric */ 35860089Seric 35960089Seric #ifdef NDBM 36060089Seric 36160089Seric /* 36260089Seric ** DBM_MAP_OPEN -- DBM-style map open 36360089Seric */ 36460089Seric 36560089Seric bool 36660089Seric ndbm_map_open(map, mode) 36760089Seric MAP *map; 36860089Seric int mode; 36960089Seric { 37060089Seric DBM *dbm; 37160089Seric 37260537Seric if (tTd(38, 2)) 37360089Seric printf("ndbm_map_open(%s, %d)\n", map->map_file, mode); 37460089Seric 37560207Seric if (mode == O_RDWR) 37660207Seric mode |= O_CREAT|O_TRUNC; 37760207Seric 37860089Seric /* open the database */ 37960089Seric dbm = dbm_open(map->map_file, mode, DBMMODE); 38056822Seric if (dbm == NULL) 38156822Seric { 38260207Seric if (!bitset(MF_OPTIONAL, map->map_mflags)) 38356836Seric syserr("Cannot open DBM database %s", map->map_file); 38456822Seric return FALSE; 38556822Seric } 38660089Seric map->map_db1 = (void *) dbm; 38760207Seric if (mode == O_RDONLY && bitset(MF_ALIAS, map->map_mflags)) 38860207Seric aliaswait(map, ".dir"); 38956822Seric return TRUE; 39056822Seric } 39160089Seric 39260089Seric 39360089Seric /* 39456822Seric ** DBM_MAP_LOOKUP -- look up a datum in a DBM-type map 39556822Seric */ 39656822Seric 39756822Seric char * 39860089Seric ndbm_map_lookup(map, name, av, statp) 39956822Seric MAP *map; 40060089Seric char *name; 40156822Seric char **av; 40259084Seric int *statp; 40356822Seric { 40456822Seric datum key, val; 40560089Seric char keybuf[MAXNAME + 1]; 40656822Seric 40760537Seric if (tTd(38, 20)) 40860089Seric printf("ndbm_map_lookup(%s)\n", name); 40960089Seric 41060089Seric key.dptr = name; 41160089Seric key.dsize = strlen(name); 41260207Seric if (!bitset(MF_NOFOLDCASE, map->map_mflags)) 41357014Seric { 41460089Seric if (key.dsize > sizeof keybuf - 1) 41560089Seric key.dsize = sizeof keybuf - 1; 41660089Seric bcopy(key.dptr, keybuf, key.dsize + 1); 41760089Seric makelower(keybuf); 41860089Seric key.dptr = keybuf; 41957014Seric } 42060207Seric if (bitset(MF_INCLNULL, map->map_mflags)) 42156822Seric key.dsize++; 42260089Seric (void) lockfile(dbm_dirfno((DBM *) map->map_db1), map->map_file, LOCK_SH); 42360089Seric val = dbm_fetch((DBM *) map->map_db1, key); 42460089Seric (void) lockfile(dbm_dirfno((DBM *) map->map_db1), map->map_file, LOCK_UN); 42556822Seric if (val.dptr == NULL) 42656822Seric return NULL; 42760207Seric if (bitset(MF_MATCHONLY, map->map_mflags)) 42860089Seric av = NULL; 42960089Seric return map_rewrite(map, val.dptr, val.dsize, av); 43056822Seric } 43156822Seric 43256822Seric 43356822Seric /* 43460089Seric ** DBM_MAP_STORE -- store a datum in the database 43556822Seric */ 43656822Seric 43760089Seric void 43860089Seric ndbm_map_store(map, lhs, rhs) 43960089Seric register MAP *map; 44060089Seric char *lhs; 44160089Seric char *rhs; 44260089Seric { 44360089Seric datum key; 44460089Seric datum data; 44560089Seric int stat; 44660089Seric 44760537Seric if (tTd(38, 12)) 44860089Seric printf("ndbm_map_store(%s, %s)\n", lhs, rhs); 44960089Seric 45060089Seric key.dsize = strlen(lhs); 45160089Seric key.dptr = lhs; 45260089Seric 45360089Seric data.dsize = strlen(rhs); 45460089Seric data.dptr = rhs; 45560089Seric 45660207Seric if (bitset(MF_INCLNULL, map->map_mflags)) 45760089Seric { 45860089Seric key.dsize++; 45960089Seric data.dsize++; 46060089Seric } 46160089Seric 46260089Seric stat = dbm_store((DBM *) map->map_db1, key, data, DBM_INSERT); 46360089Seric if (stat > 0) 46460089Seric { 46560089Seric usrerr("050 Warning: duplicate alias name %s", lhs); 46660089Seric stat = dbm_store((DBM *) map->map_db1, key, data, DBM_REPLACE); 46760089Seric } 46860089Seric if (stat != 0) 46960089Seric syserr("readaliases: dbm put (%s)", lhs); 47060089Seric } 47160089Seric 47260089Seric 47360089Seric /* 47460207Seric ** NDBM_MAP_CLOSE -- close the database 47560089Seric */ 47660089Seric 47760089Seric void 47860089Seric ndbm_map_close(map) 47960089Seric register MAP *map; 48060089Seric { 48160207Seric if (bitset(MF_WRITABLE, map->map_mflags)) 48260089Seric { 48360089Seric #ifdef YPCOMPAT 48460089Seric char buf[200]; 48560089Seric 48660089Seric (void) sprintf(buf, "%010ld", curtime()); 48760089Seric ndbm_map_store(map, "YP_LAST_MODIFIED", buf); 48860089Seric 48960089Seric (void) myhostname(buf, sizeof buf); 49060089Seric ndbm_map_store(map, "YP_MASTER_NAME", buf); 49160089Seric #endif 49260089Seric 49360089Seric /* write out the distinguished alias */ 49460089Seric ndbm_map_store(map, "@", "@"); 49560089Seric } 49660089Seric dbm_close((DBM *) map->map_db1); 49760089Seric } 49860089Seric 49960089Seric #endif 50060089Seric /* 50160582Seric ** NEWDB (Hash and BTree) Modules 50260089Seric */ 50360089Seric 50460089Seric #ifdef NEWDB 50560089Seric 50660089Seric /* 50760582Seric ** BT_MAP_OPEN, HASH_MAP_OPEN -- database open primitives. 50860582Seric ** 50960582Seric ** These do rather bizarre locking. If you can lock on open, 51060582Seric ** do that to avoid the condition of opening a database that 51160582Seric ** is being rebuilt. If you don't, we'll try to fake it, but 51260582Seric ** there will be a race condition. If opening for read-only, 51360582Seric ** we immediately release the lock to avoid freezing things up. 51460582Seric ** We really ought to hold the lock, but guarantee that we won't 51560582Seric ** be pokey about it. That's hard to do. 51660089Seric */ 51760089Seric 51856822Seric bool 51960089Seric bt_map_open(map, mode) 52056822Seric MAP *map; 52160089Seric int mode; 52256822Seric { 52356822Seric DB *db; 52460228Seric int i; 52560582Seric int omode; 52660089Seric char buf[MAXNAME]; 52756822Seric 52860537Seric if (tTd(38, 2)) 52960089Seric printf("bt_map_open(%s, %d)\n", map->map_file, mode); 53060089Seric 53160582Seric omode = mode; 53260582Seric if (omode == O_RDWR) 53360582Seric { 53460582Seric omode |= O_CREAT|O_TRUNC; 53560582Seric #if defined(O_EXLOCK) && !defined(LOCKF) 53660582Seric omode |= O_EXLOCK; 53760582Seric # if !defined(OLD_NEWDB) 53860582Seric } 53960582Seric else 54060582Seric { 54160582Seric omode |= O_SHLOCK; 54260582Seric # endif 54360582Seric #endif 54460582Seric } 54560207Seric 54660228Seric (void) strcpy(buf, map->map_file); 54760228Seric i = strlen(buf); 54860228Seric if (i < 3 || strcmp(&buf[i - 3], ".db") != 0) 54960228Seric (void) strcat(buf, ".db"); 55060582Seric db = dbopen(buf, omode, DBMMODE, DB_BTREE, NULL); 55156822Seric if (db == NULL) 55256822Seric { 55360207Seric if (!bitset(MF_OPTIONAL, map->map_mflags)) 55456836Seric syserr("Cannot open BTREE database %s", map->map_file); 55556822Seric return FALSE; 55656822Seric } 55760582Seric #if !defined(OLD_NEWDB) && !defined(LOCKF) 55860582Seric # if !defined(O_EXLOCK) 55960582Seric if (mode == O_RDWR) 56060582Seric (void) lockfile(db->fd(db), map->map_file, LOCK_EX); 56160582Seric # else 56260582Seric if (mode == O_RDONLY) 56360582Seric (void) lockfile(db->fd(db), map->map_file, LOCK_UN); 56460582Seric # endif 56560582Seric #endif 566*60585Seric 567*60585Seric /* try to make sure that at least the database header is on disk */ 568*60585Seric if (mode == O_RDWR) 569*60585Seric (void) db->sync(db, 0); 570*60585Seric 57160089Seric map->map_db2 = (void *) db; 57260207Seric if (mode == O_RDONLY && bitset(MF_ALIAS, map->map_mflags)) 57360207Seric aliaswait(map, ".db"); 57456822Seric return TRUE; 57556822Seric } 57656822Seric 57756822Seric 57856822Seric /* 57956822Seric ** HASH_MAP_INIT -- HASH-style map initialization 58056822Seric */ 58156822Seric 58256822Seric bool 58360089Seric hash_map_open(map, mode) 58456822Seric MAP *map; 58560089Seric int mode; 58656822Seric { 58756822Seric DB *db; 58860228Seric int i; 58960582Seric int omode; 59060089Seric char buf[MAXNAME]; 59156822Seric 59260537Seric if (tTd(38, 2)) 59360089Seric printf("hash_map_open(%s, %d)\n", map->map_file, mode); 59460089Seric 59560582Seric omode = mode; 59660582Seric if (omode == O_RDWR) 59760582Seric { 59860582Seric omode |= O_CREAT|O_TRUNC; 59960582Seric #if defined(O_EXLOCK) && !defined(LOCKF) 60060582Seric omode |= O_EXLOCK; 60160582Seric # if !defined(OLD_NEWDB) 60260582Seric } 60360582Seric else 60460582Seric { 60560582Seric omode |= O_SHLOCK; 60660582Seric # endif 60760582Seric #endif 60860582Seric } 60960207Seric 61060228Seric (void) strcpy(buf, map->map_file); 61160228Seric i = strlen(buf); 61260228Seric if (i < 3 || strcmp(&buf[i - 3], ".db") != 0) 61360228Seric (void) strcat(buf, ".db"); 61460582Seric db = dbopen(buf, omode, DBMMODE, DB_HASH, NULL); 61556822Seric if (db == NULL) 61656822Seric { 61760207Seric if (!bitset(MF_OPTIONAL, map->map_mflags)) 61856836Seric syserr("Cannot open HASH database %s", map->map_file); 61956822Seric return FALSE; 62056822Seric } 62160582Seric #if !defined(OLD_NEWDB) && !defined(LOCKF) 62260582Seric # if !defined(O_EXLOCK) 62360582Seric if (mode == O_RDWR) 62460582Seric (void) lockfile(db->fd(db), map->map_file, LOCK_EX); 62560582Seric # else 62660582Seric if (mode == O_RDONLY) 62760582Seric (void) lockfile(db->fd(db), map->map_file, LOCK_UN); 62860582Seric # endif 62960582Seric #endif 630*60585Seric 631*60585Seric /* try to make sure that at least the database header is on disk */ 632*60585Seric if (mode == O_RDWR) 633*60585Seric (void) db->sync(db, 0); 634*60585Seric 63560089Seric map->map_db2 = (void *) db; 63660207Seric if (mode == O_RDONLY && bitset(MF_ALIAS, map->map_mflags)) 63760207Seric aliaswait(map, ".db"); 63856822Seric return TRUE; 63956822Seric } 64056822Seric 64156822Seric 64256822Seric /* 64356822Seric ** DB_MAP_LOOKUP -- look up a datum in a BTREE- or HASH-type map 64456822Seric */ 64556822Seric 64656822Seric char * 64760089Seric db_map_lookup(map, name, av, statp) 64856822Seric MAP *map; 64960089Seric char *name; 65056822Seric char **av; 65159084Seric int *statp; 65256822Seric { 65356822Seric DBT key, val; 65460422Seric register DB *db = (DB *) map->map_db2; 65560422Seric int st; 65660422Seric int saveerrno; 65760089Seric char keybuf[MAXNAME + 1]; 65856822Seric 65960537Seric if (tTd(38, 20)) 66060089Seric printf("db_map_lookup(%s)\n", name); 66160089Seric 66260089Seric key.size = strlen(name); 66360089Seric if (key.size > sizeof keybuf - 1) 66460089Seric key.size = sizeof keybuf - 1; 66560089Seric key.data = keybuf; 66660089Seric bcopy(name, keybuf, key.size + 1); 66760207Seric if (!bitset(MF_NOFOLDCASE, map->map_mflags)) 66860089Seric makelower(keybuf); 66960207Seric if (bitset(MF_INCLNULL, map->map_mflags)) 67056822Seric key.size++; 67160422Seric #ifndef OLD_NEWDB 67260422Seric (void) lockfile(db->fd(db), map->map_file, LOCK_SH); 67360422Seric #endif 67460422Seric st = db->get(db, &key, &val, 0); 67560422Seric saveerrno = errno; 67660422Seric #ifndef OLD_NEWDB 67760422Seric (void) lockfile(db->fd(db), map->map_file, LOCK_UN); 67860422Seric #endif 67960422Seric if (st != 0) 68060422Seric { 68160422Seric errno = saveerrno; 68260422Seric if (st < 0) 68360422Seric syserr("db_map_lookup: get (%s)", name); 68456822Seric return NULL; 68560422Seric } 68660207Seric if (bitset(MF_MATCHONLY, map->map_mflags)) 68760089Seric av = NULL; 68860089Seric return map_rewrite(map, val.data, val.size, av); 68956822Seric } 69056822Seric 69160089Seric 69260089Seric /* 69360089Seric ** DB_MAP_STORE -- store a datum in the NEWDB database 69456822Seric */ 69556822Seric 69660089Seric void 69760089Seric db_map_store(map, lhs, rhs) 69860089Seric register MAP *map; 69960089Seric char *lhs; 70060089Seric char *rhs; 70156822Seric { 70260089Seric int stat; 70360089Seric DBT key; 70460089Seric DBT data; 70560089Seric register DB *db = map->map_db2; 70656822Seric 70760537Seric if (tTd(38, 20)) 70860089Seric printf("db_map_store(%s, %s)\n", lhs, rhs); 70960089Seric 71060089Seric key.size = strlen(lhs); 71160089Seric key.data = lhs; 71260089Seric 71360089Seric data.size = strlen(rhs); 71460089Seric data.data = rhs; 71560089Seric 71660207Seric if (bitset(MF_INCLNULL, map->map_mflags)) 71756822Seric { 71860089Seric key.size++; 71960089Seric data.size++; 72060089Seric } 72156836Seric 72260089Seric stat = db->put(db, &key, &data, R_NOOVERWRITE); 72360089Seric if (stat > 0) 72460089Seric { 72560089Seric usrerr("050 Warning: duplicate alias name %s", lhs); 72660089Seric stat = db->put(db, &key, &data, 0); 72760089Seric } 72860089Seric if (stat != 0) 72960089Seric syserr("readaliases: db put (%s)", lhs); 73060089Seric } 73156836Seric 73256847Seric 73360089Seric /* 73460089Seric ** DB_MAP_CLOSE -- add distinguished entries and close the database 73560089Seric */ 73660089Seric 73760089Seric void 73860089Seric db_map_close(map) 73960089Seric MAP *map; 74060089Seric { 74160089Seric register DB *db = map->map_db2; 74260089Seric 74360537Seric if (tTd(38, 9)) 74460207Seric printf("db_map_close(%s, %x)\n", map->map_file, map->map_mflags); 74560089Seric 74660207Seric if (bitset(MF_WRITABLE, map->map_mflags)) 74758804Seric { 74860089Seric /* write out the distinguished alias */ 74960089Seric db_map_store(map, "@", "@"); 75058804Seric } 75158963Seric 75260089Seric if (db->close(db) != 0) 75360089Seric syserr("readaliases: db close failure"); 75456822Seric } 75557208Seric 75660089Seric #endif 75760089Seric /* 75860089Seric ** NIS Modules 75960089Seric */ 76060089Seric 76160089Seric # ifdef NIS 76260089Seric 76357208Seric /* 76460089Seric ** NIS_MAP_OPEN -- open DBM map 76557208Seric */ 76657208Seric 76757208Seric bool 76860089Seric nis_map_open(map, mode) 76957208Seric MAP *map; 77060089Seric int mode; 77157208Seric { 77257216Seric int yperr; 77360215Seric register char *p; 77460215Seric auto char *vp; 77560215Seric auto int vsize; 77657216Seric char *master; 77757216Seric 77860537Seric if (tTd(38, 2)) 77960089Seric printf("nis_map_open(%s)\n", map->map_file); 78060089Seric 78160207Seric if (mode != O_RDONLY) 78260207Seric { 78360207Seric errno = ENODEV; 78460207Seric return FALSE; 78560207Seric } 78660207Seric 78760089Seric p = strchr(map->map_file, '@'); 78860089Seric if (p != NULL) 78960089Seric { 79060089Seric *p++ = '\0'; 79160089Seric if (*p != '\0') 79260089Seric map->map_domain = p; 79360089Seric } 79460215Seric 79560089Seric if (map->map_domain == NULL) 79660089Seric yp_get_default_domain(&map->map_domain); 79760089Seric 79860089Seric if (*map->map_file == '\0') 79960089Seric map->map_file = "mail.aliases"; 80060089Seric 80160215Seric /* check to see if this map actually exists */ 80260089Seric yperr = yp_match(map->map_domain, map->map_file, "@", 1, 80360089Seric &vp, &vsize); 80460537Seric if (tTd(38, 10)) 80560089Seric printf("nis_map_open: yp_match(%s, %s) => %s\n", 80660089Seric map->map_domain, map->map_file, yperr_string(yperr)); 80760089Seric if (yperr == 0 || yperr == YPERR_KEY || yperr == YPERR_BUSY) 80860089Seric return TRUE; 80960215Seric 81060215Seric if (!bitset(MF_OPTIONAL, map->map_mflags)) 81160215Seric syserr("Cannot bind to domain %s: %s", map->map_domain, 81260215Seric yperr_string(yperr)); 81360215Seric 81460089Seric return FALSE; 81560089Seric } 81660089Seric 81760089Seric 81860089Seric /* 81957208Seric ** NIS_MAP_LOOKUP -- look up a datum in a NIS map 82057208Seric */ 82157208Seric 82257208Seric char * 82360089Seric nis_map_lookup(map, name, av, statp) 82457208Seric MAP *map; 82560089Seric char *name; 82657208Seric char **av; 82759084Seric int *statp; 82857208Seric { 82957208Seric char *vp; 83057642Seric auto int vsize; 83159274Seric int buflen; 83260215Seric int yperr; 83360089Seric char keybuf[MAXNAME + 1]; 83457208Seric 83560537Seric if (tTd(38, 20)) 83660089Seric printf("nis_map_lookup(%s)\n", name); 83760089Seric 83860089Seric buflen = strlen(name); 83960089Seric if (buflen > sizeof keybuf - 1) 84060089Seric buflen = sizeof keybuf - 1; 84160089Seric bcopy(name, keybuf, buflen + 1); 84260207Seric if (!bitset(MF_NOFOLDCASE, map->map_mflags)) 84360089Seric makelower(keybuf); 84460207Seric if (bitset(MF_INCLNULL, map->map_mflags)) 84559274Seric buflen++; 84660089Seric yperr = yp_match(map->map_domain, map->map_file, keybuf, buflen, 84760089Seric &vp, &vsize); 84860089Seric if (yperr != 0) 84960089Seric { 85060089Seric if (yperr != YPERR_KEY && yperr != YPERR_BUSY) 85160215Seric map->map_mflags &= ~(MF_VALID|MF_OPEN); 85257208Seric return NULL; 85360089Seric } 85460207Seric if (bitset(MF_MATCHONLY, map->map_mflags)) 85560089Seric av = NULL; 85660215Seric return map_rewrite(map, vp, vsize, av); 85757208Seric } 85857208Seric 85960089Seric 86060089Seric /* 86160207Seric ** NIS_MAP_STORE 86260089Seric */ 86360089Seric 86460089Seric void 86560089Seric nis_map_store(map, lhs, rhs) 86660089Seric MAP *map; 86760089Seric char *lhs; 86860089Seric char *rhs; 86960089Seric { 87060089Seric /* nothing */ 87160089Seric } 87260089Seric 87360089Seric 87460089Seric /* 87560207Seric ** NIS_MAP_CLOSE 87660089Seric */ 87760089Seric 87860089Seric void 87960089Seric nis_map_close(map) 88060089Seric MAP *map; 88160089Seric { 88260089Seric /* nothing */ 88360089Seric } 88460089Seric 88560089Seric #endif /* NIS */ 88657208Seric /* 88760089Seric ** STAB (Symbol Table) Modules 88860089Seric */ 88960089Seric 89060089Seric 89160089Seric /* 89260207Seric ** STAB_MAP_LOOKUP -- look up alias in symbol table 89360089Seric */ 89460089Seric 89560089Seric char * 89660089Seric stab_map_lookup(map, name) 89760089Seric register MAP *map; 89860089Seric char *name; 89960089Seric { 90060089Seric register STAB *s; 90160089Seric 90260537Seric if (tTd(38, 20)) 90360089Seric printf("stab_lookup(%s)\n", name); 90460089Seric 90560089Seric s = stab(name, ST_ALIAS, ST_FIND); 90660089Seric if (s != NULL) 90760089Seric return (s->s_alias); 90860089Seric return (NULL); 90960089Seric } 91060089Seric 91160089Seric 91260089Seric /* 91360207Seric ** STAB_MAP_STORE -- store in symtab (actually using during init, not rebuild) 91460089Seric */ 91560089Seric 91660089Seric void 91760089Seric stab_map_store(map, lhs, rhs) 91860089Seric register MAP *map; 91960089Seric char *lhs; 92060089Seric char *rhs; 92160089Seric { 92260089Seric register STAB *s; 92360089Seric 92460089Seric s = stab(lhs, ST_ALIAS, ST_ENTER); 92560089Seric s->s_alias = newstr(rhs); 92660089Seric } 92760089Seric 92860089Seric 92960089Seric /* 93060207Seric ** STAB_MAP_OPEN -- initialize (reads data file) 93160207Seric ** 93260207Seric ** This is a wierd case -- it is only intended as a fallback for 93360207Seric ** aliases. For this reason, opens for write (only during a 93460207Seric ** "newaliases") always fails, and opens for read open the 93560207Seric ** actual underlying text file instead of the database. 93660089Seric */ 93760089Seric 93860089Seric bool 93960089Seric stab_map_open(map, mode) 94060089Seric register MAP *map; 94160089Seric int mode; 94260089Seric { 94360089Seric FILE *af; 94460089Seric 94560537Seric if (tTd(38, 2)) 94660089Seric printf("stab_map_open(%s)\n", map->map_file); 94760089Seric 94860089Seric if (mode != O_RDONLY) 94960207Seric { 95060207Seric errno = ENODEV; 95160089Seric return FALSE; 95260207Seric } 95360089Seric 95460089Seric return TRUE; 95560089Seric } 95660089Seric 95760089Seric 95860089Seric /* 95960207Seric ** STAB_MAP_CLOSE -- close symbol table (???) 96060089Seric */ 96160089Seric 96260089Seric void 96360089Seric stab_map_close(map) 96460089Seric MAP *map; 96560089Seric { 96660089Seric /* ignore it */ 96760089Seric } 96860089Seric /* 96960089Seric ** Implicit Modules 97056822Seric ** 97160089Seric ** Tries several types. For back compatibility of aliases. 97256822Seric */ 97356822Seric 97460089Seric 97560089Seric /* 97660207Seric ** IMPL_MAP_LOOKUP -- lookup in best open database 97760089Seric */ 97860089Seric 97960089Seric char * 98060089Seric impl_map_lookup(map, name, av, pstat) 98160089Seric MAP *map; 98260089Seric char *name; 98356822Seric char **av; 98460089Seric int *pstat; 98556822Seric { 98660537Seric if (tTd(38, 20)) 98760089Seric printf("impl_map_lookup(%s)\n", name); 98856822Seric 98960089Seric #ifdef NEWDB 99060207Seric if (bitset(MF_IMPL_HASH, map->map_mflags)) 99160089Seric return db_map_lookup(map, name, av, pstat); 99260089Seric #endif 99360089Seric #ifdef NDBM 99460207Seric if (bitset(MF_IMPL_NDBM, map->map_mflags)) 99560089Seric return ndbm_map_lookup(map, name, av, pstat); 99660089Seric #endif 99760089Seric return stab_map_lookup(map, name, av, pstat); 99860089Seric } 99960089Seric 100060089Seric /* 100160207Seric ** IMPL_MAP_STORE -- store in open databases 100260089Seric */ 100360089Seric 100460089Seric void 100560089Seric impl_map_store(map, lhs, rhs) 100660089Seric MAP *map; 100760089Seric char *lhs; 100860089Seric char *rhs; 100960089Seric { 101060089Seric #ifdef NEWDB 101160207Seric if (bitset(MF_IMPL_HASH, map->map_mflags)) 101260089Seric db_map_store(map, lhs, rhs); 101360089Seric #endif 101460089Seric #ifdef NDBM 101560207Seric if (bitset(MF_IMPL_NDBM, map->map_mflags)) 101660089Seric ndbm_map_store(map, lhs, rhs); 101760089Seric #endif 101860089Seric stab_map_store(map, lhs, rhs); 101960089Seric } 102060089Seric 102160089Seric /* 102260089Seric ** IMPL_MAP_OPEN -- implicit database open 102360089Seric */ 102460089Seric 102560089Seric bool 102660089Seric impl_map_open(map, mode) 102760089Seric MAP *map; 102860089Seric int mode; 102960089Seric { 103060089Seric struct stat stb; 103160089Seric 103260537Seric if (tTd(38, 2)) 103360089Seric printf("impl_map_open(%s)\n", map->map_file); 103460089Seric 103560089Seric if (stat(map->map_file, &stb) < 0) 103656822Seric { 103760089Seric /* no alias file at all */ 103860089Seric return FALSE; 103956822Seric } 104056822Seric 104160089Seric #ifdef NEWDB 104260207Seric map->map_mflags |= MF_IMPL_HASH; 104360089Seric if (hash_map_open(map, mode)) 104456822Seric { 104560207Seric #if defined(NDBM) && defined(YPCOMPAT) 104660561Seric if (mode == O_RDONLY || access("/var/yp/Makefile", R_OK) != 0) 104760207Seric #endif 104860207Seric return TRUE; 104960089Seric } 105060207Seric else 105160207Seric map->map_mflags &= ~MF_IMPL_HASH; 105260089Seric #endif 105360089Seric #ifdef NDBM 105460207Seric map->map_mflags |= MF_IMPL_NDBM; 105560089Seric if (ndbm_map_open(map, mode)) 105660089Seric { 105760089Seric return TRUE; 105860089Seric } 105960207Seric else 106060207Seric map->map_mflags &= ~MF_IMPL_NDBM; 106160089Seric #endif 106256822Seric 106360207Seric #if !defined(NEWDB) && !defined(NDBM) 106460089Seric if (Verbose) 106560089Seric message("WARNING: cannot open alias database %s", map->map_file); 106660207Seric #endif 106760089Seric 106860207Seric return stab_map_open(map, mode); 106956822Seric } 107060089Seric 107160207Seric 107260089Seric /* 107360207Seric ** IMPL_MAP_CLOSE -- close any open database(s) 107460089Seric */ 107560089Seric 107660089Seric void 107760207Seric impl_map_close(map) 107860089Seric MAP *map; 107960089Seric { 108060089Seric #ifdef NEWDB 108160207Seric if (bitset(MF_IMPL_HASH, map->map_mflags)) 108260089Seric { 108360207Seric db_map_close(map); 108460207Seric map->map_mflags &= ~MF_IMPL_HASH; 108560089Seric } 108660089Seric #endif 108760089Seric 108860089Seric #ifdef NDBM 108960207Seric if (bitset(MF_IMPL_NDBM, map->map_mflags)) 109060089Seric { 109160207Seric ndbm_map_close(map); 109260207Seric map->map_mflags &= ~MF_IMPL_NDBM; 109360089Seric } 109460089Seric #endif 109560089Seric } 109660207Seric /* 109760207Seric ** NULL stubs 109860089Seric */ 109960089Seric 110060207Seric bool 110160207Seric null_map_open(map, mode) 110260089Seric MAP *map; 110360207Seric int mode; 110460089Seric { 110560207Seric return TRUE; 110660089Seric } 110760089Seric 110860207Seric void 110960207Seric null_map_close(map) 111060207Seric MAP *map; 111160089Seric { 111260207Seric return; 111360207Seric } 111460089Seric 111560207Seric void 111660207Seric null_map_store(map, key, val) 111760207Seric MAP *map; 111860207Seric char *key; 111960207Seric char *val; 112060089Seric { 112160207Seric return; 112260089Seric } 1123