1 /*
2 * mod_vhostdb - virtual hosts mapping from backend database
3 *
4 * Copyright(c) 2017 Glenn Strauss gstrauss()gluelogic.com All rights reserved
5 * License: BSD 3-clause (same as lighttpd)
6 */
7 #include "first.h"
8
9 #include <stdlib.h>
10 #include <string.h>
11
12 #include "mod_vhostdb_api.h"
13 #include "base.h"
14 #include "plugin.h"
15 #include "plugin_config.h"
16 #include "log.h"
17 #include "stat_cache.h"
18 #include "algo_splaytree.h"
19
20 /**
21 * vhostdb framework
22 */
23
24 typedef struct {
25 splay_tree *sptree; /* data in nodes of tree are (vhostdb_cache_entry *) */
26 time_t max_age;
27 } vhostdb_cache;
28
29 typedef struct {
30 const http_vhostdb_backend_t *vhostdb_backend;
31 vhostdb_cache *vhostdb_cache;
32 } plugin_config;
33
34 typedef struct {
35 PLUGIN_DATA;
36 plugin_config defaults;
37 plugin_config conf;
38 } plugin_data;
39
40 typedef struct {
41 char *server_name;
42 char *document_root;
43 uint32_t slen;
44 uint32_t dlen;
45 unix_time64_t ctime;
46 } vhostdb_cache_entry;
47
48 static vhostdb_cache_entry *
vhostdb_cache_entry_init(const buffer * const server_name,const buffer * const docroot)49 vhostdb_cache_entry_init (const buffer * const server_name, const buffer * const docroot)
50 {
51 const uint32_t slen = buffer_clen(server_name);
52 const uint32_t dlen = buffer_clen(docroot);
53 vhostdb_cache_entry * const ve =
54 ck_malloc(sizeof(vhostdb_cache_entry) + slen + dlen);
55 ve->ctime = log_monotonic_secs;
56 ve->slen = slen;
57 ve->dlen = dlen;
58 ve->server_name = (char *)(ve + 1);
59 ve->document_root = ve->server_name + slen;
60 memcpy(ve->server_name, server_name->ptr, slen);
61 memcpy(ve->document_root, docroot->ptr, dlen);
62 return ve;
63 }
64
65 static void
vhostdb_cache_entry_free(vhostdb_cache_entry * ve)66 vhostdb_cache_entry_free (vhostdb_cache_entry *ve)
67 {
68 free(ve);
69 }
70
71 static void
vhostdb_cache_free(vhostdb_cache * vc)72 vhostdb_cache_free (vhostdb_cache *vc)
73 {
74 splay_tree *sptree = vc->sptree;
75 while (sptree) {
76 vhostdb_cache_entry_free(sptree->data);
77 sptree = splaytree_delete(sptree, sptree->key);
78 }
79 free(vc);
80 }
81
82 static vhostdb_cache *
vhostdb_cache_init(const array * opts)83 vhostdb_cache_init (const array *opts)
84 {
85 vhostdb_cache *vc = ck_malloc(sizeof(vhostdb_cache));
86 vc->sptree = NULL;
87 vc->max_age = 600; /* 10 mins */
88 for (uint32_t i = 0, used = opts->used; i < used; ++i) {
89 data_unset *du = opts->data[i];
90 if (buffer_is_equal_string(&du->key, CONST_STR_LEN("max-age")))
91 vc->max_age = (time_t)
92 config_plugin_value_to_int32(du, 600); /* 10 min if invalid num */
93 }
94 return vc;
95 }
96
97 static vhostdb_cache_entry *
mod_vhostdb_cache_query(request_st * const r,plugin_data * const p)98 mod_vhostdb_cache_query (request_st * const r, plugin_data * const p)
99 {
100 const int ndx = splaytree_djbhash(BUF_PTR_LEN(&r->uri.authority));
101 splay_tree ** const sptree = &p->conf.vhostdb_cache->sptree;
102 *sptree = splaytree_splay(*sptree, ndx);
103 vhostdb_cache_entry * const ve =
104 (*sptree && (*sptree)->key == ndx) ? (*sptree)->data : NULL;
105
106 return ve
107 && buffer_is_equal_string(&r->uri.authority, ve->server_name, ve->slen)
108 ? ve
109 : NULL;
110 }
111
112 static void
mod_vhostdb_cache_insert(request_st * const r,plugin_data * const p,vhostdb_cache_entry * const ve)113 mod_vhostdb_cache_insert (request_st * const r, plugin_data * const p, vhostdb_cache_entry * const ve)
114 {
115 const int ndx = splaytree_djbhash(BUF_PTR_LEN(&r->uri.authority));
116 splay_tree ** const sptree = &p->conf.vhostdb_cache->sptree;
117 /*(not necessary to re-splay (with current usage) since single-threaded
118 * and splaytree has not been modified since mod_vhostdb_cache_query())*/
119 /* *sptree = splaytree_splay(*sptree, ndx); */
120 if (NULL == *sptree || (*sptree)->key != ndx)
121 *sptree = splaytree_insert(*sptree, ndx, ve);
122 else { /* collision; replace old entry */
123 vhostdb_cache_entry_free((*sptree)->data);
124 (*sptree)->data = ve;
125 }
126 }
127
INIT_FUNC(mod_vhostdb_init)128 INIT_FUNC(mod_vhostdb_init) {
129 return ck_calloc(1, sizeof(plugin_data));
130 }
131
FREE_FUNC(mod_vhostdb_free)132 FREE_FUNC(mod_vhostdb_free) {
133 plugin_data *p = p_d;
134
135 if (NULL == p->cvlist) return;
136 /* (init i to 0 if global context; to 1 to skip empty global context) */
137 for (int i = !p->cvlist[0].v.u2[1], used = p->nconfig; i < used; ++i) {
138 config_plugin_value_t *cpv = p->cvlist + p->cvlist[i].v.u2[0];
139 for (; -1 != cpv->k_id; ++cpv) {
140 if (cpv->vtype != T_CONFIG_LOCAL || NULL == cpv->v.v) continue;
141 switch (cpv->k_id) {
142 case 1: /* vhostdb.cache */
143 vhostdb_cache_free(cpv->v.v);
144 break;
145 default:
146 break;
147 }
148 }
149 }
150
151 http_vhostdb_dumbdata_reset();
152 }
153
mod_vhostdb_merge_config_cpv(plugin_config * const pconf,const config_plugin_value_t * const cpv)154 static void mod_vhostdb_merge_config_cpv(plugin_config * const pconf, const config_plugin_value_t * const cpv) {
155 switch (cpv->k_id) { /* index into static config_plugin_keys_t cpk[] */
156 case 0: /* vhostdb.backend */
157 if (cpv->vtype == T_CONFIG_LOCAL)
158 pconf->vhostdb_backend = cpv->v.v;
159 break;
160 case 1: /* vhostdb.cache */
161 if (cpv->vtype == T_CONFIG_LOCAL)
162 pconf->vhostdb_cache = cpv->v.v;
163 break;
164 default:/* should not happen */
165 return;
166 }
167 }
168
mod_vhostdb_merge_config(plugin_config * const pconf,const config_plugin_value_t * cpv)169 static void mod_vhostdb_merge_config(plugin_config * const pconf, const config_plugin_value_t *cpv) {
170 do {
171 mod_vhostdb_merge_config_cpv(pconf, cpv);
172 } while ((++cpv)->k_id != -1);
173 }
174
mod_vhostdb_patch_config(request_st * const r,plugin_data * const p)175 static void mod_vhostdb_patch_config(request_st * const r, plugin_data * const p) {
176 memcpy(&p->conf, &p->defaults, sizeof(plugin_config));
177 for (int i = 1, used = p->nconfig; i < used; ++i) {
178 if (config_check_cond(r, (uint32_t)p->cvlist[i].k_id))
179 mod_vhostdb_merge_config(&p->conf, p->cvlist + p->cvlist[i].v.u2[0]);
180 }
181 }
182
SETDEFAULTS_FUNC(mod_vhostdb_set_defaults)183 SETDEFAULTS_FUNC(mod_vhostdb_set_defaults) {
184 static const config_plugin_keys_t cpk[] = {
185 { CONST_STR_LEN("vhostdb.backend"),
186 T_CONFIG_STRING,
187 T_CONFIG_SCOPE_CONNECTION }
188 ,{ CONST_STR_LEN("vhostdb.cache"),
189 T_CONFIG_ARRAY,
190 T_CONFIG_SCOPE_CONNECTION }
191 ,{ NULL, 0,
192 T_CONFIG_UNSET,
193 T_CONFIG_SCOPE_UNSET }
194 };
195
196 plugin_data * const p = p_d;
197 if (!config_plugin_values_init(srv, p, cpk, "mod_vhostdb"))
198 return HANDLER_ERROR;
199
200 /* process and validate config directives
201 * (init i to 0 if global context; to 1 to skip empty global context) */
202 for (int i = !p->cvlist[0].v.u2[1]; i < p->nconfig; ++i) {
203 config_plugin_value_t *cpv = p->cvlist + p->cvlist[i].v.u2[0];
204 for (; -1 != cpv->k_id; ++cpv) {
205 switch (cpv->k_id) {
206 case 0: /* vhostdb.backend */
207 if (!buffer_is_blank(cpv->v.b)) {
208 const buffer * const b = cpv->v.b;
209 *(const void **)&cpv->v.v = http_vhostdb_backend_get(b);
210 if (NULL == cpv->v.v) {
211 log_error(srv->errh, __FILE__, __LINE__,
212 "vhostdb.backend not supported: %s", b->ptr);
213 return HANDLER_ERROR;
214 }
215 cpv->vtype = T_CONFIG_LOCAL;
216 }
217 break;
218 case 1: /* vhostdb.cache */
219 cpv->v.v = vhostdb_cache_init(cpv->v.a);
220 cpv->vtype = T_CONFIG_LOCAL;
221 break;
222 default:/* should not happen */
223 break;
224 }
225 }
226 }
227
228 /* initialize p->defaults from global config context */
229 if (p->nconfig > 0 && p->cvlist->v.u2[1]) {
230 const config_plugin_value_t *cpv = p->cvlist + p->cvlist->v.u2[0];
231 if (-1 != cpv->k_id)
232 mod_vhostdb_merge_config(&p->defaults, cpv);
233 }
234
235 return HANDLER_GO_ON;
236 }
237
REQUEST_FUNC(mod_vhostdb_handle_request_reset)238 REQUEST_FUNC(mod_vhostdb_handle_request_reset) {
239 plugin_data *p = p_d;
240 vhostdb_cache_entry *ve;
241
242 if ((ve = r->plugin_ctx[p->id])) {
243 r->plugin_ctx[p->id] = NULL;
244 vhostdb_cache_entry_free(ve);
245 }
246
247 return HANDLER_GO_ON;
248 }
249
250 __attribute_cold__
mod_vhostdb_error_500(request_st * const r)251 static handler_t mod_vhostdb_error_500 (request_st * const r)
252 {
253 r->http_status = 500; /* Internal Server Error */
254 r->handler_module = NULL;
255 return HANDLER_FINISHED;
256 }
257
mod_vhostdb_found(request_st * const r,vhostdb_cache_entry * const ve)258 static handler_t mod_vhostdb_found (request_st * const r, vhostdb_cache_entry * const ve)
259 {
260 /* fix virtual server and docroot */
261 if (ve->slen) {
262 r->server_name = &r->server_name_buf;
263 buffer_copy_string_len(&r->server_name_buf, ve->server_name, ve->slen);
264 }
265 buffer_copy_string_len(&r->physical.doc_root, ve->document_root, ve->dlen);
266 return HANDLER_GO_ON;
267 }
268
REQUEST_FUNC(mod_vhostdb_handle_docroot)269 REQUEST_FUNC(mod_vhostdb_handle_docroot) {
270 plugin_data *p = p_d;
271 vhostdb_cache_entry *ve;
272
273 /* no host specified? */
274 if (buffer_is_blank(&r->uri.authority)) return HANDLER_GO_ON;
275
276 /* check if cached this connection */
277 ve = r->plugin_ctx[p->id];
278 if (ve
279 && buffer_is_equal_string(&r->uri.authority, ve->server_name, ve->slen))
280 return mod_vhostdb_found(r, ve); /* HANDLER_GO_ON */
281
282 mod_vhostdb_patch_config(r, p);
283 if (!p->conf.vhostdb_backend) return HANDLER_GO_ON;
284
285 if (p->conf.vhostdb_cache && (ve = mod_vhostdb_cache_query(r, p)))
286 return mod_vhostdb_found(r, ve); /* HANDLER_GO_ON */
287
288 buffer * const b = r->tmp_buf; /*(cleared before use in backend->query())*/
289 const http_vhostdb_backend_t * const backend = p->conf.vhostdb_backend;
290 if (0 != backend->query(r, backend->p_d, b)) {
291 return mod_vhostdb_error_500(r); /* HANDLER_FINISHED */
292 }
293
294 if (buffer_is_blank(b)) {
295 /* no such virtual host */
296 return HANDLER_GO_ON;
297 }
298
299 /* sanity check that really is a directory */
300 buffer_append_slash(b);
301 if (!stat_cache_path_isdir(b)) {
302 log_perror(r->conf.errh, __FILE__, __LINE__, "%s", b->ptr);
303 return mod_vhostdb_error_500(r); /* HANDLER_FINISHED */
304 }
305
306 if (ve && !p->conf.vhostdb_cache)
307 vhostdb_cache_entry_free(ve);
308
309 ve = vhostdb_cache_entry_init(&r->uri.authority, b);
310
311 if (!p->conf.vhostdb_cache)
312 r->plugin_ctx[p->id] = ve;
313 else
314 mod_vhostdb_cache_insert(r, p, ve);
315
316 return mod_vhostdb_found(r, ve); /* HANDLER_GO_ON */
317 }
318
319 /* walk though cache, collect expired ids, and remove them in a second loop */
320 static void
mod_vhostdb_tag_old_entries(splay_tree * const t,int * const keys,int * const ndx,const time_t max_age,const unix_time64_t cur_ts)321 mod_vhostdb_tag_old_entries (splay_tree * const t, int * const keys, int * const ndx, const time_t max_age, const unix_time64_t cur_ts)
322 {
323 if (*ndx == 8192) return; /*(must match num array entries in keys[])*/
324 if (t->left)
325 mod_vhostdb_tag_old_entries(t->left, keys, ndx, max_age, cur_ts);
326 if (t->right)
327 mod_vhostdb_tag_old_entries(t->right, keys, ndx, max_age, cur_ts);
328 if (*ndx == 8192) return; /*(must match num array entries in keys[])*/
329
330 const vhostdb_cache_entry * const ve = t->data;
331 if (cur_ts - ve->ctime > max_age)
332 keys[(*ndx)++] = t->key;
333 }
334
335 __attribute_noinline__
336 static void
mod_vhostdb_periodic_cleanup(splay_tree ** sptree_ptr,const time_t max_age,const unix_time64_t cur_ts)337 mod_vhostdb_periodic_cleanup(splay_tree **sptree_ptr, const time_t max_age, const unix_time64_t cur_ts)
338 {
339 splay_tree *sptree = *sptree_ptr;
340 int max_ndx, i;
341 int keys[8192]; /* 32k size on stack */
342 do {
343 if (!sptree) break;
344 max_ndx = 0;
345 mod_vhostdb_tag_old_entries(sptree, keys, &max_ndx, max_age, cur_ts);
346 for (i = 0; i < max_ndx; ++i) {
347 int ndx = keys[i];
348 sptree = splaytree_splay(sptree, ndx);
349 if (sptree && sptree->key == ndx) {
350 vhostdb_cache_entry_free(sptree->data);
351 sptree = splaytree_delete(sptree, ndx);
352 }
353 }
354 } while (max_ndx == sizeof(keys)/sizeof(int));
355 *sptree_ptr = sptree;
356 }
357
TRIGGER_FUNC(mod_vhostdb_periodic)358 TRIGGER_FUNC(mod_vhostdb_periodic)
359 {
360 const plugin_data * const p = p_d;
361 const unix_time64_t cur_ts = log_monotonic_secs;
362 if (cur_ts & 0x7) return HANDLER_GO_ON; /*(continue once each 8 sec)*/
363 UNUSED(srv);
364
365 /* future: might construct array of (vhostdb_cache *) at startup
366 * to avoid the need to search for them here */
367 /* (init i to 0 if global context; to 1 to skip empty global context) */
368 if (NULL == p->cvlist) return HANDLER_GO_ON;
369 for (int i = !p->cvlist[0].v.u2[1], used = p->nconfig; i < used; ++i) {
370 const config_plugin_value_t *cpv = p->cvlist + p->cvlist[i].v.u2[0];
371 for (; cpv->k_id != -1; ++cpv) {
372 if (cpv->k_id != 1) continue; /* k_id == 1 for vhostdb.cache */
373 if (cpv->vtype != T_CONFIG_LOCAL) continue;
374 vhostdb_cache *vc = cpv->v.v;
375 mod_vhostdb_periodic_cleanup(&vc->sptree, vc->max_age, cur_ts);
376 }
377 }
378
379 return HANDLER_GO_ON;
380 }
381
382
383 __attribute_cold__
384 int mod_vhostdb_plugin_init(plugin *p);
mod_vhostdb_plugin_init(plugin * p)385 int mod_vhostdb_plugin_init(plugin *p) {
386 p->version = LIGHTTPD_VERSION_ID;
387 p->name = "vhostdb";
388 p->init = mod_vhostdb_init;
389 p->cleanup = mod_vhostdb_free;
390 p->set_defaults = mod_vhostdb_set_defaults;
391 p->handle_trigger = mod_vhostdb_periodic;
392 p->handle_docroot = mod_vhostdb_handle_docroot;
393 p->handle_request_reset = mod_vhostdb_handle_request_reset;
394
395 return 0;
396 }
397