1 /*
2 * Copyright (c) 2023 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 "disable_usb_plugin.h"
17
18 #include "bool_serializer.h"
19 #include "edm_constants.h"
20 #include "edm_ipc_interface_code.h"
21 #include "edm_utils.h"
22 #include "usb_policy_utils.h"
23 #include "plugin_manager.h"
24
25 namespace OHOS {
26 namespace EDM {
27 const bool REGISTER_RESULT = PluginManager::GetInstance()->AddPlugin(DisableUsbPlugin::GetPlugin());
28
InitPlugin(std::shared_ptr<IPluginTemplate<DisableUsbPlugin,bool>> ptr)29 void DisableUsbPlugin::InitPlugin(std::shared_ptr<IPluginTemplate<DisableUsbPlugin, bool>> ptr)
30 {
31 EDMLOGI("DisableUsbPlugin InitPlugin...");
32 std::map<std::string, std::string> perms;
33 perms.insert(std::make_pair(EdmConstants::PERMISSION_TAG_VERSION_11,
34 "ohos.permission.ENTERPRISE_MANAGE_USB"));
35 perms.insert(std::make_pair(EdmConstants::PERMISSION_TAG_VERSION_12,
36 "ohos.permission.ENTERPRISE_MANAGE_RESTRICTIONS"));
37 IPlugin::PolicyPermissionConfig config = IPlugin::PolicyPermissionConfig(perms,
38 IPlugin::PermissionType::SUPER_DEVICE_ADMIN, IPlugin::ApiType::PUBLIC);
39 ptr->InitAttribute(EdmInterfaceCode::DISABLE_USB, "disable_usb", config, true);
40 ptr->SetSerializer(BoolSerializer::GetInstance());
41 ptr->SetOnHandlePolicyListener(&DisableUsbPlugin::OnSetPolicy, FuncOperateType::SET);
42 ptr->SetOnAdminRemoveListener(&DisableUsbPlugin::OnAdminRemove);
43 }
44
OnSetPolicy(bool & data)45 ErrCode DisableUsbPlugin::OnSetPolicy(bool &data)
46 {
47 EDMLOGI("DisableUsbPlugin OnSetPolicy...disable = %{public}d", data);
48 auto policyManager = IPolicyManager::GetInstance();
49 std::string allowUsbDevicePolicy;
50 policyManager->GetPolicy("", "allowed_usb_devices", allowUsbDevicePolicy);
51 std::string usbStoragePolicy;
52 policyManager->GetPolicy("", "usb_read_only", usbStoragePolicy);
53 if (data && (!allowUsbDevicePolicy.empty() ||
54 usbStoragePolicy == std::to_string(EdmConstants::STORAGE_USB_POLICY_DISABLED) ||
55 usbStoragePolicy == std::to_string(EdmConstants::STORAGE_USB_POLICY_READ_ONLY))) {
56 EDMLOGE("DisableUsbPlugin OnSetPolicy: CONFLICT! allowedUsbDevice: %{public}s, usbStoragePolicy: %{public}s",
57 allowUsbDevicePolicy.c_str(), usbStoragePolicy.c_str());
58 return EdmReturnErrCode::CONFIGURATION_CONFLICT_FAILED;
59 }
60 return UsbPolicyUtils::SetUsbDisabled(data);
61 }
62
OnGetPolicy(std::string & policyData,MessageParcel & data,MessageParcel & reply,int32_t userId)63 ErrCode DisableUsbPlugin::OnGetPolicy(std::string &policyData, MessageParcel &data, MessageParcel &reply,
64 int32_t userId)
65 {
66 EDMLOGI("DisableUsbPlugin OnGetPolicy %{public}s...", policyData.c_str());
67 bool isDisabled = false;
68 pluginInstance_->serializer_->Deserialize(policyData, isDisabled);
69 reply.WriteInt32(ERR_OK);
70 reply.WriteBool(isDisabled);
71 return ERR_OK;
72 }
73
OnAdminRemove(const std::string & adminName,bool & data,int32_t userId)74 ErrCode DisableUsbPlugin::OnAdminRemove(const std::string &adminName, bool &data, int32_t userId)
75 {
76 EDMLOGI("DisableUsbPlugin OnAdminRemove %{public}d...", data);
77 if (!data) {
78 return ERR_OK;
79 }
80 return UsbPolicyUtils::SetUsbDisabled(!data);
81 }
82 } // namespace EDM
83 } // namespace OHOS
84