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_stub.h"
17 #include "admin.h"
18 #include "ent_info.h"
19 #include "string_ex.h"
20
21 using namespace OHOS::HiviewDFX;
22
23 namespace OHOS {
24 namespace EDM {
EnterpriseDeviceMgrStub()25 EnterpriseDeviceMgrStub::EnterpriseDeviceMgrStub()
26 {
27 AddCallFuncMap();
28 EDMLOGI("EnterpriseDeviceMgrStub()");
29 }
30
~EnterpriseDeviceMgrStub()31 EnterpriseDeviceMgrStub::~EnterpriseDeviceMgrStub()
32 {
33 EDMLOGI("~EnterpriseDeviceMgrStub()");
34 }
35
AddCallFuncMap()36 void EnterpriseDeviceMgrStub::AddCallFuncMap()
37 {
38 memberFuncMap_[ADD_DEVICE_ADMIN] = &EnterpriseDeviceMgrStub::ActiveAdminInner;
39 memberFuncMap_[REMOVE_DEVICE_ADMIN] = &EnterpriseDeviceMgrStub::DeactiveAdminInner;
40 memberFuncMap_[REMOVE_SUPER_ADMIN] = &EnterpriseDeviceMgrStub::DeactiveSuperAdminInner;
41 memberFuncMap_[GET_ACTIVE_ADMIN] = &EnterpriseDeviceMgrStub::GetActiveAdminInner;
42 memberFuncMap_[GET_ENT_INFO] = &EnterpriseDeviceMgrStub::GetEnterpriseInfoInner;
43 memberFuncMap_[SET_ENT_INFO] = &EnterpriseDeviceMgrStub::SetEnterpriseInfoInner;
44 memberFuncMap_[IS_SUPER_ADMIN] = &EnterpriseDeviceMgrStub::IsSuperAdminInner;
45 memberFuncMap_[IS_ADMIN_ACTIVE] = &EnterpriseDeviceMgrStub::IsAdminActiveInner;
46 }
47
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)48 int32_t EnterpriseDeviceMgrStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
49 MessageOption &option)
50 {
51 std::u16string descriptor = GetDescriptor();
52 std::u16string remoteDescriptor = data.ReadInterfaceToken();
53 EDMLOGI("EnterpriseDeviceMgrStub code %{public}u", code);
54 if (descriptor != remoteDescriptor) {
55 EDMLOGE("EnterpriseDeviceMgrStub code %{public}d client and service descriptors are inconsistent", code);
56 reply.WriteInt32(ERR_EDM_PARAM_ERROR);
57 return ERR_EDM_PARAM_ERROR;
58 }
59 if (SERVICE_FLAG(code)) {
60 auto func = memberFuncMap_.find(code);
61 if (func != memberFuncMap_.end()) {
62 auto memberFunc = func->second;
63 if (memberFunc != nullptr) {
64 return (this->*memberFunc)(data, reply);
65 }
66 }
67 }
68 if (POLICY_FLAG(code)) {
69 EDMLOGD("POLICY_FLAG(code:%{public}x)\n", code);
70 if (FUNC_TO_OPERATE(code) == static_cast<int>(FuncOperateType::GET)) {
71 EDMLOGD("GetDevicePolicyInner");
72 return GetDevicePolicyInner(code, data, reply);
73 } else {
74 EDMLOGD("HandleDevicePolicyInner");
75 return HandleDevicePolicyInner(code, data, reply);
76 }
77 } else {
78 EDMLOGE("!POLICY_FLAG(code)");
79 }
80
81 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
82 }
83
ActiveAdminInner(MessageParcel & data,MessageParcel & reply)84 ErrCode EnterpriseDeviceMgrStub::ActiveAdminInner(MessageParcel &data, MessageParcel &reply)
85 {
86 std::unique_ptr<AppExecFwk::ElementName> admin(data.ReadParcelable<AppExecFwk::ElementName>());
87 if (!admin) {
88 reply.WriteInt32(ERR_EDM_PARAM_ERROR);
89 return ERR_EDM_PARAM_ERROR;
90 }
91 EDMLOGD("ActiveAdminInner bundleName:: %{public}s : abilityName : %{public}s ", admin->GetBundleName().c_str(),
92 admin->GetAbilityName().c_str());
93 std::unique_ptr<EntInfo> entInfo(data.ReadParcelable<EntInfo>());
94 uint32_t type = data.ReadUint32();
95 int32_t userId = data.ReadInt32();
96 ErrCode retCode = ActiveAdmin(*admin, *entInfo, AdminType(type), userId);
97 reply.WriteInt32(retCode);
98 return (retCode != ERR_OK) ? ERR_EDM_ADD_ADMIN_FAILED : ERR_OK;
99 }
100
DeactiveAdminInner(MessageParcel & data,MessageParcel & reply)101 ErrCode EnterpriseDeviceMgrStub::DeactiveAdminInner(MessageParcel &data, MessageParcel &reply)
102 {
103 std::unique_ptr<AppExecFwk::ElementName> admin(data.ReadParcelable<AppExecFwk::ElementName>());
104 if (!admin) {
105 reply.WriteInt32(ERR_EDM_PARAM_ERROR);
106 return ERR_EDM_PARAM_ERROR;
107 }
108 int32_t userId = data.ReadInt32();
109 ErrCode retCode = DeactiveAdmin(*admin, userId);
110 if (retCode != ERR_OK) {
111 EDMLOGW("DeactiveAdminInner failed: %{public}d", retCode);
112 reply.WriteInt32(ERR_EDM_DEL_ADMIN_FAILED);
113 return ERR_EDM_DEL_ADMIN_FAILED;
114 }
115 reply.WriteInt32(ERR_OK);
116 return ERR_OK;
117 }
118
DeactiveSuperAdminInner(MessageParcel & data,MessageParcel & reply)119 ErrCode EnterpriseDeviceMgrStub::DeactiveSuperAdminInner(MessageParcel &data, MessageParcel &reply)
120 {
121 std::string bundleName = data.ReadString();
122 EDMLOGD("DeactiveSuperAdminInner bundleName:: %{public}s :", bundleName.c_str());
123 ErrCode retCode = DeactiveSuperAdmin(bundleName);
124 if (retCode != ERR_OK) {
125 reply.WriteInt32(ERR_EDM_DEL_SUPER_ADMIN_FAILED);
126 return ERR_EDM_DEL_SUPER_ADMIN_FAILED;
127 }
128 reply.WriteInt32(ERR_OK);
129 return retCode;
130 }
131
HandleDevicePolicyInner(uint32_t code,MessageParcel & data,MessageParcel & reply)132 ErrCode EnterpriseDeviceMgrStub::HandleDevicePolicyInner(uint32_t code, MessageParcel &data, MessageParcel &reply)
133 {
134 std::unique_ptr<AppExecFwk::ElementName> admin(data.ReadParcelable<AppExecFwk::ElementName>());
135 if (!admin) {
136 EDMLOGW("HandleDevicePolicyInner: ReadParcelable failed");
137 return ERR_EDM_PARAM_ERROR;
138 }
139 ErrCode errCode = HandleDevicePolicy(code, *admin, data);
140 reply.WriteInt32(errCode);
141 return errCode;
142 }
143
GetDevicePolicyInner(uint32_t code,MessageParcel & data,MessageParcel & reply)144 ErrCode EnterpriseDeviceMgrStub::GetDevicePolicyInner(uint32_t code, MessageParcel &data, MessageParcel &reply)
145 {
146 AppExecFwk::ElementName *admin = nullptr;
147 if (data.ReadInt32() != ERR_OK) {
148 admin = AppExecFwk::ElementName::Unmarshalling(data);
149 } else {
150 admin = nullptr;
151 }
152 if (admin != nullptr) {
153 EDMLOGD("GetDevicePolicyInner bundleName:: %{public}s : abilityName : %{public}s code : %{public}x",
154 admin->GetBundleName().c_str(), admin->GetAbilityName().c_str(), code);
155 }
156 ErrCode retCode = GetDevicePolicy(code, admin, reply);
157 delete admin;
158 return retCode;
159 }
160
GetActiveAdminInner(MessageParcel & data,MessageParcel & reply)161 ErrCode EnterpriseDeviceMgrStub::GetActiveAdminInner(MessageParcel &data, MessageParcel &reply)
162 {
163 EDMLOGD("EnterpriseDeviceMgrStub:GetActiveAdmin");
164 uint32_t type = AdminType::UNKNOWN;
165 if (!data.ReadUint32(type)) {
166 reply.WriteInt32(ERR_EDM_PARAM_ERROR);
167 EDMLOGE("EnterpriseDeviceMgrStub:GetActiveAdminInner read type fail %{public}u", type);
168 return ERR_EDM_PARAM_ERROR;
169 }
170 std::vector<std::string> activeAdminList;
171 ErrCode res = GetActiveAdmin((AdminType)type, activeAdminList);
172 if (FAILED(res)) {
173 EDMLOGE("EnterpriseDeviceMgrStub:GetActiveAdmin failed:%{public}d", res);
174 reply.WriteInt32(res);
175 return res;
176 }
177 reply.WriteInt32(ERR_OK);
178 std::vector<std::u16string> writeArray;
179 ArrayPolicyUtils::StringToU16String(activeAdminList, writeArray);
180 reply.WriteString16Vector(writeArray);
181 return ERR_OK;
182 }
183
GetEnterpriseInfoInner(MessageParcel & data,MessageParcel & reply)184 ErrCode EnterpriseDeviceMgrStub::GetEnterpriseInfoInner(MessageParcel &data, MessageParcel &reply)
185 {
186 EDMLOGD("EnterpriseDeviceMgrStub:GetEnterpriseInfoInner");
187 std::unique_ptr<AppExecFwk::ElementName> admin(data.ReadParcelable<AppExecFwk::ElementName>());
188 if (!admin) {
189 reply.WriteInt32(ERR_EDM_PARAM_ERROR);
190 return ERR_EDM_PARAM_ERROR;
191 }
192 EDMLOGD("GetEnterpriseInfoInner bundleName:: %{public}s : abilityName : %{public}s ",
193 admin->GetBundleName().c_str(), admin->GetAbilityName().c_str());
194
195 return GetEnterpriseInfo(*admin, reply);
196 }
197
IsSuperAdminInner(MessageParcel & data,MessageParcel & reply)198 ErrCode EnterpriseDeviceMgrStub::IsSuperAdminInner(MessageParcel &data, MessageParcel &reply)
199 {
200 EDMLOGD("EnterpriseDeviceMgrStub:IsSuperAdminInner");
201 std::string bundleName = data.ReadString();
202 EDMLOGD("IsSuperAdminInner bundleName:: %{public}s :", bundleName.c_str());
203 bool ret = IsSuperAdmin(bundleName);
204 reply.WriteInt32(ret);
205 return ERR_OK;
206 }
207
IsAdminActiveInner(MessageParcel & data,MessageParcel & reply)208 ErrCode EnterpriseDeviceMgrStub::IsAdminActiveInner(MessageParcel &data, MessageParcel &reply)
209 {
210 EDMLOGD("EnterpriseDeviceMgrStub:IsAdminActive");
211 std::unique_ptr<AppExecFwk::ElementName> admin(data.ReadParcelable<AppExecFwk::ElementName>());
212 if (!admin) {
213 reply.WriteInt32(ERR_EDM_PARAM_ERROR);
214 return ERR_EDM_PARAM_ERROR;
215 }
216 EDMLOGD("GetEnterpriseInfoInner bundleName:: %{public}s : abilityName : %{public}s ",
217 admin->GetBundleName().c_str(), admin->GetAbilityName().c_str());
218 bool ret = IsAdminActive(*admin);
219 reply.WriteInt32(ret);
220 return ERR_OK;
221 }
222
SetEnterpriseInfoInner(MessageParcel & data,MessageParcel & reply)223 ErrCode EnterpriseDeviceMgrStub::SetEnterpriseInfoInner(MessageParcel &data, MessageParcel &reply)
224 {
225 EDMLOGD("EnterpriseDeviceMgrStub:SetEnterpriseInfoInner");
226 std::unique_ptr<AppExecFwk::ElementName> admin(data.ReadParcelable<AppExecFwk::ElementName>());
227 if (!admin) {
228 reply.WriteInt32(ERR_EDM_PARAM_ERROR);
229 return ERR_EDM_PARAM_ERROR;
230 }
231 std::unique_ptr<EntInfo> entInfo(data.ReadParcelable<EntInfo>());
232 ErrCode code = SetEnterpriseInfo(*admin, *entInfo);
233 if (code != ERR_OK) {
234 EDMLOGW("EnterpriseDeviceMgrStub:SetEnterpriseInfoInner failed:%{public}d", code);
235 reply.WriteInt32(ERR_EDM_SET_ENTINFO_FAILED);
236 return code;
237 }
238 reply.WriteInt32(ERR_OK);
239 return ERR_OK;
240 }
241 } // namespace EDM
242 } // namespace OHOS