Apply Cynara gdbus peer credentials 00/41400/1
authorMu-Woong <muwoong.lee@samsung.com>
Mon, 15 Jun 2015 05:56:30 +0000 (14:56 +0900)
committerMu-Woong <muwoong.lee@samsung.com>
Mon, 15 Jun 2015 05:56:30 +0000 (14:56 +0900)
Change-Id: Ib366e1a45d4fcafd98ed76c84d746876ef9a26e7
Signed-off-by: Mu-Woong <muwoong.lee@samsung.com>
CMakeLists.txt
packaging/context-service.spec
src/access_control/peer_creds.cpp [new file with mode: 0644]
src/access_control/peer_creds.h [new file with mode: 0644]
src/client_request.cpp
src/client_request.h
src/dbus_server_impl.cpp

index b45e4592eeb9f00f0121068642b59693b85ac509..712a53ff95b6bdef03795f824750635bd4e92f07 100644 (file)
@@ -21,6 +21,7 @@ SET(DEPS
        capi-security-privilege-manager
        alarm-service
        notification
+       cynara-creds-gdbus
        clips
        context-common
        device-context-provider
index 42b8da08475c387fbc2f1afdd04bda240fc0655d..c7278be0835537a8cb0999ece682f6c3c83d5714 100644 (file)
@@ -21,6 +21,8 @@ BuildRequires: pkgconfig(capi-security-privilege-manager)
 BuildRequires: pkgconfig(alarm-service)
 BuildRequires: pkgconfig(notification)
 
+BuildRequires: pkgconfig(cynara-creds-gdbus)
+
 BuildRequires: pkgconfig(clips)
 BuildRequires: pkgconfig(context-common)
 BuildRequires: pkgconfig(context)
diff --git a/src/access_control/peer_creds.cpp b/src/access_control/peer_creds.cpp
new file mode 100644 (file)
index 0000000..afcb70f
--- /dev/null
@@ -0,0 +1,39 @@
+/*
+ * Copyright (c) 2015 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <cynara-creds-gdbus.h>
+#include <types_internal.h>
+#include "peer_creds.h"
+
+std::string ctx::peer_creds::get_smack_label(GDBusConnection *connection, const char *unique_name)
+{
+       gchar *client = NULL;
+       int err = cynara_creds_gdbus_get_client(connection, unique_name, CLIENT_METHOD_SMACK, &client);
+       IF_FAIL_RETURN_TAG(err == CYNARA_API_SUCCESS, "", _E, "cynara_creds_gdbus_get_client() failed");
+
+       std::string ret = client;
+       g_free(client);
+       return ret;
+}
+
+pid_t ctx::peer_creds::get_pid(GDBusConnection *connection, const char *unique_name)
+{
+       pid_t pid = -1;
+       int err = cynara_creds_gdbus_get_pid(connection, unique_name, &pid);
+       IF_FAIL_RETURN_TAG(err == CYNARA_API_SUCCESS, -1, _E, "cynara_creds_gdbus_get_pid() failed");
+
+       return pid;
+}
diff --git a/src/access_control/peer_creds.h b/src/access_control/peer_creds.h
new file mode 100644 (file)
index 0000000..e2704e5
--- /dev/null
@@ -0,0 +1,31 @@
+/*
+ * Copyright (c) 2015 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __CONTEXT_PEER_CREDENTIALS_H__
+#define __CONTEXT_PEER_CREDENTIALS_H__
+
+#include <sys/types.h>
+#include <gio/gio.h>
+#include <string>
+
+namespace ctx {
+       namespace peer_creds {
+               std::string get_smack_label(GDBusConnection *connection, const char *unique_name);
+               pid_t get_pid(GDBusConnection *connection, const char *unique_name);
+       }
+}      /* namespace ctx */
+
+#endif /* End of __CONTEXT_PEER_CREDENTIALS_H__ */
index d1b8a9502fe2c85ff43c578fb77a5ac82b509a9f..1ce081d20287718fc2d2538a333f12d3f9136adf 100644 (file)
 
 #include <unistd.h>
 #include <glib.h>
-#include <security-server.h>
 #include <app_manager.h>
 #include <types_internal.h>
 #include <dbus_server.h>
 #include "zone_util_impl.h"
 #include "dbus_server_impl.h"
-#include "access_control/privilege.h"
 #include "client_request.h"
 
-ctx::client_request::client_request(int type, const char* client, int req_id, const char* subj, const char* desc, const char* cookie, GDBusMethodInvocation *inv)
+ctx::client_request::client_request(int type, const char* client, int req_id, const char* subj, const char* desc, GDBusMethodInvocation *inv)
        : request_info(type, client, req_id, subj, desc)
        , invocation(inv)
 {
-       gsize size;
-       int client_pid;
-       char *decoded = NULL;
-       const char *zone_name = NULL;
-       char *pkg_id = NULL;
-
-       decoded = reinterpret_cast<char*>(g_base64_decode(cookie, &size));
-       IF_FAIL_CATCH_TAG(decoded, _E, "Cookie decoding failed");
-
-       raw_cookie = decoded;
-       client_pid = security_server_get_cookie_pid(decoded);
-       pkg_id = security_server_get_smacklabel_cookie(decoded);
-       g_free(decoded);
-       IF_FAIL_CATCH_TAG(client_pid > 0, _E, "Invalid PID (%d)", client_pid);
-
-       if (pkg_id == NULL) {
-               _W(RED("security_server_get_smacklabel_cookie() failed"));
-               char* app_id = NULL;
-               app_manager_get_app_id(client_pid, &app_id);
-               client_app_id = ctx::privilege_manager::get_pkg_id(app_id);
-               g_free(app_id);
-       } else {
-               //FIXME: Yes.. this is actually the package id
-               client_app_id = pkg_id;
-               g_free(pkg_id);
-       }
-
-       zone_name = ctx::zone_util::get_name_by_pid(client_pid);
-       IF_FAIL_CATCH_TAG(zone_name, _E, RED("Zone name retrieval failed"));
-       _zone_name = zone_name;
-
-       _SD(CYAN("Package: '%s' / Zone: '%s'"), client_app_id.c_str(), zone_name);
-       return;
-
-CATCH:
-       invocation = NULL;
-       throw ERR_OPERATION_FAILED;
 }
 
 ctx::client_request::~client_request()
@@ -74,9 +35,12 @@ ctx::client_request::~client_request()
                g_dbus_method_invocation_return_value(invocation, g_variant_new("(iss)", ERR_OPERATION_FAILED, EMPTY_JSON_OBJECT, EMPTY_JSON_OBJECT));
 }
 
