示例#1
0
文件: main.c 项目: YmrDtnJu/jabberd2
static void _c2s_ar_free(const char *module, int modulelen, void *val, void *arg) {
    authreg_t ar = (authreg_t) val;
    authreg_free(ar);
}
示例#2
0
/** get a handle for the named module */
authreg_t authreg_init(c2s_t c2s, char *name) {
    char mod_fullpath[PATH_MAX], *modules_path;
    ar_module_init_fn init_fn = NULL;
    authreg_t ar;
    void *handle;

    /* load authreg module */
    modules_path = config_get_one(c2s->config, "authreg.path", 0);
    if (modules_path != NULL)
        log_write(c2s->log, LOG_NOTICE, "modules search path: %s", modules_path);
    else
        log_write(c2s->log, LOG_NOTICE, "modules search path undefined, using default: "LIBRARY_DIR);

    log_write(c2s->log, LOG_INFO, "loading '%s' authreg module", name);
#ifndef _WIN32
    if (modules_path != NULL)
        snprintf(mod_fullpath, PATH_MAX, "%s/authreg_%s.so", modules_path, name);
    else
        snprintf(mod_fullpath, PATH_MAX, "%s/authreg_%s.so", LIBRARY_DIR, name);
    handle = dlopen(mod_fullpath, RTLD_LAZY);
    if (handle != NULL)
        init_fn = dlsym(handle, "ar_init");
#else
    if (modules_path != NULL)
        snprintf(mod_fullpath, PATH_MAX, "%s\\authreg_%s.dll", modules_path, name);
    else
        snprintf(mod_fullpath, PATH_MAX, "authreg_%s.dll", name);
    handle = (void*) LoadLibrary(mod_fullpath);
    if (handle != NULL)
        init_fn = (ar_module_init_fn)GetProcAddress((HMODULE) handle, "ar_init");
#endif

    if (handle != NULL && init_fn != NULL) {
        log_debug(ZONE, "preloaded module '%s' (not initialized yet)", name);
    } else {
#ifndef _WIN32
        log_write(c2s->log, LOG_ERR, "failed loading authreg module '%s' (%s)", name, dlerror());
        if (handle != NULL)
            dlclose(handle);
#else
        log_write(c2s->log, LOG_ERR, "failed loading authreg module '%s' (errcode: %x)", name, GetLastError());
        if (handle != NULL)
            FreeLibrary((HMODULE) handle);
#endif
        return NULL;
    }

    /* make a new one */
    ar = (authreg_t) calloc(1, sizeof(struct authreg_st));

    ar->c2s = c2s;

    /* call the initialiser */
    if((init_fn)(ar) != 0)
    {
        log_write(c2s->log, LOG_ERR, "failed to initialize auth module '%s'", name);
        authreg_free(ar);
        return NULL;
    }

    /* we need user_exists(), at the very least */
    if(ar->user_exists == NULL)
    {
        log_write(c2s->log, LOG_ERR, "auth module '%s' has no check for user existence", name);
        authreg_free(ar);
        return NULL;
    }
    
    /* its good */
    log_write(c2s->log, LOG_NOTICE, "initialized auth module '%s'", name);

    return ar;
}