From ef7a27e80c0eb62a9b45e8de9717bb91832c6c50 Mon Sep 17 00:00:00 2001 From: eric Date: Wed, 26 Dec 2018 15:55:09 +0000 Subject: [PATCH] introduce a table_match() function to check for a key in a table ok gilles@ --- usr.sbin/smtpd/lka_filter.c | 22 +++++++++++----------- usr.sbin/smtpd/ruleset.c | 33 ++++++++------------------------- usr.sbin/smtpd/smtpd.h | 3 ++- usr.sbin/smtpd/table.c | 8 +++++++- 4 files changed, 28 insertions(+), 38 deletions(-) diff --git a/usr.sbin/smtpd/lka_filter.c b/usr.sbin/smtpd/lka_filter.c index 35fc0bc31cc..eeba23e2453 100644 --- a/usr.sbin/smtpd/lka_filter.c +++ b/usr.sbin/smtpd/lka_filter.c @@ -1,4 +1,4 @@ -/* $OpenBSD: lka_filter.c,v 1.32 2018/12/26 14:15:12 eric Exp $ */ +/* $OpenBSD: lka_filter.c,v 1.33 2018/12/26 15:55:09 eric Exp $ */ /* * Copyright (c) 2018 Gilles Chehade @@ -806,7 +806,7 @@ filter_check_rdns_table(struct filter *filter, enum table_service kind, const ch if (filter->config->rdns_table == NULL) return 0; - if (table_lookup(filter->config->rdns_table, key, kind, NULL) > 0) + if (table_match(filter->config->rdns_table, kind, key) > 0) ret = 1; return filter->config->not_rdns_table < 0 ? !ret : ret; @@ -820,7 +820,7 @@ filter_check_rdns_regex(struct filter *filter, const char *key) if (filter->config->rdns_regex == NULL) return 0; - if (table_lookup(filter->config->rdns_regex, key, K_REGEX, NULL) > 0) + if (table_match(filter->config->rdns_regex, K_REGEX, key) > 0) ret = 1; return filter->config->not_rdns_regex < 0 ? !ret : ret; } @@ -833,7 +833,7 @@ filter_check_src_table(struct filter *filter, enum table_service kind, const cha if (filter->config->src_table == NULL) return 0; - if (table_lookup(filter->config->src_table, key, kind, NULL) > 0) + if (table_match(filter->config->src_table, kind, key) > 0) ret = 1; return filter->config->not_src_table < 0 ? !ret : ret; } @@ -846,7 +846,7 @@ filter_check_src_regex(struct filter *filter, const char *key) if (filter->config->src_regex == NULL) return 0; - if (table_lookup(filter->config->src_regex, key, K_REGEX, NULL) > 0) + if (table_match(filter->config->src_regex, K_REGEX, key) > 0) ret = 1; return filter->config->not_src_regex < 0 ? !ret : ret; } @@ -859,7 +859,7 @@ filter_check_helo_table(struct filter *filter, enum table_service kind, const ch if (filter->config->helo_table == NULL) return 0; - if (table_lookup(filter->config->helo_table, key, kind, NULL) > 0) + if (table_match(filter->config->helo_table, kind, key) > 0) ret = 1; return filter->config->not_helo_table < 0 ? !ret : ret; } @@ -872,7 +872,7 @@ filter_check_helo_regex(struct filter *filter, const char *key) if (filter->config->helo_regex == NULL) return 0; - if (table_lookup(filter->config->helo_regex, key, K_REGEX, NULL) > 0) + if (table_match(filter->config->helo_regex, K_REGEX, key) > 0) ret = 1; return filter->config->not_helo_regex < 0 ? !ret : ret; } @@ -885,7 +885,7 @@ filter_check_mail_from_table(struct filter *filter, enum table_service kind, con if (filter->config->mail_from_table == NULL) return 0; - if (table_lookup(filter->config->mail_from_table, key, kind, NULL) > 0) + if (table_match(filter->config->mail_from_table, kind, key) > 0) ret = 1; return filter->config->not_mail_from_table < 0 ? !ret : ret; } @@ -898,7 +898,7 @@ filter_check_mail_from_regex(struct filter *filter, const char *key) if (filter->config->mail_from_regex == NULL) return 0; - if (table_lookup(filter->config->mail_from_regex, key, K_REGEX, NULL) > 0) + if (table_match(filter->config->mail_from_regex, K_REGEX, key) > 0) ret = 1; return filter->config->not_mail_from_regex < 0 ? !ret : ret; } @@ -911,7 +911,7 @@ filter_check_rcpt_to_table(struct filter *filter, enum table_service kind, const if (filter->config->rcpt_to_table == NULL) return 0; - if (table_lookup(filter->config->rcpt_to_table, key, kind, NULL) > 0) + if (table_match(filter->config->rcpt_to_table, kind, key) > 0) ret = 1; return filter->config->not_rcpt_to_table < 0 ? !ret : ret; } @@ -924,7 +924,7 @@ filter_check_rcpt_to_regex(struct filter *filter, const char *key) if (filter->config->rcpt_to_regex == NULL) return 0; - if (table_lookup(filter->config->rcpt_to_regex, key, K_REGEX, NULL) > 0) + if (table_match(filter->config->rcpt_to_regex, K_REGEX, key) > 0) ret = 1; return filter->config->not_rcpt_to_regex < 0 ? !ret : ret; } diff --git a/usr.sbin/smtpd/ruleset.c b/usr.sbin/smtpd/ruleset.c index 1239f927876..5af1b042e9b 100644 --- a/usr.sbin/smtpd/ruleset.c +++ b/usr.sbin/smtpd/ruleset.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ruleset.c,v 1.39 2018/12/26 14:15:12 eric Exp $ */ +/* $OpenBSD: ruleset.c,v 1.40 2018/12/26 15:55:09 eric Exp $ */ /* * Copyright (c) 2009 Gilles Chehade @@ -34,22 +34,6 @@ #include "log.h" -static int -ruleset_match_table_lookup(struct table *table, const char *key, enum table_service service) -{ - switch (table_lookup(table, key, service, NULL)) { - case 1: - return 1; - case -1: - log_warnx("warn: failure to perform a table lookup on table %s", - table->t_name); - return -1; - default: - break; - } - return 0; -} - static int ruleset_match_tag(struct rule *r, const struct envelope *evp) { @@ -64,7 +48,7 @@ ruleset_match_tag(struct rule *r, const struct envelope *evp) service = K_REGEX; table = table_find(env, r->table_tag, NULL); - if ((ret = ruleset_match_table_lookup(table, evp->tag, service)) < 0) + if ((ret = table_match(table, service, evp->tag)) < 0) return ret; return r->flag_tag < 0 ? !ret : ret; @@ -100,7 +84,7 @@ ruleset_match_from(struct rule *r, const struct envelope *evp) service = K_REGEX; table = table_find(env, r->table_from, NULL); - if ((ret = ruleset_match_table_lookup(table, key, service)) < 0) + if ((ret = table_match(table, service, key)) < 0) return -1; return r->flag_from < 0 ? !ret : ret; @@ -120,8 +104,7 @@ ruleset_match_to(struct rule *r, const struct envelope *evp) service = K_REGEX; table = table_find(env, r->table_for, NULL); - if ((ret = ruleset_match_table_lookup(table, evp->dest.domain, - service)) < 0) + if ((ret = table_match(table, service, evp->dest.domain)) < 0) return -1; return r->flag_for < 0 ? !ret : ret; @@ -141,7 +124,7 @@ ruleset_match_smtp_helo(struct rule *r, const struct envelope *evp) service = K_REGEX; table = table_find(env, r->table_smtp_helo, NULL); - if ((ret = ruleset_match_table_lookup(table, evp->helo, service)) < 0) + if ((ret = table_match(table, service, evp->helo)) < 0) return -1; return r->flag_smtp_helo < 0 ? !ret : ret; @@ -172,7 +155,7 @@ ruleset_match_smtp_auth(struct rule *r, const struct envelope *evp) /* * table = table_find(m->from_table, NULL); * key = evp->username; - * return ruleset_match_table_lookup(table, key, K_CREDENTIALS); + * return table_match(table, K_CREDENTIALS, key); */ return -1; @@ -201,7 +184,7 @@ ruleset_match_smtp_mail_from(struct rule *r, const struct envelope *evp) return -1; table = table_find(env, r->table_smtp_mail_from, NULL); - if ((ret = ruleset_match_table_lookup(table, key, service)) < 0) + if ((ret = table_match(table, service, key)) < 0) return -1; return r->flag_smtp_mail_from < 0 ? !ret : ret; @@ -225,7 +208,7 @@ ruleset_match_smtp_rcpt_to(struct rule *r, const struct envelope *evp) return -1; table = table_find(env, r->table_smtp_rcpt_to, NULL); - if ((ret = ruleset_match_table_lookup(table, key, service)) < 0) + if ((ret = table_match(table, service, key)) < 0) return -1; return r->flag_smtp_rcpt_to < 0 ? !ret : ret; diff --git a/usr.sbin/smtpd/smtpd.h b/usr.sbin/smtpd/smtpd.h index 6c1bdbbe0a5..b57f3812c79 100644 --- a/usr.sbin/smtpd/smtpd.h +++ b/usr.sbin/smtpd/smtpd.h @@ -1,4 +1,4 @@ -/* $OpenBSD: smtpd.h,v 1.605 2018/12/26 14:15:12 eric Exp $ */ +/* $OpenBSD: smtpd.h,v 1.606 2018/12/26 15:55:09 eric Exp $ */ /* * Copyright (c) 2008 Gilles Chehade @@ -1604,6 +1604,7 @@ void table_close(struct table *); int table_check_use(struct table *, uint32_t, uint32_t); int table_check_type(struct table *, uint32_t); int table_check_service(struct table *, uint32_t); +int table_match(struct table *, enum table_service, const char *); int table_lookup(struct table *, const char *, enum table_service, union lookup *); int table_fetch(struct table *, enum table_service, union lookup *); diff --git a/usr.sbin/smtpd/table.c b/usr.sbin/smtpd/table.c index e4dc739d573..81f72f0db5c 100644 --- a/usr.sbin/smtpd/table.c +++ b/usr.sbin/smtpd/table.c @@ -1,4 +1,4 @@ -/* $OpenBSD: table.c,v 1.36 2018/12/26 14:15:13 eric Exp $ */ +/* $OpenBSD: table.c,v 1.37 2018/12/26 15:55:09 eric Exp $ */ /* * Copyright (c) 2013 Eric Faurot @@ -115,6 +115,12 @@ table_find(struct smtpd *conf, const char *name, const char *tag) return dict_get(conf->sc_tables_dict, buf); } +int +table_match(struct table *table, enum table_service kind, const char *key) +{ + return table_lookup(table, key, kind, NULL); +} + int table_lookup(struct table *table, const char *key, enum table_service kind, union lookup *lk) -- 2.20.1