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