ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/modules.c
Revision: 978
Committed: Sun Aug 9 09:47:58 2009 UTC (16 years ago) by michael
Content type: text/x-csrc
Original Path: ircd-hybrid-7.2/src/modules.c
File size: 12218 byte(s)
Log Message:
- avoid using native basename() since some implementations may or may not modify passed data

File Contents

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

Properties

Name Value
svn:eol-style native
svn:keywords Id Revision