refactoring: merge 'can' with 'can_multi' functions 48/202548/6
authorAdrian Szyndela <adrian.s@samsung.com>
Fri, 29 Mar 2019 14:22:52 +0000 (15:22 +0100)
committerAdrian Szyndela <adrian.s@samsung.com>
Wed, 3 Apr 2019 07:07:01 +0000 (09:07 +0200)
Change-Id: I17889df47ac3a5f740eb528f7f56c3e3b8b7edc5

src/internal/internal.cpp
src/internal/internal.h
src/libdbuspolicy1.cpp
src/test-libdbuspolicy1-method.cpp
src/test-libdbuspolicy1-send_destination_prefix-deny.cpp
src/test-libdbuspolicy1-signal.cpp

index 8ad20ea..108a9e7 100644 (file)
@@ -102,35 +102,24 @@ int __internal_can_send(BusType bus_type,
                                                const gid_t group,
                                                const char* const label,
                                                const char* const destination,
+                                               const char** const destination_names,
+                                               uint16_t destination_names_size,
                                                const char* const path,
                                                const char* const interface,
                                                const char* const member,
                                                int type)
 {
        MatchItemSend matcher(interface, member, path, static_cast<MessageType>(type));
-       if (!matcher.addNames(destination)) {
-               tslog::log_verbose("Destination too long: ", destination, "\n");
-               return false;
-       }
-       return static_cast<int>(policy_checker(bus_type).check(user, group, label, matcher));
-}
-
-int __internal_can_send_multi_dest(BusType bus_type,
-                                                                  const uid_t user,
-                                                                  const gid_t group,
-                                                                  const char* const label,
-                                                                  const char** const destination,
-                                                                  uint16_t destination_size,
-                                                                  const char* const path,
-                                                                  const char* const interface,
-                                                                  const char* const member,
-                                                                  int type)
-{
-       MatchItemSend matcher(interface, member, path, static_cast<MessageType>(type));
-       if (destination)
-               for (size_t i = 0; i < destination_size; i++) {
-                       matcher.addName(destination[i]);
+       if (destination_names_size == 0) {
+               if (!matcher.addNames(destination)) {
+                       tslog::log_verbose("Destination too long: ", destination, "\n");
+                       return false;
                }
+       } else {
+               assert(destination_names);
+               for (size_t i = 0; i < destination_names_size; i++)
+                       matcher.addName(destination_names[i]);
+       }
        return static_cast<int>(policy_checker(bus_type).check(user, group, label, matcher));
 }
 
@@ -139,39 +128,27 @@ int __internal_can_recv(BusType bus_type,
                                                const gid_t group,
                                                const char* const label,
                                                const char* const sender,
+                                               const char** const sender_names,
+                                               uint16_t sender_names_size,
                                                const char* const path,
                                                const char* const interface,
                                                const char* const member,
                                                int type)
 {
        MatchItemReceive matcher(interface, member, path, static_cast<MessageType>(type));
-       if (!matcher.addNames(sender)) {
-               tslog::log_verbose("Sender too long: ", sender, "\n");
-               return false;
-       }
-       return static_cast<int>(policy_checker(bus_type).check(user, group, label, matcher));
-}
-
-int __internal_can_recv_multi(BusType bus_type,
-       const uid_t user,
-       const gid_t group,
-       const char* const label,
-       const char** const sender,
-       uint16_t sender_size,
-       const char* const path,
-       const char* const interface,
-       const char* const member,
-       int type)
-{
-       MatchItemReceive matcher(interface, member, path, static_cast<MessageType>(type));
-       if (sender)
-               for (size_t i = 0; i < sender_size; i++) {
-                       matcher.addName(sender[i]);
+       if (0 == sender_names_size) {
+               if (!matcher.addNames(sender)) {
+                       tslog::log_verbose("Sender too long: ", sender, "\n");
+                       return false;
                }
+       } else {
+               assert(sender_names);
+               for (size_t i = 0; i < sender_names_size; i++)
+                       matcher.addName(sender_names[i]);
+       }
        return static_cast<int>(policy_checker(bus_type).check(user, group, label, matcher));
 }
 
-
 int __internal_can_own(BusType bus_type,
                                           const uid_t user,
                                           const gid_t group,
index 92a1acc..c22a5d0 100644 (file)
 #ifndef _LIBDBUSPOLICY1_INTERNAL_H_
 #define _LIBDBUSPOLICY1_INTERNAL_H_
 
-#ifdef __cplusplus
-extern "C" {
-#endif
-
 #include <pthread.h>
 #include <sys/types.h>
 #include <stdbool.h>
@@ -96,6 +92,8 @@ int __internal_can_open(BusType bus_type,
  * \param[in] group User group id
  * \param[in] label Sender label
  * \param[in] destination Message destination
+ * \param[in] destination_names Array containing names owned by the destination
+ * \param[in] destination_names_size Size of destination_names array
  * \param[in] path Path
  * \param[in] interface Interface name
  * \param[in] member Member name
@@ -107,40 +105,21 @@ int __internal_can_send(BusType bus_type,
                                                const gid_t  group,
                                                const char* const label,
                                                const char* const destination,
+                                               const char** const destination_names,
+                                               uint16_t destination_names_size,
                                                const char* const path,
                                                const char* const interface,
                                                const char* const member,
                                                int type);
 
-/** Checks if user can send messages (multiple destinations).
- * \param[in] bus_type Bus type (system/session)
- * \param[in] user User id
- * \param[in] group User group id
- * \param[in] label Sender label
- * \param[in] destination Message destination
- * \param[in] path Path
- * \param[in] interface Interface name
- * \param[in] member Member name
- * \param[in] type Message type
- * \return 1 on allow, 0 on deny, negative error code otherwise
- */
-int __internal_can_send_multi_dest(BusType bus_type,
-                                                                  const uid_t user,
-                                                                  const gid_t group,
-                                                                  const char* const label,
-                                                                  const char** const destination,
-                                                                  uint16_t destination_size,
-                                                                  const char* const path,
-                                                                  const char* const interface,
-                                                                  const char* const member,
-                                                                  int type);
-
 /** Check if user can receive messages.
  * \param[in] bus_type Bus type (system/session)
  * \param[in] user User id
  * \param[in] group User group id
  * \param[in] label User label
  * \param[in] sender Sender of received message
+ * \param[in] sender_names Array containing names owned by the sender
+ * \param[in] sender_names_size Size of sender_names array
  * \param[in] path Path
  * \param[in] interface Interface name
  * \param[in] member Member name
@@ -152,29 +131,8 @@ int __internal_can_recv(BusType bus_type,
                                                gid_t group,
                                                const char* const label,
                                                const char* const sender,
-                                               const char* const path,
-                                               const char* const interface,
-                                               const char* const member,
-                                               int type);
-
-/** Check if user can receive messages.
- * \param[in] bus_type Bus type (system/session)
- * \param[in] user User id
- * \param[in] group User group id
- * \param[in] label User label
- * \param[in] sender Sender of received message
- * \param[in] path Path
- * \param[in] interface Interface name
- * \param[in] member Member name
- * \param[in] type Message type
- * \return 1 on allow, 0 on deny, negative error code otherwise
- */
-int __internal_can_recv_multi(BusType bus_type,
-                                               uid_t user,
-                                               gid_t group,
-                                               const char* const label,
-                                               const char** const sender,
-                                               uint16_t sender_size,
+                                               const char** const sender_names,
+                                               uint16_t sender_names_size,
                                                const char* const path,
                                                const char* const interface,
                                                const char* const member,
@@ -193,8 +151,4 @@ int __internal_can_own(BusType bus_type,
                                           gid_t group,
                                           const char* const label,
                                           const char* const service);
-#ifdef __cplusplus
-} /* extern "C" */
-#endif
-
 #endif
index 9e32499..91063b1 100644 (file)
@@ -279,10 +279,10 @@ DBUSPOLICY1_EXPORT int dbuspolicy1_check_out(void* configuration,
                free_conn_info = true;
        }
 
-       if (0 == info.names_num)
-               r = __internal_can_send(kconn->bus_type, g_udesc.uid, g_udesc.gid, g_udesc.label, destination, path, interface, member, message_type);
-       else
-               r = __internal_can_send_multi_dest(kconn->bus_type, g_udesc.uid, g_udesc.gid, g_udesc.label, info.k_names, info.names_num, path, interface, member, message_type);
+       r = __internal_can_send(kconn->bus_type,
+                       g_udesc.uid, g_udesc.gid, g_udesc.label,
+                       destination, info.k_names, info.names_num,
+                       path, interface, member, message_type);
 
        if (r <= 0)
                goto end;
@@ -310,10 +310,10 @@ DBUSPOLICY1_EXPORT int dbuspolicy1_check_out(void* configuration,
        }
        free_conn_info = true;
 
-       if (0 == info.names_num)
-               r = __internal_can_recv(kconn->bus_type, dest_uid_n, dest_gid_n, dest_label.c_str(), sender, path, interface, member, message_type);
-       else
-               r = __internal_can_recv_multi(kconn->bus_type, dest_uid_n, dest_gid_n, dest_label.c_str(), info.k_names, info.names_num, path, interface, member, message_type);
+       r = __internal_can_recv(kconn->bus_type,
+                       dest_uid_n, dest_gid_n, dest_label.c_str(),
+                       sender, info.k_names, info.names_num,
+                       path, interface, member, message_type);
 
 end:
        if (free_conn_info)
@@ -353,10 +353,10 @@ DBUSPOLICY1_EXPORT int dbuspolicy1_check_in(void* configuration,
                        goto end;
        }
 
-       if (0 == info.names_num)
-               r = __internal_can_send(kconn->bus_type, sender_uid, sender_gid, sender_label, destination, path, interface, member, message_type);
-       else
-               r = __internal_can_send_multi_dest(kconn->bus_type, sender_uid, sender_gid, sender_label, info.k_names, info.names_num, path, interface, member, message_type);
+       r = __internal_can_send(kconn->bus_type,
+                       sender_uid, sender_gid, sender_label,
+                       destination, info.k_names, info.names_num,
+                       path, interface, member, message_type);
 
        if (r <= 0)
                goto end;
@@ -366,7 +366,11 @@ DBUSPOLICY1_EXPORT int dbuspolicy1_check_in(void* configuration,
        /* it is not needed to use __internal_can_recv_multi here. */
        if (!sender)
                sender = ":";
-       r = __internal_can_recv(kconn->bus_type, g_udesc.uid, g_udesc.gid, g_udesc.label, sender, path, interface, member, message_type);
+       r = __internal_can_recv(kconn->bus_type,
+                       g_udesc.uid, g_udesc.gid, g_udesc.label,
+                       sender, nullptr, 0,
+                       path, interface, member, message_type);
+
        if (r <= 0)
                goto end;
 end:
index d7db435..85ce088 100644 (file)
@@ -68,9 +68,17 @@ bool method_test() {
        for (i = 0; i < sizeof(method_tests)/sizeof(struct MethodTest); i++) {
                if (method_tests[i].recv_send == MessageDirection::SEND)
                {
-                       ret = __internal_can_send(SYSTEM_BUS, method_tests[i].user, method_tests[i].group, method_tests[i].label, method_tests[i].name,  method_tests[i].path, method_tests[i].interface, method_tests[i].member, static_cast<int>(method_tests[i].type));
+                       ret = __internal_can_send(SYSTEM_BUS,
+                                       method_tests[i].user, method_tests[i].group, method_tests[i].label,
+                                       method_tests[i].name, nullptr, 0,
+                                       method_tests[i].path, method_tests[i].interface, method_tests[i].member,
+                                       static_cast<int>(method_tests[i].type));
                } else if (method_tests[i].recv_send == MessageDirection::RECEIVE) {
-                       ret = __internal_can_recv(SYSTEM_BUS, method_tests[i].user,  method_tests[i].group, method_tests[i].label,  method_tests[i].name, method_tests[i].path, method_tests[i].interface, method_tests[i].member, static_cast<int>(method_tests[i].type));
+                       ret = __internal_can_recv(SYSTEM_BUS,
+                                       method_tests[i].user, method_tests[i].group, method_tests[i].label,
+                                       method_tests[i].name, nullptr, 0,
+                                       method_tests[i].path, method_tests[i].interface, method_tests[i].member,
+                                       static_cast<int>(method_tests[i].type));
                }
                if ( (int)((method_tests[i].expected_result)) != ret) {
                        printf("[ERROR][%d] method test failed: %d %d ", i, (int)((method_tests[i].expected_result)), ret);
index 2f2fbe5..da6a09c 100644 (file)
@@ -141,7 +141,7 @@ bool test()
 
        for (i = 0; i < sizeof(tests)/sizeof(struct Test); i++) {
                ret = __internal_can_send(SYSTEM_BUS, tests[i].user,
-                               tests[i].group, tests[i].label, tests[i].destination,
+                               tests[i].group, tests[i].label, tests[i].destination, nullptr, 0,
                                tests[i].path, tests[i].interface, tests[i].member,
                                (int)tests[i].type);
 
index 27069e5..84c3af4 100644 (file)
@@ -33,7 +33,10 @@ bool signal_test() {
        bool ret = true;
        __internal_init(SYSTEM_BUS, "tests/default_allow/system.conf");
        for (i = 0; i < sizeof(signal_tests)/sizeof(struct SignalTest); i++) {
-               ret = __internal_can_send(SYSTEM_BUS, signal_tests[i].user, signal_tests[i].group, signal_tests[i].label, signal_tests[i].dest, NULL, signal_tests[i].interface, NULL, DBUSPOLICY_MESSAGE_TYPE_SIGNAL);
+               ret = __internal_can_send(SYSTEM_BUS,
+                               signal_tests[i].user, signal_tests[i].group, signal_tests[i].label,
+                               signal_tests[i].dest, nullptr, 0,
+                               NULL, signal_tests[i].interface, NULL, DBUSPOLICY_MESSAGE_TYPE_SIGNAL);
                if ( (int)((signal_tests[i].expected_result)) != ret) {
                        printf("[ERROR][%d] signal test failed: %d %d ", i, (int)((signal_tests[i].expected_result)), ret);
                        signalTest_print(&signal_tests[i], ret);