1 /*
2 * Copyright (c) 2022 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 "dms_token_callback.h"
17
18 #include "accesstoken_kit.h"
19 #include "bundle/bundle_manager_internal.h"
20 #include "distributed_sched_permission.h"
21 #include "distributed_sched_service.h"
22 #include "distributed_sched_utils.h"
23 #include "dtbschedmgr_device_info_storage.h"
24 #include "dtbschedmgr_log.h"
25 #include "ipc_skeleton.h"
26 #include "iservice_registry.h"
27 #include "multi_user_manager.h"
28 #include "parcel_helper.h"
29 #include "system_ability.h"
30 #include "system_ability_definition.h"
31
32 using namespace OHOS::Security;
33
34 namespace OHOS {
35 namespace DistributedSchedule {
36 const std::string TAG = "DmsTokenCallback";
37 const std::string FOUNDATION_PROCESS_NAME = "foundation";
38 const std::string DMS_SRC_NETWORK_ID = "dmsSrcNetworkId";
39
SendResult(OHOS::AAFwk::Want & want,int32_t callerUid,int32_t requestCode,uint32_t accessToken,int32_t resultCode)40 int32_t DmsTokenCallback::SendResult(OHOS::AAFwk::Want& want, int32_t callerUid,
41 int32_t requestCode, uint32_t accessToken, int32_t resultCode)
42 {
43 if (!MultiUserManager::GetInstance().IsCallerForeground(callerUid)) {
44 HILOGW("The current user is not foreground. callingUid: %{public}d .", callerUid);
45 return DMS_NOT_FOREGROUND_USER;
46 }
47 AccessToken::NativeTokenInfo nativeTokenInfo;
48 int32_t ret = AccessToken::AccessTokenKit::GetNativeTokenInfo(IPCSkeleton::GetCallingTokenID(),
49 nativeTokenInfo);
50 if (ret != ERR_OK || nativeTokenInfo.processName != FOUNDATION_PROCESS_NAME) {
51 HILOGE("check foundation call failed");
52 return INVALID_PARAMETERS_ERR;
53 }
54 std::string localDeviceId;
55 std::string deviceId = want.GetStringParam(DMS_SRC_NETWORK_ID);
56 if (!GetLocalDeviceId(localDeviceId) || !CheckDeviceId(localDeviceId, deviceId)) {
57 HILOGE("check deviceId failed");
58 return INVALID_PARAMETERS_ERR;
59 }
60 sptr<IDistributedSched> remoteDms = GetRemoteDms(deviceId);
61 if (remoteDms == nullptr) {
62 HILOGE("get remoteDms failed");
63 return INVALID_PARAMETERS_ERR;
64 }
65 nlohmann::json extraInfoJson;
66 CallerInfo callerInfo = {.uid = callerUid, .sourceDeviceId = localDeviceId, .accessToken = accessToken,
67 .extraInfoJson = extraInfoJson};
68 if (!BundleManagerInternal::GetCallerAppIdFromBms(callerInfo.uid, callerInfo.callerAppId)) {
69 HILOGE("GetCallerAppIdFromBms failed");
70 return INVALID_PARAMETERS_ERR;
71 }
72 if (!BundleManagerInternal::GetBundleNameListFromBms(callerInfo.uid, callerInfo.bundleNames)) {
73 HILOGE("GetBundleNameListFromBms failed");
74 return INVALID_PARAMETERS_ERR;
75 }
76 AccountInfo accountInfo;
77 ret = DistributedSchedPermission::GetInstance().GetAccountInfo(deviceId, callerInfo, accountInfo);
78 if (ret != ERR_OK) {
79 HILOGE("GetAccountInfo failed");
80 return ret;
81 }
82 AAFwk::Want newWant = want;
83 newWant.SetParam("callerbundleNames", callerInfo.bundleNames);
84 HILOGI("[PerformanceTest] SendResult transact begin");
85 int32_t result = remoteDms->SendResultFromRemote(newWant, requestCode, callerInfo, accountInfo, resultCode);
86 HILOGI("[PerformanceTest] SendResult transact end");
87 return result;
88 }
89
GetLocalDeviceId(std::string & localDeviceId)90 bool DmsTokenCallback::GetLocalDeviceId(std::string& localDeviceId)
91 {
92 if (!DtbschedmgrDeviceInfoStorage::GetInstance().GetLocalDeviceId(localDeviceId)) {
93 HILOGE("GetLocalDeviceId failed");
94 return false;
95 }
96 return true;
97 }
98
CheckDeviceId(const std::string & localDeviceId,const std::string & remoteDeviceId)99 bool DmsTokenCallback::CheckDeviceId(const std::string& localDeviceId, const std::string& remoteDeviceId)
100 {
101 // remoteDeviceId must not same with localDeviceId
102 if (localDeviceId.empty() || remoteDeviceId.empty() || localDeviceId == remoteDeviceId) {
103 HILOGE("check deviceId failed");
104 return false;
105 }
106 return true;
107 }
108
GetRemoteDms(const std::string & remoteDeviceId)109 sptr<IDistributedSched> DmsTokenCallback::GetRemoteDms(const std::string& remoteDeviceId)
110 {
111 if (remoteDeviceId.empty()) {
112 HILOGE("GetRemoteDms remoteDeviceId is empty");
113 return nullptr;
114 }
115 HILOGD("GetRemoteDms connect deviceid is %s", GetAnonymStr(remoteDeviceId).c_str());
116 auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
117 if (samgr == nullptr) {
118 HILOGE("GetRemoteDms failed to connect to systemAbilityMgr!");
119 return nullptr;
120 }
121 HILOGD("[PerformanceTest] GetRemoteDms begin");
122 auto object = samgr->CheckSystemAbility(DISTRIBUTED_SCHED_SA_ID, remoteDeviceId);
123 HILOGD("[PerformanceTest] GetRemoteDms end");
124 if (object == nullptr) {
125 HILOGE("GetRemoteDms failed to get remote DistributedSched %{private}s", GetAnonymStr(remoteDeviceId).c_str());
126 return nullptr;
127 }
128 return iface_cast<IDistributedSched>(object);
129 }
130 } // namespace Distributedschedule
131 } // namespace OHOS