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