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