• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_constants.h"
23 #include "edm_errors.h"
24 #include "edm_load_manager.h"
25 #include "edm_log.h"
26 #include "edm_sys_manager.h"
27 #include "func_code.h"
28 #include "parameters.h"
29 #include "system_ability_definition.h"
30 
31 namespace OHOS {
32 namespace EDM {
33 std::shared_ptr<EnterpriseDeviceMgrProxy> EnterpriseDeviceMgrProxy::instance_ = nullptr;
34 std::mutex EnterpriseDeviceMgrProxy::mutexLock_;
35 const std::u16string DESCRIPTOR = u"ohos.edm.IEnterpriseDeviceMgr";
36 const uint32_t GET_ENABLE_ADMIN = 5;
37 
GetInstance()38 std::shared_ptr<EnterpriseDeviceMgrProxy> EnterpriseDeviceMgrProxy::GetInstance()
39 {
40     if (instance_ == nullptr) {
41         std::lock_guard<std::mutex> lock(mutexLock_);
42         if (instance_ == nullptr) {
43             std::shared_ptr<EnterpriseDeviceMgrProxy> temp = std::make_shared<EnterpriseDeviceMgrProxy>();
44             instance_ = temp;
45         }
46     }
47     return instance_;
48 }
49 
DestroyInstance()50 void EnterpriseDeviceMgrProxy::DestroyInstance()
51 {
52     std::lock_guard<std::mutex> lock(mutexLock_);
53     if (instance_ != nullptr) {
54         instance_.reset();
55         instance_ = nullptr;
56     }
57 }
58 
IsEdmEnabled()59 bool EnterpriseDeviceMgrProxy::IsEdmEnabled()
60 {
61     std::string edmParaValue = system::GetParameter("persist.edm.edm_enable", "false");
62     EDMLOGD("EnterpriseDeviceMgrProxy::GetParameter %{public}s", edmParaValue.c_str());
63     return edmParaValue == "true";
64 }
65 
EnableAdmin(AppExecFwk::ElementName & admin,EntInfo & entInfo,AdminType type,int32_t userId)66 ErrCode EnterpriseDeviceMgrProxy::EnableAdmin(AppExecFwk::ElementName &admin, EntInfo &entInfo, AdminType type,
67     int32_t userId)
68 {
69     EDMLOGD("EnterpriseDeviceMgrProxy::EnableAdmin");
70     sptr<IRemoteObject> remote = LoadAndGetEdmService();
71     if (!remote) {
72         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
73     }
74     MessageParcel data;
75     MessageParcel reply;
76     MessageOption option;
77     data.WriteInterfaceToken(DESCRIPTOR);
78     data.WriteParcelable(&admin);
79     entInfo.Marshalling(data);
80     data.WriteInt32(static_cast<int32_t>(type));
81     data.WriteInt32(userId);
82     ErrCode res = remote->SendRequest(EdmInterfaceCode::ADD_DEVICE_ADMIN, data, reply, option);
83     if (FAILED(res)) {
84         EDMLOGE("EnterpriseDeviceMgrProxy:EnableAdmin send request fail. %{public}d", res);
85         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
86     }
87     int32_t resCode = ERR_INVALID_VALUE;
88     if (!reply.ReadInt32(resCode) || FAILED(resCode)) {
89         EDMLOGW("EnterpriseDeviceMgrProxy:EnableAdmin get result code fail. %{public}d", resCode);
90         return resCode;
91     }
92     return ERR_OK;
93 }
94 
DisableAdmin(AppExecFwk::ElementName & admin,int32_t userId)95 ErrCode EnterpriseDeviceMgrProxy::DisableAdmin(AppExecFwk::ElementName &admin, int32_t userId)
96 {
97     EDMLOGD("EnterpriseDeviceMgrProxy::DisableAdmin");
98     if (!IsEdmEnabled()) {
99         return EdmReturnErrCode::DISABLE_ADMIN_FAILED;
100     }
101     sptr<IRemoteObject> remote = LoadAndGetEdmService();
102     if (!remote) {
103         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
104     }
105     MessageParcel data;
106     MessageParcel reply;
107     MessageOption option;
108     data.WriteInterfaceToken(DESCRIPTOR);
109     data.WriteParcelable(&admin);
110     data.WriteInt32(userId);
111     ErrCode res = remote->SendRequest(EdmInterfaceCode::REMOVE_DEVICE_ADMIN, data, reply, option);
112     if (FAILED(res)) {
113         EDMLOGE("EnterpriseDeviceMgrProxy:DisableAdmin send request fail. %{public}d", res);
114         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
115     }
116     int32_t resCode = ERR_INVALID_VALUE;
117     if (!reply.ReadInt32(resCode) || FAILED(resCode)) {
118         EDMLOGW("EnterpriseDeviceMgrProxy:DisableAdmin get result code fail. %{public}d", resCode);
119         return resCode;
120     }
121     return ERR_OK;
122 }
123 
DisableSuperAdmin(const std::string & bundleName)124 ErrCode EnterpriseDeviceMgrProxy::DisableSuperAdmin(const std::string &bundleName)
125 {
126     EDMLOGD("EnterpriseDeviceMgrProxy::DisableSuperAdmin");
127     if (!IsEdmEnabled()) {
128         return EdmReturnErrCode::DISABLE_ADMIN_FAILED;
129     }
130     sptr<IRemoteObject> remote = LoadAndGetEdmService();
131     if (!remote) {
132         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
133     }
134     MessageParcel data;
135     MessageParcel reply;
136     MessageOption option;
137     data.WriteInterfaceToken(DESCRIPTOR);
138     data.WriteString(bundleName);
139     ErrCode res = remote->SendRequest(EdmInterfaceCode::REMOVE_SUPER_ADMIN, data, reply, option);
140     if (FAILED(res)) {
141         EDMLOGE("EnterpriseDeviceMgrProxy:DisableSuperAdmin send request fail. %{public}d", res);
142         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
143     }
144     int32_t resCode = ERR_INVALID_VALUE;
145     if (!reply.ReadInt32(resCode) || FAILED(resCode)) {
146         EDMLOGW("EnterpriseDeviceMgrProxy:DisableSuperAdmin get result code fail. %{public}d", resCode);
147         return resCode;
148     }
149     return ERR_OK;
150 }
151 
GetEnabledAdmin(AdminType type,std::vector<std::string> & enabledAdminList)152 ErrCode EnterpriseDeviceMgrProxy::GetEnabledAdmin(AdminType type, std::vector<std::string> &enabledAdminList)
153 {
154     EDMLOGD("EnterpriseDeviceMgrProxy::GetEnabledAdmin");
155     if (!IsEdmEnabled()) {
156         return EdmReturnErrCode::ADMIN_INACTIVE;
157     }
158     sptr<IRemoteObject> remote = LoadAndGetEdmService();
159     if (!remote) {
160         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
161     }
162     MessageParcel data;
163     MessageParcel reply;
164     MessageOption option;
165     data.WriteInterfaceToken(DESCRIPTOR);
166     data.WriteInt32(static_cast<int32_t>(type));
167     ErrCode res = remote->SendRequest(EdmInterfaceCode::GET_ENABLED_ADMIN, data, reply, option);
168     if (FAILED(res)) {
169         EDMLOGE("EnterpriseDeviceMgrProxy:GetEnabledAdmin send request fail. %{public}d", res);
170         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
171     }
172     int32_t resCode = ERR_INVALID_VALUE;
173     if (!reply.ReadInt32(resCode) || FAILED(resCode)) {
174         EDMLOGW("EnterpriseDeviceMgrProxy:GetEnabledAdmin get result code fail. %{public}d", resCode);
175         return resCode;
176     }
177     reply.ReadStringVector(&enabledAdminList);
178     return ERR_OK;
179 }
180 
GetEnterpriseInfo(AppExecFwk::ElementName & admin,EntInfo & entInfo)181 ErrCode EnterpriseDeviceMgrProxy::GetEnterpriseInfo(AppExecFwk::ElementName &admin, EntInfo &entInfo)
182 {
183     EDMLOGD("EnterpriseDeviceMgrProxy::GetEnterpriseInfo");
184     if (!IsEdmEnabled()) {
185         return EdmReturnErrCode::ADMIN_INACTIVE;
186     }
187     sptr<IRemoteObject> remote = LoadAndGetEdmService();
188     if (!remote) {
189         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
190     }
191     MessageParcel data;
192     MessageParcel reply;
193     MessageOption option;
194     data.WriteInterfaceToken(DESCRIPTOR);
195     data.WriteParcelable(&admin);
196     ErrCode res = remote->SendRequest(EdmInterfaceCode::GET_ENT_INFO, data, reply, option);
197     if (FAILED(res)) {
198         EDMLOGE("EnterpriseDeviceMgrProxy:GetEnterpriseInfo send request fail. %{public}d", res);
199         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
200     }
201     int32_t resCode = ERR_INVALID_VALUE;
202     if (!reply.ReadInt32(resCode) || FAILED(resCode)) {
203         EDMLOGW("EnterpriseDeviceMgrProxy:GetEnterpriseInfo get result code fail. %{public}d", resCode);
204         return resCode;
205     }
206     if (!EntInfo::Unmarshalling(reply, entInfo)) {
207         EDMLOGE("EnterpriseDeviceMgrProxy::GetEnterpriseInfo read parcel fail");
208         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
209     }
210     return ERR_OK;
211 }
212 
SetEnterpriseInfo(AppExecFwk::ElementName & admin,EntInfo & entInfo)213 ErrCode EnterpriseDeviceMgrProxy::SetEnterpriseInfo(AppExecFwk::ElementName &admin, EntInfo &entInfo)
214 {
215     EDMLOGD("EnterpriseDeviceMgrProxy::SetEnterpriseInfo");
216     if (!IsEdmEnabled()) {
217         return EdmReturnErrCode::ADMIN_INACTIVE;
218     }
219     sptr<IRemoteObject> remote = LoadAndGetEdmService();
220     if (!remote) {
221         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
222     }
223     MessageParcel data;
224     MessageParcel reply;
225     MessageOption option;
226     data.WriteInterfaceToken(DESCRIPTOR);
227     data.WriteParcelable(&admin);
228     entInfo.Marshalling(data);
229     ErrCode res = remote->SendRequest(EdmInterfaceCode::SET_ENT_INFO, data, reply, option);
230     if (FAILED(res)) {
231         EDMLOGE("EnterpriseDeviceMgrProxy:SetEnterpriseInfo send request fail. %{public}d", res);
232         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
233     }
234     int32_t resCode = ERR_INVALID_VALUE;
235     if (!reply.ReadInt32(resCode) || FAILED(resCode)) {
236         EDMLOGW("EnterpriseDeviceMgrProxy:SetEnterpriseInfo get result code fail. %{public}d", resCode);
237         return resCode;
238     }
239     return ERR_OK;
240 }
241 
HandleManagedEvent(const AppExecFwk::ElementName & admin,const std::vector<uint32_t> & events,bool subscribe)242 ErrCode EnterpriseDeviceMgrProxy::HandleManagedEvent(const AppExecFwk::ElementName &admin,
243     const std::vector<uint32_t> &events, bool subscribe)
244 {
245     EDMLOGD("EnterpriseDeviceMgrProxy::SubscribeManagedEvent: %{public}d", subscribe);
246     if (!IsEdmEnabled()) {
247         return EdmReturnErrCode::ADMIN_INACTIVE;
248     }
249     sptr<IRemoteObject> remote = LoadAndGetEdmService();
250     if (!remote) {
251         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
252     }
253     MessageParcel data;
254     MessageParcel reply;
255     MessageOption option;
256     data.WriteInterfaceToken(DESCRIPTOR);
257     data.WriteParcelable(&admin);
258     data.WriteUInt32Vector(events);
259     uint32_t policyCode = EdmInterfaceCode::SUBSCRIBE_MANAGED_EVENT;
260     if (!subscribe) {
261         policyCode = EdmInterfaceCode::UNSUBSCRIBE_MANAGED_EVENT;
262     }
263     ErrCode res = remote->SendRequest(policyCode, data, reply, option);
264     if (FAILED(res)) {
265         EDMLOGE("EnterpriseDeviceMgrProxy:SubscribeManagedEvent send request fail. %{public}d", res);
266         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
267     }
268     int32_t retCode = ERR_INVALID_VALUE;
269     reply.ReadInt32(retCode);
270     return retCode;
271 }
272 
IsSuperAdmin(const std::string & bundleName,bool & result)273 ErrCode EnterpriseDeviceMgrProxy::IsSuperAdmin(const std::string &bundleName, bool &result)
274 {
275     EDMLOGD("EnterpriseDeviceMgrProxy::IsSuperAdmin");
276     result = false;
277     if (!IsEdmEnabled()) {
278         return ERR_OK;
279     }
280     sptr<IRemoteObject> remote = LoadAndGetEdmService();
281     if (!remote) {
282         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
283     }
284     MessageParcel data;
285     MessageParcel reply;
286     MessageOption option;
287     data.WriteInterfaceToken(DESCRIPTOR);
288     data.WriteString(bundleName);
289     ErrCode res = remote->SendRequest(EdmInterfaceCode::IS_SUPER_ADMIN, data, reply, option);
290     if (FAILED(res)) {
291         EDMLOGE("EnterpriseDeviceMgrProxy:IsSuperAdmin send request fail. %{public}d", res);
292         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
293     }
294     int32_t resCode = ERR_INVALID_VALUE;
295     if (!reply.ReadInt32(resCode) || FAILED(resCode)) {
296         EDMLOGW("EnterpriseDeviceMgrProxy:SetEnterpriseInfo get result code fail. %{public}d", resCode);
297         return resCode;
298     }
299     reply.ReadBool(result);
300     EDMLOGD("EnterpriseDeviceMgrProxy::IsSuperAdmin ret %{public}d", result);
301     return ERR_OK;
302 }
303 
IsAdminEnabled(AppExecFwk::ElementName & admin,int32_t userId,bool & result)304 ErrCode EnterpriseDeviceMgrProxy::IsAdminEnabled(AppExecFwk::ElementName &admin, int32_t userId, bool &result)
305 {
306     EDMLOGD("EnterpriseDeviceMgrProxy::IsAdminEnabled");
307     result = false;
308     if (!IsEdmEnabled()) {
309         return ERR_OK;
310     }
311     sptr<IRemoteObject> remote = LoadAndGetEdmService();
312     if (!remote) {
313         return false;
314     }
315     MessageParcel data;
316     MessageParcel reply;
317     MessageOption option;
318     data.WriteInterfaceToken(DESCRIPTOR);
319     data.WriteParcelable(&admin);
320     data.WriteInt32(userId);
321     ErrCode res = remote->SendRequest(EdmInterfaceCode::IS_ADMIN_ENABLED, data, reply, option);
322     if (FAILED(res)) {
323         EDMLOGE("EnterpriseDeviceMgrProxy:IsAdminEnabled send request fail. %{public}d", res);
324         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
325     }
326     int32_t resCode = ERR_INVALID_VALUE;
327     if (!reply.ReadInt32(resCode) || FAILED(resCode)) {
328         EDMLOGW("EnterpriseDeviceMgrProxy:IsAdminEnabled get result code fail. %{public}d", resCode);
329         return resCode;
330     }
331     reply.ReadBool(result);
332     EDMLOGD("EnterpriseDeviceMgrProxy::IsAdminEnabled ret %{public}d", result);
333     return ERR_OK;
334 }
335 
IsPolicyDisabled(const AppExecFwk::ElementName * admin,int policyCode,bool & result,std::string permissionTag)336 int32_t EnterpriseDeviceMgrProxy::IsPolicyDisabled(const AppExecFwk::ElementName *admin, int policyCode, bool &result,
337     std::string permissionTag)
338 {
339     MessageParcel data;
340     data.WriteInterfaceToken(DESCRIPTOR);
341     data.WriteInt32(WITHOUT_USERID);
342     data.WriteString(permissionTag);
343     if (admin != nullptr) {
344         data.WriteInt32(HAS_ADMIN);
345         data.WriteParcelable(admin);
346     } else {
347         if (!IsEdmEnabled()) {
348             result = false;
349             return ERR_OK;
350         }
351         data.WriteInt32(WITHOUT_ADMIN);
352     }
353     MessageParcel reply;
354     GetPolicy(policyCode, data, reply);
355     int32_t ret = ERR_INVALID_VALUE;
356     bool isSuccess = reply.ReadInt32(ret) && (ret == ERR_OK);
357     if (!isSuccess) {
358         EDMLOGE("IsPolicyDisabled:GetPolicy fail. %{public}d", ret);
359         return ret;
360     }
361     reply.ReadBool(result);
362     return ERR_OK;
363 }
364 
HandleDevicePolicy(int32_t policyCode,MessageParcel & data)365 int32_t EnterpriseDeviceMgrProxy::HandleDevicePolicy(int32_t policyCode, MessageParcel &data)
366 {
367     MessageParcel reply;
368     return HandleDevicePolicy(policyCode, data, reply);
369 }
370 
HandleDevicePolicy(int32_t policyCode,MessageParcel & data,MessageParcel & reply)371 int32_t EnterpriseDeviceMgrProxy::HandleDevicePolicy(int32_t policyCode, MessageParcel &data, MessageParcel &reply)
372 {
373     EDMLOGD("EnterpriseDeviceMgrProxy::HandleDevicePolicy");
374     if (!IsEdmEnabled()) {
375         return EdmReturnErrCode::ADMIN_INACTIVE;
376     }
377     sptr<IRemoteObject> remote = LoadAndGetEdmService();
378     if (!remote) {
379         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
380     }
381     MessageOption option;
382     EDMLOGD("EnterpriseDeviceMgrProxy::handleDevicePolicy::sendRequest %{public}d", policyCode);
383     ErrCode res = remote->SendRequest(policyCode, data, reply, option);
384     if (FAILED(res)) {
385         EDMLOGE("EnterpriseDeviceMgrProxy:HandleDevicePolicy send request fail. %{public}d", res);
386         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
387     }
388     int32_t ret = ERR_INVALID_VALUE;
389     reply.ReadInt32(ret);
390     return ret;
391 }
392 
AuthorizeAdmin(const AppExecFwk::ElementName & admin,const std::string & bundleName)393 ErrCode EnterpriseDeviceMgrProxy::AuthorizeAdmin(const AppExecFwk::ElementName &admin, const std::string &bundleName)
394 {
395     EDMLOGD("EnterpriseDeviceMgrProxy::AuthorizeAdmin");
396     if (!IsEdmEnabled()) {
397         return EdmReturnErrCode::ADMIN_INACTIVE;
398     }
399     sptr<IRemoteObject> remote = LoadAndGetEdmService();
400     if (!remote) {
401         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
402     }
403     MessageParcel data;
404     MessageParcel reply;
405     MessageOption option;
406     data.WriteInterfaceToken(DESCRIPTOR);
407     data.WriteParcelable(&admin);
408     data.WriteString(bundleName);
409     ErrCode res = remote->SendRequest(EdmInterfaceCode::AUTHORIZE_ADMIN, data, reply, option);
410     if (FAILED(res)) {
411         EDMLOGE("EnterpriseDeviceMgrProxy:AuthorizeAdmin send request fail. %{public}d", res);
412         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
413     }
414     int32_t resCode = ERR_INVALID_VALUE;
415     if (!reply.ReadInt32(resCode) || FAILED(resCode)) {
416         EDMLOGW("EnterpriseDeviceMgrProxy:AuthorizeAdmin get result code fail. %{public}d", resCode);
417         return resCode;
418     }
419     return ERR_OK;
420 }
421 
GetSuperAdmin(std::string & bundleName,std::string & abilityName)422 ErrCode EnterpriseDeviceMgrProxy::GetSuperAdmin(std::string &bundleName, std::string &abilityName)
423 {
424     EDMLOGD("EnterpriseDeviceMgrProxy::GetSuperAdmin");
425     if (!IsEdmEnabled()) {
426         return ERR_OK;
427     }
428     sptr<IRemoteObject> remote = LoadAndGetEdmService();
429     if (!remote) {
430         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
431     }
432     MessageParcel data;
433     MessageParcel reply;
434     MessageOption option;
435     data.WriteInterfaceToken(DESCRIPTOR);
436     ErrCode res = remote->SendRequest(EdmInterfaceCode::GET_SUPER_ADMIN_WANT_INFO, data, reply, option);
437     if (FAILED(res)) {
438         EDMLOGE("EnterpriseDeviceMgrProxy:GetSuperAdmin send request fail. %{public}d", res);
439         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
440     }
441     int32_t resCode = ERR_INVALID_VALUE;
442     if (!reply.ReadInt32(resCode) || FAILED(resCode)) {
443         EDMLOGW("EnterpriseDeviceMgrProxy:GetSuperAdmin get result code fail. %{public}d", resCode);
444         return resCode;
445     }
446     reply.ReadString(bundleName);
447     reply.ReadString(abilityName);
448     return ERR_OK;
449 }
450 
SetDelegatedPolicies(const AppExecFwk::ElementName & admin,const std::string & bundleName,const std::vector<std::string> & policies)451 ErrCode EnterpriseDeviceMgrProxy::SetDelegatedPolicies(const AppExecFwk::ElementName &admin,
452     const std::string &bundleName, const std::vector<std::string> &policies)
453 {
454     EDMLOGD("EnterpriseDeviceMgrProxy::SetDelegatedPolicies");
455     if (!IsEdmEnabled()) {
456         return EdmReturnErrCode::ADMIN_INACTIVE;
457     }
458     sptr<IRemoteObject> remote = LoadAndGetEdmService();
459     if (!remote) {
460         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
461     }
462     MessageParcel data;
463     MessageParcel reply;
464     MessageOption option;
465     data.WriteInterfaceToken(DESCRIPTOR);
466     data.WriteParcelable(&admin);
467     data.WriteString(bundleName);
468     data.WriteStringVector(policies);
469     ErrCode res = remote->SendRequest(EdmInterfaceCode::SET_DELEGATED_POLICIES, data, reply, option);
470     if (FAILED(res)) {
471         EDMLOGE("EnterpriseDeviceMgrProxy:SetDelegatedPolicies get result code fail. %{public}d", res);
472         return res;
473     }
474     return ERR_OK;
475 }
476 
GetDelegatedPolicies(const AppExecFwk::ElementName & admin,const std::string & bundleNameOrPolicyName,uint32_t code,std::vector<std::string> & result)477 ErrCode EnterpriseDeviceMgrProxy::GetDelegatedPolicies(const AppExecFwk::ElementName &admin,
478     const std::string &bundleNameOrPolicyName, uint32_t code, std::vector<std::string> &result)
479 {
480     EDMLOGD("EnterpriseDeviceMgrProxy::GetDelegatedPolicies");
481     if (!IsEdmEnabled()) {
482         return EdmReturnErrCode::ADMIN_INACTIVE;
483     }
484     sptr<IRemoteObject> remote = LoadAndGetEdmService();
485     if (!remote) {
486         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
487     }
488     MessageParcel data;
489     MessageParcel reply;
490     MessageOption option;
491     data.WriteInterfaceToken(DESCRIPTOR);
492     data.WriteParcelable(&admin);
493     data.WriteString(bundleNameOrPolicyName);
494     ErrCode res = remote->SendRequest(code, data, reply, option);
495     if (FAILED(res)) {
496         EDMLOGE("EnterpriseDeviceMgrProxy:GetDelegatedPolicies get result code fail. %{public}d", res);
497         return res;
498     }
499     uint32_t size = reply.ReadUint32();
500     if (size > EdmConstants::POLICIES_MAX_SIZE) {
501         EDMLOGE("EnterpriseDeviceMgrProxy:GetDelegatedPolicies get result size is too large.");
502         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
503     }
504     reply.ReadStringVector(&result);
505     return ERR_OK;
506 }
507 
GetAdmins(std::vector<std::shared_ptr<AAFwk::Want>> & wants)508 ErrCode EnterpriseDeviceMgrProxy::GetAdmins(std::vector<std::shared_ptr<AAFwk::Want>> &wants)
509 {
510     EDMLOGD("EnterpriseDeviceMgrProxy::GetAdmins");
511     if (!IsEdmEnabled()) {
512         return ERR_OK;
513     }
514     sptr<IRemoteObject> remote = LoadAndGetEdmService();
515     if (!remote) {
516         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
517     }
518     MessageParcel data;
519     MessageParcel reply;
520     MessageOption option;
521     data.WriteInterfaceToken(DESCRIPTOR);
522     ErrCode res = remote->SendRequest(EdmInterfaceCode::GET_ADMINS, data, reply, option);
523     if (FAILED(res)) {
524         EDMLOGE("EnterpriseDeviceMgrProxy:GetAdmins send request fail. %{public}d", res);
525         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
526     }
527     int32_t resCode = ERR_INVALID_VALUE;
528     if (!reply.ReadInt32(resCode) || FAILED(resCode)) {
529         EDMLOGW("EnterpriseDeviceMgrProxy:GetAdmins get result code fail. %{public}d", resCode);
530         return resCode;
531     }
532 
533     uint32_t size = reply.ReadUint32();
534     if (size > EdmConstants::DEFAULT_LOOP_MAX_SIZE) {
535         EDMLOGE("EnterpriseDeviceMgrProxy:GetAdmins size error. size: %{public}d", size);
536         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
537     }
538     for (uint32_t i = 0; i < size; i++) {
539         std::shared_ptr<AAFwk::Want> want(reply.ReadParcelable<AAFwk::Want>());
540         wants.push_back(want);
541     }
542     return ERR_OK;
543 }
544 
CheckAndGetAdminProvisionInfo(AppExecFwk::ElementName & admin,std::string & bundleName)545 ErrCode EnterpriseDeviceMgrProxy::CheckAndGetAdminProvisionInfo(AppExecFwk::ElementName &admin,
546     std::string &bundleName)
547 {
548     std::uint32_t funcCode =
549         POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::GET, (std::uint32_t)EdmInterfaceCode::GET_ADMINPROVISION_INFO);
550     sptr<IRemoteObject> remote = LoadAndGetEdmService();
551     if (!remote) {
552         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
553     }
554     MessageParcel data;
555     data.WriteInterfaceToken(DESCRIPTOR);
556     data.WriteInt32(WITHOUT_USERID);
557     data.WriteParcelable(&admin);
558     data.WriteString(WITHOUT_PERMISSION_TAG);
559     MessageParcel reply;
560     MessageOption option;
561     ErrCode res = remote->SendRequest(funcCode, data, reply, option);
562     if (FAILED(res)) {
563         EDMLOGE("EnterpriseDeviceMgrProxy:CheckAndGetAdminProvisionInfo send request fail. %{public}d", res);
564         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
565     }
566     int32_t resCode = ERR_INVALID_VALUE;
567     if (!reply.ReadInt32(resCode) || FAILED(resCode)) {
568         EDMLOGE("EnterpriseDeviceMgrProxy:CheckAndGetAdminProvisionInfo get result code fail. %{public}d", resCode);
569         return resCode;
570     }
571     bundleName = reply.ReadString();
572     EDMLOGI("EnterpriseDeviceMgrProxy:CheckAndGetAdminProvisionInfo result. %{public}s", bundleName.c_str());
573     return ERR_OK;
574 }
575 
GetPolicyValue(AppExecFwk::ElementName * admin,int policyCode,std::string & policyData,int32_t userId)576 bool EnterpriseDeviceMgrProxy::GetPolicyValue(AppExecFwk::ElementName *admin, int policyCode, std::string &policyData,
577     int32_t userId)
578 {
579     MessageParcel reply;
580     if (!GetPolicyData(admin, policyCode, userId, reply)) {
581         return false;
582     }
583     reply.ReadString(policyData);
584     return true;
585 }
586 
GetPolicyArray(AppExecFwk::ElementName * admin,int policyCode,std::vector<std::string> & policyData,int32_t userId)587 bool EnterpriseDeviceMgrProxy::GetPolicyArray(AppExecFwk::ElementName *admin, int policyCode,
588     std::vector<std::string> &policyData, int32_t userId)
589 {
590     MessageParcel reply;
591     if (!GetPolicyData(admin, policyCode, userId, reply)) {
592         return false;
593     }
594     int32_t size = reply.ReadInt32();
595     EDMLOGD("EnterpriseDeviceMgrProxy::GetPolicyArray size: %{public}d.", size);
596     return reply.ReadStringVector(&policyData);
597 }
598 
GetPolicyMap(AppExecFwk::ElementName * admin,int policyCode,std::map<std::string,std::string> & policyData,int32_t userId)599 bool EnterpriseDeviceMgrProxy::GetPolicyMap(AppExecFwk::ElementName *admin, int policyCode,
600     std::map<std::string, std::string> &policyData, int32_t userId)
601 {
602     MessageParcel reply;
603     if (!GetPolicyData(admin, policyCode, userId, reply)) {
604         return false;
605     }
606     std::vector<std::string> keys;
607     if (!reply.ReadStringVector(&keys)) {
608         EDMLOGE("EnterpriseDeviceMgrProxy::read map keys fail.");
609         return false;
610     }
611     std::vector<std::string> values;
612     if (!reply.ReadStringVector(&values)) {
613         EDMLOGE("EnterpriseDeviceMgrProxy::read map values fail.");
614         return false;
615     }
616     if (keys.size() != values.size()) {
617         EDMLOGE("EnterpriseDeviceMgrProxy::read map fail.");
618         return false;
619     }
620     policyData.clear();
621     for (uint64_t i = 0; i < keys.size(); ++i) {
622         policyData.insert(std::make_pair(keys.at(i), values.at(i)));
623     }
624     return true;
625 }
626 
GetPolicyData(AppExecFwk::ElementName * admin,int policyCode,int32_t userId,MessageParcel & reply)627 bool EnterpriseDeviceMgrProxy::GetPolicyData(AppExecFwk::ElementName *admin, int policyCode, int32_t userId,
628     MessageParcel &reply)
629 {
630     if (!IsEdmEnabled()) {
631         return false;
632     }
633     MessageParcel data;
634     data.WriteInterfaceToken(DESCRIPTOR);
635     data.WriteInt32(HAS_USERID);
636     data.WriteInt32(userId);
637     data.WriteString(WITHOUT_PERMISSION_TAG);
638     if (admin != nullptr) {
639         data.WriteInt32(HAS_ADMIN);
640         data.WriteParcelable(admin);
641     } else {
642         data.WriteInt32(WITHOUT_ADMIN);
643     }
644     int32_t ret = ERR_INVALID_VALUE;
645     return GetPolicy(policyCode, data, reply) && reply.ReadInt32(ret) && (ret == ERR_OK);
646 }
647 
GetPolicy(int policyCode,MessageParcel & data,MessageParcel & reply)648 bool EnterpriseDeviceMgrProxy::GetPolicy(int policyCode, MessageParcel &data, MessageParcel &reply)
649 {
650     if (!IsEdmEnabled()) {
651         reply.WriteInt32(EdmReturnErrCode::ADMIN_INACTIVE);
652         return false;
653     }
654     if (policyCode < 0) {
655         EDMLOGE("EnterpriseDeviceMgrProxy:GetPolicy invalid policyCode:%{public}d", policyCode);
656         return false;
657     }
658     std::uint32_t funcCode = POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::GET, (std::uint32_t)policyCode);
659     sptr<IRemoteObject> remote = LoadAndGetEdmService();
660     if (!remote) {
661         reply.WriteInt32(EdmReturnErrCode::SYSTEM_ABNORMALLY);
662         return false;
663     }
664     MessageOption option;
665     ErrCode res = remote->SendRequest(funcCode, data, reply, option);
666     if (FAILED(res)) {
667         EDMLOGE("EnterpriseDeviceMgrProxy:GetPolicy send request fail.");
668         return false;
669     }
670     return true;
671 }
672 
GetEnabledAdmins(std::vector<std::string> & enabledAdminList)673 void EnterpriseDeviceMgrProxy::GetEnabledAdmins(std::vector<std::string> &enabledAdminList)
674 {
675     GetEnabledAdmins(AdminType::NORMAL, enabledAdminList);
676 }
677 
GetEnabledSuperAdmin(std::string & enabledAdmin)678 void EnterpriseDeviceMgrProxy::GetEnabledSuperAdmin(std::string &enabledAdmin)
679 {
680     std::vector<std::string> enabledAdminList;
681     GetEnabledAdmins(AdminType::ENT, enabledAdminList);
682     if (!enabledAdminList.empty()) {
683         enabledAdmin = enabledAdminList[0];
684     }
685 }
686 
IsSuperAdminExist()687 bool EnterpriseDeviceMgrProxy::IsSuperAdminExist()
688 {
689     std::vector<std::string> enabledAdminList;
690     GetEnabledAdmins(AdminType::ENT, enabledAdminList);
691     return !enabledAdminList.empty();
692 }
693 
LoadAndGetEdmService()694 sptr<IRemoteObject> EnterpriseDeviceMgrProxy::LoadAndGetEdmService()
695 {
696     std::lock_guard<std::mutex> lock(mutexLock_);
697     sptr<ISystemAbilityManager> sysAbilityMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
698     if (sysAbilityMgr == nullptr) {
699         EDMLOGE("EnterpriseDeviceMgrProxy::failed to get SystemAbilityManager");
700         return nullptr;
701     }
702     auto objectSA = sysAbilityMgr->CheckSystemAbility(ENTERPRISE_DEVICE_MANAGER_SA_ID);
703     if (objectSA == nullptr) {
704         EDMLOGI("EnterpriseDeviceMgrProxy::load sa from remote");
705         return EdmLoadManager::GetInstance().LoadAndGetEdmService();
706     }
707     return EdmSysManager::GetRemoteObjectOfSystemAbility(ENTERPRISE_DEVICE_MANAGER_SA_ID);
708 }
709 
GetEnabledAdmins(AdminType type,std::vector<std::string> & enabledAdminList)710 void EnterpriseDeviceMgrProxy::GetEnabledAdmins(AdminType type, std::vector<std::string> &enabledAdminList)
711 {
712     if (!IsEdmEnabled()) {
713         return;
714     }
715     sptr<IRemoteObject> remote = LoadAndGetEdmService();
716     if (!remote) {
717         return;
718     }
719     MessageParcel data;
720     MessageParcel reply;
721     MessageOption option;
722     data.WriteInterfaceToken(DESCRIPTOR);
723     data.WriteInt32(static_cast<int32_t>(type));
724     ErrCode res = remote->SendRequest(GET_ENABLE_ADMIN, data, reply, option);
725     if (FAILED(res)) {
726         EDMLOGE("EnterpriseDeviceMgrProxy:GetEnabledAdmins send request fail.");
727         return;
728     }
729     int32_t resCode = ERR_OK;
730     if (!reply.ReadInt32(resCode) || FAILED(resCode)) {
731         EDMLOGE("EnterpriseDeviceMgrProxy:GetEnabledAdmins get result code fail.");
732         return;
733     }
734     std::vector<std::string> readArray;
735     reply.ReadStringVector(&readArray);
736     for (const std::string &item : readArray) {
737         enabledAdminList.push_back(item);
738     }
739 }
740 
SetPolicyDisabled(const AppExecFwk::ElementName & admin,bool isDisabled,uint32_t policyCode,std::string permissionTag)741 int32_t EnterpriseDeviceMgrProxy::SetPolicyDisabled(const AppExecFwk::ElementName &admin, bool isDisabled,
742     uint32_t policyCode, std::string permissionTag)
743 {
744     MessageParcel data;
745     std::uint32_t funcCode = POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::SET, policyCode);
746     data.WriteInterfaceToken(DESCRIPTOR);
747     data.WriteInt32(WITHOUT_USERID);
748     data.WriteParcelable(&admin);
749     data.WriteString(permissionTag);
750     data.WriteBool(isDisabled);
751     return HandleDevicePolicy(funcCode, data);
752 }
753 } // namespace EDM
754 } // namespace OHOS