1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "retention_file_manager.h"
17 #include "dlp_permission.h"
18 #include "dlp_permission_log.h"
19
20 namespace OHOS {
21 namespace Security {
22 namespace DlpPermission {
23 namespace {
24 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, SECURITY_DOMAIN_DLP_PERMISSION, "RetentionFileManager" };
25 const std::string PATH_SEPARATOR = "/";
26 const std::string USER_INFO_BASE = "/data/service/el1/public/dlp_permission_service";
27 const std::string DLP_RETENTION_JSON_PATH = USER_INFO_BASE + PATH_SEPARATOR + "retention_sandbox_info.json";
28 }
29
RetentionFileManager()30 RetentionFileManager::RetentionFileManager()
31 : hasInit_(false),
32 fileOperator_(std::make_shared<FileOperator>()),
33 sandboxJsonManager_(std::make_shared<SandboxJsonManager>())
34 {
35 Init();
36 }
37
~RetentionFileManager()38 RetentionFileManager::~RetentionFileManager() {}
39
GetInstance()40 RetentionFileManager& RetentionFileManager::GetInstance()
41 {
42 static RetentionFileManager instance;
43 return instance;
44 }
45
HasRetentionSandboxInfo(const std::string & bundleName)46 bool RetentionFileManager::HasRetentionSandboxInfo(const std::string& bundleName)
47 {
48 return sandboxJsonManager_->HasRetentionSandboxInfo(bundleName);
49 }
50
Init()51 bool RetentionFileManager::Init()
52 {
53 std::lock_guard<std::recursive_mutex> lock(mutex_);
54 if (hasInit_) {
55 return true;
56 }
57 if (fileOperator_->IsExistFile(DLP_RETENTION_JSON_PATH)) {
58 std::string constraintsConfigStr;
59 if (fileOperator_->GetFileContentByPath(DLP_RETENTION_JSON_PATH, constraintsConfigStr) != DLP_OK) {
60 return false;
61 }
62 if (constraintsConfigStr.empty()) {
63 hasInit_ = true;
64 return true;
65 }
66 Json callbackInfoJson = Json::parse(constraintsConfigStr, nullptr, false);
67 if (callbackInfoJson.is_discarded()) {
68 DLP_LOG_ERROR(LABEL, "callbackInfoJson is discarded");
69 return false;
70 }
71 sandboxJsonManager_->FromJson(callbackInfoJson);
72 } else {
73 if (fileOperator_->InputFileByPathAndContent(DLP_RETENTION_JSON_PATH, "") != DLP_OK) {
74 DLP_LOG_ERROR(LABEL, "InputFileByPathAndContent failed!");
75 return false;
76 }
77 }
78 hasInit_ = true;
79 return true;
80 }
81
UpdateFile(const int32_t & jsonRes)82 int32_t RetentionFileManager::UpdateFile(const int32_t& jsonRes)
83 {
84 std::lock_guard<std::recursive_mutex> lock(mutex_);
85 if (jsonRes == DLP_FILE_NO_NEED_UPDATE) {
86 return DLP_OK;
87 }
88 if (jsonRes != DLP_OK) {
89 return jsonRes;
90 }
91 std::string jsonStr = sandboxJsonManager_->ToString();
92 if (fileOperator_->InputFileByPathAndContent(DLP_RETENTION_JSON_PATH, jsonStr) != DLP_OK) {
93 DLP_LOG_ERROR(LABEL, "InputFileByPathAndContent failed!");
94 return DLP_INSERT_FILE_ERROR;
95 }
96 return DLP_OK;
97 }
98
AddSandboxInfo(const RetentionInfo & retentionInfo)99 int32_t RetentionFileManager::AddSandboxInfo(const RetentionInfo& retentionInfo)
100 {
101 if (!Init()) {
102 DLP_LOG_ERROR(LABEL, "Init failed!");
103 return DLP_RETENTION_UPDATE_ERROR;
104 }
105 int32_t res = sandboxJsonManager_->AddSandboxInfo(retentionInfo);
106 return UpdateFile(res);
107 }
108
DelSandboxInfo(uint32_t tokenId)109 int32_t RetentionFileManager::DelSandboxInfo(uint32_t tokenId)
110 {
111 if (!Init()) {
112 DLP_LOG_ERROR(LABEL, "Init failed!");
113 return DLP_RETENTION_UPDATE_ERROR;
114 }
115 int32_t res = sandboxJsonManager_->DelSandboxInfo(tokenId);
116 return UpdateFile(res);
117 }
118
CanUninstall(const uint32_t & tokenId)119 bool RetentionFileManager::CanUninstall(const uint32_t& tokenId)
120 {
121 if (!Init()) {
122 DLP_LOG_ERROR(LABEL, "Init failed!");
123 return false;
124 }
125 return sandboxJsonManager_->CanUninstall(tokenId);
126 }
127
UpdateSandboxInfo(const std::set<std::string> & docUriSet,RetentionInfo & info,bool isRetention)128 int32_t RetentionFileManager::UpdateSandboxInfo(const std::set<std::string>& docUriSet, RetentionInfo& info,
129 bool isRetention)
130 {
131 if (!Init()) {
132 DLP_LOG_ERROR(LABEL, "Init failed!");
133 return DLP_RETENTION_UPDATE_ERROR;
134 }
135 int32_t res = sandboxJsonManager_->UpdateRetentionState(docUriSet, info, isRetention);
136 return UpdateFile(res);
137 }
138
RemoveRetentionState(const std::string & bundleName,const int32_t & appIndex)139 int32_t RetentionFileManager::RemoveRetentionState(const std::string& bundleName, const int32_t& appIndex)
140 {
141 if (!Init()) {
142 DLP_LOG_ERROR(LABEL, "Init failed!");
143 return DLP_RETENTION_UPDATE_ERROR;
144 }
145 int32_t res = sandboxJsonManager_->RemoveRetentionState(bundleName, appIndex);
146 return UpdateFile(res);
147 }
148
ClearUnreservedSandbox()149 int32_t RetentionFileManager::ClearUnreservedSandbox()
150 {
151 if (!Init()) {
152 DLP_LOG_ERROR(LABEL, "Init failed!");
153 return DLP_RETENTION_UPDATE_ERROR;
154 }
155 int32_t res = sandboxJsonManager_->ClearUnreservedSandbox();
156 return UpdateFile(res);
157 }
158
GetRetentionSandboxList(const std::string & bundleName,std::vector<RetentionSandBoxInfo> & retentionSandBoxInfoVec,bool isRetention)159 int32_t RetentionFileManager::GetRetentionSandboxList(const std::string& bundleName,
160 std::vector<RetentionSandBoxInfo>& retentionSandBoxInfoVec, bool isRetention)
161 {
162 if (!Init()) {
163 DLP_LOG_ERROR(LABEL, "Init failed!");
164 return DLP_RETENTION_UPDATE_ERROR;
165 }
166 return sandboxJsonManager_->GetRetentionSandboxList(bundleName, retentionSandBoxInfoVec, isRetention);
167 }
168
GetBundleNameSetByUserId(const int32_t userId,std::set<std::string> & bundleNameSet)169 int32_t RetentionFileManager::GetBundleNameSetByUserId(const int32_t userId, std::set<std::string>& bundleNameSet)
170 {
171 if (!Init()) {
172 DLP_LOG_ERROR(LABEL, "Init failed!");
173 return DLP_RETENTION_UPDATE_ERROR;
174 }
175 return sandboxJsonManager_->GetBundleNameSetByUserId(userId, bundleNameSet);
176 }
177
RemoveRetentionInfoByUserId(const int32_t userId,const std::set<std::string> & bundleNameSet)178 int32_t RetentionFileManager::RemoveRetentionInfoByUserId(const int32_t userId,
179 const std::set<std::string>& bundleNameSet)
180 {
181 if (!Init()) {
182 DLP_LOG_ERROR(LABEL, "Init failed!");
183 return DLP_RETENTION_UPDATE_ERROR;
184 }
185 int32_t res = sandboxJsonManager_->RemoveRetentionInfoByUserId(userId, bundleNameSet);
186 return UpdateFile(res);
187 }
188
UpdateReadFlag(uint32_t tokenId)189 int32_t RetentionFileManager::UpdateReadFlag(uint32_t tokenId)
190 {
191 if (!Init()) {
192 DLP_LOG_ERROR(LABEL, "Init failed!");
193 return DLP_RETENTION_UPDATE_ERROR;
194 }
195 int32_t res = sandboxJsonManager_->UpdateReadFlag(tokenId);
196 return UpdateFile(res);
197 }
198 } // namespace DlpPermission
199 } // namespace Security
200 } // namespace OHOS
201