• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 "usb_policy_utils.h"
17 
18 #include "edm_constants.h"
19 #include "edm_log.h"
20 #include "ipolicy_manager.h"
21 #include "usb_device.h"
22 #include "usb_srv_client.h"
23 #ifdef OS_ACCOUNT_EDM_ENABLE
24 #include "os_account_manager.h"
25 #endif
26 
27 namespace OHOS {
28 namespace EDM {
SetUsbDisabled(bool data)29 ErrCode UsbPolicyUtils::SetUsbDisabled(bool data)
30 {
31     EDMLOGI("UsbPolicyUtils SetUsbDisabled...disable = %{public}d", data);
32     auto &srvClient = OHOS::USB::UsbSrvClient::GetInstance();
33     int32_t usbRet = srvClient.ManageGlobalInterface(data);
34     if (usbRet == EdmConstants::USB_ERRCODE_INTERFACE_NO_INIT) {
35         EDMLOGW("UsbPolicyUtils ManageGlobalInterface failed! USB interface not init!");
36     }
37     if (usbRet != ERR_OK && usbRet != EdmConstants::USB_ERRCODE_INTERFACE_NO_INIT) {
38         EDMLOGE("UsbPolicyUtils ManageGlobalInterface failed! ret: %{public}d", usbRet);
39         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
40     }
41     return ERR_OK;
42 }
43 
AddAllowedUsbDevices(std::vector<UsbDeviceId> data)44 ErrCode UsbPolicyUtils::AddAllowedUsbDevices(std::vector<UsbDeviceId> data)
45 {
46     EDMLOGI("UsbPolicyUtils AddAllowedUsbDevices....data size = %{public}zu", data.size());
47     auto &srvClient = OHOS::USB::UsbSrvClient::GetInstance();
48     std::vector<OHOS::USB::UsbDevice> allDevices;
49     int32_t getRet = srvClient.GetDevices(allDevices);
50     if (getRet == EdmConstants::USB_ERRCODE_INTERFACE_NO_INIT) {
51         EDMLOGW("UsbPolicyUtils getDevices failed! USB interface not init!");
52     }
53     if (getRet != ERR_OK && getRet != EdmConstants::USB_ERRCODE_INTERFACE_NO_INIT) {
54         EDMLOGE("UsbPolicyUtils getDevices failed: %{public}d", getRet);
55         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
56     }
57     EDMLOGI("UsbPolicyUtils AddAllowedUsbDevices getDevices size: %{public}zu", allDevices.size());
58     for (const auto &item : allDevices) {
59         bool isAllowed = (std::find_if(data.begin(), data.end(), [item](UsbDeviceId trustItem) {
60             return item.GetVendorId() == trustItem.GetVendorId() && item.GetProductId() == trustItem.GetProductId();
61         }) != data.end());
62         if (srvClient.ManageDevice(item.GetVendorId(), item.GetProductId(), !isAllowed) != ERR_OK) {
63             EDMLOGW("UsbPolicyUtils ManageDevice: vid:%{public}d pid:%{public}d, %{public}d failed!",
64                 item.GetVendorId(), item.GetProductId(), isAllowed);
65         }
66     }
67     return ERR_OK;
68 }
69 
SetDisallowedUsbDevices(std::vector<USB::UsbDeviceType> data)70 ErrCode UsbPolicyUtils::SetDisallowedUsbDevices(std::vector<USB::UsbDeviceType> data)
71 {
72     EDMLOGI("UsbPolicyUtils SetDisallowedUsbDevices...data size = %{public}zu", data.size());
73     auto &srvClient = OHOS::USB::UsbSrvClient::GetInstance();
74     int32_t usbRet = srvClient.ManageInterfaceType(data, true);
75     if (usbRet == EdmConstants::USB_ERRCODE_INTERFACE_NO_INIT) {
76         EDMLOGW("UsbPolicyUtils ManageInterfaceType failed! USB interface not init!");
77     }
78     if (usbRet != ERR_OK && usbRet != EdmConstants::USB_ERRCODE_INTERFACE_NO_INIT) {
79         EDMLOGE("UsbPolicyUtils ManageInterfaceType failed! ret:%{public}d", usbRet);
80         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
81     }
82     return ERR_OK;
83 }
84 
QueryAllCreatedOsAccountIds(std::vector<int32_t> & userIds)85 ErrCode UsbPolicyUtils::QueryAllCreatedOsAccountIds(std::vector<int32_t> &userIds)
86 {
87     EDMLOGI("UsbPolicyUtils QueryAllCreatedOsAccountIds enter");
88 #ifdef OS_ACCOUNT_EDM_ENABLE
89     std::vector<AccountSA::OsAccountInfo> accounts;
90     ErrCode ret = OHOS::AccountSA::OsAccountManager::QueryAllCreatedOsAccounts(accounts);
91     if (FAILED(ret) || accounts.empty()) {
92         EDMLOGE("UsbPolicyUtils QueryAllCreatedOsAccountIds, QueryAllCreatedOsAccounts failed");
93         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
94     }
95     for (auto &account : accounts) {
96         userIds.push_back(account.GetLocalId());
97     }
98     EDMLOGI("UsbPolicyUtils QueryAllCreatedOsAccountIds success, size: %{public}zu", userIds.size());
99     return ERR_OK;
100 #else
101     EDMLOGE("UsbPolicyUtils QueryAllCreatedOsAccountIds, QueryAllCreatedOsAccounts Unsupported Capabilities.");
102     return EdmReturnErrCode::SYSTEM_ABNORMALLY;
103 #endif
104 }
105 
106 #ifdef FEATURE_PC_ONLY
IsUsbStorageDeviceWriteDisallowed(bool & isDisallowed)107 ErrCode UsbPolicyUtils::IsUsbStorageDeviceWriteDisallowed(bool &isDisallowed)
108 {
109     EDMLOGI("UsbPolicyUtils IsUsbStorageDeviceWriteDisallowed enter");
110     std::vector<int32_t> userIds;
111     if (FAILED(QueryAllCreatedOsAccountIds(userIds))) {
112         EDMLOGE("UsbPolicyUtils IsUsbStorageDeviceWriteDisallowed, QueryAllCreatedOsAccountIds failed");
113         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
114     }
115 
116     auto policyManager = IPolicyManager::GetInstance();
117     std::string disallowedUsbStorageDeviceWrite;
118     for (size_t i = 0; i < userIds.size(); i++) {
119         policyManager->GetPolicy("", PolicyName::POLICY_DISALLOWED_USB_STORAGE_DEVICE_WRITE,
120             disallowedUsbStorageDeviceWrite, userIds[i]);
121         if (disallowedUsbStorageDeviceWrite == "true") {
122             EDMLOGI("UsbPolicyUtils IsUsbStorageDeviceWriteDisallowed, usbStorageDeviceWrite is disallowed");
123             isDisallowed = true;
124             return ERR_OK;
125         }
126     }
127     EDMLOGI("UsbPolicyUtils IsUsbStorageDeviceWriteDisallowed end");
128     return ERR_OK;
129 }
130 #endif
131 } // namespace EDM
132 } // namespace OHOS