1 /*
2 * Copyright (c) 2022-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 "usb_read_only_plugin.h"
17
18 #include "int_serializer.h"
19 #include "edm_constants.h"
20 #include "edm_ipc_interface_code.h"
21 #include "edm_utils.h"
22 #include "iservice_registry.h"
23 #include "parameters.h"
24 #include "usb_policy_utils.h"
25 #include "usb_srv_client.h"
26 #include "volume_external.h"
27 #include "plugin_manager.h"
28
29 namespace OHOS {
30 namespace EDM {
31 const bool REGISTER_RESULT = PluginManager::GetInstance()->AddPlugin(UsbReadOnlyPlugin::GetPlugin());
32 constexpr int32_t STORAGE_MANAGER_MANAGER_ID = 5003;
33
InitPlugin(std::shared_ptr<IPluginTemplate<UsbReadOnlyPlugin,int32_t>> ptr)34 void UsbReadOnlyPlugin::InitPlugin(std::shared_ptr<IPluginTemplate<UsbReadOnlyPlugin, int32_t>> ptr)
35 {
36 EDMLOGI("UsbReadOnlyPlugin InitPlugin...");
37 ptr->InitAttribute(EdmInterfaceCode::USB_READ_ONLY, "usb_read_only", "ohos.permission.ENTERPRISE_MANAGE_USB",
38 IPlugin::PermissionType::SUPER_DEVICE_ADMIN, true);
39 ptr->SetSerializer(IntSerializer::GetInstance());
40 ptr->SetOnHandlePolicyListener(&UsbReadOnlyPlugin::SetPolicy, FuncOperateType::SET);
41 ptr->SetOnAdminRemoveListener(&UsbReadOnlyPlugin::OnAdminRemove);
42 }
43
SetPolicy(int32_t & policyValue)44 ErrCode UsbReadOnlyPlugin::SetPolicy(int32_t &policyValue)
45 {
46 EDMLOGI("UsbReadOnlyPlugin SetPolicy: %{public}d", policyValue);
47 auto &srvClient = OHOS::USB::UsbSrvClient::GetInstance();
48 auto policyManager = IPolicyManager::GetInstance();
49 std::string disableUsbPolicy;
50 policyManager->GetPolicy("", "disable_usb", disableUsbPolicy);
51 std::string allowUsbDevicePolicy;
52 policyManager->GetPolicy("", "allowed_usb_devices", allowUsbDevicePolicy);
53 if (disableUsbPolicy == "true") {
54 EDMLOGE("OnSetPolicy: CONFLICT! isUsbDisabled: %{public}s", disableUsbPolicy.c_str());
55 return EdmReturnErrCode::CONFIGURATION_CONFLICT_FAILED;
56 }
57 if (policyValue == EdmConstants::STORAGE_USB_POLICY_DISABLED) {
58 if (!allowUsbDevicePolicy.empty()) {
59 EDMLOGE("OnSetPolicy: CONFLICT! allowedUsbDevice: %{public}s", allowUsbDevicePolicy.c_str());
60 return EdmReturnErrCode::CONFIGURATION_CONFLICT_FAILED;
61 }
62 return UsbPolicyUtils::SetStorageUsbDeviceDisabled(true);
63 }
64 std::string usbKey = "persist.filemanagement.usb.readonly";
65 std::string usbValue = (policyValue == EdmConstants::STORAGE_USB_POLICY_READ_ONLY) ? "true" : "false";
66 bool ret = OHOS::system::SetParameter(usbKey, usbValue);
67 int32_t usbRet = ERR_OK;
68 if (allowUsbDevicePolicy.empty()) {
69 usbRet = srvClient.ManageInterfaceStorage(OHOS::USB::InterfaceType::TYPE_STORAGE, false);
70 }
71 EDMLOGI("UsbReadOnlyPlugin SetPolicy sysParam: readonly value:%{public}s ret:%{public}d usbRet:%{public}d",
72 usbValue.c_str(), ret, usbRet);
73 return (ret && usbRet == ERR_OK) ? ReloadUsbDevice() : EdmReturnErrCode::SYSTEM_ABNORMALLY;
74 }
75
OnGetPolicy(std::string & policyData,MessageParcel & data,MessageParcel & reply,int32_t userId)76 ErrCode UsbReadOnlyPlugin::OnGetPolicy(std::string &policyData, MessageParcel &data, MessageParcel &reply,
77 int32_t userId)
78 {
79 EDMLOGI("UsbReadOnlyPlugin OnGetPolicy: %{public}s", policyData.c_str());
80 policyData = policyData.empty() ? "0" : policyData;
81 int32_t result = EdmConstants::STORAGE_USB_POLICY_READ_ONLY;
82 ErrCode parseRet = EdmUtils::ParseStringToInt(policyData, result);
83 if (FAILED(parseRet)) {
84 reply.WriteInt32(EdmReturnErrCode::SYSTEM_ABNORMALLY);
85 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
86 }
87 reply.WriteInt32(ERR_OK);
88 reply.WriteInt32(result);
89 return ERR_OK;
90 }
91
OnAdminRemove(const std::string & adminName,int32_t & data,int32_t userId)92 ErrCode UsbReadOnlyPlugin::OnAdminRemove(const std::string &adminName, int32_t &data, int32_t userId)
93 {
94 EDMLOGI("UsbReadOnlyPlugin OnAdminRemove adminName: %{public}s, userId: %{public}d, value: %{public}d",
95 adminName.c_str(), userId, data);
96 if (data == EdmConstants::STORAGE_USB_POLICY_DISABLED) {
97 return UsbPolicyUtils::SetStorageUsbDeviceDisabled(false);
98 }
99 std::string usbKey = "persist.filemanagement.usb.readonly";
100 std::string usbValue = "false";
101 bool ret = OHOS::system::SetParameter(usbKey, usbValue);
102 return ret ? ERR_OK : EdmReturnErrCode::SYSTEM_ABNORMALLY;
103 }
104
GetStorageManager()105 OHOS::sptr<OHOS::StorageManager::IStorageManager> UsbReadOnlyPlugin::GetStorageManager()
106 {
107 auto samgr = OHOS::SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
108 if (samgr == nullptr) {
109 EDMLOGE("UsbReadOnlyPlugin GetStorageManager:get samgr fail");
110 return nullptr;
111 }
112 sptr<IRemoteObject> obj = samgr->GetSystemAbility(STORAGE_MANAGER_MANAGER_ID);
113 if (obj == nullptr) {
114 EDMLOGE("UsbReadOnlyPlugin GetStorageManager:get storage manager client fail");
115 return nullptr;
116 }
117 auto storageMgrProxy = iface_cast<OHOS::StorageManager::IStorageManager>(obj);
118 if (storageMgrProxy == nullptr) {
119 EDMLOGE("UsbReadOnlyPlugin GetStorageManager:get storageMgrProxy fail");
120 }
121 return storageMgrProxy;
122 }
123
ReloadUsbDevice()124 ErrCode UsbReadOnlyPlugin::ReloadUsbDevice()
125 {
126 auto storageMgrProxy = GetStorageManager();
127 if (storageMgrProxy == nullptr) {
128 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
129 }
130 std::vector<StorageManager::VolumeExternal> volList;
131 int32_t storageRet = storageMgrProxy->GetAllVolumes(volList);
132 if (storageRet != ERR_OK) {
133 EDMLOGE("UsbReadOnlyPlugin SetPolicy storageMgrProxy GetAllVolumes failed! ret:%{public}d", storageRet);
134 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
135 }
136 if (volList.empty()) {
137 return ERR_OK;
138 }
139 for (auto &vol : volList) {
140 if (storageMgrProxy->Unmount(vol.GetId()) != ERR_OK) {
141 EDMLOGE("UsbReadOnlyPlugin SetPolicy storageMgrProxy Unmount failed!");
142 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
143 }
144 if (storageMgrProxy->Mount(vol.GetId()) != ERR_OK) {
145 EDMLOGE("UsbReadOnlyPlugin SetPolicy storageMgrProxy Mount failed!");
146 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
147 }
148 }
149 return ERR_OK;
150 }
151 } // namespace EDM
152 } // namespace OHOS
153