]> jfr.im git - solanum.git/blobdiff - ircd/modules.c
chmode: Get elevated access for op-only queries
[solanum.git] / ircd / modules.c
index f10232ad4a6322d0b97359d63c388398b9fa035c..e4dc728bdb4a000a448f3146ada82169ecaa39da 100644 (file)
 #include "match.h"
 #include "s_serv.h"
 #include "capability.h"
+#include "hash.h"
 
 #include <ltdl.h>
 
 #ifndef LT_MODULE_EXT
-#      error "Charybdis requires loadable module support."
+#      error "Solanum requires loadable module support."
 #endif
 
-struct module **modlist = NULL;
+rb_dlink_list module_list;
+rb_dlink_list mod_paths;
 
 static const char *core_module_table[] = {
        "m_ban",
        "m_die",
        "m_error",
+       "m_identified",
        "m_join",
        "m_kick",
        "m_kill",
@@ -65,12 +68,6 @@ static const char *core_module_table[] = {
 
 #define MOD_WARN_DELTA (90 * 86400)    /* time in seconds, 86400 seconds in a day */
 
-#define MODS_INCREMENT 10
-int num_mods = 0;
-int max_mods = MODS_INCREMENT;
-
-static rb_dlink_list mod_paths;
-
 void
 init_modules(void)
 {
@@ -80,13 +77,34 @@ init_modules(void)
                exit(EXIT_FAILURE);
        }
 
-       modlist = (struct module **) rb_malloc(sizeof(struct module *) * (MODS_INCREMENT));
-
        /* Add the default paths we look in to the module system --nenolod */
        mod_add_path(ircd_paths[IRCD_PATH_MODULES]);
        mod_add_path(ircd_paths[IRCD_PATH_AUTOLOAD_MODULES]);
 }
 
+static unsigned int prev_caps;
+
+void
+mod_remember_clicaps(void)
+{
+       prev_caps = capability_index_mask(cli_capindex);
+}
+
+void
+mod_notify_clicaps(void)
+{
+       unsigned int cur_caps = capability_index_mask(cli_capindex);
+       unsigned int del = prev_caps & ~cur_caps;
+       unsigned int new = cur_caps & ~prev_caps;
+
+       if (del)
+               sendto_local_clients_with_capability(CLICAP_CAP_NOTIFY, ":%s CAP * DEL :%s",
+                               me.name, capability_index_list(cli_capindex, del));
+       if (new)
+               sendto_local_clients_with_capability(CLICAP_CAP_NOTIFY, ":%s CAP * NEW :%s",
+                               me.name, capability_index_list(cli_capindex, new));
+}
+
 /* mod_find_path()
  *
  * input       - path
@@ -155,25 +173,27 @@ mod_clear_paths(void)
  * output       - index of module on success, -1 on failure
  * side effects - none
  */
-int
+struct module *
 findmodule_byname(const char *name)
 {
-       int i;
+       rb_dlink_node *ptr;
        char name_ext[PATH_MAX + 1];
 
        rb_strlcpy(name_ext, name, sizeof name_ext);
        rb_strlcat(name_ext, LT_MODULE_EXT, sizeof name_ext);
 
-       for (i = 0; i < num_mods; i++)
+       RB_DLINK_FOREACH(ptr, module_list.head)
        {
-               if(!irccmp(modlist[i]->name, name))
-                       return i;
+               struct module *mod = ptr->data;
+
+               if(!irccmp(mod->name, name))
+                       return mod;
 
-               if(!irccmp(modlist[i]->name, name_ext))
-                       return i;
+               if(!irccmp(mod->name, name_ext))
+                       return mod;
        }
 
-       return -1;
+       return NULL;
 }
 
 /* load_all_modules()
@@ -190,8 +210,6 @@ load_all_modules(bool warn)
        char module_fq_name[PATH_MAX + 1];
        size_t module_ext_len = strlen(LT_MODULE_EXT);
 
-       max_mods = MODS_INCREMENT;
-
        system_module_dir = opendir(ircd_paths[IRCD_PATH_AUTOLOAD_MODULES]);
 
        if(system_module_dir == NULL)
@@ -280,11 +298,13 @@ load_one_module(const char *path, int origin, bool coremodule)
        }
 
        sendto_realops_snomask(SNO_GENERAL, L_ALL, "Cannot locate module %s", path);
+
+       if (server_state_foreground)
+               ierror("cannot locate module %s", path);
+
        return false;
 }
 
-static void increase_modlist(void);
-
 static char unknown_ver[] = "<unknown>";
 static char unknown_description[] = "<none>";
 
@@ -298,9 +318,9 @@ static char unknown_description[] = "<none>";
 bool
 unload_one_module(const char *name, bool warn)
 {
-       int modindex;
+       struct module *mod;
 
-       if((modindex = findmodule_byname(name)) == -1)
+       if((mod = findmodule_byname(name)) == NULL)
                return false;
 
        /*
@@ -314,11 +334,11 @@ unload_one_module(const char *name, bool warn)
         **          -jmallett
         */
        /* Left the comment in but the code isn't here any more         -larne */
-       switch (modlist[modindex]->mapi_version)
+       switch (mod->mapi_version)
        {
        case 1:
                {
-                       struct mapi_mheader_av1 *mheader = modlist[modindex]->mapi_header;
+                       struct mapi_mheader_av1 *mheader = mod->mapi_header;
                        if(mheader->mapi_command_list)
                        {
                                struct Message **m;
@@ -342,7 +362,7 @@ unload_one_module(const char *name, bool warn)
                }
        case 2:
                {
-                       struct mapi_mheader_av2 *mheader = modlist[modindex]->mapi_header;
+                       struct mapi_mheader_av2 *mheader = mod->mapi_header;
 
                        /* XXX duplicate code :( */
                        if(mheader->mapi_command_list)
@@ -383,18 +403,14 @@ unload_one_module(const char *name, bool warn)
                                        default:
                                                sendto_realops_snomask(SNO_GENERAL, L_ALL,
                                                        "Unknown/unsupported CAP index found of type %d on capability %s when unloading %s",
-                                                       m->cap_index, m->cap_name, modlist[modindex]->name);
+                                                       m->cap_index, m->cap_name, mod->name);
                                                ilog(L_MAIN,
                                                        "Unknown/unsupported CAP index found of type %d on capability %s when unloading %s",
-                                                       m->cap_index, m->cap_name, modlist[modindex]->name);
+                                                       m->cap_index, m->cap_name, mod->name);
                                                continue;
                                        }
 
-                                       if (m->cap_id != NULL)
-                                       {
-                                               capability_orphan(idx, m->cap_name);
-                                               sendto_local_clients_with_capability(CLICAP_CAP_NOTIFY, ":%s CAP * DEL :%s", me.name, m->cap_name);
-                                       }
+                                       capability_orphan(idx, m->cap_name);
                                }
                        }
                        break;
