• 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_mgr_service.h"
17 #include <cerrno>
18 #include "account_dump_helper.h"
19 #include "account_log_wrapper.h"
20 #include "app_account_manager_service.h"
21 #include "datetime_ex.h"
22 #include "device_account_info.h"
23 #include "directory_ex.h"
24 #include "file_ex.h"
25 #include "hisysevent_adapter.h"
26 #include "hitrace_meter.h"
27 #include "if_system_ability_manager.h"
28 #include "ipc_skeleton.h"
29 #include "iservice_registry.h"
30 #include "perf_stat.h"
31 #include "string_ex.h"
32 #include "system_ability_definition.h"
33 #include "account_info.h"
34 
35 namespace OHOS {
36 namespace AccountSA {
37 namespace {
38 const bool REGISTER_RESULT =
39     SystemAbility::MakeAndRegisterAbility(&DelayedRefSingleton<AccountMgrService>::GetInstance());
40 const std::string DEVICE_OWNER_DIR = "/data/service/el1/public/account/0/";
CreateDeviceDir()41 void CreateDeviceDir()
42 {
43     if (!OHOS::FileExists(DEVICE_OWNER_DIR)) {
44         ACCOUNT_LOGI("Device owner dir not exist, create!");
45         if (!OHOS::ForceCreateDirectory(DEVICE_OWNER_DIR)) {
46             ACCOUNT_LOGW("Create device owner dir failure! errno %{public}d.", errno);
47             ReportOsAccountOperationFail(0, OPERATION_FORCE_CREATE_DIRECTORY, errno, DEVICE_OWNER_DIR);
48         } else {
49             if (!OHOS::ChangeModeDirectory(DEVICE_OWNER_DIR, S_IRWXU)) {
50                 ReportOsAccountOperationFail(0, OPERATION_CHANGE_MODE_DIRECTORY, errno, DEVICE_OWNER_DIR);
51                 ACCOUNT_LOGW("failed to create dir, path = %{public}s errno %{public}d.",
52                     DEVICE_OWNER_DIR.c_str(), errno);
53             }
54         }
55     }
56 }
57 }
58 IAccountContext *IAccountContext::instance_ = nullptr;
59 
AccountMgrService()60 AccountMgrService::AccountMgrService() : SystemAbility(SUBSYS_ACCOUNT_SYS_ABILITY_ID_BEGIN, true)
61 {
62     PerfStat::GetInstance().SetInstanceCreateTime(GetTickCount());
63 }
64 
~AccountMgrService()65 AccountMgrService::~AccountMgrService()
66 {}
67 
UpdateOhosAccountInfo(const std::string & accountName,const std::string & uid,const std::string & eventStr)68 bool AccountMgrService::UpdateOhosAccountInfo(
69     const std::string &accountName, const std::string &uid, const std::string &eventStr)
70 {
71     if (!ohosAccountMgr_->OhosAccountStateChange(accountName, uid, eventStr)) {
72         ACCOUNT_LOGE("Ohos account state change failed");
73         return false;
74     }
75 
76     return true;
77 }
78 
SetOhosAccountInfo(const OhosAccountInfo & ohosAccountInfo,const std::string & eventStr)79 std::int32_t AccountMgrService::SetOhosAccountInfo(const OhosAccountInfo &ohosAccountInfo,
80     const std::string &eventStr)
81 {
82     if (!ohosAccountMgr_->OhosAccountStateChange(ohosAccountInfo, eventStr)) {
83         ACCOUNT_LOGE("Ohos account state change failed");
84         return ERR_ACCOUNT_ZIDL_ACCOUNT_SERVICE_ERROR;
85     }
86 
87     return ERR_OK;
88 }
89 
QueryOhosAccountInfo(void)90 std::pair<bool, OhosAccountInfo> AccountMgrService::QueryOhosAccountInfo(void)
91 {
92     return QueryOhosAccountInfoByUserId(IPCSkeleton::GetCallingUid() / UID_TRANSFORM_DIVISOR);
93 }
94 
GetOhosAccountInfo(OhosAccountInfo & info)95 ErrCode AccountMgrService::GetOhosAccountInfo(OhosAccountInfo &info)
96 {
97     return GetOhosAccountInfoByUserId(IPCSkeleton::GetCallingUid() / UID_TRANSFORM_DIVISOR, info);
98 }
99 
GetOhosAccountInfoByUserId(int32_t userId,OhosAccountInfo & info)100 ErrCode AccountMgrService::GetOhosAccountInfoByUserId(int32_t userId, OhosAccountInfo &info)
101 {
102     AccountInfo accountInfo;
103     ErrCode ret = ohosAccountMgr_->GetAccountInfoByUserId(userId, accountInfo);
104     if (ret != ERR_OK) {
105         return ret;
106     }
107     info = accountInfo.ohosAccountInfo_;
108     return ERR_OK;
109 }
110 
QueryOhosAccountInfoByUserId(std::int32_t userId)111 std::pair<bool, OhosAccountInfo> AccountMgrService::QueryOhosAccountInfoByUserId(std::int32_t userId)
112 {
113     AccountInfo accountInfo;
114     ErrCode ret = ohosAccountMgr_->GetAccountInfoByUserId(userId, accountInfo);
115     bool flag = true;
116     if (ret != ERR_OK) {
117         flag = false;
118     }
119     return std::make_pair(flag, OhosAccountInfo(
120         accountInfo.ohosAccountInfo_.name_, accountInfo.ohosAccountInfo_.uid_, accountInfo.ohosAccountInfo_.status_));
121 }
122 
QueryDeviceAccountId(std::int32_t & accountId)123 std::int32_t AccountMgrService::QueryDeviceAccountId(std::int32_t &accountId)
124 {
125     const std::int32_t uid = IPCSkeleton::GetCallingUid();
126     accountId = uid / UID_TRANSFORM_DIVISOR;
127     return ERR_OK;
128 }
129 
GetAppAccountService()130 sptr<IRemoteObject> AccountMgrService::GetAppAccountService()
131 {
132     if (appAccountManagerService_ != nullptr) {
133         return appAccountManagerService_->AsObject();
134     }
135     return nullptr;
136 }
137 
GetOsAccountService()138 sptr<IRemoteObject> AccountMgrService::GetOsAccountService()
139 {
140     if (osAccountManagerService_ != nullptr) {
141         return osAccountManagerService_->AsObject();
142     }
143     return nullptr;
144 }
145 
GetAccountIAMService()146 sptr<IRemoteObject> AccountMgrService::GetAccountIAMService()
147 {
148 #ifdef HAS_USER_AUTH_PART
149     if (accountIAMService_ != nullptr) {
150         return accountIAMService_->AsObject();
151     }
152 #endif
153     return nullptr;
154 }
155 
GetDomainAccountService()156 sptr<IRemoteObject> AccountMgrService::GetDomainAccountService()
157 {
158     if (domainAccountMgrService_ != nullptr) {
159         return domainAccountMgrService_->AsObject();
160     }
161     return nullptr;
162 }
163 
IsServiceStarted(void) const164 bool AccountMgrService::IsServiceStarted(void) const
165 {
166     return (state_ == STATE_RUNNING);
167 }
168 
OnStart()169 void AccountMgrService::OnStart()
170 {
171     if (state_ == ServiceRunningState::STATE_RUNNING) {
172         ACCOUNT_LOGI("AccountMgrService has already started.");
173         return;
174     }
175 
176     UpdateTraceLabel();
177     StartTrace(HITRACE_TAG_ACCOUNT_MANAGER, "accountmgr service onstart");
178     CountTrace(HITRACE_TAG_ACCOUNT_MANAGER, "activeid", -1);
179 
180     PerfStat::GetInstance().SetInstanceStartTime(GetTickCount());
181     ACCOUNT_LOGI("start is triggered");
182     if (!Init()) {
183         ACCOUNT_LOGE("failed to init AccountMgrService");
184         FinishTrace(HITRACE_TAG_ACCOUNT_MANAGER);
185         return;
186     }
187     state_ = ServiceRunningState::STATE_RUNNING;
188 
189     // create and start basic accounts
190     osAccountManagerService_->CreateBasicAccounts();
191     ACCOUNT_LOGI("AccountMgrService::OnStart start service finished.");
192     FinishTrace(HITRACE_TAG_ACCOUNT_MANAGER);
193 }
194 
OnStop()195 void AccountMgrService::OnStop()
196 {
197     PerfStat::GetInstance().SetInstanceStopTime(GetTickCount());
198     ACCOUNT_LOGI("onstop is called");
199     IAccountContext::SetInstance(nullptr);
200     SelfClean();
201 }
202 
Init()203 bool AccountMgrService::Init()
204 {
205     if (state_ == ServiceRunningState::STATE_RUNNING) {
206         ACCOUNT_LOGW("Service is already running!");
207         return false;
208     }
209 
210     CreateDeviceDir();
211 
212     if (!registerToService_) {
213         if (!Publish(&DelayedRefSingleton<AccountMgrService>::GetInstance())) {
214             ReportServiceStartFail(ERR_ACCOUNT_MGR_ADD_TO_SA_ERROR, "Publish service failed!");
215             ACCOUNT_LOGE("AccountMgrService::Init Publish failed!");
216             return false;
217         }
218         registerToService_ = true;
219     }
220     PerfStat::GetInstance().SetInstanceInitTime(GetTickCount());
221     ohosAccountMgr_ = std::make_shared<OhosAccountManager>();
222     if (!ohosAccountMgr_->OnInitialize()) {
223         ACCOUNT_LOGE("Ohos account manager initialize failed");
224         ReportServiceStartFail(ERR_ACCOUNT_MGR_OHOS_MGR_INIT_ERROR, "OnInitialize failed!");
225         return false;
226     }
227 
228     IAccountContext::SetInstance(this);
229     appAccountManagerService_ = new (std::nothrow) AppAccountManagerService();
230     if (appAccountManagerService_ == nullptr) {
231         ReportServiceStartFail(ERR_ACCOUNT_COMMON_INSUFFICIENT_MEMORY_ERROR,
232             "Insufficient memory to create app account manager service");
233         ACCOUNT_LOGE("memory alloc failed for appAccountManagerService!");
234         return false;
235     }
236     osAccountManagerService_ = new (std::nothrow) OsAccountManagerService();
237     if (osAccountManagerService_ == nullptr) {
238         ACCOUNT_LOGE("memory alloc failed for osAccountManagerService_!");
239         appAccountManagerService_ = nullptr;
240         ReportServiceStartFail(ERR_ACCOUNT_COMMON_INSUFFICIENT_MEMORY_ERROR,
241             "Insufficient memory to create os account manager service");
242         return false;
243     }
244     if ((!CreateIAMService()) || (!CreateDomainService())) {
245         appAccountManagerService_ = nullptr;
246         osAccountManagerService_ = nullptr;
247 #ifdef HAS_USER_AUTH_PART
248         accountIAMService_ = nullptr;
249 #endif
250         return false;
251     }
252     dumpHelper_ = std::make_unique<AccountDumpHelper>(ohosAccountMgr_, osAccountManagerService_.GetRefPtr());
253     ACCOUNT_LOGI("init end success");
254     return true;
255 }
256 
CreateIAMService()257 bool AccountMgrService::CreateIAMService()
258 {
259 #ifdef HAS_USER_AUTH_PART
260     accountIAMService_ = new (std::nothrow) AccountIAMService();
261     if (accountIAMService_ == nullptr) {
262         ACCOUNT_LOGE("memory alloc for AccountIAMService failed!");
263         ReportServiceStartFail(ERR_ACCOUNT_COMMON_INSUFFICIENT_MEMORY_ERROR,
264             "Insufficient memory to create account iam service");
265         return false;
266     }
267 #endif
268     return true;
269 }
270 
CreateDomainService()271 bool AccountMgrService::CreateDomainService()
272 {
273     domainAccountMgrService_ = new (std::nothrow) DomainAccountManagerService();
274     if (domainAccountMgrService_ == nullptr) {
275         ACCOUNT_LOGE("memory alloc for DomainAccountManagerService failed!");
276         ReportServiceStartFail(ERR_ACCOUNT_COMMON_INSUFFICIENT_MEMORY_ERROR,
277             "Insufficient memory to create domain account manager service");
278         return false;
279     }
280     return true;
281 }
282 
Dump(std::int32_t fd,const std::vector<std::u16string> & args)283 std::int32_t AccountMgrService::Dump(std::int32_t fd, const std::vector<std::u16string> &args)
284 {
285     if (fd < 0) {
286         ACCOUNT_LOGE("dump fd invalid");
287         return ERR_ACCOUNT_MGR_DUMP_ERROR;
288     }
289 
290     if (dumpHelper_ == nullptr) {
291         ACCOUNT_LOGE("dumpHelper_ is nullptr!");
292         return ERR_ACCOUNT_MGR_DUMP_ERROR;
293     }
294 
295     std::vector<std::string> argsInStr;
296     std::transform(args.begin(), args.end(), std::back_inserter(argsInStr),
297         [](const auto &arg) { return Str16ToStr8(arg); });
298 
299     std::string result;
300     dumpHelper_->Dump(argsInStr, result);
301     std::int32_t ret = dprintf(fd, "%s", result.c_str());
302     if (ret < 0) {
303         ACCOUNT_LOGE("dprintf to dump fd failed");
304         return ERR_ACCOUNT_MGR_DUMP_ERROR;
305     }
306     return ERR_OK;
307 }
308 
SelfClean()309 void AccountMgrService::SelfClean()
310 {
311     state_ = ServiceRunningState::STATE_NOT_START;
312     registerToService_ = false;
313     ACCOUNT_LOGI("self-clean finished");
314 }
315 
HandleNotificationEvents(const std::string & eventStr)316 void AccountMgrService::HandleNotificationEvents(const std::string &eventStr)
317 {
318     if (state_ == ServiceRunningState::STATE_NOT_START) {
319         ACCOUNT_LOGW("service not running for handling event: %{public}s", eventStr.c_str());
320         return;
321     }
322 }
323 }  // namespace AccountSA
324 }  // namespace OHOS