diff --git a/commondependency/include/deviceprofile_connector.h b/commondependency/include/deviceprofile_connector.h index c573d6822719414dacdea851ff6a91568f83db3b..ea07716dc7598700e95a5af095d0fed1f5618708 100644 --- a/commondependency/include/deviceprofile_connector.h +++ b/commondependency/include/deviceprofile_connector.h @@ -155,6 +155,10 @@ public: void DeleteAclForAccountLogOut(const std::string &localUdid, int32_t userId, const std::string &remoteUdid); std::map GetDeviceIdAndBindType(int32_t userId, const std::string &accountId, const std::string &localUdid); + std::vector GetAllAccessControlProfile(); + void DeleteAccessControlById(int64_t accessControlId); + void HandleDeviceUnBind(int32_t bindType, const std::string &peerUdid, + const std::string &localUdid, int32_t localUserId, const std::string &localAccountId); private: int32_t HandleDmAuthForm(DistributedDeviceProfile::AccessControlProfile profiles, DmDiscoveryInfo discoveryInfo); void GetParamBindTypeVec(DistributedDeviceProfile::AccessControlProfile profiles, std::string pkgName, diff --git a/commondependency/src/deviceprofile_connector.cpp b/commondependency/src/deviceprofile_connector.cpp index e50a64d1d94e875021a014a01cd3c7f68d700cf3..f28cc244af1fa6a7e053702c16f4ea9245e53e3c 100644 --- a/commondependency/src/deviceprofile_connector.cpp +++ b/commondependency/src/deviceprofile_connector.cpp @@ -532,6 +532,40 @@ std::map DeviceProfileConnector::GetDeviceIdAndBindType(in return deviceIdMap; } +std::vector DeviceProfileConnector::GetAllAccessControlProfile() +{ + std::vector profiles; + if (DistributedDeviceProfileClient::GetInstance().GetAllAccessControlProfile(profiles) != DM_OK) { + LOGE("DP failed."); + } + return profiles; +} + +void DeviceProfileConnector::DeleteAccessControlById(int64_t accessControlId) +{ + DistributedDeviceProfileClient::GetInstance().DeleteAccessControlProfile(accessControlId); +} + +void DeviceProfileConnector::HandleDeviceUnBind(int32_t bindType, const std::string &peerUdid, + const std::string &localUdid, int32_t localUserId, const std::string &localAccountId) +{ + std::vector profiles = + DeviceProfileConnector::GetInstance().GetAllAccessControlProfile(); + if (profiles.empty()) { + LOGI("profiles is empty"); + return; + } + for (auto &item : profiles) { + if (item.GetBindType() == bindType && + item.GetTrustDeviceId() == peerUdid && + item.GetAccesser().GetAccesserDeviceId() == localUdid && + item.GetAccesser().GetAccesserUserId() == localUserId && + item.GetAccesser().GetAccesserAccountId() == localAccountId) { + DeviceProfileConnector::GetInstance().DeleteAccessControlById(item.GetAccessControlId()); + } + } +} + void DeviceProfileConnector::UpdateBindType(const std::string &udid, int32_t bindType, std::map &deviceMap) { diff --git a/interfaces/inner_kits/native_cpp/src/ipc/standard/ipc_client_manager.cpp b/interfaces/inner_kits/native_cpp/src/ipc/standard/ipc_client_manager.cpp index 4dbe079ba6ecf4d1407024606c94611a80529f7c..1c643054567bdbcf5896ac02920156709afea10f 100644 --- a/interfaces/inner_kits/native_cpp/src/ipc/standard/ipc_client_manager.cpp +++ b/interfaces/inner_kits/native_cpp/src/ipc/standard/ipc_client_manager.cpp @@ -132,7 +132,6 @@ int32_t IpcClientManager::UnInit(const std::string &pkgName) if (dmListener_.empty()) { if (dmRecipient_ != nullptr) { dmInterface_->AsObject()->RemoveDeathRecipient(dmRecipient_); - dmRecipient_ = nullptr; } dmInterface_ = nullptr; } diff --git a/services/implementation/include/device_manager_service_impl.h b/services/implementation/include/device_manager_service_impl.h index 078ce4f3f0a4c6d6acc603884698fed0b7ac7861..ead505bce26098553d10480e8d1d0a92717bb367 100644 --- a/services/implementation/include/device_manager_service_impl.h +++ b/services/implementation/include/device_manager_service_impl.h @@ -142,6 +142,8 @@ public: void HandleDeviceScreenStatusChange(DmDeviceInfo &devInfo); void HandleCredentialAuthStatus(const std::string &proofInfo, uint16_t deviceTypeId, int32_t errcode); int32_t SaveOnlineDeviceInfo(const std::vector &deviceList); + void HandleDeviceUnBind(int32_t bindType, const std::string &peerUdid, + const std::string &localUdid, int32_t localUserId, const std::string &localAccountId); private: int32_t PraseNotifyEventJson(const std::string &event, nlohmann::json &jsonObject); std::string GetUdidHashByNetworkId(const std::string &networkId); diff --git a/services/implementation/include/device_manager_service_impl_lite.h b/services/implementation/include/device_manager_service_impl_lite.h index 1149c539fbd9836403f30ad1fa4a930924aaa3da..8377e3561caa9549133b11730a434e2e87ee5900 100644 --- a/services/implementation/include/device_manager_service_impl_lite.h +++ b/services/implementation/include/device_manager_service_impl_lite.h @@ -143,6 +143,8 @@ public: void HandleCredentialAuthStatus(const std::string &proofInfo, uint16_t deviceTypeId, int32_t errcode); int32_t SaveOnlineDeviceInfo(const std::vector &deviceList); + void HandleDeviceUnBind(int32_t bindType, const std::string &peerUdid, const std::string &localUdid, + int32_t localUserId, const std::string &localAccountId); private: std::string GetUdidHashByNetworkId(const std::string &networkId); diff --git a/services/implementation/src/device_manager_service_impl.cpp b/services/implementation/src/device_manager_service_impl.cpp index cc0c19aac64aa80ea02cea7d849e0c524c7d0044..903510a9bd5c50a22b998b48f9b03f551a017e78 100644 --- a/services/implementation/src/device_manager_service_impl.cpp +++ b/services/implementation/src/device_manager_service_impl.cpp @@ -839,6 +839,13 @@ int32_t DeviceManagerServiceImpl::SaveOnlineDeviceInfo(const std::vector advertiseMgr_; std::shared_ptr discoveryMgr_; std::shared_ptr softbusListener_; + std::shared_ptr hichainListener_; std::shared_ptr listener_; std::shared_ptr dmServiceImpl_; std::shared_ptr dmServiceImplExt_; diff --git a/services/service/include/hichain/hichain_listener.h b/services/service/include/hichain/hichain_listener.h new file mode 100644 index 0000000000000000000000000000000000000000..0adb7b0c6cc125d635481a063b548a21e6972f8d --- /dev/null +++ b/services/service/include/hichain/hichain_listener.h @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_DM_HICHAIN_LISTENER_H +#define OHOS_DM_HICHAIN_LISTENER_H + +#include + +#include "device_auth.h" +#include "nlohmann/json.hpp" + +namespace OHOS { +namespace DistributedHardware { + +struct GroupInformation { + std::string groupName; + std::string groupId; + std::string groupOwner; + int32_t groupType; + int32_t groupVisibility; + int32_t userId; + std::string osAccountId; + + GroupInformation() : groupName(""), groupId(""), groupOwner(""), groupType(0), + groupVisibility(0), userId(0), osAccountId("") { + } +}; + +void from_json(const nlohmann::json &jsonObject, GroupInformation &groupInfo); + +class HichainListener { +public: + HichainListener(); + ~HichainListener(); + void RegisterDataChangeCb(); + static void OnHichainDeviceUnBound(const char *peerUdid, const char *groupInfo); + +private: + const DeviceGroupManager *deviceGroupManager_ = nullptr; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DM_HICHAIN_LISTENER_H \ No newline at end of file diff --git a/services/service/include/idevice_manager_service_impl.h b/services/service/include/idevice_manager_service_impl.h index dfdbad6880644925a8cbcf2dcdcbf46f75e2df95..0cc0d388b0ddd17df427320fc48686e034bb5855 100644 --- a/services/service/include/idevice_manager_service_impl.h +++ b/services/service/include/idevice_manager_service_impl.h @@ -290,6 +290,8 @@ public: virtual void HandleCredentialAuthStatus(const std::string &proofInfo, uint16_t deviceTypeId, int32_t errcode) = 0; virtual int32_t SaveOnlineDeviceInfo(const std::vector &deviceList) = 0; + virtual void HandleDeviceUnBind(int32_t bindType, const std::string &peerUdid, + const std::string &localUdid, int32_t localUserId, const std::string &localAccountId) = 0; }; using CreateDMServiceFuncPtr = IDeviceManagerServiceImpl *(*)(void); diff --git a/services/service/src/device_manager_service.cpp b/services/service/src/device_manager_service.cpp index 8c287727c2e50fa864bbb09ec02c02a0c9b9d6e3..777879cb960b55f172c973d1c9b24a380326b8f0 100755 --- a/services/service/src/device_manager_service.cpp +++ b/services/service/src/device_manager_service.cpp @@ -100,6 +100,15 @@ int32_t DeviceManagerService::InitSoftbusListener() return DM_OK; } +void DeviceManagerService::InitHichainListener() +{ + LOGI("DeviceManagerService::InitHichainListener Start."); + if (hichainListener_ == nullptr) { + hichainListener_ = std::make_shared(); + } + hichainListener_->RegisterDataChangeCb(); +} + #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) #if defined(SUPPORT_BLUETOOTH) || defined(SUPPORT_WIFI) void DeviceManagerService::SubscribePublishCommonEvent() @@ -1844,5 +1853,18 @@ void DeviceManagerService::HandleCredentialAuthStatus(const std::string &proofIn dmServiceImpl_->HandleCredentialAuthStatus(proofInfo, deviceTypeId, errcode); } } + +void DeviceManagerService::HandleDeviceUnBind(const char *peerUdid, const GroupInformation &groupInfo) +{ + LOGI("DeviceManagerService::HandleDeviceUnBind start."); + char localUdidTemp[DEVICE_UUID_LENGTH] = {0}; + GetDevUdid(localUdidTemp, DEVICE_UUID_LENGTH); + std::string localUdid = std::string(localUdidTemp); + if (IsDMServiceImplReady()) { + dmServiceImpl_->HandleDeviceUnBind(groupInfo.groupType, std::string(peerUdid), + localUdid, groupInfo.userId, groupInfo.osAccountId); + } + return; +} } // namespace DistributedHardware } // namespace OHOS \ No newline at end of file diff --git a/services/service/src/hichain/hichain_listener.cpp b/services/service/src/hichain/hichain_listener.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5c4cfba90854399408f16ae11affadb0ebac875e --- /dev/null +++ b/services/service/src/hichain/hichain_listener.cpp @@ -0,0 +1,110 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "hichain_listener.h" + +#include "device_manager_service.h" +#include "dm_anonymous.h" +#include "dm_constants.h" +#include "dm_log.h" +#include "multiple_user_connector.h" + +namespace OHOS { +namespace DistributedHardware { + +namespace { + constexpr uint32_t MAX_DATA_LEN = 65536; + constexpr uint32_t DM_IDENTICAL_ACCOUNT = 1; +} + +static DataChangeListener dataChangeListener_ = { + .onDeviceUnBound = HichainListener::OnHichainDeviceUnBound, +}; + +void from_json(const nlohmann::json &jsonObject, GroupInformation &groupInfo) +{ + if (jsonObject.find(FIELD_GROUP_TYPE) != jsonObject.end() && jsonObject.at(FIELD_GROUP_TYPE).is_number_integer()) { + groupInfo.groupType = jsonObject.at(FIELD_GROUP_TYPE).get(); + } + // FIELD_USER_ID is osAccountId + if (jsonObject.find(FIELD_USER_ID) != jsonObject.end() && jsonObject.at(FIELD_USER_ID).is_string()) { + groupInfo.osAccountId = jsonObject.at(FIELD_USER_ID).get(); + } + // FIELD_OS_ACCOUNT_ID is userId + if (jsonObject.find(FIELD_OS_ACCOUNT_ID) != jsonObject.end() && + jsonObject.at(FIELD_OS_ACCOUNT_ID).is_number_integer()) { + groupInfo.userId = jsonObject.at(FIELD_OS_ACCOUNT_ID).get(); + } +} + +HichainListener::HichainListener() +{ + LOGI("HichainListener constructor start."); + InitDeviceAuthService(); + deviceGroupManager_ = GetGmInstance(); + if (deviceGroupManager_ == nullptr) { + LOGE("[HICHAIN]failed to init group manager."); + return; + } + LOGI("HichainListener::constructor success."); +} + +HichainListener::~HichainListener() +{ + LOGI("HichainListener::destructor."); +} + +void HichainListener::RegisterDataChangeCb() +{ + LOGI("HichainListener::RegisterDataChangeCb start"); + if (deviceGroupManager_ == nullptr) { + LOGE("deviceGroupManager_ is null!"); + return; + } + int32_t ret = deviceGroupManager_->regDataChangeListener(DM_PKG_NAME, &dataChangeListener_); + if (ret != DM_OK) { + LOGE("[HICHAIN]regDataChangeListener failed with ret: %{public}d.", ret); + return; + } + LOGI("RegisterDataChangeCb success!"); +} + +void HichainListener::OnHichainDeviceUnBound(const char *peerUdid, const char *groupInfo) +{ + LOGI("HichainListener::onDeviceUnBound start"); + if (peerUdid == nullptr || groupInfo == nullptr) { + LOGE("peerUdid or groupInfo is null!"); + return; + } + if (strlen(peerUdid) > MAX_DATA_LEN || strlen(groupInfo) > MAX_DATA_LEN) { + LOGE("peerUdid or groupInfo is invalid"); + return; + } + nlohmann::json groupInfoJsonObj = nlohmann::json::parse(std::string(groupInfo), nullptr, false); + GroupInformation hichainGroupInfo; + from_json(groupInfoJsonObj, hichainGroupInfo); + if (hichainGroupInfo.groupType != DM_IDENTICAL_ACCOUNT) { + LOGI("groupType is %{public}d, not idential account.", hichainGroupInfo.groupType); + return; + } + string accountId = MultipleUserConnector::GetOhosAccountId(); + if (accountId == hichainGroupInfo.osAccountId && accountId != "ohosAnonymousUid") { + LOGI("accountId = %{public}s.", GetAnonyString(accountId).c_str()); + DeviceManagerService::GetInstance().HandleDeviceUnBind(peerUdid, hichainGroupInfo); + return; + } +} +} // namespace DistributedHardware +} // namespace OHOS \ No newline at end of file diff --git a/services/service/src/ipc/standard/ipc_server_stub.cpp b/services/service/src/ipc/standard/ipc_server_stub.cpp index 691eb903c244142b3be075e3cfca8a391767fa33..a31516b7cbff03d4296c7ea3b4bcbb061fb95662 100644 --- a/services/service/src/ipc/standard/ipc_server_stub.cpp +++ b/services/service/src/ipc/standard/ipc_server_stub.cpp @@ -70,6 +70,8 @@ void IpcServerStub::OnStart() #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) AddSystemAbilityListener(DISTRIBUTED_KV_DATA_SERVICE_ABILITY_ID); #endif + AddSystemAbilityListener(DEVICE_AUTH_SERVICE_ID); + AddSystemAbilityListener(ACCESS_TOKEN_MANAGER_SERVICE_ID); DeviceManagerService::GetInstance().SubscribePackageCommonEvent(); } @@ -111,6 +113,14 @@ void IpcServerStub::OnAddSystemAbility(int32_t systemAbilityId, const std::strin return; } #endif + if (systemAbilityId == DEVICE_AUTH_SERVICE_ID) { + DeviceManagerService::GetInstance().InitHichainListener(); + return; + } + if (systemAbilityId == ACCESS_TOKEN_MANAGER_SERVICE_ID) { + DeviceManagerService::GetInstance().InitHichainListener(); + return; + } } void IpcServerStub::OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId)