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
24 namespace OHOS {
25 namespace AccountSA {
26 namespace {
27 const std::int32_t ARGS_SIZE_ONE = 1;
28 const std::int32_t ARGS_SIZE_TWO = 2;
29 const std::int32_t FIRST_PARAMETER = 0;
30 const std::int32_t SECOND_PARAMETER = 1;
31 const std::string ARGS_HELP = "-h";
32 const std::string ARGS_OHOS_ACCOUNT_INFOS = "-ohos_account_infos";
33 const std::string ARGS_OS_ACCOUNT_INFOS = "-os_account_infos";
34 const std::string ARGS_SHOW_LOG_LEVEL = "-show_log_level";
35 const std::string ARGS_SET_LOG_LEVEL = "-set_log_level";
36 const std::string ARGS_DUMP_TIME_INFO = "-time_info_dump";
37 const std::string ILLEGAL_INFORMATION = "Account Manager service, enter '-h' for usage.\n";
38 const std::string SYSTEM_ERROR = "System error: ";
39 const double ANONYMIZE_RATIO = 0.8;
40 const size_t MIN_ANONYMIZE_PART_LEN = 10;
41 const size_t MAX_INTERCEPT_PART_LEN = 4;
42 const size_t INTERCEPT_HEAD_PART_LEN_FOR_NAME = 1;
43 const std::string DEFAULT_ANON_STR = "**********";
AnonymizeNameStr(const std::string & nameStr)44 std::string AnonymizeNameStr(const std::string& nameStr)
45 {
46 if (nameStr == DEFAULT_OHOS_ACCOUNT_NAME || nameStr.empty()) {
47 return nameStr;
48 }
49 std::string retStr = nameStr.substr(0, INTERCEPT_HEAD_PART_LEN_FOR_NAME) + DEFAULT_ANON_STR;
50 return retStr;
51 }
52
AnonymizeUidStr(const std::string & uidStr)53 std::string AnonymizeUidStr(const std::string& uidStr)
54 {
55 if (uidStr == DEFAULT_OHOS_ACCOUNT_UID || uidStr.empty()) {
56 return uidStr;
57 }
58
59 size_t anonymizeLen = static_cast<size_t>(static_cast<double>(uidStr.length()) * ANONYMIZE_RATIO);
60 size_t interceptLen = (uidStr.length() - anonymizeLen) / 2; // Half head and half tail
61 if (anonymizeLen < MIN_ANONYMIZE_PART_LEN || interceptLen == 0) {
62 return DEFAULT_ANON_STR;
63 }
64 interceptLen = (interceptLen > MAX_INTERCEPT_PART_LEN ? MAX_INTERCEPT_PART_LEN : interceptLen);
65
66 std::string retStr = uidStr.substr(0, interceptLen);
67 retStr += DEFAULT_ANON_STR;
68 retStr += uidStr.substr(uidStr.length() - interceptLen);
69 return retStr;
70 }
71 } // namespace
72
AccountDumpHelper(OsAccountManagerService * osAccountMgrService)73 AccountDumpHelper::AccountDumpHelper(OsAccountManagerService* osAccountMgrService)
74 {
75 osAccountMgrService_ = osAccountMgrService;
76 }
77
Dump(const std::vector<std::string> & args,std::string & result) const78 void AccountDumpHelper::Dump(const std::vector<std::string>& args, std::string& result) const
79 {
80 result.clear();
81 auto argsSize = args.size();
82 if (argsSize == ARGS_SIZE_ONE) {
83 ProcessOneParameter(args[FIRST_PARAMETER], result);
84 } else if (argsSize == ARGS_SIZE_TWO) {
85 ProcessTwoParameter(args[FIRST_PARAMETER], args[SECOND_PARAMETER], result);
86 } else {
87 ShowIllegalInformation(result);
88 }
89 }
90
ShowHelp(std::string & result) const91 void AccountDumpHelper::ShowHelp(std::string& result) const
92 {
93 result.append("Usage:dump <command> [options]\n")
94 .append("Description:\n")
95 .append("-ohos_account_infos :")
96 .append("dump all distributed account information in the system\n")
97 .append("-os_account_infos :")
98 .append("dump all os account information in the system\n")
99 .append("-show_log_level :")
100 .append("show account SA's log level\n")
101 .append("-set_log_level <level> :")
102 .append("set account SA's log level\n")
103 .append("-time_info_dump :")
104 .append("dump some important time points\n");
105 }
106
ShowIllegalInformation(std::string & result) const107 void AccountDumpHelper::ShowIllegalInformation(std::string& result) const
108 {
109 result.append(ILLEGAL_INFORMATION);
110 }
111
ShowOhosAccountInfo(std::string & result) const112 void AccountDumpHelper::ShowOhosAccountInfo(std::string& result) const
113 {
114 // check os account list
115 std::vector<OsAccountInfo> osAccountInfos;
116 ErrCode ret = IInnerOsAccountManager::GetInstance().QueryAllCreatedOsAccounts(osAccountInfos);
117 if (ret != ERR_OK) {
118 result.append("Cannot query os account list, error code ");
119 result.append(std::to_string(ret));
120 return;
121 }
122
123 if (osAccountInfos.empty()) {
124 result.append(SYSTEM_ERROR + "os account list empty.\n");
125 return;
126 }
127
128 result.append("OhosAccount info:\n");
129 for (size_t i = 0; i < osAccountInfos.size(); ++i) {
130 AccountInfo accountInfo;
131 (void)OhosAccountManager::GetInstance().GetAccountInfoByUserId(osAccountInfos[i].GetLocalId(), accountInfo);
132 result.append(" Bind local user id: ");
133 result.append(std::to_string(accountInfo.userId_) + "\n");
134 result.append(" OhosAccount name : ");
135 result.append(AnonymizeNameStr(accountInfo.ohosAccountInfo_.name_) + "\n");
136 result.append(" OhosAccount uid : ");
137 result.append(AnonymizeUidStr(accountInfo.ohosAccountInfo_.uid_) + "\n");
138 result.append(" OhosAccount status : ");
139 result.append(std::to_string(accountInfo.ohosAccountInfo_.status_) + "\n");
140 result.append(" OhosAccount bind time: ");
141 result.append(std::to_string(accountInfo.bindTime_) + "\n");
142 }
143 }
144
ShowOsAccountInfo(std::string & result) const145 void AccountDumpHelper::ShowOsAccountInfo(std::string& result) const
146 {
147 if (osAccountMgrService_ == nullptr) {
148 result.append(SYSTEM_ERROR + "service ptr is null!\n");
149 ACCOUNT_LOGE("service ptr is null!");
150 return;
151 }
152
153 std::vector<std::string> states;
154 ErrCode ret = osAccountMgrService_->DumpOsAccountInfo(states);
155 if (ret != ERR_OK) {
156 result.append("Cannot query os account list, error code ");
157 result.append(std::to_string(ret));
158 return;
159 }
160
161 if (states.empty()) {
162 result.append(SYSTEM_ERROR + "os account list empty.\n");
163 return;
164 }
165
166 result.append("OsAccount info:\n");
167 for (size_t i = 0; i < states.size(); ++i) {
168 result.append(" " + states[i] + "\n");
169 }
170 }
171
ProcessOneParameter(const std::string & arg,std::string & result) const172 void AccountDumpHelper::ProcessOneParameter(const std::string& arg, std::string& result) const
173 {
174 if (arg == ARGS_HELP) {
175 ShowHelp(result);
176 } else if (arg == ARGS_OHOS_ACCOUNT_INFOS) {
177 ShowOhosAccountInfo(result);
178 } else if (arg == ARGS_OS_ACCOUNT_INFOS) {
179 ShowOsAccountInfo(result);
180 } else if (arg == ARGS_SHOW_LOG_LEVEL) {
181 auto logLevel = static_cast<std::int32_t>(AccountLogWrapper::GetLogLevel());
182 result.append("Current Log Level: " + std::to_string(logLevel) + "\n");
183 } else if (arg == ARGS_DUMP_TIME_INFO) {
184 PerfStat::GetInstance().Dump(result);
185 } else {
186 ShowHelp(result);
187 }
188 }
189
SetLogLevel(const std::string & levelStr,std::string & result) const190 void AccountDumpHelper::SetLogLevel(const std::string& levelStr, std::string& result) const
191 {
192 if (!regex_match(levelStr, std::regex("^\\-?\\d+$"))) {
193 ACCOUNT_LOGE("Invalid format of log level");
194 result.append("Invalid format of log level\n");
195 return;
196 }
197 auto level = std::stoi(levelStr);
198 if ((level < static_cast<std::int32_t>(AccountLogLevel::DEBUG)) ||
199 (level > static_cast<std::int32_t>(AccountLogLevel::FATAL))) {
200 result.append("Invalid logLevel\n");
201 } else {
202 AccountLogLevel logLevel = static_cast<AccountLogLevel>(level);
203 AccountLogWrapper::SetLogLevel(logLevel);
204 result.append("Set logLevel success\n");
205 }
206 }
207
ProcessTwoParameter(const std::string & arg1,const std::string & arg2,std::string & result) const208 void AccountDumpHelper::ProcessTwoParameter(const std::string& arg1, const std::string& arg2, std::string& result) const
209 {
210 if (arg1 == ARGS_SET_LOG_LEVEL) {
211 SetLogLevel(arg2, result);
212 } else {
213 ShowHelp(result);
214 }
215 }
216 } // namespace AccountSA
217 } // namespace OHOS
218