Unregister package from security manager by pkgid
[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_->pkg_path.get().empty())
17     return false;
18   if (!boost::filesystem::exists(context_->pkg_path.get()) && 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_->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_->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(
59       context_->pkgid.get(), context_->pkg_path.get(), context_->uid.get(),
60       &context_->certificate_info.get(), context_->manifest_data.get(),
61       context_->cross_app_rules.get(), &error_message)) {
62     LOG(ERROR) << "Unsuccessful update";
63     if (!error_message.empty()) {
64       LOG(ERROR) << "error_message: " << error_message;
65       on_error(Status::RECOVERY_ERROR, error_message);
66     }
67     return Status::RECOVERY_ERROR;
68   }
69   if (!RegisterSecurityContextForPath(
70       context_->pkgid.get(), context_->pkg_path.get(), context_->uid.get(),
71       context_->is_readonly_package.get(), &error_message)) {
72     if (!error_message.empty()) {
73       LOG(ERROR) << "error_message: " << error_message;
74       on_error(Status::SECURITY_ERROR, error_message);
75     }
76     return Status::RECOVERY_ERROR;
77   }
78   return Status::OK;
79 }
80
81 }  // namespace security
82 }  // namespace common_installer