[core] consolidate backend network write handlers
[lighttpd.git] / src / mod_mysql_vhost.c
blob3102db52c4bf7f4af7ecf690f820d16664d1af77
1 #include "first.h"
3 #include <unistd.h>
4 #include <errno.h>
5 #include <string.h>
7 #include <mysql.h>
9 #include "plugin.h"
10 #include "fdevent.h"
11 #include "log.h"
13 #include "stat_cache.h"
16 * Plugin for lighttpd to use MySQL
17 * for domain to directory lookups,
18 * i.e virtual hosts (vhosts).
20 * /ada@riksnet.se 2004-12-06
23 typedef struct {
24 MYSQL *mysql;
25 buffer *mysql_query;
27 buffer *mydb;
28 buffer *myuser;
29 buffer *mypass;
30 buffer *mysock;
32 buffer *hostname;
33 unsigned short port;
34 } plugin_config;
36 /* global plugin data */
37 typedef struct {
38 PLUGIN_DATA;
40 buffer *tmp_buf;
42 plugin_config **config_storage;
44 plugin_config conf;
45 } plugin_data;
47 /* per connection plugin data */
48 typedef struct {
49 buffer *server_name;
50 buffer *document_root;
51 } plugin_connection_data;
53 /* init the plugin data */
54 INIT_FUNC(mod_mysql_vhost_init) {
55 plugin_data *p;
57 p = calloc(1, sizeof(*p));
59 p->tmp_buf = buffer_init();
61 return p;
64 /* cleanup the plugin data */
65 SERVER_FUNC(mod_mysql_vhost_cleanup) {
66 plugin_data *p = p_d;
68 UNUSED(srv);
70 if (!p) return HANDLER_GO_ON;
72 if (p->config_storage) {
73 size_t i;
74 for (i = 0; i < srv->config_context->used; i++) {
75 plugin_config *s = p->config_storage[i];
77 if (!s) continue;
79 mysql_close(s->mysql);
81 buffer_free(s->mysql_query);
82 buffer_free(s->mydb);
83 buffer_free(s->myuser);
84 buffer_free(s->mypass);
85 buffer_free(s->mysock);
86 buffer_free(s->hostname);
88 free(s);
90 free(p->config_storage);
92 buffer_free(p->tmp_buf);
94 free(p);
96 return HANDLER_GO_ON;
99 /* handle the plugin per connection data */
100 static void* mod_mysql_vhost_connection_data(server *srv, connection *con, void *p_d)
102 plugin_data *p = p_d;
103 plugin_connection_data *c = con->plugin_ctx[p->id];
105 UNUSED(srv);
107 if (c) return c;
108 c = calloc(1, sizeof(*c));
110 c->server_name = buffer_init();
111 c->document_root = buffer_init();
113 return con->plugin_ctx[p->id] = c;
116 /* destroy the plugin per connection data */
117 CONNECTION_FUNC(mod_mysql_vhost_handle_connection_reset) {
118 plugin_data *p = p_d;
119 plugin_connection_data *c = con->plugin_ctx[p->id];
121 UNUSED(srv);
123 if (!c) return HANDLER_GO_ON;
125 buffer_free(c->server_name);
126 buffer_free(c->document_root);
128 free(c);
130 con->plugin_ctx[p->id] = NULL;
131 return HANDLER_GO_ON;
134 /* set configuration values */
135 SERVER_FUNC(mod_mysql_vhost_set_defaults) {
136 plugin_data *p = p_d;
137 size_t i = 0;
139 config_values_t cv[] = {
140 { "mysql-vhost.db", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 0 */
141 { "mysql-vhost.user", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 1 */
142 { "mysql-vhost.pass", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 2 */
143 { "mysql-vhost.sock", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 3 */
144 { "mysql-vhost.sql", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 4 */
145 { "mysql-vhost.hostname", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 5 */
146 { "mysql-vhost.port", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 6 */
147 { NULL, NULL, T_CONFIG_UNSET, T_CONFIG_SCOPE_UNSET }
150 p->config_storage = calloc(1, srv->config_context->used * sizeof(plugin_config *));
152 for (i = 0; i < srv->config_context->used; i++) {
153 data_config const* config = (data_config const*)srv->config_context->data[i];
154 plugin_config *s;
156 s = calloc(1, sizeof(plugin_config));
157 s->mysql_query = buffer_init();
158 s->mydb = buffer_init();
159 s->myuser = buffer_init();
160 s->mypass = buffer_init();
161 s->mysock = buffer_init();
162 s->hostname = buffer_init();
163 s->port = 0; /* default port for mysql */
164 s->mysql = NULL;
166 cv[0].destination = s->mydb;
167 cv[1].destination = s->myuser;
168 cv[2].destination = s->mypass;
169 cv[3].destination = s->mysock;
170 cv[4].destination = s->mysql_query;
171 cv[5].destination = s->hostname;
172 cv[6].destination = &(s->port);
174 p->config_storage[i] = s;
176 if (config_insert_values_global(srv, config->value, cv, i == 0 ? T_CONFIG_SCOPE_SERVER : T_CONFIG_SCOPE_CONNECTION)) {
177 return HANDLER_ERROR;
180 /* required:
181 * - username
182 * - database
184 * optional:
185 * - password, default: empty
186 * - socket, default: mysql default
187 * - hostname, if set overrides socket
188 * - port, default: 3306
191 /* all have to be set */
192 if (!(buffer_string_is_empty(s->myuser) ||
193 buffer_string_is_empty(s->mydb))) {
194 my_bool reconnect = 1;
196 if (NULL == (s->mysql = mysql_init(NULL))) {
197 log_error_write(srv, __FILE__, __LINE__, "s", "mysql_init() failed, exiting...");
198 return HANDLER_ERROR;
201 #if MYSQL_VERSION_ID >= 50013
202 /* in mysql versions above 5.0.3 the reconnect flag is off by default */
203 mysql_options(s->mysql, MYSQL_OPT_RECONNECT, &reconnect);
204 #endif
206 #define FOO(x) (buffer_string_is_empty(s->x) ? NULL : s->x->ptr)
208 #if MYSQL_VERSION_ID >= 40100
209 /* CLIENT_MULTI_STATEMENTS first appeared in 4.1 */
210 if (!mysql_real_connect(s->mysql, FOO(hostname), FOO(myuser), FOO(mypass),
211 FOO(mydb), s->port, FOO(mysock), CLIENT_MULTI_STATEMENTS)) {
212 #else
213 if (!mysql_real_connect(s->mysql, FOO(hostname), FOO(myuser), FOO(mypass),
214 FOO(mydb), s->port, FOO(mysock), 0)) {
215 #endif
216 log_error_write(srv, __FILE__, __LINE__, "s", mysql_error(s->mysql));
217 return HANDLER_ERROR;
219 #undef FOO
221 fdevent_setfd_cloexec(s->mysql->net.fd);
225 return HANDLER_GO_ON;
228 #define PATCH(x) \
229 p->conf.x = s->x;
230 static int mod_mysql_vhost_patch_connection(server *srv, connection *con, plugin_data *p) {
231 size_t i, j;
232 plugin_config *s = p->config_storage[0];
234 PATCH(mysql_query);
235 PATCH(mysql);
237 /* skip the first, the global context */
238 for (i = 1; i < srv->config_context->used; i++) {
239 data_config *dc = (data_config *)srv->config_context->data[i];
240 s = p->config_storage[i];
242 /* condition didn't match */
243 if (!config_check_cond(srv, con, dc)) continue;
245 /* merge config */
246 for (j = 0; j < dc->value->used; j++) {
247 data_unset *du = dc->value->data[j];
249 if (buffer_is_equal_string(du->key, CONST_STR_LEN("mysql-vhost.sql"))) {
250 PATCH(mysql_query);
254 if (s->mysql) {
255 PATCH(mysql);
259 return 0;
261 #undef PATCH
264 /* handle document root request */
265 CONNECTION_FUNC(mod_mysql_vhost_handle_docroot) {
266 plugin_data *p = p_d;
267 plugin_connection_data *c;
268 stat_cache_entry *sce;
270 unsigned cols;
271 MYSQL_ROW row;
272 MYSQL_RES *result = NULL;
274 /* no host specified? */
275 if (buffer_string_is_empty(con->uri.authority)) return HANDLER_GO_ON;
277 mod_mysql_vhost_patch_connection(srv, con, p);
279 if (!p->conf.mysql) return HANDLER_GO_ON;
280 if (buffer_string_is_empty(p->conf.mysql_query)) return HANDLER_GO_ON;
282 /* sets up connection data if not done yet */
283 c = mod_mysql_vhost_connection_data(srv, con, p_d);
285 /* check if cached this connection */
286 if (buffer_is_equal(c->server_name, con->uri.authority)) goto GO_ON;
288 /* build and run SQL query */
289 buffer_string_set_length(p->tmp_buf, 0);
290 for (char *b = p->conf.mysql_query->ptr, *d; *b; b = d+1) {
291 if (NULL != (d = strchr(b, '?'))) {
292 /* escape the uri.authority */
293 unsigned long to_len;
294 buffer_append_string_len(p->tmp_buf, b, (size_t)(d - b));
295 buffer_string_prepare_append(p->tmp_buf, buffer_string_length(con->uri.authority) * 2);
296 to_len = mysql_real_escape_string(p->conf.mysql,
297 p->tmp_buf->ptr + buffer_string_length(p->tmp_buf),
298 CONST_BUF_LEN(con->uri.authority));
299 if ((unsigned long)~0 == to_len) goto ERR500;
300 buffer_commit(p->tmp_buf, to_len);
301 } else {
302 d = p->conf.mysql_query->ptr + buffer_string_length(p->conf.mysql_query);
303 buffer_append_string_len(p->tmp_buf, b, (size_t)(d - b));
304 break;
307 if (mysql_real_query(p->conf.mysql, CONST_BUF_LEN(p->tmp_buf))) {
308 log_error_write(srv, __FILE__, __LINE__, "s", mysql_error(p->conf.mysql));
309 goto ERR500;
311 result = mysql_store_result(p->conf.mysql);
312 cols = mysql_num_fields(result);
313 row = mysql_fetch_row(result);
314 if (!row || cols < 1) {
315 /* no such virtual host */
316 mysql_free_result(result);
317 #if MYSQL_VERSION_ID >= 40100
318 while (mysql_next_result(p->conf.mysql) == 0);
319 #endif
320 return HANDLER_GO_ON;
323 /* sanity check that really is a directory */
324 buffer_copy_string(p->tmp_buf, row[0]);
325 buffer_append_slash(p->tmp_buf);
327 if (HANDLER_ERROR == stat_cache_get_entry(srv, con, p->tmp_buf, &sce)) {
328 log_error_write(srv, __FILE__, __LINE__, "sb", strerror(errno), p->tmp_buf);
329 goto ERR500;
331 if (!S_ISDIR(sce->st.st_mode)) {
332 log_error_write(srv, __FILE__, __LINE__, "sb", "Not a directory", p->tmp_buf);
333 goto ERR500;
336 /* cache the data */
337 buffer_copy_buffer(c->server_name, con->uri.authority);
338 buffer_copy_buffer(c->document_root, p->tmp_buf);
340 mysql_free_result(result);
341 #if MYSQL_VERSION_ID >= 40100
342 while (mysql_next_result(p->conf.mysql) == 0);
343 #endif
345 /* fix virtual server and docroot */
346 GO_ON:
347 buffer_copy_buffer(con->server_name, c->server_name);
348 buffer_copy_buffer(con->physical.doc_root, c->document_root);
350 return HANDLER_GO_ON;
352 ERR500:
353 if (result) mysql_free_result(result);
354 #if MYSQL_VERSION_ID >= 40100
355 while (mysql_next_result(p->conf.mysql) == 0);
356 #endif
357 con->http_status = 500; /* Internal Error */
358 con->mode = DIRECT;
359 return HANDLER_FINISHED;
362 /* this function is called at dlopen() time and inits the callbacks */
363 int mod_mysql_vhost_plugin_init(plugin *p);
364 int mod_mysql_vhost_plugin_init(plugin *p) {
365 p->version = LIGHTTPD_VERSION_ID;
366 p->name = buffer_init_string("mysql_vhost");
368 p->init = mod_mysql_vhost_init;
369 p->cleanup = mod_mysql_vhost_cleanup;
370 p->connection_reset = mod_mysql_vhost_handle_connection_reset;
372 p->set_defaults = mod_mysql_vhost_set_defaults;
373 p->handle_docroot = mod_mysql_vhost_handle_docroot;
375 return 0;