diff options
-rw-r--r-- | usr.sbin/smtpd/aliases.c | 20 | ||||
-rw-r--r-- | usr.sbin/smtpd/lka.c | 12 | ||||
-rw-r--r-- | usr.sbin/smtpd/lka_session.c | 6 | ||||
-rw-r--r-- | usr.sbin/smtpd/makemap.c | 8 | ||||
-rw-r--r-- | usr.sbin/smtpd/mproc.c | 61 | ||||
-rw-r--r-- | usr.sbin/smtpd/ruleset.c | 8 | ||||
-rw-r--r-- | usr.sbin/smtpd/smtpd-api.h | 8 | ||||
-rw-r--r-- | usr.sbin/smtpd/smtpd.c | 57 | ||||
-rw-r--r-- | usr.sbin/smtpd/smtpd.h | 21 | ||||
-rw-r--r-- | usr.sbin/smtpd/table-ldap/Makefile | 3 | ||||
-rw-r--r-- | usr.sbin/smtpd/table.c | 51 | ||||
-rw-r--r-- | usr.sbin/smtpd/table_api.c | 61 | ||||
-rw-r--r-- | usr.sbin/smtpd/table_db.c | 10 | ||||
-rw-r--r-- | usr.sbin/smtpd/table_getpwnam.c | 6 | ||||
-rw-r--r-- | usr.sbin/smtpd/table_ldap.c | 14 | ||||
-rw-r--r-- | usr.sbin/smtpd/table_passwd.c | 14 | ||||
-rw-r--r-- | usr.sbin/smtpd/table_proc.c | 78 | ||||
-rw-r--r-- | usr.sbin/smtpd/table_socketmap.c | 14 | ||||
-rw-r--r-- | usr.sbin/smtpd/table_sqlite.c | 17 | ||||
-rw-r--r-- | usr.sbin/smtpd/table_static.c | 10 |
20 files changed, 306 insertions, 173 deletions
diff --git a/usr.sbin/smtpd/aliases.c b/usr.sbin/smtpd/aliases.c index 899540289a3..8070ae84581 100644 --- a/usr.sbin/smtpd/aliases.c +++ b/usr.sbin/smtpd/aliases.c @@ -1,4 +1,4 @@ -/* $OpenBSD: aliases.c,v 1.64 2013/05/24 17:03:14 eric Exp $ */ +/* $OpenBSD: aliases.c,v 1.65 2014/07/08 13:49:09 eric Exp $ */ /* * Copyright (c) 2008 Gilles Chehade <gilles@poolp.org> @@ -50,7 +50,7 @@ aliases_get(struct expand *expand, const char *username) userbase = expand->rule->r_userbase; xlowercase(buf, username, sizeof(buf)); - ret = table_lookup(mapping, buf, K_ALIAS, &lk); + ret = table_lookup(mapping, NULL, buf, K_ALIAS, &lk); if (ret <= 0) return ret; @@ -87,7 +87,7 @@ aliases_virtual_check(struct table *table, const struct mailaddr *maddr) xlowercase(buf, buf, sizeof(buf)); /* First, we lookup for full entry: user@domain */ - ret = table_lookup(table, buf, K_ALIAS, NULL); + ret = table_lookup(table, NULL, buf, K_ALIAS, NULL); if (ret < 0) return (-1); if (ret) @@ -96,7 +96,7 @@ aliases_virtual_check(struct table *table, const struct mailaddr *maddr) /* Failed ? We lookup for username only */ pbuf = strchr(buf, '@'); *pbuf = '\0'; - ret = table_lookup(table, buf, K_ALIAS, NULL); + ret = table_lookup(table, NULL, buf, K_ALIAS, NULL); if (ret < 0) return (-1); if (ret) @@ -104,14 +104,14 @@ aliases_virtual_check(struct table *table, const struct mailaddr *maddr) *pbuf = '@'; /* Failed ? We lookup for catch all for virtual domain */ - ret = table_lookup(table, pbuf, K_ALIAS, NULL); + ret = table_lookup(table, NULL, pbuf, K_ALIAS, NULL); if (ret < 0) return (-1); if (ret) return 1; /* Failed ? We lookup for a *global* catch all */ - ret = table_lookup(table, "@", K_ALIAS, NULL); + ret = table_lookup(table, NULL, "@", K_ALIAS, NULL); if (ret <= 0) return (ret); @@ -139,7 +139,7 @@ aliases_virtual_get(struct expand *expand, const struct mailaddr *maddr) xlowercase(buf, buf, sizeof(buf)); /* First, we lookup for full entry: user@domain */ - ret = table_lookup(mapping, buf, K_ALIAS, &lk); + ret = table_lookup(mapping, NULL, buf, K_ALIAS, &lk); if (ret < 0) return (-1); if (ret) @@ -148,7 +148,7 @@ aliases_virtual_get(struct expand *expand, const struct mailaddr *maddr) /* Failed ? We lookup for username only */ pbuf = strchr(buf, '@'); *pbuf = '\0'; - ret = table_lookup(mapping, buf, K_ALIAS, &lk); + ret = table_lookup(mapping, NULL, buf, K_ALIAS, &lk); if (ret < 0) return (-1); if (ret) @@ -156,14 +156,14 @@ aliases_virtual_get(struct expand *expand, const struct mailaddr *maddr) *pbuf = '@'; /* Failed ? We lookup for catch all for virtual domain */ - ret = table_lookup(mapping, pbuf, K_ALIAS, &lk); + ret = table_lookup(mapping, NULL, pbuf, K_ALIAS, &lk); if (ret < 0) return (-1); if (ret) goto expand; /* Failed ? We lookup for a *global* catch all */ - ret = table_lookup(mapping, "@", K_ALIAS, &lk); + ret = table_lookup(mapping, NULL, "@", K_ALIAS, &lk); if (ret <= 0) return (ret); diff --git a/usr.sbin/smtpd/lka.c b/usr.sbin/smtpd/lka.c index ad0e0dde371..5d8cd57e70d 100644 --- a/usr.sbin/smtpd/lka.c +++ b/usr.sbin/smtpd/lka.c @@ -1,4 +1,4 @@ -/* $OpenBSD: lka.c,v 1.170 2014/05/22 20:31:03 gilles Exp $ */ +/* $OpenBSD: lka.c,v 1.171 2014/07/08 13:49:09 eric Exp $ */ /* * Copyright (c) 2008 Pierre-Yves Ritschard <pyr@openbsd.org> @@ -339,7 +339,7 @@ lka_imsg(struct mproc *p, struct imsg *imsg) m_add_int(p, LKA_TEMPFAIL); } else { - ret = table_fetch(table, K_SOURCE, &lk); + ret = table_fetch(table, NULL, K_SOURCE, &lk); if (ret == -1) m_add_int(p, LKA_TEMPFAIL); else if (ret == 0) @@ -531,7 +531,7 @@ lka_authenticate(const char *tablename, const char *user, const char *password) return (LKA_TEMPFAIL); } - switch (table_lookup(table, user, K_CREDENTIALS, &lk)) { + switch (table_lookup(table, NULL, user, K_CREDENTIALS, &lk)) { case -1: log_warnx("warn: user credentials lookup fail for %s:%s", tablename, user); @@ -561,7 +561,7 @@ lka_credentials(const char *tablename, const char *label, char *dst, size_t sz) dst[0] = '\0'; - switch(table_lookup(table, label, K_CREDENTIALS, &lk)) { + switch(table_lookup(table, NULL, label, K_CREDENTIALS, &lk)) { case -1: log_warnx("warn: credentials lookup fail for %s:%s", tablename, label); @@ -602,7 +602,7 @@ lka_userinfo(const char *tablename, const char *username, struct userinfo *res) return (LKA_TEMPFAIL); } - switch (table_lookup(table, username, K_USERINFO, &lk)) { + switch (table_lookup(table, NULL, username, K_USERINFO, &lk)) { case -1: log_warnx("warn: failure during userinfo lookup %s:%s", tablename, username); @@ -632,7 +632,7 @@ lka_addrname(const char *tablename, const struct sockaddr *sa, return (LKA_TEMPFAIL); } - switch (table_lookup(table, source, K_ADDRNAME, &lk)) { + switch (table_lookup(table, NULL, source, K_ADDRNAME, &lk)) { case -1: log_warnx("warn: failure during helo lookup %s:%s", tablename, source); diff --git a/usr.sbin/smtpd/lka_session.c b/usr.sbin/smtpd/lka_session.c index d3ab19e01ba..5f5380d3752 100644 --- a/usr.sbin/smtpd/lka_session.c +++ b/usr.sbin/smtpd/lka_session.c @@ -1,4 +1,4 @@ -/* $OpenBSD: lka_session.c,v 1.67 2014/04/30 09:17:29 gilles Exp $ */ +/* $OpenBSD: lka_session.c,v 1.68 2014/07/08 13:49:09 eric Exp $ */ /* * Copyright (c) 2011 Gilles Chehade <gilles@poolp.org> @@ -385,7 +385,7 @@ lka_expand(struct lka_session *lks, struct rule *rule, struct expandnode *xn) break; } - r = table_lookup(rule->r_userbase, xn->u.user, K_USERINFO, &lk); + r = table_lookup(rule->r_userbase, NULL, xn->u.user, K_USERINFO, &lk); if (r == -1) { log_trace(TRACE_EXPAND, "expand: lka_expand: " "backend error while searching user"); @@ -516,7 +516,7 @@ lka_submit(struct lka_session *lks, struct rule *rule, struct expandnode *xn) sizeof(ep->agent.mda.username)); } - r = table_lookup(rule->r_userbase, ep->agent.mda.username, + r = table_lookup(rule->r_userbase, NULL, ep->agent.mda.username, K_USERINFO, &lk); if (r <= 0) { lks->error = (r == -1) ? LKA_TEMPFAIL : LKA_PERMFAIL; diff --git a/usr.sbin/smtpd/makemap.c b/usr.sbin/smtpd/makemap.c index ad456565afb..250bd89ea44 100644 --- a/usr.sbin/smtpd/makemap.c +++ b/usr.sbin/smtpd/makemap.c @@ -1,4 +1,4 @@ -/* $OpenBSD: makemap.c,v 1.48 2013/12/26 17:25:32 eric Exp $ */ +/* $OpenBSD: makemap.c,v 1.49 2014/07/08 13:49:09 eric Exp $ */ /* * Copyright (c) 2008 Gilles Chehade <gilles@poolp.org> @@ -81,6 +81,12 @@ purge_config(uint8_t what) } int +fork_proc_backend(const char *backend, const char *conf, const char *procname) +{ + return (-1); +} + +int main(int argc, char *argv[]) { struct stat sb; diff --git a/usr.sbin/smtpd/mproc.c b/usr.sbin/smtpd/mproc.c index 50e484fd9c6..c36775a6539 100644 --- a/usr.sbin/smtpd/mproc.c +++ b/usr.sbin/smtpd/mproc.c @@ -1,4 +1,4 @@ -/* $OpenBSD: mproc.c,v 1.9 2014/04/29 19:13:13 reyk Exp $ */ +/* $OpenBSD: mproc.c,v 1.10 2014/07/08 13:49:09 eric Exp $ */ /* * Copyright (c) 2012 Eric Faurot <eric@faurot.net> @@ -43,7 +43,7 @@ static void mproc_dispatch(int, short, void *); static ssize_t msgbuf_write2(struct msgbuf *); int -mproc_fork(struct mproc *p, const char *path, const char *arg) +mproc_fork(struct mproc *p, const char *path, char *argv[]) { int sp[2]; @@ -62,8 +62,8 @@ mproc_fork(struct mproc *p, const char *path, const char *arg) if (closefrom(STDERR_FILENO + 1) < 0) exit(1); - execl(path, arg, NULL); - err(1, "execl"); + execv(path, argv); + err(1, "execv: %s", path); } /* parent process */ @@ -72,7 +72,7 @@ mproc_fork(struct mproc *p, const char *path, const char *arg) return (0); err: - log_warn("warn: Failed to start process %s, instance of %s", arg, path); + log_warn("warn: Failed to start process %s, instance of %s", argv[0], path); close(sp[0]); close(sp[1]); return (-1); @@ -446,7 +446,7 @@ m_error(const char *error) proc_name(smtpd_process), imsg_to_str(current->hdr.type), error); - fatalx(buf); + fatalx("%s", buf); } void @@ -618,6 +618,25 @@ m_add_envelope(struct mproc *m, const struct envelope *evp) #endif void +m_add_params(struct mproc *m, struct dict *d) +{ + const char *key; + char *value; + void *iter; + + if (d == NULL) { + m_add_size(m, 0); + return; + } + m_add_size(m, dict_count(d)); + iter = NULL; + while (dict_iter(d, &iter, &key, (void **)&value)) { + m_add_string(m, key); + m_add_string(m, value); + } +} + +void m_get_int(struct msg *m, int *i) { m_get_typed(m, M_INT, i, sizeof(*i)); @@ -719,3 +738,33 @@ m_get_envelope(struct msg *m, struct envelope *evp) #endif } #endif + +void +m_get_params(struct msg *m, struct dict *d) +{ + size_t c; + const char *key; + const char *value; + char *tmp; + + dict_init(d); + + m_get_size(m, &c); + + for (; c; c--) { + m_get_string(m, &key); + m_get_string(m, &value); + if ((tmp = strdup(value)) == NULL) + fatal("m_get_params"); + dict_set(d, key, tmp); + } +} + +void +m_clear_params(struct dict *d) +{ + char *value; + + while (dict_poproot(d, (void **)&value)) + free(value); +} diff --git a/usr.sbin/smtpd/ruleset.c b/usr.sbin/smtpd/ruleset.c index 9e8afbde20d..902f3c9da16 100644 --- a/usr.sbin/smtpd/ruleset.c +++ b/usr.sbin/smtpd/ruleset.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ruleset.c,v 1.29 2013/11/06 10:01:29 eric Exp $ */ +/* $OpenBSD: ruleset.c,v 1.30 2014/07/08 13:49:09 eric Exp $ */ /* * Copyright (c) 2009 Gilles Chehade <gilles@poolp.org> @@ -84,7 +84,7 @@ ruleset_match(const struct envelope *evp) } ret = r->r_destination == NULL ? 1 : - table_lookup(r->r_destination, maddr->domain, K_DOMAIN, + table_lookup(r->r_destination, NULL, maddr->domain, K_DOMAIN, NULL); if (ret == -1) { errno = EAGAIN; @@ -122,7 +122,7 @@ ruleset_check_source(struct table *table, const struct sockaddr_storage *ss, key = "local"; else key = ss_to_text(ss); - switch (table_lookup(table, key, K_NETADDR, NULL)) { + switch (table_lookup(table, NULL, key, K_NETADDR, NULL)) { case 1: return 1; case -1: @@ -145,7 +145,7 @@ ruleset_check_mailaddr(struct table *table, const struct mailaddr *maddr) if (key == NULL) return -1; - switch (table_lookup(table, key, K_MAILADDR, NULL)) { + switch (table_lookup(table, NULL, key, K_MAILADDR, NULL)) { case 1: return 1; case -1: diff --git a/usr.sbin/smtpd/smtpd-api.h b/usr.sbin/smtpd/smtpd-api.h index 0570700cca8..83f96918c9a 100644 --- a/usr.sbin/smtpd/smtpd-api.h +++ b/usr.sbin/smtpd/smtpd-api.h @@ -1,4 +1,4 @@ -/* $OpenBSD: smtpd-api.h,v 1.16 2014/02/04 15:44:06 eric Exp $ */ +/* $OpenBSD: smtpd-api.h,v 1.17 2014/07/08 13:49:09 eric Exp $ */ /* * Copyright (c) 2013 Eric Faurot <eric@openbsd.org> @@ -378,9 +378,9 @@ int scheduler_api_dispatch(void); /* table */ void table_api_on_update(int(*)(void)); -void table_api_on_check(int(*)(int, const char *)); -void table_api_on_lookup(int(*)(int, const char *, char *, size_t)); -void table_api_on_fetch(int(*)(int, char *, size_t)); +void table_api_on_check(int(*)(int, struct dict *, const char *)); +void table_api_on_lookup(int(*)(int, struct dict *, const char *, char *, size_t)); +void table_api_on_fetch(int(*)(int, struct dict *, char *, size_t)); int table_api_dispatch(void); const char *table_api_get_name(void); diff --git a/usr.sbin/smtpd/smtpd.c b/usr.sbin/smtpd/smtpd.c index b7c1d6cf263..e1a2e7c5658 100644 --- a/usr.sbin/smtpd/smtpd.c +++ b/usr.sbin/smtpd/smtpd.c @@ -1,4 +1,4 @@ -/* $OpenBSD: smtpd.c,v 1.227 2014/07/07 09:11:24 eric Exp $ */ +/* $OpenBSD: smtpd.c,v 1.228 2014/07/08 13:49:09 eric Exp $ */ /* * Copyright (c) 2008 Gilles Chehade <gilles@poolp.org> @@ -765,6 +765,61 @@ post_fork(int proc) } } +int +fork_proc_backend(const char *key, const char *conf, const char *procname) +{ + pid_t pid; + int sp[2]; + char path[SMTPD_MAXPATHLEN]; + char name[SMTPD_MAXPATHLEN]; + char *arg; + + if (strlcpy(name, conf, sizeof(name)) >= sizeof(name)) { + log_warnx("warn: %s-proc: conf too long", key); + return (0); + } + + arg = strchr(name, ':'); + if (arg) + *arg++ = '\0'; + + if (snprintf(path, sizeof(path), PATH_LIBEXEC "/%s-%s", key, name) >= + (ssize_t)sizeof(path)) { + log_warn("warn: %s-proc: exec path too long", key); + return (-1); + } + + if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, sp) == -1) { + log_warn("warn: %s-proc: socketpair", key); + return (-1); + } + + if ((pid = fork()) == -1) { + log_warn("warn: %s-proc: fork", key); + close(sp[0]); + close(sp[1]); + return (-1); + } + + if (pid == 0) { + /* child process */ + dup2(sp[0], STDIN_FILENO); + if (closefrom(STDERR_FILENO + 1) < 0) + exit(1); + + if (procname == NULL) + procname = name; + + execl(path, procname, arg, NULL); + err(1, "execl: %s", path); + } + + /* parent process */ + close(sp[0]); + + return (sp[1]); +} + struct child * child_add(pid_t pid, int type, const char *title) { diff --git a/usr.sbin/smtpd/smtpd.h b/usr.sbin/smtpd/smtpd.h index 7023a9fe6b1..d39eb0e629e 100644 --- a/usr.sbin/smtpd/smtpd.h +++ b/usr.sbin/smtpd/smtpd.h @@ -1,4 +1,4 @@ -/* $OpenBSD: smtpd.h,v 1.461 2014/05/04 16:38:19 reyk Exp $ */ +/* $OpenBSD: smtpd.h,v 1.462 2014/07/08 13:49:09 eric Exp $ */ /* * Copyright (c) 2008 Gilles Chehade <gilles@poolp.org> @@ -55,8 +55,7 @@ #define PATH_PURGE "/purge" #define PATH_TEMPORARY "/temporary" -#define PATH_FILTERS "/usr/libexec/smtpd" -#define PATH_TABLES "/usr/libexec/smtpd" +#define PATH_LIBEXEC "/usr/libexec/smtpd" /* @@ -332,8 +331,8 @@ struct table_backend { void *(*open)(struct table *); int (*update)(struct table *); void (*close)(void *); - int (*lookup)(void *, const char *, enum table_service, union lookup *); - int (*fetch)(void *, enum table_service, union lookup *); + int (*lookup)(void *, struct dict *, const char *, enum table_service, union lookup *); + int (*fetch)(void *, struct dict *, enum table_service, union lookup *); }; @@ -1200,7 +1199,7 @@ void mfa_build_fd_chain(uint64_t, int); /* mproc.c */ -int mproc_fork(struct mproc *, const char*, const char *); +int mproc_fork(struct mproc *, const char*, char **); void mproc_init(struct mproc *, int); void mproc_clear(struct mproc *); void mproc_enable(struct mproc *); @@ -1224,6 +1223,7 @@ void m_add_id(struct mproc *, uint64_t); void m_add_sockaddr(struct mproc *, const struct sockaddr *); void m_add_mailaddr(struct mproc *, const struct mailaddr *); void m_add_envelope(struct mproc *, const struct envelope *); +void m_add_params(struct mproc *, struct dict *); void m_close(struct mproc *); void m_flush(struct mproc *); @@ -1242,6 +1242,8 @@ void m_get_id(struct msg *, uint64_t *); void m_get_sockaddr(struct msg *, struct sockaddr *); void m_get_mailaddr(struct msg *, struct mailaddr *); void m_get_envelope(struct msg *, struct envelope *); +void m_get_params(struct msg *, struct dict *); +void m_clear_params(struct dict *); /* mta.c */ @@ -1331,6 +1333,7 @@ const char *proc_name(enum smtp_proc_type); const char *proc_title(enum smtp_proc_type); const char *imsg_to_str(int); void log_imsg(int, int, struct imsg *); +int fork_proc_backend(const char *, const char *, const char *); /* ssl_smtpd.c */ @@ -1360,19 +1363,17 @@ 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_lookup(struct table *, const char *, enum table_service, +int table_lookup(struct table *, struct dict *, const char *, enum table_service, union lookup *); -int table_fetch(struct table *, enum table_service, union lookup *); +int table_fetch(struct table *, struct dict *, enum table_service, union lookup *); void table_destroy(struct table *); void table_add(struct table *, const char *, const char *); -void table_delete(struct table *, const char *); int table_domain_match(const char *, const char *); int table_netaddr_match(const char *, const char *); int table_mailaddr_match(const char *, const char *); void table_open_all(void); void table_dump_all(void); void table_close_all(void); -const void *table_get(struct table *, const char *); int table_parse_lookup(enum table_service, const char *, const char *, union lookup *); diff --git a/usr.sbin/smtpd/table-ldap/Makefile b/usr.sbin/smtpd/table-ldap/Makefile index f974b8207e8..1bfabbe5514 100644 --- a/usr.sbin/smtpd/table-ldap/Makefile +++ b/usr.sbin/smtpd/table-ldap/Makefile @@ -1,4 +1,4 @@ -# $OpenBSD: Makefile,v 1.3 2014/04/01 23:14:25 fgsch Exp $ +# $OpenBSD: Makefile,v 1.4 2014/07/08 13:49:09 eric Exp $ .PATH: ${.CURDIR}/.. @@ -8,6 +8,7 @@ SRCS= table_ldap.c SRCS+= table_api.c SRCS+= aldap.c SRCS+= ber.c +SRCS+= dict.c SRCS+= log.c NOMAN= noman diff --git a/usr.sbin/smtpd/table.c b/usr.sbin/smtpd/table.c index 47dc00626b3..f4717bfc7c2 100644 --- a/usr.sbin/smtpd/table.c +++ b/usr.sbin/smtpd/table.c @@ -1,4 +1,4 @@ -/* $OpenBSD: table.c,v 1.16 2014/05/09 21:30:11 tedu Exp $ */ +/* $OpenBSD: table.c,v 1.17 2014/07/08 13:49:09 eric Exp $ */ /* * Copyright (c) 2013 Eric Faurot <eric@openbsd.org> @@ -116,7 +116,7 @@ table_find(const char *name, const char *tag) } int -table_lookup(struct table *table, const char *key, enum table_service kind, +table_lookup(struct table *table, struct dict *params, const char *key, enum table_service kind, union lookup *lk) { int r; @@ -130,7 +130,7 @@ table_lookup(struct table *table, const char *key, enum table_service kind, return -1; } - r = table->t_backend->lookup(table->t_handle, lkey, kind, lk); + r = table->t_backend->lookup(table->t_handle, params, lkey, kind, lk); if (r == 1) log_trace(TRACE_LOOKUP, "lookup: %s \"%s\" as %s in table %s:%s -> %s%s%s", @@ -155,14 +155,14 @@ table_lookup(struct table *table, const char *key, enum table_service kind, } int -table_fetch(struct table *table, enum table_service kind, union lookup *lk) +table_fetch(struct table *table, struct dict *params, enum table_service kind, union lookup *lk) { int r; if (table->t_backend->fetch == NULL) return (-1); - r = table->t_backend->fetch(table->t_handle, kind, lk); + r = table->t_backend->fetch(table->t_handle, params, kind, lk); if (r == 1) log_trace(TRACE_LOOKUP, "lookup: fetch %s from table %s:%s -> %s%s%s", @@ -196,34 +196,35 @@ table_create(const char *backend, const char *name, const char *tag, if (name && tag) { if ((size_t)snprintf(buf, sizeof(buf), "%s#%s", name, tag) >= sizeof(buf)) - errx(1, "table_create: name too long \"%s#%s\"", + fatalx("table_create: name too long \"%s#%s\"", name, tag); name = buf; } if (name && table_find(name, NULL)) - errx(1, "table_create: table \"%s\" already defined", name); + fatalx("table_create: table \"%s\" already defined", name); if ((tb = table_backend_lookup(backend)) == NULL) { - if ((size_t)snprintf(path, sizeof(path), PATH_TABLES "/table-%s", + if ((size_t)snprintf(path, sizeof(path), PATH_LIBEXEC "/table-%s", backend) >= sizeof(path)) { - errx(1, "table_create: path too long \"" - PATH_TABLES "/table-%s\"", backend); + fatalx("table_create: path too long \"" + PATH_LIBEXEC "/table-%s\"", backend); } if (stat(path, &sb) == 0) { tb = table_backend_lookup("proc"); + (void)strlcpy(path, backend, sizeof(path)); if (config) { - (void)strlcat(path, " ", sizeof(path)); + (void)strlcat(path, ":", sizeof(path)); if (strlcat(path, config, sizeof(path)) >= sizeof(path)) - errx(1, "table_create: config file path too long"); + fatalx("table_create: config file path too long"); } config = path; } } if (tb == NULL) - errx(1, "table_create: backend \"%s\" does not exist", backend); + fatalx("table_create: backend \"%s\" does not exist", backend); t = xcalloc(1, sizeof(*t), "table_create"); t->t_backend = tb; @@ -238,7 +239,7 @@ table_create(const char *backend, const char *name, const char *tag, if (config) { if (strlcpy(t->t_config, config, sizeof t->t_config) >= sizeof t->t_config) - errx(1, "table_create: table config \"%s\" too large", + fatalx("table_create: table config \"%s\" too large", t->t_config); } @@ -251,7 +252,7 @@ table_create(const char *backend, const char *name, const char *tag, else { n = strlcpy(t->t_name, name, sizeof(t->t_name)); if (n >= sizeof(t->t_name)) - errx(1, "table_create: table name too long"); + fatalx("table_create: table name too long"); } dict_init(&t->t_dict); @@ -286,7 +287,7 @@ table_add(struct table *t, const char *key, const char *val) char lkey[1024], *old; if (t->t_type & T_DYNAMIC) - errx(1, "table_add: cannot add to table"); + fatalx("table_add: cannot add to table"); if (! lowercase(lkey, key, sizeof lkey)) { log_warnx("warn: lookup key too long: %s", key); @@ -301,22 +302,6 @@ table_add(struct table *t, const char *key, const char *val) } } -const void * -table_get(struct table *t, const char *key) -{ - if (t->t_type & T_DYNAMIC) - errx(1, "table_get: cannot get from table"); - return dict_get(&t->t_dict, key); -} - -void -table_delete(struct table *t, const char *key) -{ - if (t->t_type & T_DYNAMIC) - errx(1, "table_delete: cannot delete from table"); - free(dict_pop(&t->t_dict, key)); -} - int table_check_type(struct table *t, uint32_t mask) { @@ -518,7 +503,7 @@ table_open_all(void) iter = NULL; while (dict_iter(env->sc_tables_dict, &iter, NULL, (void **)&t)) if (! table_open(t)) - errx(1, "failed to open table %s", t->t_name); + fatalx("failed to open table %s", t->t_name); } void diff --git a/usr.sbin/smtpd/table_api.c b/usr.sbin/smtpd/table_api.c index 501053ce2d0..c5c42393da2 100644 --- a/usr.sbin/smtpd/table_api.c +++ b/usr.sbin/smtpd/table_api.c @@ -1,4 +1,4 @@ -/* $OpenBSD: table_api.c,v 1.4 2014/02/04 13:55:34 eric Exp $ */ +/* $OpenBSD: table_api.c,v 1.5 2014/07/08 13:49:09 eric Exp $ */ /* * Copyright (c) 2013 Eric Faurot <eric@openbsd.org> @@ -34,9 +34,9 @@ #include "log.h" static int (*handler_update)(void); -static int (*handler_check)(int, const char *); -static int (*handler_lookup)(int, const char *, char *, size_t); -static int (*handler_fetch)(int, char *, size_t); +static int (*handler_check)(int, struct dict *, const char *); +static int (*handler_lookup)(int, struct dict *, const char *, char *, size_t); +static int (*handler_fetch)(int, struct dict *, char *, size_t); static int quit; static struct imsgbuf ibuf; @@ -101,10 +101,40 @@ table_msg_close(void) buf = NULL; } +static int +table_read_params(struct dict *params) +{ + size_t count; + char *key; + char *value; + + dict_init(params); + + table_msg_get(&count, sizeof(count)); + + for (;count; count--) { + key = rdata; + table_msg_get(NULL, strlen(key) + 1); + value = rdata; + table_msg_get(NULL, strlen(value) + 1); + dict_set(params, key, value); + } + + return (0); +} + +static void +table_clear_params(struct dict *params) +{ + while (dict_poproot(params, NULL)) + ; +} + static void table_msg_dispatch(void) { struct table_open_params op; + struct dict params; char res[4096]; int type, r; @@ -142,6 +172,7 @@ table_msg_dispatch(void) case PROC_TABLE_CHECK: table_msg_get(&type, sizeof(type)); + table_read_params(¶ms); if (rlen == 0) { log_warnx("warn: table-api: no key"); fatalx("table-api: exiting"); @@ -152,9 +183,10 @@ table_msg_dispatch(void) } if (handler_check) - r = handler_check(type, rdata); + r = handler_check(type, ¶ms, rdata); else r = -1; + table_clear_params(¶ms); table_msg_get(NULL, rlen); table_msg_end(); @@ -164,7 +196,7 @@ table_msg_dispatch(void) case PROC_TABLE_LOOKUP: table_msg_get(&type, sizeof(type)); - + table_read_params(¶ms); if (rlen == 0) { log_warnx("warn: table-api: no key"); fatalx("table-api: exiting"); @@ -175,10 +207,10 @@ table_msg_dispatch(void) } if (handler_lookup) - r = handler_lookup(type, rdata, res, sizeof(res)); + r = handler_lookup(type, ¶ms, rdata, res, sizeof(res)); else r = -1; - + table_clear_params(¶ms); table_msg_get(NULL, rlen); table_msg_end(); @@ -191,12 +223,13 @@ table_msg_dispatch(void) case PROC_TABLE_FETCH: table_msg_get(&type, sizeof(type)); - table_msg_end(); - + table_read_params(¶ms); if (handler_fetch) - r = handler_fetch(type, res, sizeof(res)); + r = handler_fetch(type, ¶ms, res, sizeof(res)); else r = -1; + table_clear_params(¶ms); + table_msg_end(); table_msg_add(&r, sizeof(r)); if (r == 1) @@ -217,19 +250,19 @@ table_api_on_update(int(*cb)(void)) } void -table_api_on_check(int(*cb)(int, const char *)) +table_api_on_check(int(*cb)(int, struct dict *, const char *)) { handler_check = cb; } void -table_api_on_lookup(int(*cb)(int, const char *, char *, size_t)) +table_api_on_lookup(int(*cb)(int, struct dict *, const char *, char *, size_t)) { handler_lookup = cb; } void -table_api_on_fetch(int(*cb)(int, char *, size_t)) +table_api_on_fetch(int(*cb)(int, struct dict *, char *, size_t)) { handler_fetch = cb; } diff --git a/usr.sbin/smtpd/table_db.c b/usr.sbin/smtpd/table_db.c index 9b442bbdc77..a0dc5b326e1 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.6 2014/04/18 06:59:15 gilles Exp $ */ +/* $OpenBSD: table_db.c,v 1.7 2014/07/08 13:49:09 eric Exp $ */ /* * Copyright (c) 2011 Gilles Chehade <gilles@poolp.org> @@ -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 *, const char *, enum table_service, union lookup *); -static int table_db_fetch(void *, enum table_service, union lookup *); +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 void table_db_close(void *); static char *table_db_get_entry(void *, const char *, size_t *); @@ -142,7 +142,7 @@ table_db_close(void *hdl) } static int -table_db_lookup(void *hdl, const char *key, enum table_service service, +table_db_lookup(void *hdl, struct dict *params, const char *key, enum table_service service, union lookup *lk) { struct dbhandle *handle = hdl; @@ -184,7 +184,7 @@ table_db_lookup(void *hdl, const char *key, enum table_service service, } static int -table_db_fetch(void *hdl, enum table_service service, union lookup *lk) +table_db_fetch(void *hdl, struct dict *params, enum table_service service, union lookup *lk) { struct dbhandle *handle = hdl; struct table *table = handle->table; diff --git a/usr.sbin/smtpd/table_getpwnam.c b/usr.sbin/smtpd/table_getpwnam.c index f66effbf414..52e79c0d772 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.2 2013/05/24 17:03:14 eric Exp $ */ +/* $OpenBSD: table_getpwnam.c,v 1.3 2014/07/08 13:49:09 eric Exp $ */ /* * Copyright (c) 2012 Gilles Chehade <gilles@poolp.org> @@ -40,7 +40,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 *, const char *, enum table_service, +static int table_getpwnam_lookup(void *, struct dict *, const char *, enum table_service, union lookup *); static void table_getpwnam_close(void *); @@ -81,7 +81,7 @@ table_getpwnam_close(void *hdl) } static int -table_getpwnam_lookup(void *hdl, const char *key, enum table_service kind, +table_getpwnam_lookup(void *hdl, struct dict *params, const char *key, enum table_service kind, union lookup *lk) { struct passwd *pw; diff --git a/usr.sbin/smtpd/table_ldap.c b/usr.sbin/smtpd/table_ldap.c index d0681f9e2d5..a0b3a4e6377 100644 --- a/usr.sbin/smtpd/table_ldap.c +++ b/usr.sbin/smtpd/table_ldap.c @@ -1,4 +1,4 @@ -/* $OpenBSD: table_ldap.c,v 1.10 2014/06/10 16:43:46 gilles Exp $ */ +/* $OpenBSD: table_ldap.c,v 1.11 2014/07/08 13:49:09 eric Exp $ */ /* * Copyright (c) 2013 Eric Faurot <eric@openbsd.org> @@ -59,9 +59,9 @@ struct query { }; static int table_ldap_update(void); -static int table_ldap_check(int, const char *); -static int table_ldap_lookup(int, const char *, char *, size_t); -static int table_ldap_fetch(int, char *, size_t); +static int table_ldap_check(int, struct dict *, const char *); +static int table_ldap_lookup(int, struct dict *, const char *, char *, size_t); +static int table_ldap_fetch(int, struct dict *, char *, size_t); static int ldap_config(void); static int ldap_open(void); @@ -135,7 +135,7 @@ table_ldap_update(void) } static int -table_ldap_check(int service, const char *key) +table_ldap_check(int service, struct dict *params, const char *key) { switch(service) { case K_ALIAS: @@ -149,7 +149,7 @@ table_ldap_check(int service, const char *key) } static int -table_ldap_lookup(int service, const char *key, char *dst, size_t sz) +table_ldap_lookup(int service, struct dict *params, const char *key, char *dst, size_t sz) { switch(service) { case K_ALIAS: @@ -163,7 +163,7 @@ table_ldap_lookup(int service, const char *key, char *dst, size_t sz) } static int -table_ldap_fetch(int service, char *dst, size_t sz) +table_ldap_fetch(int service, struct dict *params, char *dst, size_t sz) { return (-1); } diff --git a/usr.sbin/smtpd/table_passwd.c b/usr.sbin/smtpd/table_passwd.c index 203fad644f5..85b2fe9fa7e 100644 --- a/usr.sbin/smtpd/table_passwd.c +++ b/usr.sbin/smtpd/table_passwd.c @@ -1,4 +1,4 @@ -/* $OpenBSD: table_passwd.c,v 1.6 2014/07/01 17:33:51 matthieu Exp $ */ +/* $OpenBSD: table_passwd.c,v 1.7 2014/07/08 13:49:09 eric Exp $ */ /* * Copyright (c) 2013 Gilles Chehade <gilles@poolp.org> @@ -30,9 +30,9 @@ #include "log.h" static int table_passwd_update(void); -static int table_passwd_check(int, const char *); -static int table_passwd_lookup(int, const char *, char *, size_t); -static int table_passwd_fetch(int, char *, size_t); +static int table_passwd_check(int, struct dict *, const char *); +static int table_passwd_lookup(int, struct dict *, const char *, char *, size_t); +static int table_passwd_fetch(int, struct dict *, char *, size_t); static int parse_passwd_entry(struct passwd *, const char *); static char *config; @@ -143,13 +143,13 @@ err: } static int -table_passwd_check(int service, const char *key) +table_passwd_check(int service, struct dict *params, const char *key) { return (-1); } static int -table_passwd_lookup(int service, const char *key, char *dst, size_t sz) +table_passwd_lookup(int service, struct dict *params, const char *key, char *dst, size_t sz) { int r; struct passwd pw; @@ -191,7 +191,7 @@ table_passwd_lookup(int service, const char *key, char *dst, size_t sz) } static int -table_passwd_fetch(int service, char *dst, size_t sz) +table_passwd_fetch(int service, struct dict *params, char *dst, size_t sz) { return (-1); } diff --git a/usr.sbin/smtpd/table_proc.c b/usr.sbin/smtpd/table_proc.c index d02f76433c5..c968ccedc29 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.2 2014/02/04 13:55:34 eric Exp $ */ +/* $OpenBSD: table_proc.c,v 1.3 2014/07/08 13:49:09 eric Exp $ */ /* * Copyright (c) 2013 Eric Faurot <eric@openbsd.org> @@ -119,41 +119,17 @@ table_proc_end(void) static void * table_proc_open(struct table *table) { - int sp[2]; struct table_proc_priv *priv; - char *environ_new[2]; struct table_open_params op; + int fd; - errno = 0; - - if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, sp) < 0) { - log_warn("warn: table-proc: socketpair"); - return (NULL); - } priv = xcalloc(1, sizeof(*priv), "table_proc_open"); - if ((priv->pid = fork()) == -1) { - log_warn("warn: table-proc: fork"); - goto err; - } - - if (priv->pid == 0) { - /* child process */ - dup2(sp[0], STDIN_FILENO); - if (closefrom(STDERR_FILENO + 1) < 0) - exit(1); - - environ_new[0] = "PATH=" _PATH_DEFPATH; - environ_new[1] = (char *)NULL; - environ = environ_new; - execle("/bin/sh", "/bin/sh", "-c", table->t_config, (char *)NULL, - environ_new); - fatal("execl"); - } + fd = fork_proc_backend("table", table->t_config, table->t_name); + if (fd == -1) + fatalx("table-proc: exiting"); - /* parent process */ - close(sp[0]); - imsg_init(&priv->ibuf, sp[1]); + imsg_init(&priv->ibuf, fd); memset(&op, 0, sizeof op); op.version = PROC_TABLE_API_VERSION; @@ -164,11 +140,6 @@ table_proc_open(struct table *table) table_proc_end(); return (priv); -err: - free(priv); - close(sp[0]); - close(sp[1]); - return (NULL); } static int @@ -196,7 +167,36 @@ table_proc_close(void *arg) } static int -table_proc_lookup(void *arg, const char *k, enum table_service s, +imsg_add_params(struct ibuf *buf, struct dict *params) +{ + size_t count; + const char *key; + char *value; + void *iter; + + count = 0; + if (params) + count = dict_count(params); + + if (imsg_add(buf, &count, sizeof(count)) == -1) + return (-1); + + if (count == 0) + return (0); + + iter = NULL; + while (dict_iter(params, &iter, &key, (void **)&value)) { + if (imsg_add(buf, key, strlen(key) + 1) == -1) + return (-1); + if (imsg_add(buf, value, strlen(value) + 1) == -1) + return (-1); + } + + return (0); +} + +static int +table_proc_lookup(void *arg, struct dict *params, const char *k, enum table_service s, union lookup *lk) { struct table_proc_priv *priv = arg; @@ -211,6 +211,8 @@ table_proc_lookup(void *arg, const char *k, enum table_service s, return (-1); if (imsg_add(buf, &s, sizeof(s)) == -1) return (-1); + if (imsg_add_params(buf, params) == -1) + return (-1); if (imsg_add(buf, k, strlen(k) + 1) == -1) return (-1); imsg_close(&priv->ibuf, buf); @@ -237,7 +239,7 @@ table_proc_lookup(void *arg, const char *k, enum table_service s, } static int -table_proc_fetch(void *arg, enum table_service s, union lookup *lk) +table_proc_fetch(void *arg, struct dict *params, enum table_service s, union lookup *lk) { struct table_proc_priv *priv = arg; struct ibuf *buf; @@ -248,6 +250,8 @@ table_proc_fetch(void *arg, enum table_service s, union lookup *lk) return (-1); if (imsg_add(buf, &s, sizeof(s)) == -1) return (-1); + if (imsg_add_params(buf, params) == -1) + return (-1); imsg_close(&priv->ibuf, buf); table_proc_call(priv); diff --git a/usr.sbin/smtpd/table_socketmap.c b/usr.sbin/smtpd/table_socketmap.c index ea49af80b56..437470f7021 100644 --- a/usr.sbin/smtpd/table_socketmap.c +++ b/usr.sbin/smtpd/table_socketmap.c @@ -1,4 +1,4 @@ -/* $OpenBSD: table_socketmap.c,v 1.3 2014/04/19 14:21:26 gilles Exp $ */ +/* $OpenBSD: table_socketmap.c,v 1.4 2014/07/08 13:49:09 eric Exp $ */ /* * Copyright (c) 2014 Gilles Chehade <gilles@poolp.org> @@ -34,9 +34,9 @@ #include "log.h" static int table_socketmap_update(void); -static int table_socketmap_lookup(int, const char *, char *, size_t); -static int table_socketmap_check(int, const char *); -static int table_socketmap_fetch(int, char *, size_t); +static int table_socketmap_lookup(int, struct dict *, const char *, char *, size_t); +static int table_socketmap_check(int, struct dict *, const char *); +static int table_socketmap_fetch(int, struct dict *, char *, size_t); static int table_socketmap_connect(const char *); @@ -212,13 +212,13 @@ table_socketmap_update(void) } static int -table_socketmap_check(int service, const char *key) +table_socketmap_check(int service, struct dict *params, const char *key) { return (-1); } static int -table_socketmap_lookup(int service, const char *key, char *dst, size_t sz) +table_socketmap_lookup(int service, struct dict *params, const char *key, char *dst, size_t sz) { int r; enum socketmap_reply rep; @@ -255,7 +255,7 @@ table_socketmap_lookup(int service, const char *key, char *dst, size_t sz) } static int -table_socketmap_fetch(int service, char *key, size_t sz) +table_socketmap_fetch(int service, struct dict *params, char *key, size_t sz) { return (-1); } diff --git a/usr.sbin/smtpd/table_sqlite.c b/usr.sbin/smtpd/table_sqlite.c index 45015a14b58..9ce3a2b0bf2 100644 --- a/usr.sbin/smtpd/table_sqlite.c +++ b/usr.sbin/smtpd/table_sqlite.c @@ -1,4 +1,4 @@ -/* $OpenBSD: table_sqlite.c,v 1.13 2014/07/01 17:33:51 matthieu Exp $ */ +/* $OpenBSD: table_sqlite.c,v 1.14 2014/07/08 13:49:09 eric Exp $ */ /* * Copyright (c) 2013 Eric Faurot <eric@openbsd.org> @@ -45,9 +45,9 @@ enum { }; static int table_sqlite_update(void); -static int table_sqlite_lookup(int, const char *, char *, size_t); -static int table_sqlite_check(int, const char *); -static int table_sqlite_fetch(int, char *, size_t); +static int table_sqlite_lookup(int, struct dict *, const char *, char *, size_t); +static int table_sqlite_check(int, struct dict *, const char *); +static int table_sqlite_fetch(int, struct dict *, char *, size_t); static sqlite3_stmt *table_sqlite_query(const char *, int); @@ -133,7 +133,7 @@ table_sqlite_prepare_stmt(sqlite3 *_db, const char *query, int ncols) goto end; } if (sqlite3_column_count(stmt) != ncols) { - log_warnx("warn: table-sqlite: columns: invalid resultset"); + log_warnx("warn: table-sqlite: columns: invalid columns count for query: %s", query); goto end; } @@ -371,7 +371,7 @@ table_sqlite_query(const char *key, int service) } static int -table_sqlite_check(int service, const char *key) +table_sqlite_check(int service, struct dict *params, const char *key) { sqlite3_stmt *stmt; int r; @@ -393,7 +393,7 @@ table_sqlite_check(int service, const char *key) } static int -table_sqlite_lookup(int service, const char *key, char *dst, size_t sz) +table_sqlite_lookup(int service, struct dict *params, const char *key, char *dst, size_t sz) { sqlite3_stmt *stmt; const char *value; @@ -435,7 +435,6 @@ table_sqlite_lookup(int service, const char *key, char *dst, size_t sz) } s = sqlite3_step(stmt); } while (s == SQLITE_ROW); - if (s != SQLITE_ROW && s != SQLITE_DONE) { log_warnx("warn: table-sqlite: sqlite3_step: %s", sqlite3_errmsg(db)); @@ -479,7 +478,7 @@ table_sqlite_lookup(int service, const char *key, char *dst, size_t sz) } static int -table_sqlite_fetch(int service, char *dst, size_t sz) +table_sqlite_fetch(int service, struct dict *params, char *dst, size_t sz) { const char *k; int s; diff --git a/usr.sbin/smtpd/table_static.c b/usr.sbin/smtpd/table_static.c index a8d8411712c..e7eb5291959 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.8 2013/11/28 10:43:37 eric Exp $ */ +/* $OpenBSD: table_static.c,v 1.9 2014/07/08 13:49:09 eric Exp $ */ /* * Copyright (c) 2013 Eric Faurot <eric@openbsd.org> @@ -41,9 +41,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 *, const char *, enum table_service, +static int table_static_lookup(void *, struct dict *, const char *, enum table_service, union lookup *); -static int table_static_fetch(void *, enum table_service, union lookup *); +static int table_static_fetch(void *, struct dict *, enum table_service, union lookup *); static void table_static_close(void *); static int table_static_parse(struct table *, const char *, enum table_type); @@ -189,7 +189,7 @@ table_static_close(void *hdl) } static int -table_static_lookup(void *hdl, const char *key, enum table_service service, +table_static_lookup(void *hdl, struct dict *params, const char *key, enum table_service service, union lookup *lk) { struct table *m = hdl; @@ -235,7 +235,7 @@ table_static_lookup(void *hdl, const char *key, enum table_service service, } static int -table_static_fetch(void *hdl, enum table_service service, union lookup *lk) +table_static_fetch(void *hdl, struct dict *params, enum table_service service, union lookup *lk) { struct table *t = hdl; const char *k; |