From f9fe722cad244e86defeb4f30bde166f0cb52f3a Mon Sep 17 00:00:00 2001 From: hhhhs9527 Date: Thu, 30 Jun 2022 09:50:13 +0800 Subject: [PATCH 01/15] 2022/06/30 Signed-off-by:hanshu5 Signed-off-by: hhhhs9527 --- frameworks/common/BUILD.gn | 3 +- frameworks/common/include/constant_common.h | 44 + frameworks/common/src/constant_common.cpp | 49 ++ .../accesstoken/src/accesstoken_kit.cpp | 737 ++++++++-------- .../innerkits/privacy/src/privacy_kit.cpp | 5 +- interfaces/innerkits/tokensync/BUILD.gn | 1 + .../tokensync/src/token_sync_kit.cpp | 107 +-- .../service/accesstoken_manager_service.cpp | 19 +- .../src/token/accesstoken_info_manager.cpp | 67 +- .../accesstoken_remote_token_manager.cpp | 24 +- .../privacymanager/include/common/constant.h | 2 - .../privacymanager/src/common/constant.cpp | 14 - .../src/record/permission_record_manager.cpp | 3 +- .../src/service/privacy_manager_service.cpp | 3 +- .../include/common/constant.h | 20 - .../tokensyncmanager/src/common/constant.cpp | 18 - .../src/remote/remote_command_manager.cpp | 19 +- .../soft_bus_device_connection_listener.cpp | 240 ++--- .../src/remote/soft_bus_manager.cpp | 818 +++++++++--------- .../service/token_sync_manager_service.cpp | 11 +- .../token_sync_service_test.cpp | 3 +- 21 files changed, 1129 insertions(+), 1078 deletions(-) create mode 100644 frameworks/common/include/constant_common.h create mode 100644 frameworks/common/src/constant_common.cpp diff --git a/frameworks/common/BUILD.gn b/frameworks/common/BUILD.gn index e4c4118eb..d873a073d 100644 --- a/frameworks/common/BUILD.gn +++ b/frameworks/common/BUILD.gn @@ -34,13 +34,14 @@ ohos_shared_library("accesstoken_common_cxx") { sources = [ "src/data_validator.cpp", "src/random_mbedtls.cpp", + "src/constant_common.cpp", ] deps = [ "//third_party/mbedtls:mbedtls_shared", "//utils/native/base:utils", ] - external_deps = [ "hiviewdfx_hilog_native:libhilog" ] + external_deps = [ "hiviewdfx_hilog_native:libhilog","startup_l2:syspara", ] cflags_cc = [ "-DHILOG_ENABLE" ] } diff --git a/frameworks/common/include/constant_common.h b/frameworks/common/include/constant_common.h new file mode 100644 index 000000000..b666920c4 --- /dev/null +++ b/frameworks/common/include/constant_common.h @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2022 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 FRAMEWORK_CONSTANT_COMMON_H +#define FRAMEWORK_CONSTANT_COMMON_H + +#include +#include + +namespace OHOS { +namespace Security { +namespace AccessToken { +class ConstantCommon { +public: + /** + * Device id length. + */ + const static int32_t DEVICE_UUID_LENGTH = 65; + const static int32_t DELAY_SYNC_TOKEN_MS = 3000; + static constexpr int32_t ENCRYPTLEN = 4; + static constexpr int32_t ENCRYPTBEGIN = 0; + static constexpr int32_t ENCRYPTEND = 3; + static std::string EncryptDevId(std::string deviceId); + + /** + * GetLocalDeviceId + */ + static std::string GetLocalDeviceId(); +}; +} +} +} +#endif \ No newline at end of file diff --git a/frameworks/common/src/constant_common.cpp b/frameworks/common/src/constant_common.cpp new file mode 100644 index 000000000..feabc4277 --- /dev/null +++ b/frameworks/common/src/constant_common.cpp @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2021-2022 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 "constant_common.h" +#include "parameter.h" + +namespace OHOS { +namespace Security { +namespace AccessToken { +namespace { +static const std::string REPLACE_TARGET = "****"; +} // namespace +std::string ConstantCommon::EncryptDevId(std::string deviceId) +{ + std::string result=deviceId; + if (deviceId.size() > 2*ENCRYPTLEN) { + result.replace(ENCRYPTBEGIN + ENCRYPTLEN, deviceId.size() - 2 * ENCRYPTLEN, REPLACE_TARGET); + } else { + result.replace(ENCRYPTBEGIN, deviceId.size(), REPLACE_TARGET); + } + return result; +} + +std::string ConstantCommon::GetLocalDeviceId() +{ + static std::string localDeviceId; + if (!localDeviceId.empty()) { + return localDeviceId; + } + const int32_t DEVICE_UUID_LENGTH = 65; + char udid[DEVICE_UUID_LENGTH] = {0}; + GetDevUdid(udid, DEVICE_UUID_LENGTH); + localDeviceId = udid; + return localDeviceId; +} +} // namespace AccessToken +} // namespace Security +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/innerkits/accesstoken/src/accesstoken_kit.cpp b/interfaces/innerkits/accesstoken/src/accesstoken_kit.cpp index 66ca1dadc..fd2a1487c 100644 --- a/interfaces/innerkits/accesstoken/src/accesstoken_kit.cpp +++ b/interfaces/innerkits/accesstoken/src/accesstoken_kit.cpp @@ -1,368 +1,369 @@ -/* - * Copyright (c) 2021-2022 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 "accesstoken_kit.h" - -#include -#include - -#include "accesstoken_log.h" -#include "accesstoken_manager_client.h" -#include "data_validator.h" - -namespace OHOS { -namespace Security { -namespace AccessToken { -namespace { -static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_ACCESSTOKEN, "AccessTokenKit"}; -} // namespace - -AccessTokenIDEx AccessTokenKit::AllocHapToken(const HapInfoParams& info, const HapPolicyParams& policy) -{ - AccessTokenIDEx res = {0}; - ACCESSTOKEN_LOG_INFO(LABEL, "%{public}s called", __func__); - if ((!DataValidator::IsUserIdValid(info.userID)) || !DataValidator::IsAppIDDescValid(info.appIDDesc) || - !DataValidator::IsBundleNameValid(info.bundleName) || !DataValidator::IsAplNumValid(policy.apl) || - !DataValidator::IsDomainValid(policy.domain) || !DataValidator::IsDlpTypeValid(info.dlpType)) { - ACCESSTOKEN_LOG_ERROR(LABEL, "input param failed"); - return res; - } - - return AccessTokenManagerClient::GetInstance().AllocHapToken(info, policy); -} - -AccessTokenID AccessTokenKit::AllocLocalTokenID(const std::string& remoteDeviceID, AccessTokenID remoteTokenID) -{ - ACCESSTOKEN_LOG_INFO(LABEL, "%{public}s called, deviceID=%{private}s tokenID=%{public}d", - __func__, remoteDeviceID.c_str(), remoteTokenID); -#ifdef DEBUG_API_PERFORMANCE - ACCESSTOKEN_LOG_INFO(LABEL, "api_performance:start call"); - AccessTokenID resID = AccessTokenManagerClient::GetInstance().AllocLocalTokenID(remoteDeviceID, remoteTokenID); - ACCESSTOKEN_LOG_INFO(LABEL, "api_performance:end call"); - return resID; -#else - return AccessTokenManagerClient::GetInstance().AllocLocalTokenID(remoteDeviceID, remoteTokenID); -#endif -} - -int AccessTokenKit::UpdateHapToken(AccessTokenID tokenID, const std::string& appIDDesc, const HapPolicyParams& policy) -{ - ACCESSTOKEN_LOG_INFO(LABEL, "%{public}s called", __func__); - if ((tokenID == 0) || (!DataValidator::IsAppIDDescValid(appIDDesc)) || - (!DataValidator::IsAplNumValid(policy.apl))) { - ACCESSTOKEN_LOG_ERROR(LABEL, "input param failed"); - return RET_FAILED; - } - return AccessTokenManagerClient::GetInstance().UpdateHapToken(tokenID, appIDDesc, policy); -} - -int AccessTokenKit::DeleteToken(AccessTokenID tokenID) -{ - ACCESSTOKEN_LOG_INFO(LABEL, "%{public}s called", __func__); - if (tokenID == 0) { - ACCESSTOKEN_LOG_ERROR(LABEL, "tokenID is invalid"); - return RET_FAILED; - } - ACCESSTOKEN_LOG_INFO(LABEL, "tokenID=%{public}d", tokenID); - return AccessTokenManagerClient::GetInstance().DeleteToken(tokenID); -} - -ATokenTypeEnum AccessTokenKit::GetTokenType(AccessTokenID tokenID) -{ - ACCESSTOKEN_LOG_INFO(LABEL, "%{public}s called", __func__); - if (tokenID == 0) { - ACCESSTOKEN_LOG_ERROR(LABEL, "tokenID is invalid"); - return TOKEN_INVALID; - } - ACCESSTOKEN_LOG_INFO(LABEL, "tokenID=%{public}d", tokenID); - return AccessTokenManagerClient::GetInstance().GetTokenType(tokenID); -} - -ATokenTypeEnum AccessTokenKit::GetTokenTypeFlag(AccessTokenID tokenID) -{ - if (tokenID == 0) { - return TOKEN_INVALID; - } - AccessTokenIDInner *idInner = reinterpret_cast(&tokenID); - return (ATokenTypeEnum)(idInner->type); -} - -int AccessTokenKit::CheckNativeDCap(AccessTokenID tokenID, const std::string& dcap) -{ - ACCESSTOKEN_LOG_INFO(LABEL, "%{public}s called", __func__); - if (tokenID == 0) { - ACCESSTOKEN_LOG_ERROR(LABEL, "tokenID is invalid"); - return RET_FAILED; - } - if (!DataValidator::IsDcapValid(dcap)) { - ACCESSTOKEN_LOG_ERROR(LABEL, "dcap is invalid"); - return RET_FAILED; - } - ACCESSTOKEN_LOG_INFO(LABEL, "tokenID=%{public}d, dcap=%{public}s", tokenID, dcap.c_str()); - return AccessTokenManagerClient::GetInstance().CheckNativeDCap(tokenID, dcap); -} - -AccessTokenID AccessTokenKit::GetHapTokenID(int userID, const std::string& bundleName, int instIndex) -{ - ACCESSTOKEN_LOG_INFO(LABEL, "%{public}s called", __func__); - if (!DataValidator::IsUserIdValid(userID) || !DataValidator::IsBundleNameValid(bundleName)) { - ACCESSTOKEN_LOG_ERROR(LABEL, "hap token param failed"); - return 0; - } - ACCESSTOKEN_LOG_INFO(LABEL, "int userID=%{public}d, bundleName=%{public}s, instIndex=%{public}d", - userID, bundleName.c_str(), instIndex); - return AccessTokenManagerClient::GetInstance().GetHapTokenID(userID, bundleName, instIndex); -} - -int AccessTokenKit::GetHapTokenInfo(AccessTokenID tokenID, HapTokenInfo& hapTokenInfoRes) -{ - ACCESSTOKEN_LOG_INFO(LABEL, "%{public}s called", __func__); - if (tokenID == 0) { - ACCESSTOKEN_LOG_ERROR(LABEL, "tokenID is invalid"); - return RET_FAILED; - } - ACCESSTOKEN_LOG_INFO(LABEL, "tokenID=%{public}d", tokenID); - - return AccessTokenManagerClient::GetInstance().GetHapTokenInfo(tokenID, hapTokenInfoRes); -} - -int AccessTokenKit::GetNativeTokenInfo(AccessTokenID tokenID, NativeTokenInfo& nativeTokenInfoRes) -{ - ACCESSTOKEN_LOG_INFO(LABEL, "%{public}s called", __func__); - ACCESSTOKEN_LOG_INFO(LABEL, "tokenID=%{public}d", tokenID); - - return AccessTokenManagerClient::GetInstance().GetNativeTokenInfo(tokenID, nativeTokenInfoRes); -} - -PermissionOper AccessTokenKit::GetSelfPermissionsState(std::vector& permList) -{ - ACCESSTOKEN_LOG_INFO(LABEL, "called."); - return AccessTokenManagerClient::GetInstance().GetSelfPermissionsState(permList); -} - -int AccessTokenKit::VerifyAccessToken(AccessTokenID tokenID, const std::string& permissionName) -{ - ACCESSTOKEN_LOG_INFO(LABEL, "%{public}s called", __func__); - if (tokenID == 0) { - ACCESSTOKEN_LOG_ERROR(LABEL, "tokenID is invalid"); - return PERMISSION_DENIED; - } - if (!DataValidator::IsPermissionNameValid(permissionName)) { - ACCESSTOKEN_LOG_ERROR(LABEL, "permissionName is invalid"); - return PERMISSION_DENIED; - } - ACCESSTOKEN_LOG_INFO(LABEL, "tokenID=%{public}d, permissionName=%{public}s", tokenID, permissionName.c_str()); - return AccessTokenManagerClient::GetInstance().VerifyAccessToken(tokenID, permissionName); -} - -int AccessTokenKit::VerifyAccessToken( - AccessTokenID callerTokenID, AccessTokenID firstTokenID, const std::string& permissionName) -{ - int ret = AccessTokenKit::VerifyAccessToken(callerTokenID, permissionName); - if (ret != PERMISSION_GRANTED) { - return ret; - } - if (firstTokenID == FIRSTCALLER_TOKENID_DEFAULT) { - return ret; - } - return AccessTokenKit::VerifyAccessToken(firstTokenID, permissionName); -} - -int AccessTokenKit::VerifyNativeToken(AccessTokenID tokenID, const std::string& permissionName) -{ - ACCESSTOKEN_LOG_INFO(LABEL, "%{public}s called", __func__); - if (tokenID == 0) { - ACCESSTOKEN_LOG_ERROR(LABEL, "tokenID=%{public}d is invalid", tokenID); - return PERMISSION_DENIED; - } - if (!DataValidator::IsPermissionNameValid(permissionName)) { - ACCESSTOKEN_LOG_ERROR(LABEL, "permissionName is invalid"); - return PERMISSION_DENIED; - } - ACCESSTOKEN_LOG_INFO(LABEL, "tokenID=%{public}d, permissionName=%{public}s", tokenID, permissionName.c_str()); - return AccessTokenManagerClient::GetInstance().VerifyNativeToken(tokenID, permissionName); -} - -int AccessTokenKit::GetDefPermission(const std::string& permissionName, PermissionDef& permissionDefResult) -{ - ACCESSTOKEN_LOG_INFO(LABEL, "%{public}s called", __func__); - if (!DataValidator::IsPermissionNameValid(permissionName)) { - ACCESSTOKEN_LOG_ERROR(LABEL, "permissionName is invalid"); - return RET_FAILED; - } - ACCESSTOKEN_LOG_INFO(LABEL, "permissionName=%{public}s", permissionName.c_str()); - - int ret = AccessTokenManagerClient::GetInstance().GetDefPermission(permissionName, permissionDefResult); - ACCESSTOKEN_LOG_INFO(LABEL, "GetDefPermission bundleName = %{public}s", permissionDefResult.bundleName.c_str()); - - return ret; -} - -int AccessTokenKit::GetDefPermissions(AccessTokenID tokenID, std::vector& permDefList) -{ - ACCESSTOKEN_LOG_INFO(LABEL, "%{public}s called", __func__); - if (tokenID == 0) { - ACCESSTOKEN_LOG_ERROR(LABEL, "tokenID is invalid"); - return RET_FAILED; - } - ACCESSTOKEN_LOG_INFO(LABEL, "tokenID=%{public}d", tokenID); - - return AccessTokenManagerClient::GetInstance().GetDefPermissions(tokenID, permDefList); -} - -int AccessTokenKit::GetReqPermissions( - AccessTokenID tokenID, std::vector& reqPermList, bool isSystemGrant) -{ - ACCESSTOKEN_LOG_INFO(LABEL, "%{public}s called", __func__); - if (tokenID == 0) { - ACCESSTOKEN_LOG_ERROR(LABEL, "tokenID is invalid"); - return RET_FAILED; - } - ACCESSTOKEN_LOG_INFO(LABEL, "tokenID=%{public}d, isSystemGrant=%{public}d", tokenID, isSystemGrant); - - return AccessTokenManagerClient::GetInstance().GetReqPermissions(tokenID, reqPermList, isSystemGrant); -} - -int AccessTokenKit::GetPermissionFlag(AccessTokenID tokenID, const std::string& permissionName) -{ - ACCESSTOKEN_LOG_INFO(LABEL, "%{public}s called", __func__); - if (tokenID == 0) { - ACCESSTOKEN_LOG_ERROR(LABEL, "tokenID is invalid"); - return PERMISSION_DEFAULT_FLAG; - } - if (!DataValidator::IsPermissionNameValid(permissionName)) { - ACCESSTOKEN_LOG_ERROR(LABEL, "permissionName is invalid"); - return PERMISSION_DEFAULT_FLAG; - } - ACCESSTOKEN_LOG_INFO(LABEL, "tokenID=%{public}d, permissionName=%{public}s", tokenID, permissionName.c_str()); - return AccessTokenManagerClient::GetInstance().GetPermissionFlag(tokenID, permissionName); -} - -int AccessTokenKit::GrantPermission(AccessTokenID tokenID, const std::string& permissionName, int flag) -{ - ACCESSTOKEN_LOG_INFO(LABEL, "%{public}s called", __func__); - if (tokenID == 0) { - ACCESSTOKEN_LOG_ERROR(LABEL, "tokenID is invalid"); - return RET_FAILED; - } - if (!DataValidator::IsPermissionNameValid(permissionName)) { - ACCESSTOKEN_LOG_ERROR(LABEL, "permissionName is invalid"); - return RET_FAILED; - } - if (!DataValidator::IsPermissionFlagValid(flag)) { - ACCESSTOKEN_LOG_ERROR(LABEL, "flag is invalid"); - return RET_FAILED; - } - ACCESSTOKEN_LOG_INFO(LABEL, "tokenID=%{public}d, permissionName=%{public}s, flag=%{public}d", - tokenID, permissionName.c_str(), flag); - return AccessTokenManagerClient::GetInstance().GrantPermission(tokenID, permissionName, flag); -} - -int AccessTokenKit::RevokePermission(AccessTokenID tokenID, const std::string& permissionName, int flag) -{ - ACCESSTOKEN_LOG_INFO(LABEL, "%{public}s called", __func__); - if (tokenID == 0) { - ACCESSTOKEN_LOG_ERROR(LABEL, "tokenID is invalid"); - return RET_FAILED; - } - if (!DataValidator::IsPermissionNameValid(permissionName)) { - ACCESSTOKEN_LOG_ERROR(LABEL, "permissionName is invalid"); - return RET_FAILED; - } - if (!DataValidator::IsPermissionFlagValid(flag)) { - ACCESSTOKEN_LOG_ERROR(LABEL, "flag is invalid"); - return RET_FAILED; - } - ACCESSTOKEN_LOG_INFO(LABEL, "tokenID=%{public}d, permissionName=%{public}s, flag=%{public}d", - tokenID, permissionName.c_str(), flag); - return AccessTokenManagerClient::GetInstance().RevokePermission(tokenID, permissionName, flag); -} - -int AccessTokenKit::ClearUserGrantedPermissionState(AccessTokenID tokenID) -{ - ACCESSTOKEN_LOG_INFO(LABEL, "%{public}s called", __func__); - if (tokenID == 0) { - ACCESSTOKEN_LOG_ERROR(LABEL, "tokenID is invalid"); - return RET_FAILED; - } - ACCESSTOKEN_LOG_INFO(LABEL, "tokenID=%{public}d", tokenID); - return AccessTokenManagerClient::GetInstance().ClearUserGrantedPermissionState(tokenID); -} - -#ifdef TOKEN_SYNC_ENABLE -int AccessTokenKit::GetHapTokenInfoFromRemote(AccessTokenID tokenID, HapTokenInfoForSync& hapSync) -{ - ACCESSTOKEN_LOG_INFO(LABEL, "%{public}s called", __func__); - if (tokenID == 0) { - ACCESSTOKEN_LOG_ERROR(LABEL, "tokenID is invalid"); - return RET_FAILED; - } - ACCESSTOKEN_LOG_INFO(LABEL, "tokenID=%{public}d", tokenID); - - return AccessTokenManagerClient::GetInstance().GetHapTokenInfoFromRemote(tokenID, hapSync); -} - -int AccessTokenKit::GetAllNativeTokenInfo(std::vector& nativeTokenInfosRes) -{ - ACCESSTOKEN_LOG_INFO(LABEL, "%{public}s called", __func__); - - return AccessTokenManagerClient::GetInstance().GetAllNativeTokenInfo(nativeTokenInfosRes); -} - -int AccessTokenKit::SetRemoteHapTokenInfo(const std::string& deviceID, - const HapTokenInfoForSync& hapSync) -{ - ACCESSTOKEN_LOG_INFO(LABEL, "%{public}s called, deviceID=%{private}s tokenID=%{public}d", - __func__, deviceID.c_str(), hapSync.baseInfo.tokenID); - return AccessTokenManagerClient::GetInstance().SetRemoteHapTokenInfo(deviceID, hapSync); -} - -int AccessTokenKit::SetRemoteNativeTokenInfo(const std::string& deviceID, - std::vector& nativeTokenInfoList) -{ - ACCESSTOKEN_LOG_INFO(LABEL, "%{public}s called, deviceID=%{private}s", __func__, deviceID.c_str()); - return AccessTokenManagerClient::GetInstance() - .SetRemoteNativeTokenInfo(deviceID, nativeTokenInfoList); -} - -int AccessTokenKit::DeleteRemoteToken(const std::string& deviceID, AccessTokenID tokenID) -{ - ACCESSTOKEN_LOG_INFO(LABEL, "%{public}s called, deviceID=%{private}s tokenID=%{public}d", - __func__, deviceID.c_str(), tokenID); - return AccessTokenManagerClient::GetInstance().DeleteRemoteToken(deviceID, tokenID); -} - -int AccessTokenKit::DeleteRemoteDeviceTokens(const std::string& deviceID) -{ - ACCESSTOKEN_LOG_INFO(LABEL, "%{public}s called, deviceID=%{private}s", __func__, deviceID.c_str()); - return AccessTokenManagerClient::GetInstance().DeleteRemoteDeviceTokens(deviceID); -} - -AccessTokenID AccessTokenKit::GetRemoteNativeTokenID(const std::string& deviceID, AccessTokenID tokenID) -{ - ACCESSTOKEN_LOG_INFO(LABEL, "%{public}s called, deviceID=%{private}s tokenID=%{public}d", - __func__, deviceID.c_str(), tokenID); - return AccessTokenManagerClient::GetInstance().GetRemoteNativeTokenID(deviceID, tokenID); -} -#endif - -void AccessTokenKit::DumpTokenInfo(std::string& dumpInfo) -{ - ACCESSTOKEN_LOG_INFO(LABEL, "%{public}s called", __func__); - AccessTokenManagerClient::GetInstance().DumpTokenInfo(dumpInfo); -} -} // namespace AccessToken -} // namespace Security -} // namespace OHOS +/* + * Copyright (c) 2021-2022 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 "accesstoken_kit.h" + +#include +#include + +#include "accesstoken_log.h" +#include "accesstoken_manager_client.h" +#include "constant_common.h" +#include "data_validator.h" + +namespace OHOS { +namespace Security { +namespace AccessToken { +namespace { +static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_ACCESSTOKEN, "AccessTokenKit"}; +} // namespace + +AccessTokenIDEx AccessTokenKit::AllocHapToken(const HapInfoParams& info, const HapPolicyParams& policy) +{ + AccessTokenIDEx res = {0}; + ACCESSTOKEN_LOG_INFO(LABEL, "%{public}s called", __func__); + if ((!DataValidator::IsUserIdValid(info.userID)) || !DataValidator::IsAppIDDescValid(info.appIDDesc) || + !DataValidator::IsBundleNameValid(info.bundleName) || !DataValidator::IsAplNumValid(policy.apl) || + !DataValidator::IsDomainValid(policy.domain) || !DataValidator::IsDlpTypeValid(info.dlpType)) { + ACCESSTOKEN_LOG_ERROR(LABEL, "input param failed"); + return res; + } + + return AccessTokenManagerClient::GetInstance().AllocHapToken(info, policy); +} + +AccessTokenID AccessTokenKit::AllocLocalTokenID(const std::string& remoteDeviceID, AccessTokenID remoteTokenID) +{ + ACCESSTOKEN_LOG_INFO(LABEL, "%{public}s called, deviceID=%{public}s tokenID=%{public}d", + __func__, ConstantCommon::EncryptDevId(remoteDeviceID).c_str(), remoteTokenID); +#ifdef DEBUG_API_PERFORMANCE + ACCESSTOKEN_LOG_INFO(LABEL, "api_performance:start call"); + AccessTokenID resID = AccessTokenManagerClient::GetInstance().AllocLocalTokenID(remoteDeviceID, remoteTokenID); + ACCESSTOKEN_LOG_INFO(LABEL, "api_performance:end call"); + return resID; +#else + return AccessTokenManagerClient::GetInstance().AllocLocalTokenID(remoteDeviceID, remoteTokenID); +#endif +} + +int AccessTokenKit::UpdateHapToken(AccessTokenID tokenID, const std::string& appIDDesc, const HapPolicyParams& policy) +{ + ACCESSTOKEN_LOG_INFO(LABEL, "%{public}s called", __func__); + if ((tokenID == 0) || (!DataValidator::IsAppIDDescValid(appIDDesc)) || + (!DataValidator::IsAplNumValid(policy.apl))) { + ACCESSTOKEN_LOG_ERROR(LABEL, "input param failed"); + return RET_FAILED; + } + return AccessTokenManagerClient::GetInstance().UpdateHapToken(tokenID, appIDDesc, policy); +} + +int AccessTokenKit::DeleteToken(AccessTokenID tokenID) +{ + ACCESSTOKEN_LOG_INFO(LABEL, "%{public}s called", __func__); + if (tokenID == 0) { + ACCESSTOKEN_LOG_ERROR(LABEL, "tokenID is invalid"); + return RET_FAILED; + } + ACCESSTOKEN_LOG_INFO(LABEL, "tokenID=%{public}d", tokenID); + return AccessTokenManagerClient::GetInstance().DeleteToken(tokenID); +} + +ATokenTypeEnum AccessTokenKit::GetTokenType(AccessTokenID tokenID) +{ + ACCESSTOKEN_LOG_INFO(LABEL, "%{public}s called", __func__); + if (tokenID == 0) { + ACCESSTOKEN_LOG_ERROR(LABEL, "tokenID is invalid"); + return TOKEN_INVALID; + } + ACCESSTOKEN_LOG_INFO(LABEL, "tokenID=%{public}d", tokenID); + return AccessTokenManagerClient::GetInstance().GetTokenType(tokenID); +} + +ATokenTypeEnum AccessTokenKit::GetTokenTypeFlag(AccessTokenID tokenID) +{ + if (tokenID == 0) { + return TOKEN_INVALID; + } + AccessTokenIDInner *idInner = reinterpret_cast(&tokenID); + return (ATokenTypeEnum)(idInner->type); +} + +int AccessTokenKit::CheckNativeDCap(AccessTokenID tokenID, const std::string& dcap) +{ + ACCESSTOKEN_LOG_INFO(LABEL, "%{public}s called", __func__); + if (tokenID == 0) { + ACCESSTOKEN_LOG_ERROR(LABEL, "tokenID is invalid"); + return RET_FAILED; + } + if (!DataValidator::IsDcapValid(dcap)) { + ACCESSTOKEN_LOG_ERROR(LABEL, "dcap is invalid"); + return RET_FAILED; + } + ACCESSTOKEN_LOG_INFO(LABEL, "tokenID=%{public}d, dcap=%{public}s", tokenID, dcap.c_str()); + return AccessTokenManagerClient::GetInstance().CheckNativeDCap(tokenID, dcap); +} + +AccessTokenID AccessTokenKit::GetHapTokenID(int userID, const std::string& bundleName, int instIndex) +{ + ACCESSTOKEN_LOG_INFO(LABEL, "%{public}s called", __func__); + if (!DataValidator::IsUserIdValid(userID) || !DataValidator::IsBundleNameValid(bundleName)) { + ACCESSTOKEN_LOG_ERROR(LABEL, "hap token param failed"); + return 0; + } + ACCESSTOKEN_LOG_INFO(LABEL, "int userID=%{public}d, bundleName=%{public}s, instIndex=%{public}d", + userID, bundleName.c_str(), instIndex); + return AccessTokenManagerClient::GetInstance().GetHapTokenID(userID, bundleName, instIndex); +} + +int AccessTokenKit::GetHapTokenInfo(AccessTokenID tokenID, HapTokenInfo& hapTokenInfoRes) +{ + ACCESSTOKEN_LOG_INFO(LABEL, "%{public}s called", __func__); + if (tokenID == 0) { + ACCESSTOKEN_LOG_ERROR(LABEL, "tokenID is invalid"); + return RET_FAILED; + } + ACCESSTOKEN_LOG_INFO(LABEL, "tokenID=%{public}d", tokenID); + + return AccessTokenManagerClient::GetInstance().GetHapTokenInfo(tokenID, hapTokenInfoRes); +} + +int AccessTokenKit::GetNativeTokenInfo(AccessTokenID tokenID, NativeTokenInfo& nativeTokenInfoRes) +{ + ACCESSTOKEN_LOG_INFO(LABEL, "%{public}s called", __func__); + ACCESSTOKEN_LOG_INFO(LABEL, "tokenID=%{public}d", tokenID); + + return AccessTokenManagerClient::GetInstance().GetNativeTokenInfo(tokenID, nativeTokenInfoRes); +} + +PermissionOper AccessTokenKit::GetSelfPermissionsState(std::vector& permList) +{ + ACCESSTOKEN_LOG_INFO(LABEL, "called."); + return AccessTokenManagerClient::GetInstance().GetSelfPermissionsState(permList); +} + +int AccessTokenKit::VerifyAccessToken(AccessTokenID tokenID, const std::string& permissionName) +{ + ACCESSTOKEN_LOG_INFO(LABEL, "%{public}s called", __func__); + if (tokenID == 0) { + ACCESSTOKEN_LOG_ERROR(LABEL, "tokenID is invalid"); + return PERMISSION_DENIED; + } + if (!DataValidator::IsPermissionNameValid(permissionName)) { + ACCESSTOKEN_LOG_ERROR(LABEL, "permissionName is invalid"); + return PERMISSION_DENIED; + } + ACCESSTOKEN_LOG_INFO(LABEL, "tokenID=%{public}d, permissionName=%{public}s", tokenID, permissionName.c_str()); + return AccessTokenManagerClient::GetInstance().VerifyAccessToken(tokenID, permissionName); +} + +int AccessTokenKit::VerifyAccessToken( + AccessTokenID callerTokenID, AccessTokenID firstTokenID, const std::string& permissionName) +{ + int ret = AccessTokenKit::VerifyAccessToken(callerTokenID, permissionName); + if (ret != PERMISSION_GRANTED) { + return ret; + } + if (firstTokenID == FIRSTCALLER_TOKENID_DEFAULT) { + return ret; + } + return AccessTokenKit::VerifyAccessToken(firstTokenID, permissionName); +} + +int AccessTokenKit::VerifyNativeToken(AccessTokenID tokenID, const std::string& permissionName) +{ + ACCESSTOKEN_LOG_INFO(LABEL, "%{public}s called", __func__); + if (tokenID == 0) { + ACCESSTOKEN_LOG_ERROR(LABEL, "tokenID=%{public}d is invalid", tokenID); + return PERMISSION_DENIED; + } + if (!DataValidator::IsPermissionNameValid(permissionName)) { + ACCESSTOKEN_LOG_ERROR(LABEL, "permissionName is invalid"); + return PERMISSION_DENIED; + } + ACCESSTOKEN_LOG_INFO(LABEL, "tokenID=%{public}d, permissionName=%{public}s", tokenID, permissionName.c_str()); + return AccessTokenManagerClient::GetInstance().VerifyNativeToken(tokenID, permissionName); +} + +int AccessTokenKit::GetDefPermission(const std::string& permissionName, PermissionDef& permissionDefResult) +{ + ACCESSTOKEN_LOG_INFO(LABEL, "%{public}s called", __func__); + if (!DataValidator::IsPermissionNameValid(permissionName)) { + ACCESSTOKEN_LOG_ERROR(LABEL, "permissionName is invalid"); + return RET_FAILED; + } + ACCESSTOKEN_LOG_INFO(LABEL, "permissionName=%{public}s", permissionName.c_str()); + + int ret = AccessTokenManagerClient::GetInstance().GetDefPermission(permissionName, permissionDefResult); + ACCESSTOKEN_LOG_INFO(LABEL, "GetDefPermission bundleName = %{public}s", permissionDefResult.bundleName.c_str()); + + return ret; +} + +int AccessTokenKit::GetDefPermissions(AccessTokenID tokenID, std::vector& permDefList) +{ + ACCESSTOKEN_LOG_INFO(LABEL, "%{public}s called", __func__); + if (tokenID == 0) { + ACCESSTOKEN_LOG_ERROR(LABEL, "tokenID is invalid"); + return RET_FAILED; + } + ACCESSTOKEN_LOG_INFO(LABEL, "tokenID=%{public}d", tokenID); + + return AccessTokenManagerClient::GetInstance().GetDefPermissions(tokenID, permDefList); +} + +int AccessTokenKit::GetReqPermissions( + AccessTokenID tokenID, std::vector& reqPermList, bool isSystemGrant) +{ + ACCESSTOKEN_LOG_INFO(LABEL, "%{public}s called", __func__); + if (tokenID == 0) { + ACCESSTOKEN_LOG_ERROR(LABEL, "tokenID is invalid"); + return RET_FAILED; + } + ACCESSTOKEN_LOG_INFO(LABEL, "tokenID=%{public}d, isSystemGrant=%{public}d", tokenID, isSystemGrant); + + return AccessTokenManagerClient::GetInstance().GetReqPermissions(tokenID, reqPermList, isSystemGrant); +} + +int AccessTokenKit::GetPermissionFlag(AccessTokenID tokenID, const std::string& permissionName) +{ + ACCESSTOKEN_LOG_INFO(LABEL, "%{public}s called", __func__); + if (tokenID == 0) { + ACCESSTOKEN_LOG_ERROR(LABEL, "tokenID is invalid"); + return PERMISSION_DEFAULT_FLAG; + } + if (!DataValidator::IsPermissionNameValid(permissionName)) { + ACCESSTOKEN_LOG_ERROR(LABEL, "permissionName is invalid"); + return PERMISSION_DEFAULT_FLAG; + } + ACCESSTOKEN_LOG_INFO(LABEL, "tokenID=%{public}d, permissionName=%{public}s", tokenID, permissionName.c_str()); + return AccessTokenManagerClient::GetInstance().GetPermissionFlag(tokenID, permissionName); +} + +int AccessTokenKit::GrantPermission(AccessTokenID tokenID, const std::string& permissionName, int flag) +{ + ACCESSTOKEN_LOG_INFO(LABEL, "%{public}s called", __func__); + if (tokenID == 0) { + ACCESSTOKEN_LOG_ERROR(LABEL, "tokenID is invalid"); + return RET_FAILED; + } + if (!DataValidator::IsPermissionNameValid(permissionName)) { + ACCESSTOKEN_LOG_ERROR(LABEL, "permissionName is invalid"); + return RET_FAILED; + } + if (!DataValidator::IsPermissionFlagValid(flag)) { + ACCESSTOKEN_LOG_ERROR(LABEL, "flag is invalid"); + return RET_FAILED; + } + ACCESSTOKEN_LOG_INFO(LABEL, "tokenID=%{public}d, permissionName=%{public}s, flag=%{public}d", + tokenID, permissionName.c_str(), flag); + return AccessTokenManagerClient::GetInstance().GrantPermission(tokenID, permissionName, flag); +} + +int AccessTokenKit::RevokePermission(AccessTokenID tokenID, const std::string& permissionName, int flag) +{ + ACCESSTOKEN_LOG_INFO(LABEL, "%{public}s called", __func__); + if (tokenID == 0) { + ACCESSTOKEN_LOG_ERROR(LABEL, "tokenID is invalid"); + return RET_FAILED; + } + if (!DataValidator::IsPermissionNameValid(permissionName)) { + ACCESSTOKEN_LOG_ERROR(LABEL, "permissionName is invalid"); + return RET_FAILED; + } + if (!DataValidator::IsPermissionFlagValid(flag)) { + ACCESSTOKEN_LOG_ERROR(LABEL, "flag is invalid"); + return RET_FAILED; + } + ACCESSTOKEN_LOG_INFO(LABEL, "tokenID=%{public}d, permissionName=%{public}s, flag=%{public}d", + tokenID, permissionName.c_str(), flag); + return AccessTokenManagerClient::GetInstance().RevokePermission(tokenID, permissionName, flag); +} + +int AccessTokenKit::ClearUserGrantedPermissionState(AccessTokenID tokenID) +{ + ACCESSTOKEN_LOG_INFO(LABEL, "%{public}s called", __func__); + if (tokenID == 0) { + ACCESSTOKEN_LOG_ERROR(LABEL, "tokenID is invalid"); + return RET_FAILED; + } + ACCESSTOKEN_LOG_INFO(LABEL, "tokenID=%{public}d", tokenID); + return AccessTokenManagerClient::GetInstance().ClearUserGrantedPermissionState(tokenID); +} + +#ifdef TOKEN_SYNC_ENABLE +int AccessTokenKit::GetHapTokenInfoFromRemote(AccessTokenID tokenID, HapTokenInfoForSync& hapSync) +{ + ACCESSTOKEN_LOG_INFO(LABEL, "%{public}s called", __func__); + if (tokenID == 0) { + ACCESSTOKEN_LOG_ERROR(LABEL, "tokenID is invalid"); + return RET_FAILED; + } + ACCESSTOKEN_LOG_INFO(LABEL, "tokenID=%{public}d", tokenID); + + return AccessTokenManagerClient::GetInstance().GetHapTokenInfoFromRemote(tokenID, hapSync); +} + +int AccessTokenKit::GetAllNativeTokenInfo(std::vector& nativeTokenInfosRes) +{ + ACCESSTOKEN_LOG_INFO(LABEL, "%{public}s called", __func__); + + return AccessTokenManagerClient::GetInstance().GetAllNativeTokenInfo(nativeTokenInfosRes); +} + +int AccessTokenKit::SetRemoteHapTokenInfo(const std::string& deviceID, + const HapTokenInfoForSync& hapSync) +{ + ACCESSTOKEN_LOG_INFO(LABEL, "%{public}s called, deviceID=%{public}s tokenID=%{public}d", + __func__, ConstantCommon::EncryptDevId(deviceID).c_str(), hapSync.baseInfo.tokenID); + return AccessTokenManagerClient::GetInstance().SetRemoteHapTokenInfo(deviceID, hapSync); +} + +int AccessTokenKit::SetRemoteNativeTokenInfo(const std::string& deviceID, + std::vector& nativeTokenInfoList) +{ + ACCESSTOKEN_LOG_INFO(LABEL, "%{public}s called, deviceID=%{public}s", __func__, ConstantCommon::EncryptDevId(deviceID).c_str()); + return AccessTokenManagerClient::GetInstance() + .SetRemoteNativeTokenInfo(deviceID, nativeTokenInfoList); +} + +int AccessTokenKit::DeleteRemoteToken(const std::string& deviceID, AccessTokenID tokenID) +{ + ACCESSTOKEN_LOG_INFO(LABEL, "%{public}s called, deviceID=%{public}s tokenID=%{public}d", + __func__, ConstantCommon::EncryptDevId(deviceID).c_str(), tokenID); + return AccessTokenManagerClient::GetInstance().DeleteRemoteToken(deviceID, tokenID); +} + +int AccessTokenKit::DeleteRemoteDeviceTokens(const std::string& deviceID) +{ + ACCESSTOKEN_LOG_INFO(LABEL, "%{public}s called, deviceID=%{public}s", __func__, ConstantCommon::EncryptDevId(deviceID).c_str()); + return AccessTokenManagerClient::GetInstance().DeleteRemoteDeviceTokens(deviceID); +} + +AccessTokenID AccessTokenKit::GetRemoteNativeTokenID(const std::string& deviceID, AccessTokenID tokenID) +{ + ACCESSTOKEN_LOG_INFO(LABEL, "%{public}s called, deviceID=%{public}s tokenID=%{public}d", + __func__, ConstantCommon::EncryptDevId(deviceID).c_str(), tokenID); + return AccessTokenManagerClient::GetInstance().GetRemoteNativeTokenID(deviceID, tokenID); +} +#endif + +void AccessTokenKit::DumpTokenInfo(std::string& dumpInfo) +{ + ACCESSTOKEN_LOG_INFO(LABEL, "%{public}s called", __func__); + AccessTokenManagerClient::GetInstance().DumpTokenInfo(dumpInfo); +} +} // namespace AccessToken +} // namespace Security +} // namespace OHOS diff --git a/interfaces/innerkits/privacy/src/privacy_kit.cpp b/interfaces/innerkits/privacy/src/privacy_kit.cpp index 75ef66a5f..bca1ed93d 100644 --- a/interfaces/innerkits/privacy/src/privacy_kit.cpp +++ b/interfaces/innerkits/privacy/src/privacy_kit.cpp @@ -19,6 +19,7 @@ #include #include "accesstoken_log.h" +#include "constant_common.h" #include "privacy_manager_client.h" namespace OHOS { @@ -53,8 +54,8 @@ int32_t PrivacyKit::StopUsingPermission(AccessTokenID tokenID, const std::string int32_t PrivacyKit::RemovePermissionUsedRecords(AccessTokenID tokenID, const std::string& deviceID) { - ACCESSTOKEN_LOG_DEBUG(LABEL, "Entry, tokenID=0x%{public}x, deviceID=%{private}s", - tokenID, deviceID.c_str()); + ACCESSTOKEN_LOG_DEBUG(LABEL, "Entry, tokenID=0x%{public}x, deviceID=%{public}s", + tokenID, ConstantCommon::EncryptDevId(deviceID).c_str()); return PrivacyManagerClient::GetInstance().RemovePermissionUsedRecords(tokenID, deviceID); } diff --git a/interfaces/innerkits/tokensync/BUILD.gn b/interfaces/innerkits/tokensync/BUILD.gn index 991849b9f..f9bc3b6fc 100644 --- a/interfaces/innerkits/tokensync/BUILD.gn +++ b/interfaces/innerkits/tokensync/BUILD.gn @@ -50,6 +50,7 @@ ohos_shared_library("libtokensync_sdk") { deps = [ "//base/security/access_token/frameworks/accesstoken:accesstoken_communication_adapter_cxx", "//utils/native/base:utils", + "//base/security/access_token/frameworks/common:accesstoken_common_cxx", ] external_deps = [ diff --git a/interfaces/innerkits/tokensync/src/token_sync_kit.cpp b/interfaces/innerkits/tokensync/src/token_sync_kit.cpp index 415cf1f0f..28de089ff 100644 --- a/interfaces/innerkits/tokensync/src/token_sync_kit.cpp +++ b/interfaces/innerkits/tokensync/src/token_sync_kit.cpp @@ -1,53 +1,54 @@ -/* - * Copyright (c) 2022 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 "token_sync_kit.h" - -#include -#include - -#include "accesstoken_log.h" -#include "token_sync_manager_client.h" - -namespace OHOS { -namespace Security { -namespace AccessToken { -using namespace std; - -namespace { -static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_ACCESSTOKEN, "TokenSyncKit"}; -} // namespace - -int TokenSyncKit::GetRemoteHapTokenInfo(const std::string& deviceID, AccessTokenID tokenID) -{ - ACCESSTOKEN_LOG_INFO(LABEL, "%{public}s called, deviceID=%{private}s tokenID=%{public}d", - __func__, deviceID.c_str(), tokenID); - return TokenSyncManagerClient::GetInstance().GetRemoteHapTokenInfo(deviceID, tokenID); -} - -int TokenSyncKit::DeleteRemoteHapTokenInfo(AccessTokenID tokenID) -{ - ACCESSTOKEN_LOG_INFO(LABEL, "%{public}s called, tokenID=%{public}d", __func__, tokenID); - return TokenSyncManagerClient::GetInstance().DeleteRemoteHapTokenInfo(tokenID); -} - -int TokenSyncKit::UpdateRemoteHapTokenInfo(const HapTokenInfoForSync& tokenInfo) -{ - ACCESSTOKEN_LOG_INFO(LABEL, "%{public}s called tokenID=%{public}d", __func__, tokenInfo.baseInfo.tokenID); - return TokenSyncManagerClient::GetInstance().UpdateRemoteHapTokenInfo(tokenInfo); -} -} // namespace AccessToken -} // namespace Security -} // namespace OHOS +/* + * Copyright (c) 2022 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 "token_sync_kit.h" + +#include +#include + +#include "accesstoken_log.h" +#include "constant_common.h" +#include "token_sync_manager_client.h" + +namespace OHOS { +namespace Security { +namespace AccessToken { +using namespace std; + +namespace { +static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_ACCESSTOKEN, "TokenSyncKit"}; +} // namespace + +int TokenSyncKit::GetRemoteHapTokenInfo(const std::string& deviceID, AccessTokenID tokenID) +{ + ACCESSTOKEN_LOG_INFO(LABEL, "%{public}s called, deviceID=%{public}s tokenID=%{public}d", + __func__, ConstantCommon::EncryptDevId(deviceID).c_str(), tokenID); + return TokenSyncManagerClient::GetInstance().GetRemoteHapTokenInfo(deviceID, tokenID); +} + +int TokenSyncKit::DeleteRemoteHapTokenInfo(AccessTokenID tokenID) +{ + ACCESSTOKEN_LOG_INFO(LABEL, "%{public}s called, tokenID=%{public}d", __func__, tokenID); + return TokenSyncManagerClient::GetInstance().DeleteRemoteHapTokenInfo(tokenID); +} + +int TokenSyncKit::UpdateRemoteHapTokenInfo(const HapTokenInfoForSync& tokenInfo) +{ + ACCESSTOKEN_LOG_INFO(LABEL, "%{public}s called tokenID=%{public}d", __func__, tokenInfo.baseInfo.tokenID); + return TokenSyncManagerClient::GetInstance().UpdateRemoteHapTokenInfo(tokenInfo); +} +} // namespace AccessToken +} // namespace Security +} // namespace OHOS diff --git a/services/accesstokenmanager/main/cpp/src/service/accesstoken_manager_service.cpp b/services/accesstokenmanager/main/cpp/src/service/accesstoken_manager_service.cpp index 24fcdf6aa..ef810e3e1 100644 --- a/services/accesstokenmanager/main/cpp/src/service/accesstoken_manager_service.cpp +++ b/services/accesstokenmanager/main/cpp/src/service/accesstoken_manager_service.cpp @@ -25,6 +25,7 @@ #include "atm_device_state_callback.h" #include "device_manager.h" #endif +#include "constant_common.h" #include "hap_token_info.h" #include "hap_token_info_inner.h" #include "ipc_skeleton.h" @@ -247,8 +248,8 @@ AccessTokenID AccessTokenManagerService::GetHapTokenID(int userID, const std::st AccessTokenID AccessTokenManagerService::AllocLocalTokenID( const std::string& remoteDeviceID, AccessTokenID remoteTokenID) { - ACCESSTOKEN_LOG_INFO(LABEL, "called, remoteDeviceID: %{private}s, remoteTokenID: %{public}d", - remoteDeviceID.c_str(), remoteTokenID); + ACCESSTOKEN_LOG_INFO(LABEL, "called, remoteDeviceID: %{public}s, remoteTokenID: %{public}d", + ConstantCommon::EncryptDevId(remoteDeviceID).c_str(), remoteTokenID); return AccessTokenInfoManager::GetInstance().AllocLocalTokenID(remoteDeviceID, remoteTokenID); } @@ -303,7 +304,7 @@ int AccessTokenManagerService::GetAllNativeTokenInfo(std::vector& nativeTokenInfoForSyncParcel) { - ACCESSTOKEN_LOG_INFO(LABEL, "called, deviceID: %{private}s", deviceID.c_str()); + ACCESSTOKEN_LOG_INFO(LABEL, "called, deviceID: %{public}s", ConstantCommon::EncryptDevId(deviceID).c_str()); std::vector nativeList; @@ -325,8 +326,8 @@ int AccessTokenManagerService::SetRemoteNativeTokenInfo(const std::string& devic int AccessTokenManagerService::DeleteRemoteToken(const std::string& deviceID, AccessTokenID tokenID) { - ACCESSTOKEN_LOG_INFO(LABEL, "called, deviceID: %{private}s, token id %{public}d", - deviceID.c_str(), tokenID); + ACCESSTOKEN_LOG_INFO(LABEL, "called, deviceID: %{public}s, token id %{public}d", + ConstantCommon::EncryptDevId(deviceID).c_str(), tokenID); return AccessTokenInfoManager::GetInstance().DeleteRemoteToken(deviceID, tokenID); } @@ -334,15 +335,15 @@ int AccessTokenManagerService::DeleteRemoteToken(const std::string& deviceID, Ac AccessTokenID AccessTokenManagerService::GetRemoteNativeTokenID(const std::string& deviceID, AccessTokenID tokenID) { - ACCESSTOKEN_LOG_INFO(LABEL, "called, deviceID: %{private}s, token id %{public}d", - deviceID.c_str(), tokenID); + ACCESSTOKEN_LOG_INFO(LABEL, "called, deviceID: %{public}s, token id %{public}d", + ConstantCommon::EncryptDevId(deviceID).c_str(), tokenID); return AccessTokenInfoManager::GetInstance().GetRemoteNativeTokenID(deviceID, tokenID); } int AccessTokenManagerService::DeleteRemoteDeviceTokens(const std::string& deviceID) { - ACCESSTOKEN_LOG_INFO(LABEL, "called, deviceID: %{private}s", deviceID.c_str()); + ACCESSTOKEN_LOG_INFO(LABEL, "called, deviceID: %{public}s", ConstantCommon::EncryptDevId(deviceID).c_str()); return AccessTokenInfoManager::GetInstance().DeleteRemoteDeviceTokens(deviceID); } diff --git a/services/accesstokenmanager/main/cpp/src/token/accesstoken_info_manager.cpp b/services/accesstokenmanager/main/cpp/src/token/accesstoken_info_manager.cpp index e8f8a0366..aae03e9eb 100644 --- a/services/accesstokenmanager/main/cpp/src/token/accesstoken_info_manager.cpp +++ b/services/accesstokenmanager/main/cpp/src/token/accesstoken_info_manager.cpp @@ -19,6 +19,7 @@ #include "accesstoken_id_manager.h" #include "accesstoken_log.h" #include "accesstoken_remote_token_manager.h" +#include "constant_common.h" #include "data_storage.h" #include "data_translator.h" #include "data_validator.h" @@ -647,15 +648,15 @@ int AccessTokenInfoManager::SetRemoteHapTokenInfo(const std::string& deviceID, H || !DataValidator::IsDlpTypeValid(hapSync.baseInfo.dlpType) || hapSync.baseInfo.ver != DEFAULT_TOKEN_VERSION || AccessTokenIDManager::GetInstance().GetTokenIdTypeEnum(hapSync.baseInfo.tokenID) != TOKEN_HAP) { - ACCESSTOKEN_LOG_ERROR(LABEL, "device %{private}s parms invalid", deviceID.c_str()); + ACCESSTOKEN_LOG_ERROR(LABEL, "device %{public}s parms invalid", ConstantCommon::EncryptDevId(deviceID).c_str()); return RET_FAILED; } AccessTokenID remoteID = hapSync.baseInfo.tokenID; AccessTokenID mapID = AccessTokenRemoteTokenManager::GetInstance().GetDeviceMappingTokenID(deviceID, remoteID); if (mapID != 0) { - ACCESSTOKEN_LOG_INFO(LABEL, "device %{private}s token %{public}u update exist remote hap token %{public}u.", - deviceID.c_str(), remoteID, mapID); + ACCESSTOKEN_LOG_INFO(LABEL, "device %{public}s token %{public}u update exist remote hap token %{public}u.", + ConstantCommon::EncryptDevId(deviceID).c_str(), remoteID, mapID); // update remote token mapping id hapSync.baseInfo.tokenID = mapID; hapSync.baseInfo.deviceID = deviceID; @@ -665,7 +666,7 @@ int AccessTokenInfoManager::SetRemoteHapTokenInfo(const std::string& deviceID, H mapID = AccessTokenRemoteTokenManager::GetInstance().MapRemoteDeviceTokenToLocal(deviceID, remoteID); if (mapID == 0) { ACCESSTOKEN_LOG_ERROR( - LABEL, "device %{private}s token %{public}u map failed.", deviceID.c_str(), remoteID); + LABEL, "device %{public}s token %{public}u map failed.", ConstantCommon::EncryptDevId(deviceID).c_str(), remoteID); return RET_FAILED; } @@ -675,12 +676,12 @@ int AccessTokenInfoManager::SetRemoteHapTokenInfo(const std::string& deviceID, H if (CreateRemoteHapTokenInfo(mapID, hapSync) == RET_FAILED) { AccessTokenRemoteTokenManager::GetInstance().RemoveDeviceMappingTokenID(deviceID, mapID); - ACCESSTOKEN_LOG_INFO(LABEL, "device %{private}s token %{public}u map to local token %{public}u failed.", - deviceID.c_str(), remoteID, mapID); + ACCESSTOKEN_LOG_INFO(LABEL, "device %{public}s token %{public}u map to local token %{public}u failed.", + ConstantCommon::EncryptDevId(deviceID).c_str(), remoteID, mapID); return RET_FAILED; } - ACCESSTOKEN_LOG_INFO(LABEL, "device %{private}s token %{public}u map to local token %{public}u success.", - deviceID.c_str(), remoteID, mapID); + ACCESSTOKEN_LOG_INFO(LABEL, "device %{public}s token %{public}u map to local token %{public}u success.", + ConstantCommon::EncryptDevId(deviceID).c_str(), remoteID, mapID); return RET_SUCCESS; } @@ -688,7 +689,7 @@ int AccessTokenInfoManager::SetRemoteNativeTokenInfo(const std::string& deviceID std::vector& nativeTokenInfoList) { if (!DataValidator::IsDeviceIdValid(deviceID)) { - ACCESSTOKEN_LOG_ERROR(LABEL, "device %{private}s parms invalid", deviceID.c_str()); + ACCESSTOKEN_LOG_ERROR(LABEL, "device %{public}s parms invalid", ConstantCommon::EncryptDevId(deviceID).c_str()); return RET_FAILED; } @@ -699,8 +700,8 @@ int AccessTokenInfoManager::SetRemoteNativeTokenInfo(const std::string& deviceID nativeToken.baseInfo.dcap.size() <= 0 || AccessTokenIDManager::GetInstance().GetTokenIdTypeEnum(nativeToken.baseInfo.tokenID) != TOKEN_NATIVE) { ACCESSTOKEN_LOG_ERROR( - LABEL, "device %{private}s token %{public}u is invalid.", - deviceID.c_str(), nativeToken.baseInfo.tokenID); + LABEL, "device %{public}s token %{public}u is invalid.", + ConstantCommon::EncryptDevId(deviceID).c_str(), nativeToken.baseInfo.tokenID); continue; } @@ -708,8 +709,8 @@ int AccessTokenInfoManager::SetRemoteNativeTokenInfo(const std::string& deviceID AccessTokenID mapID = AccessTokenRemoteTokenManager::GetInstance().GetDeviceMappingTokenID(deviceID, remoteID); if (mapID != 0) { ACCESSTOKEN_LOG_ERROR( - LABEL, "device %{private}s token %{public}u has maped, no need update it.", - deviceID.c_str(), nativeToken.baseInfo.tokenID); + LABEL, "device %{public}s token %{public}u has maped, no need update it.", + ConstantCommon::EncryptDevId(deviceID).c_str(), nativeToken.baseInfo.tokenID); continue; } @@ -717,32 +718,32 @@ int AccessTokenInfoManager::SetRemoteNativeTokenInfo(const std::string& deviceID if (mapID == 0) { AccessTokenRemoteTokenManager::GetInstance().RemoveDeviceMappingTokenID(deviceID, mapID); ACCESSTOKEN_LOG_ERROR( - LABEL, "device %{private}s token %{public}u map failed.", - deviceID.c_str(), remoteID); + LABEL, "device %{public}s token %{public}u map failed.", + ConstantCommon::EncryptDevId(deviceID).c_str(), remoteID); continue; } nativeToken.baseInfo.tokenID = mapID; - ACCESSTOKEN_LOG_INFO(LABEL, "device %{private}s token %{public}u map to local token %{public}u.", - deviceID.c_str(), remoteID, mapID); + ACCESSTOKEN_LOG_INFO(LABEL, "device %{public}s token %{public}u map to local token %{public}u.", + ConstantCommon::EncryptDevId(deviceID).c_str(), remoteID, mapID); std::shared_ptr nativePtr = std::make_shared(nativeToken.baseInfo, nativeToken.permStateList); if (nativePtr == nullptr) { AccessTokenRemoteTokenManager::GetInstance().RemoveDeviceMappingTokenID(deviceID, mapID); - ACCESSTOKEN_LOG_ERROR(LABEL, "device %{private}s tokenId %{public}u alloc local token failed.", - deviceID.c_str(), remoteID); + ACCESSTOKEN_LOG_ERROR(LABEL, "device %{public}s tokenId %{public}u alloc local token failed.", + ConstantCommon::EncryptDevId(deviceID).c_str(), remoteID); continue; } nativePtr->SetRemote(true); int ret = AddNativeTokenInfo(nativePtr); if (ret != RET_SUCCESS) { AccessTokenRemoteTokenManager::GetInstance().RemoveDeviceMappingTokenID(deviceID, mapID); - ACCESSTOKEN_LOG_ERROR(LABEL, "device %{private}s tokenId %{public}u add local token failed.", - deviceID.c_str(), remoteID); + ACCESSTOKEN_LOG_ERROR(LABEL, "device %{public}s tokenId %{public}u add local token failed.", + ConstantCommon::EncryptDevId(deviceID).c_str(), remoteID); continue; } - ACCESSTOKEN_LOG_INFO(LABEL, "device %{private}s token %{public}u map token %{public}u add success.", - deviceID.c_str(), remoteID, mapID); + ACCESSTOKEN_LOG_INFO(LABEL, "device %{public}s token %{public}u map token %{public}u add success.", + ConstantCommon::EncryptDevId(deviceID).c_str(), remoteID, mapID); } return RET_SUCCESS; @@ -751,13 +752,13 @@ int AccessTokenInfoManager::SetRemoteNativeTokenInfo(const std::string& deviceID int AccessTokenInfoManager::DeleteRemoteToken(const std::string& deviceID, AccessTokenID tokenID) { if (!DataValidator::IsDeviceIdValid(deviceID)) { - ACCESSTOKEN_LOG_ERROR(LABEL, "device %{private}s parms invalid", deviceID.c_str()); + ACCESSTOKEN_LOG_ERROR(LABEL, "device %{public}s parms invalid", ConstantCommon::EncryptDevId(deviceID).c_str()); return RET_FAILED; } AccessTokenID mapID = AccessTokenRemoteTokenManager::GetInstance().GetDeviceMappingTokenID(deviceID, tokenID); if (mapID == 0) { - ACCESSTOKEN_LOG_ERROR(LABEL, "device %{private}s tokenId %{public}u is not mapped", - deviceID.c_str(), tokenID); + ACCESSTOKEN_LOG_ERROR(LABEL, "device %{public}s tokenId %{public}u is not mapped", + ConstantCommon::EncryptDevId(deviceID).c_str(), tokenID); return RET_FAILED; } @@ -788,7 +789,7 @@ AccessTokenID AccessTokenInfoManager::GetRemoteNativeTokenID(const std::string& { if (!DataValidator::IsDeviceIdValid(deviceID) || AccessTokenIDManager::GetInstance().GetTokenIdTypeEnum(tokenID) != TOKEN_NATIVE) { - ACCESSTOKEN_LOG_ERROR(LABEL, "device %{private}s parms invalid", deviceID.c_str()); + ACCESSTOKEN_LOG_ERROR(LABEL, "device %{public}s parms invalid", ConstantCommon::EncryptDevId(deviceID).c_str()); return 0; } return AccessTokenRemoteTokenManager::GetInstance().GetDeviceMappingTokenID(deviceID, tokenID); @@ -797,13 +798,13 @@ AccessTokenID AccessTokenInfoManager::GetRemoteNativeTokenID(const std::string& int AccessTokenInfoManager::DeleteRemoteDeviceTokens(const std::string& deviceID) { if (!DataValidator::IsDeviceIdValid(deviceID)) { - ACCESSTOKEN_LOG_ERROR(LABEL, "device %{private}s parms invalid", deviceID.c_str()); + ACCESSTOKEN_LOG_ERROR(LABEL, "device %{public}s parms invalid", ConstantCommon::EncryptDevId(deviceID).c_str()); return RET_FAILED; } std::vector remoteTokens; int ret = AccessTokenRemoteTokenManager::GetInstance().GetDeviceAllRemoteTokenID(deviceID, remoteTokens); if (ret == RET_FAILED) { - ACCESSTOKEN_LOG_ERROR(LABEL, "device %{private}s have no remote token", deviceID.c_str()); + ACCESSTOKEN_LOG_ERROR(LABEL, "device %{public}s have no remote token", ConstantCommon::EncryptDevId(deviceID).c_str()); return RET_FAILED; } for (AccessTokenID remoteID : remoteTokens) { @@ -830,11 +831,11 @@ AccessTokenID AccessTokenInfoManager::AllocLocalTokenID(const std::string& remot AccessTokenID remoteTokenID) { if (!DataValidator::IsDeviceIdValid(remoteDeviceID)) { - ACCESSTOKEN_LOG_ERROR(LABEL, "device %{private}s parms invalid", remoteDeviceID.c_str()); + ACCESSTOKEN_LOG_ERROR(LABEL, "device %{public}s parms invalid", ConstantCommon::EncryptDevId(remoteDeviceID).c_str()); return 0; } std::string remoteUdid = GetUdidByNodeId(remoteDeviceID); - ACCESSTOKEN_LOG_ERROR(LABEL, "device %{private}s remoteUdid", remoteUdid.c_str()); + ACCESSTOKEN_LOG_ERROR(LABEL, "device %{public}s remoteUdid", ConstantCommon::EncryptDevId(remoteUdid).c_str()); AccessTokenID mapID = AccessTokenRemoteTokenManager::GetInstance().GetDeviceMappingTokenID(remoteUdid, remoteTokenID); if (mapID != 0) { @@ -842,8 +843,8 @@ AccessTokenID AccessTokenInfoManager::AllocLocalTokenID(const std::string& remot } int ret = TokenSyncKit::GetRemoteHapTokenInfo(remoteUdid, remoteTokenID); if (ret != RET_SUCCESS) { - ACCESSTOKEN_LOG_ERROR(LABEL, "device %{private}s token %{public}u sync failed", - remoteUdid.c_str(), remoteTokenID); + ACCESSTOKEN_LOG_ERROR(LABEL, "device %{public}s token %{public}u sync failed", + ConstantCommon::EncryptDevId(remoteUdid).c_str(), remoteTokenID); return 0; } diff --git a/services/accesstokenmanager/main/cpp/src/token/accesstoken_remote_token_manager.cpp b/services/accesstokenmanager/main/cpp/src/token/accesstoken_remote_token_manager.cpp index 6f5b1e567..e2b6a3641 100644 --- a/services/accesstokenmanager/main/cpp/src/token/accesstoken_remote_token_manager.cpp +++ b/services/accesstokenmanager/main/cpp/src/token/accesstoken_remote_token_manager.cpp @@ -18,7 +18,7 @@ #include "accesstoken_id_manager.h" #include "accesstoken_log.h" #include "data_validator.h" - +#include "constant_common.h" namespace OHOS { namespace Security { namespace AccessToken { @@ -44,7 +44,7 @@ AccessTokenID AccessTokenRemoteTokenManager::MapRemoteDeviceTokenToLocal(const s { if (!DataValidator::IsDeviceIdValid(deviceID) || !DataValidator::IsTokenIDValid(remoteID)) { ACCESSTOKEN_LOG_ERROR( - LABEL, "device %{private}s or token %{public}x is invalid.", deviceID.c_str(), remoteID); + LABEL, "device %{public}s or token %{public}x is invalid.", ConstantCommon::EncryptDevId(deviceID).c_str(), remoteID); return 0; } ATokenTypeEnum tokeType = AccessTokenIDManager::GetInstance().GetTokenIdTypeEnum(remoteID); @@ -63,8 +63,8 @@ AccessTokenID AccessTokenRemoteTokenManager::MapRemoteDeviceTokenToLocal(const s if (device.MappingTokenIDPairMap_.count(remoteID) > 0) { mapID = device.MappingTokenIDPairMap_[remoteID]; ACCESSTOKEN_LOG_ERROR( - LABEL, "device %{private}s token %{public}x has already mapped, maptokenID is %{public}x.", - deviceID.c_str(), remoteID, mapID); + LABEL, "device %{public}s token %{public}x has already mapped, maptokenID is %{public}x.", + ConstantCommon::EncryptDevId(deviceID).c_str(), remoteID, mapID); return mapID; } mapPtr = &device.MappingTokenIDPairMap_; @@ -78,8 +78,8 @@ AccessTokenID AccessTokenRemoteTokenManager::MapRemoteDeviceTokenToLocal(const s mapID = AccessTokenIDManager::GetInstance().CreateAndRegisterTokenId(tokeType, dlpType); if (mapID == 0) { ACCESSTOKEN_LOG_ERROR( - LABEL, "device %{private}s token %{public}x map local Token failed.", - deviceID.c_str(), remoteID); + LABEL, "device %{public}s token %{public}x map local Token failed.", + ConstantCommon::EncryptDevId(deviceID).c_str(), remoteID); return 0; } mapPtr->insert(std::pair(remoteID, mapID)); @@ -90,12 +90,12 @@ int AccessTokenRemoteTokenManager::GetDeviceAllRemoteTokenID(const std::string& std::vector& remoteIDs) { if (!DataValidator::IsDeviceIdValid(deviceID)) { - ACCESSTOKEN_LOG_ERROR(LABEL, "device %{private}s is valid.", deviceID.c_str()); + ACCESSTOKEN_LOG_ERROR(LABEL, "device %{public}s is valid.", ConstantCommon::EncryptDevId(deviceID).c_str()); return RET_FAILED; } Utils::UniqueReadGuard infoGuard(this->remoteDeviceLock_); if (remoteDeviceMap_.count(deviceID) < 1) { - ACCESSTOKEN_LOG_ERROR(LABEL, "device %{private}s has not mapping.", deviceID.c_str()); + ACCESSTOKEN_LOG_ERROR(LABEL, "device %{public}s has not mapping.", ConstantCommon::EncryptDevId(deviceID).c_str()); return RET_FAILED; } @@ -110,14 +110,14 @@ AccessTokenID AccessTokenRemoteTokenManager::GetDeviceMappingTokenID(const std:: { if (!DataValidator::IsDeviceIdValid(deviceID) || !DataValidator::IsTokenIDValid(remoteID)) { ACCESSTOKEN_LOG_ERROR( - LABEL, "device %{private}s or token %{public}x is invalid.", deviceID.c_str(), remoteID); + LABEL, "device %{public}s or token %{public}x is invalid.", ConstantCommon::EncryptDevId(deviceID).c_str(), remoteID); return 0; } Utils::UniqueReadGuard infoGuard(this->remoteDeviceLock_); if (remoteDeviceMap_.count(deviceID) < 1 || remoteDeviceMap_[deviceID].MappingTokenIDPairMap_.count(remoteID) < 1) { - ACCESSTOKEN_LOG_ERROR(LABEL, "device %{private}s has not mapping.", deviceID.c_str()); + ACCESSTOKEN_LOG_ERROR(LABEL, "device %{public}s has not mapping.", ConstantCommon::EncryptDevId(deviceID).c_str()); return 0; } @@ -129,14 +129,14 @@ int AccessTokenRemoteTokenManager::RemoveDeviceMappingTokenID(const std::string& { if (!DataValidator::IsDeviceIdValid(deviceID) || !DataValidator::IsTokenIDValid(remoteID)) { ACCESSTOKEN_LOG_ERROR( - LABEL, "device %{private}s or token %{public}x is invalid.", deviceID.c_str(), remoteID); + LABEL, "device %{public}s or token %{public}x is invalid.", ConstantCommon::EncryptDevId(deviceID).c_str(), remoteID); return RET_FAILED; } Utils::UniqueWriteGuard infoGuard(this->remoteDeviceLock_); if (remoteDeviceMap_.count(deviceID) < 1 || remoteDeviceMap_[deviceID].MappingTokenIDPairMap_.count(remoteID) < 1) { - ACCESSTOKEN_LOG_ERROR(LABEL, "device %{private}s has not mapping.", deviceID.c_str()); + ACCESSTOKEN_LOG_ERROR(LABEL, "device %{public}s has not mapping.", ConstantCommon::EncryptDevId(deviceID).c_str()); return RET_FAILED; } diff --git a/services/privacymanager/include/common/constant.h b/services/privacymanager/include/common/constant.h index 257286a18..ca7f375f0 100644 --- a/services/privacymanager/include/common/constant.h +++ b/services/privacymanager/include/common/constant.h @@ -67,8 +67,6 @@ public: public: static bool TransferPermissionToOpcode(const std::string& permissionName, int32_t& opCode); static bool TransferOpcodeToPermission(int32_t opCode, std::string& permissionName); - - static std::string GetLocalDeviceUdid(); }; } // namespace AccessToken } // namespace Security diff --git a/services/privacymanager/src/common/constant.cpp b/services/privacymanager/src/common/constant.cpp index f0aab8648..a9628adcd 100644 --- a/services/privacymanager/src/common/constant.cpp +++ b/services/privacymanager/src/common/constant.cpp @@ -14,7 +14,6 @@ */ #include "constant.h" -#include "parameter.h" namespace OHOS { namespace Security { @@ -70,19 +69,6 @@ bool Constant::TransferOpcodeToPermission(int32_t opCode, std::string& permissio permissionName = iter->first; return true; } - -std::string Constant::GetLocalDeviceUdid() -{ - static std::string localDeviceId; - if (!localDeviceId.empty()) { - return localDeviceId; - } - const int32_t DEVICE_UUID_LENGTH = 65; - char udid[DEVICE_UUID_LENGTH] = {0}; - GetDevUdid(udid, DEVICE_UUID_LENGTH); - localDeviceId = udid; - return localDeviceId; -} } // namespace AccessToken } // namespace Security } // namespace OHOS \ No newline at end of file diff --git a/services/privacymanager/src/record/permission_record_manager.cpp b/services/privacymanager/src/record/permission_record_manager.cpp index b2f415487..237f68110 100644 --- a/services/privacymanager/src/record/permission_record_manager.cpp +++ b/services/privacymanager/src/record/permission_record_manager.cpp @@ -18,6 +18,7 @@ #include "accesstoken_kit.h" #include "accesstoken_log.h" #include "constant.h" +#include "constant_common.h" #include "data_translator.h" #include "field_const.h" #include "permission_record_repository.h" @@ -89,7 +90,7 @@ bool PermissionRecordManager::GetPermissionVisitor(AccessTokenID tokenID, Permis visitor.userId = tokenInfo.userID; visitor.bundleName = tokenInfo.bundleName; if (IsLocalDevice(tokenInfo.deviceID)) { - visitor.deviceId = Constant::GetLocalDeviceUdid(); + visitor.deviceId = ConstantCommon::GetLocalDeviceId(); visitor.isRemoteDevice = false; visitor.tokenId = tokenID; } diff --git a/services/privacymanager/src/service/privacy_manager_service.cpp b/services/privacymanager/src/service/privacy_manager_service.cpp index f9708f5a6..bce86f0d2 100644 --- a/services/privacymanager/src/service/privacy_manager_service.cpp +++ b/services/privacymanager/src/service/privacy_manager_service.cpp @@ -16,6 +16,7 @@ #include "privacy_manager_service.h" #include "accesstoken_log.h" +#include "constant_common.h" #include "constant.h" #include "ipc_skeleton.h" #include "permission_record_manager.h" @@ -94,7 +95,7 @@ int32_t PrivacyManagerService::StopUsingPermission(AccessTokenID tokenID, const int32_t PrivacyManagerService::RemovePermissionUsedRecords(AccessTokenID tokenID, const std::string& deviceID) { - ACCESSTOKEN_LOG_DEBUG(LABEL, "Entry, tokenID: 0x%{public}x, deviceID: %{private}s", tokenID, deviceID.c_str()); + ACCESSTOKEN_LOG_DEBUG(LABEL, "Entry, tokenID: 0x%{public}x, deviceID: %{public}s", tokenID, ConstantCommon::EncryptDevId(deviceID).c_str()); PermissionRecordManager::GetInstance().RemovePermissionUsedRecords(tokenID, deviceID); return Constant::SUCCESS; } diff --git a/services/tokensyncmanager/include/common/constant.h b/services/tokensyncmanager/include/common/constant.h index 3a7d0d5e6..69d9353fe 100644 --- a/services/tokensyncmanager/include/common/constant.h +++ b/services/tokensyncmanager/include/common/constant.h @@ -68,26 +68,6 @@ public: * Command result string, indicates failed. */ static const std::string COMMAND_RESULT_FAILED; - - /** - * Device id length. - */ - const static int32_t DEVICE_UUID_LENGTH = 65; - - /** - * Command status code, indicate a status of command before RPC call. - */ - const static int32_t DELAY_SYNC_TOKEN_MS = 3000; - - static constexpr int32_t ENCRYPTLEN = 4; - static constexpr int32_t ENCRYPTBEGIN = 0; - static constexpr int32_t ENCRYPTEND = 3; - static std::string EncryptDevId(std::string deviceId); - - /** - * GetLocalDeviceId - */ - static std::string GetLocalDeviceId(); }; } // namespace AccessToken } // namespace Security diff --git a/services/tokensyncmanager/src/common/constant.cpp b/services/tokensyncmanager/src/common/constant.cpp index a20a7cc05..7de4ff187 100644 --- a/services/tokensyncmanager/src/common/constant.cpp +++ b/services/tokensyncmanager/src/common/constant.cpp @@ -23,24 +23,6 @@ static const std::string REPLACE_TARGET = "****"; } // namespace const std::string Constant::COMMAND_RESULT_SUCCESS = "success"; const std::string Constant::COMMAND_RESULT_FAILED = "execute command failed"; - -std::string Constant::EncryptDevId(std::string deviceId) -{ - std::string result = deviceId; - if (deviceId.size() >= ENCRYPTLEN) { - result.replace(ENCRYPTBEGIN, ENCRYPTEND, REPLACE_TARGET); - } else { - result.replace(ENCRYPTBEGIN, result.size() - 1, REPLACE_TARGET); - } - return result; -} - -std::string Constant::GetLocalDeviceId() -{ - char deviceIdCharArray[Constant::DEVICE_UUID_LENGTH] = {0}; - GetDevUdid(deviceIdCharArray, Constant::DEVICE_UUID_LENGTH); - return deviceIdCharArray; -} } // namespace AccessToken } // namespace Security } // namespace OHOS diff --git a/services/tokensyncmanager/src/remote/remote_command_manager.cpp b/services/tokensyncmanager/src/remote/remote_command_manager.cpp index d9a604c45..3db3d103e 100644 --- a/services/tokensyncmanager/src/remote/remote_command_manager.cpp +++ b/services/tokensyncmanager/src/remote/remote_command_manager.cpp @@ -20,7 +20,8 @@ #include "token_sync_event_handler.h" #include "token_sync_manager_service.h" #include "accesstoken_kit.h" - +#include "constant_common.h" +#include "constant.h" #include @@ -80,11 +81,11 @@ void RemoteCommandManager::RemoveCommand(const std::string &udid) int RemoteCommandManager::ExecuteCommand(const std::string &udid, const std::shared_ptr &command) { if (udid.empty() || command == nullptr) { - ACCESSTOKEN_LOG_WARN(LABEL, "invalid udid: %{public}s, or null command", udid.c_str()); + ACCESSTOKEN_LOG_WARN(LABEL, "invalid udid: %{public}s, or null command", ConstantCommon::EncryptDevId(udid).c_str()); return Constant::FAILURE; } std::string uniqueId = command->remoteProtocol_.uniqueId; - ACCESSTOKEN_LOG_INFO(LABEL, "start with udid: %{public}s , uniqueId: %{public}s ", udid.c_str(), uniqueId.c_str()); + ACCESSTOKEN_LOG_INFO(LABEL, "start with udid: %{public}s , uniqueId: %{public}s ", ConstantCommon::EncryptDevId(udid).c_str(), uniqueId.c_str()); std::shared_ptr executor = GetOrCreateRemoteCommandExecutor(udid); if (executor == nullptr) { @@ -100,19 +101,19 @@ int RemoteCommandManager::ExecuteCommand(const std::string &udid, const std::sha int RemoteCommandManager::ProcessDeviceCommandImmediately(const std::string &udid) { if (udid.empty()) { - ACCESSTOKEN_LOG_WARN(LABEL, "invalid udid: %{public}s", udid.c_str()); + ACCESSTOKEN_LOG_WARN(LABEL, "invalid udid: %{public}s", ConstantCommon::EncryptDevId(udid).c_str()); return Constant::FAILURE; } - ACCESSTOKEN_LOG_INFO(LABEL, "start with udid:%{public}s ", udid.c_str()); + ACCESSTOKEN_LOG_INFO(LABEL, "start with udid:%{public}s ", ConstantCommon::EncryptDevId(udid).c_str()); auto executorIt = executors_.find(udid); if (executorIt == executors_.end()) { - ACCESSTOKEN_LOG_ERROR(LABEL, "no executor found, udid:%{public}s", udid.c_str()); + ACCESSTOKEN_LOG_ERROR(LABEL, "no executor found, udid:%{public}s", ConstantCommon::EncryptDevId(udid).c_str()); return Constant::FAILURE; } auto executor = executorIt->second; if (executor == nullptr) { - ACCESSTOKEN_LOG_INFO(LABEL, "RemoteCommandExecutor is null for udid %{public}s ", udid.c_str()); + ACCESSTOKEN_LOG_INFO(LABEL, "RemoteCommandExecutor is null for udid %{public}s ", ConstantCommon::EncryptDevId(udid).c_str()); return Constant::FAILURE; } @@ -174,7 +175,7 @@ int RemoteCommandManager::NotifyDeviceOnline(const std::string &nodeId) std::function delayed = ([=]() { const std::shared_ptr syncRemoteNativeTokenCommand = - RemoteCommandFactory::GetInstance().NewSyncRemoteNativeTokenCommand(Constant::GetLocalDeviceId(), + RemoteCommandFactory::GetInstance().NewSyncRemoteNativeTokenCommand(ConstantCommon::GetLocalDeviceId(), nodeId); const int32_t resultCode = RemoteCommandManager::GetInstance().ExecuteCommand( @@ -193,7 +194,7 @@ int RemoteCommandManager::NotifyDeviceOnline(const std::string &nodeId) ACCESSTOKEN_LOG_ERROR(LABEL, "fail to get EventHandler"); return Constant::FAILURE; } - handler->ProxyPostTask(delayed, "HandleDeviceOnline", Constant::DELAY_SYNC_TOKEN_MS); + handler->ProxyPostTask(delayed, "HandleDeviceOnline", ConstantCommon::DELAY_SYNC_TOKEN_MS); return Constant::SUCCESS; } diff --git a/services/tokensyncmanager/src/remote/soft_bus_device_connection_listener.cpp b/services/tokensyncmanager/src/remote/soft_bus_device_connection_listener.cpp index 62982e364..929740b06 100644 --- a/services/tokensyncmanager/src/remote/soft_bus_device_connection_listener.cpp +++ b/services/tokensyncmanager/src/remote/soft_bus_device_connection_listener.cpp @@ -1,120 +1,120 @@ -/* - * Copyright (c) 2022 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 "soft_bus_device_connection_listener.h" -#include "remote_command_manager.h" -#include "soft_bus_manager.h" -#include "device_info_manager.h" -#include "softbus_bus_center.h" -#include "device_manager.h" -#include "dm_device_info.h" - -namespace OHOS { -namespace Security { -namespace AccessToken { -using OHOS::DistributedHardware::DeviceStateCallback; -using OHOS::DistributedHardware::DmDeviceInfo; -using OHOS::DistributedHardware::DmInitCallback; - -namespace { -static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { - LOG_CORE, SECURITY_DOMAIN_ACCESSTOKEN, "SoftBusDeviceConnectionListener"}; -} - -const std::string ACCESSTOKEN_PACKAGE_NAME = "ohos.security.distributed_access_token"; - -SoftBusDeviceConnectionListener::SoftBusDeviceConnectionListener() -{ - ACCESSTOKEN_LOG_DEBUG(LABEL, "SoftBusDeviceConnectionListener()"); -} -SoftBusDeviceConnectionListener::~SoftBusDeviceConnectionListener() -{ - ACCESSTOKEN_LOG_DEBUG(LABEL, "~SoftBusDeviceConnectionListener()"); -} - -void SoftBusDeviceConnectionListener::OnDeviceOnline(const DmDeviceInfo &info) -{ - std::string networkId = info.deviceId; - std::string uuid = SoftBusManager::GetInstance().GetUniversallyUniqueIdByNodeId(networkId); - std::string udid = SoftBusManager::GetInstance().GetUniqueDeviceIdByNodeId(networkId); - - ACCESSTOKEN_LOG_INFO(LABEL, - "networkId: %{public}s, uuid: %{public}s, udid: %{public}s", - networkId.c_str(), - uuid.c_str(), - udid.c_str()); - - if (uuid != "" && udid != "") { - DeviceInfoManager::GetInstance().AddDeviceInfo( - networkId, uuid, udid, info.deviceName, std::to_string(info.deviceTypeId)); - RemoteCommandManager::GetInstance().NotifyDeviceOnline(udid); - } else { - ACCESSTOKEN_LOG_ERROR(LABEL, "uuid or udid is empty, online failed."); - } - // no need to load local permissions by now. -} - -void SoftBusDeviceConnectionListener::OnDeviceOffline(const DmDeviceInfo &info) -{ - std::string networkId = info.deviceId; - std::string uuid = DeviceInfoManager::GetInstance().ConvertToUniversallyUniqueIdOrFetch(networkId); - std::string udid = DeviceInfoManager::GetInstance().ConvertToUniqueDeviceIdOrFetch(networkId); - - ACCESSTOKEN_LOG_INFO(LABEL, - "networkId: %{public}s, uuid: %{public}s, udid: %{public}s", - networkId.c_str(), - uuid.c_str(), - udid.c_str()); - - if (uuid != "" && udid != "") { - RemoteCommandManager::GetInstance().NotifyDeviceOffline(uuid); - RemoteCommandManager::GetInstance().NotifyDeviceOffline(udid); - DeviceInfoManager::GetInstance().RemoveRemoteDeviceInfo(networkId, DeviceIdType::NETWORK_ID); - - std::string packageName = ACCESSTOKEN_PACKAGE_NAME; - std::string extra = ""; - std::vector deviceList; - - int32_t ret = DistributedHardware::DeviceManager::GetInstance().GetTrustedDeviceList(packageName, - extra, deviceList); - if (ret != Constant::SUCCESS) { - ACCESSTOKEN_LOG_ERROR(LABEL, "GetTrustedDeviceList error, result: %{public}d", ret); - return; - } - - if (deviceList.size() == 0) { - ACCESSTOKEN_LOG_INFO(LABEL, "there is no remote decice online, exit tokensync process"); - - exit(0); - } - } else { - ACCESSTOKEN_LOG_ERROR(LABEL, "uuid or udid is empty, offline failed."); - } -} - -void SoftBusDeviceConnectionListener::OnDeviceReady(const DmDeviceInfo &info) -{ - std::string networkId = info.deviceId; - ACCESSTOKEN_LOG_INFO(LABEL, "networkId: %{public}s", networkId.c_str()); -} - -void SoftBusDeviceConnectionListener::OnDeviceChanged(const DmDeviceInfo &info) -{ - std::string networkId = info.deviceId; - ACCESSTOKEN_LOG_INFO(LABEL, "networkId: %{public}s", networkId.c_str()); -} -} // namespace AccessToken -} // namespace Security -} // namespace OHOS +/* + * Copyright (c) 2022 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 "soft_bus_device_connection_listener.h" +#include "remote_command_manager.h" +#include "soft_bus_manager.h" +#include "device_info_manager.h" +#include "softbus_bus_center.h" +#include "device_manager.h" +#include "dm_device_info.h" +#include "constant_common.h" +namespace OHOS { +namespace Security { +namespace AccessToken { +using OHOS::DistributedHardware::DeviceStateCallback; +using OHOS::DistributedHardware::DmDeviceInfo; +using OHOS::DistributedHardware::DmInitCallback; + +namespace { +static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { + LOG_CORE, SECURITY_DOMAIN_ACCESSTOKEN, "SoftBusDeviceConnectionListener"}; +} + +const std::string ACCESSTOKEN_PACKAGE_NAME = "ohos.security.distributed_access_token"; + +SoftBusDeviceConnectionListener::SoftBusDeviceConnectionListener() +{ + ACCESSTOKEN_LOG_DEBUG(LABEL, "SoftBusDeviceConnectionListener()"); +} +SoftBusDeviceConnectionListener::~SoftBusDeviceConnectionListener() +{ + ACCESSTOKEN_LOG_DEBUG(LABEL, "~SoftBusDeviceConnectionListener()"); +} + +void SoftBusDeviceConnectionListener::OnDeviceOnline(const DmDeviceInfo &info) +{ + std::string networkId = info.deviceId; + std::string uuid = SoftBusManager::GetInstance().GetUniversallyUniqueIdByNodeId(networkId); + std::string udid = SoftBusManager::GetInstance().GetUniqueDeviceIdByNodeId(networkId); + + ACCESSTOKEN_LOG_INFO(LABEL, + "networkId: %{public}s, uuid: %{public}s, udid: %{public}s", + networkId.c_str(), + uuid.c_str(), + ConstantCommon::EncryptDevId(udid).c_str()); + + if (uuid != "" && udid != "") { + DeviceInfoManager::GetInstance().AddDeviceInfo( + networkId, uuid, udid, info.deviceName, std::to_string(info.deviceTypeId)); + RemoteCommandManager::GetInstance().NotifyDeviceOnline(udid); + } else { + ACCESSTOKEN_LOG_ERROR(LABEL, "uuid or udid is empty, online failed."); + } + // no need to load local permissions by now. +} + +void SoftBusDeviceConnectionListener::OnDeviceOffline(const DmDeviceInfo &info) +{ + std::string networkId = info.deviceId; + std::string uuid = DeviceInfoManager::GetInstance().ConvertToUniversallyUniqueIdOrFetch(networkId); + std::string udid = DeviceInfoManager::GetInstance().ConvertToUniqueDeviceIdOrFetch(networkId); + + ACCESSTOKEN_LOG_INFO(LABEL, + "networkId: %{public}s, uuid: %{public}s, udid: %{public}s", + networkId.c_str(), + uuid.c_str(), + ConstantCommon::EncryptDevId(udid).c_str()); + + if (uuid != "" && udid != "") { + RemoteCommandManager::GetInstance().NotifyDeviceOffline(uuid); + RemoteCommandManager::GetInstance().NotifyDeviceOffline(udid); + DeviceInfoManager::GetInstance().RemoveRemoteDeviceInfo(networkId, DeviceIdType::NETWORK_ID); + + std::string packageName = ACCESSTOKEN_PACKAGE_NAME; + std::string extra = ""; + std::vector deviceList; + + int32_t ret = DistributedHardware::DeviceManager::GetInstance().GetTrustedDeviceList(packageName, + extra, deviceList); + if (ret != Constant::SUCCESS) { + ACCESSTOKEN_LOG_ERROR(LABEL, "GetTrustedDeviceList error, result: %{public}d", ret); + return; + } + + if (deviceList.size() == 0) { + ACCESSTOKEN_LOG_INFO(LABEL, "there is no remote decice online, exit tokensync process"); + + exit(0); + } + } else { + ACCESSTOKEN_LOG_ERROR(LABEL, "uuid or udid is empty, offline failed."); + } +} + +void SoftBusDeviceConnectionListener::OnDeviceReady(const DmDeviceInfo &info) +{ + std::string networkId = info.deviceId; + ACCESSTOKEN_LOG_INFO(LABEL, "networkId: %{public}s", networkId.c_str()); +} + +void SoftBusDeviceConnectionListener::OnDeviceChanged(const DmDeviceInfo &info) +{ + std::string networkId = info.deviceId; + ACCESSTOKEN_LOG_INFO(LABEL, "networkId: %{public}s", networkId.c_str()); +} +} // namespace AccessToken +} // namespace Security +} // namespace OHOS diff --git a/services/tokensyncmanager/src/remote/soft_bus_manager.cpp b/services/tokensyncmanager/src/remote/soft_bus_manager.cpp index 8fe5de825..739d12bc7 100644 --- a/services/tokensyncmanager/src/remote/soft_bus_manager.cpp +++ b/services/tokensyncmanager/src/remote/soft_bus_manager.cpp @@ -1,409 +1,409 @@ -/* - * Copyright (c) 2022 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 "soft_bus_manager.h" - -#include - -#include "device_info_manager.h" -#include "parameter.h" -#include "softbus_bus_center.h" -#include "dm_device_info.h" -#include "remote_command_manager.h" - -namespace OHOS { -namespace Security { -namespace AccessToken { -namespace { -static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_ACCESSTOKEN, "SoftBusManager"}; -} -namespace { -static const std::string SESSION_GROUP_ID = "atm_dsoftbus_session_group_id"; -static const SessionAttribute SESSION_ATTR = {.dataType = TYPE_BYTES}; - -static const int REASON_EXIST = -3; -static const int OPENSESSION_RETRY_TIMES = 10 * 3; -static const int OPENSESSION_RETRY_INTERVAL_MS = 100; -static const int UDID_MAX_LENGTH = 128; // udid/uuid max length -} // namespace - -const std::string SoftBusManager::TOKEN_SYNC_PACKAGE_NAME = "ohos.security.distributed_access_token"; -const std::string SoftBusManager::SESSION_NAME = "ohos.security.atm_channel"; - -SoftBusManager::SoftBusManager() : isSoftBusServiceBindSuccess_(false), inited_(false), mutex_(), fulfillMutex_() -{ - ACCESSTOKEN_LOG_DEBUG(LABEL, "SoftBusManager()"); -} - -SoftBusManager::~SoftBusManager() -{ - ACCESSTOKEN_LOG_DEBUG(LABEL, "~SoftBusManager()"); -} - -SoftBusManager &SoftBusManager::GetInstance() -{ - static SoftBusManager instance; - return instance; -} - -int SoftBusManager::AddTrustedDeviceInfo() -{ - std::string packageName = TOKEN_SYNC_PACKAGE_NAME; - std::string extra = ""; - std::vector deviceList; - - int32_t ret = DistributedHardware::DeviceManager::GetInstance().GetTrustedDeviceList(packageName, - extra, deviceList); - if (ret != Constant::SUCCESS) { - ACCESSTOKEN_LOG_ERROR(LABEL, "AddTrustedDeviceInfo: GetTrustedDeviceList error, result: %{public}d", ret); - return Constant::FAILURE; - } - - for (DistributedHardware::DmDeviceInfo device : deviceList) { - std::string uuid = GetUuidByNodeId(device.networkId); - std::string udid = GetUdidByNodeId(device.networkId); - if (uuid.empty() || udid.empty()) { - ACCESSTOKEN_LOG_ERROR(LABEL, "uuid = %{public}s, udid = %{public}s, uuid or udid is empty, abort.", - uuid.c_str(), udid.c_str()); - continue; - } - - DeviceInfoManager::GetInstance().AddDeviceInfo(device.networkId, uuid, udid, device.deviceName, - std::to_string(device.deviceTypeId)); - RemoteCommandManager::GetInstance().NotifyDeviceOnline(udid); - } - - return Constant::SUCCESS; -} - -int SoftBusManager::DeviceInit() -{ - std::string packageName = TOKEN_SYNC_PACKAGE_NAME; - std::shared_ptr ptrDmInitCallback = std::make_shared(); - - int ret = DistributedHardware::DeviceManager::GetInstance().InitDeviceManager(packageName, ptrDmInitCallback); - if (ret != ERR_OK) { - ACCESSTOKEN_LOG_ERROR(LABEL, "Initialize: InitDeviceManager error, result: %{public}d", ret); - return ret; - } - - ret = AddTrustedDeviceInfo(); - if (ret != ERR_OK) { - ACCESSTOKEN_LOG_ERROR(LABEL, "Initialize: AddTrustedDeviceInfo error, result: %{public}d", ret); - return ret; - } - - std::string extra = ""; - std::shared_ptr ptrDeviceStateCallback = - std::make_shared(); - ret = DistributedHardware::DeviceManager::GetInstance().RegisterDevStateCallback(packageName, extra, - ptrDeviceStateCallback); - if (ret != ERR_OK) { - ACCESSTOKEN_LOG_ERROR(LABEL, "Initialize: RegisterDevStateCallback error, result: %{public}d", ret); - return ret; - } - - return ERR_OK; -} - -int SoftBusManager::SessionInit() -{ - // register session listener - ISessionListener sessionListener; - sessionListener.OnSessionOpened = SoftBusSessionListener::OnSessionOpened; - sessionListener.OnSessionClosed = SoftBusSessionListener::OnSessionClosed; - sessionListener.OnBytesReceived = SoftBusSessionListener::OnBytesReceived; - sessionListener.OnMessageReceived = SoftBusSessionListener::OnMessageReceived; - - int ret = ::CreateSessionServer(TOKEN_SYNC_PACKAGE_NAME.c_str(), SESSION_NAME.c_str(), &sessionListener); - ACCESSTOKEN_LOG_INFO(LABEL, "Initialize: createSessionServer, result: %{public}d", ret); - // REASON_EXIST - if ((ret != Constant::SUCCESS) && (ret != REASON_EXIST)) { - ACCESSTOKEN_LOG_ERROR(LABEL, "Initialize: CreateSessionServer error, result: %{public}d", ret); - return ret; - } - - return ERR_OK; -} - -void SoftBusManager::Initialize() -{ - bool inited = false; - // cas failed means already inited. - if (!inited_.compare_exchange_strong(inited, true)) { - ACCESSTOKEN_LOG_DEBUG(LABEL, "already initialized, skip"); - return; - } - - std::function runner = [&]() { - auto sleepTime = std::chrono::milliseconds(1000); - while (1) { - std::unique_lock lock(mutex_); - - int ret = DeviceInit(); - if (ret != ERR_OK) { - std::this_thread::sleep_for(sleepTime); - continue; - } - - ret = SessionInit(); - if (ret != ERR_OK) { - std::this_thread::sleep_for(sleepTime); - continue; - } - - isSoftBusServiceBindSuccess_ = true; - this->FulfillLocalDeviceInfo(); - return; - } - }; - - std::thread initThread(runner); - initThread.detach(); - ACCESSTOKEN_LOG_DEBUG(LABEL, "Initialize thread started"); -} - -void SoftBusManager::Destroy() -{ - ACCESSTOKEN_LOG_DEBUG(LABEL, "destroy, init: %{public}d, isSoftBusServiceBindSuccess: %{public}d", inited_.load(), - isSoftBusServiceBindSuccess_); - - if (!inited_.load()) { - ACCESSTOKEN_LOG_DEBUG(LABEL, "not inited, skip"); - return; - } - - std::unique_lock lock(mutex_); - if (!inited_.load()) { - ACCESSTOKEN_LOG_DEBUG(LABEL, "not inited, skip"); - return; - } - - if (isSoftBusServiceBindSuccess_) { - int32_t ret = ::RemoveSessionServer(TOKEN_SYNC_PACKAGE_NAME.c_str(), SESSION_NAME.c_str()); - ACCESSTOKEN_LOG_DEBUG(LABEL, "destroy, RemoveSessionServer: %{public}d", ret); - isSoftBusServiceBindSuccess_ = false; - } - - std::string packageName = TOKEN_SYNC_PACKAGE_NAME; - int ret = DistributedHardware::DeviceManager::GetInstance().UnRegisterDevStateCallback(packageName); - if (ret != ERR_OK) { - ACCESSTOKEN_LOG_ERROR(LABEL, "UnRegisterDevStateCallback failed, code: %{public}d", ret); - } - ret = DistributedHardware::DeviceManager::GetInstance().UnInitDeviceManager(packageName); - if (ret != ERR_OK) { - ACCESSTOKEN_LOG_ERROR(LABEL, "UnInitDeviceManager failed, code: %{public}d", ret); - } - - inited_.store(false); - - ACCESSTOKEN_LOG_DEBUG(LABEL, "destroy, done"); -} - -int32_t SoftBusManager::OpenSession(const std::string &deviceId) -{ -#ifdef DEBUG_API_PERFORMANCE - ACCESSTOKEN_LOG_INFO(LABEL, "api_performance:start open session"); -#endif - - DeviceInfo info; - bool result = DeviceInfoManager::GetInstance().GetDeviceInfo(deviceId, DeviceIdType::UNKNOWN, info); - if (!result) { - ACCESSTOKEN_LOG_WARN(LABEL, "device info notfound for deviceId %{private}s", deviceId.c_str()); - return Constant::FAILURE; - } - std::string networkId = info.deviceId.networkId; - ACCESSTOKEN_LOG_INFO(LABEL, "openSession, networkId: %{public}s", networkId.c_str()); - - // async open session, should waitting for OnSessionOpened event. - int sessionId = ::OpenSession(SESSION_NAME.c_str(), SESSION_NAME.c_str(), networkId.c_str(), - SESSION_GROUP_ID.c_str(), &SESSION_ATTR); - - ACCESSTOKEN_LOG_DEBUG(LABEL, "async open session"); - - // wait session opening - int retryTimes = 0; - int logSpan = 10; - auto sleepTime = std::chrono::milliseconds(OPENSESSION_RETRY_INTERVAL_MS); - while (retryTimes++ < OPENSESSION_RETRY_TIMES) { - if (SoftBusSessionListener::GetSessionState(sessionId) < 0) { - std::this_thread::sleep_for(sleepTime); - if (retryTimes % logSpan == 0) { - ACCESSTOKEN_LOG_INFO(LABEL, "openSession, waitting for: %{public}d ms", - retryTimes * OPENSESSION_RETRY_INTERVAL_MS); - } - continue; - } - break; - } -#ifdef DEBUG_API_PERFORMANCE - ACCESSTOKEN_LOG_INFO(LABEL, "api_performance:start open session success"); -#endif - int64_t state = SoftBusSessionListener::GetSessionState(sessionId); - if (state < 0) { - ACCESSTOKEN_LOG_ERROR(LABEL, "openSession, timeout, session: %{public}" PRId64, state); - return Constant::FAILURE; - } - - SoftBusSessionListener::DeleteSessionIdFromMap(sessionId); - - ACCESSTOKEN_LOG_DEBUG(LABEL, "openSession, succeed, session: %{public}" PRId64, state); - return sessionId; -} - -int SoftBusManager::CloseSession(int sessionId) -{ - if (sessionId < 0) { - ACCESSTOKEN_LOG_INFO(LABEL, "closeSession: session is invalid"); - return Constant::FAILURE; - } - - ::CloseSession(sessionId); - ACCESSTOKEN_LOG_INFO(LABEL, "closeSession "); - return Constant::SUCCESS; -} - -std::string SoftBusManager::GetUniversallyUniqueIdByNodeId(const std::string &nodeId) -{ - if (!DataValidator::IsDeviceIdValid(nodeId)) { - ACCESSTOKEN_LOG_ERROR(LABEL, "invalid nodeId: %{public}s", nodeId.c_str()); - return ""; - } - - std::string uuid = GetUuidByNodeId(nodeId); - if (uuid.empty()) { - ACCESSTOKEN_LOG_ERROR(LABEL, "softbus return null or empty string [%{public}s]", uuid.c_str()); - return ""; - } - - DeviceInfo info; - bool result = DeviceInfoManager::GetInstance().GetDeviceInfo(uuid, DeviceIdType::UNIVERSALLY_UNIQUE_ID, info); - if (!result) { - ACCESSTOKEN_LOG_DEBUG(LABEL, "local device info not found for uuid %{public}s", uuid.c_str()); - } else { - std::string dimUuid = info.deviceId.universallyUniqueId; - if (uuid == dimUuid) { - // refresh cache - std::function fulfillDeviceInfo = std::bind(&SoftBusManager::FulfillLocalDeviceInfo, this); - std::thread fulfill(fulfillDeviceInfo); - fulfill.detach(); - } - } - - return uuid; -} - -std::string SoftBusManager::GetUniqueDeviceIdByNodeId(const std::string &nodeId) -{ - if (!DataValidator::IsDeviceIdValid(nodeId)) { - ACCESSTOKEN_LOG_ERROR(LABEL, "invalid nodeId: %{public}s", nodeId.c_str()); - return ""; - } - std::string udid = GetUdidByNodeId(nodeId); - if (udid.empty()) { - ACCESSTOKEN_LOG_ERROR(LABEL, "softbus return null or empty string: %{public}s", udid.c_str()); - return ""; - } - char localUdid[Constant::DEVICE_UUID_LENGTH] = {0}; - ::GetDevUdid(localUdid, Constant::DEVICE_UUID_LENGTH); - if (udid == localUdid) { - // refresh cache - std::function fulfillDeviceInfo = std::bind(&SoftBusManager::FulfillLocalDeviceInfo, this); - std::thread fulfill(fulfillDeviceInfo); - fulfill.detach(); - } - return udid; -} - -std::string SoftBusManager::GetUuidByNodeId(const std::string &nodeId) const -{ - uint8_t *info = new uint8_t[UDID_MAX_LENGTH + 1]; - if (info == nullptr) { - ACCESSTOKEN_LOG_ERROR(LABEL, "no enough memory: %{public}d", UDID_MAX_LENGTH); - return ""; - } - (void)memset_s(info, UDID_MAX_LENGTH + 1, 0, UDID_MAX_LENGTH + 1); - int32_t ret = ::GetNodeKeyInfo(TOKEN_SYNC_PACKAGE_NAME.c_str(), nodeId.c_str(), - NodeDeviceInfoKey::NODE_KEY_UUID, info, UDID_MAX_LENGTH); - if (ret != Constant::SUCCESS) { - delete[] info; - ACCESSTOKEN_LOG_WARN(LABEL, "GetNodeKeyInfo error, return code: %{public}d", ret); - return ""; - } - std::string uuid(reinterpret_cast(info)); - delete[] info; - ACCESSTOKEN_LOG_DEBUG(LABEL, "call softbus finished. nodeId(in): %{public}s, uuid: %{public}s", nodeId.c_str(), - uuid.c_str()); - return uuid; -} - -std::string SoftBusManager::GetUdidByNodeId(const std::string &nodeId) const -{ - uint8_t *info = new uint8_t[UDID_MAX_LENGTH + 1]; - if (info == nullptr) { - ACCESSTOKEN_LOG_ERROR(LABEL, "no enough memory: %{public}d", UDID_MAX_LENGTH); - return ""; - } - (void)memset_s(info, UDID_MAX_LENGTH + 1, 0, UDID_MAX_LENGTH + 1); - int32_t ret = ::GetNodeKeyInfo(TOKEN_SYNC_PACKAGE_NAME.c_str(), nodeId.c_str(), - NodeDeviceInfoKey::NODE_KEY_UDID, info, UDID_MAX_LENGTH); - if (ret != Constant::SUCCESS) { - delete[] info; - ACCESSTOKEN_LOG_WARN(LABEL, "GetNodeKeyInfo error, code: %{public}d", ret); - return ""; - } - std::string udid(reinterpret_cast(info)); - delete[] info; - ACCESSTOKEN_LOG_DEBUG(LABEL, "call softbus finished: nodeId(in): %{public}s", nodeId.c_str()); - return udid; -} - -int SoftBusManager::FulfillLocalDeviceInfo() -{ - // repeated task will just skip - if (!fulfillMutex_.try_lock()) { - ACCESSTOKEN_LOG_INFO(LABEL, "FulfillLocalDeviceInfo already running, skip."); - return Constant::SUCCESS; - } - - NodeBasicInfo info; - int32_t ret = ::GetLocalNodeDeviceInfo(TOKEN_SYNC_PACKAGE_NAME.c_str(), &info); - if (ret != Constant::SUCCESS) { - ACCESSTOKEN_LOG_ERROR(LABEL, "GetLocalNodeDeviceInfo error"); - fulfillMutex_.unlock(); - return Constant::FAILURE; - } - - ACCESSTOKEN_LOG_DEBUG(LABEL, "call softbus finished, networkId:%{public}s, name:%{public}s, type:%{public}d", - info.networkId, info.deviceName, info.deviceTypeId); - - std::string uuid = GetUuidByNodeId(info.networkId); - std::string udid = GetUdidByNodeId(info.networkId); - if (uuid.empty() || udid.empty()) { - ACCESSTOKEN_LOG_ERROR(LABEL, "FulfillLocalDeviceInfo: uuid or udid is empty, abort."); - fulfillMutex_.unlock(); - return Constant::FAILURE; - } - - DeviceInfoManager::GetInstance().AddDeviceInfo(info.networkId, uuid, udid, info.deviceName, - std::to_string(info.deviceTypeId)); - ACCESSTOKEN_LOG_DEBUG(LABEL, "AddDeviceInfo finished, networkId:%{public}s", - info.networkId); - - fulfillMutex_.unlock(); - return Constant::SUCCESS; -} -} // namespace AccessToken -} // namespace Security -} // namespace OHOS +/* + * Copyright (c) 2022 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 "soft_bus_manager.h" + +#include +#include "constant_common.h" +#include "device_info_manager.h" +#include "parameter.h" +#include "softbus_bus_center.h" +#include "dm_device_info.h" +#include "remote_command_manager.h" + +namespace OHOS { +namespace Security { +namespace AccessToken { +namespace { +static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_ACCESSTOKEN, "SoftBusManager"}; +} +namespace { +static const std::string SESSION_GROUP_ID = "atm_dsoftbus_session_group_id"; +static const SessionAttribute SESSION_ATTR = {.dataType = TYPE_BYTES}; + +static const int REASON_EXIST = -3; +static const int OPENSESSION_RETRY_TIMES = 10 * 3; +static const int OPENSESSION_RETRY_INTERVAL_MS = 100; +static const int UDID_MAX_LENGTH = 128; // udid/uuid max length +} // namespace + +const std::string SoftBusManager::TOKEN_SYNC_PACKAGE_NAME = "ohos.security.distributed_access_token"; +const std::string SoftBusManager::SESSION_NAME = "ohos.security.atm_channel"; + +SoftBusManager::SoftBusManager() : isSoftBusServiceBindSuccess_(false), inited_(false), mutex_(), fulfillMutex_() +{ + ACCESSTOKEN_LOG_DEBUG(LABEL, "SoftBusManager()"); +} + +SoftBusManager::~SoftBusManager() +{ + ACCESSTOKEN_LOG_DEBUG(LABEL, "~SoftBusManager()"); +} + +SoftBusManager &SoftBusManager::GetInstance() +{ + static SoftBusManager instance; + return instance; +} + +int SoftBusManager::AddTrustedDeviceInfo() +{ + std::string packageName = TOKEN_SYNC_PACKAGE_NAME; + std::string extra = ""; + std::vector deviceList; + + int32_t ret = DistributedHardware::DeviceManager::GetInstance().GetTrustedDeviceList(packageName, + extra, deviceList); + if (ret != Constant::SUCCESS) { + ACCESSTOKEN_LOG_ERROR(LABEL, "AddTrustedDeviceInfo: GetTrustedDeviceList error, result: %{public}d", ret); + return Constant::FAILURE; + } + + for (DistributedHardware::DmDeviceInfo device : deviceList) { + std::string uuid = GetUuidByNodeId(device.networkId); + std::string udid = GetUdidByNodeId(device.networkId); + if (uuid.empty() || udid.empty()) { + ACCESSTOKEN_LOG_ERROR(LABEL, "uuid = %{public}s, udid = %{public}s, uuid or udid is empty, abort.", + uuid.c_str(), ConstantCommon::EncryptDevId(udid).c_str()); + continue; + } + + DeviceInfoManager::GetInstance().AddDeviceInfo(device.networkId, uuid, udid, device.deviceName, + std::to_string(device.deviceTypeId)); + RemoteCommandManager::GetInstance().NotifyDeviceOnline(udid); + } + + return Constant::SUCCESS; +} + +int SoftBusManager::DeviceInit() +{ + std::string packageName = TOKEN_SYNC_PACKAGE_NAME; + std::shared_ptr ptrDmInitCallback = std::make_shared(); + + int ret = DistributedHardware::DeviceManager::GetInstance().InitDeviceManager(packageName, ptrDmInitCallback); + if (ret != ERR_OK) { + ACCESSTOKEN_LOG_ERROR(LABEL, "Initialize: InitDeviceManager error, result: %{public}d", ret); + return ret; + } + + ret = AddTrustedDeviceInfo(); + if (ret != ERR_OK) { + ACCESSTOKEN_LOG_ERROR(LABEL, "Initialize: AddTrustedDeviceInfo error, result: %{public}d", ret); + return ret; + } + + std::string extra = ""; + std::shared_ptr ptrDeviceStateCallback = + std::make_shared(); + ret = DistributedHardware::DeviceManager::GetInstance().RegisterDevStateCallback(packageName, extra, + ptrDeviceStateCallback); + if (ret != ERR_OK) { + ACCESSTOKEN_LOG_ERROR(LABEL, "Initialize: RegisterDevStateCallback error, result: %{public}d", ret); + return ret; + } + + return ERR_OK; +} + +int SoftBusManager::SessionInit() +{ + // register session listener + ISessionListener sessionListener; + sessionListener.OnSessionOpened = SoftBusSessionListener::OnSessionOpened; + sessionListener.OnSessionClosed = SoftBusSessionListener::OnSessionClosed; + sessionListener.OnBytesReceived = SoftBusSessionListener::OnBytesReceived; + sessionListener.OnMessageReceived = SoftBusSessionListener::OnMessageReceived; + + int ret = ::CreateSessionServer(TOKEN_SYNC_PACKAGE_NAME.c_str(), SESSION_NAME.c_str(), &sessionListener); + ACCESSTOKEN_LOG_INFO(LABEL, "Initialize: createSessionServer, result: %{public}d", ret); + // REASON_EXIST + if ((ret != Constant::SUCCESS) && (ret != REASON_EXIST)) { + ACCESSTOKEN_LOG_ERROR(LABEL, "Initialize: CreateSessionServer error, result: %{public}d", ret); + return ret; + } + + return ERR_OK; +} + +void SoftBusManager::Initialize() +{ + bool inited = false; + // cas failed means already inited. + if (!inited_.compare_exchange_strong(inited, true)) { + ACCESSTOKEN_LOG_DEBUG(LABEL, "already initialized, skip"); + return; + } + + std::function runner = [&]() { + auto sleepTime = std::chrono::milliseconds(1000); + while (1) { + std::unique_lock lock(mutex_); + + int ret = DeviceInit(); + if (ret != ERR_OK) { + std::this_thread::sleep_for(sleepTime); + continue; + } + + ret = SessionInit(); + if (ret != ERR_OK) { + std::this_thread::sleep_for(sleepTime); + continue; + } + + isSoftBusServiceBindSuccess_ = true; + this->FulfillLocalDeviceInfo(); + return; + } + }; + + std::thread initThread(runner); + initThread.detach(); + ACCESSTOKEN_LOG_DEBUG(LABEL, "Initialize thread started"); +} + +void SoftBusManager::Destroy() +{ + ACCESSTOKEN_LOG_DEBUG(LABEL, "destroy, init: %{public}d, isSoftBusServiceBindSuccess: %{public}d", inited_.load(), + isSoftBusServiceBindSuccess_); + + if (!inited_.load()) { + ACCESSTOKEN_LOG_DEBUG(LABEL, "not inited, skip"); + return; + } + + std::unique_lock lock(mutex_); + if (!inited_.load()) { + ACCESSTOKEN_LOG_DEBUG(LABEL, "not inited, skip"); + return; + } + + if (isSoftBusServiceBindSuccess_) { + int32_t ret = ::RemoveSessionServer(TOKEN_SYNC_PACKAGE_NAME.c_str(), SESSION_NAME.c_str()); + ACCESSTOKEN_LOG_DEBUG(LABEL, "destroy, RemoveSessionServer: %{public}d", ret); + isSoftBusServiceBindSuccess_ = false; + } + + std::string packageName = TOKEN_SYNC_PACKAGE_NAME; + int ret = DistributedHardware::DeviceManager::GetInstance().UnRegisterDevStateCallback(packageName); + if (ret != ERR_OK) { + ACCESSTOKEN_LOG_ERROR(LABEL, "UnRegisterDevStateCallback failed, code: %{public}d", ret); + } + ret = DistributedHardware::DeviceManager::GetInstance().UnInitDeviceManager(packageName); + if (ret != ERR_OK) { + ACCESSTOKEN_LOG_ERROR(LABEL, "UnInitDeviceManager failed, code: %{public}d", ret); + } + + inited_.store(false); + + ACCESSTOKEN_LOG_DEBUG(LABEL, "destroy, done"); +} + +int32_t SoftBusManager::OpenSession(const std::string &deviceId) +{ +#ifdef DEBUG_API_PERFORMANCE + ACCESSTOKEN_LOG_INFO(LABEL, "api_performance:start open session"); +#endif + + DeviceInfo info; + bool result = DeviceInfoManager::GetInstance().GetDeviceInfo(deviceId, DeviceIdType::UNKNOWN, info); + if (!result) { + ACCESSTOKEN_LOG_WARN(LABEL, "device info notfound for deviceId %{public}s", ConstantCommon::EncryptDevId(deviceId).c_str()); + return Constant::FAILURE; + } + std::string networkId = info.deviceId.networkId; + ACCESSTOKEN_LOG_INFO(LABEL, "openSession, networkId: %{public}s", networkId.c_str()); + + // async open session, should waitting for OnSessionOpened event. + int sessionId = ::OpenSession(SESSION_NAME.c_str(), SESSION_NAME.c_str(), networkId.c_str(), + SESSION_GROUP_ID.c_str(), &SESSION_ATTR); + + ACCESSTOKEN_LOG_DEBUG(LABEL, "async open session"); + + // wait session opening + int retryTimes = 0; + int logSpan = 10; + auto sleepTime = std::chrono::milliseconds(OPENSESSION_RETRY_INTERVAL_MS); + while (retryTimes++ < OPENSESSION_RETRY_TIMES) { + if (SoftBusSessionListener::GetSessionState(sessionId) < 0) { + std::this_thread::sleep_for(sleepTime); + if (retryTimes % logSpan == 0) { + ACCESSTOKEN_LOG_INFO(LABEL, "openSession, waitting for: %{public}d ms", + retryTimes * OPENSESSION_RETRY_INTERVAL_MS); + } + continue; + } + break; + } +#ifdef DEBUG_API_PERFORMANCE + ACCESSTOKEN_LOG_INFO(LABEL, "api_performance:start open session success"); +#endif + int64_t state = SoftBusSessionListener::GetSessionState(sessionId); + if (state < 0) { + ACCESSTOKEN_LOG_ERROR(LABEL, "openSession, timeout, session: %{public}" PRId64, state); + return Constant::FAILURE; + } + + SoftBusSessionListener::DeleteSessionIdFromMap(sessionId); + + ACCESSTOKEN_LOG_DEBUG(LABEL, "openSession, succeed, session: %{public}" PRId64, state); + return sessionId; +} + +int SoftBusManager::CloseSession(int sessionId) +{ + if (sessionId < 0) { + ACCESSTOKEN_LOG_INFO(LABEL, "closeSession: session is invalid"); + return Constant::FAILURE; + } + + ::CloseSession(sessionId); + ACCESSTOKEN_LOG_INFO(LABEL, "closeSession "); + return Constant::SUCCESS; +} + +std::string SoftBusManager::GetUniversallyUniqueIdByNodeId(const std::string &nodeId) +{ + if (!DataValidator::IsDeviceIdValid(nodeId)) { + ACCESSTOKEN_LOG_ERROR(LABEL, "invalid nodeId: %{public}s", nodeId.c_str()); + return ""; + } + + std::string uuid = GetUuidByNodeId(nodeId); + if (uuid.empty()) { + ACCESSTOKEN_LOG_ERROR(LABEL, "softbus return null or empty string [%{public}s]", uuid.c_str()); + return ""; + } + + DeviceInfo info; + bool result = DeviceInfoManager::GetInstance().GetDeviceInfo(uuid, DeviceIdType::UNIVERSALLY_UNIQUE_ID, info); + if (!result) { + ACCESSTOKEN_LOG_DEBUG(LABEL, "local device info not found for uuid %{public}s", uuid.c_str()); + } else { + std::string dimUuid = info.deviceId.universallyUniqueId; + if (uuid == dimUuid) { + // refresh cache + std::function fulfillDeviceInfo = std::bind(&SoftBusManager::FulfillLocalDeviceInfo, this); + std::thread fulfill(fulfillDeviceInfo); + fulfill.detach(); + } + } + + return uuid; +} + +std::string SoftBusManager::GetUniqueDeviceIdByNodeId(const std::string &nodeId) +{ + if (!DataValidator::IsDeviceIdValid(nodeId)) { + ACCESSTOKEN_LOG_ERROR(LABEL, "invalid nodeId: %{public}s", nodeId.c_str()); + return ""; + } + std::string udid = GetUdidByNodeId(nodeId); + if (udid.empty()) { + ACCESSTOKEN_LOG_ERROR(LABEL, "softbus return null or empty string: %{public}s", ConstantCommon::EncryptDevId(udid).c_str()); + return ""; + } + char localUdid[ConstantCommon::DEVICE_UUID_LENGTH] = {0}; + ::GetDevUdid(localUdid, ConstantCommon::DEVICE_UUID_LENGTH); + if (udid == localUdid) { + // refresh cache + std::function fulfillDeviceInfo = std::bind(&SoftBusManager::FulfillLocalDeviceInfo, this); + std::thread fulfill(fulfillDeviceInfo); + fulfill.detach(); + } + return udid; +} + +std::string SoftBusManager::GetUuidByNodeId(const std::string &nodeId) const +{ + uint8_t *info = new uint8_t[UDID_MAX_LENGTH + 1]; + if (info == nullptr) { + ACCESSTOKEN_LOG_ERROR(LABEL, "no enough memory: %{public}d", UDID_MAX_LENGTH); + return ""; + } + (void)memset_s(info, UDID_MAX_LENGTH + 1, 0, UDID_MAX_LENGTH + 1); + int32_t ret = ::GetNodeKeyInfo(TOKEN_SYNC_PACKAGE_NAME.c_str(), nodeId.c_str(), + NodeDeviceInfoKey::NODE_KEY_UUID, info, UDID_MAX_LENGTH); + if (ret != Constant::SUCCESS) { + delete[] info; + ACCESSTOKEN_LOG_WARN(LABEL, "GetNodeKeyInfo error, return code: %{public}d", ret); + return ""; + } + std::string uuid(reinterpret_cast(info)); + delete[] info; + ACCESSTOKEN_LOG_DEBUG(LABEL, "call softbus finished. nodeId(in): %{public}s, uuid: %{public}s", nodeId.c_str(), + uuid.c_str()); + return uuid; +} + +std::string SoftBusManager::GetUdidByNodeId(const std::string &nodeId) const +{ + uint8_t *info = new uint8_t[UDID_MAX_LENGTH + 1]; + if (info == nullptr) { + ACCESSTOKEN_LOG_ERROR(LABEL, "no enough memory: %{public}d", UDID_MAX_LENGTH); + return ""; + } + (void)memset_s(info, UDID_MAX_LENGTH + 1, 0, UDID_MAX_LENGTH + 1); + int32_t ret = ::GetNodeKeyInfo(TOKEN_SYNC_PACKAGE_NAME.c_str(), nodeId.c_str(), + NodeDeviceInfoKey::NODE_KEY_UDID, info, UDID_MAX_LENGTH); + if (ret != Constant::SUCCESS) { + delete[] info; + ACCESSTOKEN_LOG_WARN(LABEL, "GetNodeKeyInfo error, code: %{public}d", ret); + return ""; + } + std::string udid(reinterpret_cast(info)); + delete[] info; + ACCESSTOKEN_LOG_DEBUG(LABEL, "call softbus finished: nodeId(in): %{public}s", nodeId.c_str()); + return udid; +} + +int SoftBusManager::FulfillLocalDeviceInfo() +{ + // repeated task will just skip + if (!fulfillMutex_.try_lock()) { + ACCESSTOKEN_LOG_INFO(LABEL, "FulfillLocalDeviceInfo already running, skip."); + return Constant::SUCCESS; + } + + NodeBasicInfo info; + int32_t ret = ::GetLocalNodeDeviceInfo(TOKEN_SYNC_PACKAGE_NAME.c_str(), &info); + if (ret != Constant::SUCCESS) { + ACCESSTOKEN_LOG_ERROR(LABEL, "GetLocalNodeDeviceInfo error"); + fulfillMutex_.unlock(); + return Constant::FAILURE; + } + + ACCESSTOKEN_LOG_DEBUG(LABEL, "call softbus finished, networkId:%{public}s, name:%{public}s, type:%{public}d", + info.networkId, info.deviceName, info.deviceTypeId); + + std::string uuid = GetUuidByNodeId(info.networkId); + std::string udid = GetUdidByNodeId(info.networkId); + if (uuid.empty() || udid.empty()) { + ACCESSTOKEN_LOG_ERROR(LABEL, "FulfillLocalDeviceInfo: uuid or udid is empty, abort."); + fulfillMutex_.unlock(); + return Constant::FAILURE; + } + + DeviceInfoManager::GetInstance().AddDeviceInfo(info.networkId, uuid, udid, info.deviceName, + std::to_string(info.deviceTypeId)); + ACCESSTOKEN_LOG_DEBUG(LABEL, "AddDeviceInfo finished, networkId:%{public}s", + info.networkId); + + fulfillMutex_.unlock(); + return Constant::SUCCESS; +} +} // namespace AccessToken +} // namespace Security +} // namespace OHOS diff --git a/services/tokensyncmanager/src/service/token_sync_manager_service.cpp b/services/tokensyncmanager/src/service/token_sync_manager_service.cpp index cb6cd72f7..6c4b7bbd2 100644 --- a/services/tokensyncmanager/src/service/token_sync_manager_service.cpp +++ b/services/tokensyncmanager/src/service/token_sync_manager_service.cpp @@ -18,6 +18,7 @@ #include #include "accesstoken_log.h" +#include "constant_common.h" #include "device_info_repository.h" #include "device_info.h" #include "remote_command_manager.h" @@ -95,7 +96,7 @@ int TokenSyncManagerService::GetRemoteHapTokenInfo(const std::string& deviceID, } std::string udid = devInfo.deviceId.uniqueDeviceId; const std::shared_ptr syncRemoteHapTokenCommand = - RemoteCommandFactory::GetInstance().NewSyncRemoteHapTokenCommand(Constant::GetLocalDeviceId(), + RemoteCommandFactory::GetInstance().NewSyncRemoteHapTokenCommand(ConstantCommon::GetLocalDeviceId(), deviceID, tokenID); const int32_t resultCode = RemoteCommandManager::GetInstance().ExecuteCommand(udid, syncRemoteHapTokenCommand); @@ -116,14 +117,14 @@ int TokenSyncManagerService::DeleteRemoteHapTokenInfo(AccessTokenID tokenID) } std::vector devices = DeviceInfoRepository::GetInstance().ListDeviceInfo(); - std::string localUdid = Constant::GetLocalDeviceId(); + std::string localUdid = ConstantCommon::GetLocalDeviceId(); for (DeviceInfo device : devices) { if (device.deviceId.uniqueDeviceId == localUdid) { ACCESSTOKEN_LOG_INFO(LABEL, "no need notify local device"); continue; } const std::shared_ptr deleteRemoteTokenCommand = - RemoteCommandFactory::GetInstance().NewDeleteRemoteTokenCommand(Constant::GetLocalDeviceId(), + RemoteCommandFactory::GetInstance().NewDeleteRemoteTokenCommand(ConstantCommon::GetLocalDeviceId(), device.deviceId.uniqueDeviceId, tokenID); const int32_t resultCode = RemoteCommandManager::GetInstance().ExecuteCommand( @@ -141,7 +142,7 @@ int TokenSyncManagerService::DeleteRemoteHapTokenInfo(AccessTokenID tokenID) int TokenSyncManagerService::UpdateRemoteHapTokenInfo(const HapTokenInfoForSync& tokenInfo) { std::vector devices = DeviceInfoRepository::GetInstance().ListDeviceInfo(); - std::string localUdid = Constant::GetLocalDeviceId(); + std::string localUdid = ConstantCommon::GetLocalDeviceId(); for (DeviceInfo device : devices) { if (device.deviceId.uniqueDeviceId == localUdid) { ACCESSTOKEN_LOG_INFO(LABEL, "no need notify local device"); @@ -149,7 +150,7 @@ int TokenSyncManagerService::UpdateRemoteHapTokenInfo(const HapTokenInfoForSync& } const std::shared_ptr updateRemoteHapTokenCommand = - RemoteCommandFactory::GetInstance().NewUpdateRemoteHapTokenCommand(Constant::GetLocalDeviceId(), + RemoteCommandFactory::GetInstance().NewUpdateRemoteHapTokenCommand(ConstantCommon::GetLocalDeviceId(), device.deviceId.uniqueDeviceId, tokenInfo); const int32_t resultCode = RemoteCommandManager::GetInstance().ExecuteCommand( diff --git a/services/tokensyncmanager/test/unittest/token_sync_service/token_sync_service_test.cpp b/services/tokensyncmanager/test/unittest/token_sync_service/token_sync_service_test.cpp index 396ceaa21..4b92519a9 100644 --- a/services/tokensyncmanager/test/unittest/token_sync_service/token_sync_service_test.cpp +++ b/services/tokensyncmanager/test/unittest/token_sync_service/token_sync_service_test.cpp @@ -25,6 +25,7 @@ #include "accesstoken_kit.h" #include "accesstoken_log.h" #include "base_remote_command.h" +#include "constant_common.h" #include "gtest/gtest.h" #include "session.h" #include "soft_bus_device_connection_listener.h" @@ -91,7 +92,7 @@ void TokenSyncServiceTest::OnDeviceOffline(const DmDeviceInfo &info) "networkId: %{public}s, uuid: %{public}s, udid: %{public}s", networkId.c_str(), uuid.c_str(), - udid.c_str()); + ConstantCommon::EncryptDevId(udid).c_str()); if (uuid != "" && udid != "") { RemoteCommandManager::GetInstance().NotifyDeviceOffline(uuid); -- Gitee From 80a50aaf7e5511aa54ed5df9343a6207af0a5fb4 Mon Sep 17 00:00:00 2001 From: hhhhs9527 Date: Thu, 30 Jun 2022 09:54:21 +0800 Subject: [PATCH 02/15] 2022/06/30 Signed-off-by:hanshu5 Signed-off-by: hhhhs9527 --- interfaces/innerkits/accesstoken/src/accesstoken_kit.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/interfaces/innerkits/accesstoken/src/accesstoken_kit.cpp b/interfaces/innerkits/accesstoken/src/accesstoken_kit.cpp index fd2a1487c..a4c7bafd8 100644 --- a/interfaces/innerkits/accesstoken/src/accesstoken_kit.cpp +++ b/interfaces/innerkits/accesstoken/src/accesstoken_kit.cpp @@ -22,7 +22,6 @@ #include "accesstoken_manager_client.h" #include "constant_common.h" #include "data_validator.h" - namespace OHOS { namespace Security { namespace AccessToken { -- Gitee From 4cb33feacc8d014d1360c762836f8f3cab5d1acb Mon Sep 17 00:00:00 2001 From: hhhhs9527 Date: Thu, 30 Jun 2022 10:10:12 +0800 Subject: [PATCH 03/15] 2022/06/30 Signed-off-by:hanshu5 Signed-off-by: hhhhs9527 --- frameworks/common/BUILD.gn | 5 ++++- interfaces/innerkits/tokensync/src/token_sync_kit.cpp | 1 - 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/frameworks/common/BUILD.gn b/frameworks/common/BUILD.gn index d873a073d..4a87b5d04 100644 --- a/frameworks/common/BUILD.gn +++ b/frameworks/common/BUILD.gn @@ -41,7 +41,10 @@ ohos_shared_library("accesstoken_common_cxx") { "//third_party/mbedtls:mbedtls_shared", "//utils/native/base:utils", ] - external_deps = [ "hiviewdfx_hilog_native:libhilog","startup_l2:syspara", ] + external_deps = [ + "hiviewdfx_hilog_native:libhilog", + "startup_l2:syspara", + ] cflags_cc = [ "-DHILOG_ENABLE" ] } diff --git a/interfaces/innerkits/tokensync/src/token_sync_kit.cpp b/interfaces/innerkits/tokensync/src/token_sync_kit.cpp index 28de089ff..4a3493ef3 100644 --- a/interfaces/innerkits/tokensync/src/token_sync_kit.cpp +++ b/interfaces/innerkits/tokensync/src/token_sync_kit.cpp @@ -21,7 +21,6 @@ #include "accesstoken_log.h" #include "constant_common.h" #include "token_sync_manager_client.h" - namespace OHOS { namespace Security { namespace AccessToken { -- Gitee From afca6e2a67a4f54398ded6ab9286ffb5273aaf9d Mon Sep 17 00:00:00 2001 From: hhhhs9527 Date: Thu, 30 Jun 2022 12:26:05 +0800 Subject: [PATCH 04/15] 2022/06/30 Signed-off-by:hanshu5 Signed-off-by: hhhhs9527 --- frameworks/common/include/constant_common.h | 1 - .../privacymanager/include/common/constant.h | 2 +- .../include/common/constant.h | 1 + .../command/delete_remote_token_command.cpp | 3 +- .../command/sync_remote_hap_token_command.cpp | 3 +- .../sync_remote_native_token_command.cpp | 3 +- .../update_remote_hap_token_command.cpp | 3 +- .../src/device/device_info_manager.cpp | 18 +- .../src/remote/remote_command_executor.cpp | 664 +++++++++--------- .../src/remote/remote_command_manager.cpp | 2 +- 10 files changed, 352 insertions(+), 348 deletions(-) diff --git a/frameworks/common/include/constant_common.h b/frameworks/common/include/constant_common.h index b666920c4..d8bae5c77 100644 --- a/frameworks/common/include/constant_common.h +++ b/frameworks/common/include/constant_common.h @@ -27,7 +27,6 @@ public: * Device id length. */ const static int32_t DEVICE_UUID_LENGTH = 65; - const static int32_t DELAY_SYNC_TOKEN_MS = 3000; static constexpr int32_t ENCRYPTLEN = 4; static constexpr int32_t ENCRYPTBEGIN = 0; static constexpr int32_t ENCRYPTEND = 3; diff --git a/services/privacymanager/include/common/constant.h b/services/privacymanager/include/common/constant.h index ca7f375f0..6d3b77f7c 100644 --- a/services/privacymanager/include/common/constant.h +++ b/services/privacymanager/include/common/constant.h @@ -62,7 +62,7 @@ public: const static int32_t RECORD_DELETE_TIME = 30 * 86400; const static int32_t PRECISE = 60; const static int32_t LATEST_RECORD_TIME = 7 * 86400; - + const static std::map PERMISSION_OPCODE_MAP; public: static bool TransferPermissionToOpcode(const std::string& permissionName, int32_t& opCode); diff --git a/services/tokensyncmanager/include/common/constant.h b/services/tokensyncmanager/include/common/constant.h index 69d9353fe..391c68658 100644 --- a/services/tokensyncmanager/include/common/constant.h +++ b/services/tokensyncmanager/include/common/constant.h @@ -68,6 +68,7 @@ public: * Command result string, indicates failed. */ static const std::string COMMAND_RESULT_FAILED; + const static int32_t DELAY_SYNC_TOKEN_MS = 3000; }; } // namespace AccessToken } // namespace Security diff --git a/services/tokensyncmanager/src/command/delete_remote_token_command.cpp b/services/tokensyncmanager/src/command/delete_remote_token_command.cpp index f09bb04c9..5167e631c 100644 --- a/services/tokensyncmanager/src/command/delete_remote_token_command.cpp +++ b/services/tokensyncmanager/src/command/delete_remote_token_command.cpp @@ -18,6 +18,7 @@ #include "accesstoken_kit.h" #include "accesstoken_log.h" #include "base_remote_command.h" +#include "constant_common.h" #include "device_info.h" #include "device_info_manager.h" @@ -73,7 +74,7 @@ void DeleteRemoteTokenCommand::Prepare() void DeleteRemoteTokenCommand::Execute() { ACCESSTOKEN_LOG_INFO(LABEL, "execute: start as: DeleteRemoteTokenCommand"); - remoteProtocol_.responseDeviceId = Constant::GetLocalDeviceId(); + remoteProtocol_.responseDeviceId = ConstantCommon::GetLocalDeviceId(); remoteProtocol_.responseVersion = Constant::DISTRIBUTED_ACCESS_TOKEN_SERVICE_VERSION; DeviceInfo devInfo; diff --git a/services/tokensyncmanager/src/command/sync_remote_hap_token_command.cpp b/services/tokensyncmanager/src/command/sync_remote_hap_token_command.cpp index 65d0ef836..4e3a48c9d 100644 --- a/services/tokensyncmanager/src/command/sync_remote_hap_token_command.cpp +++ b/services/tokensyncmanager/src/command/sync_remote_hap_token_command.cpp @@ -17,6 +17,7 @@ #include "accesstoken_kit.h" #include "accesstoken_log.h" +#include "constant_common.h" #include "base_remote_command.h" namespace OHOS { @@ -92,7 +93,7 @@ void SyncRemoteHapTokenCommand::Prepare() void SyncRemoteHapTokenCommand::Execute() { ACCESSTOKEN_LOG_INFO(LABEL, "execute: start as: SyncRemoteHapTokenCommand"); - remoteProtocol_.responseDeviceId = Constant::GetLocalDeviceId(); + remoteProtocol_.responseDeviceId = ConstantCommon::GetLocalDeviceId(); remoteProtocol_.responseVersion = Constant::DISTRIBUTED_ACCESS_TOKEN_SERVICE_VERSION; int ret = AccessTokenKit::GetHapTokenInfoFromRemote(requestTokenId_, hapTokenInfo_); diff --git a/services/tokensyncmanager/src/command/sync_remote_native_token_command.cpp b/services/tokensyncmanager/src/command/sync_remote_native_token_command.cpp index db3fec00b..b18e29414 100644 --- a/services/tokensyncmanager/src/command/sync_remote_native_token_command.cpp +++ b/services/tokensyncmanager/src/command/sync_remote_native_token_command.cpp @@ -18,6 +18,7 @@ #include "accesstoken_kit.h" #include "accesstoken_log.h" #include "base_remote_command.h" +#include "constant_common.h" #include "device_info_manager.h" namespace OHOS { @@ -76,7 +77,7 @@ void SyncRemoteNativeTokenCommand::Prepare() void SyncRemoteNativeTokenCommand::Execute() { ACCESSTOKEN_LOG_INFO(LABEL, "execute: start as: SyncRemoteNativeTokenCommand"); - remoteProtocol_.responseDeviceId = Constant::GetLocalDeviceId(); + remoteProtocol_.responseDeviceId = ConstantCommon::GetLocalDeviceId(); remoteProtocol_.responseVersion = Constant::DISTRIBUTED_ACCESS_TOKEN_SERVICE_VERSION; int ret = AccessTokenKit::GetAllNativeTokenInfo(nativeTokenInfo_); diff --git a/services/tokensyncmanager/src/command/update_remote_hap_token_command.cpp b/services/tokensyncmanager/src/command/update_remote_hap_token_command.cpp index 0d6dfe6e6..2adccf0b6 100644 --- a/services/tokensyncmanager/src/command/update_remote_hap_token_command.cpp +++ b/services/tokensyncmanager/src/command/update_remote_hap_token_command.cpp @@ -18,6 +18,7 @@ #include "accesstoken_kit.h" #include "accesstoken_log.h" #include "base_remote_command.h" +#include "constant_common.h" #include "device_info_manager.h" namespace OHOS { @@ -69,7 +70,7 @@ void UpdateRemoteHapTokenCommand::Execute() { ACCESSTOKEN_LOG_INFO(LABEL, "execute: start as: UpdateRemoteHapTokenCommand"); - remoteProtocol_.responseDeviceId = Constant::GetLocalDeviceId(); + remoteProtocol_.responseDeviceId = ConstantCommon::GetLocalDeviceId(); remoteProtocol_.responseVersion = Constant::DISTRIBUTED_ACCESS_TOKEN_SERVICE_VERSION; DeviceInfo devInfo; diff --git a/services/tokensyncmanager/src/device/device_info_manager.cpp b/services/tokensyncmanager/src/device/device_info_manager.cpp index dfe35aa9a..57d78c2cb 100644 --- a/services/tokensyncmanager/src/device/device_info_manager.cpp +++ b/services/tokensyncmanager/src/device/device_info_manager.cpp @@ -14,7 +14,7 @@ */ #include "device_info_manager.h" - +#include "constant_common.h" namespace OHOS { namespace Security { namespace AccessToken { @@ -54,8 +54,8 @@ void DeviceInfoManager::AddDeviceInfo(const std::string &networkId, const std::s void DeviceInfoManager::RemoveAllRemoteDeviceInfo() { - char deviceIdCharArray[Constant::DEVICE_UUID_LENGTH] = {0}; - GetDevUdid(deviceIdCharArray, Constant::DEVICE_UUID_LENGTH); + char deviceIdCharArray[ConstantCommon::DEVICE_UUID_LENGTH] = {0}; + GetDevUdid(deviceIdCharArray, ConstantCommon::DEVICE_UUID_LENGTH); DeviceInfo localDeviceInfoOpt; if (DeviceInfoRepository::GetInstance().FindDeviceInfo( deviceIdCharArray, DeviceIdType::UNIQUE_DISABILITY_ID, localDeviceInfoOpt)) { @@ -69,8 +69,8 @@ void DeviceInfoManager::RemoveRemoteDeviceInfo(const std::string &nodeId, Device ACCESSTOKEN_LOG_ERROR(LABEL, "removeDeviceInfoByNetworkId: nodeId is invalid"); } else { DeviceInfo deviceInfo; - char deviceIdCharArray[Constant::DEVICE_UUID_LENGTH] = {0}; - GetDevUdid(deviceIdCharArray, Constant::DEVICE_UUID_LENGTH); + char deviceIdCharArray[ConstantCommon::DEVICE_UUID_LENGTH] = {0}; + GetDevUdid(deviceIdCharArray, ConstantCommon::DEVICE_UUID_LENGTH); if (DeviceInfoRepository::GetInstance().FindDeviceInfo(nodeId, deviceIdType, deviceInfo)) { if (deviceInfo.deviceId.uniqueDeviceId != deviceIdCharArray) { DeviceInfoRepository::GetInstance().DeleteDeviceInfo(nodeId, deviceIdType); @@ -119,17 +119,17 @@ std::string DeviceInfoManager::ConvertToUniqueDeviceIdOrFetch(const std::string } else { ACCESSTOKEN_LOG_DEBUG(LABEL, "FindDeviceInfo succeed, udid and local udid is empty, nodeId(%{public}s)", - Constant::EncryptDevId(nodeId).c_str()); + nodeId.c_str()); } } else { ACCESSTOKEN_LOG_DEBUG(LABEL, "FindDeviceInfo succeed, udid is empty, nodeId(%{public}s) ", - Constant::EncryptDevId(nodeId).c_str()); + nodeId.c_str()); result = uniqueDeviceId; } } else { ACCESSTOKEN_LOG_DEBUG( - LABEL, "FindDeviceInfo failed, nodeId(%{public}s)", Constant::EncryptDevId(nodeId).c_str()); + LABEL, "FindDeviceInfo failed, nodeId(%{public}s)", nodeId.c_str()); auto list = DeviceInfoRepository::GetInstance().ListDeviceInfo(); auto iter = list.begin(); for (; iter != list.end(); iter++) { @@ -140,7 +140,7 @@ std::string DeviceInfoManager::ConvertToUniqueDeviceIdOrFetch(const std::string LABEL, ">>> DeviceInfoRepository device type: %{public}s", info.deviceType.c_str()); ACCESSTOKEN_LOG_DEBUG(LABEL, ">>> DeviceInfoRepository device network id: %{public}s", - Constant::EncryptDevId(info.deviceId.networkId).c_str()); + info.deviceId.networkId.c_str()); } } return result; diff --git a/services/tokensyncmanager/src/remote/remote_command_executor.cpp b/services/tokensyncmanager/src/remote/remote_command_executor.cpp index ceac49d9d..7d66e6d36 100644 --- a/services/tokensyncmanager/src/remote/remote_command_executor.cpp +++ b/services/tokensyncmanager/src/remote/remote_command_executor.cpp @@ -1,332 +1,332 @@ -/* - * Copyright (c) 2022 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 "remote_command_executor.h" - -#include "device_info_manager.h" -#include "parameter.h" -#include "singleton.h" -#include "soft_bus_channel.h" -#include "token_sync_event_handler.h" -#include "token_sync_manager_service.h" - -namespace OHOS { -namespace Security { -namespace AccessToken { -namespace { -static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_ACCESSTOKEN, "RemoteCommandExecutor"}; -static const std::string TASK_NAME = "RemoteCommandExecutor::ProcessBufferedCommandsWithThread"; -} // namespace -RemoteCommandExecutor::RemoteCommandExecutor(const std::string &targetNodeId) - : targetNodeId_(targetNodeId), ptrChannel_(nullptr), mutex_(), commands_(), running_(false) -{ - ACCESSTOKEN_LOG_DEBUG(LABEL, "RemoteCommandExecutor()"); -} - -RemoteCommandExecutor::~RemoteCommandExecutor() -{ - ACCESSTOKEN_LOG_DEBUG(LABEL, "~RemoteCommandExecutor() begin"); - running_ = false; -} - -const std::shared_ptr RemoteCommandExecutor::CreateChannel(const std::string &targetNodeId) -{ - ACCESSTOKEN_LOG_DEBUG(LABEL, "CreateChannel: targetNodeId=%{public}s", targetNodeId.c_str()); - // only consider SoftBusChannel - std::shared_ptr ptrChannel = std::make_shared(targetNodeId); - if (ptrChannel == nullptr) { - ACCESSTOKEN_LOG_INFO( - LABEL, "CreateChannel: create channel failed, targetNodeId=%{public}s", targetNodeId.c_str()); - } - return ptrChannel; -} - -/* - * called by RemoteCommandExecutor, RemoteCommandManager - */ -int RemoteCommandExecutor::ProcessOneCommand(const std::shared_ptr &ptrCommand) -{ - if (ptrCommand == nullptr) { - ACCESSTOKEN_LOG_WARN( - LABEL, "targetNodeId %{public}s, attempt to process on null command.", targetNodeId_.c_str()); - return Constant::SUCCESS; - } - const std::string uniqueId = ptrCommand->remoteProtocol_.uniqueId; - ACCESSTOKEN_LOG_INFO(LABEL, - "targetNodeId %{public}s, process one command start, uniqueId: %{public}s", - targetNodeId_.c_str(), - uniqueId.c_str()); - - ptrCommand->Prepare(); - int status = ptrCommand->remoteProtocol_.statusCode; - if (status != Constant::SUCCESS) { - ACCESSTOKEN_LOG_ERROR(LABEL, - "targetNodeId %{public}s, process one command error, uniqueId: %{public}s, message: " - "prepare failure code %{public}d", - targetNodeId_.c_str(), - uniqueId.c_str(), - status); - return status; - } - - char localUdid[Constant::DEVICE_UUID_LENGTH] = {0}; - ::GetDevUdid(localUdid, Constant::DEVICE_UUID_LENGTH); - if (targetNodeId_ == localUdid) { - return ExecuteRemoteCommand(ptrCommand, false); - } - - // otherwise a remote device - CreateChannelIfNeeded(); - if (ptrChannel_ == nullptr) { - ACCESSTOKEN_LOG_ERROR(LABEL, "targetNodeId %{public}s, channel is null.", targetNodeId_.c_str()); - return Constant::FAILURE; - } - if (ptrChannel_->BuildConnection() != Constant::SUCCESS) { - ACCESSTOKEN_LOG_ERROR(LABEL, "targetNodeId %{public}s, channel is not ready.", targetNodeId_.c_str()); - return Constant::FAILURE; - } - - return ExecuteRemoteCommand(ptrCommand, true); -} - -/* - * called by RemoteCommandManager - */ -int RemoteCommandExecutor::AddCommand(const std::shared_ptr &ptrCommand) -{ - if (ptrCommand == nullptr) { - ACCESSTOKEN_LOG_DEBUG(LABEL, "targetNodeId %{public}s, attempt to add an empty command.", - targetNodeId_.c_str()); - return Constant::INVALID_COMMAND; - } - - const std::string uniqueId = ptrCommand->remoteProtocol_.uniqueId; - ACCESSTOKEN_LOG_DEBUG( - LABEL, "targetNodeId %{public}s, add uniqueId %{public}s", targetNodeId_.c_str(), uniqueId.c_str()); - - std::unique_lock lock(mutex_); - - // make sure do not have the same command in the command buffer - for (auto bufferedCommand : commands_) { - if (bufferedCommand->remoteProtocol_.uniqueId == uniqueId) { - ACCESSTOKEN_LOG_WARN(LABEL, - "targetNodeId %{public}s, add uniqueId %{public}s, already exist in the buffer, skip", - targetNodeId_.c_str(), - uniqueId.c_str()); - return Constant::SUCCESS; - } - } - - commands_.push_back(ptrCommand); - return Constant::SUCCESS; -} - -/* - * called by RemoteCommandExecutor.ProcessCommandThread, RemoteCommandManager - */ -int RemoteCommandExecutor::ProcessBufferedCommands(bool standalone) -{ - ACCESSTOKEN_LOG_INFO( - LABEL, "begin, targetNodeId: %{public}s, standalone: %{public}d", targetNodeId_.c_str(), standalone); - - std::unique_lock lock(mutex_); - - if (commands_.empty()) { - ACCESSTOKEN_LOG_WARN(LABEL, "no command, targetNodeId %{public}s", targetNodeId_.c_str()); - running_ = false; - return Constant::SUCCESS; - } - - running_ = true; - while (true) { - // interrupt - if (!running_) { - ACCESSTOKEN_LOG_INFO( - LABEL, "end with running flag == false, targetNodeId: %{public}s", targetNodeId_.c_str()); - return Constant::FAILURE; - } - // end - if (commands_.empty()) { - running_ = false; - ACCESSTOKEN_LOG_INFO(LABEL, "end, no command left, targetNodeId: %{public}s", targetNodeId_.c_str()); - return Constant::SUCCESS; - } - - // consume queue to execute - const std::shared_ptr bufferedCommand = commands_.front(); - int status = ProcessOneCommand(bufferedCommand); - if (status == Constant::SUCCESS) { - commands_.pop_front(); - continue; - } else if (status == Constant::FAILURE_BUT_CAN_RETRY) { - ACCESSTOKEN_LOG_WARN(LABEL, - "execute failed and wait to retry, targetNodeId: %{public}s, message: %{public}s, and will retry ", - targetNodeId_.c_str(), - bufferedCommand->remoteProtocol_.message.c_str()); - - // now, the retry at once will have no effective because the network problem - // so if the before the step, one command is added, and run this function - // it should also not need to restart to process the commands buffer at once. - running_ = false; - return Constant::FAILURE; - } else { - // this command failed, move on to execute next command - commands_.pop_front(); - ACCESSTOKEN_LOG_ERROR(LABEL, - "execute failed, targetNodeId: %{public}s, commandName: %{public}s, message: %{public}s", - targetNodeId_.c_str(), - bufferedCommand->remoteProtocol_.commandName.c_str(), - bufferedCommand->remoteProtocol_.message.c_str()); - } - } -} - -/* - * called by RemoteCommandManager - */ -void RemoteCommandExecutor::ProcessBufferedCommandsWithThread() -{ - ACCESSTOKEN_LOG_INFO(LABEL, "begin, targetNodeId: %{public}s", targetNodeId_.c_str()); - - std::unique_lock lock(mutex_); - - if (commands_.empty()) { - ACCESSTOKEN_LOG_INFO(LABEL, "No buffered commands. targetNodeId: %{public}s", targetNodeId_.c_str()); - return; - } - if (running_) { - // task is running, do not need to start one more - ACCESSTOKEN_LOG_WARN(LABEL, "task busy. targetNodeId: %{public}s", targetNodeId_.c_str()); - return; - } - - running_ = true; - const std::function runner = std::bind(&RemoteCommandExecutor::ProcessBufferedCommands, this, true); - - std::shared_ptr handler = - DelayedSingleton::GetInstance()->GetSendEventHandler(); - if (handler == nullptr) { - ACCESSTOKEN_LOG_ERROR(LABEL, "fail to get EventHandler"); - return; - } - bool result = handler->ProxyPostTask(runner, TASK_NAME); - if (!result) { - ACCESSTOKEN_LOG_ERROR(LABEL, "post task failed, targetNodeId: %{public}s", targetNodeId_.c_str()); - } - ACCESSTOKEN_LOG_INFO(LABEL, - "post task succeed, targetNodeId: %{public}s, taskName: %{public}s", - targetNodeId_.c_str(), - TASK_NAME.c_str()); -} - -int RemoteCommandExecutor::ExecuteRemoteCommand( - const std::shared_ptr &ptrCommand, const bool isRemote) -{ - std::string uniqueId = ptrCommand->remoteProtocol_.uniqueId; - ACCESSTOKEN_LOG_INFO(LABEL, - "targetNodeId %{public}s, uniqueId %{public}s, remote %{public}d: start to execute", - targetNodeId_.c_str(), - uniqueId.c_str(), - isRemote); - - ptrCommand->remoteProtocol_.statusCode = Constant::STATUS_CODE_BEFORE_RPC; - - if (!isRemote) { - // Local device, play myself. - ptrCommand->Execute(); - int code = ClientProcessResult(ptrCommand); - ACCESSTOKEN_LOG_DEBUG(LABEL, - "command finished with status: %{public}d, message: %{public}s", - ptrCommand->remoteProtocol_.statusCode, - ptrCommand->remoteProtocol_.message.c_str()); - return code; - } - - std::string responseString = - ptrChannel_->ExecuteCommand(ptrCommand->remoteProtocol_.commandName, ptrCommand->ToJsonPayload()); - ACCESSTOKEN_LOG_INFO(LABEL, "command executed uniqueId %{public}s", uniqueId.c_str()); - if (responseString.empty()) { - ACCESSTOKEN_LOG_WARN(LABEL, - "targetNodeId %{public}s, uniqueId %{public}s, execute remote command error, response is empty.", - targetNodeId_.c_str(), - uniqueId.c_str()); - // if command send failed, also try to close session - if (commands_.empty()) { - ptrChannel_->CloseConnection(); - } - return Constant::FAILURE; - } - - std::shared_ptr ptrResponseCommand = - RemoteCommandFactory::GetInstance().NewRemoteCommandFromJson( - ptrCommand->remoteProtocol_.commandName, responseString); - if (ptrResponseCommand == nullptr) { - ACCESSTOKEN_LOG_ERROR(LABEL, "targetNodeId %{public}s, get null response command!", targetNodeId_.c_str()); - return Constant::FAILURE; - } - int32_t result = ClientProcessResult(ptrResponseCommand); - if (commands_.empty()) { - ptrChannel_->CloseConnection(); - } - ACCESSTOKEN_LOG_DEBUG(LABEL, - "command finished with status: %{public}d, message: %{public}s", - ptrResponseCommand->remoteProtocol_.statusCode, - ptrResponseCommand->remoteProtocol_.message.c_str()); - return result; -} - -void RemoteCommandExecutor::CreateChannelIfNeeded() -{ - std::unique_lock lock(mutex_); - if (ptrChannel_ != nullptr) { - ACCESSTOKEN_LOG_INFO(LABEL, "targetNodeId %{public}s, channel is exist.", targetNodeId_.c_str()); - return; - } - - ptrChannel_ = CreateChannel(targetNodeId_); -} - -int RemoteCommandExecutor::ClientProcessResult(const std::shared_ptr &ptrCommand) -{ - std::string uniqueId = ptrCommand->remoteProtocol_.uniqueId; - if (ptrCommand->remoteProtocol_.statusCode == Constant::STATUS_CODE_BEFORE_RPC) { - ACCESSTOKEN_LOG_ERROR(LABEL, - "targetNodeId %{public}s, uniqueId %{public}s, status code after RPC is same as before, the remote side " - "may not " - "support this command", - targetNodeId_.c_str(), - uniqueId.c_str()); - return Constant::FAILURE; - } - - ptrCommand->Finish(); - int status = ptrCommand->remoteProtocol_.statusCode; - if (status != Constant::SUCCESS) { - ACCESSTOKEN_LOG_ERROR(LABEL, - "targetNodeId %{public}s, uniqueId %{public}s, execute failed, message: %{public}s", - targetNodeId_.c_str(), - uniqueId.c_str(), - ptrCommand->remoteProtocol_.message.c_str()); - } else { - ACCESSTOKEN_LOG_INFO(LABEL, - "targetNodeId %{public}s, uniqueId %{public}s, execute succeed.", - targetNodeId_.c_str(), - uniqueId.c_str()); - } - return status; -} -} // namespace AccessToken -} // namespace Security -} // namespace OHOS +/* + * Copyright (c) 2022 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 "remote_command_executor.h" +#include "constant_common.h" +#include "device_info_manager.h" +#include "parameter.h" +#include "singleton.h" +#include "soft_bus_channel.h" +#include "token_sync_event_handler.h" +#include "token_sync_manager_service.h" + +namespace OHOS { +namespace Security { +namespace AccessToken { +namespace { +static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_ACCESSTOKEN, "RemoteCommandExecutor"}; +static const std::string TASK_NAME = "RemoteCommandExecutor::ProcessBufferedCommandsWithThread"; +} // namespace +RemoteCommandExecutor::RemoteCommandExecutor(const std::string &targetNodeId) + : targetNodeId_(targetNodeId), ptrChannel_(nullptr), mutex_(), commands_(), running_(false) +{ + ACCESSTOKEN_LOG_DEBUG(LABEL, "RemoteCommandExecutor()"); +} + +RemoteCommandExecutor::~RemoteCommandExecutor() +{ + ACCESSTOKEN_LOG_DEBUG(LABEL, "~RemoteCommandExecutor() begin"); + running_ = false; +} + +const std::shared_ptr RemoteCommandExecutor::CreateChannel(const std::string &targetNodeId) +{ + ACCESSTOKEN_LOG_DEBUG(LABEL, "CreateChannel: targetNodeId=%{public}s", targetNodeId.c_str()); + // only consider SoftBusChannel + std::shared_ptr ptrChannel = std::make_shared(targetNodeId); + if (ptrChannel == nullptr) { + ACCESSTOKEN_LOG_INFO( + LABEL, "CreateChannel: create channel failed, targetNodeId=%{public}s", targetNodeId.c_str()); + } + return ptrChannel; +} + +/* + * called by RemoteCommandExecutor, RemoteCommandManager + */ +int RemoteCommandExecutor::ProcessOneCommand(const std::shared_ptr &ptrCommand) +{ + if (ptrCommand == nullptr) { + ACCESSTOKEN_LOG_WARN( + LABEL, "targetNodeId %{public}s, attempt to process on null command.", targetNodeId_.c_str()); + return Constant::SUCCESS; + } + const std::string uniqueId = ptrCommand->remoteProtocol_.uniqueId; + ACCESSTOKEN_LOG_INFO(LABEL, + "targetNodeId %{public}s, process one command start, uniqueId: %{public}s", + targetNodeId_.c_str(), + uniqueId.c_str()); + + ptrCommand->Prepare(); + int status = ptrCommand->remoteProtocol_.statusCode; + if (status != Constant::SUCCESS) { + ACCESSTOKEN_LOG_ERROR(LABEL, + "targetNodeId %{public}s, process one command error, uniqueId: %{public}s, message: " + "prepare failure code %{public}d", + targetNodeId_.c_str(), + uniqueId.c_str(), + status); + return status; + } + + char localUdid[ConstantCommon::DEVICE_UUID_LENGTH] = {0}; + ::GetDevUdid(localUdid, ConstantCommon::DEVICE_UUID_LENGTH); + if (targetNodeId_ == localUdid) { + return ExecuteRemoteCommand(ptrCommand, false); + } + + // otherwise a remote device + CreateChannelIfNeeded(); + if (ptrChannel_ == nullptr) { + ACCESSTOKEN_LOG_ERROR(LABEL, "targetNodeId %{public}s, channel is null.", targetNodeId_.c_str()); + return Constant::FAILURE; + } + if (ptrChannel_->BuildConnection() != Constant::SUCCESS) { + ACCESSTOKEN_LOG_ERROR(LABEL, "targetNodeId %{public}s, channel is not ready.", targetNodeId_.c_str()); + return Constant::FAILURE; + } + + return ExecuteRemoteCommand(ptrCommand, true); +} + +/* + * called by RemoteCommandManager + */ +int RemoteCommandExecutor::AddCommand(const std::shared_ptr &ptrCommand) +{ + if (ptrCommand == nullptr) { + ACCESSTOKEN_LOG_DEBUG(LABEL, "targetNodeId %{public}s, attempt to add an empty command.", + targetNodeId_.c_str()); + return Constant::INVALID_COMMAND; + } + + const std::string uniqueId = ptrCommand->remoteProtocol_.uniqueId; + ACCESSTOKEN_LOG_DEBUG( + LABEL, "targetNodeId %{public}s, add uniqueId %{public}s", targetNodeId_.c_str(), uniqueId.c_str()); + + std::unique_lock lock(mutex_); + + // make sure do not have the same command in the command buffer + for (auto bufferedCommand : commands_) { + if (bufferedCommand->remoteProtocol_.uniqueId == uniqueId) { + ACCESSTOKEN_LOG_WARN(LABEL, + "targetNodeId %{public}s, add uniqueId %{public}s, already exist in the buffer, skip", + targetNodeId_.c_str(), + uniqueId.c_str()); + return Constant::SUCCESS; + } + } + + commands_.push_back(ptrCommand); + return Constant::SUCCESS; +} + +/* + * called by RemoteCommandExecutor.ProcessCommandThread, RemoteCommandManager + */ +int RemoteCommandExecutor::ProcessBufferedCommands(bool standalone) +{ + ACCESSTOKEN_LOG_INFO( + LABEL, "begin, targetNodeId: %{public}s, standalone: %{public}d", targetNodeId_.c_str(), standalone); + + std::unique_lock lock(mutex_); + + if (commands_.empty()) { + ACCESSTOKEN_LOG_WARN(LABEL, "no command, targetNodeId %{public}s", targetNodeId_.c_str()); + running_ = false; + return Constant::SUCCESS; + } + + running_ = true; + while (true) { + // interrupt + if (!running_) { + ACCESSTOKEN_LOG_INFO( + LABEL, "end with running flag == false, targetNodeId: %{public}s", targetNodeId_.c_str()); + return Constant::FAILURE; + } + // end + if (commands_.empty()) { + running_ = false; + ACCESSTOKEN_LOG_INFO(LABEL, "end, no command left, targetNodeId: %{public}s", targetNodeId_.c_str()); + return Constant::SUCCESS; + } + + // consume queue to execute + const std::shared_ptr bufferedCommand = commands_.front(); + int status = ProcessOneCommand(bufferedCommand); + if (status == Constant::SUCCESS) { + commands_.pop_front(); + continue; + } else if (status == Constant::FAILURE_BUT_CAN_RETRY) { + ACCESSTOKEN_LOG_WARN(LABEL, + "execute failed and wait to retry, targetNodeId: %{public}s, message: %{public}s, and will retry ", + targetNodeId_.c_str(), + bufferedCommand->remoteProtocol_.message.c_str()); + + // now, the retry at once will have no effective because the network problem + // so if the before the step, one command is added, and run this function + // it should also not need to restart to process the commands buffer at once. + running_ = false; + return Constant::FAILURE; + } else { + // this command failed, move on to execute next command + commands_.pop_front(); + ACCESSTOKEN_LOG_ERROR(LABEL, + "execute failed, targetNodeId: %{public}s, commandName: %{public}s, message: %{public}s", + targetNodeId_.c_str(), + bufferedCommand->remoteProtocol_.commandName.c_str(), + bufferedCommand->remoteProtocol_.message.c_str()); + } + } +} + +/* + * called by RemoteCommandManager + */ +void RemoteCommandExecutor::ProcessBufferedCommandsWithThread() +{ + ACCESSTOKEN_LOG_INFO(LABEL, "begin, targetNodeId: %{public}s", targetNodeId_.c_str()); + + std::unique_lock lock(mutex_); + + if (commands_.empty()) { + ACCESSTOKEN_LOG_INFO(LABEL, "No buffered commands. targetNodeId: %{public}s", targetNodeId_.c_str()); + return; + } + if (running_) { + // task is running, do not need to start one more + ACCESSTOKEN_LOG_WARN(LABEL, "task busy. targetNodeId: %{public}s", targetNodeId_.c_str()); + return; + } + + running_ = true; + const std::function runner = std::bind(&RemoteCommandExecutor::ProcessBufferedCommands, this, true); + + std::shared_ptr handler = + DelayedSingleton::GetInstance()->GetSendEventHandler(); + if (handler == nullptr) { + ACCESSTOKEN_LOG_ERROR(LABEL, "fail to get EventHandler"); + return; + } + bool result = handler->ProxyPostTask(runner, TASK_NAME); + if (!result) { + ACCESSTOKEN_LOG_ERROR(LABEL, "post task failed, targetNodeId: %{public}s", targetNodeId_.c_str()); + } + ACCESSTOKEN_LOG_INFO(LABEL, + "post task succeed, targetNodeId: %{public}s, taskName: %{public}s", + targetNodeId_.c_str(), + TASK_NAME.c_str()); +} + +int RemoteCommandExecutor::ExecuteRemoteCommand( + const std::shared_ptr &ptrCommand, const bool isRemote) +{ + std::string uniqueId = ptrCommand->remoteProtocol_.uniqueId; + ACCESSTOKEN_LOG_INFO(LABEL, + "targetNodeId %{public}s, uniqueId %{public}s, remote %{public}d: start to execute", + targetNodeId_.c_str(), + uniqueId.c_str(), + isRemote); + + ptrCommand->remoteProtocol_.statusCode = Constant::STATUS_CODE_BEFORE_RPC; + + if (!isRemote) { + // Local device, play myself. + ptrCommand->Execute(); + int code = ClientProcessResult(ptrCommand); + ACCESSTOKEN_LOG_DEBUG(LABEL, + "command finished with status: %{public}d, message: %{public}s", + ptrCommand->remoteProtocol_.statusCode, + ptrCommand->remoteProtocol_.message.c_str()); + return code; + } + + std::string responseString = + ptrChannel_->ExecuteCommand(ptrCommand->remoteProtocol_.commandName, ptrCommand->ToJsonPayload()); + ACCESSTOKEN_LOG_INFO(LABEL, "command executed uniqueId %{public}s", uniqueId.c_str()); + if (responseString.empty()) { + ACCESSTOKEN_LOG_WARN(LABEL, + "targetNodeId %{public}s, uniqueId %{public}s, execute remote command error, response is empty.", + targetNodeId_.c_str(), + uniqueId.c_str()); + // if command send failed, also try to close session + if (commands_.empty()) { + ptrChannel_->CloseConnection(); + } + return Constant::FAILURE; + } + + std::shared_ptr ptrResponseCommand = + RemoteCommandFactory::GetInstance().NewRemoteCommandFromJson( + ptrCommand->remoteProtocol_.commandName, responseString); + if (ptrResponseCommand == nullptr) { + ACCESSTOKEN_LOG_ERROR(LABEL, "targetNodeId %{public}s, get null response command!", targetNodeId_.c_str()); + return Constant::FAILURE; + } + int32_t result = ClientProcessResult(ptrResponseCommand); + if (commands_.empty()) { + ptrChannel_->CloseConnection(); + } + ACCESSTOKEN_LOG_DEBUG(LABEL, + "command finished with status: %{public}d, message: %{public}s", + ptrResponseCommand->remoteProtocol_.statusCode, + ptrResponseCommand->remoteProtocol_.message.c_str()); + return result; +} + +void RemoteCommandExecutor::CreateChannelIfNeeded() +{ + std::unique_lock lock(mutex_); + if (ptrChannel_ != nullptr) { + ACCESSTOKEN_LOG_INFO(LABEL, "targetNodeId %{public}s, channel is exist.", targetNodeId_.c_str()); + return; + } + + ptrChannel_ = CreateChannel(targetNodeId_); +} + +int RemoteCommandExecutor::ClientProcessResult(const std::shared_ptr &ptrCommand) +{ + std::string uniqueId = ptrCommand->remoteProtocol_.uniqueId; + if (ptrCommand->remoteProtocol_.statusCode == Constant::STATUS_CODE_BEFORE_RPC) { + ACCESSTOKEN_LOG_ERROR(LABEL, + "targetNodeId %{public}s, uniqueId %{public}s, status code after RPC is same as before, the remote side " + "may not " + "support this command", + targetNodeId_.c_str(), + uniqueId.c_str()); + return Constant::FAILURE; + } + + ptrCommand->Finish(); + int status = ptrCommand->remoteProtocol_.statusCode; + if (status != Constant::SUCCESS) { + ACCESSTOKEN_LOG_ERROR(LABEL, + "targetNodeId %{public}s, uniqueId %{public}s, execute failed, message: %{public}s", + targetNodeId_.c_str(), + uniqueId.c_str(), + ptrCommand->remoteProtocol_.message.c_str()); + } else { + ACCESSTOKEN_LOG_INFO(LABEL, + "targetNodeId %{public}s, uniqueId %{public}s, execute succeed.", + targetNodeId_.c_str(), + uniqueId.c_str()); + } + return status; +} +} // namespace AccessToken +} // namespace Security +} // namespace OHOS diff --git a/services/tokensyncmanager/src/remote/remote_command_manager.cpp b/services/tokensyncmanager/src/remote/remote_command_manager.cpp index 3db3d103e..4af330090 100644 --- a/services/tokensyncmanager/src/remote/remote_command_manager.cpp +++ b/services/tokensyncmanager/src/remote/remote_command_manager.cpp @@ -194,7 +194,7 @@ int RemoteCommandManager::NotifyDeviceOnline(const std::string &nodeId) ACCESSTOKEN_LOG_ERROR(LABEL, "fail to get EventHandler"); return Constant::FAILURE; } - handler->ProxyPostTask(delayed, "HandleDeviceOnline", ConstantCommon::DELAY_SYNC_TOKEN_MS); + handler->ProxyPostTask(delayed, "HandleDeviceOnline", Constant::DELAY_SYNC_TOKEN_MS); return Constant::SUCCESS; } -- Gitee From 3ee87949f1db06fe82ae3ad91c84767cdff88821 Mon Sep 17 00:00:00 2001 From: hhhhs9527 Date: Thu, 30 Jun 2022 12:44:15 +0800 Subject: [PATCH 05/15] 2022/06/30 Signed-off-by:hanshu5 Signed-off-by: hhhhs9527 --- frameworks/common/BUILD.gn | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frameworks/common/BUILD.gn b/frameworks/common/BUILD.gn index 4a87b5d04..e534fc59b 100644 --- a/frameworks/common/BUILD.gn +++ b/frameworks/common/BUILD.gn @@ -32,9 +32,9 @@ ohos_shared_library("accesstoken_common_cxx") { ] sources = [ + "src/constant_common.cpp", "src/data_validator.cpp", "src/random_mbedtls.cpp", - "src/constant_common.cpp", ] deps = [ -- Gitee From 7689fc0fe3f2ecd9326bcfdfbf3edc1b9c69a59d Mon Sep 17 00:00:00 2001 From: hhhhs9527 Date: Thu, 30 Jun 2022 13:19:43 +0800 Subject: [PATCH 06/15] 2022/06/30 Signed-off-by:hanshu5 Signed-off-by: hhhhs9527 --- frameworks/common/src/constant_common.cpp | 2 +- .../command/delete_remote_token_command.cpp | 1 + .../command/sync_remote_hap_token_command.cpp | 1 + .../sync_remote_native_token_command.cpp | 1 + .../update_remote_hap_token_command.cpp | 1 + .../test/mock/src/constant_mock.cpp | 18 ++++++++---------- 6 files changed, 13 insertions(+), 11 deletions(-) diff --git a/frameworks/common/src/constant_common.cpp b/frameworks/common/src/constant_common.cpp index feabc4277..3ec9cb6d3 100644 --- a/frameworks/common/src/constant_common.cpp +++ b/frameworks/common/src/constant_common.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2022 Huawei Device Co., Ltd. + * Copyright (c) 2022 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 diff --git a/services/tokensyncmanager/src/command/delete_remote_token_command.cpp b/services/tokensyncmanager/src/command/delete_remote_token_command.cpp index 5167e631c..a0043c0e9 100644 --- a/services/tokensyncmanager/src/command/delete_remote_token_command.cpp +++ b/services/tokensyncmanager/src/command/delete_remote_token_command.cpp @@ -19,6 +19,7 @@ #include "accesstoken_log.h" #include "base_remote_command.h" #include "constant_common.h" +#include "constant.h" #include "device_info.h" #include "device_info_manager.h" diff --git a/services/tokensyncmanager/src/command/sync_remote_hap_token_command.cpp b/services/tokensyncmanager/src/command/sync_remote_hap_token_command.cpp index 4e3a48c9d..dc12d829b 100644 --- a/services/tokensyncmanager/src/command/sync_remote_hap_token_command.cpp +++ b/services/tokensyncmanager/src/command/sync_remote_hap_token_command.cpp @@ -18,6 +18,7 @@ #include "accesstoken_kit.h" #include "accesstoken_log.h" #include "constant_common.h" +#include "constant.h" #include "base_remote_command.h" namespace OHOS { diff --git a/services/tokensyncmanager/src/command/sync_remote_native_token_command.cpp b/services/tokensyncmanager/src/command/sync_remote_native_token_command.cpp index b18e29414..8b54c70ba 100644 --- a/services/tokensyncmanager/src/command/sync_remote_native_token_command.cpp +++ b/services/tokensyncmanager/src/command/sync_remote_native_token_command.cpp @@ -19,6 +19,7 @@ #include "accesstoken_log.h" #include "base_remote_command.h" #include "constant_common.h" +#include "constant.h" #include "device_info_manager.h" namespace OHOS { diff --git a/services/tokensyncmanager/src/command/update_remote_hap_token_command.cpp b/services/tokensyncmanager/src/command/update_remote_hap_token_command.cpp index 2adccf0b6..78865714e 100644 --- a/services/tokensyncmanager/src/command/update_remote_hap_token_command.cpp +++ b/services/tokensyncmanager/src/command/update_remote_hap_token_command.cpp @@ -19,6 +19,7 @@ #include "accesstoken_log.h" #include "base_remote_command.h" #include "constant_common.h" +#include "constant.h" #include "device_info_manager.h" namespace OHOS { diff --git a/services/tokensyncmanager/test/mock/src/constant_mock.cpp b/services/tokensyncmanager/test/mock/src/constant_mock.cpp index 6998e7fd6..4b69e132b 100644 --- a/services/tokensyncmanager/test/mock/src/constant_mock.cpp +++ b/services/tokensyncmanager/test/mock/src/constant_mock.cpp @@ -12,7 +12,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#include "constant.h" +#include "constant_common.h" namespace OHOS { namespace Security { @@ -20,21 +20,19 @@ namespace AccessToken { namespace { static const std::string REPLACE_TARGET = "****"; } // namespace -const std::string Constant::COMMAND_RESULT_SUCCESS = "success"; -const std::string Constant::COMMAND_RESULT_FAILED = "execute command failed"; -std::string Constant::EncryptDevId(std::string deviceId) +std::string ConstantCommon::EncryptDevId(std::string deviceId) { - std::string result = deviceId; - if (deviceId.size() >= ENCRYPTLEN) { - result.replace(ENCRYPTBEGIN, ENCRYPTEND, REPLACE_TARGET); + std::string result=deviceId; + if (deviceId.size() > 2*ENCRYPTLEN) { + result.replace(ENCRYPTBEGIN + ENCRYPTLEN, deviceId.size() - 2 * ENCRYPTLEN, REPLACE_TARGET); } else { - result.replace(ENCRYPTBEGIN, result.size() - 1, REPLACE_TARGET); - } + result.replace(ENCRYPTBEGIN, deviceId.size(), REPLACE_TARGET); + } return result; } -std::string Constant::GetLocalDeviceId() +std::string ConstantCommon::GetLocalDeviceId() { return "local:udid-001"; } -- Gitee From 380181cbec62c22a13ae723690119a35c9702ebb Mon Sep 17 00:00:00 2001 From: hhhhs9527 Date: Thu, 30 Jun 2022 13:51:26 +0800 Subject: [PATCH 07/15] 2022/06/30 Signed-off-by:hanshu5 Signed-off-by: hhhhs9527 --- frameworks/common/BUILD.gn | 4 ++-- services/tokensyncmanager/include/common/constant.h | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/frameworks/common/BUILD.gn b/frameworks/common/BUILD.gn index e534fc59b..e3629cf62 100644 --- a/frameworks/common/BUILD.gn +++ b/frameworks/common/BUILD.gn @@ -41,9 +41,9 @@ ohos_shared_library("accesstoken_common_cxx") { "//third_party/mbedtls:mbedtls_shared", "//utils/native/base:utils", ] - external_deps = [ + external_deps = [ "hiviewdfx_hilog_native:libhilog", - "startup_l2:syspara", + "startup_l2:syspara", ] cflags_cc = [ "-DHILOG_ENABLE" ] diff --git a/services/tokensyncmanager/include/common/constant.h b/services/tokensyncmanager/include/common/constant.h index 391c68658..9e37085cd 100644 --- a/services/tokensyncmanager/include/common/constant.h +++ b/services/tokensyncmanager/include/common/constant.h @@ -62,12 +62,12 @@ public: /** * Command result string, indicates success. */ - static const std::string COMMAND_RESULT_SUCCESS; + static const std::string COMMAND_RESULT_SUCCESS = "success"; /** * Command result string, indicates failed. */ - static const std::string COMMAND_RESULT_FAILED; + static const std::string COMMAND_RESULT_FAILED = "execute command failed"; const static int32_t DELAY_SYNC_TOKEN_MS = 3000; }; } // namespace AccessToken -- Gitee From ed6ef3896122dcd8bc887733d39ba199542512c7 Mon Sep 17 00:00:00 2001 From: hhhhs9527 Date: Thu, 30 Jun 2022 13:52:58 +0800 Subject: [PATCH 08/15] 2022/06/30 Signed-off-by:hanshu5 Signed-off-by: hhhhs9527 --- services/tokensyncmanager/include/common/constant.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/services/tokensyncmanager/include/common/constant.h b/services/tokensyncmanager/include/common/constant.h index 9e37085cd..391c68658 100644 --- a/services/tokensyncmanager/include/common/constant.h +++ b/services/tokensyncmanager/include/common/constant.h @@ -62,12 +62,12 @@ public: /** * Command result string, indicates success. */ - static const std::string COMMAND_RESULT_SUCCESS = "success"; + static const std::string COMMAND_RESULT_SUCCESS; /** * Command result string, indicates failed. */ - static const std::string COMMAND_RESULT_FAILED = "execute command failed"; + static const std::string COMMAND_RESULT_FAILED; const static int32_t DELAY_SYNC_TOKEN_MS = 3000; }; } // namespace AccessToken -- Gitee From 6240399020668fabe95c1d7b5af27d2372b343ec Mon Sep 17 00:00:00 2001 From: hhhhs9527 Date: Thu, 30 Jun 2022 14:21:03 +0800 Subject: [PATCH 09/15] 2022/06/30 Signed-off-by:hanshu5 Signed-off-by: hhhhs9527 --- interfaces/innerkits/tokensync/BUILD.gn | 2 +- services/tokensyncmanager/test/mock/src/constant_mock.cpp | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/interfaces/innerkits/tokensync/BUILD.gn b/interfaces/innerkits/tokensync/BUILD.gn index f9bc3b6fc..c595ead52 100644 --- a/interfaces/innerkits/tokensync/BUILD.gn +++ b/interfaces/innerkits/tokensync/BUILD.gn @@ -49,8 +49,8 @@ ohos_shared_library("libtokensync_sdk") { deps = [ "//base/security/access_token/frameworks/accesstoken:accesstoken_communication_adapter_cxx", - "//utils/native/base:utils", "//base/security/access_token/frameworks/common:accesstoken_common_cxx", + "//utils/native/base:utils", ] external_deps = [ diff --git a/services/tokensyncmanager/test/mock/src/constant_mock.cpp b/services/tokensyncmanager/test/mock/src/constant_mock.cpp index 4b69e132b..4a96e31f4 100644 --- a/services/tokensyncmanager/test/mock/src/constant_mock.cpp +++ b/services/tokensyncmanager/test/mock/src/constant_mock.cpp @@ -13,6 +13,7 @@ * limitations under the License. */ #include "constant_common.h" +#include "constant.h" namespace OHOS { namespace Security { @@ -20,7 +21,8 @@ namespace AccessToken { namespace { static const std::string REPLACE_TARGET = "****"; } // namespace - +const std::string Constant::COMMAND_RESULT_SUCCESS = "success"; +const std::string Constant::COMMAND_RESULT_FAILED = "execute command failed"; std::string ConstantCommon::EncryptDevId(std::string deviceId) { std::string result=deviceId; -- Gitee From 78a831d19d76811c97726287acb678853869fb6c Mon Sep 17 00:00:00 2001 From: hhhhs9527 Date: Thu, 30 Jun 2022 15:35:37 +0800 Subject: [PATCH 10/15] 2022/06/30 Signed-off-by:hanshu5 Signed-off-by: hhhhs9527 --- frameworks/common/include/constant_common.h | 1 + frameworks/common/src/constant_common.cpp | 8 +++---- .../accesstoken/src/accesstoken_kit.cpp | 6 ++++-- .../src/token/accesstoken_info_manager.cpp | 10 +++++---- .../accesstoken_remote_token_manager.cpp | 21 +++++++++++-------- .../src/service/privacy_manager_service.cpp | 3 ++- .../src/remote/remote_command_manager.cpp | 11 ++++++---- .../src/remote/soft_bus_manager.cpp | 6 ++++-- .../test/mock/src/constant_mock.cpp | 8 +++---- .../token_sync_service_test.cpp | 2 +- 10 files changed, 45 insertions(+), 31 deletions(-) diff --git a/frameworks/common/include/constant_common.h b/frameworks/common/include/constant_common.h index d8bae5c77..f520054fc 100644 --- a/frameworks/common/include/constant_common.h +++ b/frameworks/common/include/constant_common.h @@ -27,6 +27,7 @@ public: * Device id length. */ const static int32_t DEVICE_UUID_LENGTH = 65; + static constexpr int32_t MINDEVICEIDLEN = 8; static constexpr int32_t ENCRYPTLEN = 4; static constexpr int32_t ENCRYPTBEGIN = 0; static constexpr int32_t ENCRYPTEND = 3; diff --git a/frameworks/common/src/constant_common.cpp b/frameworks/common/src/constant_common.cpp index 3ec9cb6d3..1ecf48f55 100644 --- a/frameworks/common/src/constant_common.cpp +++ b/frameworks/common/src/constant_common.cpp @@ -23,12 +23,12 @@ static const std::string REPLACE_TARGET = "****"; } // namespace std::string ConstantCommon::EncryptDevId(std::string deviceId) { - std::string result=deviceId; - if (deviceId.size() > 2*ENCRYPTLEN) { - result.replace(ENCRYPTBEGIN + ENCRYPTLEN, deviceId.size() - 2 * ENCRYPTLEN, REPLACE_TARGET); + std::string result = deviceId; + if (deviceId.size() > MINDEVICEIDLEN) { + result.replace(ENCRYPTBEGIN + ENCRYPTLEN, deviceId.size() - MINDEVICEIDLEN, REPLACE_TARGET); } else { result.replace(ENCRYPTBEGIN, deviceId.size(), REPLACE_TARGET); - } + } return result; } diff --git a/interfaces/innerkits/accesstoken/src/accesstoken_kit.cpp b/interfaces/innerkits/accesstoken/src/accesstoken_kit.cpp index a4c7bafd8..2ae4d281b 100644 --- a/interfaces/innerkits/accesstoken/src/accesstoken_kit.cpp +++ b/interfaces/innerkits/accesstoken/src/accesstoken_kit.cpp @@ -332,7 +332,8 @@ int AccessTokenKit::SetRemoteHapTokenInfo(const std::string& deviceID, int AccessTokenKit::SetRemoteNativeTokenInfo(const std::string& deviceID, std::vector& nativeTokenInfoList) { - ACCESSTOKEN_LOG_INFO(LABEL, "%{public}s called, deviceID=%{public}s", __func__, ConstantCommon::EncryptDevId(deviceID).c_str()); + ACCESSTOKEN_LOG_INFO(LABEL, "%{public}s called, deviceID=%{public}s", __func__, + ConstantCommon::EncryptDevId(deviceID).c_str()); return AccessTokenManagerClient::GetInstance() .SetRemoteNativeTokenInfo(deviceID, nativeTokenInfoList); } @@ -346,7 +347,8 @@ int AccessTokenKit::DeleteRemoteToken(const std::string& deviceID, AccessTokenID int AccessTokenKit::DeleteRemoteDeviceTokens(const std::string& deviceID) { - ACCESSTOKEN_LOG_INFO(LABEL, "%{public}s called, deviceID=%{public}s", __func__, ConstantCommon::EncryptDevId(deviceID).c_str()); + ACCESSTOKEN_LOG_INFO(LABEL, "%{public}s called, deviceID=%{public}s", __func__, + ConstantCommon::EncryptDevId(deviceID).c_str()); return AccessTokenManagerClient::GetInstance().DeleteRemoteDeviceTokens(deviceID); } diff --git a/services/accesstokenmanager/main/cpp/src/token/accesstoken_info_manager.cpp b/services/accesstokenmanager/main/cpp/src/token/accesstoken_info_manager.cpp index aae03e9eb..4951ff39e 100644 --- a/services/accesstokenmanager/main/cpp/src/token/accesstoken_info_manager.cpp +++ b/services/accesstokenmanager/main/cpp/src/token/accesstoken_info_manager.cpp @@ -665,8 +665,8 @@ int AccessTokenInfoManager::SetRemoteHapTokenInfo(const std::string& deviceID, H mapID = AccessTokenRemoteTokenManager::GetInstance().MapRemoteDeviceTokenToLocal(deviceID, remoteID); if (mapID == 0) { - ACCESSTOKEN_LOG_ERROR( - LABEL, "device %{public}s token %{public}u map failed.", ConstantCommon::EncryptDevId(deviceID).c_str(), remoteID); + ACCESSTOKEN_LOG_ERROR(LABEL, "device %{public}s token %{public}u map failed.", + ConstantCommon::EncryptDevId(deviceID).c_str(), remoteID); return RET_FAILED; } @@ -804,7 +804,8 @@ int AccessTokenInfoManager::DeleteRemoteDeviceTokens(const std::string& deviceID std::vector remoteTokens; int ret = AccessTokenRemoteTokenManager::GetInstance().GetDeviceAllRemoteTokenID(deviceID, remoteTokens); if (ret == RET_FAILED) { - ACCESSTOKEN_LOG_ERROR(LABEL, "device %{public}s have no remote token", ConstantCommon::EncryptDevId(deviceID).c_str()); + ACCESSTOKEN_LOG_ERROR(LABEL, "device %{public}s have no remote token", + ConstantCommon::EncryptDevId(deviceID).c_str()); return RET_FAILED; } for (AccessTokenID remoteID : remoteTokens) { @@ -831,7 +832,8 @@ AccessTokenID AccessTokenInfoManager::AllocLocalTokenID(const std::string& remot AccessTokenID remoteTokenID) { if (!DataValidator::IsDeviceIdValid(remoteDeviceID)) { - ACCESSTOKEN_LOG_ERROR(LABEL, "device %{public}s parms invalid", ConstantCommon::EncryptDevId(remoteDeviceID).c_str()); + ACCESSTOKEN_LOG_ERROR(LABEL, "device %{public}s parms invalid", + ConstantCommon::EncryptDevId(remoteDeviceID).c_str()); return 0; } std::string remoteUdid = GetUdidByNodeId(remoteDeviceID); diff --git a/services/accesstokenmanager/main/cpp/src/token/accesstoken_remote_token_manager.cpp b/services/accesstokenmanager/main/cpp/src/token/accesstoken_remote_token_manager.cpp index e2b6a3641..14d4bcf9d 100644 --- a/services/accesstokenmanager/main/cpp/src/token/accesstoken_remote_token_manager.cpp +++ b/services/accesstokenmanager/main/cpp/src/token/accesstoken_remote_token_manager.cpp @@ -43,8 +43,8 @@ AccessTokenID AccessTokenRemoteTokenManager::MapRemoteDeviceTokenToLocal(const s AccessTokenID remoteID) { if (!DataValidator::IsDeviceIdValid(deviceID) || !DataValidator::IsTokenIDValid(remoteID)) { - ACCESSTOKEN_LOG_ERROR( - LABEL, "device %{public}s or token %{public}x is invalid.", ConstantCommon::EncryptDevId(deviceID).c_str(), remoteID); + ACCESSTOKEN_LOG_ERROR(LABEL, "device %{public}s or token %{public}x is invalid.", + ConstantCommon::EncryptDevId(deviceID).c_str(), remoteID); return 0; } ATokenTypeEnum tokeType = AccessTokenIDManager::GetInstance().GetTokenIdTypeEnum(remoteID); @@ -95,7 +95,8 @@ int AccessTokenRemoteTokenManager::GetDeviceAllRemoteTokenID(const std::string& } Utils::UniqueReadGuard infoGuard(this->remoteDeviceLock_); if (remoteDeviceMap_.count(deviceID) < 1) { - ACCESSTOKEN_LOG_ERROR(LABEL, "device %{public}s has not mapping.", ConstantCommon::EncryptDevId(deviceID).c_str()); + ACCESSTOKEN_LOG_ERROR(LABEL, "device %{public}s has not mapping.", + ConstantCommon::EncryptDevId(deviceID).c_str()); return RET_FAILED; } @@ -109,15 +110,16 @@ AccessTokenID AccessTokenRemoteTokenManager::GetDeviceMappingTokenID(const std:: AccessTokenID remoteID) { if (!DataValidator::IsDeviceIdValid(deviceID) || !DataValidator::IsTokenIDValid(remoteID)) { - ACCESSTOKEN_LOG_ERROR( - LABEL, "device %{public}s or token %{public}x is invalid.", ConstantCommon::EncryptDevId(deviceID).c_str(), remoteID); + ACCESSTOKEN_LOG_ERROR(LABEL, "device %{public}s or token %{public}x is invalid.", + ConstantCommon::EncryptDevId(deviceID).c_str(), remoteID); return 0; } Utils::UniqueReadGuard infoGuard(this->remoteDeviceLock_); if (remoteDeviceMap_.count(deviceID) < 1 || remoteDeviceMap_[deviceID].MappingTokenIDPairMap_.count(remoteID) < 1) { - ACCESSTOKEN_LOG_ERROR(LABEL, "device %{public}s has not mapping.", ConstantCommon::EncryptDevId(deviceID).c_str()); + ACCESSTOKEN_LOG_ERROR(LABEL, "device %{public}s has not mapping.", + ConstantCommon::EncryptDevId(deviceID).c_str()); return 0; } @@ -128,15 +130,16 @@ int AccessTokenRemoteTokenManager::RemoveDeviceMappingTokenID(const std::string& AccessTokenID remoteID) { if (!DataValidator::IsDeviceIdValid(deviceID) || !DataValidator::IsTokenIDValid(remoteID)) { - ACCESSTOKEN_LOG_ERROR( - LABEL, "device %{public}s or token %{public}x is invalid.", ConstantCommon::EncryptDevId(deviceID).c_str(), remoteID); + ACCESSTOKEN_LOG_ERROR(LABEL, "device %{public}s or token %{public}x is invalid.", + ConstantCommon::EncryptDevId(deviceID).c_str(), remoteID); return RET_FAILED; } Utils::UniqueWriteGuard infoGuard(this->remoteDeviceLock_); if (remoteDeviceMap_.count(deviceID) < 1 || remoteDeviceMap_[deviceID].MappingTokenIDPairMap_.count(remoteID) < 1) { - ACCESSTOKEN_LOG_ERROR(LABEL, "device %{public}s has not mapping.", ConstantCommon::EncryptDevId(deviceID).c_str()); + ACCESSTOKEN_LOG_ERROR(LABEL, "device %{public}s has not mapping.", + ConstantCommon::EncryptDevId(deviceID).c_str()); return RET_FAILED; } diff --git a/services/privacymanager/src/service/privacy_manager_service.cpp b/services/privacymanager/src/service/privacy_manager_service.cpp index bce86f0d2..2b02b1657 100644 --- a/services/privacymanager/src/service/privacy_manager_service.cpp +++ b/services/privacymanager/src/service/privacy_manager_service.cpp @@ -95,7 +95,8 @@ int32_t PrivacyManagerService::StopUsingPermission(AccessTokenID tokenID, const int32_t PrivacyManagerService::RemovePermissionUsedRecords(AccessTokenID tokenID, const std::string& deviceID) { - ACCESSTOKEN_LOG_DEBUG(LABEL, "Entry, tokenID: 0x%{public}x, deviceID: %{public}s", tokenID, ConstantCommon::EncryptDevId(deviceID).c_str()); + ACCESSTOKEN_LOG_DEBUG(LABEL, "Entry, tokenID: 0x%{public}x, deviceID: %{public}s", + tokenID, ConstantCommon::EncryptDevId(deviceID).c_str()); PermissionRecordManager::GetInstance().RemovePermissionUsedRecords(tokenID, deviceID); return Constant::SUCCESS; } diff --git a/services/tokensyncmanager/src/remote/remote_command_manager.cpp b/services/tokensyncmanager/src/remote/remote_command_manager.cpp index 4af330090..91041e0e1 100644 --- a/services/tokensyncmanager/src/remote/remote_command_manager.cpp +++ b/services/tokensyncmanager/src/remote/remote_command_manager.cpp @@ -14,6 +14,7 @@ */ #include "remote_command_manager.h" +#include #include "device_info_manager.h" #include "sync_remote_native_token_command.h" #include "remote_command_factory.h" @@ -22,7 +23,6 @@ #include "accesstoken_kit.h" #include "constant_common.h" #include "constant.h" -#include namespace OHOS { @@ -81,11 +81,13 @@ void RemoteCommandManager::RemoveCommand(const std::string &udid) int RemoteCommandManager::ExecuteCommand(const std::string &udid, const std::shared_ptr &command) { if (udid.empty() || command == nullptr) { - ACCESSTOKEN_LOG_WARN(LABEL, "invalid udid: %{public}s, or null command", ConstantCommon::EncryptDevId(udid).c_str()); + ACCESSTOKEN_LOG_WARN(LABEL, "invalid udid: %{public}s, or null command", + ConstantCommon::EncryptDevId(udid).c_str()); return Constant::FAILURE; } std::string uniqueId = command->remoteProtocol_.uniqueId; - ACCESSTOKEN_LOG_INFO(LABEL, "start with udid: %{public}s , uniqueId: %{public}s ", ConstantCommon::EncryptDevId(udid).c_str(), uniqueId.c_str()); + ACCESSTOKEN_LOG_INFO(LABEL, "start with udid: %{public}s , uniqueId: %{public}s ", + ConstantCommon::EncryptDevId(udid).c_str(), uniqueId.c_str()); std::shared_ptr executor = GetOrCreateRemoteCommandExecutor(udid); if (executor == nullptr) { @@ -113,7 +115,8 @@ int RemoteCommandManager::ProcessDeviceCommandImmediately(const std::string &udi auto executor = executorIt->second; if (executor == nullptr) { - ACCESSTOKEN_LOG_INFO(LABEL, "RemoteCommandExecutor is null for udid %{public}s ", ConstantCommon::EncryptDevId(udid).c_str()); + ACCESSTOKEN_LOG_INFO(LABEL, "RemoteCommandExecutor is null for udid %{public}s ", + ConstantCommon::EncryptDevId(udid).c_str()); return Constant::FAILURE; } diff --git a/services/tokensyncmanager/src/remote/soft_bus_manager.cpp b/services/tokensyncmanager/src/remote/soft_bus_manager.cpp index 739d12bc7..c54516361 100644 --- a/services/tokensyncmanager/src/remote/soft_bus_manager.cpp +++ b/services/tokensyncmanager/src/remote/soft_bus_manager.cpp @@ -220,7 +220,8 @@ int32_t SoftBusManager::OpenSession(const std::string &deviceId) DeviceInfo info; bool result = DeviceInfoManager::GetInstance().GetDeviceInfo(deviceId, DeviceIdType::UNKNOWN, info); if (!result) { - ACCESSTOKEN_LOG_WARN(LABEL, "device info notfound for deviceId %{public}s", ConstantCommon::EncryptDevId(deviceId).c_str()); + ACCESSTOKEN_LOG_WARN(LABEL, "device info notfound for deviceId %{public}s", + ConstantCommon::EncryptDevId(deviceId).c_str()); return Constant::FAILURE; } std::string networkId = info.deviceId.networkId; @@ -312,7 +313,8 @@ std::string SoftBusManager::GetUniqueDeviceIdByNodeId(const std::string &nodeId) } std::string udid = GetUdidByNodeId(nodeId); if (udid.empty()) { - ACCESSTOKEN_LOG_ERROR(LABEL, "softbus return null or empty string: %{public}s", ConstantCommon::EncryptDevId(udid).c_str()); + ACCESSTOKEN_LOG_ERROR(LABEL, "softbus return null or empty string: %{public}s", + ConstantCommon::EncryptDevId(udid).c_str()); return ""; } char localUdid[ConstantCommon::DEVICE_UUID_LENGTH] = {0}; diff --git a/services/tokensyncmanager/test/mock/src/constant_mock.cpp b/services/tokensyncmanager/test/mock/src/constant_mock.cpp index 4a96e31f4..fef282afc 100644 --- a/services/tokensyncmanager/test/mock/src/constant_mock.cpp +++ b/services/tokensyncmanager/test/mock/src/constant_mock.cpp @@ -25,12 +25,12 @@ const std::string Constant::COMMAND_RESULT_SUCCESS = "success"; const std::string Constant::COMMAND_RESULT_FAILED = "execute command failed"; std::string ConstantCommon::EncryptDevId(std::string deviceId) { - std::string result=deviceId; - if (deviceId.size() > 2*ENCRYPTLEN) { - result.replace(ENCRYPTBEGIN + ENCRYPTLEN, deviceId.size() - 2 * ENCRYPTLEN, REPLACE_TARGET); + std::string result = deviceId; + if (deviceId.size() > MINDEVICEIDLEN) { + result.replace(ENCRYPTBEGIN + ENCRYPTLEN, deviceId.size() - MINDEVICEIDLEN, REPLACE_TARGET); } else { result.replace(ENCRYPTBEGIN, deviceId.size(), REPLACE_TARGET); - } + } return result; } diff --git a/services/tokensyncmanager/test/unittest/token_sync_service/token_sync_service_test.cpp b/services/tokensyncmanager/test/unittest/token_sync_service/token_sync_service_test.cpp index 4b92519a9..722c97a0b 100644 --- a/services/tokensyncmanager/test/unittest/token_sync_service/token_sync_service_test.cpp +++ b/services/tokensyncmanager/test/unittest/token_sync_service/token_sync_service_test.cpp @@ -22,11 +22,11 @@ #include #include +#include "gtest/gtest.h" #include "accesstoken_kit.h" #include "accesstoken_log.h" #include "base_remote_command.h" #include "constant_common.h" -#include "gtest/gtest.h" #include "session.h" #include "soft_bus_device_connection_listener.h" #include "soft_bus_session_listener.h" -- Gitee From 8d14f469d98b2c24fb09ca48dff6ee817ba9066a Mon Sep 17 00:00:00 2001 From: hhhhs9527 Date: Thu, 30 Jun 2022 15:57:36 +0800 Subject: [PATCH 11/15] 2022/06/30 Signed-off-by:hanshu5 Signed-off-by: hhhhs9527 --- .../innerkits/accesstoken/src/accesstoken_kit.cpp | 4 ++-- .../main/cpp/src/token/accesstoken_info_manager.cpp | 6 +++--- .../src/token/accesstoken_remote_token_manager.cpp | 12 ++++++------ .../src/service/privacy_manager_service.cpp | 2 +- .../src/remote/remote_command_manager.cpp | 6 +++--- .../tokensyncmanager/src/remote/soft_bus_manager.cpp | 4 ++-- 6 files changed, 17 insertions(+), 17 deletions(-) diff --git a/interfaces/innerkits/accesstoken/src/accesstoken_kit.cpp b/interfaces/innerkits/accesstoken/src/accesstoken_kit.cpp index 2ae4d281b..7359e5d9f 100644 --- a/interfaces/innerkits/accesstoken/src/accesstoken_kit.cpp +++ b/interfaces/innerkits/accesstoken/src/accesstoken_kit.cpp @@ -332,7 +332,7 @@ int AccessTokenKit::SetRemoteHapTokenInfo(const std::string& deviceID, int AccessTokenKit::SetRemoteNativeTokenInfo(const std::string& deviceID, std::vector& nativeTokenInfoList) { - ACCESSTOKEN_LOG_INFO(LABEL, "%{public}s called, deviceID=%{public}s", __func__, + ACCESSTOKEN_LOG_INFO(LABEL, "%{public}s called, deviceID=%{public}s", __func__, ConstantCommon::EncryptDevId(deviceID).c_str()); return AccessTokenManagerClient::GetInstance() .SetRemoteNativeTokenInfo(deviceID, nativeTokenInfoList); @@ -347,7 +347,7 @@ int AccessTokenKit::DeleteRemoteToken(const std::string& deviceID, AccessTokenID int AccessTokenKit::DeleteRemoteDeviceTokens(const std::string& deviceID) { - ACCESSTOKEN_LOG_INFO(LABEL, "%{public}s called, deviceID=%{public}s", __func__, + ACCESSTOKEN_LOG_INFO(LABEL, "%{public}s called, deviceID=%{public}s", __func__, ConstantCommon::EncryptDevId(deviceID).c_str()); return AccessTokenManagerClient::GetInstance().DeleteRemoteDeviceTokens(deviceID); } diff --git a/services/accesstokenmanager/main/cpp/src/token/accesstoken_info_manager.cpp b/services/accesstokenmanager/main/cpp/src/token/accesstoken_info_manager.cpp index 4951ff39e..3b1e05894 100644 --- a/services/accesstokenmanager/main/cpp/src/token/accesstoken_info_manager.cpp +++ b/services/accesstokenmanager/main/cpp/src/token/accesstoken_info_manager.cpp @@ -665,7 +665,7 @@ int AccessTokenInfoManager::SetRemoteHapTokenInfo(const std::string& deviceID, H mapID = AccessTokenRemoteTokenManager::GetInstance().MapRemoteDeviceTokenToLocal(deviceID, remoteID); if (mapID == 0) { - ACCESSTOKEN_LOG_ERROR(LABEL, "device %{public}s token %{public}u map failed.", + ACCESSTOKEN_LOG_ERROR(LABEL, "device %{public}s token %{public}u map failed.", ConstantCommon::EncryptDevId(deviceID).c_str(), remoteID); return RET_FAILED; } @@ -804,7 +804,7 @@ int AccessTokenInfoManager::DeleteRemoteDeviceTokens(const std::string& deviceID std::vector remoteTokens; int ret = AccessTokenRemoteTokenManager::GetInstance().GetDeviceAllRemoteTokenID(deviceID, remoteTokens); if (ret == RET_FAILED) { - ACCESSTOKEN_LOG_ERROR(LABEL, "device %{public}s have no remote token", + ACCESSTOKEN_LOG_ERROR(LABEL, "device %{public}s have no remote token", ConstantCommon::EncryptDevId(deviceID).c_str()); return RET_FAILED; } @@ -832,7 +832,7 @@ AccessTokenID AccessTokenInfoManager::AllocLocalTokenID(const std::string& remot AccessTokenID remoteTokenID) { if (!DataValidator::IsDeviceIdValid(remoteDeviceID)) { - ACCESSTOKEN_LOG_ERROR(LABEL, "device %{public}s parms invalid", + ACCESSTOKEN_LOG_ERROR(LABEL, "device %{public}s parms invalid", ConstantCommon::EncryptDevId(remoteDeviceID).c_str()); return 0; } diff --git a/services/accesstokenmanager/main/cpp/src/token/accesstoken_remote_token_manager.cpp b/services/accesstokenmanager/main/cpp/src/token/accesstoken_remote_token_manager.cpp index 14d4bcf9d..e96bdc0f7 100644 --- a/services/accesstokenmanager/main/cpp/src/token/accesstoken_remote_token_manager.cpp +++ b/services/accesstokenmanager/main/cpp/src/token/accesstoken_remote_token_manager.cpp @@ -43,7 +43,7 @@ AccessTokenID AccessTokenRemoteTokenManager::MapRemoteDeviceTokenToLocal(const s AccessTokenID remoteID) { if (!DataValidator::IsDeviceIdValid(deviceID) || !DataValidator::IsTokenIDValid(remoteID)) { - ACCESSTOKEN_LOG_ERROR(LABEL, "device %{public}s or token %{public}x is invalid.", + ACCESSTOKEN_LOG_ERROR(LABEL, "device %{public}s or token %{public}x is invalid.", ConstantCommon::EncryptDevId(deviceID).c_str(), remoteID); return 0; } @@ -95,7 +95,7 @@ int AccessTokenRemoteTokenManager::GetDeviceAllRemoteTokenID(const std::string& } Utils::UniqueReadGuard infoGuard(this->remoteDeviceLock_); if (remoteDeviceMap_.count(deviceID) < 1) { - ACCESSTOKEN_LOG_ERROR(LABEL, "device %{public}s has not mapping.", + ACCESSTOKEN_LOG_ERROR(LABEL, "device %{public}s has not mapping.", ConstantCommon::EncryptDevId(deviceID).c_str()); return RET_FAILED; } @@ -110,7 +110,7 @@ AccessTokenID AccessTokenRemoteTokenManager::GetDeviceMappingTokenID(const std:: AccessTokenID remoteID) { if (!DataValidator::IsDeviceIdValid(deviceID) || !DataValidator::IsTokenIDValid(remoteID)) { - ACCESSTOKEN_LOG_ERROR(LABEL, "device %{public}s or token %{public}x is invalid.", + ACCESSTOKEN_LOG_ERROR(LABEL, "device %{public}s or token %{public}x is invalid.", ConstantCommon::EncryptDevId(deviceID).c_str(), remoteID); return 0; } @@ -118,7 +118,7 @@ AccessTokenID AccessTokenRemoteTokenManager::GetDeviceMappingTokenID(const std:: Utils::UniqueReadGuard infoGuard(this->remoteDeviceLock_); if (remoteDeviceMap_.count(deviceID) < 1 || remoteDeviceMap_[deviceID].MappingTokenIDPairMap_.count(remoteID) < 1) { - ACCESSTOKEN_LOG_ERROR(LABEL, "device %{public}s has not mapping.", + ACCESSTOKEN_LOG_ERROR(LABEL, "device %{public}s has not mapping.", ConstantCommon::EncryptDevId(deviceID).c_str()); return 0; } @@ -130,7 +130,7 @@ int AccessTokenRemoteTokenManager::RemoveDeviceMappingTokenID(const std::string& AccessTokenID remoteID) { if (!DataValidator::IsDeviceIdValid(deviceID) || !DataValidator::IsTokenIDValid(remoteID)) { - ACCESSTOKEN_LOG_ERROR(LABEL, "device %{public}s or token %{public}x is invalid.", + ACCESSTOKEN_LOG_ERROR(LABEL, "device %{public}s or token %{public}x is invalid.", ConstantCommon::EncryptDevId(deviceID).c_str(), remoteID); return RET_FAILED; } @@ -138,7 +138,7 @@ int AccessTokenRemoteTokenManager::RemoveDeviceMappingTokenID(const std::string& Utils::UniqueWriteGuard infoGuard(this->remoteDeviceLock_); if (remoteDeviceMap_.count(deviceID) < 1 || remoteDeviceMap_[deviceID].MappingTokenIDPairMap_.count(remoteID) < 1) { - ACCESSTOKEN_LOG_ERROR(LABEL, "device %{public}s has not mapping.", + ACCESSTOKEN_LOG_ERROR(LABEL, "device %{public}s has not mapping.", ConstantCommon::EncryptDevId(deviceID).c_str()); return RET_FAILED; } diff --git a/services/privacymanager/src/service/privacy_manager_service.cpp b/services/privacymanager/src/service/privacy_manager_service.cpp index 2b02b1657..b36dc3acb 100644 --- a/services/privacymanager/src/service/privacy_manager_service.cpp +++ b/services/privacymanager/src/service/privacy_manager_service.cpp @@ -95,7 +95,7 @@ int32_t PrivacyManagerService::StopUsingPermission(AccessTokenID tokenID, const int32_t PrivacyManagerService::RemovePermissionUsedRecords(AccessTokenID tokenID, const std::string& deviceID) { - ACCESSTOKEN_LOG_DEBUG(LABEL, "Entry, tokenID: 0x%{public}x, deviceID: %{public}s", + ACCESSTOKEN_LOG_DEBUG(LABEL, "Entry, tokenID: 0x%{public}x, deviceID: %{public}s", tokenID, ConstantCommon::EncryptDevId(deviceID).c_str()); PermissionRecordManager::GetInstance().RemovePermissionUsedRecords(tokenID, deviceID); return Constant::SUCCESS; diff --git a/services/tokensyncmanager/src/remote/remote_command_manager.cpp b/services/tokensyncmanager/src/remote/remote_command_manager.cpp index 91041e0e1..ba05954a5 100644 --- a/services/tokensyncmanager/src/remote/remote_command_manager.cpp +++ b/services/tokensyncmanager/src/remote/remote_command_manager.cpp @@ -81,12 +81,12 @@ void RemoteCommandManager::RemoveCommand(const std::string &udid) int RemoteCommandManager::ExecuteCommand(const std::string &udid, const std::shared_ptr &command) { if (udid.empty() || command == nullptr) { - ACCESSTOKEN_LOG_WARN(LABEL, "invalid udid: %{public}s, or null command", + ACCESSTOKEN_LOG_WARN(LABEL, "invalid udid: %{public}s, or null command", ConstantCommon::EncryptDevId(udid).c_str()); return Constant::FAILURE; } std::string uniqueId = command->remoteProtocol_.uniqueId; - ACCESSTOKEN_LOG_INFO(LABEL, "start with udid: %{public}s , uniqueId: %{public}s ", + ACCESSTOKEN_LOG_INFO(LABEL, "start with udid: %{public}s , uniqueId: %{public}s ", ConstantCommon::EncryptDevId(udid).c_str(), uniqueId.c_str()); std::shared_ptr executor = GetOrCreateRemoteCommandExecutor(udid); @@ -115,7 +115,7 @@ int RemoteCommandManager::ProcessDeviceCommandImmediately(const std::string &udi auto executor = executorIt->second; if (executor == nullptr) { - ACCESSTOKEN_LOG_INFO(LABEL, "RemoteCommandExecutor is null for udid %{public}s ", + ACCESSTOKEN_LOG_INFO(LABEL, "RemoteCommandExecutor is null for udid %{public}s ", ConstantCommon::EncryptDevId(udid).c_str()); return Constant::FAILURE; } diff --git a/services/tokensyncmanager/src/remote/soft_bus_manager.cpp b/services/tokensyncmanager/src/remote/soft_bus_manager.cpp index c54516361..7cda4f32b 100644 --- a/services/tokensyncmanager/src/remote/soft_bus_manager.cpp +++ b/services/tokensyncmanager/src/remote/soft_bus_manager.cpp @@ -220,7 +220,7 @@ int32_t SoftBusManager::OpenSession(const std::string &deviceId) DeviceInfo info; bool result = DeviceInfoManager::GetInstance().GetDeviceInfo(deviceId, DeviceIdType::UNKNOWN, info); if (!result) { - ACCESSTOKEN_LOG_WARN(LABEL, "device info notfound for deviceId %{public}s", + ACCESSTOKEN_LOG_WARN(LABEL, "device info notfound for deviceId %{public}s", ConstantCommon::EncryptDevId(deviceId).c_str()); return Constant::FAILURE; } @@ -313,7 +313,7 @@ std::string SoftBusManager::GetUniqueDeviceIdByNodeId(const std::string &nodeId) } std::string udid = GetUdidByNodeId(nodeId); if (udid.empty()) { - ACCESSTOKEN_LOG_ERROR(LABEL, "softbus return null or empty string: %{public}s", + ACCESSTOKEN_LOG_ERROR(LABEL, "softbus return null or empty string: %{public}s", ConstantCommon::EncryptDevId(udid).c_str()); return ""; } -- Gitee From 9357f30bf64e7fd9051b65d342a1e7af428d9083 Mon Sep 17 00:00:00 2001 From: hhhhs9527 Date: Thu, 30 Jun 2022 17:22:22 +0800 Subject: [PATCH 12/15] 2022/06/30 Signed-off-by:hanshu5 Signed-off-by: hhhhs9527 --- .../privacymanager/include/common/constant.h | 2 +- .../src/device/device_info_manager.cpp | 21 ++++++++++--------- .../src/remote/remote_command_executor.cpp | 3 +-- .../src/remote/soft_bus_manager.cpp | 3 +-- 4 files changed, 14 insertions(+), 15 deletions(-) diff --git a/services/privacymanager/include/common/constant.h b/services/privacymanager/include/common/constant.h index 6d3b77f7c..ca7f375f0 100644 --- a/services/privacymanager/include/common/constant.h +++ b/services/privacymanager/include/common/constant.h @@ -62,7 +62,7 @@ public: const static int32_t RECORD_DELETE_TIME = 30 * 86400; const static int32_t PRECISE = 60; const static int32_t LATEST_RECORD_TIME = 7 * 86400; - + const static std::map PERMISSION_OPCODE_MAP; public: static bool TransferPermissionToOpcode(const std::string& permissionName, int32_t& opCode); diff --git a/services/tokensyncmanager/src/device/device_info_manager.cpp b/services/tokensyncmanager/src/device/device_info_manager.cpp index 57d78c2cb..9c6723618 100644 --- a/services/tokensyncmanager/src/device/device_info_manager.cpp +++ b/services/tokensyncmanager/src/device/device_info_manager.cpp @@ -15,6 +15,7 @@ #include "device_info_manager.h" #include "constant_common.h" + namespace OHOS { namespace Security { namespace AccessToken { @@ -54,11 +55,11 @@ void DeviceInfoManager::AddDeviceInfo(const std::string &networkId, const std::s void DeviceInfoManager::RemoveAllRemoteDeviceInfo() { - char deviceIdCharArray[ConstantCommon::DEVICE_UUID_LENGTH] = {0}; - GetDevUdid(deviceIdCharArray, ConstantCommon::DEVICE_UUID_LENGTH); + std::string localDevice = ConstantCommon::GetLocalDeviceId(); + DeviceInfo localDeviceInfoOpt; if (DeviceInfoRepository::GetInstance().FindDeviceInfo( - deviceIdCharArray, DeviceIdType::UNIQUE_DISABILITY_ID, localDeviceInfoOpt)) { + localDevice, DeviceIdType::UNIQUE_DISABILITY_ID, localDeviceInfoOpt)) { DeviceInfoRepository::GetInstance().DeleteAllDeviceInfoExceptOne(localDeviceInfoOpt); } } @@ -69,10 +70,9 @@ void DeviceInfoManager::RemoveRemoteDeviceInfo(const std::string &nodeId, Device ACCESSTOKEN_LOG_ERROR(LABEL, "removeDeviceInfoByNetworkId: nodeId is invalid"); } else { DeviceInfo deviceInfo; - char deviceIdCharArray[ConstantCommon::DEVICE_UUID_LENGTH] = {0}; - GetDevUdid(deviceIdCharArray, ConstantCommon::DEVICE_UUID_LENGTH); + std::string localDevice = ConstantCommon::GetLocalDeviceId(); if (DeviceInfoRepository::GetInstance().FindDeviceInfo(nodeId, deviceIdType, deviceInfo)) { - if (deviceInfo.deviceId.uniqueDeviceId != deviceIdCharArray) { + if (deviceInfo.deviceId.uniqueDeviceId != localDevice) { DeviceInfoRepository::GetInstance().DeleteDeviceInfo(nodeId, deviceIdType); } } @@ -119,17 +119,18 @@ std::string DeviceInfoManager::ConvertToUniqueDeviceIdOrFetch(const std::string } else { ACCESSTOKEN_LOG_DEBUG(LABEL, "FindDeviceInfo succeed, udid and local udid is empty, nodeId(%{public}s)", - nodeId.c_str()); + ConstantCommon::EncryptDevId(nodeId).c_str()); } } else { ACCESSTOKEN_LOG_DEBUG(LABEL, "FindDeviceInfo succeed, udid is empty, nodeId(%{public}s) ", - nodeId.c_str()); + ConstantCommon::EncryptDevId(nodeId).c_str()); result = uniqueDeviceId; } } else { ACCESSTOKEN_LOG_DEBUG( - LABEL, "FindDeviceInfo failed, nodeId(%{public}s)", nodeId.c_str()); + LABEL, "FindDeviceInfo failed, nodeId(%{public}s)", + ConstantCommon::EncryptDevId(nodeId).c_str()); auto list = DeviceInfoRepository::GetInstance().ListDeviceInfo(); auto iter = list.begin(); for (; iter != list.end(); iter++) { @@ -140,7 +141,7 @@ std::string DeviceInfoManager::ConvertToUniqueDeviceIdOrFetch(const std::string LABEL, ">>> DeviceInfoRepository device type: %{public}s", info.deviceType.c_str()); ACCESSTOKEN_LOG_DEBUG(LABEL, ">>> DeviceInfoRepository device network id: %{public}s", - info.deviceId.networkId.c_str()); + ConstantCommon::EncryptDevId(info.deviceId.networkId).c_str()); } } return result; diff --git a/services/tokensyncmanager/src/remote/remote_command_executor.cpp b/services/tokensyncmanager/src/remote/remote_command_executor.cpp index 7d66e6d36..659a8acf3 100644 --- a/services/tokensyncmanager/src/remote/remote_command_executor.cpp +++ b/services/tokensyncmanager/src/remote/remote_command_executor.cpp @@ -81,8 +81,7 @@ int RemoteCommandExecutor::ProcessOneCommand(const std::shared_ptr fulfillDeviceInfo = std::bind(&SoftBusManager::FulfillLocalDeviceInfo, this); -- Gitee From 692df9934adc7f3ed7f63a2de0a37f04097ec29c Mon Sep 17 00:00:00 2001 From: hhhhs9527 Date: Thu, 30 Jun 2022 21:00:51 +0800 Subject: [PATCH 13/15] 2022/06/30 Signed-off-by:hanshu5 Signed-off-by: hhhhs9527 --- .../nativetoken/src/nativetoken_json_oper.c | 4 ++-- .../src/service/token_sync_manager_service.cpp | 12 ++++++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/interfaces/innerkits/nativetoken/src/nativetoken_json_oper.c b/interfaces/innerkits/nativetoken/src/nativetoken_json_oper.c index 7921b36ce..278de5d6e 100644 --- a/interfaces/innerkits/nativetoken/src/nativetoken_json_oper.c +++ b/interfaces/innerkits/nativetoken/src/nativetoken_json_oper.c @@ -66,7 +66,7 @@ uint32_t GetAplFromJson(cJSON *cjsonItem, NativeTokenList *tokenNode) } int32_t apl = cJSON_GetNumberValue(aplJson); if (apl <= 0 || apl > SYSTEM_CORE) { - AT_LOG_ERROR("[ATLIB-%s]:apl = %d in file is invalid.", __func__, apl); + AT_LOG_ERROR("[ATLIB-%s]:apl = %{public}d in file is invalid.", __func__, apl); return ATRET_FAILED; } tokenNode->apl = aplJson->valueint; @@ -78,7 +78,7 @@ uint32_t GetInfoArrFromJson(cJSON *cjsonItem, char *strArr[], int32_t *strNum, S cJSON *strArrJson = cJSON_GetObjectItem(cjsonItem, attr->strKey); int32_t size = cJSON_GetArraySize(strArrJson); if (size > attr->maxStrNum) { - AT_LOG_ERROR("[ATLIB-%s]:size = %d is invalid.", __func__, size); + AT_LOG_ERROR("[ATLIB-%s]:size = %{public}d is invalid.", __func__, size); return ATRET_FAILED; } *strNum = size; diff --git a/services/tokensyncmanager/src/service/token_sync_manager_service.cpp b/services/tokensyncmanager/src/service/token_sync_manager_service.cpp index 6c4b7bbd2..517adc0d6 100644 --- a/services/tokensyncmanager/src/service/token_sync_manager_service.cpp +++ b/services/tokensyncmanager/src/service/token_sync_manager_service.cpp @@ -102,10 +102,10 @@ int TokenSyncManagerService::GetRemoteHapTokenInfo(const std::string& deviceID, const int32_t resultCode = RemoteCommandManager::GetInstance().ExecuteCommand(udid, syncRemoteHapTokenCommand); if (resultCode != Constant::SUCCESS) { ACCESSTOKEN_LOG_INFO(LABEL, - "RemoteExecutorManager executeCommand SyncRemoteHapTokenCommand failed, return %d", resultCode); + "RemoteExecutorManager executeCommand SyncRemoteHapTokenCommand failed, return %{public}d", resultCode); return resultCode; } - ACCESSTOKEN_LOG_INFO(LABEL, "get resultCode: %d", resultCode); + ACCESSTOKEN_LOG_INFO(LABEL, "get resultCode: %{public}d", resultCode); return RET_SUCCESS; } @@ -131,10 +131,10 @@ int TokenSyncManagerService::DeleteRemoteHapTokenInfo(AccessTokenID tokenID) device.deviceId.uniqueDeviceId, deleteRemoteTokenCommand); if (resultCode != Constant::SUCCESS) { ACCESSTOKEN_LOG_INFO(LABEL, - "RemoteExecutorManager executeCommand DeleteRemoteTokenCommand failed, return %d", resultCode); + "RemoteExecutorManager executeCommand DeleteRemoteTokenCommand failed, return %{public}d", resultCode); continue; } - ACCESSTOKEN_LOG_INFO(LABEL, "get resultCode: %d", resultCode); + ACCESSTOKEN_LOG_INFO(LABEL, "get resultCode: %{public}d", resultCode); } return RET_SUCCESS; } @@ -157,10 +157,10 @@ int TokenSyncManagerService::UpdateRemoteHapTokenInfo(const HapTokenInfoForSync& device.deviceId.uniqueDeviceId, updateRemoteHapTokenCommand); if (resultCode != Constant::SUCCESS) { ACCESSTOKEN_LOG_INFO(LABEL, - "RemoteExecutorManager executeCommand updateRemoteHapTokenCommand failed, return %d", resultCode); + "RemoteExecutorManager executeCommand updateRemoteHapTokenCommand failed, return %{public}d", resultCode); continue; } - ACCESSTOKEN_LOG_INFO(LABEL, "get resultCode: %d", resultCode); + ACCESSTOKEN_LOG_INFO(LABEL, "get resultCode: %{public}d", resultCode); } return RET_SUCCESS; -- Gitee From ae868a8eafb116adaaf93e336a59b055f655fa6a Mon Sep 17 00:00:00 2001 From: hhhhs9527 Date: Thu, 30 Jun 2022 21:13:02 +0800 Subject: [PATCH 14/15] 2022/06/30 Signed-off-by:hanshu5 Signed-off-by: hhhhs9527 --- .../src/service/token_sync_manager_service.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/services/tokensyncmanager/src/service/token_sync_manager_service.cpp b/services/tokensyncmanager/src/service/token_sync_manager_service.cpp index 517adc0d6..88d354599 100644 --- a/services/tokensyncmanager/src/service/token_sync_manager_service.cpp +++ b/services/tokensyncmanager/src/service/token_sync_manager_service.cpp @@ -157,7 +157,8 @@ int TokenSyncManagerService::UpdateRemoteHapTokenInfo(const HapTokenInfoForSync& device.deviceId.uniqueDeviceId, updateRemoteHapTokenCommand); if (resultCode != Constant::SUCCESS) { ACCESSTOKEN_LOG_INFO(LABEL, - "RemoteExecutorManager executeCommand updateRemoteHapTokenCommand failed, return %{public}d", resultCode); + "RemoteExecutorManager executeCommand updateRemoteHapTokenCommand failed, return %{public}d", + resultCode); continue; } ACCESSTOKEN_LOG_INFO(LABEL, "get resultCode: %{public}d", resultCode); -- Gitee From d4f3931dc60569b5bd5c24dfd7476928a5cfa09e Mon Sep 17 00:00:00 2001 From: hhhhs9527 Date: Thu, 30 Jun 2022 22:19:45 +0800 Subject: [PATCH 15/15] 2022/06/30 Signed-off-by:hanshu5 Signed-off-by: hhhhs9527 --- .../innerkits/nativetoken/src/nativetoken_json_oper.c | 4 ++-- .../main/cpp/src/token/native_token_info_inner.cpp | 6 +++--- .../main/cpp/src/token/native_token_receptor.cpp | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/interfaces/innerkits/nativetoken/src/nativetoken_json_oper.c b/interfaces/innerkits/nativetoken/src/nativetoken_json_oper.c index 278de5d6e..7921b36ce 100644 --- a/interfaces/innerkits/nativetoken/src/nativetoken_json_oper.c +++ b/interfaces/innerkits/nativetoken/src/nativetoken_json_oper.c @@ -66,7 +66,7 @@ uint32_t GetAplFromJson(cJSON *cjsonItem, NativeTokenList *tokenNode) } int32_t apl = cJSON_GetNumberValue(aplJson); if (apl <= 0 || apl > SYSTEM_CORE) { - AT_LOG_ERROR("[ATLIB-%s]:apl = %{public}d in file is invalid.", __func__, apl); + AT_LOG_ERROR("[ATLIB-%s]:apl = %d in file is invalid.", __func__, apl); return ATRET_FAILED; } tokenNode->apl = aplJson->valueint; @@ -78,7 +78,7 @@ uint32_t GetInfoArrFromJson(cJSON *cjsonItem, char *strArr[], int32_t *strNum, S cJSON *strArrJson = cJSON_GetObjectItem(cjsonItem, attr->strKey); int32_t size = cJSON_GetArraySize(strArrJson); if (size > attr->maxStrNum) { - AT_LOG_ERROR("[ATLIB-%s]:size = %{public}d is invalid.", __func__, size); + AT_LOG_ERROR("[ATLIB-%s]:size = %d is invalid.", __func__, size); return ATRET_FAILED; } *strNum = size; diff --git a/services/accesstokenmanager/main/cpp/src/token/native_token_info_inner.cpp b/services/accesstokenmanager/main/cpp/src/token/native_token_info_inner.cpp index 00c656f84..2ad119e8b 100644 --- a/services/accesstokenmanager/main/cpp/src/token/native_token_info_inner.cpp +++ b/services/accesstokenmanager/main/cpp/src/token/native_token_info_inner.cpp @@ -65,7 +65,7 @@ int NativeTokenInfoInner::Init(AccessTokenID id, const std::string& processName, tokenInfoBasic_.processName = processName; if (!DataValidator::IsAplNumValid(apl)) { ACCESSTOKEN_LOG_ERROR(LABEL, - "tokenID: %{public}u init failed, apl %{public}d is invalid", + "tokenID: %{public}u init failed, apl %d is invalid", tokenInfoBasic_.tokenID, apl); return RET_FAILED; } @@ -128,7 +128,7 @@ int NativeTokenInfoInner::RestoreNativeTokenInfo(AccessTokenID tokenId, const Ge int aplNum = inGenericValues.GetInt(FIELD_APL); if (!DataValidator::IsAplNumValid(aplNum)) { ACCESSTOKEN_LOG_ERROR(LABEL, - "tokenID: %{public}u apl is error, value %{public}d", + "tokenID: %{public}u apl is error, value %d", tokenInfoBasic_.tokenID, aplNum); return RET_FAILED; } @@ -136,7 +136,7 @@ int NativeTokenInfoInner::RestoreNativeTokenInfo(AccessTokenID tokenId, const Ge tokenInfoBasic_.ver = (char)inGenericValues.GetInt(FIELD_TOKEN_VERSION); if (tokenInfoBasic_.ver != DEFAULT_TOKEN_VERSION) { ACCESSTOKEN_LOG_ERROR(LABEL, - "tokenID: %{public}u version is error, version %{public}d", + "tokenID: %{public}u version is error, version %d", tokenInfoBasic_.tokenID, tokenInfoBasic_.ver); return RET_FAILED; } diff --git a/services/accesstokenmanager/main/cpp/src/token/native_token_receptor.cpp b/services/accesstokenmanager/main/cpp/src/token/native_token_receptor.cpp index 8c7d5f9b3..157b8b3cd 100644 --- a/services/accesstokenmanager/main/cpp/src/token/native_token_receptor.cpp +++ b/services/accesstokenmanager/main/cpp/src/token/native_token_receptor.cpp @@ -151,7 +151,7 @@ int NativeTokenReceptor::ReadCfgFile(std::string& nativeRawData) { int32_t fd = open(NATIVE_TOKEN_CONFIG_FILE.c_str(), O_RDONLY); if (fd < 0) { - ACCESSTOKEN_LOG_ERROR(LABEL, "open failed errno %{public}d.", errno); + ACCESSTOKEN_LOG_ERROR(LABEL, "open failed errno %d.", errno); return RET_FAILED; } struct stat statBuffer; -- Gitee