1 #include "first.h" 2 3 #include "plugin.h" 4 #include "base.h" 5 #include "array.h" 6 #include "log.h" 7 8 #include <string.h> 9 #include <stdlib.h> 10 11 array plugin_stats; /* global */ 12 13 #ifdef HAVE_VALGRIND_VALGRIND_H 14 # include <valgrind/valgrind.h> 15 #endif 16 17 #if !defined(__WIN32) && !defined(LIGHTTPD_STATIC) 18 # include <dlfcn.h> 19 #endif 20 /* 21 * 22 * if you change this enum to add a new callback, be sure 23 * - that PLUGIN_FUNC_SIZEOF is the last entry 24 * - that you add: 25 * 1. PLUGIN_CALL_... as callback-dispatcher 26 * 2. count and assignment in plugins_call_init() 27 * 28 */ 29 30 typedef enum { 31 PLUGIN_FUNC_HANDLE_URI_CLEAN, 32 PLUGIN_FUNC_HANDLE_DOCROOT, 33 PLUGIN_FUNC_HANDLE_PHYSICAL, 34 PLUGIN_FUNC_HANDLE_SUBREQUEST_START, 35 /* PLUGIN_FUNC_HANDLE_SUBREQUEST, *//* max one handler_module per req */ 36 PLUGIN_FUNC_HANDLE_RESPONSE_START, 37 PLUGIN_FUNC_HANDLE_REQUEST_DONE, 38 PLUGIN_FUNC_HANDLE_REQUEST_RESET, 39 PLUGIN_FUNC_HANDLE_REQUEST_ENV, 40 PLUGIN_FUNC_HANDLE_CONNECTION_ACCEPT, 41 PLUGIN_FUNC_HANDLE_CONNECTION_SHUT_WR, 42 PLUGIN_FUNC_HANDLE_CONNECTION_CLOSE, 43 PLUGIN_FUNC_HANDLE_TRIGGER, 44 PLUGIN_FUNC_HANDLE_WAITPID, 45 PLUGIN_FUNC_HANDLE_SIGHUP, 46 /* PLUGIN_FUNC_INIT, *//* handled here in plugin.c */ 47 /* PLUGIN_FUNC_CLEANUP, *//* handled here in plugin.c */ 48 PLUGIN_FUNC_SET_DEFAULTS, 49 PLUGIN_FUNC_WORKER_INIT, 50 51 PLUGIN_FUNC_SIZEOF 52 } plugin_t; 53 54 static plugin *plugin_init(void) { 55 plugin *p; 56 57 p = calloc(1, sizeof(*p)); 58 force_assert(NULL != p); 59 60 return p; 61 } 62 63 static void plugin_free(plugin *p) { 64 if (NULL == p) return; /*(should not happen w/ current usage)*/ 65 #if !defined(LIGHTTPD_STATIC) 66 if (p->lib) { 67 #if defined(HAVE_VALGRIND_VALGRIND_H) 68 /*if (!RUNNING_ON_VALGRIND) */ 69 #endif 70 #if defined(__WIN32) 71 FreeLibrary(p->lib); 72 #else 73 dlclose(p->lib); 74 #endif 75 } 76 #endif 77 78 free(p); 79 } 80 81 static void plugins_register(server *srv, plugin *p) { 82 plugin **ps; 83 if (srv->plugins.used == srv->plugins.size) { 84 srv->plugins.size += 4; 85 srv->plugins.ptr = realloc(srv->plugins.ptr, srv->plugins.size * sizeof(*ps)); 86 force_assert(NULL != srv->plugins.ptr); 87 } 88 89 ps = srv->plugins.ptr; 90 ps[srv->plugins.used++] = p; 91 } 92 93 /** 94 * 95 * 96 * 97 */ 98 99 #if defined(LIGHTTPD_STATIC) 100 101 /* pre-declare functions, as there is no header for them */ 102 #define PLUGIN_INIT(x)\ 103 int x ## _plugin_init(plugin *p); 104 105 #include "plugin-static.h" 106 107 #undef PLUGIN_INIT 108 109 /* build NULL-terminated table of name + init-function */ 110 111 typedef struct { 112 const char* name; 113 int (*plugin_init)(plugin *p); 114 } plugin_load_functions; 115 116 static const plugin_load_functions load_functions[] = { 117 #define PLUGIN_INIT(x) \ 118 { #x, &x ## _plugin_init }, 119 120 #include "plugin-static.h" 121 122 { NULL, NULL } 123 #undef PLUGIN_INIT 124 }; 125 126 int plugins_load(server *srv) { 127 for (uint32_t i = 0; i < srv->srvconf.modules->used; ++i) { 128 data_string *ds = (data_string *)srv->srvconf.modules->data[i]; 129 char *module = ds->value.ptr; 130 131 uint32_t j; 132 for (j = 0; load_functions[j].name; ++j) { 133 if (0 == strcmp(load_functions[j].name, module)) { 134 plugin * const p = plugin_init(); 135 if ((*load_functions[j].plugin_init)(p)) { 136 log_error(srv->errh, __FILE__, __LINE__, "%s plugin init failed", module); 137 plugin_free(p); 138 return -1; 139 } 140 plugins_register(srv, p); 141 break; 142 } 143 } 144 if (!load_functions[j].name) { 145 log_error(srv->errh, __FILE__, __LINE__, "%s plugin not found", module); 146 return -1; 147 } 148 } 149 150 return 0; 151 } 152 #else /* defined(LIGHTTPD_STATIC) */ 153 int plugins_load(server *srv) { 154 buffer * const tb = srv->tmp_buf; 155 plugin *p; 156 int (*init)(plugin *pl); 157 158 for (uint32_t i = 0; i < srv->srvconf.modules->used; ++i) { 159 const buffer * const module = &((data_string *)srv->srvconf.modules->data[i])->value; 160 buffer_copy_path_len2(tb, BUF_PTR_LEN(srv->srvconf.modules_dir), 161 BUF_PTR_LEN(module)); 162 #if defined(__WIN32) || defined(__CYGWIN__) 163 buffer_append_string_len(tb, CONST_STR_LEN(".dll")); 164 #else 165 buffer_append_string_len(tb, CONST_STR_LEN(".so")); 166 #endif 167 168 p = plugin_init(); 169 #ifdef __WIN32 170 if (NULL == (p->lib = LoadLibrary(tb->ptr))) { 171 LPVOID lpMsgBuf; 172 FormatMessage( 173 FORMAT_MESSAGE_ALLOCATE_BUFFER | 174 FORMAT_MESSAGE_FROM_SYSTEM, 175 NULL, 176 GetLastError(), 177 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), 178 (LPTSTR) &lpMsgBuf, 179 0, NULL); 180 181 log_error(srv->errh, __FILE__, __LINE__, 182 "LoadLibrary() failed %s %s", lpMsgBuf, tb->ptr); 183 184 plugin_free(p); 185 186 return -1; 187 188 } 189 #else 190 if (NULL == (p->lib = dlopen(tb->ptr, RTLD_NOW|RTLD_GLOBAL))) { 191 log_error(srv->errh, __FILE__, __LINE__, 192 "dlopen() failed for: %s %s", tb->ptr, dlerror()); 193 194 plugin_free(p); 195 196 return -1; 197 } 198 199 #endif 200 buffer_clear(tb); 201 buffer_append_str2(tb, BUF_PTR_LEN(module), 202 CONST_STR_LEN("_plugin_init")); 203 204 #ifdef __WIN32 205 init = GetProcAddress(p->lib, tb->ptr); 206 207 if (init == NULL) { 208 LPVOID lpMsgBuf; 209 FormatMessage( 210 FORMAT_MESSAGE_ALLOCATE_BUFFER | 211 FORMAT_MESSAGE_FROM_SYSTEM, 212 NULL, 213 GetLastError(), 214 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), 215 (LPTSTR) &lpMsgBuf, 216 0, NULL); 217 218 log_error(srv->errh, __FILE__, __LINE__, 219 "getprocaddress failed: %s %s", tb->ptr, lpMsgBuf); 220 221 plugin_free(p); 222 return -1; 223 } 224 225 #else 226 #if 1 227 init = (int (*)(plugin *))(intptr_t)dlsym(p->lib, tb->ptr); 228 #else 229 *(void **)(&init) = dlsym(p->lib, tb->ptr); 230 #endif 231 if (NULL == init) { 232 const char *error = dlerror(); 233 if (error != NULL) { 234 log_error(srv->errh, __FILE__, __LINE__, "dlsym: %s", error); 235 } else { 236 log_error(srv->errh, __FILE__, __LINE__, "dlsym symbol not found: %s", tb->ptr); 237 } 238 239 plugin_free(p); 240 return -1; 241 } 242 243 #endif 244 if ((*init)(p)) { 245 log_error(srv->errh, __FILE__, __LINE__, "%s plugin init failed", module->ptr); 246 247 plugin_free(p); 248 return -1; 249 } 250 #if 0 251 log_error(srv->errh, __FILE__, __LINE__, "%s plugin loaded", module->ptr); 252 #endif 253 plugins_register(srv, p); 254 } 255 256 return 0; 257 } 258 #endif /* defined(LIGHTTPD_STATIC) */ 259 260 typedef struct { 261 handler_t(*fn)(); 262 plugin_data_base *data; 263 } plugin_fn_data; 264 265 __attribute_hot__ 266 static handler_t plugins_call_fn_req_data(request_st * const r, const int e) { 267 const void * const plugin_slots = r->con->plugin_slots; 268 const uint32_t offset = ((const uint16_t *)plugin_slots)[e]; 269 if (0 == offset) return HANDLER_GO_ON; 270 const plugin_fn_data *plfd = (const plugin_fn_data *) 271 (((uintptr_t)plugin_slots) + offset); 272 handler_t rc = HANDLER_GO_ON; 273 while (plfd->fn && (rc = plfd->fn(r, plfd->data)) == HANDLER_GO_ON) 274 ++plfd; 275 return rc; 276 } 277 278 __attribute_hot__ 279 static handler_t plugins_call_fn_con_data(connection * const con, const int e) { 280 const void * const plugin_slots = con->plugin_slots; 281 const uint32_t offset = ((const uint16_t *)plugin_slots)[e]; 282 if (0 == offset) return HANDLER_GO_ON; 283 const plugin_fn_data *plfd = (const plugin_fn_data *) 284 (((uintptr_t)plugin_slots) + offset); 285 handler_t rc = HANDLER_GO_ON; 286 while (plfd->fn && (rc = plfd->fn(con, plfd->data)) == HANDLER_GO_ON) 287 ++plfd; 288 return rc; 289 } 290 291 static handler_t plugins_call_fn_srv_data(server * const srv, const int e) { 292 const uint32_t offset = ((const uint16_t *)srv->plugin_slots)[e]; 293 if (0 == offset) return HANDLER_GO_ON; 294 const plugin_fn_data *plfd = (const plugin_fn_data *) 295 (((uintptr_t)srv->plugin_slots) + offset); 296 handler_t rc = HANDLER_GO_ON; 297 while (plfd->fn && (rc = plfd->fn(srv,plfd->data)) == HANDLER_GO_ON) 298 ++plfd; 299 return rc; 300 } 301 302 static void plugins_call_fn_srv_data_all(server * const srv, const int e) { 303 const uint32_t offset = ((const uint16_t *)srv->plugin_slots)[e]; 304 if (0 == offset) return; 305 const plugin_fn_data *plfd = (const plugin_fn_data *) 306 (((uintptr_t)srv->plugin_slots) + offset); 307 for (; plfd->fn; ++plfd) 308 plfd->fn(srv, plfd->data); 309 } 310 311 /** 312 * plugins that use 313 * 314 * - request_st *r 315 * - void *p_d (plugin_data *) 316 */ 317 318 #define PLUGIN_CALL_FN_REQ_DATA(x, y) \ 319 handler_t plugins_call_##y(request_st * const r) {\ 320 return plugins_call_fn_req_data(r, x); \ 321 } 322 323 PLUGIN_CALL_FN_REQ_DATA(PLUGIN_FUNC_HANDLE_URI_CLEAN, handle_uri_clean) 324 PLUGIN_CALL_FN_REQ_DATA(PLUGIN_FUNC_HANDLE_DOCROOT, handle_docroot) 325 PLUGIN_CALL_FN_REQ_DATA(PLUGIN_FUNC_HANDLE_PHYSICAL, handle_physical) 326 PLUGIN_CALL_FN_REQ_DATA(PLUGIN_FUNC_HANDLE_SUBREQUEST_START, handle_subrequest_start) 327 PLUGIN_CALL_FN_REQ_DATA(PLUGIN_FUNC_HANDLE_RESPONSE_START, handle_response_start) 328 PLUGIN_CALL_FN_REQ_DATA(PLUGIN_FUNC_HANDLE_REQUEST_DONE, handle_request_done) 329 PLUGIN_CALL_FN_REQ_DATA(PLUGIN_FUNC_HANDLE_REQUEST_RESET, handle_request_reset) 330 PLUGIN_CALL_FN_REQ_DATA(PLUGIN_FUNC_HANDLE_REQUEST_ENV, handle_request_env) 331 332 /** 333 * plugins that use 334 * 335 * - connection *con 336 * - void *p_d (plugin_data *) 337 */ 338 339 #define PLUGIN_CALL_FN_CON_DATA(x, y) \ 340 handler_t plugins_call_##y(connection *con) {\ 341 return plugins_call_fn_con_data(con, x); \ 342 } 343 344 PLUGIN_CALL_FN_CON_DATA(PLUGIN_FUNC_HANDLE_CONNECTION_ACCEPT, handle_connection_accept) 345 PLUGIN_CALL_FN_CON_DATA(PLUGIN_FUNC_HANDLE_CONNECTION_SHUT_WR, handle_connection_shut_wr) 346 PLUGIN_CALL_FN_CON_DATA(PLUGIN_FUNC_HANDLE_CONNECTION_CLOSE, handle_connection_close) 347 348 #undef PLUGIN_CALL_FN_SRV_CON_DATA 349 350 /** 351 * plugins that use 352 * 353 * - server *srv 354 * - void *p_d (plugin_data *) 355 */ 356 357 handler_t plugins_call_set_defaults(server *srv) { 358 return plugins_call_fn_srv_data(srv, PLUGIN_FUNC_SET_DEFAULTS); 359 } 360 361 handler_t plugins_call_worker_init(server *srv) { 362 return plugins_call_fn_srv_data(srv, PLUGIN_FUNC_WORKER_INIT); 363 } 364 365 void plugins_call_handle_trigger(server *srv) { 366 plugins_call_fn_srv_data_all(srv, PLUGIN_FUNC_HANDLE_TRIGGER); 367 } 368 369 void plugins_call_handle_sighup(server *srv) { 370 plugins_call_fn_srv_data_all(srv, PLUGIN_FUNC_HANDLE_SIGHUP); 371 } 372 373 handler_t plugins_call_handle_waitpid(server *srv, pid_t pid, int status) { 374 const uint32_t offset = 375 ((const uint16_t *)srv->plugin_slots)[PLUGIN_FUNC_HANDLE_WAITPID]; 376 if (0 == offset) return HANDLER_GO_ON; 377 const plugin_fn_data *plfd = (const plugin_fn_data *) 378 (((uintptr_t)srv->plugin_slots) + offset); 379 handler_t rc = HANDLER_GO_ON; 380 while (plfd->fn&&(rc=plfd->fn(srv,plfd->data,pid,status))==HANDLER_GO_ON) 381 ++plfd; 382 return rc; 383 } 384 385 static void plugins_call_cleanup(server * const srv) { 386 plugin ** const ps = srv->plugins.ptr; 387 for (uint32_t i = 0; i < srv->plugins.used; ++i) { 388 plugin *p = ps[i]; 389 if (NULL == p) continue; 390 if (NULL != p->data) { 391 plugin_data_base *pd = p->data; 392 if (p->cleanup) 393 p->cleanup(p->data); 394 free(pd->cvlist); 395 free(pd); 396 p->data = NULL; 397 } 398 } 399 } 400 401 /** 402 * 403 * - call init function of all plugins to init the plugin-internals 404 * - added each plugin that supports has callback to the corresponding slot 405 * 406 * - is only called once. 407 */ 408 409 __attribute_cold__ 410 static void plugins_call_init_slot(server *srv, handler_t(*fn)(), void *data, const uint32_t offset) { 411 if (fn) { 412 plugin_fn_data *plfd = (plugin_fn_data *) 413 (((uintptr_t)srv->plugin_slots) + offset); 414 while (plfd->fn) ++plfd; 415 plfd->fn = fn; 416 plfd->data = data; 417 } 418 } 419 420 handler_t plugins_call_init(server *srv) { 421 plugin ** const ps = srv->plugins.ptr; 422 uint16_t offsets[PLUGIN_FUNC_SIZEOF]; 423 memset(offsets, 0, sizeof(offsets)); 424 425 for (uint32_t i = 0; i < srv->plugins.used; ++i) { 426 /* check which calls are supported */ 427 428 plugin *p = ps[i]; 429 430 if (p->init) { 431 if (NULL == (p->data = p->init())) { 432 log_error(srv->errh, __FILE__, __LINE__, 433 "plugin-init failed for module %s", p->name); 434 return HANDLER_ERROR; 435 } 436 437 ((plugin_data_base *)(p->data))->self = p; 438 ((plugin_data_base *)(p->data))->id = i + 1; 439 440 if (p->version != LIGHTTPD_VERSION_ID) { 441 log_error(srv->errh, __FILE__, __LINE__, 442 "plugin-version doesn't match lighttpd-version for %s", p->name); 443 return HANDLER_ERROR; 444 } 445 } 446 447 if (p->priv_defaults && HANDLER_ERROR==p->priv_defaults(srv, p->data)) { 448 return HANDLER_ERROR; 449 } 450 451 if (p->handle_uri_clean) 452 ++offsets[PLUGIN_FUNC_HANDLE_URI_CLEAN]; 453 if (p->handle_uri_raw && !p->handle_uri_clean) 454 ++offsets[PLUGIN_FUNC_HANDLE_URI_CLEAN]; /*(same as above)*/ 455 if (p->handle_request_env) 456 ++offsets[PLUGIN_FUNC_HANDLE_REQUEST_ENV]; 457 if (p->handle_request_done) 458 ++offsets[PLUGIN_FUNC_HANDLE_REQUEST_DONE]; 459 if (p->handle_connection_accept) 460 ++offsets[PLUGIN_FUNC_HANDLE_CONNECTION_ACCEPT]; 461 if (p->handle_connection_shut_wr) 462 ++offsets[PLUGIN_FUNC_HANDLE_CONNECTION_SHUT_WR]; 463 if (p->handle_connection_close) 464 ++offsets[PLUGIN_FUNC_HANDLE_CONNECTION_CLOSE]; 465 if (p->handle_trigger) 466 ++offsets[PLUGIN_FUNC_HANDLE_TRIGGER]; 467 if (p->handle_sighup) 468 ++offsets[PLUGIN_FUNC_HANDLE_SIGHUP]; 469 if (p->handle_waitpid) 470 ++offsets[PLUGIN_FUNC_HANDLE_WAITPID]; 471 if (p->handle_subrequest_start) 472 ++offsets[PLUGIN_FUNC_HANDLE_SUBREQUEST_START]; 473 if (p->handle_response_start) 474 ++offsets[PLUGIN_FUNC_HANDLE_RESPONSE_START]; 475 if (p->handle_docroot) 476 ++offsets[PLUGIN_FUNC_HANDLE_DOCROOT]; 477 if (p->handle_physical) 478 ++offsets[PLUGIN_FUNC_HANDLE_PHYSICAL]; 479 if (p->handle_request_reset) 480 ++offsets[PLUGIN_FUNC_HANDLE_REQUEST_RESET]; 481 if (p->set_defaults) 482 ++offsets[PLUGIN_FUNC_SET_DEFAULTS]; 483 if (p->worker_init) 484 ++offsets[PLUGIN_FUNC_WORKER_INIT]; 485 } 486 487 uint32_t nslots = 488 (sizeof(offsets)+sizeof(plugin_fn_data)-1) / sizeof(plugin_fn_data); 489 for (uint32_t i = 0; i < PLUGIN_FUNC_SIZEOF; ++i) { 490 if (offsets[i]) { 491 uint32_t offset = nslots; 492 nslots += offsets[i]+1; /* +1 to mark end of each list */ 493 force_assert(offset * sizeof(plugin_fn_data) <= USHRT_MAX); 494 offsets[i] = (uint16_t)(offset * sizeof(plugin_fn_data)); 495 } 496 } 497 498 /* allocate and fill slots of two dimensional array */ 499 srv->plugin_slots = calloc(nslots, sizeof(plugin_fn_data)); 500 force_assert(NULL != srv->plugin_slots); 501 memcpy(srv->plugin_slots, offsets, sizeof(offsets)); 502 503 /* add handle_uri_raw before handle_uri_clean, but in same slot */ 504 for (uint32_t i = 0; i < srv->plugins.used; ++i) { 505 plugin * const p = ps[i]; 506 plugins_call_init_slot(srv, p->handle_uri_raw, p->data, 507 offsets[PLUGIN_FUNC_HANDLE_URI_CLEAN]); 508 } 509 510 for (uint32_t i = 0; i < srv->plugins.used; ++i) { 511 plugin * const p = ps[i]; 512 513 if (!p->handle_uri_raw) 514 plugins_call_init_slot(srv, p->handle_uri_clean, p->data, 515 offsets[PLUGIN_FUNC_HANDLE_URI_CLEAN]); 516 plugins_call_init_slot(srv, p->handle_request_env, p->data, 517 offsets[PLUGIN_FUNC_HANDLE_REQUEST_ENV]); 518 plugins_call_init_slot(srv, p->handle_request_done, p->data, 519 offsets[PLUGIN_FUNC_HANDLE_REQUEST_DONE]); 520 plugins_call_init_slot(srv, p->handle_connection_accept, p->data, 521 offsets[PLUGIN_FUNC_HANDLE_CONNECTION_ACCEPT]); 522 plugins_call_init_slot(srv, p->handle_connection_shut_wr, p->data, 523 offsets[PLUGIN_FUNC_HANDLE_CONNECTION_SHUT_WR]); 524 plugins_call_init_slot(srv, p->handle_connection_close, p->data, 525 offsets[PLUGIN_FUNC_HANDLE_CONNECTION_CLOSE]); 526 plugins_call_init_slot(srv, p->handle_trigger, p->data, 527 offsets[PLUGIN_FUNC_HANDLE_TRIGGER]); 528 plugins_call_init_slot(srv, p->handle_sighup, p->data, 529 offsets[PLUGIN_FUNC_HANDLE_SIGHUP]); 530 plugins_call_init_slot(srv, p->handle_waitpid, p->data, 531 offsets[PLUGIN_FUNC_HANDLE_WAITPID]); 532 plugins_call_init_slot(srv, p->handle_subrequest_start, p->data, 533 offsets[PLUGIN_FUNC_HANDLE_SUBREQUEST_START]); 534 plugins_call_init_slot(srv, p->handle_response_start, p->data, 535 offsets[PLUGIN_FUNC_HANDLE_RESPONSE_START]); 536 plugins_call_init_slot(srv, p->handle_docroot, p->data, 537 offsets[PLUGIN_FUNC_HANDLE_DOCROOT]); 538 plugins_call_init_slot(srv, p->handle_physical, p->data, 539 offsets[PLUGIN_FUNC_HANDLE_PHYSICAL]); 540 plugins_call_init_slot(srv, p->handle_request_reset, p->data, 541 offsets[PLUGIN_FUNC_HANDLE_REQUEST_RESET]); 542 plugins_call_init_slot(srv, p->set_defaults, p->data, 543 offsets[PLUGIN_FUNC_SET_DEFAULTS]); 544 plugins_call_init_slot(srv, p->worker_init, p->data, 545 offsets[PLUGIN_FUNC_WORKER_INIT]); 546 } 547 548 return HANDLER_GO_ON; 549 } 550 551 void plugins_free(server *srv) { 552 if (srv->plugin_slots) { 553 plugins_call_cleanup(srv); 554 free(srv->plugin_slots); 555 srv->plugin_slots = NULL; 556 } 557 558 for (uint32_t i = 0; i < srv->plugins.used; ++i) { 559 plugin_free(((plugin **)srv->plugins.ptr)[i]); 560 } 561 free(srv->plugins.ptr); 562 srv->plugins.ptr = NULL; 563 srv->plugins.used = 0; 564 srv->plugins.size = 0; 565 array_free_data(&plugin_stats); 566 } 567