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 #define LOG_TAG "DataShareProfileConfig"
16
17 #include "data_share_profile_config.h"
18
19 #include <algorithm>
20 #include <cerrno>
21 #include <fstream>
22 #include <sstream>
23 #include <string>
24 #include <unistd.h>
25
26 #include "bundle_mgr_proxy.h"
27 #include "datashare_errno.h"
28 #include "log_print.h"
29 #include "uri_utils.h"
30 #include "utils/anonymous.h"
31 #include "log_debug.h"
32
33 namespace OHOS {
34 namespace DataShare {
35 constexpr const char *PROFILE_FILE_PREFIX = "$profile:";
36 constexpr const char *SEPARATOR = "/";
37 static constexpr int PATH_SIZE = 2;
38 static constexpr int MAX_ALLOWLIST_COUNT = 256;
39 const size_t PROFILE_PREFIX_LEN = strlen(PROFILE_FILE_PREFIX);
Marshal(json & node) const40 bool Config::Marshal(json &node) const
41 {
42 SetValue(node[GET_NAME(uri)], uri);
43 SetValue(node[GET_NAME(crossUserMode)], crossUserMode);
44 SetValue(node[GET_NAME(readPermission)], readPermission);
45 SetValue(node[GET_NAME(writePermission)], writePermission);
46 return true;
47 }
48
Unmarshal(const json & node)49 bool Config::Unmarshal(const json &node)
50 {
51 bool ret = GetValue(node, GET_NAME(uri), uri);
52 GetValue(node, GET_NAME(crossUserMode), crossUserMode);
53 GetValue(node, GET_NAME(readPermission), readPermission);
54 GetValue(node, GET_NAME(writePermission), writePermission);
55 return ret;
56 }
57
Marshal(json & node) const58 bool LaunchInfo::Marshal(json &node) const
59 {
60 SetValue(node[GET_NAME(storeId)], storeId);
61 SetValue(node[GET_NAME(tableNames)], tableNames);
62 return true;
63 }
64
Unmarshal(const json & node)65 bool LaunchInfo::Unmarshal(const json &node)
66 {
67 GetValue(node, GET_NAME(storeId), storeId);
68 GetValue(node, GET_NAME(tableNames), tableNames);
69 return true;
70 }
71
Marshal(json & node) const72 bool AllowList::Marshal(json &node) const
73 {
74 SetValue(node[GET_NAME(appIdentifier)], appIdentifier);
75 SetValue(node[GET_NAME(onlyMain)], onlyMain);
76 return true;
77 }
78
Unmarshal(const json & node)79 bool AllowList::Unmarshal(const json &node)
80 {
81 // when onlyMain is invalid, do not get appIdentifier, or if appIdentifier matched, onlyMain may not be expected
82 if (GetValue(node, GET_NAME(onlyMain), onlyMain)) {
83 GetValue(node, GET_NAME(appIdentifier), appIdentifier);
84 }
85 return true;
86 }
87
Marshal(json & node) const88 bool ProfileInfo::Marshal(json &node) const
89 {
90 SetValue(node[GET_NAME(tableConfig)], tableConfig);
91 SetValue(node[GET_NAME(isSilentProxyEnable)], isSilentProxyEnable);
92 SetValue(node[GET_NAME(path)], storeName + SEPARATOR + tableName);
93 SetValue(node[GET_NAME(scope)], scope);
94 SetValue(node[GET_NAME(type)], type);
95 SetValue(node[GET_NAME(launchInfos)], launchInfos);
96 SetValue(node[GET_NAME(allowLists)], allowLists);
97 SetValue(node[GET_NAME(storeMetaDataFromUri)], storeMetaDataFromUri);
98 SetValue(node[GET_NAME(launchForCleanData)], launchForCleanData);
99 SetValue(node[GET_NAME(backup)], backup);
100 SetValue(node[GET_NAME(extUri)], extUri);
101 return true;
102 }
103
Unmarshal(const json & node)104 bool ProfileInfo::Unmarshal(const json &node)
105 {
106 GetValue(node, GET_NAME(tableConfig), tableConfig);
107 GetValue(node, GET_NAME(isSilentProxyEnable), isSilentProxyEnable);
108 GetValue(node, GET_NAME(scope), scope);
109 GetValue(node, GET_NAME(type), type);
110 GetValue(node, GET_NAME(launchInfos), launchInfos);
111 GetValue(node, GET_NAME(allowLists), allowLists);
112 if (allowLists.size() > MAX_ALLOWLIST_COUNT) {
113 allowLists.resize(MAX_ALLOWLIST_COUNT);
114 }
115 GetValue(node, GET_NAME(storeMetaDataFromUri), storeMetaDataFromUri);
116 GetValue(node, GET_NAME(launchForCleanData), launchForCleanData);
117 GetValue(node, GET_NAME(backup), backup);
118 GetValue(node, GET_NAME(extUri), extUri);
119 std::string path;
120 auto ret = GetValue(node, GET_NAME(path), path);
121 if (ret) {
122 std::vector<std::string> splitPath;
123 SplitStr(path, SEPARATOR, splitPath);
124 if (splitPath.size() < PATH_SIZE) {
125 return false;
126 }
127
128 if (splitPath[0].empty() || splitPath[1].empty()) {
129 return false;
130 }
131 storeName = splitPath[0];
132 tableName = splitPath[1];
133 }
134 return true;
135 }
136
GetDataProperties(const std::vector<AppExecFwk::Metadata> & metadata,const std::string & resPath,const std::string & hapPath,const std::string & name)137 std::pair<int, ProfileInfo> DataShareProfileConfig::GetDataProperties(
138 const std::vector<AppExecFwk::Metadata> &metadata, const std::string &resPath,
139 const std::string &hapPath, const std::string &name)
140 {
141 ProfileInfo profileInfo;
142 std::string resourcePath = !hapPath.empty() ? hapPath : resPath;
143 std::string info = GetProfileInfoByMetadata(metadata, resourcePath, hapPath, name);
144 if (info.empty()) {
145 return std::make_pair(NOT_FOUND, profileInfo);
146 }
147 if (!profileInfo.Unmarshall(info)) {
148 return std::make_pair(ERROR, profileInfo);
149 }
150 return std::make_pair(SUCCESS, profileInfo);
151 }
152
GetProfileInfoByMetadata(const std::vector<AppExecFwk::Metadata> & metadata,const std::string & resourcePath,const std::string & hapPath,const std::string & name)153 std::string DataShareProfileConfig::GetProfileInfoByMetadata(const std::vector<AppExecFwk::Metadata> &metadata,
154 const std::string &resourcePath, const std::string &hapPath, const std::string &name)
155 {
156 std::string profileInfo;
157 if (metadata.empty() || resourcePath.empty()) {
158 return profileInfo;
159 }
160 auto it = std::find_if(metadata.begin(), metadata.end(), [&name](AppExecFwk::Metadata meta) {
161 return meta.name == name;
162 });
163 if (it != metadata.end()) {
164 std::shared_ptr<ResourceManager> resMgr = InitResMgr(resourcePath);
165 if (resMgr == nullptr) {
166 return profileInfo;
167 }
168 return GetResFromResMgr((*it).resource, *resMgr, hapPath);
169 }
170
171 return profileInfo;
172 }
173
InitResMgr(const std::string & resourcePath)174 std::shared_ptr<ResourceManager> DataShareProfileConfig::InitResMgr(const std::string &resourcePath)
175 {
176 std::shared_ptr<ResourceManager> resMgr(CreateResourceManager());
177 if (resMgr == nullptr) {
178 return nullptr;
179 }
180
181 std::unique_ptr<ResConfig> resConfig(CreateResConfig());
182 if (resConfig == nullptr) {
183 return nullptr;
184 }
185 resMgr->UpdateResConfig(*resConfig);
186 resMgr->AddResource(resourcePath.c_str());
187 return resMgr;
188 }
189
GetResFromResMgr(const std::string & resName,ResourceManager & resMgr,const std::string & hapPath)190 std::string DataShareProfileConfig::GetResFromResMgr(
191 const std::string &resName, ResourceManager &resMgr, const std::string &hapPath)
192 {
193 std::string profileInfo;
194 if (resName.empty()) {
195 return profileInfo;
196 }
197
198 size_t pos = resName.rfind(PROFILE_FILE_PREFIX);
199 if ((pos == std::string::npos) || (pos == resName.length() - PROFILE_PREFIX_LEN)) {
200 ZLOGE("res name invalid, resName is %{public}s", resName.c_str());
201 return profileInfo;
202 }
203 std::string profileName = resName.substr(pos + PROFILE_PREFIX_LEN);
204 // hap is compressed status, get file content.
205 if (!hapPath.empty()) {
206 ZLOGD_MACRO("compressed status.");
207 std::unique_ptr<uint8_t[]> fileContent = nullptr;
208 size_t len = 0;
209 RState ret = resMgr.GetProfileDataByName(profileName.c_str(), len, fileContent);
210 if (ret != SUCCESS || fileContent == nullptr) {
211 ZLOGE("failed, ret is %{public}d, profileName is %{public}s", ret, profileName.c_str());
212 return profileInfo;
213 }
214 if (len == 0) {
215 ZLOGE("fileContent is empty, profileName is %{public}s", profileName.c_str());
216 return profileInfo;
217 }
218 std::string rawData(fileContent.get(), fileContent.get() + len);
219 if (!Config::IsJson(rawData)) {
220 ZLOGE("rawData is not json, profileName is %{public}s", profileName.c_str());
221 return profileInfo;
222 }
223 return rawData;
224 }
225 // hap is decompressed status, get file path then read file.
226 std::string resPath;
227 RState ret = resMgr.GetProfileByName(profileName.c_str(), resPath);
228 if (ret != SUCCESS) {
229 ZLOGE("profileName not found, ret is %{public}d, profileName is %{public}s", ret, profileName.c_str());
230 return profileInfo;
231 }
232 std::string profile = ReadProfile(resPath);
233 if (profile.empty()) {
234 ZLOGE("Read profile failed, resPath is %{public}s", resPath.c_str());
235 return profileInfo;
236 }
237 return profile;
238 }
239
IsFileExisted(const std::string & filePath)240 bool DataShareProfileConfig::IsFileExisted(const std::string &filePath)
241 {
242 if (filePath.empty()) {
243 return false;
244 }
245 if (access(filePath.c_str(), F_OK) != 0) {
246 ZLOGE("can not access file, errno is %{public}d, filePath is %{public}s", errno, filePath.c_str());
247 return false;
248 }
249 return true;
250 }
251
ReadProfile(const std::string & resPath)252 std::string DataShareProfileConfig::ReadProfile(const std::string &resPath)
253 {
254 if (!IsFileExisted(resPath)) {
255 return "";
256 }
257 std::fstream in;
258 in.open(resPath, std::ios_base::in | std::ios_base::binary);
259 if (!in.is_open()) {
260 ZLOGE("the file can not open, errno is %{public}d", errno);
261 return "";
262 }
263 std::ostringstream tmp;
264 tmp << in.rdbuf();
265 std::string content = tmp.str();
266 if (content.empty()) {
267 ZLOGE("the file is empty, resPath is %{public}s", resPath.c_str());
268 return "";
269 }
270 return content;
271 }
272
GetProfileInfo(const std::string & calledBundleName,int32_t currentUserId,std::map<std::string,ProfileInfo> & profileInfos)273 bool DataShareProfileConfig::GetProfileInfo(const std::string &calledBundleName, int32_t currentUserId,
274 std::map<std::string, ProfileInfo> &profileInfos)
275 {
276 BundleConfig bundleInfo;
277 // profile is the same when app clone
278 if (BundleMgrProxy::GetInstance()->GetBundleInfoFromBMS(calledBundleName,
279 currentUserId, bundleInfo) != E_OK) {
280 ZLOGE("data share GetBundleInfoFromBMS failed! bundleName: %{public}s, currentUserId = %{public}d",
281 calledBundleName.c_str(), currentUserId);
282 return false;
283 }
284 for (auto &item : bundleInfo.extensionInfos) {
285 if (item.type != AppExecFwk::ExtensionAbilityType::DATASHARE) {
286 continue;
287 }
288 auto profileInfo = item.profileInfo;
289 if (profileInfo.resultCode == ERROR || profileInfo.resultCode == NOT_FOUND) {
290 continue;
291 }
292 profileInfos[item.uri] = profileInfo.profile;
293 }
294 return true;
295 }
296
GetAccessCrossMode(const ProfileInfo & profileInfo,const std::string & tableUri,const std::string & storeUri)297 AccessCrossMode DataShareProfileConfig::GetAccessCrossMode(const ProfileInfo &profileInfo,
298 const std::string &tableUri, const std::string &storeUri)
299 {
300 auto crossMode = std::make_pair(AccessCrossMode::USER_UNDEFINED, DataShareProfileConfig::UNDEFINED_PRIORITY);
301 for (auto const &item : profileInfo.tableConfig) {
302 if (item.uri == tableUri) {
303 SetCrossUserMode(TABLE_MATCH_PRIORITY, item.crossUserMode, crossMode);
304 continue;
305 }
306 if (item.uri == storeUri) {
307 SetCrossUserMode(STORE_MATCH_PRIORITY, item.crossUserMode, crossMode);
308 continue;
309 }
310 if (item.uri == "*") {
311 SetCrossUserMode(COMMON_MATCH_PRIORITY, item.crossUserMode, crossMode);
312 continue;
313 }
314 }
315 if (crossMode.second != UNDEFINED_PRIORITY) {
316 return crossMode.first;
317 }
318 return AccessCrossMode::USER_UNDEFINED;
319 }
320
SetCrossUserMode(uint8_t priority,uint8_t crossMode,std::pair<AccessCrossMode,int8_t> & mode)321 void DataShareProfileConfig::SetCrossUserMode(uint8_t priority, uint8_t crossMode,
322 std::pair<AccessCrossMode, int8_t> &mode)
323 {
324 if (mode.second < priority && crossMode > AccessCrossMode::USER_UNDEFINED &&
325 crossMode < AccessCrossMode::USER_MAX) {
326 mode.first = static_cast<AccessCrossMode>(crossMode);
327 mode.second = priority;
328 }
329 }
330 } // namespace DataShare
331 } // namespace OHOS
332
333