• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "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 "plugin_manager.h"
30 
31 namespace OHOS {
32 namespace EDM {
33 const bool REGISTER_RESULT = PluginManager::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         "ohos.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,int32_t userId)47 ErrCode AllowUsbDevicesPlugin::OnSetPolicy(std::vector<UsbDeviceId> &data,
48     std::vector<UsbDeviceId> &currentData, 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 input data is too large");
57         return EdmReturnErrCode::PARAM_ERROR;
58     }
59 
60     auto policyManager = IPolicyManager::GetInstance();
61     std::string disableUsbPolicy;
62     policyManager->GetPolicy("", "disable_usb", disableUsbPolicy);
63     std::string usbStoragePolicy;
64     policyManager->GetPolicy("", "usb_read_only", usbStoragePolicy);
65     if (disableUsbPolicy == "true" || usbStoragePolicy == std::to_string(EdmConstants::STORAGE_USB_POLICY_DISABLED)) {
66         EDMLOGE("AllowUsbDevicesPlugin OnSetPolicy: CONFLICT! isUsbDisabled: %{public}s, usbStoragePolicy: %{public}s",
67             disableUsbPolicy.c_str(), usbStoragePolicy.c_str());
68         return EdmReturnErrCode::CONFIGURATION_CONFLICT_FAILED;
69     }
70 
71     std::vector<UsbDeviceId> mergeData = ArrayUsbDeviceIdSerializer::GetInstance()->SetUnionPolicyData(data,
72         currentData);
73     if (mergeData.size() > EdmConstants::ALLOWED_USB_DEVICES_MAX_SIZE) {
74         EDMLOGE("AllowUsbDevicesPlugin OnSetPolicy merge data is too large");
75         return EdmReturnErrCode::PARAM_ERROR;
76     }
77     ErrCode errCode = UsbPolicyUtils::AddAllowedUsbDevices(mergeData);
78     if (errCode != ERR_OK) {
79         return errCode;
80     }
81     currentData = mergeData;
82     return ERR_OK;
83 }
84 
OnRemovePolicy(std::vector<UsbDeviceId> & data,std::vector<UsbDeviceId> & currentData,int32_t userId)85 ErrCode AllowUsbDevicesPlugin::OnRemovePolicy(std::vector<UsbDeviceId> &data,
86     std::vector<UsbDeviceId> &currentData, int32_t userId)
87 {
88     EDMLOGD("AllowUsbDevicesPlugin OnRemovePolicy userId : %{public}d:", userId);
89     if (data.empty()) {
90         EDMLOGW("AllowUsbDevicesPlugin OnRemovePolicy data is empty:");
91         return ERR_OK;
92     }
93     if (data.size() > EdmConstants::ALLOWED_USB_DEVICES_MAX_SIZE) {
94         EDMLOGE("AllowUsbDevicesPlugin OnRemovePolicy input data is too large");
95         return EdmReturnErrCode::PARAM_ERROR;
96     }
97     std::vector<UsbDeviceId> mergeData =
98         ArrayUsbDeviceIdSerializer::GetInstance()->SetDifferencePolicyData(data, currentData);
99     if (mergeData.empty()) {
100         auto &srvClient = OHOS::USB::UsbSrvClient::GetInstance();
101         std::vector<OHOS::USB::UsbDevice> allDevices;
102         int32_t getRet = srvClient.GetDevices(allDevices);
103         if (getRet != ERR_OK) {
104             EDMLOGE("AllowUsbDevicesPlugin OnSetPolicy getDevices failed: %{public}d", getRet);
105             return EdmReturnErrCode::SYSTEM_ABNORMALLY;
106         }
107         EDMLOGI("AllowUsbDevicesPlugin OnRemovePolicy: clear to empty, enable all.");
108         std::for_each(allDevices.begin(), allDevices.end(), [&](const auto usbDevice) {
109             if (srvClient.ManageDevice(usbDevice.GetVendorId(), usbDevice.GetProductId(), false) != ERR_OK) {
110                 EDMLOGW("AllowUsbDevicesPlugin OnRemovePolicy ManageDevice vid: %{public}d, pid: %{public}d failed!",
111                     usbDevice.GetVendorId(), usbDevice.GetProductId());
112             }
113         });
114         currentData = mergeData;
115         return ERR_OK;
116     }
117     EDMLOGI("AllowUsbDevicesPlugin OnRemovePolicy: remove data size: %{public}zu", data.size());
118     auto &srvClient = OHOS::USB::UsbSrvClient::GetInstance();
119     std::for_each(data.begin(), data.end(), [&](const auto usbDeviceId) {
120         if (srvClient.ManageDevice(usbDeviceId.GetVendorId(), usbDeviceId.GetProductId(), true) != ERR_OK) {
121             EDMLOGW("AllowUsbDevicesPlugin OnRemovePolicy ManageDevice vid: %{public}d, pid: %{public}d failed!",
122                 usbDeviceId.GetVendorId(), usbDeviceId.GetProductId());
123         }
124     });
125     currentData = mergeData;
126     return ERR_OK;
127 }
128 
OnGetPolicy(std::string & policyData,MessageParcel & data,MessageParcel & reply,int32_t userId)129 ErrCode AllowUsbDevicesPlugin::OnGetPolicy(std::string &policyData, MessageParcel &data, MessageParcel &reply,
130     int32_t userId)
131 {
132     EDMLOGI("AllowUsbDevicesPlugin OnGetPolicy policyData : %{public}s, userId : %{public}d",
133         policyData.c_str(), userId);
134     std::vector<UsbDeviceId> usbDeviceIds;
135     ArrayUsbDeviceIdSerializer::GetInstance()->Deserialize(policyData, usbDeviceIds);
136     reply.WriteInt32(ERR_OK);
137     reply.WriteInt32(usbDeviceIds.size());
138     for (const auto &usbDeviceId : usbDeviceIds) {
139         EDMLOGD("AllowUsbDevicesPlugin OnGetPolicy: currentData: vid: %{public}d, pid: %{public}d",
140             usbDeviceId.GetVendorId(), usbDeviceId.GetProductId());
141         if (!usbDeviceId.Marshalling(reply)) {
142             EDMLOGE("AllowUsbDevicesPlugin OnGetPolicy: write parcel failed!");
143             return EdmReturnErrCode::SYSTEM_ABNORMALLY;
144         }
145     }
146     return ERR_OK;
147 }
148 
OnAdminRemove(const std::string & adminName,std::vector<UsbDeviceId> & data,int32_t userId)149 ErrCode AllowUsbDevicesPlugin::OnAdminRemove(const std::string &adminName, std::vector<UsbDeviceId> &data,
150     int32_t userId)
151 {
152     EDMLOGD("AllowUsbDevicesPlugin OnAdminRemove");
153     if (data.empty()) {
154         EDMLOGW("AllowUsbDevicesPlugin OnRemovePolicy data is empty:");
155         return ERR_OK;
156     }
157     return UsbPolicyUtils::SetUsbDisabled(false);
158 }
159 } // namespace EDM
160 } // namespace OHOS
161