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"));
+ 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,
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"));
+ handle, "csre_cs_engine_get_data_version"));
this->m_pc.fpGetEngineLatestUpdateTime =
reinterpret_cast<FpGetEngineLatestUpdateTime>(dlsym(handle,
"csre_cs_engine_get_latest_update_time"));
- this->m_pc.fpGetEngineActivated = reinterpret_cast<FpGetEngineActivated>(dlsym(handle,
- "csre_cs_engine_get_activated"));
+ 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"));
+ handle, "csre_cs_engine_get_vendor_logo"));
if (this->m_pc.fpGlobalInit == nullptr || this->m_pc.fpGlobalDeinit == nullptr ||
this->m_pc.fpContextCreate == nullptr ||
}
}
-void CsLoader::reset(const std::string &enginePath, const std::string &roResDir,
- const std::string &rwWorkingDir)
+void CsLoader::deinit()
{
// ignore return value
this->m_pc.fpGlobalDeinit();
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,
CsLoader::~CsLoader()
{
- // ignore return value
- this->m_pc.fpGlobalDeinit();
-
- if (this->m_pc.dlhandle) {
- dlclose(this->m_pc.dlhandle);
- this->m_pc.dlhandle = nullptr;
- }
+ this->deinit();
}
-CsEngineContext::CsEngineContext(CsLoader &loader) : m_loader(loader), m_context(nullptr)
+
+CsEngineContext::CsEngineContext(const std::shared_ptr<CsLoader> &loader) :
+ m_loader(loader), m_context(nullptr)
{
- toException(this->m_loader.contextCreate(this->m_context));
+ if (!this->m_loader)
+ ThrowExc(EngineNotExist, "null loader means engine not exist!");
+
+ toException(this->m_loader->contextCreate(this->m_context));
}
CsEngineContext::~CsEngineContext()
{
- toException(this->m_loader.contextDestroy(this->m_context));
+ toException(this->m_loader->contextDestroy(this->m_context));
}
csre_cs_context_h &CsEngineContext::get(void)
return this->m_context;
}
-CsEngineInfo::CsEngineInfo(CsLoader &loader) : m_loader(loader), m_info(nullptr)
+
+CsEngineInfo::CsEngineInfo(const std::shared_ptr<CsLoader> &loader) :
+ m_loader(loader), m_info(nullptr)
{
- toException(this->m_loader.getEngineInfo(this->m_info));
+ if (!this->m_loader)
+ ThrowExc(EngineNotExist, "null loader means engine not exist!");
+
+ toException(this->m_loader->getEngineInfo(this->m_info));
}
CsEngineInfo::~CsEngineInfo()
{
- toException(this->m_loader.destroyEngine(this->m_info));
+ toException(this->m_loader->destroyEngine(this->m_info));
}
csre_cs_engine_h &CsEngineInfo::get(void)
*/
#pragma once
-#include <ctime>
+#include <memory>
#include <vector>
#include <string>
+#include <ctime>
#include <csre-content-screening-types.h>
#include <csre-content-screening-engine-info.h>
+#include "service/iloader.h"
+
namespace Csr {
-class CsLoader {
+class CsLoader : public ILoader {
public:
CsLoader(const std::string &enginePath, const std::string &roResDir,
const std::string &rwWorkingDir);
virtual ~CsLoader();
- 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> &,
csre_cs_detected_h *);
int scanFile(csre_cs_context_h, const std::string &, csre_cs_detected_h *);
- int scanAppOnCloud(csre_cs_context_h, const std::string &,
- csre_cs_detected_h *);
+ int scanAppOnCloud(csre_cs_context_h, const std::string &, csre_cs_detected_h *);
int getSeverity(csre_cs_detected_h, csre_cs_severity_level_e *);
int getMalwareName(csre_cs_detected_h, std::string &);
csre_cs_detected_h *);
using FpScanFile = int(*)(csre_cs_context_h, const char *,
csre_cs_detected_h *);
- using FpScanAppOnCloud = int(*)(csre_cs_context_h, const char *,
- csre_cs_detected_h *);
+ using FpScanAppOnCloud = int(*)(csre_cs_context_h, const char *, csre_cs_detected_h *);
using FpGetSeverity = int(*)(csre_cs_detected_h, csre_cs_severity_level_e *);
using FpGetMalwareName = int(*)(csre_cs_detected_h, const char **);
using FpGetDetailedUrl = int(*)(csre_cs_detected_h, const char **);
FpGetEngineVendorLogo fpGetEngineVendorLogo;
};
- void init(const std::string &, const std::string &, const std::string &);
+ virtual void init(const std::string &, const std::string &, const std::string &) override;
+ virtual void deinit(void) override;
PluginContext m_pc;
};
class CsEngineContext {
public:
- CsEngineContext(CsLoader &);
+ CsEngineContext(const std::shared_ptr<CsLoader> &);
~CsEngineContext();
csre_cs_context_h &get(void);
private:
- CsLoader &m_loader;
+ std::shared_ptr<CsLoader> m_loader;
csre_cs_context_h m_context;
};
class CsEngineInfo {
public:
- CsEngineInfo(CsLoader &);
+ CsEngineInfo(const std::shared_ptr<CsLoader> &);
~CsEngineInfo();
csre_cs_engine_h &get(void);
private:
- CsLoader &m_loader;
+ std::shared_ptr<CsLoader> m_loader;
csre_cs_engine_h m_info;
};
} // namespace anonymous
-CsLogic::CsLogic(CsLoader &loader, Db::Manager &db) : m_loader(loader), m_db(db)
+CsLogic::CsLogic(const std::shared_ptr<CsLoader> &loader,
+ const std::shared_ptr<Db::Manager> &db) :
+ m_loader(loader), m_db(db)
{
- CsEngineInfo csEngineInfo(this->m_loader);
- toException(this->m_loader.getEngineDataVersion(csEngineInfo.get(),
- this->m_dataVersion));
+ if (!this->m_db)
+ ThrowExc(DbFailed, "Failed to init db");
+
+ if (this->m_loader) {
+ CsEngineInfo csEngineInfo(this->m_loader);
+ toException(this->m_loader->getEngineDataVersion(csEngineInfo.get(),
+ this->m_dataVersion));
+ }
}
RawBuffer CsLogic::scanData(const CsContext &context, const RawBuffer &data)
{
EXCEPTION_GUARD_START
- if (this->m_db.getEngineState(CSR_ENGINE_CS) != CSR_STATE_ENABLE)
+ if (this->m_db->getEngineState(CSR_ENGINE_CS) != CSR_STATE_ENABLE)
ThrowExc(EngineDisabled, "engine is disabled");
setCoreUsage(context.coreUsage);
auto timestamp = ::time(nullptr);
- toException(this->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)
auto timestamp = ::time(nullptr);
csre_cs_detected_h result;
- toException(this->m_loader.scanAppOnCloud(c, pkgPath, &result));
+ toException(this->m_loader->scanAppOnCloud(c, pkgPath, &result));
if (!result)
return BinaryQueue::Serialize(CSR_ERROR_NONE).pop();
CsDetectedPtr CsLogic::scanAppDelta(const std::string &pkgPath, const std::string &pkgId,
std::string &riskiestPath)
{
- auto starttime = time(nullptr);
+ auto starttime = ::time(nullptr);
CsEngineContext engineContext(this->m_loader);
auto &c = engineContext.get();
- auto lastScanTime = this->m_db.getLastScanTime(pkgPath, this->m_dataVersion);
+ auto lastScanTime = this->m_db->getLastScanTime(pkgPath, this->m_dataVersion);
// traverse files in app and take which is more danger than riskiest
auto visitor = FsVisitor::create(pkgPath, lastScanTime);
auto timestamp = ::time(nullptr);
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) {
if (lastScanTime != -1)
- this->m_db.deleteDetectedByFilepathOnPath(file->getPath());
+ this->m_db->deleteDetectedByFilepathOnPath(file->getPath());
continue;
}
candidate.isApp = true;
candidate.pkgId = pkgId;
- this->m_db.insertName(pkgPath);
- this->m_db.insertDetected(candidate, file->getPath(), this->m_dataVersion);
+ this->m_db->insertName(pkgPath);
+ this->m_db->insertDetected(candidate, file->getPath(), this->m_dataVersion);
if (!riskiest) {
riskiest.reset(new CsDetected(std::move(candidate)));
}
}
- this->m_db.insertLastScanTime(pkgPath, starttime, this->m_dataVersion);
+ this->m_db->insertLastScanTime(pkgPath, starttime, this->m_dataVersion);
return riskiest;
}
return this->scanAppOnCloud(context, pkgPath, pkgId);
// old history
- auto history = this->m_db.getWorstByPkgId(pkgId);
+ auto history = this->m_db->getWorstByPkgId(pkgId);
// riskiest detected among newly scanned files
std::string riskiestPath;
auto riskiest = this->scanAppDelta(pkgPath, pkgId, riskiestPath);
// history after delta scan. if worst file is changed, it's rescanned in scanAppDelta
// and deleted from db if it's cured. if history != nullptr && after == nullptr,
// it means worst detected item is cured anyway.
- auto after = this->m_db.getWorstByPkgId(pkgId);
+ auto after = this->m_db->getWorstByPkgId(pkgId);
if (history && after && riskiest) {
if (*history < *riskiest) {
INFO("worst case is remained but the more worst newly detected. on pkg[" <<
pkgPath << "]");
if (history->isIgnored)
- this->m_db.updateIgnoreFlag(pkgPath, false);
+ this->m_db->updateIgnoreFlag(pkgPath, false);
- this->m_db.insertWorst(pkgId, pkgPath, riskiestPath);
+ this->m_db->insertWorst(pkgId, pkgPath, riskiestPath);
return this->handleAskUser(context, *riskiest);
} else {
"worse case in db and compare it with riskiest first. on pkg[" << pkgPath <<
"]");
Db::RowShPtr worse;
- for (auto &row : this->m_db.getDetectedByFilepathOnDir(pkgPath))
+ for (auto &row : this->m_db->getDetectedByFilepathOnDir(pkgPath))
if (!worse || *worse < *row)
worse = std::move(row);
INFO("worst case is deleted but the more worst newly detected. on pkg[" <<
pkgPath << "]");
if (history->isIgnored)
- this->m_db.updateIgnoreFlag(pkgPath, false);
+ this->m_db->updateIgnoreFlag(pkgPath, false);
- this->m_db.insertWorst(pkgId, pkgPath, riskiestPath);
+ this->m_db->insertWorst(pkgId, pkgPath, riskiestPath);
return this->handleAskUser(context, *riskiest);
} else {
INFO("worst case is deleted but same or less level newly detected. on pkg[" <<
pkgPath << "]");
- this->m_db.insertWorst(pkgId, pkgPath, riskiestPath);
+ this->m_db->insertWorst(pkgId, pkgPath, riskiestPath);
if (history->isIgnored)
return BinaryQueue::Serialize(CSR_ERROR_NONE).pop();
return this->handleAskUser(context, *riskiest);
}
} else if (history && !after && !riskiest) {
- auto rows = this->m_db.getDetectedByFilepathOnDir(pkgPath);
+ auto rows = this->m_db->getDetectedByFilepathOnDir(pkgPath);
if (!rows.empty()) {
INFO("worst case is deleted cascadingly and NO new detected and "
worse = std::move(row);
if (worse) {
- this->m_db.insertWorst(pkgId, pkgPath, worse->fileInAppPath);
+ this->m_db->insertWorst(pkgId, pkgPath, worse->fileInAppPath);
if (worse->isIgnored)
return BinaryQueue::Serialize(CSR_ERROR_NONE).pop();
INFO("worst case is deleted cascadingly and NO new detected and "
"NO worse case. the pkg[" << pkgPath << "] is clean.");
- this->m_db.deleteDetectedByNameOnPath(pkgPath);
+ this->m_db->deleteDetectedByNameOnPath(pkgPath);
return BinaryQueue::Serialize(CSR_ERROR_NONE).pop();
} else if (!history && riskiest) {
INFO("no history and new detected");
- this->m_db.insertWorst(pkgId, pkgPath, riskiestPath);
+ this->m_db->insertWorst(pkgId, pkgPath, riskiestPath);
return this->handleAskUser(context, *riskiest);
} else {
auto timestamp = ::time(nullptr);
csre_cs_detected_h result;
- toException(this->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 = this->convert(result, filepath, timestamp);
- this->m_db.insertName(d.targetName);
- this->m_db.insertDetected(d, d.targetName, this->m_dataVersion);
+ this->m_db->insertName(d.targetName);
+ this->m_db->insertDetected(d, d.targetName, this->m_dataVersion);
return this->handleAskUser(context, d, std::forward<FilePtr>(fileptr));
}
{
EXCEPTION_GUARD_START
- if (this->m_db.getEngineState(CSR_ENGINE_CS) != CSR_STATE_ENABLE)
+ if (this->m_db->getEngineState(CSR_ENGINE_CS) != CSR_STATE_ENABLE)
ThrowExc(EngineDisabled, "engine is disabled");
setCoreUsage(context.coreUsage);
DEBUG("Scan request on file: " << filepath);
- auto history = this->m_db.getDetectedByNameOnPath(filepath);
+ auto history = this->m_db->getDetectedByNameOnPath(filepath);
FilePtr fileptr;
// OR there's no history at all.
if (fileptr) {
if (history)
- this->m_db.deleteDetectedByNameOnPath(filepath);
+ this->m_db->deleteDetectedByNameOnPath(filepath);
if (fileptr->isDir())
ThrowExc(FileSystemError, "file type shouldn't be directory: " << filepath);
{
EXCEPTION_GUARD_START
- if (this->m_db.getEngineState(CSR_ENGINE_CS) != CSR_STATE_ENABLE)
+ if (this->m_db->getEngineState(CSR_ENGINE_CS) != CSR_STATE_ENABLE)
ThrowExc(EngineDisabled, "engine is disabled");
- auto lastScanTime = this->m_db.getLastScanTime(dir, this->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 : this->m_db.getDetectedByNameOnDir(File::getPkgPath(dir))) {
+ for (auto &row : this->m_db->getDetectedByNameOnDir(File::getPkgPath(dir))) {
try {
auto fileptr = File::create(row->targetName);
fileset.insert(fileptr->isInApp() ?
fileptr->getAppPkgPath() : fileptr->getPath());
} catch (const FileDoNotExist &) {
- this->m_db.deleteDetectedByNameOnPath(row->targetName);
+ this->m_db->deleteDetectedByNameOnPath(row->targetName);
} catch (const FileSystemError &) {
- this->m_db.deleteDetectedByNameOnPath(row->targetName);
+ this->m_db->deleteDetectedByNameOnPath(row->targetName);
}
}
}
{
EXCEPTION_GUARD_START
- if (this->m_db.getEngineState(CSR_ENGINE_CS) != CSR_STATE_ENABLE)
+ if (this->m_db->getEngineState(CSR_ENGINE_CS) != CSR_STATE_ENABLE)
ThrowExc(EngineDisabled, "engine is disabled");
StrSet canonicalized;
{
EXCEPTION_GUARD_START
- this->m_db.insertLastScanTime(dir, ts, this->m_dataVersion);
+ this->m_db->insertLastScanTime(dir, ts, this->m_dataVersion);
return BinaryQueue::Serialize(CSR_ERROR_NONE).pop();
const auto &targetName = (file->isInApp() ? file->getAppPkgPath() : filepath);
- auto history = this->m_db.getDetectedByNameOnPath(targetName);
+ auto history = this->m_db->getDetectedByNameOnPath(targetName);
if (!history) {
ERROR("Target to be judged doesn't exist in db. name: " << targetName);
case CSR_CS_ACTION_REMOVE:
file->remove();
- this->m_db.deleteDetectedByNameOnPath(targetName);
+ this->m_db->deleteDetectedByNameOnPath(targetName);
break;
case CSR_CS_ACTION_IGNORE:
- this->m_db.updateIgnoreFlag(targetName, true);
+ this->m_db->updateIgnoreFlag(targetName, true);
break;
case CSR_CS_ACTION_UNIGNORE:
- this->m_db.updateIgnoreFlag(targetName, false);
+ this->m_db->updateIgnoreFlag(targetName, false);
break;
default:
{
EXCEPTION_GUARD_START
- auto row = this->m_db.getDetectedByNameOnPath(File::getPkgPath(filepath));
+ auto row = this->m_db->getDetectedByNameOnPath(File::getPkgPath(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 : this->m_db.getDetectedByNameOnDir(File::getPkgPath(dir)))
+ for (auto &row : this->m_db->getDetectedByNameOnDir(File::getPkgPath(dir)))
if (!row->isIgnored)
rows.emplace_back(std::move(row));
});
{
EXCEPTION_GUARD_START
- auto row = this->m_db.getDetectedByNameOnPath(File::getPkgPath(filepath));
+ auto row = this->m_db->getDetectedByNameOnPath(File::getPkgPath(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 : this->m_db.getDetectedByNameOnDir(File::getPkgPath(dir)))
+ for (auto &row : this->m_db->getDetectedByNameOnDir(File::getPkgPath(dir)))
if (row->isIgnored)
rows.emplace_back(std::move(row));
});
WARN("File type is changed, considered as different file: " << d.targetName);
}
- this->m_db.deleteDetectedByNameOnPath(File::getPkgPath(d.targetName));
+ this->m_db->deleteDetectedByNameOnPath(File::getPkgPath(d.targetName));
}
return BinaryQueue::Serialize(CSR_ERROR_NONE, d).pop();
csre_cs_severity_level_e eseverity = CSRE_CS_SEVERITY_LOW;
- 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->getSeverity(result, &eseverity));
+ toException(this->m_loader->getMalwareName(result, d.malwareName));
+ toException(this->m_loader->getDetailedUrl(result, d.detailedUrl));
d.ts = timestamp;
d.severity = Csr::convert(eseverity);
*/
#pragma once
+#include <memory>
#include <string>
#include <ctime>
class CsLogic : public Logic {
public:
- CsLogic(CsLoader &loader, Db::Manager &db);
+ CsLogic(const std::shared_ptr<CsLoader> &loader,
+ const std::shared_ptr<Db::Manager> &db);
virtual ~CsLogic() = default;
RawBuffer scanData(const CsContext &context, const RawBuffer &data);
RawBuffer handleAskUser(const CsContext &c, CsDetected &d,
FilePtr &&fileptr = nullptr);
- CsLoader &m_loader;
- Db::Manager &m_db;
+ std::shared_ptr<CsLoader> m_loader;
+ std::shared_ptr<Db::Manager> m_db;
std::string m_dataVersion;
};
} // namespace anonymous
-EmLogic::EmLogic(CsLoader &cs, WpLoader &wp, Db::Manager &db) :
- m_cs(cs), m_wp(wp), m_db(db)
+EmLogic::EmLogic(const std::shared_ptr<CsLoader> &cs, const std::shared_ptr<WpLoader> &wp,
+ const std::shared_ptr<Db::Manager> &db) : m_cs(cs), m_wp(wp), m_db(db)
{
+ if (!this->m_db)
+ ThrowExc(DbFailed, "DB init failed");
}
RawBuffer EmLogic::getEngineName(const EmContext &context)
auto &c = engineInfo.get();
std::string value;
- toException(this->m_cs.getEngineName(c, value));
+ toException(this->m_cs->getEngineName(c, value));
return BinaryQueue::Serialize(CSR_ERROR_NONE, value).pop();
} else {
auto &c = engineInfo.get();
std::string value;
- toException(this->m_wp.getEngineName(c, value));
+ toException(this->m_wp->getEngineName(c, value));
return BinaryQueue::Serialize(CSR_ERROR_NONE, value).pop();
}
auto &c = engineInfo.get();
std::string value;
- toException(this->m_cs.getEngineVendor(c, value));
+ toException(this->m_cs->getEngineVendor(c, value));
return BinaryQueue::Serialize(CSR_ERROR_NONE, value).pop();
} else {
auto &c = engineInfo.get();
std::string value;
- toException(this->m_wp.getEngineVendor(c, value));
+ toException(this->m_wp->getEngineVendor(c, value));
return BinaryQueue::Serialize(CSR_ERROR_NONE, value).pop();
}
auto &c = engineInfo.get();
std::string value;
- toException(this->m_cs.getEngineVersion(c, value));
+ toException(this->m_cs->getEngineVersion(c, value));
return BinaryQueue::Serialize(CSR_ERROR_NONE, value).pop();
} else {
auto &c = engineInfo.get();
std::string value;
- toException(this->m_wp.getEngineVersion(c, value));
+ toException(this->m_wp->getEngineVersion(c, value));
return BinaryQueue::Serialize(CSR_ERROR_NONE, value).pop();
}
auto &c = engineInfo.get();
std::string value;
- toException(this->m_cs.getEngineDataVersion(c, value));
+ toException(this->m_cs->getEngineDataVersion(c, value));
return BinaryQueue::Serialize(CSR_ERROR_NONE, value).pop();
} else {
auto &c = engineInfo.get();
std::string value;
- toException(this->m_wp.getEngineDataVersion(c, value));
+ toException(this->m_wp->getEngineDataVersion(c, value));
return BinaryQueue::Serialize(CSR_ERROR_NONE, value).pop();
}
auto &c = engineInfo.get();
time_t value;
- toException(this->m_cs.getEngineLatestUpdateTime(c, &value));
+ toException(this->m_cs->getEngineLatestUpdateTime(c, &value));
int64_t ts64 = static_cast<int64_t>(value);
auto &c = engineInfo.get();
time_t value;
- toException(this->m_wp.getEngineLatestUpdateTime(c, &value));
+ toException(this->m_wp->getEngineLatestUpdateTime(c, &value));
int64_t ts64 = static_cast<int64_t>(value);
auto &c = engineInfo.get();
csre_cs_activated_e value;
- toException(this->m_cs.getEngineActivated(c, &value));
+ toException(this->m_cs->getEngineActivated(c, &value));
if (value == CSRE_CS_ACTIVATED)
activated = CSR_ACTIVATED;
auto &c = engineInfo.get();
csre_wp_activated_e value;
- toException(this->m_wp.getEngineActivated(c, &value));
+ toException(this->m_wp->getEngineActivated(c, &value));
if (value == CSRE_WP_ACTIVATED)
activated = CSR_ACTIVATED;
if (!_isValid(context.engineId))
ThrowExc(InternalError, "invalid engine id comes to get engine state.");
- auto state = this->m_db.getEngineState(context.engineId);
+ auto state = this->m_db->getEngineState(context.engineId);
return BinaryQueue::Serialize(CSR_ERROR_NONE,
static_cast<int>(state) == -1 ? static_cast<int>(CSR_STATE_DISABLE) :
if (!_isValid(context.engineId) || !_isValid(state))
ThrowExc(InternalError, "invalid argument comes to set engine state.");
- this->m_db.setEngineState(context.engineId, state);
+ this->m_db->setEngineState(context.engineId, state);
return BinaryQueue::Serialize(CSR_ERROR_NONE).pop();
class EmLogic : public Logic {
public:
- EmLogic(CsLoader &cs, WpLoader &wp, Db::Manager &db);
+ EmLogic(const std::shared_ptr<CsLoader> &cs, const std::shared_ptr<WpLoader> &wp,
+ const std::shared_ptr<Db::Manager> &db);
virtual ~EmLogic() = default;
RawBuffer getEngineName(const EmContext &context);
RawBuffer setEngineState(const EmContext &context, csr_state_e state);
private:
- CsLoader &m_cs;
- WpLoader &m_wp;
- Db::Manager &m_db;
+ std::shared_ptr<CsLoader> m_cs;
+ std::shared_ptr<WpLoader> m_wp;
+ std::shared_ptr<Db::Manager> m_db;
};
}
using DbFailed = DerivedException<CSR_ERROR_DB>;
using RemoveFailed = DerivedException<CSR_ERROR_REMOVE_FAILED>;
using FileChanged = DerivedException<CSR_ERROR_FILE_CHANGED>;
+using EngineNotExist = DerivedException<CSR_ERROR_ENGINE_NOT_EXIST>;
using EngineError = DerivedException<CSR_ERROR_ENGINE_INTERNAL>;
using EngineNotActivated = DerivedException<CSR_ERROR_ENGINE_NOT_ACTIVATED>;
using EngineDisabled = DerivedException<CSR_ERROR_ENGINE_DISABLED>;
--- /dev/null
+/*
+ * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+/*
+ * @file iloader.h
+ * @author Kyungwook Tak (k.tak@samsung.com)
+ * @version 1.0
+ * @brief engine loader abstract class
+ */
+#pragma once
+
+namespace Csr {
+
+class ILoader {
+public:
+ void reset(const std::string &enginePath, const std::string &roResDir,
+ const std::string &rwWorkingDir)
+ {
+ this->deinit();
+ this->init(enginePath, roResDir, rwWorkingDir);
+ }
+
+protected:
+ virtual ~ILoader() = default;
+
+ virtual void deinit() = 0;
+ virtual void init(const std::string &enginePath, const std::string &roResDir,
+ const std::string &rwWorkingDir) = 0;
+};
+
+}
ServerService::ServerService() :
Service(),
- m_workqueue(2, 10),
- m_cs(new CsLoader(CS_ENGINE_PATH, ENGINE_DIR, ENGINE_RW_WORKING_DIR)),
- m_wp(new WpLoader(WP_ENGINE_PATH, ENGINE_DIR, 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_db),
- m_emlogic(*m_cs, *m_wp, *m_db)
+ m_workqueue(2, 10)
{
+ this->m_db = std::make_shared<Db::Manager>(RW_DBSPACE "/.csr.db", RO_DBSPACE);
+
+ try {
+ this->m_cs = std::make_shared<CsLoader>(CS_ENGINE_PATH, ENGINE_DIR,
+ ENGINE_RW_WORKING_DIR);
+ } catch (const Exception &e) {
+ ERROR("Excetpion in content screening loader: " << e.what() <<
+ " error: " << e.error() << " treat it as ENGINE_NOT_EXIST.");
+ }
+
+ try {
+ this->m_wp = std::make_shared<WpLoader>(WP_ENGINE_PATH, ENGINE_DIR,
+ ENGINE_RW_WORKING_DIR);
+ } catch (const Exception &e) {
+ ERROR("Exception in web protection loader: " << e.what() <<
+ " error: " << e.error() << " treat it as ENGINE_NOT_EXIST.");
+ }
+
+ this->m_cslogic.reset(new CsLogic(this->m_cs, this->m_db));
+ this->m_wplogic.reset(new WpLogic(this->m_wp, this->m_db));
+ this->m_emlogic.reset(new EmLogic(this->m_cs, this->m_wp, this->m_db));
+
this->add(SockId::CS);
this->add(SockId::WP);
this->add(SockId::ADMIN);
{
EXCEPTION_GUARD_START
+ // need not to try resetting engine because the engine is pre-loaded (RO) and it
+ // cannot be fixed in dynamically except platform-development time.
+ if (!this->m_cs)
+ ThrowExc(EngineNotExist, "Content screening engine is not exist!");
+
BinaryQueue q;
q.push(data);
RawBuffer data;
q.Deserialize(cptr, data);
- return m_cslogic.scanData(*cptr, data);
+ return this->m_cslogic->scanData(*cptr, data);
}
case CommandId::SCAN_FILE: {
std::string filepath;
q.Deserialize(cptr, filepath);
- return m_cslogic.scanFile(*cptr, filepath);
+ return this->m_cslogic->scanFile(*cptr, filepath);
}
case CommandId::GET_SCANNABLE_FILES: {
std::string dir;
q.Deserialize(dir);
- return m_cslogic.getScannableFiles(dir);
+ return this->m_cslogic->getScannableFiles(dir);
}
case CommandId::CANONICALIZE_PATHS: {
StrSet paths;
q.Deserialize(paths);
- return m_cslogic.canonicalizePaths(paths);
+ return this->m_cslogic->canonicalizePaths(paths);
}
case CommandId::SET_DIR_TIMESTAMP: {
int64_t ts64 = 0;
q.Deserialize(dir, ts64);
- return m_cslogic.setDirTimestamp(dir, static_cast<time_t>(ts64));
+ return this->m_cslogic->setDirTimestamp(dir, static_cast<time_t>(ts64));
}
case CommandId::JUDGE_STATUS: {
int intAction;
q.Deserialize(filepath, intAction);
- return m_cslogic.judgeStatus(filepath, static_cast<csr_cs_action_e>(intAction));
+ return this->m_cslogic->judgeStatus(filepath,
+ static_cast<csr_cs_action_e>(intAction));
}
case CommandId::GET_DETECTED: {
std::string filepath;
q.Deserialize(filepath);
- return m_cslogic.getDetected(filepath);
+ return this->m_cslogic->getDetected(filepath);
}
case CommandId::GET_DETECTED_LIST: {
StrSet dirSet;
q.Deserialize(dirSet);
- return m_cslogic.getDetectedList(dirSet);
+ return this->m_cslogic->getDetectedList(dirSet);
}
case CommandId::GET_IGNORED: {
std::string filepath;
q.Deserialize(filepath);
- return m_cslogic.getIgnored(filepath);
+ return this->m_cslogic->getIgnored(filepath);
}
case CommandId::GET_IGNORED_LIST: {
StrSet dirSet;
q.Deserialize(dirSet);
- return m_cslogic.getIgnoredList(dirSet);
+ return this->m_cslogic->getIgnoredList(dirSet);
}
default:
{
EXCEPTION_GUARD_START
+ if (!this->m_wp)
+ ThrowExc(EngineNotExist, "Web protection engine is not exist!");
+
hasPermission(conn);
BinaryQueue q;
std::string url;
q.Deserialize(cptr, url);
- return m_wplogic.checkUrl(*cptr, url);
+ return this->m_wplogic->checkUrl(*cptr, url);
}
default:
{
EXCEPTION_GUARD_START
- hasPermission(conn);
-
BinaryQueue q;
q.push(data);
auto cid = extractCommandId(q);
+ EmContextShPtr cptr;
+ q.Deserialize(cptr);
+
+ if (!cptr)
+ ThrowExc(SocketError, "engine context for processAdmin should be exist!");
+
+ switch (cptr->engineId) {
+ case CSR_ENGINE_CS:
+ if (!this->m_cs)
+ ThrowExc(EngineNotExist, "Content screening engine is not exist!");
+ break;
+ case CSR_ENGINE_WP:
+ if (!this->m_wp)
+ ThrowExc(EngineNotExist, "Web protection engine is not exist!");
+ break;
+ default:
+ throw std::invalid_argument("context engine Id is invalid for processAdmin!");
+ }
+
+ hasPermission(conn);
+
INFO("Admin request process. command id: " << cidToString(cid));
switch (cid) {
case CommandId::EM_GET_NAME: {
- EmContextShPtr cptr;
- q.Deserialize(cptr);
-
- return m_emlogic.getEngineName(*cptr);
+ return this->m_emlogic->getEngineName(*cptr);
}
case CommandId::EM_GET_VENDOR: {
- EmContextShPtr cptr;
- q.Deserialize(cptr);
-
- return m_emlogic.getEngineVendor(*cptr);
+ return this->m_emlogic->getEngineVendor(*cptr);
}
case CommandId::EM_GET_VERSION: {
- EmContextShPtr cptr;
- q.Deserialize(cptr);
-
- return m_emlogic.getEngineVersion(*cptr);
+ return this->m_emlogic->getEngineVersion(*cptr);
}
case CommandId::EM_GET_DATA_VERSION: {
- EmContextShPtr cptr;
- q.Deserialize(cptr);
-
- return m_emlogic.getEngineDataVersion(*cptr);
+ return this->m_emlogic->getEngineDataVersion(*cptr);
}
case CommandId::EM_GET_UPDATED_TIME: {
- EmContextShPtr cptr;
- q.Deserialize(cptr);
-
- return m_emlogic.getEngineUpdatedTime(*cptr);
+ return this->m_emlogic->getEngineUpdatedTime(*cptr);
}
case CommandId::EM_GET_ACTIVATED: {
- EmContextShPtr cptr;
- q.Deserialize(cptr);
-
- return m_emlogic.getEngineActivated(*cptr);
+ return this->m_emlogic->getEngineActivated(*cptr);
}
case CommandId::EM_GET_STATE: {
- EmContextShPtr cptr;
- q.Deserialize(cptr);
-
- return m_emlogic.getEngineState(*cptr);
+ return this->m_emlogic->getEngineState(*cptr);
}
case CommandId::EM_SET_STATE: {
- EmContextShPtr cptr;
int intState;
- q.Deserialize(cptr, intState);
+ q.Deserialize(intState);
- return m_emlogic.setEngineState(*cptr, static_cast<csr_state_e>(intState));
+ return this->m_emlogic->setEngineState(*cptr, static_cast<csr_state_e>(intState));
}
default:
auto inbufPtr = std::make_shared<RawBuffer>(connection->receive());
- m_workqueue.submit([this, &connection, process, inbufPtr]() {
+ this->m_workqueue.submit([this, &connection, process, inbufPtr]() {
auto outbuf = (*process)(connection, *inbufPtr);
connection->send(outbuf);
ThreadPool m_workqueue;
- std::unique_ptr<CsLoader> m_cs;
- std::unique_ptr<WpLoader> m_wp;
- std::unique_ptr<Db::Manager> m_db;
+ std::shared_ptr<CsLoader> m_cs;
+ std::shared_ptr<WpLoader> m_wp;
+ std::shared_ptr<Db::Manager> m_db;
- CsLogic m_cslogic;
- WpLogic m_wplogic;
- EmLogic m_emlogic;
+ std::unique_ptr<CsLogic> m_cslogic;
+ std::unique_ptr<WpLogic> m_wplogic;
+ std::unique_ptr<EmLogic> m_emlogic;
};
if (enginePath.empty() || roResDir.empty() || rwWorkingDir.empty())
ThrowExc(InternalError, "empty string comes in to loader init");
+ INFO("Load WP-Engine plugin start. engine path: " << enginePath);
+
void *handle = dlopen(enginePath.c_str(), RTLD_LAZY);
if (handle == nullptr)
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"));
+ 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,
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"));
+ handle, "csre_wp_engine_get_data_version"));
this->m_pc.fpGetEngineLatestUpdateTime =
reinterpret_cast<FpGetEngineLatestUpdateTime>(dlsym(handle,
"csre_wp_engine_get_latest_update_time"));
- this->m_pc.fpGetEngineActivated = reinterpret_cast<FpGetEngineActivated>(dlsym(handle,
- "csre_wp_engine_get_activated"));
+ 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"));
+ 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.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.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) {
+ 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. "
}
}
-WpLoader::WpLoader(const std::string &enginePath, const std::string &roResDir,
- const std::string &rwWorkingDir)
-{
- this->init(enginePath, roResDir, rwWorkingDir);
-}
-
-WpLoader::~WpLoader()
+void WpLoader::deinit()
{
// ignore return value
this->m_pc.fpGlobalDeinit();
}
}
-WpEngineContext::WpEngineContext(WpLoader &loader) :
+WpLoader::WpLoader(const std::string &enginePath, const std::string &roResDir,
+ const std::string &rwWorkingDir)
+{
+ this->init(enginePath, roResDir, rwWorkingDir);
+}
+
+WpLoader::~WpLoader()
+{
+ this->deinit();
+}
+
+
+WpEngineContext::WpEngineContext(const std::shared_ptr<WpLoader> &loader) :
m_loader(loader), m_context(nullptr)
{
- toException(this->m_loader.contextCreate(this->m_context));
+ if (!this->m_loader)
+ ThrowExc(EngineNotExist, "null loader means engine not exist!");
+
+ toException(this->m_loader->contextCreate(this->m_context));
}
WpEngineContext::~WpEngineContext()
{
- toException(this->m_loader.contextDestroy(this->m_context));
+ toException(this->m_loader->contextDestroy(this->m_context));
}
csre_wp_context_h &WpEngineContext::get(void)
return this->m_context;
}
-WpEngineInfo::WpEngineInfo(WpLoader &loader) :
+
+WpEngineInfo::WpEngineInfo(const std::shared_ptr<WpLoader> &loader) :
m_loader(loader), m_info(nullptr)
{
- toException(this->m_loader.getEngineInfo(this->m_info));
+ if (!this->m_loader)
+ ThrowExc(EngineNotExist, "null loader means engine not exist!");
+
+ toException(this->m_loader->getEngineInfo(this->m_info));
}
WpEngineInfo::~WpEngineInfo()
{
- toException(this->m_loader.destroyEngine(this->m_info));
+ toException(this->m_loader->destroyEngine(this->m_info));
}
csre_wp_engine_h &WpEngineInfo::get(void)
*/
#pragma once
-#include <ctime>
+#include <memory>
#include <vector>
#include <string>
+#include <ctime>
#include <csre-web-protection-types.h>
#include <csre-web-protection-engine-info.h>
+#include "service/iloader.h"
+
namespace Csr {
-class WpLoader {
+class WpLoader : public ILoader {
public:
WpLoader(const std::string &enginePath, const std::string &roResDir,
const std::string &rwWorkingDir);
virtual ~WpLoader();
- 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 &);
+ virtual void init(const std::string &, const std::string &, const std::string &) override;
+ virtual void deinit(void) override;
PluginContext m_pc;
};
class WpEngineContext {
public:
- WpEngineContext(WpLoader &);
+ WpEngineContext(const std::shared_ptr<WpLoader> &);
~WpEngineContext();
csre_wp_context_h &get(void);
private:
- WpLoader &m_loader;
+ std::shared_ptr<WpLoader> m_loader;
csre_wp_context_h m_context;
};
class WpEngineInfo {
public:
- WpEngineInfo(WpLoader &);
+ WpEngineInfo(const std::shared_ptr<WpLoader> &);
~WpEngineInfo();
csre_wp_engine_h &get(void);
private:
- WpLoader &m_loader;
+ std::shared_ptr<WpLoader> m_loader;
csre_wp_engine_h m_info;
};
namespace Csr {
-WpLogic::WpLogic(WpLoader &loader, Db::Manager &db) : m_loader(loader), m_db(db)
+WpLogic::WpLogic(const std::shared_ptr<WpLoader> &loader,
+ const std::shared_ptr<Db::Manager> &db) :
+ m_loader(loader), m_db(db)
{
- WpEngineInfo wpEngineInfo(this->m_loader);
- toException(this->m_loader.getEngineDataVersion(wpEngineInfo.get(),
- this->m_dataVersion));
+ if (!this->m_db)
+ ThrowExc(DbFailed, "DB init failed.");
+
+ if (this->m_loader) {
+ WpEngineInfo wpEngineInfo(this->m_loader);
+ toException(this->m_loader->getEngineDataVersion(wpEngineInfo.get(),
+ this->m_dataVersion));
+ }
}
RawBuffer WpLogic::checkUrl(const WpContext &context, const std::string &url)
{
EXCEPTION_GUARD_START
- if (this->m_db.getEngineState(CSR_ENGINE_WP) != CSR_STATE_ENABLE)
+ if (this->m_db->getEngineState(CSR_ENGINE_WP) != CSR_STATE_ENABLE)
ThrowExc(EngineDisabled, "engine is disabled");
WpEngineContext engineContext(this->m_loader);
auto &c = engineContext.get();
csre_wp_check_result_h result;
- toException(this->m_loader.checkUrl(c, url.c_str(), &result));
+ toException(this->m_loader->checkUrl(c, url.c_str(), &result));
auto wr = convert(result);
WpResult wr;
csre_wp_risk_level_e elevel;
- toException(this->m_loader.getDetailedUrl(r, wr.detailedUrl));
- toException(this->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 <memory>
#include <string>
#include "common/types.h"
class WpLogic : public Logic {
public:
- WpLogic(WpLoader &loader, Db::Manager &db);
+ WpLogic(const std::shared_ptr<WpLoader> &loader,
+ const std::shared_ptr<Db::Manager> &db);
virtual ~WpLogic() = default;
RawBuffer checkUrl(const WpContext &context, const std::string &url);
const std::string &url,
const WpResult &result);
- WpLoader &m_loader;
- Db::Manager &m_db;
+ std::shared_ptr<WpLoader> m_loader;
+ std::shared_ptr<Db::Manager> m_db;
std::string m_dataVersion;
};
* @version 1.0
*/
#define BOOST_TEST_MODULE CSR_API_TEST
+
+#include <iostream>
+
#include <boost/test/unit_test.hpp>
#include <boost/test/unit_test_log.hpp>
#include <boost/test/results_reporter.hpp>
{
csr_engine_h handle;
auto ret = csr_get_current_engine(id, &handle);
- if (ret != CSR_ERROR_NONE)
+ if (ret == CSR_ERROR_ENGINE_NOT_EXIST) {
+ std::cerr << "Engine not exist! engine id: " << static_cast<int>(id) << std::endl;
+ return CSR_STATE_DISABLE;
+ } else if (ret != CSR_ERROR_NONE) {
throw std::logic_error("Failed to csr_get_current_engine.");
+ }
csr_state_e current;
ret = csr_engine_get_state(handle, ¤t);
- if (ret != CSR_ERROR_NONE)
+ if (ret == CSR_ERROR_ENGINE_NOT_EXIST) {
+ std::cerr << "Engine not exist! engine id: " << static_cast<int>(id) << std::endl;
+ return CSR_STATE_DISABLE;
+ } else if (ret != CSR_ERROR_NONE) {
throw std::logic_error("Failed to csr_get_state.");
+ }
if (current == state)
return current;