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*68735Seric static char sccsid[] = "@(#)map.c 8.52 (Berkeley) 04/07/95"; 1156822Seric #endif /* not lint */ 1256822Seric 1356822Seric #include "sendmail.h" 1456822Seric 1560089Seric #ifdef NDBM 1668509Seric # include <ndbm.h> 1756822Seric #endif 1860089Seric #ifdef NEWDB 1968509Seric # include <db.h> 2056822Seric #endif 2160089Seric #ifdef NIS 2268524Seric struct dom_binding { int dummy; }; /* needed on IRIX */ 2368509Seric # include <rpcsvc/ypclnt.h> 2457208Seric #endif 2556822Seric 2656822Seric /* 2760089Seric ** MAP.C -- implementations for various map classes. 2856822Seric ** 2960089Seric ** Each map class implements a series of functions: 3060089Seric ** 3160089Seric ** bool map_parse(MAP *map, char *args) 3260089Seric ** Parse the arguments from the config file. Return TRUE 3360089Seric ** if they were ok, FALSE otherwise. Fill in map with the 3460089Seric ** values. 3560089Seric ** 3660222Seric ** char *map_lookup(MAP *map, char *key, char **args, int *pstat) 3760222Seric ** Look up the key in the given map. If found, do any 3860222Seric ** rewriting the map wants (including "args" if desired) 3960089Seric ** and return the value. Set *pstat to the appropriate status 4060222Seric ** on error and return NULL. Args will be NULL if called 4160222Seric ** from the alias routines, although this should probably 4260222Seric ** not be relied upon. It is suggested you call map_rewrite 4360222Seric ** to return the results -- it takes care of null termination 4460222Seric ** and uses a dynamically expanded buffer as needed. 4560089Seric ** 4660089Seric ** void map_store(MAP *map, char *key, char *value) 4760089Seric ** Store the key:value pair in the map. 4860089Seric ** 4960089Seric ** bool map_open(MAP *map, int mode) 5060222Seric ** Open the map for the indicated mode. Mode should 5160222Seric ** be either O_RDONLY or O_RDWR. Return TRUE if it 5260222Seric ** was opened successfully, FALSE otherwise. If the open 5360222Seric ** failed an the MF_OPTIONAL flag is not set, it should 5460222Seric ** also print an error. If the MF_ALIAS bit is set 5560222Seric ** and this map class understands the @:@ convention, it 5660222Seric ** should call aliaswait() before returning. 5760089Seric ** 5860089Seric ** void map_close(MAP *map) 5960089Seric ** Close the map. 6060089Seric */ 6160089Seric 6260089Seric #define DBMMODE 0644 6364718Seric 6464718Seric extern bool aliaswait __P((MAP *, char *, int)); 6560089Seric /* 6660089Seric ** MAP_PARSEARGS -- parse config line arguments for database lookup 6760089Seric ** 6860089Seric ** This is a generic version of the map_parse method. 6960089Seric ** 7056822Seric ** Parameters: 7160089Seric ** map -- the map being initialized. 7260089Seric ** ap -- a pointer to the args on the config line. 7356822Seric ** 7456822Seric ** Returns: 7560089Seric ** TRUE -- if everything parsed OK. 7656822Seric ** FALSE -- otherwise. 7756822Seric ** 7856822Seric ** Side Effects: 7960089Seric ** null terminates the filename; stores it in map 8056822Seric */ 8156822Seric 8256822Seric bool 8360089Seric map_parseargs(map, ap) 8456822Seric MAP *map; 8560089Seric char *ap; 8656822Seric { 8760089Seric register char *p = ap; 8856822Seric 8963753Seric map->map_mflags |= MF_TRY0NULL | MF_TRY1NULL; 9060089Seric for (;;) 9160089Seric { 9260089Seric while (isascii(*p) && isspace(*p)) 9360089Seric p++; 9460089Seric if (*p != '-') 9560089Seric break; 9660089Seric switch (*++p) 9760089Seric { 9860089Seric case 'N': 9960207Seric map->map_mflags |= MF_INCLNULL; 10063753Seric map->map_mflags &= ~MF_TRY0NULL; 10160089Seric break; 10260089Seric 10363753Seric case 'O': 10463753Seric map->map_mflags &= ~MF_TRY1NULL; 10563753Seric break; 10663753Seric 10760089Seric case 'o': 10860207Seric map->map_mflags |= MF_OPTIONAL; 10960089Seric break; 11060089Seric 11160089Seric case 'f': 11260207Seric map->map_mflags |= MF_NOFOLDCASE; 11360089Seric break; 11460089Seric 11560089Seric case 'm': 11660207Seric map->map_mflags |= MF_MATCHONLY; 11760089Seric break; 11860089Seric 11968497Seric case 'A': 12068497Seric map->map_mflags |= MF_APPEND; 12168497Seric break; 12268497Seric 12360089Seric case 'a': 12460089Seric map->map_app = ++p; 12560089Seric break; 12668350Seric 12768350Seric case 'k': 12868350Seric while (isascii(*++p) && isspace(*p)) 12968350Seric continue; 13068350Seric map->map_keycolnm = p; 13168350Seric break; 13268350Seric 13368350Seric case 'v': 13468350Seric while (isascii(*++p) && isspace(*p)) 13568350Seric continue; 13668350Seric map->map_valcolnm = p; 13768350Seric break; 13868350Seric 13968350Seric case 'z': 14068350Seric if (*++p != '\\') 14168350Seric map->map_coldelim = *p; 14268350Seric else 14368350Seric { 14468350Seric switch (*++p) 14568350Seric { 14668350Seric case 'n': 14768350Seric map->map_coldelim = '\n'; 14868350Seric break; 14968350Seric 15068350Seric case 't': 15168350Seric map->map_coldelim = '\t'; 15268350Seric break; 15368350Seric 15468350Seric default: 15568350Seric map->map_coldelim = '\\'; 15668350Seric } 15768350Seric } 15868350Seric break; 15968497Seric #ifdef RESERVED_FOR_SUN 16068497Seric case 'd': 16168497Seric map->map_mflags |= MF_DOMAIN_WIDE; 16268497Seric break; 16368497Seric 16468497Seric case 's': 16568497Seric /* info type */ 16668497Seric break; 16768497Seric #endif 16860089Seric } 16960089Seric while (*p != '\0' && !(isascii(*p) && isspace(*p))) 17060089Seric p++; 17160089Seric if (*p != '\0') 17260089Seric *p++ = '\0'; 17360089Seric } 17460089Seric if (map->map_app != NULL) 17560089Seric map->map_app = newstr(map->map_app); 17668350Seric if (map->map_keycolnm != NULL) 17768350Seric map->map_keycolnm = newstr(map->map_keycolnm); 17868350Seric if (map->map_valcolnm != NULL) 17968350Seric map->map_valcolnm = newstr(map->map_valcolnm); 18060089Seric 18160089Seric if (*p != '\0') 18260089Seric { 18360089Seric map->map_file = p; 18460089Seric while (*p != '\0' && !(isascii(*p) && isspace(*p))) 18560089Seric p++; 18660089Seric if (*p != '\0') 18760089Seric *p++ = '\0'; 18860089Seric map->map_file = newstr(map->map_file); 18960089Seric } 19060089Seric 19160089Seric while (*p != '\0' && isascii(*p) && isspace(*p)) 19260089Seric p++; 19360089Seric if (*p != '\0') 19460089Seric map->map_rebuild = newstr(p); 19560089Seric 19668350Seric if (map->map_file == NULL && 19768350Seric !bitset(MCF_OPTFILE, map->map_class->map_cflags)) 19857208Seric { 19960089Seric syserr("No file name for %s map %s", 20060089Seric map->map_class->map_cname, map->map_mname); 20156822Seric return FALSE; 20257208Seric } 20360089Seric return TRUE; 20460089Seric } 20560089Seric /* 20660089Seric ** MAP_REWRITE -- rewrite a database key, interpolating %n indications. 20760089Seric ** 20860089Seric ** It also adds the map_app string. It can be used as a utility 20960089Seric ** in the map_lookup method. 21060089Seric ** 21160089Seric ** Parameters: 21260089Seric ** map -- the map that causes this. 21360089Seric ** s -- the string to rewrite, NOT necessarily null terminated. 21460089Seric ** slen -- the length of s. 21560089Seric ** av -- arguments to interpolate into buf. 21660089Seric ** 21760089Seric ** Returns: 21867895Seric ** Pointer to rewritten result. This is static data that 21967895Seric ** should be copied if it is to be saved! 22060089Seric ** 22160089Seric ** Side Effects: 22260089Seric ** none. 22360089Seric */ 22460089Seric 22560089Seric char * 22660089Seric map_rewrite(map, s, slen, av) 22760089Seric register MAP *map; 22860089Seric register char *s; 22960089Seric int slen; 23060089Seric char **av; 23160089Seric { 23260089Seric register char *bp; 23360089Seric register char c; 23460089Seric char **avp; 23560089Seric register char *ap; 23660089Seric int i; 23760089Seric int len; 23867895Seric static int buflen = -1; 23967895Seric static char *buf = NULL; 24060089Seric 24160537Seric if (tTd(39, 1)) 24260089Seric { 24360256Seric printf("map_rewrite(%.*s), av =", slen, s); 24460256Seric if (av == NULL) 24560256Seric printf(" (nullv)"); 24660256Seric else 24760256Seric { 24860256Seric for (avp = av; *avp != NULL; avp++) 24960256Seric printf("\n\t%s", *avp); 25060256Seric } 25160256Seric printf("\n"); 25260089Seric } 25360089Seric 25460089Seric /* count expected size of output (can safely overestimate) */ 25560089Seric i = len = slen; 25660089Seric if (av != NULL) 25760089Seric { 25860089Seric bp = s; 25960089Seric for (i = slen; --i >= 0 && (c = *bp++) != 0; ) 26060089Seric { 26160089Seric if (c != '%') 26260089Seric continue; 26360089Seric if (--i < 0) 26460089Seric break; 26560089Seric c = *bp++; 26660089Seric if (!(isascii(c) && isdigit(c))) 26760089Seric continue; 26863937Seric for (avp = av; --c >= '0' && *avp != NULL; avp++) 26960089Seric continue; 27060089Seric if (*avp == NULL) 27160089Seric continue; 27260089Seric len += strlen(*avp); 27360089Seric } 27460089Seric } 27560089Seric if (map->map_app != NULL) 27660089Seric len += strlen(map->map_app); 27767895Seric if (buflen < ++len) 27860089Seric { 27960089Seric /* need to malloc additional space */ 28067895Seric buflen = len; 28167895Seric if (buf != NULL) 28267895Seric free(buf); 28367895Seric buf = xalloc(buflen); 28460089Seric } 28560089Seric 28667895Seric bp = buf; 28760089Seric if (av == NULL) 28860089Seric { 28960089Seric bcopy(s, bp, slen); 29060089Seric bp += slen; 29160089Seric } 29260089Seric else 29360089Seric { 29460089Seric while (--slen >= 0 && (c = *s++) != '\0') 29560089Seric { 29660089Seric if (c != '%') 29760089Seric { 29860089Seric pushc: 29960089Seric *bp++ = c; 30060089Seric continue; 30160089Seric } 30260089Seric if (--slen < 0 || (c = *s++) == '\0') 30360089Seric c = '%'; 30460089Seric if (c == '%') 30560089Seric goto pushc; 30660089Seric if (!(isascii(c) && isdigit(c))) 30760089Seric { 30860089Seric *bp++ = '%'; 30960089Seric goto pushc; 31060089Seric } 31163937Seric for (avp = av; --c >= '0' && *avp != NULL; avp++) 31260089Seric continue; 31360089Seric if (*avp == NULL) 31460089Seric continue; 31560089Seric 31660089Seric /* transliterate argument into output string */ 31760089Seric for (ap = *avp; (c = *ap++) != '\0'; ) 31860089Seric *bp++ = c; 31960089Seric } 32060089Seric } 32160089Seric if (map->map_app != NULL) 32260089Seric strcpy(bp, map->map_app); 32360089Seric else 32460089Seric *bp = '\0'; 32560537Seric if (tTd(39, 1)) 32667895Seric printf("map_rewrite => %s\n", buf); 32767895Seric return buf; 32860089Seric } 32960089Seric /* 33060537Seric ** INITMAPS -- initialize for aliasing 33160537Seric ** 33260537Seric ** Parameters: 33360537Seric ** rebuild -- if TRUE, this rebuilds the cached versions. 33460537Seric ** e -- current envelope. 33560537Seric ** 33660537Seric ** Returns: 33760537Seric ** none. 33860537Seric ** 33960537Seric ** Side Effects: 34060537Seric ** initializes aliases: 34160537Seric ** if NDBM: opens the database. 34260537Seric ** if ~NDBM: reads the aliases into the symbol table. 34360537Seric */ 34460537Seric 34560537Seric initmaps(rebuild, e) 34660537Seric bool rebuild; 34760537Seric register ENVELOPE *e; 34860537Seric { 34960537Seric extern void map_init(); 35060537Seric 35164671Seric #ifdef XDEBUG 35264671Seric checkfd012("entering initmaps"); 35364671Seric #endif 35460537Seric CurEnv = e; 35565085Seric if (rebuild) 35665085Seric { 35765085Seric stabapply(map_init, 1); 35865085Seric stabapply(map_init, 2); 35965085Seric } 36065085Seric else 36165085Seric { 36265085Seric stabapply(map_init, 0); 36365085Seric } 36464671Seric #ifdef XDEBUG 36564671Seric checkfd012("exiting initmaps"); 36664671Seric #endif 36760537Seric } 36860537Seric 36960537Seric void 37060537Seric map_init(s, rebuild) 37160537Seric register STAB *s; 37260537Seric int rebuild; 37360537Seric { 37460537Seric register MAP *map; 37560537Seric 37660537Seric /* has to be a map */ 37760537Seric if (s->s_type != ST_MAP) 37860537Seric return; 37960537Seric 38060537Seric map = &s->s_map; 38160537Seric if (!bitset(MF_VALID, map->map_mflags)) 38260537Seric return; 38360537Seric 38460537Seric if (tTd(38, 2)) 38568350Seric printf("map_init(%s:%s, %s, %d)\n", 38664690Seric map->map_class->map_cname == NULL ? "NULL" : 38764690Seric map->map_class->map_cname, 38868350Seric map->map_mname == NULL ? "NULL" : map->map_mname, 38965085Seric map->map_file == NULL ? "NULL" : map->map_file, 39065085Seric rebuild); 39160537Seric 39265085Seric if (rebuild == (bitset(MF_ALIAS, map->map_mflags) && 39365085Seric bitset(MCF_REBUILDABLE, map->map_class->map_cflags) ? 1 : 2)) 39465085Seric { 39565085Seric if (tTd(38, 3)) 39665085Seric printf("\twrong pass\n"); 39765085Seric return; 39865085Seric } 39965085Seric 40060537Seric /* if already open, close it (for nested open) */ 40160537Seric if (bitset(MF_OPEN, map->map_mflags)) 40260537Seric { 40360537Seric map->map_class->map_close(map); 40460537Seric map->map_mflags &= ~(MF_OPEN|MF_WRITABLE); 40560537Seric } 40660537Seric 40765085Seric if (rebuild == 2) 40860537Seric { 40965085Seric rebuildaliases(map, FALSE); 41060537Seric } 41160537Seric else 41260537Seric { 41360537Seric if (map->map_class->map_open(map, O_RDONLY)) 41460537Seric { 41560537Seric if (tTd(38, 4)) 41668350Seric printf("\t%s:%s %s: valid\n", 41764690Seric map->map_class->map_cname == NULL ? "NULL" : 41864690Seric map->map_class->map_cname, 41968350Seric map->map_mname == NULL ? "NULL" : 42068350Seric map->map_mname, 42164690Seric map->map_file == NULL ? "NULL" : 42264690Seric map->map_file); 42360537Seric map->map_mflags |= MF_OPEN; 42460537Seric } 42568350Seric else 42668350Seric { 42768350Seric if (tTd(38, 4)) 42868350Seric printf("\t%s:%s %s: invalid: %s\n", 42968350Seric map->map_class->map_cname == NULL ? "NULL" : 43068350Seric map->map_class->map_cname, 43168350Seric map->map_mname == NULL ? "NULL" : 43268350Seric map->map_mname, 43368350Seric map->map_file == NULL ? "NULL" : 43468350Seric map->map_file, 43568350Seric errstring(errno)); 43668350Seric if (!bitset(MF_OPTIONAL, map->map_mflags)) 43768350Seric { 43868350Seric extern MAPCLASS BogusMapClass; 43968350Seric 44068350Seric map->map_class = &BogusMapClass; 44168350Seric map->map_mflags |= MF_OPEN; 44268350Seric } 44368350Seric } 44460537Seric } 44560537Seric } 44660537Seric /* 44760089Seric ** NDBM modules 44860089Seric */ 44960089Seric 45060089Seric #ifdef NDBM 45160089Seric 45260089Seric /* 45360089Seric ** DBM_MAP_OPEN -- DBM-style map open 45460089Seric */ 45560089Seric 45660089Seric bool 45760089Seric ndbm_map_open(map, mode) 45860089Seric MAP *map; 45960089Seric int mode; 46060089Seric { 46164284Seric register DBM *dbm; 46264284Seric struct stat st; 46360089Seric 46460537Seric if (tTd(38, 2)) 46568350Seric printf("ndbm_map_open(%s, %s, %d)\n", 46668350Seric map->map_mname, map->map_file, mode); 46760089Seric 46860207Seric if (mode == O_RDWR) 46960207Seric mode |= O_CREAT|O_TRUNC; 47060207Seric 47160089Seric /* open the database */ 47260089Seric dbm = dbm_open(map->map_file, mode, DBMMODE); 47356822Seric if (dbm == NULL) 47456822Seric { 47564718Seric if (aliaswait(map, ".pag", FALSE)) 47664718Seric return TRUE; 47760207Seric if (!bitset(MF_OPTIONAL, map->map_mflags)) 47856836Seric syserr("Cannot open DBM database %s", map->map_file); 47956822Seric return FALSE; 48056822Seric } 48160089Seric map->map_db1 = (void *) dbm; 48264964Seric if (mode == O_RDONLY) 48364964Seric { 48464964Seric if (bitset(MF_ALIAS, map->map_mflags) && 48564964Seric !aliaswait(map, ".pag", TRUE)) 48664718Seric return FALSE; 48764964Seric } 48864964Seric else 48964964Seric { 49064964Seric int fd; 49164964Seric 49264964Seric /* exclusive lock for duration of rebuild */ 49364964Seric fd = dbm_dirfno((DBM *) map->map_db1); 49464964Seric if (fd >= 0 && !bitset(MF_LOCKED, map->map_mflags) && 49564964Seric lockfile(fd, map->map_file, ".dir", LOCK_EX)) 49664964Seric map->map_mflags |= MF_LOCKED; 49764964Seric } 49864718Seric if (fstat(dbm_dirfno((DBM *) map->map_db1), &st) >= 0) 49964284Seric map->map_mtime = st.st_mtime; 50056822Seric return TRUE; 50156822Seric } 50260089Seric 50360089Seric 50460089Seric /* 50556822Seric ** DBM_MAP_LOOKUP -- look up a datum in a DBM-type map 50656822Seric */ 50756822Seric 50856822Seric char * 50960089Seric ndbm_map_lookup(map, name, av, statp) 51056822Seric MAP *map; 51160089Seric char *name; 51256822Seric char **av; 51359084Seric int *statp; 51456822Seric { 51556822Seric datum key, val; 51664373Seric int fd; 51760089Seric char keybuf[MAXNAME + 1]; 51856822Seric 51960537Seric if (tTd(38, 20)) 52068350Seric printf("ndbm_map_lookup(%s, %s)\n", 52168350Seric map->map_mname, name); 52260089Seric 52360089Seric key.dptr = name; 52460089Seric key.dsize = strlen(name); 52560207Seric if (!bitset(MF_NOFOLDCASE, map->map_mflags)) 52657014Seric { 52760089Seric if (key.dsize > sizeof keybuf - 1) 52860089Seric key.dsize = sizeof keybuf - 1; 52960089Seric bcopy(key.dptr, keybuf, key.dsize + 1); 53060089Seric makelower(keybuf); 53160089Seric key.dptr = keybuf; 53257014Seric } 53364373Seric fd = dbm_dirfno((DBM *) map->map_db1); 53464388Seric if (fd >= 0 && !bitset(MF_LOCKED, map->map_mflags)) 53564373Seric (void) lockfile(fd, map->map_file, ".dir", LOCK_SH); 53663753Seric val.dptr = NULL; 53763753Seric if (bitset(MF_TRY0NULL, map->map_mflags)) 53863753Seric { 53963753Seric val = dbm_fetch((DBM *) map->map_db1, key); 54063753Seric if (val.dptr != NULL) 54163753Seric map->map_mflags &= ~MF_TRY1NULL; 54263753Seric } 54363753Seric if (val.dptr == NULL && bitset(MF_TRY1NULL, map->map_mflags)) 54463753Seric { 54556822Seric key.dsize++; 54663753Seric val = dbm_fetch((DBM *) map->map_db1, key); 54763753Seric if (val.dptr != NULL) 54863753Seric map->map_mflags &= ~MF_TRY0NULL; 54963753Seric } 55064388Seric if (fd >= 0 && !bitset(MF_LOCKED, map->map_mflags)) 55164373Seric (void) lockfile(fd, map->map_file, ".dir", LOCK_UN); 55256822Seric if (val.dptr == NULL) 55356822Seric return NULL; 55460207Seric if (bitset(MF_MATCHONLY, map->map_mflags)) 55563753Seric return map_rewrite(map, name, strlen(name), NULL); 55663753Seric else 55763753Seric return map_rewrite(map, val.dptr, val.dsize, av); 55856822Seric } 55956822Seric 56056822Seric 56156822Seric /* 56260089Seric ** DBM_MAP_STORE -- store a datum in the database 56356822Seric */ 56456822Seric 56560089Seric void 56660089Seric ndbm_map_store(map, lhs, rhs) 56760089Seric register MAP *map; 56860089Seric char *lhs; 56960089Seric char *rhs; 57060089Seric { 57160089Seric datum key; 57260089Seric datum data; 57360089Seric int stat; 57460089Seric 57560537Seric if (tTd(38, 12)) 57668350Seric printf("ndbm_map_store(%s, %s, %s)\n", 57768350Seric map->map_mname, lhs, rhs); 57860089Seric 57960089Seric key.dsize = strlen(lhs); 58060089Seric key.dptr = lhs; 58160089Seric 58260089Seric data.dsize = strlen(rhs); 58360089Seric data.dptr = rhs; 58460089Seric 58560207Seric if (bitset(MF_INCLNULL, map->map_mflags)) 58660089Seric { 58760089Seric key.dsize++; 58860089Seric data.dsize++; 58960089Seric } 59060089Seric 59160089Seric stat = dbm_store((DBM *) map->map_db1, key, data, DBM_INSERT); 59260089Seric if (stat > 0) 59360089Seric { 59468497Seric if (!bitset(MF_APPEND, map->map_mflags)) 59568497Seric usrerr("050 Warning: duplicate alias name %s", lhs); 59668497Seric else 59768497Seric { 59868497Seric static char *buf = NULL; 59968497Seric static int bufsiz = 0; 60068497Seric datum old; 60168497Seric 60268497Seric old.dptr = ndbm_map_lookup(map, key.dptr, NULL); 60368497Seric if (old.dptr != NULL && *old.dptr != '\0') 60468497Seric { 60568497Seric old.dsize = strlen(old.dptr); 60668497Seric if (data.dsize + old.dsize + 2 > bufsiz) 60768497Seric { 60868497Seric if (buf != NULL) 60968497Seric (void) free(buf); 61068497Seric bufsiz = data.dsize + old.dsize + 2; 61168497Seric buf = xalloc(bufsiz); 61268497Seric } 61368497Seric sprintf(buf, "%s,%s", data.dptr, old.dptr); 61468497Seric data.dsize = data.dsize + old.dsize + 1; 61568497Seric data.dptr = buf; 61668497Seric if (tTd(38, 9)) 61768497Seric printf("ndbm_map_store append=%s\n", data.dptr); 61868497Seric } 61968497Seric } 62060089Seric stat = dbm_store((DBM *) map->map_db1, key, data, DBM_REPLACE); 62160089Seric } 62260089Seric if (stat != 0) 62360089Seric syserr("readaliases: dbm put (%s)", lhs); 62460089Seric } 62560089Seric 62660089Seric 62760089Seric /* 62860207Seric ** NDBM_MAP_CLOSE -- close the database 62960089Seric */ 63060089Seric 63160089Seric void 63260089Seric ndbm_map_close(map) 63360089Seric register MAP *map; 63460089Seric { 63566773Seric if (tTd(38, 9)) 63668350Seric printf("ndbm_map_close(%s, %s, %x)\n", 63768350Seric map->map_mname, map->map_file, map->map_mflags); 63866773Seric 63960207Seric if (bitset(MF_WRITABLE, map->map_mflags)) 64060089Seric { 64164250Seric #ifdef NIS 64264075Seric bool inclnull; 64360089Seric char buf[200]; 64460089Seric 64564075Seric inclnull = bitset(MF_INCLNULL, map->map_mflags); 64664075Seric map->map_mflags &= ~MF_INCLNULL; 64764075Seric 64860089Seric (void) sprintf(buf, "%010ld", curtime()); 64960089Seric ndbm_map_store(map, "YP_LAST_MODIFIED", buf); 65060089Seric 65164941Seric (void) gethostname(buf, sizeof buf); 65260089Seric ndbm_map_store(map, "YP_MASTER_NAME", buf); 65364075Seric 65464075Seric if (inclnull) 65564075Seric map->map_mflags |= MF_INCLNULL; 65660089Seric #endif 65760089Seric 65860089Seric /* write out the distinguished alias */ 65960089Seric ndbm_map_store(map, "@", "@"); 66060089Seric } 66160089Seric dbm_close((DBM *) map->map_db1); 66260089Seric } 66360089Seric 66460089Seric #endif 66560089Seric /* 66660582Seric ** NEWDB (Hash and BTree) Modules 66760089Seric */ 66860089Seric 66960089Seric #ifdef NEWDB 67060089Seric 67160089Seric /* 67260582Seric ** BT_MAP_OPEN, HASH_MAP_OPEN -- database open primitives. 67360582Seric ** 67460582Seric ** These do rather bizarre locking. If you can lock on open, 67560582Seric ** do that to avoid the condition of opening a database that 67660582Seric ** is being rebuilt. If you don't, we'll try to fake it, but 67760582Seric ** there will be a race condition. If opening for read-only, 67860582Seric ** we immediately release the lock to avoid freezing things up. 67960582Seric ** We really ought to hold the lock, but guarantee that we won't 68060582Seric ** be pokey about it. That's hard to do. 68160089Seric */ 68260089Seric 68356822Seric bool 68460089Seric bt_map_open(map, mode) 68556822Seric MAP *map; 68660089Seric int mode; 68756822Seric { 68856822Seric DB *db; 68960228Seric int i; 69060582Seric int omode; 69164373Seric int fd; 69264284Seric struct stat st; 69368528Seric char buf[MAXNAME + 1]; 69456822Seric 69560537Seric if (tTd(38, 2)) 69668350Seric printf("bt_map_open(%s, %s, %d)\n", 69768350Seric map->map_mname, map->map_file, mode); 69860089Seric 69960582Seric omode = mode; 70060582Seric if (omode == O_RDWR) 70160582Seric { 70260582Seric omode |= O_CREAT|O_TRUNC; 70365830Seric #if defined(O_EXLOCK) && HASFLOCK 70460582Seric omode |= O_EXLOCK; 70566843Seric # if !OLD_NEWDB 70660582Seric } 70760582Seric else 70860582Seric { 70960582Seric omode |= O_SHLOCK; 71060582Seric # endif 71160582Seric #endif 71260582Seric } 71360207Seric 71460228Seric (void) strcpy(buf, map->map_file); 71560228Seric i = strlen(buf); 71660228Seric if (i < 3 || strcmp(&buf[i - 3], ".db") != 0) 71760228Seric (void) strcat(buf, ".db"); 71860582Seric db = dbopen(buf, omode, DBMMODE, DB_BTREE, NULL); 71956822Seric if (db == NULL) 72056822Seric { 72164718Seric #ifdef MAYBENEXTRELEASE 72264718Seric if (aliaswait(map, ".db", FALSE)) 72364718Seric return TRUE; 72464718Seric #endif 72560207Seric if (!bitset(MF_OPTIONAL, map->map_mflags)) 72656836Seric syserr("Cannot open BTREE database %s", map->map_file); 72756822Seric return FALSE; 72856822Seric } 72968350Seric #if !OLD_NEWDB 73064373Seric fd = db->fd(db); 73168350Seric # if HASFLOCK 73268350Seric # if !defined(O_EXLOCK) 73364373Seric if (mode == O_RDWR && fd >= 0) 73464388Seric { 73564388Seric if (lockfile(fd, map->map_file, ".db", LOCK_EX)) 73664388Seric map->map_mflags |= MF_LOCKED; 73764388Seric } 73868350Seric # else 73964373Seric if (mode == O_RDONLY && fd >= 0) 74064373Seric (void) lockfile(fd, map->map_file, ".db", LOCK_UN); 74164388Seric else 74264388Seric map->map_mflags |= MF_LOCKED; 74368350Seric # endif 74460582Seric # endif 74560582Seric #endif 74660585Seric 74760585Seric /* try to make sure that at least the database header is on disk */ 74860585Seric if (mode == O_RDWR) 74966843Seric #if OLD_NEWDB 75064373Seric (void) db->sync(db); 75164373Seric #else 75260585Seric (void) db->sync(db, 0); 75360585Seric 75464373Seric if (fd >= 0 && fstat(fd, &st) >= 0) 75564284Seric map->map_mtime = st.st_mtime; 75664284Seric #endif 75764284Seric 75860089Seric map->map_db2 = (void *) db; 75960207Seric if (mode == O_RDONLY && bitset(MF_ALIAS, map->map_mflags)) 76064718Seric if (!aliaswait(map, ".db", TRUE)) 76164718Seric return FALSE; 76256822Seric return TRUE; 76356822Seric } 76456822Seric 76556822Seric 76656822Seric /* 76756822Seric ** HASH_MAP_INIT -- HASH-style map initialization 76856822Seric */ 76956822Seric 77056822Seric bool 77160089Seric hash_map_open(map, mode) 77256822Seric MAP *map; 77360089Seric int mode; 77456822Seric { 77556822Seric DB *db; 77660228Seric int i; 77760582Seric int omode; 77864373Seric int fd; 77964284Seric struct stat st; 78068528Seric char buf[MAXNAME + 1]; 78156822Seric 78260537Seric if (tTd(38, 2)) 78368350Seric printf("hash_map_open(%s, %s, %d)\n", 78468350Seric map->map_mname, map->map_file, mode); 78560089Seric 78660582Seric omode = mode; 78760582Seric if (omode == O_RDWR) 78860582Seric { 78960582Seric omode |= O_CREAT|O_TRUNC; 79065830Seric #if defined(O_EXLOCK) && HASFLOCK 79160582Seric omode |= O_EXLOCK; 79266843Seric # if !OLD_NEWDB 79360582Seric } 79460582Seric else 79560582Seric { 79660582Seric omode |= O_SHLOCK; 79760582Seric # endif 79860582Seric #endif 79960582Seric } 80060207Seric 80160228Seric (void) strcpy(buf, map->map_file); 80260228Seric i = strlen(buf); 80360228Seric if (i < 3 || strcmp(&buf[i - 3], ".db") != 0) 80460228Seric (void) strcat(buf, ".db"); 80560582Seric db = dbopen(buf, omode, DBMMODE, DB_HASH, NULL); 80656822Seric if (db == NULL) 80756822Seric { 80864718Seric #ifdef MAYBENEXTRELEASE 80964718Seric if (aliaswait(map, ".db", FALSE)) 81064718Seric return TRUE; 81164718Seric #endif 81260207Seric if (!bitset(MF_OPTIONAL, map->map_mflags)) 81356836Seric syserr("Cannot open HASH database %s", map->map_file); 81456822Seric return FALSE; 81556822Seric } 81668350Seric #if !OLD_NEWDB 81764373Seric fd = db->fd(db); 81868350Seric # if HASFLOCK 81968350Seric # if !defined(O_EXLOCK) 82064373Seric if (mode == O_RDWR && fd >= 0) 82164388Seric { 82264388Seric if (lockfile(fd, map->map_file, ".db", LOCK_EX)) 82364388Seric map->map_mflags |= MF_LOCKED; 82464388Seric } 82568350Seric # else 82664373Seric if (mode == O_RDONLY && fd >= 0) 82764373Seric (void) lockfile(fd, map->map_file, ".db", LOCK_UN); 82864388Seric else 82964388Seric map->map_mflags |= MF_LOCKED; 83068350Seric # endif 83160582Seric # endif 83260582Seric #endif 83360585Seric 83460585Seric /* try to make sure that at least the database header is on disk */ 83560585Seric if (mode == O_RDWR) 83666843Seric #if OLD_NEWDB 83764373Seric (void) db->sync(db); 83864373Seric #else 83960585Seric (void) db->sync(db, 0); 84060585Seric 84164373Seric if (fd >= 0 && fstat(fd, &st) >= 0) 84264284Seric map->map_mtime = st.st_mtime; 84364284Seric #endif 84464284Seric 84560089Seric map->map_db2 = (void *) db; 84660207Seric if (mode == O_RDONLY && bitset(MF_ALIAS, map->map_mflags)) 84764718Seric if (!aliaswait(map, ".db", TRUE)) 84864718Seric return FALSE; 84956822Seric return TRUE; 85056822Seric } 85156822Seric 85256822Seric 85356822Seric /* 85456822Seric ** DB_MAP_LOOKUP -- look up a datum in a BTREE- or HASH-type map 85556822Seric */ 85656822Seric 85756822Seric char * 85860089Seric db_map_lookup(map, name, av, statp) 85956822Seric MAP *map; 86060089Seric char *name; 86156822Seric char **av; 86259084Seric int *statp; 86356822Seric { 86456822Seric DBT key, val; 86560422Seric register DB *db = (DB *) map->map_db2; 86660422Seric int st; 86760422Seric int saveerrno; 86864373Seric int fd; 86960089Seric char keybuf[MAXNAME + 1]; 87056822Seric 87160537Seric if (tTd(38, 20)) 87268350Seric printf("db_map_lookup(%s, %s)\n", 87368350Seric map->map_mname, name); 87460089Seric 87560089Seric key.size = strlen(name); 87660089Seric if (key.size > sizeof keybuf - 1) 87760089Seric key.size = sizeof keybuf - 1; 87860089Seric key.data = keybuf; 87960089Seric bcopy(name, keybuf, key.size + 1); 88060207Seric if (!bitset(MF_NOFOLDCASE, map->map_mflags)) 88160089Seric makelower(keybuf); 88266843Seric #if !OLD_NEWDB 88364388Seric fd = db->fd(db); 88464388Seric if (fd >= 0 && !bitset(MF_LOCKED, map->map_mflags)) 88564388Seric (void) lockfile(db->fd(db), map->map_file, ".db", LOCK_SH); 88660422Seric #endif 88763753Seric st = 1; 88863753Seric if (bitset(MF_TRY0NULL, map->map_mflags)) 88963753Seric { 89063753Seric st = db->get(db, &key, &val, 0); 89163753Seric if (st == 0) 89263753Seric map->map_mflags &= ~MF_TRY1NULL; 89363753Seric } 89463753Seric if (st != 0 && bitset(MF_TRY1NULL, map->map_mflags)) 89563753Seric { 89663753Seric key.size++; 89763753Seric st = db->get(db, &key, &val, 0); 89863753Seric if (st == 0) 89963753Seric map->map_mflags &= ~MF_TRY0NULL; 90063753Seric } 90160422Seric saveerrno = errno; 90266843Seric #if !OLD_NEWDB 90364388Seric if (fd >= 0 && !bitset(MF_LOCKED, map->map_mflags)) 90464373Seric (void) lockfile(fd, map->map_file, ".db", LOCK_UN); 90560422Seric #endif 90660422Seric if (st != 0) 90760422Seric { 90860422Seric errno = saveerrno; 90960422Seric if (st < 0) 91060422Seric syserr("db_map_lookup: get (%s)", name); 91156822Seric return NULL; 91260422Seric } 91360207Seric if (bitset(MF_MATCHONLY, map->map_mflags)) 91463753Seric return map_rewrite(map, name, strlen(name), NULL); 91563753Seric else 91663753Seric return map_rewrite(map, val.data, val.size, av); 91756822Seric } 91856822Seric 91960089Seric 92060089Seric /* 92160089Seric ** DB_MAP_STORE -- store a datum in the NEWDB database 92256822Seric */ 92356822Seric 92460089Seric void 92560089Seric db_map_store(map, lhs, rhs) 92660089Seric register MAP *map; 92760089Seric char *lhs; 92860089Seric char *rhs; 92956822Seric { 93060089Seric int stat; 93160089Seric DBT key; 93260089Seric DBT data; 93360089Seric register DB *db = map->map_db2; 93456822Seric 93560537Seric if (tTd(38, 20)) 93668350Seric printf("db_map_store(%s, %s, %s)\n", 93768350Seric map->map_mname, lhs, rhs); 93860089Seric 93960089Seric key.size = strlen(lhs); 94060089Seric key.data = lhs; 94160089Seric 94260089Seric data.size = strlen(rhs); 94360089Seric data.data = rhs; 94460089Seric 94560207Seric if (bitset(MF_INCLNULL, map->map_mflags)) 94656822Seric { 94760089Seric key.size++; 94860089Seric data.size++; 94960089Seric } 95056836Seric 95160089Seric stat = db->put(db, &key, &data, R_NOOVERWRITE); 95260089Seric if (stat > 0) 95360089Seric { 95468497Seric if (!bitset(MF_APPEND, map->map_mflags)) 95568497Seric usrerr("050 Warning: duplicate alias name %s", lhs); 95668497Seric else 95768497Seric { 95868497Seric static char *buf = NULL; 95968497Seric static int bufsiz = 0; 96068497Seric DBT old; 96168497Seric 96268497Seric old.data = db_map_lookup(map, key.data, NULL, &stat); 96368497Seric if (old.data != NULL) 96468497Seric { 96568497Seric old.size = strlen(old.data); 96668497Seric if (data.size + old.size + 2 > bufsiz) 96768497Seric { 96868497Seric if (buf != NULL) 96968497Seric (void) free(buf); 97068497Seric bufsiz = data.size + old.size + 2; 97168497Seric buf = xalloc(bufsiz); 97268497Seric } 97368497Seric sprintf(buf, "%s,%s", data.data, old.data); 97468497Seric data.size = data.size + old.size + 1; 97568497Seric data.data = buf; 97668497Seric if (tTd(38, 9)) 97768497Seric printf("db_map_store append=%s\n", data.data); 97868497Seric } 97968497Seric } 98060089Seric stat = db->put(db, &key, &data, 0); 98160089Seric } 98260089Seric if (stat != 0) 98360089Seric syserr("readaliases: db put (%s)", lhs); 98460089Seric } 98556836Seric 98656847Seric 98760089Seric /* 98860089Seric ** DB_MAP_CLOSE -- add distinguished entries and close the database 98960089Seric */ 99060089Seric 99160089Seric void 99260089Seric db_map_close(map) 99360089Seric MAP *map; 99460089Seric { 99560089Seric register DB *db = map->map_db2; 99660089Seric 99760537Seric if (tTd(38, 9)) 99868350Seric printf("db_map_close(%s, %s, %x)\n", 99968350Seric map->map_mname, map->map_file, map->map_mflags); 100060089Seric 100160207Seric if (bitset(MF_WRITABLE, map->map_mflags)) 100258804Seric { 100360089Seric /* write out the distinguished alias */ 100460089Seric db_map_store(map, "@", "@"); 100558804Seric } 100658963Seric 100760089Seric if (db->close(db) != 0) 100860089Seric syserr("readaliases: db close failure"); 100956822Seric } 101057208Seric 101160089Seric #endif 101260089Seric /* 101360089Seric ** NIS Modules 101460089Seric */ 101560089Seric 101660089Seric # ifdef NIS 101760089Seric 101864369Seric # ifndef YPERR_BUSY 101964369Seric # define YPERR_BUSY 16 102064369Seric # endif 102164369Seric 102257208Seric /* 102360089Seric ** NIS_MAP_OPEN -- open DBM map 102457208Seric */ 102557208Seric 102657208Seric bool 102760089Seric nis_map_open(map, mode) 102857208Seric MAP *map; 102960089Seric int mode; 103057208Seric { 103157216Seric int yperr; 103260215Seric register char *p; 103360215Seric auto char *vp; 103460215Seric auto int vsize; 103557216Seric char *master; 103657216Seric 103760537Seric if (tTd(38, 2)) 103868350Seric printf("nis_map_open(%s, %s)\n", 103968350Seric map->map_mname, map->map_file); 104060089Seric 104160207Seric if (mode != O_RDONLY) 104260207Seric { 104364650Seric /* issue a pseudo-error message */ 104464650Seric #ifdef ENOSYS 104564650Seric errno = ENOSYS; 104664650Seric #else 104764650Seric # ifdef EFTYPE 104864650Seric errno = EFTYPE; 104964650Seric # else 105064650Seric errno = ENXIO; 105164650Seric # endif 105264650Seric #endif 105360207Seric return FALSE; 105460207Seric } 105560207Seric 105660089Seric p = strchr(map->map_file, '@'); 105760089Seric if (p != NULL) 105860089Seric { 105960089Seric *p++ = '\0'; 106060089Seric if (*p != '\0') 106160089Seric map->map_domain = p; 106260089Seric } 106360215Seric 106460089Seric if (*map->map_file == '\0') 106560089Seric map->map_file = "mail.aliases"; 106660089Seric 106766157Seric if (map->map_domain == NULL) 106866157Seric { 106966157Seric yperr = yp_get_default_domain(&map->map_domain); 107066157Seric if (yperr != 0) 107166157Seric { 107266744Seric if (!bitset(MF_OPTIONAL, map->map_mflags)) 107368350Seric syserr("421 NIS map %s specified, but NIS not running\n", 107466744Seric map->map_file); 107566157Seric return FALSE; 107666157Seric } 107766157Seric } 107866157Seric 107960215Seric /* check to see if this map actually exists */ 108060089Seric yperr = yp_match(map->map_domain, map->map_file, "@", 1, 108160089Seric &vp, &vsize); 108260537Seric if (tTd(38, 10)) 108360089Seric printf("nis_map_open: yp_match(%s, %s) => %s\n", 108460089Seric map->map_domain, map->map_file, yperr_string(yperr)); 108560089Seric if (yperr == 0 || yperr == YPERR_KEY || yperr == YPERR_BUSY) 108668350Seric { 108768350Seric if (!bitset(MF_ALIAS, map->map_mflags) || 108868350Seric aliaswait(map, NULL, TRUE)) 108968350Seric return TRUE; 109068350Seric } 109160215Seric 109260215Seric if (!bitset(MF_OPTIONAL, map->map_mflags)) 1093*68735Seric { 1094*68735Seric syserr("421 Cannot bind to map %s in domain %s: %s", 1095*68735Seric map->map_file, map->map_domain, yperr_string(yperr)); 1096*68735Seric } 109760215Seric 109860089Seric return FALSE; 109960089Seric } 110060089Seric 110160089Seric 110260089Seric /* 110357208Seric ** NIS_MAP_LOOKUP -- look up a datum in a NIS map 110457208Seric */ 110557208Seric 110657208Seric char * 110760089Seric nis_map_lookup(map, name, av, statp) 110857208Seric MAP *map; 110960089Seric char *name; 111057208Seric char **av; 111159084Seric int *statp; 111257208Seric { 111357208Seric char *vp; 111457642Seric auto int vsize; 111559274Seric int buflen; 111660215Seric int yperr; 111760089Seric char keybuf[MAXNAME + 1]; 111857208Seric 111960537Seric if (tTd(38, 20)) 112068350Seric printf("nis_map_lookup(%s, %s)\n", 112168350Seric map->map_mname, name); 112260089Seric 112360089Seric buflen = strlen(name); 112460089Seric if (buflen > sizeof keybuf - 1) 112560089Seric buflen = sizeof keybuf - 1; 112660089Seric bcopy(name, keybuf, buflen + 1); 112760207Seric if (!bitset(MF_NOFOLDCASE, map->map_mflags)) 112860089Seric makelower(keybuf); 112963753Seric yperr = YPERR_KEY; 113063753Seric if (bitset(MF_TRY0NULL, map->map_mflags)) 113163753Seric { 113263753Seric yperr = yp_match(map->map_domain, map->map_file, keybuf, buflen, 113363753Seric &vp, &vsize); 113463753Seric if (yperr == 0) 113563753Seric map->map_mflags &= ~MF_TRY1NULL; 113663753Seric } 113763753Seric if (yperr == YPERR_KEY && bitset(MF_TRY1NULL, map->map_mflags)) 113863753Seric { 113959274Seric buflen++; 114063753Seric yperr = yp_match(map->map_domain, map->map_file, keybuf, buflen, 114163753Seric &vp, &vsize); 114263753Seric if (yperr == 0) 114363753Seric map->map_mflags &= ~MF_TRY0NULL; 114463753Seric } 114560089Seric if (yperr != 0) 114660089Seric { 114760089Seric if (yperr != YPERR_KEY && yperr != YPERR_BUSY) 114860215Seric map->map_mflags &= ~(MF_VALID|MF_OPEN); 114957208Seric return NULL; 115060089Seric } 115160207Seric if (bitset(MF_MATCHONLY, map->map_mflags)) 115263753Seric return map_rewrite(map, name, strlen(name), NULL); 115363753Seric else 115463753Seric return map_rewrite(map, vp, vsize, av); 115557208Seric } 115657208Seric 115768350Seric #endif 115868350Seric /* 115968350Seric ** NISPLUS Modules 116068350Seric ** 116168350Seric ** This code donated by Sun Microsystems. 116268350Seric */ 116367848Seric 116468350Seric #ifdef NISPLUS 116568350Seric 116668350Seric #undef NIS /* symbol conflict in nis.h */ 116768350Seric #include <rpcsvc/nis.h> 116868350Seric #include <rpcsvc/nislib.h> 116968350Seric 117068350Seric #define EN_col(col) zo_data.objdata_u.en_data.en_cols.en_cols_val[(col)].ec_value.ec_value_val 117168350Seric #define COL_NAME(res,i) ((res->objects.objects_val)->TA_data.ta_cols.ta_cols_val)[i].tc_name 117268350Seric #define COL_MAX(res) ((res->objects.objects_val)->TA_data.ta_cols.ta_cols_len) 117368350Seric #define PARTIAL_NAME(x) ((x)[strlen(x) - 1] != '.') 117468350Seric 117567848Seric /* 117668350Seric ** NISPLUS_MAP_OPEN -- open nisplus table 117767848Seric */ 117867848Seric 117968350Seric bool 118068350Seric nisplus_map_open(map, mode) 118167848Seric MAP *map; 118268350Seric int mode; 118367848Seric { 118468350Seric register char *p; 118568350Seric char qbuf[MAXLINE + NIS_MAXNAMELEN]; 118668350Seric nis_result *res = NULL; 118768350Seric u_int objs_len; 118868350Seric nis_object *obj_ptr; 118968350Seric int retry_cnt, max_col, i; 119068350Seric 119168350Seric if (tTd(38, 2)) 119268350Seric printf("nisplus_map_open(%s, %s, %d)\n", 119368350Seric map->map_mname, map->map_file, mode); 119468350Seric 119568350Seric if (mode != O_RDONLY) 119668350Seric { 119768350Seric errno = ENODEV; 119868350Seric return FALSE; 119968350Seric } 120068350Seric 120168350Seric if (*map->map_file == '\0') 120268350Seric map->map_file = "mail_aliases.org_dir"; 120368350Seric 120468350Seric if (PARTIAL_NAME(map->map_file) && map->map_domain == NULL) 120568350Seric { 120668350Seric /* set default NISPLUS Domain to $m */ 120768350Seric extern char *nisplus_default_domain(); 120868350Seric 120968350Seric map->map_domain = newstr(nisplus_default_domain()); 121068350Seric if (tTd(38, 2)) 121168350Seric printf("nisplus_map_open(%s): using domain %s\n", 121268350Seric map->map_file, map->map_domain); 121368350Seric } 121468350Seric if (!PARTIAL_NAME(map->map_file)) 121568350Seric map->map_domain = newstr(""); 121668350Seric 121768350Seric /* check to see if this map actually exists */ 121868350Seric if (PARTIAL_NAME(map->map_file)) 121968350Seric sprintf(qbuf, "%s.%s", map->map_file, map->map_domain); 122068350Seric else 122168350Seric strcpy(qbuf, map->map_file); 122268350Seric 122368350Seric retry_cnt = 0; 122468350Seric while (res == NULL || res->status != NIS_SUCCESS) 122568350Seric { 122668350Seric res = nis_lookup(qbuf, FOLLOW_LINKS); 122768350Seric switch (res->status) 122868350Seric { 122968350Seric case NIS_SUCCESS: 123068350Seric case NIS_TRYAGAIN: 123168350Seric case NIS_RPCERROR: 123268350Seric case NIS_NAMEUNREACHABLE: 123368350Seric break; 123468350Seric 123568350Seric default: /* all other nisplus errors */ 123668350Seric #if 0 123768350Seric if (!bitset(MF_OPTIONAL, map->map_mflags)) 123868350Seric syserr("421 Cannot find table %s.%s: %s", 123968350Seric map->map_file, map->map_domain, 124068350Seric nis_sperrno(res->status)); 124168350Seric #endif 124268350Seric errno = EBADR; 124368350Seric return FALSE; 124468350Seric } 124568350Seric sleep(2); /* try not to overwhelm hosed server */ 124668350Seric if (retry_cnt++ > 4) 124768350Seric { 124868350Seric errno = EBADR; 124968350Seric return FALSE; 125068350Seric } 125168350Seric } 125268350Seric 125368350Seric if (NIS_RES_NUMOBJ(res) != 1 || 125468350Seric (NIS_RES_OBJECT(res)->zo_data.zo_type != TABLE_OBJ)) 125568350Seric { 125668350Seric if (tTd(38, 10)) 125768350Seric printf("nisplus_map_open: %s is not a table\n", qbuf); 125868350Seric #if 0 125968350Seric if (!bitset(MF_OPTIONAL, map->map_mflags)) 126068350Seric syserr("421 %s.%s: %s is not a table", 126168350Seric map->map_file, map->map_domain, 126268350Seric nis_sperrno(res->status)); 126368350Seric #endif 126468350Seric errno = EBADR; 126568350Seric return FALSE; 126668350Seric } 126768350Seric /* default key column is column 0 */ 126868350Seric if (map->map_keycolnm == NULL) 126968350Seric map->map_keycolnm = newstr(COL_NAME(res,0)); 127068350Seric 127168350Seric max_col = COL_MAX(res); 127268350Seric 127368350Seric /* verify the key column exist */ 127468350Seric for (i=0; i< max_col; i++) 127568350Seric { 127668350Seric if (!strcmp(map->map_keycolnm, COL_NAME(res,i))) 127768350Seric break; 127868350Seric } 127968350Seric if (i == max_col) 128068350Seric { 128168350Seric if (tTd(38, 2)) 128268350Seric printf("nisplus_map_open(%s): can not find key column %s\n", 128368350Seric map->map_file, map->map_keycolnm); 128468350Seric errno = EBADR; 128568350Seric return FALSE; 128668350Seric } 128768350Seric 128868350Seric /* default value column is the last column */ 128968350Seric if (map->map_valcolnm == NULL) 129068350Seric { 129168350Seric map->map_valcolno = max_col - 1; 129268350Seric return TRUE; 129368350Seric } 129468350Seric 129568350Seric for (i=0; i< max_col; i++) 129668350Seric { 129768350Seric if (strcmp(map->map_valcolnm, COL_NAME(res,i)) == 0) 129868350Seric { 129968350Seric map->map_valcolno = i; 130068350Seric return TRUE; 130168350Seric } 130268350Seric } 130368350Seric 130468350Seric if (tTd(38, 2)) 130568350Seric printf("nisplus_map_open(%s): can not find column %s\n", 130668350Seric map->map_file, map->map_keycolnm); 130768350Seric errno = EBADR; 130868350Seric return FALSE; 130967848Seric } 131067848Seric 131167848Seric 131267848Seric /* 131368350Seric ** NISPLUS_MAP_LOOKUP -- look up a datum in a NISPLUS table 131467848Seric */ 131567848Seric 131668350Seric char * 131768350Seric nisplus_map_lookup(map, name, av, statp) 131867848Seric MAP *map; 131968350Seric char *name; 132068350Seric char **av; 132168350Seric int *statp; 132267848Seric { 132368350Seric char *vp; 132468350Seric auto int vsize; 132568350Seric int buflen; 132668350Seric char search_key[MAXNAME + 1]; 132768350Seric char qbuf[MAXLINE + NIS_MAXNAMELEN]; 132868350Seric nis_result *result; 132968350Seric 133068350Seric if (tTd(38, 20)) 133168350Seric printf("nisplus_map_lookup(%s, %s)\n", 133268350Seric map->map_mname, name); 133368350Seric 133468350Seric if (!bitset(MF_OPEN, map->map_mflags)) 133568350Seric { 133668350Seric if (nisplus_map_open(map, O_RDONLY)) 133768350Seric map->map_mflags |= MF_OPEN; 133868350Seric else 133968350Seric { 134068350Seric *statp = EX_UNAVAILABLE; 134168350Seric return NULL; 134268350Seric } 134368350Seric } 134468350Seric 134568350Seric buflen = strlen(name); 134668350Seric if (buflen > sizeof search_key - 1) 134768350Seric buflen = sizeof search_key - 1; 134868350Seric bcopy(name, search_key, buflen + 1); 134968350Seric if (!bitset(MF_NOFOLDCASE, map->map_mflags)) 135068350Seric makelower(search_key); 135168350Seric 135268350Seric /* construct the query */ 135368350Seric if (PARTIAL_NAME(map->map_file)) 135468350Seric sprintf(qbuf, "[%s=%s],%s.%s", map->map_keycolnm, 135568350Seric search_key, map->map_file, map->map_domain); 135668350Seric else 135768350Seric sprintf(qbuf, "[%s=%s],%s", map->map_keycolnm, 135868350Seric search_key, map->map_file); 135968350Seric 136068350Seric if (tTd(38, 20)) 136168350Seric printf("qbuf=%s\n", qbuf); 136268350Seric result = nis_list(qbuf, FOLLOW_LINKS | FOLLOW_PATH, NULL, NULL); 136368350Seric if (result->status == NIS_SUCCESS) 136468350Seric { 136568350Seric int count; 136668350Seric char *str; 136768350Seric 136868350Seric if ((count = NIS_RES_NUMOBJ(result)) != 1) 136968350Seric { 137068350Seric if (LogLevel > 10) 137168350Seric syslog(LOG_WARNING, 137268350Seric "%s:Lookup error, expected 1 entry, got (%d)", 137368350Seric map->map_file, count); 137468350Seric 137568350Seric /* ignore second entry */ 137668350Seric if (tTd(38, 20)) 137768350Seric printf("nisplus_map_lookup(%s), got %d entries, additional entries ignored\n", 137868350Seric name, count); 137968350Seric } 138068350Seric 138168350Seric vp = ((NIS_RES_OBJECT(result))->EN_col(map->map_valcolno)); 138268350Seric /* set the length of the result */ 138368350Seric if (vp == NULL) 138468350Seric vp = ""; 138568350Seric vsize = strlen(vp); 138668350Seric if (tTd(38, 20)) 138768350Seric printf("nisplus_map_lookup(%s), found %s\n", 138868350Seric name, vp); 138968350Seric if (bitset(MF_MATCHONLY, map->map_mflags)) 139068350Seric str = map_rewrite(map, name, strlen(name), NULL); 139168350Seric else 139268350Seric str = map_rewrite(map, vp, vsize, av); 139368350Seric nis_freeresult(result); 139468350Seric #ifdef MAP_EXIT_STAT 139568350Seric *statp = EX_OK; 139668350Seric #endif 139768350Seric return str; 139868350Seric } 139968350Seric else 140068350Seric { 140168350Seric #ifdef MAP_EXIT_STAT 140268350Seric if (result->status == NIS_NOTFOUND) 140368350Seric *statp = EX_NOTFOUND; 140468350Seric else if (result->status == NIS_TRYAGAIN) 140568350Seric *statp = EX_TEMPFAIL; 140668350Seric else 140768350Seric { 140868350Seric *statp = EX_UNAVAILABLE; 140968350Seric map->map_mflags &= ~(MF_VALID|MF_OPEN); 141068350Seric } 141168350Seric #else 141268350Seric if ((result->status != NIS_NOTFOUND) && 141368350Seric (result->status != NIS_TRYAGAIN)) 141468350Seric map->map_mflags &= ~(MF_VALID|MF_OPEN); 141568350Seric #endif 141668350Seric } 141768350Seric if (tTd(38, 20)) 141868350Seric printf("nisplus_map_lookup(%s), failed\n", name); 141968350Seric nis_freeresult(result); 142068350Seric return NULL; 142167848Seric } 142267848Seric 142368350Seric 142468350Seric char * 142568350Seric nisplus_default_domain() 142668350Seric { 142768528Seric static char default_domain[MAXNAME + 1] = ""; 142868350Seric char *p; 142968350Seric 143068350Seric if (default_domain[0] != '\0') 143168350Seric return(default_domain); 143268350Seric 143368458Seric p = nis_local_directory(); 143468350Seric strcpy(default_domain, p); 143568458Seric return default_domain; 143668350Seric } 143768350Seric 143868350Seric #endif /* NISPLUS */ 143967848Seric /* 144068350Seric ** HESIOD Modules 144168350Seric */ 144268350Seric 144368350Seric #ifdef HESIOD 144468350Seric 144568350Seric #include <hesiod.h> 144668350Seric 144768350Seric char * 144868350Seric hes_map_lookup(map, name, av, statp) 144968350Seric MAP *map; 145068350Seric char *name; 145168350Seric char **av; 145268350Seric int *statp; 145368350Seric { 145468350Seric char **hp; 145568350Seric char *retdata = NULL; 145668350Seric int i; 145768350Seric 145868350Seric if (tTd(38, 20)) 145968350Seric printf("hes_map_lookup(%s, %s)\n", map->map_file, name); 146068350Seric 146168350Seric hp = hes_resolve(name, map->map_file); 146268350Seric if (hp == NULL) 146368350Seric return NULL; 146468350Seric 146568350Seric if (hp[0] != NULL) 146668350Seric { 146768350Seric if (tTd(38, 20)) 146868395Seric printf(" %d %s\n", i, hp[0]); 146968350Seric if (bitset(MF_MATCHONLY, map->map_mflags)) 147068350Seric retdata = map_rewrite(map, name, strlen(name), NULL); 147168350Seric else 147268350Seric retdata = map_rewrite(map, hp[0], strlen(hp[0]), av); 147368350Seric } 147468350Seric 147568350Seric for (i = 0; hp[i] != NULL; i++) 147668350Seric free(hp[i]); 147768350Seric free(hp); 147868350Seric return retdata; 147968350Seric } 148068350Seric 148168350Seric #endif 148268350Seric /* 148368350Seric ** NeXT NETINFO Modules 148468350Seric */ 148568350Seric 148668350Seric #ifdef NETINFO 148768350Seric 148868350Seric #define NETINFO_DEFAULT_DIR "/aliases" 148968350Seric #define NETINFO_DEFAULT_PROPERTY "members" 149068350Seric 149168350Seric 149268350Seric /* 149368350Seric ** NI_MAP_OPEN -- open NetInfo Aliases 149468350Seric */ 149568350Seric 149668350Seric bool 149768350Seric ni_map_open(map, mode) 149868350Seric MAP *map; 149968350Seric int mode; 150068350Seric { 150168350Seric char *p; 150268350Seric 150368350Seric if (tTd(38, 20)) 150468350Seric printf("ni_map_open: %s\n", map->map_file); 150568350Seric 150668350Seric if (*map->map_file == '\0') 150768350Seric map->map_file = NETINFO_DEFAULT_DIR; 150868350Seric 150968350Seric if (map->map_valcolnm == NULL) 151068350Seric map->map_valcolnm = NETINFO_DEFAULT_PROPERTY; 151168350Seric 151268350Seric if (map->map_coldelim == '\0' && bitset(MF_ALIAS, map->map_mflags)) 151368350Seric map->map_coldelim = ','; 151468350Seric 151568350Seric return TRUE; 151668350Seric } 151768350Seric 151868350Seric 151968350Seric /* 152068350Seric ** NI_MAP_LOOKUP -- look up a datum in NetInfo 152168350Seric */ 152268350Seric 152368350Seric char * 152468350Seric ni_map_lookup(map, name, av, statp) 152568350Seric MAP *map; 152668350Seric char *name; 152768350Seric char **av; 152868350Seric int *statp; 152968350Seric { 153068350Seric char *res; 153168350Seric char *propval; 153268350Seric extern char *ni_propval(); 153368350Seric 153468350Seric if (tTd(38, 20)) 153568350Seric printf("ni_map_lookup(%s, %s)\n", 153668350Seric map->map_mname, name); 153768350Seric 153868350Seric propval = ni_propval(map->map_file, map->map_keycolnm, name, 153968350Seric map->map_valcolnm, map->map_coldelim); 154068350Seric 154168350Seric if (propval == NULL) 154268350Seric return NULL; 154368350Seric 154468350Seric if (bitset(MF_MATCHONLY, map->map_mflags)) 154568350Seric res = map_rewrite(map, name, strlen(name), NULL); 154668350Seric else 154768350Seric res = map_rewrite(map, propval, strlen(propval), av); 154868350Seric free(propval); 154968350Seric return res; 155068350Seric } 155168350Seric 155268350Seric #endif 155368350Seric /* 155468350Seric ** TEXT (unindexed text file) Modules 155568350Seric ** 155668350Seric ** This code donated by Sun Microsystems. 155768350Seric */ 155868350Seric 155968350Seric 156068350Seric /* 156168350Seric ** TEXT_MAP_OPEN -- open text table 156268350Seric */ 156368350Seric 156468350Seric bool 156568350Seric text_map_open(map, mode) 156668350Seric MAP *map; 156768350Seric int mode; 156868350Seric { 156968350Seric struct stat sbuf; 157068350Seric 157168350Seric if (tTd(38, 2)) 157268350Seric printf("text_map_open(%s, %s, %d)\n", 157368350Seric map->map_mname, map->map_file, mode); 157468350Seric 157568350Seric if (mode != O_RDONLY) 157668350Seric { 157768350Seric errno = ENODEV; 157868350Seric return FALSE; 157968350Seric } 158068350Seric 158168350Seric if (*map->map_file == '\0') 158268350Seric { 158368350Seric if (tTd(38, 2)) 158468350Seric printf("text_map_open: file name required\n"); 158568350Seric return FALSE; 158668350Seric } 158768350Seric 158868350Seric if (map->map_file[0] != '/') 158968350Seric { 159068350Seric if (tTd(38, 2)) 159168350Seric printf("text_map_open(%s): file name must be fully qualified\n", 159268350Seric map->map_file); 159368350Seric return FALSE; 159468350Seric } 159568350Seric /* check to see if this map actually accessable */ 159668350Seric if (access(map->map_file, R_OK) <0) 159768350Seric return FALSE; 159868350Seric 159968350Seric /* check to see if this map actually exist */ 160068350Seric if (stat(map->map_file, &sbuf) <0) 160168350Seric { 160268350Seric if (tTd(38, 2)) 160368350Seric printf("text_map_open(%s): can not stat %s\n", 160468350Seric map->map_file, map->map_file); 160568350Seric return FALSE; 160668350Seric } 160768350Seric 160868350Seric if (!S_ISREG(sbuf.st_mode)) 160968350Seric { 161068350Seric if (tTd(38, 2)) 161168350Seric printf("text_map_open(%s): %s is not a file\n", 161268350Seric map->map_file, map->map_file); 161368350Seric return FALSE; 161468350Seric } 161568350Seric 161668350Seric if (map->map_keycolnm == NULL) 161768350Seric map->map_keycolno = 0; 161868350Seric else 161968350Seric { 162068350Seric if (!isdigit(*map->map_keycolnm)) 162168350Seric { 162268350Seric if (tTd(38, 2)) 162368350Seric printf("text_map_open(%s): -k should specify a number, not %s\n", 162468350Seric map->map_file, map->map_keycolnm); 162568350Seric return FALSE; 162668350Seric } 162768350Seric map->map_keycolno = atoi(map->map_keycolnm); 162868350Seric } 162968350Seric 163068350Seric if (map->map_valcolnm == NULL) 163168350Seric map->map_valcolno = 0; 163268350Seric else 163368350Seric { 163468350Seric if (!isdigit(*map->map_valcolnm)) 163568350Seric { 163668350Seric if (tTd(38, 2)) 163768350Seric printf("text_map_open(%s): -v should specify a number, not %s\n", 163868350Seric map->map_file, map->map_valcolnm); 163968350Seric return FALSE; 164068350Seric } 164168350Seric map->map_valcolno = atoi(map->map_valcolnm); 164268350Seric } 164368350Seric 164468350Seric if (tTd(38, 2)) 164568350Seric { 164668520Seric printf("text_map_open(%s): delimiter = ", 164768520Seric map->map_file); 164868520Seric if (map->map_coldelim == '\0') 164968520Seric printf("(white space)\n"); 165068520Seric else 165168520Seric printf("%c\n", map->map_coldelim); 165268350Seric } 165368350Seric 165468350Seric return TRUE; 165568350Seric } 165668350Seric 165768350Seric 165868350Seric /* 165968350Seric ** TEXT_MAP_LOOKUP -- look up a datum in a TEXT table 166068350Seric */ 166168350Seric 166268350Seric char * 166368350Seric text_map_lookup(map, name, av, statp) 166468350Seric MAP *map; 166568350Seric char *name; 166668350Seric char **av; 166768350Seric int *statp; 166868350Seric { 166968350Seric char *vp; 167068350Seric auto int vsize; 167168350Seric int buflen; 167268350Seric char search_key[MAXNAME + 1]; 167368350Seric char linebuf[MAXLINE]; 167468350Seric FILE *f; 167568528Seric char buf[MAXNAME + 1]; 167668350Seric char delim; 167768350Seric int key_idx; 167868350Seric bool found_it; 167968350Seric extern char *get_column(); 168068350Seric 168168350Seric 168268350Seric found_it = FALSE; 168368350Seric if (tTd(38, 20)) 168468350Seric printf("text_map_lookup(%s)\n", name); 168568350Seric 168668350Seric buflen = strlen(name); 168768350Seric if (buflen > sizeof search_key - 1) 168868350Seric buflen = sizeof search_key - 1; 168968350Seric bcopy(name, search_key, buflen + 1); 169068350Seric if (!bitset(MF_NOFOLDCASE, map->map_mflags)) 169168350Seric makelower(search_key); 169268350Seric 169368350Seric f = fopen(map->map_file, "r"); 169468350Seric if (f == NULL) 169568350Seric { 169668350Seric map->map_mflags &= ~(MF_VALID|MF_OPEN); 169768350Seric *statp = EX_UNAVAILABLE; 169868350Seric return NULL; 169968350Seric } 170068350Seric key_idx = map->map_keycolno; 170168350Seric delim = map->map_coldelim; 170268350Seric while (fgets(linebuf, MAXLINE, f)) 170368350Seric { 170468350Seric char *lf; 170568350Seric if (linebuf[0] == '#') 170668350Seric continue; /* skip comment line */ 170768350Seric if (lf = strchr(linebuf, '\n')) 170868350Seric *lf = '\0'; 170968350Seric if (!strcasecmp(search_key, 171068350Seric get_column(linebuf, key_idx, delim, buf))) 171168350Seric { 171268350Seric found_it = TRUE; 171368350Seric break; 171468350Seric } 171568350Seric } 171668350Seric fclose(f); 171768350Seric if (!found_it) 171868350Seric { 171968350Seric #ifdef MAP_EXIT_STAT 172068350Seric *statp = EX_NOTFOUND; 172168350Seric #endif 172268350Seric return(NULL); 172368350Seric } 172468350Seric vp = get_column(linebuf, map->map_valcolno, delim, buf); 172568350Seric vsize = strlen(vp); 172668350Seric #ifdef MAP_EXIT_STAT 172768350Seric *statp = EX_OK; 172868350Seric #endif 172968350Seric if (bitset(MF_MATCHONLY, map->map_mflags)) 173068350Seric return map_rewrite(map, name, strlen(name), NULL); 173168350Seric else 173268350Seric return map_rewrite(map, vp, vsize, av); 173368350Seric } 173468350Seric /* 173560089Seric ** STAB (Symbol Table) Modules 173660089Seric */ 173760089Seric 173860089Seric 173960089Seric /* 174060207Seric ** STAB_MAP_LOOKUP -- look up alias in symbol table 174160089Seric */ 174260089Seric 174360089Seric char * 174461707Seric stab_map_lookup(map, name, av, pstat) 174560089Seric register MAP *map; 174660089Seric char *name; 174761707Seric char **av; 174861707Seric int *pstat; 174960089Seric { 175060089Seric register STAB *s; 175160089Seric 175260537Seric if (tTd(38, 20)) 175368350Seric printf("stab_lookup(%s, %s)\n", 175468350Seric map->map_mname, name); 175560089Seric 175660089Seric s = stab(name, ST_ALIAS, ST_FIND); 175760089Seric if (s != NULL) 175860089Seric return (s->s_alias); 175960089Seric return (NULL); 176060089Seric } 176160089Seric 176260089Seric 176360089Seric /* 176460207Seric ** STAB_MAP_STORE -- store in symtab (actually using during init, not rebuild) 176560089Seric */ 176660089Seric 176760089Seric void 176860089Seric stab_map_store(map, lhs, rhs) 176960089Seric register MAP *map; 177060089Seric char *lhs; 177160089Seric char *rhs; 177260089Seric { 177360089Seric register STAB *s; 177460089Seric 177560089Seric s = stab(lhs, ST_ALIAS, ST_ENTER); 177660089Seric s->s_alias = newstr(rhs); 177760089Seric } 177860089Seric 177960089Seric 178060089Seric /* 178160207Seric ** STAB_MAP_OPEN -- initialize (reads data file) 178260207Seric ** 178360207Seric ** This is a wierd case -- it is only intended as a fallback for 178460207Seric ** aliases. For this reason, opens for write (only during a 178560207Seric ** "newaliases") always fails, and opens for read open the 178660207Seric ** actual underlying text file instead of the database. 178760089Seric */ 178860089Seric 178960089Seric bool 179060089Seric stab_map_open(map, mode) 179160089Seric register MAP *map; 179260089Seric int mode; 179360089Seric { 179463835Seric FILE *af; 179564284Seric struct stat st; 179663835Seric 179760537Seric if (tTd(38, 2)) 179868350Seric printf("stab_map_open(%s, %s)\n", 179968350Seric map->map_mname, map->map_file); 180060089Seric 180160089Seric if (mode != O_RDONLY) 180260207Seric { 180360207Seric errno = ENODEV; 180460089Seric return FALSE; 180560207Seric } 180660089Seric 180763835Seric af = fopen(map->map_file, "r"); 180863835Seric if (af == NULL) 180963835Seric return FALSE; 181068350Seric readaliases(map, af, FALSE, FALSE); 181164284Seric 181264284Seric if (fstat(fileno(af), &st) >= 0) 181364284Seric map->map_mtime = st.st_mtime; 181463835Seric fclose(af); 181563835Seric 181660089Seric return TRUE; 181760089Seric } 181860089Seric /* 181960089Seric ** Implicit Modules 182056822Seric ** 182160089Seric ** Tries several types. For back compatibility of aliases. 182256822Seric */ 182356822Seric 182460089Seric 182560089Seric /* 182660207Seric ** IMPL_MAP_LOOKUP -- lookup in best open database 182760089Seric */ 182860089Seric 182960089Seric char * 183060089Seric impl_map_lookup(map, name, av, pstat) 183160089Seric MAP *map; 183260089Seric char *name; 183356822Seric char **av; 183460089Seric int *pstat; 183556822Seric { 183660537Seric if (tTd(38, 20)) 183768350Seric printf("impl_map_lookup(%s, %s)\n", 183868350Seric map->map_mname, name); 183956822Seric 184060089Seric #ifdef NEWDB 184160207Seric if (bitset(MF_IMPL_HASH, map->map_mflags)) 184260089Seric return db_map_lookup(map, name, av, pstat); 184360089Seric #endif 184460089Seric #ifdef NDBM 184560207Seric if (bitset(MF_IMPL_NDBM, map->map_mflags)) 184660089Seric return ndbm_map_lookup(map, name, av, pstat); 184760089Seric #endif 184860089Seric return stab_map_lookup(map, name, av, pstat); 184960089Seric } 185060089Seric 185160089Seric /* 185260207Seric ** IMPL_MAP_STORE -- store in open databases 185360089Seric */ 185460089Seric 185560089Seric void 185660089Seric impl_map_store(map, lhs, rhs) 185760089Seric MAP *map; 185860089Seric char *lhs; 185960089Seric char *rhs; 186060089Seric { 186160089Seric #ifdef NEWDB 186260207Seric if (bitset(MF_IMPL_HASH, map->map_mflags)) 186360089Seric db_map_store(map, lhs, rhs); 186460089Seric #endif 186560089Seric #ifdef NDBM 186660207Seric if (bitset(MF_IMPL_NDBM, map->map_mflags)) 186760089Seric ndbm_map_store(map, lhs, rhs); 186860089Seric #endif 186960089Seric stab_map_store(map, lhs, rhs); 187060089Seric } 187160089Seric 187260089Seric /* 187360089Seric ** IMPL_MAP_OPEN -- implicit database open 187460089Seric */ 187560089Seric 187660089Seric bool 187760089Seric impl_map_open(map, mode) 187860089Seric MAP *map; 187960089Seric int mode; 188060089Seric { 188160089Seric struct stat stb; 188260089Seric 188360537Seric if (tTd(38, 2)) 188468350Seric printf("impl_map_open(%s, %s, %d)\n", 188568350Seric map->map_mname, map->map_file, mode); 188660089Seric 188760089Seric if (stat(map->map_file, &stb) < 0) 188856822Seric { 188960089Seric /* no alias file at all */ 189064718Seric if (tTd(38, 3)) 189164718Seric printf("no map file\n"); 189260089Seric return FALSE; 189356822Seric } 189456822Seric 189560089Seric #ifdef NEWDB 189660207Seric map->map_mflags |= MF_IMPL_HASH; 189760089Seric if (hash_map_open(map, mode)) 189856822Seric { 189964250Seric #if defined(NDBM) && defined(NIS) 190060561Seric if (mode == O_RDONLY || access("/var/yp/Makefile", R_OK) != 0) 190160207Seric #endif 190260207Seric return TRUE; 190360089Seric } 190460207Seric else 190560207Seric map->map_mflags &= ~MF_IMPL_HASH; 190660089Seric #endif 190760089Seric #ifdef NDBM 190860207Seric map->map_mflags |= MF_IMPL_NDBM; 190960089Seric if (ndbm_map_open(map, mode)) 191060089Seric { 191160089Seric return TRUE; 191260089Seric } 191360207Seric else 191460207Seric map->map_mflags &= ~MF_IMPL_NDBM; 191560089Seric #endif 191656822Seric 191764650Seric #if defined(NEWDB) || defined(NDBM) 191860089Seric if (Verbose) 191960089Seric message("WARNING: cannot open alias database %s", map->map_file); 192064964Seric #else 192164964Seric if (mode != O_RDONLY) 192264964Seric usrerr("Cannot rebuild aliases: no database format defined"); 192360207Seric #endif 192460089Seric 192560207Seric return stab_map_open(map, mode); 192656822Seric } 192760089Seric 192860207Seric 192960089Seric /* 193060207Seric ** IMPL_MAP_CLOSE -- close any open database(s) 193160089Seric */ 193260089Seric 193360089Seric void 193460207Seric impl_map_close(map) 193560089Seric MAP *map; 193660089Seric { 193768350Seric if (tTd(38, 20)) 193868350Seric printf("impl_map_close(%s, %s, %x)\n", 193968350Seric map->map_mname, map->map_file, map->map_mflags); 194060089Seric #ifdef NEWDB 194160207Seric if (bitset(MF_IMPL_HASH, map->map_mflags)) 194260089Seric { 194360207Seric db_map_close(map); 194460207Seric map->map_mflags &= ~MF_IMPL_HASH; 194560089Seric } 194660089Seric #endif 194760089Seric 194860089Seric #ifdef NDBM 194960207Seric if (bitset(MF_IMPL_NDBM, map->map_mflags)) 195060089Seric { 195160207Seric ndbm_map_close(map); 195260207Seric map->map_mflags &= ~MF_IMPL_NDBM; 195360089Seric } 195460089Seric #endif 195560089Seric } 195660207Seric /* 195768350Seric ** User map class. 195868350Seric ** 195968350Seric ** Provides access to the system password file. 196068350Seric */ 196168350Seric 196268350Seric /* 196368350Seric ** USER_MAP_OPEN -- open user map 196468350Seric ** 196568350Seric ** Really just binds field names to field numbers. 196668350Seric */ 196768350Seric 196868350Seric bool 196968350Seric user_map_open(map, mode) 197068350Seric MAP *map; 197168350Seric int mode; 197268350Seric { 197368350Seric if (tTd(38, 2)) 197468350Seric printf("user_map_open(%s)\n", map->map_mname); 197568350Seric 197668350Seric if (mode != O_RDONLY) 197768350Seric { 197868350Seric /* issue a pseudo-error message */ 197968350Seric #ifdef ENOSYS 198068350Seric errno = ENOSYS; 198168350Seric #else 198268350Seric # ifdef EFTYPE 198368350Seric errno = EFTYPE; 198468350Seric # else 198568350Seric errno = ENXIO; 198668350Seric # endif 198768350Seric #endif 198868350Seric return FALSE; 198968350Seric } 199068350Seric if (map->map_valcolnm == NULL) 199168350Seric /* nothing */ ; 199268350Seric else if (strcasecmp(map->map_valcolnm, "name") == 0) 199368350Seric map->map_valcolno = 1; 199468350Seric else if (strcasecmp(map->map_valcolnm, "passwd") == 0) 199568350Seric map->map_valcolno = 2; 199668350Seric else if (strcasecmp(map->map_valcolnm, "uid") == 0) 199768350Seric map->map_valcolno = 3; 199868350Seric else if (strcasecmp(map->map_valcolnm, "gid") == 0) 199968350Seric map->map_valcolno = 4; 200068350Seric else if (strcasecmp(map->map_valcolnm, "gecos") == 0) 200168350Seric map->map_valcolno = 5; 200268350Seric else if (strcasecmp(map->map_valcolnm, "dir") == 0) 200368350Seric map->map_valcolno = 6; 200468350Seric else if (strcasecmp(map->map_valcolnm, "shell") == 0) 200568350Seric map->map_valcolno = 7; 200668350Seric else 200768350Seric { 200868350Seric syserr("User map %s: unknown column name %s", 200968350Seric map->map_mname, map->map_valcolnm); 201068350Seric return FALSE; 201168350Seric } 201268350Seric return TRUE; 201368350Seric } 201468350Seric 201568350Seric 201668350Seric /* 201768350Seric ** USER_MAP_LOOKUP -- look up a user in the passwd file. 201868350Seric */ 201968350Seric 202068350Seric char * 202168350Seric user_map_lookup(map, key, av, statp) 202268350Seric MAP *map; 202368350Seric char *key; 202468350Seric char **av; 202568350Seric int *statp; 202668350Seric { 202768350Seric struct passwd *pw; 202868350Seric 202968350Seric if (tTd(38, 20)) 203068350Seric printf("user_map_lookup(%s, %s)\n", 203168350Seric map->map_mname, key); 203268350Seric 203368693Seric pw = sm_getpwnam(key); 203468350Seric if (pw == NULL) 203568350Seric return NULL; 203668350Seric if (bitset(MF_MATCHONLY, map->map_mflags)) 203768350Seric return map_rewrite(map, key, strlen(key), NULL); 203868350Seric else 203968350Seric { 204068433Seric char *rwval = NULL; 204168350Seric char buf[30]; 204268350Seric 204368350Seric switch (map->map_valcolno) 204468350Seric { 204568350Seric case 0: 204668350Seric case 1: 204768350Seric rwval = pw->pw_name; 204868350Seric break; 204968350Seric 205068350Seric case 2: 205168350Seric rwval = pw->pw_passwd; 205268350Seric break; 205368350Seric 205468350Seric case 3: 205568350Seric sprintf(buf, "%d", pw->pw_uid); 205668350Seric rwval = buf; 205768350Seric break; 205868350Seric 205968350Seric case 4: 206068350Seric sprintf(buf, "%d", pw->pw_gid); 206168350Seric rwval = buf; 206268350Seric break; 206368350Seric 206468350Seric case 5: 206568350Seric rwval = pw->pw_gecos; 206668350Seric break; 206768350Seric 206868350Seric case 6: 206968350Seric rwval = pw->pw_dir; 207068350Seric break; 207168350Seric 207268350Seric case 7: 207368350Seric rwval = pw->pw_shell; 207468350Seric break; 207568350Seric } 207668350Seric return map_rewrite(map, rwval, strlen(rwval), av); 207768350Seric } 207868350Seric } 207968350Seric /* 208068350Seric ** BESTMX -- find the best MX for a name 208168350Seric ** 208268350Seric ** This is really a hack, but I don't see any obvious way 208368350Seric ** to generalize it at the moment. 208468350Seric */ 208568350Seric 208668350Seric #if NAMED_BIND 208768350Seric 208868350Seric char * 208968350Seric bestmx_map_lookup(map, name, av, statp) 209068350Seric MAP *map; 209168350Seric char *name; 209268350Seric char **av; 209368350Seric int *statp; 209468350Seric { 209568350Seric int nmx; 209668350Seric auto int rcode; 209768350Seric char *mxhosts[MAXMXHOSTS + 1]; 209868350Seric 209968350Seric nmx = getmxrr(name, mxhosts, FALSE, &rcode); 210068350Seric if (nmx <= 0) 210168350Seric return NULL; 210268350Seric if (bitset(MF_MATCHONLY, map->map_mflags)) 210368350Seric return map_rewrite(map, name, strlen(name), NULL); 210468350Seric else 210568350Seric return map_rewrite(map, mxhosts[0], strlen(mxhosts[0]), av); 210668350Seric } 210768350Seric 210868350Seric #endif 210968350Seric /* 211068350Seric ** Sequenced map type. 211168350Seric ** 211268350Seric ** Tries each map in order until something matches, much like 211368350Seric ** implicit. Stores go to the first map in the list that can 211468350Seric ** support storing. 211568350Seric ** 211668350Seric ** This is slightly unusual in that there are two interfaces. 211768350Seric ** The "sequence" interface lets you stack maps arbitrarily. 211868350Seric ** The "switch" interface builds a sequence map by looking 211968350Seric ** at a system-dependent configuration file such as 212068350Seric ** /etc/nsswitch.conf on Solaris or /etc/svc.conf on Ultrix. 212168350Seric ** 212268350Seric ** We don't need an explicit open, since all maps are 212368350Seric ** opened during startup, including underlying maps. 212468350Seric */ 212568350Seric 212668350Seric /* 212768350Seric ** SEQ_MAP_PARSE -- Sequenced map parsing 212868350Seric */ 212968350Seric 213068350Seric bool 213168350Seric seq_map_parse(map, ap) 213268350Seric MAP *map; 213368350Seric char *ap; 213468350Seric { 213568350Seric int maxmap; 213668350Seric 213768350Seric if (tTd(38, 2)) 213868350Seric printf("seq_map_parse(%s, %s)\n", map->map_mname, ap); 213968350Seric maxmap = 0; 214068350Seric while (*ap != '\0') 214168350Seric { 214268350Seric register char *p; 214368350Seric STAB *s; 214468350Seric 214568350Seric /* find beginning of map name */ 214668350Seric while (isascii(*ap) && isspace(*ap)) 214768350Seric ap++; 214868350Seric for (p = ap; isascii(*p) && isalnum(*p); p++) 214968350Seric continue; 215068350Seric if (*p != '\0') 215168350Seric *p++ = '\0'; 215268350Seric while (*p != '\0' && (!isascii(*p) || !isalnum(*p))) 215368350Seric p++; 215468350Seric if (*ap == '\0') 215568350Seric { 215668350Seric ap = p; 215768350Seric continue; 215868350Seric } 215968350Seric s = stab(ap, ST_MAP, ST_FIND); 216068350Seric if (s == NULL) 216168350Seric { 216268350Seric syserr("Sequence map %s: unknown member map %s", 216368350Seric map->map_mname, ap); 216468350Seric } 216568350Seric else if (maxmap == MAXMAPSTACK) 216668350Seric { 216768350Seric syserr("Sequence map %s: too many member maps (%d max)", 216868350Seric map->map_mname, MAXMAPSTACK); 216968350Seric maxmap++; 217068350Seric } 217168350Seric else if (maxmap < MAXMAPSTACK) 217268350Seric { 217368350Seric map->map_stack[maxmap++] = &s->s_map; 217468350Seric } 217568350Seric ap = p; 217668350Seric } 217768350Seric return TRUE; 217868350Seric } 217968350Seric 218068350Seric 218168350Seric /* 218268350Seric ** SWITCH_MAP_OPEN -- open a switched map 218368350Seric ** 218468350Seric ** This looks at the system-dependent configuration and builds 218568350Seric ** a sequence map that does the same thing. 218668350Seric ** 218768350Seric ** Every system must define a switch_map_find routine in conf.c 218868350Seric ** that will return the list of service types associated with a 218968350Seric ** given service class. 219068350Seric */ 219168350Seric 219268350Seric bool 219368350Seric switch_map_open(map, mode) 219468350Seric MAP *map; 219568350Seric int mode; 219668350Seric { 219768350Seric int mapno; 219868350Seric int nmaps; 219968350Seric char *maptype[MAXMAPSTACK]; 220068350Seric 220168350Seric if (tTd(38, 2)) 220268350Seric printf("switch_map_open(%s, %s, %d)\n", 220368350Seric map->map_mname, map->map_file, mode); 220468350Seric 220568350Seric nmaps = switch_map_find(map->map_file, maptype, map->map_return); 220668350Seric if (tTd(38, 19)) 220768350Seric { 220868350Seric printf("\tswitch_map_find => %d\n", nmaps); 220968350Seric for (mapno = 0; mapno < nmaps; mapno++) 221068350Seric printf("\t\t%s\n", maptype[mapno]); 221168350Seric } 221268350Seric if (nmaps <= 0 || nmaps > MAXMAPSTACK) 221368350Seric return FALSE; 221468350Seric 221568350Seric for (mapno = 0; mapno < nmaps; mapno++) 221668350Seric { 221768350Seric register STAB *s; 221868350Seric char nbuf[MAXNAME + 1]; 221968350Seric 222068350Seric if (maptype[mapno] == NULL) 222168350Seric continue; 222268350Seric (void) sprintf(nbuf, "%s.%s", map->map_file, maptype[mapno]); 222368350Seric s = stab(nbuf, ST_MAP, ST_FIND); 222468350Seric if (s == NULL) 222568350Seric { 222668350Seric syserr("Switch map %s: unknown member map %s", 222768350Seric map->map_mname, nbuf); 222868350Seric } 222968350Seric else 223068350Seric { 223168350Seric map->map_stack[mapno] = &s->s_map; 223268350Seric if (tTd(38, 4)) 223368350Seric printf("\tmap_stack[%d] = %s:%s\n", 223468350Seric mapno, s->s_map.map_class->map_cname, 223568350Seric nbuf); 223668350Seric } 223768350Seric } 223868350Seric return TRUE; 223968350Seric } 224068350Seric 224168350Seric 224268350Seric /* 224368350Seric ** SEQ_MAP_CLOSE -- close all underlying maps 224468350Seric */ 224568350Seric 224668350Seric seq_map_close(map) 224768350Seric MAP *map; 224868350Seric { 224968350Seric int mapno; 225068350Seric 225168350Seric if (tTd(38, 20)) 225268350Seric printf("seq_map_close(%s)\n", map->map_mname); 225368350Seric for (mapno = 0; mapno < MAXMAPSTACK; mapno++) 225468350Seric { 225568350Seric MAP *mm = map->map_stack[mapno]; 225668350Seric 225768350Seric if (mm == NULL || !bitset(MF_OPEN, mm->map_mflags)) 225868350Seric continue; 225968350Seric mm->map_class->map_close(mm); 226068350Seric } 226168350Seric } 226268350Seric 226368350Seric 226468350Seric /* 226568350Seric ** SEQ_MAP_LOOKUP -- sequenced map lookup 226668350Seric */ 226768350Seric 226868350Seric char * 226968350Seric seq_map_lookup(map, key, args, pstat) 227068350Seric MAP *map; 227168350Seric char *key; 227268350Seric char **args; 227368350Seric int *pstat; 227468350Seric { 227568350Seric int mapno; 227668350Seric int mapbit = 0x01; 227768350Seric 227868350Seric if (tTd(38, 20)) 227968350Seric printf("seq_map_lookup(%s, %s)\n", map->map_mname, key); 228068350Seric 228168350Seric for (mapno = 0; mapno < MAXMAPSTACK; mapbit <<= 1, mapno++) 228268350Seric { 228368350Seric MAP *mm = map->map_stack[mapno]; 228468350Seric int stat = 0; 228568350Seric char *rv; 228668350Seric 228768350Seric if (mm == NULL) 228868350Seric continue; 228968350Seric if (!bitset(MF_OPEN, mm->map_mflags)) 229068350Seric { 229168350Seric if (bitset(mapbit, map->map_return[MA_UNAVAIL])) 229268350Seric { 229368350Seric *pstat = EX_UNAVAILABLE; 229468350Seric return NULL; 229568350Seric } 229668350Seric continue; 229768350Seric } 229868350Seric rv = mm->map_class->map_lookup(mm, key, args, &stat); 229968350Seric if (rv != NULL) 230068350Seric return rv; 230168350Seric if (stat == 0 && bitset(mapbit, map->map_return[MA_NOTFOUND])) 230268350Seric return NULL; 230368350Seric if (stat != 0 && bitset(mapbit, map->map_return[MA_TRYAGAIN])) 230468350Seric { 230568350Seric *pstat = stat; 230668350Seric return NULL; 230768350Seric } 230868350Seric } 230968350Seric return NULL; 231068350Seric } 231168350Seric 231268350Seric 231368350Seric /* 231468350Seric ** SEQ_MAP_STORE -- sequenced map store 231568350Seric */ 231668350Seric 231768350Seric void 231868350Seric seq_map_store(map, key, val) 231968350Seric MAP *map; 232068350Seric char *key; 232168350Seric char *val; 232268350Seric { 232368350Seric int mapno; 232468350Seric 232568350Seric if (tTd(38, 12)) 232668350Seric printf("seq_map_store(%s, %s, %s)\n", 232768350Seric map->map_mname, key, val); 232868350Seric 232968350Seric for (mapno = 0; mapno < MAXMAPSTACK; mapno++) 233068350Seric { 233168350Seric MAP *mm = map->map_stack[mapno]; 233268350Seric 233368350Seric if (mm == NULL || !bitset(MF_WRITABLE, mm->map_mflags)) 233468350Seric continue; 233568350Seric 233668350Seric mm->map_class->map_store(mm, key, val); 233768350Seric return; 233868350Seric } 233968350Seric syserr("seq_map_store(%s, %s, %s): no writable map", 234068350Seric map->map_mname, key, val); 234168350Seric } 234268350Seric /* 234360207Seric ** NULL stubs 234460089Seric */ 234560089Seric 234660207Seric bool 234760207Seric null_map_open(map, mode) 234860089Seric MAP *map; 234960207Seric int mode; 235060089Seric { 235160207Seric return TRUE; 235260089Seric } 235360089Seric 235460207Seric void 235560207Seric null_map_close(map) 235660207Seric MAP *map; 235760089Seric { 235860207Seric return; 235960207Seric } 236060089Seric 236160207Seric void 236260207Seric null_map_store(map, key, val) 236360207Seric MAP *map; 236460207Seric char *key; 236560207Seric char *val; 236660089Seric { 236760207Seric return; 236860089Seric } 236968350Seric 237068350Seric 237168350Seric /* 237268350Seric ** BOGUS stubs 237368350Seric */ 237468350Seric 237568350Seric char * 237668350Seric bogus_map_lookup(map, key, args, pstat) 237768350Seric MAP *map; 237868350Seric char *key; 237968350Seric char **args; 238068350Seric int *pstat; 238168350Seric { 238268350Seric *pstat = EX_TEMPFAIL; 238368350Seric return NULL; 238468350Seric } 238568350Seric 238668350Seric MAPCLASS BogusMapClass = 238768350Seric { 238868350Seric "bogus-map", NULL, 0, 238968350Seric NULL, bogus_map_lookup, null_map_store, 239068350Seric null_map_open, null_map_close, 239168350Seric }; 2392