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 "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 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> mergeData = ArrayUsbDeviceIdSerializer::GetInstance()->SetUnionPolicyData(data,
64         currentData);
65     if (mergeData.size() > EdmConstants::ALLOWED_USB_DEVICES_MAX_SIZE) {
66         EDMLOGE("AllowUsbDevicesPlugin OnSetPolicy union data size=[%{public}zu] is too large", mergeData.size());
67         return EdmReturnErrCode::PARAM_ERROR;
68     }
69     ErrCode errCode = UsbPolicyUtils::AddAllowedUsbDevices(mergeData);
70     if (errCode != ERR_OK) {
71         return errCode;
72     }
73     currentData = mergeData;
74     return ERR_OK;
75 }
76 
HasConflictPolicy()77 bool AllowUsbDevicesPlugin::HasConflictPolicy()
78 {
79     auto policyManager = IPolicyManager::GetInstance();
80     std::string disableUsbPolicy;
81     policyManager->GetPolicy("", "disable_usb", disableUsbPolicy);
82     if (disableUsbPolicy == "true") {
83         EDMLOGE("AllowUsbDevicesPlugin POLICY CONFLICT! Usb is disabled.");
84         return true;
85     }
86 
87     std::string usbStoragePolicy;
88     policyManager->GetPolicy("", "usb_read_only", usbStoragePolicy);
89     if (usbStoragePolicy == std::to_string(EdmConstants::STORAGE_USB_POLICY_DISABLED)) {
90         EDMLOGE("AllowUsbDevicesPlugin POLICY CONFLICT! usbStoragePolicy is disabled.");
91         return true;
92     }
93 
94     std::string disallowUsbDevicePolicy;
95     policyManager->GetPolicy("", "disallowed_usb_devices", disallowUsbDevicePolicy);
96     if (!disallowUsbDevicePolicy.empty()) {
97         EDMLOGE("AllowUsbDevicesPlugin POLICY CONFLICT! disallowedUsbDevice: %{public}s",
98             disallowUsbDevicePolicy.c_str());
99         return true;
100     }
101     return false;
102 }
103 
OnRemovePolicy(std::vector<UsbDeviceId> & data,std::vector<UsbDeviceId> & currentData,int32_t userId)104 ErrCode AllowUsbDevicesPlugin::OnRemovePolicy(std::vector<UsbDeviceId> &data,
105     std::vector<UsbDeviceId> &currentData, int32_t userId)
106 {
107     EDMLOGD("AllowUsbDevicesPlugin OnRemovePolicy userId : %{public}d:", userId);
108     if (data.empty()) {
109         EDMLOGW("AllowUsbDevicesPlugin OnRemovePolicy data is empty:");
110         return ERR_OK;
111     }
112     if (data.size() > EdmConstants::ALLOWED_USB_DEVICES_MAX_SIZE) {
113         EDMLOGE("AllowUsbDevicesPlugin OnRemovePolicy input data is too large");
114         return EdmReturnErrCode::PARAM_ERROR;
115     }
116     std::vector<UsbDeviceId> mergeData =
117         ArrayUsbDeviceIdSerializer::GetInstance()->SetDifferencePolicyData(data, currentData);
118     if (mergeData.empty()) {
119         auto &srvClient = OHOS::USB::UsbSrvClient::GetInstance();
120         std::vector<OHOS::USB::UsbDevice> allDevices;
121         int32_t getRet = srvClient.GetDevices(allDevices);
122         if (getRet == EdmConstants::USB_ERRCODE_INTERFACE_NO_INIT) {
123             EDMLOGW("AllowUsbDevicesPlugin OnRemovePolicy: getDevices failed! USB interface not init!");
124         }
125         if (getRet != ERR_OK && getRet != EdmConstants::USB_ERRCODE_INTERFACE_NO_INIT) {
126             EDMLOGE("AllowUsbDevicesPlugin OnRemovePolicy getDevices failed: %{public}d", getRet);
127             return EdmReturnErrCode::SYSTEM_ABNORMALLY;
128         }
129         EDMLOGI("AllowUsbDevicesPlugin OnRemovePolicy: clear to empty, enable all.");
130         std::for_each(allDevices.begin(), allDevices.end(), [&](const auto usbDevice) {
131             if (srvClient.ManageDevice(usbDevice.GetVendorId(), usbDevice.GetProductId(), false) != ERR_OK) {
132                 EDMLOGW("AllowUsbDevicesPlugin OnRemovePolicy ManageDevice vid: %{public}d, pid: %{public}d failed!",
133                     usbDevice.GetVendorId(), usbDevice.GetProductId());
134             }
135         });
136         currentData = mergeData;
137         return ERR_OK;
138     }
139     EDMLOGI("AllowUsbDevicesPlugin OnRemovePolicy: remove data size: %{public}zu", data.size());
140     auto &srvClient = OHOS::USB::UsbSrvClient::GetInstance();
141     std::for_each(data.begin(), data.end(), [&](const auto usbDeviceId) {
142         if (srvClient.ManageDevice(usbDeviceId.GetVendorId(), usbDeviceId.GetProductId(), true) != ERR_OK) {
143             EDMLOGW("AllowUsbDevicesPlugin OnRemovePolicy ManageDevice vid: %{public}d, pid: %{public}d failed!",
144                 usbDeviceId.GetVendorId(), usbDeviceId.GetProductId());
145         }
146     });
147     currentData = mergeData;
148     return ERR_OK;
149 }
150 
OnGetPolicy(std::string & policyData,MessageParcel & data,MessageParcel & reply,int32_t userId)151 ErrCode AllowUsbDevicesPlugin::OnGetPolicy(std::string &policyData, MessageParcel &data, MessageParcel &reply,
152     int32_t userId)
153 {
154     EDMLOGI("AllowUsbDevicesPlugin OnGetPolicy policyData : %{public}s, userId : %{public}d",
155         policyData.c_str(), userId);
156     std::vector<UsbDeviceId> usbDeviceIds;
157     ArrayUsbDeviceIdSerializer::GetInstance()->Deserialize(policyData, usbDeviceIds);
158     reply.WriteInt32(ERR_OK);
159     reply.WriteUint32(usbDeviceIds.size());
160     for (const auto &usbDeviceId : usbDeviceIds) {
161         EDMLOGD("AllowUsbDevicesPlugin OnGetPolicy: currentData: vid: %{public}d, pid: %{public}d",
162             usbDeviceId.GetVendorId(), usbDeviceId.GetProductId());
163         if (!usbDeviceId.Marshalling(reply)) {
164             EDMLOGE("AllowUsbDevicesPlugin OnGetPolicy: write parcel failed!");
165             return EdmReturnErrCode::SYSTEM_ABNORMALLY;
166         }
167     }
168     return ERR_OK;
169 }
170 
OnAdminRemove(const std::string & adminName,std::vector<UsbDeviceId> & data,int32_t userId)171 ErrCode AllowUsbDevicesPlugin::OnAdminRemove(const std::string &adminName, std::vector<UsbDeviceId> &data,
172     int32_t userId)
173 {
174     EDMLOGD("AllowUsbDevicesPlugin OnAdminRemove");
175     if (data.empty()) {
176         EDMLOGW("AllowUsbDevicesPlugin OnRemovePolicy data is empty:");
177         return ERR_OK;
178     }
179     return UsbPolicyUtils::SetUsbDisabled(false);
180 }
181 } // namespace EDM
182 } // namespace OHOS
183