From 2a281ec6ea5143138402d151a347def7f73f536c Mon Sep 17 00:00:00 2001 From: Glenn Strauss Date: Sun, 3 Nov 2019 01:02:14 -0400 Subject: [PATCH] [mod_vhostdb*] use config_plugin_values_init() --- src/mod_vhostdb.c | 142 +++++++++++++++++----------------- src/mod_vhostdb_dbi.c | 163 +++++++++++++++++++++++----------------- src/mod_vhostdb_ldap.c | 159 ++++++++++++++++++++++----------------- src/mod_vhostdb_mysql.c | 158 +++++++++++++++++++++----------------- src/mod_vhostdb_pgsql.c | 158 +++++++++++++++++++++----------------- 5 files changed, 433 insertions(+), 347 deletions(-) diff --git a/src/mod_vhostdb.c b/src/mod_vhostdb.c index 183f1436..c6fb30dc 100644 --- a/src/mod_vhostdb.c +++ b/src/mod_vhostdb.c @@ -15,112 +15,106 @@ */ typedef struct { - buffer *vhostdb_backend_conf; - - /* generated */ const http_vhostdb_backend_t *vhostdb_backend; } plugin_config; typedef struct { PLUGIN_DATA; - plugin_config **config_storage; + plugin_config defaults; plugin_config conf; - buffer *tmp_buf; + buffer tmp_buf; } plugin_data; INIT_FUNC(mod_vhostdb_init) { - plugin_data *p = calloc(1, sizeof(*p)); - p->tmp_buf = buffer_init(); - return p; + return calloc(1, sizeof(plugin_data)); } FREE_FUNC(mod_vhostdb_free) { plugin_data *p = p_d; if (!p) return HANDLER_GO_ON; - if (p->config_storage) { - size_t i; - for (i = 0; i < srv->config_context->used; i++) { - plugin_config *s = p->config_storage[i]; - if (NULL == s) continue; - buffer_free(s->vhostdb_backend_conf); - free(s); - } - free(p->config_storage); - } + free(p->tmp_buf.ptr); - free(p->tmp_buf); + free(p->cvlist); free(p); UNUSED(srv); return HANDLER_GO_ON; } -SETDEFAULTS_FUNC(mod_vhostdb_set_defaults) { - plugin_data *p = p_d; - config_values_t cv[] = { - { "vhostdb.backend", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 0 */ - - { NULL, NULL, T_CONFIG_UNSET, T_CONFIG_SCOPE_UNSET } - }; - - p->config_storage = calloc(srv->config_context->used, sizeof(plugin_config *)); - - for (size_t i = 0; i < srv->config_context->used; ++i) { - data_config const *config = (data_config const*)srv->config_context->data[i]; - plugin_config *s = calloc(1, sizeof(plugin_config)); - s->vhostdb_backend_conf = buffer_init(); - - cv[0].destination = s->vhostdb_backend_conf; - - p->config_storage[i] = s; - - if (0 != config_insert_values_global(srv, config->value, cv, i == 0 ? T_CONFIG_SCOPE_SERVER : T_CONFIG_SCOPE_CONNECTION)) { - return HANDLER_ERROR; - } - - if (!buffer_string_is_empty(s->vhostdb_backend_conf)) { - s->vhostdb_backend = - http_vhostdb_backend_get(s->vhostdb_backend_conf); - if (NULL == s->vhostdb_backend) { - log_error_write(srv, __FILE__, __LINE__, "sb", - "vhostdb.backend not supported:", - s->vhostdb_backend_conf); - return HANDLER_ERROR; - } - } +static void mod_vhostdb_merge_config_cpv(plugin_config * const pconf, const config_plugin_value_t * const cpv) { + switch (cpv->k_id) { /* index into static config_plugin_keys_t cpk[] */ + case 0: /* vhostdb.backend */ + if (cpv->vtype == T_CONFIG_LOCAL) + pconf->vhostdb_backend = cpv->v.v; + break; + default:/* should not happen */ + return; } - - return HANDLER_GO_ON; } -#define PATCH(x) \ - p->conf.x = s->x; -static int mod_vhostdb_patch_connection(server *srv, connection *con, plugin_data *p) { - plugin_config *s = p->config_storage[0]; - PATCH(vhostdb_backend); - - /* skip the first, the global context */ - for (size_t i = 1; i < srv->config_context->used; ++i) { - if (!config_check_cond(con, i)) continue; /* condition not matched */ +static void mod_vhostdb_merge_config(plugin_config * const pconf, const config_plugin_value_t *cpv) { + do { + mod_vhostdb_merge_config_cpv(pconf, cpv); + } while ((++cpv)->k_id != -1); +} - data_config *dc = (data_config *)srv->config_context->data[i]; - s = p->config_storage[i]; +static void mod_vhostdb_patch_config(connection * const con, plugin_data * const p) { + memcpy(&p->conf, &p->defaults, sizeof(plugin_config)); + for (int i = 1, used = p->nconfig; i < used; ++i) { + if (config_check_cond(con, (uint32_t)p->cvlist[i].k_id)) + mod_vhostdb_merge_config(&p->conf, p->cvlist + p->cvlist[i].v.u2[0]); + } +} - /* merge config */ - for (size_t j = 0; j < dc->value->used; ++j) { - data_unset *du = dc->value->data[j]; +SETDEFAULTS_FUNC(mod_vhostdb_set_defaults) { + static const config_plugin_keys_t cpk[] = { + { CONST_STR_LEN("vhostdb.backend"), + T_CONFIG_STRING, + T_CONFIG_SCOPE_CONNECTION } + ,{ NULL, 0, + T_CONFIG_UNSET, + T_CONFIG_SCOPE_UNSET } + }; - if (buffer_is_equal_string(&du->key, CONST_STR_LEN("vhostdb.backend"))) { - PATCH(vhostdb_backend); + plugin_data * const p = p_d; + if (!config_plugin_values_init(srv, p, cpk, "mod_vhostdb")) + return HANDLER_ERROR; + + /* process and validate config directives + * (init i to 0 if global context; to 1 to skip empty global context) */ + for (int i = !p->cvlist[0].v.u2[1]; i < p->nconfig; ++i) { + config_plugin_value_t *cpv = p->cvlist + p->cvlist[i].v.u2[0]; + for (; -1 != cpv->k_id; ++cpv) { + switch (cpv->k_id) { + case 0: /* vhostdb.backend */ + if (!buffer_string_is_empty(cpv->v.b)) { + const buffer * const b = cpv->v.b; + *(const void **)&cpv->v.v = http_vhostdb_backend_get(b); + if (NULL == cpv->v.v) { + log_error(srv->errh, __FILE__, __LINE__, + "vhostdb.backend not supported: %s", b->ptr); + return HANDLER_ERROR; + } + cpv->vtype = T_CONFIG_LOCAL; + } + break; + default:/* should not happen */ + break; } } } - return 0; + /* initialize p->defaults from global config context */ + if (p->nconfig > 0 && p->cvlist->v.u2[1]) { + const config_plugin_value_t *cpv = p->cvlist + p->cvlist->v.u2[0]; + if (-1 != cpv->k_id) + mod_vhostdb_merge_config(&p->defaults, cpv); + } + + return HANDLER_GO_ON; } -#undef PATCH typedef struct { buffer *server_name; @@ -190,10 +184,10 @@ CONNECTION_FUNC(mod_vhostdb_handle_docroot) { return mod_vhostdb_found(con, ve); /* HANDLER_GO_ON */ } - mod_vhostdb_patch_connection(srv, con, p); + mod_vhostdb_patch_config(con, p); if (!p->conf.vhostdb_backend) return HANDLER_GO_ON; - b = p->tmp_buf; + b = &p->tmp_buf; backend = p->conf.vhostdb_backend; if (0 != backend->query(srv, con, backend->p_d, b)) { return mod_vhostdb_error_500(con); /* HANDLER_FINISHED */ diff --git a/src/mod_vhostdb_dbi.c b/src/mod_vhostdb_dbi.c index 6b751a9c..5ca19ffb 100644 --- a/src/mod_vhostdb_dbi.c +++ b/src/mod_vhostdb_dbi.c @@ -31,12 +31,11 @@ typedef struct { typedef struct { void *vdata; - array *options; } plugin_config; typedef struct { PLUGIN_DATA; - plugin_config **config_storage; + plugin_config defaults; plugin_config conf; } plugin_data; @@ -68,7 +67,7 @@ static void mod_vhostdb_dbconf_free (void *vdata) free(dbconf); } -static int mod_vhostdb_dbconf_setup (server *srv, array *opts, void **vdata) +static int mod_vhostdb_dbconf_setup (server *srv, const array *opts, void **vdata) { const buffer *sqlquery = NULL; const buffer *dbtype=NULL, *dbname=NULL; @@ -151,13 +150,16 @@ static int mod_vhostdb_dbconf_setup (server *srv, array *opts, void **vdata) /* connect to database */ mod_vhostdb_dbi_error_callback(dbconn, dbconf); - if (dbconf->reconnect_count >= 3) return -1; + if (dbconf->reconnect_count >= 3) { + mod_vhostdb_dbconf_free(dbconf); + return -1; + } } return 0; } -static void mod_vhostdb_patch_connection (server *srv, connection *con, plugin_data *p); +static void mod_vhostdb_patch_config (connection * const con, plugin_data * const p); static int mod_vhostdb_dbi_query(server *srv, connection *con, void *p_d, buffer *docroot) { @@ -171,7 +173,7 @@ static int mod_vhostdb_dbi_query(server *srv, connection *con, void *p_d, buffer buffer *sqlquery = docroot; buffer_clear(sqlquery); /*(also resets docroot (alias))*/ - mod_vhostdb_patch_connection(srv, con, p); + mod_vhostdb_patch_config(con, p); if (NULL == p->conf.vdata) return 0; /*(after resetting docroot)*/ dbconf = (vhostdb_config *)p->conf.vdata; @@ -231,91 +233,114 @@ INIT_FUNC(mod_vhostdb_init) { return p; } +static void mod_vhostdb_free_config(plugin_data * const p) { + if (NULL == p->cvlist) return; + /* (init i to 0 if global context; to 1 to skip empty global context) */ + for (int i = !p->cvlist[0].v.u2[1], used = p->nconfig; i < used; ++i) { + config_plugin_value_t *cpv = p->cvlist + p->cvlist[i].v.u2[0]; + for (; -1 != cpv->k_id; ++cpv) { + if (cpv->vtype != T_CONFIG_LOCAL || NULL == cpv->v.v) continue; + switch (cpv->k_id) { + case 0: /* vhostdb. */ + mod_vhostdb_dbconf_free(cpv->v.v); + break; + default: + break; + } + } + } +} + FREE_FUNC(mod_vhostdb_cleanup) { plugin_data *p = p_d; if (!p) return HANDLER_GO_ON; - if (p->config_storage) { - for (size_t i = 0; i < srv->config_context->used; i++) { - plugin_config *s = p->config_storage[i]; - if (!s) continue; - mod_vhostdb_dbconf_free(s->vdata); - array_free(s->options); - free(s); - } - free(p->config_storage); - } + mod_vhostdb_free_config(p); + + free(p->cvlist); free(p); UNUSED(srv); return HANDLER_GO_ON; } -SETDEFAULTS_FUNC(mod_vhostdb_set_defaults) { - plugin_data *p = p_d; - - config_values_t cv[] = { - { "vhostdb.dbi", NULL, T_CONFIG_ARRAY, T_CONFIG_SCOPE_CONNECTION }, - { NULL, NULL, T_CONFIG_UNSET, T_CONFIG_SCOPE_UNSET } - }; - - p->config_storage = calloc(srv->config_context->used, sizeof(plugin_config *)); +static void mod_vhostdb_merge_config_cpv(plugin_config * const pconf, const config_plugin_value_t * const cpv) { + switch (cpv->k_id) { /* index into static config_plugin_keys_t cpk[] */ + case 0: /* vhostdb. */ + if (cpv->vtype == T_CONFIG_LOCAL) + pconf->vdata = cpv->v.v; + break; + default:/* should not happen */ + return; + } +} - for (size_t i = 0; i < srv->config_context->used; ++i) { - data_config const *config = (data_config const*)srv->config_context->data[i]; - plugin_config *s = calloc(1, sizeof(plugin_config)); +static void mod_vhostdb_merge_config(plugin_config * const pconf, const config_plugin_value_t *cpv) { + do { + mod_vhostdb_merge_config_cpv(pconf, cpv); + } while ((++cpv)->k_id != -1); +} - s->options = array_init(); - cv[0].destination = s->options; +static void mod_vhostdb_patch_config(connection * const con, plugin_data * const p) { + memcpy(&p->conf, &p->defaults, sizeof(plugin_config)); + for (int i = 1, used = p->nconfig; i < used; ++i) { + if (config_check_cond(con, (uint32_t)p->cvlist[i].k_id)) + mod_vhostdb_merge_config(&p->conf,p->cvlist + p->cvlist[i].v.u2[0]); + } +} - p->config_storage[i] = s; +SETDEFAULTS_FUNC(mod_vhostdb_set_defaults) { + static const config_plugin_keys_t cpk[] = { + { CONST_STR_LEN("vhostdb.dbi"), + T_CONFIG_ARRAY, + T_CONFIG_SCOPE_CONNECTION } + ,{ NULL, 0, + T_CONFIG_UNSET, + T_CONFIG_SCOPE_UNSET } + }; - if (config_insert_values_global(srv, config->value, cv, i == 0 ? T_CONFIG_SCOPE_SERVER : T_CONFIG_SCOPE_CONNECTION)) { - return HANDLER_ERROR; + plugin_data * const p = p_d; + if (!config_plugin_values_init(srv, p, cpk, "mod_vhostdb_dbi")) + return HANDLER_ERROR; + + /* process and validate config directives + * (init i to 0 if global context; to 1 to skip empty global context) */ + for (int i = !p->cvlist[0].v.u2[1]; i < p->nconfig; ++i) { + config_plugin_value_t *cpv = p->cvlist + p->cvlist[i].v.u2[0]; + for (; -1 != cpv->k_id; ++cpv) { + switch (cpv->k_id) { + case 0: /* vhostdb. */ + if (cpv->v.a->used) { + if (!array_is_kvstring(cpv->v.a)) { + log_error(srv->errh, __FILE__, __LINE__, + "unexpected value for %s; " + "expected list of \"option\" => \"value\"", + cpk[cpv->k_id].k); + return HANDLER_ERROR; + } + if (0 != mod_vhostdb_dbconf_setup(srv, cpv->v.a, &cpv->v.v)) + return HANDLER_ERROR; + if (NULL != cpv->v.v) + cpv->vtype = T_CONFIG_LOCAL; + } + break; + default:/* should not happen */ + break; + } } + } - if (!array_is_kvany(s->options)) { - log_error_write(srv, __FILE__, __LINE__, "s", - "unexpected value for vhostdb.dbi; expected list of \"option\" => \"value\""); - return HANDLER_ERROR; - } - - if (s->options->used - && 0 != mod_vhostdb_dbconf_setup(srv, s->options, &s->vdata)) { - return HANDLER_ERROR; - } + /* initialize p->defaults from global config context */ + if (p->nconfig > 0 && p->cvlist->v.u2[1]) { + const config_plugin_value_t *cpv = p->cvlist + p->cvlist->v.u2[0]; + if (-1 != cpv->k_id) + mod_vhostdb_merge_config(&p->defaults, cpv); } return HANDLER_GO_ON; } -#define PATCH(x) \ - p->conf.x = s->x; -static void mod_vhostdb_patch_connection (server *srv, connection *con, plugin_data *p) -{ - plugin_config *s = p->config_storage[0]; - PATCH(vdata); - - /* skip the first, the global context */ - for (size_t i = 1; i < srv->config_context->used; ++i) { - if (!config_check_cond(con, i)) continue; /* condition not matched */ - - data_config *dc = (data_config *)srv->config_context->data[i]; - s = p->config_storage[i]; - - /* merge config */ - for (size_t j = 0; j < dc->value->used; ++j) { - data_unset *du = dc->value->data[j]; - - if (buffer_is_equal_string(&du->key, CONST_STR_LEN("vhostdb.dbi"))) { - PATCH(vdata); - } - } - } -} -#undef PATCH -/* this function is called at dlopen() time and inits the callbacks */ int mod_vhostdb_dbi_plugin_init (plugin *p); int mod_vhostdb_dbi_plugin_init (plugin *p) { diff --git a/src/mod_vhostdb_ldap.c b/src/mod_vhostdb_ldap.c index 3f09234f..ffee2a9b 100644 --- a/src/mod_vhostdb_ldap.c +++ b/src/mod_vhostdb_ldap.c @@ -31,12 +31,11 @@ typedef struct { typedef struct { void *vdata; - array *options; } plugin_config; typedef struct { PLUGIN_DATA; - plugin_config **config_storage; + plugin_config defaults; plugin_config conf; } plugin_data; @@ -81,7 +80,7 @@ static void mod_vhostdb_dbconf_add_scheme (server *srv, buffer *host) } } -static int mod_vhostdb_dbconf_setup (server *srv, array *opts, void **vdata) +static int mod_vhostdb_dbconf_setup (server *srv, const array *opts, void **vdata) { const buffer *filter = NULL; const char *attr = "documentRoot"; @@ -151,6 +150,7 @@ static int mod_vhostdb_dbconf_setup (server *srv, array *opts, void **vdata) dbconf->starttls = starttls; *vdata = dbconf; } + return 0; } @@ -369,7 +369,7 @@ static LDAPMessage * mod_authn_ldap_search(server *srv, vhostdb_config *s, char return lm; } -static void mod_vhostdb_patch_connection (server *srv, connection *con, plugin_data *p); +static void mod_vhostdb_patch_config (connection * const con, plugin_data * const p); static int mod_vhostdb_ldap_query(server *srv, connection *con, void *p_d, buffer *docroot) { @@ -386,7 +386,7 @@ static int mod_vhostdb_ldap_query(server *srv, connection *con, void *p_d, buffe buffer *filter = docroot; buffer_clear(filter); /*(also resets docroot (alias))*/ - mod_vhostdb_patch_connection(srv, con, p); + mod_vhostdb_patch_config(con, p); if (NULL == p->conf.vdata) return 0; /*(after resetting docroot)*/ dbconf = (vhostdb_config *)p->conf.vdata; dbconf->srv = srv; @@ -459,91 +459,114 @@ INIT_FUNC(mod_vhostdb_init) { return p; } +static void mod_vhostdb_free_config(plugin_data * const p) { + if (NULL == p->cvlist) return; + /* (init i to 0 if global context; to 1 to skip empty global context) */ + for (int i = !p->cvlist[0].v.u2[1], used = p->nconfig; i < used; ++i) { + config_plugin_value_t *cpv = p->cvlist + p->cvlist[i].v.u2[0]; + for (; -1 != cpv->k_id; ++cpv) { + if (cpv->vtype != T_CONFIG_LOCAL || NULL == cpv->v.v) continue; + switch (cpv->k_id) { + case 0: /* vhostdb. */ + mod_vhostdb_dbconf_free(cpv->v.v); + break; + default: + break; + } + } + } +} + FREE_FUNC(mod_vhostdb_cleanup) { plugin_data *p = p_d; if (!p) return HANDLER_GO_ON; - if (p->config_storage) { - for (size_t i = 0; i < srv->config_context->used; i++) { - plugin_config *s = p->config_storage[i]; - if (!s) continue; - mod_vhostdb_dbconf_free(s->vdata); - array_free(s->options); - free(s); - } - free(p->config_storage); - } + mod_vhostdb_free_config(p); + + free(p->cvlist); free(p); UNUSED(srv); return HANDLER_GO_ON; } -SETDEFAULTS_FUNC(mod_vhostdb_set_defaults) { - plugin_data *p = p_d; - - config_values_t cv[] = { - { "vhostdb.ldap", NULL, T_CONFIG_ARRAY, T_CONFIG_SCOPE_CONNECTION }, - { NULL, NULL, T_CONFIG_UNSET, T_CONFIG_SCOPE_UNSET } - }; - - p->config_storage = calloc(srv->config_context->used, sizeof(plugin_config *)); +static void mod_vhostdb_merge_config_cpv(plugin_config * const pconf, const config_plugin_value_t * const cpv) { + switch (cpv->k_id) { /* index into static config_plugin_keys_t cpk[] */ + case 0: /* vhostdb. */ + if (cpv->vtype == T_CONFIG_LOCAL) + pconf->vdata = cpv->v.v; + break; + default:/* should not happen */ + return; + } +} - for (size_t i = 0; i < srv->config_context->used; ++i) { - data_config const *config = (data_config const*)srv->config_context->data[i]; - plugin_config *s = calloc(1, sizeof(plugin_config)); +static void mod_vhostdb_merge_config(plugin_config * const pconf, const config_plugin_value_t *cpv) { + do { + mod_vhostdb_merge_config_cpv(pconf, cpv); + } while ((++cpv)->k_id != -1); +} - s->options = array_init(); - cv[0].destination = s->options; +static void mod_vhostdb_patch_config(connection * const con, plugin_data * const p) { + memcpy(&p->conf, &p->defaults, sizeof(plugin_config)); + for (int i = 1, used = p->nconfig; i < used; ++i) { + if (config_check_cond(con, (uint32_t)p->cvlist[i].k_id)) + mod_vhostdb_merge_config(&p->conf,p->cvlist + p->cvlist[i].v.u2[0]); + } +} - p->config_storage[i] = s; +SETDEFAULTS_FUNC(mod_vhostdb_set_defaults) { + static const config_plugin_keys_t cpk[] = { + { CONST_STR_LEN("vhostdb.ldap"), + T_CONFIG_ARRAY, + T_CONFIG_SCOPE_CONNECTION } + ,{ NULL, 0, + T_CONFIG_UNSET, + T_CONFIG_SCOPE_UNSET } + }; - if (config_insert_values_global(srv, config->value, cv, i == 0 ? T_CONFIG_SCOPE_SERVER : T_CONFIG_SCOPE_CONNECTION)) { - return HANDLER_ERROR; + plugin_data * const p = p_d; + if (!config_plugin_values_init(srv, p, cpk, "mod_vhostdb_ldap")) + return HANDLER_ERROR; + + /* process and validate config directives + * (init i to 0 if global context; to 1 to skip empty global context) */ + for (int i = !p->cvlist[0].v.u2[1]; i < p->nconfig; ++i) { + config_plugin_value_t *cpv = p->cvlist + p->cvlist[i].v.u2[0]; + for (; -1 != cpv->k_id; ++cpv) { + switch (cpv->k_id) { + case 0: /* vhostdb. */ + if (cpv->v.a->used) { + if (!array_is_kvstring(cpv->v.a)) { + log_error(srv->errh, __FILE__, __LINE__, + "unexpected value for %s; " + "expected list of \"option\" => \"value\"", + cpk[cpv->k_id].k); + return HANDLER_ERROR; + } + if (0 != mod_vhostdb_dbconf_setup(srv, cpv->v.a, &cpv->v.v)) + return HANDLER_ERROR; + if (NULL != cpv->v.v) + cpv->vtype = T_CONFIG_LOCAL; + } + break; + default:/* should not happen */ + break; + } } + } - if (!array_is_kvstring(s->options)) { - log_error_write(srv, __FILE__, __LINE__, "s", - "unexpected value for vhostdb.ldap; expected list of \"option\" => \"value\""); - return HANDLER_ERROR; - } - - if (s->options->used - && 0 != mod_vhostdb_dbconf_setup(srv, s->options, &s->vdata)) { - return HANDLER_ERROR; - } + /* initialize p->defaults from global config context */ + if (p->nconfig > 0 && p->cvlist->v.u2[1]) { + const config_plugin_value_t *cpv = p->cvlist + p->cvlist->v.u2[0]; + if (-1 != cpv->k_id) + mod_vhostdb_merge_config(&p->defaults, cpv); } return HANDLER_GO_ON; } -#define PATCH(x) \ - p->conf.x = s->x; -static void mod_vhostdb_patch_connection (server *srv, connection *con, plugin_data *p) -{ - plugin_config *s = p->config_storage[0]; - PATCH(vdata); - - /* skip the first, the global context */ - for (size_t i = 1; i < srv->config_context->used; ++i) { - if (!config_check_cond(con, i)) continue; /* condition not matched */ - - data_config *dc = (data_config *)srv->config_context->data[i]; - s = p->config_storage[i]; - - /* merge config */ - for (size_t j = 0; j < dc->value->used; ++j) { - data_unset *du = dc->value->data[j]; - - if (buffer_is_equal_string(&du->key,CONST_STR_LEN("vhostdb.ldap"))) { - PATCH(vdata); - } - } - } -} -#undef PATCH -/* this function is called at dlopen() time and inits the callbacks */ int mod_vhostdb_ldap_plugin_init (plugin *p); int mod_vhostdb_ldap_plugin_init (plugin *p) { diff --git a/src/mod_vhostdb_mysql.c b/src/mod_vhostdb_mysql.c index 753d0a9d..bd5b84dd 100644 --- a/src/mod_vhostdb_mysql.c +++ b/src/mod_vhostdb_mysql.c @@ -22,12 +22,11 @@ typedef struct { typedef struct { void *vdata; - array *options; } plugin_config; typedef struct { PLUGIN_DATA; - plugin_config **config_storage; + plugin_config defaults; plugin_config conf; } plugin_data; @@ -39,7 +38,7 @@ static void mod_vhostdb_dbconf_free (void *vdata) free(dbconf); } -static int mod_vhostdb_dbconf_setup (server *srv, array *opts, void **vdata) +static int mod_vhostdb_dbconf_setup (server *srv, const array *opts, void **vdata) { const buffer *sqlquery = NULL; const char *dbname=NULL, *user=NULL, *pass=NULL, *host=NULL, *sock=NULL; @@ -121,7 +120,7 @@ static int mod_vhostdb_dbconf_setup (server *srv, array *opts, void **vdata) return 0; } -static void mod_vhostdb_patch_connection (server *srv, connection *con, plugin_data *p); +static void mod_vhostdb_patch_config (connection * const con, plugin_data * const p); static int mod_vhostdb_mysql_query(server *srv, connection *con, void *p_d, buffer *docroot) { @@ -135,7 +134,7 @@ static int mod_vhostdb_mysql_query(server *srv, connection *con, void *p_d, buff buffer *sqlquery = docroot; buffer_clear(sqlquery); /*(also resets docroot (alias))*/ - mod_vhostdb_patch_connection(srv, con, p); + mod_vhostdb_patch_config(con, p); if (NULL == p->conf.vdata) return 0; /*(after resetting docroot)*/ dbconf = (vhostdb_config *)p->conf.vdata; @@ -195,91 +194,114 @@ INIT_FUNC(mod_vhostdb_init) { return p; } +static void mod_vhostdb_free_config(plugin_data * const p) { + if (NULL == p->cvlist) return; + /* (init i to 0 if global context; to 1 to skip empty global context) */ + for (int i = !p->cvlist[0].v.u2[1], used = p->nconfig; i < used; ++i) { + config_plugin_value_t *cpv = p->cvlist + p->cvlist[i].v.u2[0]; + for (; -1 != cpv->k_id; ++cpv) { + if (cpv->vtype != T_CONFIG_LOCAL || NULL == cpv->v.v) continue; + switch (cpv->k_id) { + case 0: /* vhostdb. */ + mod_vhostdb_dbconf_free(cpv->v.v); + break; + default: + break; + } + } + } +} + FREE_FUNC(mod_vhostdb_cleanup) { plugin_data *p = p_d; if (!p) return HANDLER_GO_ON; - if (p->config_storage) { - for (size_t i = 0; i < srv->config_context->used; i++) { - plugin_config *s = p->config_storage[i]; - if (!s) continue; - mod_vhostdb_dbconf_free(s->vdata); - array_free(s->options); - free(s); - } - free(p->config_storage); - } + mod_vhostdb_free_config(p); + + free(p->cvlist); free(p); UNUSED(srv); return HANDLER_GO_ON; } -SETDEFAULTS_FUNC(mod_vhostdb_set_defaults) { - plugin_data *p = p_d; - - config_values_t cv[] = { - { "vhostdb.mysql", NULL, T_CONFIG_ARRAY, T_CONFIG_SCOPE_CONNECTION }, - { NULL, NULL, T_CONFIG_UNSET, T_CONFIG_SCOPE_UNSET } - }; - - p->config_storage = calloc(srv->config_context->used, sizeof(plugin_config *)); +static void mod_vhostdb_merge_config_cpv(plugin_config * const pconf, const config_plugin_value_t * const cpv) { + switch (cpv->k_id) { /* index into static config_plugin_keys_t cpk[] */ + case 0: /* vhostdb. */ + if (cpv->vtype == T_CONFIG_LOCAL) + pconf->vdata = cpv->v.v; + break; + default:/* should not happen */ + return; + } +} - for (size_t i = 0; i < srv->config_context->used; ++i) { - data_config const *config = (data_config const*)srv->config_context->data[i]; - plugin_config *s = calloc(1, sizeof(plugin_config)); +static void mod_vhostdb_merge_config(plugin_config * const pconf, const config_plugin_value_t *cpv) { + do { + mod_vhostdb_merge_config_cpv(pconf, cpv); + } while ((++cpv)->k_id != -1); +} - s->options = array_init(); - cv[0].destination = s->options; +static void mod_vhostdb_patch_config(connection * const con, plugin_data * const p) { + memcpy(&p->conf, &p->defaults, sizeof(plugin_config)); + for (int i = 1, used = p->nconfig; i < used; ++i) { + if (config_check_cond(con, (uint32_t)p->cvlist[i].k_id)) + mod_vhostdb_merge_config(&p->conf,p->cvlist + p->cvlist[i].v.u2[0]); + } +} - p->config_storage[i] = s; +SETDEFAULTS_FUNC(mod_vhostdb_set_defaults) { + static const config_plugin_keys_t cpk[] = { + { CONST_STR_LEN("vhostdb.mysql"), + T_CONFIG_ARRAY, + T_CONFIG_SCOPE_CONNECTION } + ,{ NULL, 0, + T_CONFIG_UNSET, + T_CONFIG_SCOPE_UNSET } + }; - if (config_insert_values_global(srv, config->value, cv, i == 0 ? T_CONFIG_SCOPE_SERVER : T_CONFIG_SCOPE_CONNECTION)) { - return HANDLER_ERROR; + plugin_data * const p = p_d; + if (!config_plugin_values_init(srv, p, cpk, "mod_vhostdb_mysql")) + return HANDLER_ERROR; + + /* process and validate config directives + * (init i to 0 if global context; to 1 to skip empty global context) */ + for (int i = !p->cvlist[0].v.u2[1]; i < p->nconfig; ++i) { + config_plugin_value_t *cpv = p->cvlist + p->cvlist[i].v.u2[0]; + for (; -1 != cpv->k_id; ++cpv) { + switch (cpv->k_id) { + case 0: /* vhostdb. */ + if (cpv->v.a->used) { + if (!array_is_kvstring(cpv->v.a)) { + log_error(srv->errh, __FILE__, __LINE__, + "unexpected value for %s; " + "expected list of \"option\" => \"value\"", + cpk[cpv->k_id].k); + return HANDLER_ERROR; + } + if (0 != mod_vhostdb_dbconf_setup(srv, cpv->v.a, &cpv->v.v)) + return HANDLER_ERROR; + if (NULL != cpv->v.v) + cpv->vtype = T_CONFIG_LOCAL; + } + break; + default:/* should not happen */ + break; + } } + } - if (!array_is_kvstring(s->options)) { - log_error_write(srv, __FILE__, __LINE__, "s", - "unexpected value for vhostdb.mysql; expected list of \"option\" => \"value\""); - return HANDLER_ERROR; - } - - if (s->options->used - && 0 != mod_vhostdb_dbconf_setup(srv, s->options, &s->vdata)) { - return HANDLER_ERROR; - } + /* initialize p->defaults from global config context */ + if (p->nconfig > 0 && p->cvlist->v.u2[1]) { + const config_plugin_value_t *cpv = p->cvlist + p->cvlist->v.u2[0]; + if (-1 != cpv->k_id) + mod_vhostdb_merge_config(&p->defaults, cpv); } return HANDLER_GO_ON; } -#define PATCH(x) \ - p->conf.x = s->x; -static void mod_vhostdb_patch_connection (server *srv, connection *con, plugin_data *p) -{ - plugin_config *s = p->config_storage[0]; - PATCH(vdata); - - /* skip the first, the global context */ - for (size_t i = 1; i < srv->config_context->used; ++i) { - if (!config_check_cond(con, i)) continue; /* condition not matched */ - - data_config *dc = (data_config *)srv->config_context->data[i]; - s = p->config_storage[i]; - - /* merge config */ - for (size_t j = 0; j < dc->value->used; ++j) { - data_unset *du = dc->value->data[j]; - - if (buffer_is_equal_string(&du->key,CONST_STR_LEN("vhostdb.mysql"))){ - PATCH(vdata); - } - } - } -} -#undef PATCH -/* this function is called at dlopen() time and inits the callbacks */ int mod_vhostdb_mysql_plugin_init (plugin *p); int mod_vhostdb_mysql_plugin_init (plugin *p) { diff --git a/src/mod_vhostdb_pgsql.c b/src/mod_vhostdb_pgsql.c index 261e4be2..49ab1953 100644 --- a/src/mod_vhostdb_pgsql.c +++ b/src/mod_vhostdb_pgsql.c @@ -21,12 +21,11 @@ typedef struct { typedef struct { void *vdata; - array *options; } plugin_config; typedef struct { PLUGIN_DATA; - plugin_config **config_storage; + plugin_config defaults; plugin_config conf; } plugin_data; @@ -38,7 +37,7 @@ static void mod_vhostdb_dbconf_free (void *vdata) free(dbconf); } -static int mod_vhostdb_dbconf_setup (server *srv, array *opts, void **vdata) +static int mod_vhostdb_dbconf_setup (server *srv, const array *opts, void **vdata) { const buffer *sqlquery = NULL; const char *dbname=NULL, *user=NULL, *pass=NULL, *host=NULL, *port=NULL; @@ -100,7 +99,7 @@ static int mod_vhostdb_dbconf_setup (server *srv, array *opts, void **vdata) return 0; } -static void mod_vhostdb_patch_connection (server *srv, connection *con, plugin_data *p); +static void mod_vhostdb_patch_config(connection * const con, plugin_data * const p); static int mod_vhostdb_pgsql_query(server *srv, connection *con, void *p_d, buffer *docroot) { @@ -113,7 +112,7 @@ static int mod_vhostdb_pgsql_query(server *srv, connection *con, void *p_d, buff buffer *sqlquery = docroot; buffer_clear(sqlquery); /*(also resets docroot (alias))*/ - mod_vhostdb_patch_connection(srv, con, p); + mod_vhostdb_patch_config(con, p); if (NULL == p->conf.vdata) return 0; /*(after resetting docroot)*/ dbconf = (vhostdb_config *)p->conf.vdata; @@ -172,91 +171,114 @@ INIT_FUNC(mod_vhostdb_init) { return p; } +static void mod_vhostdb_free_config(plugin_data * const p) { + if (NULL == p->cvlist) return; + /* (init i to 0 if global context; to 1 to skip empty global context) */ + for (int i = !p->cvlist[0].v.u2[1], used = p->nconfig; i < used; ++i) { + config_plugin_value_t *cpv = p->cvlist + p->cvlist[i].v.u2[0]; + for (; -1 != cpv->k_id; ++cpv) { + if (cpv->vtype != T_CONFIG_LOCAL || NULL == cpv->v.v) continue; + switch (cpv->k_id) { + case 0: /* vhostdb. */ + mod_vhostdb_dbconf_free(cpv->v.v); + break; + default: + break; + } + } + } +} + FREE_FUNC(mod_vhostdb_cleanup) { plugin_data *p = p_d; if (!p) return HANDLER_GO_ON; - if (p->config_storage) { - for (size_t i = 0; i < srv->config_context->used; i++) { - plugin_config *s = p->config_storage[i]; - if (!s) continue; - mod_vhostdb_dbconf_free(s->vdata); - array_free(s->options); - free(s); - } - free(p->config_storage); - } + mod_vhostdb_free_config(p); + + free(p->cvlist); free(p); UNUSED(srv); return HANDLER_GO_ON; } -SETDEFAULTS_FUNC(mod_vhostdb_set_defaults) { - plugin_data *p = p_d; - - config_values_t cv[] = { - { "vhostdb.pgsql", NULL, T_CONFIG_ARRAY, T_CONFIG_SCOPE_CONNECTION }, - { NULL, NULL, T_CONFIG_UNSET, T_CONFIG_SCOPE_UNSET } - }; - - p->config_storage = calloc(srv->config_context->used, sizeof(plugin_config *)); +static void mod_vhostdb_merge_config_cpv(plugin_config * const pconf, const config_plugin_value_t * const cpv) { + switch (cpv->k_id) { /* index into static config_plugin_keys_t cpk[] */ + case 0: /* vhostdb. */ + if (cpv->vtype == T_CONFIG_LOCAL) + pconf->vdata = cpv->v.v; + break; + default:/* should not happen */ + return; + } +} - for (size_t i = 0; i < srv->config_context->used; ++i) { - data_config const *config = (data_config const*)srv->config_context->data[i]; - plugin_config *s = calloc(1, sizeof(plugin_config)); +static void mod_vhostdb_merge_config(plugin_config * const pconf, const config_plugin_value_t *cpv) { + do { + mod_vhostdb_merge_config_cpv(pconf, cpv); + } while ((++cpv)->k_id != -1); +} - s->options = array_init(); - cv[0].destination = s->options; +static void mod_vhostdb_patch_config(connection * const con, plugin_data * const p) { + memcpy(&p->conf, &p->defaults, sizeof(plugin_config)); + for (int i = 1, used = p->nconfig; i < used; ++i) { + if (config_check_cond(con, (uint32_t)p->cvlist[i].k_id)) + mod_vhostdb_merge_config(&p->conf,p->cvlist + p->cvlist[i].v.u2[0]); + } +} - p->config_storage[i] = s; +SETDEFAULTS_FUNC(mod_vhostdb_set_defaults) { + static const config_plugin_keys_t cpk[] = { + { CONST_STR_LEN("vhostdb.pgsql"), + T_CONFIG_ARRAY, + T_CONFIG_SCOPE_CONNECTION } + ,{ NULL, 0, + T_CONFIG_UNSET, + T_CONFIG_SCOPE_UNSET } + }; - if (config_insert_values_global(srv, config->value, cv, i == 0 ? T_CONFIG_SCOPE_SERVER : T_CONFIG_SCOPE_CONNECTION)) { - return HANDLER_ERROR; + plugin_data * const p = p_d; + if (!config_plugin_values_init(srv, p, cpk, "mod_vhostdb_pgsql")) + return HANDLER_ERROR; + + /* process and validate config directives + * (init i to 0 if global context; to 1 to skip empty global context) */ + for (int i = !p->cvlist[0].v.u2[1]; i < p->nconfig; ++i) { + config_plugin_value_t *cpv = p->cvlist + p->cvlist[i].v.u2[0]; + for (; -1 != cpv->k_id; ++cpv) { + switch (cpv->k_id) { + case 0: /* vhostdb. */ + if (cpv->v.a->used) { + if (!array_is_kvstring(cpv->v.a)) { + log_error(srv->errh, __FILE__, __LINE__, + "unexpected value for %s; " + "expected list of \"option\" => \"value\"", + cpk[cpv->k_id].k); + return HANDLER_ERROR; + } + if (0 != mod_vhostdb_dbconf_setup(srv, cpv->v.a, &cpv->v.v)) + return HANDLER_ERROR; + if (NULL != cpv->v.v) + cpv->vtype = T_CONFIG_LOCAL; + } + break; + default:/* should not happen */ + break; + } } + } - if (!array_is_kvstring(s->options)) { - log_error_write(srv, __FILE__, __LINE__, "s", - "unexpected value for vhostdb.pgsql; expected list of \"option\" => \"value\""); - return HANDLER_ERROR; - } - - if (s->options->used - && 0 != mod_vhostdb_dbconf_setup(srv, s->options, &s->vdata)) { - return HANDLER_ERROR; - } + /* initialize p->defaults from global config context */ + if (p->nconfig > 0 && p->cvlist->v.u2[1]) { + const config_plugin_value_t *cpv = p->cvlist + p->cvlist->v.u2[0]; + if (-1 != cpv->k_id) + mod_vhostdb_merge_config(&p->defaults, cpv); } return HANDLER_GO_ON; } -#define PATCH(x) \ - p->conf.x = s->x; -static void mod_vhostdb_patch_connection (server *srv, connection *con, plugin_data *p) -{ - plugin_config *s = p->config_storage[0]; - PATCH(vdata); - - /* skip the first, the global context */ - for (size_t i = 1; i < srv->config_context->used; ++i) { - if (!config_check_cond(con, i)) continue; /* condition not matched */ - - data_config *dc = (data_config *)srv->config_context->data[i]; - s = p->config_storage[i]; - - /* merge config */ - for (size_t j = 0; j < dc->value->used; ++j) { - data_unset *du = dc->value->data[j]; - - if (buffer_is_equal_string(&du->key,CONST_STR_LEN("vhostdb.pgsql"))){ - PATCH(vdata); - } - } - } -} -#undef PATCH -/* this function is called at dlopen() time and inits the callbacks */ int mod_vhostdb_pgsql_plugin_init (plugin *p); int mod_vhostdb_pgsql_plugin_init (plugin *p) {