Fix smack labeling for lib rpk
[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
79   if (!HasOwnerRwOtherRoPaths(context_->GetPkgPath()))
80     return Status::OK;
81
82   if (!RegisterSecurityContextForPath(context_->pkgid.get(),
83       context_->pkg_type.get(), context_->GetPkgPath(), context_->uid.get(),
84       context_->is_readonly_package.get(), &error_message)) {
85     if (!error_message.empty()) {
86       LOG(ERROR) << "error_message: " << error_message;
87       on_error(Status::RECOVERY_ERROR, error_message);
88     }
89     return Status::RECOVERY_ERROR;
90   }
91   return Status::OK;
92 }
93
94 Step::Status StepRecoverSecurity::RecoveryReadonlyUpdateInstall() {
95   if (!Check(false)) {
96     LOG(ERROR) << "Invalid parameters";
97     return Status::INVALID_VALUE;
98   }
99   recovery::RecoveryFile* recovery_file =
100       context_->recovery_info.get().recovery_file.get();
101   if (!recovery_file->security_operation_done()) {
102     LOG(DEBUG) << "security_operation_done false skip recover security";
103     return Status::OK;
104   }
105   std::string error_message;
106   if (!RegisterSecurityContextForManifest(context_, &error_message)) {
107     LOG(ERROR) << "Unsuccessful update";
108     if (!error_message.empty()) {
109       LOG(ERROR) << "error_message: " << error_message;
110       on_error(Status::RECOVERY_ERROR, error_message);
111     }
112     return Status::RECOVERY_ERROR;
113   }
114   return Status::OK;
115 }
116
117 }  // namespace security
118 }  // namespace common_installer