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
36 /* global plugin data */
42 plugin_config
**config_storage
;
47 /* per connection plugin data */
50 buffer
*document_root
;
51 } plugin_connection_data
;
53 /* init the plugin data */
54 INIT_FUNC(mod_mysql_vhost_init
) {
57 p
= calloc(1, sizeof(*p
));
59 p
->tmp_buf
= buffer_init();
64 /* cleanup the plugin data */
65 SERVER_FUNC(mod_mysql_vhost_cleanup
) {
70 if (!p
) return HANDLER_GO_ON
;
72 if (p
->config_storage
) {
74 for (i
= 0; i
< srv
->config_context
->used
; i
++) {
75 plugin_config
*s
= p
->config_storage
[i
];
79 mysql_close(s
->mysql
);
81 buffer_free(s
->mysql_query
);
83 buffer_free(s
->myuser
);
84 buffer_free(s
->mypass
);
85 buffer_free(s
->mysock
);
86 buffer_free(s
->hostname
);
90 free(p
->config_storage
);
92 buffer_free(p
->tmp_buf
);
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
];
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
];
123 if (!c
) return HANDLER_GO_ON
;
125 buffer_free(c
->server_name
);
126 buffer_free(c
->document_root
);
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
;
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
];
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 */
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
;
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
);
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
)) {
213 if (!mysql_real_connect(s
->mysql
, FOO(hostname
), FOO(myuser
), FOO(mypass
),
214 FOO(mydb
), s
->port
, FOO(mysock
), 0)) {
216 log_error_write(srv
, __FILE__
, __LINE__
, "s", mysql_error(s
->mysql
));
217 return HANDLER_ERROR
;
221 fdevent_setfd_cloexec(s
->mysql
->net
.fd
);
225 return HANDLER_GO_ON
;
230 static int mod_mysql_vhost_patch_connection(server
*srv
, connection
*con
, plugin_data
*p
) {
232 plugin_config
*s
= p
->config_storage
[0];
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;
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"))) {
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
;
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
);
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
));
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
));
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);
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
);
331 if (!S_ISDIR(sce
->st
.st_mode
)) {
332 log_error_write(srv
, __FILE__
, __LINE__
, "sb", "Not a directory", p
->tmp_buf
);
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);
345 /* fix virtual server and docroot */
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
;
353 if (result
) mysql_free_result(result
);
354 #if MYSQL_VERSION_ID >= 40100
355 while (mysql_next_result(p
->conf
.mysql
) == 0);
357 con
->http_status
= 500; /* Internal Error */
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
;