diff options
author | Gilles Chehade <gilles@cvs.openbsd.org> | 2012-05-13 00:10:50 +0000 |
---|---|---|
committer | Gilles Chehade <gilles@cvs.openbsd.org> | 2012-05-13 00:10:50 +0000 |
commit | d4519c494c7974b50ea27f225264140734385a4b (patch) | |
tree | 1eb6171f3d6576734c9b808deb6534dd93cf65ac /usr.sbin/smtpd/map_db.c | |
parent | be2353b2a7b188c78bbf94f4c28cef23d8951bd7 (diff) |
- cleanup parse.y by removing lots of code that should not have been there,
but in ruleset.c and util.c instead.
- introduce the new map_compare() map API call to allow iterating over keys
and comparing them with provided key using provided function. this allows
checking a partial key in a key set, very useful for comparing an address
to a set of netmask.
- introduce new map kind K_NETADDR
- implement K_NETADDR for map_db and map_stdio
- teach ruleset checking how to use the map_compare() with K_NETADDR
we can now do the following:
map "srcaddr" source plain "/etc/mail/srcaddr.txt"
accept from map srcaddr for domain "openbsd.org" [...]
Diffstat (limited to 'usr.sbin/smtpd/map_db.c')
-rw-r--r-- | usr.sbin/smtpd/map_db.c | 58 |
1 files changed, 56 insertions, 2 deletions
diff --git a/usr.sbin/smtpd/map_db.c b/usr.sbin/smtpd/map_db.c index 860a8a54c6f..6b1e462d61c 100644 --- a/usr.sbin/smtpd/map_db.c +++ b/usr.sbin/smtpd/map_db.c @@ -1,4 +1,4 @@ -/* $OpenBSD: map_db.c,v 1.2 2012/05/12 15:29:16 gilles Exp $ */ +/* $OpenBSD: map_db.c,v 1.3 2012/05/13 00:10:49 gilles Exp $ */ /* * Copyright (c) 2011 Gilles Chehade <gilles@openbsd.org> @@ -39,18 +39,22 @@ /* db(3) backend */ static void *map_db_open(char *); static void *map_db_lookup(void *, char *, enum map_kind); +static int map_db_compare(void *, char *, enum map_kind, + int (*)(char *, char *)); static void map_db_close(void *); static char *map_db_get_entry(void *, char *, size_t *); static void *map_db_credentials(char *, char *, size_t); static void *map_db_alias(char *, char *, size_t); static void *map_db_virtual(char *, char *, size_t); +static void *map_db_netaddr(char *, char *, size_t); struct map_backend map_backend_db = { map_db_open, map_db_close, - map_db_lookup + map_db_lookup, + map_db_compare }; @@ -93,6 +97,10 @@ map_db_lookup(void *hdl, char *key, enum map_kind kind) ret = map_db_virtual(key, line, len); break; + case K_NETADDR: + ret = map_db_netaddr(key, line, len); + break; + default: break; } @@ -102,6 +110,32 @@ map_db_lookup(void *hdl, char *key, enum map_kind kind) return ret; } +static int +map_db_compare(void *hdl, char *key, enum map_kind kind, + int (*func)(char *, char *)) +{ + int ret = 0; + DB *db = hdl; + DBT dbk; + DBT dbd; + int r; + char *buf = NULL; + + for (r = db->seq(db, &dbk, &dbd, R_FIRST); !r; + r = db->seq(db, &dbk, &dbd, R_NEXT)) { + buf = calloc(dbk.size+1, 1); + if (buf == NULL) + fatalx("calloc"); + strlcpy(buf, dbk.data, dbk.size+1); + log_debug("key: %s, buf: %s", key, buf); + if (func(key, buf)) + ret = 1; + free(buf); + if (ret) + break; + } + return ret; +} static char * map_db_get_entry(void *hdl, char *key, size_t *len) @@ -256,3 +290,23 @@ error: free(map_virtual); return NULL; } + + +static void * +map_db_netaddr(char *key, char *line, size_t len) +{ + struct map_netaddr *map_netaddr = NULL; + + map_netaddr = calloc(1, sizeof(struct map_netaddr)); + if (map_netaddr == NULL) + fatalx("calloc"); + + if (! text_to_netaddr(&map_netaddr->netaddr, line)) + goto error; + + return map_netaddr; + +error: + free(map_netaddr); + return NULL; +} |