} // namespace anonymous
-int CsLoader::globalInit(const std::string &ro_res_dir,
- const std::string &rw_working_dir)
-{
- if (ro_res_dir.empty() || rw_working_dir.empty())
- throw std::invalid_argument("cs loader global init");
-
- return m_pc.fpGlobalInit(ro_res_dir.c_str(), rw_working_dir.c_str());
-}
-
-int CsLoader::globalDeinit()
-{
- return m_pc.fpGlobalDeinit();
-}
-
int CsLoader::contextCreate(csre_cs_context_h &c)
{
- return m_pc.fpContextCreate(&c);
+ return this->m_pc.fpContextCreate(&c);
}
int CsLoader::contextDestroy(csre_cs_context_h c)
if (c == nullptr)
throw std::invalid_argument("cs loader context destroy");
- return m_pc.fpContextDestroy(c);
+ return this->m_pc.fpContextDestroy(c);
}
int CsLoader::scanData(csre_cs_context_h c,
if (c == nullptr || data.empty() || pdetected == nullptr)
throw std::invalid_argument("cs loader scan data");
- return m_pc.fpScanData(c, data.data(), data.size(), pdetected);
+ return this->m_pc.fpScanData(c, data.data(), data.size(), pdetected);
}
int CsLoader::scanFile(csre_cs_context_h c, const std::string &filepath,
if (c == nullptr || filepath.empty() || pdetected == nullptr)
throw std::invalid_argument("cs loader scan file");
- return m_pc.fpScanFile(c, filepath.c_str(), pdetected);
+ return this->m_pc.fpScanFile(c, filepath.c_str(), pdetected);
}
int CsLoader::scanAppOnCloud(csre_cs_context_h c, const std::string &appdir,
if (c == nullptr || appdir.empty() || pdetected == nullptr)
throw std::invalid_argument("cs loader scan app on cloud");
- return m_pc.fpScanAppOnCloud(c, appdir.c_str(), pdetected);
+ return this->m_pc.fpScanAppOnCloud(c, appdir.c_str(), pdetected);
}
int CsLoader::getSeverity(csre_cs_detected_h d,
if (d == nullptr || pseverity == nullptr)
throw std::invalid_argument("cs loader get severity");
- return m_pc.fpGetSeverity(d, pseverity);
+ return this->m_pc.fpGetSeverity(d, pseverity);
}
int CsLoader::getMalwareName(csre_cs_detected_h d, std::string &value)
throw std::invalid_argument("cs loader get malware name");
return getValueCstr(value, [&](const char **cvalue) {
- return m_pc.fpGetMalwareName(d, cvalue);
+ return this->m_pc.fpGetMalwareName(d, cvalue);
});
}
throw std::invalid_argument("cs loader get detailed url");
return getValueCstr(value, [&](const char **cvalue) {
- return m_pc.fpGetDetailedUrl(d, cvalue);
+ return this->m_pc.fpGetDetailedUrl(d, cvalue);
});
}
if (d == nullptr || timestamp == nullptr)
throw std::invalid_argument("cs loader get time stamp");
- return m_pc.fpGetTimestamp(d, timestamp);
+ return this->m_pc.fpGetTimestamp(d, timestamp);
}
int CsLoader::getErrorString(int ec, std::string &value)
{
return getValueCstr(value, [&](const char **cvalue) {
- return m_pc.fpGetErrorString(ec, cvalue);
+ return this->m_pc.fpGetErrorString(ec, cvalue);
});
}
int CsLoader::getEngineInfo(csre_cs_engine_h &e)
{
- return m_pc.fpGetEngineInfo(&e);
+ return this->m_pc.fpGetEngineInfo(&e);
}
int CsLoader::destroyEngine(csre_cs_engine_h e)
{
- return m_pc.fpDestroyEngine(e);
+ return this->m_pc.fpDestroyEngine(e);
}
int CsLoader::getEngineApiVersion(csre_cs_engine_h e, std::string &value)
throw std::invalid_argument("cs loader get error string");
return getValueCstr(value, [&](const char **cvalue) {
- return m_pc.fpGetEngineApiVersion(e, cvalue);
+ return this->m_pc.fpGetEngineApiVersion(e, cvalue);
});
}
throw std::invalid_argument("cs loader get engine vendor");
return getValueCstr(value, [&](const char **cvalue) {
- return m_pc.fpGetEngineVendor(e, cvalue);
+ return this->m_pc.fpGetEngineVendor(e, cvalue);
});
}
throw std::invalid_argument("cs loader get engine name");
return getValueCstr(value, [&](const char **cvalue) {
- return m_pc.fpGetEngineName(e, cvalue);
+ return this->m_pc.fpGetEngineName(e, cvalue);
});
}
throw std::invalid_argument("cs loader get engine version");
return getValueCstr(value, [&](const char **cvalue) {
- return m_pc.fpGetEngineVersion(e, cvalue);
+ return this->m_pc.fpGetEngineVersion(e, cvalue);
});
}
throw std::invalid_argument("cs loader get engine version");
return getValueCstr(value, [&](const char **cvalue) {
- return m_pc.fpGetEngineDataVersion(e, cvalue);
+ return this->m_pc.fpGetEngineDataVersion(e, cvalue);
});
}
if (e == nullptr || ptime == nullptr)
throw std::invalid_argument("cs loader get latest update time");
- return m_pc.fpGetEngineLatestUpdateTime(e, ptime);
+ return this->m_pc.fpGetEngineLatestUpdateTime(e, ptime);
}
int CsLoader::getEngineActivated(csre_cs_engine_h e,
if (e == nullptr || pactivated == nullptr)
throw std::invalid_argument("cs loader get engine activated");
- return m_pc.fpGetEngineActivated(e, pactivated);
+ return this->m_pc.fpGetEngineActivated(e, pactivated);
}
int CsLoader::getEngineVendorLogo(csre_cs_engine_h e,
unsigned char *cvalue = nullptr;
unsigned int size = 0;
- auto retval = m_pc.fpGetEngineVendorLogo(e, &cvalue, &size);
+ auto retval = this->m_pc.fpGetEngineVendorLogo(e, &cvalue, &size);
if (retval == CSRE_ERROR_NONE && cvalue != nullptr && size != 0)
value.assign(cvalue, cvalue + size);
return retval;
}
-CsLoader::CsLoader(const std::string &enginePath)
+void CsLoader::init(const std::string &enginePath, const std::string &roResDir,
+ const std::string &rwWorkingDir)
{
+ if (enginePath.empty() || roResDir.empty() || rwWorkingDir.empty())
+ ThrowExc(InternalError, "empty string comes in to loader init");
+
INFO("Load CS-Engine plugin start. engine path: " << enginePath);
void *handle = dlopen(enginePath.c_str(), RTLD_LAZY);
ThrowExc(InternalError, "engine dlopen error. path: " << enginePath <<
" errno: " << errno);
- m_pc.dlhandle = handle;
-
- m_pc.fpGlobalInit = reinterpret_cast<FpGlobalInit>(dlsym(handle,
- "csre_cs_global_initialize"));
- m_pc.fpGlobalDeinit = reinterpret_cast<FpGlobalDeinit>(dlsym(handle,
- "csre_cs_global_deinitialize"));
- m_pc.fpContextCreate = reinterpret_cast<FpContextCreate>(dlsym(handle,
- "csre_cs_context_create"));
- m_pc.fpContextDestroy = reinterpret_cast<FpContextDestroy>(dlsym(handle,
- "csre_cs_context_destroy"));
- m_pc.fpScanData = reinterpret_cast<FpScanData>(dlsym(handle,
- "csre_cs_scan_data"));
- m_pc.fpScanFile = reinterpret_cast<FpScanFile>(dlsym(handle,
- "csre_cs_scan_file"));
- m_pc.fpScanAppOnCloud = reinterpret_cast<FpScanAppOnCloud>(dlsym(handle,
- "csre_cs_scan_app_on_cloud"));
- m_pc.fpGetSeverity = reinterpret_cast<FpGetSeverity>(dlsym(handle,
- "csre_cs_detected_get_severity"));
- m_pc.fpGetMalwareName = reinterpret_cast<FpGetMalwareName>(dlsym(handle,
- "csre_cs_detected_get_malware_name"));
- m_pc.fpGetDetailedUrl = reinterpret_cast<FpGetDetailedUrl>(dlsym(handle,
- "csre_cs_detected_get_detailed_url"));
- m_pc.fpGetTimestamp = reinterpret_cast<FpGetTimestamp>(dlsym(handle,
- "csre_cs_detected_get_timestamp"));
- m_pc.fpGetErrorString = reinterpret_cast <FpGetErrorString>(dlsym(handle,
- "csre_cs_get_error_string"));
- m_pc.fpGetEngineInfo = reinterpret_cast<FpGetEngineInfo>(dlsym(handle,
- "csre_cs_engine_get_info"));
- m_pc.fpDestroyEngine = reinterpret_cast<FpDestroyEngine>(dlsym(handle,
- "csre_cs_engine_destroy"));
- m_pc.fpGetEngineApiVersion = reinterpret_cast<FpGetEngineApiVersion>(dlsym(
- handle, "csre_cs_engine_get_api_version"));
- m_pc.fpGetEngineVendor = reinterpret_cast<FpGetEngineVendor>(dlsym(handle,
- "csre_cs_engine_get_vendor"));
- m_pc.fpGetEngineName = reinterpret_cast<FpGetEngineName>(dlsym(handle,
- "csre_cs_engine_get_name"));
- m_pc.fpGetEngineVersion = reinterpret_cast<FpGetEngineVersion>(dlsym(handle,
- "csre_cs_engine_get_version"));
- m_pc.fpGetEngineDataVersion = reinterpret_cast<FpGetEngineDataVersion>(dlsym(
- handle, "csre_cs_engine_get_data_version"));
- m_pc.fpGetEngineLatestUpdateTime =
+ this->m_pc.dlhandle = handle;
+
+ this->m_pc.fpGlobalInit = reinterpret_cast<FpGlobalInit>(dlsym(handle,
+ "csre_cs_global_initialize"));
+ this->m_pc.fpGlobalDeinit = reinterpret_cast<FpGlobalDeinit>(dlsym(handle,
+ "csre_cs_global_deinitialize"));
+ this->m_pc.fpContextCreate = reinterpret_cast<FpContextCreate>(dlsym(handle,
+ "csre_cs_context_create"));
+ this->m_pc.fpContextDestroy = reinterpret_cast<FpContextDestroy>(dlsym(handle,
+ "csre_cs_context_destroy"));
+ this->m_pc.fpScanData = reinterpret_cast<FpScanData>(dlsym(handle,
+ "csre_cs_scan_data"));
+ this->m_pc.fpScanFile = reinterpret_cast<FpScanFile>(dlsym(handle,
+ "csre_cs_scan_file"));
+ this->m_pc.fpScanAppOnCloud = reinterpret_cast<FpScanAppOnCloud>(dlsym(handle,
+ "csre_cs_scan_app_on_cloud"));
+ this->m_pc.fpGetSeverity = reinterpret_cast<FpGetSeverity>(dlsym(handle,
+ "csre_cs_detected_get_severity"));
+ this->m_pc.fpGetMalwareName = reinterpret_cast<FpGetMalwareName>(dlsym(handle,
+ "csre_cs_detected_get_malware_name"));
+ this->m_pc.fpGetDetailedUrl = reinterpret_cast<FpGetDetailedUrl>(dlsym(handle,
+ "csre_cs_detected_get_detailed_url"));
+ this->m_pc.fpGetTimestamp = reinterpret_cast<FpGetTimestamp>(dlsym(handle,
+ "csre_cs_detected_get_timestamp"));
+ this->m_pc.fpGetErrorString = reinterpret_cast <FpGetErrorString>(dlsym(handle,
+ "csre_cs_get_error_string"));
+ this->m_pc.fpGetEngineInfo = reinterpret_cast<FpGetEngineInfo>(dlsym(handle,
+ "csre_cs_engine_get_info"));
+ this->m_pc.fpDestroyEngine = reinterpret_cast<FpDestroyEngine>(dlsym(handle,
+ "csre_cs_engine_destroy"));
+ this->m_pc.fpGetEngineApiVersion = reinterpret_cast<FpGetEngineApiVersion>(dlsym(
+ handle, "csre_cs_engine_get_api_version"));
+ this->m_pc.fpGetEngineVendor = reinterpret_cast<FpGetEngineVendor>(dlsym(handle,
+ "csre_cs_engine_get_vendor"));
+ this->m_pc.fpGetEngineName = reinterpret_cast<FpGetEngineName>(dlsym(handle,
+ "csre_cs_engine_get_name"));
+ this->m_pc.fpGetEngineVersion = reinterpret_cast<FpGetEngineVersion>(dlsym(handle,
+ "csre_cs_engine_get_version"));
+ this->m_pc.fpGetEngineDataVersion = reinterpret_cast<FpGetEngineDataVersion>(dlsym(
+ handle, "csre_cs_engine_get_data_version"));
+ this->m_pc.fpGetEngineLatestUpdateTime =
reinterpret_cast<FpGetEngineLatestUpdateTime>(dlsym(handle,
"csre_cs_engine_get_latest_update_time"));
- m_pc.fpGetEngineActivated = reinterpret_cast<FpGetEngineActivated>(dlsym(handle,
- "csre_cs_engine_get_activated"));
- m_pc.fpGetEngineVendorLogo = reinterpret_cast<FpGetEngineVendorLogo>(dlsym(
- handle, "csre_cs_engine_get_vendor_logo"));
-
- if (m_pc.fpGlobalInit == nullptr || m_pc.fpGlobalDeinit == nullptr ||
- m_pc.fpContextCreate == nullptr || m_pc.fpContextDestroy == nullptr ||
- m_pc.fpScanData == nullptr || m_pc.fpScanFile == nullptr ||
- m_pc.fpScanAppOnCloud == nullptr || m_pc.fpGetSeverity == nullptr ||
- m_pc.fpGetMalwareName == nullptr ||
- m_pc.fpGetDetailedUrl == nullptr || m_pc.fpGetTimestamp == nullptr ||
- m_pc.fpGetErrorString == nullptr || m_pc.fpGetEngineInfo == nullptr ||
- m_pc.fpGetEngineApiVersion == nullptr || m_pc.fpGetEngineVendor == nullptr ||
- m_pc.fpGetEngineName == nullptr || m_pc.fpGetEngineVersion == nullptr ||
- m_pc.fpGetEngineDataVersion == nullptr ||
- m_pc.fpGetEngineLatestUpdateTime == nullptr ||
- m_pc.fpGetEngineActivated == nullptr || m_pc.fpGetEngineVendorLogo == nullptr) {
- dlclose(handle);
+ this->m_pc.fpGetEngineActivated = reinterpret_cast<FpGetEngineActivated>(dlsym(handle,
+ "csre_cs_engine_get_activated"));
+ this->m_pc.fpGetEngineVendorLogo = reinterpret_cast<FpGetEngineVendorLogo>(dlsym(
+ handle, "csre_cs_engine_get_vendor_logo"));
+
+ if (this->m_pc.fpGlobalInit == nullptr || this->m_pc.fpGlobalDeinit == nullptr ||
+ this->m_pc.fpContextCreate == nullptr ||
+ this->m_pc.fpContextDestroy == nullptr ||
+ this->m_pc.fpScanData == nullptr || this->m_pc.fpScanFile == nullptr ||
+ this->m_pc.fpScanAppOnCloud == nullptr ||
+ this->m_pc.fpGetSeverity == nullptr ||
+ this->m_pc.fpGetMalwareName == nullptr ||
+ this->m_pc.fpGetDetailedUrl == nullptr ||
+ this->m_pc.fpGetTimestamp == nullptr ||
+ this->m_pc.fpGetErrorString == nullptr ||
+ this->m_pc.fpGetEngineInfo == nullptr ||
+ this->m_pc.fpGetEngineApiVersion == nullptr ||
+ this->m_pc.fpGetEngineVendor == nullptr ||
+ this->m_pc.fpGetEngineName == nullptr ||
+ this->m_pc.fpGetEngineVersion == nullptr ||
+ this->m_pc.fpGetEngineDataVersion == nullptr ||
+ this->m_pc.fpGetEngineLatestUpdateTime == nullptr ||
+ this->m_pc.fpGetEngineActivated == nullptr ||
+ this->m_pc.fpGetEngineVendorLogo == nullptr) {
+ dlclose(this->m_pc.dlhandle);
+ this->m_pc.dlhandle = nullptr;
ThrowExc(EngineError, "Failed to load funcs from engine library. "
"engine path: " << enginePath << "errno: " << errno);
}
+
+ auto ret = this->m_pc.fpGlobalInit(roResDir.c_str(), rwWorkingDir.c_str());
+
+ if (ret != CSRE_ERROR_NONE) {
+ dlclose(this->m_pc.dlhandle);
+ this->m_pc.dlhandle = nullptr;
+ toException(ret);
+ }
+}
+
+void CsLoader::reset(const std::string &enginePath, const std::string &roResDir,
+ const std::string &rwWorkingDir)
+{
+ // ignore return value
+ this->m_pc.fpGlobalDeinit();
+
+ if (this->m_pc.dlhandle) {
+ dlclose(this->m_pc.dlhandle);
+ this->m_pc.dlhandle = nullptr;
+ }
+
+ this->init(enginePath, roResDir, rwWorkingDir);
+}
+
+CsLoader::CsLoader(const std::string &enginePath, const std::string &roResDir,
+ const std::string &rwWorkingDir)
+{
+ this->init(enginePath, roResDir, rwWorkingDir);
}
CsLoader::~CsLoader()
{
- dlclose(m_pc.dlhandle);
+ // ignore return value
+ this->m_pc.fpGlobalDeinit();
+
+ if (this->m_pc.dlhandle) {
+ dlclose(this->m_pc.dlhandle);
+ this->m_pc.dlhandle = nullptr;
+ }
}
CsEngineContext::CsEngineContext(CsLoader &loader) : m_loader(loader), m_context(nullptr)
{
- toException(m_loader.contextCreate(m_context));
+ toException(this->m_loader.contextCreate(this->m_context));
}
CsEngineContext::~CsEngineContext()
{
- toException(m_loader.contextDestroy(m_context));
+ toException(this->m_loader.contextDestroy(this->m_context));
}
csre_cs_context_h &CsEngineContext::get(void)
{
- return m_context;
+ return this->m_context;
}
CsEngineInfo::CsEngineInfo(CsLoader &loader) : m_loader(loader), m_info(nullptr)
{
- toException(m_loader.getEngineInfo(m_info));
+ toException(this->m_loader.getEngineInfo(this->m_info));
}
CsEngineInfo::~CsEngineInfo()
{
- toException(m_loader.destroyEngine(m_info));
+ toException(this->m_loader.destroyEngine(this->m_info));
}
csre_cs_engine_h &CsEngineInfo::get(void)
{
- return m_info;
+ return this->m_info;
}
}
class CsLoader {
public:
- CsLoader(const std::string &);
+ CsLoader(const std::string &enginePath, const std::string &roResDir,
+ const std::string &rwWorkingDir);
virtual ~CsLoader();
- int globalInit(const std::string &, const std::string &);
- int globalDeinit();
+
+ void reset(const std::string &enginePath, const std::string &roResDir,
+ const std::string &rwWorkingDir);
+
int contextCreate(csre_cs_context_h &);
int contextDestroy(csre_cs_context_h);
int scanData(csre_cs_context_h, const std::vector<unsigned char> &,
FpGetEngineVendorLogo fpGetEngineVendorLogo;
};
+ void init(const std::string &, const std::string &, const std::string &);
+
PluginContext m_pc;
};
} // namespace anonymous
-CsLogic::CsLogic() :
- m_loader(new CsLoader(CS_ENGINE_PATH)),
- m_db(new Db::Manager(RW_DBSPACE "/.csr.db", RO_DBSPACE))
+CsLogic::CsLogic(CsLoader &loader, Db::Manager &db) : m_loader(loader), m_db(db)
{
- // TODO: Provide engine-specific res/working dirs
- toException(m_loader->globalInit(SAMPLE_ENGINE_RO_RES_DIR,
- SAMPLE_ENGINE_RW_WORKING_DIR));
-
- CsEngineInfo csEngineInfo(*m_loader);
- toException(m_loader->getEngineDataVersion(csEngineInfo.get(), m_dataVersion));
+ CsEngineInfo csEngineInfo(this->m_loader);
+ toException(this->m_loader.getEngineDataVersion(csEngineInfo.get(),
+ this->m_dataVersion));
}
CsLogic::~CsLogic()
{
- try {
- toException(m_loader->globalDeinit());
- } catch (const Exception &e) {
- ERROR("ignore all custom exceptions in logic dtor: " << e.error() <<
- " " << e.what());
- }
}
RawBuffer CsLogic::scanData(const CsContext &context, const RawBuffer &data)
setCoreUsage(context.coreUsage);
- CsEngineContext engineContext(*m_loader);
+ CsEngineContext engineContext(this->m_loader);
auto &c = engineContext.get();
csre_cs_detected_h result;
- toException(m_loader->scanData(c, data, &result));
+ toException(this->m_loader.scanData(c, data, &result));
// detected handle is null if it's safe
if (result == nullptr)
const std::string &pkgPath,
const std::string &pkgId)
{
- CsEngineContext engineContext(*m_loader);
+ CsEngineContext engineContext(this->m_loader);
auto &c = engineContext.get();
csre_cs_detected_h result;
- toException(m_loader->scanAppOnCloud(c, pkgPath, &result));
+ toException(this->m_loader.scanAppOnCloud(c, pkgPath, &result));
if (!result)
return BinaryQueue::Serialize(CSR_ERROR_NONE).pop();
{
auto starttime = time(nullptr);
- CsEngineContext engineContext(*this->m_loader);
+ CsEngineContext engineContext(this->m_loader);
auto &c = engineContext.get();
// traverse files in app and take which is more danger than riskiest.
auto visitor = FsVisitor::create(
- pkgPath,
- this->m_db->getLastScanTime(pkgPath, this->m_dataVersion));
+ pkgPath,
+ this->m_db.getLastScanTime(pkgPath, this->m_dataVersion));
CsDetectedPtr riskiest;
while (auto file = visitor->next()) {
csre_cs_detected_h result;
- toException(this->m_loader->scanFile(c, file->getPath(), &result));
+ toException(this->m_loader.scanFile(c, file->getPath(), &result));
if (!result)
continue;
auto candidate = convert(result, file->getPath());
candidate.isApp = true;
candidate.pkgId = pkgId;
- this->m_db->insertDetectedMalware(candidate, this->m_dataVersion);
+ this->m_db.insertDetectedMalware(candidate, this->m_dataVersion);
if (!riskiest)
riskiest.reset(new CsDetected(std::move(candidate)));
*riskiest = std::move(candidate);
}
- this->m_db->insertLastScanTime(pkgPath, starttime, this->m_dataVersion);
+ this->m_db.insertLastScanTime(pkgPath, starttime, this->m_dataVersion);
if (riskiest) {
INFO("Riskiest malware selected in pkg: " << pkgPath);
return this->scanAppOnCloud(context, pkgPath, pkgId);
auto riskiest = this->scanAppDelta(pkgPath, pkgId);
- auto history = this->m_db->getDetectedMalware(pkgPath);
+ auto history = this->m_db.getDetectedMalware(pkgPath);
if (riskiest && history) {
if (*riskiest > *history) {
INFO("new malware found and more risky! history should be updated.");
- this->m_db->insertDetectedMalware(*riskiest, this->m_dataVersion);
+ this->m_db.insertDetectedMalware(*riskiest, this->m_dataVersion);
} else {
INFO("new malware is found but not riskier than history. history reusable.");
history->response = history->isIgnored
- ? CSR_CS_IGNORE : this->getUserResponse(context, *history);
+ ? CSR_CS_IGNORE : this->getUserResponse(context, *history);
return this->handleUserResponse(*history);
}
} else if (riskiest && !history) {
INFO("new malware found and no history exist! history should be inserted.");
- this->m_db->insertDetectedMalware(*riskiest, this->m_dataVersion);
+ this->m_db.insertDetectedMalware(*riskiest, this->m_dataVersion);
} else if (!riskiest && history) {
INFO("no malware found and history exist! history reusable.");
history->response = history->isIgnored
- ? CSR_CS_IGNORE : this->getUserResponse(context, *history);
+ ? CSR_CS_IGNORE : this->getUserResponse(context, *history);
return this->handleUserResponse(*history);
} else {
INFO("no malware found and no history exist! it's clean!");
RawBuffer CsLogic::scanFileWithoutDelta(const CsContext &context,
const std::string &filepath, FilePtr &&fileptr)
{
- CsEngineContext engineContext(*m_loader);
+ CsEngineContext engineContext(this->m_loader);
auto &c = engineContext.get();
csre_cs_detected_h result;
- toException(m_loader->scanFile(c, filepath, &result));
+ toException(this->m_loader.scanFile(c, filepath, &result));
// detected handle is null if it's safe
if (result == nullptr)
auto d = convert(result, filepath);
- m_db->insertDetectedMalware(d, m_dataVersion);
+ this->m_db.insertDetectedMalware(d, this->m_dataVersion);
d.response = getUserResponse(context, d);
return handleUserResponse(d, std::forward<FilePtr>(fileptr));
DEBUG("Scan request on file: " << filepath);
- auto history = m_db->getDetectedMalware(filepath);
+ auto history = this->m_db.getDetectedMalware(filepath);
FilePtr fileptr;
// OR there's no history at all.
if (fileptr) {
if (history)
- m_db->deleteDetectedMalware(filepath);
+ this->m_db.deleteDetectedMalware(filepath);
if (fileptr->isDir())
ThrowExc(FileSystemError, "file type shouldn't be directory: " << filepath);
DEBUG("Usable scan history exist on file: " << filepath);
history->response = history->isIgnored
- ? CSR_CS_IGNORE : getUserResponse(context, *history);
+ ? CSR_CS_IGNORE : getUserResponse(context, *history);
return handleUserResponse(*history);
EXCEPTION_GUARD_CLOSER(ret)
{
EXCEPTION_GUARD_START
- auto lastScanTime = m_db->getLastScanTime(dir, m_dataVersion);
+ auto lastScanTime = this->m_db.getLastScanTime(dir, this->m_dataVersion);
auto visitor = FsVisitor::create(dir, lastScanTime);
if (lastScanTime != -1) {
// for case: scan history exist and not modified.
- for (auto &row : m_db->getDetectedMalwares(dir)) {
+ for (auto &row : this->m_db.getDetectedMalwares(dir)) {
try {
auto fileptr = File::create(row->targetName);
+
if (fileptr->isInApp())
fileset.insert(fileptr->getAppPkgPath());
else
fileset.insert(fileptr->getPath());
} catch (const FileDoNotExist &) {
- m_db->deleteDetectedMalware(row->targetName);
+ this->m_db.deleteDetectedMalware(row->targetName);
} catch (const FileSystemError &) {
- m_db->deleteDetectedMalware(row->targetName);
+ this->m_db.deleteDetectedMalware(row->targetName);
}
}
}
// to set scan time early is safe because file which is modified between
// scan start time and end time will be traversed by FsVisitor and re-scanned
// being compared to start time as modified since.
- m_db->insertLastScanTime(dir, time(nullptr), m_dataVersion);
+ this->m_db.insertLastScanTime(dir, time(nullptr), this->m_dataVersion);
return BinaryQueue::Serialize(CSR_ERROR_NONE, fileset).pop();
{
EXCEPTION_GUARD_START
- auto history = m_db->getDetectedMalware(filepath);
+ auto history = this->m_db.getDetectedMalware(filepath);
if (!history) {
ERROR("Target to be judged doesn't exist in db. name: " << filepath);
if (fileptr) {
ERROR("Target modified since db delta inserted. name: " << filepath);
- m_db->deleteDetectedMalware(filepath);
+ this->m_db.deleteDetectedMalware(filepath);
// TODO: is it okay to just refresh db and return success?
return BinaryQueue::Serialize(CSR_ERROR_NONE).pop();
"in same path: " << filepath);
}
- m_db->deleteDetectedMalware(filepath);
+ this->m_db.deleteDetectedMalware(filepath);
break;
case CSR_CS_ACTION_IGNORE:
- m_db->setDetectedMalwareIgnored(filepath, true);
+ this->m_db.setDetectedMalwareIgnored(filepath, true);
break;
case CSR_CS_ACTION_UNIGNORE:
- m_db->setDetectedMalwareIgnored(filepath, false);
+ this->m_db.setDetectedMalwareIgnored(filepath, false);
break;
default:
{
EXCEPTION_GUARD_START
- auto row = m_db->getDetectedMalware(filepath);
+ auto row = this->m_db.getDetectedMalware(filepath);
if (row)
return BinaryQueue::Serialize(CSR_ERROR_NONE, row).pop();
Db::RowShPtrs rows;
std::for_each(dirSet.begin(), dirSet.end(),
[this, &rows](const std::string & dir) {
- for (auto &row : m_db->getDetectedMalwares(dir))
+ for (auto &row : this->m_db.getDetectedMalwares(dir))
rows.emplace_back(std::move(row));
});
{
EXCEPTION_GUARD_START
- auto row = m_db->getDetectedMalware(filepath);
+ auto row = this->m_db.getDetectedMalware(filepath);
if (row && row->isIgnored)
return BinaryQueue::Serialize(CSR_ERROR_NONE, row).pop();
Db::RowShPtrs rows;
std::for_each(dirSet.begin(), dirSet.end(),
[this, &rows](const std::string & dir) {
- for (auto &row : m_db->getDetectedMalwares(dir))
+ for (auto &row : this->m_db.getDetectedMalwares(dir))
if (row->isIgnored)
rows.emplace_back(std::move(row));
});
{
switch (d.response) {
case CSR_CS_IGNORE:
- m_db->setDetectedMalwareIgnored(d.targetName, true);
+ this->m_db.setDetectedMalwareIgnored(d.targetName, true);
break;
case CSR_CS_REMOVE:
WARN("File type is changed. it's considered as different file: " << d.targetName);
}
- m_db->deleteDetectedMalware(d.targetName);
+ this->m_db.deleteDetectedMalware(d.targetName);
break;
case CSR_CS_SKIP:
csre_cs_severity_level_e eseverity = CSRE_CS_SEVERITY_LOW;
- toException(m_loader->getSeverity(result, &eseverity));
- toException(m_loader->getMalwareName(result, d.malwareName));
- toException(m_loader->getDetailedUrl(result, d.detailedUrl));
- toException(m_loader->getTimestamp(result, &d.ts));
+ toException(this->m_loader.getSeverity(result, &eseverity));
+ toException(this->m_loader.getMalwareName(result, d.malwareName));
+ toException(this->m_loader.getDetailedUrl(result, d.detailedUrl));
+ toException(this->m_loader.getTimestamp(result, &d.ts));
d.severity = Csr::convert(eseverity);
#pragma once
#include <string>
-#include <memory>
#include "common/types.h"
#include "common/cs-context.h"
class CsLogic : public Logic {
public:
- CsLogic();
+ CsLogic(CsLoader &loader, Db::Manager &db);
virtual ~CsLogic();
RawBuffer scanData(const CsContext &context, const RawBuffer &data);
static csr_cs_user_response_e getUserResponse(const CsContext &,
const CsDetected &);
- std::unique_ptr<CsLoader> m_loader;
- std::unique_ptr<Db::Manager> m_db;
+ CsLoader &m_loader;
+ Db::Manager &m_db;
std::string m_dataVersion;
};
namespace Csr {
-// TODO: make loaders to singleton for not being loaded multiple time
-// make db manager to singleton
-EmLogic::EmLogic() :
- m_cs(new CsLoader(CS_ENGINE_PATH)),
- m_wp(new WpLoader(WP_ENGINE_PATH)),
- m_db(new Db::Manager(RW_DBSPACE "/.csr.db", RO_DBSPACE))
+EmLogic::EmLogic(CsLoader &cs, WpLoader &wp, Db::Manager &db) :
+ m_cs(cs), m_wp(wp), m_db(db)
{
- // TODO: Provide engine-specific res/working dirs
- toException(m_cs->globalInit(SAMPLE_ENGINE_RO_RES_DIR,
- SAMPLE_ENGINE_RW_WORKING_DIR));
-
- toException(m_wp->globalInit(SAMPLE_ENGINE_RO_RES_DIR,
- SAMPLE_ENGINE_RW_WORKING_DIR));
}
EmLogic::~EmLogic()
{
- try {
- toException(m_cs->globalDeinit());
- toException(m_wp->globalDeinit());
- } catch (const Exception &e) {
- ERROR("ignore all custom exceptions in logic dtor: " << e.error() <<
- " " << e.what());
- }
}
-// TODO: make parent class of cs-loader and wp-loader for
-// using engine-manager related engine APIs samely
RawBuffer EmLogic::getEngineName(const EmContext &context)
{
EXCEPTION_GUARD_START
if (context.engineId == CSR_ENGINE_CS) {
- CsEngineInfo engineInfo(*m_cs);
+ CsEngineInfo engineInfo(this->m_cs);
auto &c = engineInfo.get();
std::string value;
- toException(m_cs->getEngineName(c, value));
+ toException(this->m_cs.getEngineName(c, value));
EmString emString;
emString.value = std::move(value);
return BinaryQueue::Serialize(CSR_ERROR_NONE, emString).pop();
} else {
- WpEngineInfo engineInfo(*m_wp);
+ WpEngineInfo engineInfo(this->m_wp);
auto &c = engineInfo.get();
std::string value;
- toException(m_wp->getEngineName(c, value));
+ toException(this->m_wp.getEngineName(c, value));
EmString emString;
emString.value = std::move(value);
EXCEPTION_GUARD_START
if (context.engineId == CSR_ENGINE_CS) {
- CsEngineInfo engineInfo(*m_cs);
+ CsEngineInfo engineInfo(this->m_cs);
auto &c = engineInfo.get();
std::string value;
- toException(m_cs->getEngineVendor(c, value));
+ toException(this->m_cs.getEngineVendor(c, value));
EmString emString;
emString.value = std::move(value);
return BinaryQueue::Serialize(CSR_ERROR_NONE, emString).pop();
} else {
- WpEngineInfo engineInfo(*m_wp);
+ WpEngineInfo engineInfo(this->m_wp);
auto &c = engineInfo.get();
std::string value;
- toException(m_wp->getEngineVendor(c, value));
+ toException(this->m_wp.getEngineVendor(c, value));
EmString emString;
emString.value = std::move(value);
EXCEPTION_GUARD_START
if (context.engineId == CSR_ENGINE_CS) {
- CsEngineInfo engineInfo(*m_cs);
+ CsEngineInfo engineInfo(this->m_cs);
auto &c = engineInfo.get();
std::string value;
- toException(m_cs->getEngineVersion(c, value));
+ toException(this->m_cs.getEngineVersion(c, value));
EmString emString;
emString.value = std::move(value);
return BinaryQueue::Serialize(CSR_ERROR_NONE, emString).pop();
} else {
- WpEngineInfo engineInfo(*m_wp);
+ WpEngineInfo engineInfo(this->m_wp);
auto &c = engineInfo.get();
std::string value;
- toException(m_wp->getEngineVersion(c, value));
+ toException(this->m_wp.getEngineVersion(c, value));
EmString emString;
emString.value = std::move(value);
EXCEPTION_GUARD_START
if (context.engineId == CSR_ENGINE_CS) {
- CsEngineInfo engineInfo(*m_cs);
+ CsEngineInfo engineInfo(this->m_cs);
auto &c = engineInfo.get();
std::string value;
- toException(m_cs->getEngineDataVersion(c, value));
+ toException(this->m_cs.getEngineDataVersion(c, value));
EmString emString;
emString.value = std::move(value);
return BinaryQueue::Serialize(CSR_ERROR_NONE, emString).pop();
} else {
- WpEngineInfo engineInfo(*m_wp);
+ WpEngineInfo engineInfo(this->m_wp);
auto &c = engineInfo.get();
std::string value;
- toException(m_wp->getEngineDataVersion(c, value));
+ toException(this->m_wp.getEngineDataVersion(c, value));
EmString emString;
emString.value = std::move(value);
EXCEPTION_GUARD_START
if (context.engineId == CSR_ENGINE_CS) {
- CsEngineInfo engineInfo(*m_cs);
+ CsEngineInfo engineInfo(this->m_cs);
auto &c = engineInfo.get();
time_t value;
- toException(m_cs->getEngineLatestUpdateTime(c, &value));
+ toException(this->m_cs.getEngineLatestUpdateTime(c, &value));
return BinaryQueue::Serialize(CSR_ERROR_NONE, value).pop();
} else {
- WpEngineInfo engineInfo(*m_wp);
+ WpEngineInfo engineInfo(this->m_wp);
auto &c = engineInfo.get();
time_t value;
- toException(m_wp->getEngineLatestUpdateTime(c, &value));
+ toException(this->m_wp.getEngineLatestUpdateTime(c, &value));
return BinaryQueue::Serialize(CSR_ERROR_NONE, value).pop();
}
csr_activated_e activated = CSR_NOT_ACTIVATED;
if (context.engineId == CSR_ENGINE_CS) {
- CsEngineInfo engineInfo(*m_cs);
+ CsEngineInfo engineInfo(this->m_cs);
auto &c = engineInfo.get();
csre_cs_activated_e value;
- toException(m_cs->getEngineActivated(c, &value));
+ toException(this->m_cs.getEngineActivated(c, &value));
if (value == CSRE_CS_ACTIVATED)
activated = CSR_ACTIVATED;
ThrowExc(EngineError, "Invalid returned activated val: " <<
static_cast<int>(value));
} else {
- WpEngineInfo engineInfo(*m_wp);
+ WpEngineInfo engineInfo(this->m_wp);
auto &c = engineInfo.get();
csre_wp_activated_e value;
- toException(m_wp->getEngineActivated(c, &value));
+ toException(this->m_wp.getEngineActivated(c, &value));
if (value == CSRE_WP_ACTIVATED)
activated = CSR_ACTIVATED;
{
EXCEPTION_GUARD_START
- auto state = m_db->getEngineState(static_cast<int>(context.engineId));
+ auto state = this->m_db.getEngineState(static_cast<int>(context.engineId));
if (state == -1)
ThrowExc(DbFailed, "No engine state exist...");
{
EXCEPTION_GUARD_START
- m_db->setEngineState(static_cast<int>(context.engineId), static_cast<int>(state));
+ this->m_db.setEngineState(static_cast<int>(context.engineId), static_cast<int>(state));
return BinaryQueue::Serialize(CSR_ERROR_NONE).pop();
class EmLogic : public Logic {
public:
- EmLogic();
+ EmLogic(CsLoader &cs, WpLoader &wp, Db::Manager &db);
virtual ~EmLogic();
RawBuffer getEngineName(const EmContext &context);
RawBuffer setEngineState(const EmContext &context, csr_state_e state);
private:
- std::unique_ptr<CsLoader> m_cs;
- std::unique_ptr<WpLoader> m_wp;
- std::unique_ptr<Db::Manager> m_db;
+ CsLoader &m_cs;
+ WpLoader &m_wp;
+ Db::Manager &m_db;
};
}
}
-ServerService::ServerService() : Service(), m_workqueue(2, 10)
+ServerService::ServerService() :
+ Service(),
+ m_workqueue(2, 10),
+ m_cs(new CsLoader(CS_ENGINE_PATH, SAMPLE_ENGINE_RO_RES_DIR, SAMPLE_ENGINE_RW_WORKING_DIR)),
+ m_wp(new WpLoader(WP_ENGINE_PATH, SAMPLE_ENGINE_RO_RES_DIR, SAMPLE_ENGINE_RW_WORKING_DIR)),
+ m_db(new Db::Manager(RW_DBSPACE "/.csr.db", RO_DBSPACE)),
+ m_cslogic(*m_cs, *m_db),
+ m_wplogic(*m_wp),
+ m_emlogic(*m_cs, *m_wp, *m_db)
{
this->add(SockId::CS);
this->add(SockId::WP);
*/
#pragma once
+#include <memory>
+
#include "common/service.h"
#include "common/types.h"
#include "service/thread-pool.h"
#include "service/cs-logic.h"
#include "service/wp-logic.h"
#include "service/em-logic.h"
+#include "service/cs-loader.h"
+#include "service/wp-loader.h"
+#include "db/manager.h"
namespace Csr {
ThreadPool m_workqueue;
+ std::unique_ptr<CsLoader> m_cs;
+ std::unique_ptr<WpLoader> m_wp;
+ std::unique_ptr<Db::Manager> m_db;
+
CsLogic m_cslogic;
WpLogic m_wplogic;
EmLogic m_emlogic;
+
};
}
} // namespace anonymous
-int WpLoader::globalInit(const std::string &ro_res_dir,
- const std::string &rw_working_dir)
-{
- if (ro_res_dir.empty() || rw_working_dir.empty())
- throw std::invalid_argument("wp loader global init");
-
- return m_pc.fpGlobalInit(ro_res_dir.c_str(), rw_working_dir.c_str());
-}
-
-int WpLoader::globalDeinit()
-{
- return m_pc.fpGlobalDeinit();
-}
-
int WpLoader::contextCreate(csre_wp_context_h &c)
{
- return m_pc.fpContextCreate(&c);
+ return this->m_pc.fpContextCreate(&c);
}
int WpLoader::contextDestroy(csre_wp_context_h c)
if (c == nullptr)
throw std::invalid_argument("wp loader context destroy");
- return m_pc.fpContextDestroy(c);
+ return this->m_pc.fpContextDestroy(c);
}
int WpLoader::checkUrl(csre_wp_context_h c, const std::string &url,
if (c == nullptr || url.empty() || presult == nullptr)
throw std::invalid_argument("wp loader check url error");
- return m_pc.fpCheckUrl(c, (const char *)url.c_str(), presult);
+ return this->m_pc.fpCheckUrl(c, (const char *)url.c_str(), presult);
}
int WpLoader::getRiskLevel(csre_wp_check_result_h r,
if (r == nullptr || plevel == nullptr)
throw std::invalid_argument("wp loader Get Risk Level error");
- return m_pc.fpGetRiskLevel(r, plevel);
+ return this->m_pc.fpGetRiskLevel(r, plevel);
}
int WpLoader::getDetailedUrl(csre_wp_check_result_h r, std::string &value)
throw std::invalid_argument("wp loader get detailed url error");
return getValueCstr(value, [&](const char **cvalue) {
- return m_pc.fpGetDetailedUrl(r, cvalue);
+ return this->m_pc.fpGetDetailedUrl(r, cvalue);
});
}
int WpLoader::getErrorString(int ec, std::string &value)
{
return getValueCstr(value, [&](const char **cvalue) {
- return m_pc.fpGetErrorString(ec, cvalue);
+ return this->m_pc.fpGetErrorString(ec, cvalue);
});
}
int WpLoader::getEngineInfo(csre_wp_engine_h &e)
{
- return m_pc.fpGetEngineInfo(&e);
+ return this->m_pc.fpGetEngineInfo(&e);
}
int WpLoader::destroyEngine(csre_wp_engine_h e)
{
- return m_pc.fpDestroyEngine(e);
+ return this->m_pc.fpDestroyEngine(e);
}
int WpLoader::getEngineApiVersion(csre_wp_engine_h e, std::string &value)
throw std::invalid_argument("wp loader get error string");
return getValueCstr(value, [&](const char **cvalue) {
- return m_pc.fpGetEngineApiVersion(e, cvalue);
+ return this->m_pc.fpGetEngineApiVersion(e, cvalue);
});
}
throw std::invalid_argument("wp loader get engine vendor");
return getValueCstr(value, [&](const char **cvalue) {
- return m_pc.fpGetEngineVendor(e, cvalue);
+ return this->m_pc.fpGetEngineVendor(e, cvalue);
});
}
throw std::invalid_argument("wp loader get engine name");
return getValueCstr(value, [&](const char **cvalue) {
- return m_pc.fpGetEngineName(e, cvalue);
+ return this->m_pc.fpGetEngineName(e, cvalue);
});
}
throw std::invalid_argument("wp loader get engine version");
return getValueCstr(value, [&](const char **cvalue) {
- return m_pc.fpGetEngineVersion(e, cvalue);
+ return this->m_pc.fpGetEngineVersion(e, cvalue);
});
}
throw std::invalid_argument("wp loader get engine version");
return getValueCstr(value, [&](const char **cvalue) {
- return m_pc.fpGetEngineDataVersion(e, cvalue);
+ return this->m_pc.fpGetEngineDataVersion(e, cvalue);
});
}
if (e == nullptr || ptime == nullptr)
throw std::invalid_argument("wp loader get latest update time");
- return m_pc.fpGetEngineLatestUpdateTime(e, ptime);
+ return this->m_pc.fpGetEngineLatestUpdateTime(e, ptime);
}
int WpLoader::getEngineActivated(csre_wp_engine_h e,
if (e == nullptr || pactivated == nullptr)
throw std::invalid_argument("wp loader get engine activated");
- return m_pc.fpGetEngineActivated(e, pactivated);
+ return this->m_pc.fpGetEngineActivated(e, pactivated);
}
int WpLoader::getEngineVendorLogo(csre_wp_engine_h e,
unsigned char *cvalue = nullptr;
unsigned int size = 0;
- auto retval = m_pc.fpGetEngineVendorLogo(e, &cvalue, &size);
+ auto retval = this->m_pc.fpGetEngineVendorLogo(e, &cvalue, &size);
if (retval == CSRE_ERROR_NONE && cvalue != nullptr && size != 0)
value.assign(cvalue, cvalue + size);
return retval;
}
-WpLoader::WpLoader(const std::string &enginePath)
+void WpLoader::init(const std::string &enginePath, const std::string &roResDir,
+ const std::string &rwWorkingDir)
{
- INFO("Load wp-Engine plugin start. engine path: " << enginePath);
+ if (enginePath.empty() || roResDir.empty() || rwWorkingDir.empty())
+ ThrowExc(InternalError, "empty string comes in to loader init");
void *handle = dlopen(enginePath.c_str(), RTLD_LAZY);
ThrowExc(InternalError, "engine dlopen error. path: " << enginePath <<
" errno: " << errno);
- m_pc.dlhandle = handle;
-
- m_pc.fpGlobalInit = reinterpret_cast<FpGlobalInit>(dlsym(handle,
- "csre_wp_global_initialize"));
- m_pc.fpGlobalDeinit = reinterpret_cast<FpGlobalDeinit>(dlsym(handle,
- "csre_wp_global_deinitialize"));
- m_pc.fpContextCreate = reinterpret_cast<FpContextCreate>(dlsym(handle,
- "csre_wp_context_create"));
- m_pc.fpContextDestroy = reinterpret_cast<FpContextDestroy>(dlsym(handle,
- "csre_wp_context_destroy"));
- m_pc.fpCheckUrl = reinterpret_cast<FpCheckUrl>(dlsym(handle,
- "csre_wp_check_url"));
- m_pc.fpGetRiskLevel = reinterpret_cast<FpGetRiskLevel>(dlsym(handle,
- "csre_wp_result_get_risk_level"));
- m_pc.fpGetDetailedUrl = reinterpret_cast<FPGetDetailedUrl>(dlsym(handle,
- "csre_wp_result_get_detailed_url"));
- m_pc.fpGetErrorString = reinterpret_cast <FpGetErrorString>(dlsym(handle,
- "csre_wp_get_error_string"));
- m_pc.fpGetEngineInfo = reinterpret_cast<FpGetEngineInfo>(dlsym(handle,
- "csre_wp_engine_get_info"));
- m_pc.fpDestroyEngine = reinterpret_cast<FpDestroyEngine>(dlsym(handle,
- "csre_wp_engine_destroy"));
- m_pc.fpGetEngineApiVersion = reinterpret_cast<FpGetEngineApiVersion>(dlsym(
- handle, "csre_wp_engine_get_api_version"));
- m_pc.fpGetEngineVendor = reinterpret_cast<FpGetEngineVendor>(dlsym(handle,
- "csre_wp_engine_get_vendor"));
- m_pc.fpGetEngineName = reinterpret_cast<FpGetEngineName>(dlsym(handle,
- "csre_wp_engine_get_name"));
- m_pc.fpGetEngineVersion = reinterpret_cast<FpGetEngineVersion>(dlsym(handle,
- "csre_wp_engine_get_version"));
- m_pc.fpGetEngineDataVersion = reinterpret_cast<FpGetEngineDataVersion>(dlsym(
- handle, "csre_wp_engine_get_data_version"));
- m_pc.fpGetEngineLatestUpdateTime =
+ this->m_pc.dlhandle = handle;
+
+ this->m_pc.fpGlobalInit = reinterpret_cast<FpGlobalInit>(dlsym(handle,
+ "csre_wp_global_initialize"));
+ this->m_pc.fpGlobalDeinit = reinterpret_cast<FpGlobalDeinit>(dlsym(handle,
+ "csre_wp_global_deinitialize"));
+ this->m_pc.fpContextCreate = reinterpret_cast<FpContextCreate>(dlsym(handle,
+ "csre_wp_context_create"));
+ this->m_pc.fpContextDestroy = reinterpret_cast<FpContextDestroy>(dlsym(handle,
+ "csre_wp_context_destroy"));
+ this->m_pc.fpCheckUrl = reinterpret_cast<FpCheckUrl>(dlsym(handle,
+ "csre_wp_check_url"));
+ this->m_pc.fpGetRiskLevel = reinterpret_cast<FpGetRiskLevel>(dlsym(handle,
+ "csre_wp_result_get_risk_level"));
+ this->m_pc.fpGetDetailedUrl = reinterpret_cast<FPGetDetailedUrl>(dlsym(handle,
+ "csre_wp_result_get_detailed_url"));
+ this->m_pc.fpGetErrorString = reinterpret_cast <FpGetErrorString>(dlsym(handle,
+ "csre_wp_get_error_string"));
+ this->m_pc.fpGetEngineInfo = reinterpret_cast<FpGetEngineInfo>(dlsym(handle,
+ "csre_wp_engine_get_info"));
+ this->m_pc.fpDestroyEngine = reinterpret_cast<FpDestroyEngine>(dlsym(handle,
+ "csre_wp_engine_destroy"));
+ this->m_pc.fpGetEngineApiVersion = reinterpret_cast<FpGetEngineApiVersion>(dlsym(
+ handle, "csre_wp_engine_get_api_version"));
+ this->m_pc.fpGetEngineVendor = reinterpret_cast<FpGetEngineVendor>(dlsym(handle,
+ "csre_wp_engine_get_vendor"));
+ this->m_pc.fpGetEngineName = reinterpret_cast<FpGetEngineName>(dlsym(handle,
+ "csre_wp_engine_get_name"));
+ this->m_pc.fpGetEngineVersion = reinterpret_cast<FpGetEngineVersion>(dlsym(handle,
+ "csre_wp_engine_get_version"));
+ this->m_pc.fpGetEngineDataVersion = reinterpret_cast<FpGetEngineDataVersion>(dlsym(
+ handle, "csre_wp_engine_get_data_version"));
+ this->m_pc.fpGetEngineLatestUpdateTime =
reinterpret_cast<FpGetEngineLatestUpdateTime>(dlsym(handle,
"csre_wp_engine_get_latest_update_time"));
- m_pc.fpGetEngineActivated = reinterpret_cast<FpGetEngineActivated>(dlsym(handle,
- "csre_wp_engine_get_activated"));
- m_pc.fpGetEngineVendorLogo = reinterpret_cast<FpGetEngineVendorLogo>(dlsym(
- handle, "csre_wp_engine_get_vendor_logo"));
-
- if (m_pc.fpGlobalInit == nullptr || m_pc.fpGlobalDeinit == nullptr ||
- m_pc.fpContextCreate == nullptr || m_pc.fpContextDestroy == nullptr ||
- m_pc.fpCheckUrl == nullptr || m_pc.fpGetRiskLevel == nullptr ||
- m_pc.fpGetDetailedUrl == nullptr || m_pc.fpGetErrorString == nullptr ||
- m_pc.fpGetEngineInfo == nullptr || m_pc.fpDestroyEngine == nullptr ||
- m_pc.fpGetEngineApiVersion == nullptr || m_pc.fpGetEngineVendor == nullptr ||
- m_pc.fpGetEngineName == nullptr || m_pc.fpGetEngineVersion == nullptr ||
- m_pc.fpGetEngineDataVersion == nullptr ||
- m_pc.fpGetEngineLatestUpdateTime == nullptr ||
- m_pc.fpGetEngineActivated == nullptr || m_pc.fpGetEngineVendorLogo == nullptr) {
- dlclose(handle);
+ this->m_pc.fpGetEngineActivated = reinterpret_cast<FpGetEngineActivated>(dlsym(handle,
+ "csre_wp_engine_get_activated"));
+ this->m_pc.fpGetEngineVendorLogo = reinterpret_cast<FpGetEngineVendorLogo>(dlsym(
+ handle, "csre_wp_engine_get_vendor_logo"));
+
+ if (this->m_pc.fpGlobalInit == nullptr || this->m_pc.fpGlobalDeinit == nullptr ||
+ this->m_pc.fpContextCreate == nullptr || this->m_pc.fpContextDestroy == nullptr ||
+ this->m_pc.fpCheckUrl == nullptr || this->m_pc.fpGetRiskLevel == nullptr ||
+ this->m_pc.fpGetDetailedUrl == nullptr || this->m_pc.fpGetErrorString == nullptr ||
+ this->m_pc.fpGetEngineInfo == nullptr || this->m_pc.fpDestroyEngine == nullptr ||
+ this->m_pc.fpGetEngineApiVersion == nullptr || this->m_pc.fpGetEngineVendor == nullptr ||
+ this->m_pc.fpGetEngineName == nullptr || this->m_pc.fpGetEngineVersion == nullptr ||
+ this->m_pc.fpGetEngineDataVersion == nullptr ||
+ this->m_pc.fpGetEngineLatestUpdateTime == nullptr ||
+ this->m_pc.fpGetEngineActivated == nullptr || this->m_pc.fpGetEngineVendorLogo == nullptr) {
+ dlclose(this->m_pc.dlhandle);
+ this->m_pc.dlhandle = nullptr;
ThrowExc(EngineError, "Failed to load funcs from engine library. "
"engine path: " << enginePath << "errno: " << errno);
}
+
+ auto ret = this->m_pc.fpGlobalInit(roResDir.c_str(), rwWorkingDir.c_str());
+
+ if (ret != CSRE_ERROR_NONE) {
+ dlclose(this->m_pc.dlhandle);
+ this->m_pc.dlhandle = nullptr;
+ toException(ret);
+ }
+}
+
+WpLoader::WpLoader(const std::string &enginePath, const std::string &roResDir,
+ const std::string &rwWorkingDir)
+{
+ this->init(enginePath, roResDir, rwWorkingDir);
}
WpLoader::~WpLoader()
{
- dlclose(m_pc.dlhandle);
+ // ignore return value
+ this->m_pc.fpGlobalDeinit();
+
+ if (this->m_pc.dlhandle) {
+ dlclose(this->m_pc.dlhandle);
+ this->m_pc.dlhandle = nullptr;
+ }
}
WpEngineContext::WpEngineContext(WpLoader &loader) :
m_loader(loader), m_context(nullptr)
{
- toException(m_loader.contextCreate(m_context));
+ toException(this->m_loader.contextCreate(this->m_context));
}
WpEngineContext::~WpEngineContext()
{
- toException(m_loader.contextDestroy(m_context));
+ toException(this->m_loader.contextDestroy(this->m_context));
}
csre_wp_context_h &WpEngineContext::get(void)
{
- return m_context;
+ return this->m_context;
}
WpEngineInfo::WpEngineInfo(WpLoader &loader) :
m_loader(loader), m_info(nullptr)
{
- toException(m_loader.getEngineInfo(m_info));
+ toException(this->m_loader.getEngineInfo(this->m_info));
}
WpEngineInfo::~WpEngineInfo()
{
- toException(m_loader.destroyEngine(m_info));
+ toException(this->m_loader.destroyEngine(this->m_info));
}
csre_wp_engine_h &WpEngineInfo::get(void)
{
- return m_info;
+ return this->m_info;
}
}
class WpLoader {
public:
- WpLoader(const std::string &);
+ WpLoader(const std::string &enginePath, const std::string &roResDir,
+ const std::string &rwWorkingDir);
virtual ~WpLoader();
- int globalInit(const std::string &, const std::string &);
- int globalDeinit();
+
+ void reset(const std::string &enginePath, const std::string &roResDir,
+ const std::string &rwWorkingDir);
+
int contextCreate(csre_wp_context_h &);
int contextDestroy(csre_wp_context_h);
int checkUrl(csre_wp_context_h handle, const std::string &,
FpGetEngineVendorLogo fpGetEngineVendorLogo;
};
+ void init(const std::string &, const std::string &, const std::string &);
+
PluginContext m_pc;
};
namespace Csr {
-WpLogic::WpLogic() : m_loader(new WpLoader(WP_ENGINE_PATH))
+WpLogic::WpLogic(WpLoader &loader) : m_loader(loader)
{
- // TODO: Provide engine-specific res/working dirs
- toException(m_loader->globalInit(SAMPLE_ENGINE_RO_RES_DIR,
- SAMPLE_ENGINE_RW_WORKING_DIR));
-
- WpEngineInfo wpEngineInfo(*m_loader);
- toException(m_loader->getEngineDataVersion(wpEngineInfo.get(), m_dataVersion));
+ WpEngineInfo wpEngineInfo(this->m_loader);
+ toException(this->m_loader.getEngineDataVersion(wpEngineInfo.get(),
+ this->m_dataVersion));
}
WpLogic::~WpLogic()
{
- try {
- toException(m_loader->globalDeinit());
- } catch (const Exception &e) {
- ERROR("ignore all custom exceptions in logic dtor: " << e.error() <<
- " " << e.what());
- }
}
RawBuffer WpLogic::checkUrl(const WpContext &context, const std::string &url)
{
EXCEPTION_GUARD_START
- WpEngineContext engineContext(*m_loader);
+ WpEngineContext engineContext(this->m_loader);
auto &c = engineContext.get();
csre_wp_check_result_h result;
- toException(m_loader->checkUrl(c, url.c_str(), &result));
+ toException(this->m_loader.checkUrl(c, url.c_str(), &result));
auto wr = convert(result);
EXCEPTION_GUARD_END
}
-csr_wp_user_response_e WpLogic::getUserResponse(const WpContext &c,
- const std::string &url,
- const WpResult &wr)
+csr_wp_user_response_e WpLogic::getUserResponse(const WpContext &c, const std::string &url,
+ const WpResult &wr)
{
if (c.askUser == CSR_WP_NOT_ASK_USER)
return CSR_WP_NO_ASK_USER;
WpResult wr;
csre_wp_risk_level_e elevel;
- toException(m_loader->getDetailedUrl(r, wr.detailedUrl));
- toException(m_loader->getRiskLevel(r, &elevel));
+ toException(this->m_loader.getDetailedUrl(r, wr.detailedUrl));
+ toException(this->m_loader.getRiskLevel(r, &elevel));
wr.riskLevel = Csr::convert(elevel);
return wr;
#pragma once
#include <string>
-#include <memory>
#include "common/types.h"
#include "common/wp-context.h"
class WpLogic : public Logic {
public:
- WpLogic();
+ WpLogic(WpLoader &loader);
virtual ~WpLogic();
RawBuffer checkUrl(const WpContext &context, const std::string &url);
WpResult convert(csre_wp_check_result_h &result);
static csr_wp_user_response_e getUserResponse(const WpContext &context,
- const std::string &url,
- const WpResult &result);
-
- std::unique_ptr<WpLoader> m_loader;
+ const std::string &url,
+ const WpResult &result);
+ WpLoader &m_loader;
std::string m_dataVersion;
};
std::string malware_name;
ASSERT_IF(loader.getMalwareName(detected, malware_name), CSRE_ERROR_NONE);
+
if (expected_malware_name != nullptr)
ASSERT_IF(malware_name, expected_malware_name);
std::string detailed_url;
ASSERT_IF(loader.getDetailedUrl(detected, detailed_url), CSRE_ERROR_NONE);
+
if (expected_detailed_url != nullptr)
ASSERT_IF(detailed_url, expected_detailed_url);
time_t timestamp;
ASSERT_IF(loader.getTimestamp(detected, ×tamp), CSRE_ERROR_NONE);
+
if (expected_timestamp != 0)
ASSERT_IF(timestamp, expected_timestamp);
template <>
struct Handle<csre_cs_context_h> {
- Handle() : loader(SAMPLE_ENGINE_DIR "/libcsr-cs-engine.so")
+ Handle() :
+ loader(SAMPLE_ENGINE_DIR "/libcsr-cs-engine.so",
+ SAMPLE_ENGINE_RO_RES_DIR,
+ SAMPLE_ENGINE_RW_WORKING_DIR)
{
- ASSERT_IF(
- loader.globalInit(SAMPLE_ENGINE_RO_RES_DIR, SAMPLE_ENGINE_RW_WORKING_DIR),
- CSRE_ERROR_NONE);
ASSERT_IF(loader.contextCreate(context), CSRE_ERROR_NONE);
}
~Handle()
{
ASSERT_IF(loader.contextDestroy(context), CSRE_ERROR_NONE);
- ASSERT_IF(loader.globalDeinit(), CSRE_ERROR_NONE);
}
Csr::CsLoader loader;
template <>
struct Handle<csre_cs_engine_h> {
- Handle() : loader(SAMPLE_ENGINE_DIR "/libcsr-cs-engine.so")
+ Handle() :
+ loader(SAMPLE_ENGINE_DIR "/libcsr-cs-engine.so",
+ SAMPLE_ENGINE_RO_RES_DIR,
+ SAMPLE_ENGINE_RW_WORKING_DIR)
{
- ASSERT_IF(
- loader.globalInit(SAMPLE_ENGINE_RO_RES_DIR, SAMPLE_ENGINE_RW_WORKING_DIR),
- CSRE_ERROR_NONE);
ASSERT_IF(loader.getEngineInfo(context), CSRE_ERROR_NONE);
}
~Handle()
{
ASSERT_IF(loader.destroyEngine(context), CSRE_ERROR_NONE);
- ASSERT_IF(loader.globalDeinit(), CSRE_ERROR_NONE);
}
Csr::CsLoader loader;
template <>
struct Handle<csre_wp_context_h> {
- Handle() : loader(SAMPLE_ENGINE_DIR "/libcsr-wp-engine.so")
+ Handle() :
+ loader(SAMPLE_ENGINE_DIR "/libcsr-wp-engine.so",
+ SAMPLE_ENGINE_RO_RES_DIR,
+ SAMPLE_ENGINE_RW_WORKING_DIR)
{
- ASSERT_IF(
- loader.globalInit(SAMPLE_ENGINE_RO_RES_DIR, SAMPLE_ENGINE_RW_WORKING_DIR),
- CSRE_ERROR_NONE);
ASSERT_IF(loader.contextCreate(context), CSRE_ERROR_NONE);
}
~Handle()
{
ASSERT_IF(loader.contextDestroy(context), CSRE_ERROR_NONE);
- ASSERT_IF(loader.globalDeinit(), CSRE_ERROR_NONE);
}
Csr::WpLoader loader;
template <>
struct Handle<csre_wp_engine_h> {
- Handle() : loader(SAMPLE_ENGINE_DIR "/libcsr-wp-engine.so")
+ Handle() :
+ loader(SAMPLE_ENGINE_DIR "/libcsr-wp-engine.so",
+ SAMPLE_ENGINE_RO_RES_DIR,
+ SAMPLE_ENGINE_RW_WORKING_DIR)
{
- ASSERT_IF(
- loader.globalInit(SAMPLE_ENGINE_RO_RES_DIR, SAMPLE_ENGINE_RW_WORKING_DIR),
- CSRE_ERROR_NONE);
ASSERT_IF(loader.getEngineInfo(context), CSRE_ERROR_NONE);
}
~Handle()
{
ASSERT_IF(loader.destroyEngine(context), CSRE_ERROR_NONE);
- ASSERT_IF(loader.globalDeinit(), CSRE_ERROR_NONE);
}
Csr::WpLoader loader;