Remove boost dependency
[platform/core/appfw/app-installers.git] / src / common / step / filesystem / step_migrate_legacy_external_image.cc
1 // Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
2 // Use of this source code is governed by a apache 2.0 license that can be
3 // found in the LICENSE file.
4
5 #include "common/step/filesystem/step_migrate_legacy_external_image.h"
6
7 #include <app2ext_interface.h>
8 #include <tzplatform_config.h>
9
10 #include <filesystem>
11 #include <string>
12
13 #include "common/security_registration.h"
14 #include "common/utils/pkgmgr_query.h"
15
16 namespace fs = std::filesystem;
17
18 namespace {
19
20 const uid_t kDefaultUserUid = tzplatform_getuid(TZ_SYS_DEFAULT_USER);
21 const uid_t kGlobalUserUid = tzplatform_getuid(TZ_SYS_GLOBALAPP_USER);
22 const char kInstalledExternally[] = "installed_external";
23
24 }  // namespace
25
26 namespace common_installer {
27 namespace security {
28
29 Step::Status StepMigrateLegacyExtImage::precheck() {
30   // Policy : migration target user is 'default' user.
31   uid_t uid =  context_->uid.get();
32   if (uid != kDefaultUserUid && uid != kGlobalUserUid) {
33     LOG(ERROR) << "current target uid (" << uid <<") is not allowed";
34     return Step::Status::INVALID_VALUE;
35   }
36
37   if (context_->GetPkgPath().empty()) {
38     LOG(ERROR) << "pkg_path attribute is empty";
39     return Step::Status::INVALID_VALUE;
40   }
41   if (!std::filesystem::exists(context_->GetPkgPath())) {
42     LOG(ERROR) << "pkg_path ("
43                << context_->GetPkgPath()
44                << ") path does not exist";
45     return Step::Status::INVALID_VALUE;
46   }
47
48   if (context_->pkgid.get().empty()) {
49     LOG(ERROR) << "pkgid attribute is empty";
50     return Step::Status::INVALID_VALUE;
51   }
52
53   return Status::OK;
54 }
55
56 Step::Status StepMigrateLegacyExtImage::process() {
57   std::string pkgid = context_->pkgid.get();
58   uid_t uid = context_->uid.get();
59   PkgQueryInterface pkg_query(pkgid, uid);
60   if (pkg_query.IsGlobalPackage())
61     context_->uid.set(kGlobalUserUid);
62
63   std::string storage_str = pkg_query.StorageForPkgId();
64   if (strcmp(storage_str.c_str(), kInstalledExternally)) {
65     LOG(ERROR) << "Pkg not installed at external storage";
66     return Step::Status::ERROR;
67   }
68
69   context_->external_storage =
70       ExternalStorage::MigrateExternalStorage(context_->request_type.get(),
71           context_->pkgid.get(),
72           context_->uid.get());
73   if (!context_->external_storage) {
74     LOG(ERROR) << "Can not initialize external storage";
75     return Step::Status::ERROR;
76   }
77
78   std::string error_message;
79   if (!RegisterSecurityContextForPathExternalOnly(pkgid,
80                         context_->pkg_type.get(), context_->GetPkgPath(),
81                         uid, &error_message)) {
82     if (!error_message.empty()) {
83       LOG(ERROR) << "error_message: " << error_message;
84       on_error_->on_error(Status::SECURITY_ERROR, error_message);
85     }
86     return Status::SECURITY_ERROR;
87   }
88
89   LOG(DEBUG) << "Security context installed";
90   return Status::OK;
91 }
92
93 Step::Status StepMigrateLegacyExtImage::undo() {
94   if (context_->external_storage)
95     context_->external_storage->Abort();
96   return Status::OK;
97 }
98
99 Step::Status StepMigrateLegacyExtImage::clean() {
100   if (context_->external_storage)
101     context_->external_storage->Commit();
102   return Status::OK;
103 }
104
105 }  // namespace security
106 }  // namespace common_installer