]> jfr.im git - solanum.git/blob - ircd/modules.c
Merge branch 'check-one-kline' of https://github.com/edk0/charybdis into edk0-check...
[solanum.git] / ircd / modules.c
1 /*
2 * ircd-ratbox: A slightly useful ircd.
3 * modules.c: A module loader.
4 *
5 * Copyright (C) 1990 Jarkko Oikarinen and University of Oulu, Co Center
6 * Copyright (C) 1996-2002 Hybrid Development Team
7 * Copyright (C) 2002-2005 ircd-ratbox development team
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 * USA
23 */
24
25 #include "stdinc.h"
26 #include "modules.h"
27 #include "logger.h"
28 #include "ircd.h"
29 #include "client.h"
30 #include "send.h"
31 #include "s_conf.h"
32 #include "s_newconf.h"
33 #include "numeric.h"
34 #include "parse.h"
35 #include "ircd_defs.h"
36 #include "match.h"
37 #include "s_serv.h"
38 #include "capability.h"
39
40 #include <ltdl.h>
41
42 #ifndef LT_MODULE_EXT
43 # error "Charybdis requires loadable module support."
44 #endif
45
46 rb_dlink_list module_list;
47 rb_dlink_list mod_paths;
48
49 static const char *core_module_table[] = {
50 "m_ban",
51 "m_die",
52 "m_error",
53 "m_join",
54 "m_kick",
55 "m_kill",
56 "m_message",
57 "m_mode",
58 "m_modules",
59 "m_nick",
60 "m_part",
61 "m_quit",
62 "m_server",
63 "m_squit",
64 NULL
65 };
66
67 #define MOD_WARN_DELTA (90 * 86400) /* time in seconds, 86400 seconds in a day */
68
69 void
70 init_modules(void)
71 {
72 if(lt_dlinit())
73 {
74 ilog(L_MAIN, "lt_dlinit failed");
75 exit(EXIT_FAILURE);
76 }
77
78 /* Add the default paths we look in to the module system --nenolod */
79 mod_add_path(ircd_paths[IRCD_PATH_MODULES]);
80 mod_add_path(ircd_paths[IRCD_PATH_AUTOLOAD_MODULES]);
81 }
82
83 /* mod_find_path()
84 *
85 * input - path
86 * output - none
87 * side effects - returns a module path from path
88 */
89 static char *
90 mod_find_path(const char *path)
91 {
92 rb_dlink_node *ptr;
93 char *mpath;
94
95 RB_DLINK_FOREACH(ptr, mod_paths.head)
96 {
97 mpath = ptr->data;
98
99 if(!strcmp(path, mpath))
100 return mpath;
101 }
102
103 return NULL;
104 }
105
106 /* mod_add_path
107 *
108 * input - path
109 * ouput -
110 * side effects - adds path to list
111 */
112 void
113 mod_add_path(const char *path)
114 {
115 char *pathst;
116
117 if(mod_find_path(path))
118 return;
119
120 pathst = rb_strdup(path);
121 rb_dlinkAddAlloc(pathst, &mod_paths);
122 }
123
124 /* mod_clear_paths()
125 *
126 * input -
127 * output -
128 * side effects - clear the lists of paths
129 */
130 void
131 mod_clear_paths(void)
132 {
133 rb_dlink_node *ptr, *next_ptr;
134
135 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, mod_paths.head)
136 {
137 rb_free(ptr->data);
138 rb_free_rb_dlink_node(ptr);
139 }
140
141 mod_paths.head = mod_paths.tail = NULL;
142 mod_paths.length = 0;
143 }
144
145 /* findmodule_byname
146 *
147 * input - module to load
148 * output - index of module on success, -1 on failure
149 * side effects - none
150 */
151 struct module *
152 findmodule_byname(const char *name)
153 {
154 rb_dlink_node *ptr;
155 char name_ext[PATH_MAX + 1];
156
157 rb_strlcpy(name_ext, name, sizeof name_ext);
158 rb_strlcat(name_ext, LT_MODULE_EXT, sizeof name_ext);
159
160 RB_DLINK_FOREACH(ptr, module_list.head)
161 {
162 struct module *mod = ptr->data;
163
164 if(!irccmp(mod->name, name))
165 return mod;
166
167 if(!irccmp(mod->name, name_ext))
168 return mod;
169 }
170
171 return NULL;
172 }
173
174 /* load_all_modules()
175 *
176 * input -
177 * output -
178 * side effects -
179 */
180 void
181 load_all_modules(bool warn)
182 {
183 DIR *system_module_dir = NULL;
184 struct dirent *ldirent = NULL;
185 char module_fq_name[PATH_MAX + 1];
186 size_t module_ext_len = strlen(LT_MODULE_EXT);
187
188 system_module_dir = opendir(ircd_paths[IRCD_PATH_AUTOLOAD_MODULES]);
189
190 if(system_module_dir == NULL)
191 {
192 ilog(L_MAIN, "Could not load modules from %s: %s", ircd_paths[IRCD_PATH_AUTOLOAD_MODULES], strerror(errno));
193 return;
194 }
195
196 while ((ldirent = readdir(system_module_dir)) != NULL)
197 {
198 size_t len = strlen(ldirent->d_name);
199
200 if(len > module_ext_len &&
201 rb_strncasecmp(ldirent->d_name + (len - module_ext_len), LT_MODULE_EXT, module_ext_len) == 0)
202 {
203 (void) snprintf(module_fq_name, sizeof(module_fq_name), "%s%c%s",
204 ircd_paths[IRCD_PATH_AUTOLOAD_MODULES], RB_PATH_SEPARATOR, ldirent->d_name);
205 (void) load_a_module(module_fq_name, warn, MAPI_ORIGIN_CORE, false);
206 }
207
208 }
209 (void) closedir(system_module_dir);
210 }
211
212 /* load_core_modules()
213 *
214 * input -
215 * output -
216 * side effects - core modules are loaded, if any fail, kill ircd
217 */
218 void
219 load_core_modules(bool warn)
220 {
221 char module_name[PATH_MAX];
222 int i;
223
224
225 for (i = 0; core_module_table[i]; i++)
226 {
227 snprintf(module_name, sizeof(module_name), "%s%c%s", ircd_paths[IRCD_PATH_MODULES], RB_PATH_SEPARATOR,
228 core_module_table[i]);
229
230 if(load_a_module(module_name, warn, MAPI_ORIGIN_CORE, true) == false)
231 {
232 ilog(L_MAIN,
233 "Error loading core module %s: terminating ircd",
234 core_module_table[i]);
235 exit(EXIT_FAILURE);
236 }
237 }
238 }
239
240 /* load_one_module()
241 *
242 * input -
243 * output -
244 * side effects -
245 */
246 bool
247 load_one_module(const char *path, int origin, bool coremodule)
248 {
249 char modpath[PATH_MAX];
250 rb_dlink_node *pathst;
251
252 if (server_state_foreground)
253 inotice("loading module %s ...", path);
254
255 if(coremodule)
256 origin = MAPI_ORIGIN_CORE;
257
258 RB_DLINK_FOREACH(pathst, mod_paths.head)
259 {
260 struct stat statbuf;
261 const char *mpath = pathst->data;
262
263 snprintf(modpath, sizeof(modpath), "%s%c%s%s", mpath, RB_PATH_SEPARATOR, path, LT_MODULE_EXT);
264 if((strstr(modpath, "../") == NULL) && (strstr(modpath, "/..") == NULL))
265 {
266 if(stat(modpath, &statbuf) == 0 && S_ISREG(statbuf.st_mode))
267 {
268 /* Regular files only please */
269 return load_a_module(modpath, true, origin, coremodule);
270 }
271
272 }
273 }
274
275 sendto_realops_snomask(SNO_GENERAL, L_ALL, "Cannot locate module %s", path);
276 return false;
277 }
278
279 static char unknown_ver[] = "<unknown>";
280 static char unknown_description[] = "<none>";
281
282 /* unload_one_module()
283 *
284 * inputs - name of module to unload
285 * - true to say modules unloaded, false to not
286 * output - true if successful, false if error
287 * side effects - module is unloaded
288 */
289 bool
290 unload_one_module(const char *name, bool warn)
291 {
292 struct module *mod;
293
294 if((mod = findmodule_byname(name)) == NULL)
295 return false;
296
297 /*
298 ** XXX - The type system in C does not allow direct conversion between
299 ** data and function pointers, but as it happens, most C compilers will
300 ** safely do this, however it is a theoretical overlow to cast as we
301 ** must do here. I have library functions to take care of this, but
302 ** despite being more "correct" for the C language, this is more
303 ** practical. Removing the abuse of the ability to cast ANY pointer
304 ** to and from an integer value here will break some compilers.
305 ** -jmallett
306 */
307 /* Left the comment in but the code isn't here any more -larne */
308 switch (mod->mapi_version)
309 {
310 case 1:
311 {
312 struct mapi_mheader_av1 *mheader = mod->mapi_header;
313 if(mheader->mapi_command_list)
314 {
315 struct Message **m;
316 for (m = mheader->mapi_command_list; *m; ++m)
317 mod_del_cmd(*m);
318 }
319
320 /* hook events are never removed, we simply lose the
321 * ability to call them --fl
322 */
323 if(mheader->mapi_hfn_list)
324 {
325 mapi_hfn_list_av1 *m;
326 for (m = mheader->mapi_hfn_list; m->hapi_name; ++m)
327 remove_hook(m->hapi_name, m->fn);
328 }
329
330 if(mheader->mapi_unregister)
331 mheader->mapi_unregister();
332 break;
333 }
334 case 2:
335 {
336 struct mapi_mheader_av2 *mheader = mod->mapi_header;
337
338 /* XXX duplicate code :( */
339 if(mheader->mapi_command_list)
340 {
341 struct Message **m;
342 for (m = mheader->mapi_command_list; *m; ++m)
343 mod_del_cmd(*m);
344 }
345
346 /* hook events are never removed, we simply lose the
347 * ability to call them --fl
348 */
349 if(mheader->mapi_hfn_list)
350 {
351 mapi_hfn_list_av1 *m;
352 for (m = mheader->mapi_hfn_list; m->hapi_name; ++m)
353 remove_hook(m->hapi_name, m->fn);
354 }
355
356 if(mheader->mapi_unregister)
357 mheader->mapi_unregister();
358
359 if(mheader->mapi_cap_list)
360 {
361 mapi_cap_list_av2 *m;
362 for (m = mheader->mapi_cap_list; m->cap_name; ++m)
363 {
364 struct CapabilityIndex *idx;
365
366 switch (m->cap_index)
367 {
368 case MAPI_CAP_CLIENT:
369 idx = cli_capindex;
370 break;
371 case MAPI_CAP_SERVER:
372 idx = serv_capindex;
373 break;
374 default:
375 sendto_realops_snomask(SNO_GENERAL, L_ALL,
376 "Unknown/unsupported CAP index found of type %d on capability %s when unloading %s",
377 m->cap_index, m->cap_name, mod->name);
378 ilog(L_MAIN,
379 "Unknown/unsupported CAP index found of type %d on capability %s when unloading %s",
380 m->cap_index, m->cap_name, mod->name);
381 continue;
382 }
383
384 if (m->cap_id != NULL)
385 {
386 capability_orphan(idx, m->cap_name);
387 sendto_local_clients_with_capability(CLICAP_CAP_NOTIFY, ":%s CAP * DEL :%s", me.name, m->cap_name);
388 }
389 }
390 }
391 break;
392 }
393 default:
394 sendto_realops_snomask(SNO_GENERAL, L_ALL,
395 "Unknown/unsupported MAPI version %d when unloading %s!",
396 mod->mapi_version, mod->name);
397 ilog(L_MAIN, "Unknown/unsupported MAPI version %d when unloading %s!",
398 mod->mapi_version, mod->name);
399 break;
400 }
401
402 lt_dlclose(mod->address);
403
404 rb_dlinkDelete(&mod->node, &module_list);
405 rb_free(mod->name);
406 rb_free(mod);
407
408 if(warn)
409 {
410 ilog(L_MAIN, "Module %s unloaded", name);
411 sendto_realops_snomask(SNO_GENERAL, L_ALL, "Module %s unloaded", name);
412 }
413
414 return true;
415 }
416
417 /*
418 * load_a_module()
419 *
420 * inputs - path name of module, bool to notice, int of origin, bool if core
421 * output - false if error true if success
422 * side effects - loads a module if successful
423 */
424 bool
425 load_a_module(const char *path, bool warn, int origin, bool core)
426 {
427 struct module *mod;
428 lt_dlhandle tmpptr;
429 char *mod_displayname, *c;
430 const char *ver, *description = NULL;
431
432 int *mapi_version;
433
434 mod_displayname = rb_basename(path);
435
436 /* Trim off the ending for the display name if we have to */
437 if((c = rb_strcasestr(mod_displayname, LT_MODULE_EXT)) != NULL)
438 *c = '\0';
439
440 tmpptr = lt_dlopenext(path);
441
442 if(tmpptr == NULL)
443 {
444 const char *err = lt_dlerror();
445
446 sendto_realops_snomask(SNO_GENERAL, L_ALL,
447 "Error loading module %s: %s", mod_displayname, err);
448 ilog(L_MAIN, "Error loading module %s: %s", mod_displayname, err);
449 rb_free(mod_displayname);
450 return false;
451 }
452
453 /*
454 * _mheader is actually a struct mapi_mheader_*, but mapi_version
455 * is always the first member of this structure, so we treate it
456 * as a single int in order to determine the API version.
457 * -larne.
458 */
459 mapi_version = (int *) (uintptr_t) lt_dlsym(tmpptr, "_mheader");
460 if((mapi_version == NULL
461 && (mapi_version = (int *) (uintptr_t) lt_dlsym(tmpptr, "__mheader")) == NULL)
462 || MAPI_MAGIC(*mapi_version) != MAPI_MAGIC_HDR)
463 {
464 sendto_realops_snomask(SNO_GENERAL, L_ALL,
465 "Data format error: module %s has no MAPI header.",
466 mod_displayname);
467 ilog(L_MAIN, "Data format error: module %s has no MAPI header.", mod_displayname);
468 (void) lt_dlclose(tmpptr);
469 rb_free(mod_displayname);
470 return false;
471 }
472
473 switch (MAPI_VERSION(*mapi_version))
474 {
475 case 1:
476 {
477 struct mapi_mheader_av1 *mheader = (struct mapi_mheader_av1 *)(void *)mapi_version; /* see above */
478 if(mheader->mapi_register && (mheader->mapi_register() == -1))
479 {
480 ilog(L_MAIN, "Module %s indicated failure during load.",
481 mod_displayname);
482 sendto_realops_snomask(SNO_GENERAL, L_ALL,
483 "Module %s indicated failure during load.",
484 mod_displayname);
485 lt_dlclose(tmpptr);
486 rb_free(mod_displayname);
487 return false;
488 }
489 if(mheader->mapi_command_list)
490 {
491 struct Message **m;
492 for (m = mheader->mapi_command_list; *m; ++m)
493 mod_add_cmd(*m);
494 }
495
496 if(mheader->mapi_hook_list)
497 {
498 mapi_hlist_av1 *m;
499 for (m = mheader->mapi_hook_list; m->hapi_name; ++m)
500 *m->hapi_id = register_hook(m->hapi_name);
501 }
502
503 if(mheader->mapi_hfn_list)
504 {
505 mapi_hfn_list_av1 *m;
506 for (m = mheader->mapi_hfn_list; m->hapi_name; ++m)
507 add_hook(m->hapi_name, m->fn);
508 }
509
510 ver = mheader->mapi_module_version;
511 break;
512 }
513 case 2:
514 {
515 struct mapi_mheader_av2 *mheader = (struct mapi_mheader_av2 *)(void *)mapi_version; /* see above */
516
517 /* XXX duplicated code :( */
518 if(mheader->mapi_register && (mheader->mapi_register() == -1))
519 {
520 ilog(L_MAIN, "Module %s indicated failure during load.",
521 mod_displayname);
522 sendto_realops_snomask(SNO_GENERAL, L_ALL,
523 "Module %s indicated failure during load.",
524 mod_displayname);
525 lt_dlclose(tmpptr);
526 rb_free(mod_displayname);
527 return false;
528 }
529
530 /* Basic date code checks
531 *
532 * Don't make them fatal, but do complain about differences within a certain time frame.
533 * Later on if there are major API changes we can add fatal checks.
534 * -- Elizafox
535 */
536 if(mheader->mapi_datecode != datecode && mheader->mapi_datecode > 0)
537 {
538 long int delta = datecode - mheader->mapi_datecode;
539 if (delta > MOD_WARN_DELTA)
540 {
541 delta /= 86400;
542 iwarn("Module %s build date is out of sync with ircd build date by %ld days, expect problems",
543 mod_displayname, delta);
544 sendto_realops_snomask(SNO_GENERAL, L_ALL,
545 "Module %s build date is out of sync with ircd build date by %ld days, expect problems",
546 mod_displayname, delta);
547 }
548 }
549
550 if(mheader->mapi_command_list)
551 {
552 struct Message **m;
553 for (m = mheader->mapi_command_list; *m; ++m)
554 mod_add_cmd(*m);
555 }
556
557 if(mheader->mapi_hook_list)
558 {
559 mapi_hlist_av1 *m;
560 for (m = mheader->mapi_hook_list; m->hapi_name; ++m)
561 *m->hapi_id = register_hook(m->hapi_name);
562 }
563
564 if(mheader->mapi_hfn_list)
565 {
566 mapi_hfn_list_av1 *m;
567 for (m = mheader->mapi_hfn_list; m->hapi_name; ++m)
568 add_hook(m->hapi_name, m->fn);
569 }
570
571 /* New in MAPI v2 - version replacement */
572 ver = mheader->mapi_module_version ? mheader->mapi_module_version : ircd_version;
573 description = mheader->mapi_module_description;
574
575 if(mheader->mapi_cap_list)
576 {
577 mapi_cap_list_av2 *m;
578 for (m = mheader->mapi_cap_list; m->cap_name; ++m)
579 {
580 struct CapabilityIndex *idx;
581 int result;
582
583 switch (m->cap_index)
584 {
585 case MAPI_CAP_CLIENT:
586 idx = cli_capindex;
587 break;
588 case MAPI_CAP_SERVER:
589 idx = serv_capindex;
590 break;
591 default:
592 sendto_realops_snomask(SNO_GENERAL, L_ALL,
593 "Unknown/unsupported CAP index found of type %d on capability %s when loading %s",
594 m->cap_index, m->cap_name, mod_displayname);
595 ilog(L_MAIN,
596 "Unknown/unsupported CAP index found of type %d on capability %s when loading %s",
597 m->cap_index, m->cap_name, mod_displayname);
598 continue;
599 }
600
601 result = capability_put(idx, m->cap_name, m->cap_ownerdata);
602 if (m->cap_id != NULL)
603 {
604 *(m->cap_id) = result;
605 sendto_local_clients_with_capability(CLICAP_CAP_NOTIFY, ":%s CAP * ADD :%s", me.name, m->cap_name);
606 }
607 }
608 }
609 }
610
611 break;
612 default:
613 ilog(L_MAIN, "Module %s has unknown/unsupported MAPI version %d.",
614 mod_displayname, MAPI_VERSION(*mapi_version));
615 sendto_realops_snomask(SNO_GENERAL, L_ALL,
616 "Module %s has unknown/unsupported MAPI version %d.",
617 mod_displayname, *mapi_version);
618 lt_dlclose(tmpptr);
619 rb_free(mod_displayname);
620 return false;
621 }
622
623 if(ver == NULL)
624 ver = unknown_ver;
625
626 if(description == NULL)
627 description = unknown_description;
628
629 mod = rb_malloc(sizeof(struct module));
630 mod->address = tmpptr;
631 mod->version = ver;
632 mod->description = description;
633 mod->core = core;
634 mod->name = rb_strdup(mod_displayname);
635 mod->mapi_header = mapi_version;
636 mod->mapi_version = MAPI_VERSION(*mapi_version);
637 mod->origin = origin;
638 rb_dlinkAdd(mod, &mod->node, &module_list);
639
640 if(warn)
641 {
642 const char *o;
643
644 switch (origin)
645 {
646 case MAPI_ORIGIN_EXTENSION:
647 o = "extension";
648 break;
649 case MAPI_ORIGIN_CORE:
650 o = "core";
651 break;
652 default:
653 o = "unknown";
654 break;
655 }
656
657 sendto_realops_snomask(SNO_GENERAL, L_ALL,
658 "Module %s [version: %s; MAPI version: %d; origin: %s; description: \"%s\"] loaded at %p",
659 mod_displayname, ver, MAPI_VERSION(*mapi_version), o, description,
660 (void *) tmpptr);
661 ilog(L_MAIN, "Module %s [version: %s; MAPI version: %d; origin: %s; description: \"%s\"] loaded at %p",
662 mod_displayname, ver, MAPI_VERSION(*mapi_version), o, description, (void *) tmpptr);
663 }
664 rb_free(mod_displayname);
665 return true;
666 }
667
668 void
669 modules_do_restart(void *unused)
670 {
671 unsigned int modnum = 0;
672 rb_dlink_node *ptr, *nptr;
673
674 RB_DLINK_FOREACH_SAFE(ptr, nptr, module_list.head)
675 {
676 struct module *mod = ptr->data;
677 if(!unload_one_module(mod->name, false))
678 {
679 ilog(L_MAIN, "Module Restart: %s was not unloaded %s",
680 mod->name,
681 mod->core? "(core module)" : "");
682
683 if(!mod->core)
684 sendto_realops_snomask(SNO_GENERAL, L_NETWIDE,
685 "Module Restart: %s failed to unload",
686 mod->name);
687 continue;
688 }
689
690 modnum++;
691 }
692
693 load_all_modules(false);
694 load_core_modules(false);
695 rehash(false);
696
697 sendto_realops_snomask(SNO_GENERAL, L_NETWIDE,
698 "Module Restart: %u modules unloaded, %lu modules loaded",
699 modnum, rb_dlink_list_length(&module_list));
700 ilog(L_MAIN, "Module Restart: %u modules unloaded, %lu modules loaded", modnum, rb_dlink_list_length(&module_list));
701 }