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