Internal encryption: Fix missing elm config files and locales
[platform/core/security/ode.git] / server / internal-encryption.cpp
index 284e436..6a8a90a 100644 (file)
@@ -21,6 +21,7 @@
 #include <sys/reboot.h>
 
 #include <vconf.h>
+#include <tzplatform_config.h>
 #include <klay/process.h>
 #include <klay/file-user.h>
 #include <klay/filesystem.h>
@@ -29,7 +30,7 @@
 
 #include "vconf.h"
 #include "progress-bar.h"
-#include "engine/dmcrypt-engine.h"
+#include "engine/encryption/dmcrypt-engine.h"
 #include "key-manager/key-manager.h"
 
 #include "rmi/internal-encryption.h"
 
 #define PRIVILEGE_PLATFORM "http://tizen.org/privilege/internal/default/platform"
 
+const std::string PROG_FACTORY_RESET = "/usr/bin/dbus-send";
+const std::vector<std::string> wipeCommand = {
+    PROG_FACTORY_RESET,
+    "--system",
+    "--type=signal",
+    "--print-reply",
+    "--dest=com.samsung.factoryreset",
+    "/com/samsung/factoryreset",
+    "com.samsung.factoryreset.start.setting"
+};
+
 namespace ode {
 
 namespace {
@@ -80,6 +92,27 @@ void stopDependedSystemdServices()
 }
 
 void showProgressUI(const std::string type) {
+       ::tzplatform_set_user(::tzplatform_getuid(TZ_SYS_DEFAULT_USER));
+       std::string defaultUserHome(::tzplatform_getenv(TZ_USER_HOME));
+       ::tzplatform_reset_user();
+
+       ::tzplatform_set_user(::getuid());
+       std::string currentUserHome(::tzplatform_getenv(TZ_USER_HOME));
+       ::tzplatform_reset_user();
+
+       INFO("Home directory : " + currentUserHome);
+
+       runtime::File shareDirectory(currentUserHome + "/share");
+       if (!shareDirectory.exists()) {
+               shareDirectory.makeDirectory();
+       }
+
+       runtime::File elmConfigDir(currentUserHome + "/share/.elementary");
+       if (!elmConfigDir.exists()) {
+               runtime::File defaultElmConfigDir(defaultUserHome + "/share/.elementary");
+               defaultElmConfigDir.copyTo(shareDirectory.getPath());
+       }
+
        std::vector<std::string> args = {
                "ode", "progress", type, "Internal"
        };
@@ -204,7 +237,6 @@ int InternalEncryption::encrypt(const std::string& password, unsigned int option
                        }
 
                        INFO("Encryption started...");
-                       ::vconf_set_str(INTERNAL_STATE_VCONF_KEY, "error_partially_encrypted");
                        engine->encrypt(MasterKey, options);
                        setOptions(options & getSupportedOptions());
                        INFO("Sync disk...");
@@ -212,10 +244,11 @@ int InternalEncryption::encrypt(const std::string& password, unsigned int option
                        INFO("Encryption completed");
 
                        ::vconf_set_str(INTERNAL_STATE_VCONF_KEY, "encrypted");
+                       ::reboot(RB_AUTOBOOT);
                } catch (runtime::Exception &e) {
+                       ::vconf_set_str(INTERNAL_STATE_VCONF_KEY, "error_partially_encrypted");
                        ERROR("Encryption failed - " + std::string(e.what()));
                }
-               ::reboot(RB_AUTOBOOT);
        };
 
        std::thread asyncWork(encryptWorker);
@@ -255,17 +288,17 @@ int InternalEncryption::decrypt(const std::string& password)
                        }
 
                        INFO("Decryption started...");
-                       ::vconf_set_str(INTERNAL_STATE_VCONF_KEY, "error_partially_encrypted");
                        engine->decrypt(MasterKey, getOptions());
                        INFO("Sync disk...");
                        sync();
                        INFO("Decryption completed");
 
                        ::vconf_set_str(INTERNAL_STATE_VCONF_KEY, "unencrypted");
+                       ::reboot(RB_AUTOBOOT);
                } catch (runtime::Exception &e) {
+                       ::vconf_set_str(INTERNAL_STATE_VCONF_KEY, "error_partially_encrypted");
                        ERROR("Decryption failed - " + std::string(e.what()));
                }
-               ::reboot(RB_AUTOBOOT);
        };
 
        std::thread asyncWork(decryptWorker);
@@ -274,6 +307,22 @@ int InternalEncryption::decrypt(const std::string& password)
        return 0;
 }
 
+int InternalEncryption::recovery()
+{
+       if (getState() != State::Unencrypted) {
+               return -1;
+       }
+
+       //TODO
+       runtime::Process proc(PROG_FACTORY_RESET, wipeCommand);
+       if (proc.execute() == -1) {
+               ERROR("Failed to launch factory-reset");
+               return -2;
+       }
+
+       return 0;
+}
+
 int InternalEncryption::isPasswordInitialized()
 {
        if (engine->isKeyMetaSet()) {