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/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()->GetBundleInfoFromBMSWithCheck(
34 context->calledBundleName, context->visitedUserId, context->bundleInfo) != E_OK) {
35 ZLOGE("GetBundleInfoFromBMSWithCheck 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 std::string uri = context->uri;
44 URIUtils::FormatUri(uri);
45 for (auto const &hapModuleInfo : context->bundleInfo.hapModuleInfos) {
46 auto proxyDatas = hapModuleInfo.proxyDatas;
47 for (auto const &proxyData : proxyDatas) {
48 if (proxyData.uri != uri) {
49 continue;
50 }
51 context->permission = context->isRead ? proxyData.requiredReadPermission
52 : proxyData.requiredWritePermission;
53 if (context->permission.empty()) {
54 context->permission = "reject";
55 }
56 auto properties = proxyData.profileInfo;
57 if (properties.resultCode == ERROR || properties.resultCode == NOT_FOUND) {
58 return true;
59 }
60 GetContextInfoFromDataProperties(properties.profile, hapModuleInfo.moduleName, context);
61 return true;
62 }
63 }
64 if (context->callerBundleName == context->calledBundleName) {
65 ZLOGI("access private data, caller and called is same, go");
66 return true;
67 }
68 // cross permission can only cross uri like weather,can not cross like datashareproxy://weather
69 if (context->isAllowCrossPer && !URIUtils::IsDataProxyURI(context->uri)) {
70 ZLOGI("access has white permission, go");
71 return true;
72 }
73 context->errCode = E_URI_NOT_EXIST;
74 ZLOGI("not find DataProperties! %{public}s is private", URIUtils::Anonymous(context->uri).c_str());
75 return false;
76 }
77
GetContextInfoFromDataProperties(const ProfileInfo & properties,const std::string & moduleName,std::shared_ptr<Context> context)78 bool LoadConfigFromDataProxyNodeStrategy::GetContextInfoFromDataProperties(const ProfileInfo &properties,
79 const std::string &moduleName, std::shared_ptr<Context> context)
80 {
81 if (properties.scope == MODULE_SCOPE) {
82 // module scope
83 context->calledModuleName = moduleName;
84 }
85 context->calledStoreName = properties.storeName;
86 context->calledTableName = properties.tableName;
87 context->type = properties.type;
88 return true;
89 }
90
LoadConfigFromUri(std::shared_ptr<Context> context)91 bool LoadConfigFromDataProxyNodeStrategy::LoadConfigFromUri(std::shared_ptr<Context> context)
92 {
93 if (!URIUtils::GetBundleNameFromProxyURI(context->uri, context->calledBundleName)) {
94 return false;
95 }
96 return true;
97 }
98 } // namespace OHOS::DataShare