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