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 
GetPolicyValue(AppExecFwk::ElementName * admin,int policyCode,std::string & policyData,int32_t userId)508 bool EnterpriseDeviceMgrProxy::GetPolicyValue(AppExecFwk::ElementName *admin, int policyCode, std::string &policyData,
509     int32_t userId)
510 {
511     MessageParcel reply;
512     if (!GetPolicyData(admin, policyCode, userId, reply)) {
513         return false;
514     }
515     reply.ReadString(policyData);
516     return true;
517 }
518 
GetPolicyArray(AppExecFwk::ElementName * admin,int policyCode,std::vector<std::string> & policyData,int32_t userId)519 bool EnterpriseDeviceMgrProxy::GetPolicyArray(AppExecFwk::ElementName *admin, int policyCode,
520     std::vector<std::string> &policyData, int32_t userId)
521 {
522     MessageParcel reply;
523     if (!GetPolicyData(admin, policyCode, userId, reply)) {
524         return false;
525     }
526     int32_t size = reply.ReadInt32();
527     EDMLOGD("EnterpriseDeviceMgrProxy::GetPolicyArray size: %{public}d.", size);
528     return reply.ReadStringVector(&policyData);
529 }
530 
GetPolicyMap(AppExecFwk::ElementName * admin,int policyCode,std::map<std::string,std::string> & policyData,int32_t userId)531 bool EnterpriseDeviceMgrProxy::GetPolicyMap(AppExecFwk::ElementName *admin, int policyCode,
532     std::map<std::string, std::string> &policyData, int32_t userId)
533 {
534     MessageParcel reply;
535     if (!GetPolicyData(admin, policyCode, userId, reply)) {
536         return false;
537     }
538     std::vector<std::string> keys;
539     if (!reply.ReadStringVector(&keys)) {
540         EDMLOGE("EnterpriseDeviceMgrProxy::read map keys fail.");
541         return false;
542     }
543     std::vector<std::string> values;
544     if (!reply.ReadStringVector(&values)) {
545         EDMLOGE("EnterpriseDeviceMgrProxy::read map values fail.");
546         return false;
547     }
548     if (keys.size() != values.size()) {
549         EDMLOGE("EnterpriseDeviceMgrProxy::read map fail.");
550         return false;
551     }
552     policyData.clear();
553     for (uint64_t i = 0; i < keys.size(); ++i) {
554         policyData.insert(std::make_pair(keys.at(i), values.at(i)));
555     }
556     return true;
557 }
558 
GetPolicyData(AppExecFwk::ElementName * admin,int policyCode,int32_t userId,MessageParcel & reply)559 bool EnterpriseDeviceMgrProxy::GetPolicyData(AppExecFwk::ElementName *admin, int policyCode, int32_t userId,
560     MessageParcel &reply)
561 {
562     if (!IsEdmEnabled()) {
563         return false;
564     }
565     MessageParcel data;
566     data.WriteInterfaceToken(DESCRIPTOR);
567     data.WriteInt32(HAS_USERID);
568     data.WriteInt32(userId);
569     data.WriteString(WITHOUT_PERMISSION_TAG);
570     if (admin != nullptr) {
571         data.WriteInt32(HAS_ADMIN);
572         data.WriteParcelable(admin);
573     } else {
574         data.WriteInt32(WITHOUT_ADMIN);
575     }
576     int32_t ret = ERR_INVALID_VALUE;
577     return GetPolicy(policyCode, data, reply) && reply.ReadInt32(ret) && (ret == ERR_OK);
578 }
579 
GetPolicy(int policyCode,MessageParcel & data,MessageParcel & reply)580 bool EnterpriseDeviceMgrProxy::GetPolicy(int policyCode, MessageParcel &data, MessageParcel &reply)
581 {
582     if (!IsEdmEnabled()) {
583         reply.WriteInt32(EdmReturnErrCode::ADMIN_INACTIVE);
584         return false;
585     }
586     if (policyCode < 0) {
587         EDMLOGE("EnterpriseDeviceMgrProxy:GetPolicy invalid policyCode:%{public}d", policyCode);
588         return false;
589     }
590     std::uint32_t funcCode = POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::GET, (std::uint32_t)policyCode);
591     sptr<IRemoteObject> remote = LoadAndGetEdmService();
592     if (!remote) {
593         reply.WriteInt32(EdmReturnErrCode::SYSTEM_ABNORMALLY);
594         return false;
595     }
596     MessageOption option;
597     ErrCode res = remote->SendRequest(funcCode, data, reply, option);
598     if (FAILED(res)) {
599         EDMLOGE("EnterpriseDeviceMgrProxy:GetPolicy send request fail.");
600         return false;
601     }
602     return true;
603 }
604 
GetEnabledAdmins(std::vector<std::string> & enabledAdminList)605 void EnterpriseDeviceMgrProxy::GetEnabledAdmins(std::vector<std::string> &enabledAdminList)
606 {
607     GetEnabledAdmins(AdminType::NORMAL, enabledAdminList);
608 }
609 
GetEnabledSuperAdmin(std::string & enabledAdmin)610 void EnterpriseDeviceMgrProxy::GetEnabledSuperAdmin(std::string &enabledAdmin)
611 {
612     std::vector<std::string> enabledAdminList;
613     GetEnabledAdmins(AdminType::ENT, enabledAdminList);
614     if (!enabledAdminList.empty()) {
615         enabledAdmin = enabledAdminList[0];
616     }
617 }
618 
IsSuperAdminExist()619 bool EnterpriseDeviceMgrProxy::IsSuperAdminExist()
620 {
621     std::vector<std::string> enabledAdminList;
622     GetEnabledAdmins(AdminType::ENT, enabledAdminList);
623     return !enabledAdminList.empty();
624 }
625 
LoadAndGetEdmService()626 sptr<IRemoteObject> EnterpriseDeviceMgrProxy::LoadAndGetEdmService()
627 {
628     std::lock_guard<std::mutex> lock(mutexLock_);
629     sptr<ISystemAbilityManager> sysAbilityMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
630     if (sysAbilityMgr == nullptr) {
631         EDMLOGE("EnterpriseDeviceMgrProxy::failed to get SystemAbilityManager");
632         return nullptr;
633     }
634     auto objectSA = sysAbilityMgr->CheckSystemAbility(ENTERPRISE_DEVICE_MANAGER_SA_ID);
635     if (objectSA == nullptr) {
636         EDMLOGI("EnterpriseDeviceMgrProxy::load sa from remote");
637         return EdmLoadManager::GetInstance().LoadAndGetEdmService();
638     }
639     return EdmSysManager::GetRemoteObjectOfSystemAbility(ENTERPRISE_DEVICE_MANAGER_SA_ID);
640 }
641 
GetEnabledAdmins(AdminType type,std::vector<std::string> & enabledAdminList)642 void EnterpriseDeviceMgrProxy::GetEnabledAdmins(AdminType type, std::vector<std::string> &enabledAdminList)
643 {
644     if (!IsEdmEnabled()) {
645         return;
646     }
647     sptr<IRemoteObject> remote = LoadAndGetEdmService();
648     if (!remote) {
649         return;
650     }
651     MessageParcel data;
652     MessageParcel reply;
653     MessageOption option;
654     data.WriteInterfaceToken(DESCRIPTOR);
655     data.WriteInt32(static_cast<int32_t>(type));
656     ErrCode res = remote->SendRequest(GET_ENABLE_ADMIN, data, reply, option);
657     if (FAILED(res)) {
658         EDMLOGE("EnterpriseDeviceMgrProxy:GetEnabledAdmins send request fail.");
659         return;
660     }
661     int32_t resCode = ERR_OK;
662     if (!reply.ReadInt32(resCode) || FAILED(resCode)) {
663         EDMLOGE("EnterpriseDeviceMgrProxy:GetEnabledAdmins get result code fail.");
664         return;
665     }
666     std::vector<std::string> readArray;
667     reply.ReadStringVector(&readArray);
668     for (const std::string &item : readArray) {
669         enabledAdminList.push_back(item);
670     }
671 }
672 
SetPolicyDisabled(const AppExecFwk::ElementName & admin,bool isDisabled,uint32_t policyCode,std::string permissionTag)673 int32_t EnterpriseDeviceMgrProxy::SetPolicyDisabled(const AppExecFwk::ElementName &admin, bool isDisabled,
674     uint32_t policyCode, std::string permissionTag)
675 {
676     MessageParcel data;
677     std::uint32_t funcCode = POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::SET, policyCode);
678     data.WriteInterfaceToken(DESCRIPTOR);
679     data.WriteInt32(WITHOUT_USERID);
680     data.WriteParcelable(&admin);
681     data.WriteString(permissionTag);
682     data.WriteBool(isDisabled);
683     return HandleDevicePolicy(funcCode, data);
684 }
685 } // namespace EDM
686 } // namespace OHOS