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 "disabled_network_interface_plugin.h"
17
18 #include "edm_constants.h"
19 #include "edm_ipc_interface_code.h"
20 #include "ethernet_client.h"
21 #include "map_string_serializer.h"
22 #include "plugin_manager.h"
23
24 namespace OHOS {
25 namespace EDM {
26 const std::string IF_CFG_DOWN = "down";
27 const bool REGISTER_RESULT = PluginManager::GetInstance()->AddPlugin(DisabledNetworkInterfacePlugin::GetPlugin());
28
InitPlugin(std::shared_ptr<IPluginTemplate<DisabledNetworkInterfacePlugin,std::map<std::string,std::string>>> ptr)29 void DisabledNetworkInterfacePlugin::InitPlugin(
30 std::shared_ptr<IPluginTemplate<DisabledNetworkInterfacePlugin, std::map<std::string, std::string>>> ptr)
31 {
32 EDMLOGI("DisabledNetworkInterfacePlugin InitPlugin...");
33 ptr->InitAttribute(EdmInterfaceCode::DISABLED_NETWORK_INTERFACE, "disabled_network_interface", false);
34 std::map<std::string, std::string> setPerms;
35 setPerms.insert(std::make_pair(EdmConstants::PERMISSION_TAG_VERSION_11, "ohos.permission.ENTERPRISE_SET_NETWORK"));
36 setPerms.insert(std::make_pair(EdmConstants::PERMISSION_TAG_VERSION_12,
37 "ohos.permission.ENTERPRISE_MANAGE_NETWORK"));
38 IPlugin::PolicyPermissionConfig setConfig = IPlugin::PolicyPermissionConfig(setPerms,
39 IPlugin::PermissionType::SUPER_DEVICE_ADMIN, IPlugin::ApiType::PUBLIC);
40 ptr->InitPermission(FuncOperateType::SET, setConfig);
41 std::map<std::string, std::string> getPerms;
42 getPerms.insert(std::make_pair(EdmConstants::PERMISSION_TAG_VERSION_11,
43 "ohos.permission.ENTERPRISE_GET_NETWORK_INFO"));
44 getPerms.insert(std::make_pair(EdmConstants::PERMISSION_TAG_VERSION_12,
45 "ohos.permission.ENTERPRISE_MANAGE_NETWORK"));
46 IPlugin::PolicyPermissionConfig getConfig = IPlugin::PolicyPermissionConfig(getPerms,
47 IPlugin::PermissionType::SUPER_DEVICE_ADMIN, IPlugin::ApiType::PUBLIC);
48 ptr->InitPermission(FuncOperateType::GET, getConfig);
49 ptr->SetSerializer(MapStringSerializer::GetInstance());
50 ptr->SetOnHandlePolicyListener(&DisabledNetworkInterfacePlugin::OnSetPolicy, FuncOperateType::SET);
51 }
52
OnGetPolicy(std::string & policyData,MessageParcel & data,MessageParcel & reply,int32_t userId)53 ErrCode DisabledNetworkInterfacePlugin::OnGetPolicy(std::string &policyData, MessageParcel &data, MessageParcel &reply,
54 int32_t userId)
55 {
56 EDMLOGD("DisabledNetworkInterfacePlugin OnGetPolicy.");
57 nmd::InterfaceConfigurationParcel config;
58 std::string networkInterface;
59 data.ReadString(networkInterface);
60 DelayedSingleton<NetManagerStandard::EthernetClient>::GetInstance()->GetInterfaceConfig(networkInterface, config);
61 if (config.flags.empty()) {
62 EDMLOGD("network interface does not exist");
63 reply.WriteInt32(EdmReturnErrCode::PARAM_ERROR);
64 return EdmReturnErrCode::PARAM_ERROR;
65 }
66 reply.WriteInt32(ERR_OK);
67 reply.WriteBool(std::find(config.flags.begin(), config.flags.end(), IF_CFG_DOWN) != config.flags.end());
68 return ERR_OK;
69 }
70
OnSetPolicy(std::map<std::string,std::string> & policyData)71 ErrCode DisabledNetworkInterfacePlugin::OnSetPolicy(std::map<std::string, std::string> &policyData)
72 {
73 EDMLOGD("DisabledNetworkInterfacePlugin OnSetPolicy.");
74 if (policyData.empty()) {
75 return EdmReturnErrCode::PARAM_ERROR;
76 }
77 // policyData contains one key value pair, the key is the network interface and the value is "true" of "false".
78 auto it = policyData.begin();
79 nmd::InterfaceConfigurationParcel config;
80 DelayedSingleton<NetManagerStandard::EthernetClient>::GetInstance()->GetInterfaceConfig(it->first, config);
81 if (config.flags.empty()) {
82 EDMLOGD("network interface does not exist");
83 return EdmReturnErrCode::PARAM_ERROR;
84 }
85 int32_t ret = it->second == "true" ?
86 DelayedSingleton<NetManagerStandard::EthernetClient>::GetInstance()->SetInterfaceDown(it->first) :
87 DelayedSingleton<NetManagerStandard::EthernetClient>::GetInstance()->SetInterfaceUp(it->first);
88 if (FAILED(ret)) {
89 EDMLOGD("DisabledNetworkInterfacePlugin:OnSetPolicy send request fail. %{public}d", ret);
90 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
91 }
92 return ERR_OK;
93 }
94 } // namespace EDM
95 } // namespace OHOS
96