1 /*
2 * mod_vhostdb_mysql - virtual hosts mapping from backend MySQL/MariaDB database
3 *
4 * Copyright(c) 2017 Glenn Strauss gstrauss()gluelogic.com All rights reserved
5 * License: BSD 3-clause (same as lighttpd)
6 */
7 #include "first.h"
8
9 #include <string.h>
10 #include <stdlib.h>
11
12 #include <mysql.h>
13
14 #include "mod_vhostdb_api.h"
15 #include "base.h"
16 #include "fdevent.h"
17 #include "log.h"
18 #include "plugin.h"
19
20 /*
21 * virtual host plugin using MySQL/MariaDB for domain to directory lookups
22 */
23
24 typedef struct {
25 MYSQL *dbconn;
26 const buffer *sqlquery;
27 } vhostdb_config;
28
29 typedef struct {
30 void *vdata;
31 } plugin_config;
32
33 typedef struct {
34 PLUGIN_DATA;
35 plugin_config defaults;
36 plugin_config conf;
37 } plugin_data;
38
mod_vhostdb_dbconf_free(void * vdata)39 static void mod_vhostdb_dbconf_free (void *vdata)
40 {
41 vhostdb_config *dbconf = (vhostdb_config *)vdata;
42 if (!dbconf) return;
43 mysql_close(dbconf->dbconn);
44 free(dbconf);
45 }
46
mod_vhostdb_dbconf_setup(server * srv,const array * opts,void ** vdata)47 static int mod_vhostdb_dbconf_setup (server *srv, const array *opts, void **vdata)
48 {
49 const buffer *sqlquery = NULL;
50 const char *dbname=NULL, *user=NULL, *pass=NULL, *host=NULL, *sock=NULL;
51 unsigned int port = 0;
52
53 for (size_t i = 0; i < opts->used; ++i) {
54 const data_string *ds = (data_string *)opts->data[i];
55 if (ds->type == TYPE_STRING && !buffer_is_blank(&ds->value)) {
56 if (buffer_is_equal_caseless_string(&ds->key, CONST_STR_LEN("sql"))) {
57 sqlquery = &ds->value;
58 } else if (buffer_is_equal_caseless_string(&ds->key, CONST_STR_LEN("dbname"))) {
59 dbname = ds->value.ptr;
60 } else if (buffer_is_equal_caseless_string(&ds->key, CONST_STR_LEN("user"))) {
61 user = ds->value.ptr;
62 } else if (buffer_is_equal_caseless_string(&ds->key, CONST_STR_LEN("password"))) {
63 pass = ds->value.ptr;
64 } else if (buffer_is_equal_caseless_string(&ds->key, CONST_STR_LEN("host"))) {
65 host = ds->value.ptr;
66 } else if (buffer_is_equal_caseless_string(&ds->key, CONST_STR_LEN("port"))) {
67 port = strtoul(ds->value.ptr, NULL, 10);
68 } else if (buffer_is_equal_caseless_string(&ds->key, CONST_STR_LEN("sock"))) {
69 sock = ds->value.ptr;
70 }
71 }
72 }
73
74 /* required:
75 * - sql (sql query)
76 * - dbname
77 * - user
78 *
79 * optional:
80 * - password, default: empty
81 * - socket, default: mysql default
82 * - hostname, if set overrides socket
83 * - port, default: 3306
84 */
85
86 if (NULL != sqlquery && !buffer_is_blank(sqlquery)
87 && dbname && *dbname && user && *user) {
88 vhostdb_config *dbconf;
89 MYSQL *dbconn = mysql_init(NULL);
90 if (NULL == dbconn) {
91 log_error(srv->errh, __FILE__, __LINE__,
92 "mysql_init() failed, exiting...");
93 return -1;
94 }
95
96 #if MYSQL_VERSION_ID >= 50013
97 /* in mysql versions above 5.0.3 the reconnect flag is off by default */
98 {
99 char reconnect = 1;
100 mysql_options(dbconn, MYSQL_OPT_RECONNECT, &reconnect);
101 }
102 #endif
103
104 /* CLIENT_MULTI_STATEMENTS first appeared in 4.1 */
105 #if MYSQL_VERSION_ID < 40100
106 #ifndef CLIENT_MULTI_STATEMENTS
107 #define CLIENT_MULTI_STATEMENTS 0
108 #endif
109 #endif
110 if (!mysql_real_connect(dbconn, host, user, pass, dbname, port, sock,
111 CLIENT_MULTI_STATEMENTS)) {
112 log_error(srv->errh, __FILE__, __LINE__, "%s", mysql_error(dbconn));
113 mysql_close(dbconn);
114 return -1;
115 }
116
117 fdevent_setfd_cloexec(dbconn->net.fd);
118
119 dbconf = (vhostdb_config *)ck_calloc(1, sizeof(*dbconf));
120 dbconf->dbconn = dbconn;
121 dbconf->sqlquery = sqlquery;
122 *vdata = dbconf;
123 }
124
125 return 0;
126 }
127
128 static void mod_vhostdb_patch_config (request_st * const r, plugin_data * const p);
129
mod_vhostdb_mysql_query(request_st * const r,void * p_d,buffer * docroot)130 static int mod_vhostdb_mysql_query(request_st * const r, void *p_d, buffer *docroot)
131 {
132 plugin_data *p = (plugin_data *)p_d;
133 vhostdb_config *dbconf;
134 unsigned cols;
135 MYSQL_ROW row;
136 MYSQL_RES *result;
137
138 /*(reuse buffer for sql query before generating docroot result)*/
139 buffer *sqlquery = docroot;
140 buffer_clear(sqlquery); /*(also resets docroot (alias))*/
141
142 mod_vhostdb_patch_config(r, p);
143 if (NULL == p->conf.vdata) return 0; /*(after resetting docroot)*/
144 dbconf = (vhostdb_config *)p->conf.vdata;
145
146 for (char *b = dbconf->sqlquery->ptr, *d; *b; b = d+1) {
147 if (NULL != (d = strchr(b, '?'))) {
148 /* escape the uri.authority */
149 unsigned long len;
150 buffer_append_string_len(sqlquery, b, (size_t)(d - b));
151 buffer_string_prepare_append(sqlquery,
152 buffer_clen(&r->uri.authority) * 2);
153 len = mysql_real_escape_string(dbconf->dbconn,
154 sqlquery->ptr + buffer_clen(sqlquery),
155 BUF_PTR_LEN(&r->uri.authority));
156 if ((unsigned long)~0 == len) return -1;
157 buffer_commit(sqlquery, len);
158 } else {
159 d = dbconf->sqlquery->ptr + buffer_clen(dbconf->sqlquery);
160 buffer_append_string_len(sqlquery, b, (size_t)(d - b));
161 break;
162 }
163 }
164
165 if (mysql_real_query(dbconf->dbconn, BUF_PTR_LEN(sqlquery))) {
166 log_error(r->conf.errh, __FILE__, __LINE__, "%s",
167 mysql_error(dbconf->dbconn));
168 buffer_clear(docroot); /*(reset buffer; no result)*/
169 return -1;
170 }
171
172 buffer_clear(docroot); /*(reset buffer to store result)*/
173
174 result = mysql_store_result(dbconf->dbconn);
175 cols = mysql_num_fields(result);
176 row = mysql_fetch_row(result);
177 if (row && cols >= 1) {
178 buffer_copy_string(docroot, row[0]);
179 } /* else no such virtual host */
180
181 mysql_free_result(result);
182 #if MYSQL_VERSION_ID >= 40100
183 while (0 == mysql_next_result(dbconf->dbconn)) ;
184 #endif
185 return 0;
186 }
187
188
189
190
INIT_FUNC(mod_vhostdb_init)191 INIT_FUNC(mod_vhostdb_init) {
192 static http_vhostdb_backend_t http_vhostdb_backend_mysql =
193 { "mysql", mod_vhostdb_mysql_query, NULL };
194 plugin_data *p = ck_calloc(1, sizeof(*p));
195
196 /* register http_vhostdb_backend_mysql */
197 http_vhostdb_backend_mysql.p_d = p;
198 http_vhostdb_backend_set(&http_vhostdb_backend_mysql);
199
200 return p;
201 }
202
FREE_FUNC(mod_vhostdb_cleanup)203 FREE_FUNC(mod_vhostdb_cleanup) {
204 plugin_data * const p = p_d;
205 if (NULL == p->cvlist) return;
206 /* (init i to 0 if global context; to 1 to skip empty global context) */
207 for (int i = !p->cvlist[0].v.u2[1], used = p->nconfig; i < used; ++i) {
208 config_plugin_value_t *cpv = p->cvlist + p->cvlist[i].v.u2[0];
209 for (; -1 != cpv->k_id; ++cpv) {
210 if (cpv->vtype != T_CONFIG_LOCAL || NULL == cpv->v.v) continue;
211 switch (cpv->k_id) {
212 case 0: /* vhostdb.<db> */
213 mod_vhostdb_dbconf_free(cpv->v.v);
214 break;
215 default:
216 break;
217 }
218 }
219 }
220 }
221
mod_vhostdb_merge_config_cpv(plugin_config * const pconf,const config_plugin_value_t * const cpv)222 static void mod_vhostdb_merge_config_cpv(plugin_config * const pconf, const config_plugin_value_t * const cpv) {
223 switch (cpv->k_id) { /* index into static config_plugin_keys_t cpk[] */
224 case 0: /* vhostdb.<db> */
225 if (cpv->vtype == T_CONFIG_LOCAL)
226 pconf->vdata = cpv->v.v;
227 break;
228 default:/* should not happen */
229 return;
230 }
231 }
232
mod_vhostdb_merge_config(plugin_config * const pconf,const config_plugin_value_t * cpv)233 static void mod_vhostdb_merge_config(plugin_config * const pconf, const config_plugin_value_t *cpv) {
234 do {
235 mod_vhostdb_merge_config_cpv(pconf, cpv);
236 } while ((++cpv)->k_id != -1);
237 }
238
mod_vhostdb_patch_config(request_st * const r,plugin_data * const p)239 static void mod_vhostdb_patch_config(request_st * const r, plugin_data * const p) {
240 p->conf = p->defaults; /* copy small struct instead of memcpy() */
241 /*memcpy(&p->conf, &p->defaults, sizeof(plugin_config));*/
242 for (int i = 1, used = p->nconfig; i < used; ++i) {
243 if (config_check_cond(r, (uint32_t)p->cvlist[i].k_id))
244 mod_vhostdb_merge_config(&p->conf,p->cvlist + p->cvlist[i].v.u2[0]);
245 }
246 }
247
SETDEFAULTS_FUNC(mod_vhostdb_set_defaults)248 SETDEFAULTS_FUNC(mod_vhostdb_set_defaults) {
249 static const config_plugin_keys_t cpk[] = {
250 { CONST_STR_LEN("vhostdb.mysql"),
251 T_CONFIG_ARRAY_KVSTRING,
252 T_CONFIG_SCOPE_CONNECTION }
253 ,{ NULL, 0,
254 T_CONFIG_UNSET,
255 T_CONFIG_SCOPE_UNSET }
256 };
257
258 plugin_data * const p = p_d;
259 if (!config_plugin_values_init(srv, p, cpk, "mod_vhostdb_mysql"))
260 return HANDLER_ERROR;
261
262 /* process and validate config directives
263 * (init i to 0 if global context; to 1 to skip empty global context) */
264 for (int i = !p->cvlist[0].v.u2[1]; i < p->nconfig; ++i) {
265 config_plugin_value_t *cpv = p->cvlist + p->cvlist[i].v.u2[0];
266 for (; -1 != cpv->k_id; ++cpv) {
267 switch (cpv->k_id) {
268 case 0: /* vhostdb.<db> */
269 if (cpv->v.a->used) {
270 if (0 != mod_vhostdb_dbconf_setup(srv, cpv->v.a, &cpv->v.v))
271 return HANDLER_ERROR;
272 if (NULL != cpv->v.v)
273 cpv->vtype = T_CONFIG_LOCAL;
274 }
275 break;
276 default:/* should not happen */
277 break;
278 }
279 }
280 }
281
282 /* initialize p->defaults from global config context */
283 if (p->nconfig > 0 && p->cvlist->v.u2[1]) {
284 const config_plugin_value_t *cpv = p->cvlist + p->cvlist->v.u2[0];
285 if (-1 != cpv->k_id)
286 mod_vhostdb_merge_config(&p->defaults, cpv);
287 }
288
289 return HANDLER_GO_ON;
290 }
291
292
293 __attribute_cold__
294 int mod_vhostdb_mysql_plugin_init (plugin *p);
mod_vhostdb_mysql_plugin_init(plugin * p)295 int mod_vhostdb_mysql_plugin_init (plugin *p)
296 {
297 p->version = LIGHTTPD_VERSION_ID;
298 p->name = "vhostdb_mysql";
299
300 p->init = mod_vhostdb_init;
301 p->cleanup = mod_vhostdb_cleanup;
302 p->set_defaults = mod_vhostdb_set_defaults;
303
304 return 0;
305 }
306