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 "ent_info.h"
20 #include "string_ex.h"
21 
22 using namespace OHOS::HiviewDFX;
23 
24 namespace OHOS {
25 namespace EDM {
26 #ifdef EDM_SUPPORT_ALL_ENABLE
27 constexpr int32_t DEFAULT_USER_ID = 100;
28 constexpr int32_t WITHOUT_FUNCTION_CODE = -1;
29 #endif
EnterpriseDeviceMgrStub()30 EnterpriseDeviceMgrStub::EnterpriseDeviceMgrStub() : IRemoteStub(true)
31 {
32     InitSystemCodeList();
33     EDMLOGI("EnterpriseDeviceMgrStub()");
34 }
35 
36 #ifdef EDM_SUPPORT_ALL_ENABLE
CallFuncByCode(uint32_t code, MessageParcel &data, MessageParcel &reply)37 ErrCode EnterpriseDeviceMgrStub::CallFuncByCode(uint32_t code, MessageParcel &data, MessageParcel &reply)
38 {
39     switch (code) {
40         case EdmInterfaceCode::ADD_DEVICE_ADMIN:
41             return EnableAdminInner(data, reply);
42         case EdmInterfaceCode::REMOVE_DEVICE_ADMIN:
43             return DisableAdminInner(data, reply);
44         case EdmInterfaceCode::REMOVE_SUPER_ADMIN:
45             return DisableSuperAdminInner(data, reply);
46         case EdmInterfaceCode::GET_ENABLED_ADMIN:
47             return GetEnabledAdminInner(data, reply);
48         case EdmInterfaceCode::GET_ENT_INFO:
49             return GetEnterpriseInfoInner(data, reply);
50         case EdmInterfaceCode::SET_ENT_INFO:
51             return SetEnterpriseInfoInner(data, reply);
52         case EdmInterfaceCode::IS_SUPER_ADMIN:
53             return IsSuperAdminInner(data, reply);
54         case EdmInterfaceCode::IS_ADMIN_ENABLED:
55             return IsAdminEnabledInner(data, reply);
56         case EdmInterfaceCode::SUBSCRIBE_MANAGED_EVENT:
57             return SubscribeManagedEventInner(data, reply);
58         case EdmInterfaceCode::UNSUBSCRIBE_MANAGED_EVENT:
59             return UnsubscribeManagedEventInner(data, reply);
60         case EdmInterfaceCode::AUTHORIZE_ADMIN:
61             return AuthorizeAdminInner(data, reply);
62         case EdmInterfaceCode::GET_SUPER_ADMIN_WANT_INFO:
63             return GetSuperAdminInner(data, reply);
64         default:
65             return WITHOUT_FUNCTION_CODE;
66     }
67 }
68 #endif
69 
InitSystemCodeList()70 void EnterpriseDeviceMgrStub::InitSystemCodeList()
71 {
72     systemCodeList = {
73         EdmInterfaceCode::ADD_DEVICE_ADMIN,
74         EdmInterfaceCode::REMOVE_SUPER_ADMIN,
75         EdmInterfaceCode::GET_ENABLED_ADMIN,
76         EdmInterfaceCode::GET_ENT_INFO,
77         EdmInterfaceCode::SET_ENT_INFO,
78         EdmInterfaceCode::IS_SUPER_ADMIN,
79         EdmInterfaceCode::IS_ADMIN_ENABLED,
80         EdmInterfaceCode::AUTHORIZE_ADMIN,
81         EdmInterfaceCode::GET_SUPER_ADMIN_WANT_INFO,
82     };
83 }
84 
OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)85 int32_t EnterpriseDeviceMgrStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
86     MessageOption &option)
87 {
88 #ifdef EDM_SUPPORT_ALL_ENABLE
89     std::u16string descriptor = GetDescriptor();
90     std::u16string remoteDescriptor = data.ReadInterfaceToken();
91     EDMLOGI("EnterpriseDeviceMgrStub code %{public}u", code);
92     if (descriptor != remoteDescriptor) {
93         EDMLOGE("EnterpriseDeviceMgrStub code %{public}d client and service descriptors are inconsistent", code);
94         reply.WriteInt32(EdmReturnErrCode::PARAM_ERROR);
95         return ERR_OK;
96     }
97     if (SERVICE_FLAG(code)) {
98         if (std::find(systemCodeList.begin(), systemCodeList.end(), code) != systemCodeList.end() &&
99             !GetAccessTokenMgr()->IsSystemAppOrNative()) {
100             EDMLOGE("EnterpriseDeviceMgrStub not system app or native process");
101             reply.WriteInt32(EdmReturnErrCode::SYSTEM_API_DENIED);
102             return ERR_OK;
103         }
104         ErrCode ret = CallFuncByCode(code, data, reply);
105         if (ret != WITHOUT_FUNCTION_CODE) {
106             return ret;
107         }
108     }
109     if (POLICY_FLAG(code)) {
110         EDMLOGD("POLICY_FLAG(code:%{public}x)\n", code);
111         int32_t hasUserId;
112         int32_t userId = DEFAULT_USER_ID;
113         data.ReadInt32(hasUserId);
114         if (hasUserId == 1) {
115             data.ReadInt32(userId);
116         }
117         if (FUNC_TO_OPERATE(code) == static_cast<int>(FuncOperateType::GET)) {
118             EDMLOGD("GetDevicePolicyInner");
119             return GetDevicePolicyInner(code, data, reply, userId);
120         } else {
121             EDMLOGD("HandleDevicePolicyInner");
122             return HandleDevicePolicyInner(code, data, reply, userId);
123         }
124     } else {
125         EDMLOGE("!POLICY_FLAG(code)");
126     }
127 
128     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
129 #else
130     EDMLOGI("EnterpriseDeviceMgrStub edm unsupport.");
131     reply.WriteInt32(EdmReturnErrCode::INTERFACE_UNSUPPORTED);
132     return ERR_OK;
133 #endif
134 }
135 
GetExternalManagerFactory()136 std::shared_ptr<IExternalManagerFactory> EnterpriseDeviceMgrStub::GetExternalManagerFactory()
137 {
138     return externalManagerFactory_;
139 }
140 
GetAccessTokenMgr()141 std::shared_ptr<IEdmAccessTokenManager> EnterpriseDeviceMgrStub::GetAccessTokenMgr()
142 {
143     return GetExternalManagerFactory()->CreateAccessTokenManager();
144 }
145 
EnableAdminInner(MessageParcel &data, MessageParcel &reply)146 ErrCode EnterpriseDeviceMgrStub::EnableAdminInner(MessageParcel &data, MessageParcel &reply)
147 {
148     std::unique_ptr<AppExecFwk::ElementName> admin(data.ReadParcelable<AppExecFwk::ElementName>());
149     if (!admin) {
150         reply.WriteInt32(EdmReturnErrCode::PARAM_ERROR);
151         return ERR_OK;
152     }
153     EDMLOGD("EnableAdminInner bundleName:: %{public}s : abilityName : %{public}s ", admin->GetBundleName().c_str(),
154         admin->GetAbilityName().c_str());
155     EntInfo entInfo;
156     if (!EntInfo::Unmarshalling(data, entInfo)) {
157         EDMLOGE("EnterpriseDeviceMgrStub::EnableAdminInner read parcel fail");
158         reply.WriteInt32(EdmReturnErrCode::PARAM_ERROR);
159         return ERR_OK;
160     }
161     int32_t type = data.ReadInt32();
162     int32_t userId = data.ReadInt32();
163     AdminType adminType = AdminType::UNKNOWN;
164     if (type == static_cast<int32_t>(AdminType::NORMAL) || type == static_cast<int32_t>(AdminType::ENT)) {
165         adminType = static_cast<AdminType>(type);
166     }
167     ErrCode retCode = EnableAdmin(*admin, entInfo, adminType, userId);
168     reply.WriteInt32(retCode);
169     return ERR_OK;
170 }
171 
DisableAdminInner(MessageParcel &data, MessageParcel &reply)172 ErrCode EnterpriseDeviceMgrStub::DisableAdminInner(MessageParcel &data, MessageParcel &reply)
173 {
174     std::unique_ptr<AppExecFwk::ElementName> admin(data.ReadParcelable<AppExecFwk::ElementName>());
175     if (!admin) {
176         reply.WriteInt32(EdmReturnErrCode::PARAM_ERROR);
177         return ERR_OK;
178     }
179     int32_t userId = data.ReadInt32();
180     ErrCode retCode = DisableAdmin(*admin, userId);
181     reply.WriteInt32(retCode);
182     return ERR_OK;
183 }
184 
DisableSuperAdminInner(MessageParcel &data, MessageParcel &reply)185 ErrCode EnterpriseDeviceMgrStub::DisableSuperAdminInner(MessageParcel &data, MessageParcel &reply)
186 {
187     std::string bundleName = data.ReadString();
188     EDMLOGD("DisableSuperAdminInner bundleName:: %{public}s :", bundleName.c_str());
189     ErrCode retCode = DisableSuperAdmin(bundleName);
190     reply.WriteInt32(retCode);
191     return retCode;
192 }
193 
HandleDevicePolicyInner(uint32_t code, MessageParcel &data, MessageParcel &reply, int32_t userId)194 ErrCode EnterpriseDeviceMgrStub::HandleDevicePolicyInner(uint32_t code, MessageParcel &data, MessageParcel &reply,
195     int32_t userId)
196 {
197     std::unique_ptr<AppExecFwk::ElementName> admin(data.ReadParcelable<AppExecFwk::ElementName>());
198     if (!admin) {
199         EDMLOGW("HandleDevicePolicyInner: ReadParcelable failed");
200         reply.WriteInt32(EdmReturnErrCode::PARAM_ERROR);
201         return ERR_OK;
202     }
203     ErrCode errCode = HandleDevicePolicy(code, *admin, data, reply, userId);
204     reply.WriteInt32(errCode);
205     return ERR_OK;
206 }
207 
GetDevicePolicyInner(uint32_t code, MessageParcel &data, MessageParcel &reply, int32_t userId)208 ErrCode EnterpriseDeviceMgrStub::GetDevicePolicyInner(uint32_t code, MessageParcel &data, MessageParcel &reply,
209     int32_t userId)
210 {
211     ErrCode errCode = GetDevicePolicy(code, data, reply, userId);
212     reply.WriteInt32(errCode);
213     return ERR_OK;
214 }
215 
GetEnabledAdminInner(MessageParcel &data, MessageParcel &reply)216 ErrCode EnterpriseDeviceMgrStub::GetEnabledAdminInner(MessageParcel &data, MessageParcel &reply)
217 {
218     EDMLOGD("EnterpriseDeviceMgrStub:GetEnabledAdmin");
219     int32_t type = static_cast<int32_t>(AdminType::UNKNOWN);
220     if (!data.ReadInt32(type)) {
221         reply.WriteInt32(EdmReturnErrCode::PARAM_ERROR);
222         EDMLOGE("EnterpriseDeviceMgrStub:GetEnabledAdminInner read type fail %{public}u", type);
223         return ERR_OK;
224     }
225     std::vector<std::string> enabledAdminList;
226     ErrCode res = GetEnabledAdmin((AdminType)type, enabledAdminList);
227     if (FAILED(res)) {
228         EDMLOGE("EnterpriseDeviceMgrStub:GetEnabledAdmin failed:%{public}d", res);
229         reply.WriteInt32(res);
230         return ERR_OK;
231     }
232     reply.WriteInt32(ERR_OK);
233     reply.WriteStringVector(enabledAdminList);
234     return ERR_OK;
235 }
236 
GetEnterpriseInfoInner(MessageParcel &data, MessageParcel &reply)237 ErrCode EnterpriseDeviceMgrStub::GetEnterpriseInfoInner(MessageParcel &data, MessageParcel &reply)
238 {
239     EDMLOGD("EnterpriseDeviceMgrStub:GetEnterpriseInfoInner");
240     std::unique_ptr<AppExecFwk::ElementName> admin(data.ReadParcelable<AppExecFwk::ElementName>());
241     if (!admin) {
242         reply.WriteInt32(EdmReturnErrCode::PARAM_ERROR);
243         return ERR_OK;
244     }
245     EDMLOGD("GetEnterpriseInfoInner bundleName:: %{public}s : abilityName : %{public}s ",
246         admin->GetBundleName().c_str(), admin->GetAbilityName().c_str());
247 
248     return GetEnterpriseInfo(*admin, reply);
249 }
250 
IsSuperAdminInner(MessageParcel &data, MessageParcel &reply)251 ErrCode EnterpriseDeviceMgrStub::IsSuperAdminInner(MessageParcel &data, MessageParcel &reply)
252 {
253     EDMLOGD("EnterpriseDeviceMgrStub:IsSuperAdminInner");
254     std::string bundleName = data.ReadString();
255     EDMLOGD("IsSuperAdminInner bundleName:: %{public}s :", bundleName.c_str());
256     bool ret = IsSuperAdmin(bundleName);
257     reply.WriteInt32(ERR_OK);
258     reply.WriteBool(ret);
259     return ERR_OK;
260 }
261 
IsAdminEnabledInner(MessageParcel &data, MessageParcel &reply)262 ErrCode EnterpriseDeviceMgrStub::IsAdminEnabledInner(MessageParcel &data, MessageParcel &reply)
263 {
264     EDMLOGD("EnterpriseDeviceMgrStub:IsAdminEnabled");
265     std::unique_ptr<AppExecFwk::ElementName> admin(data.ReadParcelable<AppExecFwk::ElementName>());
266     if (!admin) {
267         EDMLOGE("EnterpriseDeviceMgrStub::IsAdminEnabledInner read parcel fail");
268         reply.WriteInt32(EdmReturnErrCode::PARAM_ERROR);
269         return ERR_OK;
270     }
271     int32_t userId = data.ReadInt32();
272     bool ret = IsAdminEnabled(*admin, userId);
273     reply.WriteInt32(ERR_OK);
274     reply.WriteBool(ret);
275     return ERR_OK;
276 }
277 
SetEnterpriseInfoInner(MessageParcel &data, MessageParcel &reply)278 ErrCode EnterpriseDeviceMgrStub::SetEnterpriseInfoInner(MessageParcel &data, MessageParcel &reply)
279 {
280     EDMLOGD("EnterpriseDeviceMgrStub:SetEnterpriseInfoInner");
281     std::unique_ptr<AppExecFwk::ElementName> admin(data.ReadParcelable<AppExecFwk::ElementName>());
282     if (!admin) {
283         reply.WriteInt32(EdmReturnErrCode::PARAM_ERROR);
284         return ERR_OK;
285     }
286     EntInfo entInfo;
287     if (!EntInfo::Unmarshalling(data, entInfo)) {
288         reply.WriteInt32(EdmReturnErrCode::PARAM_ERROR);
289         return ERR_OK;
290     }
291     ErrCode ret = SetEnterpriseInfo(*admin, entInfo);
292     reply.WriteInt32(ret);
293     return ERR_OK;
294 }
295 
SubscribeManagedEventInner(MessageParcel &data, MessageParcel &reply)296 ErrCode EnterpriseDeviceMgrStub::SubscribeManagedEventInner(MessageParcel &data, MessageParcel &reply)
297 {
298     EDMLOGD("EnterpriseDeviceMgrStub:SubscribeManagedEventInner");
299     return SubscribeManagedEventInner(data, reply, true);
300 }
301 
UnsubscribeManagedEventInner(MessageParcel &data, MessageParcel &reply)302 ErrCode EnterpriseDeviceMgrStub::UnsubscribeManagedEventInner(MessageParcel &data, MessageParcel &reply)
303 {
304     EDMLOGD("EnterpriseDeviceMgrStub:UnsubscribeManagedEventInner");
305     return SubscribeManagedEventInner(data, reply, false);
306 }
307 
SubscribeManagedEventInner(MessageParcel &data, MessageParcel &reply, bool subscribe)308 ErrCode EnterpriseDeviceMgrStub::SubscribeManagedEventInner(MessageParcel &data, MessageParcel &reply, bool subscribe)
309 {
310     std::unique_ptr<AppExecFwk::ElementName> admin(data.ReadParcelable<AppExecFwk::ElementName>());
311     if (!admin) {
312         reply.WriteInt32(EdmReturnErrCode::PARAM_ERROR);
313         return ERR_OK;
314     }
315     std::vector<uint32_t> events;
316     data.ReadUInt32Vector(&events);
317     ErrCode code;
318     if (subscribe) {
319         code = SubscribeManagedEvent(*admin, events);
320     } else {
321         code = UnsubscribeManagedEvent(*admin, events);
322     }
323     reply.WriteInt32(code);
324     return ERR_OK;
325 }
326 
AuthorizeAdminInner(MessageParcel &data, MessageParcel &reply)327 ErrCode EnterpriseDeviceMgrStub::AuthorizeAdminInner(MessageParcel &data, MessageParcel &reply)
328 {
329     EDMLOGD("EnterpriseDeviceMgrStub:AuthorizeAdminInner.");
330     std::unique_ptr<AppExecFwk::ElementName> admin(data.ReadParcelable<AppExecFwk::ElementName>());
331     if (!admin) {
332         reply.WriteInt32(EdmReturnErrCode::PARAM_ERROR);
333         return ERR_OK;
334     }
335     std::string bundleName = data.ReadString();
336     ErrCode ret = AuthorizeAdmin(*admin, bundleName);
337     reply.WriteInt32(ret);
338     return ERR_OK;
339 }
340 
GetSuperAdminInner(MessageParcel &data, MessageParcel &reply)341 ErrCode EnterpriseDeviceMgrStub::GetSuperAdminInner(MessageParcel &data, MessageParcel &reply)
342 {
343     EDMLOGD("EnterpriseDeviceMgrStub:IsSuperAdminInner");
344     ErrCode ret = GetSuperAdmin(reply);
345     reply.WriteInt32(ERR_OK);
346     reply.WriteBool(ret);
347     return ERR_OK;
348 }
349 } // namespace EDM
350 } // namespace OHOS