16 #include "stat_cache.h"
17 #ifdef DEBUG_MOD_MYSQL_VHOST
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
43 /* global plugin data */
49 plugin_config
**config_storage
;
54 /* per connection plugin data */
57 buffer
*document_root
;
58 } plugin_connection_data
;
60 /* init the plugin data */
61 INIT_FUNC(mod_mysql_vhost_init
) {
64 p
= calloc(1, sizeof(*p
));
66 p
->tmp_buf
= buffer_init();
71 /* cleanup the plugin data */
72 SERVER_FUNC(mod_mysql_vhost_cleanup
) {
78 log_error_write(srv
, __FILE__
, __LINE__
, "ss",
79 "mod_mysql_vhost_cleanup", p
? "yes" : "NO");
81 if (!p
) return HANDLER_GO_ON
;
83 if (p
->config_storage
) {
85 for (i
= 0; i
< srv
->config_context
->used
; i
++) {
86 plugin_config
*s
= p
->config_storage
[i
];
90 mysql_close(s
->mysql
);
92 buffer_free(s
->mysql_query
);
94 buffer_free(s
->myuser
);
95 buffer_free(s
->mypass
);
96 buffer_free(s
->mysock
);
97 buffer_free(s
->hostname
);
101 free(p
->config_storage
);
103 buffer_free(p
->tmp_buf
);
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
];
119 log_error_write(srv
, __FILE__
, __LINE__
, "ss",
120 "mod_mysql_connection_data", c
? "old" : "NEW");
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
];
140 log_error_write(srv
, __FILE__
, __LINE__
, "ss",
141 "mod_mysql_vhost_handle_connection_close", c
? "yes" : "NO");
144 if (!c
) return HANDLER_GO_ON
;
146 buffer_free(c
->server_name
);
147 buffer_free(c
->document_root
);
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
;
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
];
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 */
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
;
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
);
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
)) {
234 if (!mysql_real_connect(s
->mysql
, FOO(hostname
), FOO(myuser
), FOO(mypass
),
235 FOO(mydb
), s
->port
, FOO(mysock
), 0)) {
237 log_error_write(srv
, __FILE__
, __LINE__
, "s", mysql_error(s
->mysql
));
238 return HANDLER_ERROR
;
242 fd_close_on_exec(s
->mysql
->net
.fd
);
246 return HANDLER_GO_ON
;
251 static int mod_mysql_vhost_patch_connection(server
*srv
, connection
*con
, plugin_data
*p
) {
253 plugin_config
*s
= p
->config_storage
[0];
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;
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"))) {
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
;
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
);
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
));
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
));
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);
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
);
354 if (!S_ISDIR(sce
->st
.st_mode
)) {
355 log_error_write(srv
, __FILE__
, __LINE__
, "sb", "Not a directory", p
->tmp_buf
);
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);
368 /* fix virtual server and docroot */
370 buffer_copy_buffer(con
->server_name
, c
->server_name
);
371 buffer_copy_buffer(con
->physical
.doc_root
, c
->document_root
);
374 log_error_write(srv
, __FILE__
, __LINE__
, "sbb",
375 result
? "NOT CACHED" : "cached",
376 con
->server_name
, con
->physical
.doc_root
);
378 return HANDLER_GO_ON
;
381 if (result
) mysql_free_result(result
);
382 #if MYSQL_VERSION_ID >= 40100
383 while (mysql_next_result(p
->conf
.mysql
) == 0);
385 con
->http_status
= 500; /* Internal Error */
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
;
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");