Enable dbus activation 91/207591/13
authorKrzysztof Jackiewicz <k.jackiewicz@samsung.com>
Thu, 6 Jun 2019 13:03:14 +0000 (15:03 +0200)
committerKrzysztof Jackiewicz <k.jackiewicz@samsung.com>
Fri, 2 Aug 2019 11:46:12 +0000 (13:46 +0200)
- Register dbus service for external card notification
- Activate ode on dbus service method call
- Store external card mount point in vconf/buxton
- Lazily initialize ecryptfs engine
- Stop ode service after 5s from dbus wakeup
- Use the old sd card mountpoint if not given via dbus call
- Apply security policy

Change-Id: Ibea2663689f99a3812692895dd5efb12e068e121

CMakeLists.txt
packaging/ode.spec
server/CMakeLists.txt
server/dbus/org.tizen.ode.conf [new file with mode: 0644]
server/dbus/org.tizen.ode.service.in [new file with mode: 0644]
server/external-encryption.cpp
server/external-encryption.h
server/server.cpp
server/server.h
server/systemd/ode.service.in

index cb00110..93cad22 100755 (executable)
@@ -104,6 +104,18 @@ IF(NOT DEFINED SOCKET_PATH)
        SET(SOCKET_PATH "${RUN_DIR}/.ode.sock")
 ENDIF(NOT DEFINED SOCKET_PATH)
 
+IF(NOT DEFINED DBUS_SYSTEM_SERVICES_DIR)
+       SET(DBUS_SYSTEM_SERVICES_DIR "${DATA_DIR}/dbus-1/system-services/")
+ENDIF(NOT DEFINED DBUS_SYSTEM_SERVICES_DIR)
+
+IF(NOT DEFINED DBUS_CONF_DIR)
+       SET(DBUS_CONF_DIR "${SYSCONF_INSTALL_DIR}/dbus-1/system.d/")
+ENDIF(NOT DEFINED DBUS_CONF_DIR)
+
+IF(NOT DEFINED DBUS_NAME)
+       SET(DBUS_NAME "org.tizen.${PROJECT_NAME}")
+ENDIF(NOT DEFINED DBUS_NAME)
+
 SET(KEY_STORAGE_PLUGIN "key-storage-plugin")
 
 ADD_DEFINITIONS(-DKEY_STORAGE_PLUGIN_DIR="${KEY_STORAGE_PLUGIN_DIR}")
