From: Konrad Lipinski Date: Mon, 7 Nov 2016 16:05:44 +0000 (+0100) Subject: turn DbAdapter into direct member of XmlParser X-Git-Tag: submit/tizen_3.0/20161109.081209~1 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=1549df0b26957a918dea2a1c94ee6f410f88dab0;p=platform%2Fcore%2Fsystem%2Flibdbuspolicy.git turn DbAdapter into direct member of XmlParser Change-Id: Ic807550cabd2cad3177c35a6af14d142b29eaf0b (cherry picked from commit 1c8bd17ca32a21ca16f2bf625fe788eacf2e0a72) --- diff --git a/src/internal/internal.cpp b/src/internal/internal.cpp index 1445f7b..12c54fa 100644 --- a/src/internal/internal.cpp +++ b/src/internal/internal.cpp @@ -28,8 +28,6 @@ #include "../libdbuspolicy1-private.h" -static ldp_xml_parser::NaivePolicyChecker policy_checker; - static const char* get_str(const char* const szstr) { return (szstr != NULL) ? szstr : ""; } @@ -37,7 +35,6 @@ static const char* get_str(const char* const szstr) { int __internal_init(bool bus_type, const char* const config_name) { ldp_xml_parser::XmlParser p; - p.registerAdapter(policy_checker.generateAdapter()); auto err = p.parsePolicy(bus_type, get_str(config_name)); return err.get(); } diff --git a/src/internal/naive_policy_checker.cpp b/src/internal/naive_policy_checker.cpp index 5ebaac1..2fd0b4a 100644 --- a/src/internal/naive_policy_checker.cpp +++ b/src/internal/naive_policy_checker.cpp @@ -9,6 +9,8 @@ using namespace ldp_xml_parser; +ldp_xml_parser::NaivePolicyChecker policy_checker; + static void __log_item(const MatchItemSR& item) { char tmp[MAX_LOG_LINE]; @@ -16,12 +18,6 @@ static void __log_item(const MatchItemSR& item) std::cout << "checkpolicy for: " << i_str <checkItemSR(bus_type, uid, gid, label, matcher, type); } + Decision NaivePolicyChecker::checkPolicySR(const NaivePolicyDb::PolicySR& policy, const MatchItemSR& item, const char*& privilege) diff --git a/src/internal/naive_policy_checker.hpp b/src/internal/naive_policy_checker.hpp index 8504a14..3471cae 100644 --- a/src/internal/naive_policy_checker.hpp +++ b/src/internal/naive_policy_checker.hpp @@ -37,9 +37,6 @@ namespace ldp_xml_parser /** Policy databases for system and session bus */ NaivePolicyDb m_bus_db[2]; - /** Adapter to policies database */ - DbAdapter* m_adapter; - /** Retrieves policy db * \param[in] type Type of database (system/session bus) * \return Returns reference to chosen bus policy db @@ -115,14 +112,14 @@ namespace ldp_xml_parser gid_t gid, const char* label, const ItemOwn& item); - public: - ~NaivePolicyChecker(); - /** Generates adapter for db with policies - * \ingroup Implementation + /** Provides db handle for parsing purposes */ - DbAdapter& generateAdapter(); + inline NaivePolicyDb &db(bool sessionBus) { return m_bus_db[sessionBus]; } + + friend class DbAdapter; // give adapters access to db() + public: /** Checks ownership policy for given item * \param[in] bus_type Bus type (system/session) * \param[in] uid User id @@ -156,4 +153,7 @@ namespace ldp_xml_parser ItemType type); }; } + +extern ldp_xml_parser::NaivePolicyChecker policy_checker; + #endif diff --git a/src/internal/policy.cpp b/src/internal/policy.cpp index 5089349..752750f 100644 --- a/src/internal/policy.cpp +++ b/src/internal/policy.cpp @@ -4,6 +4,7 @@ */ #include "policy.hpp" +#include "naive_policy_checker.hpp" #include "naive_policy_db.hpp" #include "tslog.hpp" #include @@ -42,11 +43,10 @@ static inline const char* __decision_to_str(Decision dec) { return message_decision[static_cast(dec)]; } -DbAdapter::DbAdapter(NaivePolicyDb& system, NaivePolicyDb& session) - : __system_db(system), __session_db(session), __attr(false), __tag_state(NONE) { +DbAdapter::DbAdapter() : __attr(false), __tag_state(NONE) { } -uid_t DbAdapter::convertToUid(const char* user) { +static uid_t convertToUid(const char* user) { long val = -1; errno = 0; val = std::strtol(user, NULL, 10); @@ -64,7 +64,7 @@ uid_t DbAdapter::convertToUid(const char* user) { return pwd->pw_uid; } -gid_t DbAdapter::convertToGid(const char* group) { +static gid_t convertToGid(const char* group) { long val = -1; errno = 0; val = std::strtol(group, NULL, 10); @@ -182,12 +182,8 @@ void DbAdapter::xmlTraversal(bool bus, updateDecision(v, policy_type, policy_type_value, t, attr); xmlTraversal(bus, v.second, t, policy_type, policy_type_value, attr, level + 1); } - if (!pt.empty() && level > 1) { - if (bus) - __builder.generateItem(__session_db, policy_type, policy_type_value); - else - __builder.generateItem(__system_db, policy_type, policy_type_value); - } + if (!pt.empty() && level > 1) + __builder.generateItem(policy_checker.db(bus), policy_type, policy_type_value); } } diff --git a/src/internal/policy.hpp b/src/internal/policy.hpp index de8619a..5f4d84f 100644 --- a/src/internal/policy.hpp +++ b/src/internal/policy.hpp @@ -247,8 +247,6 @@ namespace ldp_xml_parser POLICY, ALLOW_DENY_CHECK }; - NaivePolicyDb& __system_db; - NaivePolicyDb& __session_db; bool __attr; state __tag_state; ItemBuilder __builder; @@ -265,10 +263,8 @@ namespace ldp_xml_parser bool attr = false, int level = 0); public: - DbAdapter(NaivePolicyDb& system, NaivePolicyDb& session); + DbAdapter(); void updateDb(bool bus, boost::property_tree::ptree& xmlTree, std::vector& incl_dirs); - static uid_t convertToUid(const char* user); - static gid_t convertToGid(const char* group); }; } #endif diff --git a/src/internal/xml_parser.hpp b/src/internal/xml_parser.hpp index 6582479..f874320 100644 --- a/src/internal/xml_parser.hpp +++ b/src/internal/xml_parser.hpp @@ -43,17 +43,13 @@ namespace ldp_xml_parser ErrCode err = parse(bus, fname); return err; } - /** Registers DBAdapter */ - void registerAdapter(DbAdapter& adapter) { - __adapter = &adapter; - } private: /** Vector containing parsed policy */ static std::set __parsed; - /** Adapter which allows to acces parsed policies */ - DbAdapter* __adapter; + /** Adapter which allows to access parsed policies */ + DbAdapter __adapter; /** Parses config file and all files included in it */ ErrCode parse(bool bus, std::string const &filename) { @@ -125,7 +121,7 @@ namespace ldp_xml_parser boost::property_tree::ptree pt; read_xml(filename, pt); if (!pt.empty()) { - __adapter->updateDb(bus, pt, incl_dirs); + __adapter.updateDb(bus, pt, incl_dirs); } } catch (const boost::property_tree::xml_parser::xml_parser_error& ex) { ret.first = ErrCode::error(ex.what());