ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/releases/8.2.21/src/modules.c
Revision: 1437
Committed: Mon Jun 18 19:02:48 2012 UTC (14 years, 4 months ago) by michael
Content type: text/x-csrc
Original Path: ircd-hybrid-8/src/modules.c
File size: 13509 byte(s)
Log Message:
- Removed MODLOAD, MODRELOAD, MODUNLOAD and MODLIST command handlers.
  They all have been replaced by the new "MODULE" command which now
  deals with the LOAD/UNLOAD/RELOAD/LIST parameters.

File Contents

# User Rev Content
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 1238 static 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 1437 static void mo_module(struct Client *, struct Client *, int, char *[]);
68 adx 30
69 michael 1437 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 adx 30 };
73    
74 michael 1237 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 michael 1238 assert(dlink_list_length(&modules_list) > 0);
99     dlinkDelete(&modp->node, &modules_list);
100 michael 1237 MyFree(modp->name);
101    
102     lt_dlclose(modp->handle);
103    
104     if (warn == 1)
105     {
106 michael 1247 ilog(LOG_TYPE_IRCD, "Module %s unloaded", name);
107 michael 1237 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 michael 1404 load_a_module(const char *path, int warn)
121 michael 1237 {
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 michael 1247 ilog(LOG_TYPE_IRCD, "Error loading module %s: %s", mod_basename, err);
135 michael 1237 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 michael 1247 ilog(LOG_TYPE_IRCD, "Module %s has no module_entry export", mod_basename);
143 michael 1237 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 michael 1238 dlinkAdd(modp, &modp->node, &modules_list);
154 michael 1237
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 michael 1247 ilog(LOG_TYPE_IRCD, "Module %s [version: %s handle: %p] loaded.",
164 michael 1237 modp->name, modp->version, tmpptr);
165     }
166    
167     return 0;
168     }
169    
170 adx 30 /*
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 michael 1237 if (lt_dlinit())
181     {
182 michael 1247 ilog(LOG_TYPE_IRCD, "Couldn't initialize the libltdl run time dynamic"
183 michael 1237 " link library. Exiting.");
184     exit(0);
185     }
186 michael 912
187 michael 1437 mod_add_cmd(&module_msgtab);
188 adx 30 }
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 michael 1404 struct module_path *mpath = ptr->data;
204 adx 30
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 michael 1237 dlink_node *ptr = NULL, *next_ptr = NULL;
259 adx 30
260     DLINK_FOREACH_SAFE(ptr, next_ptr, mod_paths.head)
261     {
262 michael 1404 dlinkDelete(ptr, &mod_paths);
263     MyFree(ptr->data);
264 adx 30 }
265    
266     DLINK_FOREACH_SAFE(ptr, next_ptr, conf_modules.head)
267     {
268 michael 1404 dlinkDelete(ptr, &conf_modules);
269     MyFree(ptr->data);
270 adx 30 }
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 michael 1230 struct module *
280 adx 30 findmodule_byname(const char *name)
281     {
282 michael 1230 dlink_node *ptr = NULL;
283 adx 30
284 michael 1238 DLINK_FOREACH(ptr, modules_list.head)
285 adx 30 {
286 michael 1230 struct module *modp = ptr->data;
287 adx 30
288 db 218 if (strcmp(modp->name, name) == 0)
289 michael 1230 return modp;
290 adx 30 }
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 michael 1247 ilog(LOG_TYPE_IRCD, "Could not load modules from %s: %s",
311 adx 30 AUTOMODPATH, strerror(errno));
312     return;
313     }
314    
315     while ((ldirent = readdir(system_module_dir)) != NULL)
316     {
317 michael 912 if (modules_valid_suffix(ldirent->d_name))
318 adx 30 {
319     snprintf(module_fq_name, sizeof(module_fq_name), "%s/%s",
320     AUTOMODPATH, ldirent->d_name);
321 michael 1404 load_a_module(module_fq_name, warn);
322 adx 30 }
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 michael 1404 struct module_path *mpath = ptr->data;
342 adx 30
343     if (findmodule_byname(mpath->path) == NULL)
344 michael 1404 load_one_module(mpath->path);
345 adx 30 }
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 michael 912 snprintf(module_name, sizeof(module_name), "%s%s",
363     MODPATH, core_module_table[i]);
364 adx 30
365 michael 1404 if (load_a_module(module_name, warn) == -1)
366 adx 30 {
367 michael 1247 ilog(LOG_TYPE_IRCD, "Error loading core module %s: terminating ircd",
368 michael 912 core_module_table[i]);
369 adx 30 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 michael 1404 load_one_module(const char *path)
383 adx 30 {
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 michael 912 if (!modules_valid_suffix(path))
395     continue;
396    
397 adx 30 if (strstr(modpath, "../") == NULL &&
398     strstr(modpath, "/..") == NULL)
399     if (!stat(modpath, &statbuf))
400 michael 1404 if (S_ISREG(statbuf.st_mode)) /* Regular files only please */
401     return load_a_module(modpath, 1);
402 adx 30 }
403    
404     sendto_realops_flags(UMODE_ALL, L_ALL,
405     "Cannot locate module %s", path);
406 michael 1247 ilog(LOG_TYPE_IRCD, "Cannot locate module %s", path);
407 adx 30 return -1;
408     }
409    
410 michael 1437 /*! \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 adx 30 static void
425 michael 1437 mo_module(struct Client *client_p, struct Client *source_p,
426     int parc, char *parv[])
427 adx 30 {
428 michael 978 const char *m_bn = NULL;
429 michael 1437 struct module *modp = NULL;
430     int check_core;
431 adx 30
432 michael 1228 if (!HasOFlag(source_p, OPER_FLAG_MODULE))
433 adx 30 {
434     sendto_one(source_p, form_str(ERR_NOPRIVILEGES),
435     me.name, source_p->name);
436     return;
437     }
438    
439 michael 1437 if (EmptyString(parv[1]))
440 adx 30 {
441 michael 1437 sendto_one(source_p, form_str(ERR_NEEDMOREPARAMS),
442     me.name, source_p->name, "MODULE");
443 adx 30 return;
444     }
445    
446 michael 1437 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 adx 30
455 michael 1437 load_one_module(parv[2]);
456 adx 30 return;
457     }
458    
459 michael 1437 if (!irccmp(parv[1], "UNLOAD"))
460 adx 30 {
461 michael 1437 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 adx 30
468 michael 1437 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 adx 30 return;
480     }
481    
482 michael 1437 if (!irccmp(parv[1], "RELOAD"))
483 adx 30 {
484 michael 1437 if (!strcmp(parv[2], "*"))
485     {
486     unsigned int modnum = 0;
487     dlink_node *ptr = NULL, *ptr_next = NULL;
488 adx 30
489 michael 1437 sendto_one(source_p, ":%s NOTICE %s :Reloading all modules",
490     me.name, source_p->name);
491 adx 30
492 michael 1437 modnum = dlink_list_length(&modules_list);
493 adx 30
494 michael 1437 DLINK_FOREACH_SAFE(ptr, ptr_next, modules_list.head)
495     {
496     modp = ptr->data;
497     unload_one_module(modp->name, 0);
498     }
499 michael 1436
500 michael 1437 load_all_modules(0);
501     load_conf_modules();
502     load_core_modules(0);
503 michael 1436
504 michael 1437 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 michael 1436
512 michael 1437 if ((modp = findmodule_byname((m_bn = libio_basename(parv[2])))) == NULL)
513 michael 1436 {
514 michael 1437 sendto_one(source_p, ":%s NOTICE %s :Module %s is not loaded",
515     me.name, source_p->name, m_bn);
516     return;
517 michael 1436 }
518    
519 michael 1437 check_core = (modp->flags & MODULE_FLAG_CORE) != 0;
520 michael 1436
521 michael 1437 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 michael 1436
528 michael 1437 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 adx 30
536     return;
537     }
538    
539 michael 1437 if (!irccmp(parv[1], "LIST"))
540 adx 30 {
541 michael 1437 const dlink_node *ptr = NULL;
542 adx 30
543 michael 1437 DLINK_FOREACH(ptr, modules_list.head)
544     {
545     if (parc > 2 && !match(parv[2], modp->name))
546     continue;
547 adx 30
548 michael 1437 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 adx 30
553 michael 1437 sendto_one(source_p, form_str(RPL_ENDOFMODLIST),
554 adx 30 me.name, source_p->name);
555     return;
556     }
557     }

Properties

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