1 /*
2 * Copyright (c) 2022-2024 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 entInfo.Marshalling(data);
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_INVALID_VALUE;
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_INVALID_VALUE;
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_INVALID_VALUE;
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_INVALID_VALUE;
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_INVALID_VALUE;
201 if (!reply.ReadInt32(resCode) || FAILED(resCode)) {
202 EDMLOGW("EnterpriseDeviceMgrProxy:GetEnterpriseInfo get result code fail. %{public}d", resCode);
203 return resCode;
204 }
205 if (!EntInfo::Unmarshalling(reply, entInfo)) {
206 EDMLOGE("EnterpriseDeviceMgrProxy::GetEnterpriseInfo read parcel fail");
207 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
208 }
209 return ERR_OK;
210 }
211
SetEnterpriseInfo(AppExecFwk::ElementName & admin,EntInfo & entInfo)212 ErrCode EnterpriseDeviceMgrProxy::SetEnterpriseInfo(AppExecFwk::ElementName &admin, EntInfo &entInfo)
213 {
214 EDMLOGD("EnterpriseDeviceMgrProxy::SetEnterpriseInfo");
215 if (!IsEdmEnabled()) {
216 return EdmReturnErrCode::ADMIN_INACTIVE;
217 }
218 sptr<IRemoteObject> remote = LoadAndGetEdmService();
219 if (!remote) {
220 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
221 }
222 MessageParcel data;
223 MessageParcel reply;
224 MessageOption option;
225 data.WriteInterfaceToken(DESCRIPTOR);
226 data.WriteParcelable(&admin);
227 entInfo.Marshalling(data);
228 ErrCode res = remote->SendRequest(EdmInterfaceCode::SET_ENT_INFO, data, reply, option);
229 if (FAILED(res)) {
230 EDMLOGE("EnterpriseDeviceMgrProxy:SetEnterpriseInfo send request fail. %{public}d", res);
231 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
232 }
233 int32_t resCode = ERR_INVALID_VALUE;
234 if (!reply.ReadInt32(resCode) || FAILED(resCode)) {
235 EDMLOGW("EnterpriseDeviceMgrProxy:SetEnterpriseInfo get result code fail. %{public}d", resCode);
236 return resCode;
237 }
238 return ERR_OK;
239 }
240
HandleManagedEvent(const AppExecFwk::ElementName & admin,const std::vector<uint32_t> & events,bool subscribe)241 ErrCode EnterpriseDeviceMgrProxy::HandleManagedEvent(const AppExecFwk::ElementName &admin,
242 const std::vector<uint32_t> &events, bool subscribe)
243 {
244 EDMLOGD("EnterpriseDeviceMgrProxy::SubscribeManagedEvent: %{public}d", subscribe);
245 if (!IsEdmEnabled()) {
246 return EdmReturnErrCode::ADMIN_INACTIVE;
247 }
248 sptr<IRemoteObject> remote = LoadAndGetEdmService();
249 if (!remote) {
250 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
251 }
252 MessageParcel data;
253 MessageParcel reply;
254 MessageOption option;
255 data.WriteInterfaceToken(DESCRIPTOR);
256 data.WriteParcelable(&admin);
257 data.WriteUInt32Vector(events);
258 uint32_t policyCode = EdmInterfaceCode::SUBSCRIBE_MANAGED_EVENT;
259 if (!subscribe) {
260 policyCode = EdmInterfaceCode::UNSUBSCRIBE_MANAGED_EVENT;
261 }
262 ErrCode res = remote->SendRequest(policyCode, data, reply, option);
263 if (FAILED(res)) {
264 EDMLOGE("EnterpriseDeviceMgrProxy:SubscribeManagedEvent send request fail. %{public}d", res);
265 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
266 }
267 int32_t retCode = ERR_INVALID_VALUE;
268 reply.ReadInt32(retCode);
269 return retCode;
270 }
271
IsSuperAdmin(const std::string & bundleName,bool & result)272 ErrCode EnterpriseDeviceMgrProxy::IsSuperAdmin(const std::string &bundleName, bool &result)
273 {
274 EDMLOGD("EnterpriseDeviceMgrProxy::IsSuperAdmin");
275 result = false;
276 if (!IsEdmEnabled()) {
277 return ERR_OK;
278 }
279 sptr<IRemoteObject> remote = LoadAndGetEdmService();
280 if (!remote) {
281 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
282 }
283 MessageParcel data;
284 MessageParcel reply;
285 MessageOption option;
286 data.WriteInterfaceToken(DESCRIPTOR);
287 data.WriteString(bundleName);
288 ErrCode res = remote->SendRequest(EdmInterfaceCode::IS_SUPER_ADMIN, data, reply, option);
289 if (FAILED(res)) {
290 EDMLOGE("EnterpriseDeviceMgrProxy:IsSuperAdmin send request fail. %{public}d", res);
291 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
292 }
293 int32_t resCode = ERR_INVALID_VALUE;
294 if (!reply.ReadInt32(resCode) || FAILED(resCode)) {
295 EDMLOGW("EnterpriseDeviceMgrProxy:SetEnterpriseInfo get result code fail. %{public}d", resCode);
296 return resCode;
297 }
298 reply.ReadBool(result);
299 EDMLOGD("EnterpriseDeviceMgrProxy::IsSuperAdmin ret %{public}d", result);
300 return ERR_OK;
301 }
302
IsAdminEnabled(AppExecFwk::ElementName & admin,int32_t userId,bool & result)303 ErrCode EnterpriseDeviceMgrProxy::IsAdminEnabled(AppExecFwk::ElementName &admin, int32_t userId, bool &result)
304 {
305 EDMLOGD("EnterpriseDeviceMgrProxy::IsAdminEnabled");
306 result = false;
307 if (!IsEdmEnabled()) {
308 return ERR_OK;
309 }
310 sptr<IRemoteObject> remote = LoadAndGetEdmService();
311 if (!remote) {
312 return false;
313 }
314 MessageParcel data;
315 MessageParcel reply;
316 MessageOption option;
317 data.WriteInterfaceToken(DESCRIPTOR);
318 data.WriteParcelable(&admin);
319 data.WriteInt32(userId);
320 ErrCode res = remote->SendRequest(EdmInterfaceCode::IS_ADMIN_ENABLED, data, reply, option);
321 if (FAILED(res)) {
322 EDMLOGE("EnterpriseDeviceMgrProxy:IsAdminEnabled send request fail. %{public}d", res);
323 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
324 }
325 int32_t resCode = ERR_INVALID_VALUE;
326 if (!reply.ReadInt32(resCode) || FAILED(resCode)) {
327 EDMLOGW("EnterpriseDeviceMgrProxy:IsAdminEnabled get result code fail. %{public}d", resCode);
328 return resCode;
329 }
330 reply.ReadBool(result);
331 EDMLOGD("EnterpriseDeviceMgrProxy::IsAdminEnabled ret %{public}d", result);
332 return ERR_OK;
333 }
334
IsPolicyDisabled(const AppExecFwk::ElementName * admin,int policyCode,bool & result,std::string permissionTag)335 int32_t EnterpriseDeviceMgrProxy::IsPolicyDisabled(const AppExecFwk::ElementName *admin, int policyCode, bool &result,
336 std::string permissionTag)
337 {
338 MessageParcel data;
339 data.WriteInterfaceToken(DESCRIPTOR);
340 data.WriteInt32(WITHOUT_USERID);
341 data.WriteString(permissionTag);
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
GetSuperAdmin(std::string & bundleName,std::string & abilityName)421 ErrCode EnterpriseDeviceMgrProxy::GetSuperAdmin(std::string &bundleName, std::string &abilityName)
422 {
423 EDMLOGD("EnterpriseDeviceMgrProxy::GetSuperAdmin");
424 if (!IsEdmEnabled()) {
425 return ERR_OK;
426 }
427 sptr<IRemoteObject> remote = LoadAndGetEdmService();
428 if (!remote) {
429 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
430 }
431 MessageParcel data;
432 MessageParcel reply;
433 MessageOption option;
434 data.WriteInterfaceToken(DESCRIPTOR);
435 ErrCode res = remote->SendRequest(EdmInterfaceCode::GET_SUPER_ADMIN_WANT_INFO, data, reply, option);
436 if (FAILED(res)) {
437 EDMLOGE("EnterpriseDeviceMgrProxy:GetSuperAdmin send request fail. %{public}d", res);
438 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
439 }
440 int32_t resCode = ERR_INVALID_VALUE;
441 if (!reply.ReadInt32(resCode) || FAILED(resCode)) {
442 EDMLOGW("EnterpriseDeviceMgrProxy:GetSuperAdmin get result code fail. %{public}d", resCode);
443 return resCode;
444 }
445 reply.ReadString(bundleName);
446 reply.ReadString(abilityName);
447 return ERR_OK;
448 }
449
GetPolicyValue(AppExecFwk::ElementName * admin,int policyCode,std::string & policyData,int32_t userId)450 bool EnterpriseDeviceMgrProxy::GetPolicyValue(AppExecFwk::ElementName *admin, int policyCode, std::string &policyData,
451 int32_t userId)
452 {
453 MessageParcel reply;
454 if (!GetPolicyData(admin, policyCode, userId, reply)) {
455 return false;
456 }
457 reply.ReadString(policyData);
458 return true;
459 }
460
GetPolicyArray(AppExecFwk::ElementName * admin,int policyCode,std::vector<std::string> & policyData,int32_t userId)461 bool EnterpriseDeviceMgrProxy::GetPolicyArray(AppExecFwk::ElementName *admin, int policyCode,
462 std::vector<std::string> &policyData, int32_t userId)
463 {
464 MessageParcel reply;
465 if (!GetPolicyData(admin, policyCode, userId, reply)) {
466 return false;
467 }
468 int32_t size = reply.ReadInt32();
469 EDMLOGD("EnterpriseDeviceMgrProxy::GetPolicyArray size: %{public}d.", size);
470 return reply.ReadStringVector(&policyData);
471 }
472
GetPolicyMap(AppExecFwk::ElementName * admin,int policyCode,std::map<std::string,std::string> & policyData,int32_t userId)473 bool EnterpriseDeviceMgrProxy::GetPolicyMap(AppExecFwk::ElementName *admin, int policyCode,
474 std::map<std::string, std::string> &policyData, int32_t userId)
475 {
476 MessageParcel reply;
477 if (!GetPolicyData(admin, policyCode, userId, reply)) {
478 return false;
479 }
480 std::vector<std::string> keys;
481 if (!reply.ReadStringVector(&keys)) {
482 EDMLOGE("EnterpriseDeviceMgrProxy::read map keys fail.");
483 return false;
484 }
485 std::vector<std::string> values;
486 if (!reply.ReadStringVector(&values)) {
487 EDMLOGE("EnterpriseDeviceMgrProxy::read map values fail.");
488 return false;
489 }
490 if (keys.size() != values.size()) {
491 EDMLOGE("EnterpriseDeviceMgrProxy::read map fail.");
492 return false;
493 }
494 policyData.clear();
495 for (uint64_t i = 0; i < keys.size(); ++i) {
496 policyData.insert(std::make_pair(keys.at(i), values.at(i)));
497 }
498 return true;
499 }
500
GetPolicyData(AppExecFwk::ElementName * admin,int policyCode,int32_t userId,MessageParcel & reply)501 bool EnterpriseDeviceMgrProxy::GetPolicyData(AppExecFwk::ElementName *admin, int policyCode, int32_t userId,
502 MessageParcel &reply)
503 {
504 if (!IsEdmEnabled()) {
505 return false;
506 }
507 MessageParcel data;
508 data.WriteInterfaceToken(DESCRIPTOR);
509 data.WriteInt32(HAS_USERID);
510 data.WriteInt32(userId);
511 data.WriteString(WITHOUT_PERMISSION_TAG);
512 if (admin != nullptr) {
513 data.WriteInt32(HAS_ADMIN);
514 data.WriteParcelable(admin);
515 } else {
516 data.WriteInt32(WITHOUT_ADMIN);
517 }
518 int32_t ret = ERR_INVALID_VALUE;
519 return GetPolicy(policyCode, data, reply) && reply.ReadInt32(ret) && (ret == ERR_OK);
520 }
521
GetPolicy(int policyCode,MessageParcel & data,MessageParcel & reply)522 bool EnterpriseDeviceMgrProxy::GetPolicy(int policyCode, MessageParcel &data, MessageParcel &reply)
523 {
524 if (!IsEdmEnabled()) {
525 reply.WriteInt32(EdmReturnErrCode::ADMIN_INACTIVE);
526 return false;
527 }
528 if (policyCode < 0) {
529 EDMLOGE("EnterpriseDeviceMgrProxy:GetPolicy invalid policyCode:%{public}d", policyCode);
530 return false;
531 }
532 std::uint32_t funcCode = POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::GET, (std::uint32_t)policyCode);
533 sptr<IRemoteObject> remote = LoadAndGetEdmService();
534 if (!remote) {
535 reply.WriteInt32(EdmReturnErrCode::SYSTEM_ABNORMALLY);
536 return false;
537 }
538 MessageOption option;
539 ErrCode res = remote->SendRequest(funcCode, data, reply, option);
540 if (FAILED(res)) {
541 EDMLOGE("EnterpriseDeviceMgrProxy:GetPolicy send request fail.");
542 return false;
543 }
544 return true;
545 }
546
GetEnabledAdmins(std::vector<std::string> & enabledAdminList)547 void EnterpriseDeviceMgrProxy::GetEnabledAdmins(std::vector<std::string> &enabledAdminList)
548 {
549 GetEnabledAdmins(AdminType::NORMAL, enabledAdminList);
550 }
551
GetEnabledSuperAdmin(std::string & enabledAdmin)552 void EnterpriseDeviceMgrProxy::GetEnabledSuperAdmin(std::string &enabledAdmin)
553 {
554 std::vector<std::string> enabledAdminList;
555 GetEnabledAdmins(AdminType::ENT, enabledAdminList);
556 if (!enabledAdminList.empty()) {
557 enabledAdmin = enabledAdminList[0];
558 }
559 }
560
IsSuperAdminExist()561 bool EnterpriseDeviceMgrProxy::IsSuperAdminExist()
562 {
563 std::vector<std::string> enabledAdminList;
564 GetEnabledAdmins(AdminType::ENT, enabledAdminList);
565 return !enabledAdminList.empty();
566 }
567
LoadAndGetEdmService()568 sptr<IRemoteObject> EnterpriseDeviceMgrProxy::LoadAndGetEdmService()
569 {
570 std::lock_guard<std::mutex> lock(mutexLock_);
571 sptr<ISystemAbilityManager> sysAbilityMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
572 if (sysAbilityMgr == nullptr) {
573 EDMLOGE("EnterpriseDeviceMgrProxy::failed to get SystemAbilityManager");
574 return nullptr;
575 }
576 auto objectSA = sysAbilityMgr->CheckSystemAbility(ENTERPRISE_DEVICE_MANAGER_SA_ID);
577 if (objectSA == nullptr) {
578 EDMLOGI("EnterpriseDeviceMgrProxy::load sa from remote");
579 return EdmLoadManager::GetInstance().LoadAndGetEdmService();
580 }
581 return EdmSysManager::GetRemoteObjectOfSystemAbility(ENTERPRISE_DEVICE_MANAGER_SA_ID);
582 }
583
GetEnabledAdmins(AdminType type,std::vector<std::string> & enabledAdminList)584 void EnterpriseDeviceMgrProxy::GetEnabledAdmins(AdminType type, std::vector<std::string> &enabledAdminList)
585 {
586 if (!IsEdmEnabled()) {
587 return;
588 }
589 sptr<IRemoteObject> remote = LoadAndGetEdmService();
590 if (!remote) {
591 return;
592 }
593 MessageParcel data;
594 MessageParcel reply;
595 MessageOption option;
596 data.WriteInterfaceToken(DESCRIPTOR);
597 data.WriteInt32(static_cast<int32_t>(type));
598 ErrCode res = remote->SendRequest(GET_ENABLE_ADMIN, data, reply, option);
599 if (FAILED(res)) {
600 EDMLOGE("EnterpriseDeviceMgrProxy:GetEnabledAdmins send request fail.");
601 return;
602 }
603 int32_t resCode = ERR_OK;
604 if (!reply.ReadInt32(resCode) || FAILED(resCode)) {
605 EDMLOGE("EnterpriseDeviceMgrProxy:GetEnabledAdmins get result code fail.");
606 return;
607 }
608 std::vector<std::string> readArray;
609 reply.ReadStringVector(&readArray);
610 for (const std::string &item : readArray) {
611 enabledAdminList.push_back(item);
612 }
613 }
614
SetPolicyDisabled(const AppExecFwk::ElementName & admin,bool isDisabled,uint32_t policyCode,std::string permissionTag)615 int32_t EnterpriseDeviceMgrProxy::SetPolicyDisabled(const AppExecFwk::ElementName &admin, bool isDisabled,
616 uint32_t policyCode, std::string permissionTag)
617 {
618 MessageParcel data;
619 std::uint32_t funcCode = POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::SET, policyCode);
620 data.WriteInterfaceToken(DESCRIPTOR);
621 data.WriteInt32(WITHOUT_USERID);
622 data.WriteParcelable(&admin);
623 data.WriteString(permissionTag);
624 data.WriteBool(isDisabled);
625 return HandleDevicePolicy(funcCode, data);
626 }
627 } // namespace EDM
628 } // namespace OHOS