diff --git a/services/implementation/include/authentication/dm_auth_manager.h b/services/implementation/include/authentication/dm_auth_manager.h index a9631252feb1c1ff7c4579e27af10ec1006cc279..5a9e031b84faaa7d3a664c2988c68b82d07600e9 100644 --- a/services/implementation/include/authentication/dm_auth_manager.h +++ b/services/implementation/include/authentication/dm_auth_manager.h @@ -494,9 +494,6 @@ public: static bool IsPinCodeValid(int32_t numpin); bool IsImportedAuthCodeValid(); bool IsSrc(); - void GetCallerInfo(DmBindCallerInfo &callerInfo); - void SetCallerInfo(const DmBindCallerInfo &callerInfo); - void ClearCallerInfo(); private: bool IsHmlSessionType(); @@ -513,7 +510,6 @@ private: const std::string &extra); int32_t CheckAuthParamVaildExtra(const std::string &extra, const std::string &deviceId); bool CheckHmlParamValid(JsonObject &jsonObject); - bool CheckProcessNameInWhiteList(const std::string &processName); void ProcessSourceMsg(); void ProcessSinkMsg(); std::string GetAccountGroupIdHash(); @@ -539,7 +535,6 @@ private: void MemberJoinAuthRequest(int64_t requestId, int32_t status); void PutSrcAccessControlList(DmAccesser &accesser, DmAccessee &accessee, const std::string &localUdid); void PutSinkAccessControlList(DmAccesser &accesser, DmAccessee &accessee, const std::string &localUdid); - void GetCallerInfo(const std::string &pkgName, JsonObject &jsonObject); public: void RequestCredential(); @@ -582,7 +577,6 @@ private: void PutAccessControlList(); void SinkAuthenticateFinish(); void SrcAuthenticateFinish(); - std::string GetBundleLable(const std::string &bundleName); bool IsScreenLocked(); void CheckAndEndTvDream(); std::string ConvertSinkVersion(const std::string &version); @@ -592,8 +586,6 @@ private: void GetPeerUdidHash(int32_t sessionId, std::string &peerUdidHash); void DeleteOffLineTimer(int32_t sessionId); bool IsAllowDeviceBind(); - int32_t GetBindLevel(int32_t bindLevel); - std::string GetBundleName(JsonObject &jsonObject); int32_t GetBinderInfo(); void SetProcessInfo(); int32_t GetCloseSessionDelaySeconds(std::string &delaySecondsStr); @@ -604,6 +596,7 @@ private: int32_t GetTokenIdByBundleName(int32_t userId, std::string &bundleName, int64_t &tokenId); bool CheckBindLevel(const JsonItemObject &jsonObj, const std::string &key, int32_t &bindLevel); void RegisterCleanNotifyCallback(CleanNotifyCallback cleanNotifyCallback); + void GetBindCallerInfo(); private: std::shared_ptr softbusConnector_; @@ -651,9 +644,8 @@ private: std::map> sessionKeyIdAsyncResult_; bool isWaitingJoinLnnCallback_ = false; CleanNotifyCallback cleanNotifyCallback_{nullptr}; - std::mutex callerInfoMutex_; - DmBindCallerInfo callerInfo_; - bool callerInfoReady_ = false; + std::mutex bindParamMutex_; + std::map bindParam_; }; } // namespace DistributedHardware } // namespace OHOS diff --git a/services/implementation/include/authentication_v2/auth_manager.h b/services/implementation/include/authentication_v2/auth_manager.h index f0cb9b39df20a62970951e641dc277237f01d039..e9928b6c20c07dda7e7ffb26ac510d0602900278 100644 --- a/services/implementation/include/authentication_v2/auth_manager.h +++ b/services/implementation/include/authentication_v2/auth_manager.h @@ -120,11 +120,10 @@ public: void RegisterCleanNotifyCallback(CleanNotifyCallback cleanNotifyCallback); void ClearSoftbusSessionCallback(); void PrepareSoftbusSessionCallback(); - void GetCallerInfo(DmBindCallerInfo &callerInfo); - void SetCallerInfo(const DmBindCallerInfo &callerInfo); protected: std::shared_ptr context_; + std::mutex bindParamMutex_; std::map bindParam_; PeerTargetId targetId_; @@ -146,6 +145,7 @@ private: int32_t AuthenticateDevice(const std::string &sessionName, int32_t authType, const std::string &deviceId, const std::string &extra); void ParseUltrasonicSide(const JsonObject &jsonObject); + void GetBindCallerInfo(); }; class AuthSrcManager : public AuthManager { diff --git a/services/implementation/include/authentication_v2/dm_auth_context.h b/services/implementation/include/authentication_v2/dm_auth_context.h index e54240926f9a0da5ad65f3728af5f3da321b9dea..c3336ecf245893cae4e02e78747cc2e68720f42d 100644 --- a/services/implementation/include/authentication_v2/dm_auth_context.h +++ b/services/implementation/include/authentication_v2/dm_auth_context.h @@ -76,13 +76,6 @@ enum DmAuthScope { DM_AUTH_SCOPE_APP, }; -enum DmRole { - DM_ROLE_UNKNOWN = 0, - DM_ROLE_USER = 1, - DM_ROLE_SA, - DM_ROLE_FA, -}; - enum { DM_UNKNOWN_TYPE = 0, DM_SAME_ACCOUNT_TYPE = 1, diff --git a/services/implementation/include/authentication_v2/dm_auth_manager_base.h b/services/implementation/include/authentication_v2/dm_auth_manager_base.h index 2ceff22d20a5c9dc8f805652e4d53202bea1c077..708aad02e04686d9986ab264c392a20a82aeb0e1 100644 --- a/services/implementation/include/authentication_v2/dm_auth_manager_base.h +++ b/services/implementation/include/authentication_v2/dm_auth_manager_base.h @@ -102,15 +102,15 @@ extern const int32_t DM_AUTH_TYPE_MIN; extern const int32_t MIN_PIN_TOKEN; extern const int32_t MAX_PIN_TOKEN; -typedef struct DmBindCallerInfo { - int32_t userId = -1; - int32_t tokenId = -1; - std::string bundleName = ""; - std::string hostPkgLabel = ""; -} DmBindCallerInfo; - using CleanNotifyCallback = std::function; +enum DmRole { + DM_ROLE_UNKNOWN = 0, + DM_ROLE_USER = 1, + DM_ROLE_SA, + DM_ROLE_FA, +}; + class AuthManagerBase : public ISoftbusSessionCallback, public ISoftbusConnectorCallback, public IHiChainConnectorCallback, @@ -242,8 +242,7 @@ public: void EnableInsensibleSwitching(); void DisableInsensibleSwitching(); int32_t ParseAuthType(const std::map &bindParam, int32_t &authType); - virtual void GetCallerInfo(DmBindCallerInfo &callerInfo) = 0; - virtual void SetCallerInfo(const DmBindCallerInfo &callerInfo) = 0; + static bool CheckProcessNameInWhiteList(const std::string &processName); protected: bool NeedInsensibleSwitching(); bool isTransferReady_{true}; diff --git a/services/implementation/include/authentication_v2/dm_auth_state.h b/services/implementation/include/authentication_v2/dm_auth_state.h index 51ed72ec0255e4b9f328d3a3579548c2b138e547..b01040a6130fee2b1e5f8d2619ac784dede58e9e 100644 --- a/services/implementation/include/authentication_v2/dm_auth_state.h +++ b/services/implementation/include/authentication_v2/dm_auth_state.h @@ -154,7 +154,6 @@ public: void SetAclExtraInfo(std::shared_ptr context); void SetAclInfo(std::shared_ptr context); int32_t GetAclBindType(std::shared_ptr context, std::string credId); - static bool CheckProcessNameInWhiteList(const std::string &processName); int32_t GetOutputState(const std::string &processName, int32_t state); int32_t GetOutputReplay(const std::string &processName, int32_t replay); static uint64_t GetSysTimeMs(); diff --git a/services/implementation/include/device_manager_service_impl.h b/services/implementation/include/device_manager_service_impl.h index ada8d996659c88753bd805c16121bdb29422821a..8371ea70960f1cdfcec21c8673daaddd5f5c2eb1 100644 --- a/services/implementation/include/device_manager_service_impl.h +++ b/services/implementation/include/device_manager_service_impl.h @@ -60,6 +60,14 @@ struct Config { int32_t authenticationType{0}; }; +typedef struct DmBindCallerInfo { + int32_t userId = -1; + int32_t tokenId = -1; + int32_t bindLevel = -1; + std::string bundleName = ""; + std::string hostPkgLabel = ""; +} DmBindCallerInfo; + class DeviceManagerServiceImpl : public IDeviceManagerServiceImpl { public: DeviceManagerServiceImpl(); @@ -243,6 +251,12 @@ private: void CheckIsLastLnnAcl(DistributedDeviceProfile::AccessControlProfile profile, DistributedDeviceProfile::AccessControlProfile delProfile, DmOfflineParam &lnnAclParam, bool &isLastLnnAcl, const std::string &localUdid); + void BindTargetImpl(uint64_t tokenId, const std::string &pkgName, const PeerTargetId &targetId, + const std::map &bindParam); + void GetBindCallerInfo(DmBindCallerInfo &bindCallerInfo); + void SetBindCallerInfoToBindParam(const std::map &bindParam, + std::map &bindParamTmp, const DmBindCallerInfo &bindCallerInfo); + std::string GetBundleLable(const std::string &bundleName); private: std::shared_ptr authMgr_; // Old protocol only std::mutex authMgrMtx_; diff --git a/services/implementation/src/authentication_v2/auth_manager.cpp b/services/implementation/src/authentication_v2/auth_manager.cpp index 662df9c9717a4c72ca4230e6cf130c74845a4783..9f34cbfaf4fb6194f33cd8dab9f81ce2b076c39c 100644 --- a/services/implementation/src/authentication_v2/auth_manager.cpp +++ b/services/implementation/src/authentication_v2/auth_manager.cpp @@ -151,7 +151,10 @@ AuthManager::~AuthManager() context_->timer->DeleteAll(); LOGI("AuthManager context variables destroy successful."); } - bindParam_.clear(); + { + std::lock_guard lock(bindParamMutex_); + bindParam_.clear(); + } LOGI("DmAuthManager destructor"); } @@ -418,10 +421,6 @@ std::string AuthManager::GetBundleName(const JsonObject &jsonObject) void AuthManager::ParseJsonObject(const JsonObject &jsonObject) { - if (jsonObject.IsDiscarded()) { - return; - } - if (jsonObject[APP_OPERATION_KEY].IsString()) { context_->appOperation = jsonObject[APP_OPERATION_KEY].Get(); } @@ -437,10 +436,7 @@ void AuthManager::ParseJsonObject(const JsonObject &jsonObject) context_->connDelayCloseTime = GetCloseSessionDelaySeconds(delaySecondsStr); } - context_->accesser.bundleName = GetBundleName(jsonObject); - bindParam_[BUNDLE_NAME_KEY] = context_->accesser.bundleName; context_->accessee.bundleName = context_->accesser.bundleName; - if (jsonObject[TAG_PEER_BUNDLE_NAME].IsString() && !jsonObject[TAG_PEER_BUNDLE_NAME].Get().empty()) { context_->accessee.bundleName = jsonObject[TAG_PEER_BUNDLE_NAME].Get(); context_->accessee.oldBundleName = context_->accessee.bundleName; @@ -522,7 +518,7 @@ int32_t GetBindLevel(int32_t bindLevel) std::string processName = ""; int32_t ret = AppManager::GetInstance().GetCallerProcessName(processName); LOGI("GetBindLevel processName = %{public}s", GetAnonyString(processName).c_str()); - if (ret == DM_OK && DmAuthState::CheckProcessNameInWhiteList(processName)) { + if (ret == DM_OK && AuthManagerBase::CheckProcessNameInWhiteList(processName)) { return USER; } #endif @@ -548,7 +544,6 @@ void AuthManager::GetAuthParam(const std::string &pkgName, int32_t authType, GetDevUdid(localDeviceId, DEVICE_UUID_LENGTH); context_->accesser.deviceId = std::string(localDeviceId); context_->pkgName = pkgName; - context_->pkgLabel = GetBundleLabel(pkgName); context_->authType = (DmAuthType)authType; context_->accesser.deviceName = context_->listener->GetLocalDisplayDeviceNameForPrivacy(); context_->accesser.deviceType = context_->softbusConnector->GetLocalDeviceTypeId(); @@ -572,15 +567,9 @@ void AuthManager::GetAuthParam(const std::string &pkgName, int32_t authType, CheckBindLevel(jsonObject, TAG_BIND_LEVEL, context_->accesser.oldBindLevel); context_->accesser.oldBindLevel = GetBindLevel(context_->accesser.oldBindLevel); - context_->accesser.bindLevel = DmRole::DM_ROLE_FA; - if (AppManager::GetInstance().IsSystemApp()) { - context_->accesser.bindLevel = DmRole::DM_ROLE_FA; - } - if (AppManager::GetInstance().IsSystemSA()) { - context_->accesser.bindLevel = DmRole::DM_ROLE_SA; - } - if (DmAuthState::CheckProcessNameInWhiteList(context_->accesser.bundleName)) { - context_->accesser.bindLevel = DmRole::DM_ROLE_USER; + { + std::lock_guard lock(bindParamMutex_); + bindParam_["bindCallerOldBindLevel"] = std::to_string(context_->accesser.oldBindLevel); } } @@ -607,12 +596,10 @@ void AuthManager::InitAuthState(const std::string &pkgName, int32_t authType, int32_t AuthManager::AuthenticateDevice(const std::string &pkgName, int32_t authType, const std::string &deviceId, const std::string &extra) { - LOGI("AuthManager::AuthenticateDevice start auth type %{public}d.", authType); + LOGI("AuthManager::AuthenticateDevice start auth type %{public}d, extra %{public}s.", authType, extra.c_str()); SetAuthType(authType); - int32_t userId = -1; - MultipleUserConnector::GetCallerUserId(userId); context_->processInfo.pkgName = pkgName; - context_->processInfo.userId = userId; + GetBindCallerInfo(); int32_t ret = CheckAuthParamVaild(pkgName, authType, deviceId, extra); if (ret != DM_OK) { LOGE("AuthManager::AuthenticateDevice failed, param is invaild."); @@ -662,7 +649,10 @@ int32_t AuthManager::BindTarget(const std::string &pkgName, const PeerTargetId & return ERR_DM_INPUT_PARA_INVALID; } context_->peerTargetId = targetId_; - bindParam_ = bindParam; + { + std::lock_guard lock(bindParamMutex_); + bindParam_ = bindParam; + } if (!targetId.deviceId.empty()) { ret = AuthenticateDevice(pkgName, authType, targetId.deviceId, ParseExtraFromMap(bindParam)); if (ret != DM_OK) { @@ -1016,8 +1006,11 @@ void AuthManager::GetBindTargetParams(std::string &pkgName, PeerTargetId &target { pkgName = context_->pkgName; targetId = targetId_; - bindParam = bindParam_; + { + std::lock_guard lock(bindParamMutex_); + bindParam = bindParam_; + } LOGI("AuthManager::GetBindTargetParams get pkgName %{public}s to reuse", pkgName.c_str()); return; } @@ -1035,17 +1028,27 @@ void AuthManager::ClearSoftbusSessionCallback() void AuthManager::PrepareSoftbusSessionCallback() {} -void AuthManager::GetCallerInfo(DmBindCallerInfo &callerInfo) +void AuthManager::GetBindCallerInfo() { - callerInfo.userId = context_->accesser.userId; - callerInfo.tokenId = context_->accesser.tokenId; - callerInfo.bundleName = context_->accesser.bundleName; - callerInfo.hostPkgLabel = context_->pkgLabel; -} - -void AuthManager::SetCallerInfo(const DmBindCallerInfo &callerInfo) -{ - (void)callerInfo; + LOGI("start."); + { + std::lock_guard lock(bindParamMutex_); + if (bindParam_.find("bindCallerUserId") != bindParam_.end()) { + context_->processInfo.userId = std::atoi(bindParam_["bindCallerUserId"].c_str()); + } + if (bindParam_.find("bindCallerTokenId") != bindParam_.end()) { + context_->accesser.tokenId = std::atoi(bindParam_["bindCallerTokenId"].c_str()); + } + if (bindParam_.find("bindCallerBindLevel") != bindParam_.end()) { + context_->accesser.bindLevel = std::atoi(bindParam_["bindCallerBindLevel"].c_str()); + } + if (bindParam_.find("bindCallerBundleName") != bindParam_.end()) { + context_->accesser.bindLevel = bindParam_["bindCallerBundleName"]; + } + if (bindParam_.find("bindCallerHostPkgLabel") != bindParam_.end()) { + context_->pkgLabel = bindParam_["bindCallerHostPkgLabel"]; + } + } } } // namespace DistributedHardware } // namespace OHOS \ No newline at end of file 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 fa963619f93e7a21141146a7ec0cb9f52f42cee2..061259ec9fe9ed99c8cc41fe96bff67814715ad9 100644 --- a/services/implementation/src/authentication_v2/auth_stages/auth_negotiate.cpp +++ b/services/implementation/src/authentication_v2/auth_stages/auth_negotiate.cpp @@ -148,7 +148,7 @@ int32_t AuthSinkNegotiateStateMachine::RespQueryAcceseeIds(std::shared_ptraccessee.bundleName.c_str()); return ERR_DM_GET_TOKENID_FAILED; } - if (DmAuthState::CheckProcessNameInWhiteList(context->accessee.bundleName)) { + if (AuthManagerBase::CheckProcessNameInWhiteList(context->accessee.bundleName)) { context->accessee.bindLevel = DmRole::DM_ROLE_USER; } context->accessee.tokenIdHash = Crypto::GetTokenIdHash(std::to_string(context->accessee.tokenId)); diff --git a/services/implementation/src/authentication_v2/dm_auth_manager_base.cpp b/services/implementation/src/authentication_v2/dm_auth_manager_base.cpp index 49eb28e1781bf68134cc0407b4bae2ae8c0d4130..6eb16828c6f16b0b18aa3452084a0056678678f8 100644 --- a/services/implementation/src/authentication_v2/dm_auth_manager_base.cpp +++ b/services/implementation/src/authentication_v2/dm_auth_manager_base.cpp @@ -98,6 +98,15 @@ const int32_t SESSION_HEARTBEAT_TIMEOUT = 50; const int32_t PIN_AUTH_TIMEOUT = 60; const int32_t EVENT_TIMEOUT = 5000; // 5000 ms +constexpr int32_t OPEN_PROCESS_NAME_WHITE_LIST_NUM = 1; +constexpr int32_t CLOSE_PROCESS_NAME_WHITE_LIST_NUM = 2; +constexpr const static char* OPEN_PROCESS_NAME_WHITE_LIST[OPEN_PROCESS_NAME_WHITE_LIST_NUM] = { + "com.example.myapplication" +}; +constexpr const static char* CLOSE_PROCESS_NAME_WHITE_LIST[CLOSE_PROCESS_NAME_WHITE_LIST_NUM] = { + "CollaborationFwk", + "gameservice_server" +}; int32_t AuthManagerBase::AuthenticateDevice(const std::string &pkgName, int32_t authType, const std::string &deviceId, const std::string &extra) @@ -510,5 +519,34 @@ int32_t AuthManagerBase::EndDream() LOGI("end dream success"); return DM_OK; } + +bool AuthManagerBase::CheckProcessNameInWhiteList(const std::string &processName) +{ + LOGI("AuthManagerBase::CheckProcessNameInWhiteList start"); + if (processName.empty()) { + LOGE("processName is empty"); + return false; + } + uint16_t index = 0; +#ifdef DEVICE_MANAGER_COMMON_FLAG + for (; index < OPEN_PROCESS_NAME_WHITE_LIST_NUM; ++index) { + std::string whitePkgName(OPEN_PROCESS_NAME_WHITE_LIST[index]); + if (processName == whitePkgName) { + LOGI("processName = %{public}s in whiteList.", processName.c_str()); + return true; + } + } +#else + for (; index < CLOSE_PROCESS_NAME_WHITE_LIST_NUM; ++index) { + std::string whitePkgName(CLOSE_PROCESS_NAME_WHITE_LIST[index]); + if (processName == whitePkgName) { + LOGI("processName = %{public}s in whiteList.", processName.c_str()); + return true; + } + } +#endif + LOGI("CheckProcessNameInWhiteList: %{public}s invalid.", processName.c_str()); + return false; +} } // namespace DistributedHardware } // namespace OHOS \ No newline at end of file diff --git a/services/implementation/src/authentication_v2/dm_auth_state.cpp b/services/implementation/src/authentication_v2/dm_auth_state.cpp index 4b9ff4f3db6f81e94a8bbc5f4c9caee206b72dec..86aa32e393d33dbb6920f0dee2a88014468518c4 100644 --- a/services/implementation/src/authentication_v2/dm_auth_state.cpp +++ b/services/implementation/src/authentication_v2/dm_auth_state.cpp @@ -61,17 +61,6 @@ const std::map NEW_AND_OLD_REPLAY_MAPPING = { { SOFTBUS_OK, SOFTBUS_OK } }; -constexpr int32_t OPEN_PROCESS_NAME_WHITE_LIST_NUM = 1; -constexpr int32_t CLOSE_PROCESS_NAME_WHITE_LIST_NUM = 2; -constexpr const static char* OPEN_PROCESS_NAME_WHITE_LIST[OPEN_PROCESS_NAME_WHITE_LIST_NUM] = { - "com.example.myapplication" -}; -constexpr const static char* CLOSE_PROCESS_NAME_WHITE_LIST[CLOSE_PROCESS_NAME_WHITE_LIST_NUM] = { - "CollaborationFwk", - "gameservice_server" -}; - - int32_t DmAuthState::GetTaskTimeout(std::shared_ptr context, const char* taskName, int32_t taskTimeOut) { LOGI("GetTaskTimeout, taskName: %{public}s, authType_: %{public}d", taskName, context->authType); @@ -338,35 +327,6 @@ bool DmAuthState::HaveSameTokenId(std::shared_ptr context, const (srcTokenIdHash == context->accessee.tokenIdHash)); } -bool DmAuthState::CheckProcessNameInWhiteList(const std::string &processName) -{ - LOGI("DmAuthState::CheckProcessNameInWhiteList start"); - if (processName.empty()) { - LOGE("processName is empty"); - return false; - } - uint16_t index = 0; -#ifdef DEVICE_MANAGER_COMMON_FLAG - for (; index < OPEN_PROCESS_NAME_WHITE_LIST_NUM; ++index) { - std::string whitePkgName(OPEN_PROCESS_NAME_WHITE_LIST[index]); - if (processName == whitePkgName) { - LOGI("processName = %{public}s in whiteList.", processName.c_str()); - return true; - } - } -#else - for (; index < CLOSE_PROCESS_NAME_WHITE_LIST_NUM; ++index) { - std::string whitePkgName(CLOSE_PROCESS_NAME_WHITE_LIST[index]); - if (processName == whitePkgName) { - LOGI("processName = %{public}s in whiteList.", processName.c_str()); - return true; - } - } -#endif - LOGI("CheckProcessNameInWhiteList: %{public}s invalid.", processName.c_str()); - return false; -} - int32_t DmAuthState::GetOutputState(const std::string &processName, int32_t state) { LOGI("state %{public}d.", state); diff --git a/services/implementation/src/device_manager_service_impl.cpp b/services/implementation/src/device_manager_service_impl.cpp index 326615429cc0f29734cc6051867cbefddc3b1ca2..af1fa73edeaf978d233d7ea4b52557b4fb2ee245 100644 --- a/services/implementation/src/device_manager_service_impl.cpp +++ b/services/implementation/src/device_manager_service_impl.cpp @@ -21,6 +21,8 @@ #include #include "app_manager.h" +#include "bundle_mgr_interface.h" +#include "bundle_mgr_proxy.h" #include "dm_error_type.h" #include "dm_anonymous.h" #include "dm_constants.h" @@ -37,6 +39,8 @@ #include "common_event_support.h" using namespace OHOS::EventFwk; #endif +#include "iservice_registry.h" +#include "system_ability_definition.h" namespace OHOS { namespace DistributedHardware { @@ -843,8 +847,6 @@ int32_t DeviceManagerServiceImpl::TransferSrcOldAuthMgr(std::shared_ptr std::map bindParam; auto authMgr = GetAuthMgrByTokenId(tokenId); authMgr->GetBindTargetParams(pkgName, peerTargetId, bindParam); - DmBindCallerInfo callerInfo; - authMgr->GetCallerInfo(callerInfo); int32_t authType = -1; authMgr->ParseAuthType(bindParam, authType); authMgrMap_.erase(tokenId); @@ -858,7 +860,6 @@ int32_t DeviceManagerServiceImpl::TransferSrcOldAuthMgr(std::shared_ptr return ret; } authMgr = nullptr; - authMgr_->SetCallerInfo(callerInfo); if (authMgr_->BindTarget(pkgName, peerTargetId, bindParam, sessionId, 0) != DM_OK) { LOGE("DeviceManagerServiceImpl::TransferSrcOldAuthMgr authManager BindTarget failed"); return ERR_DM_AUTH_FAILED; @@ -1536,14 +1537,9 @@ int32_t DeviceManagerServiceImpl::ParseConnectAddr(const PeerTargetId &targetId, return DM_OK; } -int32_t DeviceManagerServiceImpl::BindTarget(const std::string &pkgName, const PeerTargetId &targetId, - const std::map &bindParam) +void DeviceManagerServiceImpl::BindTargetImpl(uint64_t tokenId, const std::string &pkgName, + const PeerTargetId &targetId, const std::map &bindParam) { - if (pkgName.empty()) { - LOGE("BindTarget failed, pkgName is empty."); - return ERR_DM_INPUT_PARA_INVALID; - } - std::string deviceId = ""; PeerTargetId targetIdTmp = const_cast(targetId); if (ParseConnectAddr(targetId, deviceId, bindParam) == DM_OK) { @@ -1593,7 +1589,31 @@ int32_t DeviceManagerServiceImpl::BindTarget(const std::string &pkgName, const P LOGE("authMgr BindTarget failed, ret %{public}d.", ret); CleanAuthMgrByLogicalSessionId(logicalSessionId); } - return ret; + LOGI("DeviceManagerServiceImpl BindTargetImpl thread end, tokenId %{public}" PRID64".", tokenId); + return; +} + +int32_t DeviceManagerServiceImpl::BindTarget(const std::string &pkgName, const PeerTargetId &targetId, + const std::map &bindParam) +{ + if (pkgName.empty()) { + LOGE("BindTarget failed, pkgName is empty."); + return ERR_DM_INPUT_PARA_INVALID; + } + DmBindCallerInfo bindCallerInfo; + GetBindCallerInfo(bindCallerInfo); + std::map bindParamTmp; + SetBindCallerInfoToBindParam(bindParam, bindParamTmp, bindCallerInfo); + uint64_t tokenId = IPCSkeleton::GetCallingTokenID(); + if (authMgrMap_.find(tokenId) == authMgrMap_.end()) { + std::thread newThread(&DeviceManagerServiceImpl::BindTargetImpl, this, tokenId, pkgName, + targetId, bindParamTmp); + newThread.detach(); + } else { + LOGE("BindTarget failed, this device is being bound. please try again later."); + return ERR_DM_AUTH_BUSINESS_BUSY; + } + return DM_OK; } int32_t DeviceManagerServiceImpl::DpAclAdd(const std::string &udid) @@ -2430,6 +2450,84 @@ void DeviceManagerServiceImpl::HandleCommonEventBroadCast(const std::vector(DmRole::DM_ROLE_FA); + if (AppManager::GetInstance().IsSystemApp()) { + bindLevel = static_cast(DmRole::DM_ROLE_FA); + } + if (AppManager::GetInstance().IsSystemSA()) { + bindLevel = static_cast(DmRole::DM_ROLE_SA); + } + if (AuthManagerBase::CheckProcessNameInWhiteList(bundleName)) { + bindLevel = static_cast(DmRole::DM_ROLE_USER); + } + std::string hostPkgLabel = GetBundleLable(bundleName); + bindCallerInfo.userId = userId; + bindCallerInfo.tokenId = callingTokenId; + bindCallerInfo.bindLevel = bindLevel; + bindCallerInfo.bundleName = bundleName; + bindCallerInfo.hostPkgLabel = hostPkgLabel; + LOGI("userId %{public}d, tokenId %{public}d, bindLevel %{public}d, bundleName %{public}s, hostPkgLabel %{public}s", + userId, callingTokenId, bindLevel, GetAnonyString(bundleName).c_str(), GetAnonyString(hostPkgLabel).c_str()); +} + +std::string DeviceManagerServiceImpl::GetBundleLable(const std::string &bundleName) +{ + auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (samgr == nullptr) { + LOGE("Get ability manager failed"); + return bundleName; + } + + sptr object = samgr->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID); + if (object == nullptr) { + LOGE("object is NULL."); + return bundleName; + } + + sptr bms = iface_cast(object); + if (bms == nullptr) { + LOGE("bundle manager service is NULL."); + return bundleName; + } + + auto bundleResourceProxy = bms->GetBundleResourceProxy(); + if (bundleResourceProxy == nullptr) { + LOGE("GetBundleResourceProxy fail"); + return bundleName; + } + AppExecFwk::BundleResourceInfo resourceInfo; + auto result = bundleResourceProxy->GetBundleResourceInfo(bundleName, + static_cast(OHOS::AppExecFwk::ResourceFlag::GET_RESOURCE_INFO_ALL), resourceInfo); + if (result != ERR_OK) { + LOGE("GetBundleResourceInfo failed"); + return bundleName; + } + LOGI("bundle resource label is %{public}s ", (resourceInfo.label).c_str()); + return resourceInfo.label; +} + +void DeviceManagerServiceImpl::SetBindCallerInfoToBindParam(const std::map &bindParam, + std::map &bindParamTmp, const DmBindCallerInfo &bindCallerInfo) +{ + LOGI("start."); + bindParamTmp = const_cast &>(bindParam); + bindParamTmp["bindCallerUserId"] = std::to_string(bindCallerInfo.userId); + bindParamTmp["bindCallerTokenId"] = std::to_string(bindCallerInfo.tokenId); + bindParamTmp["bindCallerBindLevel"] = std::to_string(bindCallerInfo.bindLevel); + bindParamTmp["bindCallerBundleName"] = bindCallerInfo.bundleName; + bindParamTmp["bindCallerHostPkgLabel"] = bindCallerInfo.hostPkgLabel; +} + extern "C" IDeviceManagerServiceImpl *CreateDMServiceObject(void) { return new DeviceManagerServiceImpl;