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_stub.h"
17
18 #include "admin.h"
19 #include "edm_constants.h"
20 #include "ent_info.h"
21 #include "string_ex.h"
22
23 using namespace OHOS::HiviewDFX;
24
25 namespace OHOS {
26 namespace EDM {
EnterpriseDeviceMgrStub()27 EnterpriseDeviceMgrStub::EnterpriseDeviceMgrStub() : EnterpriseDeviceMgrIdlStub(false)
28 {
29 InitSystemCodeList();
30 EDMLOGI("EnterpriseDeviceMgrStub()");
31 }
32
InitSystemCodeList()33 void EnterpriseDeviceMgrStub::InitSystemCodeList()
34 {
35 systemCodeList = {
36 EdmInterfaceCode::ADD_DEVICE_ADMIN,
37 EdmInterfaceCode::REMOVE_SUPER_ADMIN,
38 EdmInterfaceCode::GET_ENABLED_ADMIN,
39 EdmInterfaceCode::GET_ENT_INFO,
40 EdmInterfaceCode::SET_ENT_INFO,
41 EdmInterfaceCode::IS_SUPER_ADMIN,
42 EdmInterfaceCode::IS_ADMIN_ENABLED,
43 EdmInterfaceCode::AUTHORIZE_ADMIN,
44 EdmInterfaceCode::GET_SUPER_ADMIN_WANT_INFO,
45 EdmInterfaceCode::GET_ADMINS,
46 EdmInterfaceCode::REPLACE_SUPER_ADMIN,
47 EdmInterfaceCode::SET_ADMIN_RUNNING_MODE,
48 EdmInterfaceCode::SET_DELEGATED_POLICIES_OVERRIDE,
49 };
50 }
51
52 #ifdef EDM_SUPPORT_ALL_ENABLE
OnRemoteRequestIdl(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)53 ErrCode EnterpriseDeviceMgrStub::OnRemoteRequestIdl(uint32_t code, MessageParcel &data, MessageParcel &reply,
54 MessageOption &option)
55 {
56 EDMLOGI("EnterpriseDeviceMgrStub OnRemoteRequestIdl code %{public}u", code);
57 if (std::find(systemCodeList.begin(), systemCodeList.end(), code) != systemCodeList.end() &&
58 !PermissionChecker::GetInstance()->CheckIsSystemAppOrNative()) {
59 EDMLOGE("EnterpriseDeviceMgrStub OnRemoteRequestIdl not system app or native process");
60 return EdmReturnErrCode::SYSTEM_API_DENIED;
61 }
62 return EnterpriseDeviceMgrIdlStub::OnRemoteRequest(code, data, reply, option);
63 }
64 #endif
65
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)66 int32_t EnterpriseDeviceMgrStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
67 MessageOption &option)
68 {
69 #ifdef EDM_SUPPORT_ALL_ENABLE
70 if (SERVICE_FLAG(code)) {
71 return OnRemoteRequestIdl(code, data, reply, option);
72 }
73 std::u16string descriptor = GetDescriptor();
74 std::u16string remoteDescriptor = data.ReadInterfaceToken();
75 EDMLOGI("EnterpriseDeviceMgrStub code %{public}u", code);
76 if (descriptor != remoteDescriptor) {
77 EDMLOGE("EnterpriseDeviceMgrStub code %{public}d client and service descriptors are inconsistent", code);
78 reply.WriteInt32(EdmReturnErrCode::PARAM_ERROR);
79 return ERR_OK;
80 }
81 if (POLICY_FLAG(code)) {
82 EDMLOGD("POLICY_FLAG(code:%{public}x)\n", code);
83 int32_t hasUserId = 0;
84 int32_t userId = EdmConstants::DEFAULT_USER_ID;
85 data.ReadInt32(hasUserId);
86 if (hasUserId == 1) {
87 data.ReadInt32(userId);
88 }
89 if (FUNC_TO_POLICY(code) == (std::uint32_t)EdmInterfaceCode::GET_ADMINPROVISION_INFO) {
90 return CheckAndGetAdminProvisionInfoInner(code, data, reply, userId);
91 }
92 if (FUNC_TO_OPERATE(code) == static_cast<int>(FuncOperateType::GET)) {
93 EDMLOGD("GetDevicePolicyInner");
94 return GetDevicePolicyInner(code, data, reply, userId, hasUserId);
95 } else {
96 EDMLOGD("HandleDevicePolicyInner");
97 return HandleDevicePolicyInner(code, data, reply, userId);
98 }
99 } else {
100 EDMLOGE("!POLICY_FLAG(code)");
101 }
102
103 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
104 #else
105 EDMLOGI("EnterpriseDeviceMgrStub edm unsupport.");
106 reply.WriteInt32(EdmReturnErrCode::INTERFACE_UNSUPPORTED);
107 return ERR_OK;
108 #endif
109 }
110
GetExternalManagerFactory()111 std::shared_ptr<IExternalManagerFactory> EnterpriseDeviceMgrStub::GetExternalManagerFactory()
112 {
113 return externalManagerFactory_;
114 }
115
HandleDevicePolicyInner(uint32_t code,MessageParcel & data,MessageParcel & reply,int32_t userId)116 ErrCode EnterpriseDeviceMgrStub::HandleDevicePolicyInner(uint32_t code, MessageParcel &data, MessageParcel &reply,
117 int32_t userId)
118 {
119 std::unique_ptr<AppExecFwk::ElementName> admin(data.ReadParcelable<AppExecFwk::ElementName>());
120 if (!admin) {
121 EDMLOGW("HandleDevicePolicyInner: ReadParcelable failed");
122 reply.WriteInt32(EdmReturnErrCode::PARAM_ERROR);
123 return ERR_OK;
124 }
125 ErrCode errCode = HandleDevicePolicy(code, *admin, data, reply, userId);
126 reply.WriteInt32(errCode);
127 return ERR_OK;
128 }
129
GetDevicePolicyInner(uint32_t code,MessageParcel & data,MessageParcel & reply,int32_t userId,int32_t hasUserId)130 ErrCode EnterpriseDeviceMgrStub::GetDevicePolicyInner(uint32_t code, MessageParcel &data, MessageParcel &reply,
131 int32_t userId, int32_t hasUserId)
132 {
133 ErrCode errCode = GetDevicePolicy(code, data, reply, userId, hasUserId);
134 reply.WriteInt32(errCode);
135 return ERR_OK;
136 }
137
CheckAndGetAdminProvisionInfoInner(uint32_t code,MessageParcel & data,MessageParcel & reply,int32_t userId)138 ErrCode EnterpriseDeviceMgrStub::CheckAndGetAdminProvisionInfoInner(uint32_t code, MessageParcel &data,
139 MessageParcel &reply, int32_t userId)
140 {
141 ErrCode errCode = CheckAndGetAdminProvisionInfo(code, data, reply, userId);
142 reply.WriteInt32(errCode);
143 return ERR_OK;
144 }
145 } // namespace EDM
146 } // namespace OHOS