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*60256Seric static char sccsid[] = "@(#)map.c 6.19 (Berkeley) 05/24/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 16460089Seric char * 16560089Seric map_rewrite(map, s, slen, av) 16660089Seric register MAP *map; 16760089Seric register char *s; 16860089Seric int slen; 16960089Seric char **av; 17060089Seric { 17160089Seric register char *bp; 17260089Seric register char c; 17360089Seric char **avp; 17460089Seric register char *ap; 17560089Seric int i; 17660089Seric int len; 17760089Seric static int buflen = -1; 17860089Seric static char *buf = NULL; 17960089Seric 18060089Seric if (tTd(23, 1)) 18160089Seric { 182*60256Seric printf("map_rewrite(%.*s), av =", slen, s); 183*60256Seric if (av == NULL) 184*60256Seric printf(" (nullv)"); 185*60256Seric else 186*60256Seric { 187*60256Seric for (avp = av; *avp != NULL; avp++) 188*60256Seric printf("\n\t%s", *avp); 189*60256Seric } 190*60256Seric printf("\n"); 19160089Seric } 19260089Seric 19360089Seric /* count expected size of output (can safely overestimate) */ 19460089Seric i = len = slen; 19560089Seric if (av != NULL) 19660089Seric { 19760089Seric bp = s; 19860089Seric for (i = slen; --i >= 0 && (c = *bp++) != 0; ) 19960089Seric { 20060089Seric if (c != '%') 20160089Seric continue; 20260089Seric if (--i < 0) 20360089Seric break; 20460089Seric c = *bp++; 20560089Seric if (!(isascii(c) && isdigit(c))) 20660089Seric continue; 20760089Seric c -= 0; 20860089Seric for (avp = av; --c >= 0 && *avp != NULL; avp++) 20960089Seric continue; 21060089Seric if (*avp == NULL) 21160089Seric continue; 21260089Seric len += strlen(*avp); 21360089Seric } 21460089Seric } 21560089Seric if (map->map_app != NULL) 21660089Seric len += strlen(map->map_app); 21760089Seric if (buflen < ++len) 21860089Seric { 21960089Seric /* need to malloc additional space */ 22060089Seric buflen = len; 22160089Seric if (buf != NULL) 22260089Seric free(buf); 22360089Seric buf = xalloc(buflen); 22460089Seric } 22560089Seric 22660089Seric bp = buf; 22760089Seric if (av == NULL) 22860089Seric { 22960089Seric bcopy(s, bp, slen); 23060089Seric bp += slen; 23160089Seric } 23260089Seric else 23360089Seric { 23460089Seric while (--slen >= 0 && (c = *s++) != '\0') 23560089Seric { 23660089Seric if (c != '%') 23760089Seric { 23860089Seric pushc: 23960089Seric *bp++ = c; 24060089Seric continue; 24160089Seric } 24260089Seric if (--slen < 0 || (c = *s++) == '\0') 24360089Seric c = '%'; 24460089Seric if (c == '%') 24560089Seric goto pushc; 24660089Seric if (!(isascii(c) && isdigit(c))) 24760089Seric { 24860089Seric *bp++ = '%'; 24960089Seric goto pushc; 25060089Seric } 25160089Seric c -= '0'; 25260089Seric for (avp = av; --c >= 0 && *avp != NULL; avp++) 25360089Seric continue; 25460089Seric if (*avp == NULL) 25560089Seric continue; 25660089Seric 25760089Seric /* transliterate argument into output string */ 25860089Seric for (ap = *avp; (c = *ap++) != '\0'; ) 25960089Seric *bp++ = c; 26060089Seric } 26160089Seric } 26260089Seric if (map->map_app != NULL) 26360089Seric strcpy(bp, map->map_app); 26460089Seric else 26560089Seric *bp = '\0'; 26660089Seric if (tTd(23, 1)) 26760089Seric printf("map_rewrite => %s\n", buf); 26860089Seric return buf; 26960089Seric } 27060089Seric /* 27160089Seric ** NDBM modules 27260089Seric */ 27360089Seric 27460089Seric #ifdef NDBM 27560089Seric 27660089Seric /* 27760089Seric ** DBM_MAP_OPEN -- DBM-style map open 27860089Seric */ 27960089Seric 28060089Seric bool 28160089Seric ndbm_map_open(map, mode) 28260089Seric MAP *map; 28360089Seric int mode; 28460089Seric { 28560089Seric DBM *dbm; 28660089Seric 28760089Seric if (tTd(27, 2)) 28860089Seric printf("ndbm_map_open(%s, %d)\n", map->map_file, mode); 28960089Seric 29060207Seric if (mode == O_RDWR) 29160207Seric mode |= O_CREAT|O_TRUNC; 29260207Seric 29360089Seric /* open the database */ 29460089Seric dbm = dbm_open(map->map_file, mode, DBMMODE); 29556822Seric if (dbm == NULL) 29656822Seric { 29760207Seric if (!bitset(MF_OPTIONAL, map->map_mflags)) 29856836Seric syserr("Cannot open DBM database %s", map->map_file); 29956822Seric return FALSE; 30056822Seric } 30160089Seric map->map_db1 = (void *) dbm; 30260207Seric if (mode == O_RDONLY && bitset(MF_ALIAS, map->map_mflags)) 30360207Seric aliaswait(map, ".dir"); 30456822Seric return TRUE; 30556822Seric } 30660089Seric 30760089Seric 30860089Seric /* 30956822Seric ** DBM_MAP_LOOKUP -- look up a datum in a DBM-type map 31056822Seric */ 31156822Seric 31256822Seric char * 31360089Seric ndbm_map_lookup(map, name, av, statp) 31456822Seric MAP *map; 31560089Seric char *name; 31656822Seric char **av; 31759084Seric int *statp; 31856822Seric { 31956822Seric datum key, val; 32060089Seric char keybuf[MAXNAME + 1]; 32156822Seric 32260089Seric if (tTd(27, 20)) 32360089Seric printf("ndbm_map_lookup(%s)\n", name); 32460089Seric 32560089Seric key.dptr = name; 32660089Seric key.dsize = strlen(name); 32760207Seric if (!bitset(MF_NOFOLDCASE, map->map_mflags)) 32857014Seric { 32960089Seric if (key.dsize > sizeof keybuf - 1) 33060089Seric key.dsize = sizeof keybuf - 1; 33160089Seric bcopy(key.dptr, keybuf, key.dsize + 1); 33260089Seric makelower(keybuf); 33360089Seric key.dptr = keybuf; 33457014Seric } 33560207Seric if (bitset(MF_INCLNULL, map->map_mflags)) 33656822Seric key.dsize++; 33760089Seric (void) lockfile(dbm_dirfno((DBM *) map->map_db1), map->map_file, LOCK_SH); 33860089Seric val = dbm_fetch((DBM *) map->map_db1, key); 33960089Seric (void) lockfile(dbm_dirfno((DBM *) map->map_db1), map->map_file, LOCK_UN); 34056822Seric if (val.dptr == NULL) 34156822Seric return NULL; 34260207Seric if (bitset(MF_MATCHONLY, map->map_mflags)) 34360089Seric av = NULL; 34460089Seric return map_rewrite(map, val.dptr, val.dsize, av); 34556822Seric } 34656822Seric 34756822Seric 34856822Seric /* 34960089Seric ** DBM_MAP_STORE -- store a datum in the database 35056822Seric */ 35156822Seric 35260089Seric void 35360089Seric ndbm_map_store(map, lhs, rhs) 35460089Seric register MAP *map; 35560089Seric char *lhs; 35660089Seric char *rhs; 35760089Seric { 35860089Seric datum key; 35960089Seric datum data; 36060089Seric int stat; 36160089Seric 36260089Seric if (tTd(27, 12)) 36360089Seric printf("ndbm_map_store(%s, %s)\n", lhs, rhs); 36460089Seric 36560089Seric key.dsize = strlen(lhs); 36660089Seric key.dptr = lhs; 36760089Seric 36860089Seric data.dsize = strlen(rhs); 36960089Seric data.dptr = rhs; 37060089Seric 37160207Seric if (bitset(MF_INCLNULL, map->map_mflags)) 37260089Seric { 37360089Seric key.dsize++; 37460089Seric data.dsize++; 37560089Seric } 37660089Seric 37760089Seric stat = dbm_store((DBM *) map->map_db1, key, data, DBM_INSERT); 37860089Seric if (stat > 0) 37960089Seric { 38060089Seric usrerr("050 Warning: duplicate alias name %s", lhs); 38160089Seric stat = dbm_store((DBM *) map->map_db1, key, data, DBM_REPLACE); 38260089Seric } 38360089Seric if (stat != 0) 38460089Seric syserr("readaliases: dbm put (%s)", lhs); 38560089Seric } 38660089Seric 38760089Seric 38860089Seric /* 38960207Seric ** NDBM_MAP_CLOSE -- close the database 39060089Seric */ 39160089Seric 39260089Seric void 39360089Seric ndbm_map_close(map) 39460089Seric register MAP *map; 39560089Seric { 39660207Seric if (bitset(MF_WRITABLE, map->map_mflags)) 39760089Seric { 39860089Seric #ifdef YPCOMPAT 39960089Seric char buf[200]; 40060089Seric 40160089Seric (void) sprintf(buf, "%010ld", curtime()); 40260089Seric ndbm_map_store(map, "YP_LAST_MODIFIED", buf); 40360089Seric 40460089Seric (void) myhostname(buf, sizeof buf); 40560089Seric ndbm_map_store(map, "YP_MASTER_NAME", buf); 40660089Seric #endif 40760089Seric 40860089Seric /* write out the distinguished alias */ 40960089Seric ndbm_map_store(map, "@", "@"); 41060089Seric } 41160089Seric dbm_close((DBM *) map->map_db1); 41260089Seric } 41360089Seric 41460089Seric #endif 41560089Seric /* 41660089Seric ** HASH (NEWDB) Modules 41760089Seric */ 41860089Seric 41960089Seric #ifdef NEWDB 42060089Seric 42160089Seric /* 42260089Seric ** BTREE_MAP_PARSE -- BTREE-style map initialization 42360089Seric */ 42460089Seric 42556822Seric bool 42660089Seric bt_map_open(map, mode) 42756822Seric MAP *map; 42860089Seric int mode; 42956822Seric { 43056822Seric DB *db; 43160228Seric int i; 43260089Seric char buf[MAXNAME]; 43356822Seric 43460089Seric if (tTd(27, 2)) 43560089Seric printf("bt_map_open(%s, %d)\n", map->map_file, mode); 43660089Seric 43760207Seric if (mode == O_RDWR) 43860207Seric mode |= O_CREAT|O_TRUNC; 43960207Seric 44060228Seric (void) strcpy(buf, map->map_file); 44160228Seric i = strlen(buf); 44260228Seric if (i < 3 || strcmp(&buf[i - 3], ".db") != 0) 44360228Seric (void) strcat(buf, ".db"); 44460222Seric db = dbopen(buf, mode, DBMMODE, DB_BTREE, NULL); 44556822Seric if (db == NULL) 44656822Seric { 44760207Seric if (!bitset(MF_OPTIONAL, map->map_mflags)) 44856836Seric syserr("Cannot open BTREE database %s", map->map_file); 44956822Seric return FALSE; 45056822Seric } 45160089Seric map->map_db2 = (void *) db; 45260207Seric if (mode == O_RDONLY && bitset(MF_ALIAS, map->map_mflags)) 45360207Seric aliaswait(map, ".db"); 45456822Seric return TRUE; 45556822Seric } 45656822Seric 45756822Seric 45856822Seric /* 45956822Seric ** HASH_MAP_INIT -- HASH-style map initialization 46056822Seric */ 46156822Seric 46256822Seric bool 46360089Seric hash_map_open(map, mode) 46456822Seric MAP *map; 46560089Seric int mode; 46656822Seric { 46756822Seric DB *db; 46860228Seric int i; 46960089Seric char buf[MAXNAME]; 47056822Seric 47160089Seric if (tTd(27, 2)) 47260089Seric printf("hash_map_open(%s, %d)\n", map->map_file, mode); 47360089Seric 47460207Seric if (mode == O_RDWR) 47560207Seric mode |= O_CREAT|O_TRUNC; 47660207Seric 47760228Seric (void) strcpy(buf, map->map_file); 47860228Seric i = strlen(buf); 47960228Seric if (i < 3 || strcmp(&buf[i - 3], ".db") != 0) 48060228Seric (void) strcat(buf, ".db"); 48160222Seric db = dbopen(buf, mode, DBMMODE, DB_HASH, NULL); 48256822Seric if (db == NULL) 48356822Seric { 48460207Seric if (!bitset(MF_OPTIONAL, map->map_mflags)) 48556836Seric syserr("Cannot open HASH database %s", map->map_file); 48656822Seric return FALSE; 48756822Seric } 48860089Seric map->map_db2 = (void *) db; 48960207Seric if (mode == O_RDONLY && bitset(MF_ALIAS, map->map_mflags)) 49060207Seric aliaswait(map, ".db"); 49156822Seric return TRUE; 49256822Seric } 49356822Seric 49456822Seric 49556822Seric /* 49656822Seric ** DB_MAP_LOOKUP -- look up a datum in a BTREE- or HASH-type map 49756822Seric */ 49856822Seric 49956822Seric char * 50060089Seric db_map_lookup(map, name, av, statp) 50156822Seric MAP *map; 50260089Seric char *name; 50356822Seric char **av; 50459084Seric int *statp; 50556822Seric { 50656822Seric DBT key, val; 50760089Seric char keybuf[MAXNAME + 1]; 50856822Seric 50960089Seric if (tTd(27, 20)) 51060089Seric printf("db_map_lookup(%s)\n", name); 51160089Seric 51260089Seric key.size = strlen(name); 51360089Seric if (key.size > sizeof keybuf - 1) 51460089Seric key.size = sizeof keybuf - 1; 51560089Seric key.data = keybuf; 51660089Seric bcopy(name, keybuf, key.size + 1); 51760207Seric if (!bitset(MF_NOFOLDCASE, map->map_mflags)) 51860089Seric makelower(keybuf); 51960207Seric if (bitset(MF_INCLNULL, map->map_mflags)) 52056822Seric key.size++; 52160089Seric if (((DB *) map->map_db2)->get((DB *) map->map_db2, &key, &val, 0) != 0) 52256822Seric return NULL; 52360207Seric if (bitset(MF_MATCHONLY, map->map_mflags)) 52460089Seric av = NULL; 52560089Seric return map_rewrite(map, val.data, val.size, av); 52656822Seric } 52756822Seric 52860089Seric 52960089Seric /* 53060089Seric ** DB_MAP_STORE -- store a datum in the NEWDB database 53156822Seric */ 53256822Seric 53360089Seric void 53460089Seric db_map_store(map, lhs, rhs) 53560089Seric register MAP *map; 53660089Seric char *lhs; 53760089Seric char *rhs; 53856822Seric { 53960089Seric int stat; 54060089Seric DBT key; 54160089Seric DBT data; 54260089Seric register DB *db = map->map_db2; 54356822Seric 54460089Seric if (tTd(27, 20)) 54560089Seric printf("db_map_store(%s, %s)\n", lhs, rhs); 54660089Seric 54760089Seric key.size = strlen(lhs); 54860089Seric key.data = lhs; 54960089Seric 55060089Seric data.size = strlen(rhs); 55160089Seric data.data = rhs; 55260089Seric 55360207Seric if (bitset(MF_INCLNULL, map->map_mflags)) 55456822Seric { 55560089Seric key.size++; 55660089Seric data.size++; 55760089Seric } 55856836Seric 55960089Seric stat = db->put(db, &key, &data, R_NOOVERWRITE); 56060089Seric if (stat > 0) 56160089Seric { 56260089Seric usrerr("050 Warning: duplicate alias name %s", lhs); 56360089Seric stat = db->put(db, &key, &data, 0); 56460089Seric } 56560089Seric if (stat != 0) 56660089Seric syserr("readaliases: db put (%s)", lhs); 56760089Seric } 56856836Seric 56956847Seric 57060089Seric /* 57160089Seric ** DB_MAP_CLOSE -- add distinguished entries and close the database 57260089Seric */ 57360089Seric 57460089Seric void 57560089Seric db_map_close(map) 57660089Seric MAP *map; 57760089Seric { 57860089Seric register DB *db = map->map_db2; 57960089Seric 58060089Seric if (tTd(27, 9)) 58160207Seric printf("db_map_close(%s, %x)\n", map->map_file, map->map_mflags); 58260089Seric 58360207Seric if (bitset(MF_WRITABLE, map->map_mflags)) 58458804Seric { 58560089Seric /* write out the distinguished alias */ 58660089Seric db_map_store(map, "@", "@"); 58758804Seric } 58858963Seric 58960089Seric if (db->close(db) != 0) 59060089Seric syserr("readaliases: db close failure"); 59156822Seric } 59257208Seric 59360089Seric #endif 59460089Seric /* 59560089Seric ** NIS Modules 59660089Seric */ 59760089Seric 59860089Seric # ifdef NIS 59960089Seric 60057208Seric /* 60160089Seric ** NIS_MAP_OPEN -- open DBM map 60257208Seric */ 60357208Seric 60457208Seric bool 60560089Seric nis_map_open(map, mode) 60657208Seric MAP *map; 60760089Seric int mode; 60857208Seric { 60957216Seric int yperr; 61060215Seric register char *p; 61160215Seric auto char *vp; 61260215Seric auto int vsize; 61357216Seric char *master; 61457216Seric 61560089Seric if (tTd(27, 2)) 61660089Seric printf("nis_map_open(%s)\n", map->map_file); 61760089Seric 61860207Seric if (mode != O_RDONLY) 61960207Seric { 62060207Seric errno = ENODEV; 62160207Seric return FALSE; 62260207Seric } 62360207Seric 62460089Seric p = strchr(map->map_file, '@'); 62560089Seric if (p != NULL) 62660089Seric { 62760089Seric *p++ = '\0'; 62860089Seric if (*p != '\0') 62960089Seric map->map_domain = p; 63060089Seric } 63160215Seric 63260089Seric if (map->map_domain == NULL) 63360089Seric yp_get_default_domain(&map->map_domain); 63460089Seric 63560089Seric if (*map->map_file == '\0') 63660089Seric map->map_file = "mail.aliases"; 63760089Seric 63860215Seric /* check to see if this map actually exists */ 63960089Seric yperr = yp_match(map->map_domain, map->map_file, "@", 1, 64060089Seric &vp, &vsize); 64160089Seric if (tTd(27, 10)) 64260089Seric printf("nis_map_open: yp_match(%s, %s) => %s\n", 64360089Seric map->map_domain, map->map_file, yperr_string(yperr)); 64460089Seric if (yperr == 0 || yperr == YPERR_KEY || yperr == YPERR_BUSY) 64560089Seric return TRUE; 64660215Seric 64760215Seric if (!bitset(MF_OPTIONAL, map->map_mflags)) 64860215Seric syserr("Cannot bind to domain %s: %s", map->map_domain, 64960215Seric yperr_string(yperr)); 65060215Seric 65160089Seric return FALSE; 65260089Seric } 65360089Seric 65460089Seric 65560089Seric /* 65657208Seric ** NIS_MAP_LOOKUP -- look up a datum in a NIS map 65757208Seric */ 65857208Seric 65957208Seric char * 66060089Seric nis_map_lookup(map, name, av, statp) 66157208Seric MAP *map; 66260089Seric char *name; 66357208Seric char **av; 66459084Seric int *statp; 66557208Seric { 66657208Seric char *vp; 66757642Seric auto int vsize; 66859274Seric int buflen; 66960215Seric int yperr; 67060089Seric char keybuf[MAXNAME + 1]; 67157208Seric 67260089Seric if (tTd(27, 20)) 67360089Seric printf("nis_map_lookup(%s)\n", name); 67460089Seric 67560089Seric buflen = strlen(name); 67660089Seric if (buflen > sizeof keybuf - 1) 67760089Seric buflen = sizeof keybuf - 1; 67860089Seric bcopy(name, keybuf, buflen + 1); 67960207Seric if (!bitset(MF_NOFOLDCASE, map->map_mflags)) 68060089Seric makelower(keybuf); 68160207Seric if (bitset(MF_INCLNULL, map->map_mflags)) 68259274Seric buflen++; 68360089Seric yperr = yp_match(map->map_domain, map->map_file, keybuf, buflen, 68460089Seric &vp, &vsize); 68560089Seric if (yperr != 0) 68660089Seric { 68760089Seric if (yperr != YPERR_KEY && yperr != YPERR_BUSY) 68860215Seric map->map_mflags &= ~(MF_VALID|MF_OPEN); 68957208Seric return NULL; 69060089Seric } 69160207Seric if (bitset(MF_MATCHONLY, map->map_mflags)) 69260089Seric av = NULL; 69360215Seric return map_rewrite(map, vp, vsize, av); 69457208Seric } 69557208Seric 69660089Seric 69760089Seric /* 69860207Seric ** NIS_MAP_STORE 69960089Seric */ 70060089Seric 70160089Seric void 70260089Seric nis_map_store(map, lhs, rhs) 70360089Seric MAP *map; 70460089Seric char *lhs; 70560089Seric char *rhs; 70660089Seric { 70760089Seric /* nothing */ 70860089Seric } 70960089Seric 71060089Seric 71160089Seric /* 71260207Seric ** NIS_MAP_CLOSE 71360089Seric */ 71460089Seric 71560089Seric void 71660089Seric nis_map_close(map) 71760089Seric MAP *map; 71860089Seric { 71960089Seric /* nothing */ 72060089Seric } 72160089Seric 72260089Seric #endif /* NIS */ 72357208Seric /* 72460089Seric ** STAB (Symbol Table) Modules 72560089Seric */ 72660089Seric 72760089Seric 72860089Seric /* 72960207Seric ** STAB_MAP_LOOKUP -- look up alias in symbol table 73060089Seric */ 73160089Seric 73260089Seric char * 73360089Seric stab_map_lookup(map, name) 73460089Seric register MAP *map; 73560089Seric char *name; 73660089Seric { 73760089Seric register STAB *s; 73860089Seric 73960089Seric if (tTd(27, 20)) 74060089Seric printf("stab_lookup(%s)\n", name); 74160089Seric 74260089Seric s = stab(name, ST_ALIAS, ST_FIND); 74360089Seric if (s != NULL) 74460089Seric return (s->s_alias); 74560089Seric return (NULL); 74660089Seric } 74760089Seric 74860089Seric 74960089Seric /* 75060207Seric ** STAB_MAP_STORE -- store in symtab (actually using during init, not rebuild) 75160089Seric */ 75260089Seric 75360089Seric void 75460089Seric stab_map_store(map, lhs, rhs) 75560089Seric register MAP *map; 75660089Seric char *lhs; 75760089Seric char *rhs; 75860089Seric { 75960089Seric register STAB *s; 76060089Seric 76160089Seric s = stab(lhs, ST_ALIAS, ST_ENTER); 76260089Seric s->s_alias = newstr(rhs); 76360089Seric } 76460089Seric 76560089Seric 76660089Seric /* 76760207Seric ** STAB_MAP_OPEN -- initialize (reads data file) 76860207Seric ** 76960207Seric ** This is a wierd case -- it is only intended as a fallback for 77060207Seric ** aliases. For this reason, opens for write (only during a 77160207Seric ** "newaliases") always fails, and opens for read open the 77260207Seric ** actual underlying text file instead of the database. 77360089Seric */ 77460089Seric 77560089Seric bool 77660089Seric stab_map_open(map, mode) 77760089Seric register MAP *map; 77860089Seric int mode; 77960089Seric { 78060089Seric FILE *af; 78160089Seric 78260089Seric if (tTd(27, 2)) 78360089Seric printf("stab_map_open(%s)\n", map->map_file); 78460089Seric 78560089Seric if (mode != O_RDONLY) 78660207Seric { 78760207Seric errno = ENODEV; 78860089Seric return FALSE; 78960207Seric } 79060089Seric 79160089Seric return TRUE; 79260089Seric } 79360089Seric 79460089Seric 79560089Seric /* 79660207Seric ** STAB_MAP_CLOSE -- close symbol table (???) 79760089Seric */ 79860089Seric 79960089Seric void 80060089Seric stab_map_close(map) 80160089Seric MAP *map; 80260089Seric { 80360089Seric /* ignore it */ 80460089Seric } 80560089Seric /* 80660089Seric ** Implicit Modules 80756822Seric ** 80860089Seric ** Tries several types. For back compatibility of aliases. 80956822Seric */ 81056822Seric 81160089Seric 81260089Seric /* 81360207Seric ** IMPL_MAP_LOOKUP -- lookup in best open database 81460089Seric */ 81560089Seric 81660089Seric char * 81760089Seric impl_map_lookup(map, name, av, pstat) 81860089Seric MAP *map; 81960089Seric char *name; 82056822Seric char **av; 82160089Seric int *pstat; 82256822Seric { 82360089Seric if (tTd(27, 20)) 82460089Seric printf("impl_map_lookup(%s)\n", name); 82556822Seric 82660089Seric #ifdef NEWDB 82760207Seric if (bitset(MF_IMPL_HASH, map->map_mflags)) 82860089Seric return db_map_lookup(map, name, av, pstat); 82960089Seric #endif 83060089Seric #ifdef NDBM 83160207Seric if (bitset(MF_IMPL_NDBM, map->map_mflags)) 83260089Seric return ndbm_map_lookup(map, name, av, pstat); 83360089Seric #endif 83460089Seric return stab_map_lookup(map, name, av, pstat); 83560089Seric } 83660089Seric 83760089Seric /* 83860207Seric ** IMPL_MAP_STORE -- store in open databases 83960089Seric */ 84060089Seric 84160089Seric void 84260089Seric impl_map_store(map, lhs, rhs) 84360089Seric MAP *map; 84460089Seric char *lhs; 84560089Seric char *rhs; 84660089Seric { 84760089Seric #ifdef NEWDB 84860207Seric if (bitset(MF_IMPL_HASH, map->map_mflags)) 84960089Seric db_map_store(map, lhs, rhs); 85060089Seric #endif 85160089Seric #ifdef NDBM 85260207Seric if (bitset(MF_IMPL_NDBM, map->map_mflags)) 85360089Seric ndbm_map_store(map, lhs, rhs); 85460089Seric #endif 85560089Seric stab_map_store(map, lhs, rhs); 85660089Seric } 85760089Seric 85860089Seric /* 85960089Seric ** IMPL_MAP_OPEN -- implicit database open 86060089Seric */ 86160089Seric 86260089Seric bool 86360089Seric impl_map_open(map, mode) 86460089Seric MAP *map; 86560089Seric int mode; 86660089Seric { 86760089Seric struct stat stb; 86860089Seric 86960089Seric if (tTd(27, 2)) 87060089Seric printf("impl_map_open(%s)\n", map->map_file); 87160089Seric 87260089Seric if (stat(map->map_file, &stb) < 0) 87356822Seric { 87460089Seric /* no alias file at all */ 87560089Seric return FALSE; 87656822Seric } 87756822Seric 87860089Seric #ifdef NEWDB 87960207Seric map->map_mflags |= MF_IMPL_HASH; 88060089Seric if (hash_map_open(map, mode)) 88156822Seric { 88260207Seric #if defined(NDBM) && defined(YPCOMPAT) 88360207Seric if (mode == O_RDONLY || access("/var/yp/Makefile", R_OK) == 0) 88460207Seric #endif 88560207Seric return TRUE; 88660089Seric } 88760207Seric else 88860207Seric map->map_mflags &= ~MF_IMPL_HASH; 88960089Seric #endif 89060089Seric #ifdef NDBM 89160207Seric map->map_mflags |= MF_IMPL_NDBM; 89260089Seric if (ndbm_map_open(map, mode)) 89360089Seric { 89460089Seric return TRUE; 89560089Seric } 89660207Seric else 89760207Seric map->map_mflags &= ~MF_IMPL_NDBM; 89860089Seric #endif 89956822Seric 90060207Seric #if !defined(NEWDB) && !defined(NDBM) 90160089Seric if (Verbose) 90260089Seric message("WARNING: cannot open alias database %s", map->map_file); 90360207Seric #endif 90460089Seric 90560207Seric return stab_map_open(map, mode); 90656822Seric } 90760089Seric 90860207Seric 90960089Seric /* 91060207Seric ** IMPL_MAP_CLOSE -- close any open database(s) 91160089Seric */ 91260089Seric 91360089Seric void 91460207Seric impl_map_close(map) 91560089Seric MAP *map; 91660089Seric { 91760089Seric #ifdef NEWDB 91860207Seric if (bitset(MF_IMPL_HASH, map->map_mflags)) 91960089Seric { 92060207Seric db_map_close(map); 92160207Seric map->map_mflags &= ~MF_IMPL_HASH; 92260089Seric } 92360089Seric #endif 92460089Seric 92560089Seric #ifdef NDBM 92660207Seric if (bitset(MF_IMPL_NDBM, map->map_mflags)) 92760089Seric { 92860207Seric ndbm_map_close(map); 92960207Seric map->map_mflags &= ~MF_IMPL_NDBM; 93060089Seric } 93160089Seric #endif 93260089Seric } 93360207Seric /* 93460207Seric ** NULL stubs 93560089Seric */ 93660089Seric 93760207Seric bool 93860207Seric null_map_open(map, mode) 93960089Seric MAP *map; 94060207Seric int mode; 94160089Seric { 94260207Seric return TRUE; 94360089Seric } 94460089Seric 94560207Seric void 94660207Seric null_map_close(map) 94760207Seric MAP *map; 94860089Seric { 94960207Seric return; 95060207Seric } 95160089Seric 95260207Seric void 95360207Seric null_map_store(map, key, val) 95460207Seric MAP *map; 95560207Seric char *key; 95660207Seric char *val; 95760089Seric { 95860207Seric return; 95960089Seric } 960