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*56836Seric static char sccsid[] = "@(#)map.c 5.2 (Berkeley) 11/15/92"; 1156822Seric #endif /* not lint */ 1256822Seric 1356822Seric #include "sendmail.h" 1456822Seric #include <sys/file.h> 1556822Seric 1656822Seric #ifdef DBM_MAP 1756822Seric #include <ndbm.h> 1856822Seric #endif 1956822Seric #if defined(HASH_MAP) || defined(BTREE_MAP) 2056822Seric #include <db.h> 2156822Seric #endif 2256822Seric 2356822Seric 2456822Seric #ifdef DBM_MAP 2556822Seric 2656822Seric /* 2756822Seric ** DBM_MAP_INIT -- DBM-style map initialization 2856822Seric ** 2956822Seric ** Parameters: 3056822Seric ** map -- the pointer to the actual map 3156822Seric ** mapname -- the name of the map (for error messages) 3256822Seric ** args -- a pointer to the config file line arguments 3356822Seric ** 3456822Seric ** Returns: 3556822Seric ** TRUE -- if it could successfully open the map. 3656822Seric ** FALSE -- otherwise. 3756822Seric ** 3856822Seric ** Side Effects: 3956822Seric ** Gives an error if it can't open the map. 4056822Seric */ 4156822Seric 4256822Seric bool 4356822Seric dbm_map_init(map, mapname, args) 4456822Seric MAP *map; 4556822Seric char *mapname; 4656822Seric char *args; 4756822Seric { 4856822Seric DBM *dbm; 4956822Seric 5056822Seric map_parseargs(map, &args, mapname); 5156822Seric if (map->map_file == NULL) 5256822Seric return FALSE; 5356822Seric dbm = dbm_open(map->map_file, O_RDONLY, 0644); 5456822Seric if (dbm == NULL) 5556822Seric { 56*56836Seric if (!bitset(MF_OPTIONAL, map->map_flags)) 57*56836Seric syserr("Cannot open DBM database %s", map->map_file); 5856822Seric return FALSE; 5956822Seric } 6056822Seric map->map_db = (void *) dbm; 6156822Seric return TRUE; 6256822Seric } 6356822Seric /* 6456822Seric ** DBM_MAP_LOOKUP -- look up a datum in a DBM-type map 6556822Seric ** 6656822Seric ** Parameters: 6756822Seric ** map -- the map to look up in. 6856822Seric ** buf -- a pointer to to the buffer containing the key. 6956822Seric ** This is a null terminated string. 7056822Seric ** bufsiz -- the size of buf -- note that this is in general 7156822Seric ** larger that strlen(buf), and buf can be changed 7256822Seric ** in place if desired. 7356822Seric ** av -- arguments from the config file (can be interpolated 7456822Seric ** into the final result). 7556822Seric ** 7656822Seric ** Returns: 7756822Seric ** A pointer to the rewritten result. 7856822Seric ** NULL if not found in the map. 7956822Seric */ 8056822Seric 8156822Seric char * 8256822Seric dbm_map_lookup(map, buf, bufsiz, av) 8356822Seric MAP *map; 8456822Seric char buf[]; 8556822Seric int bufsiz; 8656822Seric char **av; 8756822Seric { 8856822Seric datum key, val; 8956822Seric 9056822Seric key.dptr = buf; 9156822Seric key.dsize = strlen(buf); 9256822Seric if (bitset(MF_INCLNULL, map->map_flags)) 9356822Seric key.dsize++; 9456822Seric val = dbm_fetch(map->map_db, key); 9556822Seric if (val.dptr == NULL) 9656822Seric return NULL; 9756822Seric map_rewrite(val.dptr, val.dsize, buf, bufsiz, av); 9856822Seric return buf; 9956822Seric } 10056822Seric 10156822Seric #endif /* DBM_MAP */ 10256822Seric 10356822Seric #ifdef BTREE_MAP 10456822Seric 10556822Seric /* 10656822Seric ** BTREE_MAP_INIT -- BTREE-style map initialization 10756822Seric ** 10856822Seric ** Parameters: 10956822Seric ** map -- the pointer to the actual map 11056822Seric ** mapname -- the name of the map (for error messages) 11156822Seric ** args -- a pointer to the config file line arguments 11256822Seric ** 11356822Seric ** Returns: 11456822Seric ** TRUE -- if it could successfully open the map. 11556822Seric ** FALSE -- otherwise. 11656822Seric ** 11756822Seric ** Side Effects: 11856822Seric ** Gives an error if it can't open the map. 11956822Seric */ 12056822Seric 12156822Seric bool 12256822Seric bt_map_init(map, mapname, args) 12356822Seric MAP *map; 12456822Seric char *mapname; 12556822Seric char *args; 12656822Seric { 12756822Seric DB *db; 12856822Seric 12956822Seric map_parseargs(map, &args, mapname); 13056822Seric if (map->map_file == NULL) 13156822Seric return FALSE; 13256822Seric db = dbopen(map->map_file, O_RDONLY, 0644, DB_BTREE, NULL); 13356822Seric if (db == NULL) 13456822Seric { 135*56836Seric if (!bitset(MF_OPTIONAL, map->map_flags)) 136*56836Seric syserr("Cannot open BTREE database %s", map->map_file); 13756822Seric return FALSE; 13856822Seric } 13956822Seric map->map_db = (void *) db; 14056822Seric return TRUE; 14156822Seric } 14256822Seric 14356822Seric #endif /* BTREE_MAP */ 14456822Seric 14556822Seric #ifdef HASH_MAP 14656822Seric 14756822Seric /* 14856822Seric ** HASH_MAP_INIT -- HASH-style map initialization 14956822Seric ** 15056822Seric ** Parameters: 15156822Seric ** map -- the pointer to the actual map 15256822Seric ** mapname -- the name of the map (for error messages) 15356822Seric ** args -- a pointer to the config file line arguments 15456822Seric ** 15556822Seric ** Returns: 15656822Seric ** TRUE -- if it could successfully open the map. 15756822Seric ** FALSE -- otherwise. 15856822Seric ** 15956822Seric ** Side Effects: 16056822Seric ** Gives an error if it can't open the map. 16156822Seric */ 16256822Seric 16356822Seric bool 16456822Seric hash_map_init(map, mapname, args) 16556822Seric MAP *map; 16656822Seric char *mapname; 16756822Seric char *args; 16856822Seric { 16956822Seric DB *db; 17056822Seric 17156822Seric map_parseargs(map, &args, mapname); 17256822Seric if (map->map_file == NULL) 17356822Seric return FALSE; 17456822Seric db = dbopen(map->map_file, O_RDONLY, 0644, DB_HASH, NULL); 17556822Seric if (db == NULL) 17656822Seric { 177*56836Seric if (!bitset(MF_OPTIONAL, map->map_flags)) 178*56836Seric syserr("Cannot open HASH database %s", map->map_file); 17956822Seric return FALSE; 18056822Seric } 18156822Seric map->map_db = (void *) db; 18256822Seric return TRUE; 18356822Seric } 18456822Seric 18556822Seric #endif /* HASH_MAP */ 18656822Seric 18756822Seric #if defined(BTREE_MAP) || defined(HASH_MAP) 18856822Seric 18956822Seric /* 19056822Seric ** DB_MAP_LOOKUP -- look up a datum in a BTREE- or HASH-type map 19156822Seric ** 19256822Seric ** Parameters: 19356822Seric ** map -- the map to look up in. 19456822Seric ** buf -- a pointer to to the buffer containing the key. 19556822Seric ** This is a null terminated string. 19656822Seric ** bufsiz -- the size of buf -- note that this is in general 19756822Seric ** larger that strlen(buf), and buf can be changed 19856822Seric ** in place if desired. 19956822Seric ** av -- arguments from the config file (can be interpolated 20056822Seric ** into the final result). 20156822Seric ** 20256822Seric ** Returns: 20356822Seric ** A pointer to the rewritten result. 20456822Seric ** NULL if not found in the map. 20556822Seric */ 20656822Seric 20756822Seric char * 20856822Seric db_map_lookup(map, buf, bufsiz, av) 20956822Seric MAP *map; 21056822Seric char buf[]; 21156822Seric int bufsiz; 21256822Seric char **av; 21356822Seric { 21456822Seric DBT key, val; 21556822Seric 21656822Seric key.data = buf; 21756822Seric key.size = strlen(buf); 21856822Seric if (bitset(MF_INCLNULL, map->map_flags)) 21956822Seric key.size++; 220*56836Seric if (((DB *) map->map_db)->get((DB *) map->map_db, &key, &val, 0) != 0) 22156822Seric return NULL; 22256822Seric map_rewrite(val.data, val.size, buf, bufsiz, av); 22356822Seric return buf; 22456822Seric } 22556822Seric 22656822Seric #endif /* BTREE_MAP || HASH_MAP */ 22756822Seric /* 22856822Seric ** MAP_PARSEARGS -- parse config line arguments for database lookup 22956822Seric ** 23056822Seric ** Parameters: 23156822Seric ** map -- the map being initialized. 23256822Seric ** pp -- an indirect pointer to the config line. It will 23356822Seric ** be replaced with a pointer to the next field 23456822Seric ** on the line. 23556822Seric ** mapname -- the name of the map (for errors). 23656822Seric ** 23756822Seric ** Returns: 23856822Seric ** none 23956822Seric ** 24056822Seric ** Side Effects: 24156822Seric ** null terminates the filename; stores it in map 24256822Seric */ 24356822Seric 24456822Seric map_parseargs(map, pp, mapname) 24556822Seric MAP *map; 24656822Seric char **pp; 24756822Seric char *mapname; 24856822Seric { 24956822Seric register char *p = *pp; 25056822Seric 25156822Seric for (;;) 25256822Seric { 25356822Seric while (isspace(*p)) 25456822Seric p++; 25556822Seric if (*p != '-') 25656822Seric break; 25756822Seric switch (*++p) 25856822Seric { 25956822Seric case 'N': 26056822Seric map->map_flags |= MF_INCLNULL; 26156822Seric break; 262*56836Seric 263*56836Seric case 'o': 264*56836Seric map->map_flags |= MF_OPTIONAL; 265*56836Seric break; 266*56836Seric 267*56836Seric case 'a': 268*56836Seric map->map_app = ++p; 269*56836Seric break; 27056822Seric } 27156822Seric while (*p != '\0' && !isspace(*p)) 27256822Seric p++; 273*56836Seric if (*p != '\0') 274*56836Seric *p++ = 0; 27556822Seric } 276*56836Seric if (map->map_app != NULL) 277*56836Seric map->map_app = newstr(map->map_app); 27856822Seric 27956822Seric if (*p == '\0') 28056822Seric { 28156822Seric syserr("No file name for map %s", mapname); 28256822Seric return NULL; 28356822Seric } 28456822Seric map->map_file = p; 28556822Seric while (*p != '\0' && !isspace(*p)) 28656822Seric p++; 28756822Seric if (*p != '\0') 28856822Seric *p++ = '\0'; 289*56836Seric map->map_file = newstr(map->map_file); 29056822Seric *pp = p; 29156822Seric } 29256822Seric /* 29356822Seric ** MAP_REWRITE -- rewrite a database key, interpolating %n indications. 29456822Seric ** 29556822Seric ** Parameters: 29656822Seric ** s -- the string to rewrite, NOT necessarily null terminated. 29756822Seric ** slen -- the length of s. 29856822Seric ** buf -- the place to write it. 29956822Seric ** buflen -- the length of buf. 30056822Seric ** av -- arguments to interpolate into buf. 30156822Seric ** 30256822Seric ** Returns: 30356822Seric ** none. 30456822Seric ** 30556822Seric ** Side Effects: 30656822Seric ** none. 30756822Seric */ 30856822Seric 30956822Seric map_rewrite(s, slen, buf, buflen, av) 31056822Seric register char *s; 31156822Seric int slen; 31256822Seric char buf[]; 31356822Seric int buflen; 31456822Seric char **av; 31556822Seric { 31656822Seric register char *bp; 31756822Seric char *buflim; 31856822Seric register char c; 31956822Seric char **avp; 32056822Seric register char *ap; 32156822Seric 32256822Seric if (tTd(23, 1)) 32356822Seric { 32456822Seric printf("map_rewrite(%.*s), av =\n", slen, s); 32556822Seric for (avp = av; *avp != NULL; avp++) 32656822Seric printf("\t%s\n", *avp); 32756822Seric } 32856822Seric 32956822Seric bp = buf; 33056822Seric buflim = &buf[buflen - 2]; 33156822Seric while (--slen >= 0 && (c = *s++) != '\0') 33256822Seric { 33356822Seric if (c != '%') 33456822Seric { 33556822Seric pushc: 33656822Seric if (bp < buflim) 33756822Seric *bp++ = c; 33856822Seric continue; 33956822Seric } 34056822Seric if (--slen < 0 || (c = *s++) == '\0') 34156822Seric c = '%'; 34256822Seric if (c == '%') 34356822Seric goto pushc; 34456822Seric if (!isdigit(c)) 34556822Seric { 34656822Seric *bp++ = '%'; 34756822Seric goto pushc; 34856822Seric } 34956822Seric c -= '0'; 35056822Seric for (avp = av; --c >= 0 && *avp != NULL; avp++) 35156822Seric continue; 35256822Seric if (*avp == NULL) 35356822Seric continue; 35456822Seric 35556822Seric /* transliterate argument into output string */ 35656822Seric for (ap = *avp; (c = *ap++) != '\0'; ) 35756822Seric { 35856822Seric if (bp < buflim) 35956822Seric *bp++ = c; 36056822Seric } 36156822Seric } 36256822Seric *bp++ = '\0'; 36356822Seric if (tTd(23, 1)) 36456822Seric printf("map_rewrite => %s\n", buf); 36556822Seric } 366