From 36f31b6d816f29300f868025dc5bd9bc567fae1f Mon Sep 17 00:00:00 2001 From: zwx1066281 Date: Mon, 21 Feb 2022 11:00:02 +0800 Subject: [PATCH] add atm dump feature Signed-off-by: zwx1066281 --- BUILD.gn | 1 + .../include/i_accesstoken_manager.h | 4 +- .../accesstoken/include/accesstoken_kit.h | 2 +- .../accesstoken/src/accesstoken_kit.cpp | 5 +- .../src/accesstoken_manager_client.cpp | 9 +-- .../src/accesstoken_manager_client.h | 2 +- .../src/accesstoken_manager_proxy.cpp | 18 ++--- .../src/accesstoken_manager_proxy.h | 2 +- interfaces/kits/accesstoken/BUILD.gn | 22 +----- .../accesstoken/napi/src/napi_atmanager.cpp | 2 +- .../service/accesstoken_manager_service.h | 2 +- .../service/accesstoken_manager_stub.h | 2 +- .../include/token/accesstoken_info_manager.h | 2 +- .../src/permission/permission_policy_set.cpp | 69 ++++++++-------- .../service/accesstoken_manager_service.cpp | 7 +- .../src/service/accesstoken_manager_stub.cpp | 12 ++- .../src/token/accesstoken_info_manager.cpp | 9 ++- .../cpp/src/token/hap_token_info_inner.cpp | 22 +++--- .../cpp/src/token/native_token_info_inner.cpp | 16 ++-- .../cpp/src/accesstoken_info_manager_test.cpp | 2 +- tools/accesstoken/BUILD.gn | 62 +++++++++++++++ tools/accesstoken/include/atm_command.h | 48 +++++++++++ tools/accesstoken/include/atm_receiver_impl.h | 43 ++++++++++ tools/accesstoken/src/atm_command.cpp | 79 +++++++++++++++++++ tools/accesstoken/src/atm_receiver_impl.cpp | 59 ++++++++++++++ tools/accesstoken/src/main.cpp | 24 ++++++ 26 files changed, 418 insertions(+), 107 deletions(-) create mode 100644 tools/accesstoken/BUILD.gn create mode 100644 tools/accesstoken/include/atm_command.h create mode 100644 tools/accesstoken/include/atm_receiver_impl.h create mode 100644 tools/accesstoken/src/atm_command.cpp create mode 100644 tools/accesstoken/src/atm_receiver_impl.cpp create mode 100644 tools/accesstoken/src/main.cpp diff --git a/BUILD.gn b/BUILD.gn index d57c193e9..debb31a87 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -22,6 +22,7 @@ group("accesstoken_build_module") { "//base/security/access_token/interfaces/kits:napi_packages", "//base/security/access_token/services/accesstokenmanager:accesstoken_manager_service", "//base/security/access_token/services/accesstokenmanager/main/sa_profile:accesstoken_sa_profile_standard", + "//base/security/access_token/tools/accesstoken:tools_atm", ] } } diff --git a/frameworks/accesstoken/include/i_accesstoken_manager.h b/frameworks/accesstoken/include/i_accesstoken_manager.h index 12b1d3165..8ef43afdc 100644 --- a/frameworks/accesstoken/include/i_accesstoken_manager.h +++ b/frameworks/accesstoken/include/i_accesstoken_manager.h @@ -68,7 +68,7 @@ public: virtual int DeleteRemoteToken(const std::string& deviceID, AccessTokenID tokenID) = 0; virtual int DeleteRemoteDeviceTokens(const std::string& deviceID) = 0; - virtual int DumpToken(std::string& dumpInfo) = 0; + virtual void DumpTokenInfo(std::string& tokenInfo) = 0; enum class InterfaceCode { VERIFY_ACCESSTOKEN = 0xff10, @@ -96,7 +96,7 @@ public: DELETE_REMOTE_TOKEN_INFO = 0xff2b, DELETE_REMOTE_DEVICE_TOKEN = 0xff2c, - DUMP = 0xff30, + DUMP_TOKENINFO = 0xff30, }; }; } // namespace AccessToken diff --git a/interfaces/innerkits/accesstoken/include/accesstoken_kit.h b/interfaces/innerkits/accesstoken/include/accesstoken_kit.h index 495f451d9..36fc79342 100644 --- a/interfaces/innerkits/accesstoken/include/accesstoken_kit.h +++ b/interfaces/innerkits/accesstoken/include/accesstoken_kit.h @@ -60,7 +60,7 @@ public: std::vector& nativeTokenInfoList); static int DeleteRemoteToken(const std::string& deviceID, AccessTokenID tokenID); static int DeleteRemoteDeviceTokens(const std::string& deviceID); - static int DumpToken(std::string& dumpInfo); + static void DumpTokenInfo(std::string& dumpInfo); }; } // namespace AccessToken } // namespace Security diff --git a/interfaces/innerkits/accesstoken/src/accesstoken_kit.cpp b/interfaces/innerkits/accesstoken/src/accesstoken_kit.cpp index dbdad0dab..5121e13dd 100644 --- a/interfaces/innerkits/accesstoken/src/accesstoken_kit.cpp +++ b/interfaces/innerkits/accesstoken/src/accesstoken_kit.cpp @@ -321,9 +321,10 @@ int AccessTokenKit::DeleteRemoteDeviceTokens(const std::string& deviceID) return AccessTokenManagerClient::GetInstance().DeleteRemoteDeviceTokens(deviceID); } -int AccessTokenKit::DumpToken(std::string& dumpInfo) +void AccessTokenKit::DumpTokenInfo(std::string& dumpInfo) { - return AccessTokenManagerClient::GetInstance().DumpToken(dumpInfo); + ACCESSTOKEN_LOG_INFO(LABEL, "%{public}s called", __func__); + AccessTokenManagerClient::GetInstance().DumpTokenInfo(dumpInfo); } } // namespace AccessToken } // namespace Security diff --git a/interfaces/innerkits/accesstoken/src/accesstoken_manager_client.cpp b/interfaces/innerkits/accesstoken/src/accesstoken_manager_client.cpp index fc9225c37..6b3c71432 100644 --- a/interfaces/innerkits/accesstoken/src/accesstoken_manager_client.cpp +++ b/interfaces/innerkits/accesstoken/src/accesstoken_manager_client.cpp @@ -360,16 +360,15 @@ int AccessTokenManagerClient::DeleteRemoteDeviceTokens(const std::string& device return res; } -int AccessTokenManagerClient::DumpToken(std::string& dumpInfo) +void AccessTokenManagerClient::DumpTokenInfo(std::string& dumpInfo) { ACCESSTOKEN_LOG_DEBUG(LABEL, "%{public}s: called!", __func__); auto proxy = GetProxy(); if (proxy == nullptr) { - ACCESSTOKEN_LOG_ERROR(LABEL, "proxy is null"); - return RET_FAILED; + ACCESSTOKEN_LOG_ERROR(LABEL, "%{public}s: proxy is null", __func__); + return; } - AccessTokenID res = proxy->DumpToken(dumpInfo); - return res; + proxy->DumpTokenInfo(dumpInfo); } sptr AccessTokenManagerClient::GetProxy() diff --git a/interfaces/innerkits/accesstoken/src/accesstoken_manager_client.h b/interfaces/innerkits/accesstoken/src/accesstoken_manager_client.h index d3cc13fca..4d260b6df 100644 --- a/interfaces/innerkits/accesstoken/src/accesstoken_manager_client.h +++ b/interfaces/innerkits/accesstoken/src/accesstoken_manager_client.h @@ -64,7 +64,7 @@ public: std::vector& nativeTokenInfoList); int DeleteRemoteToken(const std::string& deviceID, AccessTokenID tokenID); int DeleteRemoteDeviceTokens(const std::string& deviceID); - int DumpToken(std::string& dumpInfo); + void DumpTokenInfo(std::string& dumpInfo); private: AccessTokenManagerClient(); diff --git a/interfaces/innerkits/accesstoken/src/accesstoken_manager_proxy.cpp b/interfaces/innerkits/accesstoken/src/accesstoken_manager_proxy.cpp index af6849ecb..6bc666ef5 100644 --- a/interfaces/innerkits/accesstoken/src/accesstoken_manager_proxy.cpp +++ b/interfaces/innerkits/accesstoken/src/accesstoken_manager_proxy.cpp @@ -801,29 +801,27 @@ int AccessTokenManagerProxy::DeleteRemoteDeviceTokens(const std::string& deviceI return result; } -int AccessTokenManagerProxy::DumpToken(std::string& dumpInfo) +void AccessTokenManagerProxy::DumpTokenInfo(std::string& dumpInfo) { MessageParcel data; data.WriteInterfaceToken(IAccessTokenManager::GetDescriptor()); MessageParcel reply; - MessageOption option(MessageOption::TF_SYNC); + MessageOption option; sptr remote = Remote(); if (remote == nullptr) { - ACCESSTOKEN_LOG_ERROR(LABEL, "remote service null."); - return RET_FAILED; + ACCESSTOKEN_LOG_ERROR(LABEL, "%{public}s: remote service null.", __func__); + return; } int32_t requestResult = remote->SendRequest( - static_cast(IAccessTokenManager::InterfaceCode::DUMP), data, reply, option); + static_cast(IAccessTokenManager::InterfaceCode::DUMP_TOKENINFO), data, reply, option); if (requestResult != NO_ERROR) { - ACCESSTOKEN_LOG_ERROR(LABEL, "send request fail, result: %{public}d", requestResult); - return RET_FAILED; + ACCESSTOKEN_LOG_ERROR(LABEL, "%{public}s send request fail, result: %{public}d", __func__, requestResult); + return; } dumpInfo = reply.ReadString(); - AccessTokenID result = reply.ReadInt32(); - ACCESSTOKEN_LOG_DEBUG(LABEL, "get result from server data = %{public}d", result); - return result; + ACCESSTOKEN_LOG_DEBUG(LABEL, "%{public}s get result from server dumpInfo = %{public}s", __func__, dumpInfo.c_str()); } } // namespace AccessToken } // namespace Security diff --git a/interfaces/innerkits/accesstoken/src/accesstoken_manager_proxy.h b/interfaces/innerkits/accesstoken/src/accesstoken_manager_proxy.h index 55655a6f4..5e5b55172 100644 --- a/interfaces/innerkits/accesstoken/src/accesstoken_manager_proxy.h +++ b/interfaces/innerkits/accesstoken/src/accesstoken_manager_proxy.h @@ -66,7 +66,7 @@ public: int DeleteRemoteToken(const std::string& deviceID, AccessTokenID tokenID) override; int DeleteRemoteDeviceTokens(const std::string& deviceID) override; - int DumpToken(std::string& dumpInfo) override; + void DumpTokenInfo(std::string& dumpInfo) override; private: static inline BrokerDelegator delegator_; }; diff --git a/interfaces/kits/accesstoken/BUILD.gn b/interfaces/kits/accesstoken/BUILD.gn index 845943ab3..69a05a5d6 100644 --- a/interfaces/kits/accesstoken/BUILD.gn +++ b/interfaces/kits/accesstoken/BUILD.gn @@ -15,34 +15,18 @@ import("//build/ohos.gni") ohos_shared_library("libabilityaccessctrl") { include_dirs = [ - "//foundation/appexecfwk/standard/interfaces/innerkits/libeventhandler/include", - "//foundation/distributedschedule/samgr/interfaces/innerkits/samgr_proxy/include", - "//foundation/ace/napi/interfaces/kits", - "//third_party/json/single_include", - "//third_party/node/src", - "//utils/system/safwk/native/include", - "//foundation/communication/dsoftbus/interfaces/kits/transport", - "//foundation/communication/dsoftbus/interfaces/kits/common", - "//foundation/communication/dsoftbus/interfaces/kits/bus_center", - "//third_party/json/include", - "//foundation/aafwk/standard/interfaces/innerkits/ability_manager/include", "//base/security/access_token/frameworks/common/include", - "//base/security/access_token/interfaces/kits/accesstoken/napi/include", "//base/security/access_token/interfaces/innerkits/accesstoken/include", + "//base/security/access_token/interfaces/kits/accesstoken/napi/include", + "//foundation/ace/napi/interfaces/innerkits", + "//foundation/ace/napi/interfaces/kits", ] sources = [ "//base/security/access_token/interfaces/kits/accesstoken/napi/src/napi_atmanager.cpp" ] deps = [ - "//base/notification/ans_standard/frameworks/ans/core:ans_core", "//base/security/access_token/interfaces/innerkits/accesstoken:libaccesstoken_sdk", - "//base/startup/syspara_lite/interfaces/innerkits/native/syspara:syspara", - "//foundation/aafwk/standard/interfaces/innerkits/base:base", "//foundation/ace/napi:ace_napi", - "//foundation/appexecfwk/standard/interfaces/innerkits/libeventhandler:libeventhandler", - "//foundation/distributedschedule/safwk/interfaces/innerkits/safwk:system_ability_fwk", - "//foundation/distributedschedule/samgr/interfaces/innerkits/samgr_proxy:samgr_proxy", - "//utils/native/base:utils", ] cflags_cc = [ "-DHILOG_ENABLE" ] external_deps = [ diff --git a/interfaces/kits/accesstoken/napi/src/napi_atmanager.cpp b/interfaces/kits/accesstoken/napi/src/napi_atmanager.cpp index 02dd841c4..80cdb438e 100644 --- a/interfaces/kits/accesstoken/napi/src/napi_atmanager.cpp +++ b/interfaces/kits/accesstoken/napi/src/napi_atmanager.cpp @@ -268,7 +268,7 @@ void NapiAtManager::GrantUserGrantedPermissionExcute(napi_env env, void *data) asyncContext->permissionName, asyncContext->flag); - ACCESSTOKEN_LOG_DEBUG(LABEL, + ACCESSTOKEN_LOG_DEBUG(LABEL, "tokenId = %{public}d, permissionName = %{public}s, flag = %{public}d, grant result = %{public}d.", asyncContext->tokenId, asyncContext->permissionName, asyncContext->flag, asyncContext->result); diff --git a/services/accesstokenmanager/main/cpp/include/service/accesstoken_manager_service.h b/services/accesstokenmanager/main/cpp/include/service/accesstoken_manager_service.h index cbb3cfa14..8968ab844 100644 --- a/services/accesstokenmanager/main/cpp/include/service/accesstoken_manager_service.h +++ b/services/accesstokenmanager/main/cpp/include/service/accesstoken_manager_service.h @@ -66,7 +66,7 @@ public: int DeleteRemoteToken(const std::string& deviceID, AccessTokenID tokenID) override; int DeleteRemoteDeviceTokens(const std::string& deviceID) override; - int DumpToken(std::string& dumpInfo) override; + void DumpTokenInfo(std::string& dumpInfo) override; private: bool Initialize() const; diff --git a/services/accesstokenmanager/main/cpp/include/service/accesstoken_manager_stub.h b/services/accesstokenmanager/main/cpp/include/service/accesstoken_manager_stub.h index c4fbb77e1..332718599 100644 --- a/services/accesstokenmanager/main/cpp/include/service/accesstoken_manager_stub.h +++ b/services/accesstokenmanager/main/cpp/include/service/accesstoken_manager_stub.h @@ -60,7 +60,7 @@ private: void DeleteRemoteTokenInner(MessageParcel& data, MessageParcel& reply); void DeleteRemoteDeviceTokensInner(MessageParcel& data, MessageParcel& reply); void GetRemoteHapTokenIDInner(MessageParcel& data, MessageParcel& reply); - void DumpTokenInner(MessageParcel& data, MessageParcel& reply); + void DumpTokenInfoInner(MessageParcel& data, MessageParcel& reply); bool IsAuthorizedCalling() const; static const int SYSTEM_UID = 1000; diff --git a/services/accesstokenmanager/main/cpp/include/token/accesstoken_info_manager.h b/services/accesstokenmanager/main/cpp/include/token/accesstoken_info_manager.h index 3c4329380..1da2564f6 100644 --- a/services/accesstokenmanager/main/cpp/include/token/accesstoken_info_manager.h +++ b/services/accesstokenmanager/main/cpp/include/token/accesstoken_info_manager.h @@ -50,7 +50,7 @@ public: AccessTokenID AllocLocalTokenID(const std::string& remoteDeviceID, AccessTokenID remoteTokenID); void ProcessNativeTokenInfos(const std::vector>& tokenInfos); int UpdateHapToken(AccessTokenID tokenID, const std::string& appIDDesc, const HapPolicyParams& policy); - void Dump(std::string& dumpInfo); + void DumpTokenInfo(std::string& dumpInfo); void RefreshTokenInfoIfNeeded(); /* tokensync needed */ diff --git a/services/accesstokenmanager/main/cpp/src/permission/permission_policy_set.cpp b/services/accesstokenmanager/main/cpp/src/permission/permission_policy_set.cpp index 9cb3d0507..5ede90e96 100644 --- a/services/accesstokenmanager/main/cpp/src/permission/permission_policy_set.cpp +++ b/services/accesstokenmanager/main/cpp/src/permission/permission_policy_set.cpp @@ -247,75 +247,82 @@ void PermissionPolicySet::GetPermissionStateList(std::vector infoGuard(this->permPolicySetLock_); - info.append(",\n\t"); - info.append(R"("permDefList": [)"); + info.append(R"( "permDefList": [)"); + info.append("\n"); for (auto iter = permList_.begin(); iter != permList_.end(); iter++) { - info.append("\n\t\t"); PermDefToString(*iter, info); if (iter != (permList_.end() - 1)) { - info.append(","); + info.append(",\n"); } } - info.append("]"); + info.append("\n ],\n"); - info.append(",\n\t"); - info.append(R"("permStateList": [)"); + info.append(R"( "permStateList": [)"); + info.append("\n"); for (auto iter = permStateList_.begin(); iter != permStateList_.end(); iter++) { - info.append("\n\t\t"); PermStateFullToString(*iter, info); if (iter != (permStateList_.end() - 1)) { - info.append(","); + info.append(",\n"); } } - info.append("]"); + info.append("\n ]\n"); } } // namespace AccessToken } // namespace Security 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 4fe4e741b..87236d583 100644 --- a/services/accesstokenmanager/main/cpp/src/service/accesstoken_manager_service.cpp +++ b/services/accesstokenmanager/main/cpp/src/service/accesstoken_manager_service.cpp @@ -293,10 +293,11 @@ int AccessTokenManagerService::DeleteRemoteDeviceTokens(const std::string& devic return AccessTokenInfoManager::GetInstance().DeleteRemoteDeviceTokens(deviceID); } -int AccessTokenManagerService::DumpToken(std::string& dumpInfo) +void AccessTokenManagerService::DumpTokenInfo(std::string& dumpInfo) { - AccessTokenInfoManager::GetInstance().Dump(dumpInfo); - return 0; + ACCESSTOKEN_LOG_INFO(LABEL, "%{public}s called", __func__); + + AccessTokenInfoManager::GetInstance().DumpTokenInfo(dumpInfo); } bool AccessTokenManagerService::Initialize() const diff --git a/services/accesstokenmanager/main/cpp/src/service/accesstoken_manager_stub.cpp b/services/accesstokenmanager/main/cpp/src/service/accesstoken_manager_stub.cpp index afb050cda..24712019f 100644 --- a/services/accesstokenmanager/main/cpp/src/service/accesstoken_manager_stub.cpp +++ b/services/accesstokenmanager/main/cpp/src/service/accesstoken_manager_stub.cpp @@ -368,17 +368,15 @@ void AccessTokenManagerStub::DeleteRemoteDeviceTokensInner(MessageParcel& data, reply.WriteInt32(result); } -void AccessTokenManagerStub::DumpTokenInner(MessageParcel& data, MessageParcel& reply) +void AccessTokenManagerStub::DumpTokenInfoInner(MessageParcel& data, MessageParcel& reply) { if (!IsAuthorizedCalling()) { ACCESSTOKEN_LOG_INFO(LABEL, "%{public}s called, permission denied", __func__); - reply.WriteInt32(RET_FAILED); return; } - std::string dumpInfo; - int result = this->DumpToken(dumpInfo); + std::string dumpInfo = ""; + this->DumpTokenInfo(dumpInfo); reply.WriteString(dumpInfo); - reply.WriteUint32(result); } bool AccessTokenManagerStub::IsAuthorizedCalling() const @@ -436,8 +434,8 @@ AccessTokenManagerStub::AccessTokenManagerStub() &AccessTokenManagerStub::DeleteRemoteTokenInner; requestFuncMap_[static_cast(IAccessTokenManager::InterfaceCode::DELETE_REMOTE_DEVICE_TOKEN)] = &AccessTokenManagerStub::DeleteRemoteDeviceTokensInner; - requestFuncMap_[static_cast(IAccessTokenManager::InterfaceCode::DUMP)] = - &AccessTokenManagerStub::DumpTokenInner; + requestFuncMap_[static_cast(IAccessTokenManager::InterfaceCode::DUMP_TOKENINFO)] = + &AccessTokenManagerStub::DumpTokenInfoInner; } AccessTokenManagerStub::~AccessTokenManagerStub() 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 c504c2c72..a0fd03dbd 100644 --- a/services/accesstokenmanager/main/cpp/src/token/accesstoken_info_manager.cpp +++ b/services/accesstokenmanager/main/cpp/src/token/accesstoken_info_manager.cpp @@ -821,25 +821,28 @@ void AccessTokenInfoManager::RefreshTokenInfoIfNeeded() }); } -void AccessTokenInfoManager::Dump(std::string& dumpInfo) +void AccessTokenInfoManager::DumpTokenInfo(std::string& dumpInfo) { + ACCESSTOKEN_LOG_INFO(LABEL, "%{public}s called", __func__); + ACCESSTOKEN_LOG_INFO(LABEL, "get hapTokenInfo"); + Utils::UniqueReadGuard hapInfoGuard(this->hapTokenInfoLock_); for (auto iter = hapTokenInfoMap_.begin(); iter != hapTokenInfoMap_.end(); iter++) { if (iter->second != nullptr) { - dumpInfo.append("\n"); iter->second->ToString(dumpInfo); dumpInfo.append("\n"); } } + ACCESSTOKEN_LOG_INFO(LABEL, "get nativeTokenInfo"); Utils::UniqueReadGuard nativeInfoGuard(this->nativeTokenInfoLock_); for (auto iter = nativeTokenInfoMap_.begin(); iter != nativeTokenInfoMap_.end(); iter++) { if (iter->second != nullptr) { - dumpInfo.append("\n"); iter->second->ToString(dumpInfo); dumpInfo.append("\n"); } } + ACCESSTOKEN_LOG_INFO(LABEL, "get tokeninfo: %{public}s", dumpInfo.c_str()); } } // namespace AccessToken } // namespace Security diff --git a/services/accesstokenmanager/main/cpp/src/token/hap_token_info_inner.cpp b/services/accesstokenmanager/main/cpp/src/token/hap_token_info_inner.cpp index bb2ab3d95..acf394435 100644 --- a/services/accesstokenmanager/main/cpp/src/token/hap_token_info_inner.cpp +++ b/services/accesstokenmanager/main/cpp/src/token/hap_token_info_inner.cpp @@ -226,16 +226,18 @@ void HapTokenInfoInner::SetRemote(bool isRemote) void HapTokenInfoInner::ToString(std::string& info) const { - info.append(R"({"tokenID": )" + std::to_string(tokenInfoBasic_.tokenID)); - info.append(R"(, "tokenAttr": )" + std::to_string(tokenInfoBasic_.tokenAttr)); - info.append(R"(, "ver": )" + std::to_string(tokenInfoBasic_.ver)); - info.append(R"(, "userId": )" + std::to_string(tokenInfoBasic_.userID)); - info.append(R"(, "bundleName": ")" + tokenInfoBasic_.bundleName + R"(")"); - info.append(R"(, "instIndex": )" + std::to_string(tokenInfoBasic_.instIndex)); - info.append(R"(, "appID": ")" + tokenInfoBasic_.appID + R"(")"); - info.append(R"(, "deviceID": ")" + tokenInfoBasic_.deviceID + R"(")"); - info.append(R"(, "apl": )" + std::to_string(tokenInfoBasic_.apl)); - info.append(R"(, "isRemote": )" + std::to_string(isRemote_)); + info.append(R"({)"); + info.append("\n"); + info.append(R"( "tokenID": )" + std::to_string(tokenInfoBasic_.tokenID) + ",\n"); + info.append(R"( "tokenAttr": )" + std::to_string(tokenInfoBasic_.tokenAttr) + ",\n"); + info.append(R"( "ver": )" + std::to_string(tokenInfoBasic_.ver) + ",\n"); + info.append(R"( "userId": )" + std::to_string(tokenInfoBasic_.userID) + ",\n"); + info.append(R"( "bundleName": ")" + tokenInfoBasic_.bundleName + R"(")" + ",\n"); + info.append(R"( "instIndex": )" + std::to_string(tokenInfoBasic_.instIndex) + ",\n"); + info.append(R"( "appID": ")" + tokenInfoBasic_.appID + R"(")" + ",\n"); + info.append(R"( "deviceID": ")" + tokenInfoBasic_.deviceID + R"(")" + ",\n"); + info.append(R"( "apl": )" + std::to_string(tokenInfoBasic_.apl) + ",\n"); + info.append(R"( "isRemote": )" + std::to_string(isRemote_) + ",\n"); if (permPolicySet_ != nullptr) { permPolicySet_->ToString(info); 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 0e5f70d85..ba2f269bb 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 @@ -184,13 +184,15 @@ void NativeTokenInfoInner::SetDcaps(const std::string& dcapStr) void NativeTokenInfoInner::ToString(std::string& info) const { - info.append(R"({"tokenID": )" + std::to_string(tokenInfoBasic_.tokenID)); - info.append(R"(, "tokenAttr": )" + std::to_string(tokenInfoBasic_.tokenAttr)); - info.append(R"(, "ver": )" + std::to_string(tokenInfoBasic_.ver)); - info.append(R"(, "processName": ")" + tokenInfoBasic_.processName + R"(")"); - info.append(R"(, "apl": )" + std::to_string(tokenInfoBasic_.apl)); - info.append(R"(, "dcap": ")" + DcapToString(tokenInfoBasic_.dcap) + R"(")"); - info.append(R"(, "isRemote": )" + std::to_string(isRemote_)); + info.append(R"({)"); + info.append("\n"); + info.append(R"( "tokenID": )" + std::to_string(tokenInfoBasic_.tokenID) + ",\n"); + info.append(R"( "tokenAttr": )" + std::to_string(tokenInfoBasic_.tokenAttr) + ",\n"); + info.append(R"( "ver": )" + std::to_string(tokenInfoBasic_.ver) + ",\n"); + info.append(R"( "processName": ")" + tokenInfoBasic_.processName + R"(")" + ",\n"); + info.append(R"( "apl": )" + std::to_string(tokenInfoBasic_.apl) + ",\n"); + info.append(R"( "dcap": ")" + DcapToString(tokenInfoBasic_.dcap) + R"(")" + ",\n"); + info.append(R"( "isRemote": )" + std::to_string(isRemote_) + ",\n"); info.append("}"); } } // namespace AccessToken diff --git a/services/accesstokenmanager/test/unittest/cpp/src/accesstoken_info_manager_test.cpp b/services/accesstokenmanager/test/unittest/cpp/src/accesstoken_info_manager_test.cpp index d981b08d9..9f13e1488 100644 --- a/services/accesstokenmanager/test/unittest/cpp/src/accesstoken_info_manager_test.cpp +++ b/services/accesstokenmanager/test/unittest/cpp/src/accesstoken_info_manager_test.cpp @@ -101,7 +101,7 @@ HWTEST_F(AccessTokenInfoManagerTest, Init001, TestSize.Level1) { AccessTokenInfoManager::GetInstance().Init(); std::string dumpInfo; - AccessTokenInfoManager::GetInstance().Dump(dumpInfo); + AccessTokenInfoManager::GetInstance().DumpTokenInfo(dumpInfo); GTEST_LOG_(INFO) << "dump all:" << dumpInfo.c_str(); // delete test token diff --git a/tools/accesstoken/BUILD.gn b/tools/accesstoken/BUILD.gn new file mode 100644 index 000000000..b7e339513 --- /dev/null +++ b/tools/accesstoken/BUILD.gn @@ -0,0 +1,62 @@ +# Copyright (c) 2021 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. + +import("//build/ohos.gni") + +ohos_source_set("tools_atm_source_set") { + include_dirs = [ + "//base/security/access_token/tools/accesstoken/include", + "//base/security/access_token/frameworks/common/include", + "//foundation/aafwk/standard/tools/aa/include", + "//base/security/access_token/interfaces/innerkits/accesstoken/main/cpp/include", + "//foundation/appexecfwk/standard/interfaces/innerkits/appexecfwk_core/include/bundlemgr", + "//foundation/aafwk/standard/services/common/include", + ] + + sources = [ + "//foundation/aafwk/standard/tools/aa/src/shell_command.cpp", + "src/atm_command.cpp", + "src/atm_receiver_impl.cpp", + "src/main.cpp", + ] + + deps = [ + "//base/security/access_token/interfaces/innerkits/accesstoken:libaccesstoken_sdk", + "//foundation/aafwk/standard/tools/aa:aa", + "//foundation/appexecfwk/standard/interfaces/innerkits/appexecfwk_core:appexecfwk_core", + "//utils/native/base:utils", + ] + + cflags = [ "-DHILOG_ENABLE" ] + + if (target_cpu == "arm") { + cflags += [ "-DBINDER_IPC_32BIT" ] + } + + external_deps = [ + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + ] +} + +ohos_executable("atm") { + deps = [ ":tools_atm_source_set" ] + + install_enable = true + subsystem_name = "security" + part_name = "access_token" +} + +group("tools_atm") { + deps = [ ":atm" ] +} diff --git a/tools/accesstoken/include/atm_command.h b/tools/accesstoken/include/atm_command.h new file mode 100644 index 000000000..a8c88c7da --- /dev/null +++ b/tools/accesstoken/include/atm_command.h @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2021 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 ACCESSTOKENMANAGER_COMMAND_H +#define ACCESSTOKENMANAGER_COMMAND_H + +#include "shell_command.h" + +namespace OHOS { +namespace Security { +namespace AccessToken { +const std::string TOOLS_NAME = "atm"; +const std::string HELP_MSG = "usage: atm \n" + "These are common atm commands list:\n" + " help list available commands\n" + " dump list token info\n"; + +class AtmCommand : public OHOS::AAFwk::ShellCommand { +public: + AtmCommand(int argc, char *argv[]); + ~AtmCommand() override + {} + +private: + ErrCode CreateCommandMap() override; + ErrCode CreateMessageMap() override; + ErrCode init() override; + + ErrCode RunAsHelpCommand(); + ErrCode RunAsDumpCommand(); +}; +} // namespace AccessToken +} // namespace Security +} // namespace OHOS + +#endif // ACCESSTOKENMANAGER_COMMAND_H \ No newline at end of file diff --git a/tools/accesstoken/include/atm_receiver_impl.h b/tools/accesstoken/include/atm_receiver_impl.h new file mode 100644 index 000000000..bc6a077ee --- /dev/null +++ b/tools/accesstoken/include/atm_receiver_impl.h @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2021 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 ACCESSTOKENMANAGER_RECEIVER_IMPL_H +#define ACCESSTOKENMANAGER_RECEIVER_IMPL_H + +#include +#include "status_receiver_host.h" + +namespace OHOS { +namespace Security { +namespace AccessToken { +class AtmReceiverImpl : public AppExecFwk::StatusReceiverHost { +public: + AtmReceiverImpl(); + virtual ~AtmReceiverImpl() override; + + virtual void OnStatusNotify(const int process) override; + virtual void OnFinished(const int32_t resultCode, const std::string &resultMsg) override; + int32_t GetResultCode() const; + +private: + mutable std::promise resultMsgSignal_; + + DISALLOW_COPY_AND_MOVE(AtmReceiverImpl); +}; +} // namespace AccessToken +} // namespace Security +} // namespace OHOS + +#endif // ACCESSTOKENMANAGER_RECEIVER_IMPL_H \ No newline at end of file diff --git a/tools/accesstoken/src/atm_command.cpp b/tools/accesstoken/src/atm_command.cpp new file mode 100644 index 000000000..a8ed7ed82 --- /dev/null +++ b/tools/accesstoken/src/atm_command.cpp @@ -0,0 +1,79 @@ +/* + * Copyright (c) 2021 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 "atm_command.h" + +#include "accesstoken_kit.h" +#include "status_receiver_host.h" + +namespace OHOS { +namespace Security { +namespace AccessToken { +AtmCommand::AtmCommand(int argc, char *argv[]) : ShellCommand(argc, argv, TOOLS_NAME) +{} + +ErrCode AtmCommand::CreateCommandMap() +{ + commandMap_ = { + {"help", std::bind(&AtmCommand::RunAsHelpCommand, this)}, + {"dump", std::bind(&AtmCommand::RunAsDumpCommand, this)}, + }; + + return OHOS::ERR_OK; +} + +ErrCode AtmCommand::CreateMessageMap() +{ + messageMap_ = { + // error + message + // currently there is no error to use + // { + // AppExecFwk::IStatusReceiver::ERR_USER_REMOVE_FALIED, + // "error: user remove failed.", + // }, + }; + + return OHOS::ERR_OK; +} + +ErrCode AtmCommand::init() +{ + ErrCode result = OHOS::ERR_OK; + + // there is no need to get proxy currently, the function used in class AccessTokenKit is static + + return result; +} + +ErrCode AtmCommand::RunAsHelpCommand() +{ + resultReceiver_.append(HELP_MSG); + + return OHOS::ERR_OK; +} + +ErrCode AtmCommand::RunAsDumpCommand() +{ + int result = OHOS::ERR_OK; + std::string tokenInfo = ""; + + AccessTokenKit::DumpTokenInfo(tokenInfo); + resultReceiver_ = tokenInfo + "\n"; + + return result; +} +} // namespace AccessToken +} // namespace Security +} // namespace OHOS \ No newline at end of file diff --git a/tools/accesstoken/src/atm_receiver_impl.cpp b/tools/accesstoken/src/atm_receiver_impl.cpp new file mode 100644 index 000000000..24824bbeb --- /dev/null +++ b/tools/accesstoken/src/atm_receiver_impl.cpp @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2021 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_log.h" +#include "atm_receiver_impl.h" + +namespace OHOS { +namespace Security { +namespace AccessToken { +namespace { +static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { + LOG_CORE, SECURITY_DOMAIN_ACCESSTOKEN, "AccessTokenManagerTools" +}; +} + +AtmReceiverImpl::AtmReceiverImpl() +{ + ACCESSTOKEN_LOG_DEBUG(LABEL, "create atm status receiver instance"); +} + +AtmReceiverImpl::~AtmReceiverImpl() +{ + ACCESSTOKEN_LOG_DEBUG(LABEL, "destory atm status receiver instance"); +} + +void AtmReceiverImpl::OnFinished(const int32_t resultCode, const std::string &resultMsg) +{ + ACCESSTOKEN_LOG_DEBUG(LABEL, "on finished result is %{public}d, %{public}s", resultCode, resultMsg.c_str()); + resultMsgSignal_.set_value(resultCode); +} + +void AtmReceiverImpl::OnStatusNotify(const int process) +{ + ACCESSTOKEN_LOG_DEBUG(LABEL, "on OnStatusNotify is %{public}d", process); +} + +int32_t AtmReceiverImpl::GetResultCode() const +{ + auto future = resultMsgSignal_.get_future(); + future.wait(); + int32_t resultCode = future.get(); + + return resultCode; +} +} // namespace AccessToken +} // namespace Security +} // namespace OHOS \ No newline at end of file diff --git a/tools/accesstoken/src/main.cpp b/tools/accesstoken/src/main.cpp new file mode 100644 index 000000000..0fdb5fac3 --- /dev/null +++ b/tools/accesstoken/src/main.cpp @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2021 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 + +#include "atm_command.h" + +int main(int argc, char *argv[]) +{ + OHOS::Security::AccessToken::AtmCommand cmd(argc, argv); + std::cout << cmd.ExecCommand(); + return 0; +} \ No newline at end of file -- Gitee