Add ENUM configurable search path
[asterisk/asterisk.git] / loader.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * Module Loader
5  * 
6  * Copyright (C) 1999, Mark Spencer
7  *
8  * Mark Spencer <markster@linux-support.net>
9  *
10  * This program is free software, distributed under the terms of
11  * the GNU General Public License
12  */
13
14 #include <stdio.h>
15 #include <dirent.h>
16 #include <unistd.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <asterisk/module.h>
20 #include <asterisk/options.h>
21 #include <asterisk/config.h>
22 #include <asterisk/logger.h>
23 #include <asterisk/channel.h>
24 #include <asterisk/term.h>
25 #include <asterisk/manager.h>
26 #include <asterisk/enum.h>
27 #include <dlfcn.h>
28 #include <asterisk/md5.h>
29 #include <pthread.h>
30 #include "asterisk.h"
31 #include "astconf.h"
32
33 #ifndef RTLD_NOW
34 #define RTLD_NOW 0
35 #endif
36
37 static char expected_key[] =
38 { 0x8e, 0x93, 0x22, 0x83, 0xf5, 0xc3, 0xc0, 0x75,
39   0xff, 0x8b, 0xa9, 0xbe, 0x7c, 0x43, 0x74, 0x63 };
40
41 struct module {
42         int (*load_module)(void);
43         int (*unload_module)(void);
44         int (*usecount)(void);
45         char *(*description)(void);
46         char *(*key)(void);
47         int (*reload)(void);
48         void *lib;
49         char resource[256];
50         struct module *next;
51 };
52
53 static int printdigest(unsigned char *d)
54 {
55         int x;
56         char buf[256];
57         char buf2[16];
58         snprintf(buf, sizeof(buf), "Unexpected signature:");
59         for (x=0;x<16;x++) {
60                 snprintf(buf2, sizeof(buf2), " %02x", *(d++));
61                 strcat(buf, buf2);
62         }
63         strcat(buf, "\n");
64         ast_log(LOG_DEBUG, buf);
65         return 0;
66 }
67
68 static int key_matches(char *key1, char *key2)
69 {
70         int match = 1;
71         int x;
72         for (x=0;x<16;x++) {
73                 match &= (key1[x] == key2[x]);
74         }
75         return match;
76 }
77
78 static int verify_key(char *key)
79 {
80         struct MD5Context c;
81         char digest[16];
82         MD5Init(&c);
83         MD5Update(&c, key, strlen(key));
84         MD5Final(digest, &c);
85         if (key_matches(expected_key, digest))
86                 return 0;
87         printdigest(digest);
88         return -1;
89 }
90
91 static struct loadupdate {
92         int (*updater)(void);
93         struct loadupdate *next;
94 } *updaters = NULL;
95
96 static pthread_mutex_t modlock = AST_MUTEX_INITIALIZER;
97
98 static struct module *module_list=NULL;
99
100 int ast_unload_resource(char *resource_name, int force)
101 {
102         struct module *m, *ml = NULL;
103         int res = -1;
104         if (ast_pthread_mutex_lock(&modlock))
105                 ast_log(LOG_WARNING, "Failed to lock\n");
106         m = module_list;
107         while(m) {
108                 if (!strcasecmp(m->resource, resource_name)) {
109                         if ((res = m->usecount()) > 0)  {
110                                 if (force) 
111                                         ast_log(LOG_WARNING, "Warning:  Forcing removal of module %s with use count %d\n", resource_name, res);
112                                 else {
113                                         ast_log(LOG_WARNING, "Soft unload failed, '%s' has use count %d\n", resource_name, res);
114                                         ast_pthread_mutex_unlock(&modlock);
115                                         return -1;
116                                 }
117                         }
118                         res = m->unload_module();
119                         if (res) {
120                                 ast_log(LOG_WARNING, "Firm unload failed for %s\n", resource_name);
121                                 if (force <= AST_FORCE_FIRM) {
122                                         ast_pthread_mutex_unlock(&modlock);
123                                         return -1;
124                                 } else
125                                         ast_log(LOG_WARNING, "** Dangerous **: Unloading resource anyway, at user request\n");
126                         }
127                         if (ml)
128                                 ml->next = m->next;
129                         else
130                                 module_list = m->next;
131                         dlclose(m->lib);
132                         free(m);
133                 }
134                 ml = m;
135                 m = m->next;
136         }
137         ast_pthread_mutex_unlock(&modlock);
138         ast_update_use_count();
139         return res;
140 }
141
142 void ast_module_reload(void)
143 {
144         struct module *m;
145
146         /* We'll do the logger and manager the favor of calling its reload here first */
147         reload_manager();
148         ast_enum_reload();
149
150         ast_pthread_mutex_lock(&modlock);
151         m = module_list;
152         while(m) {
153                 if (m->reload) {
154                         if (option_verbose > 2) 
155                                 ast_verbose(VERBOSE_PREFIX_3 "Reloading module '%s' (%s)\n", m->resource, m->description());
156                         m->reload();
157                 }
158                 m = m->next;
159         }
160         ast_pthread_mutex_unlock(&modlock);
161 }
162
163 int ast_load_resource(char *resource_name)
164 {
165         static char fn[256];
166         int errors=0;
167         int res;
168         struct module *m;
169         int flags=RTLD_NOW;
170         char *val;
171         char *key;
172         int o;
173         struct ast_config *cfg;
174         char tmp[80];
175         /* Keep the module file parsing silent */
176         o = option_verbose;
177         if (strncasecmp(resource_name, "res_", 4)) {
178                 option_verbose = 0;
179                 cfg = ast_load(AST_MODULE_CONFIG);
180                 option_verbose = o;
181                 if (cfg) {
182 #ifdef RTLD_GLOBAL
183                         if ((val = ast_variable_retrieve(cfg, "global", resource_name))
184                                         && ast_true(val))
185                                 flags |= RTLD_GLOBAL;
186 #endif
187                         ast_destroy(cfg);
188                 }
189         } else {
190                 /* Resource modules are always loaded global and lazy */
191 #ifdef RTLD_GLOBAL
192                 flags = (RTLD_GLOBAL | RTLD_LAZY);
193 #else
194                 flags = RTLD_LAZY;
195 #endif
196         }
197         
198         if (ast_pthread_mutex_lock(&modlock))
199                 ast_log(LOG_WARNING, "Failed to lock\n");
200         m = module_list;
201         while(m) {
202                 if (!strcasecmp(m->resource, resource_name)) {
203                         ast_log(LOG_WARNING, "Module '%s' already exists\n", resource_name);
204                         ast_pthread_mutex_unlock(&modlock);
205                         return -1;
206                 }
207                 m = m->next;
208         }
209         m = malloc(sizeof(struct module));      
210         if (!m) {
211                 ast_log(LOG_WARNING, "Out of memory\n");
212                 ast_pthread_mutex_unlock(&modlock);
213                 return -1;
214         }
215         strncpy(m->resource, resource_name, sizeof(m->resource)-1);
216         if (resource_name[0] == '/') {
217                 strncpy(fn, resource_name, sizeof(fn)-1);
218         } else {
219                 snprintf(fn, sizeof(fn), "%s/%s", (char *)ast_config_AST_MODULE_DIR, resource_name);
220         }
221         m->lib = dlopen(fn, flags);
222         if (!m->lib) {
223                 ast_log(LOG_WARNING, "%s\n", dlerror());
224                 free(m);
225                 ast_pthread_mutex_unlock(&modlock);
226                 return -1;
227         }
228         m->load_module = dlsym(m->lib, "load_module");
229         if (m->load_module == NULL)
230                 m->load_module = dlsym(m->lib, "_load_module");
231         if (!m->load_module) {
232                 ast_log(LOG_WARNING, "No load_module in module %s\n", fn);
233                 errors++;
234         }
235         m->unload_module = dlsym(m->lib, "unload_module");
236         if (m->unload_module == NULL)
237                 m->unload_module = dlsym(m->lib, "_unload_module");
238         if (!m->unload_module) {
239                 ast_log(LOG_WARNING, "No unload_module in module %s\n", fn);
240                 errors++;
241         }
242         m->usecount = dlsym(m->lib, "usecount");
243         if (m->usecount == NULL)
244                 m->usecount = dlsym(m->lib, "_usecount");
245         if (!m->usecount) {
246                 ast_log(LOG_WARNING, "No usecount in module %s\n", fn);
247                 errors++;
248         }
249         m->description = dlsym(m->lib, "description");
250         if (m->description == NULL)
251                 m->description = dlsym(m->lib, "_description");
252         if (!m->description) {
253                 ast_log(LOG_WARNING, "No description in module %s\n", fn);
254                 errors++;
255         }
256         m->key = dlsym(m->lib, "key");
257         if (m->key == NULL)
258                 m->key = dlsym(m->lib, "_key");
259         if (!m->key) {
260                 ast_log(LOG_WARNING, "No key routine in module %s\n", fn);
261                 errors++;
262         }
263         m->reload = dlsym(m->lib, "reload");
264         if (m->reload == NULL)
265                 m->reload = dlsym(m->lib, "_reload");
266         if (m->key && !(key = m->key())) {
267                 ast_log(LOG_WARNING, "Key routine returned NULL in module %s\n", fn);
268                 errors++;
269         } else
270                 key = NULL;
271         if (key && verify_key(key)) {
272                 ast_log(LOG_WARNING, "Unexpected key returned by module %s\n", fn);
273                 errors++;
274         }
275         if (errors) {
276                 ast_log(LOG_WARNING, "%d error(s) loading module %s, aborted\n", errors, fn);
277                 dlclose(m->lib);
278                 free(m);
279                 ast_pthread_mutex_unlock(&modlock);
280                 return -1;
281         }
282         if (!fully_booted) {
283                 if (option_verbose) 
284                         ast_verbose( " => (%s)\n", term_color(tmp, m->description(), COLOR_BROWN, COLOR_BLACK, sizeof(tmp)));
285                 if (option_console && !option_verbose)
286                         ast_verbose( ".");
287         } else {
288                 if (option_verbose)
289                         ast_verbose(VERBOSE_PREFIX_1 "Loaded %s => (%s)\n", fn, m->description());
290         }
291         m->next = module_list;
292         
293         module_list = m;
294         ast_pthread_mutex_unlock(&modlock);
295         if ((res = m->load_module())) {
296                 ast_log(LOG_WARNING, "%s: load_module failed, returning %d\n", m->resource, res);
297                 ast_unload_resource(resource_name, 0);
298                 return -1;
299         }
300         ast_update_use_count();
301         return 0;
302 }       
303
304 static int ast_resource_exists(char *resource)
305 {
306         struct module *m;
307         if (ast_pthread_mutex_lock(&modlock))
308                 ast_log(LOG_WARNING, "Failed to lock\n");
309         m = module_list;
310         while(m) {
311                 if (!strcasecmp(resource, m->resource))
312                         break;
313                 m = m->next;
314         }
315         ast_pthread_mutex_unlock(&modlock);
316         if (m)
317                 return -1;
318         else
319                 return 0;
320 }
321
322 int load_modules()
323 {
324         struct ast_config *cfg;
325         struct ast_variable *v;
326         char tmp[80];
327         if (option_verbose) 
328                 ast_verbose( "Asterisk Dynamic Loader Starting:\n");
329         cfg = ast_load(AST_MODULE_CONFIG);
330         if (cfg) {
331                 /* Load explicitly defined modules */
332                 v = ast_variable_browse(cfg, "modules");
333                 while(v) {
334                         if (!strcasecmp(v->name, "load")) {
335                                 if (option_debug && !option_verbose)
336                                         ast_log(LOG_DEBUG, "Loading module %s\n", v->value);
337                                 if (option_verbose) {
338                                         ast_verbose( VERBOSE_PREFIX_1 "[%s]", term_color(tmp, v->value, COLOR_BRWHITE, 0, sizeof(tmp)));
339                                         fflush(stdout);
340                                 }
341                                 if (ast_load_resource(v->value)) {
342                                         ast_log(LOG_WARNING, "Loading module %s failed!\n", v->value);
343                                         if (cfg)
344                                                 ast_destroy(cfg);
345                                         return -1;
346                                 }
347                         }
348                         v=v->next;
349                 }
350         }
351         if (!cfg || ast_true(ast_variable_retrieve(cfg, "modules", "autoload"))) {
352                 /* Load all modules */
353                 DIR *mods;
354                 struct dirent *d;
355                 int x;
356                 /* Make two passes.  First, load any resource modules, then load the others. */
357                 for (x=0;x<2;x++) {
358                         mods = opendir((char *)ast_config_AST_MODULE_DIR);
359                         if (mods) {
360                                 while((d = readdir(mods))) {
361                                         /* Must end in .so to load it.  */
362                                         if ((strlen(d->d_name) > 3) && (x || !strncasecmp(d->d_name, "res_", 4)) && 
363                                             !strcasecmp(d->d_name + strlen(d->d_name) - 3, ".so") &&
364                                                 !ast_resource_exists(d->d_name)) {
365                                                 /* It's a shared library -- Just be sure we're allowed to load it -- kinda
366                                                    an inefficient way to do it, but oh well. */
367                                                 if (cfg) {
368                                                         v = ast_variable_browse(cfg, "modules");
369                                                         while(v) {
370                                                                 if (!strcasecmp(v->name, "noload") &&
371                                                                     !strcasecmp(v->value, d->d_name)) 
372                                                                         break;
373                                                                 v = v->next;
374                                                         }
375                                                         if (v) {
376                                                                 if (option_verbose) {
377                                                                         ast_verbose( VERBOSE_PREFIX_1 "[skipping %s]\n", d->d_name);
378                                                                         fflush(stdout);
379                                                                 }
380                                                                 continue;
381                                                         }
382                                                         
383                                                 }
384                                             if (option_debug && !option_verbose)
385                                                         ast_log(LOG_DEBUG, "Loading module %s\n", d->d_name);
386                                                 if (option_verbose) {
387                                                         ast_verbose( VERBOSE_PREFIX_1 "[%s]", term_color(tmp, d->d_name, COLOR_BRWHITE, 0, sizeof(tmp)));
388                                                         fflush(stdout);
389                                                 }
390                                                 if (ast_load_resource(d->d_name)) {
391                                                         ast_log(LOG_WARNING, "Loading module %s failed!\n", d->d_name);
392                                                         if (cfg)
393                                                                 ast_destroy(cfg);
394                                                         return -1;
395                                                 }
396                                         }
397                                 }
398                                 closedir(mods);
399                         } else {
400                                 if (!option_quiet)
401                                         ast_log(LOG_WARNING, "Unable to open modules directory %s.\n", (char *)ast_config_AST_MODULE_DIR);
402                         }
403                 }
404         } 
405         ast_destroy(cfg);
406         return 0;
407 }
408
409 void ast_update_use_count(void)
410 {
411         /* Notify any module monitors that the use count for a 
412            resource has changed */
413         struct loadupdate *m;
414         if (ast_pthread_mutex_lock(&modlock))
415                 ast_log(LOG_WARNING, "Failed to lock\n");
416         m = updaters;
417         while(m) {
418                 m->updater();
419                 m = m->next;
420         }
421         ast_pthread_mutex_unlock(&modlock);
422         
423 }
424
425 int ast_update_module_list(int (*modentry)(char *module, char *description, int usecnt))
426 {
427         struct module *m;
428         int unlock = -1;
429         if (pthread_mutex_trylock(&modlock))
430                 unlock = 0;
431         m = module_list;
432         while(m) {
433                 modentry(m->resource, m->description(), m->usecount());
434                 m = m->next;
435         }
436         if (unlock)
437                 ast_pthread_mutex_unlock(&modlock);
438         return 0;
439 }
440
441 int ast_loader_register(int (*v)(void)) 
442 {
443         struct loadupdate *tmp;
444         /* XXX Should be more flexible here, taking > 1 verboser XXX */
445         if ((tmp = malloc(sizeof (struct loadupdate)))) {
446                 tmp->updater = v;
447                 if (ast_pthread_mutex_lock(&modlock))
448                         ast_log(LOG_WARNING, "Failed to lock\n");
449                 tmp->next = updaters;
450                 updaters = tmp;
451                 ast_pthread_mutex_unlock(&modlock);
452                 return 0;
453         }
454         return -1;
455 }
456
457 int ast_loader_unregister(int (*v)(void))
458 {
459         int res = -1;
460         struct loadupdate *tmp, *tmpl=NULL;
461         if (ast_pthread_mutex_lock(&modlock))
462                 ast_log(LOG_WARNING, "Failed to lock\n");
463         tmp = updaters;
464         while(tmp) {
465                 if (tmp->updater == v)  {
466                         if (tmpl)
467                                 tmpl->next = tmp->next;
468                         else
469                                 updaters = tmp->next;
470                         break;
471                 }
472                 tmpl = tmp;
473                 tmp = tmp->next;
474         }
475         if (tmp)
476                 res = 0;
477         ast_pthread_mutex_unlock(&modlock);
478         return res;
479 }