• 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_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", true);
34     std::map<std::string, std::map<IPlugin::PermissionType, std::string>> tagPermissionsForSet;
35     std::map<IPlugin::PermissionType, std::string> typePermissionsForTag11ForSet;
36     std::map<IPlugin::PermissionType, std::string> typePermissionsForTag12ForSet;
37     typePermissionsForTag11ForSet.emplace(IPlugin::PermissionType::SUPER_DEVICE_ADMIN,
38         "ohos.permission.ENTERPRISE_SET_NETWORK");
39     typePermissionsForTag12ForSet.emplace(IPlugin::PermissionType::SUPER_DEVICE_ADMIN,
40         "ohos.permission.ENTERPRISE_MANAGE_NETWORK");
41     tagPermissionsForSet.emplace(EdmConstants::PERMISSION_TAG_VERSION_11, typePermissionsForTag11ForSet);
42     tagPermissionsForSet.emplace(EdmConstants::PERMISSION_TAG_VERSION_12, typePermissionsForTag12ForSet);
43     IPlugin::PolicyPermissionConfig setConfig = IPlugin::PolicyPermissionConfig(tagPermissionsForSet,
44         IPlugin::ApiType::PUBLIC);
45     ptr->InitPermission(FuncOperateType::SET, setConfig);
46 
47     std::map<std::string, std::map<IPlugin::PermissionType, std::string>> tagPermissionsForGet;
48     std::map<IPlugin::PermissionType, std::string> typePermissionsForTag11ForGet;
49     std::map<IPlugin::PermissionType, std::string> typePermissionsForTag12ForGet;
50     typePermissionsForTag11ForGet.emplace(IPlugin::PermissionType::SUPER_DEVICE_ADMIN,
51         "ohos.permission.ENTERPRISE_GET_NETWORK_INFO");
52     typePermissionsForTag12ForGet.emplace(IPlugin::PermissionType::SUPER_DEVICE_ADMIN,
53         "ohos.permission.ENTERPRISE_MANAGE_NETWORK");
54     tagPermissionsForGet.emplace(EdmConstants::PERMISSION_TAG_VERSION_11, typePermissionsForTag11ForGet);
55     tagPermissionsForGet.emplace(EdmConstants::PERMISSION_TAG_VERSION_12, typePermissionsForTag12ForGet);
56     IPlugin::PolicyPermissionConfig getConfig = IPlugin::PolicyPermissionConfig(tagPermissionsForGet,
57         IPlugin::ApiType::PUBLIC);
58     ptr->InitPermission(FuncOperateType::GET, getConfig);
59     ptr->SetSerializer(MapStringSerializer::GetInstance());
60     ptr->SetOnHandlePolicyListener(&DisabledNetworkInterfacePlugin::OnSetPolicy, FuncOperateType::SET);
61 }
62 
OnGetPolicy(std::string & policyData,MessageParcel & data,MessageParcel & reply,int32_t userId)63 ErrCode DisabledNetworkInterfacePlugin::OnGetPolicy(std::string &policyData, MessageParcel &data, MessageParcel &reply,
64     int32_t userId)
65 {
66     EDMLOGD("DisabledNetworkInterfacePlugin OnGetPolicy.");
67     nmd::InterfaceConfigurationParcel config;
68     std::string networkInterface;
69     data.ReadString(networkInterface);
70     DelayedSingleton<NetManagerStandard::EthernetClient>::GetInstance()->GetInterfaceConfig(networkInterface, config);
71     if (config.flags.empty()) {
72         EDMLOGD("network interface does not exist");
73         reply.WriteInt32(EdmReturnErrCode::PARAM_ERROR);
74         return EdmReturnErrCode::PARAM_ERROR;
75     }
76     reply.WriteInt32(ERR_OK);
77     reply.WriteBool(std::find(config.flags.begin(), config.flags.end(), IF_CFG_DOWN) != config.flags.end());
78     return ERR_OK;
79 }
80 
OnSetPolicy(std::map<std::string,std::string> & policyData)81 ErrCode DisabledNetworkInterfacePlugin::OnSetPolicy(std::map<std::string, std::string> &policyData)
82 {
83     EDMLOGD("DisabledNetworkInterfacePlugin OnSetPolicy.");
84     if (policyData.empty()) {
85         return EdmReturnErrCode::PARAM_ERROR;
86     }
87     // policyData contains one key value pair, the key is the network interface and the value is "true" of "false".
88     auto it = policyData.begin();
89     nmd::InterfaceConfigurationParcel config;
90     DelayedSingleton<NetManagerStandard::EthernetClient>::GetInstance()->GetInterfaceConfig(it->first, config);
91     if (config.flags.empty()) {
92         EDMLOGD("network interface does not exist");
93         return EdmReturnErrCode::PARAM_ERROR;
94     }
95     int32_t ret = it->second == "true" ?
96         DelayedSingleton<NetManagerStandard::EthernetClient>::GetInstance()->SetInterfaceDown(it->first) :
97         DelayedSingleton<NetManagerStandard::EthernetClient>::GetInstance()->SetInterfaceUp(it->first);
98     if (FAILED(ret)) {
99         EDMLOGD("DisabledNetworkInterfacePlugin:OnSetPolicy send request fail. %{public}d", ret);
100         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
101     }
102     return ERR_OK;
103 }
104 } // namespace EDM
105 } // namespace OHOS
106