Changeset 180 for trunk/c2s/authreg.c

Show
Ignore:
Timestamp:
28/04/07 23:56:58 (19 months ago)
Author:
smoku
Message:

Dynamically loading auth/reg/storage modules. Closes #52

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/c2s/authreg.c

    r177 r180  
    2020 
    2121#include "c2s.h" 
    22  
    2322#include <stringprep.h> 
     23#include <dlfcn.h> 
    2424 
    2525/* authreg module manager */ 
    26  
    27 /* if you add a module, you'll need to update these arrays */ 
    28  
    29 #ifdef STORAGE_MYSQL 
    30 extern int ar_mysql_init(authreg_t); 
    31 #endif 
    32 #ifdef STORAGE_PGSQL 
    33 extern int ar_pgsql_init(authreg_t); 
    34 #endif 
    35 #ifdef STORAGE_DB 
    36 extern int ar_db_init(authreg_t); 
    37 #endif 
    38 #ifdef STORAGE_LDAP 
    39 extern int ar_ldap_init(authreg_t); 
    40 #endif 
    41 #ifdef STORAGE_PAM 
    42 extern int ar_pam_init(authreg_t); 
    43 #endif 
    44 #ifdef STORAGE_PIPE 
    45 extern int ar_pipe_init(authreg_t); 
    46 #endif 
    47 #ifdef STORAGE_ANON 
    48 extern int ar_anon_init(authreg_t); 
    49 #endif 
    50 #ifdef STORAGE_SQLITE 
    51 extern int ar_sqlite_init(authreg_t); 
    52 #endif 
    53  
    54 static const char *module_names[] = { 
    55 #ifdef STORAGE_MYSQL 
    56     "mysql", 
    57 #endif 
    58 #ifdef STORAGE_PGSQL 
    59     "pgsql", 
    60 #endif 
    61 #ifdef STORAGE_DB 
    62     "db", 
    63 #endif 
    64 #ifdef STORAGE_LDAP 
    65     "ldap", 
    66 #endif 
    67 #ifdef STORAGE_PAM 
    68     "pam", 
    69 #endif 
    70 #ifdef STORAGE_PIPE 
    71     "pipe", 
    72 #endif 
    73 #ifdef STORAGE_ANON 
    74     "anon", 
    75 #endif 
    76 #ifdef STORAGE_SQLITE 
    77     "sqlite", 
    78 #endif 
    79     NULL 
    80 }; 
    81  
    82 ar_module_init_fn module_inits[] = { 
    83 #ifdef STORAGE_MYSQL 
    84     ar_mysql_init, 
    85 #endif 
    86 #ifdef STORAGE_PGSQL 
    87     ar_pgsql_init, 
    88 #endif 
    89 #ifdef STORAGE_DB 
    90     ar_db_init, 
    91 #endif 
    92 #ifdef STORAGE_LDAP 
    93     ar_ldap_init, 
    94 #endif 
    95 #ifdef STORAGE_PAM 
    96     ar_pam_init, 
    97 #endif 
    98 #ifdef STORAGE_PIPE 
    99     ar_pipe_init, 
    100 #endif 
    101 #ifdef STORAGE_ANON 
    102     ar_anon_init, 
    103 #endif 
    104 #ifdef STORAGE_SQLITE 
    105     ar_sqlite_init, 
    106 #endif 
    107     NULL 
    108 }; 
    10926 
    11027typedef struct _authreg_error_st { 
     
    11734/** get a handle for the named module */ 
    11835authreg_t authreg_init(c2s_t c2s, char *name) { 
    119     int n; 
    120     ar_module_init_fn init = NULL; 
     36    char mod_fullpath[PATH_MAX], *modules_path; 
     37    ar_module_init_fn init_fn = NULL; 
    12138    authreg_t ar; 
    122  
    123     /* hunt it down */ 
    124     n = 0; 
    125     while(module_names[n] != NULL) 
    126     { 
    127         if(strcmp(module_names[n], name) == 0) 
    128         { 
    129             init = module_inits[n]; 
    130             break; 
    131         } 
    132         n++; 
    133     } 
    134  
    135     if(init == NULL) 
    136     { 
    137         log_write(c2s->log, LOG_ERR, "no such auth module '%s'", name); 
     39    void *handle; 
     40 
     41    /* load authreg module */ 
     42    modules_path = config_get_one(c2s->config, "authreg.path", 0); 
     43    if (modules_path != NULL) 
     44        log_write(c2s->log, LOG_NOTICE, "modules search path: %s", modules_path); 
     45    else 
     46        log_write(c2s->log, LOG_NOTICE, "modules search path undefined, using default: "LIBRARY_DIR); 
     47 
     48    log_write(c2s->log, LOG_INFO, "loading '%s' authreg module", name); 
     49#ifndef WIN32 
     50    if (modules_path != NULL) 
     51        snprintf(mod_fullpath, PATH_MAX, "%s/authreg_%s.so", modules_path, name); 
     52    else 
     53        snprintf(mod_fullpath, PATH_MAX, "%s/authreg_%s.so", LIBRARY_DIR, name); 
     54    handle = dlopen(mod_fullpath, RTLD_LAZY); 
     55    if (handle != NULL) 
     56        init_fn = dlsym(handle, "ar_init"); 
     57#else 
     58    if (modules_path != NULL) 
     59        snprintf(mod_fullpath, PATH_MAX, "%s\\authreg_%s.dll", modules_path, name); 
     60    else 
     61        snprintf(mod_fullpath, PATH_MAX, "authreg_%s.dll", name); 
     62    handle = (void*) LoadLibrary(mod_fullpath); 
     63    if (handle != NULL) 
     64        init_fn = GetProcAddress((HMODULE) handle, "ar_init"); 
     65#endif 
     66 
     67    if (handle != NULL && init_fn != NULL) { 
     68        log_debug(ZONE, "preloaded module '%s' (not initialized yet)", name); 
     69    } else { 
     70#ifndef WIN32 
     71        log_write(c2s->log, LOG_ERR, "failed loading authreg module '%s' (%s)", name, dlerror()); 
     72        if (handle != NULL) 
     73            dlclose(handle); 
     74#else 
     75        log_write(c2s->log, LOG_ERR, "failed loading authreg module '%s' (errcode: %x)", name, GetLastError()); 
     76        if (handle != NULL) 
     77            FreeLibrary((HMODULE) handle); 
     78#endif 
    13879        return NULL; 
    13980    } 
     
    14687 
    14788    /* call the initialiser */ 
    148     if((init)(ar) != 0) 
    149     { 
    150         log_write(c2s->log, LOG_ERR, "failed to initialise auth module '%s'", name); 
     89    if((init_fn)(ar) != 0) 
     90    { 
     91        log_write(c2s->log, LOG_ERR, "failed to initialize auth module '%s'", name); 
    15192        authreg_free(ar); 
    15293        return NULL; 
     
    162103     
    163104    /* its good */ 
    164     log_write(c2s->log, LOG_NOTICE, "initialised auth module '%s'", name); 
     105    log_write(c2s->log, LOG_NOTICE, "initialized auth module '%s'", name); 
    165106 
    166107    return ar;