1 /*
2 * Copyright (c) 2022-2023 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 "enterprise_device_mgr_proxy.h"
17
18 #include <iservice_registry.h>
19 #include <string_ex.h>
20
21 #include "admin_type.h"
22 #include "edm_errors.h"
23 #include "edm_load_manager.h"
24 #include "edm_log.h"
25 #include "edm_sys_manager.h"
26 #include "func_code.h"
27 #include "parameters.h"
28 #include "system_ability_definition.h"
29
30 namespace OHOS {
31 namespace EDM {
32 std::shared_ptr<EnterpriseDeviceMgrProxy> EnterpriseDeviceMgrProxy::instance_ = nullptr;
33 std::mutex EnterpriseDeviceMgrProxy::mutexLock_;
34 const std::u16string DESCRIPTOR = u"ohos.edm.IEnterpriseDeviceMgr";
35 const uint32_t GET_ENABLE_ADMIN = 5;
36
GetInstance()37 std::shared_ptr<EnterpriseDeviceMgrProxy> EnterpriseDeviceMgrProxy::GetInstance()
38 {
39 if (instance_ == nullptr) {
40 std::lock_guard<std::mutex> lock(mutexLock_);
41 if (instance_ == nullptr) {
42 std::shared_ptr<EnterpriseDeviceMgrProxy> temp = std::make_shared<EnterpriseDeviceMgrProxy>();
43 instance_ = temp;
44 }
45 }
46 return instance_;
47 }
48
DestroyInstance()49 void EnterpriseDeviceMgrProxy::DestroyInstance()
50 {
51 std::lock_guard<std::mutex> lock(mutexLock_);
52 if (instance_ != nullptr) {
53 instance_.reset();
54 instance_ = nullptr;
55 }
56 }
57
IsEdmEnabled()58 bool EnterpriseDeviceMgrProxy::IsEdmEnabled()
59 {
60 std::string edmParaValue = system::GetParameter("persist.edm.edm_enable", "false");
61 EDMLOGD("EnterpriseDeviceMgrProxy::GetParameter %{public}s", edmParaValue.c_str());
62 return edmParaValue == "true";
63 }
64
EnableAdmin(AppExecFwk::ElementName & admin,EntInfo & entInfo,AdminType type,int32_t userId)65 ErrCode EnterpriseDeviceMgrProxy::EnableAdmin(AppExecFwk::ElementName &admin, EntInfo &entInfo, AdminType type,
66 int32_t userId)
67 {
68 EDMLOGD("EnterpriseDeviceMgrProxy::EnableAdmin");
69 sptr<IRemoteObject> remote = LoadAndGetEdmService();
70 if (!remote) {
71 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
72 }
73 MessageParcel data;
74 MessageParcel reply;
75 MessageOption option;
76 data.WriteInterfaceToken(DESCRIPTOR);
77 data.WriteParcelable(&admin);
78 data.WriteParcelable(&entInfo);
79 data.WriteInt32(static_cast<int32_t>(type));
80 data.WriteInt32(userId);
81 ErrCode res = remote->SendRequest(EdmInterfaceCode::ADD_DEVICE_ADMIN, data, reply, option);
82 if (FAILED(res)) {
83 EDMLOGE("EnterpriseDeviceMgrProxy:EnableAdmin send request fail. %{public}d", res);
84 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
85 }
86 int32_t resCode = ERR_OK;
87 if (!reply.ReadInt32(resCode) || FAILED(resCode)) {
88 EDMLOGW("EnterpriseDeviceMgrProxy:EnableAdmin get result code fail. %{public}d", resCode);
89 return resCode;
90 }
91 return ERR_OK;
92 }
93
DisableAdmin(AppExecFwk::ElementName & admin,int32_t userId)94 ErrCode EnterpriseDeviceMgrProxy::DisableAdmin(AppExecFwk::ElementName &admin, int32_t userId)
95 {
96 EDMLOGD("EnterpriseDeviceMgrProxy::DisableAdmin");
97 if (!IsEdmEnabled()) {
98 return EdmReturnErrCode::DISABLE_ADMIN_FAILED;
99 }
100 sptr<IRemoteObject> remote = LoadAndGetEdmService();
101 if (!remote) {
102 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
103 }
104 MessageParcel data;
105 MessageParcel reply;
106 MessageOption option;
107 data.WriteInterfaceToken(DESCRIPTOR);
108 data.WriteParcelable(&admin);
109 data.WriteInt32(userId);
110 ErrCode res = remote->SendRequest(EdmInterfaceCode::REMOVE_DEVICE_ADMIN, data, reply, option);
111 if (FAILED(res)) {
112 EDMLOGE("EnterpriseDeviceMgrProxy:DisableAdmin send request fail. %{public}d", res);
113 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
114 }
115 int32_t resCode = ERR_OK;
116 if (!reply.ReadInt32(resCode) || FAILED(resCode)) {
117 EDMLOGW("EnterpriseDeviceMgrProxy:DisableAdmin get result code fail. %{public}d", resCode);
118 return resCode;
119 }
120 return ERR_OK;
121 }
122
DisableSuperAdmin(const std::string & bundleName)123 ErrCode EnterpriseDeviceMgrProxy::DisableSuperAdmin(const std::string &bundleName)
124 {
125 EDMLOGD("EnterpriseDeviceMgrProxy::DisableSuperAdmin");
126 if (!IsEdmEnabled()) {
127 return EdmReturnErrCode::DISABLE_ADMIN_FAILED;
128 }
129 sptr<IRemoteObject> remote = LoadAndGetEdmService();
130 if (!remote) {
131 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
132 }
133 MessageParcel data;
134 MessageParcel reply;
135 MessageOption option;
136 data.WriteInterfaceToken(DESCRIPTOR);
137 data.WriteString(bundleName);
138 ErrCode res = remote->SendRequest(EdmInterfaceCode::REMOVE_SUPER_ADMIN, data, reply, option);
139 if (FAILED(res)) {
140 EDMLOGE("EnterpriseDeviceMgrProxy:DisableSuperAdmin send request fail. %{public}d", res);
141 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
142 }
143 int32_t resCode = ERR_OK;
144 if (!reply.ReadInt32(resCode) || FAILED(resCode)) {
145 EDMLOGW("EnterpriseDeviceMgrProxy:DisableSuperAdmin get result code fail. %{public}d", resCode);
146 return resCode;
147 }
148 return ERR_OK;
149 }
150
GetEnabledAdmin(AdminType type,std::vector<std::string> & enabledAdminList)151 ErrCode EnterpriseDeviceMgrProxy::GetEnabledAdmin(AdminType type, std::vector<std::string> &enabledAdminList)
152 {
153 EDMLOGD("EnterpriseDeviceMgrProxy::GetEnabledAdmin");
154 if (!IsEdmEnabled()) {
155 return EdmReturnErrCode::ADMIN_INACTIVE;
156 }
157 sptr<IRemoteObject> remote = LoadAndGetEdmService();
158 if (!remote) {
159 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
160 }
161 MessageParcel data;
162 MessageParcel reply;
163 MessageOption option;
164 data.WriteInterfaceToken(DESCRIPTOR);
165 data.WriteInt32(static_cast<int32_t>(type));
166 ErrCode res = remote->SendRequest(EdmInterfaceCode::GET_ENABLED_ADMIN, data, reply, option);
167 if (FAILED(res)) {
168 EDMLOGE("EnterpriseDeviceMgrProxy:GetEnabledAdmin send request fail. %{public}d", res);
169 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
170 }
171 int32_t resCode = ERR_OK;
172 if (!reply.ReadInt32(resCode) || FAILED(resCode)) {
173 EDMLOGW("EnterpriseDeviceMgrProxy:GetEnabledAdmin get result code fail. %{public}d", resCode);
174 return resCode;
175 }
176 reply.ReadStringVector(&enabledAdminList);
177 return ERR_OK;
178 }
179
GetEnterpriseInfo(AppExecFwk::ElementName & admin,EntInfo & entInfo)180 ErrCode EnterpriseDeviceMgrProxy::GetEnterpriseInfo(AppExecFwk::ElementName &admin, EntInfo &entInfo)
181 {
182 EDMLOGD("EnterpriseDeviceMgrProxy::GetEnterpriseInfo");
183 if (!IsEdmEnabled()) {
184 return EdmReturnErrCode::ADMIN_INACTIVE;
185 }
186 sptr<IRemoteObject> remote = LoadAndGetEdmService();
187 if (!remote) {
188 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
189 }
190 MessageParcel data;
191 MessageParcel reply;
192 MessageOption option;
193 data.WriteInterfaceToken(DESCRIPTOR);
194 data.WriteParcelable(&admin);
195 ErrCode res = remote->SendRequest(EdmInterfaceCode::GET_ENT_INFO, data, reply, option);
196 if (FAILED(res)) {
197 EDMLOGE("EnterpriseDeviceMgrProxy:GetEnterpriseInfo send request fail. %{public}d", res);
198 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
199 }
200 int32_t resCode = ERR_OK;
201 if (!reply.ReadInt32(resCode) || FAILED(resCode)) {
202 EDMLOGW("EnterpriseDeviceMgrProxy:GetEnterpriseInfo get result code fail. %{public}d", resCode);
203 return resCode;
204 }
205 std::unique_ptr<EntInfo> info(reply.ReadParcelable<EntInfo>());
206 if (!info) {
207 EDMLOGE("EnterpriseDeviceMgrProxy::GetEnterpriseInfo read parcel fail");
208 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
209 }
210 entInfo = *info;
211 return ERR_OK;
212 }
213
SetEnterpriseInfo(AppExecFwk::ElementName & admin,EntInfo & entInfo)214 ErrCode EnterpriseDeviceMgrProxy::SetEnterpriseInfo(AppExecFwk::ElementName &admin, EntInfo &entInfo)
215 {
216 EDMLOGD("EnterpriseDeviceMgrProxy::SetEnterpriseInfo");
217 if (!IsEdmEnabled()) {
218 return EdmReturnErrCode::ADMIN_INACTIVE;
219 }
220 sptr<IRemoteObject> remote = LoadAndGetEdmService();
221 if (!remote) {
222 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
223 }
224 MessageParcel data;
225 MessageParcel reply;
226 MessageOption option;
227 data.WriteInterfaceToken(DESCRIPTOR);
228 data.WriteParcelable(&admin);
229 data.WriteParcelable(&entInfo);
230 ErrCode res = remote->SendRequest(EdmInterfaceCode::SET_ENT_INFO, data, reply, option);
231 if (FAILED(res)) {
232 EDMLOGE("EnterpriseDeviceMgrProxy:SetEnterpriseInfo send request fail. %{public}d", res);
233 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
234 }
235 int32_t resCode = ERR_OK;
236 if (!reply.ReadInt32(resCode) || FAILED(resCode)) {
237 EDMLOGW("EnterpriseDeviceMgrProxy:SetEnterpriseInfo get result code fail. %{public}d", resCode);
238 return resCode;
239 }
240 return ERR_OK;
241 }
242
HandleManagedEvent(const AppExecFwk::ElementName & admin,const std::vector<uint32_t> & events,bool subscribe)243 ErrCode EnterpriseDeviceMgrProxy::HandleManagedEvent(const AppExecFwk::ElementName &admin,
244 const std::vector<uint32_t> &events, bool subscribe)
245 {
246 EDMLOGD("EnterpriseDeviceMgrProxy::SubscribeManagedEvent: %{public}d", subscribe);
247 if (!IsEdmEnabled()) {
248 return EdmReturnErrCode::ADMIN_INACTIVE;
249 }
250 sptr<IRemoteObject> remote = LoadAndGetEdmService();
251 if (!remote) {
252 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
253 }
254 MessageParcel data;
255 MessageParcel reply;
256 MessageOption option;
257 data.WriteInterfaceToken(DESCRIPTOR);
258 data.WriteParcelable(&admin);
259 data.WriteUInt32Vector(events);
260 uint32_t policyCode = EdmInterfaceCode::SUBSCRIBE_MANAGED_EVENT;
261 if (!subscribe) {
262 policyCode = EdmInterfaceCode::UNSUBSCRIBE_MANAGED_EVENT;
263 }
264 ErrCode res = remote->SendRequest(policyCode, data, reply, option);
265 if (FAILED(res)) {
266 EDMLOGE("EnterpriseDeviceMgrProxy:SubscribeManagedEvent send request fail. %{public}d", res);
267 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
268 }
269 int32_t retCode;
270 reply.ReadInt32(retCode);
271 return retCode;
272 }
273
IsSuperAdmin(const std::string & bundleName,bool & result)274 ErrCode EnterpriseDeviceMgrProxy::IsSuperAdmin(const std::string &bundleName, bool &result)
275 {
276 EDMLOGD("EnterpriseDeviceMgrProxy::IsSuperAdmin");
277 result = false;
278 if (!IsEdmEnabled()) {
279 return ERR_OK;
280 }
281 sptr<IRemoteObject> remote = LoadAndGetEdmService();
282 if (!remote) {
283 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
284 }
285 MessageParcel data;
286 MessageParcel reply;
287 MessageOption option;
288 data.WriteInterfaceToken(DESCRIPTOR);
289 data.WriteString(bundleName);
290 ErrCode res = remote->SendRequest(EdmInterfaceCode::IS_SUPER_ADMIN, data, reply, option);
291 if (FAILED(res)) {
292 EDMLOGE("EnterpriseDeviceMgrProxy:IsSuperAdmin send request fail. %{public}d", res);
293 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
294 }
295 int32_t resCode = ERR_OK;
296 if (!reply.ReadInt32(resCode) || FAILED(resCode)) {
297 EDMLOGW("EnterpriseDeviceMgrProxy:SetEnterpriseInfo get result code fail. %{public}d", resCode);
298 return resCode;
299 }
300 reply.ReadBool(result);
301 EDMLOGD("EnterpriseDeviceMgrProxy::IsSuperAdmin ret %{public}d", result);
302 return ERR_OK;
303 }
304
IsAdminEnabled(AppExecFwk::ElementName & admin,int32_t userId,bool & result)305 ErrCode EnterpriseDeviceMgrProxy::IsAdminEnabled(AppExecFwk::ElementName &admin, int32_t userId, bool &result)
306 {
307 EDMLOGD("EnterpriseDeviceMgrProxy::IsAdminEnabled");
308 result = false;
309 if (!IsEdmEnabled()) {
310 return ERR_OK;
311 }
312 sptr<IRemoteObject> remote = LoadAndGetEdmService();
313 if (!remote) {
314 return false;
315 }
316 MessageParcel data;
317 MessageParcel reply;
318 MessageOption option;
319 data.WriteInterfaceToken(DESCRIPTOR);
320 data.WriteParcelable(&admin);
321 data.WriteInt32(userId);
322 ErrCode res = remote->SendRequest(EdmInterfaceCode::IS_ADMIN_ENABLED, data, reply, option);
323 if (FAILED(res)) {
324 EDMLOGE("EnterpriseDeviceMgrProxy:IsAdminEnabled send request fail. %{public}d", res);
325 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
326 }
327 int32_t resCode = ERR_OK;
328 if (!reply.ReadInt32(resCode) || FAILED(resCode)) {
329 EDMLOGW("EnterpriseDeviceMgrProxy:IsAdminEnabled get result code fail. %{public}d", resCode);
330 return resCode;
331 }
332 reply.ReadBool(result);
333 EDMLOGD("EnterpriseDeviceMgrProxy::IsAdminEnabled ret %{public}d", result);
334 return ERR_OK;
335 }
336
IsPolicyDisabled(const AppExecFwk::ElementName * admin,int policyCode,bool & result)337 int32_t EnterpriseDeviceMgrProxy::IsPolicyDisabled(const AppExecFwk::ElementName *admin, int policyCode, bool &result)
338 {
339 MessageParcel data;
340 data.WriteInterfaceToken(DESCRIPTOR);
341 data.WriteInt32(WITHOUT_USERID);
342 if (admin != nullptr) {
343 data.WriteInt32(HAS_ADMIN);
344 data.WriteParcelable(admin);
345 } else {
346 if (!IsEdmEnabled()) {
347 result = false;
348 return ERR_OK;
349 }
350 data.WriteInt32(WITHOUT_ADMIN);
351 }
352 MessageParcel reply;
353 GetPolicy(policyCode, data, reply);
354 int32_t ret = ERR_INVALID_VALUE;
355 bool isSuccess = reply.ReadInt32(ret) && (ret == ERR_OK);
356 if (!isSuccess) {
357 EDMLOGE("IsPolicyDisabled:GetPolicy fail. %{public}d", ret);
358 return ret;
359 }
360 reply.ReadBool(result);
361 return ERR_OK;
362 }
363
HandleDevicePolicy(int32_t policyCode,MessageParcel & data)364 int32_t EnterpriseDeviceMgrProxy::HandleDevicePolicy(int32_t policyCode, MessageParcel &data)
365 {
366 MessageParcel reply;
367 return HandleDevicePolicy(policyCode, data, reply);
368 }
369
HandleDevicePolicy(int32_t policyCode,MessageParcel & data,MessageParcel & reply)370 int32_t EnterpriseDeviceMgrProxy::HandleDevicePolicy(int32_t policyCode, MessageParcel &data, MessageParcel &reply)
371 {
372 EDMLOGD("EnterpriseDeviceMgrProxy::HandleDevicePolicy");
373 if (!IsEdmEnabled()) {
374 return EdmReturnErrCode::ADMIN_INACTIVE;
375 }
376 sptr<IRemoteObject> remote = LoadAndGetEdmService();
377 if (!remote) {
378 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
379 }
380 MessageOption option;
381 EDMLOGD("EnterpriseDeviceMgrProxy::handleDevicePolicy::sendRequest %{public}d", policyCode);
382 ErrCode res = remote->SendRequest(policyCode, data, reply, option);
383 if (FAILED(res)) {
384 EDMLOGE("EnterpriseDeviceMgrProxy:HandleDevicePolicy send request fail. %{public}d", res);
385 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
386 }
387 int32_t ret = ERR_INVALID_VALUE;
388 reply.ReadInt32(ret);
389 return ret;
390 }
391
AuthorizeAdmin(const AppExecFwk::ElementName & admin,const std::string & bundleName)392 ErrCode EnterpriseDeviceMgrProxy::AuthorizeAdmin(const AppExecFwk::ElementName &admin, const std::string &bundleName)
393 {
394 EDMLOGD("EnterpriseDeviceMgrProxy::AuthorizeAdmin");
395 if (!IsEdmEnabled()) {
396 return EdmReturnErrCode::ADMIN_INACTIVE;
397 }
398 sptr<IRemoteObject> remote = LoadAndGetEdmService();
399 if (!remote) {
400 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
401 }
402 MessageParcel data;
403 MessageParcel reply;
404 MessageOption option;
405 data.WriteInterfaceToken(DESCRIPTOR);
406 data.WriteParcelable(&admin);
407 data.WriteString(bundleName);
408 ErrCode res = remote->SendRequest(EdmInterfaceCode::AUTHORIZE_ADMIN, data, reply, option);
409 if (FAILED(res)) {
410 EDMLOGE("EnterpriseDeviceMgrProxy:AuthorizeAdmin send request fail. %{public}d", res);
411 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
412 }
413 int32_t resCode = ERR_INVALID_VALUE;
414 if (!reply.ReadInt32(resCode) || FAILED(resCode)) {
415 EDMLOGW("EnterpriseDeviceMgrProxy:AuthorizeAdmin get result code fail. %{public}d", resCode);
416 return resCode;
417 }
418 return ERR_OK;
419 }
420
GetPolicyValue(AppExecFwk::ElementName * admin,int policyCode,std::string & policyData,int32_t userId)421 bool EnterpriseDeviceMgrProxy::GetPolicyValue(AppExecFwk::ElementName *admin, int policyCode, std::string &policyData,
422 int32_t userId)
423 {
424 MessageParcel reply;
425 if (!GetPolicyData(admin, policyCode, userId, reply)) {
426 return false;
427 }
428 reply.ReadString(policyData);
429 return true;
430 }
431
GetPolicyArray(AppExecFwk::ElementName * admin,int policyCode,std::vector<std::string> & policyData,int32_t userId)432 bool EnterpriseDeviceMgrProxy::GetPolicyArray(AppExecFwk::ElementName *admin, int policyCode,
433 std::vector<std::string> &policyData, int32_t userId)
434 {
435 MessageParcel reply;
436 if (!GetPolicyData(admin, policyCode, userId, reply)) {
437 return false;
438 }
439 int32_t size = reply.ReadInt32();
440 EDMLOGD("EnterpriseDeviceMgrProxy::GetPolicyArray size: %{public}d.", size);
441 return reply.ReadStringVector(&policyData);
442 }
443
GetPolicyMap(AppExecFwk::ElementName * admin,int policyCode,std::map<std::string,std::string> & policyData,int32_t userId)444 bool EnterpriseDeviceMgrProxy::GetPolicyMap(AppExecFwk::ElementName *admin, int policyCode,
445 std::map<std::string, std::string> &policyData, int32_t userId)
446 {
447 MessageParcel reply;
448 if (!GetPolicyData(admin, policyCode, userId, reply)) {
449 return false;
450 }
451 std::vector<std::string> keys;
452 if (!reply.ReadStringVector(&keys)) {
453 EDMLOGE("EnterpriseDeviceMgrProxy::read map keys fail.");
454 return false;
455 }
456 std::vector<std::string> values;
457 if (!reply.ReadStringVector(&values)) {
458 EDMLOGE("EnterpriseDeviceMgrProxy::read map values fail.");
459 return false;
460 }
461 if (keys.size() != values.size()) {
462 EDMLOGE("EnterpriseDeviceMgrProxy::read map fail.");
463 return false;
464 }
465 policyData.clear();
466 for (uint64_t i = 0; i < keys.size(); ++i) {
467 policyData.insert(std::make_pair(keys.at(i), values.at(i)));
468 }
469 return true;
470 }
471
GetPolicyData(AppExecFwk::ElementName * admin,int policyCode,int32_t userId,MessageParcel & reply)472 bool EnterpriseDeviceMgrProxy::GetPolicyData(AppExecFwk::ElementName *admin, int policyCode, int32_t userId,
473 MessageParcel &reply)
474 {
475 if (!IsEdmEnabled()) {
476 return false;
477 }
478 MessageParcel data;
479 data.WriteInterfaceToken(DESCRIPTOR);
480 data.WriteInt32(HAS_USERID);
481 data.WriteInt32(userId);
482 if (admin != nullptr) {
483 data.WriteInt32(HAS_ADMIN);
484 data.WriteParcelable(admin);
485 } else {
486 data.WriteInt32(WITHOUT_ADMIN);
487 }
488 int32_t ret = ERR_INVALID_VALUE;
489 return GetPolicy(policyCode, data, reply) && reply.ReadInt32(ret) && (ret == ERR_OK);
490 }
491
GetPolicy(int policyCode,MessageParcel & data,MessageParcel & reply)492 bool EnterpriseDeviceMgrProxy::GetPolicy(int policyCode, MessageParcel &data, MessageParcel &reply)
493 {
494 if (!IsEdmEnabled()) {
495 reply.WriteInt32(EdmReturnErrCode::ADMIN_INACTIVE);
496 return false;
497 }
498 if (policyCode < 0) {
499 EDMLOGE("EnterpriseDeviceMgrProxy:GetPolicy invalid policyCode:%{public}d", policyCode);
500 return false;
501 }
502 std::uint32_t funcCode = POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::GET, (std::uint32_t)policyCode);
503 sptr<IRemoteObject> remote = LoadAndGetEdmService();
504 if (!remote) {
505 reply.WriteInt32(EdmReturnErrCode::SYSTEM_ABNORMALLY);
506 return false;
507 }
508 MessageOption option;
509 ErrCode res = remote->SendRequest(funcCode, data, reply, option);
510 if (FAILED(res)) {
511 EDMLOGE("EnterpriseDeviceMgrProxy:GetPolicy send request fail.");
512 return false;
513 }
514 return true;
515 }
516
GetEnabledAdmins(std::vector<std::string> & enabledAdminList)517 void EnterpriseDeviceMgrProxy::GetEnabledAdmins(std::vector<std::string> &enabledAdminList)
518 {
519 GetEnabledAdmins(AdminType::NORMAL, enabledAdminList);
520 }
521
GetEnabledSuperAdmin(std::string & enabledAdmin)522 void EnterpriseDeviceMgrProxy::GetEnabledSuperAdmin(std::string &enabledAdmin)
523 {
524 std::vector<std::string> enabledAdminList;
525 GetEnabledAdmins(AdminType::ENT, enabledAdminList);
526 if (!enabledAdminList.empty()) {
527 enabledAdmin = enabledAdminList[0];
528 }
529 }
530
IsSuperAdminExist()531 bool EnterpriseDeviceMgrProxy::IsSuperAdminExist()
532 {
533 std::vector<std::string> enabledAdminList;
534 GetEnabledAdmins(AdminType::ENT, enabledAdminList);
535 return !enabledAdminList.empty();
536 }
537
LoadAndGetEdmService()538 sptr<IRemoteObject> EnterpriseDeviceMgrProxy::LoadAndGetEdmService()
539 {
540 std::lock_guard<std::mutex> lock(mutexLock_);
541 sptr<ISystemAbilityManager> sysAbilityMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
542 if (sysAbilityMgr == nullptr) {
543 EDMLOGE("EnterpriseDeviceMgrProxy::failed to get SystemAbilityManager");
544 return nullptr;
545 }
546 auto objectSA = sysAbilityMgr->CheckSystemAbility(ENTERPRISE_DEVICE_MANAGER_SA_ID);
547 if (objectSA == nullptr) {
548 EDMLOGI("EnterpriseDeviceMgrProxy::load sa from remote");
549 return EdmLoadManager::GetInstance().LoadAndGetEdmService();
550 }
551 return EdmSysManager::GetRemoteObjectOfSystemAbility(ENTERPRISE_DEVICE_MANAGER_SA_ID);
552 }
553
GetEnabledAdmins(AdminType type,std::vector<std::string> & enabledAdminList)554 void EnterpriseDeviceMgrProxy::GetEnabledAdmins(AdminType type, std::vector<std::string> &enabledAdminList)
555 {
556 if (!IsEdmEnabled()) {
557 return;
558 }
559 sptr<IRemoteObject> remote = LoadAndGetEdmService();
560 if (!remote) {
561 return;
562 }
563 MessageParcel data;
564 MessageParcel reply;
565 MessageOption option;
566 data.WriteInterfaceToken(DESCRIPTOR);
567 data.WriteInt32(static_cast<int32_t>(type));
568 ErrCode res = remote->SendRequest(GET_ENABLE_ADMIN, data, reply, option);
569 if (FAILED(res)) {
570 EDMLOGE("EnterpriseDeviceMgrProxy:GetEnabledAdmins send request fail.");
571 return;
572 }
573 int32_t resCode = ERR_OK;
574 if (!reply.ReadInt32(resCode) || FAILED(resCode)) {
575 EDMLOGE("EnterpriseDeviceMgrProxy:GetEnabledAdmins get result code fail.");
576 return;
577 }
578 std::vector<std::string> readArray;
579 reply.ReadStringVector(&readArray);
580 for (const std::string &item : readArray) {
581 enabledAdminList.push_back(item);
582 }
583 }
584
SetPolicyDisabled(const AppExecFwk::ElementName & admin,bool isDisabled,uint32_t policyCode)585 int32_t EnterpriseDeviceMgrProxy::SetPolicyDisabled(const AppExecFwk::ElementName &admin, bool isDisabled,
586 uint32_t policyCode)
587 {
588 MessageParcel data;
589 std::uint32_t funcCode = POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::SET, policyCode);
590 data.WriteInterfaceToken(DESCRIPTOR);
591 data.WriteInt32(WITHOUT_USERID);
592 data.WriteParcelable(&admin);
593 data.WriteBool(isDisabled);
594 return HandleDevicePolicy(funcCode, data);
595 }
596 } // namespace EDM
597 } // namespace OHOS