156822Seric /* 256822Seric * Copyright (c) 1992 Eric P. Allman. 362526Sbostic * Copyright (c) 1992, 1993 462526Sbostic * The Regents of the University of California. All rights reserved. 556822Seric * 656822Seric * %sccs.include.redist.c% 756822Seric */ 856822Seric 956822Seric #ifndef lint 10*64369Seric static char sccsid[] = "@(#)map.c 8.10 (Berkeley) 08/28/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 8663753Seric map->map_mflags |= MF_TRY0NULL | MF_TRY1NULL; 8760089Seric for (;;) 8860089Seric { 8960089Seric while (isascii(*p) && isspace(*p)) 9060089Seric p++; 9160089Seric if (*p != '-') 9260089Seric break; 9360089Seric switch (*++p) 9460089Seric { 9560089Seric case 'N': 9660207Seric map->map_mflags |= MF_INCLNULL; 9763753Seric map->map_mflags &= ~MF_TRY0NULL; 9860089Seric break; 9960089Seric 10063753Seric case 'O': 10163753Seric map->map_mflags &= ~MF_TRY1NULL; 10263753Seric break; 10363753Seric 10460089Seric case 'o': 10560207Seric map->map_mflags |= MF_OPTIONAL; 10660089Seric break; 10760089Seric 10860089Seric case 'f': 10960207Seric map->map_mflags |= MF_NOFOLDCASE; 11060089Seric break; 11160089Seric 11260089Seric case 'm': 11360207Seric map->map_mflags |= MF_MATCHONLY; 11460089Seric break; 11560089Seric 11660089Seric case 'a': 11760089Seric map->map_app = ++p; 11860089Seric break; 11960089Seric } 12060089Seric while (*p != '\0' && !(isascii(*p) && isspace(*p))) 12160089Seric p++; 12260089Seric if (*p != '\0') 12360089Seric *p++ = '\0'; 12460089Seric } 12560089Seric if (map->map_app != NULL) 12660089Seric map->map_app = newstr(map->map_app); 12760089Seric 12860089Seric if (*p != '\0') 12960089Seric { 13060089Seric map->map_file = p; 13160089Seric while (*p != '\0' && !(isascii(*p) && isspace(*p))) 13260089Seric p++; 13360089Seric if (*p != '\0') 13460089Seric *p++ = '\0'; 13560089Seric map->map_file = newstr(map->map_file); 13660089Seric } 13760089Seric 13860089Seric while (*p != '\0' && isascii(*p) && isspace(*p)) 13960089Seric p++; 14060089Seric if (*p != '\0') 14160089Seric map->map_rebuild = newstr(p); 14260089Seric 14356822Seric if (map->map_file == NULL) 14457208Seric { 14560089Seric syserr("No file name for %s map %s", 14660089Seric map->map_class->map_cname, map->map_mname); 14756822Seric return FALSE; 14857208Seric } 14960089Seric return TRUE; 15060089Seric } 15160089Seric /* 15260089Seric ** MAP_REWRITE -- rewrite a database key, interpolating %n indications. 15360089Seric ** 15460089Seric ** It also adds the map_app string. It can be used as a utility 15560089Seric ** in the map_lookup method. 15660089Seric ** 15760089Seric ** Parameters: 15860089Seric ** map -- the map that causes this. 15960089Seric ** s -- the string to rewrite, NOT necessarily null terminated. 16060089Seric ** slen -- the length of s. 16160089Seric ** av -- arguments to interpolate into buf. 16260089Seric ** 16360089Seric ** Returns: 16460089Seric ** Pointer to rewritten result. 16560089Seric ** 16660089Seric ** Side Effects: 16760089Seric ** none. 16860089Seric */ 16960089Seric 17060492Seric struct rwbuf 17160492Seric { 17260492Seric int rwb_len; /* size of buffer */ 17360492Seric char *rwb_buf; /* ptr to buffer */ 17460492Seric }; 17560492Seric 17660492Seric struct rwbuf RwBufs[2]; /* buffers for rewriting output */ 17760492Seric 17860089Seric char * 17960089Seric map_rewrite(map, s, slen, av) 18060089Seric register MAP *map; 18160089Seric register char *s; 18260089Seric int slen; 18360089Seric char **av; 18460089Seric { 18560089Seric register char *bp; 18660089Seric register char c; 18760089Seric char **avp; 18860089Seric register char *ap; 18960492Seric register struct rwbuf *rwb; 19060089Seric int i; 19160089Seric int len; 19260089Seric 19360537Seric if (tTd(39, 1)) 19460089Seric { 19560256Seric printf("map_rewrite(%.*s), av =", slen, s); 19660256Seric if (av == NULL) 19760256Seric printf(" (nullv)"); 19860256Seric else 19960256Seric { 20060256Seric for (avp = av; *avp != NULL; avp++) 20160256Seric printf("\n\t%s", *avp); 20260256Seric } 20360256Seric printf("\n"); 20460089Seric } 20560089Seric 20660492Seric rwb = RwBufs; 20760492Seric if (av == NULL) 20860492Seric rwb++; 20960492Seric 21060089Seric /* count expected size of output (can safely overestimate) */ 21160089Seric i = len = slen; 21260089Seric if (av != NULL) 21360089Seric { 21460089Seric bp = s; 21560089Seric for (i = slen; --i >= 0 && (c = *bp++) != 0; ) 21660089Seric { 21760089Seric if (c != '%') 21860089Seric continue; 21960089Seric if (--i < 0) 22060089Seric break; 22160089Seric c = *bp++; 22260089Seric if (!(isascii(c) && isdigit(c))) 22360089Seric continue; 22463937Seric for (avp = av; --c >= '0' && *avp != NULL; avp++) 22560089Seric continue; 22660089Seric if (*avp == NULL) 22760089Seric continue; 22860089Seric len += strlen(*avp); 22960089Seric } 23060089Seric } 23160089Seric if (map->map_app != NULL) 23260089Seric len += strlen(map->map_app); 23360492Seric if (rwb->rwb_len < ++len) 23460089Seric { 23560089Seric /* need to malloc additional space */ 23660492Seric rwb->rwb_len = len; 23760492Seric if (rwb->rwb_buf != NULL) 23860492Seric free(rwb->rwb_buf); 23960492Seric rwb->rwb_buf = xalloc(rwb->rwb_len); 24060089Seric } 24160089Seric 24260492Seric bp = rwb->rwb_buf; 24360089Seric if (av == NULL) 24460089Seric { 24560089Seric bcopy(s, bp, slen); 24660089Seric bp += slen; 24760089Seric } 24860089Seric else 24960089Seric { 25060089Seric while (--slen >= 0 && (c = *s++) != '\0') 25160089Seric { 25260089Seric if (c != '%') 25360089Seric { 25460089Seric pushc: 25560089Seric *bp++ = c; 25660089Seric continue; 25760089Seric } 25860089Seric if (--slen < 0 || (c = *s++) == '\0') 25960089Seric c = '%'; 26060089Seric if (c == '%') 26160089Seric goto pushc; 26260089Seric if (!(isascii(c) && isdigit(c))) 26360089Seric { 26460089Seric *bp++ = '%'; 26560089Seric goto pushc; 26660089Seric } 26763937Seric for (avp = av; --c >= '0' && *avp != NULL; avp++) 26860089Seric continue; 26960089Seric if (*avp == NULL) 27060089Seric continue; 27160089Seric 27260089Seric /* transliterate argument into output string */ 27360089Seric for (ap = *avp; (c = *ap++) != '\0'; ) 27460089Seric *bp++ = c; 27560089Seric } 27660089Seric } 27760089Seric if (map->map_app != NULL) 27860089Seric strcpy(bp, map->map_app); 27960089Seric else 28060089Seric *bp = '\0'; 28160537Seric if (tTd(39, 1)) 28260492Seric printf("map_rewrite => %s\n", rwb->rwb_buf); 28360492Seric return rwb->rwb_buf; 28460089Seric } 28560089Seric /* 28660537Seric ** INITMAPS -- initialize for aliasing 28760537Seric ** 28860537Seric ** Parameters: 28960537Seric ** rebuild -- if TRUE, this rebuilds the cached versions. 29060537Seric ** e -- current envelope. 29160537Seric ** 29260537Seric ** Returns: 29360537Seric ** none. 29460537Seric ** 29560537Seric ** Side Effects: 29660537Seric ** initializes aliases: 29760537Seric ** if NDBM: opens the database. 29860537Seric ** if ~NDBM: reads the aliases into the symbol table. 29960537Seric */ 30060537Seric 30160537Seric initmaps(rebuild, e) 30260537Seric bool rebuild; 30360537Seric register ENVELOPE *e; 30460537Seric { 30560537Seric extern void map_init(); 30660537Seric 30760537Seric CurEnv = e; 30860537Seric stabapply(map_init, rebuild); 30960537Seric } 31060537Seric 31160537Seric void 31260537Seric map_init(s, rebuild) 31360537Seric register STAB *s; 31460537Seric int rebuild; 31560537Seric { 31660537Seric register MAP *map; 31760537Seric 31860537Seric /* has to be a map */ 31960537Seric if (s->s_type != ST_MAP) 32060537Seric return; 32160537Seric 32260537Seric map = &s->s_map; 32360537Seric if (!bitset(MF_VALID, map->map_mflags)) 32460537Seric return; 32560537Seric 32660537Seric if (tTd(38, 2)) 32760537Seric printf("map_init(%s:%s)\n", 32860537Seric map->map_class->map_cname, map->map_file); 32960537Seric 33060537Seric /* if already open, close it (for nested open) */ 33160537Seric if (bitset(MF_OPEN, map->map_mflags)) 33260537Seric { 33360537Seric map->map_class->map_close(map); 33460537Seric map->map_mflags &= ~(MF_OPEN|MF_WRITABLE); 33560537Seric } 33660537Seric 33760537Seric if (rebuild) 33860537Seric { 33960926Seric if (bitset(MF_ALIAS, map->map_mflags) && 34060926Seric bitset(MCF_REBUILDABLE, map->map_class->map_cflags)) 34160537Seric rebuildaliases(map, FALSE); 34260537Seric } 34360537Seric else 34460537Seric { 34560537Seric if (map->map_class->map_open(map, O_RDONLY)) 34660537Seric { 34760537Seric if (tTd(38, 4)) 34860537Seric printf("%s:%s: valid\n", 34960537Seric map->map_class->map_cname, 35060537Seric map->map_file); 35160537Seric map->map_mflags |= MF_OPEN; 35260537Seric } 35360537Seric else if (tTd(38, 4)) 35460537Seric printf("%s:%s: invalid: %s\n", 35560537Seric map->map_class->map_cname, 35660537Seric map->map_file, 35760537Seric errstring(errno)); 35860537Seric } 35960537Seric } 36060537Seric /* 36160089Seric ** NDBM modules 36260089Seric */ 36360089Seric 36460089Seric #ifdef NDBM 36560089Seric 36660089Seric /* 36760089Seric ** DBM_MAP_OPEN -- DBM-style map open 36860089Seric */ 36960089Seric 37060089Seric bool 37160089Seric ndbm_map_open(map, mode) 37260089Seric MAP *map; 37360089Seric int mode; 37460089Seric { 37564284Seric register DBM *dbm; 37664284Seric struct stat st; 37760089Seric 37860537Seric if (tTd(38, 2)) 37960089Seric printf("ndbm_map_open(%s, %d)\n", map->map_file, mode); 38060089Seric 38160207Seric if (mode == O_RDWR) 38260207Seric mode |= O_CREAT|O_TRUNC; 38360207Seric 38460089Seric /* open the database */ 38560089Seric dbm = dbm_open(map->map_file, mode, DBMMODE); 38656822Seric if (dbm == NULL) 38756822Seric { 38860207Seric if (!bitset(MF_OPTIONAL, map->map_mflags)) 38956836Seric syserr("Cannot open DBM database %s", map->map_file); 39056822Seric return FALSE; 39156822Seric } 39260089Seric map->map_db1 = (void *) dbm; 39360207Seric if (mode == O_RDONLY && bitset(MF_ALIAS, map->map_mflags)) 39460604Seric aliaswait(map, ".pag"); 39564284Seric if (fstat(dbm_dirfno(dbm), &st) >= 0) 39664284Seric map->map_mtime = st.st_mtime; 39756822Seric return TRUE; 39856822Seric } 39960089Seric 40060089Seric 40160089Seric /* 40256822Seric ** DBM_MAP_LOOKUP -- look up a datum in a DBM-type map 40356822Seric */ 40456822Seric 40556822Seric char * 40660089Seric ndbm_map_lookup(map, name, av, statp) 40756822Seric MAP *map; 40860089Seric char *name; 40956822Seric char **av; 41059084Seric int *statp; 41156822Seric { 41256822Seric datum key, val; 41360089Seric char keybuf[MAXNAME + 1]; 41456822Seric 41560537Seric if (tTd(38, 20)) 41660089Seric printf("ndbm_map_lookup(%s)\n", name); 41760089Seric 41860089Seric key.dptr = name; 41960089Seric key.dsize = strlen(name); 42060207Seric if (!bitset(MF_NOFOLDCASE, map->map_mflags)) 42157014Seric { 42260089Seric if (key.dsize > sizeof keybuf - 1) 42360089Seric key.dsize = sizeof keybuf - 1; 42460089Seric bcopy(key.dptr, keybuf, key.dsize + 1); 42560089Seric makelower(keybuf); 42660089Seric key.dptr = keybuf; 42757014Seric } 42864335Seric (void) lockfile(dbm_dirfno((DBM *) map->map_db1), map->map_file, 42964335Seric ".dir", LOCK_SH); 43063753Seric val.dptr = NULL; 43163753Seric if (bitset(MF_TRY0NULL, map->map_mflags)) 43263753Seric { 43363753Seric val = dbm_fetch((DBM *) map->map_db1, key); 43463753Seric if (val.dptr != NULL) 43563753Seric map->map_mflags &= ~MF_TRY1NULL; 43663753Seric } 43763753Seric if (val.dptr == NULL && bitset(MF_TRY1NULL, map->map_mflags)) 43863753Seric { 43956822Seric key.dsize++; 44063753Seric val = dbm_fetch((DBM *) map->map_db1, key); 44163753Seric if (val.dptr != NULL) 44263753Seric map->map_mflags &= ~MF_TRY0NULL; 44363753Seric } 44464335Seric (void) lockfile(dbm_dirfno((DBM *) map->map_db1), map->map_file, 44564335Seric ".dir", LOCK_UN); 44656822Seric if (val.dptr == NULL) 44756822Seric return NULL; 44860207Seric if (bitset(MF_MATCHONLY, map->map_mflags)) 44963753Seric return map_rewrite(map, name, strlen(name), NULL); 45063753Seric else 45163753Seric return map_rewrite(map, val.dptr, val.dsize, av); 45256822Seric } 45356822Seric 45456822Seric 45556822Seric /* 45660089Seric ** DBM_MAP_STORE -- store a datum in the database 45756822Seric */ 45856822Seric 45960089Seric void 46060089Seric ndbm_map_store(map, lhs, rhs) 46160089Seric register MAP *map; 46260089Seric char *lhs; 46360089Seric char *rhs; 46460089Seric { 46560089Seric datum key; 46660089Seric datum data; 46760089Seric int stat; 46860089Seric 46960537Seric if (tTd(38, 12)) 47060089Seric printf("ndbm_map_store(%s, %s)\n", lhs, rhs); 47160089Seric 47260089Seric key.dsize = strlen(lhs); 47360089Seric key.dptr = lhs; 47460089Seric 47560089Seric data.dsize = strlen(rhs); 47660089Seric data.dptr = rhs; 47760089Seric 47860207Seric if (bitset(MF_INCLNULL, map->map_mflags)) 47960089Seric { 48060089Seric key.dsize++; 48160089Seric data.dsize++; 48260089Seric } 48360089Seric 48460089Seric stat = dbm_store((DBM *) map->map_db1, key, data, DBM_INSERT); 48560089Seric if (stat > 0) 48660089Seric { 48760089Seric usrerr("050 Warning: duplicate alias name %s", lhs); 48860089Seric stat = dbm_store((DBM *) map->map_db1, key, data, DBM_REPLACE); 48960089Seric } 49060089Seric if (stat != 0) 49160089Seric syserr("readaliases: dbm put (%s)", lhs); 49260089Seric } 49360089Seric 49460089Seric 49560089Seric /* 49660207Seric ** NDBM_MAP_CLOSE -- close the database 49760089Seric */ 49860089Seric 49960089Seric void 50060089Seric ndbm_map_close(map) 50160089Seric register MAP *map; 50260089Seric { 50360207Seric if (bitset(MF_WRITABLE, map->map_mflags)) 50460089Seric { 50564250Seric #ifdef NIS 50664075Seric bool inclnull; 50760089Seric char buf[200]; 50860089Seric 50964075Seric inclnull = bitset(MF_INCLNULL, map->map_mflags); 51064075Seric map->map_mflags &= ~MF_INCLNULL; 51164075Seric 51260089Seric (void) sprintf(buf, "%010ld", curtime()); 51360089Seric ndbm_map_store(map, "YP_LAST_MODIFIED", buf); 51460089Seric 51560089Seric (void) myhostname(buf, sizeof buf); 51660089Seric ndbm_map_store(map, "YP_MASTER_NAME", buf); 51764075Seric 51864075Seric if (inclnull) 51964075Seric map->map_mflags |= MF_INCLNULL; 52060089Seric #endif 52160089Seric 52260089Seric /* write out the distinguished alias */ 52360089Seric ndbm_map_store(map, "@", "@"); 52460089Seric } 52560089Seric dbm_close((DBM *) map->map_db1); 52660089Seric } 52760089Seric 52860089Seric #endif 52960089Seric /* 53060582Seric ** NEWDB (Hash and BTree) Modules 53160089Seric */ 53260089Seric 53360089Seric #ifdef NEWDB 53460089Seric 53560089Seric /* 53660582Seric ** BT_MAP_OPEN, HASH_MAP_OPEN -- database open primitives. 53760582Seric ** 53860582Seric ** These do rather bizarre locking. If you can lock on open, 53960582Seric ** do that to avoid the condition of opening a database that 54060582Seric ** is being rebuilt. If you don't, we'll try to fake it, but 54160582Seric ** there will be a race condition. If opening for read-only, 54260582Seric ** we immediately release the lock to avoid freezing things up. 54360582Seric ** We really ought to hold the lock, but guarantee that we won't 54460582Seric ** be pokey about it. That's hard to do. 54560089Seric */ 54660089Seric 54756822Seric bool 54860089Seric bt_map_open(map, mode) 54956822Seric MAP *map; 55060089Seric int mode; 55156822Seric { 55256822Seric DB *db; 55360228Seric int i; 55460582Seric int omode; 55564284Seric struct stat st; 55660089Seric char buf[MAXNAME]; 55756822Seric 55860537Seric if (tTd(38, 2)) 55960089Seric printf("bt_map_open(%s, %d)\n", map->map_file, mode); 56060089Seric 56160582Seric omode = mode; 56260582Seric if (omode == O_RDWR) 56360582Seric { 56460582Seric omode |= O_CREAT|O_TRUNC; 56564035Seric #if defined(O_EXLOCK) && defined(HASFLOCK) 56660582Seric omode |= O_EXLOCK; 56760582Seric # if !defined(OLD_NEWDB) 56860582Seric } 56960582Seric else 57060582Seric { 57160582Seric omode |= O_SHLOCK; 57260582Seric # endif 57360582Seric #endif 57460582Seric } 57560207Seric 57660228Seric (void) strcpy(buf, map->map_file); 57760228Seric i = strlen(buf); 57860228Seric if (i < 3 || strcmp(&buf[i - 3], ".db") != 0) 57960228Seric (void) strcat(buf, ".db"); 58060582Seric db = dbopen(buf, omode, DBMMODE, DB_BTREE, NULL); 58156822Seric if (db == NULL) 58256822Seric { 58360207Seric if (!bitset(MF_OPTIONAL, map->map_mflags)) 58456836Seric syserr("Cannot open BTREE database %s", map->map_file); 58556822Seric return FALSE; 58656822Seric } 58764035Seric #if !defined(OLD_NEWDB) && defined(HASFLOCK) 58860582Seric # if !defined(O_EXLOCK) 58960582Seric if (mode == O_RDWR) 59064335Seric (void) lockfile(db->fd(db), map->map_file, ".db", LOCK_EX); 59160582Seric # else 59260582Seric if (mode == O_RDONLY) 59364335Seric (void) lockfile(db->fd(db), map->map_file, ".db", LOCK_UN); 59460582Seric # endif 59560582Seric #endif 59660585Seric 59760585Seric /* try to make sure that at least the database header is on disk */ 59860585Seric if (mode == O_RDWR) 59960585Seric (void) db->sync(db, 0); 60060585Seric 60164284Seric #ifndef OLD_NEWDB 60264284Seric if (fstat(db->fd(db), &st) >= 0) 60364284Seric map->map_mtime = st.st_mtime; 60464284Seric #endif 60564284Seric 60660089Seric map->map_db2 = (void *) db; 60760207Seric if (mode == O_RDONLY && bitset(MF_ALIAS, map->map_mflags)) 60860207Seric aliaswait(map, ".db"); 60956822Seric return TRUE; 61056822Seric } 61156822Seric 61256822Seric 61356822Seric /* 61456822Seric ** HASH_MAP_INIT -- HASH-style map initialization 61556822Seric */ 61656822Seric 61756822Seric bool 61860089Seric hash_map_open(map, mode) 61956822Seric MAP *map; 62060089Seric int mode; 62156822Seric { 62256822Seric DB *db; 62360228Seric int i; 62460582Seric int omode; 62564284Seric struct stat st; 62660089Seric char buf[MAXNAME]; 62756822Seric 62860537Seric if (tTd(38, 2)) 62960089Seric printf("hash_map_open(%s, %d)\n", map->map_file, mode); 63060089Seric 63160582Seric omode = mode; 63260582Seric if (omode == O_RDWR) 63360582Seric { 63460582Seric omode |= O_CREAT|O_TRUNC; 63564035Seric #if defined(O_EXLOCK) && defined(HASFLOCK) 63660582Seric omode |= O_EXLOCK; 63760582Seric # if !defined(OLD_NEWDB) 63860582Seric } 63960582Seric else 64060582Seric { 64160582Seric omode |= O_SHLOCK; 64260582Seric # endif 64360582Seric #endif 64460582Seric } 64560207Seric 64660228Seric (void) strcpy(buf, map->map_file); 64760228Seric i = strlen(buf); 64860228Seric if (i < 3 || strcmp(&buf[i - 3], ".db") != 0) 64960228Seric (void) strcat(buf, ".db"); 65060582Seric db = dbopen(buf, omode, DBMMODE, DB_HASH, NULL); 65156822Seric if (db == NULL) 65256822Seric { 65360207Seric if (!bitset(MF_OPTIONAL, map->map_mflags)) 65456836Seric syserr("Cannot open HASH database %s", map->map_file); 65556822Seric return FALSE; 65656822Seric } 65764035Seric #if !defined(OLD_NEWDB) && defined(HASFLOCK) 65860582Seric # if !defined(O_EXLOCK) 65960582Seric if (mode == O_RDWR) 66064335Seric (void) lockfile(db->fd(db), map->map_file, ".db", LOCK_EX); 66160582Seric # else 66260582Seric if (mode == O_RDONLY) 66364335Seric (void) lockfile(db->fd(db), map->map_file, ".db", LOCK_UN); 66460582Seric # endif 66560582Seric #endif 66660585Seric 66760585Seric /* try to make sure that at least the database header is on disk */ 66860585Seric if (mode == O_RDWR) 66960585Seric (void) db->sync(db, 0); 67060585Seric 67164284Seric #ifndef OLD_NEWDB 67264284Seric if (fstat(db->fd(db), &st) >= 0) 67364284Seric map->map_mtime = st.st_mtime; 67464284Seric #endif 67564284Seric 67660089Seric map->map_db2 = (void *) db; 67760207Seric if (mode == O_RDONLY && bitset(MF_ALIAS, map->map_mflags)) 67860207Seric aliaswait(map, ".db"); 67956822Seric return TRUE; 68056822Seric } 68156822Seric 68256822Seric 68356822Seric /* 68456822Seric ** DB_MAP_LOOKUP -- look up a datum in a BTREE- or HASH-type map 68556822Seric */ 68656822Seric 68756822Seric char * 68860089Seric db_map_lookup(map, name, av, statp) 68956822Seric MAP *map; 69060089Seric char *name; 69156822Seric char **av; 69259084Seric int *statp; 69356822Seric { 69456822Seric DBT key, val; 69560422Seric register DB *db = (DB *) map->map_db2; 69660422Seric int st; 69760422Seric int saveerrno; 69860089Seric char keybuf[MAXNAME + 1]; 69956822Seric 70060537Seric if (tTd(38, 20)) 70160089Seric printf("db_map_lookup(%s)\n", name); 70260089Seric 70360089Seric key.size = strlen(name); 70460089Seric if (key.size > sizeof keybuf - 1) 70560089Seric key.size = sizeof keybuf - 1; 70660089Seric key.data = keybuf; 70760089Seric bcopy(name, keybuf, key.size + 1); 70860207Seric if (!bitset(MF_NOFOLDCASE, map->map_mflags)) 70960089Seric makelower(keybuf); 71060422Seric #ifndef OLD_NEWDB 71164335Seric (void) lockfile(db->fd(db), map->map_file, ".db", LOCK_SH); 71260422Seric #endif 71363753Seric st = 1; 71463753Seric if (bitset(MF_TRY0NULL, map->map_mflags)) 71563753Seric { 71663753Seric st = db->get(db, &key, &val, 0); 71763753Seric if (st == 0) 71863753Seric map->map_mflags &= ~MF_TRY1NULL; 71963753Seric } 72063753Seric if (st != 0 && bitset(MF_TRY1NULL, map->map_mflags)) 72163753Seric { 72263753Seric key.size++; 72363753Seric st = db->get(db, &key, &val, 0); 72463753Seric if (st == 0) 72563753Seric map->map_mflags &= ~MF_TRY0NULL; 72663753Seric } 72760422Seric saveerrno = errno; 72860422Seric #ifndef OLD_NEWDB 72964335Seric (void) lockfile(db->fd(db), map->map_file, ".db", LOCK_UN); 73060422Seric #endif 73160422Seric if (st != 0) 73260422Seric { 73360422Seric errno = saveerrno; 73460422Seric if (st < 0) 73560422Seric syserr("db_map_lookup: get (%s)", name); 73656822Seric return NULL; 73760422Seric } 73860207Seric if (bitset(MF_MATCHONLY, map->map_mflags)) 73963753Seric return map_rewrite(map, name, strlen(name), NULL); 74063753Seric else 74163753Seric return map_rewrite(map, val.data, val.size, av); 74256822Seric } 74356822Seric 74460089Seric 74560089Seric /* 74660089Seric ** DB_MAP_STORE -- store a datum in the NEWDB database 74756822Seric */ 74856822Seric 74960089Seric void 75060089Seric db_map_store(map, lhs, rhs) 75160089Seric register MAP *map; 75260089Seric char *lhs; 75360089Seric char *rhs; 75456822Seric { 75560089Seric int stat; 75660089Seric DBT key; 75760089Seric DBT data; 75860089Seric register DB *db = map->map_db2; 75956822Seric 76060537Seric if (tTd(38, 20)) 76160089Seric printf("db_map_store(%s, %s)\n", lhs, rhs); 76260089Seric 76360089Seric key.size = strlen(lhs); 76460089Seric key.data = lhs; 76560089Seric 76660089Seric data.size = strlen(rhs); 76760089Seric data.data = rhs; 76860089Seric 76960207Seric if (bitset(MF_INCLNULL, map->map_mflags)) 77056822Seric { 77160089Seric key.size++; 77260089Seric data.size++; 77360089Seric } 77456836Seric 77560089Seric stat = db->put(db, &key, &data, R_NOOVERWRITE); 77660089Seric if (stat > 0) 77760089Seric { 77860089Seric usrerr("050 Warning: duplicate alias name %s", lhs); 77960089Seric stat = db->put(db, &key, &data, 0); 78060089Seric } 78160089Seric if (stat != 0) 78260089Seric syserr("readaliases: db put (%s)", lhs); 78360089Seric } 78456836Seric 78556847Seric 78660089Seric /* 78760089Seric ** DB_MAP_CLOSE -- add distinguished entries and close the database 78860089Seric */ 78960089Seric 79060089Seric void 79160089Seric db_map_close(map) 79260089Seric MAP *map; 79360089Seric { 79460089Seric register DB *db = map->map_db2; 79560089Seric 79660537Seric if (tTd(38, 9)) 79760207Seric printf("db_map_close(%s, %x)\n", map->map_file, map->map_mflags); 79860089Seric 79960207Seric if (bitset(MF_WRITABLE, map->map_mflags)) 80058804Seric { 80160089Seric /* write out the distinguished alias */ 80260089Seric db_map_store(map, "@", "@"); 80358804Seric } 80458963Seric 80560089Seric if (db->close(db) != 0) 80660089Seric syserr("readaliases: db close failure"); 80756822Seric } 80857208Seric 80960089Seric #endif 81060089Seric /* 81160089Seric ** NIS Modules 81260089Seric */ 81360089Seric 81460089Seric # ifdef NIS 81560089Seric 816*64369Seric # ifndef YPERR_BUSY 817*64369Seric # define YPERR_BUSY 16 818*64369Seric # endif 819*64369Seric 82057208Seric /* 82160089Seric ** NIS_MAP_OPEN -- open DBM map 82257208Seric */ 82357208Seric 82457208Seric bool 82560089Seric nis_map_open(map, mode) 82657208Seric MAP *map; 82760089Seric int mode; 82857208Seric { 82957216Seric int yperr; 83060215Seric register char *p; 83160215Seric auto char *vp; 83260215Seric auto int vsize; 83357216Seric char *master; 83457216Seric 83560537Seric if (tTd(38, 2)) 83660089Seric printf("nis_map_open(%s)\n", map->map_file); 83760089Seric 83860207Seric if (mode != O_RDONLY) 83960207Seric { 84060207Seric errno = ENODEV; 84160207Seric return FALSE; 84260207Seric } 84360207Seric 84460089Seric p = strchr(map->map_file, '@'); 84560089Seric if (p != NULL) 84660089Seric { 84760089Seric *p++ = '\0'; 84860089Seric if (*p != '\0') 84960089Seric map->map_domain = p; 85060089Seric } 85160215Seric 85260089Seric if (map->map_domain == NULL) 85360089Seric yp_get_default_domain(&map->map_domain); 85460089Seric 85560089Seric if (*map->map_file == '\0') 85660089Seric map->map_file = "mail.aliases"; 85760089Seric 85860215Seric /* check to see if this map actually exists */ 85960089Seric yperr = yp_match(map->map_domain, map->map_file, "@", 1, 86060089Seric &vp, &vsize); 86160537Seric if (tTd(38, 10)) 86260089Seric printf("nis_map_open: yp_match(%s, %s) => %s\n", 86360089Seric map->map_domain, map->map_file, yperr_string(yperr)); 86460089Seric if (yperr == 0 || yperr == YPERR_KEY || yperr == YPERR_BUSY) 86560089Seric return TRUE; 86660215Seric 86760215Seric if (!bitset(MF_OPTIONAL, map->map_mflags)) 86860215Seric syserr("Cannot bind to domain %s: %s", map->map_domain, 86960215Seric yperr_string(yperr)); 87060215Seric 87160089Seric return FALSE; 87260089Seric } 87360089Seric 87460089Seric 87560089Seric /* 87657208Seric ** NIS_MAP_LOOKUP -- look up a datum in a NIS map 87757208Seric */ 87857208Seric 87957208Seric char * 88060089Seric nis_map_lookup(map, name, av, statp) 88157208Seric MAP *map; 88260089Seric char *name; 88357208Seric char **av; 88459084Seric int *statp; 88557208Seric { 88657208Seric char *vp; 88757642Seric auto int vsize; 88859274Seric int buflen; 88960215Seric int yperr; 89060089Seric char keybuf[MAXNAME + 1]; 89157208Seric 89260537Seric if (tTd(38, 20)) 89360089Seric printf("nis_map_lookup(%s)\n", name); 89460089Seric 89560089Seric buflen = strlen(name); 89660089Seric if (buflen > sizeof keybuf - 1) 89760089Seric buflen = sizeof keybuf - 1; 89860089Seric bcopy(name, keybuf, buflen + 1); 89960207Seric if (!bitset(MF_NOFOLDCASE, map->map_mflags)) 90060089Seric makelower(keybuf); 90163753Seric yperr = YPERR_KEY; 90263753Seric if (bitset(MF_TRY0NULL, map->map_mflags)) 90363753Seric { 90463753Seric yperr = yp_match(map->map_domain, map->map_file, keybuf, buflen, 90563753Seric &vp, &vsize); 90663753Seric if (yperr == 0) 90763753Seric map->map_mflags &= ~MF_TRY1NULL; 90863753Seric } 90963753Seric if (yperr == YPERR_KEY && bitset(MF_TRY1NULL, map->map_mflags)) 91063753Seric { 91159274Seric buflen++; 91263753Seric yperr = yp_match(map->map_domain, map->map_file, keybuf, buflen, 91363753Seric &vp, &vsize); 91463753Seric if (yperr == 0) 91563753Seric map->map_mflags &= ~MF_TRY0NULL; 91663753Seric } 91760089Seric if (yperr != 0) 91860089Seric { 91960089Seric if (yperr != YPERR_KEY && yperr != YPERR_BUSY) 92060215Seric map->map_mflags &= ~(MF_VALID|MF_OPEN); 92157208Seric return NULL; 92260089Seric } 92360207Seric if (bitset(MF_MATCHONLY, map->map_mflags)) 92463753Seric return map_rewrite(map, name, strlen(name), NULL); 92563753Seric else 92663753Seric return map_rewrite(map, vp, vsize, av); 92757208Seric } 92857208Seric 92960089Seric 93060089Seric /* 93160207Seric ** NIS_MAP_STORE 93260089Seric */ 93360089Seric 93460089Seric void 93560089Seric nis_map_store(map, lhs, rhs) 93660089Seric MAP *map; 93760089Seric char *lhs; 93860089Seric char *rhs; 93960089Seric { 94060089Seric /* nothing */ 94160089Seric } 94260089Seric 94360089Seric 94460089Seric /* 94560207Seric ** NIS_MAP_CLOSE 94660089Seric */ 94760089Seric 94860089Seric void 94960089Seric nis_map_close(map) 95060089Seric MAP *map; 95160089Seric { 95260089Seric /* nothing */ 95360089Seric } 95460089Seric 95560089Seric #endif /* NIS */ 95657208Seric /* 95760089Seric ** STAB (Symbol Table) Modules 95860089Seric */ 95960089Seric 96060089Seric 96160089Seric /* 96260207Seric ** STAB_MAP_LOOKUP -- look up alias in symbol table 96360089Seric */ 96460089Seric 96560089Seric char * 96661707Seric stab_map_lookup(map, name, av, pstat) 96760089Seric register MAP *map; 96860089Seric char *name; 96961707Seric char **av; 97061707Seric int *pstat; 97160089Seric { 97260089Seric register STAB *s; 97360089Seric 97460537Seric if (tTd(38, 20)) 97560089Seric printf("stab_lookup(%s)\n", name); 97660089Seric 97760089Seric s = stab(name, ST_ALIAS, ST_FIND); 97860089Seric if (s != NULL) 97960089Seric return (s->s_alias); 98060089Seric return (NULL); 98160089Seric } 98260089Seric 98360089Seric 98460089Seric /* 98560207Seric ** STAB_MAP_STORE -- store in symtab (actually using during init, not rebuild) 98660089Seric */ 98760089Seric 98860089Seric void 98960089Seric stab_map_store(map, lhs, rhs) 99060089Seric register MAP *map; 99160089Seric char *lhs; 99260089Seric char *rhs; 99360089Seric { 99460089Seric register STAB *s; 99560089Seric 99660089Seric s = stab(lhs, ST_ALIAS, ST_ENTER); 99760089Seric s->s_alias = newstr(rhs); 99860089Seric } 99960089Seric 100060089Seric 100160089Seric /* 100260207Seric ** STAB_MAP_OPEN -- initialize (reads data file) 100360207Seric ** 100460207Seric ** This is a wierd case -- it is only intended as a fallback for 100560207Seric ** aliases. For this reason, opens for write (only during a 100660207Seric ** "newaliases") always fails, and opens for read open the 100760207Seric ** actual underlying text file instead of the database. 100860089Seric */ 100960089Seric 101060089Seric bool 101160089Seric stab_map_open(map, mode) 101260089Seric register MAP *map; 101360089Seric int mode; 101460089Seric { 101563835Seric FILE *af; 101664284Seric struct stat st; 101763835Seric 101860537Seric if (tTd(38, 2)) 101960089Seric printf("stab_map_open(%s)\n", map->map_file); 102060089Seric 102160089Seric if (mode != O_RDONLY) 102260207Seric { 102360207Seric errno = ENODEV; 102460089Seric return FALSE; 102560207Seric } 102660089Seric 102763835Seric af = fopen(map->map_file, "r"); 102863835Seric if (af == NULL) 102963835Seric return FALSE; 103063835Seric readaliases(map, af, TRUE); 103164284Seric 103264284Seric if (fstat(fileno(af), &st) >= 0) 103364284Seric map->map_mtime = st.st_mtime; 103463835Seric fclose(af); 103563835Seric 103660089Seric return TRUE; 103760089Seric } 103860089Seric 103960089Seric 104060089Seric /* 104160207Seric ** STAB_MAP_CLOSE -- close symbol table (???) 104260089Seric */ 104360089Seric 104460089Seric void 104560089Seric stab_map_close(map) 104660089Seric MAP *map; 104760089Seric { 104860089Seric /* ignore it */ 104960089Seric } 105060089Seric /* 105160089Seric ** Implicit Modules 105256822Seric ** 105360089Seric ** Tries several types. For back compatibility of aliases. 105456822Seric */ 105556822Seric 105660089Seric 105760089Seric /* 105860207Seric ** IMPL_MAP_LOOKUP -- lookup in best open database 105960089Seric */ 106060089Seric 106160089Seric char * 106260089Seric impl_map_lookup(map, name, av, pstat) 106360089Seric MAP *map; 106460089Seric char *name; 106556822Seric char **av; 106660089Seric int *pstat; 106756822Seric { 106860537Seric if (tTd(38, 20)) 106960089Seric printf("impl_map_lookup(%s)\n", name); 107056822Seric 107160089Seric #ifdef NEWDB 107260207Seric if (bitset(MF_IMPL_HASH, map->map_mflags)) 107360089Seric return db_map_lookup(map, name, av, pstat); 107460089Seric #endif 107560089Seric #ifdef NDBM 107660207Seric if (bitset(MF_IMPL_NDBM, map->map_mflags)) 107760089Seric return ndbm_map_lookup(map, name, av, pstat); 107860089Seric #endif 107960089Seric return stab_map_lookup(map, name, av, pstat); 108060089Seric } 108160089Seric 108260089Seric /* 108360207Seric ** IMPL_MAP_STORE -- store in open databases 108460089Seric */ 108560089Seric 108660089Seric void 108760089Seric impl_map_store(map, lhs, rhs) 108860089Seric MAP *map; 108960089Seric char *lhs; 109060089Seric char *rhs; 109160089Seric { 109260089Seric #ifdef NEWDB 109360207Seric if (bitset(MF_IMPL_HASH, map->map_mflags)) 109460089Seric db_map_store(map, lhs, rhs); 109560089Seric #endif 109660089Seric #ifdef NDBM 109760207Seric if (bitset(MF_IMPL_NDBM, map->map_mflags)) 109860089Seric ndbm_map_store(map, lhs, rhs); 109960089Seric #endif 110060089Seric stab_map_store(map, lhs, rhs); 110160089Seric } 110260089Seric 110360089Seric /* 110460089Seric ** IMPL_MAP_OPEN -- implicit database open 110560089Seric */ 110660089Seric 110760089Seric bool 110860089Seric impl_map_open(map, mode) 110960089Seric MAP *map; 111060089Seric int mode; 111160089Seric { 111260089Seric struct stat stb; 111360089Seric 111460537Seric if (tTd(38, 2)) 111560089Seric printf("impl_map_open(%s)\n", map->map_file); 111660089Seric 111760089Seric if (stat(map->map_file, &stb) < 0) 111856822Seric { 111960089Seric /* no alias file at all */ 112060089Seric return FALSE; 112156822Seric } 112256822Seric 112360089Seric #ifdef NEWDB 112460207Seric map->map_mflags |= MF_IMPL_HASH; 112560089Seric if (hash_map_open(map, mode)) 112656822Seric { 112764250Seric #if defined(NDBM) && defined(NIS) 112860561Seric if (mode == O_RDONLY || access("/var/yp/Makefile", R_OK) != 0) 112960207Seric #endif 113060207Seric return TRUE; 113160089Seric } 113260207Seric else 113360207Seric map->map_mflags &= ~MF_IMPL_HASH; 113460089Seric #endif 113560089Seric #ifdef NDBM 113660207Seric map->map_mflags |= MF_IMPL_NDBM; 113760089Seric if (ndbm_map_open(map, mode)) 113860089Seric { 113960089Seric return TRUE; 114060089Seric } 114160207Seric else 114260207Seric map->map_mflags &= ~MF_IMPL_NDBM; 114360089Seric #endif 114456822Seric 114560207Seric #if !defined(NEWDB) && !defined(NDBM) 114660089Seric if (Verbose) 114760089Seric message("WARNING: cannot open alias database %s", map->map_file); 114860207Seric #endif 114960089Seric 115060207Seric return stab_map_open(map, mode); 115156822Seric } 115260089Seric 115360207Seric 115460089Seric /* 115560207Seric ** IMPL_MAP_CLOSE -- close any open database(s) 115660089Seric */ 115760089Seric 115860089Seric void 115960207Seric impl_map_close(map) 116060089Seric MAP *map; 116160089Seric { 116260089Seric #ifdef NEWDB 116360207Seric if (bitset(MF_IMPL_HASH, map->map_mflags)) 116460089Seric { 116560207Seric db_map_close(map); 116660207Seric map->map_mflags &= ~MF_IMPL_HASH; 116760089Seric } 116860089Seric #endif 116960089Seric 117060089Seric #ifdef NDBM 117160207Seric if (bitset(MF_IMPL_NDBM, map->map_mflags)) 117260089Seric { 117360207Seric ndbm_map_close(map); 117460207Seric map->map_mflags &= ~MF_IMPL_NDBM; 117560089Seric } 117660089Seric #endif 117760089Seric } 117860207Seric /* 117960207Seric ** NULL stubs 118060089Seric */ 118160089Seric 118260207Seric bool 118360207Seric null_map_open(map, mode) 118460089Seric MAP *map; 118560207Seric int mode; 118660089Seric { 118760207Seric return TRUE; 118860089Seric } 118960089Seric 119060207Seric void 119160207Seric null_map_close(map) 119260207Seric MAP *map; 119360089Seric { 119460207Seric return; 119560207Seric } 119660089Seric 119760207Seric void 119860207Seric null_map_store(map, key, val) 119960207Seric MAP *map; 120060207Seric char *key; 120160207Seric char *val; 120260089Seric { 120360207Seric return; 120460089Seric } 1205