xref: /lighttpd1.4/src/mod_vhostdb.c (revision 2e0edb85)
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 *
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       malloc(sizeof(vhostdb_cache_entry) + slen + dlen);
55     force_assert(ve);
56     ve->ctime = log_monotonic_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_unset *du = opts->data[i];
92         if (buffer_is_equal_string(&du->key, CONST_STR_LEN("max-age")))
93             vc->max_age = (time_t)config_plugin_value_to_int32(du, vc->max_age);
94     }
95     return vc;
96 }
97 
98 static vhostdb_cache_entry *
99 mod_vhostdb_cache_query (request_st * const r, plugin_data * const p)
100 {
101     const int ndx = splaytree_djbhash(BUF_PTR_LEN(&r->uri.authority));
102     splay_tree ** const sptree = &p->conf.vhostdb_cache->sptree;
103     *sptree = splaytree_splay(*sptree, ndx);
104     vhostdb_cache_entry * const ve =
105       (*sptree && (*sptree)->key == ndx) ? (*sptree)->data : NULL;
106 
107     return ve
108         && buffer_is_equal_string(&r->uri.authority, ve->server_name, ve->slen)
109       ? ve
110       : NULL;
111 }
112 
113 static void
114 mod_vhostdb_cache_insert (request_st * const r, plugin_data * const p, vhostdb_cache_entry * const ve)
115 {
116     const int ndx = splaytree_djbhash(BUF_PTR_LEN(&r->uri.authority));
117     splay_tree ** const sptree = &p->conf.vhostdb_cache->sptree;
118     /*(not necessary to re-splay (with current usage) since single-threaded
119      * and splaytree has not been modified since mod_vhostdb_cache_query())*/
120     /* *sptree = splaytree_splay(*sptree, ndx); */
121     if (NULL == *sptree || (*sptree)->key != ndx)
122         *sptree = splaytree_insert(*sptree, ndx, ve);
123     else { /* collision; replace old entry */
124         vhostdb_cache_entry_free((*sptree)->data);
125         (*sptree)->data = ve;
126     }
127 }
128 
129 INIT_FUNC(mod_vhostdb_init) {
130     return calloc(1, sizeof(plugin_data));
131 }
132 
133 FREE_FUNC(mod_vhostdb_free) {
134     plugin_data *p = p_d;
135 
136     if (NULL == p->cvlist) return;
137     /* (init i to 0 if global context; to 1 to skip empty global context) */
138     for (int i = !p->cvlist[0].v.u2[1], used = p->nconfig; i < used; ++i) {
139         config_plugin_value_t *cpv = p->cvlist + p->cvlist[i].v.u2[0];
140         for (; -1 != cpv->k_id; ++cpv) {
141             if (cpv->vtype != T_CONFIG_LOCAL || NULL == cpv->v.v) continue;
142             switch (cpv->k_id) {
143               case 1: /* vhostdb.cache */
144                 vhostdb_cache_free(cpv->v.v);
145                 break;
146               default:
147                 break;
148             }
149         }
150     }
151 
152     http_vhostdb_dumbdata_reset();
153 }
154 
155 static void mod_vhostdb_merge_config_cpv(plugin_config * const pconf, const config_plugin_value_t * const cpv) {
156     switch (cpv->k_id) { /* index into static config_plugin_keys_t cpk[] */
157       case 0: /* vhostdb.backend */
158         if (cpv->vtype == T_CONFIG_LOCAL)
159             pconf->vhostdb_backend = cpv->v.v;
160         break;
161       case 1: /* vhostdb.cache */
162         if (cpv->vtype == T_CONFIG_LOCAL)
163             pconf->vhostdb_cache = cpv->v.v;
164         break;
165       default:/* should not happen */
166         return;
167     }
168 }
169 
170 static void mod_vhostdb_merge_config(plugin_config * const pconf, const config_plugin_value_t *cpv) {
171     do {
172         mod_vhostdb_merge_config_cpv(pconf, cpv);
173     } while ((++cpv)->k_id != -1);
174 }
175 
176 static void mod_vhostdb_patch_config(request_st * const r, plugin_data * const p) {
177     memcpy(&p->conf, &p->defaults, sizeof(plugin_config));
178     for (int i = 1, used = p->nconfig; i < used; ++i) {
179         if (config_check_cond(r, (uint32_t)p->cvlist[i].k_id))
180             mod_vhostdb_merge_config(&p->conf, p->cvlist + p->cvlist[i].v.u2[0]);
181     }
182 }
183 
184 SETDEFAULTS_FUNC(mod_vhostdb_set_defaults) {
185     static const config_plugin_keys_t cpk[] = {
186       { CONST_STR_LEN("vhostdb.backend"),
187         T_CONFIG_STRING,
188         T_CONFIG_SCOPE_CONNECTION }
189      ,{ CONST_STR_LEN("vhostdb.cache"),
190         T_CONFIG_ARRAY,
191         T_CONFIG_SCOPE_CONNECTION }
192      ,{ NULL, 0,
193         T_CONFIG_UNSET,
194         T_CONFIG_SCOPE_UNSET }
195     };
196 
197     plugin_data * const p = p_d;
198     if (!config_plugin_values_init(srv, p, cpk, "mod_vhostdb"))
199         return HANDLER_ERROR;
200 
201     /* process and validate config directives
202      * (init i to 0 if global context; to 1 to skip empty global context) */
203     for (int i = !p->cvlist[0].v.u2[1]; i < p->nconfig; ++i) {
204         config_plugin_value_t *cpv = p->cvlist + p->cvlist[i].v.u2[0];
205         for (; -1 != cpv->k_id; ++cpv) {
206             switch (cpv->k_id) {
207               case 0: /* vhostdb.backend */
208                 if (!buffer_is_blank(cpv->v.b)) {
209                     const buffer * const b = cpv->v.b;
210                     *(const void **)&cpv->v.v = http_vhostdb_backend_get(b);
211                     if (NULL == cpv->v.v) {
212                         log_error(srv->errh, __FILE__, __LINE__,
213                           "vhostdb.backend not supported: %s", b->ptr);
214                         return HANDLER_ERROR;
215                     }
216                     cpv->vtype = T_CONFIG_LOCAL;
217                 }
218                 break;
219               case 1: /* vhostdb.cache */
220                 cpv->v.v = vhostdb_cache_init(cpv->v.a);
221                 cpv->vtype = T_CONFIG_LOCAL;
222                 break;
223               default:/* should not happen */
224                 break;
225             }
226         }
227     }
228 
229     /* initialize p->defaults from global config context */
230     if (p->nconfig > 0 && p->cvlist->v.u2[1]) {
231         const config_plugin_value_t *cpv = p->cvlist + p->cvlist->v.u2[0];
232         if (-1 != cpv->k_id)
233             mod_vhostdb_merge_config(&p->defaults, cpv);
234     }
235 
236     return HANDLER_GO_ON;
237 }
238 
239 REQUEST_FUNC(mod_vhostdb_handle_request_reset) {
240     plugin_data *p = p_d;
241     vhostdb_cache_entry *ve;
242 
243     if ((ve = r->plugin_ctx[p->id])) {
244         r->plugin_ctx[p->id] = NULL;
245         vhostdb_cache_entry_free(ve);
246     }
247 
248     return HANDLER_GO_ON;
249 }
250 
251 __attribute_cold__
252 static handler_t mod_vhostdb_error_500 (request_st * const r)
253 {
254     r->http_status = 500; /* Internal Server Error */
255     r->handler_module = NULL;
256     return HANDLER_FINISHED;
257 }
258 
259 static handler_t mod_vhostdb_found (request_st * const r, vhostdb_cache_entry * const ve)
260 {
261     /* fix virtual server and docroot */
262     if (ve->slen) {
263         r->server_name = &r->server_name_buf;
264         buffer_copy_string_len(&r->server_name_buf, ve->server_name, ve->slen);
265     }
266     buffer_copy_string_len(&r->physical.doc_root, ve->document_root, ve->dlen);
267     return HANDLER_GO_ON;
268 }
269 
270 REQUEST_FUNC(mod_vhostdb_handle_docroot) {
271     plugin_data *p = p_d;
272     vhostdb_cache_entry *ve;
273 
274     /* no host specified? */
275     if (buffer_is_blank(&r->uri.authority)) return HANDLER_GO_ON;
276 
277     /* check if cached this connection */
278     ve = r->plugin_ctx[p->id];
279     if (ve
280         && buffer_is_equal_string(&r->uri.authority, ve->server_name, ve->slen))
281         return mod_vhostdb_found(r, ve); /* HANDLER_GO_ON */
282 
283     mod_vhostdb_patch_config(r, p);
284     if (!p->conf.vhostdb_backend) return HANDLER_GO_ON;
285 
286     if (p->conf.vhostdb_cache && (ve = mod_vhostdb_cache_query(r, p)))
287         return mod_vhostdb_found(r, ve); /* HANDLER_GO_ON */
288 
289     buffer * const b = r->tmp_buf; /*(cleared before use in backend->query())*/
290     const http_vhostdb_backend_t * const backend = p->conf.vhostdb_backend;
291     if (0 != backend->query(r, backend->p_d, b)) {
292         return mod_vhostdb_error_500(r); /* HANDLER_FINISHED */
293     }
294 
295     if (buffer_is_blank(b)) {
296         /* no such virtual host */
297         return HANDLER_GO_ON;
298     }
299 
300     /* sanity check that really is a directory */
301     buffer_append_slash(b);
302     if (!stat_cache_path_isdir(b)) {
303         log_perror(r->conf.errh, __FILE__, __LINE__, "%s", b->ptr);
304         return mod_vhostdb_error_500(r); /* HANDLER_FINISHED */
305     }
306 
307     if (ve && !p->conf.vhostdb_cache)
308         vhostdb_cache_entry_free(ve);
309 
310     ve = vhostdb_cache_entry_init(&r->uri.authority, b);
311 
312     if (!p->conf.vhostdb_cache)
313         r->plugin_ctx[p->id] = ve;
314     else
315         mod_vhostdb_cache_insert(r, p, ve);
316 
317     return mod_vhostdb_found(r, ve); /* HANDLER_GO_ON */
318 }
319 
320 /* walk though cache, collect expired ids, and remove them in a second loop */
321 static void
322 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)
323 {
324     if (*ndx == 8192) return; /*(must match num array entries in keys[])*/
325     if (t->left)
326         mod_vhostdb_tag_old_entries(t->left, keys, ndx, max_age, cur_ts);
327     if (t->right)
328         mod_vhostdb_tag_old_entries(t->right, keys, ndx, max_age, cur_ts);
329     if (*ndx == 8192) return; /*(must match num array entries in keys[])*/
330 
331     const vhostdb_cache_entry * const ve = t->data;
332     if (cur_ts - ve->ctime > max_age)
333         keys[(*ndx)++] = t->key;
334 }
335 
336 __attribute_noinline__
337 static void
338 mod_vhostdb_periodic_cleanup(splay_tree **sptree_ptr, const time_t max_age, const unix_time64_t cur_ts)
339 {
340     splay_tree *sptree = *sptree_ptr;
341     int max_ndx, i;
342     int keys[8192]; /* 32k size on stack */
343     do {
344         if (!sptree) break;
345         max_ndx = 0;
346         mod_vhostdb_tag_old_entries(sptree, keys, &max_ndx, max_age, cur_ts);
347         for (i = 0; i < max_ndx; ++i) {
348             int ndx = keys[i];
349             sptree = splaytree_splay(sptree, ndx);
350             if (sptree && sptree->key == ndx) {
351                 vhostdb_cache_entry_free(sptree->data);
352                 sptree = splaytree_delete(sptree, ndx);
353             }
354         }
355     } while (max_ndx == sizeof(keys)/sizeof(int));
356     *sptree_ptr = sptree;
357 }
358 
359 TRIGGER_FUNC(mod_vhostdb_periodic)
360 {
361     const plugin_data * const p = p_d;
362     const unix_time64_t cur_ts = log_monotonic_secs;
363     if (cur_ts & 0x7) return HANDLER_GO_ON; /*(continue once each 8 sec)*/
364     UNUSED(srv);
365 
366     /* future: might construct array of (vhostdb_cache *) at startup
367      *         to avoid the need to search for them here */
368     /* (init i to 0 if global context; to 1 to skip empty global context) */
369     if (NULL == p->cvlist) return HANDLER_GO_ON;
370     for (int i = !p->cvlist[0].v.u2[1], used = p->nconfig; i < used; ++i) {
371         const config_plugin_value_t *cpv = p->cvlist + p->cvlist[i].v.u2[0];
372         for (; cpv->k_id != -1; ++cpv) {
373             if (cpv->k_id != 1) continue; /* k_id == 1 for vhostdb.cache */
374             if (cpv->vtype != T_CONFIG_LOCAL) continue;
375             vhostdb_cache *vc = cpv->v.v;
376             mod_vhostdb_periodic_cleanup(&vc->sptree, vc->max_age, cur_ts);
377         }
378     }
379 
380     return HANDLER_GO_ON;
381 }
382 
383 
384 int 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