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