Skip the security registration if it is unnecessary
[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   recovery::RecoveryFile* recovery_file =
29       context_->recovery_info.get().recovery_file.get();
30   if (!recovery_file->security_operation_done()) {
31     LOG(DEBUG) << "security_operation_done false skip recover security";
32     return Status::OK;
33   }
34   std::string error_message;
35   if (!context_->manifest_data.get()) {
36     if (!UnregisterSecurityContextForPkgId(context_->pkgid.get(),
37         context_->pkg_type.get(), context_->uid.get(), &error_message, true)) {
38       LOG(ERROR) << "Unsuccessful install";
39       if (!error_message.empty()) {
40         LOG(ERROR) << "error_message: " << error_message;
41         on_error(Status::RECOVERY_ERROR, error_message);
42       }
43       return Status::RECOVERY_ERROR;
44     }
45   } else if (!UnregisterSecurityContextForManifest(
46       context_->pkgid.get(), context_->pkg_type.get(), context_->uid.get(),
47       context_->manifest_data.get(), &error_message)) {
48     LOG(ERROR) << "Unsuccessful install";
49     if (!error_message.empty()) {
50       LOG(ERROR) << "error_message: " << error_message;
51       on_error(Status::RECOVERY_ERROR, error_message);
52     }
53     return Status::RECOVERY_ERROR;
54   }
55   return Status::OK;
56 }
57
58 Step::Status StepRecoverSecurity::RecoveryUpdate() {
59   if (!Check(true)) {
60     LOG(ERROR) << "Invalid parameters";
61     return Status::INVALID_VALUE;
62   }
63   recovery::RecoveryFile* recovery_file =
64       context_->recovery_info.get().recovery_file.get();
65   if (!recovery_file->security_operation_done()) {
66     LOG(DEBUG) << "security_operation_done false skip recover security";
67     return Status::OK;
68   }
69   std::string error_message;
70   if (!RegisterSecurityContextForManifest(context_, &error_message)) {
71     LOG(ERROR) << "Unsuccessful update";
72     if (!error_message.empty()) {
73       LOG(ERROR) << "error_message: " << error_message;
74       on_error(Status::RECOVERY_ERROR, error_message);
75     }
76     return Status::RECOVERY_ERROR;
77   }
78   if (!RegisterSecurityContextForPath(
79       context_->pkgid.get(), context_->GetPkgPath(), context_->uid.get(),
80       context_->is_readonly_package.get(), &error_message)) {
81     if (!error_message.empty()) {
82       LOG(ERROR) << "error_message: " << error_message;
83       on_error(Status::RECOVERY_ERROR, error_message);
84     }
85     return Status::RECOVERY_ERROR;
86   }
87   return Status::OK;
88 }
89
90 Step::Status StepRecoverSecurity::RecoveryReadonlyUpdateInstall() {
91   if (!Check(false)) {
92     LOG(ERROR) << "Invalid parameters";
93     return Status::INVALID_VALUE;
94   }
95   recovery::RecoveryFile* recovery_file =
96       context_->recovery_info.get().recovery_file.get();
97   if (!recovery_file->security_operation_done()) {
98     LOG(DEBUG) << "security_operation_done false skip recover security";
99     return Status::OK;
100   }
101   std::string error_message;
102   if (!RegisterSecurityContextForManifest(context_, &error_message)) {
103     LOG(ERROR) << "Unsuccessful update";
104     if (!error_message.empty()) {
105       LOG(ERROR) << "error_message: " << error_message;
106       on_error(Status::RECOVERY_ERROR, error_message);
107     }
108     return Status::RECOVERY_ERROR;
109   }
110   return Status::OK;
111 }
112
113 }  // namespace security
114 }  // namespace common_installer