1
|
#include <unistd.h>
|
2
|
#include <stdio.h>
|
3
|
#include <errno.h>
|
4
|
#include <fcntl.h>
|
5
|
#include <strings.h>
|
6
|
|
7
|
#ifdef HAVE_CONFIG_H
|
8
|
#include "config.h"
|
9
|
#endif
|
10
|
|
11
|
#ifdef HAVE_MYSQL
|
12
|
#include <mysql.h>
|
13
|
#endif
|
14
|
|
15
|
#include "plugin.h"
|
16
|
#include "log.h"
|
17
|
|
18
|
#include "stat_cache.h"
|
19
|
#ifdef DEBUG_MOD_MYSQL_VHOST
|
20
|
#define DEBUG
|
21
|
#endif
|
22
|
|
23
|
/*
|
24
|
* Plugin for lighttpd to use MySQL
|
25
|
* for domain to directory lookups,
|
26
|
* i.e virtual hosts (vhosts).
|
27
|
*
|
28
|
* Optionally sets fcgi_offset and fcgi_arg
|
29
|
* in preparation for fcgi.c to handle
|
30
|
* per-user fcgi chroot jails.
|
31
|
*
|
32
|
* /ada@riksnet.se 2004-12-06
|
33
|
*/
|
34
|
|
35
|
#ifdef HAVE_MYSQL
|
36
|
typedef struct {
|
37
|
MYSQL *mysql;
|
38
|
|
39
|
buffer *mydb;
|
40
|
buffer *myuser;
|
41
|
buffer *mypass;
|
42
|
buffer *mysock;
|
43
|
|
44
|
buffer *hostname;
|
45
|
unsigned short port;
|
46
|
|
47
|
buffer *mysql_pre;
|
48
|
buffer *mysql_mid;
|
49
|
buffer *mysql_post;
|
50
|
} plugin_config;
|
51
|
|
52
|
/* global plugin data */
|
53
|
typedef struct {
|
54
|
PLUGIN_DATA;
|
55
|
|
56
|
buffer *tmp_buf;
|
57
|
|
58
|
plugin_config **config_storage;
|
59
|
|
60
|
plugin_config conf;
|
61
|
} plugin_data;
|
62
|
|
63
|
/* per connection plugin data */
|
64
|
typedef struct {
|
65
|
buffer *server_name;
|
66
|
buffer *document_root;
|
67
|
buffer *fcgi_arg;
|
68
|
unsigned fcgi_offset;
|
69
|
} plugin_connection_data;
|
70
|
|
71
|
/* init the plugin data */
|
72
|
INIT_FUNC(mod_mysql_vhost_init) {
|
73
|
plugin_data *p;
|
74
|
|
75
|
p = calloc(1, sizeof(*p));
|
76
|
|
77
|
p->tmp_buf = buffer_init();
|
78
|
|
79
|
return p;
|
80
|
}
|
81
|
|
82
|
/* cleanup the plugin data */
|
83
|
SERVER_FUNC(mod_mysql_vhost_cleanup) {
|
84
|
plugin_data *p = p_d;
|
85
|
|
86
|
UNUSED(srv);
|
87
|
|
88
|
#ifdef DEBUG
|
89
|
log_error_write(srv, __FILE__, __LINE__, "ss",
|
90
|
"mod_mysql_vhost_cleanup", p ? "yes" : "NO");
|
91
|
#endif
|
92
|
if (!p) return HANDLER_GO_ON;
|
93
|
|
94
|
if (p->config_storage) {
|
95
|
size_t i;
|
96
|
for (i = 0; i < srv->config_context->used; i++) {
|
97
|
plugin_config *s = p->config_storage[i];
|
98
|
|
99
|
if (!s) continue;
|
100
|
|
101
|
mysql_close(s->mysql);
|
102
|
|
103
|
buffer_free(s->mydb);
|
104
|
buffer_free(s->myuser);
|
105
|
buffer_free(s->mypass);
|
106
|
buffer_free(s->mysock);
|
107
|
buffer_free(s->mysql_pre);
|
108
|
buffer_free(s->mysql_mid);
|
109
|
buffer_free(s->mysql_post);
|
110
|
buffer_free(s->hostname);
|
111
|
|
112
|
free(s);
|
113
|
}
|
114
|
free(p->config_storage);
|
115
|
}
|
116
|
buffer_free(p->tmp_buf);
|
117
|
|
118
|
free(p);
|
119
|
|
120
|
return HANDLER_GO_ON;
|
121
|
}
|
122
|
|
123
|
/* handle the plugin per connection data */
|
124
|
static void* mod_mysql_vhost_connection_data(server *srv, connection *con, void *p_d)
|
125
|
{
|
126
|
plugin_data *p = p_d;
|
127
|
plugin_connection_data *c = con->plugin_ctx[p->id];
|
128
|
|
129
|
UNUSED(srv);
|
130
|
|
131
|
#ifdef DEBUG
|
132
|
log_error_write(srv, __FILE__, __LINE__, "ss",
|
133
|
"mod_mysql_connection_data", c ? "old" : "NEW");
|
134
|
#endif
|
135
|
|
136
|
if (c) return c;
|
137
|
c = calloc(1, sizeof(*c));
|
138
|
|
139
|
c->server_name = buffer_init();
|
140
|
c->document_root = buffer_init();
|
141
|
c->fcgi_arg = buffer_init();
|
142
|
c->fcgi_offset = 0;
|
143
|
|
144
|
return con->plugin_ctx[p->id] = c;
|
145
|
}
|
146
|
|
147
|
/* destroy the plugin per connection data */
|
148
|
CONNECTION_FUNC(mod_mysql_vhost_handle_connection_close) {
|
149
|
plugin_data *p = p_d;
|
150
|
plugin_connection_data *c = con->plugin_ctx[p->id];
|
151
|
|
152
|
UNUSED(srv);
|
153
|
|
154
|
#ifdef DEBUG
|
155
|
log_error_write(srv, __FILE__, __LINE__, "ss",
|
156
|
"mod_mysql_vhost_handle_connection_close", c ? "yes" : "NO");
|
157
|
#endif
|
158
|
|
159
|
if (!c) return HANDLER_GO_ON;
|
160
|
|
161
|
buffer_free(c->server_name);
|
162
|
buffer_free(c->document_root);
|
163
|
buffer_free(c->fcgi_arg);
|
164
|
c->fcgi_offset = 0;
|
165
|
|
166
|
free(c);
|
167
|
|
168
|
con->plugin_ctx[p->id] = NULL;
|
169
|
return HANDLER_GO_ON;
|
170
|
}
|
171
|
|
172
|
/* set configuration values */
|
173
|
SERVER_FUNC(mod_mysql_vhost_set_defaults) {
|
174
|
plugin_data *p = p_d;
|
175
|
|
176
|
char *qmarkA;
|
177
|
char *qmarkB;
|
178
|
size_t i = 0;
|
179
|
|
180
|
config_values_t cv[] = {
|
181
|
{ "mysql-vhost.db", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_SERVER },
|
182
|
{ "mysql-vhost.user", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_SERVER },
|
183
|
{ "mysql-vhost.pass", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_SERVER },
|
184
|
{ "mysql-vhost.sock", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_SERVER },
|
185
|
{ "mysql-vhost.sql", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_SERVER },
|
186
|
{ "mysql-vhost.hostname", NULL, T_CONFIG_STRING,T_CONFIG_SCOPE_SERVER },
|
187
|
{ "mysql-vhost.port", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_SERVER },
|
188
|
{ NULL, NULL, T_CONFIG_UNSET, T_CONFIG_SCOPE_UNSET }
|
189
|
};
|
190
|
|
191
|
p->config_storage = calloc(1, srv->config_context->used * sizeof(specific_config *));
|
192
|
|
193
|
for (i = 0; i < srv->config_context->used; i++) {
|
194
|
plugin_config *s;
|
195
|
buffer *sel;
|
196
|
|
197
|
s = calloc(1, sizeof(plugin_config));
|
198
|
s->mydb = buffer_init();
|
199
|
s->myuser = buffer_init();
|
200
|
s->mypass = buffer_init();
|
201
|
s->mysock = buffer_init();
|
202
|
s->hostname = buffer_init();
|
203
|
s->port = 0; /* default port for mysql */
|
204
|
sel = buffer_init();
|
205
|
s->mysql = NULL;
|
206
|
|
207
|
s->mysql_pre = buffer_init();
|
208
|
s->mysql_mid = buffer_init();
|
209
|
s->mysql_post = buffer_init();
|
210
|
|
211
|
cv[0].destination = s->mydb;
|
212
|
cv[1].destination = s->myuser;
|
213
|
cv[2].destination = s->mypass;
|
214
|
cv[3].destination = s->mysock;
|
215
|
cv[4].destination = sel;
|
216
|
cv[5].destination = s->hostname;
|
217
|
cv[6].destination = &(s->port);
|
218
|
|
219
|
p->config_storage[i] = s;
|
220
|
|
221
|
if (config_insert_values_global(srv,
|
222
|
((data_config *)srv->config_context->data[i])->value,
|
223
|
cv)) return HANDLER_ERROR;
|
224
|
|
225
|
s->mysql_pre = buffer_init();
|
226
|
s->mysql_mid = buffer_init();
|
227
|
s->mysql_post = buffer_init();
|
228
|
|
229
|
// quick dirty changed manually - parses 2 questionmarks:
|
230
|
// the first - and the last. - hopefully
|
231
|
if (sel->used && (qmarkA = strchr(sel->ptr, '?')) && (qmarkB = strrchr(sel->ptr, '?'))) {
|
232
|
*qmarkA = '\0';
|
233
|
*qmarkB = '\0';
|
234
|
buffer_copy_string(s->mysql_pre, sel->ptr);
|
235
|
buffer_copy_string(s->mysql_mid, qmarkA+1);
|
236
|
buffer_copy_string(s->mysql_post, qmarkB+1);
|
237
|
} else {
|
238
|
buffer_copy_string_buffer(s->mysql_pre, sel);
|
239
|
}
|
240
|
|
241
|
/* Original-Code for Lines 231 to 239:
|
242
|
if (sel->used && (qmark = strchr(sel->ptr, '?'))) {
|
243
|
*qmark = '\0';
|
244
|
buffer_copy_string(s->mysql_pre, sel->ptr);
|
245
|
buffer_copy_string(s->mysql_post, qmark+1);
|
246
|
} else {
|
247
|
buffer_copy_string_buffer(s->mysql_pre, sel);
|
248
|
}
|
249
|
*/
|
250
|
|
251
|
/* required:
|
252
|
* - username
|
253
|
* - database
|
254
|
*
|
255
|
* optional:
|
256
|
* - password, default: empty
|
257
|
* - socket, default: mysql default
|
258
|
* - hostname, if set overrides socket
|
259
|
* - port, default: 3306
|
260
|
*/
|
261
|
|
262
|
/* all have to be set */
|
263
|
if (!(buffer_is_empty(s->myuser) ||
|
264
|
buffer_is_empty(s->mydb))) {
|
265
|
my_bool reconnect = 1;
|
266
|
int fd;
|
267
|
|
268
|
if (NULL == (s->mysql = mysql_init(NULL))) {
|
269
|
log_error_write(srv, __FILE__, __LINE__, "s", "mysql_init() failed, exiting...");
|
270
|
|
271
|
return HANDLER_ERROR;
|
272
|
}
|
273
|
|
274
|
#if MYSQL_VERSION_ID >= 50013
|
275
|
/* in mysql versions above 5.0.3 the reconnect flag is off by default */
|
276
|
mysql_options(s->mysql, MYSQL_OPT_RECONNECT, &reconnect);
|
277
|
#endif
|
278
|
|
279
|
#define FOO(x) (s->x->used ? s->x->ptr : NULL)
|
280
|
|
281
|
if (!mysql_real_connect(s->mysql, FOO(hostname), FOO(myuser), FOO(mypass),
|
282
|
FOO(mydb), s->port, FOO(mysock), 0)) {
|
283
|
log_error_write(srv, __FILE__, __LINE__, "s", mysql_error(s->mysql));
|
284
|
|
285
|
return HANDLER_ERROR;
|
286
|
}
|
287
|
#undef FOO
|
288
|
/* set close_on_exec for mysql the hard way */
|
289
|
/* Note: this only works as it is done during startup, */
|
290
|
/* otherwise we cannot be sure that mysql is fd i-1 */
|
291
|
if (-1 == (fd = open("/dev/null", 0))) {
|
292
|
close(fd);
|
293
|
fcntl(fd-1, F_SETFD, FD_CLOEXEC);
|
294
|
}
|
295
|
}
|
296
|
}
|
297
|
|
298
|
|
299
|
|
300
|
return HANDLER_GO_ON;
|
301
|
}
|
302
|
|
303
|
#define PATCH(x) \
|
304
|
p->conf.x = s->x;
|
305
|
static int mod_mysql_vhost_patch_connection(server *srv, connection *con, plugin_data *p) {
|
306
|
size_t i, j;
|
307
|
plugin_config *s = p->config_storage[0];
|
308
|
|
309
|
PATCH(mysql_pre);
|
310
|
PATCH(mysql_mid);
|
311
|
PATCH(mysql_post);
|
312
|
#ifdef HAVE_MYSQL
|
313
|
PATCH(mysql);
|
314
|
#endif
|
315
|
|
316
|
/* skip the first, the global context */
|
317
|
for (i = 1; i < srv->config_context->used; i++) {
|
318
|
data_config *dc = (data_config *)srv->config_context->data[i];
|
319
|
s = p->config_storage[i];
|
320
|
|
321
|
/* condition didn't match */
|
322
|
if (!config_check_cond(srv, con, dc)) continue;
|
323
|
|
324
|
/* merge config */
|
325
|
for (j = 0; j < dc->value->used; j++) {
|
326
|
data_unset *du = dc->value->data[j];
|
327
|
|
328
|
if (buffer_is_equal_string(du->key, CONST_STR_LEN("mysql-vhost.sql"))) {
|
329
|
PATCH(mysql_pre);
|
330
|
PATCH(mysql_mid);
|
331
|
PATCH(mysql_post);
|
332
|
}
|
333
|
}
|
334
|
|
335
|
if (s->mysql) {
|
336
|
PATCH(mysql);
|
337
|
}
|
338
|
}
|
339
|
|
340
|
return 0;
|
341
|
}
|
342
|
#undef PATCH
|
343
|
|
344
|
|
345
|
/* handle document root request */
|
346
|
CONNECTION_FUNC(mod_mysql_vhost_handle_docroot) {
|
347
|
plugin_data *p = p_d;
|
348
|
plugin_connection_data *c;
|
349
|
stat_cache_entry *sce;
|
350
|
|
351
|
unsigned cols;
|
352
|
MYSQL_ROW row;
|
353
|
MYSQL_RES *result = NULL;
|
354
|
|
355
|
/* no host specified? */
|
356
|
if (!con->uri.authority->used) return HANDLER_GO_ON;
|
357
|
|
358
|
mod_mysql_vhost_patch_connection(srv, con, p);
|
359
|
|
360
|
if (!p->conf.mysql) return HANDLER_GO_ON;
|
361
|
|
362
|
/* sets up connection data if not done yet */
|
363
|
c = mod_mysql_vhost_connection_data(srv, con, p_d);
|
364
|
|
365
|
/* check if cached this connection */
|
366
|
if (c->server_name->used && /* con->uri.authority->used && */
|
367
|
buffer_is_equal(c->server_name, con->uri.authority)) goto GO_ON;
|
368
|
|
369
|
/* build and run SQL query */
|
370
|
buffer_copy_string_buffer(p->tmp_buf, p->conf.mysql_pre);
|
371
|
if (p->conf.mysql_mid->used) {
|
372
|
buffer_append_string_buffer(p->tmp_buf, con->uri.authority);
|
373
|
buffer_append_string_buffer(p->tmp_buf, p->conf.mysql_mid);
|
374
|
}
|
375
|
if (p->conf.mysql_post->used) {
|
376
|
buffer_append_string_buffer(p->tmp_buf, con->uri.authority);
|
377
|
buffer_append_string_buffer(p->tmp_buf, p->conf.mysql_post);
|
378
|
}
|
379
|
if (mysql_query(p->conf.mysql, p->tmp_buf->ptr)) {
|
380
|
log_error_write(srv, __FILE__, __LINE__, "s", mysql_error(p->conf.mysql));
|
381
|
goto ERR500;
|
382
|
}
|
383
|
result = mysql_store_result(p->conf.mysql);
|
384
|
cols = mysql_num_fields(result);
|
385
|
row = mysql_fetch_row(result);
|
386
|
if (!row || cols < 1) {
|
387
|
/* no such virtual host */
|
388
|
mysql_free_result(result);
|
389
|
return HANDLER_GO_ON;
|
390
|
}
|
391
|
|
392
|
/* sanity check that really is a directory */
|
393
|
buffer_copy_string(p->tmp_buf, row[0]);
|
394
|
BUFFER_APPEND_SLASH(p->tmp_buf);
|
395
|
|
396
|
if (HANDLER_ERROR == stat_cache_get_entry(srv, con, p->tmp_buf, &sce)) {
|
397
|
log_error_write(srv, __FILE__, __LINE__, "sb", strerror(errno), p->tmp_buf);
|
398
|
goto ERR500;
|
399
|
}
|
400
|
if (!S_ISDIR(sce->st.st_mode)) {
|
401
|
log_error_write(srv, __FILE__, __LINE__, "sb", "Not a directory", p->tmp_buf);
|
402
|
goto ERR500;
|
403
|
}
|
404
|
|
405
|
/* cache the data */
|
406
|
buffer_copy_string_buffer(c->server_name, con->uri.authority);
|
407
|
buffer_copy_string_buffer(c->document_root, p->tmp_buf);
|
408
|
|
409
|
/* fcgi_offset and fcgi_arg are optional */
|
410
|
if (cols > 1 && row[1]) {
|
411
|
c->fcgi_offset = atoi(row[1]);
|
412
|
|
413
|
if (cols > 2 && row[2]) {
|
414
|
buffer_copy_string(c->fcgi_arg, row[2]);
|
415
|
} else {
|
416
|
c->fcgi_arg->used = 0;
|
417
|
}
|
418
|
} else {
|
419
|
c->fcgi_offset = c->fcgi_arg->used = 0;
|
420
|
}
|
421
|
mysql_free_result(result);
|
422
|
|
423
|
/* fix virtual server and docroot */
|
424
|
GO_ON: buffer_copy_string_buffer(con->server_name, c->server_name);
|
425
|
buffer_copy_string_buffer(con->physical.doc_root, c->document_root);
|
426
|
|
427
|
#ifdef DEBUG
|
428
|
log_error_write(srv, __FILE__, __LINE__, "sbbdb",
|
429
|
result ? "NOT CACHED" : "cached",
|
430
|
con->server_name, con->physical.doc_root,
|
431
|
c->fcgi_offset, c->fcgi_arg);
|
432
|
#endif
|
433
|
return HANDLER_GO_ON;
|
434
|
|
435
|
ERR500: if (result) mysql_free_result(result);
|
436
|
con->http_status = 500; /* Internal Error */
|
437
|
return HANDLER_FINISHED;
|
438
|
}
|
439
|
|
440
|
/* this function is called at dlopen() time and inits the callbacks */
|
441
|
int mod_mysql_vhost_plugin_init(plugin *p) {
|
442
|
p->version = LIGHTTPD_VERSION_ID;
|
443
|
p->name = buffer_init_string("mysql_vhost");
|
444
|
|
445
|
p->init = mod_mysql_vhost_init;
|
446
|
p->cleanup = mod_mysql_vhost_cleanup;
|
447
|
p->handle_request_done = mod_mysql_vhost_handle_connection_close;
|
448
|
|
449
|
p->set_defaults = mod_mysql_vhost_set_defaults;
|
450
|
p->handle_docroot = mod_mysql_vhost_handle_docroot;
|
451
|
|
452
|
return 0;
|
453
|
}
|
454
|
#else
|
455
|
/* we don't have mysql support, this plugin does nothing */
|
456
|
int mod_mysql_vhost_plugin_init(plugin *p) {
|
457
|
p->version = LIGHTTPD_VERSION_ID;
|
458
|
p->name = buffer_init_string("mysql_vhost");
|
459
|
|
460
|
return 0;
|
461
|
}
|
462
|
#endif
|