[mod_cgi] skip local-redir handling if to self (fixes #2779, #2108)
[lighttpd.git] / src / mod_mysql_vhost.c
blob29a241ef04fad27661f6509a550e9a553af68d41
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 #include <mysql.h>
11 #include "plugin.h"
12 #include "log.h"
14 #include "stat_cache.h"
15 #ifdef DEBUG_MOD_MYSQL_VHOST
16 #define DEBUG
17 #endif
20 * Plugin for lighttpd to use MySQL
21 * for domain to directory lookups,
22 * i.e virtual hosts (vhosts).
24 * /ada@riksnet.se 2004-12-06
27 typedef struct {
28 MYSQL *mysql;
29 buffer *mysql_query;
31 buffer *mydb;
32 buffer *myuser;
33 buffer *mypass;
34 buffer *mysock;
36 buffer *hostname;
37 unsigned short port;
38 } plugin_config;
40 /* global plugin data */
41 typedef struct {
42 PLUGIN_DATA;
44 buffer *tmp_buf;
46 plugin_config **config_storage;
48 plugin_config conf;
49 } plugin_data;
51 /* per connection plugin data */
52 typedef struct {
53 buffer *server_name;
54 buffer *document_root;
55 } plugin_connection_data;
57 /* init the plugin data */
58 INIT_FUNC(mod_mysql_vhost_init) {
59 plugin_data *p;
61 p = calloc(1, sizeof(*p));
63 p->tmp_buf = buffer_init();
65 return p;
68 /* cleanup the plugin data */
69 SERVER_FUNC(mod_mysql_vhost_cleanup) {
70 plugin_data *p = p_d;
72 UNUSED(srv);
74 #ifdef DEBUG
75 log_error_write(srv, __FILE__, __LINE__, "ss",
76 "mod_mysql_vhost_cleanup", p ? "yes" : "NO");
77 #endif
78 if (!p) return HANDLER_GO_ON;
80 if (p->config_storage) {
81 size_t i;
82 for (i = 0; i < srv->config_context->used; i++) {
83 plugin_config *s = p->config_storage[i];
85 if (!s) continue;
87 mysql_close(s->mysql);
89 buffer_free(s->mysql_query);
90 buffer_free(s->mydb);
91 buffer_free(s->myuser);
92 buffer_free(s->mypass);
93 buffer_free(s->mysock);
94 buffer_free(s->hostname);
96 free(s);
98 free(p->config_storage);
100 buffer_free(p->tmp_buf);
102 free(p);
104 return HANDLER_GO_ON;
107 /* handle the plugin per connection data */
108 static void* mod_mysql_vhost_connection_data(server *srv, connection *con, void *p_d)
110 plugin_data *p = p_d;
111 plugin_connection_data *c = con->plugin_ctx[p->id];
113 UNUSED(srv);
115 #ifdef DEBUG
116 log_error_write(srv, __FILE__, __LINE__, "ss",
117 "mod_mysql_connection_data", c ? "old" : "NEW");
118 #endif
120 if (c) return c;
121 c = calloc(1, sizeof(*c));
123 c->server_name = buffer_init();
124 c->document_root = buffer_init();
126 return con->plugin_ctx[p->id] = c;
129 /* destroy the plugin per connection data */
130 CONNECTION_FUNC(mod_mysql_vhost_handle_connection_close) {
131 plugin_data *p = p_d;
132 plugin_connection_data *c = con->plugin_ctx[p->id];
134 UNUSED(srv);
136 #ifdef DEBUG
137 log_error_write(srv, __FILE__, __LINE__, "ss",
138 "mod_mysql_vhost_handle_connection_close", c ? "yes" : "NO");
139 #endif
141 if (!c) return HANDLER_GO_ON;
143 buffer_free(c->server_name);
144 buffer_free(c->document_root);
146 free(c);
148 con->plugin_ctx[p->id] = NULL;
149 return HANDLER_GO_ON;
152 /* set configuration values */
153 SERVER_FUNC(mod_mysql_vhost_set_defaults) {
154 plugin_data *p = p_d;
155 size_t i = 0;
157 config_values_t cv[] = {
158 { "mysql-vhost.db", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 0 */
159 { "mysql-vhost.user", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 1 */
160 { "mysql-vhost.pass", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 2 */
161 { "mysql-vhost.sock", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 3 */
162 { "mysql-vhost.sql", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 4 */
163 { "mysql-vhost.hostname", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 5 */
164 { "mysql-vhost.port", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 6 */
165 { NULL, NULL, T_CONFIG_UNSET, T_CONFIG_SCOPE_UNSET }
168 p->config_storage = calloc(1, srv->config_context->used * sizeof(plugin_config *));
170 for (i = 0; i < srv->config_context->used; i++) {
171 data_config const* config = (data_config const*)srv->config_context->data[i];
172 plugin_config *s;
174 s = calloc(1, sizeof(plugin_config));
175 s->mysql_query = buffer_init();
176 s->mydb = buffer_init();
177 s->myuser = buffer_init();
178 s->mypass = buffer_init();
179 s->mysock = buffer_init();
180 s->hostname = buffer_init();
181 s->port = 0; /* default port for mysql */
182 s->mysql = NULL;
184 cv[0].destination = s->mydb;
185 cv[1].destination = s->myuser;
186 cv[2].destination = s->mypass;
187 cv[3].destination = s->mysock;
188 cv[4].destination = s->mysql_query;
189 cv[5].destination = s->hostname;
190 cv[6].destination = &(s->port);
192 p->config_storage[i] = s;
194 if (config_insert_values_global(srv, config->value, cv, i == 0 ? T_CONFIG_SCOPE_SERVER : T_CONFIG_SCOPE_CONNECTION)) {
195 return HANDLER_ERROR;
198 /* required:
199 * - username
200 * - database
202 * optional:
203 * - password, default: empty
204 * - socket, default: mysql default
205 * - hostname, if set overrides socket
206 * - port, default: 3306
209 /* all have to be set */
210 if (!(buffer_string_is_empty(s->myuser) ||
211 buffer_string_is_empty(s->mydb))) {
212 my_bool reconnect = 1;
214 if (NULL == (s->mysql = mysql_init(NULL))) {
215 log_error_write(srv, __FILE__, __LINE__, "s", "mysql_init() failed, exiting...");
216 return HANDLER_ERROR;
219 #if MYSQL_VERSION_ID >= 50013
220 /* in mysql versions above 5.0.3 the reconnect flag is off by default */
221 mysql_options(s->mysql, MYSQL_OPT_RECONNECT, &reconnect);
222 #endif
224 #define FOO(x) (buffer_string_is_empty(s->x) ? NULL : s->x->ptr)
226 #if MYSQL_VERSION_ID >= 40100
227 /* CLIENT_MULTI_STATEMENTS first appeared in 4.1 */
228 if (!mysql_real_connect(s->mysql, FOO(hostname), FOO(myuser), FOO(mypass),
229 FOO(mydb), s->port, FOO(mysock), CLIENT_MULTI_STATEMENTS)) {
230 #else
231 if (!mysql_real_connect(s->mysql, FOO(hostname), FOO(myuser), FOO(mypass),
232 FOO(mydb), s->port, FOO(mysock), 0)) {
233 #endif
234 log_error_write(srv, __FILE__, __LINE__, "s", mysql_error(s->mysql));
235 return HANDLER_ERROR;
237 #undef FOO
239 fd_close_on_exec(s->mysql->net.fd);
243 return HANDLER_GO_ON;
246 #define PATCH(x) \
247 p->conf.x = s->x;
248 static int mod_mysql_vhost_patch_connection(server *srv, connection *con, plugin_data *p) {
249 size_t i, j;
250 plugin_config *s = p->config_storage[0];
252 PATCH(mysql_query);
253 PATCH(mysql);
255 /* skip the first, the global context */
256 for (i = 1; i < srv->config_context->used; i++) {
257 data_config *dc = (data_config *)srv->config_context->data[i];
258 s = p->config_storage[i];
260 /* condition didn't match */
261 if (!config_check_cond(srv, con, dc)) continue;
263 /* merge config */
264 for (j = 0; j < dc->value->used; j++) {
265 data_unset *du = dc->value->data[j];
267 if (buffer_is_equal_string(du->key, CONST_STR_LEN("mysql-vhost.sql"))) {
268 PATCH(mysql_query);
272 if (s->mysql) {
273 PATCH(mysql);
277 return 0;
279 #undef PATCH
282 /* handle document root request */
283 CONNECTION_FUNC(mod_mysql_vhost_handle_docroot) {
284 plugin_data *p = p_d;
285 plugin_connection_data *c;
286 stat_cache_entry *sce;
288 unsigned cols;
289 MYSQL_ROW row;
290 MYSQL_RES *result = NULL;
292 /* no host specified? */
293 if (buffer_string_is_empty(con->uri.authority)) return HANDLER_GO_ON;
295 mod_mysql_vhost_patch_connection(srv, con, p);
297 if (!p->conf.mysql) return HANDLER_GO_ON;
298 if (buffer_string_is_empty(p->conf.mysql_query)) return HANDLER_GO_ON;
300 /* sets up connection data if not done yet */
301 c = mod_mysql_vhost_connection_data(srv, con, p_d);
303 /* check if cached this connection */
304 if (buffer_is_equal(c->server_name, con->uri.authority)) goto GO_ON;
306 /* build and run SQL query */
307 buffer_string_set_length(p->tmp_buf, 0);
308 for (char *b = p->conf.mysql_query->ptr, *d; *b; b = d+1) {
309 if (NULL != (d = strchr(b, '?'))) {
310 /* escape the uri.authority */
311 unsigned long to_len;
312 buffer_append_string_len(p->tmp_buf, b, (size_t)(d - b));
313 buffer_string_prepare_append(p->tmp_buf, buffer_string_length(con->uri.authority) * 2);
314 to_len = mysql_real_escape_string(p->conf.mysql,
315 p->tmp_buf->ptr + buffer_string_length(p->tmp_buf),
316 CONST_BUF_LEN(con->uri.authority));
317 if ((unsigned long)~0 == to_len) goto ERR500;
318 buffer_commit(p->tmp_buf, to_len);
319 } else {
320 d = p->conf.mysql_query->ptr + buffer_string_length(p->conf.mysql_query);
321 buffer_append_string_len(p->tmp_buf, b, (size_t)(d - b));
322 break;
325 if (mysql_real_query(p->conf.mysql, CONST_BUF_LEN(p->tmp_buf))) {
326 log_error_write(srv, __FILE__, __LINE__, "s", mysql_error(p->conf.mysql));
327 goto ERR500;
329 result = mysql_store_result(p->conf.mysql);
330 cols = mysql_num_fields(result);
331 row = mysql_fetch_row(result);
332 if (!row || cols < 1) {
333 /* no such virtual host */
334 mysql_free_result(result);
335 #if MYSQL_VERSION_ID >= 40100
336 while (mysql_next_result(p->conf.mysql) == 0);
337 #endif
338 return HANDLER_GO_ON;
341 /* sanity check that really is a directory */
342 buffer_copy_string(p->tmp_buf, row[0]);
343 buffer_append_slash(p->tmp_buf);
345 if (HANDLER_ERROR == stat_cache_get_entry(srv, con, p->tmp_buf, &sce)) {
346 log_error_write(srv, __FILE__, __LINE__, "sb", strerror(errno), p->tmp_buf);
347 goto ERR500;
349 if (!S_ISDIR(sce->st.st_mode)) {
350 log_error_write(srv, __FILE__, __LINE__, "sb", "Not a directory", p->tmp_buf);
351 goto ERR500;
354 /* cache the data */
355 buffer_copy_buffer(c->server_name, con->uri.authority);
356 buffer_copy_buffer(c->document_root, p->tmp_buf);
358 mysql_free_result(result);
359 #if MYSQL_VERSION_ID >= 40100
360 while (mysql_next_result(p->conf.mysql) == 0);
361 #endif
363 /* fix virtual server and docroot */
364 GO_ON:
365 buffer_copy_buffer(con->server_name, c->server_name);
366 buffer_copy_buffer(con->physical.doc_root, c->document_root);
368 #ifdef DEBUG
369 log_error_write(srv, __FILE__, __LINE__, "sbb",
370 result ? "NOT CACHED" : "cached",
371 con->server_name, con->physical.doc_root);
372 #endif
373 return HANDLER_GO_ON;
375 ERR500:
376 if (result) mysql_free_result(result);
377 #if MYSQL_VERSION_ID >= 40100
378 while (mysql_next_result(p->conf.mysql) == 0);
379 #endif
380 con->http_status = 500; /* Internal Error */
381 con->mode = DIRECT;
382 return HANDLER_FINISHED;
385 /* this function is called at dlopen() time and inits the callbacks */
386 int mod_mysql_vhost_plugin_init(plugin *p);
387 int mod_mysql_vhost_plugin_init(plugin *p) {
388 p->version = LIGHTTPD_VERSION_ID;
389 p->name = buffer_init_string("mysql_vhost");
391 p->init = mod_mysql_vhost_init;
392 p->cleanup = mod_mysql_vhost_cleanup;
393 p->connection_reset = mod_mysql_vhost_handle_connection_close;
395 p->set_defaults = mod_mysql_vhost_set_defaults;
396 p->handle_docroot = mod_mysql_vhost_handle_docroot;
398 return 0;