ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/modules.c
Revision: 8752
Committed: Tue Jan 1 11:07:01 2019 UTC (6 years, 7 months ago) by michael
Content type: text/x-csrc
File size: 9224 byte(s)
Log Message:
- Update copyright years

File Contents

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

Properties

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