Ejemplo n.º 1
0
//---------------------------------------------------------------------------
TModule *TModuleFactoryJava::CreateModule(const string &path){
	if (obj_SaoriFactory==NULL){
		GetLogger().GetStream(LOG_INFO) << "[SAORI Java] Factory not found." << endl;
		return NULL;
	}
	GetLogger().GetStream(LOG_INFO) << "[SAORI Java] CreateModule(" << path << ")" << endl;

	JavaVM *vm; JNIEnv *env;
	if (JNI_OK!=setup(GetLogger(), &vm, &env)) return NULL;

	string fn=CanonicalPath(string(path));
	jstring jfn=env->NewStringUTF(fn.c_str());
	jobject jmodule=env->CallObjectMethod(
		obj_SaoriFactory, mid_createModule, jfn);
	if (jmodule==NULL){
		GetLogger().GetStream(LOG_ERROR) << "[SAORI Java] Create module failed." << endl;
		return NULL;
	}
	jobject gref_jmodule=env->NewGlobalRef(jmodule);
	TModule *ret=new TModuleJava(*this, fn, (SAORI_HANDLE)gref_jmodule);
	if (ret->Initialize()){
		return ret;
	}else{
		GetLogger().GetStream(LOG_ERROR) << "[SAORI Java] Initialize module failed." << endl;
		ret->Unload();
		DeleteModule(ret);
		return NULL;
	}
}
Ejemplo n.º 2
0
/**
 * \fn bool ModuleHandler::Unload(Module *m)
 * \brief Unloads the Module safely and announces the Module unload event
 * \param Module the Module to be unloaded
 */
bool ModuleHandler::Unload(Module *m)
{
	if(!m || m->GetPermanent())
		return false;

	FOREACH_MOD(OnModuleUnload, m);
	return DeleteModule(m);
}
Ejemplo n.º 3
0
ModuleReturn ModuleManager::UnloadModule(Module *m, User *u)
{
	if (!m)
		return MOD_ERR_PARAMS;

	FOREACH_MOD(I_OnModuleUnload, OnModuleUnload(u, m));

	return DeleteModule(m);
}
Ejemplo n.º 4
0
/**
 * \fn void ModuleHandler::UnloadAll()
 * \brief Safely unloads ALL Modules from navn
 */