-const char* ctx::client_request::get_cookie()
+bool ctx::client_request::set_peer_creds(const char *smack_label, const char *zone)
 {
-       return raw_cookie.c_str();
+       IF_FAIL_RETURN_TAG(smack_label && zone, false, _E, "Invalid parameter");
+       client_app_id = smack_label;
+       _zone_name = zone;
+       return true;
 }
 
 const char* ctx::client_request::get_app_id()
index fc4737eb1c0593c6daa11a30fff0b9d485649706..bc7e8f364934f3366b52d4fb299da937f019093a 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2014 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2015 Samsung Electronics Co., Ltd.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -24,10 +24,10 @@ namespace ctx {
 
        class client_request : public request_info {
                public:
-                       client_request(int type, const char* client, int req_id, const char* subj, const char* desc, const char* cookie, GDBusMethodInvocation *inv);
+                       client_request(int type, const char* client, int req_id, const char* subj, const char* desc, GDBusMethodInvocation *inv);
                        ~client_request();
 
-                       const char* get_cookie();
+                       bool set_peer_creds(const char *smack_label, const char *zone);
                        const char* get_app_id();
 
                        bool reply(int error);
@@ -37,9 +37,7 @@ namespace ctx {
 
                private:
                        GDBusMethodInvocation *invocation;
-                       std::string raw_cookie;
                        std::string client_app_id;
-                       std::string exec_path;
        };
 
 }      /* namespace ctx */
index 1834df743555f3f9c557965c00c84d025ae84707..93400e7fa2c6ed8fa253d8e5b65b24b08e4624f0 100644 (file)
@@ -22,6 +22,8 @@
 #include <dbus_listener_iface.h>
 #include "server.h"
 #include "client_request.h"
+#include "access_control/peer_creds.h"
+#include "zone_util_impl.h"
 #include "dbus_server_impl.h"
 
 static ctx::dbus_server_impl *_instance = NULL;
@@ -78,17 +80,20 @@ static void handle_request(const char *sender, GVariant *param, GDBusMethodInvoc
        _I("[%s] ReqId: %d, Subject: %s", req_type_to_str(req_type), req_id, subject);
        _SI("Input: %s", input);
 
-       //TODO: Parameter validation
-
        ctx::client_request *request = NULL;
        try {
-               request = new ctx::client_request(req_type, sender, req_id, subject, input, cookie, invocation);
+               request = new ctx::client_request(req_type, sender, req_id, subject, input, invocation);
        } catch (std::bad_alloc& ba) {
                _E("Memory allocation failed");
                g_dbus_method_invocation_return_value(invocation, g_variant_new("(iss)", ERR_OPERATION_FAILED, EMPTY_JSON_OBJECT, EMPTY_JSON_OBJECT));
                return;
-       } catch (int e) {
-               _E("Caught %d", e);
+       }
+
+       std::string smack_label = ctx::peer_creds::get_smack_label(dbus_connection, sender);
+       pid_t pid = ctx::peer_creds::get_pid(dbus_connection, sender);
+       const char* zone = ctx::zone_util::get_name_by_pid(pid);
+
+       if (smack_label.empty() || !request->set_peer_creds(smack_label.c_str(), zone)) {
                g_dbus_method_invocation_return_value(invocation, g_variant_new("(iss)", ERR_OPERATION_FAILED, EMPTY_JSON_OBJECT, EMPTY_JSON_OBJECT));
                return;
        }