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*60582Seric static char sccsid[] = "@(#)map.c 6.24 (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 /* 501*60582Seric ** NEWDB (Hash and BTree) Modules 50260089Seric */ 50360089Seric 50460089Seric #ifdef NEWDB 50560089Seric 50660089Seric /* 507*60582Seric ** BT_MAP_OPEN, HASH_MAP_OPEN -- database open primitives. 508*60582Seric ** 509*60582Seric ** These do rather bizarre locking. If you can lock on open, 510*60582Seric ** do that to avoid the condition of opening a database that 511*60582Seric ** is being rebuilt. If you don't, we'll try to fake it, but 512*60582Seric ** there will be a race condition. If opening for read-only, 513*60582Seric ** we immediately release the lock to avoid freezing things up. 514*60582Seric ** We really ought to hold the lock, but guarantee that we won't 515*60582Seric ** 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; 525*60582Seric 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 531*60582Seric omode = mode; 532*60582Seric if (omode == O_RDWR) 533*60582Seric { 534*60582Seric omode |= O_CREAT|O_TRUNC; 535*60582Seric #if defined(O_EXLOCK) && !defined(LOCKF) 536*60582Seric omode |= O_EXLOCK; 537*60582Seric # if !defined(OLD_NEWDB) 538*60582Seric } 539*60582Seric else 540*60582Seric { 541*60582Seric omode |= O_SHLOCK; 542*60582Seric # endif 543*60582Seric #endif 544*60582Seric } 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"); 550*60582Seric 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 } 557*60582Seric #if !defined(OLD_NEWDB) && !defined(LOCKF) 558*60582Seric # if !defined(O_EXLOCK) 559*60582Seric if (mode == O_RDWR) 560*60582Seric (void) lockfile(db->fd(db), map->map_file, LOCK_EX); 561*60582Seric # else 562*60582Seric if (mode == O_RDONLY) 563*60582Seric (void) lockfile(db->fd(db), map->map_file, LOCK_UN); 564*60582Seric # endif 565*60582Seric #endif 56660089Seric map->map_db2 = (void *) db; 56760207Seric if (mode == O_RDONLY && bitset(MF_ALIAS, map->map_mflags)) 56860207Seric aliaswait(map, ".db"); 56956822Seric return TRUE; 57056822Seric } 57156822Seric 57256822Seric 57356822Seric /* 57456822Seric ** HASH_MAP_INIT -- HASH-style map initialization 57556822Seric */ 57656822Seric 57756822Seric bool 57860089Seric hash_map_open(map, mode) 57956822Seric MAP *map; 58060089Seric int mode; 58156822Seric { 58256822Seric DB *db; 58360228Seric int i; 584*60582Seric int omode; 58560089Seric char buf[MAXNAME]; 58656822Seric 58760537Seric if (tTd(38, 2)) 58860089Seric printf("hash_map_open(%s, %d)\n", map->map_file, mode); 58960089Seric 590*60582Seric omode = mode; 591*60582Seric if (omode == O_RDWR) 592*60582Seric { 593*60582Seric omode |= O_CREAT|O_TRUNC; 594*60582Seric #if defined(O_EXLOCK) && !defined(LOCKF) 595*60582Seric omode |= O_EXLOCK; 596*60582Seric # if !defined(OLD_NEWDB) 597*60582Seric } 598*60582Seric else 599*60582Seric { 600*60582Seric omode |= O_SHLOCK; 601*60582Seric # endif 602*60582Seric #endif 603*60582Seric } 60460207Seric 60560228Seric (void) strcpy(buf, map->map_file); 60660228Seric i = strlen(buf); 60760228Seric if (i < 3 || strcmp(&buf[i - 3], ".db") != 0) 60860228Seric (void) strcat(buf, ".db"); 609*60582Seric db = dbopen(buf, omode, DBMMODE, DB_HASH, NULL); 61056822Seric if (db == NULL) 61156822Seric { 61260207Seric if (!bitset(MF_OPTIONAL, map->map_mflags)) 61356836Seric syserr("Cannot open HASH database %s", map->map_file); 61456822Seric return FALSE; 61556822Seric } 616*60582Seric #if !defined(OLD_NEWDB) && !defined(LOCKF) 617*60582Seric # if !defined(O_EXLOCK) 618*60582Seric if (mode == O_RDWR) 619*60582Seric (void) lockfile(db->fd(db), map->map_file, LOCK_EX); 620*60582Seric # else 621*60582Seric if (mode == O_RDONLY) 622*60582Seric (void) lockfile(db->fd(db), map->map_file, LOCK_UN); 623*60582Seric # endif 624*60582Seric #endif 62560089Seric map->map_db2 = (void *) db; 62660207Seric if (mode == O_RDONLY && bitset(MF_ALIAS, map->map_mflags)) 62760207Seric aliaswait(map, ".db"); 62856822Seric return TRUE; 62956822Seric } 63056822Seric 63156822Seric 63256822Seric /* 63356822Seric ** DB_MAP_LOOKUP -- look up a datum in a BTREE- or HASH-type map 63456822Seric */ 63556822Seric 63656822Seric char * 63760089Seric db_map_lookup(map, name, av, statp) 63856822Seric MAP *map; 63960089Seric char *name; 64056822Seric char **av; 64159084Seric int *statp; 64256822Seric { 64356822Seric DBT key, val; 64460422Seric register DB *db = (DB *) map->map_db2; 64560422Seric int st; 64660422Seric int saveerrno; 64760089Seric char keybuf[MAXNAME + 1]; 64856822Seric 64960537Seric if (tTd(38, 20)) 65060089Seric printf("db_map_lookup(%s)\n", name); 65160089Seric 65260089Seric key.size = strlen(name); 65360089Seric if (key.size > sizeof keybuf - 1) 65460089Seric key.size = sizeof keybuf - 1; 65560089Seric key.data = keybuf; 65660089Seric bcopy(name, keybuf, key.size + 1); 65760207Seric if (!bitset(MF_NOFOLDCASE, map->map_mflags)) 65860089Seric makelower(keybuf); 65960207Seric if (bitset(MF_INCLNULL, map->map_mflags)) 66056822Seric key.size++; 66160422Seric #ifndef OLD_NEWDB 66260422Seric (void) lockfile(db->fd(db), map->map_file, LOCK_SH); 66360422Seric #endif 66460422Seric st = db->get(db, &key, &val, 0); 66560422Seric saveerrno = errno; 66660422Seric #ifndef OLD_NEWDB 66760422Seric (void) lockfile(db->fd(db), map->map_file, LOCK_UN); 66860422Seric #endif 66960422Seric if (st != 0) 67060422Seric { 67160422Seric errno = saveerrno; 67260422Seric if (st < 0) 67360422Seric syserr("db_map_lookup: get (%s)", name); 67456822Seric return NULL; 67560422Seric } 67660207Seric if (bitset(MF_MATCHONLY, map->map_mflags)) 67760089Seric av = NULL; 67860089Seric return map_rewrite(map, val.data, val.size, av); 67956822Seric } 68056822Seric 68160089Seric 68260089Seric /* 68360089Seric ** DB_MAP_STORE -- store a datum in the NEWDB database 68456822Seric */ 68556822Seric 68660089Seric void 68760089Seric db_map_store(map, lhs, rhs) 68860089Seric register MAP *map; 68960089Seric char *lhs; 69060089Seric char *rhs; 69156822Seric { 69260089Seric int stat; 69360089Seric DBT key; 69460089Seric DBT data; 69560089Seric register DB *db = map->map_db2; 69656822Seric 69760537Seric if (tTd(38, 20)) 69860089Seric printf("db_map_store(%s, %s)\n", lhs, rhs); 69960089Seric 70060089Seric key.size = strlen(lhs); 70160089Seric key.data = lhs; 70260089Seric 70360089Seric data.size = strlen(rhs); 70460089Seric data.data = rhs; 70560089Seric 70660207Seric if (bitset(MF_INCLNULL, map->map_mflags)) 70756822Seric { 70860089Seric key.size++; 70960089Seric data.size++; 71060089Seric } 71156836Seric 71260089Seric stat = db->put(db, &key, &data, R_NOOVERWRITE); 71360089Seric if (stat > 0) 71460089Seric { 71560089Seric usrerr("050 Warning: duplicate alias name %s", lhs); 71660089Seric stat = db->put(db, &key, &data, 0); 71760089Seric } 71860089Seric if (stat != 0) 71960089Seric syserr("readaliases: db put (%s)", lhs); 72060089Seric } 72156836Seric 72256847Seric 72360089Seric /* 72460089Seric ** DB_MAP_CLOSE -- add distinguished entries and close the database 72560089Seric */ 72660089Seric 72760089Seric void 72860089Seric db_map_close(map) 72960089Seric MAP *map; 73060089Seric { 73160089Seric register DB *db = map->map_db2; 73260089Seric 73360537Seric if (tTd(38, 9)) 73460207Seric printf("db_map_close(%s, %x)\n", map->map_file, map->map_mflags); 73560089Seric 73660207Seric if (bitset(MF_WRITABLE, map->map_mflags)) 73758804Seric { 73860089Seric /* write out the distinguished alias */ 73960089Seric db_map_store(map, "@", "@"); 74058804Seric } 74158963Seric 74260089Seric if (db->close(db) != 0) 74360089Seric syserr("readaliases: db close failure"); 74456822Seric } 74557208Seric 74660089Seric #endif 74760089Seric /* 74860089Seric ** NIS Modules 74960089Seric */ 75060089Seric 75160089Seric # ifdef NIS 75260089Seric 75357208Seric /* 75460089Seric ** NIS_MAP_OPEN -- open DBM map 75557208Seric */ 75657208Seric 75757208Seric bool 75860089Seric nis_map_open(map, mode) 75957208Seric MAP *map; 76060089Seric int mode; 76157208Seric { 76257216Seric int yperr; 76360215Seric register char *p; 76460215Seric auto char *vp; 76560215Seric auto int vsize; 76657216Seric char *master; 76757216Seric 76860537Seric if (tTd(38, 2)) 76960089Seric printf("nis_map_open(%s)\n", map->map_file); 77060089Seric 77160207Seric if (mode != O_RDONLY) 77260207Seric { 77360207Seric errno = ENODEV; 77460207Seric return FALSE; 77560207Seric } 77660207Seric 77760089Seric p = strchr(map->map_file, '@'); 77860089Seric if (p != NULL) 77960089Seric { 78060089Seric *p++ = '\0'; 78160089Seric if (*p != '\0') 78260089Seric map->map_domain = p; 78360089Seric } 78460215Seric 78560089Seric if (map->map_domain == NULL) 78660089Seric yp_get_default_domain(&map->map_domain); 78760089Seric 78860089Seric if (*map->map_file == '\0') 78960089Seric map->map_file = "mail.aliases"; 79060089Seric 79160215Seric /* check to see if this map actually exists */ 79260089Seric yperr = yp_match(map->map_domain, map->map_file, "@", 1, 79360089Seric &vp, &vsize); 79460537Seric if (tTd(38, 10)) 79560089Seric printf("nis_map_open: yp_match(%s, %s) => %s\n", 79660089Seric map->map_domain, map->map_file, yperr_string(yperr)); 79760089Seric if (yperr == 0 || yperr == YPERR_KEY || yperr == YPERR_BUSY) 79860089Seric return TRUE; 79960215Seric 80060215Seric if (!bitset(MF_OPTIONAL, map->map_mflags)) 80160215Seric syserr("Cannot bind to domain %s: %s", map->map_domain, 80260215Seric yperr_string(yperr)); 80360215Seric 80460089Seric return FALSE; 80560089Seric } 80660089Seric 80760089Seric 80860089Seric /* 80957208Seric ** NIS_MAP_LOOKUP -- look up a datum in a NIS map 81057208Seric */ 81157208Seric 81257208Seric char * 81360089Seric nis_map_lookup(map, name, av, statp) 81457208Seric MAP *map; 81560089Seric char *name; 81657208Seric char **av; 81759084Seric int *statp; 81857208Seric { 81957208Seric char *vp; 82057642Seric auto int vsize; 82159274Seric int buflen; 82260215Seric int yperr; 82360089Seric char keybuf[MAXNAME + 1]; 82457208Seric 82560537Seric if (tTd(38, 20)) 82660089Seric printf("nis_map_lookup(%s)\n", name); 82760089Seric 82860089Seric buflen = strlen(name); 82960089Seric if (buflen > sizeof keybuf - 1) 83060089Seric buflen = sizeof keybuf - 1; 83160089Seric bcopy(name, keybuf, buflen + 1); 83260207Seric if (!bitset(MF_NOFOLDCASE, map->map_mflags)) 83360089Seric makelower(keybuf); 83460207Seric if (bitset(MF_INCLNULL, map->map_mflags)) 83559274Seric buflen++; 83660089Seric yperr = yp_match(map->map_domain, map->map_file, keybuf, buflen, 83760089Seric &vp, &vsize); 83860089Seric if (yperr != 0) 83960089Seric { 84060089Seric if (yperr != YPERR_KEY && yperr != YPERR_BUSY) 84160215Seric map->map_mflags &= ~(MF_VALID|MF_OPEN); 84257208Seric return NULL; 84360089Seric } 84460207Seric if (bitset(MF_MATCHONLY, map->map_mflags)) 84560089Seric av = NULL; 84660215Seric return map_rewrite(map, vp, vsize, av); 84757208Seric } 84857208Seric 84960089Seric 85060089Seric /* 85160207Seric ** NIS_MAP_STORE 85260089Seric */ 85360089Seric 85460089Seric void 85560089Seric nis_map_store(map, lhs, rhs) 85660089Seric MAP *map; 85760089Seric char *lhs; 85860089Seric char *rhs; 85960089Seric { 86060089Seric /* nothing */ 86160089Seric } 86260089Seric 86360089Seric 86460089Seric /* 86560207Seric ** NIS_MAP_CLOSE 86660089Seric */ 86760089Seric 86860089Seric void 86960089Seric nis_map_close(map) 87060089Seric MAP *map; 87160089Seric { 87260089Seric /* nothing */ 87360089Seric } 87460089Seric 87560089Seric #endif /* NIS */ 87657208Seric /* 87760089Seric ** STAB (Symbol Table) Modules 87860089Seric */ 87960089Seric 88060089Seric 88160089Seric /* 88260207Seric ** STAB_MAP_LOOKUP -- look up alias in symbol table 88360089Seric */ 88460089Seric 88560089Seric char * 88660089Seric stab_map_lookup(map, name) 88760089Seric register MAP *map; 88860089Seric char *name; 88960089Seric { 89060089Seric register STAB *s; 89160089Seric 89260537Seric if (tTd(38, 20)) 89360089Seric printf("stab_lookup(%s)\n", name); 89460089Seric 89560089Seric s = stab(name, ST_ALIAS, ST_FIND); 89660089Seric if (s != NULL) 89760089Seric return (s->s_alias); 89860089Seric return (NULL); 89960089Seric } 90060089Seric 90160089Seric 90260089Seric /* 90360207Seric ** STAB_MAP_STORE -- store in symtab (actually using during init, not rebuild) 90460089Seric */ 90560089Seric 90660089Seric void 90760089Seric stab_map_store(map, lhs, rhs) 90860089Seric register MAP *map; 90960089Seric char *lhs; 91060089Seric char *rhs; 91160089Seric { 91260089Seric register STAB *s; 91360089Seric 91460089Seric s = stab(lhs, ST_ALIAS, ST_ENTER); 91560089Seric s->s_alias = newstr(rhs); 91660089Seric } 91760089Seric 91860089Seric 91960089Seric /* 92060207Seric ** STAB_MAP_OPEN -- initialize (reads data file) 92160207Seric ** 92260207Seric ** This is a wierd case -- it is only intended as a fallback for 92360207Seric ** aliases. For this reason, opens for write (only during a 92460207Seric ** "newaliases") always fails, and opens for read open the 92560207Seric ** actual underlying text file instead of the database. 92660089Seric */ 92760089Seric 92860089Seric bool 92960089Seric stab_map_open(map, mode) 93060089Seric register MAP *map; 93160089Seric int mode; 93260089Seric { 93360089Seric FILE *af; 93460089Seric 93560537Seric if (tTd(38, 2)) 93660089Seric printf("stab_map_open(%s)\n", map->map_file); 93760089Seric 93860089Seric if (mode != O_RDONLY) 93960207Seric { 94060207Seric errno = ENODEV; 94160089Seric return FALSE; 94260207Seric } 94360089Seric 94460089Seric return TRUE; 94560089Seric } 94660089Seric 94760089Seric 94860089Seric /* 94960207Seric ** STAB_MAP_CLOSE -- close symbol table (???) 95060089Seric */ 95160089Seric 95260089Seric void 95360089Seric stab_map_close(map) 95460089Seric MAP *map; 95560089Seric { 95660089Seric /* ignore it */ 95760089Seric } 95860089Seric /* 95960089Seric ** Implicit Modules 96056822Seric ** 96160089Seric ** Tries several types. For back compatibility of aliases. 96256822Seric */ 96356822Seric 96460089Seric 96560089Seric /* 96660207Seric ** IMPL_MAP_LOOKUP -- lookup in best open database 96760089Seric */ 96860089Seric 96960089Seric char * 97060089Seric impl_map_lookup(map, name, av, pstat) 97160089Seric MAP *map; 97260089Seric char *name; 97356822Seric char **av; 97460089Seric int *pstat; 97556822Seric { 97660537Seric if (tTd(38, 20)) 97760089Seric printf("impl_map_lookup(%s)\n", name); 97856822Seric 97960089Seric #ifdef NEWDB 98060207Seric if (bitset(MF_IMPL_HASH, map->map_mflags)) 98160089Seric return db_map_lookup(map, name, av, pstat); 98260089Seric #endif 98360089Seric #ifdef NDBM 98460207Seric if (bitset(MF_IMPL_NDBM, map->map_mflags)) 98560089Seric return ndbm_map_lookup(map, name, av, pstat); 98660089Seric #endif 98760089Seric return stab_map_lookup(map, name, av, pstat); 98860089Seric } 98960089Seric 99060089Seric /* 99160207Seric ** IMPL_MAP_STORE -- store in open databases 99260089Seric */ 99360089Seric 99460089Seric void 99560089Seric impl_map_store(map, lhs, rhs) 99660089Seric MAP *map; 99760089Seric char *lhs; 99860089Seric char *rhs; 99960089Seric { 100060089Seric #ifdef NEWDB 100160207Seric if (bitset(MF_IMPL_HASH, map->map_mflags)) 100260089Seric db_map_store(map, lhs, rhs); 100360089Seric #endif 100460089Seric #ifdef NDBM 100560207Seric if (bitset(MF_IMPL_NDBM, map->map_mflags)) 100660089Seric ndbm_map_store(map, lhs, rhs); 100760089Seric #endif 100860089Seric stab_map_store(map, lhs, rhs); 100960089Seric } 101060089Seric 101160089Seric /* 101260089Seric ** IMPL_MAP_OPEN -- implicit database open 101360089Seric */ 101460089Seric 101560089Seric bool 101660089Seric impl_map_open(map, mode) 101760089Seric MAP *map; 101860089Seric int mode; 101960089Seric { 102060089Seric struct stat stb; 102160089Seric 102260537Seric if (tTd(38, 2)) 102360089Seric printf("impl_map_open(%s)\n", map->map_file); 102460089Seric 102560089Seric if (stat(map->map_file, &stb) < 0) 102656822Seric { 102760089Seric /* no alias file at all */ 102860089Seric return FALSE; 102956822Seric } 103056822Seric 103160089Seric #ifdef NEWDB 103260207Seric map->map_mflags |= MF_IMPL_HASH; 103360089Seric if (hash_map_open(map, mode)) 103456822Seric { 103560207Seric #if defined(NDBM) && defined(YPCOMPAT) 103660561Seric if (mode == O_RDONLY || access("/var/yp/Makefile", R_OK) != 0) 103760207Seric #endif 103860207Seric return TRUE; 103960089Seric } 104060207Seric else 104160207Seric map->map_mflags &= ~MF_IMPL_HASH; 104260089Seric #endif 104360089Seric #ifdef NDBM 104460207Seric map->map_mflags |= MF_IMPL_NDBM; 104560089Seric if (ndbm_map_open(map, mode)) 104660089Seric { 104760089Seric return TRUE; 104860089Seric } 104960207Seric else 105060207Seric map->map_mflags &= ~MF_IMPL_NDBM; 105160089Seric #endif 105256822Seric 105360207Seric #if !defined(NEWDB) && !defined(NDBM) 105460089Seric if (Verbose) 105560089Seric message("WARNING: cannot open alias database %s", map->map_file); 105660207Seric #endif 105760089Seric 105860207Seric return stab_map_open(map, mode); 105956822Seric } 106060089Seric 106160207Seric 106260089Seric /* 106360207Seric ** IMPL_MAP_CLOSE -- close any open database(s) 106460089Seric */ 106560089Seric 106660089Seric void 106760207Seric impl_map_close(map) 106860089Seric MAP *map; 106960089Seric { 107060089Seric #ifdef NEWDB 107160207Seric if (bitset(MF_IMPL_HASH, map->map_mflags)) 107260089Seric { 107360207Seric db_map_close(map); 107460207Seric map->map_mflags &= ~MF_IMPL_HASH; 107560089Seric } 107660089Seric #endif 107760089Seric 107860089Seric #ifdef NDBM 107960207Seric if (bitset(MF_IMPL_NDBM, map->map_mflags)) 108060089Seric { 108160207Seric ndbm_map_close(map); 108260207Seric map->map_mflags &= ~MF_IMPL_NDBM; 108360089Seric } 108460089Seric #endif 108560089Seric } 108660207Seric /* 108760207Seric ** NULL stubs 108860089Seric */ 108960089Seric 109060207Seric bool 109160207Seric null_map_open(map, mode) 109260089Seric MAP *map; 109360207Seric int mode; 109460089Seric { 109560207Seric return TRUE; 109660089Seric } 109760089Seric 109860207Seric void 109960207Seric null_map_close(map) 110060207Seric MAP *map; 110160089Seric { 110260207Seric return; 110360207Seric } 110460089Seric 110560207Seric void 110660207Seric null_map_store(map, key, val) 110760207Seric MAP *map; 110860207Seric char *key; 110960207Seric char *val; 111060089Seric { 111160207Seric return; 111260089Seric } 1113