Restructure peer credentialing routine to support Cynara 62/49762/1
authorMu-Woong <muwoong.lee@samsung.com>
Tue, 20 Oct 2015 05:40:13 +0000 (14:40 +0900)
committerMu-Woong <muwoong.lee@samsung.com>
Tue, 20 Oct 2015 05:40:13 +0000 (14:40 +0900)
Change-Id: Ice4e35f9c4152391ca362ba715d6cbd38ae8d44e
Signed-off-by: Mu-Woong <muwoong.lee@samsung.com>
15 files changed:
src/access_control/peer_creds.cpp
src/access_control/peer_creds.h
src/access_control/privilege.cpp
src/access_control/privilege.h
src/client_request.cpp
src/client_request.h
src/context_mgr_impl.cpp
src/context_mgr_impl.h
src/context_trigger/context_monitor.cpp
src/context_trigger/fact_reader.cpp
src/context_trigger/fact_request.cpp
src/context_trigger/fact_request.h
src/dbus_server_impl.cpp
src/request.cpp
src/request.h

index e35dcfb7c527876cd49c9212115ba1f6e11066e3..8a81aaa5b9b965086fb39164312699b8fde046d0 100644 (file)
  */
 
 #include <cynara-creds-gdbus.h>
+#include <app_manager.h>
 #include <types_internal.h>
 #include "peer_creds.h"
 
-bool ctx::peer_creds::get(GDBusConnection *connection, const char *unique_name, std::string &smack_label, pid_t &pid)
+ctx::credentials::credentials(char *_app_id, char *_client) :
+       app_id(_app_id),
+       client(_client)
 {
-       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, false, _E, "cynara_creds_gdbus_get_client() failed");
+}
 
-       smack_label = client;
+ctx::credentials::~credentials()
+{
+       g_free(app_id);
        g_free(client);
+}
+
+bool ctx::peer_creds::get(GDBusConnection *connection, const char *unique_name, ctx::credentials **creds)
+{
+       pid_t pid = 0;
+       char *app_id = NULL;
+       gchar *client = NULL;
+       int err;
 
        err = cynara_creds_gdbus_get_pid(connection, unique_name, &pid);
-       IF_FAIL_RETURN_TAG(err == CYNARA_API_SUCCESS, false, _E, "cynara_creds_gdbus_get_pid() failed");
+       IF_FAIL_RETURN_TAG(err == CYNARA_API_SUCCESS, false, _E, "Peer credentialing failed");
+
+       err = cynara_creds_gdbus_get_client(connection, unique_name, CLIENT_METHOD_DEFAULT, &client);
+       IF_FAIL_CATCH_TAG(err == CYNARA_API_SUCCESS, _E, "Peer credentialing failed");
+
+       /* TODO: session & user */
+
+       app_manager_get_app_id(pid, &app_id);
+       _D("AppId: %s", app_id);
+
+       *creds = new(std::nothrow) credentials(app_id, client);
+       IF_FAIL_CATCH_TAG(*creds, _E, "Memory allocation failed");
 
        return true;
+
+CATCH:
+       g_free(app_id);
+       g_free(client);
+       return false;
 }
index 76b51d4ae4db385d66d9da168cb4fa38f55113a4..31ac9a09b24b4d9b55e039d3a13a575160d636a2 100644 (file)
 #include <string>
 
 namespace ctx {
+
+       class credentials {
+       public:
+               char *app_id;
+               char *client;   /* smack label */
+               credentials(char *_app_id, char *_client);
+               ~credentials();
+       };
+
        namespace peer_creds {
-               bool get(GDBusConnection *connection, const char *unique_name, std::string &smack_label, pid_t &pid);
+               bool get(GDBusConnection *connection, const char *unique_name, credentials **creds);
        }
 }      /* namespace ctx */
 
index 673834420f8409d9ee3990d95a330a83df9f8a15..f49797e16e05731e40563dcccad5fa1545ae64a2 100644 (file)
 
 #include <string>
 #include <types_internal.h>
