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*56847Seric static char sccsid[] = "@(#)map.c 5.3 (Berkeley) 11/16/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 { 5656836Seric if (!bitset(MF_OPTIONAL, map->map_flags)) 5756836Seric 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 { 13556836Seric if (!bitset(MF_OPTIONAL, map->map_flags)) 13656836Seric 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 { 17756836Seric if (!bitset(MF_OPTIONAL, map->map_flags)) 17856836Seric 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); 218*56847Seric if (bitset(MF_FOLDCASE, map->map_flags)) 219*56847Seric { 220*56847Seric register char *p; 221*56847Seric 222*56847Seric for (p = buf; *p != '\0'; p++) 223*56847Seric if (isupper(*p)) 224*56847Seric *p = tolower(*p); 225*56847Seric } 22656822Seric if (bitset(MF_INCLNULL, map->map_flags)) 22756822Seric key.size++; 22856836Seric if (((DB *) map->map_db)->get((DB *) map->map_db, &key, &val, 0) != 0) 22956822Seric return NULL; 23056822Seric map_rewrite(val.data, val.size, buf, bufsiz, av); 23156822Seric return buf; 23256822Seric } 23356822Seric 23456822Seric #endif /* BTREE_MAP || HASH_MAP */ 23556822Seric /* 23656822Seric ** MAP_PARSEARGS -- parse config line arguments for database lookup 23756822Seric ** 23856822Seric ** Parameters: 23956822Seric ** map -- the map being initialized. 24056822Seric ** pp -- an indirect pointer to the config line. It will 24156822Seric ** be replaced with a pointer to the next field 24256822Seric ** on the line. 24356822Seric ** mapname -- the name of the map (for errors). 24456822Seric ** 24556822Seric ** Returns: 24656822Seric ** none 24756822Seric ** 24856822Seric ** Side Effects: 24956822Seric ** null terminates the filename; stores it in map 25056822Seric */ 25156822Seric 25256822Seric map_parseargs(map, pp, mapname) 25356822Seric MAP *map; 25456822Seric char **pp; 25556822Seric char *mapname; 25656822Seric { 25756822Seric register char *p = *pp; 25856822Seric 25956822Seric for (;;) 26056822Seric { 26156822Seric while (isspace(*p)) 26256822Seric p++; 26356822Seric if (*p != '-') 26456822Seric break; 26556822Seric switch (*++p) 26656822Seric { 26756822Seric case 'N': 26856822Seric map->map_flags |= MF_INCLNULL; 26956822Seric break; 27056836Seric 27156836Seric case 'o': 27256836Seric map->map_flags |= MF_OPTIONAL; 27356836Seric break; 27456836Seric 275*56847Seric case 'f': 276*56847Seric map->map_flags |= MF_FOLDCASE; 277*56847Seric break; 278*56847Seric 27956836Seric case 'a': 28056836Seric map->map_app = ++p; 28156836Seric break; 28256822Seric } 28356822Seric while (*p != '\0' && !isspace(*p)) 28456822Seric p++; 28556836Seric if (*p != '\0') 28656836Seric *p++ = 0; 28756822Seric } 28856836Seric if (map->map_app != NULL) 28956836Seric map->map_app = newstr(map->map_app); 29056822Seric 29156822Seric if (*p == '\0') 29256822Seric { 29356822Seric syserr("No file name for map %s", mapname); 29456822Seric return NULL; 29556822Seric } 29656822Seric map->map_file = p; 29756822Seric while (*p != '\0' && !isspace(*p)) 29856822Seric p++; 29956822Seric if (*p != '\0') 30056822Seric *p++ = '\0'; 30156836Seric map->map_file = newstr(map->map_file); 30256822Seric *pp = p; 30356822Seric } 30456822Seric /* 30556822Seric ** MAP_REWRITE -- rewrite a database key, interpolating %n indications. 30656822Seric ** 30756822Seric ** Parameters: 30856822Seric ** s -- the string to rewrite, NOT necessarily null terminated. 30956822Seric ** slen -- the length of s. 31056822Seric ** buf -- the place to write it. 31156822Seric ** buflen -- the length of buf. 31256822Seric ** av -- arguments to interpolate into buf. 31356822Seric ** 31456822Seric ** Returns: 31556822Seric ** none. 31656822Seric ** 31756822Seric ** Side Effects: 31856822Seric ** none. 31956822Seric */ 32056822Seric 32156822Seric map_rewrite(s, slen, buf, buflen, av) 32256822Seric register char *s; 32356822Seric int slen; 32456822Seric char buf[]; 32556822Seric int buflen; 32656822Seric char **av; 32756822Seric { 32856822Seric register char *bp; 32956822Seric char *buflim; 33056822Seric register char c; 33156822Seric char **avp; 33256822Seric register char *ap; 33356822Seric 33456822Seric if (tTd(23, 1)) 33556822Seric { 33656822Seric printf("map_rewrite(%.*s), av =\n", slen, s); 33756822Seric for (avp = av; *avp != NULL; avp++) 33856822Seric printf("\t%s\n", *avp); 33956822Seric } 34056822Seric 34156822Seric bp = buf; 34256822Seric buflim = &buf[buflen - 2]; 34356822Seric while (--slen >= 0 && (c = *s++) != '\0') 34456822Seric { 34556822Seric if (c != '%') 34656822Seric { 34756822Seric pushc: 34856822Seric if (bp < buflim) 34956822Seric *bp++ = c; 35056822Seric continue; 35156822Seric } 35256822Seric if (--slen < 0 || (c = *s++) == '\0') 35356822Seric c = '%'; 35456822Seric if (c == '%') 35556822Seric goto pushc; 35656822Seric if (!isdigit(c)) 35756822Seric { 35856822Seric *bp++ = '%'; 35956822Seric goto pushc; 36056822Seric } 36156822Seric c -= '0'; 36256822Seric for (avp = av; --c >= 0 && *avp != NULL; avp++) 36356822Seric continue; 36456822Seric if (*avp == NULL) 36556822Seric continue; 36656822Seric 36756822Seric /* transliterate argument into output string */ 36856822Seric for (ap = *avp; (c = *ap++) != '\0'; ) 36956822Seric { 37056822Seric if (bp < buflim) 37156822Seric *bp++ = c; 37256822Seric } 37356822Seric } 37456822Seric *bp++ = '\0'; 37556822Seric if (tTd(23, 1)) 37656822Seric printf("map_rewrite => %s\n", buf); 37756822Seric } 378