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