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