]> jfr.im git - solanum.git/blob - ircd/modules.c
ircd: modules: findmodule_byname(): also check LT_MODULE_EXT here
[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_nick",
58 "m_part",
59 "m_quit",
60 "m_server",
61 "m_squit",
62 NULL
63 };
64
65 #define MOD_WARN_DELTA (90 * 86400) /* time in seconds, 86400 seconds in a day */
66
67 #define MODS_INCREMENT 10
68 int num_mods = 0;
69 int max_mods = MODS_INCREMENT;
70
71 static rb_dlink_list mod_paths;
72
73 static void mo_modload(struct MsgBuf *, struct Client *, struct Client *, int, const char **);
74 static void mo_modlist(struct MsgBuf *, struct Client *, struct Client *, int, const char **);
75 static void mo_modreload(struct MsgBuf *, struct Client *, struct Client *, int, const char **);
76 static void mo_modunload(struct MsgBuf *, struct Client *, struct Client *, int, const char **);
77 static void mo_modrestart(struct MsgBuf *, struct Client *, struct Client *, int, const char **);
78
79 static void me_modload(struct MsgBuf *, struct Client *, struct Client *, int, const char **);
80 static void me_modlist(struct MsgBuf *, struct Client *, struct Client *, int, const char **);
81 static void me_modreload(struct MsgBuf *, struct Client *, struct Client *, int, const char **);
82 static void me_modunload(struct MsgBuf *, struct Client *, struct Client *, int, const char **);
83 static void me_modrestart(struct MsgBuf *, struct Client *, struct Client *, int, const char **);
84
85 static void do_modload(struct Client *, const char *);
86 static void do_modunload(struct Client *, const char *);
87 static void do_modreload(struct Client *, const char *);
88 static void do_modlist(struct Client *, const char *);
89 static void do_modrestart(struct Client *);
90
91 struct Message modload_msgtab = {
92 "MODLOAD", 0, 0, 0, 0,
93 {mg_unreg, mg_not_oper, mg_ignore, mg_ignore, {me_modload, 2}, {mo_modload, 2}}
94 };
95
96 struct Message modunload_msgtab = {
97 "MODUNLOAD", 0, 0, 0, 0,
98 {mg_unreg, mg_not_oper, mg_ignore, mg_ignore, {me_modunload, 2}, {mo_modunload, 2}}
99 };
100
101 struct Message modreload_msgtab = {
102 "MODRELOAD", 0, 0, 0, 0,
103 {mg_unreg, mg_not_oper, mg_ignore, mg_ignore, {me_modreload, 2}, {mo_modreload, 2}}
104 };
105
106 struct Message modlist_msgtab = {
107 "MODLIST", 0, 0, 0, 0,
108 {mg_unreg, mg_not_oper, mg_ignore, mg_ignore, {me_modlist, 0}, {mo_modlist, 0}}
109 };
110
111 struct Message modrestart_msgtab = {
112 "MODRESTART", 0, 0, 0, 0,
113 {mg_unreg, mg_not_oper, mg_ignore, mg_ignore, {me_modrestart, 0}, {mo_modrestart, 0}}
114 };
115
116 void
117 modules_init(void)
118 {
119 if(lt_dlinit())
120 {
121 ilog(L_MAIN, "lt_dlinit failed");
122 exit(0);
123 }
124
125 mod_add_cmd(&modload_msgtab);
126 mod_add_cmd(&modunload_msgtab);
127 mod_add_cmd(&modreload_msgtab);
128 mod_add_cmd(&modlist_msgtab);
129 mod_add_cmd(&modrestart_msgtab);
130
131 /* Add the default paths we look in to the module system --nenolod */
132 mod_add_path(MODPATH);
133 mod_add_path(AUTOMODPATH);
134 }
135
136 /* mod_find_path()
137 *
138 * input - path
139 * output - none
140 * side effects - returns a module path from path
141 */
142 static char *
143 mod_find_path(const char *path)
144 {
145 rb_dlink_node *ptr;
146 char *mpath;
147
148 RB_DLINK_FOREACH(ptr, mod_paths.head)
149 {
150 mpath = ptr->data;
151
152 if(!strcmp(path, mpath))
153 return mpath;
154 }
155
156 return NULL;
157 }
158
159 /* mod_add_path
160 *
161 * input - path
162 * ouput -
163 * side effects - adds path to list
164 */
165 void
166 mod_add_path(const char *path)
167 {
168 char *pathst;
169
170 if(mod_find_path(path))
171 return;
172
173 pathst = rb_strdup(path);
174 rb_dlinkAddAlloc(pathst, &mod_paths);
175 }
176
177 /* mod_clear_paths()
178 *
179 * input -
180 * output -
181 * side effects - clear the lists of paths
182 */
183 void
184 mod_clear_paths(void)
185 {
186 rb_dlink_node *ptr, *next_ptr;
187
188 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, mod_paths.head)
189 {
190 rb_free(ptr->data);
191 rb_free_rb_dlink_node(ptr);
192 }
193
194 mod_paths.head = mod_paths.tail = NULL;
195 mod_paths.length = 0;
196 }
197
198 /* findmodule_byname
199 *
200 * input -
201 * output -
202 * side effects -
203 */
204
205 int
206 findmodule_byname(const char *name)
207 {
208 int i;
209 char name_ext[PATH_MAX + 1];
210
211 rb_strlcpy(name_ext, name, sizeof basename_ext);
212 rb_strlcat(name_ext, LT_MODULE_EXT, sizeof basename_ext);
213
214 for (i = 0; i < num_mods; i++)
215 {
216 if(!irccmp(modlist[i]->name, name))
217 return i;
218
219 if(!irccmp(modlist[i]->name, name_ext))
220 return i;
221 }
222
223 return -1;
224 }
225
226 /* load_all_modules()
227 *
228 * input -
229 * output -
230 * side effects -
231 */
232 void
233 load_all_modules(int warn)
234 {
235 DIR *system_module_dir = NULL;
236 struct dirent *ldirent = NULL;
237 char module_fq_name[PATH_MAX + 1];
238 size_t module_ext_len = strlen(LT_MODULE_EXT);
239
240 modules_init();
241
242 modlist = (struct module **) rb_malloc(sizeof(struct module *) * (MODS_INCREMENT));
243
244 max_mods = MODS_INCREMENT;
245
246 system_module_dir = opendir(AUTOMODPATH);
247
248 if(system_module_dir == NULL)
249 {
250 ilog(L_MAIN, "Could not load modules from %s: %s", AUTOMODPATH, strerror(errno));
251 return;
252 }
253
254 while ((ldirent = readdir(system_module_dir)) != NULL)
255 {
256 struct stat s;
257 size_t len;
258
259 len = strlen(ldirent->d_name);
260 if(len > module_ext_len && !strcasecmp(ldirent->d_name + (len - module_ext_len), LT_MODULE_EXT))
261 {
262 (void) snprintf(module_fq_name, sizeof(module_fq_name), "%s/%s", AUTOMODPATH, ldirent->d_name);
263 (void) load_a_module(module_fq_name, warn, MAPI_ORIGIN_CORE, 0);
264 }
265
266 }
267 (void) closedir(system_module_dir);
268 }
269
270 /* load_core_modules()
271 *
272 * input -
273 * output -
274 * side effects - core modules are loaded, if any fail, kill ircd
275 */
276 void
277 load_core_modules(int warn)
278 {
279 char module_name[PATH_MAX];
280 int i;
281
282
283 for (i = 0; core_module_table[i]; i++)
284 {
285 snprintf(module_name, sizeof(module_name), "%s/%s%s", MODPATH,
286 core_module_table[i], LT_MODULE_EXT);
287
288 if(load_a_module(module_name, warn, MAPI_ORIGIN_CORE, 1) == -1)
289 {
290 ilog(L_MAIN,
291 "Error loading core module %s: terminating ircd",
292 core_module_table[i]);
293 exit(0);
294 }
295 }
296 }
297
298 /* load_one_module()
299 *
300 * input -
301 * output -
302 * side effects -
303 */
304 int
305 load_one_module(const char *path, int origin, int coremodule)
306 {
307 char modpath[PATH_MAX];
308 rb_dlink_node *pathst;
309 const char *mpath;
310 struct stat statbuf;
311
312 if (server_state_foreground)
313 inotice("loading module %s ...", path);
314
315 if(coremodule != 0)
316 {
317 coremodule = 1;
318 origin = MAPI_ORIGIN_CORE;
319 }
320
321 RB_DLINK_FOREACH(pathst, mod_paths.head)
322 {
323 mpath = pathst->data;
324
325 snprintf(modpath, sizeof(modpath), "%s/%s%s", mpath, path, LT_MODULE_EXT);
326 if((strstr(modpath, "../") == NULL) && (strstr(modpath, "/..") == NULL))
327 {
328 if(stat(modpath, &statbuf) == 0)
329 {
330 if(S_ISREG(statbuf.st_mode))
331 {
332 /* Regular files only please */
333 return load_a_module(modpath, 1, origin, coremodule);
334 }
335 }
336
337 }
338 }
339
340 sendto_realops_snomask(SNO_GENERAL, L_ALL, "Cannot locate module %s", path);
341 return -1;
342 }
343
344
345 /* load a module .. */
346 static void
347 mo_modload(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char **parv)
348 {
349 if(!IsOperAdmin(source_p))
350 {
351 sendto_one(source_p, form_str(ERR_NOPRIVS),
352 me.name, source_p->name, "admin");
353 return;
354 }
355
356 if(parc > 2)
357 {
358 sendto_match_servs(source_p, parv[2], CAP_ENCAP, NOCAPS,
359 "ENCAP %s MODLOAD %s", parv[2], parv[1]);
360 if (match(parv[2], me.name) == 0)
361 return;
362 }
363
364 do_modload(source_p, parv[1]);
365 }
366
367 static void
368 me_modload(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char **parv)
369 {
370 if(!find_shared_conf(source_p->username, source_p->host, source_p->servptr->name, SHARED_MODULE))
371 {
372 sendto_one_notice(source_p, ":*** You do not have an appropriate shared block "
373 "to load modules on this server.");
374 return;
375 }
376
377 do_modload(source_p, parv[1]);
378 }
379
380
381 /* unload a module .. */
382 static void
383 mo_modunload(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char **parv)
384 {
385 if(!IsOperAdmin(source_p))
386 {
387 sendto_one(source_p, form_str(ERR_NOPRIVS),
388 me.name, source_p->name, "admin");
389 return;
390 }
391
392 if(parc > 2)
393 {
394 sendto_match_servs(source_p, parv[2], CAP_ENCAP, NOCAPS,
395 "ENCAP %s MODUNLOAD %s", parv[2], parv[1]);
396 if (match(parv[2], me.name) == 0)
397 return;
398 }
399
400 do_modunload(source_p, parv[1]);
401 }
402
403 static void
404 me_modunload(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char **parv)
405 {
406 if(!find_shared_conf(source_p->username, source_p->host, source_p->servptr->name, SHARED_MODULE))
407 {
408 sendto_one_notice(source_p, ":*** You do not have an appropriate shared block "
409 "to load modules on this server.");
410 return;
411 }
412
413 do_modunload(source_p, parv[1]);
414 }
415
416 /* unload and load in one! */
417 static void
418 mo_modreload(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char **parv)
419 {
420 if(!IsOperAdmin(source_p))
421 {
422 sendto_one(source_p, form_str(ERR_NOPRIVS),
423 me.name, source_p->name, "admin");
424 return;
425 }
426
427 if(parc > 2)
428 {
429 sendto_match_servs(source_p, parv[2], CAP_ENCAP, NOCAPS,
430 "ENCAP %s MODRELOAD %s", parv[2], parv[1]);
431 if (match(parv[2], me.name) == 0)
432 return;
433 }
434
435 do_modreload(source_p, parv[1]);
436 }
437
438 static void
439 me_modreload(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char **parv)
440 {
441 if(!find_shared_conf(source_p->username, source_p->host, source_p->servptr->name, SHARED_MODULE))
442 {
443 sendto_one_notice(source_p, ":*** You do not have an appropriate shared block "
444 "to load modules on this server.");
445 return;
446 }
447
448 do_modreload(source_p, parv[1]);
449 }
450
451 /* list modules .. */
452 static void
453 mo_modlist(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char **parv)
454 {
455 if(!IsOperAdmin(source_p))
456 {
457 sendto_one(source_p, form_str(ERR_NOPRIVS),
458 me.name, source_p->name, "admin");
459 return;
460 }
461
462 if(parc > 2)
463 {
464 sendto_match_servs(source_p, parv[2], CAP_ENCAP, NOCAPS,
465 "ENCAP %s MODLIST %s", parv[2], parv[1]);
466 if (match(parv[2], me.name) == 0)
467 return;
468 }
469
470 do_modlist(source_p, parc > 1 ? parv[1] : 0);
471 }
472
473 static void
474 me_modlist(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char **parv)
475 {
476 if(!find_shared_conf(source_p->username, source_p->host, source_p->servptr->name, SHARED_MODULE))
477 {
478 sendto_one_notice(source_p, ":*** You do not have an appropriate shared block "
479 "to load modules on this server.");
480 return;
481 }
482
483 do_modlist(source_p, parv[1]);
484 }
485
486 /* unload and reload all modules */
487 static void
488 mo_modrestart(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char **parv)
489 {
490 if(!IsOperAdmin(source_p))
491 {
492 sendto_one(source_p, form_str(ERR_NOPRIVS),
493 me.name, source_p->name, "admin");
494 return;
495 }
496
497 if(parc > 1)
498 {
499 sendto_match_servs(source_p, parv[1], CAP_ENCAP, NOCAPS,
500 "ENCAP %s MODRESTART", parv[1]);
501 if (match(parv[1], me.name) == 0)
502 return;
503 }
504
505 do_modrestart(source_p);
506 }
507
508 static void
509 me_modrestart(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char **parv)
510 {
511 if(!find_shared_conf(source_p->username, source_p->host, source_p->servptr->name, SHARED_MODULE))
512 {
513 sendto_one_notice(source_p, ":*** You do not have an appropriate shared block "
514 "to load modules on this server.");
515 return;
516 }
517
518 do_modrestart(source_p);
519 }
520
521 static void
522 do_modload(struct Client *source_p, const char *module)
523 {
524 char *m_bn = rb_basename(module);
525 int origin;
526
527 if(findmodule_byname(m_bn) != -1)
528 {
529 sendto_one_notice(source_p, ":Module %s is already loaded", m_bn);
530 rb_free(m_bn);
531 return;
532 }
533
534 origin = strcmp(module, m_bn) == 0 ? MAPI_ORIGIN_CORE : MAPI_ORIGIN_EXTENSION;
535 load_one_module(module, origin, 0);
536
537 rb_free(m_bn);
538 }
539
540 static void
541 do_modunload(struct Client *source_p, const char *module)
542 {
543 int modindex;
544 char *m_bn = rb_basename(module);
545
546 if((modindex = findmodule_byname(m_bn)) == -1)
547 {
548 sendto_one_notice(source_p, ":Module %s is not loaded", m_bn);
549 rb_free(m_bn);
550 return;
551 }
552
553 if(modlist[modindex]->core == 1)
554 {
555 sendto_one_notice(source_p, ":Module %s is a core module and may not be unloaded", m_bn);
556 rb_free(m_bn);
557 return;
558 }
559
560 if(unload_one_module(m_bn, 1) == -1)
561 {
562 sendto_one_notice(source_p, ":Module %s is not loaded", m_bn);
563 }
564
565 rb_free(m_bn);
566 }
567
568 static void
569 do_modreload(struct Client *source_p, const char *module)
570 {
571 int modindex;
572 int check_core;
573 char *m_bn = rb_basename(module);
574
575 if((modindex = findmodule_byname(m_bn)) == -1)
576 {
577 sendto_one_notice(source_p, ":Module %s is not loaded", m_bn);
578 rb_free(m_bn);
579 return;
580 }
581
582 check_core = modlist[modindex]->core;
583
584 if(unload_one_module(m_bn, 1) == -1)
585 {
586 sendto_one_notice(source_p, ":Module %s is not loaded", m_bn);
587 rb_free(m_bn);
588 return;
589 }
590
591 if((load_one_module(m_bn, modlist[modindex]->origin, check_core) == -1) && check_core)
592 {
593 sendto_realops_snomask(SNO_GENERAL, L_NETWIDE,
594 "Error reloading core module: %s: terminating ircd", m_bn);
595 ilog(L_MAIN, "Error loading core module %s: terminating ircd", m_bn);
596 exit(0);
597 }
598
599 rb_free(m_bn);
600 }
601
602 static void
603 do_modrestart(struct Client *source_p)
604 {
605 int modnum;
606
607 sendto_one_notice(source_p, ":Reloading all modules");
608
609 modnum = num_mods;
610 while (num_mods)
611 unload_one_module(modlist[0]->name, 0);
612
613 load_all_modules(0);
614 load_core_modules(0);
615 rehash(0);
616
617 sendto_realops_snomask(SNO_GENERAL, L_NETWIDE,
618 "Module Restart: %d modules unloaded, %d modules loaded",
619 modnum, num_mods);
620 ilog(L_MAIN, "Module Restart: %d modules unloaded, %d modules loaded", modnum, num_mods);
621 }
622
623 static void
624 do_modlist(struct Client *source_p, const char *pattern)
625 {
626 int i;
627
628 for (i = 0; i < num_mods; i++)
629 {
630 const char *origin;
631 switch (modlist[i]->origin)
632 {
633 case MAPI_ORIGIN_EXTENSION:
634 origin = "extension";
635 break;
636 case MAPI_ORIGIN_CORE:
637 origin = "builtin";
638 break;
639 default:
640 origin = "unknown";
641 break;
642 }
643
644 if(pattern)
645 {
646 if(match(pattern, modlist[i]->name))
647 {
648 sendto_one(source_p, form_str(RPL_MODLIST),
649 me.name, source_p->name,
650 modlist[i]->name,
651 (unsigned long)(uintptr_t)modlist[i]->address, origin,
652 modlist[i]->core ? " (core)" : "", modlist[i]->version, modlist[i]->description);
653 }
654 }
655 else
656 {
657 sendto_one(source_p, form_str(RPL_MODLIST),
658 me.name, source_p->name, modlist[i]->name,
659 (unsigned long)(uintptr_t)modlist[i]->address, origin,
660 modlist[i]->core ? " (core)" : "", modlist[i]->version, modlist[i]->description);
661 }
662 }
663
664 sendto_one(source_p, form_str(RPL_ENDOFMODLIST), me.name, source_p->name);
665 }
666
667 static void increase_modlist(void);
668
669 #define MODS_INCREMENT 10
670
671 static char unknown_ver[] = "<unknown>";
672 static char unknown_description[] = "<none>";
673
674 /* unload_one_module()
675 *
676 * inputs - name of module to unload
677 * - 1 to say modules unloaded, 0 to not
678 * output - 0 if successful, -1 if error
679 * side effects - module is unloaded
680 */
681 int
682 unload_one_module(const char *name, int warn)
683 {
684 int modindex;
685
686 if((modindex = findmodule_byname(name)) == -1)
687 return -1;
688
689 /*
690 ** XXX - The type system in C does not allow direct conversion between
691 ** data and function pointers, but as it happens, most C compilers will
692 ** safely do this, however it is a theoretical overlow to cast as we
693 ** must do here. I have library functions to take care of this, but
694 ** despite being more "correct" for the C language, this is more
695 ** practical. Removing the abuse of the ability to cast ANY pointer
696 ** to and from an integer value here will break some compilers.
697 ** -jmallett
698 */
699 /* Left the comment in but the code isn't here any more -larne */
700 switch (modlist[modindex]->mapi_version)
701 {
702 case 1:
703 {
704 struct mapi_mheader_av1 *mheader = modlist[modindex]->mapi_header;
705 if(mheader->mapi_command_list)
706 {
707 struct Message **m;
708 for (m = mheader->mapi_command_list; *m; ++m)
709 mod_del_cmd(*m);
710 }
711
712 /* hook events are never removed, we simply lose the
713 * ability to call them --fl
714 */
715 if(mheader->mapi_hfn_list)
716 {
717 mapi_hfn_list_av1 *m;
718 for (m = mheader->mapi_hfn_list; m->hapi_name; ++m)
719 remove_hook(m->hapi_name, m->fn);
720 }
721
722 if(mheader->mapi_unregister)
723 mheader->mapi_unregister();
724 break;
725 }
726 case 2:
727 {
728 struct mapi_mheader_av2 *mheader = modlist[modindex]->mapi_header;
729
730 /* XXX duplicate code :( */
731 if(mheader->mapi_command_list)
732 {
733 struct Message **m;
734 for (m = mheader->mapi_command_list; *m; ++m)
735 mod_del_cmd(*m);
736 }
737
738 /* hook events are never removed, we simply lose the
739 * ability to call them --fl
740 */
741 if(mheader->mapi_hfn_list)
742 {
743 mapi_hfn_list_av1 *m;
744 for (m = mheader->mapi_hfn_list; m->hapi_name; ++m)
745 remove_hook(m->hapi_name, m->fn);
746 }
747
748 if(mheader->mapi_unregister)
749 mheader->mapi_unregister();
750
751 if(mheader->mapi_cap_list)
752 {
753 mapi_cap_list_av2 *m;
754 for (m = mheader->mapi_cap_list; m->cap_name; ++m)
755 {
756 struct CapabilityIndex *idx;
757
758 switch (m->cap_index)
759 {
760 case MAPI_CAP_CLIENT:
761 idx = cli_capindex;
762 break;
763 case MAPI_CAP_SERVER:
764 idx = serv_capindex;
765 break;
766 default:
767 sendto_realops_snomask(SNO_GENERAL, L_ALL,
768 "Unknown/unsupported CAP index found of type %d on capability %s when unloading %s",
769 m->cap_index, m->cap_name, modlist[modindex]->name);
770 ilog(L_MAIN,
771 "Unknown/unsupported CAP index found of type %d on capability %s when unloading %s",
772 m->cap_index, m->cap_name, modlist[modindex]->name);
773 continue;
774 }
775
776 capability_orphan(idx, m->cap_name);
777 }
778 }
779 }
780 default:
781 sendto_realops_snomask(SNO_GENERAL, L_ALL,
782 "Unknown/unsupported MAPI version %d when unloading %s!",
783 modlist[modindex]->mapi_version, modlist[modindex]->name);
784 ilog(L_MAIN, "Unknown/unsupported MAPI version %d when unloading %s!",
785 modlist[modindex]->mapi_version, modlist[modindex]->name);
786 break;
787 }
788
789 lt_dlclose(modlist[modindex]->address);
790
791 rb_free(modlist[modindex]->name);
792 rb_free(modlist[modindex]);
793 memmove(&modlist[modindex], &modlist[modindex + 1],
794 sizeof(struct module *) * ((num_mods - 1) - modindex));
795
796 if(num_mods != 0)
797 num_mods--;
798
799 if(warn == 1)
800 {
801 ilog(L_MAIN, "Module %s unloaded", name);
802 sendto_realops_snomask(SNO_GENERAL, L_ALL, "Module %s unloaded", name);
803 }
804
805 return 0;
806 }
807
808 /*
809 * load_a_module()
810 *
811 * inputs - path name of module, int to notice, int of origin, int of core
812 * output - -1 if error 0 if success
813 * side effects - loads a module if successful
814 */
815 int
816 load_a_module(const char *path, int warn, int origin, int core)
817 {
818 lt_dlhandle tmpptr;
819 char *mod_basename;
820 const char *ver, *description = NULL;
821
822 int *mapi_version;
823
824 mod_basename = rb_basename(path);
825
826 tmpptr = lt_dlopenext(path);
827
828 if(tmpptr == NULL)
829 {
830 const char *err = lt_dlerror();
831
832 sendto_realops_snomask(SNO_GENERAL, L_ALL,
833 "Error loading module %s: %s", mod_basename, err);
834 ilog(L_MAIN, "Error loading module %s: %s", mod_basename, err);
835 rb_free(mod_basename);
836 return -1;
837 }
838
839 /*
840 * _mheader is actually a struct mapi_mheader_*, but mapi_version
841 * is always the first member of this structure, so we treate it
842 * as a single int in order to determine the API version.
843 * -larne.
844 */
845 mapi_version = (int *) (uintptr_t) lt_dlsym(tmpptr, "_mheader");
846 if((mapi_version == NULL
847 && (mapi_version = (int *) (uintptr_t) lt_dlsym(tmpptr, "__mheader")) == NULL)
848 || MAPI_MAGIC(*mapi_version) != MAPI_MAGIC_HDR)
849 {
850 sendto_realops_snomask(SNO_GENERAL, L_ALL,
851 "Data format error: module %s has no MAPI header.",
852 mod_basename);
853 ilog(L_MAIN, "Data format error: module %s has no MAPI header.", mod_basename);
854 (void) lt_dlclose(tmpptr);
855 rb_free(mod_basename);
856 return -1;
857 }
858
859 switch (MAPI_VERSION(*mapi_version))
860 {
861 case 1:
862 {
863 struct mapi_mheader_av1 *mheader = (struct mapi_mheader_av1 *)(void *)mapi_version; /* see above */
864 if(mheader->mapi_register && (mheader->mapi_register() == -1))
865 {
866 ilog(L_MAIN, "Module %s indicated failure during load.",
867 mod_basename);
868 sendto_realops_snomask(SNO_GENERAL, L_ALL,
869 "Module %s indicated failure during load.",
870 mod_basename);
871 lt_dlclose(tmpptr);
872 rb_free(mod_basename);
873 return -1;
874 }
875 if(mheader->mapi_command_list)
876 {
877 struct Message **m;
878 for (m = mheader->mapi_command_list; *m; ++m)
879 mod_add_cmd(*m);
880 }
881
882 if(mheader->mapi_hook_list)
883 {
884 mapi_hlist_av1 *m;
885 for (m = mheader->mapi_hook_list; m->hapi_name; ++m)
886 *m->hapi_id = register_hook(m->hapi_name);
887 }
888
889 if(mheader->mapi_hfn_list)
890 {
891 mapi_hfn_list_av1 *m;
892 for (m = mheader->mapi_hfn_list; m->hapi_name; ++m)
893 add_hook(m->hapi_name, m->fn);
894 }
895
896 ver = mheader->mapi_module_version;
897 break;
898 }
899 case 2:
900 {
901 struct mapi_mheader_av2 *mheader = (struct mapi_mheader_av2 *)(void *)mapi_version; /* see above */
902
903 /* XXX duplicated code :( */
904 if(mheader->mapi_register && (mheader->mapi_register() == -1))
905 {
906 ilog(L_MAIN, "Module %s indicated failure during load.",
907 mod_basename);
908 sendto_realops_snomask(SNO_GENERAL, L_ALL,
909 "Module %s indicated failure during load.",
910 mod_basename);
911 lt_dlclose(tmpptr);
912 rb_free(mod_basename);
913 return -1;
914 }
915
916 /* Basic date code checks
917 *
918 * Don't make them fatal, but do complain about differences within a certain time frame.
919 * Later on if there are major API changes we can add fatal checks.
920 * -- Elizafox
921 */
922 if(mheader->mapi_datecode != datecode && mheader->mapi_datecode > 0)
923 {
924 long int delta = datecode - mheader->mapi_datecode;
925 if (delta > MOD_WARN_DELTA)
926 {
927 delta /= 86400;
928 iwarn("Module %s build date is out of sync with ircd build date by %ld days, expect problems",
929 mod_basename, delta);
930 sendto_realops_snomask(SNO_GENERAL, L_ALL,
931 "Module %s build date is out of sync with ircd build date by %ld days, expect problems",
932 mod_basename, delta);
933 }
934 }
935
936 if(mheader->mapi_command_list)
937 {
938 struct Message **m;
939 for (m = mheader->mapi_command_list; *m; ++m)
940 mod_add_cmd(*m);
941 }
942
943 if(mheader->mapi_hook_list)
944 {
945 mapi_hlist_av1 *m;
946 for (m = mheader->mapi_hook_list; m->hapi_name; ++m)
947 *m->hapi_id = register_hook(m->hapi_name);
948 }
949
950 if(mheader->mapi_hfn_list)
951 {
952 mapi_hfn_list_av1 *m;
953 for (m = mheader->mapi_hfn_list; m->hapi_name; ++m)
954 add_hook(m->hapi_name, m->fn);
955 }
956
957 /* New in MAPI v2 - version replacement */
958 ver = mheader->mapi_module_version ? mheader->mapi_module_version : ircd_version;
959 description = mheader->mapi_module_description;
960
961 if(mheader->mapi_cap_list)
962 {
963 mapi_cap_list_av2 *m;
964 for (m = mheader->mapi_cap_list; m->cap_name; ++m)
965 {
966 struct CapabilityIndex *idx;
967 int result;
968
969 switch (m->cap_index)
970 {
971 case MAPI_CAP_CLIENT:
972 idx = cli_capindex;
973 break;
974 case MAPI_CAP_SERVER:
975 idx = serv_capindex;
976 break;
977 default:
978 sendto_realops_snomask(SNO_GENERAL, L_ALL,
979 "Unknown/unsupported CAP index found of type %d on capability %s when loading %s",
980 m->cap_index, m->cap_name, mod_basename);
981 ilog(L_MAIN,
982 "Unknown/unsupported CAP index found of type %d on capability %s when loading %s",
983 m->cap_index, m->cap_name, mod_basename);
984 continue;
985 }
986
987 result = capability_put(idx, m->cap_name, m->cap_ownerdata);
988 if (m->cap_id != NULL)
989 *(m->cap_id) = result;
990 }
991 }
992 }
993
994 break;
995 default:
996 ilog(L_MAIN, "Module %s has unknown/unsupported MAPI version %d.",
997 mod_basename, MAPI_VERSION(*mapi_version));
998 sendto_realops_snomask(SNO_GENERAL, L_ALL,
999 "Module %s has unknown/unsupported MAPI version %d.",
1000 mod_basename, *mapi_version);
1001 lt_dlclose(tmpptr);
1002 rb_free(mod_basename);
1003 return -1;
1004 }
1005
1006 if(ver == NULL)
1007 ver = unknown_ver;
1008
1009 if(description == NULL)
1010 description = unknown_description;
1011
1012 increase_modlist();
1013
1014 modlist[num_mods] = rb_malloc(sizeof(struct module));
1015 modlist[num_mods]->address = tmpptr;
1016 modlist[num_mods]->version = ver;
1017 modlist[num_mods]->description = description;
1018 modlist[num_mods]->core = core;
1019 modlist[num_mods]->name = rb_strdup(mod_basename);
1020 modlist[num_mods]->mapi_header = mapi_version;
1021 modlist[num_mods]->mapi_version = MAPI_VERSION(*mapi_version);
1022 modlist[num_mods]->origin = origin;
1023 num_mods++;
1024
1025 if(warn == 1)
1026 {
1027 const char *o;
1028
1029 switch (origin)
1030 {
1031 case MAPI_ORIGIN_EXTENSION:
1032 o = "extension";
1033 break;
1034 case MAPI_ORIGIN_CORE:
1035 o = "core";
1036 break;
1037 default:
1038 o = "unknown";
1039 break;
1040 }
1041
1042 sendto_realops_snomask(SNO_GENERAL, L_ALL,
1043 "Module %s [version: %s; MAPI version: %d; origin: %s; description: \"%s\"] loaded at %p",
1044 mod_basename, ver, MAPI_VERSION(*mapi_version), o, description,
1045 (void *) tmpptr);
1046 ilog(L_MAIN, "Module %s [version: %s; MAPI version: %d; origin: %s; description: \"%s\"] loaded at %p",
1047 mod_basename, ver, MAPI_VERSION(*mapi_version), o, description, (void *) tmpptr);
1048 }
1049 rb_free(mod_basename);
1050 return 0;
1051 }
1052
1053 /*
1054 * increase_modlist
1055 *
1056 * inputs - NONE
1057 * output - NONE
1058 * side effects - expand the size of modlist if necessary
1059 */
1060 static void
1061 increase_modlist(void)
1062 {
1063 struct module **new_modlist = NULL;
1064
1065 if((num_mods + 1) < max_mods)
1066 return;
1067
1068 new_modlist = (struct module **) rb_malloc(sizeof(struct module *) *
1069 (max_mods + MODS_INCREMENT));
1070 memcpy((void *) new_modlist, (void *) modlist, sizeof(struct module *) * num_mods);
1071
1072 rb_free(modlist);
1073 modlist = new_modlist;
1074 max_mods += MODS_INCREMENT;
1075 }