@@ -111,6 +123,7 @@ ADD_DEFINITIONS(-DKEY_STORAGE_PLUGIN_LIB="${CMAKE_SHARED_LIBRARY_PREFIX}${KEY_ST
 
 ADD_DEFINITIONS(-DUG_WAYLAND)
 ADD_DEFINITIONS(-DSOCKET_PATH="${SOCKET_PATH}")
+ADD_DEFINITIONS(-DDBUS_NAME="${DBUS_NAME}")
 
 ADD_SUBDIRECTORY(${ODE_LIB})
 ADD_SUBDIRECTORY(${ODE_KEY_STORAGE_PLUGIN})
index 39b5288..cb990a4 100755 (executable)
@@ -22,12 +22,16 @@ BuildRequires: pkgconfig(capi-system-device)
 BuildRequires: pkgconfig(libsystemd)
 BuildRequires: pkgconfig(ext2fs)
 BuildRequires: pkgconfig(com_err)
+BuildRequires: pkgconfig(gio-2.0)
 Requires: cryptsetup
 
 %global key_storage_plugin_dir %{_libdir}/ode-key-storage-plugin/
 %global softreset_dir /usr/system/RestoreDir/softreset/
 %define upgrade_script_dir /usr/share/upgrade/scripts/
 %global socket_path %{TZ_SYS_RUN}/.ode.sock
+%global dbus_system_services_dir %{_datadir}/dbus-1/system-services/
+%global dbus_conf_dir %{_sysconfdir}/dbus-1/system.d/
+%global dbus_name org.tizen.%{name}
 
 %description
 The ode package provides a daemon which is responsible for encrypting/decryption storages and secure erasing.
@@ -38,6 +42,8 @@ The ode package provides a daemon which is responsible for encrypting/decryption
 %attr(755,root,root) %{_bindir}/oded
 %{_unitdir}/ode.service
 %{_unitdir}/ode.socket
+%{dbus_system_services_dir}/%{dbus_name}.service
+%{dbus_conf_dir}/%{dbus_name}.conf
 %attr(700,root,root) %{TZ_SYS_SBIN}/ode-admin-cli
 %attr(700,root,root) %{softreset_dir}/ode_softreset.sh
 %attr(750,root,system_share) %{TZ_SYS_SBIN}/ode-fota
@@ -67,7 +73,10 @@ The ode package provides a daemon which is responsible for encrypting/decryption
          -DSOFTRESET_DIR="%{softreset_dir}" \
          -DKEY_STORAGE_PLUGIN_DIR="%{key_storage_plugin_dir}" \
          -DUPGRADE_SCRIPT_DIR="%{upgrade_script_dir}" \
-         -DSOCKET_PATH="%{socket_path}"
+         -DSOCKET_PATH="%{socket_path}" \
+         -DBUS_SYSTEM_SERVICES_DIR="%{dbus_system_services_dir}" \
+         -DBUS_CONF_DIR="%{dbus_conf_dir}" \
+         -DBUS_NAME="%{dbus_name}"
 
 make %{?jobs:-j%jobs}
 
index dd8e68e..085003a 100644 (file)
@@ -76,8 +76,11 @@ TARGET_COMPILE_DEFINITIONS(${SERVER_NAME} PRIVATE
 
 CONFIGURE_FILE(systemd/${PROJECT_NAME}.service.in systemd/${PROJECT_NAME}.service)
 CONFIGURE_FILE(systemd/${PROJECT_NAME}.socket.in systemd/${PROJECT_NAME}.socket)
+CONFIGURE_FILE(dbus/${DBUS_NAME}.service.in dbus/${DBUS_NAME}.service)
 
 INSTALL(TARGETS ${SERVER_NAME} DESTINATION ${BIN_DIR})
 INSTALL(FILES systemd/${PROJECT_NAME}.service systemd/${PROJECT_NAME}.socket DESTINATION ${SYSTEMD_UNIT_DIR})
+INSTALL(FILES dbus/${DBUS_NAME}.service DESTINATION ${DBUS_SYSTEM_SERVICES_DIR})
+INSTALL(FILES dbus/${DBUS_NAME}.conf DESTINATION ${DBUS_CONF_DIR})
 INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/engine/encryption/dummy_password DESTINATION ${PROJECT_DATA_DIR})
 INSTALL(DIRECTORY DESTINATION ${KEY_STORAGE_PLUGIN_DIR})
diff --git a/server/dbus/org.tizen.ode.conf b/server/dbus/org.tizen.ode.conf
new file mode 100644 (file)
index 0000000..111af35
--- /dev/null
@@ -0,0 +1,13 @@
+<!DOCTYPE busconfig PUBLIC "-//freedesktop//DTD D-BUS Bus Configuration 1.0//EN"
+"http://www.freedesktop.org/standards/dbus/1.0/busconfig.dtd">
+<busconfig>
+       <policy user="root">
+               <allow own="org.tizen.ode"/>
+               <allow send_destination="org.tizen.ode"/>
+       </policy>
+
+       <policy context="default">
+               <deny own="org.tizen.ode"/>
+               <deny send_destination="org.tizen.ode"/>
+       </policy>
+</busconfig>
diff --git a/server/dbus/org.tizen.ode.service.in b/server/dbus/org.tizen.ode.service.in
new file mode 100644 (file)
index 0000000..1fc65f1
--- /dev/null
@@ -0,0 +1,5 @@
+[D-BUS Service]
+Name=@DBUS_NAME@
+Exec=/bin/false
+User=root
+SystemdService=@PROJECT_NAME@.service
\ No newline at end of file
index ccae537..ab2dcd6 100644 (file)
@@ -15,6 +15,7 @@
  */
 #include <fstream>
 #include <sstream>
+#include <cstring>
 
 #include <unistd.h>
 #include <sys/mount.h>
@@ -41,8 +42,8 @@ namespace {
 
 bool isBootCompleted = false;
 
-const char *EXTERNAL_PATH              = "/media/SDCardA1";
-const char *PRIVILEGE_PLATFORM = "http://tizen.org/privilege/internal/default/platform";
+constexpr const char *DEFAULT_EXTERNAL_PATH = "/media/SDCardA1";
+const char *PRIVILEGE_PLATFORM = "http://tizen.org/privilege/internal/default/platform";
 
 void spawnUI()
 {
@@ -60,6 +61,18 @@ void spawnUI()
        }
 }
 
+
+bool isEncrypted()
+{
+       char *cryptoState = ::vconf_get_str(VCONFKEY_SDE_CRYPTO_STATE);
+       if (cryptoState == NULL)
+               return false;
+
+       bool encrypted = (strcmp(cryptoState, "encrypted") == 0);
+       free(cryptoState);
+       return encrypted;
+}
+
 void externalCallback(dbus::Variant parameters)
 {
        int intparams[6];
@@ -88,16 +101,20 @@ void externalCallback(dbus::Variant parameters)
 
        if(intparams[2] == 0) {
                INFO(SINK, "SD card not mounted, ignoring.");
+               int ret = ::vconf_unset(VCONFKEY_SDE_MOUNT_POINT);
+               if (ret != 0)
+                       ERROR(SINK, "vconf_unset() failed with " << ret);
        } else {
                INFO(SINK, "SD card mounted.");
-               char *value = ::vconf_get_str(VCONFKEY_SDE_CRYPTO_STATE);
-               if (value != NULL) {
-                       std::string valueStr(value);
-                       free(value);
-                       if (valueStr == "encrypted" && isBootCompleted) {
-                               spawnUI();
-                       }
+               int ret;
+               ret = ::vconf_set_str(VCONFKEY_SDE_MOUNT_POINT, DEFAULT_EXTERNAL_PATH);
+               if (ret != 0) {
+                       ERROR(SINK, "vconf_set() failed with " << ret);
+                       return;
                }
+
+               if (isEncrypted() && isBootCompleted)
+                       spawnUI();
        }
 }
 
@@ -108,14 +125,11 @@ void bootCompletionCallback(dbus::Variant parameters)
                sleep(8);
 
                INFO(SINK, "Boot completed.");
-
-               char *value = ::vconf_get_str(VCONFKEY_SDE_CRYPTO_STATE);
-               if (value != NULL) {
-                       std::string valueStr(value);
-                       free(value);
-                       if (valueStr == "encrypted") {
-                               spawnUI();
-                       }
+               int ret = ::vconf_set_str(VCONFKEY_SDE_MOUNT_POINT, DEFAULT_EXTERNAL_PATH);
+               if (ret != 0) {
+                       ERROR(SINK, "vconf_set() failed with " << ret);
+               } else if (isEncrypted()) {
+                       spawnUI();
                }
                isBootCompleted = true;
        };
@@ -141,6 +155,38 @@ void externalAddEventReceiver()
                                                                bootCompletionCallback);
 }
 
