2 * ircd-ratbox: A slightly useful ircd.
3 * modules.c: A module loader.
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
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.
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.
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
32 #include "s_newconf.h"
35 #include "ircd_defs.h"
41 struct module **modlist
= NULL
;
43 static const char *core_module_table
[] = {
60 #define MODS_INCREMENT 10
62 int max_mods
= MODS_INCREMENT
;
64 static rb_dlink_list mod_paths
;
66 static int mo_modload(struct MsgBuf
*, struct Client
*, struct Client
*, int, const char **);
67 static int mo_modlist(struct MsgBuf
*, struct Client
*, struct Client
*, int, const char **);
68 static int mo_modreload(struct MsgBuf
*, struct Client
*, struct Client
*, int, const char **);
69 static int mo_modunload(struct MsgBuf
*, struct Client
*, struct Client
*, int, const char **);
70 static int mo_modrestart(struct MsgBuf
*, struct Client
*, struct Client
*, int, const char **);
72 static int me_modload(struct MsgBuf
*, struct Client
*, struct Client
*, int, const char **);
73 static int me_modlist(struct MsgBuf
*, struct Client
*, struct Client
*, int, const char **);
74 static int me_modreload(struct MsgBuf
*, struct Client
*, struct Client
*, int, const char **);
75 static int me_modunload(struct MsgBuf
*, struct Client
*, struct Client
*, int, const char **);
76 static int me_modrestart(struct MsgBuf
*, struct Client
*, struct Client
*, int, const char **);
78 static int do_modload(struct Client
*, const char *);
79 static int do_modunload(struct Client
*, const char *);
80 static int do_modreload(struct Client
*, const char *);
81 static int do_modlist(struct Client
*, const char *);
82 static int do_modrestart(struct Client
*);
84 struct Message modload_msgtab
= {
85 "MODLOAD", 0, 0, 0, 0,
86 {mg_unreg
, mg_not_oper
, mg_ignore
, mg_ignore
, {me_modload
, 2}, {mo_modload
, 2}}
89 struct Message modunload_msgtab
= {
90 "MODUNLOAD", 0, 0, 0, 0,
91 {mg_unreg
, mg_not_oper
, mg_ignore
, mg_ignore
, {me_modunload
, 2}, {mo_modunload
, 2}}
94 struct Message modreload_msgtab
= {
95 "MODRELOAD", 0, 0, 0, 0,
96 {mg_unreg
, mg_not_oper
, mg_ignore
, mg_ignore
, {me_modreload
, 2}, {mo_modreload
, 2}}
99 struct Message modlist_msgtab
= {
100 "MODLIST", 0, 0, 0, 0,
101 {mg_unreg
, mg_not_oper
, mg_ignore
, mg_ignore
, {me_modlist
, 0}, {mo_modlist
, 0}}
104 struct Message modrestart_msgtab
= {
105 "MODRESTART", 0, 0, 0, 0,
106 {mg_unreg
, mg_not_oper
, mg_ignore
, mg_ignore
, {me_modrestart
, 0}, {mo_modrestart
, 0}}
114 ilog(L_MAIN
, "lt_dlinit failed");
118 mod_add_cmd(&modload_msgtab
);
119 mod_add_cmd(&modunload_msgtab
);
120 mod_add_cmd(&modreload_msgtab
);
121 mod_add_cmd(&modlist_msgtab
);
122 mod_add_cmd(&modrestart_msgtab
);
124 /* Add the default paths we look in to the module system --nenolod */
125 mod_add_path(MODPATH
);
126 mod_add_path(AUTOMODPATH
);
133 * side effects - returns a module path from path
136 mod_find_path(const char *path
)
141 RB_DLINK_FOREACH(ptr
, mod_paths
.head
)
145 if(!strcmp(path
, mpath
))
156 * side effects - adds path to list
159 mod_add_path(const char *path
)
163 if(mod_find_path(path
))
166 pathst
= rb_strdup(path
);
167 rb_dlinkAddAlloc(pathst
, &mod_paths
);
174 * side effects - clear the lists of paths
177 mod_clear_paths(void)
179 rb_dlink_node
*ptr
, *next_ptr
;
181 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, mod_paths
.head
)
184 rb_free_rb_dlink_node(ptr
);
187 mod_paths
.head
= mod_paths
.tail
= NULL
;
188 mod_paths
.length
= 0;
199 findmodule_byname(const char *name
)
203 for (i
= 0; i
< num_mods
; i
++)
205 if(!irccmp(modlist
[i
]->name
, name
))
211 /* load_all_modules()
218 load_all_modules(int warn
)
220 DIR *system_module_dir
= NULL
;
221 struct dirent
*ldirent
= NULL
;
222 char module_fq_name
[PATH_MAX
+ 1];
227 modlist
= (struct module **) rb_malloc(sizeof(struct module *) * (MODS_INCREMENT
));
229 max_mods
= MODS_INCREMENT
;
231 system_module_dir
= opendir(AUTOMODPATH
);
233 if(system_module_dir
== NULL
)
235 ilog(L_MAIN
, "Could not load modules from %s: %s", AUTOMODPATH
, strerror(errno
));
239 while ((ldirent
= readdir(system_module_dir
)) != NULL
)
241 len
= strlen(ldirent
->d_name
);
242 if((len
> 3) && !strcmp(ldirent
->d_name
+len
-3, ".la"))
244 (void) snprintf(module_fq_name
, sizeof(module_fq_name
), "%s/%s", AUTOMODPATH
, ldirent
->d_name
);
245 (void) load_a_module(module_fq_name
, warn
, 0);
249 (void) closedir(system_module_dir
);
252 /* load_core_modules()
256 * side effects - core modules are loaded, if any fail, kill ircd
259 load_core_modules(int warn
)
261 char module_name
[PATH_MAX
];
265 for (i
= 0; core_module_table
[i
]; i
++)
267 snprintf(module_name
, sizeof(module_name
), "%s/%s%s", MODPATH
,
268 core_module_table
[i
], ".la");
270 if(load_a_module(module_name
, warn
, 1) == -1)
273 "Error loading core module %s%s: terminating ircd",
274 core_module_table
[i
], ".la");
287 load_one_module(const char *path
, int coremodule
)
289 char modpath
[PATH_MAX
];
290 rb_dlink_node
*pathst
;
295 if (server_state_foreground
== 1)
296 inotice("loading module %s ...", path
);
298 RB_DLINK_FOREACH(pathst
, mod_paths
.head
)
300 mpath
= pathst
->data
;
302 snprintf(modpath
, sizeof(modpath
), "%s/%s", mpath
, path
);
303 if((strstr(modpath
, "../") == NULL
) && (strstr(modpath
, "/..") == NULL
))
305 if(stat(modpath
, &statbuf
) == 0)
307 if(S_ISREG(statbuf
.st_mode
))
309 /* Regular files only please */
311 return load_a_module(modpath
, 1, 1);
313 return load_a_module(modpath
, 1, 0);
320 sendto_realops_snomask(SNO_GENERAL
, L_ALL
, "Cannot locate module %s", path
);
325 /* load a module .. */
327 mo_modload(struct MsgBuf
*msgbuf_p
, struct Client
*client_p
, struct Client
*source_p
, int parc
, const char **parv
)
329 if(!IsOperAdmin(source_p
))
331 sendto_one(source_p
, form_str(ERR_NOPRIVS
),
332 me
.name
, source_p
->name
, "admin");
338 sendto_match_servs(source_p
, parv
[2], CAP_ENCAP
, NOCAPS
,
339 "ENCAP %s MODLOAD %s", parv
[2], parv
[1]);
340 if (match(parv
[2], me
.name
) == 0)
344 return do_modload(source_p
, parv
[1]);
348 me_modload(struct MsgBuf
*msgbuf_p
, struct Client
*client_p
, struct Client
*source_p
, int parc
, const char **parv
)
350 if(!find_shared_conf(source_p
->username
, source_p
->host
, source_p
->servptr
->name
, SHARED_MODULE
))
352 sendto_one_notice(source_p
, ":*** You do not have an appropriate shared block "
353 "to load modules on this server.");
357 return do_modload(source_p
, parv
[1]);
361 do_modload(struct Client
*source_p
, const char *module)
363 char *m_bn
= rb_basename(module);
365 if(findmodule_byname(m_bn
) != -1)
367 sendto_one_notice(source_p
, ":Module %s is already loaded", m_bn
);
372 load_one_module(module, 0);
380 /* unload a module .. */
382 mo_modunload(struct MsgBuf
*msgbuf_p
, struct Client
*client_p
, struct Client
*source_p
, int parc
, const char **parv
)
384 if(!IsOperAdmin(source_p
))
386 sendto_one(source_p
, form_str(ERR_NOPRIVS
),
387 me
.name
, source_p
->name
, "admin");
393 sendto_match_servs(source_p
, parv
[2], CAP_ENCAP
, NOCAPS
,
394 "ENCAP %s MODUNLOAD %s", parv
[2], parv
[1]);
395 if (match(parv
[2], me
.name
) == 0)
399 return do_modunload(source_p
, parv
[1]);
403 me_modunload(struct MsgBuf
*msgbuf_p
, struct Client
*client_p
, struct Client
*source_p
, int parc
, const char **parv
)
405 if(!find_shared_conf(source_p
->username
, source_p
->host
, source_p
->servptr
->name
, SHARED_MODULE
))
407 sendto_one_notice(source_p
, ":*** You do not have an appropriate shared block "
408 "to load modules on this server.");
412 return do_modunload(source_p
, parv
[1]);
416 do_modunload(struct Client
*source_p
, const char *module)
419 char *m_bn
= rb_basename(module);
421 if((modindex
= findmodule_byname(m_bn
)) == -1)
423 sendto_one_notice(source_p
, ":Module %s is not loaded", m_bn
);
428 if(modlist
[modindex
]->core
== 1)
430 sendto_one_notice(source_p
, ":Module %s is a core module and may not be unloaded", m_bn
);
435 if(unload_one_module(m_bn
, 1) == -1)
437 sendto_one_notice(source_p
, ":Module %s is not loaded", m_bn
);
444 /* unload and load in one! */
446 mo_modreload(struct MsgBuf
*msgbuf_p
, struct Client
*client_p
, struct Client
*source_p
, int parc
, const char **parv
)
448 if(!IsOperAdmin(source_p
))
450 sendto_one(source_p
, form_str(ERR_NOPRIVS
),
451 me
.name
, source_p
->name
, "admin");
457 sendto_match_servs(source_p
, parv
[2], CAP_ENCAP
, NOCAPS
,
458 "ENCAP %s MODRELOAD %s", parv
[2], parv
[1]);
459 if (match(parv
[2], me
.name
) == 0)
463 return do_modreload(source_p
, parv
[1]);
467 me_modreload(struct MsgBuf
*msgbuf_p
, struct Client
*client_p
, struct Client
*source_p
, int parc
, const char **parv
)
469 if(!find_shared_conf(source_p
->username
, source_p
->host
, source_p
->servptr
->name
, SHARED_MODULE
))
471 sendto_one_notice(source_p
, ":*** You do not have an appropriate shared block "
472 "to load modules on this server.");
476 return do_modreload(source_p
, parv
[1]);
480 do_modreload(struct Client
*source_p
, const char *module)
484 char *m_bn
= rb_basename(module);
486 if((modindex
= findmodule_byname(m_bn
)) == -1)
488 sendto_one_notice(source_p
, ":Module %s is not loaded", m_bn
);
493 check_core
= modlist
[modindex
]->core
;
495 if(unload_one_module(m_bn
, 1) == -1)
497 sendto_one_notice(source_p
, ":Module %s is not loaded", m_bn
);
502 if((load_one_module(m_bn
, check_core
) == -1) && check_core
)
504 sendto_realops_snomask(SNO_GENERAL
, L_NETWIDE
,
505 "Error reloading core module: %s: terminating ircd", m_bn
);
506 ilog(L_MAIN
, "Error loading core module %s: terminating ircd", m_bn
);
514 /* list modules .. */
516 mo_modlist(struct MsgBuf
*msgbuf_p
, struct Client
*client_p
, struct Client
*source_p
, int parc
, const char **parv
)
518 if(!IsOperAdmin(source_p
))
520 sendto_one(source_p
, form_str(ERR_NOPRIVS
),
521 me
.name
, source_p
->name
, "admin");
527 sendto_match_servs(source_p
, parv
[2], CAP_ENCAP
, NOCAPS
,
528 "ENCAP %s MODLIST %s", parv
[2], parv
[1]);
529 if (match(parv
[2], me
.name
) == 0)
533 return do_modlist(source_p
, parc
> 1 ? parv
[1] : 0);
537 me_modlist(struct MsgBuf
*msgbuf_p
, struct Client
*client_p
, struct Client
*source_p
, int parc
, const char **parv
)
539 if(!find_shared_conf(source_p
->username
, source_p
->host
, source_p
->servptr
->name
, SHARED_MODULE
))
541 sendto_one_notice(source_p
, ":*** You do not have an appropriate shared block "
542 "to load modules on this server.");
546 return do_modlist(source_p
, parv
[1]);
550 do_modlist(struct Client
*source_p
, const char *pattern
)
554 for (i
= 0; i
< num_mods
; i
++)
558 if(match(pattern
, modlist
[i
]->name
))
560 sendto_one(source_p
, form_str(RPL_MODLIST
),
561 me
.name
, source_p
->name
,
563 (unsigned long)(uintptr_t)modlist
[i
]->address
,
564 modlist
[i
]->version
, modlist
[i
]->core
? "(core)" : "");
569 sendto_one(source_p
, form_str(RPL_MODLIST
),
570 me
.name
, source_p
->name
, modlist
[i
]->name
,
571 (unsigned long)(uintptr_t)modlist
[i
]->address
, modlist
[i
]->version
,
572 modlist
[i
]->core
? "(core)" : "");
576 sendto_one(source_p
, form_str(RPL_ENDOFMODLIST
), me
.name
, source_p
->name
);
580 /* unload and reload all modules */
582 mo_modrestart(struct MsgBuf
*msgbuf_p
, struct Client
*client_p
, struct Client
*source_p
, int parc
, const char **parv
)
584 if(!IsOperAdmin(source_p
))
586 sendto_one(source_p
, form_str(ERR_NOPRIVS
),
587 me
.name
, source_p
->name
, "admin");
593 sendto_match_servs(source_p
, parv
[1], CAP_ENCAP
, NOCAPS
,
594 "ENCAP %s MODRESTART", parv
[1]);
595 if (match(parv
[1], me
.name
) == 0)
599 return do_modrestart(source_p
);
603 me_modrestart(struct MsgBuf
*msgbuf_p
, struct Client
*client_p
, struct Client
*source_p
, int parc
, const char **parv
)
605 if(!find_shared_conf(source_p
->username
, source_p
->host
, source_p
->servptr
->name
, SHARED_MODULE
))
607 sendto_one_notice(source_p
, ":*** You do not have an appropriate shared block "
608 "to load modules on this server.");
612 return do_modrestart(source_p
);
616 do_modrestart(struct Client
*source_p
)
620 sendto_one_notice(source_p
, ":Reloading all modules");
624 unload_one_module(modlist
[0]->name
, 0);
627 load_core_modules(0);
630 sendto_realops_snomask(SNO_GENERAL
, L_NETWIDE
,
631 "Module Restart: %d modules unloaded, %d modules loaded",
633 ilog(L_MAIN
, "Module Restart: %d modules unloaded, %d modules loaded", modnum
, num_mods
);
638 static void increase_modlist(void);
640 #define MODS_INCREMENT 10
642 static char unknown_ver
[] = "<unknown>";
644 /* unload_one_module()
646 * inputs - name of module to unload
647 * - 1 to say modules unloaded, 0 to not
648 * output - 0 if successful, -1 if error
649 * side effects - module is unloaded
652 unload_one_module(const char *name
, int warn
)
656 if((modindex
= findmodule_byname(name
)) == -1)
660 ** XXX - The type system in C does not allow direct conversion between
661 ** data and function pointers, but as it happens, most C compilers will
662 ** safely do this, however it is a theoretical overlow to cast as we
663 ** must do here. I have library functions to take care of this, but
664 ** despite being more "correct" for the C language, this is more
665 ** practical. Removing the abuse of the ability to cast ANY pointer
666 ** to and from an integer value here will break some compilers.
669 /* Left the comment in but the code isn't here any more -larne */
670 switch (modlist
[modindex
]->mapi_version
)
674 struct mapi_mheader_av1
*mheader
= modlist
[modindex
]->mapi_header
;
675 if(mheader
->mapi_command_list
)
678 for (m
= mheader
->mapi_command_list
; *m
; ++m
)
682 /* hook events are never removed, we simply lose the
683 * ability to call them --fl
685 if(mheader
->mapi_hfn_list
)
687 mapi_hfn_list_av1
*m
;
688 for (m
= mheader
->mapi_hfn_list
; m
->hapi_name
; ++m
)
689 remove_hook(m
->hapi_name
, m
->fn
);
692 if(mheader
->mapi_unregister
)
693 mheader
->mapi_unregister();
697 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
698 "Unknown/unsupported MAPI version %d when unloading %s!",
699 modlist
[modindex
]->mapi_version
, modlist
[modindex
]->name
);
700 ilog(L_MAIN
, "Unknown/unsupported MAPI version %d when unloading %s!",
701 modlist
[modindex
]->mapi_version
, modlist
[modindex
]->name
);
705 lt_dlclose(modlist
[modindex
]->address
);
707 rb_free(modlist
[modindex
]->name
);
708 rb_free(modlist
[modindex
]);
709 memmove(&modlist
[modindex
], &modlist
[modindex
+ 1],
710 sizeof(struct module *) * ((num_mods
- 1) - modindex
));
717 ilog(L_MAIN
, "Module %s unloaded", name
);
718 sendto_realops_snomask(SNO_GENERAL
, L_ALL
, "Module %s unloaded", name
);
728 * inputs - path name of module, int to notice, int of core
729 * output - -1 if error 0 if success
730 * side effects - loads a module if successful
733 load_a_module(const char *path
, int warn
, int core
)
741 mod_basename
= rb_basename(path
);
743 tmpptr
= lt_dlopen(path
);
747 const char *err
= lt_dlerror();
749 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
750 "Error loading module %s: %s", mod_basename
, err
);
751 ilog(L_MAIN
, "Error loading module %s: %s", mod_basename
, err
);
752 rb_free(mod_basename
);
758 * _mheader is actually a struct mapi_mheader_*, but mapi_version
759 * is always the first member of this structure, so we treate it
760 * as a single int in order to determine the API version.
763 mapi_version
= (int *) (uintptr_t) lt_dlsym(tmpptr
, "_mheader");
764 if((mapi_version
== NULL
765 && (mapi_version
= (int *) (uintptr_t) lt_dlsym(tmpptr
, "__mheader")) == NULL
)
766 || MAPI_MAGIC(*mapi_version
) != MAPI_MAGIC_HDR
)
768 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
769 "Data format error: module %s has no MAPI header.",
771 ilog(L_MAIN
, "Data format error: module %s has no MAPI header.", mod_basename
);
772 (void) lt_dlclose(tmpptr
);
773 rb_free(mod_basename
);
777 switch (MAPI_VERSION(*mapi_version
))
781 struct mapi_mheader_av1
*mheader
= (struct mapi_mheader_av1
*)(void *)mapi_version
; /* see above */
782 if(mheader
->mapi_register
&& (mheader
->mapi_register() == -1))
784 ilog(L_MAIN
, "Module %s indicated failure during load.",
786 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
787 "Module %s indicated failure during load.",
790 rb_free(mod_basename
);
793 if(mheader
->mapi_command_list
)
796 for (m
= mheader
->mapi_command_list
; *m
; ++m
)
800 if(mheader
->mapi_hook_list
)
803 for (m
= mheader
->mapi_hook_list
; m
->hapi_name
; ++m
)
804 *m
->hapi_id
= register_hook(m
->hapi_name
);
807 if(mheader
->mapi_hfn_list
)
809 mapi_hfn_list_av1
*m
;
810 for (m
= mheader
->mapi_hfn_list
; m
->hapi_name
; ++m
)
811 add_hook(m
->hapi_name
, m
->fn
);
814 ver
= mheader
->mapi_module_version
;
819 ilog(L_MAIN
, "Module %s has unknown/unsupported MAPI version %d.",
820 mod_basename
, MAPI_VERSION(*mapi_version
));
821 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
822 "Module %s has unknown/unsupported MAPI version %d.",
823 mod_basename
, *mapi_version
);
825 rb_free(mod_basename
);
834 modlist
[num_mods
] = rb_malloc(sizeof(struct module));
835 modlist
[num_mods
]->address
= tmpptr
;
836 modlist
[num_mods
]->version
= ver
;
837 modlist
[num_mods
]->core
= core
;
838 modlist
[num_mods
]->name
= rb_strdup(mod_basename
);
839 modlist
[num_mods
]->mapi_header
= mapi_version
;
840 modlist
[num_mods
]->mapi_version
= MAPI_VERSION(*mapi_version
);
845 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
846 "Module %s [version: %s; MAPI version: %d] loaded at 0x%lx",
847 mod_basename
, ver
, MAPI_VERSION(*mapi_version
),
848 (unsigned long) tmpptr
);
849 ilog(L_MAIN
, "Module %s [version: %s; MAPI version: %d] loaded at 0x%lx",
850 mod_basename
, ver
, MAPI_VERSION(*mapi_version
), (unsigned long) tmpptr
);
852 rb_free(mod_basename
);
861 * side effects - expand the size of modlist if necessary
864 increase_modlist(void)
866 struct module **new_modlist
= NULL
;
868 if((num_mods
+ 1) < max_mods
)
871 new_modlist
= (struct module **) rb_malloc(sizeof(struct module *) *
872 (max_mods
+ MODS_INCREMENT
));
873 memcpy((void *) new_modlist
, (void *) modlist
, sizeof(struct module *) * num_mods
);
876 modlist
= new_modlist
;
877 max_mods
+= MODS_INCREMENT
;