[mod_cgi] fix pipe_cloexec() when no O_CLOEXEC
[lighttpd.git] / src / mod_mysql_vhost.c
blobb7a297a1ae46cc2a10b95e9f4234604cbd3c24aa
1 #include "first.h"
3 #include <unistd.h>
4 #include <stdio.h>
5 #include <errno.h>
6 #include <fcntl.h>
7 #include <string.h>
9 #ifdef HAVE_MYSQL
10 #include <mysql.h>
11 #endif
13 #include "plugin.h"
14 #include "log.h"
16 #include "stat_cache.h"
17 #ifdef DEBUG_MOD_MYSQL_VHOST
18 #define DEBUG
19 #endif
22 * Plugin for lighttpd to use MySQL
23 * for domain to directory lookups,
24 * i.e virtual hosts (vhosts).
26 * /ada@riksnet.se 2004-12-06
29 #ifdef HAVE_MYSQL
30 typedef struct {
31 MYSQL *mysql;
32 buffer *mysql_query;
34 buffer *mydb;
35 buffer *myuser;
36 buffer *mypass;
37 buffer *mysock;
39 buffer *hostname;
40 unsigned short port;
41 } plugin_config;
43 /* global plugin data */
44 typedef struct {
45 PLUGIN_DATA;
47 buffer *tmp_buf;
49 plugin_config **config_storage;
51 plugin_config conf;
52 } plugin_data;
54 /* per connection plugin data */
55 typedef struct {
56 buffer *server_name;
57 buffer *document_root;
58 } plugin_connection_data;
60 /* init the plugin data */
61 INIT_FUNC(mod_mysql_vhost_init) {
62 plugin_data *p;
64 p = calloc(1, sizeof(*p));
66 p->tmp_buf = buffer_init();
68 return p;
71 /* cleanup the plugin data */
72 SERVER_FUNC(mod_mysql_vhost_cleanup) {
73 plugin_data *p = p_d;
75 UNUSED(srv);
77 #ifdef DEBUG
78 log_error_write(srv, __FILE__, __LINE__, "ss",
79 "mod_mysql_vhost_cleanup", p ? "yes" : "NO");
80 #endif
81 if (!p) return HANDLER_GO_ON;
83 if (p->config_storage) {
84 size_t i;
85 for (i = 0; i < srv->config_context->used; i++) {
86 plugin_config *s = p->config_storage[i];
88 if (!s) continue;
90 mysql_close(s->mysql);
92 buffer_free(s->mysql_query);
93 buffer_free(s->mydb);
94 buffer_free(s->myuser);
95 buffer_free(s->mypass);
96 buffer_free(s->mysock);
97 buffer_free(s->hostname);
99 free(s);
101 free(p->config_storage);
103 buffer_free(p->tmp_buf);
105 free(p);
107 return HANDLER_GO_ON;
110 /* handle the plugin per connection data */
111 static void* mod_mysql_vhost_connection_data(server *srv, connection *con, void *p_d)
113 plugin_data *p = p_d;
114 plugin_connection_data *c = con->plugin_ctx[p->id];
116 UNUSED(srv);
118 #ifdef DEBUG
119 log_error_write(srv, __FILE__, __LINE__, "ss",
120 "mod_mysql_connection_data", c ? "old" : "NEW");
121 #endif
123 if (c) return c;
124 c = calloc(1, sizeof(*c));
126 c->server_name = buffer_init();
127 c->document_root = buffer_init();
129 return con->plugin_ctx[p->id] = c;
132 /* destroy the plugin per connection data */
133 CONNECTION_FUNC(mod_mysql_vhost_handle_connection_close) {
134 plugin_data *p = p_d;
135 plugin_connection_data *c = con->plugin_ctx[p->id];
137 UNUSED(srv);
139 #ifdef DEBUG
140 log_error_write(srv, __FILE__, __LINE__, "ss",
141 "mod_mysql_vhost_handle_connection_close", c ? "yes" : "NO");
142 #endif
144 if (!c) return HANDLER_GO_ON;
146 buffer_free(c->server_name);
147 buffer_free(c->document_root);
149 free(c);
151 con->plugin_ctx[p->id] = NULL;
152 return HANDLER_GO_ON;
155 /* set configuration values */
156 SERVER_FUNC(mod_mysql_vhost_set_defaults) {
157 plugin_data *p = p_d;
158 size_t i = 0;
160 config_values_t cv[] = {
161 { "mysql-vhost.db", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 0 */
162 { "mysql-vhost.user", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 1 */
163 { "mysql-vhost.pass", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 2 */
164 { "mysql-vhost.sock", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 3 */
165 { "mysql-vhost.sql", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 4 */
166 { "mysql-vhost.hostname", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 5 */
167 { "mysql-vhost.port", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 6 */
168 { NULL, NULL, T_CONFIG_UNSET, T_CONFIG_SCOPE_UNSET }
171 p->config_storage = calloc(1, srv->config_context->used * sizeof(plugin_config *));
173 for (i = 0; i < srv->config_context->used; i++) {
174 data_config const* config = (data_config const*)srv->config_context->data[i];
175 plugin_config *s;
177 s = calloc(1, sizeof(plugin_config));
178 s->mysql_query = buffer_init();
179 s->mydb = buffer_init();
180 s->myuser = buffer_init();
181 s->mypass = buffer_init();
182 s->mysock = buffer_init();
183 s->hostname = buffer_init();
184 s->port = 0; /* default port for mysql */
185 s->mysql = NULL;
187 cv[0].destination = s->mydb;
188 cv[1].destination = s->myuser;
189 cv[2].destination = s->mypass;
190 cv[3].destination = s->mysock;
191 cv[4].destination = s->mysql_query;
192 cv[5].destination = s->hostname;
193 cv[6].destination = &(s->port);
195 p->config_storage[i] = s;
197 if (config_insert_values_global(srv, config->value, cv, i == 0 ? T_CONFIG_SCOPE_SERVER : T_CONFIG_SCOPE_CONNECTION)) {
198 return HANDLER_ERROR;
201 /* required:
202 * - username
203 * - database
205 * optional:
206 * - password, default: empty
207 * - socket, default: mysql default
208 * - hostname, if set overrides socket
209 * - port, default: 3306
212 /* all have to be set */
213 if (!(buffer_string_is_empty(s->myuser) ||
214 buffer_string_is_empty(s->mydb))) {
215 my_bool reconnect = 1;
217 if (NULL == (s->mysql = mysql_init(NULL))) {
218 log_error_write(srv, __FILE__, __LINE__, "s", "mysql_init() failed, exiting...");
219 return HANDLER_ERROR;
222 #if MYSQL_VERSION_ID >= 50013
223 /* in mysql versions above 5.0.3 the reconnect flag is off by default */
224 mysql_options(s->mysql, MYSQL_OPT_RECONNECT, &reconnect);
225 #endif
227 #define FOO(x) (buffer_string_is_empty(s->x) ? NULL : s->x->ptr)
229 #if MYSQL_VERSION_ID >= 40100
230 /* CLIENT_MULTI_STATEMENTS first appeared in 4.1 */
231 if (!mysql_real_connect(s->mysql, FOO(hostname), FOO(myuser), FOO(mypass),
232 FOO(mydb), s->port, FOO(mysock), CLIENT_MULTI_STATEMENTS)) {
233 #else
234 if (!mysql_real_connect(s->mysql, FOO(hostname), FOO(myuser), FOO(mypass),
235 FOO(mydb), s->port, FOO(mysock), 0)) {
236 #endif
237 log_error_write(srv, __FILE__, __LINE__, "s", mysql_error(s->mysql));
238 return HANDLER_ERROR;
240 #undef FOO
242 fd_close_on_exec(s->mysql->net.fd);
246 return HANDLER_GO_ON;
249 #define PATCH(x) \
250 p->conf.x = s->x;
251 static int mod_mysql_vhost_patch_connection(server *srv, connection *con, plugin_data *p) {
252 size_t i, j;
253 plugin_config *s = p->config_storage[0];
255 PATCH(mysql_query);
256 #ifdef HAVE_MYSQL
257 PATCH(mysql);
258 #endif
260 /* skip the first, the global context */
261 for (i = 1; i < srv->config_context->used; i++) {
262 data_config *dc = (data_config *)srv->config_context->data[i];
263 s = p->config_storage[i];
265 /* condition didn't match */
266 if (!config_check_cond(srv, con, dc)) continue;
268 /* merge config */
269 for (j = 0; j < dc->value->used; j++) {
270 data_unset *du = dc->value->data[j];
272 if (buffer_is_equal_string(du->key, CONST_STR_LEN("mysql-vhost.sql"))) {
273 PATCH(mysql_query);
277 if (s->mysql) {
278 PATCH(mysql);
282 return 0;
284 #undef PATCH
287 /* handle document root request */
288 CONNECTION_FUNC(mod_mysql_vhost_handle_docroot) {
289 plugin_data *p = p_d;
290 plugin_connection_data *c;
291 stat_cache_entry *sce;
293 unsigned cols;
294 MYSQL_ROW row;
295 MYSQL_RES *result = NULL;
297 /* no host specified? */
298 if (buffer_string_is_empty(con->uri.authority)) return HANDLER_GO_ON;
300 mod_mysql_vhost_patch_connection(srv, con, p);
302 if (!p->conf.mysql) return HANDLER_GO_ON;
303 if (buffer_string_is_empty(p->conf.mysql_query)) return HANDLER_GO_ON;
305 /* sets up connection data if not done yet */
306 c = mod_mysql_vhost_connection_data(srv, con, p_d);
308 /* check if cached this connection */
309 if (buffer_is_equal(c->server_name, con->uri.authority)) goto GO_ON;
311 /* build and run SQL query */
312 buffer_string_set_length(p->tmp_buf, 0);
313 for (char *b = p->conf.mysql_query->ptr, *d; *b; b = d+1) {
314 if (NULL != (d = strchr(b, '?'))) {
315 /* escape the uri.authority */
316 unsigned long to_len;
317 buffer_append_string_len(p->tmp_buf, b, (size_t)(d - b));
318 buffer_string_prepare_append(p->tmp_buf, buffer_string_length(con->uri.authority) * 2);
319 to_len = mysql_real_escape_string(p->conf.mysql,
320 p->tmp_buf->ptr + buffer_string_length(p->tmp_buf),
321 CONST_BUF_LEN(con->uri.authority));
322 if ((unsigned long)~0 == to_len) goto ERR500;
323 buffer_commit(p->tmp_buf, to_len);
324 } else {
325 d = p->conf.mysql_query->ptr + buffer_string_length(p->conf.mysql_query);
326 buffer_append_string_len(p->tmp_buf, b, (size_t)(d - b));
327 break;
330 if (mysql_real_query(p->conf.mysql, CONST_BUF_LEN(p->tmp_buf))) {
331 log_error_write(srv, __FILE__, __LINE__, "s", mysql_error(p->conf.mysql));
332 goto ERR500;
334 result = mysql_store_result(p->conf.mysql);
335 cols = mysql_num_fields(result);
336 row = mysql_fetch_row(result);
337 if (!row || cols < 1) {
338 /* no such virtual host */
339 mysql_free_result(result);
340 #if MYSQL_VERSION_ID >= 40100
341 while (mysql_next_result(p->conf.mysql) == 0);
342 #endif
343 return HANDLER_GO_ON;
346 /* sanity check that really is a directory */
347 buffer_copy_string(p->tmp_buf, row[0]);
348 buffer_append_slash(p->tmp_buf);
350 if (HANDLER_ERROR == stat_cache_get_entry(srv, con, p->tmp_buf, &sce)) {
351 log_error_write(srv, __FILE__, __LINE__, "sb", strerror(errno), p->tmp_buf);
352 goto ERR500;
354 if (!S_ISDIR(sce->st.st_mode)) {
355 log_error_write(srv, __FILE__, __LINE__, "sb", "Not a directory", p->tmp_buf);
356 goto ERR500;
359 /* cache the data */
360 buffer_copy_buffer(c->server_name, con->uri.authority);
361 buffer_copy_buffer(c->document_root, p->tmp_buf);
363 mysql_free_result(result);
364 #if MYSQL_VERSION_ID >= 40100
365 while (mysql_next_result(p->conf.mysql) == 0);
366 #endif
368 /* fix virtual server and docroot */
369 GO_ON:
370 buffer_copy_buffer(con->server_name, c->server_name);
371 buffer_copy_buffer(con->physical.doc_root, c->document_root);
373 #ifdef DEBUG
374 log_error_write(srv, __FILE__, __LINE__, "sbb",
375 result ? "NOT CACHED" : "cached",
376 con->server_name, con->physical.doc_root);
377 #endif
378 return HANDLER_GO_ON;
380 ERR500:
381 if (result) mysql_free_result(result);
382 #if MYSQL_VERSION_ID >= 40100
383 while (mysql_next_result(p->conf.mysql) == 0);
384 #endif
385 con->http_status = 500; /* Internal Error */
386 con->mode = DIRECT;
387 return HANDLER_FINISHED;
390 /* this function is called at dlopen() time and inits the callbacks */
391 int mod_mysql_vhost_plugin_init(plugin *p);
392 int mod_mysql_vhost_plugin_init(plugin *p) {
393 p->version = LIGHTTPD_VERSION_ID;
394 p->name = buffer_init_string("mysql_vhost");
396 p->init = mod_mysql_vhost_init;
397 p->cleanup = mod_mysql_vhost_cleanup;
398 p->connection_reset = mod_mysql_vhost_handle_connection_close;
400 p->set_defaults = mod_mysql_vhost_set_defaults;
401 p->handle_docroot = mod_mysql_vhost_handle_docroot;
403 return 0;
405 #else
406 /* we don't have mysql support, this plugin does nothing */
407 int mod_mysql_vhost_plugin_init(plugin *p);
408 int mod_mysql_vhost_plugin_init(plugin *p) {
409 p->version = LIGHTTPD_VERSION_ID;
410 p->name = buffer_init_string("mysql_vhost");
412 return 0;
414 #endif