1 |
/* |
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 |
* $Id$ |
23 |
*/ |
24 |
|
25 |
#include "ltdl.h" |
26 |
|
27 |
#include "stdinc.h" |
28 |
#include "list.h" |
29 |
#include "modules.h" |
30 |
#include "s_log.h" |
31 |
#include "ircd.h" |
32 |
#include "client.h" |
33 |
#include "send.h" |
34 |
#include "s_conf.h" |
35 |
#include "handlers.h" |
36 |
#include "numeric.h" |
37 |
#include "parse.h" |
38 |
#include "ircd_defs.h" |
39 |
#include "irc_string.h" |
40 |
#include "memory.h" |
41 |
|
42 |
|
43 |
static dlink_list modules_list = { NULL, NULL, 0 }; |
44 |
|
45 |
static const char *unknown_ver = "<unknown>"; |
46 |
|
47 |
static const char *core_module_table[] = |
48 |
{ |
49 |
"m_die.la", |
50 |
"m_error.la", |
51 |
"m_join.la", |
52 |
"m_kick.la", |
53 |
"m_kill.la", |
54 |
"m_message.la", |
55 |
"m_mode.la", |
56 |
"m_nick.la", |
57 |
"m_part.la", |
58 |
"m_quit.la", |
59 |
"m_server.la", |
60 |
"m_sjoin.la", |
61 |
"m_squit.la", |
62 |
NULL |
63 |
}; |
64 |
|
65 |
static dlink_list mod_paths = { NULL, NULL, 0 }; |
66 |
static dlink_list conf_modules = { NULL, NULL, 0 }; |
67 |
|
68 |
static void mo_modload(struct Client *, struct Client *, int, char *[]); |
69 |
static void mo_modlist(struct Client *, struct Client *, int, char *[]); |
70 |
static void mo_modreload(struct Client *, struct Client *, int, char *[]); |
71 |
static void mo_modunload(struct Client *, struct Client *, int, char *[]); |
72 |
static void mo_modrestart(struct Client *, struct Client *, int, char *[]); |
73 |
|
74 |
struct Message modload_msgtab = { |
75 |
"MODLOAD", 0, 0, 2, 0, MFLG_SLOW, 0, |
76 |
{m_unregistered, m_not_oper, m_ignore, m_ignore, mo_modload, m_ignore} |
77 |
}; |
78 |
|
79 |
struct Message modunload_msgtab = { |
80 |
"MODUNLOAD", 0, 0, 2, 0, MFLG_SLOW, 0, |
81 |
{m_unregistered, m_not_oper, m_ignore, m_ignore, mo_modunload, m_ignore} |
82 |
}; |
83 |
|
84 |
struct Message modreload_msgtab = { |
85 |
"MODRELOAD", 0, 0, 2, 0, MFLG_SLOW, 0, |
86 |
{m_unregistered, m_not_oper, m_ignore, m_ignore, mo_modreload, m_ignore} |
87 |
}; |
88 |
|
89 |
struct Message modlist_msgtab = { |
90 |
"MODLIST", 0, 0, 0, 0, MFLG_SLOW, 0, |
91 |
{m_unregistered, m_not_oper, m_ignore, m_ignore, mo_modlist, m_ignore} |
92 |
}; |
93 |
|
94 |
struct Message modrestart_msgtab = { |
95 |
"MODRESTART", 0, 0, 0, 0, MFLG_SLOW, 0, |
96 |
{m_unregistered, m_not_oper, m_ignore, m_ignore, mo_modrestart, m_ignore} |
97 |
}; |
98 |
|
99 |
|
100 |
int |
101 |
modules_valid_suffix(const char *name) |
102 |
{ |
103 |
return ((name = strrchr(name, '.'))) && !strcmp(name, ".la"); |
104 |
} |
105 |
|
106 |
/* unload_one_module() |
107 |
* |
108 |
* inputs - name of module to unload |
109 |
* - 1 to say modules unloaded, 0 to not |
110 |
* output - 0 if successful, -1 if error |
111 |
* side effects - module is unloaded |
112 |
*/ |
113 |
int |
114 |
unload_one_module(const char *name, int warn) |
115 |
{ |
116 |
struct module *modp = NULL; |
117 |
|
118 |
if ((modp = findmodule_byname(name)) == NULL) |
119 |
return -1; |
120 |
|
121 |
if (modp->modexit) |
122 |
modp->modexit(); |
123 |
|
124 |
assert(dlink_list_length(&modules_list) > 0); |
125 |
dlinkDelete(&modp->node, &modules_list); |
126 |
MyFree(modp->name); |
127 |
|
128 |
lt_dlclose(modp->handle); |
129 |
|
130 |
if (warn == 1) |
131 |
{ |
132 |
ilog(L_INFO, "Module %s unloaded", name); |
133 |
sendto_realops_flags(UMODE_ALL, L_ALL, "Module %s unloaded", name); |
134 |
} |
135 |
|
136 |
return 0; |
137 |
} |
138 |
|
139 |
/* load_a_module() |
140 |
* |
141 |
* inputs - path name of module, int to notice, int of core |
142 |
* output - -1 if error 0 if success |
143 |
* side effects - loads a module if successful |
144 |
*/ |
145 |
int |
146 |
load_a_module(const char *path, int warn, int core) |
147 |
{ |
148 |
lt_dlhandle tmpptr = NULL; |
149 |
const char *mod_basename = NULL; |
150 |
struct module *modp = NULL; |
151 |
|
152 |
if (findmodule_byname((mod_basename = libio_basename(path)))) |
153 |
return 1; |
154 |
|
155 |
if (!(tmpptr = lt_dlopen(path))) { |
156 |
const char *err = ((err = lt_dlerror())) ? err : "<unknown>"; |
157 |
|
158 |
sendto_realops_flags(UMODE_ALL, L_ALL, "Error loading module %s: %s", |
159 |
mod_basename, err); |
160 |
ilog(L_WARN, "Error loading module %s: %s", mod_basename, err); |
161 |
return -1; |
162 |
} |
163 |
|
164 |
if ((modp = lt_dlsym(tmpptr, "module_entry")) == NULL) |
165 |
{ |
166 |
sendto_realops_flags(UMODE_ALL, L_ALL, "Module %s has no module_entry export", |
167 |
mod_basename); |
168 |
ilog(L_WARN, "Module %s has no module_entry export", mod_basename); |
169 |
lt_dlclose(tmpptr); |
170 |
return -1; |
171 |
} |
172 |
|
173 |
modp->handle = tmpptr; |
174 |
|
175 |
if (EmptyString(modp->version)) |
176 |
modp->version = unknown_ver; |
177 |
|
178 |
if (core) |
179 |
modp->flags |= MODULE_FLAG_CORE; |
180 |
|
181 |
DupString(modp->name, mod_basename); |
182 |
dlinkAdd(modp, &modp->node, &modules_list); |
183 |
|
184 |
if (modp->modinit) |
185 |
modp->modinit(); |
186 |
|
187 |
if (warn == 1) |
188 |
{ |
189 |
sendto_realops_flags(UMODE_ALL, L_ALL, |
190 |
"Module %s [version: %s handle: %p] loaded.", |
191 |
modp->name, modp->version, tmpptr); |
192 |
ilog(L_WARN, "Module %s [version: %s handle: %p] loaded.", |
193 |
modp->name, modp->version, tmpptr); |
194 |
} |
195 |
|
196 |
return 0; |
197 |
} |
198 |
|
199 |
/* |
200 |
* modules_init |
201 |
* |
202 |
* input - NONE |
203 |
* output - NONE |
204 |
* side effects - The basic module manipulation modules are loaded |
205 |
*/ |
206 |
void |
207 |
modules_init(void) |
208 |
{ |
209 |
if (lt_dlinit()) |
210 |
{ |
211 |
ilog(L_ERROR, "Couldn't initialize the libltdl run time dynamic" |
212 |
" link library. Exiting."); |
213 |
exit(0); |
214 |
} |
215 |
|
216 |
mod_add_cmd(&modload_msgtab); |
217 |
mod_add_cmd(&modunload_msgtab); |
218 |
mod_add_cmd(&modreload_msgtab); |
219 |
mod_add_cmd(&modlist_msgtab); |
220 |
mod_add_cmd(&modrestart_msgtab); |
221 |
} |
222 |
|
223 |
/* mod_find_path() |
224 |
* |
225 |
* input - path |
226 |
* output - none |
227 |
* side effects - returns a module path from path |
228 |
*/ |
229 |
static struct module_path * |
230 |
mod_find_path(const char *path) |
231 |
{ |
232 |
dlink_node *ptr; |
233 |
struct module_path *mpath; |
234 |
|
235 |
DLINK_FOREACH(ptr, mod_paths.head) |
236 |
{ |
237 |
mpath = ptr->data; |
238 |
|
239 |
if (!strcmp(path, mpath->path)) |
240 |
return mpath; |
241 |
} |
242 |
|
243 |
return NULL; |
244 |
} |
245 |
|
246 |
/* mod_add_path() |
247 |
* |
248 |
* input - path |
249 |
* output - NONE |
250 |
* side effects - adds path to list |
251 |
*/ |
252 |
void |
253 |
mod_add_path(const char *path) |
254 |
{ |
255 |
struct module_path *pathst; |
256 |
|
257 |
if (mod_find_path(path)) |
258 |
return; |
259 |
|
260 |
pathst = MyMalloc(sizeof(struct module_path)); |
261 |
|
262 |
strlcpy(pathst->path, path, sizeof(pathst->path)); |
263 |
dlinkAdd(pathst, &pathst->node, &mod_paths); |
264 |
} |
265 |
|
266 |
/* add_conf_module |
267 |
* |
268 |
* input - module name |
269 |
* output - NONE |
270 |
* side effects - adds module to conf_mod |
271 |
*/ |
272 |
void |
273 |
add_conf_module(const char *name) |
274 |
{ |
275 |
struct module_path *pathst; |
276 |
|
277 |
pathst = MyMalloc(sizeof(struct module_path)); |
278 |
|
279 |
strlcpy(pathst->path, name, sizeof(pathst->path)); |
280 |
dlinkAdd(pathst, &pathst->node, &conf_modules); |
281 |
} |
282 |
|
283 |
/* mod_clear_paths() |
284 |
* |
285 |
* input - NONE |
286 |
* output - NONE |
287 |
* side effects - clear the lists of paths and conf modules |
288 |
*/ |
289 |
void |
290 |
mod_clear_paths(void) |
291 |
{ |
292 |
struct module_path *pathst = NULL; |
293 |
dlink_node *ptr = NULL, *next_ptr = NULL; |
294 |
|
295 |
DLINK_FOREACH_SAFE(ptr, next_ptr, mod_paths.head) |
296 |
{ |
297 |
pathst = ptr->data; |
298 |
|
299 |
dlinkDelete(&pathst->node, &mod_paths); |
300 |
MyFree(pathst); |
301 |
} |
302 |
|
303 |
DLINK_FOREACH_SAFE(ptr, next_ptr, conf_modules.head) |
304 |
{ |
305 |
pathst = ptr->data; |
306 |
|
307 |
dlinkDelete(&pathst->node, &conf_modules); |
308 |
MyFree(pathst); |
309 |
} |
310 |
} |
311 |
|
312 |
/* findmodule_byname |
313 |
* |
314 |
* input - name of module |
315 |
* output - NULL if not found or pointer to module |
316 |
* side effects - NONE |
317 |
*/ |
318 |
struct module * |
319 |
findmodule_byname(const char *name) |
320 |
{ |
321 |
dlink_node *ptr = NULL; |
322 |
|
323 |
DLINK_FOREACH(ptr, modules_list.head) |
324 |
{ |
325 |
struct module *modp = ptr->data; |
326 |
|
327 |
if (strcmp(modp->name, name) == 0) |
328 |
return modp; |
329 |
} |
330 |
|
331 |
return NULL; |
332 |
} |
333 |
|
334 |
/* load_all_modules() |
335 |
* |
336 |
* input - int flag warn |
337 |
* output - NONE |
338 |
* side effects - load all modules found in autoload directory |
339 |
*/ |
340 |
void |
341 |
load_all_modules(int warn) |
342 |
{ |
343 |
DIR *system_module_dir = NULL; |
344 |
struct dirent *ldirent = NULL; |
345 |
char module_fq_name[PATH_MAX + 1]; |
346 |
|
347 |
modules_init(); |
348 |
|
349 |
if ((system_module_dir = opendir(AUTOMODPATH)) == NULL) |
350 |
{ |
351 |
ilog(L_WARN, "Could not load modules from %s: %s", |
352 |
AUTOMODPATH, strerror(errno)); |
353 |
return; |
354 |
} |
355 |
|
356 |
while ((ldirent = readdir(system_module_dir)) != NULL) |
357 |
{ |
358 |
if (modules_valid_suffix(ldirent->d_name)) |
359 |
{ |
360 |
snprintf(module_fq_name, sizeof(module_fq_name), "%s/%s", |
361 |
AUTOMODPATH, ldirent->d_name); |
362 |
load_a_module(module_fq_name, warn, 0); |
363 |
} |
364 |
} |
365 |
|
366 |
closedir(system_module_dir); |
367 |
} |
368 |
|
369 |
/* load_conf_modules() |
370 |
* |
371 |
* input - NONE |
372 |
* output - NONE |
373 |
* side effects - load modules given in ircd.conf |
374 |
*/ |
375 |
void |
376 |
load_conf_modules(void) |
377 |
{ |
378 |
dlink_node *ptr = NULL; |
379 |
struct module_path *mpath = NULL; |
380 |
|
381 |
DLINK_FOREACH(ptr, conf_modules.head) |
382 |
{ |
383 |
mpath = ptr->data; |
384 |
|
385 |
if (findmodule_byname(mpath->path) == NULL) |
386 |
load_one_module(mpath->path, 0); |
387 |
} |
388 |
} |
389 |
|
390 |
/* load_core_modules() |
391 |
* |
392 |
* input - int flag warn |
393 |
* output - NONE |
394 |
* side effects - core modules are loaded, if any fail, kill ircd |
395 |
*/ |
396 |
void |
397 |
load_core_modules(int warn) |
398 |
{ |
399 |
char module_name[PATH_MAX + 1]; |
400 |
int i = 0; |
401 |
|
402 |
for (; core_module_table[i]; ++i) |
403 |
{ |
404 |
snprintf(module_name, sizeof(module_name), "%s%s", |
405 |
MODPATH, core_module_table[i]); |
406 |
|
407 |
if (load_a_module(module_name, warn, 1) == -1) |
408 |
{ |
409 |
ilog(L_CRIT, "Error loading core module %s: terminating ircd", |
410 |
core_module_table[i]); |
411 |
exit(EXIT_FAILURE); |
412 |
} |
413 |
} |
414 |
} |
415 |
|
416 |
/* load_one_module() |
417 |
* |
418 |
* input - pointer to path |
419 |
* - flagged as core module or not |
420 |
* output - -1 if error |
421 |
* side effects - module is loaded if found. |
422 |
*/ |
423 |
int |
424 |
load_one_module(const char *path, int coremodule) |
425 |
{ |
426 |
dlink_node *ptr = NULL; |
427 |
char modpath[PATH_MAX + 1]; |
428 |
struct stat statbuf; |
429 |
|
430 |
DLINK_FOREACH(ptr, mod_paths.head) |
431 |
{ |
432 |
const struct module_path *mpath = ptr->data; |
433 |
|
434 |
snprintf(modpath, sizeof(modpath), "%s/%s", mpath->path, path); |
435 |
|
436 |
if (!modules_valid_suffix(path)) |
437 |
continue; |
438 |
|
439 |
if (strstr(modpath, "../") == NULL && |
440 |
strstr(modpath, "/..") == NULL) |
441 |
{ |
442 |
if (!stat(modpath, &statbuf)) |
443 |
{ |
444 |
if (S_ISREG(statbuf.st_mode)) |
445 |
{ |
446 |
/* Regular files only please */ |
447 |
return load_a_module(modpath, 1, coremodule); |
448 |
} |
449 |
} |
450 |
} |
451 |
} |
452 |
|
453 |
sendto_realops_flags(UMODE_ALL, L_ALL, |
454 |
"Cannot locate module %s", path); |
455 |
ilog(L_WARN, "Cannot locate module %s", path); |
456 |
return -1; |
457 |
} |
458 |
|
459 |
/* load a module .. */ |
460 |
static void |
461 |
mo_modload(struct Client *client_p, struct Client *source_p, |
462 |
int parc, char *parv[]) |
463 |
{ |
464 |
const char *m_bn = NULL; |
465 |
|
466 |
if (!HasOFlag(source_p, OPER_FLAG_MODULE)) |
467 |
{ |
468 |
sendto_one(source_p, form_str(ERR_NOPRIVILEGES), |
469 |
me.name, source_p->name); |
470 |
return; |
471 |
} |
472 |
|
473 |
m_bn = libio_basename(parv[1]); |
474 |
|
475 |
if (findmodule_byname(m_bn) != NULL) |
476 |
{ |
477 |
sendto_one(source_p, ":%s NOTICE %s :Module %s is already loaded", |
478 |
me.name, source_p->name, m_bn); |
479 |
return; |
480 |
} |
481 |
|
482 |
load_one_module(parv[1], 0); |
483 |
} |
484 |
|
485 |
/* unload a module .. */ |
486 |
static void |
487 |
mo_modunload(struct Client *client_p, struct Client *source_p, |
488 |
int parc, char *parv[]) |
489 |
{ |
490 |
const char *m_bn = NULL; |
491 |
struct module *modp = NULL; |
492 |
|
493 |
if (!HasOFlag(source_p, OPER_FLAG_MODULE)) |
494 |
{ |
495 |
sendto_one(source_p, form_str(ERR_NOPRIVILEGES), |
496 |
me.name, source_p->name); |
497 |
return; |
498 |
} |
499 |
|
500 |
m_bn = libio_basename(parv[1]); |
501 |
|
502 |
if ((modp = findmodule_byname(m_bn)) == NULL) |
503 |
{ |
504 |
sendto_one(source_p, ":%s NOTICE %s :Module %s is not loaded", |
505 |
me.name, source_p->name, m_bn); |
506 |
return; |
507 |
} |
508 |
|
509 |
if (modp->flags & MODULE_FLAG_CORE) |
510 |
{ |
511 |
sendto_one(source_p, |
512 |
":%s NOTICE %s :Module %s is a core module and may not be unloaded", |
513 |
me.name, source_p->name, m_bn); |
514 |
return; |
515 |
} |
516 |
|
517 |
if (unload_one_module(m_bn, 1) == -1) |
518 |
{ |
519 |
sendto_one(source_p, ":%s NOTICE %s :Module %s is not loaded", |
520 |
me.name, source_p->name, m_bn); |
521 |
} |
522 |
} |
523 |
|
524 |
/* unload and load in one! */ |
525 |
static void |
526 |
mo_modreload(struct Client *client_p, struct Client *source_p, |
527 |
int parc, char *parv[]) |
528 |
{ |
529 |
const char *m_bn = NULL; |
530 |
struct module *modp = NULL; |
531 |
int check_core; |
532 |
|
533 |
if (!HasOFlag(source_p, OPER_FLAG_MODULE)) |
534 |
{ |
535 |
sendto_one(source_p, form_str(ERR_NOPRIVILEGES), |
536 |
me.name, source_p->name); |
537 |
return; |
538 |
} |
539 |
|
540 |
m_bn = libio_basename(parv[1]); |
541 |
|
542 |
if ((modp = findmodule_byname(m_bn)) == NULL) |
543 |
{ |
544 |
sendto_one(source_p, ":%s NOTICE %s :Module %s is not loaded", |
545 |
me.name, source_p->name, m_bn); |
546 |
return; |
547 |
} |
548 |
|
549 |
check_core = (modp->flags & MODULE_FLAG_CORE) != 0; |
550 |
|
551 |
if (unload_one_module(m_bn, 1) == -1) |
552 |
{ |
553 |
sendto_one(source_p, ":%s NOTICE %s :Module %s is not loaded", |
554 |
me.name, source_p->name, m_bn); |
555 |
return; |
556 |
} |
557 |
|
558 |
if ((load_one_module(parv[1], check_core) == -1) && check_core) |
559 |
{ |
560 |
sendto_realops_flags(UMODE_ALL, L_ALL, "Error reloading core " |
561 |
"module: %s: terminating ircd", parv[1]); |
562 |
ilog(L_CRIT, "Error loading core module %s: terminating ircd", parv[1]); |
563 |
exit(0); |
564 |
} |
565 |
} |
566 |
|
567 |
/* list modules .. */ |
568 |
static void |
569 |
mo_modlist(struct Client *client_p, struct Client *source_p, |
570 |
int parc, char *parv[]) |
571 |
{ |
572 |
const dlink_node *ptr = NULL; |
573 |
|
574 |
if (!HasOFlag(source_p, OPER_FLAG_MODULE)) |
575 |
{ |
576 |
sendto_one(source_p, form_str(ERR_NOPRIVILEGES), |
577 |
me.name, source_p->name); |
578 |
return; |
579 |
} |
580 |
|
581 |
DLINK_FOREACH(ptr, modules_list.head) |
582 |
{ |
583 |
const struct module *modp = ptr->data; |
584 |
|
585 |
if (parc > 1 && !match(parv[1], modp->name)) |
586 |
continue; |
587 |
|
588 |
sendto_one(source_p, form_str(RPL_MODLIST), me.name, source_p->name, |
589 |
modp->name, modp->handle, |
590 |
modp->version, (modp->flags & MODULE_FLAG_CORE) ?"(core)":""); |
591 |
} |
592 |
|
593 |
sendto_one(source_p, form_str(RPL_ENDOFMODLIST), |
594 |
me.name, source_p->name); |
595 |
} |
596 |
|
597 |
/* unload and reload all modules */ |
598 |
static void |
599 |
mo_modrestart(struct Client *client_p, struct Client *source_p, |
600 |
int parc, char *parv[]) |
601 |
{ |
602 |
unsigned int modnum = 0; |
603 |
dlink_node *ptr = NULL, *ptr_next = NULL; |
604 |
|
605 |
if (!HasOFlag(source_p, OPER_FLAG_MODULE)) |
606 |
{ |
607 |
sendto_one(source_p, form_str(ERR_NOPRIVILEGES), |
608 |
me.name, source_p->name); |
609 |
return; |
610 |
} |
611 |
|
612 |
sendto_one(source_p, ":%s NOTICE %s :Reloading all modules", |
613 |
me.name, source_p->name); |
614 |
|
615 |
modnum = dlink_list_length(&modules_list); |
616 |
|
617 |
DLINK_FOREACH_SAFE(ptr, ptr_next, modules_list.head) |
618 |
{ |
619 |
struct module *modp = ptr->data; |
620 |
unload_one_module(modp->name, 0); |
621 |
} |
622 |
|
623 |
load_all_modules(0); |
624 |
load_conf_modules(); |
625 |
load_core_modules(0); |
626 |
|
627 |
sendto_realops_flags(UMODE_ALL, L_ALL, |
628 |
"Module Restart: %u modules unloaded, %u modules loaded", |
629 |
modnum, dlink_list_length(&modules_list)); |
630 |
ilog(L_WARN, "Module Restart: %u modules unloaded, %u modules loaded", |
631 |
modnum, dlink_list_length(&modules_list)); |
632 |
} |