Exclude Credential from the exposed APIs 12/118312/1
authorMu-Woong Lee <muwoong.lee@samsung.com>
Fri, 10 Mar 2017 04:00:58 +0000 (13:00 +0900)
committerMu-Woong Lee <muwoong.lee@samsung.com>
Fri, 10 Mar 2017 04:00:58 +0000 (13:00 +0900)
Change-Id: I161e41789a057e7c388412152a61c28217d4539a
Signed-off-by: Mu-Woong Lee <muwoong.lee@samsung.com>
include/ClientBase.h
include/ContextTypes.h
include/MethodCall.h
include/PlatformDatabase.h
src/server/ClientBase.cpp
src/server/Credential.cpp
src/server/Credential.h [moved from include/Credential.h with 98% similarity]
src/server/MethodCall.cpp
src/server/PlatformDatabase.cpp
src/server/ServiceBase.cpp

index aa358c9..d6997d3 100644 (file)
@@ -37,17 +37,29 @@ namespace ctx {
 
                const std::string& getBusName() const;
 
-               Credential* getCredential();
-
-               ServiceBase* getHostService();
-
                // If the GVariant 'param' is floating, it is consumed.
                void publish(const std::string& signalName, GVariant* param);
 
+               ServiceBase& getHostService();
+
+               bool isVerified();
+
+               bool hasPrivilege(const char* privil) const;
+
+               uid_t getUid() const;
+
+               std::string getClientId() const;
+
+               bool isSystem() const;
+
+               static bool isSystemUid(uid_t uid);
+
        protected:
                ClientBase(ServiceBase* hostService, const std::string& busName);
 
        private:
+               bool __getCredential();
+
                ServiceBase* __hostService;
                std::string __busName;
                Credential* __credential;
index 95f2bbb..17b07d6 100644 (file)
@@ -91,4 +91,6 @@
 #define HANDLE_GERROR(Err) \
        do { if ((Err)) { _E("GError: %s", Err->message); g_error_free(Err); Err = NULL; } } while (0)
 
+#define EMPTY_STR      ""
+
 #endif /* __CONTEXT_TYPES_H__ */
index da7a7d1..251ceba 100644 (file)
@@ -30,7 +30,6 @@ namespace ctx {
                MethodCall(ClientBase* client, const std::string& methodName, GVariant* param, GDBusMethodInvocation* invocation);
                ~MethodCall();
 
-               ClientBase* getSender();
                const std::string& getMethodName() const;
 
                // The returned GVariant object should be not freed.
@@ -39,6 +38,18 @@ namespace ctx {
                bool reply(GVariant* param);
                bool reply(int error);
 
+               void publish(const std::string& signalName, GVariant* param);
+
+               bool hasPrivilege(const char* privil);
+
+               uid_t getUid();
+
+               std::string getClientId();
+
+               bool isSystem();
+
+               ClientBase& getSender();
+
        private:
                ClientBase* __client;
                std::string __methodName;
index 3300b75..17d4fdd 100644 (file)
 
 namespace ctx {
 
-       class Credential;
+       class ClientBase;
 
        class EXPORT_API PlatformDatabase : public Database {
        public:
                PlatformDatabase(const std::string& dbName, uid_t uid);
-               PlatformDatabase(const std::string& dbName, const Credential& credential);
-               PlatformDatabase(const std::string& dbName, const Credential* credential);
+               PlatformDatabase(const std::string& dbName, const ClientBase& client);
+               PlatformDatabase(const std::string& dbName, const ClientBase* client);
                PlatformDatabase(const std::string& dbName);
        };
 
index a8efcdc..3b2d8a3 100644 (file)
@@ -15,9 +15,9 @@
  */
 
 #include <ServiceBase.h>
-#include <Credential.h>
 #include <MethodCall.h>
 #include <ClientBase.h>
+#include "Credential.h"
 
 using namespace ctx;
 
@@ -38,23 +38,54 @@ const std::string& ClientBase::getBusName() const
        return __busName;
 }
 
-Credential* ClientBase::getCredential()
+bool ClientBase::__getCredential()
 {
        if (__credential)
-               return __credential;
+               return true;
 
        __credential = new(std::nothrow) Credential(__hostService->getConnection(), __busName);
-       IF_FAIL_RETURN_TAG(__credential, NULL, _E, "Memory allocation failed");
+       IF_FAIL_RETURN_TAG(__credential, false, _E, "Memory allocation failed");
 
-       return __credential;
+       return true;
 }
 
-ServiceBase* ClientBase::getHostService()
+ServiceBase& ClientBase::getHostService()
 {
-       return __hostService;
+       return *__hostService;
 }
 
 void ClientBase::publish(const std::string& signalName, GVariant* param)
 {
-       __hostService->publish(__busName, signalName, param);
+       getHostService().publish(__busName, signalName, param);
+}
+
+bool ClientBase::isVerified()
+{
+       IF_FAIL_RETURN(__getCredential(), false);
+       return __credential->valid();
+}
+
+bool ClientBase::hasPrivilege(const char* privil) const
+{
+       return __credential->hasPrivilege(privil);
+}
+
+uid_t ClientBase::getUid() const
+{
+       return __credential->getUid();
+}
+
+std::string ClientBase::getClientId() const
+{
+       return __credential->getClientId();
+}
+
+bool ClientBase::isSystem() const
+{
+       return __credential->isSystem();
+}
+
+bool ClientBase::isSystemUid(uid_t uid)
+{
+       return Credential::isSystemUid(uid);
 }
index b3ab4c6..6c5534a 100644 (file)
@@ -19,7 +19,7 @@
 #include <cynara-session.h>
 #include <cynara-client.h>
 #include <ScopeMutex.h>
-#include <Credential.h>
+#include "Credential.h"
 
 #define CACHE_SIZE     100
 
@@ -86,7 +86,7 @@ namespace {
 
 Credential::Credential(GDBusConnection* conn, const std::string& busName) :
        __pid(0),
-       __uid(0),
+       __uid(ROOT_UID),
        __clientId(NULL),
        __session(NULL),
        __user(NULL),
@@ -125,7 +125,7 @@ bool Credential::valid() const
 bool Credential::hasPrivilege(const char* privil) const
 {
        IF_FAIL_RETURN(privil, true);
-       IF_FAIL_RETURN_TAG(__clientId && __session && __user, false, _W, "Unidentified peer");
+       IF_FAIL_RETURN_TAG(valid(), false, _W, "Unidentified peer");
 
        static PrivilegeChecker privilegeChecker;
 
similarity index 98%
rename from include/Credential.h
rename to src/server/Credential.h
index ffc4991..d604f08 100644 (file)
@@ -20,6 +20,8 @@
 #include <string>
 #include <ContextTypes.h>
 
+#define ROOT_UID       0
+
 namespace ctx {
 
        class EXPORT_API Credential {
index 72919a8..7193a72 100644 (file)
@@ -41,9 +41,9 @@ MethodCall::~MethodCall()
        g_dbus_method_invocation_return_error_literal(__invocation, CTX_ERROR_DOMAIN, E_FAILED, "");
 }
 
-ClientBase* MethodCall::getSender()
+ClientBase& MethodCall::getSender()
 {
-       return __client;
+       return *__client;
 }
 
 const std::string& MethodCall::getMethodName() const
@@ -78,3 +78,28 @@ bool MethodCall::reply(int error)
 
        return true;
 }
+
+void MethodCall::publish(const std::string& signalName, GVariant* param)
+{
+       getSender().publish(signalName, param);
+}
+
+bool MethodCall::hasPrivilege(const char* privil)
+{
+       return getSender().hasPrivilege(privil);
+}
+
+uid_t MethodCall::getUid()
+{
+       return getSender().getUid();
+}
+
+std::string MethodCall::getClientId()
+{
+       return getSender().getClientId();
+}
+
+bool MethodCall::isSystem()
+{
+       return getSender().isSystem();
+}
index 4ca1dc7..9e02b9d 100644 (file)
@@ -17,8 +17,8 @@
 #include <sqlite3.h>
 #include <tzplatform_config.h>
 #include <ScopeMutex.h>
-#include <Credential.h>
 #include <Tuple.h>
+#include <ClientBase.h>
 #include <PlatformDatabase.h>
 
 #define ROOT_UID       ((uid_t)0)
@@ -65,7 +65,7 @@ static std::string __getUserPath(const std::string& dbName, uid_t uid)
 
 static std::string __getPath(const std::string& dbName, uid_t uid)
 {
-       if (Credential::isSystemUid(uid)) {
+       if (ClientBase::isSystemUid(uid)) {
                return __getSystemPath(dbName);
        } else {
                return __getUserPath(dbName, uid);
@@ -77,17 +77,17 @@ PlatformDatabase::PlatformDatabase(const std::string& dbName, uid_t uid) :
 {
 }
 
-PlatformDatabase::PlatformDatabase(const std::string& dbName, const Credential& credential) :
-       PlatformDatabase(dbName, credential.getUid())
+PlatformDatabase::PlatformDatabase(const std::string& dbName) :
+       PlatformDatabase(dbName, ROOT_UID)
 {
 }
 
-PlatformDatabase::PlatformDatabase(const std::string& dbName, const Credential* credential) :
-       PlatformDatabase(dbName, credential->getUid())
+PlatformDatabase::PlatformDatabase(const std::string& dbName, const ClientBase& client) :
+       PlatformDatabase(dbName, client.getUid())
 {
 }
 
-PlatformDatabase::PlatformDatabase(const std::string& dbName) :
-       PlatformDatabase(dbName, ROOT_UID)
+PlatformDatabase::PlatformDatabase(const std::string& dbName, const ClientBase* client) :
+       PlatformDatabase(dbName, client->getUid())
 {
 }
index 6d5eccd..a20539e 100644 (file)
@@ -276,6 +276,11 @@ ClientBase* ServiceBase::__getClient(const std::string& busName)
        ClientBase* client = createClient(busName);
        IF_FAIL_RETURN_TAG(client, NULL, _E, "Memory allocation failed");
 
+       if (!client->isVerified()) {
+               delete client;
+               return NULL;
+       }
+
        ClientInfo info = {client, __watch(busName, client)};
        __clients[busName] = info;
 
@@ -287,7 +292,7 @@ void ServiceBase::__onNameOwnerChanged(GDBusConnection* conn, const gchar* sende
                GVariant* param, gpointer userData)
 {
        ClientBase* client = static_cast<ClientBase*>(userData);
-       client->getHostService()->__removeClient(client->getBusName());
+       client->getHostService().__removeClient(client->getBusName());
 }
 
 void ServiceBase::__removeClient(const std::string& busName)