| 1 |
adx |
30 |
/* |
| 2 |
|
|
* ircd-hybrid: an advanced Internet Relay Chat Daemon(ircd). |
| 3 |
|
|
* modules.c: A module loader. |
| 4 |
|
|
* |
| 5 |
|
|
* Copyright (C) 2002 by the past and present ircd coders, and others. |
| 6 |
|
|
* |
| 7 |
|
|
* This program is free software; you can redistribute it and/or modify |
| 8 |
|
|
* it under the terms of the GNU General Public License as published by |
| 9 |
|
|
* the Free Software Foundation; either version 2 of the License, or |
| 10 |
|
|
* (at your option) any later version. |
| 11 |
|
|
* |
| 12 |
|
|
* This program is distributed in the hope that it will be useful, |
| 13 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
|
|
* GNU General Public License for more details. |
| 16 |
|
|
* |
| 17 |
|
|
* You should have received a copy of the GNU General Public License |
| 18 |
|
|
* along with this program; if not, write to the Free Software |
| 19 |
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
| 20 |
|
|
* USA |
| 21 |
|
|
* |
| 22 |
knight |
31 |
* $Id$ |
| 23 |
adx |
30 |
*/ |
| 24 |
|
|
|
| 25 |
michael |
1237 |
#include "ltdl.h" |
| 26 |
|
|
|
| 27 |
adx |
30 |
#include "stdinc.h" |
| 28 |
michael |
1011 |
#include "list.h" |
| 29 |
adx |
30 |
#include "modules.h" |
| 30 |
michael |
1309 |
#include "log.h" |
| 31 |
adx |
30 |
#include "ircd.h" |
| 32 |
|
|
#include "client.h" |
| 33 |
|
|
#include "send.h" |
| 34 |
michael |
1309 |
#include "conf.h" |
| 35 |
adx |
30 |
#include "numeric.h" |
| 36 |
|
|
#include "parse.h" |
| 37 |
|
|
#include "ircd_defs.h" |
| 38 |
|
|
#include "irc_string.h" |
| 39 |
|
|
#include "memory.h" |
| 40 |
|
|
|
| 41 |
|
|
|
| 42 |
michael |
1447 |
dlink_list modules_list = { NULL, NULL, 0 }; |
| 43 |
adx |
30 |
|
| 44 |
michael |
1237 |
static const char *unknown_ver = "<unknown>"; |
| 45 |
|
|
|
| 46 |
adx |
30 |
static const char *core_module_table[] = |
| 47 |
|
|
{ |
| 48 |
michael |
912 |
"m_die.la", |
| 49 |
michael |
946 |
"m_error.la", |
| 50 |
michael |
912 |
"m_join.la", |
| 51 |
|
|
"m_kick.la", |
| 52 |
|
|
"m_kill.la", |
| 53 |
|
|
"m_message.la", |
| 54 |
|
|
"m_mode.la", |
| 55 |
|
|
"m_nick.la", |
| 56 |
|
|
"m_part.la", |
| 57 |
|
|
"m_quit.la", |
| 58 |
|
|
"m_server.la", |
| 59 |
|
|
"m_sjoin.la", |
| 60 |
|
|
"m_squit.la", |
| 61 |
adx |
30 |
NULL |
| 62 |
|
|
}; |
| 63 |
|
|
|
| 64 |
|
|
static dlink_list mod_paths = { NULL, NULL, 0 }; |
| 65 |
|
|
static dlink_list conf_modules = { NULL, NULL, 0 }; |
| 66 |
|
|
|
| 67 |
michael |
1237 |
int |
| 68 |
|
|
modules_valid_suffix(const char *name) |
| 69 |
|
|
{ |
| 70 |
|
|
return ((name = strrchr(name, '.'))) && !strcmp(name, ".la"); |
| 71 |
|
|
} |
| 72 |
|
|
|
| 73 |
|
|
/* unload_one_module() |
| 74 |
|
|
* |
| 75 |
|
|
* inputs - name of module to unload |
| 76 |
|
|
* - 1 to say modules unloaded, 0 to not |
| 77 |
|
|
* output - 0 if successful, -1 if error |
| 78 |
|
|
* side effects - module is unloaded |
| 79 |
|
|
*/ |
| 80 |
|
|
int |
| 81 |
|
|
unload_one_module(const char *name, int warn) |
| 82 |
|
|
{ |
| 83 |
|
|
struct module *modp = NULL; |
| 84 |
|
|
|
| 85 |
|
|
if ((modp = findmodule_byname(name)) == NULL) |
| 86 |
|
|
return -1; |
| 87 |
|
|
|
| 88 |
|
|
if (modp->modexit) |
| 89 |
|
|
modp->modexit(); |
| 90 |
|
|
|
| 91 |
michael |
1238 |
assert(dlink_list_length(&modules_list) > 0); |
| 92 |
|
|
dlinkDelete(&modp->node, &modules_list); |
| 93 |
michael |
1237 |
MyFree(modp->name); |
| 94 |
|
|
|
| 95 |
|
|
lt_dlclose(modp->handle); |
| 96 |
|
|
|
| 97 |
|
|
if (warn == 1) |
| 98 |
|
|
{ |
| 99 |
michael |
1247 |
ilog(LOG_TYPE_IRCD, "Module %s unloaded", name); |
| 100 |
michael |
1618 |
sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE, |
| 101 |
|
|
"Module %s unloaded", name); |
| 102 |
michael |
1237 |
} |
| 103 |
|
|
|
| 104 |
|
|
return 0; |
| 105 |
|
|
} |
| 106 |
|
|
|
| 107 |
|
|
/* load_a_module() |
| 108 |
|
|
* |
| 109 |
|
|
* inputs - path name of module, int to notice, int of core |
| 110 |
|
|
* output - -1 if error 0 if success |
| 111 |
|
|
* side effects - loads a module if successful |
| 112 |
|
|
*/ |
| 113 |
|
|
int |
| 114 |
michael |
1404 |
load_a_module(const char *path, int warn) |
| 115 |
michael |
1237 |
{ |
| 116 |
|
|
lt_dlhandle tmpptr = NULL; |
| 117 |
|
|
const char *mod_basename = NULL; |
| 118 |
|
|
struct module *modp = NULL; |
| 119 |
|
|
|
| 120 |
|
|
if (findmodule_byname((mod_basename = libio_basename(path)))) |
| 121 |
|
|
return 1; |
| 122 |
|
|
|
| 123 |
|
|
if (!(tmpptr = lt_dlopen(path))) { |
| 124 |
|
|
const char *err = ((err = lt_dlerror())) ? err : "<unknown>"; |
| 125 |
|
|
|
| 126 |
michael |
1618 |
sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE, |
| 127 |
|
|
"Error loading module %s: %s", |
| 128 |
michael |
1237 |
mod_basename, err); |
| 129 |
michael |
1247 |
ilog(LOG_TYPE_IRCD, "Error loading module %s: %s", mod_basename, err); |
| 130 |
michael |
1237 |
return -1; |
| 131 |
|
|
} |
| 132 |
|
|
|
| 133 |
|
|
if ((modp = lt_dlsym(tmpptr, "module_entry")) == NULL) |
| 134 |
|
|
{ |
| 135 |
michael |
1574 |
const char *err = ((err = lt_dlerror())) ? err : "<unknown>"; |
| 136 |
|
|
|
| 137 |
michael |
1618 |
sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE, |
| 138 |
|
|
"Error loading module %s: %s", |
| 139 |
michael |
1574 |
mod_basename, err); |
| 140 |
|
|
ilog(LOG_TYPE_IRCD, "Error loading module %s: %s", mod_basename, err); |
| 141 |
michael |
1237 |
lt_dlclose(tmpptr); |
| 142 |
|
|
return -1; |
| 143 |
|
|
} |
| 144 |
|
|
|
| 145 |
|
|
modp->handle = tmpptr; |
| 146 |
|
|
|
| 147 |
|
|
if (EmptyString(modp->version)) |
| 148 |
|
|
modp->version = unknown_ver; |
| 149 |
|
|
|
| 150 |
michael |
1646 |
modp->name = xstrdup(mod_basename); |
| 151 |
michael |
1238 |
dlinkAdd(modp, &modp->node, &modules_list); |
| 152 |
michael |
1237 |
|
| 153 |
|
|
if (modp->modinit) |
| 154 |
|
|
modp->modinit(); |
| 155 |
|
|
|
| 156 |
|
|
if (warn == 1) |
| 157 |
|
|
{ |
| 158 |
michael |
1618 |
sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE, |
| 159 |
michael |
1237 |
"Module %s [version: %s handle: %p] loaded.", |
| 160 |
|
|
modp->name, modp->version, tmpptr); |
| 161 |
michael |
1247 |
ilog(LOG_TYPE_IRCD, "Module %s [version: %s handle: %p] loaded.", |
| 162 |
michael |
1237 |
modp->name, modp->version, tmpptr); |
| 163 |
|
|
} |
| 164 |
|
|
|
| 165 |
|
|
return 0; |
| 166 |
|
|
} |
| 167 |
|
|
|
| 168 |
adx |
30 |
/* |
| 169 |
|
|
* modules_init |
| 170 |
|
|
* |
| 171 |
|
|
* input - NONE |
| 172 |
|
|
* output - NONE |
| 173 |
|
|
* side effects - The basic module manipulation modules are loaded |
| 174 |
|
|
*/ |
| 175 |
|
|
void |
| 176 |
|
|
modules_init(void) |
| 177 |
|
|
{ |
| 178 |
michael |
1237 |
if (lt_dlinit()) |
| 179 |
|
|
{ |
| 180 |
michael |
1247 |
ilog(LOG_TYPE_IRCD, "Couldn't initialize the libltdl run time dynamic" |
| 181 |
michael |
1237 |
" link library. Exiting."); |
| 182 |
|
|
exit(0); |
| 183 |
|
|
} |
| 184 |
adx |
30 |
} |
| 185 |
|
|
|
| 186 |
|
|
/* mod_find_path() |
| 187 |
|
|
* |
| 188 |
|
|
* input - path |
| 189 |
|
|
* output - none |
| 190 |
|
|
* side effects - returns a module path from path |
| 191 |
|
|
*/ |
| 192 |
|
|
static struct module_path * |
| 193 |
|
|
mod_find_path(const char *path) |
| 194 |
|
|
{ |
| 195 |
|
|
dlink_node *ptr; |
| 196 |
|
|
|
| 197 |
|
|
DLINK_FOREACH(ptr, mod_paths.head) |
| 198 |
|
|
{ |
| 199 |
michael |
1404 |
struct module_path *mpath = ptr->data; |
| 200 |
adx |
30 |
|
| 201 |
|
|
if (!strcmp(path, mpath->path)) |
| 202 |
|
|
return mpath; |
| 203 |
|
|
} |
| 204 |
|
|
|
| 205 |
|
|
return NULL; |
| 206 |
|
|
} |
| 207 |
|
|
|
| 208 |
|
|
/* mod_add_path() |
| 209 |
|
|
* |
| 210 |
|
|
* input - path |
| 211 |
|
|
* output - NONE |
| 212 |
|
|
* side effects - adds path to list |
| 213 |
|
|
*/ |
| 214 |
|
|
void |
| 215 |
|
|
mod_add_path(const char *path) |
| 216 |
|
|
{ |
| 217 |
|
|
struct module_path *pathst; |
| 218 |
|
|
|
| 219 |
|
|
if (mod_find_path(path)) |
| 220 |
|
|
return; |
| 221 |
|
|
|
| 222 |
|
|
pathst = MyMalloc(sizeof(struct module_path)); |
| 223 |
|
|
|
| 224 |
|
|
strlcpy(pathst->path, path, sizeof(pathst->path)); |
| 225 |
|
|
dlinkAdd(pathst, &pathst->node, &mod_paths); |
| 226 |
|
|
} |
| 227 |
|
|
|
| 228 |
|
|
/* add_conf_module |
| 229 |
|
|
* |
| 230 |
|
|
* input - module name |
| 231 |
|
|
* output - NONE |
| 232 |
|
|
* side effects - adds module to conf_mod |
| 233 |
|
|
*/ |
| 234 |
|
|
void |
| 235 |
|
|
add_conf_module(const char *name) |
| 236 |
|
|
{ |
| 237 |
|
|
struct module_path *pathst; |
| 238 |
|
|
|
| 239 |
|
|
pathst = MyMalloc(sizeof(struct module_path)); |
| 240 |
|
|
|
| 241 |
|
|
strlcpy(pathst->path, name, sizeof(pathst->path)); |
| 242 |
|
|
dlinkAdd(pathst, &pathst->node, &conf_modules); |
| 243 |
|
|
} |
| 244 |
|
|
|
| 245 |
|
|
/* mod_clear_paths() |
| 246 |
|
|
* |
| 247 |
|
|
* input - NONE |
| 248 |
|
|
* output - NONE |
| 249 |
|
|
* side effects - clear the lists of paths and conf modules |
| 250 |
|
|
*/ |
| 251 |
|
|
void |
| 252 |
|
|
mod_clear_paths(void) |
| 253 |
|
|
{ |
| 254 |
michael |
1237 |
dlink_node *ptr = NULL, *next_ptr = NULL; |
| 255 |
adx |
30 |
|
| 256 |
|
|
DLINK_FOREACH_SAFE(ptr, next_ptr, mod_paths.head) |
| 257 |
|
|
{ |
| 258 |
michael |
1404 |
dlinkDelete(ptr, &mod_paths); |
| 259 |
|
|
MyFree(ptr->data); |
| 260 |
adx |
30 |
} |
| 261 |
|
|
|
| 262 |
|
|
DLINK_FOREACH_SAFE(ptr, next_ptr, conf_modules.head) |
| 263 |
|
|
{ |
| 264 |
michael |
1404 |
dlinkDelete(ptr, &conf_modules); |
| 265 |
|
|
MyFree(ptr->data); |
| 266 |
adx |
30 |
} |
| 267 |
|
|
} |
| 268 |
|
|
|
| 269 |
|
|
/* findmodule_byname |
| 270 |
|
|
* |
| 271 |
|
|
* input - name of module |
| 272 |
|
|
* output - NULL if not found or pointer to module |
| 273 |
|
|
* side effects - NONE |
| 274 |
|
|
*/ |
| 275 |
michael |
1230 |
struct module * |
| 276 |
adx |
30 |
findmodule_byname(const char *name) |
| 277 |
|
|
{ |
| 278 |
michael |
1230 |
dlink_node *ptr = NULL; |
| 279 |
adx |
30 |
|
| 280 |
michael |
1238 |
DLINK_FOREACH(ptr, modules_list.head) |
| 281 |
adx |
30 |
{ |
| 282 |
michael |
1230 |
struct module *modp = ptr->data; |
| 283 |
adx |
30 |
|
| 284 |
db |
218 |
if (strcmp(modp->name, name) == 0) |
| 285 |
michael |
1230 |
return modp; |
| 286 |
adx |
30 |
} |
| 287 |
|
|
|
| 288 |
|
|
return NULL; |
| 289 |
|
|
} |
| 290 |
|
|
|
| 291 |
|
|
/* load_all_modules() |
| 292 |
|
|
* |
| 293 |
|
|
* input - int flag warn |
| 294 |
|
|
* output - NONE |
| 295 |
|
|
* side effects - load all modules found in autoload directory |
| 296 |
|
|
*/ |
| 297 |
|
|
void |
| 298 |
|
|
load_all_modules(int warn) |
| 299 |
|
|
{ |
| 300 |
|
|
DIR *system_module_dir = NULL; |
| 301 |
|
|
struct dirent *ldirent = NULL; |
| 302 |
michael |
1737 |
char module_fq_name[HYB_PATH_MAX + 1]; |
| 303 |
adx |
30 |
|
| 304 |
|
|
if ((system_module_dir = opendir(AUTOMODPATH)) == NULL) |
| 305 |
|
|
{ |
| 306 |
michael |
1247 |
ilog(LOG_TYPE_IRCD, "Could not load modules from %s: %s", |
| 307 |
adx |
30 |
AUTOMODPATH, strerror(errno)); |
| 308 |
|
|
return; |
| 309 |
|
|
} |
| 310 |
|
|
|
| 311 |
|
|
while ((ldirent = readdir(system_module_dir)) != NULL) |
| 312 |
|
|
{ |
| 313 |
michael |
912 |
if (modules_valid_suffix(ldirent->d_name)) |
| 314 |
adx |
30 |
{ |
| 315 |
|
|
snprintf(module_fq_name, sizeof(module_fq_name), "%s/%s", |
| 316 |
|
|
AUTOMODPATH, ldirent->d_name); |
| 317 |
michael |
1404 |
load_a_module(module_fq_name, warn); |
| 318 |
adx |
30 |
} |
| 319 |
|
|
} |
| 320 |
|
|
|
| 321 |
|
|
closedir(system_module_dir); |
| 322 |
|
|
} |
| 323 |
|
|
|
| 324 |
|
|
/* load_conf_modules() |
| 325 |
|
|
* |
| 326 |
|
|
* input - NONE |
| 327 |
|
|
* output - NONE |
| 328 |
|
|
* side effects - load modules given in ircd.conf |
| 329 |
|
|
*/ |
| 330 |
|
|
void |
| 331 |
|
|
load_conf_modules(void) |
| 332 |
|
|
{ |
| 333 |
|
|
dlink_node *ptr = NULL; |
| 334 |
|
|
|
| 335 |
|
|
DLINK_FOREACH(ptr, conf_modules.head) |
| 336 |
|
|
{ |
| 337 |
michael |
1404 |
struct module_path *mpath = ptr->data; |
| 338 |
adx |
30 |
|
| 339 |
|
|
if (findmodule_byname(mpath->path) == NULL) |
| 340 |
michael |
1404 |
load_one_module(mpath->path); |
| 341 |
adx |
30 |
} |
| 342 |
|
|
} |
| 343 |
|
|
|
| 344 |
|
|
/* load_core_modules() |
| 345 |
|
|
* |
| 346 |
|
|
* input - int flag warn |
| 347 |
|
|
* output - NONE |
| 348 |
|
|
* side effects - core modules are loaded, if any fail, kill ircd |
| 349 |
|
|
*/ |
| 350 |
|
|
void |
| 351 |
|
|
load_core_modules(int warn) |
| 352 |
|
|
{ |
| 353 |
michael |
1737 |
char module_name[HYB_PATH_MAX + 1]; |
| 354 |
adx |
30 |
int i = 0; |
| 355 |
|
|
|
| 356 |
|
|
for (; core_module_table[i]; ++i) |
| 357 |
|
|
{ |
| 358 |
michael |
912 |
snprintf(module_name, sizeof(module_name), "%s%s", |
| 359 |
|
|
MODPATH, core_module_table[i]); |
| 360 |
adx |
30 |
|
| 361 |
michael |
1404 |
if (load_a_module(module_name, warn) == -1) |
| 362 |
adx |
30 |
{ |
| 363 |
michael |
1247 |
ilog(LOG_TYPE_IRCD, "Error loading core module %s: terminating ircd", |
| 364 |
michael |
912 |
core_module_table[i]); |
| 365 |
adx |
30 |
exit(EXIT_FAILURE); |
| 366 |
|
|
} |
| 367 |
|
|
} |
| 368 |
|
|
} |
| 369 |
|
|
|
| 370 |
|
|
/* load_one_module() |
| 371 |
|
|
* |
| 372 |
|
|
* input - pointer to path |
| 373 |
|
|
* - flagged as core module or not |
| 374 |
|
|
* output - -1 if error |
| 375 |
|
|
* side effects - module is loaded if found. |
| 376 |
|
|
*/ |
| 377 |
|
|
int |
| 378 |
michael |
1404 |
load_one_module(const char *path) |
| 379 |
adx |
30 |
{ |
| 380 |
|
|
dlink_node *ptr = NULL; |
| 381 |
michael |
1737 |
char modpath[HYB_PATH_MAX + 1]; |
| 382 |
adx |
30 |
struct stat statbuf; |
| 383 |
|
|
|
| 384 |
|
|
DLINK_FOREACH(ptr, mod_paths.head) |
| 385 |
|
|
{ |
| 386 |
|
|
const struct module_path *mpath = ptr->data; |
| 387 |
|
|
|
| 388 |
|
|
snprintf(modpath, sizeof(modpath), "%s/%s", mpath->path, path); |
| 389 |
|
|
|
| 390 |
michael |
912 |
if (!modules_valid_suffix(path)) |
| 391 |
|
|
continue; |
| 392 |
|
|
|
| 393 |
adx |
30 |
if (strstr(modpath, "../") == NULL && |
| 394 |
|
|
strstr(modpath, "/..") == NULL) |
| 395 |
|
|
if (!stat(modpath, &statbuf)) |
| 396 |
michael |
1404 |
if (S_ISREG(statbuf.st_mode)) /* Regular files only please */ |
| 397 |
|
|
return load_a_module(modpath, 1); |
| 398 |
adx |
30 |
} |
| 399 |
|
|
|
| 400 |
michael |
1618 |
sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE, |
| 401 |
adx |
30 |
"Cannot locate module %s", path); |
| 402 |
michael |
1247 |
ilog(LOG_TYPE_IRCD, "Cannot locate module %s", path); |
| 403 |
adx |
30 |
return -1; |
| 404 |
|
|
} |