Implement compare cert API using server-client architecture
authorJunghyun Yeon <jungh.yeon@samsung.com>
Thu, 4 Mar 2021 05:09:34 +0000 (14:09 +0900)
committer연정현/Tizen Platform Lab(SR)/Staff Engineer/삼성전자 <jungh.yeon@samsung.com>
Fri, 5 Mar 2021 00:05:43 +0000 (09:05 +0900)
Signed-off-by: Junghyun Yeon <jungh.yeon@samsung.com>
src/manager/pkginfo_manager.cc
src/manager/pkginfo_manager.h

index 1ff5278..f5b7cf5 100644 (file)
@@ -18,6 +18,7 @@
 
 #include <sys/types.h>
 
+#include <map>
 #include <string>
 
 #include "sqlite3.h"
@@ -655,6 +656,63 @@ extern "C" EXPORT_API int _pkginfo_set_usr_installed_storage(const char *pkgid,
        return PMINFO_R_OK;
 }
 
+extern "C" EXPORT_API int _certinfo_compare_certinfo(const char *l_pkgid,
+               const char *r_pkgid, pkgmgrinfo_cert_compare_result_type_e *result) {
+       char *query = sqlite3_mprintf("SELECT package, "
+                       "COALESCE(author_signer_cert, -1) FROM package_cert_info WHERE "
+                       "package IN (%Q, %Q)");
+       if (query == nullptr) {
+               LOG(ERROR) << "Out of memory";
+               return PMINFO_R_ERROR;
+       }
+       std::vector<std::string> queries;
+       queries.emplace_back(query);
+       sqlite3_free(query);
+
+       std::shared_ptr<pkgmgr_common::parcel::AbstractParcelable> parcelable(
+               new pkgmgr_common::parcel::QueryParcelable(0, queries,
+                               pkgmgr_common::database::AbstractDBHandler::DBType::DB_TYPE_FILE_CERTDB,
+                               pkgmgr_common::database::AbstractDBHandler::OperationType::OPERATION_TYPE_READ));
+       pkgmgr_client::PkgInfoClient client(parcelable, 0,
+                       pkgmgr_common::ReqType::QUERY);
+       if (!client.SendRequest()) {
+               return PMINFO_R_ERROR;
+       }
+
+       std::shared_ptr<pkgmgr_common::parcel::ResultParcelable> return_parcel(
+                       std::static_pointer_cast<pkgmgr_common::parcel::ResultParcelable>(
+                                       client.GetResultParcel()));
+
+       auto certinfo_list = return_parcel->GetResult();
+       if (certinfo_list.size() != 2)
+               return PMINFO_R_ERROR;
+
+       std::map<std::string, std::string> result_map;
+       for (auto& certinfo : certinfo_list)
+               result_map.insert(make_pair(certinfo.front(), certinfo.back()));
+
+       if (result_map.find(std::string(l_pkgid))->second == "-1" &&
+                       result_map.find(std::string(r_pkgid))->second == "-1")
+               *result = PMINFO_CERT_COMPARE_BOTH_NO_CERT;
+       else if (result_map.find(std::string(l_pkgid))->second == "-1")
+               *result = PMINFO_CERT_COMPARE_LHS_NO_CERT;
+       else if (result_map.find(std::string(r_pkgid))->second == "-1")
+               *result = PMINFO_CERT_COMPARE_RHS_NO_CERT;
+       else if (result_map.find(std::string(l_pkgid))->second ==
+                       result_map.find(std::string(r_pkgid))->second)
+               *result = PMINFO_CERT_COMPARE_MATCH;
+       else
+               *result = PMINFO_CERT_COMPARE_MISMATCH;
+
+/*
+       auto cert_list = return_parcel->GetResult();
+       for (auto& cert : cert_list) {
+
+       }
+*/
+       return PMINFO_R_OK;
+}
+
 extern "C" EXPORT_API int _parser_execute_write_query(const char *query, uid_t uid)
 {
        std::shared_ptr<pkgmgr_common::parcel::AbstractParcelable> parcelable(
index 0f5abba..f5f462e 100644 (file)
@@ -66,6 +66,9 @@ int _pkginfo_set_usr_installed_storage(const char *pkgid,
                INSTALL_LOCATION location, const char *external_pkg_path,
                uid_t uid);
 
+int _certinfo_compare_certinfo(const char *l_pkgid, const char *r_pkgid,
+               pkgmgrinfo_cert_compare_result_type_e *result);
+
 int _parser_execute_write_query(const char *query, uid_t uid);
 
 int _parser_execute_write_queries(const char **queries, int len, uid_t uid);