void ModuleHandler::UnloadAll()
{
	for(Flux::insensitive_map<Module *>::iterator it = Modules.begin(), it_end = Modules.end(); it != it_end;)
	{
		Module *m = it->second;
		++it;
		FOREACH_MOD(OnModuleUnload, m);
		DeleteModule(m);
	}

	Modules.clear();
}
Ejemplo n.º 5
0
ModuleReturn ModuleManager::LoadModule(const Anope::string &modname, User *u)
{
	if (modname.empty())
		return MOD_ERR_PARAMS;

	if (FindModule(modname))
		return MOD_ERR_EXISTS;

	Log(LOG_DEBUG) << "trying to load [" << modname <<  "]";

	/* Generate the filename for the temporary copy of the module */
	Anope::string pbuf = services_dir + "/modules/runtime/" + modname + ".so.XXXXXX";

	/* Don't skip return value checking! -GD */
	ModuleReturn ret = moduleCopyFile(modname, pbuf);
	if (ret != MOD_ERR_OK)
		return ret;

	dlerror();
	void *handle = dlopen(pbuf.c_str(), RTLD_LAZY);
	const char *err = dlerror();
	if (!handle && err && *err)
	{
		Log() << err;
		return MOD_ERR_NOLOAD;
	}

	dlerror();
	Module *(*func)(const Anope::string &, const Anope::string &) = function_cast<Module *(*)(const Anope::string &, const Anope::string &)>(dlsym(handle, "AnopeInit"));
	err = dlerror();
	if (!func && err && *err)
	{
		Log() << "No init function found, not an Anope module";
		dlclose(handle);
		return MOD_ERR_NOLOAD;
	}

	if (!func)
		throw CoreException("Couldn't find constructor, yet moderror wasn't set?");

	/* Create module. */
	Anope::string nick;
	if (u)
		nick = u->nick;

	Module *m;

	try
	{
		m = func(modname, nick);
	}
	catch (const ModuleException &ex)
	{
		Log() << "Error while loading " << modname << ": " << ex.GetReason();
		return MOD_ERR_EXCEPTION;
	}

	m->filename = pbuf;
	m->handle = handle;

	Version v = m->GetVersion();
	if (v.GetMajor() < Anope::VersionMajor() || (v.GetMajor() == Anope::VersionMajor() && v.GetMinor() < Anope::VersionMinor()))
	{
		Log() << "Module " << modname << " is compiled against an older version of Anope " << v.GetMajor() << "." << v.GetMinor() << ", this is " << Anope::VersionMajor() << "." << Anope::VersionMinor();
		DeleteModule(m);
		return MOD_ERR_VERSION;
	}
	else if (v.GetMajor() > Anope::VersionMajor() || (v.GetMajor() == Anope::VersionMajor() && v.GetMinor() > Anope::VersionMinor()))
	{
		Log() << "Module " << modname << " is compiled against a newer version of Anope " << v.GetMajor() << "." << v.GetMinor() << ", this is " << Anope::VersionMajor() << "." << Anope::VersionMinor();
		DeleteModule(m);
		return MOD_ERR_VERSION;
	}
	else if (v.GetBuild() < Anope::VersionBuild())
		Log() << "Module " << modname << " is compiled against an older revision of Anope " << v.GetBuild() << ", this is " << Anope::VersionBuild();
	else if (v.GetBuild() > Anope::VersionBuild())
		Log() << "Module " << modname << " is compiled against a newer revision of Anope " << v.GetBuild() << ", this is " << Anope::VersionBuild();
	else if (v.GetBuild() == Anope::VersionBuild())
		Log(LOG_DEBUG) << "Module " << modname << " compiled against current version of Anope " << v.GetBuild();

	if (m->type == PROTOCOL && ModuleManager::FindFirstOf(PROTOCOL) != m)
	{
		DeleteModule(m);
		Log() << "You cannot load two protocol modules";
		return MOD_ERR_UNKNOWN;
	}

	FOREACH_MOD(I_OnModuleLoad, OnModuleLoad(u, m));

	return MOD_ERR_OK;
}
Ejemplo n.º 6
0
/*************************************************************************
 *
 * m a i n
 */