+/*
+ * Introspection data for the exported object
+ *
+ * Test:
+ * dbus-send --system --type=method_call --print-reply --dest=org.tizen.ode
+ * /org/tizen/ode/ExternalEncryption org.freedesktop.DBus.Introspectable.Introspect
+ */
+const gchar introspectionXml[] =
+       "<node>"
+       "  <interface name='org.tizen.ode.ExternalEncryption'>"
+       "    <method name='UpdateCardStatus'>"
+       "      <arg type='s' name='mountpoint' direction='in'/>"
+       "      <arg type='i' name='status' direction='in'/>"
+       "    </method>"
+       "  </interface>"
+       "</node>";
+
+void dbusMethodCall(GDBusConnection *,
+                    const gchar *sender,
+                    const gchar *object,
+                    const gchar *interface,
+                    const gchar *method,
+                    GVariant *params,
+                    GDBusMethodInvocation *invocation,
+                    gpointer user_data)
+{
+       DEBUG(SINK, "dbusMethodCall sender: " << sender << ", object: " << object << ", interface: " <<
+                   interface << ", method: " << method);
+
+       static_cast<ExternalEncryptionServer*>(user_data)->dbusMethodCall(method, params, invocation);
+}
+
 unsigned int getOptions()
 {
        unsigned int result = 0;
@@ -180,7 +226,7 @@ void setOptions(unsigned int options)
        ::vconf_set_bool(VCONFKEY_SDE_ENCRYPT_NEWFILE, value);
 }
 
