refactoring: templatize NaivePolicyChecker::check 43/199143/9
authorAdrian Szyndela <adrian.s@samsung.com>
Tue, 5 Feb 2019 13:17:09 +0000 (14:17 +0100)
committerAdrian Szyndela <adrian.s@samsung.com>
Tue, 12 Feb 2019 14:39:23 +0000 (15:39 +0100)
This converts check() methods for MatchItemOwn, MatchItemSend and
MatchItemReceive to single template method.
The MatchItemAccess check() method stays in place as it requires
different params.

Change-Id: Ibbc2c0498bb9289f6dbce5ffd3206abf93115d17

src/internal/internal.cpp
src/internal/naive_policy_checker.cpp
src/internal/naive_policy_checker.hpp

index 6682767..f142cca 100755 (executable)
@@ -28,6 +28,8 @@
 #include "internal.h"
 #include "tslog.hpp"
 
+using namespace ldp_xml_parser;
+
 static const char* get_str(const char* const szstr) {
        return (szstr != NULL) ? szstr : "";
 }
@@ -97,7 +99,7 @@ int __internal_can_send(bool bus_type,
                                                const char* const member,
                                                int type)
 {
-       ldp_xml_parser::MatchItemSend matcher(interface, member, path, static_cast<ldp_xml_parser::MessageType>(type));
+       MatchItemSend matcher(interface, member, path, static_cast<MessageType>(type));
        if (!matcher.addNames(destination)) {
                tslog::log_verbose("Destination too long: ", destination, "\n");
                return false;
@@ -116,7 +118,7 @@ int __internal_can_send_multi_dest(bool bus_type,
                                                                   int type)
 {
        int i = 0;
-       ldp_xml_parser::MatchItemSend matcher(interface, member, path, static_cast<ldp_xml_parser::MessageType>(type));
+       MatchItemSend matcher(interface, member, path, static_cast<MessageType>(type));
        if (destination)
                while (destination[i]) {
                        matcher.addName(destination[i++]);
@@ -134,7 +136,7 @@ int __internal_can_recv(bool bus_type,
                                                const char* const member,
                                                int type)
 {
-       ldp_xml_parser::MatchItemReceive matcher(interface, member, path, static_cast<ldp_xml_parser::MessageType>(type));
+       MatchItemReceive matcher(interface, member, path, static_cast<MessageType>(type));
        if (!matcher.addNames(sender)) {
                tslog::log_verbose("Sender too long: ", sender, "\n");
                return false;
@@ -153,7 +155,7 @@ int __internal_can_recv_multi(bool bus_type,
        int type)
 {
        int i = 0;
-       ldp_xml_parser::MatchItemReceive matcher(interface, member, path, static_cast<ldp_xml_parser::MessageType>(type));
+       MatchItemReceive matcher(interface, member, path, static_cast<MessageType>(type));
        if (sender)
                while (sender[i]) {
                        matcher.addName(sender[i++]);
@@ -168,5 +170,5 @@ int __internal_can_own(bool bus_type,
                                           const char* const label,
                                           const char* const service)
 {
-       return static_cast<int>(policy_checker().check(bus_type, user, group, label, service));
+       return static_cast<int>(policy_checker().check(bus_type, user, group, label, MatchItemOwn(service)));
 }
index c358b97..2ba566d 100755 (executable)
@@ -69,32 +69,18 @@ DecisionResult NaivePolicyChecker::check(bool bus_type,
        return parseDecision(ret, uid, label);
 }
 
+template <typename T>
 DecisionResult NaivePolicyChecker::check(bool bus_type,
                                                           uid_t uid,
                                                           gid_t gid,
                                                           const char* const label,
-                                                          const char* const name) {
-       auto ret = checkItem(bus_type, uid, gid, MatchItemOwn(name));
-       return parseDecision(ret, uid, label);
-}
-
-DecisionResult NaivePolicyChecker::check(bool bus_type,
-                                                          uid_t uid,
-                                                          gid_t gid,
-                                                          const char* const label,
-                                                          MatchItemSend &matcher) {
-       auto ret = checkItem(bus_type, uid, gid, matcher);
-       return parseDecision(ret, uid, label);
-}
-
-DecisionResult NaivePolicyChecker::check(bool bus_type,
-                                                          uid_t uid,
-                                                          gid_t gid,
-                                                          const char* const label,
-                                                          MatchItemReceive &matcher) {
-       auto ret = checkItem(bus_type, uid, gid, matcher);
+                                                          const T &matchItem) {
+       auto ret = checkItem(bus_type, uid, gid, matchItem);
        return parseDecision(ret, uid, label);
 }
+template DecisionResult NaivePolicyChecker::check(bool, uid_t, gid_t, const char *, const MatchItemOwn &);
+template DecisionResult NaivePolicyChecker::check(bool, uid_t, gid_t, const char *, const MatchItemSend &);
+template DecisionResult NaivePolicyChecker::check(bool, uid_t, gid_t, const char *, const MatchItemReceive &);
 
 template<typename T>
 DecisionItem NaivePolicyChecker::checkItem(bool bus_type, uid_t uid, gid_t gid, const T& item) {
index e5e4b79..a1b0cdc 100644 (file)
@@ -113,41 +113,21 @@ namespace ldp_xml_parser
                                   gid_t gid,
                                   const char* const label);
 
-               /** Checks ownership policy for given item
+               /** Checks send/receive/ownership policy for given item
                 * \param[in] bus_type Bus type (system/session)
                 * \param[in] uid User id
                 * \param[in] gid User group id
                 * \param[in] label User label
-                * \param[in] name Name to own
+                * \param[in] matchItem MatchItem to check
                 * \return Returns deny=0, allow=1 or cynara error
                 * \ingroup Implementation
                 */
+               template <typename T>
                DecisionResult check(bool bus_type,
                                   uid_t uid,
                                   gid_t gid,
                                   const char* const label,
-                                  const char* const name);
-
-               /** Checks send/receive policy for given item
-                * \param[in] bus_type Bus type (system/session)
-                * \param[in] uid User id
-                * \param[in] gid User group id
-                * \param[in] label User label
-                * \param[in] matcher Structure with multiple names to check
-                * \return Returns deny=0, allow=1 or cynara error
-                * \ingroup Implementation
-                */
-               DecisionResult check(bool bus_type,
-                                  uid_t uid,
-                                  gid_t gid,
-                                  const char* const label,
-                                  MatchItemSend &matcher);
-
-               DecisionResult check(bool bus_type,
-                                  uid_t uid,
-                                  gid_t gid,
-                                  const char* const label,
-                                  MatchItemReceive &matcher);
+                                  const T &matchItem);
        };
 }