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