+#include "peer_creds.h"
 #include "privilege.h"
 
-bool ctx::privilege_manager::is_allowed(const char *client, const char *privilege)
+bool ctx::privilege_manager::is_allowed(const ctx::credentials *creds, const char *privilege)
 {
        /* TODO: need to be implemented using Cynara */
 #if 0
index 8f9c8c731d6391ed187e39988282e4dd6b88e862..bd6730c48a81f67f9de4d6aeb6d9b3000204f8a6 100644 (file)
 #define PRIV_ALARM_SET "alarm.set"
 
 namespace ctx {
+
+       /* Forward declaration */
+       class credentials;
+
        namespace privilege_manager {
-               bool is_allowed(const char *client, const char *privilege);
+
+               bool is_allowed(const credentials *creds, const char *privilege);
+
        }       /* namespace ctx::privilege_manager */
 }      /* namespace ctx */
 
index 8270b067f63e319464bcef00cc8890f5dca76136..62bc30f371ef9a9bd2ee91585cc0160c98a71b1f 100644 (file)
 #include <types_internal.h>
 #include <dbus_server.h>
 #include "dbus_server_impl.h"
+#include "access_control/peer_creds.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 *sender, char *app_id, GDBusMethodInvocation *inv)
-       : request_info(type, client, req_id, subj, desc)
-       , __app_id(app_id)
-       , __sender(sender)
+ctx::client_request::client_request(int type, int req_id, const char *subj, const char *desc,
+               ctx::credentials *creds, const char *sender, GDBusMethodInvocation *inv)
+       : request_info(type, req_id, subj, desc)
+       , __credentials(creds)
+       , __dbus_sender(sender)
        , __invocation(inv)
 {
 }
@@ -37,12 +37,28 @@ ctx::client_request::~client_request()
        if (__invocation)
                g_dbus_method_invocation_return_value(__invocation, g_variant_new("(iss)", ERR_OPERATION_FAILED, EMPTY_JSON_OBJECT, EMPTY_JSON_OBJECT));
 
-       g_free(__app_id);
+       delete __credentials;
+}
+
+const ctx::credentials* ctx::client_request::get_credentials()
+{
+       return __credentials;
 }
 
 const char* ctx::client_request::get_app_id()
 {
-       return __app_id;
+       if (__credentials)
+               return __credentials->app_id;
+
+       return NULL;
+}
+
+const char* ctx::client_request::get_client()
+{
+       if (__credentials)
+               return __credentials->client;
+
+       return NULL;
 }
 
 bool ctx::client_request::reply(int error)
@@ -111,7 +127,7 @@ bool ctx::client_request::publish(int error, ctx::json& data)
        char *data_str = data.dup_cstr();
        IF_FAIL_RETURN_TAG(data_str, false, _E, "Memory allocation failed");
 
-       dbus_server::publish(__sender.c_str(), _req_id, _subject.c_str(), error, data_str);
+       dbus_server::publish(__dbus_sender.c_str(), _req_id, _subject.c_str(), error, data_str);
        g_free(data_str);
 
        return true;
index ed5ef6a7dfe142310ecb9cc8b5ce4ce5272a7267..1f60690b7db5309ff07bd088486c85a5624a9025 100644 (file)
@@ -24,20 +24,21 @@ 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 *sender, char *app_id, GDBusMethodInvocation *inv);
+               client_request(int type, int req_id, const char *subj, const char *desc,
+                               credentials *creds, const char *sender, GDBusMethodInvocation *inv);
                ~client_request();
 
-               const char *get_app_id();
+               const credentials* get_credentials();
+               const char* get_app_id();
+               const char* get_client();
                bool reply(int error);
                bool reply(int error, ctx::json &request_result);
                bool reply(int error, ctx::json &request_result, ctx::json &data_read);
                bool publish(int error, ctx::json &data);
 
        private:
-               char *__app_id;
-               std::string __sender;
+               credentials *__credentials;
+               std::string __dbus_sender;
                GDBusMethodInvocation *__invocation;
        };
 
