From aa0d26b5361272e655ead361718c097b7e29645f Mon Sep 17 00:00:00 2001 From: eric Date: Sun, 23 Dec 2018 15:53:24 +0000 Subject: [PATCH] Simplify the table backend interface: lookup results are returned as strings, and parsing is handled by the upper layer. ok gilles@ --- usr.sbin/smtpd/smtpd.h | 8 +++----- usr.sbin/smtpd/table.c | 31 ++++++++++++++++++++++--------- usr.sbin/smtpd/table_db.c | 25 ++++++++++++++++--------- usr.sbin/smtpd/table_getpwnam.c | 26 +++++++++++--------------- usr.sbin/smtpd/table_proc.c | 19 +++++++++++-------- usr.sbin/smtpd/table_static.c | 26 +++++++++++++++++--------- 6 files changed, 80 insertions(+), 55 deletions(-) diff --git a/usr.sbin/smtpd/smtpd.h b/usr.sbin/smtpd/smtpd.h index 1e844880381..324e64323c0 100644 --- a/usr.sbin/smtpd/smtpd.h +++ b/usr.sbin/smtpd/smtpd.h @@ -1,4 +1,4 @@ -/* $OpenBSD: smtpd.h,v 1.601 2018/12/22 13:09:05 gilles Exp $ */ +/* $OpenBSD: smtpd.h,v 1.602 2018/12/23 15:53:24 eric Exp $ */ /* * Copyright (c) 2008 Gilles Chehade @@ -375,8 +375,8 @@ struct table_backend { void *(*open)(struct table *); int (*update)(struct table *); void (*close)(void *); - int (*lookup)(void *, struct dict *, const char *, enum table_service, union lookup *); - int (*fetch)(void *, struct dict *, enum table_service, union lookup *); + int (*lookup)(void *, struct dict *, const char *, enum table_service, char **); + int (*fetch)(void *, struct dict *, enum table_service, char **); }; @@ -1654,8 +1654,6 @@ int table_regex_match(const char *, const char *); void table_open_all(struct smtpd *); void table_dump_all(struct smtpd *); void table_close_all(struct smtpd *); -int table_parse_lookup(enum table_service, const char *, const char *, - union lookup *); /* to.c */ diff --git a/usr.sbin/smtpd/table.c b/usr.sbin/smtpd/table.c index 187cb0edefb..f070ab48a12 100644 --- a/usr.sbin/smtpd/table.c +++ b/usr.sbin/smtpd/table.c @@ -1,4 +1,4 @@ -/* $OpenBSD: table.c,v 1.33 2018/12/21 21:35:29 gilles Exp $ */ +/* $OpenBSD: table.c,v 1.34 2018/12/23 15:53:24 eric Exp $ */ /* * Copyright (c) 2013 Eric Faurot @@ -53,6 +53,8 @@ extern struct table_backend table_backend_proc; static const char * table_service_name(enum table_service); static const char * table_backend_name(struct table_backend *); static const char * table_dump_lookup(enum table_service, union lookup *); +static int table_parse_lookup(enum table_service, const char *, const char *, + union lookup *); static int parse_sockaddr(struct sockaddr *, int, const char *); static unsigned int last_table_id = 0; @@ -125,7 +127,7 @@ table_lookup(struct table *table, struct dict *params, const char *key, enum tab union lookup *lk) { int r; - char lkey[1024]; + char lkey[1024], *buf = NULL; if (table->t_backend->lookup == NULL) return (-1); @@ -135,9 +137,9 @@ table_lookup(struct table *table, struct dict *params, const char *key, enum tab return -1; } - r = table->t_backend->lookup(table->t_handle, params, lkey, kind, lk); + r = table->t_backend->lookup(table->t_handle, params, lkey, kind, lk ? &buf : NULL); - if (r == 1) + if (r == 1) { log_trace(TRACE_LOOKUP, "lookup: %s \"%s\" as %s in table %s:%s -> %s%s%s", lk ? "lookup" : "check", lkey, @@ -145,8 +147,11 @@ table_lookup(struct table *table, struct dict *params, const char *key, enum tab table_backend_name(table->t_backend), table->t_name, lk ? "\"" : "", - (lk) ? table_dump_lookup(kind, lk): "found", + (lk) ? buf : "found", lk ? "\"" : ""); + if (buf) + r = table_parse_lookup(kind, lkey, buf, lk); + } else log_trace(TRACE_LOOKUP, "lookup: %s \"%s\" as %s in table %s:%s -> %d", lk ? "lookup" : "check", @@ -156,6 +161,8 @@ table_lookup(struct table *table, struct dict *params, const char *key, enum tab table->t_name, r); + free(buf); + return (r); } @@ -163,20 +170,24 @@ int table_fetch(struct table *table, struct dict *params, enum table_service kind, union lookup *lk) { int r; + char *buf = NULL; if (table->t_backend->fetch == NULL) return (-1); - r = table->t_backend->fetch(table->t_handle, params, kind, lk); + r = table->t_backend->fetch(table->t_handle, params, kind, lk ? &buf : NULL); - if (r == 1) + if (r == 1) { log_trace(TRACE_LOOKUP, "lookup: fetch %s from table %s:%s -> %s%s%s", table_service_name(kind), table_backend_name(table->t_backend), table->t_name, lk ? "\"" : "", - (lk) ? table_dump_lookup(kind, lk): "found", + (lk) ? buf : "found", lk ? "\"" : ""); + if (buf) + r = table_parse_lookup(kind, NULL, buf, lk); + } else log_trace(TRACE_LOOKUP, "lookup: fetch %s from table %s:%s -> %d", table_service_name(kind), @@ -184,6 +195,8 @@ table_fetch(struct table *table, struct dict *params, enum table_service kind, u table->t_name, r); + free(buf); + return (r); } @@ -541,7 +554,7 @@ table_close_all(struct smtpd *conf) table_close(t); } -int +static int table_parse_lookup(enum table_service service, const char *key, const char *line, union lookup *lk) { diff --git a/usr.sbin/smtpd/table_db.c b/usr.sbin/smtpd/table_db.c index aeeed209e87..606c54e90f3 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.10 2018/05/31 21:06:12 gilles Exp $ */ +/* $OpenBSD: table_db.c,v 1.11 2018/12/23 15:53:24 eric Exp $ */ /* * Copyright (c) 2011 Gilles Chehade @@ -43,8 +43,8 @@ static int table_db_config(struct table *); static int table_db_update(struct table *); static void *table_db_open(struct table *); -static int table_db_lookup(void *, struct dict *, const char *, enum table_service, union lookup *); -static int table_db_fetch(void *, struct dict *, enum table_service, union lookup *); +static int table_db_lookup(void *, struct dict *, const char *, enum table_service, char **); +static int table_db_fetch(void *, struct dict *, enum table_service, char **); static void table_db_close(void *); static char *table_db_get_entry(void *, const char *, size_t *); @@ -143,7 +143,7 @@ table_db_close(void *hdl) static int table_db_lookup(void *hdl, struct dict *params, const char *key, enum table_service service, - union lookup *lk) + char **dst) { struct dbhandle *handle = hdl; struct table *table = NULL; @@ -176,15 +176,16 @@ table_db_lookup(void *hdl, struct dict *params, const char *key, enum table_serv return 0; ret = 1; - if (lk) - ret = table_parse_lookup(service, key, line, lk); - free(line); + if (dst) + *dst = line; + else + free(line); return ret; } static int -table_db_fetch(void *hdl, struct dict *params, enum table_service service, union lookup *lk) +table_db_fetch(void *hdl, struct dict *params, enum table_service service, char **dst) { struct dbhandle *handle = hdl; struct table *table = handle->table; @@ -203,7 +204,13 @@ table_db_fetch(void *hdl, struct dict *params, enum table_service service, union return 0; } - return table_parse_lookup(service, NULL, dbk.data, lk); + if (dst) { + *dst = strdup(dbk.data); + if (*dst == NULL) + return -1; + } + + return 1; } diff --git a/usr.sbin/smtpd/table_getpwnam.c b/usr.sbin/smtpd/table_getpwnam.c index 83cbf797d7b..046eed56966 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.4 2015/01/20 17:37:54 deraadt Exp $ */ +/* $OpenBSD: table_getpwnam.c,v 1.5 2018/12/23 15:53:24 eric Exp $ */ /* * Copyright (c) 2012 Gilles Chehade @@ -42,7 +42,7 @@ static int table_getpwnam_config(struct table *); static int table_getpwnam_update(struct table *); static void *table_getpwnam_open(struct table *); static int table_getpwnam_lookup(void *, struct dict *, const char *, enum table_service, - union lookup *); + char **); static void table_getpwnam_close(void *); struct table_backend table_backend_getpwnam = { @@ -83,10 +83,9 @@ table_getpwnam_close(void *hdl) static int table_getpwnam_lookup(void *hdl, struct dict *params, const char *key, enum table_service kind, - union lookup *lk) + char **dst) { struct passwd *pw; - size_t s; if (kind != K_USERINFO) return -1; @@ -101,19 +100,16 @@ table_getpwnam_lookup(void *hdl, struct dict *params, const char *key, enum tabl return -1; return 0; } - if (lk == NULL) + if (dst == NULL) return 1; - lk->userinfo.uid = pw->pw_uid; - lk->userinfo.gid = pw->pw_gid; - s = strlcpy(lk->userinfo.username, pw->pw_name, - sizeof(lk->userinfo.username)); - if (s >= sizeof(lk->userinfo.username)) - return (-1); - s = strlcpy(lk->userinfo.directory, pw->pw_dir, - sizeof(lk->userinfo.directory)); - if (s >= sizeof(lk->userinfo.directory)) - return (-1); + if (asprintf(dst, "%d:%d:%s", + pw->pw_uid, + pw->pw_gid, + pw->pw_dir) == -1) { + *dst = NULL; + return -1; + } return (1); } diff --git a/usr.sbin/smtpd/table_proc.c b/usr.sbin/smtpd/table_proc.c index 27abbfe0dec..ab3f5404a6f 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.7 2018/05/31 21:06:12 gilles Exp $ */ +/* $OpenBSD: table_proc.c,v 1.8 2018/12/23 15:53:24 eric Exp $ */ /* * Copyright (c) 2013 Eric Faurot @@ -196,15 +196,14 @@ imsg_add_params(struct ibuf *buf, struct dict *params) } static int -table_proc_lookup(void *arg, struct dict *params, const char *k, enum table_service s, - union lookup *lk) +table_proc_lookup(void *arg, struct dict *params, const char *k, enum table_service s, char **dst) { struct table_proc_priv *priv = arg; struct ibuf *buf; int r; buf = imsg_create(&priv->ibuf, - lk ? PROC_TABLE_LOOKUP : PROC_TABLE_CHECK, 0, 0, + dst ? PROC_TABLE_LOOKUP : PROC_TABLE_CHECK, 0, 0, sizeof(s) + strlen(k) + 1); if (buf == NULL) @@ -220,7 +219,7 @@ table_proc_lookup(void *arg, struct dict *params, const char *k, enum table_serv table_proc_call(priv); table_proc_read(&r, sizeof(r)); - if (r == 1 && lk) { + if (r == 1 && dst) { if (rlen == 0) { log_warnx("warn: table-proc: empty response"); fatalx("table-proc: exiting"); @@ -229,7 +228,9 @@ table_proc_lookup(void *arg, struct dict *params, const char *k, enum table_serv log_warnx("warn: table-proc: not NUL-terminated"); fatalx("table-proc: exiting"); } - r = table_parse_lookup(s, k, rdata, lk); + *dst = strdup(rdata); + if (*dst == NULL) + r = -1; table_proc_read(NULL, rlen); } @@ -239,7 +240,7 @@ table_proc_lookup(void *arg, struct dict *params, const char *k, enum table_serv } static int -table_proc_fetch(void *arg, struct dict *params, enum table_service s, union lookup *lk) +table_proc_fetch(void *arg, struct dict *params, enum table_service s, char **dst) { struct table_proc_priv *priv = arg; struct ibuf *buf; @@ -266,7 +267,9 @@ table_proc_fetch(void *arg, struct dict *params, enum table_service s, union loo log_warnx("warn: table-proc: not NUL-terminated"); fatalx("table-proc: exiting"); } - r = table_parse_lookup(s, NULL, rdata, lk); + *dst = strdup(rdata); + if (*dst == NULL) + r = -1; table_proc_read(NULL, rlen); } diff --git a/usr.sbin/smtpd/table_static.c b/usr.sbin/smtpd/table_static.c index 7be9cf21074..352a0541ab1 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.20 2018/11/01 10:47:46 gilles Exp $ */ +/* $OpenBSD: table_static.c,v 1.21 2018/12/23 15:53:24 eric Exp $ */ /* * Copyright (c) 2013 Eric Faurot @@ -43,9 +43,9 @@ static int table_static_config(struct table *); static int table_static_update(struct table *); static void *table_static_open(struct table *); static int table_static_lookup(void *, struct dict *, const char *, - enum table_service, union lookup *); + enum table_service, char **); static int table_static_fetch(void *, struct dict *, enum table_service, - union lookup *); + char **); static void table_static_close(void *); struct table_backend table_backend_static = { @@ -216,7 +216,7 @@ table_static_close(void *hdl) static int table_static_lookup(void *hdl, struct dict *params, const char *key, - enum table_service service, union lookup *lk) + enum table_service service, char **dst) { struct table *m = hdl; char *line; @@ -251,18 +251,22 @@ table_static_lookup(void *hdl, struct dict *params, const char *key, break; } - if (lk == NULL) + if (dst == NULL) return ret ? 1 : 0; if (ret == 0) return 0; - return table_parse_lookup(service, key, line, lk); + *dst = strdup(line); + if (*dst == NULL) + return -1; + + return 1; } static int table_static_fetch(void *hdl, struct dict *params, - enum table_service service, union lookup *lk) + enum table_service service, char **dst) { struct table *t = hdl; const char *k; @@ -273,8 +277,12 @@ table_static_fetch(void *hdl, struct dict *params, return 0; } - if (lk == NULL) + if (dst == NULL) return 1; - return table_parse_lookup(service, NULL, k, lk); + *dst = strdup(k); + if (*dst == NULL) + return -1; + + return 1; } -- 2.20.1