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*58963Seric static char sccsid[] = "@(#)map.c 6.8 (Berkeley) 04/04/93"; 1156822Seric #endif /* not lint */ 1256822Seric 1356822Seric #include "sendmail.h" 1456822Seric 1556822Seric #ifdef DBM_MAP 1656822Seric #include <ndbm.h> 1756822Seric #endif 1856822Seric #if defined(HASH_MAP) || defined(BTREE_MAP) 1956822Seric #include <db.h> 2056822Seric #endif 2157208Seric #ifdef NIS_MAP 2257208Seric #include <rpcsvc/ypclnt.h> 2357208Seric #endif 2456822Seric 2556822Seric 2656822Seric #ifdef DBM_MAP 2756822Seric 2856822Seric /* 2956822Seric ** DBM_MAP_INIT -- DBM-style map initialization 3056822Seric ** 3156822Seric ** Parameters: 3256822Seric ** map -- the pointer to the actual map 3356822Seric ** mapname -- the name of the map (for error messages) 3456822Seric ** args -- a pointer to the config file line arguments 3556822Seric ** 3656822Seric ** Returns: 3756822Seric ** TRUE -- if it could successfully open the map. 3856822Seric ** FALSE -- otherwise. 3956822Seric ** 4056822Seric ** Side Effects: 4156822Seric ** Gives an error if it can't open the map. 4256822Seric */ 4356822Seric 4456822Seric bool 4556822Seric dbm_map_init(map, mapname, args) 4656822Seric MAP *map; 4756822Seric char *mapname; 4856822Seric char *args; 4956822Seric { 5056822Seric DBM *dbm; 5156822Seric 5257208Seric map_parseargs(map, &args); 5356822Seric if (map->map_file == NULL) 5457208Seric { 5557208Seric syserr("No file name for DBM map %s", mapname); 5656822Seric return FALSE; 5757208Seric } 5856822Seric dbm = dbm_open(map->map_file, O_RDONLY, 0644); 5956822Seric if (dbm == NULL) 6056822Seric { 6156836Seric if (!bitset(MF_OPTIONAL, map->map_flags)) 6256836Seric syserr("Cannot open DBM database %s", map->map_file); 6356822Seric return FALSE; 6456822Seric } 6556822Seric map->map_db = (void *) dbm; 6656822Seric return TRUE; 6756822Seric } 6856822Seric /* 6956822Seric ** DBM_MAP_LOOKUP -- look up a datum in a DBM-type map 7056822Seric ** 7156822Seric ** Parameters: 7256822Seric ** map -- the map to look up in. 7356822Seric ** buf -- a pointer to to the buffer containing the key. 7456822Seric ** This is a null terminated string. 7556822Seric ** bufsiz -- the size of buf -- note that this is in general 7656822Seric ** larger that strlen(buf), and buf can be changed 7756822Seric ** in place if desired. 7856822Seric ** av -- arguments from the config file (can be interpolated 7956822Seric ** into the final result). 8056822Seric ** 8156822Seric ** Returns: 8256822Seric ** A pointer to the rewritten result. 8356822Seric ** NULL if not found in the map. 8456822Seric */ 8556822Seric 8656822Seric char * 8756822Seric dbm_map_lookup(map, buf, bufsiz, av) 8856822Seric MAP *map; 8956822Seric char buf[]; 9056822Seric int bufsiz; 9156822Seric char **av; 9256822Seric { 9356822Seric datum key, val; 9456822Seric 9556822Seric key.dptr = buf; 9656822Seric key.dsize = strlen(buf); 9757033Seric if (!bitset(MF_NOFOLDCASE, map->map_flags)) 9857014Seric { 9957014Seric register char *p; 10057014Seric 10157014Seric for (p = buf; *p != '\0'; p++) 10258050Seric if (isascii(*p) && isupper(*p)) 10357014Seric *p = tolower(*p); 10457014Seric } 10556822Seric if (bitset(MF_INCLNULL, map->map_flags)) 10656822Seric key.dsize++; 10756822Seric val = dbm_fetch(map->map_db, key); 10856822Seric if (val.dptr == NULL) 10956822Seric return NULL; 11057208Seric if (!bitset(MF_MATCHONLY, map->map_flags)) 11157208Seric map_rewrite(val.dptr, val.dsize, buf, bufsiz, av); 11256822Seric return buf; 11356822Seric } 11456822Seric 11556822Seric #endif /* DBM_MAP */ 11656822Seric 11756822Seric #ifdef BTREE_MAP 11856822Seric 11956822Seric /* 12056822Seric ** BTREE_MAP_INIT -- BTREE-style map initialization 12156822Seric ** 12256822Seric ** Parameters: 12356822Seric ** map -- the pointer to the actual map 12456822Seric ** mapname -- the name of the map (for error messages) 12556822Seric ** args -- a pointer to the config file line arguments 12656822Seric ** 12756822Seric ** Returns: 12856822Seric ** TRUE -- if it could successfully open the map. 12956822Seric ** FALSE -- otherwise. 13056822Seric ** 13156822Seric ** Side Effects: 13256822Seric ** Gives an error if it can't open the map. 13356822Seric */ 13456822Seric 13556822Seric bool 13656822Seric bt_map_init(map, mapname, args) 13756822Seric MAP *map; 13856822Seric char *mapname; 13956822Seric char *args; 14056822Seric { 14156822Seric DB *db; 14256822Seric 14357208Seric map_parseargs(map, &args); 14456822Seric if (map->map_file == NULL) 14557208Seric { 14657208Seric syserr("No file name for BTREE map %s", mapname); 14756822Seric return FALSE; 14857208Seric } 14956822Seric db = dbopen(map->map_file, O_RDONLY, 0644, DB_BTREE, NULL); 15056822Seric if (db == NULL) 15156822Seric { 15256836Seric if (!bitset(MF_OPTIONAL, map->map_flags)) 15356836Seric syserr("Cannot open BTREE database %s", map->map_file); 15456822Seric return FALSE; 15556822Seric } 15656822Seric map->map_db = (void *) db; 15756822Seric return TRUE; 15856822Seric } 15956822Seric 16056822Seric #endif /* BTREE_MAP */ 16156822Seric 16256822Seric #ifdef HASH_MAP 16356822Seric 16456822Seric /* 16556822Seric ** HASH_MAP_INIT -- HASH-style map initialization 16656822Seric ** 16756822Seric ** Parameters: 16856822Seric ** map -- the pointer to the actual map 16956822Seric ** mapname -- the name of the map (for error messages) 17056822Seric ** args -- a pointer to the config file line arguments 17156822Seric ** 17256822Seric ** Returns: 17356822Seric ** TRUE -- if it could successfully open the map. 17456822Seric ** FALSE -- otherwise. 17556822Seric ** 17656822Seric ** Side Effects: 17756822Seric ** Gives an error if it can't open the map. 17856822Seric */ 17956822Seric 18056822Seric bool 18156822Seric hash_map_init(map, mapname, args) 18256822Seric MAP *map; 18356822Seric char *mapname; 18456822Seric char *args; 18556822Seric { 18656822Seric DB *db; 18756822Seric 18857208Seric map_parseargs(map, &args); 18956822Seric if (map->map_file == NULL) 19057208Seric { 19157208Seric syserr("No file name for HASH map %s", mapname); 19256822Seric return FALSE; 19357208Seric } 19456822Seric db = dbopen(map->map_file, O_RDONLY, 0644, DB_HASH, NULL); 19556822Seric if (db == NULL) 19656822Seric { 19756836Seric if (!bitset(MF_OPTIONAL, map->map_flags)) 19856836Seric syserr("Cannot open HASH database %s", map->map_file); 19956822Seric return FALSE; 20056822Seric } 20156822Seric map->map_db = (void *) db; 20256822Seric return TRUE; 20356822Seric } 20456822Seric 20556822Seric #endif /* HASH_MAP */ 20656822Seric 20756822Seric #if defined(BTREE_MAP) || defined(HASH_MAP) 20856822Seric 20956822Seric /* 21056822Seric ** DB_MAP_LOOKUP -- look up a datum in a BTREE- or HASH-type map 21156822Seric ** 21256822Seric ** Parameters: 21356822Seric ** map -- the map to look up in. 21456822Seric ** buf -- a pointer to to the buffer containing the key. 21556822Seric ** This is a null terminated string. 21656822Seric ** bufsiz -- the size of buf -- note that this is in general 21756822Seric ** larger that strlen(buf), and buf can be changed 21856822Seric ** in place if desired. 21956822Seric ** av -- arguments from the config file (can be interpolated 22056822Seric ** into the final result). 22156822Seric ** 22256822Seric ** Returns: 22356822Seric ** A pointer to the rewritten result. 22456822Seric ** NULL if not found in the map. 22556822Seric */ 22656822Seric 22756822Seric char * 22856822Seric db_map_lookup(map, buf, bufsiz, av) 22956822Seric MAP *map; 23056822Seric char buf[]; 23156822Seric int bufsiz; 23256822Seric char **av; 23356822Seric { 23456822Seric DBT key, val; 23556822Seric 23656822Seric key.data = buf; 23756822Seric key.size = strlen(buf); 23857033Seric if (!bitset(MF_NOFOLDCASE, map->map_flags)) 23956847Seric { 24056847Seric register char *p; 24156847Seric 24256847Seric for (p = buf; *p != '\0'; p++) 24358050Seric if (isascii(*p) && isupper(*p)) 24456847Seric *p = tolower(*p); 24556847Seric } 24656822Seric if (bitset(MF_INCLNULL, map->map_flags)) 24756822Seric key.size++; 24856836Seric if (((DB *) map->map_db)->get((DB *) map->map_db, &key, &val, 0) != 0) 24956822Seric return NULL; 25057208Seric if (!bitset(MF_MATCHONLY, map->map_flags)) 25157208Seric map_rewrite(val.data, val.size, buf, bufsiz, av); 25256822Seric return buf; 25356822Seric } 25456822Seric 25556822Seric #endif /* BTREE_MAP || HASH_MAP */ 25656822Seric /* 25756822Seric ** MAP_PARSEARGS -- parse config line arguments for database lookup 25856822Seric ** 25956822Seric ** Parameters: 26056822Seric ** map -- the map being initialized. 26156822Seric ** pp -- an indirect pointer to the config line. It will 26256822Seric ** be replaced with a pointer to the next field 26356822Seric ** on the line. 26456822Seric ** 26556822Seric ** Returns: 26656822Seric ** none 26756822Seric ** 26856822Seric ** Side Effects: 26956822Seric ** null terminates the filename; stores it in map 27056822Seric */ 27156822Seric 27257208Seric map_parseargs(map, pp) 27356822Seric MAP *map; 27456822Seric char **pp; 27556822Seric { 27656822Seric register char *p = *pp; 27756822Seric 27856822Seric for (;;) 27956822Seric { 28058050Seric while (isascii(*p) && isspace(*p)) 28156822Seric p++; 28256822Seric if (*p != '-') 28356822Seric break; 28456822Seric switch (*++p) 28556822Seric { 28656822Seric case 'N': 28756822Seric map->map_flags |= MF_INCLNULL; 28856822Seric break; 28956836Seric 29056836Seric case 'o': 29156836Seric map->map_flags |= MF_OPTIONAL; 29256836Seric break; 29356836Seric 29456847Seric case 'f': 29557033Seric map->map_flags |= MF_NOFOLDCASE; 29656847Seric break; 29756847Seric 29857208Seric case 'm': 29957208Seric map->map_flags |= MF_MATCHONLY; 30057208Seric break; 30157208Seric 30256836Seric case 'a': 30356836Seric map->map_app = ++p; 30456836Seric break; 30557208Seric 30657208Seric case 'd': 30757208Seric map->map_domain = ++p; 30857208Seric break; 30956822Seric } 31058050Seric while (*p != '\0' && !(isascii(*p) && isspace(*p))) 31156822Seric p++; 31256836Seric if (*p != '\0') 31358804Seric *p++ = '\0'; 31456822Seric } 31556836Seric if (map->map_app != NULL) 31656836Seric map->map_app = newstr(map->map_app); 31757208Seric if (map->map_domain != NULL) 31857208Seric map->map_domain = newstr(map->map_domain); 31956822Seric 32056822Seric if (*p != '\0') 32158804Seric { 32258804Seric map->map_file = p; 32358804Seric while (*p != '\0' && !(isascii(*p) && isspace(*p))) 32458804Seric p++; 32558804Seric if (*p != '\0') 32658804Seric *p++ = '\0'; 32758804Seric map->map_file = newstr(map->map_file); 32858804Seric } 329*58963Seric 330*58963Seric while (*p != '\0' && isascii(*p) && isspace(*p)) 331*58963Seric p++; 33256822Seric *pp = p; 333*58963Seric if (*p != '\0') 334*58963Seric map->map_rebuild = newstr(p); 33556822Seric } 33657208Seric 33757208Seric # ifdef NIS_MAP 33857208Seric 33957208Seric /* 34057208Seric ** NIS_MAP_INIT -- initialize DBM map 34157208Seric ** 34257208Seric ** Parameters: 34357208Seric ** map -- the pointer to the actual map. 34457208Seric ** mapname -- the name of the map (for error messages). 34557208Seric ** args -- a pointer to the config file line arguments. 34657208Seric ** 34757208Seric ** Returns: 34857208Seric ** TRUE -- if it could successfully open the map. 34957208Seric ** FALSE -- otherwise. 35057208Seric ** 35157208Seric ** Side Effects: 35257208Seric ** Prints an error if it can't open the map. 35357208Seric */ 35457208Seric 35557208Seric bool 35657208Seric nis_map_init(map, mapname, args) 35757208Seric MAP *map; 35857208Seric char *mapname; 35957208Seric char *args; 36057208Seric { 36157216Seric int yperr; 36257216Seric char *master; 36357216Seric 36457208Seric /* parse arguments */ 36557208Seric map_parseargs(map, &args); 36657208Seric if (map->map_file == NULL) 36757208Seric { 36857208Seric syserr("No NIS map name for map %s", mapname); 36957208Seric return FALSE; 37057208Seric } 37157208Seric if (map->map_domain == NULL) 37257208Seric yp_get_default_domain(&map->map_domain); 37357216Seric 37457216Seric /* check to see if this map actually exists */ 37557216Seric yperr = yp_master(map->map_domain, map->map_file, &master); 37657216Seric if (yperr == 0) 37757216Seric return TRUE; 37857216Seric if (!bitset(MF_OPTIONAL, map->map_flags)) 37957216Seric syserr("Cannot bind to domain %s: %s", map->map_domain, 38057216Seric yperr_string(yperr)); 38157216Seric return FALSE; 38257208Seric } 38356822Seric /* 38457208Seric ** NIS_MAP_LOOKUP -- look up a datum in a NIS map 38557208Seric ** 38657208Seric ** Parameters: 38757208Seric ** map -- the map to look up in. 38857208Seric ** buf -- a pointer to to the buffer containing the key. 38957208Seric ** This is a null terminated string. 39057208Seric ** bufsiz -- the size of buf -- note that this is in general 39157208Seric ** larger that strlen(buf), and buf can be changed 39257208Seric ** in place if desired. 39357208Seric ** av -- arguments from the config file (can be interpolated 39457208Seric ** into the final result). 39557208Seric ** 39657208Seric ** Returns: 39757208Seric ** A pointer to the rewritten result. 39857208Seric ** NULL if not found in the map. 39957208Seric */ 40057208Seric 40157208Seric char * 40257208Seric nis_map_lookup(map, buf, bufsiz, av) 40357208Seric MAP *map; 40457208Seric char buf[]; 40557208Seric int bufsiz; 40657208Seric char **av; 40757208Seric { 40857208Seric char *vp; 40957642Seric auto int vsize; 41057208Seric 41157208Seric if (!bitset(MF_NOFOLDCASE, map->map_flags)) 41257208Seric { 41357208Seric register char *p; 41457208Seric 41557208Seric for (p = buf; *p != '\0'; p++) 41658050Seric if (isascii(*p) && isupper(*p)) 41757208Seric *p = tolower(*p); 41857208Seric } 41957642Seric if (yp_match(map->map_domain, map->map_file, buf, strlen(buf) + 1, 42057642Seric &vp, &vsize) != 0) 42157208Seric return NULL; 42257208Seric if (!bitset(MF_MATCHONLY, map->map_flags)) 42357208Seric map_rewrite(vp, vsize, buf, bufsiz, av); 42457208Seric return buf; 42557208Seric } 42657208Seric 42757208Seric #endif /* NIS_MAP */ 42857208Seric /* 42956822Seric ** MAP_REWRITE -- rewrite a database key, interpolating %n indications. 43056822Seric ** 43156822Seric ** Parameters: 43256822Seric ** s -- the string to rewrite, NOT necessarily null terminated. 43356822Seric ** slen -- the length of s. 43456822Seric ** buf -- the place to write it. 43556822Seric ** buflen -- the length of buf. 43656822Seric ** av -- arguments to interpolate into buf. 43756822Seric ** 43856822Seric ** Returns: 43956822Seric ** none. 44056822Seric ** 44156822Seric ** Side Effects: 44256822Seric ** none. 44356822Seric */ 44456822Seric 44556822Seric map_rewrite(s, slen, buf, buflen, av) 44656822Seric register char *s; 44756822Seric int slen; 44856822Seric char buf[]; 44956822Seric int buflen; 45056822Seric char **av; 45156822Seric { 45256822Seric register char *bp; 45356822Seric char *buflim; 45456822Seric register char c; 45556822Seric char **avp; 45656822Seric register char *ap; 45756822Seric 45856822Seric if (tTd(23, 1)) 45956822Seric { 46056822Seric printf("map_rewrite(%.*s), av =\n", slen, s); 46156822Seric for (avp = av; *avp != NULL; avp++) 46256822Seric printf("\t%s\n", *avp); 46356822Seric } 46456822Seric 46556822Seric bp = buf; 46656822Seric buflim = &buf[buflen - 2]; 46756822Seric while (--slen >= 0 && (c = *s++) != '\0') 46856822Seric { 46956822Seric if (c != '%') 47056822Seric { 47156822Seric pushc: 47256822Seric if (bp < buflim) 47356822Seric *bp++ = c; 47456822Seric continue; 47556822Seric } 47656822Seric if (--slen < 0 || (c = *s++) == '\0') 47756822Seric c = '%'; 47856822Seric if (c == '%') 47956822Seric goto pushc; 48058050Seric if (!(isascii(c) && isdigit(c))) 48156822Seric { 48256822Seric *bp++ = '%'; 48356822Seric goto pushc; 48456822Seric } 48556822Seric c -= '0'; 48656822Seric for (avp = av; --c >= 0 && *avp != NULL; avp++) 48756822Seric continue; 48856822Seric if (*avp == NULL) 48956822Seric continue; 49056822Seric 49156822Seric /* transliterate argument into output string */ 49256822Seric for (ap = *avp; (c = *ap++) != '\0'; ) 49356822Seric { 49456822Seric if (bp < buflim) 49556822Seric *bp++ = c; 49656822Seric } 49756822Seric } 49856822Seric *bp++ = '\0'; 49956822Seric if (tTd(23, 1)) 50056822Seric printf("map_rewrite => %s\n", buf); 50156822Seric } 502