summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--usr.sbin/smtpd/map.c47
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;
+}