• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-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 "allowed_usb_devices_plugin.h"
17 
18 #include <algorithm>
19 #include <system_ability_definition.h>
20 #include "array_usb_device_id_serializer.h"
21 #include "edm_constants.h"
22 #include "edm_ipc_interface_code.h"
23 #include "edm_sys_manager.h"
24 #include "edm_utils.h"
25 #include "usb_device.h"
26 #include "usb_device_id.h"
27 #include "usb_policy_utils.h"
28 #include "usb_srv_client.h"
29 #include "iplugin_manager.h"
30 
31 namespace OHOS {
32 namespace EDM {
33 const bool REGISTER_RESULT = IPluginManager::GetInstance()->AddPlugin(AllowUsbDevicesPlugin::GetPlugin());
34 
InitPlugin(std::shared_ptr<IPluginTemplate<AllowUsbDevicesPlugin,std::vector<UsbDeviceId>>> ptr)35 void AllowUsbDevicesPlugin::InitPlugin(
36     std::shared_ptr<IPluginTemplate<AllowUsbDevicesPlugin, std::vector<UsbDeviceId>>> ptr)
37 {
38     EDMLOGI("AllowUsbDevicesPlugin InitPlugin...");
39     ptr->InitAttribute(EdmInterfaceCode::ALLOWED_USB_DEVICES, "allowed_usb_devices",
40         EdmPermission::PERMISSION_ENTERPRISE_MANAGE_USB, IPlugin::PermissionType::SUPER_DEVICE_ADMIN, true);
41     ptr->SetSerializer(ArrayUsbDeviceIdSerializer::GetInstance());
42     ptr->SetOnHandlePolicyListener(&AllowUsbDevicesPlugin::OnSetPolicy, FuncOperateType::SET);
43     ptr->SetOnHandlePolicyListener(&AllowUsbDevicesPlugin::OnRemovePolicy, FuncOperateType::REMOVE);
44     ptr->SetOnAdminRemoveListener(&AllowUsbDevicesPlugin::OnAdminRemove);
45 }
46 
OnSetPolicy(std::vector<UsbDeviceId> & data,std::vector<UsbDeviceId> & currentData,std::vector<UsbDeviceId> & mergeData,int32_t userId)47 ErrCode AllowUsbDevicesPlugin::OnSetPolicy(std::vector<UsbDeviceId> &data,
48     std::vector<UsbDeviceId> &currentData, std::vector<UsbDeviceId> &mergeData, int32_t userId)
49 {
50     EDMLOGI("AllowUsbDevicesPlugin OnSetPolicy userId = %{public}d", userId);
51     if (data.empty()) {
52         EDMLOGW("AllowUsbDevicesPlugin OnSetPolicy data is empty");
53         return ERR_OK;
54     }
55     if (data.size() > EdmConstants::ALLOWED_USB_DEVICES_MAX_SIZE) {
56         EDMLOGE("AllowUsbDevicesPlugin OnSetPolicy data size=[%{public}zu] is too large", data.size());
57         return EdmReturnErrCode::PARAM_ERROR;
58     }
59     if (HasConflictPolicy()) {
60         return EdmReturnErrCode::CONFIGURATION_CONFLICT_FAILED;
61     }
62 
63     std::vector<UsbDeviceId> needAddData =
64         ArrayUsbDeviceIdSerializer::GetInstance()->SetDifferencePolicyData(currentData, data);
65     std::vector<UsbDeviceId> needAddMergeData =
66         ArrayUsbDeviceIdSerializer::GetInstance()->SetDifferencePolicyData(mergeData, needAddData);
67     std::vector<UsbDeviceId> afterHandle =
68         ArrayUsbDeviceIdSerializer::GetInstance()->SetUnionPolicyData(currentData, needAddData);
69     std::vector<UsbDeviceId> afterMerge =
70         ArrayUsbDeviceIdSerializer::GetInstance()->SetUnionPolicyData(mergeData, afterHandle);
71 
72     if (afterMerge.size() > EdmConstants::ALLOWED_USB_DEVICES_MAX_SIZE) {
73         EDMLOGE("AllowUsbDevicesPlugin OnSetPolicy union data size=[%{public}zu] is too large", mergeData.size());
74         return EdmReturnErrCode::PARAM_ERROR;
75     }
76     ErrCode errCode = UsbPolicyUtils::AddAllowedUsbDevices(needAddMergeData);
77     if (errCode != ERR_OK) {
78         return errCode;
79     }
80     currentData = afterHandle;
81     mergeData = afterMerge;
82     return ERR_OK;
83 }
84 
HasConflictPolicy()85 bool AllowUsbDevicesPlugin::HasConflictPolicy()
86 {
87     auto policyManager = IPolicyManager::GetInstance();
88     std::string disableUsbPolicy;
89     policyManager->GetPolicy("", "disable_usb", disableUsbPolicy);
90     if (disableUsbPolicy == "true") {
91         EDMLOGE("AllowUsbDevicesPlugin POLICY CONFLICT! Usb is disabled.");
92         return true;
93     }
94 
95     std::string usbStoragePolicy;
96     policyManager->GetPolicy("", "usb_read_only", usbStoragePolicy);
97     if (usbStoragePolicy == std::to_string(EdmConstants::STORAGE_USB_POLICY_DISABLED)) {
98         EDMLOGE("AllowUsbDevicesPlugin POLICY CONFLICT! usbStoragePolicy is disabled.");
99         return true;
100     }
101 
102     std::string disallowUsbDevicePolicy;
103     policyManager->GetPolicy("", "disallowed_usb_devices", disallowUsbDevicePolicy);
104     if (!disallowUsbDevicePolicy.empty()) {
105         EDMLOGE("AllowUsbDevicesPlugin POLICY CONFLICT! disallowedUsbDevice: %{public}s",
106             disallowUsbDevicePolicy.c_str());
107         return true;
108     }
109     return false;
110 }
111 
OnRemovePolicy(std::vector<UsbDeviceId> & data,std::vector<UsbDeviceId> & currentData,std::vector<UsbDeviceId> & mergeData,int32_t userId)112 ErrCode AllowUsbDevicesPlugin::OnRemovePolicy(std::vector<UsbDeviceId> &data, std::vector<UsbDeviceId> &currentData,
113     std::vector<UsbDeviceId> &mergeData, int32_t userId)
114 {
115     EDMLOGD("AllowUsbDevicesPlugin OnRemovePolicy userId : %{public}d:", userId);
116     if (data.empty()) {
117         EDMLOGW("AllowUsbDevicesPlugin OnRemovePolicy data is empty:");
118         return ERR_OK;
119     }
120     if (data.size() > EdmConstants::ALLOWED_USB_DEVICES_MAX_SIZE) {
121         EDMLOGE("AllowUsbDevicesPlugin OnRemovePolicy input data is too large");
122         return EdmReturnErrCode::PARAM_ERROR;
123     }
124 
125     std::vector<UsbDeviceId> afterHandle =
126         ArrayUsbDeviceIdSerializer::GetInstance()->SetDifferencePolicyData(data, currentData);
127     std::vector<UsbDeviceId> afterMerge =
128         ArrayUsbDeviceIdSerializer::GetInstance()->SetUnionPolicyData(mergeData, afterHandle);
129 
130     if (afterMerge.empty()) {
131         auto &srvClient = OHOS::USB::UsbSrvClient::GetInstance();
132         std::vector<OHOS::USB::UsbDevice> allDevices;
133         int32_t getRet = srvClient.GetDevices(allDevices);
134         if (getRet == EdmConstants::USB_ERRCODE_INTERFACE_NO_INIT) {
135             EDMLOGW("AllowUsbDevicesPlugin OnRemovePolicy: getDevices failed! USB interface not init!");
136         }
137         if (getRet != ERR_OK && getRet != EdmConstants::USB_ERRCODE_INTERFACE_NO_INIT) {
138             EDMLOGE("AllowUsbDevicesPlugin OnRemovePolicy getDevices failed: %{public}d", getRet);
139             return EdmReturnErrCode::SYSTEM_ABNORMALLY;
140         }
141         EDMLOGI("AllowUsbDevicesPlugin OnRemovePolicy: clear to empty, enable all.");
142         std::for_each(allDevices.begin(), allDevices.end(), [&](const auto usbDevice) {
143             if (srvClient.ManageDevice(usbDevice.GetVendorId(), usbDevice.GetProductId(), false) != ERR_OK) {
144                 EDMLOGW("AllowUsbDevicesPlugin OnRemovePolicy ManageDevice vid: %{public}d, pid: %{public}d failed!",
145                     usbDevice.GetVendorId(), usbDevice.GetProductId());
146             }
147         });
148     } else {
149         std::vector<UsbDeviceId> needRemovePolicy =
150             ArrayUsbDeviceIdSerializer::GetInstance()->SetDifferencePolicyData(afterHandle, currentData);
151         std::vector<UsbDeviceId> needRemoveMergePolicy =
152             ArrayUsbDeviceIdSerializer::GetInstance()->SetDifferencePolicyData(mergeData, needRemovePolicy);
153         EDMLOGI("AllowUsbDevicesPlugin OnRemovePolicy: remove data size: %{public}zu", needRemoveMergePolicy.size());
154         auto &srvClient = OHOS::USB::UsbSrvClient::GetInstance();
155         std::for_each(needRemoveMergePolicy.begin(), needRemoveMergePolicy.end(), [&](const auto usbDeviceId) {
156             if (srvClient.ManageDevice(usbDeviceId.GetVendorId(), usbDeviceId.GetProductId(), true) != ERR_OK) {
157                 EDMLOGW("AllowUsbDevicesPlugin OnRemovePolicy ManageDevice vid: %{public}d, pid: %{public}d failed!",
158                     usbDeviceId.GetVendorId(), usbDeviceId.GetProductId());
159             }
160         });
161     }
162     currentData = afterHandle;
163     mergeData = afterMerge;
164     return ERR_OK;
165 }
166 
OnAdminRemove(const std::string & adminName,std::vector<UsbDeviceId> & data,std::vector<UsbDeviceId> & mergeData,int32_t userId)167 ErrCode AllowUsbDevicesPlugin::OnAdminRemove(const std::string &adminName, std::vector<UsbDeviceId> &data,
168     std::vector<UsbDeviceId> &mergeData, int32_t userId)
169 {
170     EDMLOGD("AllowUsbDevicesPlugin OnAdminRemove");
171     if (data.empty()) {
172         EDMLOGW("AllowUsbDevicesPlugin OnRemovePolicy data is empty:");
173         return ERR_OK;
174     }
175     if (mergeData.empty()) {
176         return UsbPolicyUtils::SetUsbDisabled(false);
177     }
178     std::vector<UsbDeviceId> needRemoveMergePolicy =
179         ArrayUsbDeviceIdSerializer::GetInstance()->SetDifferencePolicyData(mergeData, data);
180     auto &srvClient = OHOS::USB::UsbSrvClient::GetInstance();
181     std::for_each(needRemoveMergePolicy.begin(), needRemoveMergePolicy.end(), [&](const auto usbDeviceId) {
182         if (srvClient.ManageDevice(usbDeviceId.GetVendorId(), usbDeviceId.GetProductId(), true) != ERR_OK) {
183             EDMLOGW("AllowUsbDevicesPlugin OnRemovePolicy ManageDevice vid: %{public}d, pid: %{public}d failed!",
184                 usbDeviceId.GetVendorId(), usbDeviceId.GetProductId());
185         }
186     });
187     return ERR_OK;
188 }
189 } // namespace EDM
190 } // namespace OHOS
191