1/* 2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16#include "account_dump_helper.h" 17#include <regex> 18#include "account_error_no.h" 19#include "account_info.h" 20#include "account_log_wrapper.h" 21#include "perf_stat.h" 22#include "string_ex.h" 23 24namespace OHOS { 25namespace AccountSA { 26namespace { 27const std::int32_t ARGS_SIZE_ONE = 1; 28const std::int32_t ARGS_SIZE_TWO = 2; 29const std::int32_t FIRST_PARAMETER = 0; 30const std::int32_t SECOND_PARAMETER = 1; 31const std::string ARGS_HELP = "-h"; 32const std::string ARGS_OHOS_ACCOUNT_INFOS = "-ohos_account_infos"; 33const std::string ARGS_OS_ACCOUNT_INFOS = "-os_account_infos"; 34const std::string ARGS_SHOW_LOG_LEVEL = "-show_log_level"; 35const std::string ARGS_SET_LOG_LEVEL = "-set_log_level"; 36const std::string ARGS_DUMP_TIME_INFO = "-time_info_dump"; 37const std::string ILLEGAL_INFORMATION = "Account Manager service, enter '-h' for usage.\n"; 38const std::string SYSTEM_ERROR = "System error: "; 39const double ANONYMIZE_RATIO = 0.8; 40const size_t MIN_ANONYMIZE_PART_LEN = 10; 41const size_t MAX_INTERCEPT_PART_LEN = 4; 42const size_t INTERCEPT_HEAD_PART_LEN_FOR_NAME = 1; 43const std::string DEFAULT_ANON_STR = "**********"; 44} // namespace 45 46std::string AccountDumpHelper::AnonymizeNameStr(const std::string& nameStr) const 47{ 48 if (nameStr == DEFAULT_OHOS_ACCOUNT_NAME || nameStr.empty()) { 49 return nameStr; 50 } 51 std::string retStr = nameStr.substr(0, INTERCEPT_HEAD_PART_LEN_FOR_NAME) + DEFAULT_ANON_STR; 52 return retStr; 53} 54 55std::string AccountDumpHelper::AnonymizeUidStr(const std::string& uidStr) const 56{ 57 if (uidStr == DEFAULT_OHOS_ACCOUNT_UID || uidStr.empty()) { 58 return uidStr; 59 } 60 61 size_t anonymizeLen = static_cast<size_t>(static_cast<double>(uidStr.length()) * ANONYMIZE_RATIO); 62 size_t interceptLen = (uidStr.length() - anonymizeLen) / 2; // Half head and half tail 63 if (anonymizeLen < MIN_ANONYMIZE_PART_LEN || interceptLen == 0) { 64 return DEFAULT_ANON_STR; 65 } 66 interceptLen = (interceptLen > MAX_INTERCEPT_PART_LEN ? MAX_INTERCEPT_PART_LEN : interceptLen); 67 68 std::string retStr = uidStr.substr(0, interceptLen); 69 retStr += DEFAULT_ANON_STR; 70 retStr += uidStr.substr(uidStr.length() - interceptLen); 71 return retStr; 72} 73 74AccountDumpHelper::AccountDumpHelper(OsAccountManagerService* osAccountMgrService) 75{ 76 osAccountMgrService_ = osAccountMgrService; 77} 78 79void AccountDumpHelper::Dump(const std::vector<std::string>& args, std::string& result) const 80{ 81 result.clear(); 82 auto argsSize = args.size(); 83 if (argsSize == ARGS_SIZE_ONE) { 84 ProcessOneParameter(args[FIRST_PARAMETER], result); 85 } else if (argsSize == ARGS_SIZE_TWO) { 86 ProcessTwoParameter(args[FIRST_PARAMETER], args[SECOND_PARAMETER], result); 87 } else { 88 ShowIllegalInformation(result); 89 } 90} 91 92void AccountDumpHelper::ShowHelp(std::string& result) const 93{ 94 result.append("Usage:dump <command> [options]\n") 95 .append("Description:\n") 96 .append("-ohos_account_infos :") 97 .append("dump all distributed account information in the system\n") 98 .append("-os_account_infos :") 99 .append("dump all os account information in the system\n") 100 .append("-show_log_level :") 101 .append("show account SA's log level\n") 102 .append("-set_log_level <level> :") 103 .append("set account SA's log level\n") 104 .append("-time_info_dump :") 105 .append("dump some important time points\n"); 106} 107 108void AccountDumpHelper::ShowIllegalInformation(std::string& result) const 109{ 110 result.append(ILLEGAL_INFORMATION); 111} 112 113void AccountDumpHelper::ShowOhosAccountInfo(std::string& result) const 114{ 115 // check os account list 116 std::vector<OsAccountInfo> osAccountInfos; 117 ErrCode ret = IInnerOsAccountManager::GetInstance().QueryAllCreatedOsAccounts(osAccountInfos); 118 if (ret != ERR_OK) { 119 result.append("Cannot query os account list, error code "); 120 result.append(std::to_string(ret)); 121 return; 122 } 123 124 if (osAccountInfos.empty()) { 125 result.append(SYSTEM_ERROR + "os account list empty.\n"); 126 return; 127 } 128 129 result.append("OhosAccount info:\n"); 130 for (size_t i = 0; i < osAccountInfos.size(); ++i) { 131 AccountInfo accountInfo; 132 (void)OhosAccountManager::GetInstance().GetAccountInfoByUserId(osAccountInfos[i].GetLocalId(), accountInfo); 133 result.append(" Bind local user id: "); 134 result.append(std::to_string(accountInfo.userId_) + "\n"); 135 result.append(" OhosAccount name : "); 136 result.append(AnonymizeNameStr(accountInfo.ohosAccountInfo_.name_) + "\n"); 137 result.append(" OhosAccount uid : "); 138 result.append(AnonymizeUidStr(accountInfo.ohosAccountInfo_.uid_) + "\n"); 139 result.append(" OhosAccount status : "); 140 result.append(std::to_string(accountInfo.ohosAccountInfo_.status_) + "\n"); 141 result.append(" OhosAccount bind time: "); 142 result.append(std::to_string(accountInfo.bindTime_) + "\n"); 143 } 144} 145 146void AccountDumpHelper::ShowOsAccountInfo(std::string& result) const 147{ 148 if (osAccountMgrService_ == nullptr) { 149 result.append(SYSTEM_ERROR + "service ptr is null!\n"); 150 ACCOUNT_LOGE("service ptr is null!"); 151 return; 152 } 153 154 std::vector<std::string> states; 155 ErrCode ret = osAccountMgrService_->DumpOsAccountInfo(states); 156 if (ret != ERR_OK) { 157 result.append("Cannot query os account list, error code "); 158 result.append(std::to_string(ret)); 159 return; 160 } 161 162 if (states.empty()) { 163 result.append(SYSTEM_ERROR + "os account list empty.\n"); 164 return; 165 } 166 167 result.append("OsAccount info:\n"); 168 for (size_t i = 0; i < states.size(); ++i) { 169 result.append(" " + states[i] + "\n"); 170 } 171} 172 173void AccountDumpHelper::ProcessOneParameter(const std::string& arg, std::string& result) const 174{ 175 if (arg == ARGS_HELP) { 176 ShowHelp(result); 177 } else if (arg == ARGS_OHOS_ACCOUNT_INFOS) { 178 ShowOhosAccountInfo(result); 179 } else if (arg == ARGS_OS_ACCOUNT_INFOS) { 180 ShowOsAccountInfo(result); 181 } else if (arg == ARGS_SHOW_LOG_LEVEL) { 182 auto logLevel = static_cast<std::int32_t>(AccountLogWrapper::GetLogLevel()); 183 result.append("Current Log Level: " + std::to_string(logLevel) + "\n"); 184 } else if (arg == ARGS_DUMP_TIME_INFO) { 185 PerfStat::GetInstance().Dump(result); 186 } else { 187 ShowHelp(result); 188 } 189} 190 191void AccountDumpHelper::SetLogLevel(const std::string& levelStr, std::string& result) const 192{ 193 if (!regex_match(levelStr, std::regex("^\\-?\\d+$"))) { 194 ACCOUNT_LOGE("Invalid format of log level"); 195 result.append("Invalid format of log level\n"); 196 return; 197 } 198 int32_t level = 0; 199 if (!StrToInt(levelStr, level)) { 200 result.append("Invalid logLevel\n"); 201 } 202 if ((level < static_cast<std::int32_t>(AccountLogLevel::DEBUG)) || 203 (level > static_cast<std::int32_t>(AccountLogLevel::FATAL))) { 204 result.append("Invalid logLevel\n"); 205 } else { 206 AccountLogLevel logLevel = static_cast<AccountLogLevel>(level); 207 AccountLogWrapper::SetLogLevel(logLevel); 208 result.append("Set logLevel success\n"); 209 } 210} 211 212void AccountDumpHelper::ProcessTwoParameter(const std::string& arg1, const std::string& arg2, std::string& result) const 213{ 214 if (arg1 == ARGS_SET_LOG_LEVEL) { 215 SetLogLevel(arg2, result); 216 } else { 217 ShowHelp(result); 218 } 219} 220} // namespace AccountSA 221} // namespace OHOS 222