1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "enterprise_device_mgr_proxy.h"
17 #include <iservice_registry.h>
18 #include <string_ex.h>
19
20 #include "admin_type.h"
21 #include "edm_errors.h"
22 #include "edm_log.h"
23 #include "func_code.h"
24 #include "system_ability_definition.h"
25
26 namespace OHOS {
27 namespace EDM {
28 std::shared_ptr<EnterpriseDeviceMgrProxy> EnterpriseDeviceMgrProxy::instance_ = nullptr;
29 std::mutex EnterpriseDeviceMgrProxy::mutexLock_;
30 const std::u16string DESCRIPTOR = u"ohos.edm.IEnterpriseDeviceMgr";
31 const uint32_t GET_ENABLE_ADMIN = 5;
32
EnterpriseDeviceMgrProxy()33 EnterpriseDeviceMgrProxy::EnterpriseDeviceMgrProxy() {}
34
~EnterpriseDeviceMgrProxy()35 EnterpriseDeviceMgrProxy::~EnterpriseDeviceMgrProxy() {}
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
ActivateAdmin(AppExecFwk::ElementName & admin,EntInfo & entInfo,AdminType type,int32_t userId)58 ErrCode EnterpriseDeviceMgrProxy::ActivateAdmin(AppExecFwk::ElementName &admin, EntInfo &entInfo, AdminType type,
59 int32_t userId)
60 {
61 EDMLOGD("EnterpriseDeviceMgrProxy::ActivateAdmin");
62 sptr<IRemoteObject> remote = GetRemoteObject();
63 if (!remote) {
64 return ERR_EDM_SERVICE_NOT_READY;
65 }
66 MessageParcel data;
67 MessageParcel reply;
68 MessageOption option;
69 data.WriteInterfaceToken(DESCRIPTOR);
70 data.WriteParcelable(&admin);
71 data.WriteParcelable(&entInfo);
72 data.WriteUint32(type);
73 data.WriteInt32(userId);
74 ErrCode res = remote->SendRequest(IEnterpriseDeviceMgr::ADD_DEVICE_ADMIN, data, reply, option);
75 if (FAILED(res)) {
76 EDMLOGE("EnterpriseDeviceMgrProxy:ActivateAdmin send request fail. %{public}d", res);
77 return ERR_EDM_SERVICE_NOT_READY;
78 }
79 int32_t resCode;
80 if (!reply.ReadInt32(resCode) || FAILED(resCode)) {
81 EDMLOGW("EnterpriseDeviceMgrProxy:ActiveAdmin get result code fail. %{public}d", resCode);
82 return resCode;
83 }
84 return ERR_OK;
85 }
86
DeactivateAdmin(AppExecFwk::ElementName & admin,int32_t userId)87 ErrCode EnterpriseDeviceMgrProxy::DeactivateAdmin(AppExecFwk::ElementName &admin, int32_t userId)
88 {
89 EDMLOGD("EnterpriseDeviceMgrProxy::DeactivateAdmin");
90 sptr<IRemoteObject> remote = GetRemoteObject();
91 if (!remote) {
92 return ERR_EDM_SERVICE_NOT_READY;
93 }
94 MessageParcel data;
95 MessageParcel reply;
96 MessageOption option;
97 data.WriteInterfaceToken(DESCRIPTOR);
98 data.WriteParcelable(&admin);
99 data.WriteInt32(userId);
100 ErrCode res = remote->SendRequest(IEnterpriseDeviceMgr::REMOVE_DEVICE_ADMIN, data, reply, option);
101 if (FAILED(res)) {
102 EDMLOGE("EnterpriseDeviceMgrProxy:DeactivateAdmin send request fail. %{public}d", res);
103 return ERR_EDM_SERVICE_NOT_READY;
104 }
105 int32_t resCode;
106 if (!reply.ReadInt32(resCode) || FAILED(resCode)) {
107 EDMLOGW("EnterpriseDeviceMgrProxy:DeactivateAdmin get result code fail. %{public}d", resCode);
108 return resCode;
109 }
110 return ERR_OK;
111 }
112
DeactivateSuperAdmin(std::string bundleName)113 ErrCode EnterpriseDeviceMgrProxy::DeactivateSuperAdmin(std::string bundleName)
114 {
115 EDMLOGD("EnterpriseDeviceMgrProxy::DeactivateSuperAdmin");
116 sptr<IRemoteObject> remote = GetRemoteObject();
117 if (!remote) {
118 return ERR_EDM_SERVICE_NOT_READY;
119 }
120 MessageParcel data;
121 MessageParcel reply;
122 MessageOption option;
123 data.WriteInterfaceToken(DESCRIPTOR);
124 data.WriteString(bundleName);
125 ErrCode res = remote->SendRequest(IEnterpriseDeviceMgr::REMOVE_SUPER_ADMIN, data, reply, option);
126 if (FAILED(res)) {
127 EDMLOGE("EnterpriseDeviceMgrProxy:DeactivateSuperAdmin send request fail. %{public}d", res);
128 return ERR_EDM_SERVICE_NOT_READY;
129 }
130 int32_t resCode;
131 if (!reply.ReadInt32(resCode) || FAILED(resCode)) {
132 EDMLOGW("EnterpriseDeviceMgrProxy:DeactivateSuperAdmin get result code fail. %{public}d", resCode);
133 return resCode;
134 }
135 return ERR_OK;
136 }
137
GetActiveAdmin(AdminType type,std::vector<std::u16string> & activeAdminList)138 ErrCode EnterpriseDeviceMgrProxy::GetActiveAdmin(AdminType type, std::vector<std::u16string> &activeAdminList)
139 {
140 EDMLOGD("EnterpriseDeviceMgrProxy::GetActiveAdmin");
141 sptr<IRemoteObject> remote = GetRemoteObject();
142 if (!remote) {
143 return ERR_EDM_SERVICE_NOT_READY;
144 }
145 MessageParcel data;
146 MessageParcel reply;
147 MessageOption option;
148 data.WriteInterfaceToken(DESCRIPTOR);
149 data.WriteUint32(type);
150 ErrCode res = remote->SendRequest(IEnterpriseDeviceMgr::GET_ACTIVE_ADMIN, data, reply, option);
151 if (FAILED(res)) {
152 EDMLOGE("EnterpriseDeviceMgrProxy:GetActiveAdmin send request fail. %{public}d", res);
153 return ERR_EDM_SERVICE_NOT_READY;
154 }
155 int32_t resCode;
156 if (!reply.ReadInt32(resCode) || FAILED(resCode)) {
157 EDMLOGW("EnterpriseDeviceMgrProxy:GetActiveAdmin get result code fail. %{public}d", resCode);
158 return resCode;
159 }
160 reply.ReadString16Vector(&activeAdminList);
161 return ERR_OK;
162 }
163
GetEnterpriseInfo(AppExecFwk::ElementName & admin,EntInfo & entInfo)164 ErrCode EnterpriseDeviceMgrProxy::GetEnterpriseInfo(AppExecFwk::ElementName &admin, EntInfo &entInfo)
165 {
166 EDMLOGD("EnterpriseDeviceMgrProxy::GetEnterpriseInfo");
167 sptr<IRemoteObject> remote = GetRemoteObject();
168 if (!remote) {
169 return ERR_EDM_SERVICE_NOT_READY;
170 }
171 MessageParcel data;
172 MessageParcel reply;
173 MessageOption option;
174 data.WriteInterfaceToken(DESCRIPTOR);
175 data.WriteParcelable(&admin);
176 ErrCode res = remote->SendRequest(IEnterpriseDeviceMgr::GET_ENT_INFO, data, reply, option);
177 if (FAILED(res)) {
178 EDMLOGE("EnterpriseDeviceMgrProxy:GetEnterpriseInfo send request fail. %{public}d", res);
179 return ERR_EDM_SERVICE_NOT_READY;
180 }
181 int32_t resCode;
182 if (!reply.ReadInt32(resCode) || FAILED(resCode)) {
183 EDMLOGW("EnterpriseDeviceMgrProxy:GetEnterpriseInfo get result code fail. %{public}d", resCode);
184 return resCode;
185 }
186 std::unique_ptr<EntInfo> info(reply.ReadParcelable<EntInfo>());
187 entInfo = *info;
188 return ERR_OK;
189 }
190
SetEnterpriseInfo(AppExecFwk::ElementName & admin,EntInfo & entInfo)191 ErrCode EnterpriseDeviceMgrProxy::SetEnterpriseInfo(AppExecFwk::ElementName &admin, EntInfo &entInfo)
192 {
193 EDMLOGD("EnterpriseDeviceMgrProxy::SetEnterpriseInfo");
194 sptr<IRemoteObject> remote = GetRemoteObject();
195 if (!remote) {
196 return ERR_EDM_SERVICE_NOT_READY;
197 }
198 MessageParcel data;
199 MessageParcel reply;
200 MessageOption option;
201 data.WriteInterfaceToken(DESCRIPTOR);
202 data.WriteParcelable(&admin);
203 data.WriteParcelable(&entInfo);
204 ErrCode res = remote->SendRequest(IEnterpriseDeviceMgr::SET_ENT_INFO, data, reply, option);
205 if (FAILED(res)) {
206 EDMLOGE("EnterpriseDeviceMgrProxy:SetEnterpriseInfo send request fail. %{public}d", res);
207 return ERR_EDM_SERVICE_NOT_READY;
208 }
209 int32_t resCode;
210 if (!reply.ReadInt32(resCode) || FAILED(resCode)) {
211 EDMLOGW("EnterpriseDeviceMgrProxy:SetEnterpriseInfo get result code fail. %{public}d", resCode);
212 return resCode;
213 }
214 return ERR_OK;
215 }
216
IsSuperAdmin(std::string bundleName)217 bool EnterpriseDeviceMgrProxy::IsSuperAdmin(std::string bundleName)
218 {
219 EDMLOGD("EnterpriseDeviceMgrProxy::IsSuperAdmin");
220 sptr<IRemoteObject> remote = GetRemoteObject();
221 if (!remote) {
222 return ERR_EDM_SERVICE_NOT_READY;
223 }
224 MessageParcel data;
225 MessageParcel reply;
226 MessageOption option;
227 data.WriteInterfaceToken(DESCRIPTOR);
228 data.WriteString(bundleName);
229 ErrCode res = remote->SendRequest(IEnterpriseDeviceMgr::IS_SUPER_ADMIN, data, reply, option);
230 if (FAILED(res)) {
231 EDMLOGE("EnterpriseDeviceMgrProxy:IsSuperAdmin send request fail. %{public}d", res);
232 return false;
233 }
234 bool ret = false;
235 reply.ReadBool(ret);
236 EDMLOGD("EnterpriseDeviceMgrProxy::IsSuperAdmin ret %{public}d", ret);
237 return ret;
238 }
239
IsAdminActive(AppExecFwk::ElementName & admin)240 bool EnterpriseDeviceMgrProxy::IsAdminActive(AppExecFwk::ElementName &admin)
241 {
242 EDMLOGD("EnterpriseDeviceMgrProxy::IsAdminActive");
243 sptr<IRemoteObject> remote = GetRemoteObject();
244 if (!remote) {
245 return ERR_EDM_SERVICE_NOT_READY;
246 }
247 MessageParcel data;
248 MessageParcel reply;
249 MessageOption option;
250 data.WriteInterfaceToken(DESCRIPTOR);
251 data.WriteParcelable(&admin);
252 ErrCode res = remote->SendRequest(IEnterpriseDeviceMgr::IS_ADMIN_ACTIVE, data, reply, option);
253 if (FAILED(res)) {
254 EDMLOGE("EnterpriseDeviceMgrProxy:IsAdminActive send request fail. %{public}d", res);
255 return false;
256 }
257 bool ret = false;
258 reply.ReadBool(ret);
259 EDMLOGD("EnterpriseDeviceMgrProxy::IsAdminActive ret %{public}d", ret);
260 return ret;
261 }
262
IsPolicyDisable(int policyCode,bool & isDisabled)263 bool EnterpriseDeviceMgrProxy::IsPolicyDisable(int policyCode, bool &isDisabled)
264 {
265 MessageParcel reply;
266 if (!GetPolicy(policyCode, reply)) {
267 isDisabled = false;
268 return false;
269 } else {
270 isDisabled = reply.ReadBool();
271 }
272 return true;
273 }
274
HandleDevicePolicy(int32_t policyCode,MessageParcel & data)275 bool EnterpriseDeviceMgrProxy::HandleDevicePolicy(int32_t policyCode, MessageParcel &data)
276 {
277 EDMLOGD("EnterpriseDeviceMgrProxy::HandleDevicePolicy");
278 sptr<IRemoteObject> remote = GetRemoteObject();
279 if (!remote) {
280 return false;
281 }
282 MessageParcel reply;
283 MessageOption option;
284 EDMLOGD("EnterpriseDeviceMgrProxy::handleDevicePolicy::sendRequest %{public}d", policyCode);
285 ErrCode res = remote->SendRequest(policyCode, data, reply, option);
286 if (FAILED(res)) {
287 EDMLOGE("EnterpriseDeviceMgrProxy:HandleDevicePolicy send request fail. %{public}d", res);
288 return false;
289 }
290 std::int32_t requestRes = ERR_INVALID_VALUE;
291 bool blRes = reply.ReadInt32(requestRes) && (requestRes == ERR_OK);
292 if (!blRes) {
293 EDMLOGW("EnterpriseDeviceMgrProxy:HandleDevicePolicy return fail. %{public}d", requestRes);
294 }
295 return blRes;
296 }
297
GetPolicyValue(int policyCode,std::string & policyData)298 bool EnterpriseDeviceMgrProxy::GetPolicyValue(int policyCode, std::string &policyData)
299 {
300 MessageParcel reply;
301 if (!GetPolicy(policyCode, reply)) {
302 return false;
303 }
304 policyData = Str16ToStr8(reply.ReadString16());
305 return true;
306 }
307
GetPolicyArray(int policyCode,std::vector<std::string> & policyData)308 bool EnterpriseDeviceMgrProxy::GetPolicyArray(int policyCode, std::vector<std::string> &policyData)
309 {
310 MessageParcel reply;
311 if (!GetPolicy(policyCode, reply)) {
312 return false;
313 }
314 std::vector<std::u16string> readVector16;
315 if (!reply.ReadString16Vector(&readVector16)) {
316 return false;
317 }
318 policyData.clear();
319 std::vector<std::string> readVector;
320 if (!readVector16.empty()) {
321 for (const auto &str16 : readVector16) {
322 policyData.push_back(Str16ToStr8(str16));
323 }
324 }
325 return true;
326 }
327
GetPolicyConfig(int policyCode,std::map<std::string,std::string> & policyData)328 bool EnterpriseDeviceMgrProxy::GetPolicyConfig(int policyCode, std::map<std::string, std::string> &policyData)
329 {
330 MessageParcel reply;
331 if (!GetPolicy(policyCode, reply)) {
332 return false;
333 }
334 std::vector<std::u16string> keys16;
335 std::vector<std::u16string> values16;
336 if (!reply.ReadString16Vector(&keys16)) {
337 EDMLOGE("EnterpriseDeviceMgrProxy::read map keys fail.");
338 return false;
339 }
340 if (!reply.ReadString16Vector(&values16)) {
341 EDMLOGE("EnterpriseDeviceMgrProxy::read map values fail.");
342 return false;
343 }
344 if (keys16.size() != values16.size()) {
345 EDMLOGE("EnterpriseDeviceMgrProxy::read map fail.");
346 return false;
347 }
348 std::vector<std::string> keys;
349 std::vector<std::string> values;
350 for (const std::u16string &key : keys16) {
351 keys.push_back(Str16ToStr8(key));
352 }
353 for (const std::u16string &value : values16) {
354 values.push_back(Str16ToStr8(value));
355 }
356 policyData.clear();
357 for (unsigned long i = 0; i < keys.size(); ++i) {
358 policyData.insert(std::make_pair(keys.at(i), values.at(i)));
359 }
360 return true;
361 }
362
GetPolicy(int policyCode,MessageParcel & reply)363 bool EnterpriseDeviceMgrProxy::GetPolicy(int policyCode, MessageParcel &reply)
364 {
365 if (policyCode < 0) {
366 EDMLOGE("EnterpriseDeviceMgrProxy:GetPolicy invalid policyCode:%{public}d", policyCode);
367 return false;
368 }
369 std::uint32_t funcCode = POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::GET, (std::uint32_t)policyCode);
370 sptr<IRemoteObject> remote = GetRemoteObject();
371 if (!remote) {
372 return false;
373 }
374 MessageParcel data;
375 data.WriteInterfaceToken(DESCRIPTOR);
376 MessageOption option;
377 ErrCode res = remote->SendRequest(funcCode, data, reply, option);
378 if (FAILED(res)) {
379 EDMLOGE("EnterpriseDeviceMgrProxy:GetPolicy send request fail.");
380 return false;
381 }
382 std::int32_t requestRes = ERR_INVALID_VALUE;
383 bool blRes = reply.ReadInt32(requestRes) && (requestRes == ERR_OK);
384 if (!blRes) {
385 EDMLOGW("EnterpriseDeviceMgrProxy:GetPolicy fail. %{public}d", requestRes);
386 }
387 return blRes;
388 }
389
GetActiveAdmins(std::vector<std::string> & activeAdminList)390 void EnterpriseDeviceMgrProxy::GetActiveAdmins(std::vector<std::string> &activeAdminList)
391 {
392 GetActiveAdmins(AdminType::NORMAL, activeAdminList);
393 }
394
GetActiveSuperAdmin(std::string & activeAdmin)395 void EnterpriseDeviceMgrProxy::GetActiveSuperAdmin(std::string &activeAdmin)
396 {
397 std::vector<std::string> activeAdminList;
398 GetActiveAdmins(AdminType::ENT, activeAdminList);
399 if (!activeAdminList.empty()) {
400 activeAdmin = activeAdminList[0];
401 }
402 }
403
IsSuperAdminExist()404 bool EnterpriseDeviceMgrProxy::IsSuperAdminExist()
405 {
406 std::vector<std::string> activeAdminList;
407 GetActiveAdmins(AdminType::ENT, activeAdminList);
408 return !activeAdminList.empty();
409 }
410
GetRemoteObject()411 sptr<IRemoteObject> EnterpriseDeviceMgrProxy::GetRemoteObject()
412 {
413 std::lock_guard<std::mutex> lock(mutexLock_);
414 sptr<ISystemAbilityManager> samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
415 if (!samgr) {
416 EDMLOGE("EnterpriseDeviceMgrProxy:GetRemoteObject get system ability manager fail.");
417 return nullptr;
418 }
419 sptr<IRemoteObject> remote = samgr->GetSystemAbility(ENTERPRISE_DEVICE_MANAGER_SA_ID);
420 if (!remote) {
421 EDMLOGE("EnterpriseDeviceMgrProxy:GetRemoteObject get system ability fail.");
422 return nullptr;
423 }
424 return remote;
425 }
426
GetActiveAdmins(std::uint32_t type,std::vector<std::string> & activeAdminList)427 void EnterpriseDeviceMgrProxy::GetActiveAdmins(std::uint32_t type, std::vector<std::string> &activeAdminList)
428 {
429 sptr<IRemoteObject> remote = GetRemoteObject();
430 if (!remote) {
431 return;
432 }
433 MessageParcel data;
434 MessageParcel reply;
435 MessageOption option;
436 data.WriteInterfaceToken(DESCRIPTOR);
437 data.WriteUint32(type);
438 ErrCode res = remote->SendRequest(GET_ENABLE_ADMIN, data, reply, option);
439 if (FAILED(res)) {
440 EDMLOGE("EnterpriseDeviceMgrProxy:GetActiveAdmins send request fail.");
441 return;
442 }
443 int32_t resCode;
444 if (!reply.ReadInt32(resCode) || FAILED(resCode)) {
445 EDMLOGW("EnterpriseDeviceMgrProxy:GetActiveAdmins get result code fail.");
446 return;
447 }
448 std::vector<std::u16string> readArray;
449 reply.ReadString16Vector(&readArray);
450 for (const std::u16string &item : readArray) {
451 activeAdminList.push_back(Str16ToStr8(item));
452 }
453 }
454 } // namespace EDM
455 } // namespace OHOS