1 /* 2 * Copyright (c) 2022-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 #ifndef OHOS_DM_SERVICE_IMPL_H 17 #define OHOS_DM_SERVICE_IMPL_H 18 19 #include <map> 20 #include <queue> 21 #include <semaphore> 22 #include <set> 23 #include <string> 24 #include <vector> 25 26 #include "access_control_profile.h" 27 #include "auth_manager.h" 28 #include "dm_ability_manager.h" 29 #include "dm_auth_manager.h" 30 #include "dm_auth_manager_base.h" 31 #include "dm_common_event_manager.h" 32 #include "dm_credential_manager.h" 33 #include "dm_device_info.h" 34 #include "dm_device_state_manager.h" 35 #include "dm_single_instance.h" 36 #include "dp_inited_callback.h" 37 #include "idevice_manager_service_impl.h" 38 #include "ipc_skeleton.h" 39 #include "mine_hichain_connector.h" 40 #include "softbus_connector.h" 41 #include "deviceprofile_connector.h" 42 43 namespace OHOS { 44 namespace DistributedHardware { 45 46 class Session { 47 public: 48 Session(int sessionId, std::string deviceId); 49 int sessionId_; 50 std::string deviceId_; 51 std::string version_{""}; 52 std::atomic<bool> flag_{false}; // Only one session is allowed 53 std::set<uint64_t> logicalSessionSet_; // Logical Session Set 54 std::atomic<int> logicalSessionCnt_{0}; 55 }; 56 57 struct Config { 58 std::string pkgName; 59 std::string authCode; 60 int32_t authenticationType{0}; 61 }; 62 63 typedef struct DmBindCallerInfo { 64 int32_t userId = -1; 65 int32_t tokenId = -1; 66 int32_t bindLevel = -1; 67 bool isSystemSA = false; 68 std::string bundleName = ""; 69 std::string hostPkgLabel = ""; 70 std::string processName = ""; 71 } DmBindCallerInfo; 72 73 class DeviceManagerServiceImpl : public IDeviceManagerServiceImpl { 74 public: 75 DeviceManagerServiceImpl(); 76 virtual ~DeviceManagerServiceImpl(); 77 78 int32_t Initialize(const std::shared_ptr<IDeviceManagerServiceListener> &listener); 79 80 void Release(); 81 82 int32_t UnAuthenticateDevice(const std::string &pkgName, const std::string &udid, int32_t bindLevel); 83 84 int32_t UnBindDevice(const std::string &pkgName, const std::string &udid, int32_t bindLevel); 85 86 int32_t UnBindDevice(const std::string &pkgName, const std::string &udid, 87 int32_t bindLevel, const std::string &extra); 88 89 int32_t SetUserOperation(std::string &pkgName, int32_t action, const std::string ¶ms); 90 91 void HandleDeviceStatusChange(DmDeviceState devState, DmDeviceInfo &devInfo); 92 93 int OnSessionOpened(int sessionId, int result); 94 95 void OnSessionClosed(int sessionId); 96 97 void OnBytesReceived(int sessionId, const void *data, unsigned int dataLen); 98 99 int OnPinHolderSessionOpened(int sessionId, int result); 100 101 void OnPinHolderSessionClosed(int sessionId); 102 103 void OnPinHolderBytesReceived(int sessionId, const void *data, unsigned int dataLen); 104 105 int32_t RequestCredential(const std::string &reqJsonStr, std::string &returnJsonStr); 106 107 int32_t ImportCredential(const std::string &pkgName, const std::string &credentialInfo); 108 109 int32_t DeleteCredential(const std::string &pkgName, const std::string &deleteInfo); 110 111 int32_t MineRequestCredential(const std::string &pkgName, std::string &returnJsonStr); 112 113 int32_t CheckCredential(const std::string &pkgName, const std::string &reqJsonStr, 114 std::string &returnJsonStr); 115 116 int32_t ImportCredential(const std::string &pkgName, const std::string &reqJsonStr, 117 std::string &returnJsonStr); 118 119 int32_t DeleteCredential(const std::string &pkgName, const std::string &reqJsonStr, 120 std::string &returnJsonStr); 121 122 int32_t RegisterCredentialCallback(const std::string &pkgName); 123 124 int32_t UnRegisterCredentialCallback(const std::string &pkgName); 125 126 int32_t NotifyEvent(const std::string &pkgName, const int32_t eventId, const std::string &event); 127 128 int32_t GetGroupType(std::vector<DmDeviceInfo> &deviceList); 129 130 int32_t GetUdidHashByNetWorkId(const char *networkId, std::string &deviceId); 131 132 void LoadHardwareFwkService(); 133 134 int32_t RegisterUiStateCallback(const std::string &pkgName); 135 136 int32_t UnRegisterUiStateCallback(const std::string &pkgName); 137 138 int32_t ImportAuthCode(const std::string &pkgName, const std::string &authCode); 139 140 int32_t ExportAuthCode(std::string &authCode); 141 142 int32_t BindTarget(const std::string &pkgName, const PeerTargetId &targetId, 143 const std::map<std::string, std::string> &bindParam); 144 145 std::unordered_map<std::string, DmAuthForm> GetAppTrustDeviceIdList(std::string pkgname); 146 147 int32_t DpAclAdd(const std::string &udid); 148 int32_t IsSameAccount(const std::string &udid); 149 uint64_t GetTokenIdByNameAndDeviceId(std::string extra, std::string requestDeviceId); 150 void ScreenCommonEventCallback(std::string commonEventType); 151 bool CheckIsSameAccount(const DmAccessCaller &caller, const std::string &srcUdid, 152 const DmAccessCallee &callee, const std::string &sinkUdid); 153 bool CheckAccessControl(const DmAccessCaller &caller, const std::string &srcUdid, 154 const DmAccessCallee &callee, const std::string &sinkUdid); 155 void HandleDeviceNotTrust(const std::string &udid); 156 int32_t GetBindLevel(const std::string &pkgName, const std::string &localUdid, 157 const std::string &udid, uint64_t &tokenId); 158 void HandleIdentAccountLogout(const DMAclQuadInfo &info, const std::string &accountId); 159 void HandleDeviceScreenStatusChange(DmDeviceInfo &devInfo); 160 int32_t StopAuthenticateDevice(const std::string &pkgName); 161 void HandleCredentialAuthStatus(const std::string &deviceList, uint16_t deviceTypeId, int32_t errcode); 162 int32_t SyncLocalAclListProcess(const DevUserInfo &localDevUserInfo, 163 const DevUserInfo &remoteDevUserInfo, std::string remoteAclList); 164 int32_t GetAclListHash(const DevUserInfo &localDevUserInfo, 165 const DevUserInfo &remoteDevUserInfo, std::string &aclList); 166 int32_t ProcessAppUnintall(const std::string &appId, int32_t accessTokenId); 167 int32_t ProcessAppUninstall(int32_t userId, int32_t accessTokenId); 168 void ProcessUnBindApp(int32_t userId, int32_t accessTokenId, const std::string &extra, const std::string &udid); 169 void HandleSyncUserIdEvent(const std::vector<uint32_t> &foregroundUserIds, 170 const std::vector<uint32_t> &backgroundUserIds, const std::string &remoteUdid, bool isCheckUserStatus); 171 void HandleUserSwitched(const std::vector<std::string> &deviceVec, int32_t currentUserId, 172 int32_t beforeUserId); 173 std::multimap<std::string, int32_t> GetDeviceIdAndUserId(int32_t localUserId); 174 int32_t SaveOnlineDeviceInfo(const std::vector<DmDeviceInfo> &deviceList); 175 void HandleDeviceUnBind(int32_t bindType, const std::string &peerUdid, 176 const std::string &localUdid, int32_t localUserId, const std::string &localAccountId); 177 int32_t RegisterAuthenticationType(int32_t authenticationType); 178 void DeleteAlwaysAllowTimeOut(); 179 void CheckDeleteCredential(const std::string &remoteUdid, int32_t remoteUserId); 180 void HandleCredentialDeleted(const char *credId, const char *credInfo, const std::string &localUdid, 181 std::string &remoteUdid, bool &isSendBroadCast); 182 void HandleShareUnbindBroadCast(const std::string &credId, const int32_t &userId, const std::string &localUdid); 183 int32_t CheckDeviceInfoPermission(const std::string &localUdid, const std::string &peerDeviceId); 184 int32_t DeleteAcl(const std::string &sessionName, const std::string &localUdid, const std::string &remoteUdid, 185 int32_t bindLevel, const std::string &extra); 186 bool IsProxyUnbind(const std::string &extra); 187 int32_t DeleteAclV2(const std::string &sessionName, const std::string &localUdid, const std::string &remoteUdid, 188 int32_t bindLevel, const std::string &extra); 189 void NotifyCleanEvent(uint64_t logicalSessionId); 190 void HandleServiceUnBindEvent(int32_t userId, const std::string &remoteUdid, 191 int32_t remoteTokenId); 192 int32_t DeleteGroup(const std::string &pkgName, const std::string &deviceId); 193 int32_t InitAndRegisterAuthMgr(bool isSrcSide, uint64_t tokenId, std::shared_ptr<Session> session, 194 uint64_t logicalSessionId, const std::string &pkgName); 195 void HandleCommonEventBroadCast(const std::vector<uint32_t> &foregroundUserIds, 196 const std::vector<uint32_t> &backgroundUserIds, const std::string &remoteUdid); 197 std::vector<std::string> GetDeviceIdByUserIdAndTokenId(int32_t userId, int32_t tokenId); 198 bool CheckSrcAccessControl(const DmAccessCaller &caller, const std::string &srcUdid, 199 const DmAccessCallee &callee, const std::string &sinkUdid); 200 bool CheckSinkAccessControl(const DmAccessCaller &caller, const std::string &srcUdid, 201 const DmAccessCallee &callee, const std::string &sinkUdid); 202 bool CheckSrcIsSameAccount(const DmAccessCaller &caller, const std::string &srcUdid, 203 const DmAccessCallee &callee, const std::string &sinkUdid); 204 bool CheckSinkIsSameAccount(const DmAccessCaller &caller, const std::string &srcUdid, 205 const DmAccessCallee &callee, const std::string &sinkUdid); 206 void DeleteHoDevice(const std::string &peerUdid, const std::vector<int32_t> &foreGroundUserIds, 207 const std::vector<int32_t> &backGroundUserIds); 208 private: 209 int32_t PraseNotifyEventJson(const std::string &event, JsonObject &jsonObject); 210 std::string GetUdidHashByNetworkId(const std::string &networkId); 211 void SetOnlineProcessInfo(const uint32_t &bindType, ProcessInfo &processInfo, DmDeviceInfo &devInfo, 212 const std::string &requestDeviceId, const std::string &trustDeviceId, DmDeviceState devState); 213 void HandleDeletedAclOffline(const std::string &trustDeviceId, 214 const std::string &requestDeviceId, DmDeviceInfo &devInfo, ProcessInfo &processInfo, DmDeviceState &devState); 215 void HandleOffline(DmDeviceState devState, DmDeviceInfo &devInfo); 216 void HandleOnline(DmDeviceState devState, DmDeviceInfo &devInfo); 217 bool CheckSharePeerSrc(const std::string &peerUdid, const std::string &localUdid); 218 std::map<std::string, int32_t> GetDeviceIdAndBindLevel(int32_t userId); 219 std::multimap<std::string, int32_t> GetDeviceIdAndUserId(int32_t userId, const std::string &accountId); 220 void HandleAccountLogoutEvent(int32_t remoteUserId, const std::string &remoteAccountHash, 221 const std::string &remoteUdid); 222 void HandleDevUnBindEvent(int32_t remoteUserId, const std::string &remoteUdid); 223 void HandleAppUnBindEvent(int32_t remoteUserId, const std::string &remoteUdid, int32_t tokenId); 224 void HandleAppUnBindEvent(int32_t remoteUserId, const std::string &remoteUdid, 225 int32_t tokenId, int32_t peerTokenId); 226 void HandleUserRemoved(std::vector<std::string> peerUdids, int32_t preUserId); 227 void HandleRemoteUserRemoved(int32_t preUserId, const std::string &remoteUdid); 228 DmAuthForm ConvertBindTypeToAuthForm(int32_t bindType); 229 std::shared_ptr<AuthManagerBase> GetAuthMgr(); 230 int32_t AddAuthMgr(uint64_t tokenId, int sessionId, std::shared_ptr<AuthManagerBase> authMgr); 231 void EraseAuthMgr(uint64_t tokenId); 232 std::shared_ptr<AuthManagerBase> GetAuthMgrByTokenId(uint64_t tokenId); 233 std::shared_ptr<AuthManagerBase> GetCurrentAuthMgr(); 234 void CreateGlobalClassicalAuthMgr(); 235 std::shared_ptr<Session> GetCurSession(int sessionId); 236 std::shared_ptr<Session> GetOrCreateSession(const std::string& deviceId, 237 const std::map<std::string, std::string> &bindParam, uint64_t tokenId); 238 int32_t ParseConnectAddr(const PeerTargetId &targetId, std::string &deviceId, 239 const std::map<std::string, std::string> &bindParam); 240 std::shared_ptr<Config> GetConfigByTokenId(); 241 int OpenAuthSession(const std::string& deviceId, const std::map<std::string, std::string> &bindParam); 242 int32_t ChangeUltrasonicTypeToPin(std::map<std::string, std::string> &bindParam); 243 int32_t TransferByAuthType(int32_t authType, 244 std::shared_ptr<Session> curSession, std::shared_ptr<AuthManagerBase> authMgr, 245 std::map<std::string, std::string> &bindParam, uint64_t logicalSessionId); 246 247 std::shared_ptr<AuthManagerBase> GetAuthMgrByMessage(int32_t msgType, uint64_t logicalSessionId, 248 const JsonObject &jsonObject, std::shared_ptr<Session> curSession); 249 int32_t TransferOldAuthMgr(int32_t msgType, const JsonObject &jsonObject, 250 std::shared_ptr<Session> curSession); 251 int32_t TransferSrcOldAuthMgr(std::shared_ptr<Session> curSession); 252 int32_t TransferSinkOldAuthMgr(const JsonObject &jsonObject, std::shared_ptr<Session> curSession); 253 int32_t GetDeviceInfo(const PeerTargetId &targetId, std::string &addrType, std::string &deviceId, 254 std::shared_ptr<DeviceInfo> deviceInfo, int32_t &index); 255 bool IsAuthNewVersion(int32_t bindLevel, std::string localUdid, std::string remoteUdid, 256 int32_t tokenId, int32_t userId); 257 void ImportConfig(std::shared_ptr<AuthManagerBase> authMgr, uint64_t tokenId, const std::string &pkgName); 258 void ImportAuthCodeToConfig(std::shared_ptr<AuthManagerBase> authMgr, uint64_t tokenId); 259 260 void CleanAuthMgrByLogicalSessionId(uint64_t logicalSessionId); 261 void CleanSessionMap(std::shared_ptr<Session> session); 262 void CleanSessionMap(int sessionId); 263 void CleanSessionMapByLogicalSessionId(uint64_t logicalSessionId); 264 int32_t DeleteAclForProcV2(const std::string &localUdid, uint32_t localTokenId, const std::string &remoteUdid, 265 int32_t bindLevel, const std::string &extra, int32_t userId); 266 int32_t DeleteSkCredAndAcl(const std::vector<DmAclIdParam> &acls); 267 void DeleteCredential(DmAclIdParam &acl); 268 void DeleteAclByTokenId(const int32_t accessTokenId, 269 std::vector<DistributedDeviceProfile::AccessControlProfile> &profiles, 270 std::map<int64_t, DistributedDeviceProfile::AccessControlProfile> &delProfileMap, 271 std::vector<std::pair<int32_t, std::string>> &delACLInfoVec, std::vector<int32_t> &userIdVec); 272 273 void GetDelACLInfoVec(const int32_t &accessTokenId, 274 std::vector<DistributedDeviceProfile::AccessControlProfile> &profiles, 275 std::vector<std::pair<int32_t, std::string>> &delACLInfoVec, std::vector<int32_t> &userIdVec, 276 const uint32_t &userId); 277 278 void DeleteAclByDelProfileMap( 279 const std::map<int64_t, DistributedDeviceProfile::AccessControlProfile> &delProfileMap, 280 std::vector<DistributedDeviceProfile::AccessControlProfile> &profiles, const std::string &localUdid); 281 282 bool CheckLnnAcl(DistributedDeviceProfile::AccessControlProfile delProfile, 283 DistributedDeviceProfile::AccessControlProfile lastprofile); 284 void CheckIsLastLnnAcl(DistributedDeviceProfile::AccessControlProfile profile, 285 DistributedDeviceProfile::AccessControlProfile delProfile, DmOfflineParam &lnnAclParam, 286 bool &isLastLnnAcl, const std::string &localUdid); 287 void BindTargetImpl(uint64_t tokenId, const std::string &pkgName, const PeerTargetId &targetId, 288 const std::map<std::string, std::string> &bindParam); 289 void GetBindCallerInfo(DmBindCallerInfo &bindCallerInfo, const std::string &pkgName); 290 void SetBindCallerInfoToBindParam(const std::map<std::string, std::string> &bindParam, 291 std::map<std::string, std::string> &bindParamTmp, const DmBindCallerInfo &bindCallerInfo); 292 std::string GetBundleLable(const std::string &bundleName); 293 int32_t GetLogicalIdAndTokenIdBySessionId(uint64_t &logicalSessionId, uint64_t &tokenId, int32_t sessionId); 294 void SaveTokenIdAndSessionId(uint64_t &tokenId, int32_t &sessionId, uint64_t &logicalSessionId); 295 void ReleaseMaps(); 296 int32_t InitNewProtocolAuthMgr(bool isSrcSide, uint64_t tokenId, uint64_t logicalSessionId, 297 const std::string &pkgName, int sessionId); 298 int32_t InitOldProtocolAuthMgr(uint64_t tokenId, const std::string &pkgName, int sessionId); 299 bool ParseConnectAddrAndSetProcessInfo(PeerTargetId &targetIdTmp, 300 const std::map<std::string, std::string> &bindParam, ProcessInfo &processInfo, 301 const std::string &pkgName, uint64_t tokenId); 302 void OnAuthResultAndOnBindResult(const ProcessInfo &processInfo, const PeerTargetId &targetId, 303 const std::string &deviceId, int32_t reason, uint64_t tokenId); 304 void GetBundleName(const DMAclQuadInfo &info, std::set<std::string> &pkgNameSet); 305 void DeleteSessionKey(int32_t userId, const DistributedDeviceProfile::AccessControlProfile &profile); 306 private: 307 std::mutex authMgrMtx_; 308 std::shared_ptr<AuthManagerBase> authMgr_; // Old protocol only 309 bool isNeedJoinLnn_ = true; 310 std::mutex isNeedJoinLnnMtx_; 311 std::shared_ptr<HiChainConnector> hiChainConnector_; 312 std::shared_ptr<HiChainAuthConnector> hiChainAuthConnector_; 313 std::shared_ptr<DmDeviceStateManager> deviceStateMgr_; 314 std::shared_ptr<SoftbusConnector> softbusConnector_; 315 std::shared_ptr<DmAbilityManager> abilityMgr_; 316 std::shared_ptr<MineHiChainConnector> mineHiChainConnector_; 317 std::shared_ptr<DmCredentialManager> credentialMgr_; 318 std::shared_ptr<DmCommonEventManager> commonEventManager_; 319 std::shared_ptr<IDeviceManagerServiceListener> listener_; 320 std::atomic<bool> isCredentialType_ = false; 321 std::mutex logoutMutex_; 322 sptr<DpInitedCallback> dpInitedCallback_ = nullptr; 323 324 // The session ID corresponding to the device ID, used only on the src side 325 std::map<std::string, int> deviceId2SessionIdMap_; 326 std::map<int, std::shared_ptr<Session>> sessionsMap_; // sessionId corresponds to the session object 327 std::mutex mapMutex_; // sessionsMap_ lock 328 std::map<int, std::condition_variable> sessionEnableCvMap_; // Condition variable corresponding to the session 329 std::map<int, std::mutex> sessionEnableMutexMap_; // Lock corresponding to the session 330 std::map<int, bool> sessionEnableCvReadyMap_; // Condition variable ready flag 331 std::map<int, bool> sessionStopMap_; // stop flag 332 std::map<int, bool> sessionEnableMap_; // enable flag 333 std::mutex logicalSessionId2TokenIdMapMtx_; 334 std::map<uint64_t, uint64_t> logicalSessionId2TokenIdMap_; // The relationship between logicalSessionId and tokenId 335 std::mutex logicalSessionId2SessionIdMapMtx_; 336 std::map<uint64_t, int> logicalSessionId2SessionIdMap_; // The relationship logicalSessionId and physical sessionId 337 std::mutex configsMapMutex_; 338 std::map<uint64_t, std::shared_ptr<Config>> configsMap_; // Import when authMgr is not initialized 339 std::mutex authMgrMapMtx_; 340 std::map<uint64_t, std::shared_ptr<AuthManagerBase>> authMgrMap_; // New protocol sharing 341 std::mutex tokenIdSessionIdMapMtx_; 342 std::map<uint64_t, int> tokenIdSessionIdMap_; // New protocol sharing 343 }; 344 345 using CreateDMServiceFuncPtr = IDeviceManagerServiceImpl *(*)(void); 346 } // namespace DistributedHardware 347 } // namespace OHOS 348 #endif // OHOS_DM_SERVICE_IMPL_H 349