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