From: eric Date: Thu, 27 Dec 2018 14:23:41 +0000 (+0000) Subject: introduce dump() and add() table methods, only implemented for static tables. X-Git-Url: http://artulab.com/gitweb/?a=commitdiff_plain;h=5f0e36ccfdb02df5b393fe82829144180a4d9529;p=openbsd introduce dump() and add() table methods, only implemented for static tables. ok gilles@ --- diff --git a/usr.sbin/smtpd/smtpd.h b/usr.sbin/smtpd/smtpd.h index 6f42711ac7d..8ac0a6d8911 100644 --- a/usr.sbin/smtpd/smtpd.h +++ b/usr.sbin/smtpd/smtpd.h @@ -1,4 +1,4 @@ -/* $OpenBSD: smtpd.h,v 1.610 2018/12/27 09:30:29 eric Exp $ */ +/* $OpenBSD: smtpd.h,v 1.611 2018/12/27 14:23:41 eric Exp $ */ /* * Copyright (c) 2008 Gilles Chehade @@ -365,6 +365,8 @@ struct table_backend { const char *name; const unsigned int services; int (*config)(struct table *); + int (*add)(struct table *, const char *, const char *); + void (*dump)(struct table *); int (*open)(struct table *); int (*update)(struct table *); void (*close)(struct table *); diff --git a/usr.sbin/smtpd/table.c b/usr.sbin/smtpd/table.c index 43ad9afabe4..f43e52973ea 100644 --- a/usr.sbin/smtpd/table.c +++ b/usr.sbin/smtpd/table.c @@ -1,4 +1,4 @@ -/* $OpenBSD: table.c,v 1.41 2018/12/27 09:30:29 eric Exp $ */ +/* $OpenBSD: table.c,v 1.42 2018/12/27 14:23:41 eric Exp $ */ /* * Copyright (c) 2013 Eric Faurot @@ -301,22 +301,11 @@ table_config(struct table *t) void table_add(struct table *t, const char *key, const char *val) { - char lkey[1024], *old; - - if (t->t_type & T_DYNAMIC) + if (t->t_backend->add == NULL) fatalx("table_add: cannot add to table"); - if (!lowercase(lkey, key, sizeof lkey)) { - log_warnx("warn: lookup key too long: %s", key); - return; - } - - old = dict_set(&t->t_dict, lkey, val ? xstrdup(val) : NULL); - if (old) { - log_warnx("warn: duplicate key \"%s\" in static table \"%s\"", - lkey, t->t_name); - free(old); - } + if (t->t_backend->add(t, key, val) == 0) + log_warnx("warn: failed to add \"%s\" in table \"%s\"", key, t->t_name); } int @@ -491,14 +480,12 @@ void table_dump_all(struct smtpd *conf) { struct table *t; - void *iter, *i2; - const char *key, *sep; - char *value; + void *iter; + const char *sep; char buf[1024]; iter = NULL; while (dict_iter(conf->sc_tables_dict, &iter, NULL, (void **)&t)) { - i2 = NULL; sep = ""; buf[0] = '\0'; if (t->t_type & T_DYNAMIC) { @@ -517,12 +504,8 @@ table_dump_all(struct smtpd *conf) } log_debug("TABLE \"%s\" type=%s config=\"%s\"", t->t_name, buf, t->t_config); - while(dict_iter(&t->t_dict, &i2, &key, (void**)&value)) { - if (value) - log_debug(" \"%s\" -> \"%s\"", key, value); - else - log_debug(" \"%s\"", key); - } + if (t->t_backend->dump) + t->t_backend->dump(t); } } diff --git a/usr.sbin/smtpd/table_db.c b/usr.sbin/smtpd/table_db.c index 9e71d1c30d3..add5dc0fd1a 100644 --- a/usr.sbin/smtpd/table_db.c +++ b/usr.sbin/smtpd/table_db.c @@ -1,4 +1,4 @@ -/* $OpenBSD: table_db.c,v 1.17 2018/12/27 09:30:29 eric Exp $ */ +/* $OpenBSD: table_db.c,v 1.18 2018/12/27 14:23:41 eric Exp $ */ /* * Copyright (c) 2011 Gilles Chehade @@ -57,6 +57,8 @@ struct table_backend table_backend_db = { "db", K_ALIAS|K_CREDENTIALS|K_DOMAIN|K_NETADDR|K_USERINFO|K_SOURCE|K_MAILADDR|K_ADDRNAME|K_MAILADDRMAP, table_db_config, + NULL, + NULL, table_db_open, table_db_update, table_db_close, diff --git a/usr.sbin/smtpd/table_getpwnam.c b/usr.sbin/smtpd/table_getpwnam.c index abc4fc8f8d2..78e6edc26da 100644 --- a/usr.sbin/smtpd/table_getpwnam.c +++ b/usr.sbin/smtpd/table_getpwnam.c @@ -1,4 +1,4 @@ -/* $OpenBSD: table_getpwnam.c,v 1.11 2018/12/27 09:30:29 eric Exp $ */ +/* $OpenBSD: table_getpwnam.c,v 1.12 2018/12/27 14:23:41 eric Exp $ */ /* * Copyright (c) 2012 Gilles Chehade @@ -49,6 +49,8 @@ struct table_backend table_backend_getpwnam = { "getpwnam", K_USERINFO, table_getpwnam_config, + NULL, + NULL, table_getpwnam_open, table_getpwnam_update, table_getpwnam_close, diff --git a/usr.sbin/smtpd/table_proc.c b/usr.sbin/smtpd/table_proc.c index f930142fff7..50592a74463 100644 --- a/usr.sbin/smtpd/table_proc.c +++ b/usr.sbin/smtpd/table_proc.c @@ -1,4 +1,4 @@ -/* $OpenBSD: table_proc.c,v 1.14 2018/12/27 09:30:29 eric Exp $ */ +/* $OpenBSD: table_proc.c,v 1.15 2018/12/27 14:23:41 eric Exp $ */ /* * Copyright (c) 2013 Eric Faurot @@ -268,6 +268,8 @@ struct table_backend table_backend_proc = { "proc", K_ANY, NULL, + NULL, + NULL, table_proc_open, table_proc_update, table_proc_close, diff --git a/usr.sbin/smtpd/table_static.c b/usr.sbin/smtpd/table_static.c index 88377b3f037..c0d122fe1f6 100644 --- a/usr.sbin/smtpd/table_static.c +++ b/usr.sbin/smtpd/table_static.c @@ -1,4 +1,4 @@ -/* $OpenBSD: table_static.c,v 1.27 2018/12/27 09:30:29 eric Exp $ */ +/* $OpenBSD: table_static.c,v 1.28 2018/12/27 14:23:41 eric Exp $ */ /* * Copyright (c) 2013 Eric Faurot @@ -40,6 +40,8 @@ /* static backend */ static int table_static_config(struct table *); +static int table_static_add(struct table *, const char *, const char *); +static void table_static_dump(struct table *); static int table_static_update(struct table *); static int table_static_open(struct table *); static int table_static_lookup(struct table *, enum table_service, const char *, @@ -53,6 +55,8 @@ struct table_backend table_backend_static = { K_SOURCE|K_MAILADDR|K_ADDRNAME|K_MAILADDRMAP|K_RELAYHOST| K_STRING|K_REGEX, table_static_config, + table_static_add, + table_static_dump, table_static_open, table_static_update, table_static_close, @@ -172,6 +176,42 @@ end: return ret; } +static int +table_static_add(struct table *table, const char *key, const char *val) +{ + char lkey[1024], *old; + + if (!lowercase(lkey, key, sizeof lkey)) { + log_warnx("warn: lookup key too long: %s", key); + return 0; + } + + old = dict_set(&table->t_dict, lkey, val ? xstrdup(val) : NULL); + if (old) { + log_warnx("warn: duplicate key \"%s\" in static table \"%s\"", + lkey, table->t_name); + free(old); + } + + return 1; +} + +static void +table_static_dump(struct table *table) +{ + const char *key; + char *value; + void *iter; + + iter = NULL; + while (dict_iter(&table->t_dict, &iter, &key, (void**)&value)) { + if (value) + log_debug(" \"%s\" -> \"%s\"", key, value); + else + log_debug(" \"%s\"", key); + } +} + static int table_static_update(struct table *table) {