| 1 |
/* |
| 2 |
* ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd) |
| 3 |
* |
| 4 |
* Copyright (c) 2000-2020 ircd-hybrid development team |
| 5 |
* |
| 6 |
* This program is free software; you can redistribute it and/or modify |
| 7 |
* it under the terms of the GNU General Public License as published by |
| 8 |
* the Free Software Foundation; either version 2 of the License, or |
| 9 |
* (at your option) any later version. |
| 10 |
* |
| 11 |
* This program is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
* GNU General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU General Public License |
| 17 |
* along with this program; if not, write to the Free Software |
| 18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 |
| 19 |
* USA |
| 20 |
*/ |
| 21 |
|
| 22 |
/*! \file m_module.c |
| 23 |
* \brief Includes required functions for processing the MODULE command. |
| 24 |
* \version $Id$ |
| 25 |
*/ |
| 26 |
|
| 27 |
#include "stdinc.h" |
| 28 |
#include "list.h" |
| 29 |
#include "client.h" |
| 30 |
#include "irc_string.h" |
| 31 |
#include "ircd.h" |
| 32 |
#include "numeric.h" |
| 33 |
#include "log.h" |
| 34 |
#include "send.h" |
| 35 |
#include "parse.h" |
| 36 |
#include "modules.h" |
| 37 |
|
| 38 |
|
| 39 |
/*! \brief LOAD subcommand handler |
| 40 |
* Attempts to load a module, throwing an error if |
| 41 |
* the module has already been loaded |
| 42 |
* \param source_p Pointer to client issuing the command |
| 43 |
* \param arg Additional argument which might be needed by this handler |
| 44 |
*/ |
| 45 |
static void |
| 46 |
module_load(struct Client *source_p, const char *arg) |
| 47 |
{ |
| 48 |
const char *m_bn = NULL; |
| 49 |
|
| 50 |
if (findmodule_byname((m_bn = libio_basename(arg)))) |
| 51 |
{ |
| 52 |
sendto_one_notice(source_p, &me, ":Module %s is already loaded", m_bn); |
| 53 |
return; |
| 54 |
} |
| 55 |
|
| 56 |
load_one_module(arg); |
| 57 |
} |
| 58 |
|
| 59 |
/*! \brief UNLOAD subcommand handler |
| 60 |
* Attempts to unload a module, throwing an error if |
| 61 |
* the module could not be found |
| 62 |
* \param source_p Pointer to client issuing the command |
| 63 |
* \param arg Additional argument which might be needed by this handler |
| 64 |
*/ |
| 65 |
static void |
| 66 |
module_unload(struct Client *source_p, const char *arg) |
| 67 |
{ |
| 68 |
const char *m_bn = NULL; |
| 69 |
const struct module *modp = NULL; |
| 70 |
|
| 71 |
if ((modp = findmodule_byname((m_bn = libio_basename(arg)))) == NULL) |
| 72 |
{ |
| 73 |
sendto_one_notice(source_p, &me, ":Module %s is not loaded", m_bn); |
| 74 |
return; |
| 75 |
} |
| 76 |
|
| 77 |
if (modp->is_core == true) |
| 78 |
{ |
| 79 |
sendto_one_notice(source_p, &me, ":Module %s is a core module and may not be unloaded", |
| 80 |
m_bn); |
| 81 |
return; |
| 82 |
} |
| 83 |
|
| 84 |
if (modp->is_resident == true) |
| 85 |
{ |
| 86 |
sendto_one_notice(source_p, &me, ":Module %s is a resident module and may not be unloaded", |
| 87 |
m_bn); |
| 88 |
return; |
| 89 |
} |
| 90 |
|
| 91 |
if (unload_one_module(m_bn, true) == false) |
| 92 |
sendto_one_notice(source_p, &me, ":Module %s is not loaded", m_bn); |
| 93 |
} |
| 94 |
|
| 95 |
/*! \brief RELOAD subcommand handler |
| 96 |
* Attempts to reload a module, throwing an error if |
| 97 |
* the module could not be found or loaded |
| 98 |
* \param source_p Pointer to client issuing the command |
| 99 |
* \param arg Additional argument which might be needed by this handler |
| 100 |
*/ |
| 101 |
static void |
| 102 |
module_reload(struct Client *source_p, const char *arg) |
| 103 |
{ |
| 104 |
const char *m_bn = NULL; |
| 105 |
struct module *modp = NULL; |
| 106 |
|
| 107 |
if (strcmp(arg, "*") == 0) |
| 108 |
{ |
| 109 |
unsigned int modnum = dlink_list_length(modules_get_list()); |
| 110 |
dlink_node *node, *node_next; |
| 111 |
|
| 112 |
sendto_one_notice(source_p, &me, ":Reloading all modules"); |
| 113 |
|
| 114 |
DLINK_FOREACH_SAFE(node, node_next, modules_get_list()->head) |
| 115 |
{ |
| 116 |
modp = node->data; |
| 117 |
|
| 118 |
if (modp->is_resident == false) |
| 119 |
unload_one_module(modp->name, false); |
| 120 |
} |
| 121 |
|
| 122 |
load_all_modules(0); |
| 123 |
load_conf_modules(); |
| 124 |
load_core_modules(0); |
| 125 |
|
| 126 |
sendto_realops_flags(UMODE_SERVNOTICE, L_ALL, SEND_NOTICE, |
| 127 |
"Module Restart: %u modules unloaded, %u modules loaded", |
| 128 |
modnum, dlink_list_length(modules_get_list())); |
| 129 |
ilog(LOG_TYPE_IRCD, "Module Restart: %u modules unloaded, %u modules loaded", |
| 130 |
modnum, dlink_list_length(modules_get_list())); |
| 131 |
return; |
| 132 |
} |
| 133 |
|
| 134 |
if ((modp = findmodule_byname((m_bn = libio_basename(arg)))) == NULL) |
| 135 |
{ |
| 136 |
sendto_one_notice(source_p, &me, ":Module %s is not loaded", m_bn); |
| 137 |
return; |
| 138 |
} |
| 139 |
|
| 140 |
if (modp->is_resident == true) |
| 141 |
{ |
| 142 |
sendto_one_notice(source_p, &me, ":Module %s is a resident module and may not be unloaded", |
| 143 |
m_bn); |
| 144 |
return; |
| 145 |
} |
| 146 |
|
| 147 |
/* Cache modp->is_core for later use after the module is unloaded */ |
| 148 |
bool is_core = modp->is_core; |
| 149 |
|
| 150 |
if (unload_one_module(m_bn, true) == false) |
| 151 |
{ |
| 152 |
sendto_one_notice(source_p, &me, ":Module %s is not loaded", m_bn); |
| 153 |
return; |
| 154 |
} |
| 155 |
|
| 156 |
if (load_one_module(arg) == false && is_core == true) |
| 157 |
{ |
| 158 |
sendto_realops_flags(UMODE_SERVNOTICE, L_ALL, SEND_NOTICE, |
| 159 |
"Error reloading core " |
| 160 |
"module: %s: terminating ircd", arg); |
| 161 |
ilog(LOG_TYPE_IRCD, "Error loading core module %s: terminating ircd", arg); |
| 162 |
exit(EXIT_FAILURE); |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
/*! \brief LIST subcommand handler |
| 167 |
* Shows a list of loaded modules |
| 168 |
* \param source_p Pointer to client issuing the command |
| 169 |
* \param arg Additional argument which might be needed by this handler |
| 170 |
*/ |
| 171 |
static void |
| 172 |
module_list(struct Client *source_p, const char *arg) |
| 173 |
{ |
| 174 |
dlink_node *node; |
| 175 |
|
| 176 |
DLINK_FOREACH(node, modules_get_list()->head) |
| 177 |
{ |
| 178 |
const struct module *modp = node->data; |
| 179 |
|
| 180 |
if (!EmptyString(arg) && match(arg, modp->name)) |
| 181 |
continue; |
| 182 |
|
| 183 |
sendto_one_numeric(source_p, &me, RPL_MODLIST, |
| 184 |
modp->name, modp->handle, |
| 185 |
modp->version, modp->is_core == true ? "(core)" : ""); |
| 186 |
} |
| 187 |
|
| 188 |
sendto_one_numeric(source_p, &me, RPL_ENDOFMODLIST); |
| 189 |
} |
| 190 |
|
| 191 |
struct ModuleStruct |
| 192 |
{ |
| 193 |
const char *const cmd; |
| 194 |
void (*const handler)(struct Client *, const char *); |
| 195 |
bool arg_required; |
| 196 |
}; |
| 197 |
|
| 198 |
static const struct ModuleStruct module_cmd_table[] = |
| 199 |
{ |
| 200 |
{ .cmd = "LOAD", .handler = module_load, .arg_required = true }, |
| 201 |
{ .cmd = "UNLOAD", .handler = module_unload, .arg_required = true }, |
| 202 |
{ .cmd = "RELOAD", .handler = module_reload, .arg_required = true }, |
| 203 |
{ .cmd = "LIST", .handler = module_list }, |
| 204 |
{ .cmd = NULL } |
| 205 |
}; |
| 206 |
|
| 207 |
/*! \brief MODULE command handler |
| 208 |
* |
| 209 |
* \param source_p Pointer to allocated Client struct from which the message |
| 210 |
* originally comes from. This can be a local or remote client. |
| 211 |
* \param parc Integer holding the number of supplied arguments. |
| 212 |
* \param parv Argument vector where parv[0] .. parv[parc-1] are non-NULL |
| 213 |
* pointers. |
| 214 |
* \note Valid arguments for this command are: |
| 215 |
* - parv[0] = command |
| 216 |
* - parv[1] = MODULE subcommand [LOAD, UNLOAD, RELOAD, LIST] |
| 217 |
* - parv[2] = module name |
| 218 |
*/ |
| 219 |
static void |
| 220 |
mo_module(struct Client *source_p, int parc, char *parv[]) |
| 221 |
{ |
| 222 |
const char *const subcmd = parv[1]; |
| 223 |
const char *const module = parv[2]; |
| 224 |
|
| 225 |
if (!HasOFlag(source_p, OPER_FLAG_MODULE)) |
| 226 |
{ |
| 227 |
sendto_one_numeric(source_p, &me, ERR_NOPRIVS, "module"); |
| 228 |
return; |
| 229 |
} |
| 230 |
|
| 231 |
if (EmptyString(subcmd)) |
| 232 |
{ |
| 233 |
sendto_one_numeric(source_p, &me, ERR_NEEDMOREPARAMS, "MODULE"); |
| 234 |
return; |
| 235 |
} |
| 236 |
|
| 237 |
for (const struct ModuleStruct *tab = module_cmd_table; tab->handler; ++tab) |
| 238 |
{ |
| 239 |
if (irccmp(tab->cmd, subcmd)) |
| 240 |
continue; |
| 241 |
|
| 242 |
if (tab->arg_required == true && EmptyString(module)) |
| 243 |
{ |
| 244 |
sendto_one_numeric(source_p, &me, ERR_NEEDMOREPARAMS, "MODULE"); |
| 245 |
return; |
| 246 |
} |
| 247 |
|
| 248 |
tab->handler(source_p, module); |
| 249 |
return; |
| 250 |
} |
| 251 |
|
| 252 |
sendto_one_notice(source_p, &me, ":%s is not a valid option. " |
| 253 |
"Choose from LOAD, UNLOAD, RELOAD, LIST", |
| 254 |
subcmd); |
| 255 |
} |
| 256 |
|
| 257 |
static struct Message module_msgtab = |
| 258 |
{ |
| 259 |
.cmd = "MODULE", |
| 260 |
.args_min = 2, |
| 261 |
.args_max = MAXPARA, |
| 262 |
.handlers[UNREGISTERED_HANDLER] = m_unregistered, |
| 263 |
.handlers[CLIENT_HANDLER] = m_not_oper, |
| 264 |
.handlers[SERVER_HANDLER] = m_ignore, |
| 265 |
.handlers[ENCAP_HANDLER] = m_ignore, |
| 266 |
.handlers[OPER_HANDLER] = mo_module |
| 267 |
}; |
| 268 |
|
| 269 |
static void |
| 270 |
module_init(void) |
| 271 |
{ |
| 272 |
mod_add_cmd(&module_msgtab); |
| 273 |
} |
| 274 |
|
| 275 |
static void |
| 276 |
module_exit(void) |
| 277 |
{ |
| 278 |
mod_del_cmd(&module_msgtab); |
| 279 |
} |
| 280 |
|
| 281 |
struct module module_entry = |
| 282 |
{ |
| 283 |
.version = "$Revision$", |
| 284 |
.modinit = module_init, |
| 285 |
.modexit = module_exit, |
| 286 |
.is_resident = true |
| 287 |
}; |