1 /*
2 * Copyright (c) 2025 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
16 #include <dlfcn.h>
17 #include <securec.h>
18 #include <cstring>
19 #include "accesstoken_kit.h"
20 #include "ipc_skeleton.h"
21 #include "lnn_ohos_account_adapter.h"
22 #include "softbus_error_code.h"
23 #include "trans_log.h"
24
25 #ifdef __aarch64__
26 #define BR_PROXY_ADAPTER_PATH "/system/lib64/libbr_proxy_adapter.z.so"
27 #else
28 #define BR_PROXY_ADAPTER_PATH "/system/lib/libbr_proxy_adapter.z.so"
29 #endif
30
31
32 using namespace OHOS;
33
34 typedef int32_t (*GetAbilityNameFunc)(char *abilityName, int32_t userId, uint32_t abilityNameLen,
35 std::string bundleName);
36 typedef int32_t (*StartAbilityFunc)(const char *bundleName, const char *abilityName);
37 static void* g_abilityMgrHandle = nullptr;
38 static StartAbilityFunc g_startAbilityFunc = nullptr;
39 static GetAbilityNameFunc g_getAbilityName = nullptr;
40
AbilityManagerClientDynamicLoader(const char * bundleName,const char * abilityName)41 static int32_t AbilityManagerClientDynamicLoader(const char *bundleName, const char *abilityName)
42 {
43 if (bundleName == nullptr || abilityName == nullptr) {
44 TRANS_LOGE(TRANS_SVC, "[br_proxy] invalid param");
45 return SOFTBUS_INVALID_PARAM;
46 }
47
48 g_abilityMgrHandle = dlopen(BR_PROXY_ADAPTER_PATH, RTLD_LAZY);
49 if (g_abilityMgrHandle == nullptr) {
50 TRANS_LOGE(TRANS_SVC, "[br_proxy] dlopen failed!");
51 return SOFTBUS_INVALID_PARAM;
52 }
53
54 g_startAbilityFunc = (StartAbilityFunc)dlsym(g_abilityMgrHandle, "StartAbility");
55 if (g_startAbilityFunc == nullptr) {
56 TRANS_LOGE(TRANS_SVC, "[br_proxy] dlsym failed!");
57 dlclose(g_abilityMgrHandle);
58 g_abilityMgrHandle = nullptr;
59 return SOFTBUS_INVALID_PARAM;
60 }
61
62 return g_startAbilityFunc(bundleName, abilityName);
63 }
64
GetAbilityName(char * abilityName,int32_t userId,uint32_t abilityNameLen,std::string bundleName)65 static int32_t GetAbilityName(char *abilityName, int32_t userId, uint32_t abilityNameLen, std::string bundleName)
66 {
67 if (abilityName == nullptr) {
68 TRANS_LOGE(TRANS_SVC, "[br_proxy] invalid param");
69 return SOFTBUS_INVALID_PARAM;
70 }
71
72 g_abilityMgrHandle = dlopen(BR_PROXY_ADAPTER_PATH, RTLD_LAZY);
73 if (g_abilityMgrHandle == nullptr) {
74 TRANS_LOGE(TRANS_SVC, "[br_proxy] dlopen failed!");
75 return SOFTBUS_INVALID_PARAM;
76 }
77
78 g_getAbilityName = (GetAbilityNameFunc)dlsym(g_abilityMgrHandle, "ProxyChannelMgrGetAbilityName");
79 if (g_getAbilityName == nullptr) {
80 TRANS_LOGE(TRANS_SVC, "[br_proxy] dlsym failed!");
81 dlclose(g_abilityMgrHandle);
82 g_abilityMgrHandle = nullptr;
83 return SOFTBUS_INVALID_PARAM;
84 }
85
86 int32_t ret = g_getAbilityName(abilityName, userId, abilityNameLen, bundleName);
87 if (ret != SOFTBUS_OK) {
88 TRANS_LOGE(TRANS_SVC, "[br_proxy] failed, ret = %{public}d", ret);
89 return ret;
90 }
91 if (g_abilityMgrHandle != nullptr) {
92 dlclose(g_abilityMgrHandle);
93 g_abilityMgrHandle = nullptr;
94 }
95 return SOFTBUS_OK;
96 }
97
BrProxyDynamicLoaderDeInit()98 void BrProxyDynamicLoaderDeInit()
99 {
100 if (g_abilityMgrHandle != nullptr) {
101 dlclose(g_abilityMgrHandle);
102 g_abilityMgrHandle = nullptr;
103 }
104 }
105
PullUpHap(const char * bundleName,const char * abilityName)106 extern "C" int32_t PullUpHap(const char *bundleName, const char *abilityName)
107 {
108 int32_t ret = AbilityManagerClientDynamicLoader(bundleName, abilityName);
109 if (ret != SOFTBUS_OK) {
110 TRANS_LOGE(TRANS_SVC, "[br_proxy] failed, ret = %{public}d", ret);
111 return ret;
112 }
113 BrProxyDynamicLoaderDeInit();
114 return SOFTBUS_OK;
115 }
116
GetCallerPid()117 extern "C" pid_t GetCallerPid()
118 {
119 return IPCSkeleton::GetCallingPid();
120 }
121
GetCallerUid()122 extern "C" pid_t GetCallerUid()
123 {
124 return IPCSkeleton::GetCallingUid();
125 }
126
GetCallerTokenId()127 extern "C" uint32_t GetCallerTokenId()
128 {
129 return IPCSkeleton::GetCallingTokenID();
130 }
131
GetCallerHapInfo(char * bundleName,uint32_t bundleNamelen,char * abilityName,uint32_t abilityNameLen)132 extern "C" int32_t GetCallerHapInfo(char *bundleName, uint32_t bundleNamelen,
133 char *abilityName, uint32_t abilityNameLen)
134 {
135 auto callerToken = IPCSkeleton::GetCallingTokenID();
136 auto type = Security::AccessToken::AccessTokenKit::GetTokenType(callerToken);
137 if (type != Security::AccessToken::ATokenTypeEnum::TOKEN_HAP) {
138 return SOFTBUS_TRANS_TOKEN_HAP_ERR;
139 }
140 Security::AccessToken::HapTokenInfo hapTokenInfoRes;
141 Security::AccessToken::AccessTokenKit::GetHapTokenInfo(callerToken, hapTokenInfoRes);
142
143 if (strcpy_s(bundleName, bundleNamelen, hapTokenInfoRes.bundleName.c_str()) != EOK) {
144 TRANS_LOGE(TRANS_SVC, "[br_proxy] copy bundleName or abilityName failed");
145 return SOFTBUS_STRCPY_ERR;
146 }
147 int32_t userId = GetActiveOsAccountIds();
148 int32_t ret = GetAbilityName(abilityName, userId, abilityNameLen, hapTokenInfoRes.bundleName);
149 if (ret != SOFTBUS_OK) {
150 TRANS_LOGE(TRANS_SVC, "[br_proxy] get abilityName failed, ret=%{public}d", ret);
151 return ret;
152 }
153
154 return SOFTBUS_OK;
155 }
156
CheckPushPermission()157 extern "C" int32_t CheckPushPermission()
158 {
159 auto callerToken = IPCSkeleton::GetCallingTokenID();
160 auto type = Security::AccessToken::AccessTokenKit::GetTokenType(callerToken);
161 if (type != Security::AccessToken::ATokenTypeEnum::TOKEN_NATIVE) {
162 TRANS_LOGE(TRANS_SVC, "[br_proxy] push must be native sa");
163 return SOFTBUS_TRANS_TOKEN_HAP_ERR;
164 }
165 TRANS_LOGI(TRANS_SVC, "[br_proxy] The push identity passes the authentication.");
166 return SOFTBUS_OK;
167 }