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 buffer_copy_buffer(tb, srv->srvconf.modules_dir); 162 buffer_append_path_len(tb, CONST_BUF_LEN(module)); 163 #if defined(__WIN32) || defined(__CYGWIN__) 164 buffer_append_string_len(tb, CONST_STR_LEN(".dll")); 165 #else 166 buffer_append_string_len(tb, CONST_STR_LEN(".so")); 167 #endif 168 169 p = plugin_init(); 170 #ifdef __WIN32 171 if (NULL == (p->lib = LoadLibrary(tb->ptr))) { 172 LPVOID lpMsgBuf; 173 FormatMessage( 174 FORMAT_MESSAGE_ALLOCATE_BUFFER | 175 FORMAT_MESSAGE_FROM_SYSTEM, 176 NULL, 177 GetLastError(), 178 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), 179 (LPTSTR) &lpMsgBuf, 180 0, NULL); 181 182 log_error(srv->errh, __FILE__, __LINE__, 183 "LoadLibrary() failed %s %s", lpMsgBuf, tb->ptr); 184 185 plugin_free(p); 186 187 return -1; 188 189 } 190 #else 191 if (NULL == (p->lib = dlopen(tb->ptr, RTLD_NOW|RTLD_GLOBAL))) { 192 log_error(srv->errh, __FILE__, __LINE__, 193 "dlopen() failed for: %s %s", tb->ptr, dlerror()); 194 195 plugin_free(p); 196 197 return -1; 198 } 199 200 #endif 201 buffer_clear(tb); 202 buffer_append_str2(tb, CONST_BUF_LEN(module), 203 CONST_STR_LEN("_plugin_init")); 204 205 #ifdef __WIN32 206 init = GetProcAddress(p->lib, tb->ptr); 207 208 if (init == NULL) { 209 LPVOID lpMsgBuf; 210 FormatMessage( 211 FORMAT_MESSAGE_ALLOCATE_BUFFER | 212 FORMAT_MESSAGE_FROM_SYSTEM, 213 NULL, 214 GetLastError(), 215 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), 216 (LPTSTR) &lpMsgBuf, 217 0, NULL); 218 219 log_error(srv->errh, __FILE__, __LINE__, 220 "getprocaddress failed: %s %s", tb->ptr, lpMsgBuf); 221 222 plugin_free(p); 223 return -1; 224 } 225 226 #else 227 #if 1 228 init = (int (*)(plugin *))(intptr_t)dlsym(p->lib, tb->ptr); 229 #else 230 *(void **)(&init) = dlsym(p->lib, tb->ptr); 231 #endif 232 if (NULL == init) { 233 const char *error = dlerror(); 234 if (error != NULL) { 235 log_error(srv->errh, __FILE__, __LINE__, "dlsym: %s", error); 236 } else { 237 log_error(srv->errh, __FILE__, __LINE__, "dlsym symbol not found: %s", tb->ptr); 238 } 239 240 plugin_free(p); 241 return -1; 242 } 243 244 #endif 245 if ((*init)(p)) { 246 log_error(srv->errh, __FILE__, __LINE__, "%s plugin init failed", module->ptr); 247 248 plugin_free(p); 249 return -1; 250 } 251 #if 0 252 log_error(srv->errh, __FILE__, __LINE__, "%s plugin loaded", module->ptr); 253 #endif 254 plugins_register(srv, p); 255 } 256 257 return 0; 258 } 259 #endif /* defined(LIGHTTPD_STATIC) */ 260 261 typedef struct { 262 handler_t(*fn)(); 263 plugin_data_base *data; 264 } plugin_fn_data; 265 266 __attribute_hot__ 267 static handler_t plugins_call_fn_req_data(request_st * const r, const int e) { 268 const void * const plugin_slots = r->con->plugin_slots; 269 const uint32_t offset = ((const uint16_t *)plugin_slots)[e]; 270 if (0 == offset) return HANDLER_GO_ON; 271 const plugin_fn_data *plfd = (const plugin_fn_data *) 272 (((uintptr_t)plugin_slots) + offset); 273 handler_t rc = HANDLER_GO_ON; 274 while (plfd->fn && (rc = plfd->fn(r, plfd->data)) == HANDLER_GO_ON) 275 ++plfd; 276 return rc; 277 } 278 279 __attribute_hot__ 280 static handler_t plugins_call_fn_con_data(connection * const con, const int e) { 281 const void * const plugin_slots = con->plugin_slots; 282 const uint32_t offset = ((const uint16_t *)plugin_slots)[e]; 283 if (0 == offset) return HANDLER_GO_ON; 284 const plugin_fn_data *plfd = (const plugin_fn_data *) 285 (((uintptr_t)plugin_slots) + offset); 286 handler_t rc = HANDLER_GO_ON; 287 while (plfd->fn && (rc = plfd->fn(con, plfd->data)) == HANDLER_GO_ON) 288 ++plfd; 289 return rc; 290 } 291 292 static handler_t plugins_call_fn_srv_data(server * const srv, const int e) { 293 const uint32_t offset = ((const uint16_t *)srv->plugin_slots)[e]; 294 if (0 == offset) return HANDLER_GO_ON; 295 const plugin_fn_data *plfd = (const plugin_fn_data *) 296 (((uintptr_t)srv->plugin_slots) + offset); 297 handler_t rc = HANDLER_GO_ON; 298 while (plfd->fn && (rc = plfd->fn(srv,plfd->data)) == HANDLER_GO_ON) 299 ++plfd; 300 return rc; 301 } 302 303 static void plugins_call_fn_srv_data_all(server * const srv, const int e) { 304 const uint32_t offset = ((const uint16_t *)srv->plugin_slots)[e]; 305 if (0 == offset) return; 306 const plugin_fn_data *plfd = (const plugin_fn_data *) 307 (((uintptr_t)srv->plugin_slots) + offset); 308 for (; plfd->fn; ++plfd) 309 plfd->fn(srv, plfd->data); 310 } 311 312 /** 313 * plugins that use 314 * 315 * - request_st *r 316 * - void *p_d (plugin_data *) 317 */ 318 319 #define PLUGIN_CALL_FN_REQ_DATA(x, y) \ 320 handler_t plugins_call_##y(request_st * const r) {\ 321 return plugins_call_fn_req_data(r, x); \ 322 } 323 324 PLUGIN_CALL_FN_REQ_DATA(PLUGIN_FUNC_HANDLE_URI_CLEAN, handle_uri_clean) 325 PLUGIN_CALL_FN_REQ_DATA(PLUGIN_FUNC_HANDLE_URI_RAW, handle_uri_raw) 326 PLUGIN_CALL_FN_REQ_DATA(PLUGIN_FUNC_HANDLE_REQUEST_ENV, handle_request_env) 327 PLUGIN_CALL_FN_REQ_DATA(PLUGIN_FUNC_HANDLE_REQUEST_DONE, handle_request_done) 328 PLUGIN_CALL_FN_REQ_DATA(PLUGIN_FUNC_HANDLE_SUBREQUEST_START, handle_subrequest_start) 329 PLUGIN_CALL_FN_REQ_DATA(PLUGIN_FUNC_HANDLE_RESPONSE_START, handle_response_start) 330 PLUGIN_CALL_FN_REQ_DATA(PLUGIN_FUNC_HANDLE_DOCROOT, handle_docroot) 331 PLUGIN_CALL_FN_REQ_DATA(PLUGIN_FUNC_HANDLE_PHYSICAL, handle_physical) 332 PLUGIN_CALL_FN_REQ_DATA(PLUGIN_FUNC_CONNECTION_RESET, handle_request_reset) 333 334 /** 335 * plugins that use 336 * 337 * - connection *con 338 * - void *p_d (plugin_data *) 339 */ 340 341 #define PLUGIN_CALL_FN_CON_DATA(x, y) \ 342 handler_t plugins_call_##y(connection *con) {\ 343 return plugins_call_fn_con_data(con, x); \ 344 } 345 346 PLUGIN_CALL_FN_CON_DATA(PLUGIN_FUNC_HANDLE_CONNECTION_ACCEPT, handle_connection_accept) 347 PLUGIN_CALL_FN_CON_DATA(PLUGIN_FUNC_HANDLE_CONNECTION_SHUT_WR, handle_connection_shut_wr) 348 PLUGIN_CALL_FN_CON_DATA(PLUGIN_FUNC_HANDLE_CONNECTION_CLOSE, handle_connection_close) 349 350 #undef PLUGIN_CALL_FN_SRV_CON_DATA 351 352 /** 353 * plugins that use 354 * 355 * - server *srv 356 * - void *p_d (plugin_data *) 357 */ 358 359 handler_t plugins_call_set_defaults(server *srv) { 360 return plugins_call_fn_srv_data(srv, PLUGIN_FUNC_SET_DEFAULTS); 361 } 362 363 handler_t plugins_call_worker_init(server *srv) { 364 return plugins_call_fn_srv_data(srv, PLUGIN_FUNC_WORKER_INIT); 365 } 366 367 void plugins_call_handle_trigger(server *srv) { 368 plugins_call_fn_srv_data_all(srv, PLUGIN_FUNC_HANDLE_TRIGGER); 369 } 370 371 void plugins_call_handle_sighup(server *srv) { 372 plugins_call_fn_srv_data_all(srv, PLUGIN_FUNC_HANDLE_SIGHUP); 373 } 374 375 handler_t plugins_call_handle_waitpid(server *srv, pid_t pid, int status) { 376 const uint32_t offset = 377 ((const uint16_t *)srv->plugin_slots)[PLUGIN_FUNC_HANDLE_WAITPID]; 378 if (0 == offset) return HANDLER_GO_ON; 379 const plugin_fn_data *plfd = (const plugin_fn_data *) 380 (((uintptr_t)srv->plugin_slots) + offset); 381 handler_t rc = HANDLER_GO_ON; 382 while (plfd->fn&&(rc=plfd->fn(srv,plfd->data,pid,status))==HANDLER_GO_ON) 383 ++plfd; 384 return rc; 385 } 386 387 static void plugins_call_cleanup(server * const srv) { 388 plugin ** const ps = srv->plugins.ptr; 389 for (uint32_t i = 0; i < srv->plugins.used; ++i) { 390 plugin *p = ps[i]; 391 if (NULL == p) continue; 392 if (NULL != p->data) { 393 plugin_data_base *pd = p->data; 394 if (p->cleanup) 395 p->cleanup(p->data); 396 free(pd->cvlist); 397 free(pd); 398 p->data = NULL; 399 } 400 } 401 } 402 403 /** 404 * 405 * - call init function of all plugins to init the plugin-internals 406 * - added each plugin that supports has callback to the corresponding slot 407 * 408 * - is only called once. 409 */ 410 411 __attribute_cold__ 412 static void plugins_call_init_slot(server *srv, handler_t(*fn)(), void *data, const uint32_t offset) { 413 if (fn) { 414 plugin_fn_data *plfd = (plugin_fn_data *) 415 (((uintptr_t)srv->plugin_slots) + offset); 416 while (plfd->fn) ++plfd; 417 plfd->fn = fn; 418 plfd->data = data; 419 } 420 } 421 422 handler_t plugins_call_init(server *srv) { 423 plugin ** const ps = srv->plugins.ptr; 424 uint16_t offsets[PLUGIN_FUNC_SIZEOF]; 425 memset(offsets, 0, sizeof(offsets)); 426 427 for (uint32_t i = 0; i < srv->plugins.used; ++i) { 428 /* check which calls are supported */ 429 430 plugin *p = ps[i]; 431 432 if (p->init) { 433 if (NULL == (p->data = p->init())) { 434 log_error(srv->errh, __FILE__, __LINE__, 435 "plugin-init failed for module %s", p->name); 436 return HANDLER_ERROR; 437 } 438 439 ((plugin_data_base *)(p->data))->self = p; 440 ((plugin_data_base *)(p->data))->id = i + 1; 441 442 if (p->version != LIGHTTPD_VERSION_ID) { 443 log_error(srv->errh, __FILE__, __LINE__, 444 "plugin-version doesn't match lighttpd-version for %s", p->name); 445 return HANDLER_ERROR; 446 } 447 } 448 449 if (p->priv_defaults && HANDLER_ERROR==p->priv_defaults(srv, p->data)) { 450 return HANDLER_ERROR; 451 } 452 453 if (p->handle_uri_clean) 454 ++offsets[PLUGIN_FUNC_HANDLE_URI_CLEAN]; 455 if (p->handle_uri_raw) 456 ++offsets[PLUGIN_FUNC_HANDLE_URI_RAW]; 457 if (p->handle_request_env) 458 ++offsets[PLUGIN_FUNC_HANDLE_REQUEST_ENV]; 459 if (p->handle_request_done) 460 ++offsets[PLUGIN_FUNC_HANDLE_REQUEST_DONE]; 461 if (p->handle_connection_accept) 462 ++offsets[PLUGIN_FUNC_HANDLE_CONNECTION_ACCEPT]; 463 if (p->handle_connection_shut_wr) 464 ++offsets[PLUGIN_FUNC_HANDLE_CONNECTION_SHUT_WR]; 465 if (p->handle_connection_close) 466 ++offsets[PLUGIN_FUNC_HANDLE_CONNECTION_CLOSE]; 467 if (p->handle_trigger) 468 ++offsets[PLUGIN_FUNC_HANDLE_TRIGGER]; 469 if (p->handle_sighup) 470 ++offsets[PLUGIN_FUNC_HANDLE_SIGHUP]; 471 if (p->handle_waitpid) 472 ++offsets[PLUGIN_FUNC_HANDLE_WAITPID]; 473 if (p->handle_subrequest_start) 474 ++offsets[PLUGIN_FUNC_HANDLE_SUBREQUEST_START]; 475 if (p->handle_response_start) 476 ++offsets[PLUGIN_FUNC_HANDLE_RESPONSE_START]; 477 if (p->handle_docroot) 478 ++offsets[PLUGIN_FUNC_HANDLE_DOCROOT]; 479 if (p->handle_physical) 480 ++offsets[PLUGIN_FUNC_HANDLE_PHYSICAL]; 481 if (p->handle_request_reset) 482 ++offsets[PLUGIN_FUNC_CONNECTION_RESET]; 483 if (p->set_defaults) 484 ++offsets[PLUGIN_FUNC_SET_DEFAULTS]; 485 if (p->worker_init) 486 ++offsets[PLUGIN_FUNC_WORKER_INIT]; 487 } 488 489 uint32_t nslots = 490 (sizeof(offsets)+sizeof(plugin_fn_data)-1) / sizeof(plugin_fn_data); 491 for (uint32_t i = 0; i < PLUGIN_FUNC_SIZEOF; ++i) { 492 if (offsets[i]) { 493 uint32_t offset = nslots; 494 nslots += offsets[i]+1; /* +1 to mark end of each list */ 495 force_assert(offset * sizeof(plugin_fn_data) <= USHRT_MAX); 496 offsets[i] = (uint16_t)(offset * sizeof(plugin_fn_data)); 497 } 498 } 499 500 /* allocate and fill slots of two dimensional array */ 501 srv->plugin_slots = calloc(nslots, sizeof(plugin_fn_data)); 502 force_assert(NULL != srv->plugin_slots); 503 memcpy(srv->plugin_slots, offsets, sizeof(offsets)); 504 505 for (uint32_t i = 0; i < srv->plugins.used; ++i) { 506 plugin * const p = ps[i]; 507 508 plugins_call_init_slot(srv, p->handle_uri_clean, p->data, 509 offsets[PLUGIN_FUNC_HANDLE_URI_CLEAN]); 510 plugins_call_init_slot(srv, p->handle_uri_raw, p->data, 511 offsets[PLUGIN_FUNC_HANDLE_URI_RAW]); 512 plugins_call_init_slot(srv, p->handle_request_env, p->data, 513 offsets[PLUGIN_FUNC_HANDLE_REQUEST_ENV]); 514 plugins_call_init_slot(srv, p->handle_request_done, p->data, 515 offsets[PLUGIN_FUNC_HANDLE_REQUEST_DONE]); 516 plugins_call_init_slot(srv, p->handle_connection_accept, p->data, 517 offsets[PLUGIN_FUNC_HANDLE_CONNECTION_ACCEPT]); 518 plugins_call_init_slot(srv, p->handle_connection_shut_wr, p->data, 519 offsets[PLUGIN_FUNC_HANDLE_CONNECTION_SHUT_WR]); 520 plugins_call_init_slot(srv, p->handle_connection_close, p->data, 521 offsets[PLUGIN_FUNC_HANDLE_CONNECTION_CLOSE]); 522 plugins_call_init_slot(srv, p->handle_trigger, p->data, 523 offsets[PLUGIN_FUNC_HANDLE_TRIGGER]); 524 plugins_call_init_slot(srv, p->handle_sighup, p->data, 525 offsets[PLUGIN_FUNC_HANDLE_SIGHUP]); 526 plugins_call_init_slot(srv, p->handle_waitpid, p->data, 527 offsets[PLUGIN_FUNC_HANDLE_WAITPID]); 528 plugins_call_init_slot(srv, p->handle_subrequest_start, p->data, 529 offsets[PLUGIN_FUNC_HANDLE_SUBREQUEST_START]); 530 plugins_call_init_slot(srv, p->handle_response_start, p->data, 531 offsets[PLUGIN_FUNC_HANDLE_RESPONSE_START]); 532 plugins_call_init_slot(srv, p->handle_docroot, p->data, 533 offsets[PLUGIN_FUNC_HANDLE_DOCROOT]); 534 plugins_call_init_slot(srv, p->handle_physical, p->data, 535 offsets[PLUGIN_FUNC_HANDLE_PHYSICAL]); 536 plugins_call_init_slot(srv, p->handle_request_reset, p->data, 537 offsets[PLUGIN_FUNC_CONNECTION_RESET]); 538 plugins_call_init_slot(srv, p->set_defaults, p->data, 539 offsets[PLUGIN_FUNC_SET_DEFAULTS]); 540 plugins_call_init_slot(srv, p->worker_init, p->data, 541 offsets[PLUGIN_FUNC_WORKER_INIT]); 542 } 543 544 return HANDLER_GO_ON; 545 } 546 547 void plugins_free(server *srv) { 548 if (srv->plugin_slots) { 549 plugins_call_cleanup(srv); 550 free(srv->plugin_slots); 551 srv->plugin_slots = NULL; 552 } 553 554 for (uint32_t i = 0; i < srv->plugins.used; ++i) { 555 plugin_free(((plugin **)srv->plugins.ptr)[i]); 556 } 557 free(srv->plugins.ptr); 558 srv->plugins.ptr = NULL; 559 srv->plugins.used = 0; 560 srv->plugins.size = 0; 561 array_free_data(&plugin_stats); 562 } 563