• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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(const std::shared_ptr<OhosAccountManager> & ohosAccountMgr,OsAccountManagerService * osAccountMgrService)73 AccountDumpHelper::AccountDumpHelper(const std::shared_ptr<OhosAccountManager>& ohosAccountMgr,
74     OsAccountManagerService *osAccountMgrService)
75     : innerMgrService_(DelayedSingleton<IInnerOsAccountManager>::GetInstance())
76 {
77     ohosAccountMgr_ = ohosAccountMgr;
78     osAccountMgrService_ = osAccountMgrService;
79 }
80 
Dump(const std::vector<std::string> & args,std::string & result) const81 void AccountDumpHelper::Dump(const std::vector<std::string>& args, std::string& result) const
82 {
83     result.clear();
84     auto argsSize = args.size();
85     if (argsSize == ARGS_SIZE_ONE) {
86         ProcessOneParameter(args[FIRST_PARAMETER], result);
87     } else if (argsSize == ARGS_SIZE_TWO) {
88         ProcessTwoParameter(args[FIRST_PARAMETER], args[SECOND_PARAMETER], result);
89     } else {
90         ShowIllegalInformation(result);
91     }
92 }
93 
ShowHelp(std::string & result) const94 void AccountDumpHelper::ShowHelp(std::string& result) const
95 {
96     result.append("Usage:dump <command> [options]\n")
97         .append("Description:\n")
98         .append("-ohos_account_infos          :")
99         .append("dump all distributed account information in the system\n")
100         .append("-os_account_infos            :")
101         .append("dump all os account information in the system\n")
102         .append("-show_log_level              :")
103         .append("show account SA's log level\n")
104         .append("-set_log_level <level>       :")
105         .append("set account SA's log level\n")
106         .append("-time_info_dump              :")
107         .append("dump some important time points\n");
108 }
109 
ShowIllegalInformation(std::string & result) const110 void AccountDumpHelper::ShowIllegalInformation(std::string& result) const
111 {
112     result.append(ILLEGAL_INFORMATION);
113 }
114 
ShowOhosAccountInfo(std::string & result) const115 void AccountDumpHelper::ShowOhosAccountInfo(std::string& result) const
116 {
117     auto lockPtr = ohosAccountMgr_.lock();
118     if (lockPtr == nullptr || innerMgrService_ == nullptr) {
119         result.append(SYSTEM_ERROR + "service ptr is null!\n");
120         ACCOUNT_LOGE("service ptr is null!");
121         return;
122     }
123 
124     // check os account list
125     std::vector<OsAccountInfo> osAccountInfos;
126     ErrCode ret = innerMgrService_->QueryAllCreatedOsAccounts(osAccountInfos);
127     if (ret != ERR_OK) {
128         result.append("Cannot query os account list, error code ");
129         result.append(std::to_string(ret));
130         return;
131     }
132 
133     if (osAccountInfos.empty()) {
134         result.append(SYSTEM_ERROR + "os account list empty.\n");
135         return;
136     }
137 
138     result.append("OhosAccount info:\n");
139     for (size_t i = 0; i < osAccountInfos.size(); ++i) {
140         AccountInfo accountInfo;
141         lockPtr->GetAccountInfoByUserId(osAccountInfos[i].GetLocalId(), accountInfo);
142         result.append("     Bind local user id: ");
143         result.append(std::to_string(accountInfo.userId_) + "\n");
144         result.append("          OhosAccount name     : ");
145         result.append(AnonymizeNameStr(accountInfo.ohosAccountInfo_.name_) + "\n");
146         result.append("          OhosAccount uid      : ");
147         result.append(AnonymizeUidStr(accountInfo.ohosAccountInfo_.uid_) + "\n");
148         result.append("          OhosAccount status   : ");
149         result.append(std::to_string(accountInfo.ohosAccountInfo_.status_) + "\n");
150         result.append("          OhosAccount bind time: ");
151         result.append(std::to_string(accountInfo.bindTime_) + "\n");
152     }
153 }
154 
ShowOsAccountInfo(std::string & result) const155 void AccountDumpHelper::ShowOsAccountInfo(std::string& result) const
156 {
157     if (osAccountMgrService_ == nullptr) {
158         result.append(SYSTEM_ERROR + "service ptr is null!\n");
159         ACCOUNT_LOGE("service ptr is null!");
160         return;
161     }
162 
163     std::vector<std::string> states;
164     ErrCode ret = osAccountMgrService_->DumpOsAccountInfo(states);
165     if (ret != ERR_OK) {
166         result.append("Cannot query os account list, error code ");
167         result.append(std::to_string(ret));
168         return;
169     }
170 
171     if (states.empty()) {
172         result.append(SYSTEM_ERROR + "os account list empty.\n");
173         return;
174     }
175 
176     result.append("OsAccount info:\n");
177     for (size_t i = 0; i < states.size(); ++i) {
178         result.append("    " + states[i] + "\n");
179     }
180 }
181 
ProcessOneParameter(const std::string & arg,std::string & result) const182 void AccountDumpHelper::ProcessOneParameter(const std::string& arg, std::string& result) const
183 {
184     if (arg == ARGS_HELP) {
185         ShowHelp(result);
186     } else if (arg == ARGS_OHOS_ACCOUNT_INFOS) {
187         ShowOhosAccountInfo(result);
188     } else if (arg == ARGS_OS_ACCOUNT_INFOS) {
189         ShowOsAccountInfo(result);
190     } else if (arg == ARGS_SHOW_LOG_LEVEL) {
191         auto logLevel = static_cast<std::int32_t>(AccountLogWrapper::GetLogLevel());
192         result.append("Current Log Level: " + std::to_string(logLevel) + "\n");
193     } else if (arg == ARGS_DUMP_TIME_INFO) {
194         PerfStat::GetInstance().Dump(result);
195     } else {
196         ShowHelp(result);
197     }
198 }
199 
SetLogLevel(const std::string & levelStr,std::string & result) const200 void AccountDumpHelper::SetLogLevel(const std::string& levelStr, std::string& result) const
201 {
202     if (!regex_match(levelStr, std::regex("^\\-?\\d+$"))) {
203         ACCOUNT_LOGE("Invalid format of log level");
204         result.append("Invalid format of log level\n");
205         return;
206     }
207     auto level = std::stoi(levelStr);
208     if ((level < static_cast<std::int32_t>(AccountLogLevel::DEBUG)) ||
209         (level > static_cast<std::int32_t>(AccountLogLevel::FATAL))) {
210         result.append("Invalid logLevel\n");
211     } else {
212         AccountLogLevel logLevel = static_cast<AccountLogLevel>(level);
213         AccountLogWrapper::SetLogLevel(logLevel);
214         result.append("Set logLevel success\n");
215     }
216 }
217 
ProcessTwoParameter(const std::string & arg1,const std::string & arg2,std::string & result) const218 void AccountDumpHelper::ProcessTwoParameter(const std::string& arg1, const std::string& arg2, std::string& result) const
219 {
220     if (arg1 == ARGS_SET_LOG_LEVEL) {
221         SetLogLevel(arg2, result);
222     } else {
223         ShowHelp(result);
224     }
225 }
226 } // namespace AccountSA
227 } // namespace OHOS
228