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