@@ -402,21 +418,18 @@ unload_one_module(const char *name, bool warn)
        default:
                sendto_realops_snomask(SNO_GENERAL, L_ALL,
                                     "Unknown/unsupported MAPI version %d when unloading %s!",
-                                    modlist[modindex]->mapi_version, modlist[modindex]->name);
+                                    mod->mapi_version, mod->name);
                ilog(L_MAIN, "Unknown/unsupported MAPI version %d when unloading %s!",
-                    modlist[modindex]->mapi_version, modlist[modindex]->name);
+                    mod->mapi_version, mod->name);
                break;
        }
 
-       lt_dlclose(modlist[modindex]->address);
+       lt_dlclose(mod->address);
 
-       rb_free(modlist[modindex]->name);
-       rb_free(modlist[modindex]);
-       memmove(&modlist[modindex], &modlist[modindex + 1],
-              sizeof(struct module *) * ((num_mods - 1) - modindex));
-
-       if(num_mods != 0)
-               num_mods--;
+       rb_dlinkDelete(&mod->node, &module_list);
+       rb_free(mod->name);
+       rb_free(mod->path);
+       rb_free(mod);
 
        if(warn)
        {
@@ -437,10 +450,10 @@ unload_one_module(const char *name, bool warn)
 bool
 load_a_module(const char *path, bool warn, int origin, bool core)
 {
+       struct module *mod;
        lt_dlhandle tmpptr;
        char *mod_displayname, *c;
        const char *ver, *description = NULL;
-       size_t module_ext_len = strlen(LT_MODULE_EXT);
 
        int *mapi_version;
 
@@ -527,6 +540,38 @@ load_a_module(const char *path, bool warn, int origin, bool core)
                {
                        struct mapi_mheader_av2 *mheader = (struct mapi_mheader_av2 *)(void *)mapi_version;     /* see above */
 
+                       if(mheader->mapi_cap_list)
+                       {
+                               mapi_cap_list_av2 *m;
+                               for (m = mheader->mapi_cap_list; m->cap_name; ++m)
+                               {
+                                       struct CapabilityIndex *idx;
+                                       int result;
+
+                                       switch (m->cap_index)
+                                       {
+                                       case MAPI_CAP_CLIENT:
+                                               idx = cli_capindex;
+                                               break;
+                                       case MAPI_CAP_SERVER:
+                                               idx = serv_capindex;
+                                               break;
+                                       default:
+                                               sendto_realops_snomask(SNO_GENERAL, L_ALL,
+                                                       "Unknown/unsupported CAP index found of type %d on capability %s when loading %s",
+                                                       m->cap_index, m->cap_name, mod_displayname);
+                                               ilog(L_MAIN,
+                                                       "Unknown/unsupported CAP index found of type %d on capability %s when loading %s",
+                                                       m->cap_index, m->cap_name, mod_displayname);
+                                               continue;
+                                       }
+
+                                       result = capability_put(idx, m->cap_name, m->cap_ownerdata);
+                                       if (m->cap_id != NULL)
+                                               *(m->cap_id) = result;
+                               }
+                       }
+
                        /* XXX duplicated code :( */
                        if(mheader->mapi_register && (mheader->mapi_register() == -1))
                        {
@@ -535,6 +580,26 @@ load_a_module(const char *path, bool warn, int origin, bool core)
                                sendto_realops_snomask(SNO_GENERAL, L_ALL,
                                                     "Module %s indicated failure during load.",
                                                     mod_displayname);
+                               if(mheader->mapi_cap_list)
+                               {
+                                       mapi_cap_list_av2 *m;
+                                       for (m = mheader->mapi_cap_list; m->cap_name; ++m)
+                                       {
+                                               struct CapabilityIndex *idx;
+                                               switch (m->cap_index)
+                                               {
+                                               case MAPI_CAP_CLIENT:
+                                                       idx = cli_capindex;
+                                                       break;
+                                               case MAPI_CAP_SERVER:
+                                                       idx = serv_capindex;
+                                                       break;
+                                               default:
+                                                       continue;
+                                               }
+                                               capability_orphan(idx, m->cap_name);
+                                       }
+                               }
                                lt_dlclose(tmpptr);
                                rb_free(mod_displayname);
                                return false;
@@ -578,47 +643,17 @@ load_a_module(const char *path, bool warn, int origin, bool core)
                        {
                                mapi_hfn_list_av1 *m;
                                for (m = mheader->mapi_hfn_list; m->hapi_name; ++m)
-                                       add_hook(m->hapi_name, m->fn);
+                               {
+                                       int priority = m->priority;
+                                       if (priority == 0)
+                                               priority = HOOK_NORMAL;
+                                       add_hook_prio(m->hapi_name, m->fn, priority);
+                               }
                        }
 
                        /* New in MAPI v2 - version replacement */
                        ver = mheader->mapi_module_version ? mheader->mapi_module_version : ircd_version;
                        description = mheader->mapi_module_description;
-
-                       if(mheader->mapi_cap_list)
-                       {
-                               mapi_cap_list_av2 *m;
-                               for (m = mheader->mapi_cap_list; m->cap_name; ++m)
-                               {
-                                       struct CapabilityIndex *idx;
-                                       int result;
-
-                                       switch (m->cap_index)
-                                       {
-                                       case MAPI_CAP_CLIENT:
-                                               idx = cli_capindex;
-                                               break;
-                                       case MAPI_CAP_SERVER:
-                                               idx = serv_capindex;
-                                               break;
-                                       default:
-                                               sendto_realops_snomask(SNO_GENERAL, L_ALL,
-                                                       "Unknown/unsupported CAP index found of type %d on capability %s when loading %s",
-                                                       m->cap_index, m->cap_name, mod_displayname);
-                                               ilog(L_MAIN,
-                                                       "Unknown/unsupported CAP index found of type %d on capability %s when loading %s",
-                                                       m->cap_index, m->cap_name, mod_displayname);
-                                               continue;
-                                       }
-
-                                       result = capability_put(idx, m->cap_name, m->cap_ownerdata);
-                                       if (m->cap_id != NULL)
-                                       {
-                                               *(m->cap_id) = result;
-                                               sendto_local_clients_with_capability(CLICAP_CAP_NOTIFY, ":%s CAP * ADD :%s", me.name, m->cap_name);
-                                       }
-                               }
-                       }
                }
 
                break;
@@ -639,18 +674,17 @@ load_a_module(const char *path, bool warn, int origin, bool core)
        if(description == NULL)
                description = unknown_description;
 
-       increase_modlist();
-
-       modlist[num_mods] = rb_malloc(sizeof(struct module));
-       modlist[num_mods]->address = tmpptr;
-       modlist[num_mods]->version = ver;
-       modlist[num_mods]->description = description;
-       modlist[num_mods]->core = core;
-       modlist[num_mods]->name = rb_strdup(mod_displayname);
-       modlist[num_mods]->mapi_header = mapi_version;
-       modlist[num_mods]->mapi_version = MAPI_VERSION(*mapi_version);
-       modlist[num_mods]->origin = origin;
-       num_mods++;
+       mod = rb_malloc(sizeof(struct module));
+       mod->address = tmpptr;
+       mod->version = ver;
+       mod->description = description;
+       mod->core = core;
+       mod->name = rb_strdup(mod_displayname);
+       mod->mapi_header = mapi_version;
+       mod->mapi_version = MAPI_VERSION(*mapi_version);
+       mod->origin = origin;
+       mod->path = rb_strdup(path);
+       rb_dlinkAdd(mod, &mod->node, &module_list);
 
        if(warn)
        {
@@ -680,26 +714,90 @@ load_a_module(const char *path, bool warn, int origin, bool core)
        return true;
 }
 
-/*
- * increase_modlist
- *
- * inputs      - NONE
- * output      - NONE
- * side effects        - expand the size of modlist if necessary
- */
-static void
-increase_modlist(void)
+void
+modules_do_reload(void *info_)
 {
-       struct module **new_modlist = NULL;
+       struct modreload *info = info_;
+       struct module *mod;
+       int check_core;
+       int origin;
+       char *m_bn = rb_basename(info->module);
+       char *path;
+       struct Client *source_p = find_id(info->id);
+
+       if((mod = findmodule_byname(m_bn)) == NULL)
+       {
+               if (source_p) sendto_one_notice(source_p, ":Module %s is not loaded", m_bn);
+               rb_free(info);
+               rb_free(m_bn);
+               return;
+       }
+
+       origin = mod->origin;
+       check_core = mod->core;
+       path = rb_strdup(mod->path);
 
-       if((num_mods + 1) < max_mods)
+       mod_remember_clicaps();
+
+       if(unload_one_module(m_bn, true) == false)
+       {
+               if (source_p) sendto_one_notice(source_p, ":Module %s is not loaded", m_bn);
+               rb_free(info);
+               rb_free(m_bn);
+               rb_free(path);
                return;
+       }
+
+       if((load_a_module(path, true, origin, check_core) == false) && check_core)
+       {
+               sendto_realops_snomask(SNO_GENERAL, L_NETWIDE,
+                                    "Error reloading core module: %s: terminating ircd", m_bn);
+               ilog(L_MAIN, "Error loading core module %s: terminating ircd", m_bn);
+               exit(0);
+       }
+
+       mod_notify_clicaps();
+
+       rb_free(info);
+       rb_free(m_bn);
+       rb_free(path);
+}
+
+void
+modules_do_restart(void *unused)
+{
+       unsigned int modnum = 0;
+       rb_dlink_node *ptr, *nptr;
+
+       mod_remember_clicaps();
+
+       RB_DLINK_FOREACH_SAFE(ptr, nptr, module_list.head)
+       {
+               struct module *mod = ptr->data;
+               if(!unload_one_module(mod->name, false))
+               {
+                       ilog(L_MAIN, "Module Restart: %s was not unloaded %s",
+                            mod->name,
+                            mod->core? "(core module)" : "");
+
+                       if(!mod->core)
+                               sendto_realops_snomask(SNO_GENERAL, L_NETWIDE,
+                                                      "Module Restart: %s failed to unload",
+                                                      mod->name);
+                       continue;
+               }
+
+               modnum++;
+       }
+
+       load_all_modules(false);
+       load_core_modules(false);
+       rehash(false);
 
-       new_modlist = (struct module **) rb_malloc(sizeof(struct module *) *
-                                                 (max_mods + MODS_INCREMENT));
-       memcpy((void *) new_modlist, (void *) modlist, sizeof(struct module *) * num_mods);
+       mod_notify_clicaps();
 
-       rb_free(modlist);
-       modlist = new_modlist;
-       max_mods += MODS_INCREMENT;
+       sendto_realops_snomask(SNO_GENERAL, L_NETWIDE,
+                            "Module Restart: %u modules unloaded, %lu modules loaded",
+                            modnum, rb_dlink_list_length(&module_list));
+       ilog(L_MAIN, "Module Restart: %u modules unloaded, %lu modules loaded", modnum, rb_dlink_list_length(&module_list));
 }