int
main(int argc, char* argv[])
{
    int errcode = SUCCESS;
    PRBool createdb, readOnly;
#define STDINBUF_SIZE 80
    char stdinbuf[STDINBUF_SIZE];

    progName = strrchr(argv[0], '/');
    progName = progName ? progName + 1 : argv[0];

    PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);

    if (parse_args(argc, argv) != SUCCESS) {
        usage();
        errcode = INVALID_USAGE_ERR;
        goto loser;
    }

    if (verify_params() != SUCCESS) {
        usage();
        errcode = INVALID_USAGE_ERR;
        goto loser;
    }

    if (command == NO_COMMAND) {
        PR_fprintf(PR_STDERR, errStrings[NO_COMMAND_ERR]);
        usage();
        errcode = INVALID_USAGE_ERR;
        goto loser;
    }

    /* Set up crypto stuff */
    createdb = command == CREATE_COMMAND;
    readOnly = ((command == LIST_COMMAND) ||
                (command == CHKFIPS_COMMAND) ||
                (command == RAW_LIST_COMMAND));

    /* Make sure browser is not running if we're writing to a database */
    /* Do this before initializing crypto */
    if (!readOnly && !force) {
        char* response;

        PR_fprintf(PR_STDOUT, msgStrings[BROWSER_RUNNING_MSG]);
        if (!PR_fgets(stdinbuf, STDINBUF_SIZE, PR_STDIN)) {
            PR_fprintf(PR_STDERR, errStrings[STDIN_READ_ERR]);
            errcode = STDIN_READ_ERR;
            goto loser;
        }
        if ((response = strtok(stdinbuf, " \r\n\t"))) {
            if (!PL_strcasecmp(response, "q")) {
                PR_fprintf(PR_STDOUT, msgStrings[ABORTING_MSG]);
                errcode = SUCCESS;
                goto loser;
            }
        }
        PR_fprintf(PR_STDOUT, "\n");
    }

    errcode = check_crypto(createdb, readOnly);
    if (errcode != SUCCESS) {
        goto loser;
    }

    if ((command == RAW_LIST_COMMAND) || (command == RAW_ADD_COMMAND)) {
        if (!moduleName) {
            char *readOnlyStr, *noCertDBStr, *sep;
            if (!secmodName)
                secmodName = "secmod.db";
            if (!dbprefix)
                dbprefix = "";
            sep = ((command == RAW_LIST_COMMAND) && nocertdb) ? "," : " ";
            readOnlyStr = (command == RAW_LIST_COMMAND) ? "readOnly" : "";
            noCertDBStr = nocertdb ? "noCertDB" : "";
            SECU_ConfigDirectory(dbdir);

            moduleName = PR_smprintf(
                "name=\"NSS default Module DB\" parameters=\"configdir=%s certPrefix=%s "
                "keyPrefix=%s secmod=%s flags=%s%s%s\" NSS=\"flags=internal,moduleDB,"
                "moduleDBOnly,critical\"",
                SECU_ConfigDirectory(NULL), dbprefix, dbprefix,
                secmodName, readOnlyStr, sep, noCertDBStr);
        }
        if (command == RAW_LIST_COMMAND) {
            errcode = RawListModule(moduleName);
        } else {
            PORT_Assert(moduleSpec);
            errcode = RawAddModule(moduleName, moduleSpec);
        }
        goto loser;
    }

    errcode = init_crypto(createdb, readOnly);
    if (errcode != SUCCESS) {
        goto loser;
    }

    errcode = LoadMechanismList();
    if (errcode != SUCCESS) {
        goto loser;
    }

    /* Execute the command */
    switch (command) {
        case ADD_COMMAND:
            errcode = AddModule(moduleName, libFile, ciphers, mechanisms, secmodString);
            break;
        case CHANGEPW_COMMAND:
            errcode = ChangePW(tokenName, pwFile, newpwFile);
            break;
        case CREATE_COMMAND:
            /* The work was already done in init_crypto() */
            break;
        case DEFAULT_COMMAND:
            errcode = SetDefaultModule(moduleName, slotName, mechanisms);
            break;
        case DELETE_COMMAND:
            errcode = DeleteModule(moduleName);
            break;
        case DISABLE_COMMAND:
            errcode = EnableModule(moduleName, slotName, PR_FALSE);
            break;
        case ENABLE_COMMAND:
            errcode = EnableModule(moduleName, slotName, PR_TRUE);
            break;
        case FIPS_COMMAND:
            errcode = FipsMode(fipsArg);
            break;
        case CHKFIPS_COMMAND:
            errcode = ChkFipsMode(fipsArg);
            break;
        case JAR_COMMAND:
            Pk11Install_SetErrorHandler(install_error);
            errcode = Pk11Install_DoInstall(jarFile, installDir, tempDir,
                                            PR_STDOUT, force, nocertdb);
            break;
        case LIST_COMMAND:
            if (moduleName) {
                errcode = ListModule(moduleName);
            } else {
                errcode = ListModules();
            }
            break;
        case UNDEFAULT_COMMAND:
            errcode = UnsetDefaultModule(moduleName, slotName, mechanisms);
            break;
        default:
            PR_fprintf(PR_STDERR, "This command is not supported yet.\n");
            errcode = INVALID_USAGE_ERR;
            break;
    }

    if (NSS_Shutdown() != SECSuccess) {
        exit(1);
    }

