diff options
author | Janusz Kozerski <j.kozerski@samsung.com> | 2015-05-28 10:41:03 +0200 |
---|---|---|
committer | Janusz Kozerski <j.kozerski@samsung.com> | 2015-06-26 12:36:50 +0200 |
commit | a282a131cd809c3f425a44f42e30dfb902bca76e (patch) | |
tree | 5542ae81ed7f55803577beed89c91004216c267a /src | |
parent | 0cad75f144c2ef178d2bc7cced6cf4c4b0e57409 (diff) | |
download | cert-checker-a282a131cd809c3f425a44f42e30dfb902bca76e.tar.gz cert-checker-a282a131cd809c3f425a44f42e30dfb902bca76e.tar.bz2 cert-checker-a282a131cd809c3f425a44f42e30dfb902bca76e.zip |
Add cert-checker database (+tests). Change include files path.
* Add m_buffer in Logic class.
* Load database into buffer in Logic::setup()
* Move include to separate folder - this solves problem with app.h file
from different package.
[Verification] Run cert-checker-tests. All should pass.
Change-Id: I809951a17fd44d5258ad91e480b6914e92d1058b
Diffstat (limited to 'src')
-rw-r--r-- | src/CMakeLists.txt | 4 | ||||
-rw-r--r-- | src/app.cpp | 31 | ||||
-rw-r--r-- | src/cert-checker.cpp | 4 | ||||
-rw-r--r-- | src/db/sql_query.cpp | 296 | ||||
-rw-r--r-- | src/dpl/core/include/dpl/colors.h | 74 | ||||
-rw-r--r-- | src/dpl/core/src/assert.cpp | 2 | ||||
-rw-r--r-- | src/dpl/core/src/colors.cpp | 71 | ||||
-rw-r--r-- | src/dpl/core/src/exception.cpp | 2 | ||||
-rw-r--r-- | src/dpl/core/src/string.cpp | 2 | ||||
-rw-r--r-- | src/dpl/db/include/dpl/db/sql_connection.h | 8 | ||||
-rw-r--r-- | src/dpl/db/src/sql_connection.cpp | 16 | ||||
-rw-r--r-- | src/include/cchecker/app.h (renamed from src/include/app.h) | 8 | ||||
-rw-r--r-- | src/include/cchecker/log.h (renamed from src/log/log.h) | 0 | ||||
-rw-r--r-- | src/include/cchecker/logic.h (renamed from src/include/logic.h) | 22 | ||||
-rw-r--r-- | src/include/cchecker/sql_query.h | 76 | ||||
-rw-r--r-- | src/logic.cpp | 60 |
16 files changed, 639 insertions, 37 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index ec7e710..33c6295 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -9,6 +9,7 @@ PKG_CHECK_MODULES(CERT_CHECKER_DEP capi-appfw-package-manager notification libsystemd-journal + libtzplatform-config sqlite3 ) @@ -30,12 +31,13 @@ SET(CERT_CHECKER_SOURCES # dpl DB ${CERT_CHECKER_SRC_PATH}/dpl/db/src/sql_connection.cpp ${CERT_CHECKER_SRC_PATH}/dpl/db/src/naive_synchronization_object.cpp + # DB + ${CERT_CHECKER_SRC_PATH}/db/sql_query.cpp ) INCLUDE_DIRECTORIES(SYSTEM ${CERT_CHECKER_DEP_INCLUDE_DIRS} ${CERT_CHECKER_SRC_PATH}/include/ - ${CERT_CHECKER_SRC_PATH}/log/ ${CERT_CHECKER_SRC_PATH}/dpl/core/include/ ${CERT_CHECKER_SRC_PATH}/dpl/db/include/ ) diff --git a/src/app.cpp b/src/app.cpp index 575cf03..58a7883 100644 --- a/src/app.cpp +++ b/src/app.cpp @@ -25,29 +25,42 @@ #include <vector> #include <sys/types.h> -#include <app.h> +#include <cchecker/app.h> namespace CCHECKER { app_t::app_t(void): - check_id(-1), // -1 as invalid check_id - assume that in database - // all check_ids will be positive - uid((uid_t)-1), // (uid_t)-1 (0xFF) is defined to be invalid uid. According - // to chown manual page, you cannot change file group of owner - // to (uid_t)-1, so we'll use it as initial, invalid value. - verified(verified_t::UNKNOWN) + check_id(-1), // -1 as invalid check_id - assume that in database + // all check_ids will be positive + uid((uid_t)-1), // (uid_t)-1 (0xFF) is defined to be invalid uid. According + // to chown manual page, you cannot change file group of owner + // to (uid_t)-1, so we'll use it as initial, invalid value. + verified(verified_t::UNKNOWN) +{} + +app_t::app_t(const std::string &app_id, + const std::string &pkg_id, + uid_t uid, + const std::vector<std::string> &certificates): + check_id(-1), + app_id(app_id), + pkg_id(pkg_id), + uid(uid), + certificates(certificates), + verified(verified_t::UNKNOWN) {} std::ostream & operator<< (std::ostream &out, const app_t &app) { - out << "app: " << app.app_id << ", pkg: " << app.pkg_id << ", uid: " << app.uid; + out << "app: " << app.app_id << ", pkg: " << app.pkg_id << ", uid: " << app.uid << + ", check_id: " << app.check_id; return out; } std::string app_t::str() const { std::stringstream ss; - ss << this; + ss << *this; return ss.str(); } diff --git a/src/cert-checker.cpp b/src/cert-checker.cpp index 984e41e..e754d94 100644 --- a/src/cert-checker.cpp +++ b/src/cert-checker.cpp @@ -22,8 +22,8 @@ #include <glib.h> -#include <log.h> -#include <logic.h> +#include <cchecker/log.h> +#include <cchecker/logic.h> using namespace CCHECKER; diff --git a/src/db/sql_query.cpp b/src/db/sql_query.cpp new file mode 100644 index 0000000..c39be95 --- /dev/null +++ b/src/db/sql_query.cpp @@ -0,0 +1,296 @@ +/* + * Copyright (c) 2015 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 sql_query.cpp + * @author Janusz Kozerski (j.kozerski@samsung.com) + * @version 1.0 + * @brief This file is the implementation of SQL queries + */ + +#include <dpl/db/sql_connection.h> +#include <cchecker/sql_query.h> +#include <cchecker/log.h> + +namespace { + + // TODO: Make defines with these identifiers. + // 101 - issuer + // 102 - url + // 103 - date + // 104 - app_id + // 105 - pkg_id + // 106 - uid + // 107 - check_id + // 108 - certificate + // 109 - verified + + // urls + const char *DB_CMD_GET_URL = + "SELECT url, date FROM ocsp_urls WHERE issuer = ?101;"; + + const char *DB_CMD_SET_URL = + "INSERT INTO ocsp_urls(issuer, url, date) VALUES(?101, ?102, ?103);"; + + const char *DB_CMD_UPDATE_URL = + "UPDATE ocsp_urls SET url=?102, date=?103 WHERE issuer=?101;"; // Issuer should be unique + + // apps + const char *DB_CMD_ADD_APP = + "INSERT INTO to_check(app_id, pkg_id, uid, verified) VALUES(?104, ?105, ?106, ?109);"; + + const char *DB_CMD_GET_CHECK_ID = + "SELECT check_id FROM to_check WHERE app_id=?104 AND pkg_id=?105 AND uid=?106;"; + + const char *DB_CMD_ADD_CERT = + "INSERT INTO certs_to_check(check_id, certificate) VALUES(?107, ?108);"; + + const char *DB_CMD_REMOVE_APP = + "DELETE FROM to_check WHERE app_id=?104 AND pkg_id=?105 AND uid=?106;"; + + const char *DB_CMD_GET_APPS = + "SELECT * FROM to_check"; + + const char *DB_CMD_GET_CERTS = + "SELECT certificate FROM certs_to_check WHERE check_id=?107;"; + + const char *DB_CMD_SET_APP_AS_VERIFIED = + "UPDATE to_check SET verified=?109 WHERE check_id=?107"; +} + +namespace CCHECKER { +namespace DB { + +SqlQuery::SqlQuery(const std::string& path) +{ + m_connection = NULL; + m_inUserTransaction = false; + + if (!connect(path)) + throw std::runtime_error("Database error"); +} + +bool SqlQuery::connect(const std::string& path) +{ + if (m_connection != NULL) { + LogError("Already connected!"); + return true; + } + + Try { + m_connection = new SqlConnection(path, SqlConnection::Flag::None, SqlConnection::Flag::Option::CRW); + m_connection->ExecCommand("VACUUM;"); + return true; + } Catch(std::bad_alloc) { + LogError("Couldn't allocate SqlConnection"); + } Catch(SqlConnection::Exception::ConnectionBroken) { + LogError("Couldn't connect to database: " << path); + } Catch(SqlConnection::Exception::InvalidColumn) { + LogError("Couldn't set the key for database"); + } Catch(SqlConnection::Exception::SyntaxError) { + LogError("Couldn't initiate the database"); + } Catch(SqlConnection::Exception::InternalError) { + LogError("Couldn't create the database"); + } + return false; +} + +SqlQuery::~SqlQuery() +{ + delete m_connection; +} + +bool SqlQuery::get_url(const std::string &issuer, std::string &url) +{ + SqlConnection::DataCommandAutoPtr getUrlCommand = + m_connection->PrepareDataCommand(DB_CMD_GET_URL); + getUrlCommand->BindString(101, issuer.c_str()); + + if (getUrlCommand->Step()) { + url = getUrlCommand->GetColumnString(0); + LogDebug("Url for " << issuer << " found in databse: " << url); + return true; + } + + LogDebug("No url for " << issuer << " in databse."); + return false; +} + +void SqlQuery::set_url(const std::string &issuer, const std::string &url, const int64_t &date) +{ + m_connection->BeginTransaction(); + SqlConnection::DataCommandAutoPtr getUrlCommand = + m_connection->PrepareDataCommand(DB_CMD_GET_URL); + getUrlCommand->BindString(101, issuer.c_str()); + + if (getUrlCommand->Step()) { // This means that url already exists in database for this issuer + // There's need to check the date + LogDebug("Url for " << issuer << " already exists. Checking the date"); + int64_t db_date = getUrlCommand->GetColumnInt64(1); + if (db_date < date) { + LogDebug("Url for " << issuer << " in database is older. Update is needed"); + // Url in DB is older - update is needed + SqlConnection::DataCommandAutoPtr updateUrlCommand = + m_connection->PrepareDataCommand(DB_CMD_UPDATE_URL); + updateUrlCommand->BindString(101, issuer.c_str()); + updateUrlCommand->BindString(102, url.c_str()); + updateUrlCommand->BindInt64(103, date); + updateUrlCommand->Step(); + } else // Url in DB is up-to-date, no need for update + LogDebug("Url for " << issuer << " in databse is up-to-date. No update needed"); + + } else { // No url in database for this issuer, add the new one + LogDebug("No url for "<< issuer << " in databse. Adding the new one."); + SqlConnection::DataCommandAutoPtr setUrlCommand = + m_connection->PrepareDataCommand(DB_CMD_SET_URL); + setUrlCommand->BindString(101, issuer.c_str()); + setUrlCommand->BindString(102, url.c_str()); + setUrlCommand->BindInt64(103, date); + setUrlCommand->Step(); + } + m_connection->CommitTransaction(); +} + +bool SqlQuery::check_if_app_exists(const app_t &app) +{ + int32_t check_id; + return get_check_id(app, check_id); +} + +bool SqlQuery::get_check_id(const app_t &app, int32_t &check_id) +{ + SqlConnection::DataCommandAutoPtr getCheckIDCommand = + m_connection->PrepareDataCommand(DB_CMD_GET_CHECK_ID); + getCheckIDCommand->BindString(104, app.app_id.c_str()); + getCheckIDCommand->BindString(105, app.pkg_id.c_str()); + getCheckIDCommand->BindInt64(106, app.uid); + if (getCheckIDCommand->Step()) { + check_id = getCheckIDCommand->GetColumnInt32(0); + LogDebug("Found check id: " << check_id << ", for app: " << app.app_id); + return true; + } + LogDebug("No check_id for app: " << app.app_id << " in database"); + return false; +} + +bool SqlQuery::add_app_to_check_list(const app_t &app) +{ + //Check if app exists in DB + if (check_if_app_exists(app)) { + LogDebug(app.str() << " already exists in database"); + return true; + } + + m_connection->BeginTransaction(); + //Add app to to_check table + SqlConnection::DataCommandAutoPtr addAppCommand = + m_connection->PrepareDataCommand(DB_CMD_ADD_APP); + addAppCommand->BindString(104, app.app_id.c_str()); + addAppCommand->BindString(105, app.pkg_id.c_str()); + addAppCommand->BindInt64(106, app.uid); + addAppCommand->BindInt32(109, static_cast<int32_t>(app_t::verified_t::UNKNOWN)); // Set app as not-verified + addAppCommand->Step(); + LogDebug("App " << app.app_id << " added to to_check table, adding certificates."); + + // Get check_id + int32_t check_id; + if (get_check_id(app, check_id)) { + // If get check_id succeed we can add certificates to database + for (const auto &iter : app.certificates) { + SqlConnection::DataCommandAutoPtr addCertCommand = + m_connection->PrepareDataCommand(DB_CMD_ADD_CERT); + addCertCommand->BindInt32(107, check_id); + addCertCommand->BindString(108, iter.c_str()); + addCertCommand->Step(); + LogDebug("Certificate for app " << app.app_id << "added"); + } + m_connection->CommitTransaction(); + return true; + } else { // If get check_id failed return false; + LogDebug("Failed while addind app "<< app.app_id << " to to_check table."); + m_connection->RollbackTransaction(); + return false; + } +} + +void SqlQuery::remove_app_from_check_list(const app_t &app) +{ + LogDebug("Removing app: " << app.str()); + + //Remove app from to_check table + SqlConnection::DataCommandAutoPtr removeAppCommand = + m_connection->PrepareDataCommand(DB_CMD_REMOVE_APP); + removeAppCommand->BindString(104, app.app_id.c_str()); + removeAppCommand->BindString(105, app.pkg_id.c_str()); + removeAppCommand->BindInt32(106, app.uid); + removeAppCommand->Step(); + LogDebug("Removed app: " << app.str()); + + // Removing certificates should be done automatically by DB because of + // ON DELETE CASCADE for check_id +} + +void SqlQuery::mark_as_verified(const app_t &app, const app_t::verified_t &verified) +{ + int32_t check_id; + + if (get_check_id(app, check_id)) { + SqlConnection::DataCommandAutoPtr setVerifiedCommand = + m_connection->PrepareDataCommand(DB_CMD_SET_APP_AS_VERIFIED); + setVerifiedCommand->BindInt32(107, check_id); + setVerifiedCommand->BindInt32(109, static_cast<int32_t>(verified)); + setVerifiedCommand->Step(); + LogDebug("App: " << app.str() << " marked as verified: " << static_cast<int32_t>(verified)); + } +} + +void SqlQuery::get_apps(std::list<app_t> &apps_buffer) +{ + // This function will fill buffer with check_id, app_id, pkg_id, uid and verified fields - + // it leaves certificates' list empty. + SqlConnection::DataCommandAutoPtr getAppsCommand = + m_connection->PrepareDataCommand(DB_CMD_GET_APPS); + + while (getAppsCommand->Step()) { + app_t app; + app.check_id = getAppsCommand->GetColumnInt32(0); + app.app_id = getAppsCommand->GetColumnString(1); + app.pkg_id = getAppsCommand->GetColumnString(2); + app.uid = getAppsCommand->GetColumnInt64(3); + app.verified = static_cast<app_t::verified_t>(getAppsCommand->GetColumnInt32(4)); + app.certificates = {}; + LogDebug("App read from DB: app_id: " << app.str() << ", verified: " << static_cast<int32_t>(app.verified)); + apps_buffer.push_back(app); + } +} + +void SqlQuery::get_app_list(std::list<app_t> &apps_buffer) +{ + get_apps(apps_buffer); + + // Get certificates for apps + for (auto &iter : apps_buffer) { + SqlConnection::DataCommandAutoPtr getCertsCommand = + m_connection->PrepareDataCommand(DB_CMD_GET_CERTS); + getCertsCommand->BindInt32(107, iter.check_id); + + while (getCertsCommand->Step()) { + iter.certificates.push_back(getCertsCommand->GetColumnString(0)); + } + } +} + +} // DB +} // CCHECKER diff --git a/src/dpl/core/include/dpl/colors.h b/src/dpl/core/include/dpl/colors.h new file mode 100644 index 0000000..d652f5b --- /dev/null +++ b/src/dpl/core/include/dpl/colors.h @@ -0,0 +1,74 @@ +/* + * Copyright (c) 2011 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 colors.h + * @author Lukasz Wrzosek (l.wrzosek@samsung.com) + * @version 1.0 + * @brief Some constants with definition of colors for Console + * and html output + */ + +#ifndef CCHECKER_COLORS_H +#define CCHECKER_COLORS_H + +namespace CCHECKER { +namespace Colors { +namespace Text { +extern const char* BOLD_GREEN_BEGIN; +extern const char* BOLD_GREEN_END; +extern const char* PURPLE_BEGIN; +extern const char* PURPLE_END; +extern const char* RED_BEGIN; +extern const char* RED_END; +extern const char* GREEN_BEGIN; +extern const char* GREEN_END; +extern const char* CYAN_BEGIN; +extern const char* CYAN_END; +extern const char* BOLD_RED_BEGIN; +extern const char* BOLD_RED_END; +extern const char* BOLD_YELLOW_BEGIN; +extern const char* BOLD_YELLOW_END; +extern const char* BOLD_GOLD_BEGIN; +extern const char* BOLD_GOLD_END; +extern const char* BOLD_WHITE_BEGIN; +extern const char* BOLD_WHITE_END; +extern const char* COLOR_END; +} //namespace Text + +namespace Html { +extern const char* BOLD_GREEN_BEGIN; +extern const char* BOLD_GREEN_END; +extern const char* PURPLE_BEGIN; +extern const char* PURPLE_END; +extern const char* RED_BEGIN; +extern const char* RED_END; +extern const char* GREEN_BEGIN; +extern const char* GREEN_END; +extern const char* CYAN_BEGIN; +extern const char* CYAN_END; +extern const char* BOLD_RED_BEGIN; +extern const char* BOLD_RED_END; +extern const char* BOLD_YELLOW_BEGIN; +extern const char* BOLD_YELLOW_END; +extern const char* BOLD_GOLD_BEGIN; +extern const char* BOLD_GOLD_END; +extern const char* BOLD_WHITE_BEGIN; +extern const char* BOLD_WHITE_END; +} //namespace Html +} //namespace Colors +} //namespace CCHECKER + +#endif /* CCHECKER_COLORS_H */ diff --git a/src/dpl/core/src/assert.cpp b/src/dpl/core/src/assert.cpp index f317dd6..c8aed8f 100644 --- a/src/dpl/core/src/assert.cpp +++ b/src/dpl/core/src/assert.cpp @@ -24,7 +24,7 @@ #include <cstdlib> #include <dpl/exception.h> -#include <log.h> +#include <cchecker/log.h> namespace CCHECKER { void AssertProc(const char *condition, diff --git a/src/dpl/core/src/colors.cpp b/src/dpl/core/src/colors.cpp new file mode 100644 index 0000000..25feadf --- /dev/null +++ b/src/dpl/core/src/colors.cpp @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2011 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 colors.cpp + * @author Lukasz Wrzosek (l.wrzosek@samsung.com) + * @version 1.0 + * @brief Some constants with definition of colors for Console + * and html output + */ +#include <stddef.h> +#include <dpl/colors.h> + +namespace CCHECKER { +namespace Colors { +namespace Text { +const char* BOLD_GREEN_BEGIN = "\033[1;32m"; +const char* BOLD_GREEN_END = "\033[m"; +const char* RED_BEGIN = "\033[0;31m"; +const char* RED_END = "\033[m"; +const char* PURPLE_BEGIN = "\033[0;35m"; +const char* PURPLE_END = "\033[m"; +const char* GREEN_BEGIN = "\033[0;32m"; +const char* GREEN_END = "\033[m"; +const char* CYAN_BEGIN = "\033[0;36m"; +const char* CYAN_END = "\033[m"; +const char* BOLD_RED_BEGIN = "\033[1;31m"; +const char* BOLD_RED_END = "\033[m"; +const char* BOLD_YELLOW_BEGIN = "\033[1;33m"; +const char* BOLD_YELLOW_END = "\033[m"; +const char* BOLD_GOLD_BEGIN = "\033[0;33m"; +const char* BOLD_GOLD_END = "\033[m"; +const char* BOLD_WHITE_BEGIN = "\033[1;37m"; +const char* BOLD_WHITE_END = "\033[m"; +const char* COLOR_END = "\033[m"; +} //namespace Text + +namespace Html { +const char* BOLD_GREEN_BEGIN = "<font color=\"green\"><b>"; +const char* BOLD_GREEN_END = "</b></font>"; +const char* PURPLE_BEGIN = "<font color=\"purple\"><b>"; +const char* PURPLE_END = "</b></font>"; +const char* RED_BEGIN = "<font color=\"red\"><b>"; +const char* RED_END = "</b></font>"; +const char* GREEN_BEGIN = "<font color=\"green\">"; +const char* GREEN_END = "</font>"; +const char* CYAN_BEGIN = "<font color=\"cyan\">"; +const char* CYAN_END = "</font>"; +const char* BOLD_RED_BEGIN = "<font color=\"red\"><b>"; +const char* BOLD_RED_END = "</b></font>"; +const char* BOLD_YELLOW_BEGIN = "<font color=\"yellow\"><b>"; +const char* BOLD_YELLOW_END = "</b></font>"; +const char* BOLD_GOLD_BEGIN = "<font color=\"gold\"><b>"; +const char* BOLD_GOLD_END = "</b></font>"; +const char* BOLD_WHITE_BEGIN = "<font color=\"white\"><b>"; +const char* BOLD_WHITE_END = "</b></font>"; +} //namespace Html +} //namespace Colors +} //namespace CCHECKER diff --git a/src/dpl/core/src/exception.cpp b/src/dpl/core/src/exception.cpp index 7b936fe..5f2e8e0 100644 --- a/src/dpl/core/src/exception.cpp +++ b/src/dpl/core/src/exception.cpp @@ -23,7 +23,7 @@ #include <dpl/exception.h> #include <cstdio> -#include <log.h> +#include <cchecker/log.h> namespace CCHECKER { Exception* Exception::m_lastException = NULL; diff --git a/src/dpl/core/src/string.cpp b/src/dpl/core/src/string.cpp index 5d33ba8..cdc83ec 100644 --- a/src/dpl/core/src/string.cpp +++ b/src/dpl/core/src/string.cpp @@ -33,7 +33,7 @@ #include <iconv.h> #include <unicode/ustring.h> -#include <log.h> +#include <cchecker/log.h> // TODO: Completely move to ICU namespace CCHECKER { diff --git a/src/dpl/db/include/dpl/db/sql_connection.h b/src/dpl/db/include/dpl/db/sql_connection.h index 07cfe13..321372b 100644 --- a/src/dpl/db/include/dpl/db/sql_connection.h +++ b/src/dpl/db/include/dpl/db/sql_connection.h @@ -33,7 +33,7 @@ #include <memory> #include <stdint.h> -#include <log.h> +#include <cchecker/log.h> namespace CCHECKER { namespace DB { @@ -486,6 +486,12 @@ class SqlConnection * @return Row ID */ RowID GetLastInsertRowID() const; + + void BeginTransaction(); + + void RollbackTransaction(); + + void CommitTransaction(); }; } // namespace DB } // namespace CCHECKER diff --git a/src/dpl/db/src/sql_connection.cpp b/src/dpl/db/src/sql_connection.cpp index 0680a58..27ea4fd 100644 --- a/src/dpl/db/src/sql_connection.cpp +++ b/src/dpl/db/src/sql_connection.cpp @@ -846,5 +846,21 @@ SqlConnection::AllocDefaultSynchronizationObject() { return new NaiveSynchronizationObject(); } + +void SqlConnection::BeginTransaction() +{ + ExecCommand("BEGIN;"); +} + +void SqlConnection::RollbackTransaction() +{ + ExecCommand("ROLLBACK;"); +} + +void SqlConnection::CommitTransaction() +{ + ExecCommand("COMMIT;"); +} + } // namespace DB } // namespace CCHECKER diff --git a/src/include/app.h b/src/include/cchecker/app.h index 7452714..c7dff35 100644 --- a/src/include/app.h +++ b/src/include/cchecker/app.h @@ -29,7 +29,7 @@ namespace CCHECKER { struct app_t { - enum class verified_t : int { + enum class verified_t : int32_t { NO = 0, YES = 1, UNKNOWN = 2 @@ -39,10 +39,14 @@ struct app_t { std::string app_id; std::string pkg_id; uid_t uid; - std::vector<std::string> certificates; + std::vector<std::string> certificates; //TODO: add typedef verified_t verified; app_t(void); + app_t(const std::string &app_id, + const std::string &pkg_id, + uid_t uid, + const std::vector<std::string> &certificates); std::string str(void) const; }; diff --git a/src/log/log.h b/src/include/cchecker/log.h index d4efcdc..d4efcdc 100644 --- a/src/log/log.h +++ b/src/include/cchecker/log.h diff --git a/src/include/logic.h b/src/include/cchecker/logic.h index c2b793d..d80ea51 100644 --- a/src/include/logic.h +++ b/src/include/cchecker/logic.h @@ -27,23 +27,29 @@ #include <package_manager.h> #include <string> #include <vector> +#include <list> -#include <app.h> +#include <cchecker/app.h> namespace CCHECKER { +namespace DB { +class SqlQuery; +} + enum error_t { NO_ERROR, REGISTER_CALLBACK_ERROR, DBUS_ERROR, - PACKAGE_MANAGER_ERROR + PACKAGE_MANAGER_ERROR, + DATABASE_ERROR }; class Logic { public: Logic(void); virtual ~Logic(void); - int setup(); + error_t setup(); static void pkg_manager_callback( const char *type, const char *package, @@ -61,14 +67,16 @@ class Logic { private: //TODO: implement missing members + error_t setup_db(); void check_ocsp(app_t &app); - void add_ocsp_url(const std::string &issuer, const std::string &url); + void add_ocsp_url(const std::string &issuer, const std::string &url, int64_t date); void pkgmanager_uninstall(const app_t &app); void get_certs_from_signature(const std::string &signature, std::vector<std::string> &cert); - error_t load_database_to_buffer(); - - error_t register_connman_signal_handler (); + void load_database_to_buffer(); + error_t register_connman_signal_handler(void); + std::list<app_t> m_buffer; + DB::SqlQuery *m_sqlquery; bool m_is_online; package_manager_h m_request; GDBusProxy *m_proxy; diff --git a/src/include/cchecker/sql_query.h b/src/include/cchecker/sql_query.h new file mode 100644 index 0000000..3996b75 --- /dev/null +++ b/src/include/cchecker/sql_query.h @@ -0,0 +1,76 @@ +/* + * Copyright (c) 2015 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 sql_query.h + * @author Janusz Kozerski (j.kozerski@samsung.com) + * @version 1.0 + * @brief This file is the implementation of SQL queries + */ + +#include <list> +#include <string> + +#include <dpl/db/sql_connection.h> +#include <cchecker/app.h> + +namespace CCHECKER { +namespace DB { +class SqlQuery { + public: + class Exception { + public: DECLARE_EXCEPTION_TYPE(CCHECKER::Exception, Base); + public: DECLARE_EXCEPTION_TYPE(Base, InternalError); + public: DECLARE_EXCEPTION_TYPE(Base, TransactionError); + public: DECLARE_EXCEPTION_TYPE(Base, InvalidArgs); + }; + SqlQuery() : + m_connection(NULL), + m_inUserTransaction(false) + {}; + explicit SqlQuery(const std::string &path); + virtual ~SqlQuery(); + + // Connecting outside the constructor + bool connect(const std::string& path); + + // OCSP urls + /** + * Returns true if url has been found in database, + * or false in other case. + */ + bool get_url(const std::string &issuer, std::string &url); + void set_url(const std::string &issuer, const std::string &url, const int64_t &date); + + // Apps + bool add_app_to_check_list(const app_t &app); + void remove_app_from_check_list(const app_t &app); + void mark_as_verified(const app_t &app, const app_t::verified_t &verified); + void get_app_list(std::list<app_t> &apps_buffer); // TODO: typedef std::list<app_t> + + protected: + SqlConnection *m_connection; + + private: + bool m_inUserTransaction; + int getDBVersion(void); + void get_apps(std::list<app_t> &apps_buffer); + bool check_if_app_exists(const app_t &app); + bool get_check_id(const app_t &app, int32_t &check_id); + int verified_enum_to_int(const app_t::verified_t &verified); + app_t::verified_t verified_int_to_enum(const int &verified); +}; +} // DB +} // CCHECKER diff --git a/src/logic.cpp b/src/logic.cpp index 10ed0fd..c43c657 100644 --- a/src/logic.cpp +++ b/src/logic.cpp @@ -19,9 +19,14 @@ * @version 1.0 * @brief This file is the implementation of SQL queries */ +#include <stdexcept> +#include <tzplatform_config.h> -#include <logic.h> -#include <log.h> +#include <cchecker/logic.h> +#include <cchecker/log.h> +#include <cchecker/sql_query.h> + +using namespace std; namespace { @@ -51,21 +56,50 @@ const char * eventStateStr(package_manager_event_state_e type) { namespace CCHECKER { +const char *const DB_PATH = tzplatform_mkpath(TZ_SYS_DB, ".cert-checker.db"); + Logic::~Logic(void) { LogDebug("Cert-checker cleaning."); if (m_proxy) g_object_unref(m_proxy); package_manager_destroy(m_request); + delete m_sqlquery; } Logic::Logic(void) : + m_sqlquery(NULL), m_is_online(false), m_proxy(NULL) {} -int Logic::setup() +error_t Logic::setup_db() +{ + // TODO: If database doesn't exist -should we create a new one? + Try { + m_sqlquery = new DB::SqlQuery(DB_PATH); + } Catch (runtime_error) { + LogError("Error while creating SqlQuery object"); + return DATABASE_ERROR; + } + + if(!m_sqlquery) { + LogError("Cannot open database"); + return DATABASE_ERROR; + } + + return NO_ERROR; +} + +error_t Logic::setup() { + // Check if DB exists and create a new one if it doesn't + error_t err = setup_db(); + if (err != NO_ERROR) { + LogError("Database error"); + return err; + } + // Add package manager callback int ret = package_manager_create(&m_request); if (ret != PACKAGE_MANAGER_ERROR_NONE) { @@ -89,7 +123,9 @@ int Logic::setup() } LogDebug("register connman event callback success"); - return load_database_to_buffer(); + load_database_to_buffer(); + + return NO_ERROR; } error_t Logic::register_connman_signal_handler(void) @@ -161,14 +197,14 @@ void Logic::connman_callback(GDBusProxy */*proxy*/, GVariant *parameters, void *logic_ptr) { - std::string signal_name_str = std::string(signal_name); + string signal_name_str = string(signal_name); if (signal_name_str != "PropertyChanged") { // Invalid param. Nothing to do here. return; } gchar *parameters_g = g_variant_print(parameters, TRUE); - std::string params_str = std::string(parameters_g); + string params_str = string(parameters_g); g_free (parameters_g); Logic *logic = static_cast<Logic*> (logic_ptr); @@ -188,10 +224,9 @@ void Logic::check_ocsp(app_t &app) (void)app; } -void Logic::add_ocsp_url(const std::string &issuer, const std::string &url) +void Logic::add_ocsp_url(const string &issuer, const string &url, int64_t date) { - (void)issuer; - (void)url; + m_sqlquery->set_url(issuer, url, date); } void Logic::pkgmanager_uninstall(const app_t &app) @@ -199,15 +234,16 @@ void Logic::pkgmanager_uninstall(const app_t &app) (void)app; } -void Logic::get_certs_from_signature(const std::string &signature, std::vector<std::string> &cert) +void Logic::get_certs_from_signature(const string &signature, vector<string> &cert) { (void)signature; (void)cert; } -error_t Logic::load_database_to_buffer() +void Logic::load_database_to_buffer() { - return error_t::NO_ERROR; + LogDebug("Loading database to the buffer"); + m_sqlquery->get_app_list(m_buffer); } } //CCHECKER |