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