• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 std::mutex g_instanceMutex;
29 }
30 
RetentionFileManager()31 RetentionFileManager::RetentionFileManager()
32     : hasInit_(false),
33       fileOperator_(std::make_shared<FileOperator>()),
34       sandboxJsonManager_(std::make_shared<SandboxJsonManager>())
35 {
36     Init();
37 }
38 
~RetentionFileManager()39 RetentionFileManager::~RetentionFileManager() {}
40 
GetInstance()41 RetentionFileManager& RetentionFileManager::GetInstance()
42 {
43     static RetentionFileManager* instance = nullptr;
44     if (instance == nullptr) {
45         std::lock_guard<std::mutex> lock(g_instanceMutex);
46         if (instance == nullptr) {
47             instance = new RetentionFileManager();
48         }
49     }
50     return *instance;
51 }
52 
HasRetentionSandboxInfo(const std::string & bundleName)53 bool RetentionFileManager::HasRetentionSandboxInfo(const std::string& bundleName)
54 {
55     return sandboxJsonManager_->HasRetentionSandboxInfo(bundleName);
56 }
57 
Init()58 bool RetentionFileManager::Init()
59 {
60     std::lock_guard<std::recursive_mutex> lock(mutex_);
61     if (hasInit_) {
62         return true;
63     }
64     if (fileOperator_->IsExistFile(DLP_RETENTION_JSON_PATH)) {
65         std::string constraintsConfigStr;
66         if (fileOperator_->GetFileContentByPath(DLP_RETENTION_JSON_PATH, constraintsConfigStr) != DLP_OK) {
67             return false;
68         }
69         if (constraintsConfigStr.empty()) {
70             hasInit_ = true;
71             return true;
72         }
73         Json callbackInfoJson = Json::parse(constraintsConfigStr, nullptr, false);
74         if (callbackInfoJson.is_discarded()) {
75             DLP_LOG_ERROR(LABEL, "callbackInfoJson is discarded");
76             return false;
77         }
78         sandboxJsonManager_->FromJson(callbackInfoJson);
79     } else {
80         if (fileOperator_->InputFileByPathAndContent(DLP_RETENTION_JSON_PATH, "") != DLP_OK) {
81             DLP_LOG_ERROR(LABEL, "InputFileByPathAndContent failed!");
82             return false;
83         }
84     }
85     hasInit_ = true;
86     return true;
87 }
88 
UpdateFile(const int32_t & jsonRes)89 int32_t RetentionFileManager::UpdateFile(const int32_t& jsonRes)
90 {
91     std::lock_guard<std::recursive_mutex> lock(mutex_);
92     if (jsonRes == DLP_FILE_NO_NEED_UPDATE) {
93         return DLP_OK;
94     }
95     if (jsonRes != DLP_OK) {
96         return jsonRes;
97     }
98     std::string jsonStr = sandboxJsonManager_->ToString();
99     if (fileOperator_->InputFileByPathAndContent(DLP_RETENTION_JSON_PATH, jsonStr) != DLP_OK) {
100         DLP_LOG_ERROR(LABEL, "InputFileByPathAndContent failed!");
101         return DLP_INSERT_FILE_ERROR;
102     }
103     return DLP_OK;
104 }
105 
AddSandboxInfo(const RetentionInfo & retentionInfo)106 int32_t RetentionFileManager::AddSandboxInfo(const RetentionInfo& retentionInfo)
107 {
108     if (!Init()) {
109         DLP_LOG_ERROR(LABEL, "Init failed!");
110         return DLP_RETENTION_UPDATE_ERROR;
111     }
112     int32_t res = sandboxJsonManager_->AddSandboxInfo(retentionInfo);
113     return UpdateFile(res);
114 }
115 
DelSandboxInfo(uint32_t tokenId)116 int32_t RetentionFileManager::DelSandboxInfo(uint32_t tokenId)
117 {
118     if (!Init()) {
119         DLP_LOG_ERROR(LABEL, "Init failed!");
120         return DLP_RETENTION_UPDATE_ERROR;
121     }
122     int32_t res = sandboxJsonManager_->DelSandboxInfo(tokenId);
123     return UpdateFile(res);
124 }
125 
CanUninstall(const uint32_t & tokenId)126 bool RetentionFileManager::CanUninstall(const uint32_t& tokenId)
127 {
128     if (!Init()) {
129         DLP_LOG_ERROR(LABEL, "Init failed!");
130         return false;
131     }
132     return sandboxJsonManager_->CanUninstall(tokenId);
133 }
134 
UpdateSandboxInfo(const std::set<std::string> & docUriSet,RetentionInfo & info,bool isRetention)135 int32_t RetentionFileManager::UpdateSandboxInfo(const std::set<std::string>& docUriSet, RetentionInfo& info,
136     bool isRetention)
137 {
138     if (!Init()) {
139         DLP_LOG_ERROR(LABEL, "Init failed!");
140         return DLP_RETENTION_UPDATE_ERROR;
141     }
142     int32_t res = sandboxJsonManager_->UpdateRetentionState(docUriSet, info, isRetention);
143     return UpdateFile(res);
144 }
145 
RemoveRetentionState(const std::string & bundleName,const int32_t & appIndex)146 int32_t RetentionFileManager::RemoveRetentionState(const std::string& bundleName, const int32_t& appIndex)
147 {
148     if (!Init()) {
149         DLP_LOG_ERROR(LABEL, "Init failed!");
150         return DLP_RETENTION_UPDATE_ERROR;
151     }
152     int32_t res = sandboxJsonManager_->RemoveRetentionState(bundleName, appIndex);
153     return UpdateFile(res);
154 }
155 
ClearUnreservedSandbox()156 int32_t RetentionFileManager::ClearUnreservedSandbox()
157 {
158     if (!Init()) {
159         DLP_LOG_ERROR(LABEL, "Init failed!");
160         return DLP_RETENTION_UPDATE_ERROR;
161     }
162     int32_t res = sandboxJsonManager_->ClearUnreservedSandbox();
163     return UpdateFile(res);
164 }
165 
GetRetentionSandboxList(const std::string & bundleName,std::vector<RetentionSandBoxInfo> & retentionSandBoxInfoVec,bool isRetention)166 int32_t RetentionFileManager::GetRetentionSandboxList(const std::string& bundleName,
167     std::vector<RetentionSandBoxInfo>& retentionSandBoxInfoVec, bool isRetention)
168 {
169     if (!Init()) {
170         DLP_LOG_ERROR(LABEL, "Init failed!");
171         return DLP_RETENTION_UPDATE_ERROR;
172     }
173     return sandboxJsonManager_->GetRetentionSandboxList(bundleName, retentionSandBoxInfoVec, isRetention);
174 }
175 
GetBundleNameSetByUserId(const int32_t userId,std::set<std::string> & bundleNameSet)176 int32_t RetentionFileManager::GetBundleNameSetByUserId(const int32_t userId, std::set<std::string>& bundleNameSet)
177 {
178     if (!Init()) {
179         DLP_LOG_ERROR(LABEL, "Init failed!");
180         return DLP_RETENTION_UPDATE_ERROR;
181     }
182     return sandboxJsonManager_->GetBundleNameSetByUserId(userId, bundleNameSet);
183 }
184 
RemoveRetentionInfoByUserId(const int32_t userId,const std::set<std::string> & bundleNameSet)185 int32_t RetentionFileManager::RemoveRetentionInfoByUserId(const int32_t userId,
186     const std::set<std::string>& bundleNameSet)
187 {
188     if (!Init()) {
189         DLP_LOG_ERROR(LABEL, "Init failed!");
190         return DLP_RETENTION_UPDATE_ERROR;
191     }
192     int32_t res = sandboxJsonManager_->RemoveRetentionInfoByUserId(userId, bundleNameSet);
193     return UpdateFile(res);
194 }
195 
UpdateReadFlag(uint32_t tokenId)196 int32_t RetentionFileManager::UpdateReadFlag(uint32_t tokenId)
197 {
198     if (!Init()) {
199         DLP_LOG_ERROR(LABEL, "Init failed!");
200         return DLP_RETENTION_UPDATE_ERROR;
201     }
202     int32_t res = sandboxJsonManager_->UpdateReadFlag(tokenId);
203     return UpdateFile(res);
204 }
205 } // namespace DlpPermission
206 } // namespace Security
207 } // namespace OHOS
208