0903017231b747d6a2394d4f26c4628867507f98
[platform/core/appfw/app-installers.git] / src / common / step / security / step_update_security.cc
1 // Copyright (c) 2015 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/security/step_update_security.h"
6
7 #include <string>
8
9 #include "common/security_registration.h"
10
11 namespace common_installer {
12 namespace security {
13
14 Step::Status StepUpdateSecurity::process() {
15   AddRecoveryInfo();
16
17   std::string error_message;
18   if (!RegisterSecurityContextForManifest(context_, &error_message)) {
19     if (!error_message.empty()) {
20       LOG(ERROR) << "error_message: " << error_message;
21       on_error(Status::SECURITY_ERROR, error_message);
22     }
23     return Status::SECURITY_ERROR;
24   }
25   if (context_->request_type.get() != RequestType::ReadonlyUpdateUninstall) {
26     if (!RegisterSecurityContextForPath(
27         context_->pkgid.get(), context_->GetPkgPath(), context_->uid.get(),
28         context_->is_readonly_package.get(), &error_message)) {
29       if (!error_message.empty()) {
30         LOG(ERROR) << "error_message: " << error_message;
31         on_error(Status::SECURITY_ERROR, error_message);
32       }
33       return Status::SECURITY_ERROR;
34     }
35   }
36   LOG(DEBUG) << "Security context updated";
37   return Status::OK;
38 }
39
40 Step::Status StepUpdateSecurity::undo() {
41   std::string error_message;
42   if (!RegisterSecurityContextForManifest(context_, &error_message)) {
43     if (!error_message.empty()) {
44       LOG(ERROR) << "error_message: " << error_message;
45     }
46     return Status::SECURITY_ERROR;
47   }
48   if (!RegisterSecurityContextForPath(
49       context_->pkgid.get(), context_->GetPkgPath(), context_->uid.get(),
50       context_->is_readonly_package.get(), &error_message)) {
51     if (!error_message.empty()) {
52       LOG(ERROR) << "error_message: " << error_message;
53       on_error(Status::SECURITY_ERROR, error_message);
54     }
55     return Status::SECURITY_ERROR;
56   }
57   LOG(DEBUG) << "Security context reverted";
58   return Status::OK;
59 }
60
61 void StepUpdateSecurity::AddRecoveryInfo() {
62   recovery::RecoveryFile* recovery_file =
63       context_->recovery_info.get().recovery_file.get();
64   if (!recovery_file)
65     return;
66
67   recovery_file->set_security_operation_done(true);
68   recovery_file->WriteAndCommitFileContent();
69 }
70
71 }  // namespace security
72 }  // namespace common_installer