loser:
    PR_Cleanup();
    return errcode;
}
Ejemplo n.º 7
0
ModuleReturn ModuleManager::LoadModule(const Anope::string &modname, User *u)
{
	if (modname.empty())
		return MOD_ERR_PARAMS;

	if (FindModule(modname))
		return MOD_ERR_EXISTS;

	Log(LOG_DEBUG) << "Trying to load module: " << modname;

#ifdef _WIN32
	/* Generate the filename for the temporary copy of the module */
	Anope::string pbuf = Anope::DataDir + "/runtime/" + modname + ".so.XXXXXX";

	/* Don't skip return value checking! -GD */
	ModuleReturn ret = moduleCopyFile(modname, pbuf);
	if (ret != MOD_ERR_OK)
	{
		if (ret == MOD_ERR_NOEXIST)
			Log(LOG_TERMINAL) << "Error while loading " << modname << " (file does not exist)";
		else if (ret == MOD_ERR_FILE_IO)
			Log(LOG_TERMINAL) << "Error while loading " << modname << " (file IO error, check file permissions and diskspace)";
		return ret;
	}
#else
	Anope::string pbuf = Anope::ModuleDir + "/modules/" + modname + ".so";
#endif

	dlerror();
	void *handle = dlopen(pbuf.c_str(), RTLD_NOW);
	const char *err = dlerror();
	if (!handle)
	{
		if (err && *err)
			Log() << err;
		return MOD_ERR_NOLOAD;
	}

	dlerror();
	Module *(*func)(const Anope::string &, const Anope::string &) = function_cast<Module *(*)(const Anope::string &, const Anope::string &)>(dlsym(handle, "AnopeInit"));
	err = dlerror();
	if (!func)
	{
		Log() << "No init function found, not an Anope module";
		if (err && *err)
			Log(LOG_DEBUG) << err;
		dlclose(handle);
		return MOD_ERR_NOLOAD;
	}
	
	/* Create module. */
	Anope::string nick;
	if (u)
		nick = u->nick;

	Module *m;

	ModuleReturn moderr = MOD_ERR_OK;
	try
	{
		m = func(modname, nick);
	}
	catch (const ModuleException &ex)
	{
		Log() << "Error while loading " << modname << ": " << ex.GetReason();
		moderr = MOD_ERR_EXCEPTION;
	}
	
	if (moderr != MOD_ERR_OK)
	{
		if (dlclose(handle))
			Log() << dlerror();
		return moderr;
	}

	m->filename = pbuf;
	m->handle = handle;

	ModuleVersion v = m->GetVersion();
	if (v.GetMajor() < Anope::VersionMajor() || (v.GetMajor() == Anope::VersionMajor() && v.GetMinor() < Anope::VersionMinor()))
	{
		Log() << "Module " << modname << " is compiled against an older version of Anope " << v.GetMajor() << "." << v.GetMinor() << ", this is " << Anope::VersionShort();
		DeleteModule(m);
		return MOD_ERR_VERSION;
	}
	else if (v.GetMajor() > Anope::VersionMajor() || (v.GetMajor() == Anope::VersionMajor() && v.GetMinor() > Anope::VersionMinor()))
	{
		Log() << "Module " << modname << " is compiled against a newer version of Anope " << v.GetMajor() << "." << v.GetMinor() << ", this is " << Anope::VersionShort();
		DeleteModule(m);
		return MOD_ERR_VERSION;
	}
	else if (v.GetPatch() < Anope::VersionPatch())
	{
		Log() << "Module " << modname << " is compiled against an older version of Anope, " << v.GetMajor() << "." << v.GetMinor() << "." << v.GetPatch() << ", this is " << Anope::VersionShort();
		DeleteModule(m);
		return MOD_ERR_VERSION;
	}
	else if (v.GetPatch() > Anope::VersionPatch())
	{
		Log() << "Module " << modname << " is compiled against a newer version of Anope, " << v.GetMajor() << "." << v.GetMinor() << "." << v.GetPatch() << ", this is " << Anope::VersionShort();
		DeleteModule(m);
		return MOD_ERR_VERSION;
	}
	else
		Log(LOG_DEBUG_2) << "Module " << modname << " is compiled against current version of Anope " << Anope::VersionShort();

	/* Initialize config */
	try
	{
		m->OnReload(Config);
	}
	catch (const ModuleException &ex)
	{
		Log() << "Module " << modname << " couldn't load:" << ex.GetReason();
		moderr = MOD_ERR_EXCEPTION;
	}
	catch (const ConfigException &ex)
	{
		Log() << "Module " << modname << " couldn't load due to configuration problems: " << ex.GetReason();
		moderr = MOD_ERR_EXCEPTION;
	}
	catch (const NotImplementedException &ex)
	{
	}
	
	if (moderr != MOD_ERR_OK)
	{
		DeleteModule(m);
		return moderr;
	}

	Log(LOG_DEBUG) << "Module " << modname << " loaded.";

	/* Attach module to all events */
	for (unsigned i = 0; i < I_SIZE; ++i)
		EventHandlers[i].push_back(m);

	FOREACH_MOD(OnModuleLoad, (u, m));

	return MOD_ERR_OK;
}