-} // namsepace
+} // namespace
 
 ExternalEncryptionServer::ExternalEncryptionServer(ServerContext &srv,
                                                                                                   KeyServer& key) :
@@ -204,10 +250,6 @@ ExternalEncryptionServer::ExternalEncryptionServer(ServerContext &srv,
 
        server.createNotification("ExternalEncryptionServer::mount");
 
-       engine.reset(new EXTERNAL_ENGINE(EXTERNAL_PATH,
-                                        EXTERNAL_PATH,
-                                        ProgressBar(VCONFKEY_SDE_ENCRYPT_PROGRESS)));
-
        externalAddEventReceiver();
 }
 
@@ -219,7 +261,7 @@ int ExternalEncryptionServer::setMountPassword(const std::string& password)
 {
        RequestLifetime rl(server);
 
-       return keyServer.get(engine->getSource(), password, mountKey);
+       return keyServer.get(getEngine().getSource(), password, mountKey);
 }
 
 int ExternalEncryptionServer::mount()
@@ -239,14 +281,15 @@ int ExternalEncryptionServer::mount()
                return error::NoSuchDevice;
        }
 
-       if (engine->isMounted()) {
+       auto& engine = getEngine();
+       if (engine.isMounted()) {
                INFO(SINK, "SD card already mounted.");
                return error::None;
        }
 
        INFO(SINK, "Mounting external storage.");
        try {
-               engine->mount(key, getOptions());
+               engine.mount(key, getOptions());
        } catch (runtime::Exception &e) {
                ERROR(SINK, "Failed to mount: " + std::string(e.what()));
                return error::Unknown;
@@ -266,17 +309,18 @@ int ExternalEncryptionServer::umount()
                return error::NoSuchDevice;
        }
 
-       if (!engine->isMounted()) {
+       auto& engine = getEngine();
+       if (!engine.isMounted()) {
                INFO(SINK, "SD card already umounted.");
                return error::None;
        }
 
        INFO(SINK, "Closing all applications using external storage.");
-       killDependentApplications(EXTERNAL_PATH);
+       killDependentApplications(engine.getDestination());
 
        INFO(SINK, "Umounting external storage.");
        try {
-               engine->umount();
+               engine.umount();
        } catch (runtime::Exception &e) {
                ERROR(SINK, "Failed to umount: " + std::string(e.what()));
                return error::Unknown;
@@ -295,18 +339,20 @@ int ExternalEncryptionServer::encrypt(const std::string &password, unsigned int
        }
 
        BinaryData masterKey;
-       int ret = keyServer.get(engine->getSource(), password, masterKey);
+       int ret = keyServer.get(getEngine().getSource(), password, masterKey);
        if (ret != error::None)
                return ret;
 
        auto encryptWorker = [masterKey, options, this](RequestLifetime&& rl) {
                try {
+                       auto& engine = getEngine();
+
                        INFO(SINK, "Closing all applications using external storage.");
-                       killDependentApplications(EXTERNAL_PATH);
+                       killDependentApplications(engine.getDestination());
 
                        INFO(SINK, "Encryption started.");
-                       engine->encrypt(masterKey, options);
-                       setOptions(options & engine->getSupportedOptions());
+                       engine.encrypt(masterKey, options);
+                       setOptions(options & engine.getSupportedOptions());
 
                        INFO(SINK, "Encryption completed.");
                        ::vconf_set_str(VCONFKEY_SDE_CRYPTO_STATE, "encrypted");
@@ -336,28 +382,30 @@ int ExternalEncryptionServer::decrypt(const std::string &password)
        }
 
        BinaryData masterKey;
-       int ret = keyServer.get(engine->getSource(), password, masterKey);
+       int ret = keyServer.get(getEngine().getSource(), password, masterKey);
        if (ret != error::None)
                return ret;
 
        auto decryptWorker = [masterKey, this](RequestLifetime&& rl) {
                try {
+                       auto& engine = getEngine();
+
                        INFO(SINK, "Closing all applications using external storage.");
-                       killDependentApplications(EXTERNAL_PATH);
+                       killDependentApplications(engine.getDestination());
 
                        INFO(SINK, "Umounting external storage.");
                        while (1) {
                                try {
-                                       engine->umount();
+                                       engine.umount();
                                        break;
                                } catch (runtime::Exception &e) {
-                                       killDependentApplications(EXTERNAL_PATH);
+                                       killDependentApplications(engine.getDestination());
                                }
                        }
 
                        INFO(SINK, "Decryption started.");
                        ::vconf_set_str(VCONFKEY_SDE_CRYPTO_STATE, "error_partially_decrypted");
-                       engine->decrypt(masterKey, getOptions());
+                       engine.decrypt(masterKey, getOptions());
 
                        INFO(SINK, "Decryption completed.");
                        ::vconf_set_str(VCONFKEY_SDE_CRYPTO_STATE, "unencrypted");
@@ -383,12 +431,13 @@ int ExternalEncryptionServer::recovery()
                return error::NoSuchDevice;
        }
 
-       for (runtime::DirectoryIterator iter(engine->getSource()), end;
+       auto& engine = getEngine();
+       for (runtime::DirectoryIterator iter(engine.getSource()), end;
                        iter != end; ++iter) {
                iter->remove(true);
        }
 
-       keyServer.removePassword(engine->getSource());
+       keyServer.removePassword(engine.getSource());
        ::vconf_set_str(VCONFKEY_SDE_CRYPTO_STATE, "unencrypted");
 
        return error::None;
@@ -396,28 +445,28 @@ int ExternalEncryptionServer::recovery()
 
 int ExternalEncryptionServer::isPasswordInitialized()
 {
-       return keyServer.isInitialized(engine->getSource());
+       return keyServer.isInitialized(getEngine().getSource());
 }
 
 int ExternalEncryptionServer::initPassword(const std::string& password)
 {
-       return keyServer.init(engine->getSource(), password, Key::DEFAULT_256BIT);
+       return keyServer.init(getEngine().getSource(), password, Key::DEFAULT_256BIT);
 }
 
 int ExternalEncryptionServer::cleanPassword(const std::string& password)
 {
-       return keyServer.remove(engine->getSource(), password);
+       return keyServer.remove(getEngine().getSource(), password);
 }
 
 int ExternalEncryptionServer::changePassword(const std::string &oldPassword,
                                                                                         const std::string &newPassword)
 {
-       return keyServer.changePassword(engine->getSource(), oldPassword, newPassword);
+       return keyServer.changePassword(getEngine().getSource(), oldPassword, newPassword);
 }
 
 int ExternalEncryptionServer::verifyPassword(const std::string& password)
 {
-       return keyServer.verifyPassword(engine->getSource(), password);
+       return keyServer.verifyPassword(getEngine().getSource(), password);
 }
 
 int ExternalEncryptionServer::getState()
@@ -431,14 +480,92 @@ unsigned int ExternalEncryptionServer::getSupportedOptions()
 {
        RequestLifetime rl(server);
 
-       return engine->getSupportedOptions();
+       return getEngine().getSupportedOptions();
 }
 
 std::string ExternalEncryptionServer::getDevicePath() const
 {
        RequestLifetime rl(server);
 
-       return engine->getSource();
+       return getEngine().getSource();
+}
+
+void ExternalEncryptionServer::dbusRegisterObject(GDBusConnection *connection)
+{
+       GError *err = NULL;
+       GDBusNodeInfo *dbusIntrospectionData = g_dbus_node_info_new_for_xml(introspectionXml, &err);
+       if (dbusIntrospectionData == NULL) {
+               ERROR(SINK, "Dbus introspection data creation failed: " << err->message);
+               return;
+       }
+
+       const GDBusInterfaceVTable interfaceVtable =
+       {
+               ode::dbusMethodCall,
+               NULL,
+               NULL
+       };
+
+       /*
+        * Test:
+        * dbus-send --system --type=method_call --print-reply --dest=org.tizen.ode
+        * /org/tizen/ode/ExternalEncryption org.tizen.ode.ExternalEncryption.UpdateCardStatus
+        * string:"/media/SDCardA1" int32:1
+        */
+       guint registrationId = g_dbus_connection_register_object(connection,
+                                                                "/org/tizen/ode/ExternalEncryption",
+                                                                dbusIntrospectionData->interfaces[0],
+                                                                &interfaceVtable,
+                                                                this,
+                                                                NULL,
+                                                                &err);
+       if (registrationId == 0)
+               ERROR(SINK, "Dbus object registration failed: " << err->message);
+
+       g_dbus_node_info_unref(dbusIntrospectionData);
+}
+
+void ExternalEncryptionServer::dbusMethodCall(const gchar *,
+                                              GVariant *params,
+                                              GDBusMethodInvocation *invocation)
+{
+       RequestLifetime rl(server);
+
+       const gchar *mountPoint;
+       gint32 cardStatus;
+
+       g_variant_get(params, "(&si)", &mountPoint, &cardStatus);
+
+       switch (cardStatus) {
+       case 0:
+               DEBUG(SINK, "Card unmounted " << mountPoint);
+               {
+                       int ret = ::vconf_unset(VCONFKEY_SDE_MOUNT_POINT);
+                       if (ret != 0)
+                               ERROR(SINK, "vconf_set_str() failed with " << ret);
+
+                       engine.reset();
+               }
+               break;
+       case 1:
+               DEBUG(SINK, "Card mounted " << mountPoint);
+               {
+                       int ret = ::vconf_set_str(VCONFKEY_SDE_MOUNT_POINT, mountPoint);
+                       if (ret != 0)
+                               ERROR(SINK, "vconf_set_str() failed with " << ret);
+                       else if (isEncrypted())
+                               spawnUI();
+               }
+               break;
+       default:
+               ERROR(SINK, "Unsupported card status: " << cardStatus);
+               g_dbus_method_invocation_return_error(invocation,
+                                                     G_DBUS_ERROR,
+                                                     G_DBUS_ERROR_INVALID_ARGS,
+                                                     "Unsupported card status");
+               return;
+       }
+       g_dbus_method_invocation_return_value(invocation, NULL);
 }
 
 int ExternalEncryptionServer::getStateInternal() const
@@ -461,4 +588,25 @@ int ExternalEncryptionServer::getStateInternal() const
        return State::NotSupported;
 }
 
+EXTERNAL_ENGINE& ExternalEncryptionServer::getEngine() const
+{
+       if (!engine) {
+               char *tmp = ::vconf_get_str(VCONFKEY_SDE_MOUNT_POINT);
+
+               static_assert(DEFAULT_EXTERNAL_PATH);
+
+               std::string mountPoint(tmp ? tmp : DEFAULT_EXTERNAL_PATH);
+               free(tmp);
+
+               runtime::File f(mountPoint);
+               mountPoint = f.readlink();
+
+               engine.reset(new EXTERNAL_ENGINE(mountPoint,
+                                                mountPoint,
+                                                ProgressBar(VCONFKEY_SDE_ENCRYPT_PROGRESS)));
+
+       }
+       return *engine;
+}
+
 } // namespace ode
index 0046b36..9ff0415 100644 (file)
@@ -21,7 +21,7 @@
 #include <mutex>
 #include <memory>
 
-#include <klay/dbus/connection.h>
+#include <gio/gio.h>
 
 #include "rmi/external-encryption.h"
 #include "key-server.h"
@@ -59,12 +59,17 @@ public:
 
        std::string getDevicePath() const;
 
+       void dbusRegisterObject(GDBusConnection *connection);
+
+       void dbusMethodCall(const gchar *method, GVariant *params, GDBusMethodInvocation *invocation);
+
 private:
        int getStateInternal() const;
+       EXTERNAL_ENGINE& getEngine() const;
 
        ServerContext& server;
 
-       std::unique_ptr<EXTERNAL_ENGINE> engine;
+       mutable std::unique_ptr<EXTERNAL_ENGINE> engine;
        BinaryData mountKey;
        KeyServer& keyServer;
 };
index 47a881f..eafb329 100644 (file)
@@ -41,8 +41,37 @@ namespace {
 
 std::unique_ptr<FileLogSink> _sink = nullptr;
 
+void dbusBusAcquired(GDBusConnection *connection, const gchar *, gpointer user_data)
+{
+       DEBUG(SINK, "dbusBusAcquired");
+
+       auto self = static_cast<ServerContext*>(user_data);
+       self->dbusRegisterObjects(connection);
+}
+
+/*
+ * Test:
+ * dbus-send --system --dest=org.freedesktop.DBus --type=method_call --print-reply
+ * /org/freedesktop/DBus org.freedesktop.DBus.ListNames
+ */
+void dbusNameAcquired(GDBusConnection *, const gchar *, gpointer )
+{
+       DEBUG(SINK, "dbusNameAcquired");
+}
+
+void dbusNameLost(GDBusConnection *, const gchar *, gpointer user_data)
+{
+       DEBUG(SINK, "dbusNameLost");
+       static_cast<ServerContext*>(user_data)->stop();
+}
+
 } // namespace
 
+void ServerContext::dbusRegisterObjects(GDBusConnection *connection)
+{
+       externalEncryption->dbusRegisterObject(connection);
+}
+
 /*
  * Timer thread fuction responsible for detecting a server inactivity period after which it will be
  * stopped.
@@ -86,6 +115,7 @@ void ServerContext::timerFunc()
        stop();
 }
 
+
 // This method may be called from different threads
 void ServerContext::requestStarted()
 {
@@ -125,6 +155,18 @@ ServerContext::ServerContext() : rmi::Service(SOCKET_PATH), currentRequests(0),
 
        KeyGenerator::init();
 
+       guint dbusOwnerId = g_bus_own_name(G_BUS_TYPE_SYSTEM,
+                                          DBUS_NAME,
+                                          G_BUS_NAME_OWNER_FLAGS_DO_NOT_QUEUE,
+                                          dbusBusAcquired,
+                                          dbusNameAcquired,
+                                          dbusNameLost,
+                                          this,
+                                          NULL);
+
+       if (dbusOwnerId == 0)
+               throw runtime::Exception("Dbus failed to own name");
+
        std::thread timerThread(&ServerContext::timerFunc, this);
        timerThread.detach();
 }
index 9f3ff0e..b9ef3f3 100644 (file)
@@ -21,6 +21,8 @@
 #include <memory>
 #include <mutex>
 
+#include <gio/gio.h>
+
 #include <klay/file-descriptor.h>
 #include <klay/rmi/service.h>
 
@@ -45,6 +47,8 @@ public:
        void requestStarted();
        void requestFinished();
 
+       void dbusRegisterObjects(GDBusConnection *connection);
+
 private:
        void timerFunc();
 
index cfc238a..6320b91 100644 (file)
@@ -3,8 +3,10 @@ Description=@PROJECT_NAME@ management daemon
 Requires=ode.socket
 
 [Service]
+Type=dbus
+BusName=@DBUS_NAME@
 SmackProcessLabel=System::Privileged
-ExecStart=@BIN_DIR@/@PROJECT_NAME@d
+ExecStart=@BIN_DIR@/@SERVER_NAME@
 Restart=on-failure
 ExecReload=/bin/kill -HUP $MAINPID
 CapabilityBoundingSet=~CAP_MAC_OVERRIDE