Use Cynara helpers to obtain connection credentials 70/42070/1
authorJacek Bukarewicz <j.bukarewicz@samsung.com>
Mon, 22 Jun 2015 11:02:24 +0000 (13:02 +0200)
committerJacek Bukarewicz <j.bukarewicz@samsung.com>
Mon, 22 Jun 2015 14:36:34 +0000 (16:36 +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: Ibacbd713a404418496649e4ea34e78dfa7c67cdb

CMakeLists.txt
packaging/tel-plugin-dbus_tapi.spec
src/common.c

index fba69e22a1a2f4c803e53ab98d157851a9c04863..cfcde7d45dc32d9405c37e2e4a013685d27006e7 100755 (executable)
@@ -22,6 +22,7 @@ pkg_check_modules(pkgs REQUIRED
        appsvc
        pkgmgr
        cynara-client
+       cynara-creds-gdbus
        cynara-session
 )
 
index 80d10029dd5f038a7444698d50f47166a5b75f6c..f15a2e1a366e6b26ed442e7d4aecd4b27c94b1f4 100755 (executable)
@@ -21,6 +21,7 @@ BuildRequires:  pkgconfig(libxml-2.0)
 BuildRequires:  pkgconfig(pkgmgr)
 BuildRequires:  pkgconfig(tcore)
 BuildRequires:  pkgconfig(cynara-client)
+BuildRequires:  pkgconfig(cynara-creds-gdbus)
 BuildRequires:  pkgconfig(cynara-session)
 
 
index a62678f137048c29c5a77c28bcaa453ce4c5ffc6..6d35c7a2562099dbe0a2d23314dbdd94adaf5440 100755 (executable)
@@ -34,6 +34,7 @@
 #include <libxml/parser.h>
 #include <libxml/tree.h>
 
+#include <cynara-creds-gdbus.h>
 #include <cynara-session.h>
 
 #include "generated-code.h"
@@ -89,20 +90,13 @@ UserRequest *dbus_plugin_macro_user_request_new(struct custom_data *ctx, void *o
 gboolean 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;
 
@@ -117,86 +111,30 @@ gboolean check_access_control (cynara *p_cynara, GDBusMethodInvocation *invoc, c
                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) {