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