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 "plugin_config.h" 12 #include "http_vhostdb.h" 13 #include "log.h" 14 #include "stat_cache.h" 15 #include "algo_splaytree.h" 16 17 #include <stdlib.h> 18 #include <string.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 39 buffer tmp_buf; 40 } plugin_data; 41 42 typedef struct { 43 char *server_name; 44 char *document_root; 45 uint32_t slen; 46 uint32_t dlen; 47 time_t ctime; 48 } vhostdb_cache_entry; 49 50 static vhostdb_cache_entry * 51 vhostdb_cache_entry_init (const buffer * const server_name, const buffer * const docroot) 52 { 53 const uint32_t slen = buffer_string_length(server_name); 54 const uint32_t dlen = buffer_string_length(docroot); 55 vhostdb_cache_entry * const ve = 56 malloc(sizeof(vhostdb_cache_entry) + slen + dlen); 57 ve->ctime = log_epoch_secs; 58 ve->slen = slen; 59 ve->dlen = dlen; 60 ve->server_name = (char *)(ve + 1); 61 ve->document_root = ve->server_name + slen; 62 memcpy(ve->server_name, server_name->ptr, slen); 63 memcpy(ve->document_root, docroot->ptr, dlen); 64 return ve; 65 } 66 67 static void 68 vhostdb_cache_entry_free (vhostdb_cache_entry *ve) 69 { 70 free(ve); 71 } 72 73 static void 74 vhostdb_cache_free (vhostdb_cache *vc) 75 { 76 splay_tree *sptree = vc->sptree; 77 while (sptree) { 78 vhostdb_cache_entry_free(sptree->data); 79 sptree = splaytree_delete(sptree, sptree->key); 80 } 81 free(vc); 82 } 83 84 static vhostdb_cache * 85 vhostdb_cache_init (const array *opts) 86 { 87 vhostdb_cache *vc = malloc(sizeof(vhostdb_cache)); 88 force_assert(vc); 89 vc->sptree = NULL; 90 vc->max_age = 600; /* 10 mins */ 91 for (uint32_t i = 0, used = opts->used; i < used; ++i) { 92 data_unset *du = opts->data[i]; 93 if (buffer_is_equal_string(&du->key, CONST_STR_LEN("max-age"))) 94 vc->max_age = (time_t)config_plugin_value_to_int32(du, vc->max_age); 95 } 96 return vc; 97 } 98 99 static vhostdb_cache_entry * 100 mod_vhostdb_cache_query (request_st * const r, plugin_data * const p) 101 { 102 const int ndx = splaytree_djbhash(CONST_BUF_LEN(&r->uri.authority)); 103 splay_tree ** const sptree = &p->conf.vhostdb_cache->sptree; 104 *sptree = splaytree_splay(*sptree, ndx); 105 vhostdb_cache_entry * const ve = 106 (*sptree && (*sptree)->key == ndx) ? (*sptree)->data : NULL; 107 108 return ve 109 && buffer_is_equal_string(&r->uri.authority, ve->server_name, ve->slen) 110 ? ve 111 : NULL; 112 } 113 114 static void 115 mod_vhostdb_cache_insert (request_st * const r, plugin_data * const p, vhostdb_cache_entry * const ve) 116 { 117 const int ndx = splaytree_djbhash(CONST_BUF_LEN(&r->uri.authority)); 118 splay_tree ** const sptree = &p->conf.vhostdb_cache->sptree; 119 /*(not necessary to re-splay (with current usage) since single-threaded 120 * and splaytree has not been modified since mod_vhostdb_cache_query())*/ 121 /* *sptree = splaytree_splay(*sptree, ndx); */ 122 if (NULL == *sptree || (*sptree)->key != ndx) 123 *sptree = splaytree_insert(*sptree, ndx, ve); 124 else { /* collision; replace old entry */ 125 vhostdb_cache_entry_free((*sptree)->data); 126 (*sptree)->data = ve; 127 } 128 } 129 130 INIT_FUNC(mod_vhostdb_init) { 131 return calloc(1, sizeof(plugin_data)); 132 } 133 134 FREE_FUNC(mod_vhostdb_free) { 135 plugin_data *p = p_d; 136 free(p->tmp_buf.ptr); 137 138 if (NULL == p->cvlist) return; 139 /* (init i to 0 if global context; to 1 to skip empty global context) */ 140 for (int i = !p->cvlist[0].v.u2[1], used = p->nconfig; i < used; ++i) { 141 config_plugin_value_t *cpv = p->cvlist + p->cvlist[i].v.u2[0]; 142 for (; -1 != cpv->k_id; ++cpv) { 143 if (cpv->vtype != T_CONFIG_LOCAL || NULL == cpv->v.v) continue; 144 switch (cpv->k_id) { 145 case 1: /* vhostdb.cache */ 146 vhostdb_cache_free(cpv->v.v); 147 break; 148 default: 149 break; 150 } 151 } 152 } 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_string_is_empty(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 r->server_name = &r->server_name_buf; 263 buffer_copy_string_len(&r->server_name_buf, ve->server_name, ve->slen); 264 buffer_copy_string_len(&r->physical.doc_root, ve->document_root, ve->dlen); 265 return HANDLER_GO_ON; 266 } 267 268 REQUEST_FUNC(mod_vhostdb_handle_docroot) { 269 plugin_data *p = p_d; 270 vhostdb_cache_entry *ve; 271 const http_vhostdb_backend_t *backend; 272 buffer *b; 273 274 /* no host specified? */ 275 if (buffer_string_is_empty(&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 b = &p->tmp_buf; 290 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_string_is_empty(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 time_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 time_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 time_t cur_ts = log_epoch_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 for (int i = 0, used = p->nconfig; i < used; ++i) { 369 const config_plugin_value_t *cpv = p->cvlist + p->cvlist[i].v.u2[0]; 370 for (; cpv->k_id != -1; ++cpv) { 371 if (cpv->k_id != 1) continue; /* k_id == 1 for vhostdb.cache */ 372 if (cpv->vtype != T_CONFIG_LOCAL) continue; 373 vhostdb_cache *vc = cpv->v.v; 374 mod_vhostdb_periodic_cleanup(&vc->sptree, vc->max_age, cur_ts); 375 } 376 } 377 378 return HANDLER_GO_ON; 379 } 380 381 382 int mod_vhostdb_plugin_init(plugin *p); 383 int mod_vhostdb_plugin_init(plugin *p) { 384 p->version = LIGHTTPD_VERSION_ID; 385 p->name = "vhostdb"; 386 p->init = mod_vhostdb_init; 387 p->cleanup = mod_vhostdb_free; 388 p->set_defaults = mod_vhostdb_set_defaults; 389 p->handle_trigger = mod_vhostdb_periodic; 390 p->handle_docroot = mod_vhostdb_handle_docroot; 391 p->handle_request_reset = mod_vhostdb_handle_request_reset; 392 393 return 0; 394 } 395