• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-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 "deviceprofile_connector.h"
17 #include "crypto_mgr.h"
18 #include "dm_anonymous.h"
19 #include "dm_constants.h"
20 #include "dm_crypto.h"
21 #include "dm_log.h"
22 #include "multiple_user_connector.h"
23 #include "distributed_device_profile_client.h"
24 #include "system_ability_definition.h"
25 
26 using namespace OHOS::DistributedDeviceProfile;
27 
28 const uint32_t INVALIED_TYPE = 0;
29 const uint32_t APP_PEER_TO_PEER_TYPE = 1;
30 const uint32_t APP_ACROSS_ACCOUNT_TYPE = 2;
31 const uint32_t DEVICE_PEER_TO_PEER_TYPE = 3;
32 const uint32_t DEVICE_ACROSS_ACCOUNT_TYPE = 4;
33 const uint32_t IDENTICAL_ACCOUNT_TYPE = 5;
34 
35 const uint32_t DM_IDENTICAL_ACCOUNT = 1;
36 const uint32_t DM_POINT_TO_POINT = 256;
37 const uint32_t DM_ACROSS_ACCOUNT = 1282;
38 const uint32_t DM_INVALIED_BINDTYPE = 2048;
39 const uint32_t DEVICE = 1;
40 const uint32_t SERVICE = 2;
41 const uint32_t APP = 3;
42 constexpr uint32_t MAX_SESSION_KEY_LENGTH = 512;
43 
44 namespace OHOS {
45 namespace DistributedHardware {
46 DM_IMPLEMENT_SINGLE_INSTANCE(DeviceProfileConnector);
GetAccessControlProfile()47 std::vector<AccessControlProfile> DeviceProfileConnector::GetAccessControlProfile()
48 {
49     std::vector<AccessControlProfile> profiles;
50     std::map<std::string, std::string> queryParams;
51     int32_t userId = MultipleUserConnector::GetCurrentAccountUserID();
52     queryParams[USERID] = std::to_string(userId);
53     if (DistributedDeviceProfileClient::GetInstance().GetAccessControlProfile(queryParams, profiles) != DM_OK) {
54         LOGE("DP GetAccessControlProfile failed.");
55     }
56     return profiles;
57 }
58 
GetAccessControlProfileByUserId(int32_t userId)59 std::vector<AccessControlProfile> DeviceProfileConnector::GetAccessControlProfileByUserId(int32_t userId)
60 {
61     std::vector<AccessControlProfile> profiles;
62     std::map<std::string, std::string> queryParams;
63     queryParams[USERID] = std::to_string(userId);
64     if (DistributedDeviceProfileClient::GetInstance().GetAccessControlProfile(queryParams, profiles) != DM_OK) {
65         LOGE("DP GetAccessControlProfileByUserId failed.");
66     }
67     return profiles;
68 }
69 
GetAclProfileByDeviceIdAndUserId(const std::string & deviceId,int32_t userId)70 std::vector<AccessControlProfile> DeviceProfileConnector::GetAclProfileByDeviceIdAndUserId(const std::string &deviceId,
71     int32_t userId)
72 {
73     std::vector<AccessControlProfile> profiles;
74     std::vector<AccessControlProfile> aclProfileVec;
75     std::map<std::string, std::string> queryParams;
76     queryParams[USERID] = std::to_string(userId);
77     if (DistributedDeviceProfileClient::GetInstance().GetAccessControlProfile(queryParams, profiles) != DM_OK) {
78         LOGE("DP GetAccessControlProfile failed.");
79         return aclProfileVec;
80     }
81     for (auto &item : profiles) {
82         if ((item.GetAccesser().GetAccesserDeviceId() == deviceId &&
83              item.GetAccesser().GetAccesserUserId() == userId) ||
84             (item.GetAccessee().GetAccesseeDeviceId() == deviceId &&
85              item.GetAccessee().GetAccesseeUserId() == userId)) {
86             aclProfileVec.push_back(item);
87         }
88     }
89     return aclProfileVec;
90 }
91 
GetAppTrustDeviceList(const std::string & pkgName,const std::string & deviceId)92 std::unordered_map<std::string, DmAuthForm> DeviceProfileConnector::GetAppTrustDeviceList(const std::string &pkgName,
93     const std::string &deviceId)
94 {
95     int32_t userId = MultipleUserConnector::GetFirstForegroundUserId();
96     std::vector<AccessControlProfile> profiles = GetAclProfileByDeviceIdAndUserId(deviceId, userId);
97     std::vector<AccessControlProfile> profilesFilter = {};
98     for (auto &item : profiles) {
99         if ((item.GetAccesser().GetAccesserUserId() == userId &&
100              item.GetAccesser().GetAccesserDeviceId() == deviceId) ||
101             (item.GetAccessee().GetAccesseeUserId() == userId &&
102              item.GetAccessee().GetAccesseeDeviceId() == deviceId)) {
103             profilesFilter.push_back(item);
104         }
105     }
106     std::unordered_map<std::string, DmAuthForm> deviceIdMap;
107     for (auto &item : profilesFilter) {
108         std::string trustDeviceId = item.GetTrustDeviceId();
109         if (trustDeviceId == deviceId || item.GetStatus() != ACTIVE) {
110             continue;
111         }
112         DmDiscoveryInfo discoveryInfo = {pkgName, deviceId};
113         int32_t bindType = HandleDmAuthForm(item, discoveryInfo);
114         LOGI("The udid %{public}s in ACL authForm is %{public}d.", GetAnonyString(trustDeviceId).c_str(), bindType);
115         if (bindType == DmAuthForm::INVALID_TYPE) {
116             continue;
117         }
118         if (deviceIdMap.find(trustDeviceId) == deviceIdMap.end()) {
119             deviceIdMap[trustDeviceId] = static_cast<DmAuthForm>(bindType);
120             continue;
121         }
122         DmAuthForm authForm = deviceIdMap.at(trustDeviceId);
123         if (bindType == authForm) {
124             continue;
125         }
126         if (bindType == DmAuthForm::IDENTICAL_ACCOUNT) {
127             deviceIdMap[trustDeviceId] = DmAuthForm::IDENTICAL_ACCOUNT;
128             continue;
129         }
130         if (bindType == DmAuthForm::PEER_TO_PEER && authForm == DmAuthForm::ACROSS_ACCOUNT) {
131             deviceIdMap[trustDeviceId] = DmAuthForm::PEER_TO_PEER;
132             continue;
133         }
134     }
135     return deviceIdMap;
136 }
137 
GetDeviceAclParam(DmDiscoveryInfo discoveryInfo,bool & isOnline,int32_t & authForm)138 int32_t DeviceProfileConnector::GetDeviceAclParam(DmDiscoveryInfo discoveryInfo, bool &isOnline, int32_t &authForm)
139 {
140     std::vector<AccessControlProfile> profiles = GetAccessControlProfileByUserId(discoveryInfo.userId);
141     std::vector<int32_t> bindTypes;
142     for (auto &item : profiles) {
143         char deviceIdHash[DM_MAX_DEVICE_ID_LEN] = {0};
144         if (Crypto::GetUdidHash(item.GetTrustDeviceId(), reinterpret_cast<uint8_t *>(deviceIdHash)) != DM_OK) {
145             LOGE("get deviceIdHash by deviceId: %{public}s failed.", GetAnonyString(deviceIdHash).c_str());
146             continue;
147         }
148         if (static_cast<std::string>(deviceIdHash) != discoveryInfo.remoteDeviceIdHash ||
149             (discoveryInfo.localDeviceId == item.GetAccesser().GetAccesserDeviceId() &&
150             discoveryInfo.userId != item.GetAccesser().GetAccesserUserId()) ||
151             (discoveryInfo.localDeviceId == item.GetAccessee().GetAccesseeDeviceId() &&
152             discoveryInfo.userId != item.GetAccessee().GetAccesseeUserId())) {
153             continue;
154         }
155         int32_t bindType = HandleDmAuthForm(item, discoveryInfo);
156         if (bindType == DmAuthForm::INVALID_TYPE) {
157             continue;
158         }
159         bindTypes.push_back(bindType);
160     }
161     if (std::count(bindTypes.begin(), bindTypes.end(), DmAuthForm::IDENTICAL_ACCOUNT) > 0) {
162         isOnline = true;
163         authForm = DmAuthForm::IDENTICAL_ACCOUNT;
164         LOGI("The found device is identical account device bind type.");
165         return DM_OK;
166     }
167     if (std::count(bindTypes.begin(), bindTypes.end(), DmAuthForm::PEER_TO_PEER) > 0) {
168         isOnline = true;
169         authForm = DmAuthForm::PEER_TO_PEER;
170         LOGI("The found device is peer-to-peer device bind-level.");
171         return DM_OK;
172     }
173     if (std::count(bindTypes.begin(), bindTypes.end(), DmAuthForm::ACROSS_ACCOUNT) > 0) {
174         isOnline = true;
175         authForm = DmAuthForm::ACROSS_ACCOUNT;
176         LOGI("The found device is across-account device bind-level.");
177         return DM_OK;
178     }
179     authForm = DmAuthForm::INVALID_TYPE;
180     return DM_OK;
181 }
182 
CheckAuthForm(DmAuthForm form,AccessControlProfile profiles,DmDiscoveryInfo discoveryInfo)183 int32_t DeviceProfileConnector::CheckAuthForm(DmAuthForm form, AccessControlProfile profiles,
184     DmDiscoveryInfo discoveryInfo)
185 {
186     if (profiles.GetBindLevel() == DEVICE || (profiles.GetBindLevel() == APP && discoveryInfo.pkgname == "")) {
187         return form;
188     }
189     if (profiles.GetBindLevel() == APP) {
190         if (discoveryInfo.pkgname == profiles.GetAccesser().GetAccesserBundleName() &&
191             discoveryInfo.localDeviceId == profiles.GetAccesser().GetAccesserDeviceId()) {
192             return form;
193         }
194         if (discoveryInfo.pkgname == profiles.GetAccessee().GetAccesseeBundleName() &&
195             discoveryInfo.localDeviceId == profiles.GetAccessee().GetAccesseeDeviceId()) {
196             return form;
197         }
198     }
199     return DmAuthForm::INVALID_TYPE;
200 }
201 
HandleDmAuthForm(AccessControlProfile profiles,DmDiscoveryInfo discoveryInfo)202 int32_t DeviceProfileConnector::HandleDmAuthForm(AccessControlProfile profiles, DmDiscoveryInfo discoveryInfo)
203 {
204     if (profiles.GetBindType() == DM_IDENTICAL_ACCOUNT) {
205         return DmAuthForm::IDENTICAL_ACCOUNT;
206     }
207     if (profiles.GetBindType() == DM_POINT_TO_POINT) {
208         return CheckAuthForm(DmAuthForm::PEER_TO_PEER, profiles, discoveryInfo);
209     }
210     if (profiles.GetBindType() == DM_ACROSS_ACCOUNT) {
211         return CheckAuthForm(DmAuthForm::ACROSS_ACCOUNT, profiles, discoveryInfo);
212     }
213     return DmAuthForm::INVALID_TYPE;
214 }
215 
CheckBindType(std::string peerUdid,std::string localUdid)216 uint32_t DeviceProfileConnector::CheckBindType(std::string peerUdid, std::string localUdid)
217 {
218     std::vector<AccessControlProfile> filterProfiles = GetAclProfileByUserId(localUdid,
219         MultipleUserConnector::GetFirstForegroundUserId(), peerUdid);
220     LOGI("filterProfiles size is %{public}zu", filterProfiles.size());
221     uint32_t highestPriority = INVALIED_TYPE;
222     for (auto &item : filterProfiles) {
223         if (peerUdid != item.GetTrustDeviceId()) {
224             continue;
225         }
226         uint32_t priority = static_cast<uint32_t>(GetAuthForm(item, peerUdid, localUdid));
227         if (priority > highestPriority) {
228             highestPriority = priority;
229         }
230     }
231     return highestPriority;
232 }
233 
GetAuthForm(DistributedDeviceProfile::AccessControlProfile profiles,const std::string & trustDev,const std::string & reqDev)234 int32_t DeviceProfileConnector::GetAuthForm(DistributedDeviceProfile::AccessControlProfile profiles,
235     const std::string &trustDev, const std::string &reqDev)
236 {
237     LOGI("BindType %{public}d, bindLevel %{public}d",
238         profiles.GetBindType(), profiles.GetBindLevel());
239     uint32_t priority = INVALIED_TYPE;
240     uint32_t bindType = profiles.GetBindType();
241     switch (bindType) {
242         case DM_IDENTICAL_ACCOUNT:
243             priority = IDENTICAL_ACCOUNT_TYPE;
244             break;
245         case DM_POINT_TO_POINT:
246             if (profiles.GetBindLevel() == DEVICE) {
247                 priority = DEVICE_PEER_TO_PEER_TYPE;
248             } else if (profiles.GetBindLevel() == APP) {
249                 priority = APP_PEER_TO_PEER_TYPE;
250             }
251             break;
252         case DM_ACROSS_ACCOUNT:
253             if (profiles.GetBindLevel() == DEVICE) {
254                 priority = DEVICE_ACROSS_ACCOUNT_TYPE;
255             } else if (profiles.GetBindLevel() == APP) {
256                 priority = APP_ACROSS_ACCOUNT_TYPE;
257             }
258             break;
259         default:
260             LOGE("unknown bind type %{public}d.", bindType);
261             break;
262     }
263     return priority;
264 }
265 
GetBindTypeByPkgName(std::string pkgName,std::string requestDeviceId,std::string trustUdid)266 std::vector<int32_t> DeviceProfileConnector::GetBindTypeByPkgName(std::string pkgName, std::string requestDeviceId,
267     std::string trustUdid)
268 {
269     LOGI("Start requestDeviceId %{public}s, trustUdid %{public}s.", GetAnonyString(requestDeviceId).c_str(),
270         GetAnonyString(trustUdid).c_str());
271     std::vector<int32_t> bindTypeVec;
272     if (requestDeviceId.empty() || trustUdid.empty() || requestDeviceId == trustUdid) {
273         LOGE("Input udid param invalied.");
274         return bindTypeVec;
275     }
276     std::vector<AccessControlProfile> profiles =
277         GetAccessControlProfileByUserId(MultipleUserConnector::GetFirstForegroundUserId());
278     LOGI("AccessControlProfile size is %{public}zu", profiles.size());
279     for (auto &item : profiles) {
280         if (trustUdid != item.GetTrustDeviceId() || item.GetStatus() != ACTIVE) {
281             continue;
282         }
283         GetParamBindTypeVec(item, requestDeviceId, bindTypeVec, trustUdid);
284     }
285     return bindTypeVec;
286 }
287 
GetTokenIdByNameAndDeviceId(std::string pkgName,std::string requestDeviceId)288 uint64_t DeviceProfileConnector::GetTokenIdByNameAndDeviceId(std::string pkgName, std::string requestDeviceId)
289 {
290     std::vector<AccessControlProfile> profiles = GetAccessControlProfile();
291     uint64_t peerTokenId = 0;
292     for (auto &item : profiles) {
293         if (item.GetAccesser().GetAccesserBundleName() == pkgName &&
294             item.GetAccesser().GetAccesserDeviceId() == requestDeviceId) {
295             peerTokenId = static_cast<uint64_t>(item.GetAccesser().GetAccesserTokenId());
296             break;
297         }
298         if (item.GetAccessee().GetAccesseeBundleName() == pkgName &&
299             item.GetAccessee().GetAccesseeDeviceId() == requestDeviceId) {
300             peerTokenId = static_cast<uint64_t>(item.GetAccessee().GetAccesseeTokenId());
301             break;
302         }
303     }
304     return peerTokenId;
305 }
306 
GetParamBindTypeVec(AccessControlProfile profile,std::string requestDeviceId,std::vector<int32_t> & bindTypeVec,std::string trustUdid)307 void DeviceProfileConnector::GetParamBindTypeVec(AccessControlProfile profile, std::string requestDeviceId,
308     std::vector<int32_t> &bindTypeVec, std::string trustUdid)
309 {
310     if (!(profile.GetAccesser().GetAccesserDeviceId() == trustUdid &&
311         profile.GetAccessee().GetAccesseeDeviceId() == requestDeviceId) &&
312         !(profile.GetAccessee().GetAccesseeDeviceId() == trustUdid &&
313         profile.GetAccesser().GetAccesserDeviceId() == requestDeviceId)) {
314         LOGE("input udid param invalied.");
315         return;
316     }
317     uint32_t bindType = profile.GetBindType();
318     switch (bindType) {
319         case DM_IDENTICAL_ACCOUNT:
320             bindTypeVec.push_back(IDENTICAL_ACCOUNT_TYPE);
321             break;
322         case DM_POINT_TO_POINT:
323             if (profile.GetBindLevel() == DEVICE) {
324                 bindTypeVec.push_back(DEVICE_PEER_TO_PEER_TYPE);
325             }
326             if (profile.GetBindLevel() == APP) {
327                 bindTypeVec.push_back(APP_PEER_TO_PEER_TYPE);
328             }
329             break;
330         case DM_ACROSS_ACCOUNT:
331             if (profile.GetBindLevel() == DEVICE) {
332                 bindTypeVec.push_back(DEVICE_ACROSS_ACCOUNT_TYPE);
333             }
334             if (profile.GetBindLevel() == APP) {
335                 bindTypeVec.push_back(APP_ACROSS_ACCOUNT_TYPE);
336             }
337             break;
338         default:
339             LOGE("unknown bind type %{public}d.", bindType);
340             break;
341     }
342 }
343 
CompareBindType(std::vector<AccessControlProfile> profiles,std::string pkgName,std::vector<int32_t> & sinkBindType,std::string localDeviceId,std::string targetDeviceId)344 std::vector<int32_t> DeviceProfileConnector::CompareBindType(std::vector<AccessControlProfile> profiles,
345     std::string pkgName, std::vector<int32_t> &sinkBindType, std::string localDeviceId, std::string targetDeviceId)
346 {
347     std::vector<int32_t> bindTypeIndex;
348     for (uint32_t index = 0; index < profiles.size(); index++) {
349         if (profiles[index].GetTrustDeviceId() != targetDeviceId || profiles[index].GetStatus() != ACTIVE) {
350             continue;
351         }
352         DmDiscoveryInfo paramInfo = {
353             .pkgname = pkgName,
354             .localDeviceId = localDeviceId,
355         };
356         ProcessBindType(profiles[index], localDeviceId, sinkBindType, bindTypeIndex, index, targetDeviceId);
357     }
358     return bindTypeIndex;
359 }
360 
ProcessBindType(AccessControlProfile profiles,std::string localDeviceId,std::vector<int32_t> & sinkBindType,std::vector<int32_t> & bindTypeIndex,uint32_t index,std::string targetDeviceId)361 void DeviceProfileConnector::ProcessBindType(AccessControlProfile profiles, std::string localDeviceId,
362     std::vector<int32_t> &sinkBindType, std::vector<int32_t> &bindTypeIndex, uint32_t index, std::string targetDeviceId)
363 {
364     if (profiles.GetBindType() == DM_IDENTICAL_ACCOUNT) {
365         sinkBindType.push_back(IDENTICAL_ACCOUNT_TYPE);
366         bindTypeIndex.push_back(index);
367     }
368     if (profiles.GetBindType() == DM_POINT_TO_POINT) {
369         if (profiles.GetBindLevel() == DEVICE) {
370             sinkBindType.push_back(DEVICE_PEER_TO_PEER_TYPE);
371             bindTypeIndex.push_back(index);
372         }
373         if (profiles.GetBindLevel() == APP) {
374             if (profiles.GetAccesser().GetAccesserDeviceId() == targetDeviceId &&
375                 profiles.GetAccessee().GetAccesseeDeviceId() == localDeviceId) {
376                 sinkBindType.push_back(APP_PEER_TO_PEER_TYPE);
377                 bindTypeIndex.push_back(index);
378             }
379             if (profiles.GetAccessee().GetAccesseeDeviceId() == targetDeviceId &&
380                 profiles.GetAccesser().GetAccesserDeviceId() == localDeviceId) {
381                 sinkBindType.push_back(APP_PEER_TO_PEER_TYPE);
382                 bindTypeIndex.push_back(index);
383             }
384         }
385     }
386     if (profiles.GetBindType() == DM_ACROSS_ACCOUNT) {
387         if (profiles.GetBindLevel() == DEVICE) {
388             sinkBindType.push_back(DEVICE_ACROSS_ACCOUNT_TYPE);
389             bindTypeIndex.push_back(index);
390         }
391         if (profiles.GetBindLevel() == APP) {
392             if (profiles.GetAccesser().GetAccesserDeviceId() == targetDeviceId &&
393                 profiles.GetAccessee().GetAccesseeDeviceId() == localDeviceId) {
394                 sinkBindType.push_back(APP_ACROSS_ACCOUNT_TYPE);
395                 bindTypeIndex.push_back(index);
396             }
397             if (profiles.GetAccessee().GetAccesseeDeviceId() == targetDeviceId &&
398                 profiles.GetAccesser().GetAccesserDeviceId() == localDeviceId) {
399                 sinkBindType.push_back(APP_ACROSS_ACCOUNT_TYPE);
400                 bindTypeIndex.push_back(index);
401             }
402         }
403     }
404 }
405 
SyncAclByBindType(std::string pkgName,std::vector<int32_t> bindTypeVec,std::string localDeviceId,std::string targetDeviceId)406 std::vector<int32_t> DeviceProfileConnector::SyncAclByBindType(std::string pkgName, std::vector<int32_t> bindTypeVec,
407     std::string localDeviceId, std::string targetDeviceId)
408 {
409     std::vector<AccessControlProfile> profiles =
410         GetAccessControlProfileByUserId(MultipleUserConnector::GetFirstForegroundUserId());
411     LOGI("AccessControlProfile size is %{public}zu", profiles.size());
412     std::vector<int32_t> sinkBindType;
413     std::vector<int32_t> bindType;
414     std::vector<int32_t> bindTypeIndex =
415         CompareBindType(profiles, pkgName, sinkBindType, localDeviceId, targetDeviceId);
416     LOGI("SyncAclByBindType sinkBindType size is %{public}zu", sinkBindType.size());
417     for (uint32_t sinkIndex = 0; sinkIndex < sinkBindType.size(); sinkIndex++) {
418         bool deleteAclFlag = true;
419         for (uint32_t srcIndex = 0; srcIndex < bindTypeVec.size(); srcIndex++) {
420             if (sinkBindType[sinkIndex] == bindTypeVec[srcIndex]) {
421                 deleteAclFlag = false;
422                 bindType.push_back(bindTypeVec[sinkIndex]);
423             }
424         }
425         if (deleteAclFlag) {
426             int32_t deleteIndex = profiles[bindTypeIndex[sinkIndex]].GetAccessControlId();
427             DistributedDeviceProfileClient::GetInstance().DeleteAccessControlProfile(deleteIndex);
428             LOGI("SyncAclByBindType deleteAcl index is %{public}d", deleteIndex);
429         }
430     }
431     return bindType;
432 }
433 
GetProcessInfoFromAclByUserId(const std::string & localDeviceId,const std::string & targetDeviceId,int32_t userId)434 std::vector<OHOS::DistributedHardware::ProcessInfo> DeviceProfileConnector::GetProcessInfoFromAclByUserId(
435     const std::string &localDeviceId, const std::string &targetDeviceId, int32_t userId)
436 {
437     std::vector<AccessControlProfile> filterProfiles = GetAclProfileByUserId(localDeviceId,
438         userId, targetDeviceId);
439     LOGI("filterProfiles size is %{public}zu", filterProfiles.size());
440     std::vector<OHOS::DistributedHardware::ProcessInfo> processInfoVec;
441     for (auto &item : filterProfiles) {
442         if (item.GetTrustDeviceId() != targetDeviceId) {
443             continue;
444         }
445         std::string accesserUdid = item.GetAccesser().GetAccesserDeviceId();
446         std::string accesseeUdid = item.GetAccessee().GetAccesseeDeviceId();
447         OHOS::DistributedHardware::ProcessInfo processInfo;
448         if (accesserUdid == localDeviceId) {
449             processInfo.pkgName = item.GetAccesser().GetAccesserBundleName();
450             processInfo.userId = item.GetAccesser().GetAccesserUserId();
451             processInfoVec.push_back(processInfo);
452             continue;
453         }
454         if (accesseeUdid == localDeviceId) {
455             processInfo.pkgName = item.GetAccessee().GetAccesseeBundleName();
456             processInfo.userId = item.GetAccessee().GetAccesseeUserId();
457             processInfoVec.push_back(processInfo);
458             continue;
459         }
460     }
461     return processInfoVec;
462 }
463 
PutAccessControlList(DmAclInfo aclInfo,DmAccesser dmAccesser,DmAccessee dmAccessee)464 int32_t DeviceProfileConnector::PutAccessControlList(DmAclInfo aclInfo, DmAccesser dmAccesser, DmAccessee dmAccessee)
465 {
466     LOGI("Start.");
467     Accesser accesser;
468     accesser.SetAccesserDeviceId(dmAccesser.requestDeviceId);
469     accesser.SetAccesserUserId(dmAccesser.requestUserId);
470     accesser.SetAccesserAccountId(dmAccesser.requestAccountId);
471     accesser.SetAccesserTokenId(dmAccesser.requestTokenId);
472     accesser.SetAccesserBundleName(dmAccesser.requestBundleName);
473     accesser.SetAccesserDeviceName(dmAccesser.requestDeviceName);
474     Accessee accessee;
475     accessee.SetAccesseeDeviceId(dmAccessee.trustDeviceId);
476     accessee.SetAccesseeUserId(dmAccessee.trustUserId);
477     accessee.SetAccesseeAccountId(dmAccessee.trustAccountId);
478     accessee.SetAccesseeTokenId(dmAccessee.trustTokenId);
479     accessee.SetAccesseeBundleName(dmAccessee.trustBundleName);
480     accessee.SetAccesseeDeviceName(dmAccessee.trustDeviceName);
481     AccessControlProfile profile;
482     profile.SetBindType(aclInfo.bindType);
483     profile.SetBindLevel(aclInfo.bindLevel);
484     profile.SetStatus(ACTIVE);
485     profile.SetTrustDeviceId(aclInfo.trustDeviceId);
486     profile.SetDeviceIdType((int32_t)DeviceIdType::UDID);
487     profile.SetDeviceIdHash(aclInfo.deviceIdHash);
488     profile.SetAuthenticationType(aclInfo.authenticationType);
489     profile.SetAccessee(accessee);
490     profile.SetAccesser(accesser);
491     int32_t ret = DistributedDeviceProfileClient::GetInstance().PutAccessControlProfile(profile);
492     if (ret != DM_OK) {
493         LOGE("PutAccessControlProfile failed.");
494     }
495     return ret;
496 }
497 
DeleteAclForAccountLogOut(const std::string & localUdid,int32_t localUserId,const std::string & peerUdid,int32_t peerUserId)498 bool DeviceProfileConnector::DeleteAclForAccountLogOut(const std::string &localUdid, int32_t localUserId,
499     const std::string &peerUdid, int32_t peerUserId)
500 {
501     LOGI("localUdid %{public}s, localUserId %{public}d, peerUdid %{public}s, peerUserId %{public}d.",
502         GetAnonyString(localUdid).c_str(), localUserId, GetAnonyString(peerUdid).c_str(), peerUserId);
503     std::vector<AccessControlProfile> profiles = GetAllAccessControlProfile();
504     std::vector<AccessControlProfile> deleteProfiles;
505     bool notifyOffline = false;
506     bool isDelete = false;
507     for (const auto &item : profiles) {
508         if (item.GetTrustDeviceId() != peerUdid) {
509             continue;
510         }
511         std::string accesserUdid = item.GetAccesser().GetAccesserDeviceId();
512         std::string accesseeUdid = item.GetAccessee().GetAccesseeDeviceId();
513         int32_t accesserUserId = item.GetAccesser().GetAccesserUserId();
514         int32_t accesseeUserId = item.GetAccessee().GetAccesseeUserId();
515         if (accesserUdid == localUdid && accesserUserId == localUserId &&
516             accesseeUdid == peerUdid && accesseeUserId == peerUserId) {
517             if (item.GetBindType() == DM_IDENTICAL_ACCOUNT) {
518                 isDelete = true;
519             }
520             deleteProfiles.push_back(item);
521             notifyOffline = (item.GetStatus() == ACTIVE);
522             continue;
523         }
524         if (accesserUdid == peerUdid && accesserUserId == peerUserId &&
525             accesseeUdid == localUdid && accesseeUserId == localUserId) {
526             if (item.GetBindType() == DM_IDENTICAL_ACCOUNT) {
527                 isDelete = true;
528             }
529             deleteProfiles.push_back(item);
530             notifyOffline = (item.GetStatus() == ACTIVE);
531             continue;
532         }
533     }
534     if (!isDelete) {
535         return false;
536     }
537     for (const auto &item : deleteProfiles) {
538         DistributedDeviceProfileClient::GetInstance().DeleteAccessControlProfile(item.GetAccessControlId());
539     }
540     return notifyOffline;
541 }
542 
DeleteAclForUserRemoved(std::string localUdid,int32_t userId)543 void DeviceProfileConnector::DeleteAclForUserRemoved(std::string localUdid, int32_t userId)
544 {
545     LOGI("localUdid %{public}s, userId %{public}d.", GetAnonyString(localUdid).c_str(), userId);
546     std::vector<AccessControlProfile> profiles = GetAccessControlProfileByUserId(userId);
547     for (const auto &item : profiles) {
548         std::string accesserUdid = item.GetAccesser().GetAccesserDeviceId();
549         std::string accesseeUdid = item.GetAccessee().GetAccesseeDeviceId();
550         int32_t accesserUserId = item.GetAccesser().GetAccesserUserId();
551         int32_t accesseeUserId = item.GetAccessee().GetAccesseeUserId();
552         if ((accesserUdid == localUdid && accesserUserId == userId) ||
553             (accesseeUdid == localUdid && accesseeUserId == userId)) {
554             DistributedDeviceProfileClient::GetInstance().DeleteAccessControlProfile(item.GetAccessControlId());
555         }
556     }
557 }
558 
DeleteAclForRemoteUserRemoved(std::string peerUdid,int32_t peerUserId,std::vector<int32_t> & userIds)559 void DeviceProfileConnector::DeleteAclForRemoteUserRemoved(std::string peerUdid, int32_t peerUserId,
560     std::vector<int32_t> &userIds)
561 {
562     LOGI("peerUdid %{public}s, peerUserId %{public}d.", GetAnonyString(peerUdid).c_str(), peerUserId);
563     std::vector<AccessControlProfile> profiles = GetAccessControlProfileByUserId(peerUserId);
564     for (const auto &item : profiles) {
565         std::string accesserUdid = item.GetAccesser().GetAccesserDeviceId();
566         std::string accesseeUdid = item.GetAccessee().GetAccesseeDeviceId();
567         int32_t accesserUserId = item.GetAccesser().GetAccesserUserId();
568         int32_t accesseeUserId = item.GetAccessee().GetAccesseeUserId();
569         if (accesserUdid == peerUdid && accesserUserId == peerUserId) {
570             DistributedDeviceProfileClient::GetInstance().DeleteAccessControlProfile(item.GetAccessControlId());
571             if (item.GetBindLevel() == DEVICE) {
572                 userIds.push_back(accesseeUserId);
573             }
574         }
575         if (accesseeUdid == peerUdid && accesseeUserId == peerUserId) {
576             DistributedDeviceProfileClient::GetInstance().DeleteAccessControlProfile(item.GetAccessControlId());
577             if (item.GetBindLevel() == DEVICE) {
578                 userIds.push_back(accesserUserId);
579             }
580         }
581     }
582 }
583 
DeleteAccessControlList(const std::string & udid)584 void DeviceProfileConnector::DeleteAccessControlList(const std::string &udid)
585 {
586     LOGI("Udid: %{public}s.", GetAnonyString(udid).c_str());
587     if (udid.empty()) {
588         LOGE("DeleteAccessControlList udid is empty.");
589         return;
590     }
591     std::vector<AccessControlProfile> profiles = GetAccessControlProfile();
592     LOGI("Size is %{public}zu", profiles.size());
593     for (const auto &item : profiles) {
594         if (item.GetTrustDeviceId() == udid) {
595             DistributedDeviceProfileClient::GetInstance().DeleteAccessControlProfile(item.GetAccessControlId());
596         }
597     }
598 }
599 
DeleteAccessControlList(const std::string & pkgName,const std::string & localDeviceId,const std::string & remoteDeviceId,int32_t bindLevel,const std::string & extra)600 DmOfflineParam DeviceProfileConnector::DeleteAccessControlList(const std::string &pkgName,
601     const std::string &localDeviceId, const std::string &remoteDeviceId,
602     int32_t bindLevel, const std::string &extra)
603 {
604     LOGI("pkgName %{public}s, localDeviceId %{public}s, remoteDeviceId %{public}s, bindLevel %{public}d.",
605         pkgName.c_str(), GetAnonyString(localDeviceId).c_str(), GetAnonyString(remoteDeviceId).c_str(), bindLevel);
606     DmOfflineParam offlineParam;
607     offlineParam.bindType = INVALIED_TYPE;
608     if (static_cast<uint32_t>(bindLevel) > APP || static_cast<uint32_t>(bindLevel) < DEVICE) {
609         LOGE("Invalied bindlevel.");
610         return offlineParam;
611     }
612     std::vector<AccessControlProfile> profiles = GetAclProfileByDeviceIdAndUserId(localDeviceId,
613         MultipleUserConnector::GetFirstForegroundUserId());
614     if (profiles.empty()) {
615         LOGE("Acl is empty.");
616         return offlineParam;
617     }
618     switch (bindLevel) {
619         case APP:
620             if (extra == "") {
621                 DeleteAppBindLevel(offlineParam, pkgName, profiles, localDeviceId, remoteDeviceId);
622             } else {
623                 DeleteAppBindLevel(offlineParam, pkgName, profiles, localDeviceId, remoteDeviceId, extra);
624             }
625             break;
626         case SERVICE:
627             DeleteServiceBindLevel(offlineParam, pkgName, profiles, localDeviceId, remoteDeviceId);
628             break;
629         case DEVICE:
630             DeleteDeviceBindLevel(offlineParam, profiles, localDeviceId, remoteDeviceId);
631             break;
632         default:
633             break;
634     }
635     return offlineParam;
636 }
637 
DeleteAppBindLevel(DmOfflineParam & offlineParam,const std::string & pkgName,const std::vector<AccessControlProfile> & profiles,const std::string & localUdid,const std::string & remoteUdid)638 void DeviceProfileConnector::DeleteAppBindLevel(DmOfflineParam &offlineParam, const std::string &pkgName,
639     const std::vector<AccessControlProfile> &profiles, const std::string &localUdid, const std::string &remoteUdid)
640 {
641     int32_t bindNums = 0;
642     int32_t deleteNums = 0;
643     for (auto &item : profiles) {
644         if (item.GetTrustDeviceId() != remoteUdid || item.GetBindType() == DM_IDENTICAL_ACCOUNT ||
645             item.GetBindLevel() != APP) {
646             continue;
647         }
648         bindNums++;
649         if (item.GetAccesser().GetAccesserBundleName() == pkgName &&
650             item.GetAccesser().GetAccesserDeviceId() == localUdid &&
651             item.GetAccessee().GetAccesseeDeviceId() == remoteUdid) {
652             DistributedDeviceProfileClient::GetInstance().DeleteAccessControlProfile(item.GetAccessControlId());
653             deleteNums++;
654             offlineParam.bindType = APP;
655             ProcessInfo processInfo;
656             processInfo.pkgName = item.GetAccesser().GetAccesserBundleName();
657             processInfo.userId = item.GetAccesser().GetAccesserUserId();
658             offlineParam.processVec.push_back(processInfo);
659             LOGI("Src delete acl pkgName %{public}s, bindType %{public}d, localUdid %{public}s, remoteUdid %{public}s",
660                 pkgName.c_str(), item.GetBindType(), GetAnonyString(localUdid).c_str(),
661                 GetAnonyString(remoteUdid).c_str());
662             continue;
663         }
664         if (item.GetAccessee().GetAccesseeBundleName() == pkgName &&
665             item.GetAccessee().GetAccesseeDeviceId() == localUdid &&
666             item.GetAccesser().GetAccesserDeviceId() == remoteUdid) {
667             DistributedDeviceProfileClient::GetInstance().DeleteAccessControlProfile(item.GetAccessControlId());
668             deleteNums++;
669             offlineParam.bindType = APP;
670             ProcessInfo processInfo;
671             processInfo.pkgName = item.GetAccessee().GetAccesseeBundleName();
672             processInfo.userId = item.GetAccessee().GetAccesseeUserId();
673             offlineParam.processVec.push_back(processInfo);
674             LOGI("Sink delete acl pkgName %{public}s, bindType %{public}d, localUdid %{public}s, remoteUdid %{public}s",
675                 pkgName.c_str(), item.GetBindType(), GetAnonyString(localUdid).c_str(),
676                 GetAnonyString(remoteUdid).c_str());
677             continue;
678         }
679     }
680     offlineParam.leftAclNumber = bindNums - deleteNums;
681 }
682 
DeleteAppBindLevel(DmOfflineParam & offlineParam,const std::string & pkgName,const std::vector<AccessControlProfile> & profiles,const std::string & localUdid,const std::string & remoteUdid,const std::string & extra)683 void DeviceProfileConnector::DeleteAppBindLevel(DmOfflineParam &offlineParam, const std::string &pkgName,
684     const std::vector<AccessControlProfile> &profiles, const std::string &localUdid,
685     const std::string &remoteUdid, const std::string &extra)
686 {
687     LOGI("DeviceProfileConnector::DeleteAppBindLevel extra %{public}s", extra.c_str());
688     int32_t bindNums = 0;
689     int32_t deleteNums = 0;
690     std::string peerBundleName = extra;
691     for (auto &item : profiles) {
692         if (item.GetTrustDeviceId() != remoteUdid || item.GetBindType() == DM_IDENTICAL_ACCOUNT ||
693             item.GetBindLevel() != APP) {
694             continue;
695         }
696         bindNums++;
697         if (item.GetAccesser().GetAccesserBundleName() == pkgName &&
698             item.GetAccessee().GetAccesseeBundleName() == peerBundleName &&
699             item.GetAccesser().GetAccesserDeviceId() == localUdid &&
700             item.GetAccessee().GetAccesseeDeviceId() == remoteUdid) {
701             DistributedDeviceProfileClient::GetInstance().DeleteAccessControlProfile(item.GetAccessControlId());
702             deleteNums++;
703             offlineParam.bindType = APP;
704             ProcessInfo processInfo;
705             processInfo.pkgName = item.GetAccesser().GetAccesserBundleName();
706             processInfo.userId = item.GetAccesser().GetAccesserUserId();
707             offlineParam.processVec.push_back(processInfo);
708             LOGI("Src delete acl pkgName %{public}s, bindType %{public}d, localUdid %{public}s, remoteUdid %{public}s",
709                 pkgName.c_str(), item.GetBindType(), GetAnonyString(localUdid).c_str(),
710                 GetAnonyString(remoteUdid).c_str());
711             continue;
712         }
713         if (item.GetAccessee().GetAccesseeBundleName() == pkgName &&
714             item.GetAccesser().GetAccesserBundleName() == peerBundleName &&
715             item.GetAccessee().GetAccesseeDeviceId() == localUdid &&
716             item.GetAccesser().GetAccesserDeviceId() == remoteUdid) {
717             DistributedDeviceProfileClient::GetInstance().DeleteAccessControlProfile(item.GetAccessControlId());
718             deleteNums++;
719             offlineParam.bindType = APP;
720             ProcessInfo processInfo;
721             processInfo.pkgName = item.GetAccessee().GetAccesseeBundleName();
722             processInfo.userId = item.GetAccessee().GetAccesseeUserId();
723             offlineParam.processVec.push_back(processInfo);
724             LOGI("Sink delete acl pkgName %{public}s, bindType %{public}d, localUdid %{public}s, remoteUdid %{public}s",
725                 pkgName.c_str(), item.GetBindType(), GetAnonyString(localUdid).c_str(),
726                 GetAnonyString(remoteUdid).c_str());
727             continue;
728         }
729     }
730     offlineParam.leftAclNumber = bindNums - deleteNums;
731 }
732 
DeleteDeviceBindLevel(DmOfflineParam & offlineParam,const std::vector<AccessControlProfile> & profiles,const std::string & localUdid,const std::string & remoteUdid)733 void DeviceProfileConnector::DeleteDeviceBindLevel(DmOfflineParam &offlineParam,
734     const std::vector<AccessControlProfile> &profiles, const std::string &localUdid, const std::string &remoteUdid)
735 {
736     int32_t bindNums = 0;
737     int32_t deleteNums = 0;
738     for (auto &item : profiles) {
739         if (item.GetTrustDeviceId() != remoteUdid || item.GetBindType() == DM_IDENTICAL_ACCOUNT) {
740             continue;
741         }
742         bindNums++;
743         if (item.GetAccesser().GetAccesserDeviceId() == localUdid &&
744             item.GetAccessee().GetAccesseeDeviceId() == remoteUdid) {
745             DistributedDeviceProfileClient::GetInstance().DeleteAccessControlProfile(item.GetAccessControlId());
746             deleteNums++;
747             offlineParam.bindType = DEVICE;
748             LOGI("Src delete acl bindType %{public}d, localUdid %{public}s, remoteUdid %{public}s", item.GetBindType(),
749                 GetAnonyString(localUdid).c_str(), GetAnonyString(remoteUdid).c_str());
750             continue;
751         }
752         if (item.GetAccessee().GetAccesseeDeviceId() == localUdid &&
753             item.GetAccesser().GetAccesserDeviceId() == remoteUdid) {
754             DistributedDeviceProfileClient::GetInstance().DeleteAccessControlProfile(item.GetAccessControlId());
755             deleteNums++;
756             offlineParam.bindType = DEVICE;
757             LOGI("Sink delete acl bindType %{public}d, localUdid %{public}s, remoteUdid %{public}s", item.GetBindType(),
758                 GetAnonyString(localUdid).c_str(), GetAnonyString(remoteUdid).c_str());
759             continue;
760         }
761     }
762     offlineParam.leftAclNumber = bindNums - deleteNums;
763 }
764 
DeleteServiceBindLevel(DmOfflineParam & offlineParam,const std::string & pkgName,const std::vector<AccessControlProfile> & profiles,const std::string & localUdid,const std::string & remoteUdid)765 void DeviceProfileConnector::DeleteServiceBindLevel(DmOfflineParam &offlineParam, const std::string &pkgName,
766     const std::vector<AccessControlProfile> &profiles, const std::string &localUdid, const std::string &remoteUdid)
767 {
768     int32_t bindNums = 0;
769     int32_t deleteNums = 0;
770     for (auto &item : profiles) {
771         if (item.GetTrustDeviceId() != remoteUdid || item.GetBindType() == DM_IDENTICAL_ACCOUNT ||
772             item.GetBindLevel() != SERVICE) {
773             continue;
774         }
775         bindNums++;
776         if (item.GetAccesser().GetAccesserBundleName() == pkgName &&
777             item.GetAccesser().GetAccesserDeviceId() == localUdid &&
778             item.GetAccessee().GetAccesseeDeviceId() == remoteUdid) {
779             DistributedDeviceProfileClient::GetInstance().DeleteAccessControlProfile(item.GetAccessControlId());
780             deleteNums++;
781             offlineParam.bindType = SERVICE;
782             LOGI("Src delete acl pkgName %{public}s, bindType %{public}d, localUdid %{public}s, remoteUdid %{public}s",
783                 pkgName.c_str(), item.GetBindType(), GetAnonyString(localUdid).c_str(),
784                 GetAnonyString(remoteUdid).c_str());
785             continue;
786         }
787         if (item.GetAccessee().GetAccesseeBundleName() == pkgName &&
788             item.GetAccessee().GetAccesseeDeviceId() == localUdid &&
789             item.GetAccesser().GetAccesserDeviceId() == remoteUdid) {
790             DistributedDeviceProfileClient::GetInstance().DeleteAccessControlProfile(item.GetAccessControlId());
791             deleteNums++;
792             offlineParam.bindType = SERVICE;
793             LOGI("Sink delete acl pkgName %{public}s, bindType %{public}d, localUdid %{public}s, remoteUdid %{public}s",
794                 pkgName.c_str(), item.GetBindType(), GetAnonyString(localUdid).c_str(),
795                 GetAnonyString(remoteUdid).c_str());
796             continue;
797         }
798     }
799     offlineParam.leftAclNumber = bindNums - deleteNums;
800 }
801 
UpdateAccessControlList(int32_t userId,std::string & oldAccountId,std::string & newAccountId)802 int32_t DeviceProfileConnector::UpdateAccessControlList(int32_t userId, std::string &oldAccountId,
803     std::string &newAccountId)
804 {
805     std::vector<AccessControlProfile> profiles = GetAccessControlProfile();
806     LOGI("AccessControlProfile size is %{public}zu", profiles.size());
807     for (auto &item : profiles) {
808         if ((item.GetAccesser().GetAccesserUserId() == userId &&
809             item.GetAccesser().GetAccesserAccountId() == oldAccountId) ||
810             (item.GetAccessee().GetAccesseeUserId() == userId &&
811             item.GetAccessee().GetAccesseeAccountId() == oldAccountId)) {
812             item.SetStatus(INACTIVE);
813             DistributedDeviceProfileClient::GetInstance().UpdateAccessControlProfile(item);
814         }
815         if ((item.GetAccesser().GetAccesserUserId() == userId &&
816             item.GetAccesser().GetAccesserAccountId() == newAccountId) ||
817             (item.GetAccessee().GetAccesseeUserId() == userId &&
818             item.GetAccessee().GetAccesseeAccountId() == newAccountId)) {
819             item.SetStatus(ACTIVE);
820             DistributedDeviceProfileClient::GetInstance().UpdateAccessControlProfile(item);
821         }
822     }
823     return DM_OK;
824 }
825 
CheckSrcDevIdInAclForDevBind(const std::string & pkgName,const std::string & deviceId)826 bool DeviceProfileConnector::CheckSrcDevIdInAclForDevBind(const std::string &pkgName, const std::string &deviceId)
827 {
828     std::vector<AccessControlProfile> profiles = GetAccessControlProfile();
829     LOGI("AccessControlProfile size is %{public}zu", profiles.size());
830     for (auto &item : profiles) {
831         if (item.GetTrustDeviceId() == deviceId && item.GetStatus() == ACTIVE &&
832             item.GetBindLevel() == DEVICE && (item.GetAccessee().GetAccesseeBundleName() == pkgName ||
833             item.GetAccesser().GetAccesserBundleName() == "") && item.GetAccessee().GetAccesseeUserId() == 0 &&
834             item.GetAccessee().GetAccesseeAccountId() == "") {
835             return true;
836         }
837     }
838     return false;
839 }
840 
CheckSinkDevIdInAclForDevBind(const std::string & pkgName,const std::string & deviceId)841 bool DeviceProfileConnector::CheckSinkDevIdInAclForDevBind(const std::string &pkgName, const std::string &deviceId)
842 {
843     std::vector<AccessControlProfile> profiles = GetAccessControlProfile();
844     LOGI("AccessControlProfile size is %{public}zu", profiles.size());
845     for (auto &item : profiles) {
846         if (item.GetTrustDeviceId() == deviceId && item.GetStatus() == ACTIVE &&
847             item.GetBindLevel() == DEVICE && (item.GetAccesser().GetAccesserBundleName() == pkgName ||
848             item.GetAccesser().GetAccesserBundleName() == "") && item.GetAccesser().GetAccesserUserId() == 0 &&
849             item.GetAccesser().GetAccesserAccountId() == "") {
850             return true;
851         }
852     }
853     return false;
854 }
855 
CheckDevIdInAclForDevBind(const std::string & pkgName,const std::string & deviceId)856 bool DeviceProfileConnector::CheckDevIdInAclForDevBind(const std::string &pkgName, const std::string &deviceId)
857 {
858     return (CheckSinkDevIdInAclForDevBind(pkgName, deviceId) || CheckSrcDevIdInAclForDevBind(pkgName, deviceId));
859 }
860 
DeleteTimeOutAcl(const std::string & deviceId)861 uint32_t DeviceProfileConnector::DeleteTimeOutAcl(const std::string &deviceId)
862 {
863     std::vector<AccessControlProfile> profiles = GetAccessControlProfile();
864     LOGI("AccessControlProfile size is %{public}zu", profiles.size());
865     uint32_t res = 0;
866     for (auto &item : profiles) {
867         if (item.GetTrustDeviceId() != deviceId || item.GetStatus() != ACTIVE) {
868             continue;
869         }
870         res++;
871         if (item.GetAuthenticationType() == ALLOW_AUTH_ONCE) {
872             res--;
873             DistributedDeviceProfileClient::GetInstance().DeleteAccessControlProfile(item.GetAccessControlId());
874         }
875     }
876     return res;
877 }
878 
GetTrustNumber(const std::string & deviceId)879 int32_t DeviceProfileConnector::GetTrustNumber(const std::string &deviceId)
880 {
881     std::vector<AccessControlProfile> profiles = GetAccessControlProfile();
882     LOGI("AccessControlProfile size is %{public}zu", profiles.size());
883     int32_t trustNumber = 0;
884     for (auto &item : profiles) {
885         if (item.GetTrustDeviceId() == deviceId && item.GetStatus() == ACTIVE) {
886             trustNumber++;
887         }
888     }
889     return trustNumber;
890 }
891 
IsSameAccount(const std::string & udid)892 int32_t DeviceProfileConnector::IsSameAccount(const std::string &udid)
893 {
894     std::vector<AccessControlProfile> profiles = GetAccessControlProfile();
895     for (auto &item : profiles) {
896         if (item.GetTrustDeviceId() == udid && item.GetStatus() == ACTIVE) {
897             if (item.GetBindType() == DM_IDENTICAL_ACCOUNT) {  // 同账号
898                 LOGI("The udid %{public}s is identical bind.", GetAnonyString(udid).c_str());
899                 return DM_OK;
900             }
901         }
902     }
903     return ERR_DM_FAILED;
904 }
905 
checkAccesserACL(AccessControlProfile & profile,const DmAccessCaller & caller,const std::string & srcUdid,const DmAccessCallee & callee,const std::string & sinkUdid)906 bool checkAccesserACL(AccessControlProfile& profile, const DmAccessCaller &caller,
907     const std::string &srcUdid, const DmAccessCallee &callee, const std::string &sinkUdid)
908 {
909     if ((profile.GetAccesser().GetAccesserUserId() == caller.userId ||
910         profile.GetAccesser().GetAccesserUserId() == 0 ||
911         profile.GetAccesser().GetAccesserUserId() == -1) &&
912         profile.GetAccesser().GetAccesserDeviceId() == srcUdid &&
913         profile.GetAccessee().GetAccesseeDeviceId() == sinkUdid) {
914         if (callee.userId != 0 && callee.userId == profile.GetAccessee().GetAccesseeUserId()) {
915             LOGI("accesser success add, callee userId not null!");
916             return true;
917         } else if (callee.userId == 0 ||
918             profile.GetAccessee().GetAccesseeUserId() == -1 ||
919             profile.GetAccessee().GetAccesseeUserId() == 0) {
920             LOGI("accesser success add!");
921             return true;
922         }
923     }
924     return false;
925 }
926 
checkAccesseeACL(AccessControlProfile & profile,const DmAccessCaller & caller,const std::string & srcUdid,const DmAccessCallee & callee,const std::string & sinkUdid)927 bool checkAccesseeACL(AccessControlProfile& profile, const DmAccessCaller &caller,
928     const std::string &srcUdid, const DmAccessCallee &callee, const std::string &sinkUdid)
929 {
930     if ((profile.GetAccessee().GetAccesseeUserId() == caller.userId ||
931         profile.GetAccessee().GetAccesseeUserId() == 0 ||
932         profile.GetAccessee().GetAccesseeUserId() == -1) &&
933         profile.GetAccessee().GetAccesseeDeviceId() == srcUdid &&
934         profile.GetAccesser().GetAccesserDeviceId() == sinkUdid) {
935         if (callee.userId != 0 && callee.userId == profile.GetAccesser().GetAccesserUserId()) {
936             LOGI("accessee success add, callee userId not null!");
937             return true;
938         } else if (callee.userId == 0 ||
939             profile.GetAccesser().GetAccesserUserId() == -1 ||
940             profile.GetAccesser().GetAccesserUserId() == 0) {
941             LOGI("accessee success add!");
942             return true;
943         }
944     }
945     return false;
946 }
947 
GetACLByDeviceIdAndUserId(std::vector<AccessControlProfile> profiles,const DmAccessCaller & caller,const std::string & srcUdid,const DmAccessCallee & callee,const std::string & sinkUdid)948 std::vector<AccessControlProfile> GetACLByDeviceIdAndUserId(std::vector<AccessControlProfile> profiles,
949     const DmAccessCaller &caller, const std::string &srcUdid, const DmAccessCallee &callee, const std::string &sinkUdid)
950 {
951     LOGI("srcUdid = %{public}s, caller.userId = %{public}s, sinkUdid = %{public}s, callee.userId = %{public}s",
952         GetAnonyString(srcUdid).c_str(), GetAnonyInt32(caller.userId).c_str(),
953         GetAnonyString(sinkUdid).c_str(), GetAnonyInt32(callee.userId).c_str());
954     std::vector<AccessControlProfile> profilesFilter;
955     for (auto &item : profiles) {
956         if (checkAccesserACL(item, caller, srcUdid, callee, sinkUdid) ||
957             checkAccesseeACL(item, caller, srcUdid, callee, sinkUdid)) {
958             profilesFilter.push_back(item);
959         }
960     }
961     return profilesFilter;
962 }
963 
CheckAccessControl(const DmAccessCaller & caller,const std::string & srcUdid,const DmAccessCallee & callee,const std::string & sinkUdid)964 int32_t DeviceProfileConnector::CheckAccessControl(const DmAccessCaller &caller, const std::string &srcUdid,
965     const DmAccessCallee &callee, const std::string &sinkUdid)
966 {
967     LOGI("PkgName = %{public}s, srcUdid = %{public}s, sinkUdid = %{public}s",
968         caller.pkgName.c_str(), GetAnonyString(srcUdid).c_str(), GetAnonyString(sinkUdid).c_str());
969     std::vector<AccessControlProfile> profiles = GetAllAccessControlProfile();
970     std::vector<AccessControlProfile> profilesFilter =
971         GetACLByDeviceIdAndUserId(profiles, caller, srcUdid, callee, sinkUdid);
972     for (auto &item : profilesFilter) {
973         if (item.GetStatus() != ACTIVE || (item.GetTrustDeviceId() != sinkUdid &&
974             item.GetTrustDeviceId() != srcUdid)) {
975             continue;
976         }
977         if (SingleUserProcess(item, caller, callee)) {
978             return DM_OK;
979         }
980     }
981     return ERR_DM_FAILED;
982 }
983 
CheckIdenticalAccount(int32_t userId,const std::string & accountId)984 bool DeviceProfileConnector::CheckIdenticalAccount(int32_t userId, const std::string &accountId)
985 {
986     std::vector<AccessControlProfile> profiles;
987     std::map<std::string, std::string> queryParams;
988     queryParams[USERID] = std::to_string(userId);
989     queryParams[ACCOUNTID] = accountId;
990     if (DistributedDeviceProfileClient::GetInstance().GetAccessControlProfile(queryParams, profiles) != DM_OK) {
991         LOGE("DP GetAccessControlProfile failed.");
992     }
993     for (auto &item : profiles) {
994         if (item.GetBindType() == DM_IDENTICAL_ACCOUNT && item.GetStatus() == ACTIVE) {
995             return true;
996         }
997     }
998     return false;
999 }
1000 
SingleUserProcess(const DistributedDeviceProfile::AccessControlProfile & profile,const DmAccessCaller & caller,const DmAccessCallee & callee)1001 bool DeviceProfileConnector::SingleUserProcess(const DistributedDeviceProfile::AccessControlProfile &profile,
1002     const DmAccessCaller &caller, const DmAccessCallee &callee)
1003 {
1004     LOGI("BindType %{public}d, bindLevel %{public}d.",
1005         profile.GetBindType(), profile.GetBindLevel());
1006     uint32_t bindType = profile.GetBindType();
1007     bool ret = false;
1008     switch (bindType) {
1009         case DM_IDENTICAL_ACCOUNT:
1010             ret = true;
1011             break;
1012         case DM_POINT_TO_POINT:
1013             if (profile.GetBindLevel() == DEVICE || profile.GetBindLevel() == SERVICE) {
1014                 ret = true;
1015             } else if (profile.GetBindLevel() == APP &&
1016                 (profile.GetAccesser().GetAccesserBundleName() == caller.pkgName ||
1017                 profile.GetAccessee().GetAccesseeBundleName() == caller.pkgName)) {
1018                 ret = CheckAppLevelAccess(profile, caller, callee);
1019             }
1020             break;
1021         case DM_ACROSS_ACCOUNT:
1022             if (profile.GetBindLevel() == DEVICE || profile.GetBindLevel() == SERVICE) {
1023                 ret = true;
1024             } else if (profile.GetBindLevel() == APP &&
1025                 (profile.GetAccesser().GetAccesserBundleName() == caller.pkgName ||
1026                 profile.GetAccessee().GetAccesseeBundleName() == caller.pkgName)) {
1027                 ret = CheckAppLevelAccess(profile, caller, callee);
1028             }
1029             break;
1030         default:
1031             LOGE("unknown bind type %{public}d.", bindType);
1032             break;
1033     }
1034     return ret;
1035 }
1036 
CheckAppLevelAccess(const DistributedDeviceProfile::AccessControlProfile & profile,const DmAccessCaller & caller,const DmAccessCallee & callee)1037 bool DeviceProfileConnector::CheckAppLevelAccess(const DistributedDeviceProfile::AccessControlProfile &profile,
1038     const DmAccessCaller &caller, const DmAccessCallee &callee)
1039 {
1040     if (caller.tokenId == 0 || callee.tokenId == 0) {
1041         return true;
1042     } else {
1043         if ((profile.GetAccesser().GetAccesserTokenId() == caller.tokenId &&
1044             profile.GetAccessee().GetAccesseeTokenId() == callee.tokenId) ||
1045             (profile.GetAccesser().GetAccesserTokenId() == callee.tokenId &&
1046             profile.GetAccessee().GetAccesseeTokenId() == caller.tokenId)) {
1047             return true;
1048         } else {
1049             return false;
1050         }
1051     }
1052 }
1053 
CheckIsSameAccount(const DmAccessCaller & caller,const std::string & srcUdid,const DmAccessCallee & callee,const std::string & sinkUdid)1054 int32_t DeviceProfileConnector::CheckIsSameAccount(const DmAccessCaller &caller, const std::string &srcUdid,
1055     const DmAccessCallee &callee, const std::string &sinkUdid)
1056 {
1057     LOGI("DeviceProfileConnector::CheckIsSameAccount pkgName %{public}s, srcUdid %{public}s, sinkUdid %{public}s",
1058         caller.pkgName.c_str(), GetAnonyString(srcUdid).c_str(), GetAnonyString(sinkUdid).c_str());
1059     std::vector<AccessControlProfile> profiles = GetAllAccessControlProfile();
1060     std::vector<AccessControlProfile> profilesFilter
1061         = GetACLByDeviceIdAndUserId(profiles, caller, srcUdid, callee, sinkUdid);
1062     for (auto &item : profilesFilter) {
1063         if (item.GetStatus() != ACTIVE || (item.GetTrustDeviceId() != sinkUdid &&
1064             item.GetTrustDeviceId() != srcUdid)) {
1065             continue;
1066         }
1067         if (item.GetBindType() == DM_IDENTICAL_ACCOUNT) {
1068             LOGI("The udid %{public}s is identical bind.", GetAnonyString(item.GetTrustDeviceId()).c_str());
1069             return DM_OK;
1070         }
1071     }
1072     return ERR_DM_FAILED;
1073 }
1074 
GetBindLevel(const std::string & pkgName,const std::string & localUdid,const std::string & udid,uint64_t & tokenId)1075 int32_t DeviceProfileConnector::GetBindLevel(const std::string &pkgName, const std::string &localUdid,
1076     const std::string &udid, uint64_t &tokenId)
1077 {
1078     LOGI("pkgName %{public}s, tokenId %{public}" PRId64", udid %{public}s.", pkgName.c_str(),
1079         tokenId, GetAnonyString(udid).c_str());
1080     std::vector<AccessControlProfile> profiles = GetAclProfileByDeviceIdAndUserId(localUdid,
1081         MultipleUserConnector::GetFirstForegroundUserId());
1082     int32_t bindLevel = INVALIED_TYPE;
1083     for (auto &item : profiles) {
1084         if (item.GetTrustDeviceId() != udid) {
1085             continue;
1086         }
1087         if (item.GetAccesser().GetAccesserBundleName() == pkgName &&
1088             item.GetAccesser().GetAccesserDeviceId() == localUdid &&
1089             item.GetAccessee().GetAccesseeDeviceId() == udid) {
1090             tokenId = static_cast<uint64_t>(item.GetAccesser().GetAccesserTokenId());
1091             bindLevel = static_cast<int32_t>(item.GetBindLevel());
1092             LOGI("Src get bindLevel %{public}d, tokenid %{public}" PRId64".", bindLevel, tokenId);
1093             continue;
1094         }
1095         if (item.GetAccessee().GetAccesseeBundleName() == pkgName &&
1096             item.GetAccessee().GetAccesseeDeviceId() == localUdid &&
1097             item.GetAccesser().GetAccesserDeviceId() == udid) {
1098             tokenId = static_cast<uint64_t>(item.GetAccessee().GetAccesseeTokenId());
1099             bindLevel = static_cast<int32_t>(item.GetBindLevel());
1100             LOGI("Sink get bindLevel %{public}d, tokenid %{public}" PRId64".", bindLevel, tokenId);
1101             continue;
1102         }
1103     }
1104     return bindLevel;
1105 }
1106 
GetDeviceIdAndBindLevel(std::vector<int32_t> userIds,const std::string & localUdid)1107 std::map<std::string, int32_t> DeviceProfileConnector::GetDeviceIdAndBindLevel(std::vector<int32_t> userIds,
1108     const std::string &localUdid)
1109 {
1110     std::vector<AccessControlProfile> profiles = GetAllAccessControlProfile();
1111     std::map<std::string, int32_t> deviceIdMap;
1112     for (const auto &item : profiles) {
1113         if (find(userIds.begin(), userIds.end(), item.GetAccesser().GetAccesserUserId()) != userIds.end() &&
1114             item.GetAccesser().GetAccesserDeviceId() == localUdid) {
1115             LOGI("Get Device Bind type localUdid %{public}s is src.", GetAnonyString(localUdid).c_str());
1116             UpdateBindType(item.GetTrustDeviceId(), item.GetBindLevel(), deviceIdMap);
1117             continue;
1118         }
1119         if (find(userIds.begin(), userIds.end(), item.GetAccessee().GetAccesseeUserId()) != userIds.end() &&
1120             item.GetAccessee().GetAccesseeDeviceId() == localUdid) {
1121             LOGI("Get Device Bind type localUdid %{public}s is sink.", GetAnonyString(localUdid).c_str());
1122             UpdateBindType(item.GetTrustDeviceId(), item.GetBindLevel(), deviceIdMap);
1123             continue;
1124         }
1125     }
1126     return deviceIdMap;
1127 }
1128 
GetDeviceIdAndUserId(int32_t userId,const std::string & accountId,const std::string & localUdid)1129 std::multimap<std::string, int32_t> DeviceProfileConnector::GetDeviceIdAndUserId(int32_t userId,
1130     const std::string &accountId, const std::string &localUdid)
1131 {
1132     LOGI("localUdid %{public}s, userId %{public}d, accountId %{public}s.", GetAnonyString(localUdid).c_str(),
1133         userId, GetAnonyString(accountId).c_str());
1134     std::vector<AccessControlProfile> profiles = GetAllAccessControlProfile();
1135     std::multimap<std::string, int32_t> deviceIdMap;
1136     for (const auto &item : profiles) {
1137         std::string accesserUdid = item.GetAccesser().GetAccesserDeviceId();
1138         std::string accesseeUdid = item.GetAccessee().GetAccesseeDeviceId();
1139         int32_t accesserUserId = item.GetAccesser().GetAccesserUserId();
1140         int32_t accesseeUserId = item.GetAccessee().GetAccesseeUserId();
1141         if (accesserUdid == localUdid && accesserUserId == userId && item.GetBindType() == DM_IDENTICAL_ACCOUNT) {
1142             LOGI("Account logout trust udid %{public}s userid %{public}d is src.",
1143                 GetAnonyString(accesseeUdid).c_str(), accesseeUserId);
1144             deviceIdMap.insert(std::pair<std::string, int32_t>(accesseeUdid, accesseeUserId));
1145             continue;
1146         }
1147         if (accesseeUdid == localUdid && accesseeUserId == userId && item.GetBindType() == DM_IDENTICAL_ACCOUNT) {
1148             LOGI("Account logout trust udid %{public}s userid %{public}d is sink.",
1149                 GetAnonyString(accesserUdid).c_str(), accesserUserId);
1150             deviceIdMap.insert(std::pair<std::string, int32_t>(accesserUdid, accesserUserId));
1151             continue;
1152         }
1153     }
1154     return deviceIdMap;
1155 }
1156 
UpdateBindType(const std::string & udid,int32_t compareParam,std::map<std::string,int32_t> & deviceMap)1157 void DeviceProfileConnector::UpdateBindType(const std::string &udid, int32_t compareParam,
1158     std::map<std::string, int32_t> &deviceMap)
1159 {
1160     LOGI("BindType %{public}d.", compareParam);
1161     if (deviceMap.find(udid) == deviceMap.end()) {
1162         deviceMap[udid] = compareParam;
1163     } else {
1164         deviceMap[udid] = std::min(deviceMap[udid], compareParam);
1165     }
1166 }
1167 
HandleAccountLogoutEvent(int32_t remoteUserId,const std::string & remoteAccountHash,const std::string & remoteUdid,const std::string & localUdid)1168 int32_t DeviceProfileConnector::HandleAccountLogoutEvent(int32_t remoteUserId,
1169     const std::string &remoteAccountHash, const std::string &remoteUdid, const std::string &localUdid)
1170 {
1171     LOGI("RemoteUserId %{public}d, remoteAccountHash %{public}s, remoteUdid %{public}s, localUdid %{public}s.",
1172         remoteUserId, GetAnonyString(remoteAccountHash).c_str(), GetAnonyString(remoteUdid).c_str(),
1173         GetAnonyString(localUdid).c_str());
1174     std::vector<AccessControlProfile> profiles = GetAccessControlProfileByUserId(remoteUserId);
1175     int32_t bindType = DM_INVALIED_BINDTYPE;
1176     for (const auto &item : profiles) {
1177         if (item.GetTrustDeviceId() != remoteUdid) {
1178             continue;
1179         }
1180         std::string accesserUdid = item.GetAccesser().GetAccesserDeviceId();
1181         std::string accesseeUdid = item.GetAccessee().GetAccesseeDeviceId();
1182         int32_t accesserUserId = item.GetAccesser().GetAccesserUserId();
1183         int32_t accesseeUserId = item.GetAccessee().GetAccesseeUserId();
1184         if (accesserUdid == localUdid && accesseeUdid == remoteUdid && accesseeUserId == remoteUserId) {
1185             DistributedDeviceProfileClient::GetInstance().DeleteAccessControlProfile(item.GetAccessControlId());
1186             bindType = DM_IDENTICAL_ACCOUNT;
1187             continue;
1188         }
1189         if (accesseeUdid == localUdid && accesserUdid == remoteUdid && accesserUserId == remoteUserId) {
1190             DistributedDeviceProfileClient::GetInstance().DeleteAccessControlProfile(item.GetAccessControlId());
1191             bindType = DM_IDENTICAL_ACCOUNT;
1192             continue;
1193         }
1194     }
1195     return bindType;
1196 }
1197 
HandleDevUnBindEvent(int32_t remoteUserId,const std::string & remoteUdid,const std::string & localUdid)1198 int32_t DeviceProfileConnector::HandleDevUnBindEvent(int32_t remoteUserId, const std::string &remoteUdid,
1199     const std::string &localUdid)
1200 {
1201     LOGI("RemoteUserId %{public}d, remoteUdid %{public}s, localUdid %{public}s.", remoteUserId,
1202         GetAnonyString(remoteUdid).c_str(), GetAnonyString(localUdid).c_str());
1203     std::vector<AccessControlProfile> profiles = GetAclProfileByDeviceIdAndUserId(remoteUdid, remoteUserId);
1204     int32_t bindType = DM_INVALIED_BINDTYPE;
1205     for (const auto &item : profiles) {
1206         if (item.GetTrustDeviceId() != remoteUdid) {
1207             continue;
1208         }
1209         if (item.GetBindType() == DM_IDENTICAL_ACCOUNT) {
1210             bindType = DM_IDENTICAL_ACCOUNT;
1211             continue;
1212         }
1213         DistributedDeviceProfileClient::GetInstance().DeleteAccessControlProfile(item.GetAccessControlId());
1214         bindType = std::min(bindType, static_cast<int32_t>(item.GetBindType()));
1215     }
1216     return bindType;
1217 }
1218 
HandleAppUnBindEvent(int32_t remoteUserId,const std::string & remoteUdid,int32_t tokenId,const std::string & localUdid)1219 OHOS::DistributedHardware::ProcessInfo DeviceProfileConnector::HandleAppUnBindEvent(int32_t remoteUserId,
1220     const std::string &remoteUdid, int32_t tokenId, const std::string &localUdid)
1221 {
1222     LOGI("RemoteUserId %{public}d, remoteUdid %{public}s, tokenId %{public}d, localUdid %{public}s.",
1223         remoteUserId, GetAnonyString(remoteUdid).c_str(), tokenId, GetAnonyString(localUdid).c_str());
1224     std::vector<AccessControlProfile> profiles = GetAccessControlProfile();
1225     ProcessInfo processInfo;
1226     for (const auto &item : profiles) {
1227         if (item.GetTrustDeviceId() != remoteUdid || item.GetBindType() == DM_IDENTICAL_ACCOUNT ||
1228             item.GetBindLevel() != APP) {
1229             continue;
1230         }
1231         if (item.GetAccesser().GetAccesserUserId() == remoteUserId &&
1232             item.GetAccesser().GetAccesserDeviceId() == remoteUdid &&
1233             static_cast<int32_t>(item.GetAccesser().GetAccesserTokenId()) == tokenId &&
1234             item.GetAccessee().GetAccesseeDeviceId() == localUdid) {
1235             LOGI("Src device unbind.");
1236             DistributedDeviceProfileClient::GetInstance().DeleteAccessControlProfile(item.GetAccessControlId());
1237             processInfo.pkgName = item.GetAccessee().GetAccesseeBundleName();
1238             processInfo.userId = item.GetAccessee().GetAccesseeUserId();
1239             continue;
1240         }
1241         if (item.GetAccessee().GetAccesseeUserId() == remoteUserId &&
1242             item.GetAccessee().GetAccesseeDeviceId() == remoteUdid &&
1243             static_cast<int32_t>(item.GetAccessee().GetAccesseeTokenId()) == tokenId &&
1244             item.GetAccesser().GetAccesserDeviceId() == localUdid) {
1245             LOGI("Sink device unbind.");
1246             DistributedDeviceProfileClient::GetInstance().DeleteAccessControlProfile(item.GetAccessControlId());
1247             processInfo.pkgName = item.GetAccesser().GetAccesserBundleName();
1248             processInfo.userId = item.GetAccesser().GetAccesserUserId();
1249             continue;
1250         }
1251     }
1252     return processInfo;
1253 }
1254 
HandleAppUnBindEvent(int32_t remoteUserId,const std::string & remoteUdid,int32_t tokenId,const std::string & localUdid,int32_t peerTokenId)1255 OHOS::DistributedHardware::ProcessInfo DeviceProfileConnector::HandleAppUnBindEvent(int32_t remoteUserId,
1256     const std::string &remoteUdid, int32_t tokenId, const std::string &localUdid, int32_t peerTokenId)
1257 {
1258     LOGI("RemoteUserId %{public}d, remoteUdid %{public}s, tokenId %{public}d, localUdid %{public}s.",
1259         remoteUserId, GetAnonyString(remoteUdid).c_str(), tokenId, GetAnonyString(localUdid).c_str());
1260     std::vector<AccessControlProfile> profiles = GetAccessControlProfile();
1261     ProcessInfo processInfo;
1262     for (const auto &item : profiles) {
1263         if (item.GetTrustDeviceId() != remoteUdid || item.GetBindType() == DM_IDENTICAL_ACCOUNT ||
1264             item.GetBindLevel() != APP) {
1265             continue;
1266         }
1267         if (item.GetAccesser().GetAccesserUserId() == remoteUserId &&
1268             item.GetAccesser().GetAccesserDeviceId() == remoteUdid &&
1269             static_cast<int32_t>(item.GetAccesser().GetAccesserTokenId()) == tokenId &&
1270             static_cast<int32_t>(item.GetAccessee().GetAccesseeTokenId()) == peerTokenId &&
1271             item.GetAccessee().GetAccesseeDeviceId() == localUdid) {
1272             LOGI("Src device unbind.");
1273             DistributedDeviceProfileClient::GetInstance().DeleteAccessControlProfile(item.GetAccessControlId());
1274             processInfo.pkgName = item.GetAccessee().GetAccesseeBundleName();
1275             processInfo.userId = item.GetAccessee().GetAccesseeUserId();
1276             continue;
1277         }
1278         if (item.GetAccessee().GetAccesseeUserId() == remoteUserId &&
1279             item.GetAccessee().GetAccesseeDeviceId() == remoteUdid &&
1280             static_cast<int32_t>(item.GetAccessee().GetAccesseeTokenId()) == tokenId &&
1281             static_cast<int32_t>(item.GetAccesser().GetAccesserTokenId()) == peerTokenId &&
1282             item.GetAccesser().GetAccesserDeviceId() == localUdid) {
1283             LOGI("Sink device unbind.");
1284             DistributedDeviceProfileClient::GetInstance().DeleteAccessControlProfile(item.GetAccessControlId());
1285             processInfo.pkgName = item.GetAccesser().GetAccesserBundleName();
1286             processInfo.userId = item.GetAccesser().GetAccesserUserId();
1287             continue;
1288         }
1289     }
1290     return processInfo;
1291 }
1292 
GetAllAccessControlProfile()1293 std::vector<AccessControlProfile> DeviceProfileConnector::GetAllAccessControlProfile()
1294 {
1295     std::vector<AccessControlProfile> profiles;
1296     if (DistributedDeviceProfileClient::GetInstance().GetAllAccessControlProfile(profiles) != DM_OK) {
1297         LOGE("DP failed.");
1298     }
1299     return profiles;
1300 }
1301 
DeleteAccessControlById(int64_t accessControlId)1302 void DeviceProfileConnector::DeleteAccessControlById(int64_t accessControlId)
1303 {
1304     DistributedDeviceProfileClient::GetInstance().DeleteAccessControlProfile(accessControlId);
1305 }
1306 
HandleUserSwitched(const std::string & localUdid,const std::vector<std::string> & deviceVec,int32_t currentUserId,int32_t beforeUserId)1307 int32_t DeviceProfileConnector::HandleUserSwitched(const std::string &localUdid,
1308     const std::vector<std::string> &deviceVec, int32_t currentUserId, int32_t beforeUserId)
1309 {
1310     if (deviceVec.empty()) {
1311         LOGI("no remote device.");
1312         return DM_OK;
1313     }
1314     std::vector<AccessControlProfile> profiles = GetAllAccessControlProfile();
1315     std::vector<AccessControlProfile> activeProfiles;
1316     std::vector<AccessControlProfile> inActiveProfiles;
1317     std::vector<AccessControlProfile> delActiveProfiles;
1318     for (auto &item : profiles) {
1319         if (std::find(deviceVec.begin(), deviceVec.end(), item.GetTrustDeviceId()) == deviceVec.end()) {
1320             continue;
1321         }
1322         if ((item.GetAccesser().GetAccesserDeviceId() == localUdid &&
1323             item.GetAccesser().GetAccesserUserId() == beforeUserId) ||
1324             (item.GetAccessee().GetAccesseeDeviceId() == localUdid &&
1325             item.GetAccessee().GetAccesseeUserId() == beforeUserId)) {
1326             if (item.GetStatus() == ACTIVE) {
1327                 item.SetStatus(INACTIVE);
1328                 inActiveProfiles.push_back(item);
1329             }
1330             if (item.GetBindType() != DM_IDENTICAL_ACCOUNT) {
1331                 delActiveProfiles.push_back(item);
1332             }
1333             continue;
1334         }
1335         if ((item.GetAccesser().GetAccesserDeviceId() == localUdid &&
1336             item.GetAccesser().GetAccesserUserId() == currentUserId && item.GetStatus() == INACTIVE) || (
1337             item.GetAccessee().GetAccesseeDeviceId() == localUdid &&
1338             item.GetAccessee().GetAccesseeUserId() == currentUserId && item.GetStatus() == INACTIVE)) {
1339             item.SetStatus(ACTIVE);
1340             activeProfiles.push_back(item);
1341             continue;
1342         }
1343     }
1344     HandleUserSwitched(activeProfiles, inActiveProfiles, delActiveProfiles);
1345     return DM_OK;
1346 }
1347 
HandleUserSwitched(const std::vector<AccessControlProfile> & activeProfiles,const std::vector<AccessControlProfile> & inActiveProfiles,const std::vector<AccessControlProfile> & delActiveProfiles)1348 void DeviceProfileConnector::HandleUserSwitched(const std::vector<AccessControlProfile> &activeProfiles,
1349     const std::vector<AccessControlProfile> &inActiveProfiles,
1350     const std::vector<AccessControlProfile> &delActiveProfiles)
1351 {
1352     for (auto &item : inActiveProfiles) {
1353         DistributedDeviceProfileClient::GetInstance().UpdateAccessControlProfile(item);
1354     }
1355     for (auto &item : delActiveProfiles) {
1356         DistributedDeviceProfileClient::GetInstance().DeleteAccessControlProfile(item.GetAccessControlId());
1357     }
1358     for (auto &item : activeProfiles) {
1359         DistributedDeviceProfileClient::GetInstance().UpdateAccessControlProfile(item);
1360     }
1361 }
1362 
GetAclProfileByUserId(const std::string & localUdid,int32_t userId,const std::string & remoteUdid)1363 std::vector<AccessControlProfile> DeviceProfileConnector::GetAclProfileByUserId(const std::string &localUdid,
1364     int32_t userId, const std::string &remoteUdid)
1365 {
1366     LOGI("localUdid %{public}s, localUserId %{public}d, remoteUdid %{public}s.", GetAnonyString(localUdid).c_str(),
1367         userId, GetAnonyString(remoteUdid).c_str());
1368     std::vector<AccessControlProfile> profiles = GetAllAccessControlProfile();
1369     std::vector<AccessControlProfile> profilesTemp;
1370     for (const auto &item : profiles) {
1371         if (item.GetAccesser().GetAccesserDeviceId() == localUdid &&
1372             item.GetAccesser().GetAccesserUserId() == userId &&
1373             item.GetAccessee().GetAccesseeDeviceId() == remoteUdid) {
1374             profilesTemp.push_back(item);
1375         } else if (item.GetAccessee().GetAccesseeDeviceId() == localUdid &&
1376             item.GetAccessee().GetAccesseeUserId() == userId &&
1377             item.GetAccesser().GetAccesserDeviceId() == remoteUdid) {
1378             profilesTemp.push_back(item);
1379         }
1380     }
1381     return profilesTemp;
1382 }
1383 
HandleSyncForegroundUserIdEvent(const std::vector<int32_t> & remoteUserIds,const std::string & remoteUdid,const std::vector<int32_t> & localUserIds,std::string & localUdid)1384 void DeviceProfileConnector::HandleSyncForegroundUserIdEvent(const std::vector<int32_t> &remoteUserIds,
1385     const std::string &remoteUdid, const std::vector<int32_t> &localUserIds, std::string &localUdid)
1386 {
1387     LOGI("localUdid %{public}s, remoteUdid %{public}s.", GetAnonyString(localUdid).c_str(),
1388         GetAnonyString(remoteUdid).c_str());
1389     std::vector<AccessControlProfile> profiles = GetAllAccessControlProfile();
1390     for (auto &item : profiles) {
1391         if (item.GetAccesser().GetAccesserDeviceId() == localUdid &&
1392             item.GetAccessee().GetAccesseeDeviceId() == remoteUdid &&
1393             find(localUserIds.begin(), localUserIds.end(),
1394                 item.GetAccesser().GetAccesserUserId()) != localUserIds.end() &&
1395             find(remoteUserIds.begin(), remoteUserIds.end(),
1396                 item.GetAccessee().GetAccesseeUserId()) != remoteUserIds.end() && item.GetStatus() == INACTIVE) {
1397             item.SetStatus(ACTIVE);
1398             DistributedDeviceProfileClient::GetInstance().UpdateAccessControlProfile(item);
1399         } else if (item.GetAccessee().GetAccesseeDeviceId() == localUdid &&
1400             item.GetAccesser().GetAccesserDeviceId() == remoteUdid &&
1401             find(localUserIds.begin(), localUserIds.end(),
1402                 item.GetAccessee().GetAccesseeUserId()) != localUserIds.end() &&
1403             find(remoteUserIds.begin(), remoteUserIds.end(),
1404                 item.GetAccesser().GetAccesserUserId()) != remoteUserIds.end() && item.GetStatus() == INACTIVE) {
1405             item.SetStatus(ACTIVE);
1406             DistributedDeviceProfileClient::GetInstance().UpdateAccessControlProfile(item);
1407         }
1408     }
1409 }
1410 
GetOfflineProcessInfo(std::string & localUdid,const std::vector<int32_t> & localUserIds,const std::string & remoteUdid,const std::vector<int32_t> & remoteUserIds)1411 std::vector<ProcessInfo> DeviceProfileConnector::GetOfflineProcessInfo(std::string &localUdid,
1412     const std::vector<int32_t> &localUserIds, const std::string &remoteUdid, const std::vector<int32_t> &remoteUserIds)
1413 {
1414     LOGI("localUdid %{public}s, remoteUdid %{public}s.", GetAnonyString(localUdid).c_str(),
1415         GetAnonyString(remoteUdid).c_str());
1416     std::vector<AccessControlProfile> profiles = GetAllAccessControlProfile();
1417     std::vector<ProcessInfo> processInfos;
1418     int32_t bindLevel = 100;
1419     for (const auto &item : profiles) {
1420         ProcessInfo processInfo;
1421         std::string accesserUdid = item.GetAccesser().GetAccesserDeviceId();
1422         std::string accesseeUdid = item.GetAccessee().GetAccesseeDeviceId();
1423         int32_t accesserUserId = item.GetAccesser().GetAccesserUserId();
1424         int32_t accesseeUserId = item.GetAccessee().GetAccesseeUserId();
1425         if (accesserUdid == localUdid && accesseeUdid == remoteUdid &&
1426             find(localUserIds.begin(), localUserIds.end(), accesserUserId) != localUserIds.end() &&
1427             find(remoteUserIds.begin(), remoteUserIds.end(), accesseeUserId) != remoteUserIds.end() &&
1428             item.GetStatus() == ACTIVE) {
1429             processInfo.pkgName = item.GetAccesser().GetAccesserBundleName();
1430             processInfo.userId = item.GetAccesser().GetAccesserUserId();
1431             bindLevel = std::min(bindLevel, static_cast<int32_t>(item.GetBindLevel()));
1432             processInfos.push_back(processInfo);
1433         } else if (accesseeUdid == localUdid && accesserUdid == remoteUdid &&
1434             find(localUserIds.begin(), localUserIds.end(), accesseeUserId) != localUserIds.end() &&
1435             find(remoteUserIds.begin(), remoteUserIds.end(), accesserUserId) != remoteUserIds.end() &&
1436             item.GetStatus() == ACTIVE) {
1437             processInfo.pkgName = item.GetAccessee().GetAccesseeBundleName();
1438             processInfo.userId = item.GetAccessee().GetAccesseeUserId();
1439             bindLevel = std::min(bindLevel, static_cast<int32_t>(item.GetBindLevel()));
1440             processInfos.push_back(processInfo);
1441         }
1442     }
1443     if (bindLevel == INVALIED_TYPE || bindLevel == DEVICE) {
1444         processInfos.clear();
1445         for (const auto &item : localUserIds) {
1446             ProcessInfo processInfo;
1447             processInfo.pkgName = std::string(DM_PKG_NAME);
1448             processInfo.userId = item;
1449             processInfos.push_back(processInfo);
1450         }
1451     }
1452     return processInfos;
1453 }
1454 
GetUserIdAndBindLevel(const std::string & localUdid,const std::string & peerUdid)1455 std::map<int32_t, int32_t> DeviceProfileConnector::GetUserIdAndBindLevel(const std::string &localUdid,
1456     const std::string &peerUdid)
1457 {
1458     LOGI("localUdid %{public}s, peerUdid %{public}s.", GetAnonyString(localUdid).c_str(),
1459         GetAnonyString(peerUdid).c_str());
1460     std::vector<AccessControlProfile> profiles = GetAllAccessControlProfile();
1461     std::map<int32_t, int32_t> userIdAndBindLevel;
1462     for (const auto &item : profiles) {
1463         std::string accesserUdid = item.GetAccesser().GetAccesserDeviceId();
1464         std::string accesseeUdid = item.GetAccessee().GetAccesseeDeviceId();
1465         int32_t accesserUserid = item.GetAccesser().GetAccesserUserId();
1466         int32_t accesseeUserid = item.GetAccessee().GetAccesseeUserId();
1467         if (accesserUdid == localUdid && accesseeUdid == peerUdid) {
1468             if (userIdAndBindLevel.find(accesserUserid) == userIdAndBindLevel.end()) {
1469                 userIdAndBindLevel[accesserUserid] = static_cast<int32_t>(item.GetBindLevel());
1470             } else {
1471                 userIdAndBindLevel[accesserUserid] =
1472                     std::min(static_cast<int32_t>(item.GetBindLevel()), userIdAndBindLevel[accesserUserid]);
1473             }
1474         } else if (accesseeUdid == localUdid && accesserUdid == peerUdid) {
1475             if (userIdAndBindLevel.find(accesseeUserid) == userIdAndBindLevel.end()) {
1476                 userIdAndBindLevel[accesseeUserid] = static_cast<int32_t>(item.GetBindLevel());
1477             } else {
1478                 userIdAndBindLevel[accesseeUserid] =
1479                     std::min(static_cast<int32_t>(item.GetBindLevel()), userIdAndBindLevel[accesseeUserid]);
1480             }
1481         }
1482     }
1483     return userIdAndBindLevel;
1484 }
1485 
UpdateACL(std::string & localUdid,const std::vector<int32_t> & localUserIds,const std::string & remoteUdid,const std::vector<int32_t> & remoteFrontUserIds,const std::vector<int32_t> & remoteBackUserIds)1486 void DeviceProfileConnector::UpdateACL(std::string &localUdid, const std::vector<int32_t> &localUserIds,
1487     const std::string &remoteUdid, const std::vector<int32_t> &remoteFrontUserIds,
1488     const std::vector<int32_t> &remoteBackUserIds)
1489 {
1490     LOGI("localUdid %{public}s, remoteUdid %{public}s.", GetAnonyString(localUdid).c_str(),
1491         GetAnonyString(remoteUdid).c_str());
1492     std::vector<AccessControlProfile> profiles = GetAllAccessControlProfile();
1493     for (auto item : profiles) {
1494         // deleta signal trust acl.
1495         DeleteSigTrustACL(item, remoteUdid, remoteFrontUserIds, remoteBackUserIds);
1496         // update identical account userId.
1497         UpdatePeerUserId(item, localUdid, localUserIds, remoteUdid, remoteFrontUserIds);
1498     }
1499 }
1500 
DeleteSigTrustACL(AccessControlProfile profile,const std::string & remoteUdid,const std::vector<int32_t> & remoteFrontUserIds,const std::vector<int32_t> & remoteBackUserIds)1501 void DeviceProfileConnector::DeleteSigTrustACL(AccessControlProfile profile, const std::string &remoteUdid,
1502     const std::vector<int32_t> &remoteFrontUserIds, const std::vector<int32_t> &remoteBackUserIds)
1503 {
1504     LOGI("start.");
1505     std::string accesserUdid = profile.GetAccesser().GetAccesserDeviceId();
1506     std::string accesseeUdid = profile.GetAccessee().GetAccesseeDeviceId();
1507     int32_t accesserUserid = profile.GetAccesser().GetAccesserUserId();
1508     int32_t accesseeUserid = profile.GetAccessee().GetAccesseeUserId();
1509     if (accesserUdid == remoteUdid && accesserUserid != 0 && accesserUserid != -1 &&
1510         find(remoteFrontUserIds.begin(), remoteFrontUserIds.end(), accesserUserid) == remoteFrontUserIds.end() &&
1511         find(remoteBackUserIds.begin(), remoteBackUserIds.end(), accesserUserid) == remoteBackUserIds.end()) {
1512         DistributedDeviceProfileClient::GetInstance().DeleteAccessControlProfile(profile.GetAccessControlId());
1513         return;
1514     }
1515     if (accesseeUdid == remoteUdid && accesseeUserid != 0 && accesseeUserid != -1 &&
1516         find(remoteFrontUserIds.begin(), remoteFrontUserIds.end(), accesseeUserid) == remoteFrontUserIds.end() &&
1517         find(remoteBackUserIds.begin(), remoteBackUserIds.end(), accesseeUserid) == remoteBackUserIds.end()) {
1518         DistributedDeviceProfileClient::GetInstance().DeleteAccessControlProfile(profile.GetAccessControlId());
1519         return;
1520     }
1521 }
1522 
UpdatePeerUserId(AccessControlProfile profile,std::string & localUdid,const std::vector<int32_t> & localUserIds,const std::string & remoteUdid,const std::vector<int32_t> & remoteFrontUserIds)1523 void DeviceProfileConnector::UpdatePeerUserId(AccessControlProfile profile, std::string &localUdid,
1524     const std::vector<int32_t> &localUserIds, const std::string &remoteUdid,
1525     const std::vector<int32_t> &remoteFrontUserIds)
1526 {
1527     LOGI("start.");
1528     std::string accesserUdid = profile.GetAccesser().GetAccesserDeviceId();
1529     std::string accesseeUdid = profile.GetAccessee().GetAccesseeDeviceId();
1530     int32_t accesserUserid = profile.GetAccesser().GetAccesserUserId();
1531     int32_t accesseeUserid = profile.GetAccessee().GetAccesseeUserId();
1532     uint32_t bindType = profile.GetBindType();
1533     if (accesserUdid == localUdid && accesseeUdid == remoteUdid && bindType == DM_IDENTICAL_ACCOUNT &&
1534         find(localUserIds.begin(), localUserIds.end(), accesserUserid) != localUserIds.end() &&
1535         (accesseeUserid == 0 || accesseeUserid == -1)) {
1536         Accessee accessee = profile.GetAccessee();
1537         accessee.SetAccesseeUserId(remoteFrontUserIds[0]);
1538         profile.SetAccessee(accessee);
1539         DistributedDeviceProfileClient::GetInstance().UpdateAccessControlProfile(profile);
1540         return;
1541     }
1542 }
1543 
GetDevIdAndUserIdByActHash(const std::string & localUdid,const std::string & peerUdid,int32_t peerUserId,const std::string & peerAccountHash)1544 std::multimap<std::string, int32_t> DeviceProfileConnector::GetDevIdAndUserIdByActHash(const std::string &localUdid,
1545     const std::string &peerUdid, int32_t peerUserId, const std::string &peerAccountHash)
1546 {
1547     LOGI("localUdid %{public}s, peerUdid %{public}s, peerUserId %{public}d, peerAccountHash %{public}s.",
1548         GetAnonyString(localUdid).c_str(), GetAnonyString(peerUdid).c_str(), peerUserId, peerAccountHash.c_str());
1549     std::vector<AccessControlProfile> profiles = GetAllAccessControlProfile();
1550     std::multimap<std::string, int32_t> deviceIdMap;
1551     for (const auto &item : profiles) {
1552         std::string accesserAccountId = item.GetAccesser().GetAccesserAccountId();
1553         std::string accesseeAccountId = item.GetAccessee().GetAccesseeAccountId();
1554         char accesserAccountIdHash[DM_MAX_DEVICE_ID_LEN] = {0};
1555         if (Crypto::GetAccountIdHash(accesserAccountId, reinterpret_cast<uint8_t *>(accesserAccountIdHash)) != DM_OK) {
1556             LOGE("GetAccountHash failed.");
1557             return deviceIdMap;
1558         }
1559         char accesseeAccountIdHash[DM_MAX_DEVICE_ID_LEN] = {0};
1560         if (Crypto::GetAccountIdHash(accesseeAccountId, reinterpret_cast<uint8_t *>(accesseeAccountIdHash)) != DM_OK) {
1561             LOGE("GetAccountHash failed.");
1562             return deviceIdMap;
1563         }
1564         LOGI("accesserAccountIdHash %{public}s, accesseeAccountIdHash %{public}s", accesserAccountIdHash,
1565             accesseeAccountIdHash);
1566         if (std::string(accesserAccountIdHash) != peerAccountHash ||
1567             std::string(accesseeAccountIdHash) != peerAccountHash) {
1568             continue;
1569         }
1570         std::string accesserUdid = item.GetAccesser().GetAccesserDeviceId();
1571         std::string accesseeUdid = item.GetAccessee().GetAccesseeDeviceId();
1572         int32_t accesserUserid = item.GetAccesser().GetAccesserUserId();
1573         int32_t accesseeUserid = item.GetAccessee().GetAccesseeUserId();
1574         if (accesserUdid == localUdid && accesseeUdid == peerUdid && accesseeUserid == peerUserId) {
1575             deviceIdMap.insert(std::pair<std::string, int32_t>(accesserUdid, accesserUserid));
1576             continue;
1577         }
1578         if (accesseeUdid == localUdid && accesserUdid == peerUdid && accesserUserid == peerUserId) {
1579             deviceIdMap.insert(std::pair<std::string, int32_t>(accesseeUdid, accesseeUserid));
1580             continue;
1581         }
1582     }
1583     return deviceIdMap;
1584 }
1585 
GetDeviceIdAndUserId(const std::string & localUdid,int32_t localUserId)1586 std::multimap<std::string, int32_t> DeviceProfileConnector::GetDeviceIdAndUserId(const std::string &localUdid,
1587     int32_t localUserId)
1588 {
1589     LOGI("localUdid %{public}s, userId %{public}d.", GetAnonyString(localUdid).c_str(), localUserId);
1590     std::vector<AccessControlProfile> profiles = GetAllAccessControlProfile();
1591     std::multimap<std::string, int32_t> deviceIdMap;
1592     for (const auto &item : profiles) {
1593         std::string accesserDeviceId = item.GetAccesser().GetAccesserDeviceId();
1594         int32_t accesserUserId = item.GetAccesser().GetAccesserUserId();
1595         std::string accesseeDeviceId = item.GetAccessee().GetAccesseeDeviceId();
1596         int32_t accesseeUserId = item.GetAccessee().GetAccesseeUserId();
1597         if (accesserDeviceId == localUdid && accesserUserId == localUserId) {
1598             if (!IsValueExist(deviceIdMap, accesseeDeviceId, accesseeUserId)) {
1599                 deviceIdMap.insert(std::pair<std::string, int32_t>(accesseeDeviceId, accesseeUserId));
1600             }
1601             continue;
1602         }
1603         if (accesseeDeviceId == localUdid && accesseeUserId == localUserId) {
1604             if (!IsValueExist(deviceIdMap, accesserDeviceId, accesserUserId)) {
1605                 deviceIdMap.insert(std::pair<std::string, int32_t>(accesserDeviceId, accesserUserId));
1606             }
1607             continue;
1608         }
1609     }
1610     return deviceIdMap;
1611 }
1612 
HandleSyncBackgroundUserIdEvent(const std::vector<int32_t> & remoteUserIds,const std::string & remoteUdid,const std::vector<int32_t> & localUserIds,std::string & localUdid)1613 void DeviceProfileConnector::HandleSyncBackgroundUserIdEvent(const std::vector<int32_t> &remoteUserIds,
1614     const std::string &remoteUdid, const std::vector<int32_t> &localUserIds, std::string &localUdid)
1615 {
1616     LOGI("localUdid %{public}s, remoteUdid %{public}s.", GetAnonyString(localUdid).c_str(),
1617         GetAnonyString(remoteUdid).c_str());
1618     std::vector<AccessControlProfile> profiles = GetAllAccessControlProfile();
1619     for (auto &item : profiles) {
1620         std::string accesserDeviceId = item.GetAccesser().GetAccesserDeviceId();
1621         std::string accesseeDeviceId = item.GetAccessee().GetAccesseeDeviceId();
1622         int32_t accesserUserId = item.GetAccesser().GetAccesserUserId();
1623         int32_t accesseeUserId = item.GetAccessee().GetAccesseeUserId();
1624         if (accesserDeviceId == localUdid && accesseeDeviceId == remoteUdid && item.GetStatus() == ACTIVE &&
1625             (find(remoteUserIds.begin(), remoteUserIds.end(), accesseeUserId) != remoteUserIds.end() ||
1626             find(localUserIds.begin(), localUserIds.end(), accesserUserId) == localUserIds.end())) {
1627             item.SetStatus(INACTIVE);
1628             DistributedDeviceProfileClient::GetInstance().UpdateAccessControlProfile(item);
1629             if (item.GetBindType() != DM_IDENTICAL_ACCOUNT) {
1630                 DistributedDeviceProfileClient::GetInstance().DeleteAccessControlProfile(item.GetAccessControlId());
1631             }
1632         } else if ((accesseeDeviceId == localUdid && accesserDeviceId == remoteUdid) && item.GetStatus() == ACTIVE &&
1633             (find(remoteUserIds.begin(), remoteUserIds.end(), accesserUserId) != remoteUserIds.end() ||
1634             find(localUserIds.begin(), localUserIds.end(), accesseeUserId) == localUserIds.end())) {
1635             item.SetStatus(INACTIVE);
1636             DistributedDeviceProfileClient::GetInstance().UpdateAccessControlProfile(item);
1637             if (item.GetBindType() != DM_IDENTICAL_ACCOUNT) {
1638                 DistributedDeviceProfileClient::GetInstance().DeleteAccessControlProfile(item.GetAccessControlId());
1639             }
1640         }
1641     }
1642 }
1643 
HandleDeviceUnBind(int32_t bindType,const std::string & peerUdid,const std::string & localUdid,int32_t localUserId,const std::string & localAccountId)1644 void DeviceProfileConnector::HandleDeviceUnBind(int32_t bindType, const std::string &peerUdid,
1645     const std::string &localUdid, int32_t localUserId, const std::string &localAccountId)
1646 {
1647     std::vector<DistributedDeviceProfile::AccessControlProfile> profiles =
1648         DeviceProfileConnector::GetInstance().GetAllAccessControlProfile();
1649     if (profiles.empty()) {
1650         LOGI("profiles is empty");
1651         return;
1652     }
1653     for (auto &item : profiles) {
1654         if ((item.GetBindType() == static_cast<uint32_t>(bindType)) &&
1655             item.GetTrustDeviceId() == peerUdid &&
1656             item.GetAccesser().GetAccesserDeviceId() == localUdid &&
1657             item.GetAccesser().GetAccesserUserId() == localUserId &&
1658             item.GetAccesser().GetAccesserAccountId() == localAccountId) {
1659             DeviceProfileConnector::GetInstance().DeleteAccessControlById(item.GetAccessControlId());
1660         }
1661     }
1662 }
1663 
SubscribeDeviceProfileInited(sptr<DistributedDeviceProfile::IDpInitedCallback> dpInitedCallback)1664 int32_t DeviceProfileConnector::SubscribeDeviceProfileInited(
1665     sptr<DistributedDeviceProfile::IDpInitedCallback> dpInitedCallback)
1666 {
1667     LOGI("In");
1668     if (dpInitedCallback == nullptr) {
1669         LOGE("dpInitedCallback is nullptr");
1670         return ERR_DM_INPUT_PARA_INVALID;
1671     }
1672     int32_t ret = DistributedDeviceProfileClient::GetInstance().SubscribeDeviceProfileInited(
1673         DISTRIBUTED_HARDWARE_DEVICEMANAGER_SA_ID, dpInitedCallback);
1674     if (ret != DM_OK) {
1675         LOGE("failed: %{public}d", ret);
1676         return ret;
1677     }
1678     return DM_OK;
1679 }
1680 
UnSubscribeDeviceProfileInited()1681 int32_t DeviceProfileConnector::UnSubscribeDeviceProfileInited()
1682 {
1683     LOGI("In");
1684     int32_t ret = DistributedDeviceProfileClient::GetInstance().UnSubscribeDeviceProfileInited(
1685         DISTRIBUTED_HARDWARE_DEVICEMANAGER_SA_ID);
1686     if (ret != DM_OK) {
1687         LOGE("failed: %{public}d", ret);
1688         return ret;
1689     }
1690     return DM_OK;
1691 }
1692 
PutAllTrustedDevices(const std::vector<DistributedDeviceProfile::TrustedDeviceInfo> & deviceInfos)1693 int32_t DeviceProfileConnector::PutAllTrustedDevices(
1694     const std::vector<DistributedDeviceProfile::TrustedDeviceInfo> &deviceInfos)
1695 {
1696     LOGI("In deviceInfos.size:%{public}zu", deviceInfos.size());
1697     int32_t ret = DistributedDeviceProfileClient::GetInstance().PutAllTrustedDevices(deviceInfos);
1698     if (ret != DM_OK) {
1699         LOGE("failed: %{public}d", ret);
1700         return ret;
1701     }
1702     return DM_OK;
1703 }
1704 
CheckDeviceInfoPermission(const std::string & localUdid,const std::string & peerDeviceId)1705 int32_t DeviceProfileConnector::CheckDeviceInfoPermission(const std::string &localUdid,
1706     const std::string &peerDeviceId)
1707 {
1708     LOGI("CheckDeviceInfoPermission Start.");
1709     int32_t localUserId = 0;
1710     uint32_t tempLocalTokenId = 0;
1711     MultipleUserConnector::GetTokenIdAndForegroundUserId(tempLocalTokenId, localUserId);
1712     int64_t localTokenId = static_cast<int64_t>(tempLocalTokenId);
1713     std::string localAccountId = MultipleUserConnector::GetOhosAccountIdByUserId(localUserId);
1714     std::vector<AccessControlProfile> profiles = GetAccessControlProfileByUserId(localUserId);
1715     for (auto &item : profiles) {
1716         if (item.GetTrustDeviceId() == peerDeviceId) {
1717             if (item.GetBindType() == DM_IDENTICAL_ACCOUNT || item.GetBindLevel() == DEVICE) {
1718                 return DM_OK;
1719             }
1720         }
1721         int32_t profileUserId = item.GetAccesser().GetAccesserUserId();
1722         if (item.GetAccesser().GetAccesserDeviceId() == localUdid &&
1723             (profileUserId == localUserId || profileUserId == -1 || profileUserId == 0) &&
1724             item.GetAccesser().GetAccesserAccountId() == localAccountId &&
1725             item.GetAccesser().GetAccesserTokenId() == localTokenId &&
1726             item.GetAccessee().GetAccesseeDeviceId() == peerDeviceId) {
1727             return DM_OK;
1728         }
1729         profileUserId = item.GetAccessee().GetAccesseeUserId();
1730         if (item.GetAccessee().GetAccesseeDeviceId() == localUdid &&
1731             (profileUserId == localUserId || profileUserId == -1 || profileUserId == 0) &&
1732             item.GetAccessee().GetAccesseeAccountId() == localAccountId &&
1733             item.GetAccessee().GetAccesseeTokenId() == localTokenId &&
1734             item.GetAccesser().GetAccesserDeviceId() == peerDeviceId) {
1735             return DM_OK;
1736         }
1737     }
1738     return ERR_DM_NO_PERMISSION;
1739 }
1740 
UpdateAclDeviceName(const std::string & udid,const std::string & newDeviceName)1741 int32_t DeviceProfileConnector::UpdateAclDeviceName(const std::string &udid, const std::string &newDeviceName)
1742 {
1743     std::vector<AccessControlProfile> allProfile =
1744         DeviceProfileConnector::GetInstance().GetAllAccessControlProfile();
1745     for (AccessControlProfile profile : allProfile) {
1746         Accessee acee = profile.GetAccessee();
1747         if (acee.GetAccesseeDeviceId() == udid) {
1748             acee.SetAccesseeDeviceName(newDeviceName);
1749             profile.SetAccessee(acee);
1750             DistributedDeviceProfileClient::GetInstance().UpdateAccessControlProfile(profile);
1751             return DM_OK;
1752         }
1753         Accesser acer = profile.GetAccesser();
1754         if (acer.GetAccesserDeviceId() == udid) {
1755             acer.SetAccesserDeviceName(newDeviceName);
1756             profile.SetAccesser(acer);
1757             DistributedDeviceProfileClient::GetInstance().UpdateAccessControlProfile(profile);
1758             return DM_OK;
1759         }
1760     }
1761     return ERR_DM_FAILED;
1762 }
1763 
PutLocalServiceInfo(const DistributedDeviceProfile::LocalServiceInfo & localServiceInfo)1764 int32_t DeviceProfileConnector::PutLocalServiceInfo(
1765     const DistributedDeviceProfile::LocalServiceInfo &localServiceInfo)
1766 {
1767     int32_t ret = DistributedDeviceProfileClient::GetInstance().PutLocalServiceInfo(localServiceInfo);
1768     if (ret != DM_OK) {
1769         LOGE("failed: %{public}d", ret);
1770         return ret;
1771     }
1772     return DM_OK;
1773 }
1774 
DeleteLocalServiceInfo(const std::string & bundleName,int32_t pinExchangeType)1775 int32_t DeviceProfileConnector::DeleteLocalServiceInfo(const std::string &bundleName, int32_t pinExchangeType)
1776 {
1777     int32_t ret = DistributedDeviceProfileClient::GetInstance().DeleteLocalServiceInfo(bundleName, pinExchangeType);
1778     if (ret != DM_OK) {
1779         LOGE("failed: %{public}d", ret);
1780         return ret;
1781     }
1782     return DM_OK;
1783 }
1784 
UpdateLocalServiceInfo(const DistributedDeviceProfile::LocalServiceInfo & localServiceInfo)1785 int32_t DeviceProfileConnector::UpdateLocalServiceInfo(
1786     const DistributedDeviceProfile::LocalServiceInfo &localServiceInfo)
1787 {
1788     int32_t ret = DistributedDeviceProfileClient::GetInstance().UpdateLocalServiceInfo(localServiceInfo);
1789     if (ret != DM_OK) {
1790         LOGE("failed: %{public}d", ret);
1791         return ret;
1792     }
1793     return DM_OK;
1794 }
1795 
GetLocalServiceInfoByBundleNameAndPinExchangeType(const std::string & bundleName,int32_t pinExchangeType,DistributedDeviceProfile::LocalServiceInfo & localServiceInfo)1796 int32_t DeviceProfileConnector::GetLocalServiceInfoByBundleNameAndPinExchangeType(const std::string &bundleName,
1797     int32_t pinExchangeType, DistributedDeviceProfile::LocalServiceInfo &localServiceInfo)
1798 {
1799     int32_t ret = DistributedDeviceProfileClient::GetInstance().GetLocalServiceInfoByBundleAndPinType(bundleName,
1800         pinExchangeType, localServiceInfo);
1801     if (ret != DM_OK) {
1802         LOGE("failed: %{public}d", ret);
1803         return ret;
1804     }
1805     return DM_OK;
1806 }
1807 
PutSessionKey(const std::vector<unsigned char> & sessionKeyArray,int32_t & sessionKeyId)1808 int32_t DeviceProfileConnector::PutSessionKey(const std::vector<unsigned char> &sessionKeyArray, int32_t &sessionKeyId)
1809 {
1810     if (sessionKeyArray.empty() || sessionKeyArray.size() > MAX_SESSION_KEY_LENGTH) {
1811         LOGE("SessionKey size invalid");
1812         return ERR_DM_FAILED;
1813     }
1814     uint32_t userId = static_cast<uint32_t>(MultipleUserConnector::GetCurrentAccountUserID());
1815     int32_t ret = DistributedDeviceProfileClient::GetInstance().PutSessionKey(userId, sessionKeyArray, sessionKeyId);
1816     if (ret != DM_OK) {
1817         LOGE("failed: %{public}d", ret);
1818         return ret;
1819     }
1820     return DM_OK;
1821 }
1822 
CheckAclStatusNotMatch(const DistributedDeviceProfile::AccessControlProfile & profile,const std::string & localUdid,const std::vector<int32_t> & foregroundUserIds,const std::vector<int32_t> & backgroundUserIds)1823 bool DeviceProfileConnector::CheckAclStatusNotMatch(const DistributedDeviceProfile::AccessControlProfile &profile,
1824     const std::string &localUdid, const std::vector<int32_t> &foregroundUserIds,
1825     const std::vector<int32_t> &backgroundUserIds)
1826 {
1827     if ((profile.GetAccesser().GetAccesserDeviceId() == localUdid &&
1828         (find(backgroundUserIds.begin(), backgroundUserIds.end(), profile.GetAccesser().GetAccesserUserId()) !=
1829         backgroundUserIds.end()) && profile.GetStatus() == ACTIVE) ||
1830         (profile.GetAccessee().GetAccesseeDeviceId() == localUdid &&
1831         (find(backgroundUserIds.begin(), backgroundUserIds.end(), profile.GetAccessee().GetAccesseeUserId()) !=
1832         backgroundUserIds.end()) && profile.GetStatus() == ACTIVE)) {
1833         return true;
1834     }
1835     if ((profile.GetAccesser().GetAccesserDeviceId() == localUdid &&
1836         (find(foregroundUserIds.begin(), foregroundUserIds.end(), profile.GetAccesser().GetAccesserUserId()) !=
1837         foregroundUserIds.end()) && profile.GetStatus() == INACTIVE) ||
1838         (profile.GetAccessee().GetAccesseeDeviceId() == localUdid &&
1839         (find(foregroundUserIds.begin(), foregroundUserIds.end(), profile.GetAccessee().GetAccesseeUserId()) !=
1840         foregroundUserIds.end()) && profile.GetStatus() == INACTIVE)) {
1841         return true;
1842     }
1843     return false;
1844 }
1845 
CheckAclStatusAndForegroundNotMatch(const std::string & localUdid,const std::vector<int32_t> & foregroundUserIds,const std::vector<int32_t> & backgroundUserIds)1846 bool DeviceProfileConnector::CheckAclStatusAndForegroundNotMatch(const std::string &localUdid,
1847     const std::vector<int32_t> &foregroundUserIds, const std::vector<int32_t> &backgroundUserIds)
1848 {
1849     std::vector<AccessControlProfile> profiles = GetAllAccessControlProfile();
1850     LOGI("CheckAclStatusAndForegroundNotMatch profiles size is %{public}zu", profiles.size());
1851     for (auto &item : profiles) {
1852         if (CheckAclStatusNotMatch(item, localUdid, foregroundUserIds, backgroundUserIds)) {
1853             return true;
1854         }
1855     }
1856     return false;
1857 }
1858 
HandleUserSwitched(const std::string & localUdid,const std::vector<std::string> & deviceVec,const std::vector<int32_t> & foregroundUserIds,const std::vector<int32_t> & backgroundUserIds)1859 int32_t DeviceProfileConnector::HandleUserSwitched(const std::string &localUdid,
1860     const std::vector<std::string> &deviceVec, const std::vector<int32_t> &foregroundUserIds,
1861     const std::vector<int32_t> &backgroundUserIds)
1862 {
1863     LOGI("OnStart HandleUserSwitched");
1864     if (deviceVec.empty()) {
1865         LOGI("no remote device.");
1866         return DM_OK;
1867     }
1868     std::vector<AccessControlProfile> profiles = GetAllAccessControlProfile();
1869     std::vector<AccessControlProfile> activeProfiles;
1870     std::vector<AccessControlProfile> inActiveProfiles;
1871     for (auto &item : profiles) {
1872         if (std::find(deviceVec.begin(), deviceVec.end(), item.GetTrustDeviceId()) == deviceVec.end()) {
1873             continue;
1874         }
1875         if ((item.GetAccesser().GetAccesserDeviceId() == localUdid &&
1876             (find(backgroundUserIds.begin(), backgroundUserIds.end(),
1877             item.GetAccesser().GetAccesserUserId()) != backgroundUserIds.end()) && item.GetStatus() == ACTIVE) ||
1878             (item.GetAccessee().GetAccesseeDeviceId() == localUdid &&
1879             (find(backgroundUserIds.begin(), backgroundUserIds.end(),
1880             item.GetAccessee().GetAccesseeUserId()) != backgroundUserIds.end()) && item.GetStatus() == ACTIVE)) {
1881             item.SetStatus(INACTIVE);
1882             inActiveProfiles.push_back(item);
1883             continue;
1884         }
1885         if ((item.GetAccesser().GetAccesserDeviceId() == localUdid &&
1886             (find(foregroundUserIds.begin(), foregroundUserIds.end(),
1887             item.GetAccesser().GetAccesserUserId()) != foregroundUserIds.end()) && item.GetStatus() == INACTIVE) || (
1888             item.GetAccessee().GetAccesseeDeviceId() == localUdid &&
1889             (find(foregroundUserIds.begin(), foregroundUserIds.end(),
1890             item.GetAccessee().GetAccesseeUserId()) != foregroundUserIds.end()) && item.GetStatus() == INACTIVE)) {
1891             item.SetStatus(ACTIVE);
1892             activeProfiles.push_back(item);
1893             continue;
1894         }
1895     }
1896     for (auto &item : inActiveProfiles) {
1897         DistributedDeviceProfileClient::GetInstance().UpdateAccessControlProfile(item);
1898         if (item.GetBindType() != DM_IDENTICAL_ACCOUNT) {
1899             DistributedDeviceProfileClient::GetInstance().DeleteAccessControlProfile(item.GetAccessControlId());
1900         }
1901     }
1902     for (auto &item : activeProfiles) {
1903         DistributedDeviceProfileClient::GetInstance().UpdateAccessControlProfile(item);
1904     }
1905     return DM_OK;
1906 }
1907 
HandleUserStop(int32_t stopUserId,const std::string & stopEventUdid)1908 int32_t DeviceProfileConnector::HandleUserStop(int32_t stopUserId, const std::string &stopEventUdid)
1909 {
1910     std::vector<AccessControlProfile> profiles = GetAllAccessControlProfile();
1911     for (auto &item : profiles) {
1912         if ((item.GetAccesser().GetAccesserDeviceId() == stopEventUdid &&
1913             item.GetAccesser().GetAccesserUserId() == stopUserId && item.GetStatus() == ACTIVE) ||
1914             (item.GetAccessee().GetAccesseeDeviceId() == stopEventUdid &&
1915             item.GetAccessee().GetAccesseeUserId() == stopUserId && item.GetStatus() == ACTIVE)) {
1916             item.SetStatus(INACTIVE);
1917             DistributedDeviceProfileClient::GetInstance().UpdateAccessControlProfile(item);
1918         }
1919     }
1920     return DM_OK;
1921 }
1922 
HandleUserStop(int32_t stopUserId,const std::string & stopEventUdid,const std::vector<std::string> & acceptEventUdids)1923 int32_t DeviceProfileConnector::HandleUserStop(int32_t stopUserId, const std::string &stopEventUdid,
1924     const std::vector<std::string> &acceptEventUdids)
1925 {
1926     if (acceptEventUdids.empty()) {
1927         LOGI("no remote device.");
1928         return DM_OK;
1929     }
1930     std::vector<AccessControlProfile> profiles = GetAllAccessControlProfile();
1931     for (auto &item : profiles) {
1932         if (std::find(acceptEventUdids.begin(), acceptEventUdids.end(), item.GetTrustDeviceId()) ==
1933             acceptEventUdids.end()) {
1934             continue;
1935         }
1936         if ((item.GetAccesser().GetAccesserDeviceId() == stopEventUdid &&
1937             item.GetAccesser().GetAccesserUserId() == stopUserId && item.GetStatus() == ACTIVE) ||
1938             (item.GetAccessee().GetAccesseeDeviceId() == stopEventUdid &&
1939             item.GetAccessee().GetAccesseeUserId() == stopUserId && item.GetStatus() == ACTIVE)) {
1940             item.SetStatus(INACTIVE);
1941             DistributedDeviceProfileClient::GetInstance().UpdateAccessControlProfile(item);
1942         }
1943     }
1944     return DM_OK;
1945 }
1946 
CreateDpConnectorInstance()1947 IDeviceProfileConnector *CreateDpConnectorInstance()
1948 {
1949     return &DeviceProfileConnector::GetInstance();
1950 }
1951 } // namespace DistributedHardware
1952 } // namespace OHOS
1953