index a055da8caff4517a99ee655216babb10d3f2e74a..b2257ccb0cceb4941685f1234669f264c05eb259 100644 (file)
@@ -160,11 +160,12 @@ void ctx::context_manager_impl::is_supported(request_info *request)
        delete request;
 }
 
-bool ctx::context_manager_impl::is_allowed(const char *client, const char *subject)
+bool ctx::context_manager_impl::is_allowed(const ctx::credentials *creds, const char *subject)
 {
+       IF_FAIL_RETURN(creds, true);    /* In case internal requests */
        provider_map_t::iterator it = provider_map.find(subject);
        IF_FAIL_RETURN(it != provider_map.end(), false);
-       IF_FAIL_RETURN(ctx::privilege_manager::is_allowed(client, it->second.privilege), false);
+       IF_FAIL_RETURN(ctx::privilege_manager::is_allowed(creds, it->second.privilege), false);
        return true;
 }
 
@@ -210,8 +211,7 @@ ctx::context_provider_iface *ctx::context_manager_impl::get_provider(ctx::reques
                return NULL;
        }
 
-       if (!STR_EQ(TRIGGER_CLIENT_NAME, request->get_client()) &&
-                       !ctx::privilege_manager::is_allowed(request->get_client(), it->second.privilege)) {
+       if (!ctx::privilege_manager::is_allowed(request->get_credentials(), it->second.privilege)) {
                _W("Permission denied");
                request->reply(ERR_PERMISSION_DENIED);
                delete request;
index 64182240bad12b21dd1a31a968e8b60e3a67fdf1..3e35e02da728d848dbffd36b56975bbf9c905590 100644 (file)
 #include <context_mgr_iface.h>
 #include "request.h"
 
-#define TRIGGER_CLIENT_NAME "TRIGGER"
-
 namespace ctx {
 
+       /* Forward declaration */
+       class credentials;
+
        class context_manager_impl : public context_manager_iface {
        public:
                typedef std::list<request_info*> request_list_t;
@@ -40,7 +41,7 @@ namespace ctx {
 
                void assign_request(ctx::request_info *request);
                bool is_supported(const char *subject);
-               bool is_allowed(const char *client, const char *subject);
+               bool is_allowed(const credentials *creds, const char *subject);
                bool pop_trigger_item(std::string &subject, int &operation, ctx::json &attributes, ctx::json &options);
 
                /* From the interface class */
index 81b89b30a6a5b0de972d355b4d355b6dc12741df..a389eb6a844538966e4c70d331548630756c7876 100644 (file)
@@ -110,7 +110,9 @@ bool ctx::context_monitor::is_supported(std::string subject)
 bool ctx::context_monitor::is_allowed(const char *client, const char *subject)
 {
        if (STR_EQ(subject, TIMER_EVENT_SUBJECT))
-               return privilege_manager::is_allowed(client, PRIV_ALARM_SET);
+               return true;
+       //TODO: re-implement above in the proper 3.0 style
+       //      return privilege_manager::is_allowed(client, PRIV_ALARM_SET);
 
        if (STR_EQ(subject, TIMER_CONDITION_SUBJECT))
                return true;
index bdf6fe6ef58aa189f001af9fe9057dd0d263fa3f..042ce2e53d4019ce0f496897aa5ad11c22e1b5de 100644 (file)
@@ -19,7 +19,6 @@
 #include "fact_request.h"
 #include "fact_reader.h"
 
-#define CLIENT_NAME                    TRIGGER_CLIENT_NAME
 #define COND_END_TIME(T)       (g_get_monotonic_time() + (T) * G_TIME_SPAN_SECOND)
 #define SUBSCRIBE_TIMEOUT      3
 #define READ_TIMEOUT           10
@@ -125,7 +124,9 @@ bool ctx::fact_reader::is_supported(const char* subject)
 
 bool ctx::fact_reader::is_allowed(const char *client, const char *subject)
 {
-       return _context_mgr->is_allowed(client, subject);
+       //TODO: re-implement this in the proper 3.0 style
+       //return _context_mgr->is_allowed(client, subject);
+       return true;
 }
 
 bool ctx::fact_reader::get_fact_definition(std::string &subject, int &operation, ctx::json &attributes, ctx::json &options)
@@ -147,7 +148,7 @@ int ctx::fact_reader::subscribe(const char* subject, json* option, bool wait_res
 
        rid = generate_req_id();
 
-       fact_request *req = new(std::nothrow) fact_request(REQ_SUBSCRIBE, CLIENT_NAME,
+       fact_request *req = new(std::nothrow) fact_request(REQ_SUBSCRIBE,
                        rid, subject, option ? option->str().c_str() : NULL, wait_response ? this : NULL);
        IF_FAIL_RETURN_TAG(req, -1, _E, "Memory allocation failed");
 
@@ -188,7 +189,7 @@ void ctx::fact_reader::unsubscribe(const char* subject, json* option)
 
 void ctx::fact_reader::unsubscribe(const char *subject, int subscription_id)
 {
-       fact_request *req = new(std::nothrow) fact_request(REQ_UNSUBSCRIBE, CLIENT_NAME, subscription_id, subject, NULL, NULL);
+       fact_request *req = new(std::nothrow) fact_request(REQ_UNSUBSCRIBE, subscription_id, subject, NULL, NULL);
        IF_FAIL_VOID_TAG(req, _E, "Memory allocation failed");
 
        g_idle_add(send_request, req);
@@ -203,7 +204,7 @@ bool ctx::fact_reader::read(const char* subject, json* option, context_fact& fac
 
        int rid = generate_req_id();
 
-       fact_request *req = new(std::nothrow) fact_request(REQ_READ_SYNC, CLIENT_NAME,
+       fact_request *req = new(std::nothrow) fact_request(REQ_READ_SYNC,
                        rid, subject, option ? option->str().c_str() : NULL, this);
        IF_FAIL_RETURN_TAG(req, false, _E, "Memory allocation failed");
 
index b4017bd847980e587db1710ca96820db00bea0ae..7e61869b0d18eecb7bd29340bf099cc2d944b0e5 100644 (file)
@@ -17,8 +17,8 @@
 #include <types_internal.h>
 #include "fact_request.h"
 
-ctx::fact_request::fact_request(int type, const char* client, int req_id, const char* subj, const char* desc, fact_reader* reader)
-       : request_info(type, client, req_id, subj, desc)
+ctx::fact_request::fact_request(int type, int req_id, const char* subj, const char* desc, fact_reader* reader)
+       : request_info(type, req_id, subj, desc)
        , _reader(reader)
        , replied(false)
 {
@@ -29,6 +29,11 @@ ctx::fact_request::~fact_request()
        reply(ERR_OPERATION_FAILED);
 }
 
+const char* ctx::fact_request::get_client()
+{
+       return "TRIGGER";
+}
+
 bool ctx::fact_request::reply(int error)
 {
        IF_FAIL_RETURN(!replied && _reader, true);
index 6ad52c536757c342d6253a5187872f2ddd32ff71..834839ee4ab014f63dd927bff6c94993fedfefe5 100644 (file)
 namespace ctx {
 
        class fact_request : public request_info {
-               public:
-                       fact_request(int type, const char* client, int req_id, const char* subj, const char* desc, fact_reader* reader);
-                       ~fact_request();
-
-                       bool reply(int error);
-                       bool reply(int error, ctx::json& request_result);
-                       bool reply(int error, ctx::json& request_result, ctx::json& data_read);
-                       bool publish(int error, ctx::json& data);
-
-               private:
-                       fact_reader *_reader;
-                       bool replied;
+       public:
+               fact_request(int type, int req_id, const char* subj, const char* desc, fact_reader* reader);
+               ~fact_request();
+
+               const char* get_client();
+               bool reply(int error);
+               bool reply(int error, ctx::json& request_result);
+               bool reply(int error, ctx::json& request_result, ctx::json& data_read);
+               bool publish(int error, ctx::json& data);
+
+       private:
+               fact_reader *_reader;
+               bool replied;
        };
 
 }      /* namespace ctx */
index 447ab2a483ce4792f5188f7fd3b03cc2d862f9bc..8c69f7561836459effcce257c414f562e1dc5582 100644 (file)
@@ -81,23 +81,19 @@ 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);
 
-       std::string smack_label;
-       pid_t pid;
+       ctx::credentials *creds = NULL;
 
-       if (!ctx::peer_creds::get(dbus_connection, sender, smack_label, pid)) {
-               _E("Peer credential failed");
+       if (!ctx::peer_creds::get(dbus_connection, sender, &creds)) {
+               _E("Peer credentialing failed");
                g_dbus_method_invocation_return_value(invocation, g_variant_new("(iss)", ERR_OPERATION_FAILED, EMPTY_JSON_OBJECT, EMPTY_JSON_OBJECT));
                return;
        }
 
-       char *app_id = NULL;
-       app_manager_get_app_id(pid, &app_id);
-       _D("AppId: %s", app_id);
-
-       ctx::client_request *request = new(std::nothrow) ctx::client_request(req_type, smack_label.c_str(), req_id, subject, input, sender, app_id, invocation);
+       ctx::client_request *request = new(std::nothrow) ctx::client_request(req_type, req_id, subject, input, creds, sender, invocation);
        if (!request) {
                _E("Memory allocation failed");
                g_dbus_method_invocation_return_value(invocation, g_variant_new("(iss)", ERR_OPERATION_FAILED, EMPTY_JSON_OBJECT, EMPTY_JSON_OBJECT));
+               delete creds;
                return;
        }
 
index df24f52bdb35b37f8a44cc70ceee02cd122ba0fb..4134bbe4dc455cf0d32c67d31fafa0a567d91e3e 100644 (file)
 #include <types_internal.h>
 #include "request.h"
 
-ctx::request_info::request_info(int type, const char* client, int req_id, const char* subj, const char* desc)
+ctx::request_info::request_info(int type, int req_id, const char* subj, const char* desc)
        : _type(type)
        , _req_id(req_id)
-       , _client(client)
        , _subject(subj)
        , _description(desc)
 {
@@ -41,9 +40,19 @@ int ctx::request_info::get_id()
        return _req_id;
 }
 
+const ctx::credentials* ctx::request_info::get_credentials()
+{
+       return NULL;
+}
+
+const char* ctx::request_info::get_app_id()
+{
+       return NULL;
+}
+
 const char* ctx::request_info::get_client()
 {
-       return _client.c_str();
+       return NULL;
 }
 
 const char* ctx::request_info::get_subject()
@@ -55,8 +64,3 @@ ctx::json& ctx::request_info::get_description()
 {
        return _description;
 }
-
-const char* ctx::request_info::get_app_id()
-{
-       return NULL;
-}
index 755a979c6c026a02a0ba547be479fc3db81f6e97..cc592236200bfd34ee83bdd6316be372b5719245 100644 (file)
 
 namespace ctx {
 
+       /* Forward declaration */
+       class credentials;
+
        class request_info {
        public:
-               request_info(int type, const char *client, int req_id, const char *subj, const char *desc);
+               request_info(int type, int req_id, const char *subj, const char *desc);
                virtual ~request_info();
 
                int get_type();
                int get_id();
-               const char *get_client();
-               const char *get_subject();
+               const char* get_subject();
                ctx::json& get_description();
 
-               virtual const char *get_app_id();
+               virtual const credentials* get_credentials();
+               virtual const char* get_app_id();
+               /* TODO: remove this get_client() */
+               virtual const char* get_client();
                virtual bool reply(int error) = 0;
                virtual bool reply(int error, ctx::json &request_result) = 0;
                virtual bool reply(int error, ctx::json &request_result, ctx::json &data_read) = 0;
@@ -42,7 +47,6 @@ namespace ctx {
        protected:
                int _type;
                int _req_id;
-               std::string _client;
                std::string _subject;
                ctx::json _description;
        };