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 "LoadConfigFromDataProxyNodeStrategy"
16
17 #include "load_config_from_data_proxy_node_strategy.h"
18
19 #include "bundle_mgr_proxy.h"
20 #include "common/uri_utils.h"
21 #include "datashare_errno.h"
22 #include "log_print.h"
23
24 namespace OHOS::DataShare {
operator ()(std::shared_ptr<Context> context)25 bool LoadConfigFromDataProxyNodeStrategy::operator()(std::shared_ptr<Context> context)
26 {
27 if (!LoadConfigFromUri(context)) {
28 return false;
29 }
30 context->type = DataProperties::PUBLISHED_DATA_TYPE;
31 if (!BundleMgrProxy::GetInstance()->GetBundleInfoFromBMS(
32 context->calledBundleName, context->currentUserId, context->bundleInfo)) {
33 ZLOGE("GetBundleInfoFromBMS failed! bundleName: %{public}s", context->calledBundleName.c_str());
34 context->errCode = E_BUNDLE_NAME_NOT_EXIST;
35 return false;
36 }
37 if (context->uri.empty()) {
38 context->permission = "reject";
39 return true;
40 }
41 for (auto &hapModuleInfo : context->bundleInfo.hapModuleInfos) {
42 auto proxyDatas = hapModuleInfo.proxyDatas;
43 for (auto &proxyData : proxyDatas) {
44 if (proxyData.uri != context->uri) {
45 continue;
46 }
47 context->permission = context->isRead ? proxyData.requiredReadPermission
48 : proxyData.requiredWritePermission;
49 if (context->permission.empty()) {
50 context->permission = "reject";
51 }
52 DataProperties properties;
53 bool isCompressed = !hapModuleInfo.hapPath.empty();
54 std::string resourcePath = isCompressed ? hapModuleInfo.hapPath : hapModuleInfo.resourcePath;
55 if (!DataShareProfileInfo::GetDataPropertiesFromProxyDatas(
56 proxyData, resourcePath, isCompressed, properties)) {
57 return true;
58 }
59 GetContextInfoFromDataProperties(properties, hapModuleInfo.moduleName, context);
60 return true;
61 }
62 }
63 if (context->callerBundleName == context->calledBundleName) {
64 ZLOGI("access private data, caller and called is same, go");
65 return true;
66 }
67 // cross permission can only cross uri like weather,can not cross like datashareproxy://weather
68 if (context->isAllowCrossPer && !URIUtils::IsDataProxyURI(context->uri)) {
69 ZLOGI("access has white permission, go");
70 return true;
71 }
72 context->errCode = E_URI_NOT_EXIST;
73 ZLOGI("not find DataProperties! %{private}s is private", context->uri.c_str());
74 return false;
75 }
76
GetContextInfoFromDataProperties(const DataProperties & properties,const std::string & moduleName,std::shared_ptr<Context> context)77 bool LoadConfigFromDataProxyNodeStrategy::GetContextInfoFromDataProperties(const DataProperties &properties,
78 const std::string &moduleName, std::shared_ptr<Context> context)
79 {
80 if (properties.scope == DataProperties::MODULE_SCOPE) {
81 // module scope
82 context->calledModuleName = moduleName;
83 }
84 context->calledStoreName = properties.storeName;
85 context->calledTableName = properties.tableName;
86 context->type = properties.type;
87 return true;
88 }
89
LoadConfigFromUri(std::shared_ptr<Context> context)90 bool LoadConfigFromDataProxyNodeStrategy::LoadConfigFromUri(std::shared_ptr<Context> context)
91 {
92 if (!URIUtils::GetBundleNameFromProxyURI(context->uri, context->calledBundleName)) {
93 return false;
94 }
95 return true;
96 }
97 } // namespace OHOS::DataShare