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 "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 static constexpr size_t MAX_FILE_SIZE = 10 * 1024 * 1024;
40 const size_t PROFILE_PREFIX_LEN = strlen(PROFILE_FILE_PREFIX);
Marshal(json & node) const41 bool Config::Marshal(json &node) const
42 {
43 SetValue(node[GET_NAME(uri)], uri);
44 SetValue(node[GET_NAME(crossUserMode)], crossUserMode);
45 SetValue(node[GET_NAME(readPermission)], readPermission);
46 SetValue(node[GET_NAME(writePermission)], writePermission);
47 return true;
48 }
49
Unmarshal(const json & node)50 bool Config::Unmarshal(const json &node)
51 {
52 bool ret = GetValue(node, GET_NAME(uri), uri);
53 GetValue(node, GET_NAME(crossUserMode), crossUserMode);
54 GetValue(node, GET_NAME(readPermission), readPermission);
55 GetValue(node, GET_NAME(writePermission), writePermission);
56 return ret;
57 }
58
Marshal(json & node) const59 bool LaunchInfo::Marshal(json &node) const
60 {
61 SetValue(node[GET_NAME(storeId)], storeId);
62 SetValue(node[GET_NAME(tableNames)], tableNames);
63 return true;
64 }
65
Unmarshal(const json & node)66 bool LaunchInfo::Unmarshal(const json &node)
67 {
68 GetValue(node, GET_NAME(storeId), storeId);
69 GetValue(node, GET_NAME(tableNames), tableNames);
70 return true;
71 }
72
Marshal(json & node) const73 bool AllowList::Marshal(json &node) const
74 {
75 SetValue(node[GET_NAME(appIdentifier)], appIdentifier);
76 SetValue(node[GET_NAME(onlyMain)], onlyMain);
77 return true;
78 }
79
Unmarshal(const json & node)80 bool AllowList::Unmarshal(const json &node)
81 {
82 // when onlyMain is invalid, do not get appIdentifier, or if appIdentifier matched, onlyMain may not be expected
83 if (GetValue(node, GET_NAME(onlyMain), onlyMain)) {
84 GetValue(node, GET_NAME(appIdentifier), appIdentifier);
85 }
86 return true;
87 }
88
Marshal(json & node) const89 bool ProfileInfo::Marshal(json &node) const
90 {
91 SetValue(node[GET_NAME(tableConfig)], tableConfig);
92 SetValue(node[GET_NAME(isSilentProxyEnable)], isSilentProxyEnable);
93 SetValue(node[GET_NAME(path)], storeName + SEPARATOR + tableName);
94 SetValue(node[GET_NAME(scope)], scope);
95 SetValue(node[GET_NAME(type)], type);
96 SetValue(node[GET_NAME(launchInfos)], launchInfos);
97 SetValue(node[GET_NAME(allowLists)], allowLists);
98 SetValue(node[GET_NAME(storeMetaDataFromUri)], storeMetaDataFromUri);
99 SetValue(node[GET_NAME(launchForCleanData)], launchForCleanData);
100 SetValue(node[GET_NAME(backup)], backup);
101 SetValue(node[GET_NAME(extUri)], extUri);
102 return true;
103 }
104
Unmarshal(const json & node)105 bool ProfileInfo::Unmarshal(const json &node)
106 {
107 GetValue(node, GET_NAME(tableConfig), tableConfig);
108 GetValue(node, GET_NAME(isSilentProxyEnable), isSilentProxyEnable);
109 GetValue(node, GET_NAME(scope), scope);
110 GetValue(node, GET_NAME(type), type);
111 GetValue(node, GET_NAME(launchInfos), launchInfos);
112 GetValue(node, GET_NAME(allowLists), allowLists);
113 if (allowLists.size() > MAX_ALLOWLIST_COUNT) {
114 allowLists.resize(MAX_ALLOWLIST_COUNT);
115 }
116 GetValue(node, GET_NAME(storeMetaDataFromUri), storeMetaDataFromUri);
117 GetValue(node, GET_NAME(launchForCleanData), launchForCleanData);
118 GetValue(node, GET_NAME(backup), backup);
119 GetValue(node, GET_NAME(extUri), extUri);
120 std::string path;
121 auto ret = GetValue(node, GET_NAME(path), path);
122 if (ret) {
123 std::vector<std::string> splitPath;
124 SplitStr(path, SEPARATOR, splitPath);
125 if (splitPath.size() < PATH_SIZE) {
126 return false;
127 }
128
129 if (splitPath[0].empty() || splitPath[1].empty()) {
130 return false;
131 }
132 storeName = splitPath[0];
133 tableName = splitPath[1];
134 }
135 return true;
136 }
137
Marshal(json & node) const138 bool SerialDataShareProxyData::Marshal(json &node) const
139 {
140 SetValue(node[GET_NAME(uri)], uri);
141 SetValue(node[GET_NAME(value)], value);
142 SetValue(node[GET_NAME(allowList)], allowList);
143 return true;
144 }
145
Unmarshal(const json & node)146 bool SerialDataShareProxyData::Unmarshal(const json &node)
147 {
148 bool ret = GetValue(node, GET_NAME(uri), uri);
149 // the value in profile can only be string, but the type of value which is published is variant,
150 // use type variant to unmarshal a string will fail, so use a string to try unmarshal again
151 if (!GetValue(node, GET_NAME(value), value)) {
152 std::string valueStr;
153 GetValue(node, GET_NAME(value), valueStr);
154 value = valueStr;
155 }
156 GetValue(node, GET_NAME(allowList), allowList);
157 return ret;
158 }
159
Marshal(json & node) const160 bool ProxyDataProfileInfo::Marshal(json &node) const
161 {
162 SetValue(node[GET_NAME(crossAppSharedConfig)], dataShareProxyDatas);
163 return true;
164 }
165
Unmarshal(const json & node)166 bool ProxyDataProfileInfo::Unmarshal(const json &node)
167 {
168 bool ret = GetValue(node, GET_NAME(crossAppSharedConfig), dataShareProxyDatas);
169 return ret;
170 }
171
GetDataProperties(const std::vector<AppExecFwk::Metadata> & metadata,const std::string & resPath,const std::string & hapPath,const std::string & name)172 std::pair<int, ProfileInfo> DataShareProfileConfig::GetDataProperties(
173 const std::vector<AppExecFwk::Metadata> &metadata, const std::string &resPath,
174 const std::string &hapPath, const std::string &name)
175 {
176 ProfileInfo profileInfo;
177 std::string resourcePath = !hapPath.empty() ? hapPath : resPath;
178 std::string info = GetProfileInfoByMetadata(metadata, resourcePath, hapPath, name);
179 if (info.empty()) {
180 return std::make_pair(NOT_FOUND, profileInfo);
181 }
182 if (!profileInfo.Unmarshall(info)) {
183 return std::make_pair(ERROR, profileInfo);
184 }
185 return std::make_pair(SUCCESS, profileInfo);
186 }
187
GetCrossAppSharedConfig(const std::string & resource,const std::string & resPath,const std::string & hapPath)188 std::pair<int, std::vector<SerialDataShareProxyData>> DataShareProfileConfig::GetCrossAppSharedConfig(
189 const std::string &resource, const std::string &resPath, const std::string &hapPath)
190 {
191 std::vector<SerialDataShareProxyData> serialProxyDatas;
192 ProxyDataProfileInfo profileInfo;
193 std::string resourcePath = !hapPath.empty() ? hapPath : resPath;
194
195 std::shared_ptr<ResourceManager> resMgr = InitResMgr(resourcePath);
196 if (resMgr == nullptr) {
197 return std::make_pair(ERROR, serialProxyDatas);
198 }
199 std::string info = GetResFromResMgr(resource, *resMgr, hapPath);
200 if (info.empty()) {
201 return std::make_pair(NOT_FOUND, serialProxyDatas);
202 }
203 if (!profileInfo.Unmarshall(info)) {
204 return std::make_pair(ERROR, serialProxyDatas);
205 }
206 return std::make_pair(SUCCESS, profileInfo.dataShareProxyDatas);
207 }
208
GetProfileInfoByMetadata(const std::vector<AppExecFwk::Metadata> & metadata,const std::string & resourcePath,const std::string & hapPath,const std::string & name)209 std::string DataShareProfileConfig::GetProfileInfoByMetadata(const std::vector<AppExecFwk::Metadata> &metadata,
210 const std::string &resourcePath, const std::string &hapPath, const std::string &name)
211 {
212 std::string profileInfo;
213 if (metadata.empty() || resourcePath.empty()) {
214 return profileInfo;
215 }
216 auto it = std::find_if(metadata.begin(), metadata.end(), [&name](AppExecFwk::Metadata meta) {
217 return meta.name == name;
218 });
219 if (it != metadata.end()) {
220 std::shared_ptr<ResourceManager> resMgr = InitResMgr(resourcePath);
221 if (resMgr == nullptr) {
222 return profileInfo;
223 }
224 return GetResFromResMgr((*it).resource, *resMgr, hapPath);
225 }
226
227 return profileInfo;
228 }
229
InitResMgr(const std::string & resourcePath)230 std::shared_ptr<ResourceManager> DataShareProfileConfig::InitResMgr(const std::string &resourcePath)
231 {
232 std::shared_ptr<ResourceManager> resMgr(CreateResourceManager(false));
233 if (resMgr == nullptr) {
234 return nullptr;
235 }
236
237 std::unique_ptr<ResConfig> resConfig(CreateResConfig());
238 if (resConfig == nullptr) {
239 return nullptr;
240 }
241 resMgr->UpdateResConfig(*resConfig);
242 resMgr->AddResource(resourcePath.c_str());
243 return resMgr;
244 }
245
GetResFromResMgr(const std::string & resName,ResourceManager & resMgr,const std::string & hapPath)246 std::string DataShareProfileConfig::GetResFromResMgr(
247 const std::string &resName, ResourceManager &resMgr, const std::string &hapPath)
248 {
249 std::string profileInfo;
250 if (resName.empty()) {
251 return profileInfo;
252 }
253
254 size_t pos = resName.rfind(PROFILE_FILE_PREFIX);
255 if ((pos == std::string::npos) || (pos == resName.length() - PROFILE_PREFIX_LEN)) {
256 ZLOGE("res name invalid, resName is %{public}s", resName.c_str());
257 return profileInfo;
258 }
259 std::string profileName = resName.substr(pos + PROFILE_PREFIX_LEN);
260 // hap is compressed status, get file content.
261 if (!hapPath.empty()) {
262 ZLOGD_MACRO("compressed status.");
263 std::unique_ptr<uint8_t[]> fileContent = nullptr;
264 size_t len = 0;
265 RState ret = resMgr.GetProfileDataByName(profileName.c_str(), len, fileContent);
266 if (ret != RState::SUCCESS || fileContent == nullptr) {
267 ZLOGE("failed, ret is %{public}d, profileName is %{public}s", ret, profileName.c_str());
268 return profileInfo;
269 }
270 if (len == 0) {
271 ZLOGE("fileContent is empty, profileName is %{public}s", profileName.c_str());
272 return profileInfo;
273 }
274 std::string rawData(fileContent.get(), fileContent.get() + len);
275 if (!Config::IsJson(rawData)) {
276 ZLOGE("rawData is not json, profileName is %{public}s", profileName.c_str());
277 return profileInfo;
278 }
279 return rawData;
280 }
281 // hap is decompressed status, get file path then read file.
282 std::string resPath;
283 RState ret = resMgr.GetProfileByName(profileName.c_str(), resPath);
284 if (ret != RState::SUCCESS) {
285 ZLOGE("profileName not found, ret is %{public}d, profileName is %{public}s", ret,
286 StringUtils::GeneralAnonymous(profileName).c_str());
287 return profileInfo;
288 }
289 std::string profile = ReadProfile(resPath);
290 if (profile.empty()) {
291 ZLOGE("Read profile failed, resPath is %{public}s", URIUtils::Anonymous(resPath).c_str());
292 return profileInfo;
293 }
294 return profile;
295 }
296
IsFileExisted(const std::string & filePath)297 bool DataShareProfileConfig::IsFileExisted(const std::string &filePath)
298 {
299 if (filePath.empty()) {
300 return false;
301 }
302 if (access(filePath.c_str(), F_OK) != 0) {
303 ZLOGE("can not access file, errno is %{public}d, filePath is %{public}s", errno,
304 URIUtils::Anonymous(filePath).c_str());
305 return false;
306 }
307 return true;
308 }
309
ReadProfile(const std::string & resPath)310 std::string DataShareProfileConfig::ReadProfile(const std::string &resPath)
311 {
312 if (!IsFileExisted(resPath)) {
313 return "";
314 }
315 std::fstream in;
316 in.open(resPath, std::ios_base::in | std::ios_base::binary);
317 if (!in.is_open()) {
318 ZLOGE("the file can not open, errno is %{public}d", errno);
319 return "";
320 }
321 std::ostringstream tmp;
322 tmp << in.rdbuf();
323 std::string content = tmp.str();
324 if (content.empty()) {
325 ZLOGE("the file is empty, resPath is %{public}s", URIUtils::Anonymous(resPath).c_str());
326 return "";
327 } else if (content.length() > MAX_FILE_SIZE) {
328 ZLOGE("the file is too large, resPath is %{public}s,", URIUtils::Anonymous(resPath).c_str());
329 return "";
330 }
331 return content;
332 }
333
GetProfileInfo(const std::string & calledBundleName,int32_t currentUserId,std::map<std::string,ProfileInfo> & profileInfos)334 bool DataShareProfileConfig::GetProfileInfo(const std::string &calledBundleName, int32_t currentUserId,
335 std::map<std::string, ProfileInfo> &profileInfos)
336 {
337 BundleConfig bundleInfo;
338 // profile is the same when app clone
339 if (BundleMgrProxy::GetInstance()->GetBundleInfoFromBMSWithCheck(calledBundleName,
340 currentUserId, bundleInfo) != E_OK) {
341 ZLOGE("data share GetBundleInfoFromBMSWithCheck failed! bundleName: %{public}s, currentUserId = %{public}d",
342 calledBundleName.c_str(), currentUserId);
343 return false;
344 }
345 for (auto &item : bundleInfo.extensionInfos) {
346 if (item.type != AppExecFwk::ExtensionAbilityType::DATASHARE) {
347 continue;
348 }
349 auto profileInfo = item.profileInfo;
350 if (profileInfo.resultCode == ERROR || profileInfo.resultCode == NOT_FOUND) {
351 continue;
352 }
353 profileInfos[item.uri] = profileInfo.profile;
354 }
355 return true;
356 }
357
GetAccessCrossMode(const ProfileInfo & profileInfo,const std::string & tableUri,const std::string & storeUri)358 AccessCrossMode DataShareProfileConfig::GetAccessCrossMode(const ProfileInfo &profileInfo,
359 const std::string &tableUri, const std::string &storeUri)
360 {
361 auto crossMode = std::make_pair(AccessCrossMode::USER_UNDEFINED, DataShareProfileConfig::UNDEFINED_PRIORITY);
362 for (auto const &item : profileInfo.tableConfig) {
363 if (item.uri == tableUri) {
364 SetCrossUserMode(TABLE_MATCH_PRIORITY, item.crossUserMode, crossMode);
365 continue;
366 }
367 if (item.uri == storeUri) {
368 SetCrossUserMode(STORE_MATCH_PRIORITY, item.crossUserMode, crossMode);
369 continue;
370 }
371 if (item.uri == "*") {
372 SetCrossUserMode(COMMON_MATCH_PRIORITY, item.crossUserMode, crossMode);
373 continue;
374 }
375 }
376 if (crossMode.second != UNDEFINED_PRIORITY) {
377 return crossMode.first;
378 }
379 return AccessCrossMode::USER_UNDEFINED;
380 }
381
SetCrossUserMode(uint8_t priority,uint8_t crossMode,std::pair<AccessCrossMode,int8_t> & mode)382 void DataShareProfileConfig::SetCrossUserMode(uint8_t priority, uint8_t crossMode,
383 std::pair<AccessCrossMode, int8_t> &mode)
384 {
385 if (mode.second < priority && crossMode > AccessCrossMode::USER_UNDEFINED &&
386 crossMode < AccessCrossMode::USER_MAX) {
387 mode.first = static_cast<AccessCrossMode>(crossMode);
388 mode.second = priority;
389 }
390 }
391 } // namespace DataShare
392 } // namespace OHOS
393
394