ffc031e17bb2797dcdda804d9efb7b00906b684d
[platform/core/appfw/app-installers.git] / src / common / step / security / step_recover_security.cc
1 // Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
2 // Use of this source code is governed by an apache-2.0 license that can be
3 // found in the LICENSE file.
4
5 #include "common/step/security/step_recover_security.h"
6
7 #include <boost/filesystem.hpp>
8 #include <string>
9
10 #include "common/security_registration.h"
11
12 namespace common_installer {
13 namespace security {
14
15 bool StepRecoverSecurity::Check(bool is_update) {
16   if (context_->GetPkgPath().empty())
17     return false;
18   if (!boost::filesystem::exists(context_->GetPkgPath()) && is_update)
19     return false;
20   if (context_->pkgid.get().empty())
21     return false;
22   return true;
23 }
24
25 Step::Status StepRecoverSecurity::RecoveryNew() {
26   if (!Check(false))
27     return Status::OK;
28   std::string error_message;
29   if (!context_->manifest_data.get()) {
30     if (!UnregisterSecurityContextForPkgId(context_->pkgid.get(),
31         context_->pkg_type.get(), context_->uid.get(), &error_message, true)) {
32       LOG(ERROR) << "Unsuccessful install";
33       if (!error_message.empty()) {
34         LOG(ERROR) << "error_message: " << error_message;
35         on_error(Status::RECOVERY_ERROR, error_message);
36       }
37       return Status::RECOVERY_ERROR;
38     }
39   } else if (!UnregisterSecurityContextForManifest(
40       context_->pkgid.get(), context_->pkg_type.get(), context_->uid.get(),
41       context_->manifest_data.get(), &error_message)) {
42     LOG(ERROR) << "Unsuccessful install";
43     if (!error_message.empty()) {
44       LOG(ERROR) << "error_message: " << error_message;
45       on_error(Status::RECOVERY_ERROR, error_message);
46     }
47     return Status::RECOVERY_ERROR;
48   }
49   return Status::OK;
50 }
51
52 Step::Status StepRecoverSecurity::RecoveryUpdate() {
53   if (!Check(true)) {
54     LOG(ERROR) << "Invalid parameters";
55     return Status::INVALID_VALUE;
56   }
57   std::string error_message;
58   if (!RegisterSecurityContextForManifest(context_, &error_message)) {
59     LOG(ERROR) << "Unsuccessful update";
60     if (!error_message.empty()) {
61       LOG(ERROR) << "error_message: " << error_message;
62       on_error(Status::RECOVERY_ERROR, error_message);
63     }
64     return Status::RECOVERY_ERROR;
65   }
66   if (!RegisterSecurityContextForPath(
67       context_->pkgid.get(), context_->GetPkgPath(), context_->uid.get(),
68       context_->is_readonly_package.get(), &error_message)) {
69     if (!error_message.empty()) {
70       LOG(ERROR) << "error_message: " << error_message;
71       on_error(Status::RECOVERY_ERROR, error_message);
72     }
73     return Status::RECOVERY_ERROR;
74   }
75   return Status::OK;
76 }
77
78 Step::Status StepRecoverSecurity::RecoveryReadonlyUpdateInstall() {
79   if (!Check(false)) {
80     LOG(ERROR) << "Invalid parameters";
81     return Status::INVALID_VALUE;
82   }
83   std::string error_message;
84   if (!RegisterSecurityContextForManifest(context_, &error_message)) {
85     LOG(ERROR) << "Unsuccessful update";
86     if (!error_message.empty()) {
87       LOG(ERROR) << "error_message: " << error_message;
88       on_error(Status::RECOVERY_ERROR, error_message);
89     }
90     return Status::RECOVERY_ERROR;
91   }
92   return Status::OK;
93 }
94
95 }  // namespace security
96 }  // namespace common_installer