2020-05-20 05:06:13 +00:00
|
|
|
/*
|
|
|
|
* mod_vhostdb_mysql - virtual hosts mapping from backend MySQL/MariaDB database
|
|
|
|
*
|
|
|
|
* Copyright(c) 2017 Glenn Strauss gstrauss()gluelogic.com All rights reserved
|
|
|
|
* License: BSD 3-clause (same as lighttpd)
|
|
|
|
*/
|
2017-01-18 05:36:49 +00:00
|
|
|
#include "first.h"
|
|
|
|
|
|
|
|
#include <mysql.h>
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "base.h"
|
|
|
|
#include "http_vhostdb.h"
|
2017-03-28 04:04:31 +00:00
|
|
|
#include "fdevent.h"
|
2017-01-18 05:36:49 +00:00
|
|
|
#include "log.h"
|
|
|
|
#include "plugin.h"
|
|
|
|
|
|
|
|
/*
|
2020-05-20 05:06:13 +00:00
|
|
|
* virtual host plugin using MySQL/MariaDB for domain to directory lookups
|
2017-01-18 05:36:49 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
MYSQL *dbconn;
|
2019-10-13 08:59:57 +00:00
|
|
|
const buffer *sqlquery;
|
2017-01-18 05:36:49 +00:00
|
|
|
} vhostdb_config;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
void *vdata;
|
|
|
|
} plugin_config;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
PLUGIN_DATA;
|
2019-11-03 05:02:14 +00:00
|
|
|
plugin_config defaults;
|
2017-01-18 05:36:49 +00:00
|
|
|
plugin_config conf;
|
|
|
|
} plugin_data;
|
|
|
|
|
|
|
|
static void mod_vhostdb_dbconf_free (void *vdata)
|
|
|
|
{
|
|
|
|
vhostdb_config *dbconf = (vhostdb_config *)vdata;
|
|
|
|
if (!dbconf) return;
|
|
|
|
mysql_close(dbconf->dbconn);
|
|
|
|
free(dbconf);
|
|
|
|
}
|
|
|
|
|
2019-11-03 05:02:14 +00:00
|
|
|
static int mod_vhostdb_dbconf_setup (server *srv, const array *opts, void **vdata)
|
2017-01-18 05:36:49 +00:00
|
|
|
{
|
2019-10-13 08:59:57 +00:00
|
|
|
const buffer *sqlquery = NULL;
|
2017-01-18 05:36:49 +00:00
|
|
|
const char *dbname=NULL, *user=NULL, *pass=NULL, *host=NULL, *sock=NULL;
|
|
|
|
unsigned int port = 0;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < opts->used; ++i) {
|
|
|
|
const data_string *ds = (data_string *)opts->data[i];
|
2019-10-13 08:59:57 +00:00
|
|
|
if (ds->type == TYPE_STRING && !buffer_string_is_empty(&ds->value)) {
|
2019-10-13 07:37:59 +00:00
|
|
|
if (buffer_is_equal_caseless_string(&ds->key, CONST_STR_LEN("sql"))) {
|
2019-10-13 08:59:57 +00:00
|
|
|
sqlquery = &ds->value;
|
2019-10-13 07:37:59 +00:00
|
|
|
} else if (buffer_is_equal_caseless_string(&ds->key, CONST_STR_LEN("dbname"))) {
|
2019-10-13 08:59:57 +00:00
|
|
|
dbname = ds->value.ptr;
|
2019-10-13 07:37:59 +00:00
|
|
|
} else if (buffer_is_equal_caseless_string(&ds->key, CONST_STR_LEN("user"))) {
|
2019-10-13 08:59:57 +00:00
|
|
|
user = ds->value.ptr;
|
2019-10-13 07:37:59 +00:00
|
|
|
} else if (buffer_is_equal_caseless_string(&ds->key, CONST_STR_LEN("password"))) {
|
2019-10-13 08:59:57 +00:00
|
|
|
pass = ds->value.ptr;
|
2019-10-13 07:37:59 +00:00
|
|
|
} else if (buffer_is_equal_caseless_string(&ds->key, CONST_STR_LEN("host"))) {
|
2019-10-13 08:59:57 +00:00
|
|
|
host = ds->value.ptr;
|
2019-10-13 07:37:59 +00:00
|
|
|
} else if (buffer_is_equal_caseless_string(&ds->key, CONST_STR_LEN("port"))) {
|
2019-10-13 08:59:57 +00:00
|
|
|
port = strtoul(ds->value.ptr, NULL, 10);
|
2019-10-13 07:37:59 +00:00
|
|
|
} else if (buffer_is_equal_caseless_string(&ds->key, CONST_STR_LEN("sock"))) {
|
2019-10-13 08:59:57 +00:00
|
|
|
sock = ds->value.ptr;
|
2017-01-18 05:36:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* required:
|
|
|
|
* - sql (sql query)
|
|
|
|
* - dbname
|
|
|
|
* - user
|
|
|
|
*
|
|
|
|
* optional:
|
|
|
|
* - password, default: empty
|
|
|
|
* - socket, default: mysql default
|
|
|
|
* - hostname, if set overrides socket
|
|
|
|
* - port, default: 3306
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (!buffer_string_is_empty(sqlquery)
|
|
|
|
&& dbname && *dbname && user && *user) {
|
|
|
|
vhostdb_config *dbconf;
|
|
|
|
MYSQL *dbconn = mysql_init(NULL);
|
|
|
|
if (NULL == dbconn) {
|
2019-11-25 06:54:08 +00:00
|
|
|
log_error(srv->errh, __FILE__, __LINE__,
|
|
|
|
"mysql_init() failed, exiting...");
|
2017-01-18 05:36:49 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if MYSQL_VERSION_ID >= 50013
|
|
|
|
/* in mysql versions above 5.0.3 the reconnect flag is off by default */
|
|
|
|
{
|
2018-10-15 04:15:13 +00:00
|
|
|
char reconnect = 1;
|
2017-01-18 05:36:49 +00:00
|
|
|
mysql_options(dbconn, MYSQL_OPT_RECONNECT, &reconnect);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* CLIENT_MULTI_STATEMENTS first appeared in 4.1 */
|
|
|
|
#if MYSQL_VERSION_ID < 40100
|
|
|
|
#ifndef CLIENT_MULTI_STATEMENTS
|
|
|
|
#define CLIENT_MULTI_STATEMENTS 0
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
if (!mysql_real_connect(dbconn, host, user, pass, dbname, port, sock,
|
|
|
|
CLIENT_MULTI_STATEMENTS)) {
|
2019-11-25 06:54:08 +00:00
|
|
|
log_error(srv->errh, __FILE__, __LINE__, "%s", mysql_error(dbconn));
|
2017-01-18 05:36:49 +00:00
|
|
|
mysql_close(dbconn);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-06-20 03:25:39 +00:00
|
|
|
fdevent_setfd_cloexec(dbconn->net.fd);
|
2017-01-18 05:36:49 +00:00
|
|
|
|
|
|
|
dbconf = (vhostdb_config *)calloc(1, sizeof(*dbconf));
|
|
|
|
dbconf->dbconn = dbconn;
|
|
|
|
dbconf->sqlquery = sqlquery;
|
|
|
|
*vdata = dbconf;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
static void mod_vhostdb_patch_config (request_st * const r, plugin_data * const p);
|
2017-01-18 05:36:49 +00:00
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
static int mod_vhostdb_mysql_query(request_st * const r, void *p_d, buffer *docroot)
|
2017-01-18 05:36:49 +00:00
|
|
|
{
|
|
|
|
plugin_data *p = (plugin_data *)p_d;
|
|
|
|
vhostdb_config *dbconf;
|
|
|
|
unsigned cols;
|
|
|
|
MYSQL_ROW row;
|
|
|
|
MYSQL_RES *result;
|
|
|
|
|
|
|
|
/*(reuse buffer for sql query before generating docroot result)*/
|
|
|
|
buffer *sqlquery = docroot;
|
2018-11-23 05:37:38 +00:00
|
|
|
buffer_clear(sqlquery); /*(also resets docroot (alias))*/
|
2017-01-18 05:36:49 +00:00
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
mod_vhostdb_patch_config(r, p);
|
2017-01-18 05:36:49 +00:00
|
|
|
if (NULL == p->conf.vdata) return 0; /*(after resetting docroot)*/
|
|
|
|
dbconf = (vhostdb_config *)p->conf.vdata;
|
|
|
|
|
|
|
|
for (char *b = dbconf->sqlquery->ptr, *d; *b; b = d+1) {
|
|
|
|
if (NULL != (d = strchr(b, '?'))) {
|
|
|
|
/* escape the uri.authority */
|
|
|
|
unsigned long len;
|
|
|
|
buffer_append_string_len(sqlquery, b, (size_t)(d - b));
|
2020-01-13 02:51:12 +00:00
|
|
|
buffer_string_prepare_append(sqlquery, buffer_string_length(&r->uri.authority) * 2);
|
2017-01-18 05:36:49 +00:00
|
|
|
len = mysql_real_escape_string(dbconf->dbconn,
|
|
|
|
sqlquery->ptr + buffer_string_length(sqlquery),
|
2020-01-13 02:51:12 +00:00
|
|
|
CONST_BUF_LEN(&r->uri.authority));
|
2017-01-18 05:36:49 +00:00
|
|
|
if ((unsigned long)~0 == len) return -1;
|
|
|
|
buffer_commit(sqlquery, len);
|
|
|
|
} else {
|
|
|
|
d = dbconf->sqlquery->ptr + buffer_string_length(dbconf->sqlquery);
|
|
|
|
buffer_append_string_len(sqlquery, b, (size_t)(d - b));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mysql_real_query(dbconf->dbconn, CONST_BUF_LEN(sqlquery))) {
|
2020-01-13 02:51:12 +00:00
|
|
|
log_error(r->conf.errh, __FILE__, __LINE__, "%s",
|
2019-11-25 06:54:08 +00:00
|
|
|
mysql_error(dbconf->dbconn));
|
2018-11-23 05:37:38 +00:00
|
|
|
buffer_clear(docroot); /*(reset buffer; no result)*/
|
2017-01-18 05:36:49 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-11-23 05:37:38 +00:00
|
|
|
buffer_clear(docroot); /*(reset buffer to store result)*/
|
2017-01-18 05:36:49 +00:00
|
|
|
|
|
|
|
result = mysql_store_result(dbconf->dbconn);
|
|
|
|
cols = mysql_num_fields(result);
|
|
|
|
row = mysql_fetch_row(result);
|
|
|
|
if (row && cols >= 1) {
|
|
|
|
buffer_copy_string(docroot, row[0]);
|
|
|
|
} /* else no such virtual host */
|
|
|
|
|
|
|
|
mysql_free_result(result);
|
|
|
|
#if MYSQL_VERSION_ID >= 40100
|
|
|
|
while (0 == mysql_next_result(dbconf->dbconn)) ;
|
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
INIT_FUNC(mod_vhostdb_init) {
|
|
|
|
static http_vhostdb_backend_t http_vhostdb_backend_mysql =
|
|
|
|
{ "mysql", mod_vhostdb_mysql_query, NULL };
|
|
|
|
plugin_data *p = calloc(1, sizeof(*p));
|
|
|
|
|
|
|
|
/* register http_vhostdb_backend_mysql */
|
|
|
|
http_vhostdb_backend_mysql.p_d = p;
|
|
|
|
http_vhostdb_backend_set(&http_vhostdb_backend_mysql);
|
|
|
|
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2019-11-19 08:39:40 +00:00
|
|
|
FREE_FUNC(mod_vhostdb_cleanup) {
|
|
|
|
plugin_data * const p = p_d;
|
2019-11-03 05:02:14 +00:00
|
|
|
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.<db> */
|
|
|
|
mod_vhostdb_dbconf_free(cpv->v.v);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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.<db> */
|
|
|
|
if (cpv->vtype == T_CONFIG_LOCAL)
|
|
|
|
pconf->vdata = cpv->v.v;
|
|
|
|
break;
|
|
|
|
default:/* should not happen */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2017-01-18 05:36:49 +00:00
|
|
|
|
2019-11-03 05:02:14 +00:00
|
|
|
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);
|
|
|
|
}
|
2017-01-18 05:36:49 +00:00
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
static void mod_vhostdb_patch_config(request_st * const r, plugin_data * const p) {
|
2020-01-11 16:07:43 +00:00
|
|
|
p->conf = p->defaults; /* copy small struct instead of memcpy() */
|
|
|
|
/*memcpy(&p->conf, &p->defaults, sizeof(plugin_config));*/
|
2019-11-03 05:02:14 +00:00
|
|
|
for (int i = 1, used = p->nconfig; i < used; ++i) {
|
2020-01-13 02:51:12 +00:00
|
|
|
if (config_check_cond(r, (uint32_t)p->cvlist[i].k_id))
|
2019-11-03 05:02:14 +00:00
|
|
|
mod_vhostdb_merge_config(&p->conf,p->cvlist + p->cvlist[i].v.u2[0]);
|
|
|
|
}
|
|
|
|
}
|
2017-01-18 05:36:49 +00:00
|
|
|
|
2019-11-03 05:02:14 +00:00
|
|
|
SETDEFAULTS_FUNC(mod_vhostdb_set_defaults) {
|
|
|
|
static const config_plugin_keys_t cpk[] = {
|
|
|
|
{ CONST_STR_LEN("vhostdb.mysql"),
|
2019-12-08 00:15:55 +00:00
|
|
|
T_CONFIG_ARRAY_KVSTRING,
|
2019-11-03 05:02:14 +00:00
|
|
|
T_CONFIG_SCOPE_CONNECTION }
|
|
|
|
,{ NULL, 0,
|
|
|
|
T_CONFIG_UNSET,
|
|
|
|
T_CONFIG_SCOPE_UNSET }
|
|
|
|
};
|
2017-01-18 05:36:49 +00:00
|
|
|
|
2019-11-03 05:02:14 +00:00
|
|
|
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.<db> */
|
|
|
|
if (cpv->v.a->used) {
|
|
|
|
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;
|
|
|
|
}
|
2017-01-18 05:36:49 +00:00
|
|
|
}
|
2019-11-03 05:02:14 +00:00
|
|
|
}
|
2017-01-18 05:36:49 +00:00
|
|
|
|
2019-11-03 05:02:14 +00:00
|
|
|
/* 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);
|
2017-01-18 05:36:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return HANDLER_GO_ON;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int mod_vhostdb_mysql_plugin_init (plugin *p);
|
|
|
|
int mod_vhostdb_mysql_plugin_init (plugin *p)
|
|
|
|
{
|
|
|
|
p->version = LIGHTTPD_VERSION_ID;
|
2019-10-19 04:30:54 +00:00
|
|
|
p->name = "vhostdb_mysql";
|
2017-01-18 05:36:49 +00:00
|
|
|
|
|
|
|
p->init = mod_vhostdb_init;
|
|
|
|
p->cleanup = mod_vhostdb_cleanup;
|
|
|
|
p->set_defaults = mod_vhostdb_set_defaults;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|