diff options
author | Gilles Chehade <gilles@cvs.openbsd.org> | 2009-03-06 23:38:19 +0000 |
---|---|---|
committer | Gilles Chehade <gilles@cvs.openbsd.org> | 2009-03-06 23:38:19 +0000 |
commit | 9a7517ee3534ef49443497b00f504702aaa3125b (patch) | |
tree | 7315c8351522190b5cb4b0099f3d8556a8532bbe | |
parent | e06de7b392fe4648cb926c977a9816c7075f1594 (diff) |
introduce map_dblookup() which allows us to query db maps for plain entries
and catch (and warn) about invalid map types.
-rw-r--r-- | usr.sbin/smtpd/map.c | 47 |
1 files changed, 46 insertions, 1 deletions
diff --git a/usr.sbin/smtpd/map.c b/usr.sbin/smtpd/map.c index 00a87f16a49..f9db97397d5 100644 --- a/usr.sbin/smtpd/map.c +++ b/usr.sbin/smtpd/map.c @@ -1,4 +1,4 @@ -/* $OpenBSD: map.c,v 1.4 2009/01/01 16:15:47 jacekm Exp $ */ +/* $OpenBSD: map.c,v 1.5 2009/03/06 23:38:18 gilles Exp $ */ /* * Copyright (c) 2008 Gilles Chehade <gilles@openbsd.org> @@ -22,8 +22,10 @@ #include <sys/param.h> #include <sys/socket.h> +#include <db.h> #include <errno.h> #include <event.h> +#include <fcntl.h> #include <stdio.h> #include <stdlib.h> #include <string.h> @@ -53,3 +55,46 @@ map_find(struct smtpd *env, objid_t id) } return (m); } + +char * +map_dblookup(struct smtpd *env, char *mapname, char *keyname) +{ + int ret; + DBT key; + DBT val; + DB *db; + struct map *map; + char *result = NULL; + + map = map_findbyname(env, mapname); + if (map == NULL) + return NULL; + + if (map->m_src != S_DB) { + log_warn("invalid map type for map \"%s\"", mapname); + return NULL; + } + + db = dbopen(map->m_config, O_RDONLY, 0600, DB_HASH, NULL); + if (db == NULL) + return NULL; + + key.data = keyname; + key.size = strlen(key.data) + 1; + + if ((ret = db->get(db, &key, &val, 0)) == -1) { + db->close(db); + return NULL; + } + + if (ret == 0) { + result = calloc(val.size, 1); + if (result == NULL) + fatal("calloc"); + (void)strlcpy(result, val.data, val.size); + } + + db->close(db); + + return ret == 0 ? result : NULL; +} |