14 #include "stat_cache.h"
15 #ifdef DEBUG_MOD_MYSQL_VHOST
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
40 /* global plugin data */
46 plugin_config
**config_storage
;
51 /* per connection plugin data */
54 buffer
*document_root
;
55 } plugin_connection_data
;
57 /* init the plugin data */
58 INIT_FUNC(mod_mysql_vhost_init
) {
61 p
= calloc(1, sizeof(*p
));
63 p
->tmp_buf
= buffer_init();
68 /* cleanup the plugin data */
69 SERVER_FUNC(mod_mysql_vhost_cleanup
) {
75 log_error_write(srv
, __FILE__
, __LINE__
, "ss",
76 "mod_mysql_vhost_cleanup", p
? "yes" : "NO");
78 if (!p
) return HANDLER_GO_ON
;
80 if (p
->config_storage
) {
82 for (i
= 0; i
< srv
->config_context
->used
; i
++) {
83 plugin_config
*s
= p
->config_storage
[i
];
87 mysql_close(s
->mysql
);
89 buffer_free(s
->mysql_query
);
91 buffer_free(s
->myuser
);
92 buffer_free(s
->mypass
);
93 buffer_free(s
->mysock
);
94 buffer_free(s
->hostname
);
98 free(p
->config_storage
);
100 buffer_free(p
->tmp_buf
);
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
];
116 log_error_write(srv
, __FILE__
, __LINE__
, "ss",
117 "mod_mysql_connection_data", c
? "old" : "NEW");
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
];
137 log_error_write(srv
, __FILE__
, __LINE__
, "ss",
138 "mod_mysql_vhost_handle_connection_close", c
? "yes" : "NO");
141 if (!c
) return HANDLER_GO_ON
;
143 buffer_free(c
->server_name
);
144 buffer_free(c
->document_root
);
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
;
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
];
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 */
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
;
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
);
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
)) {
231 if (!mysql_real_connect(s
->mysql
, FOO(hostname
), FOO(myuser
), FOO(mypass
),
232 FOO(mydb
), s
->port
, FOO(mysock
), 0)) {
234 log_error_write(srv
, __FILE__
, __LINE__
, "s", mysql_error(s
->mysql
));
235 return HANDLER_ERROR
;
239 fd_close_on_exec(s
->mysql
->net
.fd
);
243 return HANDLER_GO_ON
;
248 static int mod_mysql_vhost_patch_connection(server
*srv
, connection
*con
, plugin_data
*p
) {
250 plugin_config
*s
= p
->config_storage
[0];
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;
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"))) {
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
;
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
);
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
));
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
));
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);
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
);
349 if (!S_ISDIR(sce
->st
.st_mode
)) {
350 log_error_write(srv
, __FILE__
, __LINE__
, "sb", "Not a directory", p
->tmp_buf
);
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);
363 /* fix virtual server and docroot */
365 buffer_copy_buffer(con
->server_name
, c
->server_name
);
366 buffer_copy_buffer(con
->physical
.doc_root
, c
->document_root
);
369 log_error_write(srv
, __FILE__
, __LINE__
, "sbb",
370 result
? "NOT CACHED" : "cached",
371 con
->server_name
, con
->physical
.doc_root
);
373 return HANDLER_GO_ON
;
376 if (result
) mysql_free_result(result
);
377 #if MYSQL_VERSION_ID >= 40100
378 while (mysql_next_result(p
->conf
.mysql
) == 0);
380 con
->http_status
= 500; /* Internal Error */
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
;