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 "net_policy_client.h"
19 #include "netsys_native_service_proxy.h"
20 #include "system_ability_definition.h"
21
22 #include "edm_constants.h"
23 #include "edm_ipc_interface_code.h"
24 #include "edm_sys_manager.h"
25 #include "map_string_serializer.h"
26 #include "iplugin_manager.h"
27
28 namespace OHOS {
29 namespace EDM {
30 const bool REGISTER_RESULT = IPluginManager::GetInstance()->AddPlugin(DisabledNetworkInterfacePlugin::GetPlugin());
31
InitPlugin(std::shared_ptr<IPluginTemplate<DisabledNetworkInterfacePlugin,std::map<std::string,std::string>>> ptr)32 void DisabledNetworkInterfacePlugin::InitPlugin(
33 std::shared_ptr<IPluginTemplate<DisabledNetworkInterfacePlugin, std::map<std::string, std::string>>> ptr)
34 {
35 EDMLOGI("DisabledNetworkInterfacePlugin InitPlugin...");
36 ptr->InitAttribute(EdmInterfaceCode::DISABLED_NETWORK_INTERFACE, "disabled_network_interface", true);
37 std::map<std::string, std::map<IPlugin::PermissionType, std::string>> tagPermissionsForSet;
38 std::map<IPlugin::PermissionType, std::string> typePermissionsForTag11ForSet;
39 std::map<IPlugin::PermissionType, std::string> typePermissionsForTag12ForSet;
40 typePermissionsForTag11ForSet.emplace(IPlugin::PermissionType::SUPER_DEVICE_ADMIN,
41 EdmPermission::PERMISSION_ENTERPRISE_SET_NETWORK);
42 typePermissionsForTag12ForSet.emplace(IPlugin::PermissionType::SUPER_DEVICE_ADMIN,
43 EdmPermission::PERMISSION_ENTERPRISE_MANAGE_NETWORK);
44 tagPermissionsForSet.emplace(EdmConstants::PERMISSION_TAG_VERSION_11, typePermissionsForTag11ForSet);
45 tagPermissionsForSet.emplace(EdmConstants::PERMISSION_TAG_VERSION_12, typePermissionsForTag12ForSet);
46 IPlugin::PolicyPermissionConfig setConfig = IPlugin::PolicyPermissionConfig(tagPermissionsForSet,
47 IPlugin::ApiType::PUBLIC);
48 ptr->InitPermission(FuncOperateType::SET, setConfig);
49
50 std::map<std::string, std::map<IPlugin::PermissionType, std::string>> tagPermissionsForGet;
51 std::map<IPlugin::PermissionType, std::string> typePermissionsForTag11ForGet;
52 std::map<IPlugin::PermissionType, std::string> typePermissionsForTag12ForGet;
53 typePermissionsForTag11ForGet.emplace(IPlugin::PermissionType::SUPER_DEVICE_ADMIN,
54 EdmPermission::PERMISSION_ENTERPRISE_GET_NETWORK_INFO);
55 typePermissionsForTag12ForGet.emplace(IPlugin::PermissionType::SUPER_DEVICE_ADMIN,
56 EdmPermission::PERMISSION_ENTERPRISE_MANAGE_NETWORK);
57 tagPermissionsForGet.emplace(EdmConstants::PERMISSION_TAG_VERSION_11, typePermissionsForTag11ForGet);
58 tagPermissionsForGet.emplace(EdmConstants::PERMISSION_TAG_VERSION_12, typePermissionsForTag12ForGet);
59 IPlugin::PolicyPermissionConfig getConfig = IPlugin::PolicyPermissionConfig(tagPermissionsForGet,
60 IPlugin::ApiType::PUBLIC);
61 ptr->InitPermission(FuncOperateType::GET, getConfig);
62 ptr->SetSerializer(MapStringSerializer::GetInstance());
63 ptr->SetOnHandlePolicyListener(&DisabledNetworkInterfacePlugin::OnSetPolicy, FuncOperateType::SET);
64 ptr->SetOnAdminRemoveListener(&DisabledNetworkInterfacePlugin::OnAdminRemove);
65 }
66
OnGetPolicy(std::string & policyData,MessageParcel & data,MessageParcel & reply,int32_t userId)67 ErrCode DisabledNetworkInterfacePlugin::OnGetPolicy(std::string &policyData, MessageParcel &data, MessageParcel &reply,
68 int32_t userId)
69 {
70 EDMLOGD("DisabledNetworkInterfacePlugin OnGetPolicy.");
71 std::string networkInterface = data.ReadString();
72 auto ret = IsNetInterfaceExist(networkInterface);
73 if (FAILED(ret)) {
74 reply.WriteInt32(ret);
75 return ret;
76 }
77 std::map<std::string, std::string> policyMap;
78 if (!pluginInstance_->serializer_->Deserialize(policyData, policyMap)) {
79 EDMLOGE("DisabledNetworkInterfacePlugin OnGetPolicy get policy failed.");
80 reply.WriteInt32(EdmReturnErrCode::SYSTEM_ABNORMALLY);
81 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
82 }
83 bool isDisallowed = false;
84 if (policyMap.find(networkInterface) != policyMap.end()) {
85 isDisallowed = true;
86 }
87 reply.WriteInt32(ERR_OK);
88 reply.WriteBool(isDisallowed);
89 return ERR_OK;
90 }
91
OnSetPolicy(std::map<std::string,std::string> & data,std::map<std::string,std::string> & currentData,std::map<std::string,std::string> & mergeData,int32_t userId)92 ErrCode DisabledNetworkInterfacePlugin::OnSetPolicy(std::map<std::string, std::string> &data,
93 std::map<std::string, std::string> ¤tData, std::map<std::string, std::string> &mergeData, int32_t userId)
94 {
95 EDMLOGD("DisabledNetworkInterfacePlugin OnSetPolicy.");
96 if (data.empty()) {
97 return EdmReturnErrCode::PARAM_ERROR;
98 }
99 // data contains one key value pair, the key is the network interface and the value is "true" of "false".
100 auto it = data.begin();
101 auto ret = IsNetInterfaceExist(it->first);
102 if (FAILED(ret)) {
103 return ret;
104 }
105 if (mergeData.find(it->first) == mergeData.end()) {
106 if (!SetInterfaceDisabled(it->first, it->second == "true")) {
107 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
108 }
109 }
110 if (it->second == "true") {
111 currentData[it->first] = "true";
112 } else {
113 currentData.erase(it->first);
114 }
115 for (auto policy : currentData) {
116 if (mergeData.find(policy.first) == mergeData.end()) {
117 mergeData[policy.first] = policy.second;
118 }
119 }
120 return ERR_OK;
121 }
122
IsNetInterfaceExist(const std::string & netInterface)123 ErrCode DisabledNetworkInterfacePlugin::IsNetInterfaceExist(const std::string &netInterface)
124 {
125 auto remoteObject = EdmSysManager::GetRemoteObjectOfSystemAbility(COMM_NETSYS_NATIVE_SYS_ABILITY_ID);
126 if (remoteObject == nullptr) {
127 EDMLOGE("DisabledNetworkInterfacePlugin GetNetNativeProxy get remote object failed.");
128 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
129 }
130 auto netNativeProxy = iface_cast<NetsysNative::INetsysService>(remoteObject);
131 if (netNativeProxy == nullptr) {
132 EDMLOGE("DisabledNetworkInterfacePlugin OnGetPolicy get netNativeProxy failed.");
133 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
134 }
135 std::vector<std::string> ifaces;
136 auto ret = netNativeProxy->InterfaceGetList(ifaces);
137 if (FAILED(ret)) {
138 EDMLOGE("DisabledNetworkInterfacePlugin OnGetPolicy get interface list failed.");
139 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
140 }
141 if (ifaces.empty() || std::find(ifaces.begin(), ifaces.end(), netInterface) == ifaces.end()) {
142 EDMLOGE("DisabledNetworkInterfacePlugin OnGetPolicy network interface does not exist");
143 return EdmReturnErrCode::PARAM_ERROR;
144 }
145 return ERR_OK;
146 }
147
SetInterfaceDisabled(const std::string & ifaceName,bool status)148 bool DisabledNetworkInterfacePlugin::SetInterfaceDisabled(const std::string &ifaceName, bool status)
149 {
150 auto netPolicyClient = DelayedSingleton<NetManagerStandard::NetPolicyClient>::GetInstance();
151 if (netPolicyClient == nullptr) {
152 EDMLOGE("DisabledNetworkInterfacePlugin SetInterfaceDisabled get NetPolicyClient failed.");
153 return false;
154 }
155 std::vector<std::string> ifaceNames{ifaceName};
156 auto ret = netPolicyClient->SetNicTrafficAllowed(ifaceNames, !status);
157 if (FAILED(ret)) {
158 EDMLOGE("DisabledNetworkInterfacePlugin SetInterfaceDisabled SetNicTrafficAllowed failed, %{public}d.", ret);
159 return false;
160 }
161 return true;
162 }
163
OnAdminRemove(const std::string & adminName,std::map<std::string,std::string> & data,std::map<std::string,std::string> & mergeData,int32_t userId)164 ErrCode DisabledNetworkInterfacePlugin::OnAdminRemove(const std::string &adminName,
165 std::map<std::string, std::string> &data, std::map<std::string, std::string> &mergeData, int32_t userId)
166 {
167 for (auto &iter : data) {
168 if (mergeData.find(iter.first) == mergeData.end() && !SetInterfaceDisabled(iter.first, false)) {
169 EDMLOGE("DisabledNetworkInterfacePlugin OnAdminRemove set %{public}s allowed failed.", iter.first.c_str());
170 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
171 }
172 }
173 return ERR_OK;
174 }
175 } // namespace EDM
176 } // namespace OHOS
177