diff --git a/common/include/dm_error_type.h b/common/include/dm_error_type.h index 0bca1de1bbf53d8f18f418ade87cc96e8c8fb410..722e9b2c8c646b3aeeb200fbc7efea9936bde22e 100644 --- a/common/include/dm_error_type.h +++ b/common/include/dm_error_type.h @@ -145,6 +145,7 @@ enum { ERR_DM_SOCKET_IN_USED = 969298356, ERR_DM_ANTI_DISTURB_MODE = 969298357, ERR_DM_SKIP_AUTHENTICATE = 969298358, + ERR_DM_SERVICE_BIND_PEER_SERVICE_ID_UNPUBLISH = 969298359, }; } // namespace DistributedHardware } // namespace OHOS diff --git a/interfaces/inner_kits/native_cpp/src/device_manager_impl.cpp b/interfaces/inner_kits/native_cpp/src/device_manager_impl.cpp index 6b49cf0b07d61355fcad7d256a168facb45276fa..9c46d2e43b062cd87441dd06c6dd699353c42fb0 100644 --- a/interfaces/inner_kits/native_cpp/src/device_manager_impl.cpp +++ b/interfaces/inner_kits/native_cpp/src/device_manager_impl.cpp @@ -3118,6 +3118,10 @@ int32_t DeviceManagerImpl::UnbindServiceTarget(const std::string &pkgName, int64 int32_t DeviceManagerImpl::RegisterServiceStateCallback(const std::string &pkgName, int64_t serviceId, std::shared_ptr callback) { + if (pkgName.empty() || serviceId == 0) { + LOGE("RegisterServiceStateCallback failed: input pkgName or serviceId is empty."); + return ERR_DM_INPUT_PARA_INVALID; + } std::string key = std::to_string(serviceId); int32_t ret = DeviceManagerNotify::GetInstance().RegisterServiceStateCallback(key, callback); if (ret != DM_OK) { @@ -3130,6 +3134,10 @@ int32_t DeviceManagerImpl::RegisterServiceStateCallback(const std::string &pkgNa int32_t DeviceManagerImpl::UnRegisterServiceStateCallback(const std::string &pkgName, int64_t serviceId) { + if (pkgName.empty() || serviceId == 0) { + LOGE("UnRegisterServiceStateCallback failed: input pkgName or serviceId is empty."); + return ERR_DM_INPUT_PARA_INVALID; + } std::string key = std::to_string(serviceId); int32_t ret = DeviceManagerNotify::GetInstance().UnRegisterServiceStateCallback(key); if (ret != DM_OK) { @@ -3145,7 +3153,7 @@ int32_t DeviceManagerImpl::StartPublishService(const std::string &pkgName, std::shared_ptr callback, int64_t &serviceId) { if (pkgName.empty() || callback == nullptr) { - LOGE("DeviceManagerImpl::StartPublishService error: pkgName %{public}s invalid para", pkgName.c_str()); + LOGE("DeviceManagerImpl::StartPublishService error: pkgName or callback is empty."); return ERR_DM_INPUT_PARA_INVALID; } std::shared_ptr req = std::make_shared(); @@ -3159,6 +3167,10 @@ int32_t DeviceManagerImpl::StartPublishService(const std::string &pkgName, return ERR_DM_IPC_SEND_REQUEST_FAILED; } ret = rsp->GetErrCode(); + if (ret != DM_OK) { + LOGE("error: Failed with ret %{public}d", ret); + return ret; + } serviceId = rsp->GetServiceId(); DeviceManagerNotify::GetInstance().RegisterServicePublishCallback(serviceId, callback); LOGI("StartPublishService End"); @@ -3167,6 +3179,10 @@ int32_t DeviceManagerImpl::StartPublishService(const std::string &pkgName, int32_t DeviceManagerImpl::StopPublishService(int64_t serviceId) { + if (serviceId == 0) { + LOGE("DeviceManagerImpl::StopPublishService error: service id is empty."); + return ERR_DM_INPUT_PARA_INVALID; + } std::string key = std::to_string(serviceId); std::shared_ptr req = std::make_shared(); std::shared_ptr rsp = std::make_shared(); @@ -3188,6 +3204,11 @@ int32_t DeviceManagerImpl::StopPublishService(int64_t serviceId) int32_t DeviceManagerImpl::RegisterServiceInfo(const ServiceRegInfo &serviceInfo, int32_t ®ServiceId) { + if (serviceInfo.serviceInfo.serviceType.empty() || serviceInfo.serviceInfo.serviceName.empty() || + serviceInfo.serviceInfo.serviceDisplayName.empty()) { + LOGE("DeviceManagerImpl::RegisterServiceInfo error: service name or type or display name is empty."); + return ERR_DM_INPUT_PARA_INVALID; + } std::shared_ptr req = std::make_shared(); std::shared_ptr rsp = std::make_shared(); req->SetServiceRegInfo(serviceInfo); @@ -3209,6 +3230,10 @@ int32_t DeviceManagerImpl::RegisterServiceInfo(const ServiceRegInfo &serviceInfo int32_t DeviceManagerImpl::UnRegisterServiceInfo(int32_t regServiceId) { + if (regServiceId == 0) { + LOGE("DeviceManagerImpl::UnRegisterServiceInfo error: regServiceId is empty."); + return ERR_DM_INPUT_PARA_INVALID; + } std::shared_ptr req = std::make_shared(); std::shared_ptr rsp = std::make_shared(); req->SetRegServiceId(regServiceId); diff --git a/services/implementation/include/authentication_v2/dm_auth_state.h b/services/implementation/include/authentication_v2/dm_auth_state.h index 4a241b935c0c3498ef23ee48a9157a6248f2d1a5..fa060f7d78cfa7f19468b44c311a6354920e1b9f 100644 --- a/services/implementation/include/authentication_v2/dm_auth_state.h +++ b/services/implementation/include/authentication_v2/dm_auth_state.h @@ -553,6 +553,7 @@ private: bool IsAntiDisturbanceMode(const std::string &businessId); bool ParseAndCheckAntiDisturbanceMode(const std::string &businessId, const std::string &businessValue); void SetIsProxyBind(std::shared_ptr context); + int32_t SinkNegotiateService(std::shared_ptr context); }; class AuthSinkDataSyncState : public DmAuthState { diff --git a/services/implementation/include/device_manager_service_impl.h b/services/implementation/include/device_manager_service_impl.h index 532d0ca2248ee939c0ee627495c7488108300d8f..e4f7686b9650bc895a691bbd1cb022503165daea 100644 --- a/services/implementation/include/device_manager_service_impl.h +++ b/services/implementation/include/device_manager_service_impl.h @@ -307,7 +307,8 @@ private: const std::string &deviceId, int32_t reason, uint64_t tokenId); void GetBundleName(const DMAclQuadInfo &info, std::set &pkgNameSet); void DeleteSessionKey(int32_t userId, const DistributedDeviceProfile::AccessControlProfile &profile); - int32_t DeleteAclExtraDataServiceId(int64_t serviceId); + int32_t DeleteAclExtraDataServiceId(int64_t serviceId, int64_t tokenIdCaller, std::string &udid, + int32_t &bindLevel); private: std::mutex authMgrMtx_; std::shared_ptr authMgr_; // Old protocol only diff --git a/services/implementation/src/authentication_v2/auth_stages/auth_negotiate.cpp b/services/implementation/src/authentication_v2/auth_stages/auth_negotiate.cpp index dcdb29027852f062fc13e2a7e945ac9439613596..424e006c1a4024ce11dcf9a9dfd09232371aa5be 100644 --- a/services/implementation/src/authentication_v2/auth_stages/auth_negotiate.cpp +++ b/services/implementation/src/authentication_v2/auth_stages/auth_negotiate.cpp @@ -53,6 +53,7 @@ namespace { const char* DM_ANTI_DISTURBANCE_MODE = "is_in_anti_disturbance_mode"; const int64_t DM_MIN_RANDOM = 1; const int64_t DM_MAX_RANDOM_INT64 = INT64_MAX; + constexpr int8_t SERVICE_UNPUBLISHED_STATE = 0; } DmAuthStateType AuthSrcStartState::GetStateType() @@ -209,6 +210,31 @@ int32_t AuthSinkNegotiateStateMachine::ProcRespNegotiate5_1_0(std::shared_ptraccessee.credTypeList = credTypeJson.Dump(); + if (context->isServiceBind) { + ret = SinkNegotiateService(context); + return ret; + } + return DM_OK; +} + +int32_t AuthSinkNegotiateStateMachine::SinkNegotiateService(std::shared_ptr context) +{ + CHECK_NULL_RETURN(context, ERR_DM_POINT_NULL); + int64_t serviceId = context.accessee.serviceId; + if (serviceId == 0) { + LOGE("service id invalid."); + return ERR_DM_INPUT_PARA_INVALID; + } + ServiceInfoProfile serviceInfoProfile; + int32_t ret = DeviceProfileConnector::GetInstance().GetServiceInfoProfileByServiceId(serviceId); + if (ret != DM_OK) { + LOGE("GetServiceInfoProfileByServiceId failed, ret %{public}d.", ret); + return ret; + } + if (serviceInfoProfile.publishState == SERVICE_UNPUBLISHED_STATE) { + LOGE("service id not publish."); + return ERR_DM_SERVICE_BIND_PEER_SERVICE_ID_UNPUBLISH; + } return DM_OK; } diff --git a/services/implementation/src/device_manager_service_impl.cpp b/services/implementation/src/device_manager_service_impl.cpp index 96e3b3aad23bd17897b78e0339c7743d4ccce15b..0daf168e17313f3e59813ed6fb2185c854299e33 100644 --- a/services/implementation/src/device_manager_service_impl.cpp +++ b/services/implementation/src/device_manager_service_impl.cpp @@ -3238,8 +3238,10 @@ int32_t DeviceManagerServiceImpl::UnbindServiceTarget(const std::string &pkgName LOGE("UnbindServiceTarget failed, pkgName is empty."); return ERR_DM_INPUT_PARA_INVALID; } - - int32_t ret = DeleteAclExtraDataServiceId(serviceId); + int64_t tokenIdCaller = IPCSkeleton::GetCallingTokenID(); + std::string peerDeviceId = ""; + int32_t bindLevel = -1; + int32_t ret = DeleteAclExtraDataServiceId(serviceId, tokenIdCaller, peerDeviceId, bindLevel); if (ret != DM_OK) { LOGE("UnbindServiceTarget failed, DeleteAclExtraDataServiceId failed."); return ret; @@ -3258,10 +3260,16 @@ int32_t DeviceManagerServiceImpl::UnbindServiceTarget(const std::string &pkgName LOGE("UnbindServiceTarget failed, DeleteServiceInfoProfile failed."); return ret; } + ret = UnBindDevice(pkgName, peerDeviceId, bindLevel); + if (ret != DM_OK) { + LOGE("UnbindServiceTarget failed, UnBindDevice failed."); + return ret; + } return DM_OK; } -int32_t DeviceManagerServiceImpl::DeleteAclExtraDataServiceId(int64_t serviceId) +int32_t DeviceManagerServiceImpl::DeleteAclExtraDataServiceId(int64_t serviceId, int64_t tokenIdCaller, + std::string &udid, int32_t &bindLevel) { bool isDeletedExtra = false; char localDeviceIdTemp[DEVICE_UUID_LENGTH] = {0}; @@ -3286,11 +3294,13 @@ int32_t DeviceManagerServiceImpl::DeleteAclExtraDataServiceId(int64_t serviceId) } } std::string accesserUdid = item.GetAccesser().GetAccesserDeviceId(); - int32_t accesserUserId = item.GetAccesser().GetAccesserUserId(); - if (accesserUdid == localDeviceId) { + int64_t tokenIdAcl = item.GetAccesser().GetAccesserTokenId(); + if (accesserUdid == localDeviceId && tokenIdCaller == tokenIdAcl) { isDeletedExtra = true; json.Erase(TAG_SERVICE_ID); item.SetExtraData(json.Dump()); + udid = item.GetAccessee().GetAccesseeDeviceId(); + bindLevel = item.GetBindLevel(); DistributedDeviceProfile::DistributedDeviceProfileClient::GetInstance().UpdateAccessControlProfile(item); } } diff --git a/services/service/include/device_manager_service.h b/services/service/include/device_manager_service.h index cdeaf2919e10f509130e4535c5026a9a7bbd046b..bfdc8878c5ce598d6d16902ba38b983f722872a7 100644 --- a/services/service/include/device_manager_service.h +++ b/services/service/include/device_manager_service.h @@ -431,7 +431,6 @@ private: const std::vector &backgroundUsers); int32_t UpdateServiceInfo(int64_t serviceId); int32_t GenerateServiceId(int64_t &serviceId); - int32_t GetServiceBindLocalInfo(ServiceBindLocalInfo &localInfo); int32_t ConvertServiceInfoProfileByRegInfo(const ServiceRegInfo &serviceRegInfo, ServiceInfoProfile &serviceInfoProfile); int32_t GenerateRegServiceId(int32_t ®ServiceId); diff --git a/services/service/src/device_manager_service.cpp b/services/service/src/device_manager_service.cpp index c9d8b802895c34196a26a14d79e1d3f97985351d..65c51e53896ad1949a9e04c2e871a924da5e7fd9 100644 --- a/services/service/src/device_manager_service.cpp +++ b/services/service/src/device_manager_service.cpp @@ -104,12 +104,9 @@ namespace { constexpr const char* LOCAL_ALL_USERID = "local_all_userId"; constexpr const char* LOCAL_FOREGROUND_USERID = "local_foreground_userId"; constexpr const char* LOCAL_BACKGROUND_USERID = "local_background_userId"; - constexpr int32_t MIN_PIN_TOKEN = 1000000; - constexpr int32_t MAX_PIN_TOKEN = 90000000; - constexpr uint32_t SERVICE_ID_LENGTH = 16; - constexpr int32_t BASE_HEX = 16; constexpr int32_t GENERATE_SERVICE_ID_RETRY_TIME = 3; constexpr int32_t SERVICE_UNPUBLISHED_STATE = 0; + constexpr int32_t SERVICE_PUBLISHED_STATE = 1; } DeviceManagerService::~DeviceManagerService() @@ -4562,9 +4559,23 @@ int32_t DeviceManagerService::UnRegisterServiceInfo(int32_t regServiceId) LOGE("Invalid parameter, regServiceId is empty."); return ERR_DM_INPUT_PARA_INVALID; } + int64_t tokenIdCaller = IPCSkeleton::GetCallingTokenID(); + ServiceInfoProfile serviceInfo; + int32_t ret = DeviceProfileConnector::GetInstance().GetServiceInfoByTokenId(tonkenIdCaller, serviceInfo); + if (ret != DM_OK || serviceInfo.regServiceId != regServiceId) { + LOGE("Invalid parameter, token id or regService id is invalid."); + return ERR_DM_INPUT_PARA_INVALID; + } + if (serviceInfo.publishState == SERVICE_PUBLISHED_STATE) { + ret = StopPublishService(serviceInfo.serviceId); + if (ret != DM_OK) { + LOGE("StopPublishService failed, ret: %{public}d.", ret); + return ret; + } + } int32_t userId = -1; MultipleUserConnector::GetCallerUserId(userId); - int32_t ret = DeviceProfileConnector::GetInstance().DeleteServiceInfoProfile(regServiceId, userId); + ret = DeviceProfileConnector::GetInstance().DeleteServiceInfoProfile(regServiceId, userId); if (ret != DM_OK) { LOGE("DeleteServiceInfoProfile failed, ret: %{public}d", ret); return ret; @@ -4592,7 +4603,14 @@ int32_t DeviceManagerService::StartPublishService(const std::string &pkgName, LOGE("Invalid parameter."); return ERR_DM_INPUT_PARA_INVALID; } - int32_t ret = GenerateServiceId(publishServiceParam.serviceInfo.serviceId); + int64_t tokenIdCaller = IPCSkeleton::GetCallingTokenID(); + ServiceInfoProfile serviceInfo; + int32_t ret = DeviceProfileConnector::GetInstance().GetServiceInfoByTokenId(tonkenIdCaller, serviceInfo); + if (ret != DM_OK || serviceInfo.regServiceId != publishServiceParam.regServiceId) { + LOGE("Invalid parameter, token id or regService id is invalid."); + return ERR_DM_INPUT_PARA_INVALID; + } + ret = GenerateServiceId(publishServiceParam.serviceInfo.serviceId); if (ret != DM_OK) { LOGE("GenerateServiceId failed, ret: %{public}d", ret); return ret; @@ -4628,14 +4646,21 @@ int32_t DeviceManagerService::StopPublishService(int64_t serviceId) LOGE("The caller does not have permission to call1 StopPublishService."); return ERR_DM_NO_PERMISSION; } - if (!IsDMServiceAdapterResidentLoad()) { - LOGE("StopPublishService failed, adapter instance not init or init failed."); - return ERR_DM_UNSUPPORTED_METHOD; - } if (serviceId == 0) { LOGE("Invalid parameter, serviceId is empty."); return ERR_DM_INPUT_PARA_INVALID; } + int64_t tokenIdCaller = IPCSkeleton::GetCallingTokenID(); + ServiceInfoProfile serviceInfo; + int32_t ret = DeviceProfileConnector::GetInstance().GetServiceInfoByServiceId(serviceId, serviceInfo); + if (ret != DM_OK || serviceInfo.publishState == SERVICE_UNPUBLISHED_STATE) { + LOGE("Invalid parameter, serviceId or publish state is invalid."); + return ERR_DM_INPUT_PARA_INVALID; + } + if (!IsDMServiceAdapterResidentLoad()) { + LOGE("StopPublishService failed, adapter instance not init or init failed."); + return ERR_DM_UNSUPPORTED_METHOD; + } int32_t ret = dmServiceImplExtResident_->StopPublishService(serviceId); if (ret != DM_OK) { LOGE("StopPublishService failed in closed-source logic, ret: %{public}d", ret); @@ -4687,18 +4712,8 @@ int32_t DeviceManagerService::UpdateServiceInfo(int64_t serviceId) int32_t DeviceManagerService::GenerateServiceId(int64_t &serviceId) { LOGI("GenerateServiceId Begin."); - ServiceBindLocalInfo localInfo; - int32_t ret = GetServiceBindLocalInfo(localInfo); - if (ret != DM_OK) { - LOGE("GenerateServiceId failed, get local info error"); - return ret; - } for (int i = 0; i < GENERATE_SERVICE_ID_RETRY_TIME; i++) { - int32_t randNum = GenRandInt(MIN_PIN_TOKEN, MAX_PIN_TOKEN); - std::string serviceIdStr = - Crypto::GetHashWithSalt(std::to_string(randNum), std::to_string(localInfo.tokenId) + localInfo.localUdid); - serviceIdStr = serviceIdStr.substr(0, SERVICE_ID_LENGTH - 1); - serviceId = std::stoll(serviceIdStr, nullptr, BASE_HEX); + serviceId = GenRandLongLong(MIN_REQUEST_ID, MAX_REQUEST_ID) ServiceInfoProfile serviceInfoProfile; ret = DeviceProfileConnector::GetInstance().GetServiceInfoProfileByServiceId(serviceId, serviceInfoProfile); if (ret != DM_OK) { @@ -4710,28 +4725,14 @@ int32_t DeviceManagerService::GenerateServiceId(int64_t &serviceId) return ERR_DM_FAILED; } -int32_t DeviceManagerService::GetServiceBindLocalInfo(ServiceBindLocalInfo &localInfo) -{ - char localDeviceId[DEVICE_UUID_LENGTH] = {0}; - GetDevUdid(localDeviceId, DEVICE_UUID_LENGTH); - localInfo.localUdid = std::string(localDeviceId); - localInfo.tokenId = OHOS::IPCSkeleton::GetCallingTokenID(); - MultipleUserConnector::GetCallerUserId(localInfo.userId); - return DM_OK; -} - int32_t DeviceManagerService::ConvertServiceInfoProfileByRegInfo( const ServiceRegInfo &serviceRegInfo, ServiceInfoProfile &serviceInfoProfile) { - ServiceBindLocalInfo localInfo; - int32_t ret = GetServiceBindLocalInfo(localInfo); - if (ret != DM_OK) { - LOGE("CovertServiceInfoProfileByRegInfo failed, get local info error"); - return ret; - } - serviceInfoProfile.deviceId = localInfo.localUdid; - serviceInfoProfile.userId = localInfo.userId; - serviceInfoProfile.tokenId = localInfo.tokenId; + char localDeviceId[DEVICE_UUID_LENGTH] = {0}; + GetDevUdid(localDeviceId, DEVICE_UUID_LENGTH); + serviceInfoProfile.deviceId = std::string(localDeviceId); + MultipleUserConnector::GetCallerUserId(serviceInfoProfile.userId); + serviceInfoProfile.tokenId = OHOS::IPCSkeleton::GetCallingTokenID(); serviceInfoProfile.serviceId = serviceRegInfo.serviceInfo.serviceId; serviceInfoProfile.serviceType = serviceRegInfo.serviceInfo.serviceType; serviceInfoProfile.serviceName = serviceRegInfo.serviceInfo.serviceName;