From: Dongsun Lee Date: Wed, 20 Apr 2016 11:09:08 +0000 (+0900) Subject: Add APIs for app information X-Git-Tag: accepted/tizen/common/20160614.143943^2~187 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=refs%2Fchanges%2F03%2F66703%2F9;p=platform%2Fupstream%2Fcsr-framework.git Add APIs for app information Change-Id: I609f31c28acec33076adfd0a901fc217c10bc20f Signed-off-by: Dongsun Lee --- diff --git a/engine/content-screening/sample-engine.cpp b/engine/content-screening/sample-engine.cpp index ced14db..57bd12f 100644 --- a/engine/content-screening/sample-engine.cpp +++ b/engine/content-screening/sample-engine.cpp @@ -29,6 +29,7 @@ #include #include #include +#include #include #include #include @@ -600,8 +601,12 @@ int csre_cs_engine_get_vendor_logo(csre_cs_engine_h engine, unsigned char **logo if (logo_image == nullptr || image_size == nullptr) return CSRE_ERROR_INVALID_PARAMETER; + auto s = eng->logoImage.size(); + if (s > UINT_MAX - 1) + return CSRET_CS_ERROR_FILE_IO; + + *image_size = reinterpret_cast(s); *logo_image = eng->logoImage.data(); - *image_size = eng->logoImage.size(); return CSRE_ERROR_NONE; } diff --git a/engine/web-protection/sample-engine.cpp b/engine/web-protection/sample-engine.cpp index 269ac40..c9fa7b9 100644 --- a/engine/web-protection/sample-engine.cpp +++ b/engine/web-protection/sample-engine.cpp @@ -29,6 +29,7 @@ #include #include #include +#include #include #include #include @@ -399,8 +400,12 @@ int csre_wp_engine_get_vendor_logo(csre_wp_engine_h engine, unsigned char **vend if (vendor_logo_image == nullptr || image_size == nullptr) return CSRE_ERROR_INVALID_PARAMETER; + auto s = eng->logoImage.size(); + if (s > UINT_MAX - 1) + return CSRET_WP_ERROR_FILE_IO; + + *image_size = reinterpret_cast(s); *vendor_logo_image = eng->logoImage.data(); - *image_size = eng->logoImage.size(); return CSRE_ERROR_NONE; } diff --git a/src/framework/client/content-screening.cpp b/src/framework/client/content-screening.cpp index 8a6ab4f..df08bb3 100644 --- a/src/framework/client/content-screening.cpp +++ b/src/framework/client/content-screening.cpp @@ -26,6 +26,7 @@ #include "client/utils.h" #include "client/handle-ext.h" #include "client/async-logic.h" +#include "common/raw-buffer.h" #include "common/cs-context.h" #include "common/cs-detected.h" #include "common/command-id.h" @@ -43,6 +44,7 @@ bool _isValid(const csr_cs_core_usage_e &value) case CSR_CS_USE_CORE_HALF: case CSR_CS_USE_CORE_SINGLE: return true; + default: return false; } @@ -54,15 +56,56 @@ bool _isValid(const csr_cs_ask_user_e &value) case CSR_CS_NOT_ASK_USER: case CSR_CS_ASK_USER: return true; + default: return false; } } +bool _isValid(const csr_cs_action_e &value) +{ + switch (value) { + case CSR_CS_ACTION_REMOVE: + case CSR_CS_ACTION_IGNORE: + case CSR_CS_ACTION_UNIGNORE: + return true; + + default: + return false; + } } +template +class CptrList { +public: + CptrList(std::vector &_l) : l(_l) {} + + ~CptrList() + { + for (auto &item : l) + delete item; + } + + T *pop(void) + { + if (l.empty()) + return nullptr; + + auto iter = l.begin(); + auto item = *iter; + l.erase(iter); + + return item; + } + +private: + std::vector &l; +}; + +} // end of namespace + API -int csr_cs_context_create(csr_cs_context_h* phandle) +int csr_cs_context_create(csr_cs_context_h *phandle) { EXCEPTION_SAFE_START @@ -70,7 +113,7 @@ int csr_cs_context_create(csr_cs_context_h* phandle) return CSR_ERROR_INVALID_PARAMETER; *phandle = reinterpret_cast( - new Client::HandleExt(std::shared_ptr(new CsContext()))); + new Client::HandleExt(std::shared_ptr(new CsContext()))); return CSR_ERROR_NONE; @@ -109,7 +152,7 @@ int csr_cs_set_ask_user(csr_cs_context_h handle, csr_cs_ask_user_e ask_user) } API -int csr_cs_set_popup_message(csr_cs_context_h handle, const char* message) +int csr_cs_set_popup_message(csr_cs_context_h handle, const char *message) { EXCEPTION_SAFE_START @@ -158,31 +201,55 @@ int csr_cs_set_scan_on_cloud(csr_cs_context_h handle) } API -int csr_cs_scan_data(csr_cs_context_h handle, const unsigned char *data, unsigned int length, csr_cs_detected_h *pdetected) +int csr_cs_scan_data(csr_cs_context_h handle, const unsigned char *data, + size_t length, csr_cs_detected_h *pdetected) { - (void) handle; - (void) data; - (void) length; - (void) pdetected; + EXCEPTION_SAFE_START + + if (handle == nullptr || pdetected == nullptr || data == nullptr) + return CSR_ERROR_INVALID_PARAMETER; + + auto hExt = reinterpret_cast(handle); + auto ret = hExt->dispatch>( + CommandId::SCAN_DATA, + hExt->getContext(), + RawBuffer(data, data + length)); + + if (ret.first != CSR_ERROR_NONE) { + ERROR("Error! ret: " << ret.first); + return ret.first; + } + + if (ret.second == nullptr) + return CSR_ERROR_UNKNOWN; // deserialization logic error + + if (ret.second->hasValue()) { + hExt->add(ret.second); + *pdetected = reinterpret_cast(ret.second); + } else { + *pdetected = nullptr; + } - DEBUG("start!"); return CSR_ERROR_NONE; + + EXCEPTION_SAFE_END } API -int csr_cs_scan_file(csr_cs_context_h handle, const char *file_path, csr_cs_detected_h *pdetected) +int csr_cs_scan_file(csr_cs_context_h handle, const char *file_path, + csr_cs_detected_h *pdetected) { EXCEPTION_SAFE_START if (handle == nullptr || pdetected == nullptr - || file_path == nullptr || file_path[0] == '\0') + || file_path == nullptr || file_path[0] == '\0') return CSR_ERROR_INVALID_PARAMETER; auto hExt = reinterpret_cast(handle); auto ret = hExt->dispatch>( - CommandId::SCAN_FILE, - hExt->getContext(), - std::string(file_path)); + CommandId::SCAN_FILE, + hExt->getContext(), + std::string(file_path)); if (ret.first != CSR_ERROR_NONE) { ERROR("Error! ret: " << ret.first); @@ -205,7 +272,8 @@ int csr_cs_scan_file(csr_cs_context_h handle, const char *file_path, csr_cs_dete } API -int csr_cs_set_callback_on_file_scanned(csr_cs_context_h handle, csr_cs_on_file_scanned_cb callback) +int csr_cs_set_callback_on_file_scanned(csr_cs_context_h handle, + csr_cs_on_file_scanned_cb callback) { EXCEPTION_SAFE_START @@ -222,7 +290,8 @@ int csr_cs_set_callback_on_file_scanned(csr_cs_context_h handle, csr_cs_on_file_ } API -int csr_cs_set_callback_on_detected(csr_cs_context_h handle, csr_cs_on_detected_cb callback) +int csr_cs_set_callback_on_detected(csr_cs_context_h handle, + csr_cs_on_detected_cb callback) { EXCEPTION_SAFE_START @@ -239,7 +308,8 @@ int csr_cs_set_callback_on_detected(csr_cs_context_h handle, csr_cs_on_detected_ } API -int csr_cs_set_callback_on_completed(csr_cs_context_h handle, csr_cs_on_completed_cb callback) +int csr_cs_set_callback_on_completed(csr_cs_context_h handle, + csr_cs_on_completed_cb callback) { EXCEPTION_SAFE_START @@ -256,7 +326,8 @@ int csr_cs_set_callback_on_completed(csr_cs_context_h handle, csr_cs_on_complete } API -int csr_cs_set_callback_on_cancelled(csr_cs_context_h handle, csr_cs_on_cancelled_cb callback) +int csr_cs_set_callback_on_cancelled(csr_cs_context_h handle, + csr_cs_on_cancelled_cb callback) { EXCEPTION_SAFE_START @@ -273,7 +344,8 @@ int csr_cs_set_callback_on_cancelled(csr_cs_context_h handle, csr_cs_on_cancelle } API -int csr_cs_set_callback_on_error(csr_cs_context_h handle, csr_cs_on_error_cb callback) +int csr_cs_set_callback_on_error(csr_cs_context_h handle, + csr_cs_on_error_cb callback) { EXCEPTION_SAFE_START @@ -290,7 +362,8 @@ int csr_cs_set_callback_on_error(csr_cs_context_h handle, csr_cs_on_error_cb cal } API -int csr_cs_scan_files_async(csr_cs_context_h handle, const char **file_paths, unsigned int count, void *user_data) +int csr_cs_scan_files_async(csr_cs_context_h handle, const char **file_paths, + size_t count, void *user_data) { EXCEPTION_SAFE_START @@ -300,7 +373,8 @@ int csr_cs_scan_files_async(csr_cs_context_h handle, const char **file_paths, un auto hExt = reinterpret_cast(handle); auto fileSet(std::make_shared()); - for (unsigned int i = 0; i < count; i++) { + + for (size_t i = 0; i < count; i++) { if (file_paths[i] == nullptr) return CSR_ERROR_INVALID_PARAMETER; @@ -309,7 +383,7 @@ int csr_cs_scan_files_async(csr_cs_context_h handle, const char **file_paths, un hExt->dispatchAsync([hExt, user_data, fileSet] { Client::AsyncLogic l(hExt->getContext(), hExt->m_cb, user_data, - [&hExt] { return hExt->isStopped(); }); + [&hExt] { return hExt->isStopped(); }); l.scanFiles(fileSet).second(); }); @@ -320,7 +394,8 @@ int csr_cs_scan_files_async(csr_cs_context_h handle, const char **file_paths, un } API -int csr_cs_scan_dir_async(csr_cs_context_h handle, const char *dir_path, void *user_data) +int csr_cs_scan_dir_async(csr_cs_context_h handle, const char *dir_path, + void *user_data) { EXCEPTION_SAFE_START @@ -331,7 +406,7 @@ int csr_cs_scan_dir_async(csr_cs_context_h handle, const char *dir_path, void *u hExt->dispatchAsync([hExt, user_data, dir_path] { Client::AsyncLogic l(hExt->getContext(), hExt->m_cb, user_data, - [&hExt] { return hExt->isStopped(); }); + [&hExt] { return hExt->isStopped(); }); l.scanDir(dir_path).second(); }); @@ -342,7 +417,8 @@ int csr_cs_scan_dir_async(csr_cs_context_h handle, const char *dir_path, void *u } API -int csr_cs_scan_dirs_async(csr_cs_context_h handle, const char **dir_paths, unsigned int count, void *user_data) +int csr_cs_scan_dirs_async(csr_cs_context_h handle, const char **dir_paths, + size_t count, void *user_data) { EXCEPTION_SAFE_START @@ -352,7 +428,8 @@ int csr_cs_scan_dirs_async(csr_cs_context_h handle, const char **dir_paths, unsi auto hExt = reinterpret_cast(handle); auto dirSet(std::make_shared()); - for (unsigned int i = 0; i < count; i++) { + + for (size_t i = 0; i < count; i++) { if (dir_paths[i] == nullptr) return CSR_ERROR_INVALID_PARAMETER; @@ -361,7 +438,7 @@ int csr_cs_scan_dirs_async(csr_cs_context_h handle, const char **dir_paths, unsi hExt->dispatchAsync([hExt, user_data, dirSet] { Client::AsyncLogic l(hExt->getContext(), hExt->m_cb, user_data, - [&hExt] { return hExt->isStopped(); }); + [&hExt] { return hExt->isStopped(); }); l.scanDirs(dirSet).second(); }); @@ -392,7 +469,8 @@ int csr_cs_scan_cancel(csr_cs_context_h handle) } API -int csr_cs_detected_get_severity(csr_cs_detected_h detected, csr_cs_severity_level_e* pseverity) +int csr_cs_detected_get_severity(csr_cs_detected_h detected, + csr_cs_severity_level_e *pseverity) { EXCEPTION_SAFE_START @@ -410,7 +488,8 @@ int csr_cs_detected_get_severity(csr_cs_detected_h detected, csr_cs_severity_lev } API -int csr_cs_detected_get_threat_type(csr_cs_detected_h detected, csr_cs_threat_type_e* pthreat_type) +int csr_cs_detected_get_threat_type(csr_cs_detected_h detected, + csr_cs_threat_type_e *pthreat_type) { EXCEPTION_SAFE_START @@ -428,7 +507,8 @@ int csr_cs_detected_get_threat_type(csr_cs_detected_h detected, csr_cs_threat_ty } API -int csr_cs_detected_get_malware_name(csr_cs_detected_h detected, const char** pmalware_name) +int csr_cs_detected_get_malware_name(csr_cs_detected_h detected, + const char **pmalware_name) { EXCEPTION_SAFE_START @@ -444,7 +524,8 @@ int csr_cs_detected_get_malware_name(csr_cs_detected_h detected, const char** pm } API -int csr_cs_detected_get_detailed_url(csr_cs_detected_h detected, const char** pdetailed_url) +int csr_cs_detected_get_detailed_url(csr_cs_detected_h detected, + const char **pdetailed_url) { EXCEPTION_SAFE_START @@ -460,7 +541,8 @@ int csr_cs_detected_get_detailed_url(csr_cs_detected_h detected, const char** pd } API -int csr_cs_detected_get_timestamp(csr_cs_detected_h detected, time_t* ptimestamp) +int csr_cs_detected_get_timestamp(csr_cs_detected_h detected, + time_t *ptimestamp) { EXCEPTION_SAFE_START @@ -476,7 +558,8 @@ int csr_cs_detected_get_timestamp(csr_cs_detected_h detected, time_t* ptimestamp } API -int csr_cs_detected_get_file_name(csr_cs_detected_h detected, const char** pfile_name) +int csr_cs_detected_get_file_name(csr_cs_detected_h detected, + const char **pfile_name) { EXCEPTION_SAFE_START @@ -492,7 +575,8 @@ int csr_cs_detected_get_file_name(csr_cs_detected_h detected, const char** pfile } API -int csr_cs_detected_get_user_response(csr_cs_detected_h detected, csr_cs_user_response_e* presponse) +int csr_cs_detected_get_user_response(csr_cs_detected_h detected, + csr_cs_user_response_e *presponse) { EXCEPTION_SAFE_START @@ -510,69 +594,243 @@ int csr_cs_detected_get_user_response(csr_cs_detected_h detected, csr_cs_user_re } API -int csr_cs_judge_detected_malware(csr_cs_context_h handle, const char *file_path, csr_cs_action_e action) +int csr_cs_detected_is_app(csr_cs_detected_h detected, bool *pis_app) { - (void) handle; - (void) file_path; - (void) action; + EXCEPTION_SAFE_START + + if (detected == nullptr || pis_app == nullptr) + return CSR_ERROR_INVALID_PARAMETER; + + reinterpret_cast(detected)->get( + static_cast(CsDetected::Key::IsApp), *pis_app); - DEBUG("start!"); return CSR_ERROR_NONE; + + EXCEPTION_SAFE_END } API -int csr_cs_get_detected_malware(csr_cs_context_h handle, const char *file_path, csr_cs_detected_h *pdetected) +int csr_cs_detected_get_pkg_id(csr_cs_detected_h detected, const char **ppkg_id) { - (void) handle; - (void) file_path; - (void) pdetected; + EXCEPTION_SAFE_START + + if (detected == nullptr || ppkg_id == nullptr) + return CSR_ERROR_INVALID_PARAMETER; + + reinterpret_cast(detected)->get( + static_cast(CsDetected::Key::PkgId), ppkg_id); - DEBUG("start!"); return CSR_ERROR_NONE; + + EXCEPTION_SAFE_END } API -int csr_cs_get_detected_malwares(csr_cs_context_h handle, const char *dir, csr_cs_detected_list_h *plist, int *pcount) +int csr_cs_judge_detected_malware(csr_cs_context_h handle, + csr_cs_detected_h detected, csr_cs_action_e action) { - (void) handle; - (void) dir; - (void) plist; - (void) pcount; + EXCEPTION_SAFE_START + + if (handle == nullptr || detected == nullptr || !_isValid(action)) + return CSR_ERROR_INVALID_PARAMETER; + + auto hExt = reinterpret_cast(handle); + const char *file_path = nullptr; + reinterpret_cast(detected)->get( + static_cast(CsDetected::Key::TargetName), &file_path); + auto ret = hExt->dispatch( + CommandId::JUDGE_STATUS, + hExt->getContext(), + std::string(file_path), + static_cast(action)); + + if (ret != CSR_ERROR_NONE) { + ERROR("Error! ret: " << ret); + return ret; + } - DEBUG("start!"); return CSR_ERROR_NONE; + + EXCEPTION_SAFE_END } API -int csr_cs_get_ignored_malware(csr_cs_context_h handle, const char *file_path, csr_cs_detected_h *pdetected) +int csr_cs_get_detected_malware(csr_cs_context_h handle, const char *file_path, + csr_cs_detected_h *pdetected) { - (void) handle; - (void) file_path; - (void) pdetected; + EXCEPTION_SAFE_START + + if (handle == nullptr || file_path == nullptr || pdetected == nullptr) + return CSR_ERROR_INVALID_PARAMETER; + + auto hExt = reinterpret_cast(handle); + auto ret = hExt->dispatch>( + CommandId::GET_DETECTED, + hExt->getContext(), + std::string(file_path)); + + if (ret.first != CSR_ERROR_NONE) { + ERROR("Error! ret: " << ret.first); + return ret.first; + } + + if (ret.second == nullptr) + return CSR_ERROR_UNKNOWN; // deserialization logic error + + if (ret.second->hasValue()) { + hExt->add(ret.second); + *pdetected = reinterpret_cast(ret.second); + } else { + *pdetected = nullptr; + delete ret.second; + } - DEBUG("start!"); return CSR_ERROR_NONE; + + EXCEPTION_SAFE_END +} + +API +int csr_cs_get_detected_malwares(csr_cs_context_h handle, const char *dir, + csr_cs_detected_list_h *plist, size_t *pcount) +{ + EXCEPTION_SAFE_START + + if (handle == nullptr || dir == nullptr + || plist == nullptr || pcount == nullptr) + return CSR_ERROR_INVALID_PARAMETER; + + auto hExt = reinterpret_cast(handle); + auto ret = hExt->dispatch>>( + CommandId::GET_DETECTED_LIST, + hExt->getContext(), + std::string(dir)); + + if (ret.first != CSR_ERROR_NONE) { + ERROR("Error! ret: " << ret.first); + return ret.first; + } + + if (ret.second.empty()) { + *plist = nullptr; + *pcount = 0; + return CSR_ERROR_NONE; + } + + CptrList cptrList(ret.second); + + ResultListPtr resultListPtr(new ResultList); + + while (auto dptr = cptrList.pop()) + resultListPtr->emplace_back(std::unique_ptr(dptr)); + + *plist = reinterpret_cast(resultListPtr.get()); + *pcount = resultListPtr->size(); + + hExt->add(std::move(resultListPtr)); + + return CSR_ERROR_NONE; + + EXCEPTION_SAFE_END +} + +API +int csr_cs_get_ignored_malware(csr_cs_context_h handle, const char *file_path, + csr_cs_detected_h *pdetected) +{ + EXCEPTION_SAFE_START + + if (handle == nullptr || file_path == nullptr || pdetected == nullptr) + return CSR_ERROR_INVALID_PARAMETER; + + auto hExt = reinterpret_cast(handle); + auto ret = hExt->dispatch>( + CommandId::GET_IGNORED, + hExt->getContext(), + std::string(file_path)); + + if (ret.first != CSR_ERROR_NONE) { + ERROR("Error! ret: " << ret.first); + return ret.first; + } + + if (ret.second == nullptr) + return CSR_ERROR_UNKNOWN; // deserialization logic error + + if (ret.second->hasValue()) { + hExt->add(ret.second); + *pdetected = reinterpret_cast(ret.second); + } else { + *pdetected = nullptr; + delete ret.second; + } + + return CSR_ERROR_NONE; + + EXCEPTION_SAFE_END } API -int csr_cs_get_ignored_malwares(csr_cs_context_h handle, const char *dir, csr_cs_detected_list_h *plist, int *pcount) +int csr_cs_get_ignored_malwares(csr_cs_context_h handle, const char *dir, + csr_cs_detected_list_h *plist, size_t *pcount) { - (void) handle; - (void) dir; - (void) plist; - (void) pcount; + EXCEPTION_SAFE_START + + if (handle == nullptr || dir == nullptr + || plist == nullptr || pcount == nullptr) + return CSR_ERROR_INVALID_PARAMETER; + + auto hExt = reinterpret_cast(handle); + auto ret = hExt->dispatch>>( + CommandId::GET_IGNORED_LIST, + hExt->getContext(), + std::string(dir)); + + if (ret.first != CSR_ERROR_NONE) { + ERROR("Error! ret: " << ret.first); + return ret.first; + } + + if (ret.second.empty()) { + *plist = nullptr; + *pcount = 0; + return CSR_ERROR_NONE; + } + + CptrList cptrList(ret.second); + + ResultListPtr resultListPtr(new ResultList); + + while (auto dptr = cptrList.pop()) + resultListPtr->emplace_back(std::unique_ptr(dptr)); + + *plist = reinterpret_cast(resultListPtr.get()); + *pcount = resultListPtr->size(); + + hExt->add(std::move(resultListPtr)); - DEBUG("start!"); return CSR_ERROR_NONE; + + EXCEPTION_SAFE_END } API -int csr_cs_dlist_get_detected(csr_cs_detected_list_h list, int index, csr_cs_detected_h *pdetected) +int csr_cs_dlist_get_detected(csr_cs_detected_list_h list, size_t index, + csr_cs_detected_h *pdetected) { - (void) list; - (void) index; - (void) pdetected; + EXCEPTION_SAFE_START + + if (list == nullptr || pdetected == nullptr) + return CSR_ERROR_INVALID_PARAMETER; + + auto dListPtr = reinterpret_cast(list); + + if (index >= dListPtr->size()) + return CSR_ERROR_INVALID_PARAMETER; + + *pdetected = reinterpret_cast(dListPtr->at(index).get()); - DEBUG("start!"); return CSR_ERROR_NONE; + + EXCEPTION_SAFE_END } diff --git a/src/framework/client/handle.cpp b/src/framework/client/handle.cpp index bfd800c..779a898 100644 --- a/src/framework/client/handle.cpp +++ b/src/framework/client/handle.cpp @@ -22,6 +22,7 @@ #include "client/handle.h" #include +#include namespace Csr { namespace Client { @@ -50,5 +51,13 @@ void Handle::add(Result *result) m_ctx->add(result); } +void Handle::add(ResultListPtr &&resultListPtr) +{ + if (resultListPtr == nullptr) + throw std::logic_error("result list pointer shouldn't be null"); + + m_ctx->add(std::forward(resultListPtr)); +} + } // namespace Client } // namespace Csr diff --git a/src/framework/client/handle.h b/src/framework/client/handle.h index e3b8c70..56906ff 100644 --- a/src/framework/client/handle.h +++ b/src/framework/client/handle.h @@ -39,6 +39,7 @@ public: Type dispatch(Args &&...); void add(Result *); + void add(ResultListPtr &&); std::shared_ptr &getContext(void) noexcept; diff --git a/src/framework/common/command-id.h b/src/framework/common/command-id.h index ea720df..f3355d0 100644 --- a/src/framework/common/command-id.h +++ b/src/framework/common/command-id.h @@ -24,11 +24,22 @@ namespace Csr { enum class CommandId : int { - SCAN_FILE = 0x01, - JUDGE_STATUS = 0x02, - CHECK_URL = 0x03, - DIR_GET_RESULTS = 0x04, - DIR_GET_FILES = 0x05 + // content scanning + // scanning + SCAN_DATA = 0x1001, + SCAN_FILE = 0x1002, + // delta, history + DIR_GET_RESULTS = 0x1101, + DIR_GET_FILES = 0x1102, + GET_DETECTED = 0x1103, + GET_DETECTED_LIST = 0x1104, + GET_IGNORED = 0x1105, + GET_IGNORED_LIST = 0x1106, + // handle result + JUDGE_STATUS = 0x1201, + + // web protection + CHECK_URL = 0x2001 }; } diff --git a/src/framework/common/cs-detected.cpp b/src/framework/common/cs-detected.cpp index fe6481b..6525f0e 100644 --- a/src/framework/common/cs-detected.cpp +++ b/src/framework/common/cs-detected.cpp @@ -34,6 +34,7 @@ CsDetected::CsDetected() : m_severity(CSR_CS_SEVERITY_LOW), m_threat(CSR_CS_THREAT_GENERIC), m_response(CSR_CS_NO_ASK_USER), + m_isApp(false), m_ts(0) { } @@ -127,6 +128,21 @@ void CsDetected::set(int key, int value) setValueFlag(); } +void CsDetected::set(int key, bool value) +{ + switch (static_cast(key)) { + case Key::IsApp: + m_isApp = value; + break; + + default: + throw std::logic_error(FORMAT("Invalid key[" << key + << "] comes in to set as bool.")); + } + + setValueFlag(); +} + void CsDetected::set(int key, const std::string &value) { switch (static_cast(key)) { @@ -142,6 +158,10 @@ void CsDetected::set(int key, const std::string &value) m_detailedUrl = value; break; + case Key::PkgId: + m_pkgId = value; + break; + default: throw std::logic_error(FORMAT("Invalid key[" << key << "] comes in to set as string.")); @@ -209,6 +229,19 @@ void CsDetected::get(int key, int &value) const } } +void CsDetected::get(int key, bool &value) const +{ + switch (static_cast(key)) { + case Key::IsApp: + value = m_isApp; + break; + + default: + throw std::logic_error(FORMAT("Invalid key[" << key + << "] comes in to get as bool.")); + } +} + void CsDetected::get(int key, const char **value) const { if (value == nullptr) @@ -225,6 +258,11 @@ void CsDetected::get(int key, const char **value) const case Key::DetailedUrl: *value = m_detailedUrl.c_str(); + break; + + case Key::PkgId: + *value = m_pkgId.c_str(); + break; default: throw std::logic_error(FORMAT("Invalid key[" << key diff --git a/src/framework/common/cs-detected.h b/src/framework/common/cs-detected.h index 322e452..a99ab72 100644 --- a/src/framework/common/cs-detected.h +++ b/src/framework/common/cs-detected.h @@ -28,6 +28,10 @@ namespace Csr { +class CsDetected; +using CsDetectedPtr = std::unique_ptr; +using CsDetectedList = std::vector; + class CsDetected : public Result { public: // key for set/get @@ -35,12 +39,15 @@ public: TargetName = 0x01, // string MalwareName = 0x02, // string DetailedUrl = 0x03, // string + PkgId = 0x04, // string Severity = 0x10, // int Threat = 0x11, // int UserResponse = 0x12, // int - TimeStamp = 0x21 // time_t + TimeStamp = 0x20, // time_t + + IsApp = 0x30 // bool }; CsDetected(); @@ -53,11 +60,13 @@ public: CsDetected &operator=(CsDetected &&); virtual void set(int, int) override; + virtual void set(int, bool) override; virtual void set(int, const std::string &) override; virtual void set(int, const char *) override; virtual void set(int, time_t) override; virtual void get(int, int &) const override; + virtual void get(int, bool &) const override; virtual void get(int, const char **) const override; virtual void get(int, time_t &) const override; @@ -66,9 +75,11 @@ private: std::string m_malwareName; std::string m_detailedUrl; + std::string m_pkgId; csr_cs_severity_level_e m_severity; csr_cs_threat_type_e m_threat; csr_cs_user_response_e m_response; + bool m_isApp; time_t m_ts; }; diff --git a/src/framework/common/types.cpp b/src/framework/common/types.cpp index 27b7a00..41ae644 100644 --- a/src/framework/common/types.cpp +++ b/src/framework/common/types.cpp @@ -63,10 +63,10 @@ Context &Context::operator=(Context &&other) return *this; } -void Context::add(std::unique_ptr &&item) +void Context::add(ResultPtr &&item) { std::lock_guard l(m_mutex); - m_results.emplace_back(std::forward>(item)); + m_results.emplace_back(std::forward(item)); } void Context::add(Result *item) @@ -75,6 +75,12 @@ void Context::add(Result *item) m_results.emplace_back(item); } +void Context::add(ResultListPtr &&item) +{ + std::lock_guard l(m_mutex); + m_resultLists.emplace_back(std::forward(item)); +} + size_t Context::size() const { std::lock_guard l(m_mutex); diff --git a/src/framework/common/types.h b/src/framework/common/types.h index 27b982f..bced3a9 100644 --- a/src/framework/common/types.h +++ b/src/framework/common/types.h @@ -57,6 +57,10 @@ private: bool m_hasVal; }; +using ResultPtr = std::unique_ptr; +using ResultList = std::vector; +using ResultListPtr = std::unique_ptr; + class Context : public ISerializable, public KvpContainer { public: Context(); @@ -70,11 +74,13 @@ public: Context(const Context &); Context &operator=(const Context &); - void add(std::unique_ptr &&); + void add(ResultPtr &&); void add(Result *); + void add(ResultListPtr &&); size_t size(void) const; // for destroying with context - std::vector> m_results; + std::vector m_results; + std::vector m_resultLists; private: mutable std::mutex m_mutex; diff --git a/src/framework/service/cs-loader.h b/src/framework/service/cs-loader.h index d1390e8..c63dc6c 100755 --- a/src/framework/service/cs-loader.h +++ b/src/framework/service/cs-loader.h @@ -22,7 +22,6 @@ #pragma once #include -#include #include #include diff --git a/src/framework/service/logic.cpp b/src/framework/service/logic.cpp index d12251a..fdd93de 100644 --- a/src/framework/service/logic.cpp +++ b/src/framework/service/logic.cpp @@ -71,6 +71,13 @@ RawBuffer Logic::dispatch(const RawBuffer &in) INFO("Request dispatch! CommandId: " << static_cast(info.first)); switch (info.first) { + case CommandId::SCAN_DATA: { + CsContext context; + RawBuffer data; + info.second.Deserialize(context, data); + return scanData(context, data); + } + case CommandId::SCAN_FILE: { CsContext context; std::string filepath; @@ -100,6 +107,45 @@ RawBuffer Logic::dispatch(const RawBuffer &in) return dirGetFiles(context, dir); } + case CommandId::JUDGE_STATUS: { + CsContext context; + std::string filepath; + info.second.Deserialize(context, filepath); + return judgeStatus(context, filepath); + } + + case CommandId::GET_DETECTED: { + CsContext context; + std::string filepath; + info.second.Deserialize(context, filepath); + return getDetected(context, filepath); + } + + case CommandId::GET_DETECTED_LIST: { + CsContext context; + std::string dir; + info.second.Deserialize(context, dir); + //return getDetectedList(context, dir); + RawBuffer buff = getDetectedList(context, dir); + INFO(" CommandId::GET_DETECTED_LIST : RawBuffer Size[" << buff.size() << "]"); + return buff; + } + + case CommandId::GET_IGNORED: { + CsContext context; + std::string filepath; + info.second.Deserialize(context, filepath); + return getIgnored(context, filepath); + } + + case CommandId::GET_IGNORED_LIST: { + CsContext context; + std::string dir; + info.second.Deserialize(context, dir); + return getIgnoredList(context, dir); + } + + default: throw std::range_error(FORMAT("Command id[" << static_cast(info.first) << "] isn't in range.")); @@ -117,6 +163,15 @@ std::pair Logic::getRequestInfo(const RawBuffer &data) return std::make_pair(id, std::move(q)); } +RawBuffer Logic::scanData(const CsContext &context, const RawBuffer &data) +{ + INFO("Scan Data[size=" << data.size() << "] by engine"); + + printCsContext(context); + + return BinaryQueue::Serialize(CSR_ERROR_NONE, CsDetected()).pop(); +} + RawBuffer Logic::scanFile(const CsContext &context, const std::string &filepath) { INFO("Scan file[" << filepath << "] by engine"); @@ -144,6 +199,69 @@ RawBuffer Logic::dirGetFiles(const CsContext &context, const std::string &dir) return BinaryQueue::Serialize(CSR_ERROR_NONE, StrSet()).pop(); } +RawBuffer Logic::judgeStatus(const CsContext &context, const std::string &filepath) +{ + INFO("Judge Status[" << filepath << "] by engine"); + + printCsContext(context); + + return BinaryQueue::Serialize(CSR_ERROR_NONE).pop(); +} + +RawBuffer Logic::getDetected(const CsContext &context, const std::string &filepath) +{ + INFO("Get Detected[" << filepath << "] by engine"); + + printCsContext(context); + + CsDetected detected; + detected.set(static_cast(CsDetected::Key::TargetName), "test_file"); + + return BinaryQueue::Serialize(CSR_ERROR_NONE, detected).pop(); +} + +RawBuffer Logic::getDetectedList(const CsContext &context, const std::string &dir) +{ + INFO("Get Detected List[" << dir << "] by engine"); + + printCsContext(context); + + CsDetectedPtr detected(new CsDetected()); + detected->set(static_cast(CsDetected::Key::TargetName), "test_file"); + + CsDetectedList list; + list.emplace_back(std::move(detected)); + + return BinaryQueue::Serialize(CSR_ERROR_NONE, list).pop(); +} + +RawBuffer Logic::getIgnored(const CsContext &context, const std::string &filepath) +{ + INFO("Get Ignored[" << filepath << "] by engine"); + + printCsContext(context); + + CsDetected detected; + detected.set(static_cast(CsDetected::Key::TargetName), "test_file"); + + return BinaryQueue::Serialize(CSR_ERROR_NONE, detected).pop(); +} + +RawBuffer Logic::getIgnoredList(const CsContext &context, const std::string &dir) +{ + INFO("Get Ignored List[" << dir << "] by engine"); + + printCsContext(context); + + CsDetectedPtr detected(new CsDetected()); + detected->set(static_cast(CsDetected::Key::TargetName), "test_file"); + + CsDetectedList list; + list.emplace_back(std::move(detected)); + + return BinaryQueue::Serialize(CSR_ERROR_NONE, list).pop(); +} + RawBuffer Logic::checkUrl(const WpContext &context, const std::string &url) { INFO("Check url[" << url << "] by engine"); diff --git a/src/framework/service/logic.h b/src/framework/service/logic.h index fab9db9..2869fab 100644 --- a/src/framework/service/logic.h +++ b/src/framework/service/logic.h @@ -43,9 +43,16 @@ public: private: std::pair getRequestInfo(const RawBuffer &); + RawBuffer scanData(const CsContext &context, const RawBuffer &data); RawBuffer scanFile(const CsContext &context, const std::string &filepath); RawBuffer dirGetResults(const CsContext &context, const std::string &dir); RawBuffer dirGetFiles(const CsContext &context, const std::string &dir); + RawBuffer judgeStatus(const CsContext &context, const std::string &filepath); + RawBuffer getDetected(const CsContext &context, const std::string &filepath); + RawBuffer getDetectedList(const CsContext &context, const std::string &dir); + RawBuffer getIgnored(const CsContext &context, const std::string &filepath); + RawBuffer getIgnoredList(const CsContext &context, const std::string &dir); + RawBuffer checkUrl(const WpContext &context, const std::string &url); }; diff --git a/src/framework/service/wp-loader.h b/src/framework/service/wp-loader.h index d844f75..bedec6b 100755 --- a/src/framework/service/wp-loader.h +++ b/src/framework/service/wp-loader.h @@ -19,15 +19,12 @@ * @version 1.0 * @brief */ - #pragma once #include -#include #include #include - #include "csre/web-protection-types.h" #include "csre/web-protection-engine-info.h" diff --git a/src/include/csr/content-screening.h b/src/include/csr/content-screening.h index 91c8283..24c7007 100644 --- a/src/include/csr/content-screening.h +++ b/src/include/csr/content-screening.h @@ -23,6 +23,8 @@ #define __CSR_CONTENT_SCREENING_API_H_ #include +#include +#include #include "csr/content-screening-types.h" #include "csr/error.h" @@ -172,7 +174,7 @@ int csr_cs_set_scan_on_cloud(csr_cs_context_h handle); */ int csr_cs_scan_data(csr_cs_context_h handle, const unsigned char *data, - unsigned int length, + size_t length, csr_cs_detected_h *pdetected); /** @@ -321,7 +323,7 @@ int csr_cs_set_callback_on_file_scanned(csr_cs_context_h handle, csr_cs_on_file_ */ int csr_cs_scan_files_async(csr_cs_context_h handle, const char **file_paths, - unsigned int count, + size_t count, void *user_data); /** @@ -377,7 +379,7 @@ int csr_cs_scan_dir_async(csr_cs_context_h handle, * @retval #CSR_ERROR_NONE Successful * @retval #CSR_ERROR_INVALID_HANDLE Invalid handle * @retval #CSR_ERROR_OUT_OF_MEMORY Not enough memory - * @retval #CSR_ERROR_INVALID_PARAMETER file_paths or count is invalid + * @retval #CSR_ERROR_INVALID_PARAMETER dir_paths or count is invalid * @retval #CSR_ERROR_PERMISSION_DENIED Access denied * @retval #CSR_ERROR_FILE_NOT_FOUND File not found * @retval #CSR_ERROR_SOCKET Socket error between client and server @@ -390,7 +392,7 @@ int csr_cs_scan_dir_async(csr_cs_context_h handle, */ int csr_cs_scan_dirs_async(csr_cs_context_h handle, const char **dir_paths, - unsigned int count, + size_t count, void *user_data); @@ -523,11 +525,40 @@ int csr_cs_detected_get_file_name(csr_cs_detected_h detected, const char** pfile * @retval #CSR_ERROR_INVALID_HANDLE Invalid result handle * @retval #CSR_ERROR_INVALID_PARAMETER presponse is invalid * @retval #CSR_ERROR_UNKNOWN Error with unknown reason - * */ int csr_cs_detected_get_user_response(csr_cs_detected_h detected, csr_cs_user_response_e* presponse); /** + * @brief check if a malware was detected in an application or in a file. + * + * @param[in] detected A detected malware handle. + * @param[out] pis_app A pointer of a flag indicating the position a malware was detected. + * + * @return #CSR_ERROR_NONE on success, otherwise a negative error value + * + * @retval #CSR_ERROR_NONE Successful + * @retval #CSR_ERROR_INVALID_HANDLE Invalid result handle + * @retval #CSR_ERROR_INVALID_PARAMETER pis_app is invalid + * @retval #CSR_ERROR_UNKNOWN Error with unknown reason + */ +int csr_cs_detected_is_app(csr_cs_detected_h detected, bool* pis_app); + +/** + * @brief extracts the package id of an application where a malware is detected from detected malware handle. + * + * @param[in] detected A detected malware handle. + * @param[out] ppkg_id A pointer of the pakcage id where a malware is detected. A caller should not free this string. This is a null when a malware was not detected in an application. + * + * @return #CSR_ERROR_NONE on success, otherwise a negative error value + * + * @retval #CSR_ERROR_NONE Successful + * @retval #CSR_ERROR_INVALID_HANDLE Invalid detected malware handle + * @retval #CSR_ERROR_INVALID_PARAMETER ppkg_id is invalid + * @retval #CSR_ERROR_UNKNOWN Error with unknown reason + */ +int csr_cs_detected_get_pkg_id(csr_cs_detected_h detected, const char** ppkg_id); + +/** * @brief Judges how a detected malware file is handled. * * @details A detected malware may be removed or ignored. When action is @@ -541,7 +572,7 @@ int csr_cs_detected_get_user_response(csr_cs_detected_h detected, csr_cs_user_re * again. * * @param[in] handle CSR CS context handle returned by csr_cs_context_create(). - * @param[in] file_path A path of a detected malware file. + * @param[in] detected A handle of a detected malware. * @param[in] action An action to be taken. * * @return #CSR_ERROR_NONE on success, otherwise a negative error value @@ -549,14 +580,14 @@ int csr_cs_detected_get_user_response(csr_cs_detected_h detected, csr_cs_user_re * @retval #CSR_ERROR_NONE Successful * @retval #CSR_ERROR_INVALID_HANDLE Invalid handle * @retval #CSR_ERROR_OUT_OF_MEMORY Not enough memory - * @retval #CSR_ERROR_INVALID_PARAMETER file_path or action is invalid + * @retval #CSR_ERROR_INVALID_PARAMETER detected or action is invalid * @retval #CSR_ERROR_PERMISSION_DENIED No permission to remove * @retval #CSR_ERROR_FILE_NOT_FOUND File to take action on not found * @retval #CSR_ERROR_SOCKET Socket error between client and server * @retval #CSR_ERROR_SERVER Server has been failed for some reason * @retval #CSR_ERROR_UNKNOWN Error with unknown reason */ -int csr_cs_judge_detected_malware(csr_cs_context_h handle, const char *file_path, csr_cs_action_e action); +int csr_cs_judge_detected_malware(csr_cs_context_h handle, csr_cs_detected_h detected, csr_cs_action_e action); /** @@ -602,7 +633,7 @@ int csr_cs_get_detected_malware(csr_cs_context_h handle, const char *file_path, * @retval #CSR_ERROR_UNKNOWN Error with unknown reason */ int csr_cs_get_detected_malwares(csr_cs_context_h handle, const char *dir, - csr_cs_detected_list_h *plist, int *pcount); + csr_cs_detected_list_h *plist, size_t *pcount); /** * @brief Gets information on a ignored malware file specified by file path. @@ -647,7 +678,7 @@ int csr_cs_get_ignored_malware(csr_cs_context_h handle, const char *file_path, c * @retval #CSR_ERROR_UNKNOWN Error with unknown reason */ int csr_cs_get_ignored_malwares(csr_cs_context_h handle, const char *dir, - csr_cs_detected_list_h *plist, int *pcount); + csr_cs_detected_list_h *plist, size_t *pcount); /** * @brief Extracts the detected malware handle from the detected malware list handle. @@ -666,7 +697,7 @@ int csr_cs_get_ignored_malwares(csr_cs_context_h handle, const char *dir, * @retval #CSR_ERROR_INVALID_PARAMETER index or pdetected is invalid. * @retval #CSR_ERROR_UNKNOWN Error with unknown reason */ -int csr_cs_dlist_get_detected(csr_cs_detected_list_h list, int index, csr_cs_detected_h *pdetected); +int csr_cs_dlist_get_detected(csr_cs_detected_list_h list, size_t index, csr_cs_detected_h *pdetected); #ifdef __cplusplus } diff --git a/test/test-api-content-screening.cpp b/test/test-api-content-screening.cpp index 41e7703..539ecd3 100644 --- a/test/test-api-content-screening.cpp +++ b/test/test-api-content-screening.cpp @@ -88,6 +88,24 @@ BOOST_AUTO_TEST_CASE(set_values_to_context_negative) EXCEPTION_GUARD_END } +BOOST_AUTO_TEST_CASE(scan_data) +{ + EXCEPTION_GUARD_START + + auto c = Test::Context(); + auto context = c.get(); + csr_cs_detected_h detected; + unsigned char data[100] = {0, }; + + ASSERT_IF(csr_cs_scan_data(context, data, sizeof(data), &detected), + CSR_ERROR_NONE); + + // no malware detected + CHECK_IS_NULL(detected); + + EXCEPTION_GUARD_END +} + BOOST_AUTO_TEST_CASE(scan_file) { EXCEPTION_GUARD_START @@ -105,4 +123,94 @@ BOOST_AUTO_TEST_CASE(scan_file) EXCEPTION_GUARD_END } +BOOST_AUTO_TEST_CASE(get_detected_malware) +{ + EXCEPTION_GUARD_START + + auto c = Test::Context(); + auto context = c.get(); + csr_cs_detected_h detected; + + ASSERT_IF(csr_cs_get_detected_malware(context, "dummy_file_path", &detected), + CSR_ERROR_NONE); + + // no malware detected + CHECK_IS_NOT_NULL(detected); + + EXCEPTION_GUARD_END +} + +BOOST_AUTO_TEST_CASE(get_detected_malwares) +{ + EXCEPTION_GUARD_START + + auto c = Test::Context(); + auto context = c.get(); + csr_cs_detected_list_h detected_list; + size_t cnt = 0; + + ASSERT_IF(csr_cs_get_detected_malwares(context, "dummy_dir_path", &detected_list, &cnt), + CSR_ERROR_NONE); + + // no malware detected + CHECK_IS_NOT_NULL(detected_list); + + ASSERT_IF(cnt, static_cast(1)); + + EXCEPTION_GUARD_END +} + +BOOST_AUTO_TEST_CASE(get_ignored_malware) +{ + EXCEPTION_GUARD_START + + auto c = Test::Context(); + auto context = c.get(); + csr_cs_detected_h ignored; + + ASSERT_IF(csr_cs_get_ignored_malware(context, "dummy_file_path", &ignored), + CSR_ERROR_NONE); + + CHECK_IS_NOT_NULL(ignored); + + EXCEPTION_GUARD_END +} + +BOOST_AUTO_TEST_CASE(get_ignored_malwares) +{ + EXCEPTION_GUARD_START + + auto c = Test::Context(); + auto context = c.get(); + csr_cs_detected_list_h ignored_list; + size_t cnt = 0; + + ASSERT_IF(csr_cs_get_ignored_malwares(context, "dummy_dir_path", &ignored_list, &cnt), + CSR_ERROR_NONE); + + CHECK_IS_NOT_NULL(ignored_list); + + ASSERT_IF(cnt, static_cast(1)); + + EXCEPTION_GUARD_END +} + +BOOST_AUTO_TEST_CASE(judge_detected_malware) +{ + EXCEPTION_GUARD_START + + auto c = Test::Context(); + auto context = c.get(); + csr_cs_detected_h detected; + + ASSERT_IF(csr_cs_get_detected_malware(context, "dummy_file_path", &detected), + CSR_ERROR_NONE); + CHECK_IS_NOT_NULL(detected); + + ASSERT_IF(csr_cs_judge_detected_malware(context, detected, CSR_CS_ACTION_REMOVE), + CSR_ERROR_NONE); + + EXCEPTION_GUARD_END +} + BOOST_AUTO_TEST_SUITE_END() diff --git a/test/test-common.h b/test/test-common.h index 896e75f..60d5771 100644 --- a/test/test-common.h +++ b/test/test-common.h @@ -56,7 +56,7 @@ namespace Test { template -void _assert(T value, U expected, const std::string &filename, const std::string &funcname, unsigned int line) +void _assert(const T &value, const U &expected, const std::string &filename, const std::string &funcname, unsigned int line) { BOOST_REQUIRE_MESSAGE(value == expected, "[" << filename << " > " << funcname << " : " << line << "]"