Version 0.1.11 from FTP
[asterisk/asterisk.git] / loader.c
index 63c44df..960eda4 100755 (executable)
--- a/loader.c
+++ b/loader.c
 #include <asterisk/options.h>
 #include <asterisk/config.h>
 #include <asterisk/logger.h>
+#include <asterisk/channel.h>
 #include <dlfcn.h>
+#include <asterisk/md5.h>
 #define __USE_GNU
 #include <pthread.h>
 #include "asterisk.h"
 
+static char expected_key[] =
+{ 0x8e, 0x93, 0x22, 0x83, 0xf5, 0xc3, 0xc0, 0x75,
+  0xff, 0x8b, 0xa9, 0xbe, 0x7c, 0x43, 0x74, 0x63 };
+
 struct module {
        int (*load_module)(void);
        int (*unload_module)(void);
        int (*usecount)(void);
        char *(*description)(void);
+       char *(*key)(void);
+       int (*reload)(void);
        void *lib;
        char resource[256];
        struct module *next;
 };
 
+static int printdigest(unsigned char *d)
+{
+       int x;
+       char buf[256];
+       char buf2[16];
+       snprintf(buf, sizeof(buf), "Unexpected signature:");
+       for (x=0;x<16;x++) {
+               snprintf(buf2, sizeof(buf2), " %02x", *(d++));
+               strcat(buf, buf2);
+       }
+       strcat(buf, "\n");
+       ast_log(LOG_DEBUG, buf);
+       return 0;
+}
+
+static int key_matches(char *key1, char *key2)
+{
+       int match = 1;
+       int x;
+       for (x=0;x<16;x++) {
+               match &= (key1[x] == key2[x]);
+       }
+       return match;
+}
+
+static int verify_key(char *key)
+{
+       struct MD5Context c;
+       char digest[16];
+       MD5Init(&c);
+       MD5Update(&c, key, strlen(key));
+       MD5Final(digest, &c);
+       if (key_matches(expected_key, digest))
+               return 0;
+       printdigest(digest);
+       return -1;
+}
+
 static struct loadupdate {
        int (*updater)(void);
        struct loadupdate *next;
@@ -48,7 +94,7 @@ int ast_unload_resource(char *resource_name, int force)
 {
        struct module *m, *ml = NULL;
        int res = -1;
-       if (pthread_mutex_lock(&modlock))
+       if (ast_pthread_mutex_lock(&modlock))
                ast_log(LOG_WARNING, "Failed to lock\n");
        m = module_list;
        while(m) {
@@ -58,7 +104,7 @@ int ast_unload_resource(char *resource_name, int force)
                                        ast_log(LOG_WARNING, "Warning:  Forcing removal of module %s with use count %d\n", resource_name, res);
                                else {
                                        ast_log(LOG_WARNING, "Soft unload failed, '%s' has use count %d\n", resource_name, res);
-                                       pthread_mutex_unlock(&modlock);
+                                       ast_pthread_mutex_unlock(&modlock);
                                        return -1;
                                }
                        }
@@ -66,7 +112,7 @@ int ast_unload_resource(char *resource_name, int force)
                        if (res) {
                                ast_log(LOG_WARNING, "Firm unload failed for %s\n", resource_name);
                                if (force <= AST_FORCE_FIRM) {
-                                       pthread_mutex_unlock(&modlock);
+                                       ast_pthread_mutex_unlock(&modlock);
                                        return -1;
                                } else
                                        ast_log(LOG_WARNING, "** Dangerous **: Unloading resource anyway, at user request\n");
@@ -81,11 +127,31 @@ int ast_unload_resource(char *resource_name, int force)
                ml = m;
                m = m->next;
        }
-       pthread_mutex_unlock(&modlock);
+       ast_pthread_mutex_unlock(&modlock);
        ast_update_use_count();
        return res;
 }
 
+void ast_module_reload(void)
+{
+       struct module *m;
+
+       /* We'll do the logger the favor of calling its reload here first */
+       
+
+       ast_pthread_mutex_lock(&modlock);
+       m = module_list;
+       while(m) {
+               if (m->reload) {
+                       if (option_verbose > 2) 
+                               ast_verbose(VERBOSE_PREFIX_3 "Reloading module '%s' (%s)\n", m->resource, m->description());
+                       m->reload();
+               }
+               m = m->next;
+       }
+       ast_pthread_mutex_unlock(&modlock);
+}
+
 int ast_load_resource(char *resource_name)
 {
        static char fn[256];
@@ -94,27 +160,33 @@ int ast_load_resource(char *resource_name)
        struct module *m;
        int flags=0;
        char *val;
+       char *key;
        int o;
        struct ast_config *cfg;
        /* Keep the module file parsing silent */
        o = option_verbose;
-       option_verbose = 0;
-       cfg = ast_load(AST_MODULE_CONFIG);
-       option_verbose = o;
-       if (cfg) {
-               if ((val = ast_variable_retrieve(cfg, "global", resource_name))
-                               && ast_true(val))
-                       flags |= RTLD_GLOBAL;
-               ast_destroy(cfg);
+       if (strncasecmp(resource_name, "res_", 4)) {
+               option_verbose = 0;
+               cfg = ast_load(AST_MODULE_CONFIG);
+               option_verbose = o;
+               if (cfg) {
+                       if ((val = ast_variable_retrieve(cfg, "global", resource_name))
+                                       && ast_true(val))
+                               flags |= RTLD_GLOBAL;
+                       ast_destroy(cfg);
+               }
+       } else {
+               /* Resource modules are always loaded global */
+               flags |= RTLD_GLOBAL;
        }
        
-       if (pthread_mutex_lock(&modlock))
+       if (ast_pthread_mutex_lock(&modlock))
                ast_log(LOG_WARNING, "Failed to lock\n");
        m = module_list;
        while(m) {
                if (!strcasecmp(m->resource, resource_name)) {
                        ast_log(LOG_WARNING, "Module '%s' already exists\n", resource_name);
-                       pthread_mutex_unlock(&modlock);
+                       ast_pthread_mutex_unlock(&modlock);
                        return -1;
                }
                m = m->next;
@@ -122,12 +194,12 @@ int ast_load_resource(char *resource_name)
        m = malloc(sizeof(struct module));      
        if (!m) {
                ast_log(LOG_WARNING, "Out of memory\n");
-               pthread_mutex_unlock(&modlock);
+               ast_pthread_mutex_unlock(&modlock);
                return -1;
        }
-       strncpy(m->resource, resource_name, sizeof(m->resource));
+       strncpy(m->resource, resource_name, sizeof(m->resource)-1);
        if (resource_name[0] == '/') {
-               strncpy(fn, resource_name, sizeof(fn));
+               strncpy(fn, resource_name, sizeof(fn)-1);
        } else {
                snprintf(fn, sizeof(fn), "%s/%s", AST_MODULE_DIR, resource_name);
        }
@@ -135,7 +207,7 @@ int ast_load_resource(char *resource_name)
        if (!m->lib) {
                ast_log(LOG_WARNING, "%s\n", dlerror());
                free(m);
-               pthread_mutex_unlock(&modlock);
+               ast_pthread_mutex_unlock(&modlock);
                return -1;
        }
        m->load_module = dlsym(m->lib, "load_module");
@@ -158,11 +230,26 @@ int ast_load_resource(char *resource_name)
                ast_log(LOG_WARNING, "No description in module %s\n", fn);
                errors++;
        }
+       m->key = dlsym(m->lib, "key");
+       if (!m->key) {
+               ast_log(LOG_WARNING, "No key routine in module %s\n", fn);
+               errors++;
+       }
+       m->reload = dlsym(m->lib, "reload");
+       if (m->key && !(key = m->key())) {
+               ast_log(LOG_WARNING, "Key routine returned NULL in module %s\n", fn);
+               errors++;
+       } else
+               key = NULL;
+       if (key && verify_key(key)) {
+               ast_log(LOG_WARNING, "Unexpected key returned by module %s\n", fn);
+               errors++;
+       }
        if (errors) {
                ast_log(LOG_WARNING, "%d error(s) loading module %s, aborted\n", errors, fn);
                dlclose(m->lib);
                free(m);
-               pthread_mutex_unlock(&modlock);
+               ast_pthread_mutex_unlock(&modlock);
                return -1;
        }
        if (!fully_booted) {
@@ -175,8 +262,9 @@ int ast_load_resource(char *resource_name)
                        ast_verbose(VERBOSE_PREFIX_1 "Loaded %s => (%s)\n", fn, m->description());
        }
        m->next = module_list;
+       
        module_list = m;
-       pthread_mutex_unlock(&modlock);
+       ast_pthread_mutex_unlock(&modlock);
        if ((res = m->load_module())) {
                ast_log(LOG_WARNING, "%s: load_module failed, returning %d\n", m->resource, res);
                ast_unload_resource(resource_name, 0);
@@ -189,7 +277,7 @@ int ast_load_resource(char *resource_name)
 static int ast_resource_exists(char *resource)
 {
        struct module *m;
-       if (pthread_mutex_lock(&modlock))
+       if (ast_pthread_mutex_lock(&modlock))
                ast_log(LOG_WARNING, "Failed to lock\n");
        m = module_list;
        while(m) {
@@ -197,7 +285,7 @@ static int ast_resource_exists(char *resource)
                        break;
                m = m->next;
        }
-       pthread_mutex_unlock(&modlock);
+       ast_pthread_mutex_unlock(&modlock);
        if (m)
                return -1;
        else
@@ -236,49 +324,54 @@ int load_modules()
                /* Load all modules */
                DIR *mods;
                struct dirent *d;
-               mods = opendir(AST_MODULE_DIR);
-               if (mods) {
-                       while((d = readdir(mods))) {
-                               /* Must end in .so to load it.  */
-                               if ((strlen(d->d_name) > 3) && 
-                                   !strcasecmp(d->d_name + strlen(d->d_name) - 3, ".so") &&
-                                       !ast_resource_exists(d->d_name)) {
-                                       /* It's a shared library -- Just be sure we're allowed to load it -- kinda
-                                          an inefficient way to do it, but oh well. */
-                                       if (cfg) {
-                                               v = ast_variable_browse(cfg, "modules");
-                                               while(v) {
-                                                       if (!strcasecmp(v->name, "noload") &&
-                                                           !strcasecmp(v->value, d->d_name)) 
-                                                               break;
-                                                       v = v->next;
-                                               }
-                                               if (v) {
-                                                       if (option_verbose) {
-                                                               ast_verbose( VERBOSE_PREFIX_1 "[skipping %s]\n", d->d_name);
-                                                               fflush(stdout);
+               int x;
+               /* Make two passes.  First, load any resource modules, then load the others. */
+               for (x=0;x<2;x++) {
+                       mods = opendir(AST_MODULE_DIR);
+                       if (mods) {
+                               while((d = readdir(mods))) {
+                                       /* Must end in .so to load it.  */
+                                       if ((strlen(d->d_name) > 3) && (x || !strncasecmp(d->d_name, "res_", 4)) && 
+                                           !strcasecmp(d->d_name + strlen(d->d_name) - 3, ".so") &&
+                                               !ast_resource_exists(d->d_name)) {
+                                               /* It's a shared library -- Just be sure we're allowed to load it -- kinda
+                                                  an inefficient way to do it, but oh well. */
+                                               if (cfg) {
+                                                       v = ast_variable_browse(cfg, "modules");
+                                                       while(v) {
+                                                               if (!strcasecmp(v->name, "noload") &&
+                                                                   !strcasecmp(v->value, d->d_name)) 
+                                                                       break;
+                                                               v = v->next;
+                                                       }
+                                                       if (v) {
+                                                               if (option_verbose) {
+                                                                       ast_verbose( VERBOSE_PREFIX_1 "[skipping %s]\n", d->d_name);
+                                                                       fflush(stdout);
+                                                               }
+                                                               continue;
                                                        }
-                                                       continue;
+                                                       
+                                               }
+                                           if (option_debug && !option_verbose)
+                                                       ast_log(LOG_DEBUG, "Loading module %s\n", d->d_name);
+                                               if (option_verbose) {
+                                                       ast_verbose( VERBOSE_PREFIX_1 "[%s]", d->d_name);
+                                                       fflush(stdout);
+                                               }
+                                               if (ast_load_resource(d->d_name)) {
+                                                       ast_log(LOG_WARNING, "Loading module %s failed!\n", d->d_name);
+                                                       if (cfg)
+                                                               ast_destroy(cfg);
+                                                       return -1;
                                                }
-                                               
-                                       }
-                                   if (option_debug && !option_verbose)
-                                               ast_log(LOG_DEBUG, "Loading module %s\n", d->d_name);
-                                       if (option_verbose) {
-                                               ast_verbose( VERBOSE_PREFIX_1 "[%s]", d->d_name);
-                                               fflush(stdout);
-                                       }
-                                       if (ast_load_resource(d->d_name)) {
-                                               ast_log(LOG_WARNING, "Loading module %s failed!\n", d->d_name);
-                                               if (cfg)
-                                                       ast_destroy(cfg);
-                                               return -1;
                                        }
                                }
-                       };
-               } else {
-                       if (!option_quiet)
-                               ast_log(LOG_WARNING, "Unable to open modules directory " AST_MODULE_DIR ".\n");
+                               closedir(mods);
+                       } else {
+                               if (!option_quiet)
+                                       ast_log(LOG_WARNING, "Unable to open modules directory " AST_MODULE_DIR ".\n");
+                       }
                }
        } 
        ast_destroy(cfg);
@@ -290,14 +383,14 @@ void ast_update_use_count(void)
        /* Notify any module monitors that the use count for a 
           resource has changed */
        struct loadupdate *m;
-       if (pthread_mutex_lock(&modlock))
+       if (ast_pthread_mutex_lock(&modlock))
                ast_log(LOG_WARNING, "Failed to lock\n");
        m = updaters;
        while(m) {
                m->updater();
                m = m->next;
        }
-       pthread_mutex_unlock(&modlock);
+       ast_pthread_mutex_unlock(&modlock);
        
 }
 
@@ -313,7 +406,7 @@ int ast_update_module_list(int (*modentry)(char *module, char *description, int
                m = m->next;
        }
        if (unlock)
-               pthread_mutex_unlock(&modlock);
+               ast_pthread_mutex_unlock(&modlock);
        return 0;
 }
 
@@ -323,11 +416,11 @@ int ast_loader_register(int (*v)(void))
        /* XXX Should be more flexible here, taking > 1 verboser XXX */
        if ((tmp = malloc(sizeof (struct loadupdate)))) {
                tmp->updater = v;
-               if (pthread_mutex_lock(&modlock))
+               if (ast_pthread_mutex_lock(&modlock))
                        ast_log(LOG_WARNING, "Failed to lock\n");
                tmp->next = updaters;
                updaters = tmp;
-               pthread_mutex_unlock(&modlock);
+               ast_pthread_mutex_unlock(&modlock);
                return 0;
        }
        return -1;
@@ -337,7 +430,7 @@ int ast_loader_unregister(int (*v)(void))
 {
        int res = -1;
        struct loadupdate *tmp, *tmpl=NULL;
-       if (pthread_mutex_lock(&modlock))
+       if (ast_pthread_mutex_lock(&modlock))
                ast_log(LOG_WARNING, "Failed to lock\n");
        tmp = updaters;
        while(tmp) {
@@ -353,6 +446,6 @@ int ast_loader_unregister(int (*v)(void))
        }
        if (tmp)
                res = 0;
-       pthread_mutex_unlock(&modlock);
+       ast_pthread_mutex_unlock(&modlock);
        return res;
 }