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 #include "os_account_standard_interface.h"
16
17 #include<thread>
18
19 #include "ability_manager_client.h"
20 #include "account_log_wrapper.h"
21 #include "bundle_mgr_interface.h"
22 #include "common_event_manager.h"
23 #include "common_event_support.h"
24 #include "datetime_ex.h"
25 #include "if_system_ability_manager.h"
26 #include "iservice_registry.h"
27 #include "istorage_manager.h"
28 #include "os_account_constants.h"
29 #include "os_account_delete_user_iam_callback.h"
30 #include "os_account_stop_user_callback.h"
31 #include "storage_manager.h"
32 #include "storage_manager_proxy.h"
33 #include "system_ability_definition.h"
34 #include "useridm_client.h"
35 #include "want.h"
36
37 namespace OHOS {
38 namespace AccountSA {
SendToAMSAccountStart(OsAccountInfo & osAccountInfo)39 ErrCode OsAccountStandardInterface::SendToAMSAccountStart(OsAccountInfo &osAccountInfo)
40 {
41 ACCOUNT_LOGI("OsAccountStandardInterface SendToAMSAccountStart start");
42 ErrCode code = AAFwk::AbilityManagerClient::GetInstance()->StartUser(osAccountInfo.GetLocalId());
43 if (code != ERR_OK) {
44 ACCOUNT_LOGE("failed to AbilityManagerClient start errcode is %{public}d", code);
45 return ERR_OSACCOUNT_SERVICE_INTERFACE_TO_AM_ACCOUNT_START_ERROR;
46 }
47 ACCOUNT_LOGI("send AM to start is ok");
48 return ERR_OK;
49 }
50
SendToAMSAccountStop(OsAccountInfo & osAccountInfo)51 ErrCode OsAccountStandardInterface::SendToAMSAccountStop(OsAccountInfo &osAccountInfo)
52 {
53 ACCOUNT_LOGI("OsAccountStandardInterface SendToAMSAccountStop stop");
54 sptr<OsAccountStopUserCallback> osAccountStopUserCallback = new (std::nothrow) OsAccountStopUserCallback();
55 if (osAccountStopUserCallback == nullptr) {
56 ACCOUNT_LOGE("alloc memory for stop user callback failed!");
57 return ERR_ACCOUNT_COMMON_INSUFFICIENT_MEMORY_ERROR;
58 }
59
60 ErrCode code =
61 AAFwk::AbilityManagerClient::GetInstance()->StopUser(osAccountInfo.GetLocalId(), osAccountStopUserCallback);
62 if (code != ERR_OK) {
63 ACCOUNT_LOGE("failed to AbilityManagerClient stop errcode is %{public}d", code);
64 return ERR_OSACCOUNT_SERVICE_INTERFACE_TO_AM_ACCOUNT_START_ERROR;
65 }
66 struct tm startTime = {0};
67 struct tm nowTime = {0};
68 OHOS::GetSystemCurrentTime(&startTime);
69 OHOS::GetSystemCurrentTime(&nowTime);
70 while (OHOS::GetSecondsBetween(startTime, nowTime) < Constants::TIME_WAIT_TIME_OUT &&
71 !osAccountStopUserCallback->isCallBackOk_) {
72 std::this_thread::sleep_for(std::chrono::milliseconds(Constants::WAIT_ONE_TIME));
73 OHOS::GetSystemCurrentTime(&nowTime);
74 }
75 if (!osAccountStopUserCallback->isReaturnOk_) {
76 ACCOUNT_LOGE("failed to AbilityManagerClient stop in call back");
77 return ERR_OSACCOUNT_SERVICE_INTERFACE_TO_AM_ACCOUNT_START_ERROR;
78 }
79 ACCOUNT_LOGI("send AM to stop is ok");
80 return ERR_OK;
81 }
82
SendToBMSAccountCreate(OsAccountInfo & osAccountInfo)83 ErrCode OsAccountStandardInterface::SendToBMSAccountCreate(OsAccountInfo &osAccountInfo)
84 {
85 ACCOUNT_LOGI("OsAccountStandardInterface SendToBMSAccountCreate start");
86 sptr<ISystemAbilityManager> systemAbilityManager =
87 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
88 if (!systemAbilityManager) {
89 ACCOUNT_LOGE("failed to get system ability mgr.");
90 return ERR_OSACCOUNT_SERVICE_INTERFACE_TO_BM_ACCOUNT_CREATE_ERROR;
91 }
92
93 sptr<IRemoteObject> remoteObject = systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
94 if (!remoteObject) {
95 ACCOUNT_LOGE("failed to get bundle manager service.");
96 return ERR_OSACCOUNT_SERVICE_INTERFACE_TO_BM_ACCOUNT_CREATE_ERROR;
97 }
98
99 auto bunduleMgrProxy = iface_cast<OHOS::AppExecFwk::IBundleMgr>(remoteObject);
100 if (!bunduleMgrProxy) {
101 ACCOUNT_LOGE("failed to get bunduleMgrProxy");
102 return ERR_OSACCOUNT_SERVICE_INTERFACE_TO_BM_ACCOUNT_CREATE_ERROR;
103 }
104 auto bunduleUserMgrProxy = bunduleMgrProxy->GetBundleUserMgr();
105 if (!bunduleUserMgrProxy) {
106 ACCOUNT_LOGE("failed to get bunduleUserMgrProxy");
107 return ERR_OSACCOUNT_SERVICE_INTERFACE_TO_BM_ACCOUNT_CREATE_ERROR;
108 }
109 bunduleUserMgrProxy->CreateNewUser(osAccountInfo.GetLocalId());
110 ACCOUNT_LOGI("call bm to create user ok");
111 return ERR_OK;
112 }
113
SendToBMSAccountDelete(OsAccountInfo & osAccountInfo)114 ErrCode OsAccountStandardInterface::SendToBMSAccountDelete(OsAccountInfo &osAccountInfo)
115 {
116 ACCOUNT_LOGI("OsAccountStandardInterface SendToBMSAccountDelete start");
117 sptr<ISystemAbilityManager> systemAbilityManager =
118 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
119 if (!systemAbilityManager) {
120 ACCOUNT_LOGE("failed to get system ability mgr.");
121 return ERR_OSACCOUNT_SERVICE_INTERFACE_TO_BM_ACCOUNT_DELETE_ERROR;
122 }
123
124 sptr<IRemoteObject> remoteObject = systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
125 if (!remoteObject) {
126 ACCOUNT_LOGE("failed to get bundle manager service.");
127 return ERR_OSACCOUNT_SERVICE_INTERFACE_TO_BM_ACCOUNT_DELETE_ERROR;
128 }
129
130 auto bunduleMgrProxy = iface_cast<OHOS::AppExecFwk::IBundleMgr>(remoteObject);
131 if (!bunduleMgrProxy) {
132 ACCOUNT_LOGE("failed to get bunduleMgrProxy");
133 return ERR_OSACCOUNT_SERVICE_INTERFACE_TO_BM_ACCOUNT_DELETE_ERROR;
134 }
135 auto bunduleUserMgrProxy = bunduleMgrProxy->GetBundleUserMgr();
136 if (!bunduleUserMgrProxy) {
137 ACCOUNT_LOGE("failed to get bunduleUserMgrProxy");
138 return ERR_OSACCOUNT_SERVICE_INTERFACE_TO_BM_ACCOUNT_DELETE_ERROR;
139 }
140 bunduleUserMgrProxy->RemoveUser(osAccountInfo.GetLocalId());
141 ACCOUNT_LOGI("call bm to remove user ok");
142 return ERR_OK;
143 }
144
SendToIAMAccountDelete(OsAccountInfo & osAccountInfo)145 ErrCode OsAccountStandardInterface::SendToIAMAccountDelete(OsAccountInfo &osAccountInfo)
146 {
147 std::shared_ptr<OsAccountDeleteUserIamCallback> callback = std::make_shared<OsAccountDeleteUserIamCallback>();
148 if (callback == nullptr) {
149 ACCOUNT_LOGE("get iam callback ptr failed! insufficient memory!");
150 return ERR_ACCOUNT_COMMON_INSUFFICIENT_MEMORY_ERROR;
151 }
152
153 int32_t ret = UserIAM::UserIDM::UserIDMClient::GetInstance().EnforceDelUser(osAccountInfo.GetLocalId(), callback);
154 if (ret != 0) {
155 ACCOUNT_LOGE("iam enforce delete user failed! error %{public}d", ret);
156 return ERR_OK; // do not return fail
157 }
158
159 // wait callback
160 struct tm startTime = {0};
161 struct tm nowTime = {0};
162 OHOS::GetSystemCurrentTime(&startTime);
163 OHOS::GetSystemCurrentTime(&nowTime);
164 while (OHOS::GetSecondsBetween(startTime, nowTime) < Constants::TIME_WAIT_TIME_OUT &&
165 !callback->isIamOnResultCallBack_) {
166 std::this_thread::sleep_for(std::chrono::milliseconds(Constants::WAIT_ONE_TIME));
167 OHOS::GetSystemCurrentTime(&nowTime);
168 }
169
170 if (!callback->isIamOnResultCallBack_) {
171 ACCOUNT_LOGE("iam did not call back! timeout!");
172 return ERR_OK; // do not return fail
173 }
174 ACCOUNT_LOGI("send to iam account delete and get callback succeed!");
175 return ERR_OK;
176 }
177
SendToCESAccountCreate(OsAccountInfo & osAccountInfo)178 void OsAccountStandardInterface::SendToCESAccountCreate(OsAccountInfo &osAccountInfo)
179 {
180 int osAccountID = osAccountInfo.GetLocalId();
181 OHOS::AAFwk::Want want;
182 want.SetAction(OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_USER_ADDED);
183 OHOS::EventFwk::CommonEventData data;
184 data.SetCode(osAccountID);
185 data.SetWant(want);
186 if (!OHOS::EventFwk::CommonEventManager::PublishCommonEvent(data)) {
187 ACCOUNT_LOGE("PublishCommonEvent for create account %{public}d failed!", osAccountID);
188 } else {
189 ACCOUNT_LOGI("PublishCommonEvent for create account %{public}d succeed!", osAccountID);
190 }
191 }
192
SendToCESAccountDelete(OsAccountInfo & osAccountInfo)193 void OsAccountStandardInterface::SendToCESAccountDelete(OsAccountInfo &osAccountInfo)
194 {
195 int osAccountID = osAccountInfo.GetLocalId();
196 OHOS::AAFwk::Want want;
197 want.SetAction(OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_USER_REMOVED);
198 OHOS::EventFwk::CommonEventData data;
199 data.SetCode(osAccountID);
200 data.SetWant(want);
201 if (!OHOS::EventFwk::CommonEventManager::PublishCommonEvent(data)) {
202 ACCOUNT_LOGE("PublishCommonEvent for delete account %{public}d failed!", osAccountID);
203 } else {
204 ACCOUNT_LOGI("PublishCommonEvent for delete account %{public}d succeed!", osAccountID);
205 }
206 }
207
SendToCESAccountSwitched(OsAccountInfo & osAccountInfo)208 void OsAccountStandardInterface::SendToCESAccountSwitched(OsAccountInfo &osAccountInfo)
209 {
210 int osAccountID = osAccountInfo.GetLocalId();
211 OHOS::AAFwk::Want want;
212 want.SetAction(OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED);
213 OHOS::EventFwk::CommonEventData data;
214 data.SetCode(osAccountID);
215 data.SetWant(want);
216 if (!OHOS::EventFwk::CommonEventManager::PublishCommonEvent(data)) {
217 ACCOUNT_LOGE("PublishCommonEvent for switched to account %{public}d failed!", osAccountID);
218 } else {
219 ACCOUNT_LOGI("PublishCommonEvent for switched to account %{public}d succeed!", osAccountID);
220 }
221 }
222
SendToStorageAccountCreate(OsAccountInfo & osAccountInfo)223 ErrCode OsAccountStandardInterface::SendToStorageAccountCreate(OsAccountInfo &osAccountInfo)
224 {
225 ACCOUNT_LOGI("OsAccountStandardInterface SendToStorageAccountCreate start");
226 auto systemAbilityManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
227 if (!systemAbilityManager) {
228 ACCOUNT_LOGE("failed to get system ability mgr.");
229 return ERR_OSACCOUNT_SERVICE_INTERFACE_TO_STORAGE_ACCOUNT_CREATE_ERROR;
230 }
231 auto remote = systemAbilityManager->GetSystemAbility(STORAGE_MANAGER_MANAGER_ID);
232 if (!remote) {
233 ACCOUNT_LOGE("failed to get STORAGE_MANAGER_MANAGER_ID service.");
234 return ERR_OSACCOUNT_SERVICE_INTERFACE_TO_STORAGE_ACCOUNT_CREATE_ERROR;
235 }
236 auto proxy = iface_cast<StorageManager::IStorageManager>(remote);
237 if (!proxy) {
238 ACCOUNT_LOGE("failed to get STORAGE_MANAGER_MANAGER_ID proxy.");
239 return ERR_OSACCOUNT_SERVICE_INTERFACE_TO_STORAGE_ACCOUNT_CREATE_ERROR;
240 }
241 int err = proxy->PrepareAddUser(osAccountInfo.GetLocalId(),
242 StorageManager::CRYPTO_FLAG_EL1 | StorageManager::CRYPTO_FLAG_EL2);
243 ACCOUNT_LOGI("PrepareAddUser code is %{public}d", err);
244 ACCOUNT_LOGI("OsAccountStandardInterface PrepareAddUser succeed");
245 return ERR_OK;
246 }
247
SendToStorageAccountRemove(OsAccountInfo & osAccountInfo)248 ErrCode OsAccountStandardInterface::SendToStorageAccountRemove(OsAccountInfo &osAccountInfo)
249 {
250 ACCOUNT_LOGI("OsAccountStandardInterface SendToStorageAccountRemove start");
251 auto systemAbilityManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
252 if (!systemAbilityManager) {
253 ACCOUNT_LOGE("failed to get system ability mgr.");
254 return ERR_OSACCOUNT_SERVICE_INTERFACE_TO_STORAGE_ACCOUNT_REMOVE_ERROR;
255 }
256 auto remote = systemAbilityManager->GetSystemAbility(STORAGE_MANAGER_MANAGER_ID);
257 if (!remote) {
258 ACCOUNT_LOGE("failed to get STORAGE_MANAGER_MANAGER_ID service.");
259 return ERR_OSACCOUNT_SERVICE_INTERFACE_TO_STORAGE_ACCOUNT_REMOVE_ERROR;
260 }
261 auto proxy = iface_cast<StorageManager::IStorageManager>(remote);
262 if (!proxy) {
263 ACCOUNT_LOGE("failed to get STORAGE_MANAGER_MANAGER_ID proxy.");
264 return ERR_OSACCOUNT_SERVICE_INTERFACE_TO_STORAGE_ACCOUNT_REMOVE_ERROR;
265 }
266 int err = proxy->RemoveUser(osAccountInfo.GetLocalId(),
267 StorageManager::CRYPTO_FLAG_EL1 | StorageManager::CRYPTO_FLAG_EL2);
268 ACCOUNT_LOGI("RemoveUser code is %{public}d", err);
269 ACCOUNT_LOGI("OsAccountStandardInterface RemoveUser succeed");
270 return ERR_OK;
271 }
272
SendToStorageAccountStart(OsAccountInfo & osAccountInfo)273 ErrCode OsAccountStandardInterface::SendToStorageAccountStart(OsAccountInfo &osAccountInfo)
274 {
275 ACCOUNT_LOGI("OsAccountStandardInterface SendToStorageAccountStart start");
276 auto systemAbilityManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
277 if (!systemAbilityManager) {
278 ACCOUNT_LOGE("failed to get system ability mgr.");
279 return ERR_OSACCOUNT_SERVICE_INTERFACE_TO_STORAGE_ACCOUNT_START_ERROR;
280 }
281 auto remote = systemAbilityManager->GetSystemAbility(STORAGE_MANAGER_MANAGER_ID);
282 if (!remote) {
283 ACCOUNT_LOGE("failed to get STORAGE_MANAGER_MANAGER_ID service.");
284 return ERR_OSACCOUNT_SERVICE_INTERFACE_TO_STORAGE_ACCOUNT_START_ERROR;
285 }
286 auto proxy = iface_cast<StorageManager::IStorageManager>(remote);
287 if (!proxy) {
288 ACCOUNT_LOGE("failed to get STORAGE_MANAGER_MANAGER_ID proxy.");
289 return ERR_OSACCOUNT_SERVICE_INTERFACE_TO_STORAGE_ACCOUNT_START_ERROR;
290 }
291 int err = proxy->PrepareStartUser(osAccountInfo.GetLocalId());
292 ACCOUNT_LOGI("PrepareStartUser code is %{public}d", err);
293 ACCOUNT_LOGI("OsAccountStandardInterface PrepareStartUser succeed");
294 return ERR_OK;
295 }
296
SendToStorageAccountStop(OsAccountInfo & osAccountInfo)297 ErrCode OsAccountStandardInterface::SendToStorageAccountStop(OsAccountInfo &osAccountInfo)
298 {
299 ACCOUNT_LOGI("OsAccountStandardInterface SendToStorageAccountStop start");
300 auto systemAbilityManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
301 if (!systemAbilityManager) {
302 ACCOUNT_LOGE("failed to get system ability mgr.");
303 return ERR_OSACCOUNT_SERVICE_INTERFACE_TO_STORAGE_ACCOUNT_STOP_ERROR;
304 }
305 auto remote = systemAbilityManager->GetSystemAbility(STORAGE_MANAGER_MANAGER_ID);
306 if (!remote) {
307 ACCOUNT_LOGE("failed to get STORAGE_MANAGER_MANAGER_ID service.");
308 return ERR_OSACCOUNT_SERVICE_INTERFACE_TO_STORAGE_ACCOUNT_STOP_ERROR;
309 }
310 auto proxy = iface_cast<StorageManager::IStorageManager>(remote);
311 if (!proxy) {
312 ACCOUNT_LOGE("failed to get STORAGE_MANAGER_MANAGER_ID proxy.");
313 return ERR_OSACCOUNT_SERVICE_INTERFACE_TO_STORAGE_ACCOUNT_STOP_ERROR;
314 }
315 int err = proxy->StopUser(osAccountInfo.GetLocalId());
316 ACCOUNT_LOGI("StopUser code is %{public}d", err);
317 ACCOUNT_LOGI("OsAccountStandardInterface StopUser succeed");
318 return ERR_OK;
319 }
320 } // namespace AccountSA
321 } // namespace OHOS
322