15 #include "stat_cache.h"
18 * Plugin for lighttpd to use MySQL
19 * for domain to directory lookups,
20 * i.e virtual hosts (vhosts).
22 * /ada@riksnet.se 2004-12-06
38 /* global plugin data */
44 plugin_config
**config_storage
;
49 /* per connection plugin data */
52 buffer
*document_root
;
53 } plugin_connection_data
;
55 /* init the plugin data */
56 INIT_FUNC(mod_mysql_vhost_init
) {
59 p
= calloc(1, sizeof(*p
));
61 p
->tmp_buf
= buffer_init();
66 /* cleanup the plugin data */
67 SERVER_FUNC(mod_mysql_vhost_cleanup
) {
72 if (!p
) return HANDLER_GO_ON
;
74 if (p
->config_storage
) {
76 for (i
= 0; i
< srv
->config_context
->used
; i
++) {
77 plugin_config
*s
= p
->config_storage
[i
];
81 mysql_close(s
->mysql
);
83 buffer_free(s
->mysql_query
);
85 buffer_free(s
->myuser
);
86 buffer_free(s
->mypass
);
87 buffer_free(s
->mysock
);
88 buffer_free(s
->hostname
);
92 free(p
->config_storage
);
94 buffer_free(p
->tmp_buf
);
101 /* handle the plugin per connection data */
102 static void* mod_mysql_vhost_connection_data(server
*srv
, connection
*con
, void *p_d
)
104 plugin_data
*p
= p_d
;
105 plugin_connection_data
*c
= con
->plugin_ctx
[p
->id
];
110 c
= calloc(1, sizeof(*c
));
112 c
->server_name
= buffer_init();
113 c
->document_root
= buffer_init();
115 return con
->plugin_ctx
[p
->id
] = c
;
118 /* destroy the plugin per connection data */
119 CONNECTION_FUNC(mod_mysql_vhost_handle_connection_reset
) {
120 plugin_data
*p
= p_d
;
121 plugin_connection_data
*c
= con
->plugin_ctx
[p
->id
];
125 if (!c
) return HANDLER_GO_ON
;
127 buffer_free(c
->server_name
);
128 buffer_free(c
->document_root
);
132 con
->plugin_ctx
[p
->id
] = NULL
;
133 return HANDLER_GO_ON
;
136 /* set configuration values */
137 SERVER_FUNC(mod_mysql_vhost_set_defaults
) {
138 plugin_data
*p
= p_d
;
141 config_values_t cv
[] = {
142 { "mysql-vhost.db", NULL
, T_CONFIG_STRING
, T_CONFIG_SCOPE_CONNECTION
}, /* 0 */
143 { "mysql-vhost.user", NULL
, T_CONFIG_STRING
, T_CONFIG_SCOPE_CONNECTION
}, /* 1 */
144 { "mysql-vhost.pass", NULL
, T_CONFIG_STRING
, T_CONFIG_SCOPE_CONNECTION
}, /* 2 */
145 { "mysql-vhost.sock", NULL
, T_CONFIG_STRING
, T_CONFIG_SCOPE_CONNECTION
}, /* 3 */
146 { "mysql-vhost.sql", NULL
, T_CONFIG_STRING
, T_CONFIG_SCOPE_CONNECTION
}, /* 4 */
147 { "mysql-vhost.hostname", NULL
, T_CONFIG_STRING
, T_CONFIG_SCOPE_CONNECTION
}, /* 5 */
148 { "mysql-vhost.port", NULL
, T_CONFIG_SHORT
, T_CONFIG_SCOPE_CONNECTION
}, /* 6 */
149 { NULL
, NULL
, T_CONFIG_UNSET
, T_CONFIG_SCOPE_UNSET
}
152 p
->config_storage
= calloc(srv
->config_context
->used
, sizeof(plugin_config
*));
154 for (i
= 0; i
< srv
->config_context
->used
; i
++) {
155 data_config
const* config
= (data_config
const*)srv
->config_context
->data
[i
];
158 s
= calloc(1, sizeof(plugin_config
));
159 s
->mysql_query
= buffer_init();
160 s
->mydb
= buffer_init();
161 s
->myuser
= buffer_init();
162 s
->mypass
= buffer_init();
163 s
->mysock
= buffer_init();
164 s
->hostname
= buffer_init();
165 s
->port
= 0; /* default port for mysql */
168 cv
[0].destination
= s
->mydb
;
169 cv
[1].destination
= s
->myuser
;
170 cv
[2].destination
= s
->mypass
;
171 cv
[3].destination
= s
->mysock
;
172 cv
[4].destination
= s
->mysql_query
;
173 cv
[5].destination
= s
->hostname
;
174 cv
[6].destination
= &(s
->port
);
176 p
->config_storage
[i
] = s
;
178 if (config_insert_values_global(srv
, config
->value
, cv
, i
== 0 ? T_CONFIG_SCOPE_SERVER
: T_CONFIG_SCOPE_CONNECTION
)) {
179 return HANDLER_ERROR
;
187 * - password, default: empty
188 * - socket, default: mysql default
189 * - hostname, if set overrides socket
190 * - port, default: 3306
193 /* all have to be set */
194 if (!(buffer_string_is_empty(s
->myuser
) ||
195 buffer_string_is_empty(s
->mydb
))) {
197 if (NULL
== (s
->mysql
= mysql_init(NULL
))) {
198 log_error_write(srv
, __FILE__
, __LINE__
, "s", "mysql_init() failed, exiting...");
199 return HANDLER_ERROR
;
202 #if MYSQL_VERSION_ID >= 50013
203 /* in mysql versions above 5.0.3 the reconnect flag is off by default */
206 mysql_options(s
->mysql
, MYSQL_OPT_RECONNECT
, &reconnect
);
210 #define FOO(x) (buffer_string_is_empty(s->x) ? NULL : s->x->ptr)
212 #if MYSQL_VERSION_ID >= 40100
213 /* CLIENT_MULTI_STATEMENTS first appeared in 4.1 */
214 if (!mysql_real_connect(s
->mysql
, FOO(hostname
), FOO(myuser
), FOO(mypass
),
215 FOO(mydb
), s
->port
, FOO(mysock
), CLIENT_MULTI_STATEMENTS
)) {
217 if (!mysql_real_connect(s
->mysql
, FOO(hostname
), FOO(myuser
), FOO(mypass
),
218 FOO(mydb
), s
->port
, FOO(mysock
), 0)) {
220 log_error_write(srv
, __FILE__
, __LINE__
, "s", mysql_error(s
->mysql
));
221 return HANDLER_ERROR
;
225 fdevent_setfd_cloexec(s
->mysql
->net
.fd
);
229 return HANDLER_GO_ON
;
234 static int mod_mysql_vhost_patch_connection(server
*srv
, connection
*con
, plugin_data
*p
) {
236 plugin_config
*s
= p
->config_storage
[0];
241 /* skip the first, the global context */
242 for (i
= 1; i
< srv
->config_context
->used
; i
++) {
243 data_config
*dc
= (data_config
*)srv
->config_context
->data
[i
];
244 s
= p
->config_storage
[i
];
246 /* condition didn't match */
247 if (!config_check_cond(srv
, con
, dc
)) continue;
250 for (j
= 0; j
< dc
->value
->used
; j
++) {
251 data_unset
*du
= dc
->value
->data
[j
];
253 if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("mysql-vhost.sql"))) {
268 /* handle document root request */
269 CONNECTION_FUNC(mod_mysql_vhost_handle_docroot
) {
270 plugin_data
*p
= p_d
;
271 plugin_connection_data
*c
;
272 stat_cache_entry
*sce
;
276 MYSQL_RES
*result
= NULL
;
278 /* no host specified? */
279 if (buffer_string_is_empty(con
->uri
.authority
)) return HANDLER_GO_ON
;
281 mod_mysql_vhost_patch_connection(srv
, con
, p
);
283 if (!p
->conf
.mysql
) return HANDLER_GO_ON
;
284 if (buffer_string_is_empty(p
->conf
.mysql_query
)) return HANDLER_GO_ON
;
286 /* sets up connection data if not done yet */
287 c
= mod_mysql_vhost_connection_data(srv
, con
, p_d
);
289 /* check if cached this connection */
290 if (buffer_is_equal(c
->server_name
, con
->uri
.authority
)) goto GO_ON
;
292 /* build and run SQL query */
293 buffer_clear(p
->tmp_buf
);
294 for (char *b
= p
->conf
.mysql_query
->ptr
, *d
; *b
; b
= d
+1) {
295 if (NULL
!= (d
= strchr(b
, '?'))) {
296 /* escape the uri.authority */
297 unsigned long to_len
;
298 buffer_append_string_len(p
->tmp_buf
, b
, (size_t)(d
- b
));
299 buffer_string_prepare_append(p
->tmp_buf
, buffer_string_length(con
->uri
.authority
) * 2);
300 to_len
= mysql_real_escape_string(p
->conf
.mysql
,
301 p
->tmp_buf
->ptr
+ buffer_string_length(p
->tmp_buf
),
302 CONST_BUF_LEN(con
->uri
.authority
));
303 if ((unsigned long)~0 == to_len
) goto ERR500
;
304 buffer_commit(p
->tmp_buf
, to_len
);
306 d
= p
->conf
.mysql_query
->ptr
+ buffer_string_length(p
->conf
.mysql_query
);
307 buffer_append_string_len(p
->tmp_buf
, b
, (size_t)(d
- b
));
311 if (mysql_real_query(p
->conf
.mysql
, CONST_BUF_LEN(p
->tmp_buf
))) {
312 log_error_write(srv
, __FILE__
, __LINE__
, "s", mysql_error(p
->conf
.mysql
));
315 result
= mysql_store_result(p
->conf
.mysql
);
316 cols
= mysql_num_fields(result
);
317 row
= mysql_fetch_row(result
);
318 if (!row
|| cols
< 1) {
319 /* no such virtual host */
320 mysql_free_result(result
);
321 #if MYSQL_VERSION_ID >= 40100
322 while (mysql_next_result(p
->conf
.mysql
) == 0);
324 return HANDLER_GO_ON
;
327 /* sanity check that really is a directory */
328 buffer_copy_string(p
->tmp_buf
, row
[0]);
329 buffer_append_slash(p
->tmp_buf
);
331 if (HANDLER_ERROR
== stat_cache_get_entry(srv
, con
, p
->tmp_buf
, &sce
)) {
332 log_error_write(srv
, __FILE__
, __LINE__
, "sb", strerror(errno
), p
->tmp_buf
);
335 if (!S_ISDIR(sce
->st
.st_mode
)) {
336 log_error_write(srv
, __FILE__
, __LINE__
, "sb", "Not a directory", p
->tmp_buf
);
341 buffer_copy_buffer(c
->server_name
, con
->uri
.authority
);
342 buffer_copy_buffer(c
->document_root
, p
->tmp_buf
);
344 mysql_free_result(result
);
345 #if MYSQL_VERSION_ID >= 40100
346 while (mysql_next_result(p
->conf
.mysql
) == 0);
349 /* fix virtual server and docroot */
351 buffer_copy_buffer(con
->server_name
, c
->server_name
);
352 buffer_copy_buffer(con
->physical
.doc_root
, c
->document_root
);
354 return HANDLER_GO_ON
;
357 if (result
) mysql_free_result(result
);
358 #if MYSQL_VERSION_ID >= 40100
359 while (mysql_next_result(p
->conf
.mysql
) == 0);
361 con
->http_status
= 500; /* Internal Error */
363 return HANDLER_FINISHED
;
366 /* this function is called at dlopen() time and inits the callbacks */
367 int mod_mysql_vhost_plugin_init(plugin
*p
);
368 int mod_mysql_vhost_plugin_init(plugin
*p
) {
369 p
->version
= LIGHTTPD_VERSION_ID
;
370 p
->name
= buffer_init_string("mysql_vhost");
372 p
->init
= mod_mysql_vhost_init
;
373 p
->cleanup
= mod_mysql_vhost_cleanup
;
374 p
->connection_reset
= mod_mysql_vhost_handle_connection_reset
;
376 p
->set_defaults
= mod_mysql_vhost_set_defaults
;
377 p
->handle_docroot
= mod_mysql_vhost_handle_docroot
;