Use Cynara helpers to obtain connection credentials 71/42071/1
authorJacek Bukarewicz <j.bukarewicz@samsung.com>
Mon, 22 Jun 2015 14:08:37 +0000 (16:08 +0200)
committerJacek Bukarewicz <j.bukarewicz@samsung.com>
Mon, 22 Jun 2015 14:39:10 +0000 (16:39 +0200)
This makes the code simpler and makes it independent of non-upstream
GetConnectionSmackContext method which is going to be removed in the future.
Additionally, it is planned to implement credentials caching in the helper
library which will improve performance.
This commit also uses correct unique id to obtain credentials (sender's id
instead of service's id).

Change-Id: I32511a290ecb6bb38bdab9bfb072c144876f37e0

CMakeLists.txt
packaging/tel-plugin-packetservice.spec
src/util.c

index 7e0c0f763bee939592021a711d25eab766a75d3a..02e49e44510cc74bec7e94e79967766a0ccbe0ea 100755 (executable)
@@ -14,7 +14,7 @@ endif()
 
 # Set required packages
 INCLUDE(FindPkgConfig)
-pkg_check_modules(pkgs REQUIRED glib-2.0 gio-2.0 gio-unix-2.0 tcore iniparser libxml-2.0 vconf alarm-service cynara-client cynara-session)
+pkg_check_modules(pkgs REQUIRED glib-2.0 gio-2.0 gio-unix-2.0 tcore iniparser libxml-2.0 vconf alarm-service cynara-client cynara-creds-gdbus cynara-session)
 
 FOREACH(flag ${pkgs_CFLAGS})
        SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}")
index f026eeaf418f59f025acaf5ba575393bfe849dfa..692961bf95d10437e7428e5c0b12307091cded8a 100755 (executable)
@@ -23,6 +23,7 @@ BuildRequires:  pkgconfig(db-util)
 BuildRequires:  pkgconfig(vconf)
 BuildRequires:         pkgconfig(alarm-service)
 BuildRequires:  pkgconfig(cynara-client)
+BuildRequires:  pkgconfig(cynara-creds-gdbus)
 BuildRequires:  pkgconfig(cynara-session)
 Requires(post): /sbin/ldconfig
 Requires(postun): /sbin/ldconfig
index 4d45352770997573cd365d3ea2406be2fde58171..757bfb2b1d5838b8ca4dbef84ca8a5398c042d84 100644 (file)
@@ -28,6 +28,7 @@
 #include <libxml/tree.h>
 
 #include <cynara-session.h>
+#include <cynara-creds-gdbus.h>
 
 #include "ps.h"
 
 gboolean ps_util_check_access_control (cynara *p_cynara, GDBusMethodInvocation *invoc, const char *label, const char *perm)
 {
        GDBusConnection *conn;
-       GVariant *result_pid;
-       GVariant *param;
-       GError *error = NULL;
-       const char *sender;
-       unsigned int pid;
+       const char *sender_unique_name;
+       pid_t pid;
        int ret;
        int result = FALSE;
        /* For cynara */
-       GVariant *result_uid;
-       GVariant *result_smack;
-       const gchar *unique_name = NULL;
        gchar *client_smack = NULL;
        char *client_session = NULL;
-       unsigned int uid;
        gchar *uid_string = NULL;
        const char *privilege = NULL;
 
@@ -67,86 +61,30 @@ gboolean ps_util_check_access_control (cynara *p_cynara, GDBusMethodInvocation *
                goto OUT;
        }
 
-       unique_name = g_dbus_connection_get_unique_name(conn);
-       if (!unique_name) {
-               warn ("access control denied (fail to get unique name)");
-               goto OUT;
-       }
-
-       sender = g_dbus_method_invocation_get_sender (invoc);
-
-       param = g_variant_new ("(s)", sender);
-       if (!param) {
-               warn ("access control denied (sender info fail)");
-               goto OUT;
-       }
+       sender_unique_name = g_dbus_method_invocation_get_sender (invoc);
 
        /* Get PID */
-       result_pid = g_dbus_connection_call_sync (conn, "org.freedesktop.DBus",
-                       "/org/freedesktop/DBus",
-                       "org.freedesktop.DBus",
-                       "GetConnectionUnixProcessID",
-                       param, NULL,
-                       G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
-       if (error) {
-               warn ("access control denied (dbus error: %d(%s))",
-                               error->code, error->message);
-               g_error_free (error);
-               goto OUT;
-       }
-
-       if (!result_pid) {
-               warn ("access control denied (fail to get pid)");
+       ret = cynara_creds_gdbus_get_pid(conn, sender_unique_name, &pid);
+       if (ret != CYNARA_API_SUCCESS) {
+               warn ("access control denied (fail to get pid). ret = %d", ret);
                goto OUT;
        }
 
-       g_variant_get (result_pid, "(u)", &pid);
-       g_variant_unref (result_pid);
-
        /* Get UID */
-       result_uid = g_dbus_connection_call_sync (conn, "org.freedesktop.DBus",
-                       "/org/freedesktop/DBus",
-                       "org.freedesktop.DBus",
-                       "GetConnectionUnixUser",
-                       g_variant_new("(s)", unique_name), G_VARIANT_TYPE("(u)"),
-                       G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
-       if (error) {
-               warn ("access control denied (dbus error: %d(%s))",
-                               error->code, error->message);
-               g_error_free (error);
-               goto OUT;
-       }
-
-       if (!result_uid) {
-               warn ("access control denied (fail to get uid for cynara)");
+       ret = cynara_creds_gdbus_get_user(conn, sender_unique_name, USER_METHOD_DEFAULT, &uid_string);
+       if (ret != CYNARA_API_SUCCESS) {
+               warn ("access control denied (fail to get uid for cynara). ret = %d", ret);
                goto OUT;
        }
 
-       g_variant_get (result_uid, "(u)", &uid);
-       g_variant_unref (result_uid);
-       uid_string = g_strdup_printf("%u", uid);
-
        /* Get Smack label */
-       result_smack = g_dbus_connection_call_sync (conn, "org.freedesktop.DBus",
-                       "/org/freedesktop/DBus",
-                       "org.freedesktop.DBus",
-                       "GetConnectionSmackContext",
-                       g_variant_new("(s)", unique_name), G_VARIANT_TYPE("(s)"),
-                       G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
-       if (error) {
-               warn ("access control denied (dbus error: %d(%s))",
-                               error->code, error->message);
-               g_error_free (error);
-               goto OUT;
-       }
-       if (!result_smack) {
-               warn ("access control denied (fail to get smack for cynara)");
+       ret = cynara_creds_gdbus_get_client(conn, sender_unique_name, CLIENT_METHOD_DEFAULT, &client_smack);
+       if (ret != CYNARA_API_SUCCESS) {
+               warn ("access control denied (fail to get smack for cynara). ret = %d", ret);
                goto OUT;
        }
-       g_variant_get (result_smack, "(s)", &client_smack);
-       g_variant_unref (result_smack);
 
-       dbg ("sender: %s pid = %u uid = %u smack = %s", sender, pid, uid, client_smack);
+       dbg ("sender: %s pid = %u uid = %s smack = %s", sender_unique_name, pid, uid_string, client_smack);
 
        client_session = cynara_session_from_pid(pid);
        if (!client_session) {