1 /*
2 * Copyright (c) 2024 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 #include "netfirewall_policy_manager.h"
16
17 #include "net_manager_constants.h"
18 #include "netfirewall_rule_manager.h"
19 #include "netfirewall_rule_native_helper.h"
20 #include "netmanager_base_common_utils.h"
21 #include "netsys_controller.h"
22 #include "os_account_manager.h"
23
24 namespace OHOS {
25 namespace NetManagerStandard {
GetInstance()26 NetFirewallPolicyManager &NetFirewallPolicyManager::GetInstance()
27 {
28 static NetFirewallPolicyManager instance;
29 return instance;
30 }
31
NetFirewallPolicyManager()32 NetFirewallPolicyManager::NetFirewallPolicyManager()
33 {
34 NETMGR_EXT_LOG_I("NetFirewallPolicyManager()");
35 }
36
~NetFirewallPolicyManager()37 NetFirewallPolicyManager::~NetFirewallPolicyManager()
38 {
39 NETMGR_EXT_LOG_I("~NetFirewallPolicyManager()");
40 }
41
SetNetFirewallPolicy(const int32_t userId,const sptr<NetFirewallPolicy> & policy)42 int32_t NetFirewallPolicyManager::SetNetFirewallPolicy(const int32_t userId, const sptr<NetFirewallPolicy> &policy)
43 {
44 std::unique_lock<std::shared_mutex> locker(setPolicyMutex_);
45 if (policy == nullptr) {
46 NETMGR_EXT_LOG_E("SetNetFirewallPolicy failed, policy is nullptr.");
47 return FIREWALL_ERR_PARAMETER_ERROR;
48 }
49 auto helper = NetFirewallPreferenceHelper::CreateInstance(FirewallPreferencePathOfUser(userId));
50 if (helper == nullptr) {
51 NETMGR_EXT_LOG_E("SetNetFirewallPolicy failed, reference is nullptr.");
52 return FIREWALL_ERR_INTERNAL;
53 }
54 bool ret = true;
55 ret &= helper->SaveBool(NET_FIREWALL_IS_OPEN, policy->isOpen);
56 ret &= helper->SaveInt(NET_FIREWALL_IN_ACTION, static_cast<int>(policy->inAction));
57 ret &= helper->SaveInt(NET_FIREWALL_OUT_ACTION, static_cast<int>(policy->outAction));
58 if (!ret) {
59 NETMGR_EXT_LOG_E("SetNetFirewallPolicy failed");
60 return FIREWALL_ERR_INTERNAL;
61 }
62
63 return FIREWALL_SUCCESS;
64 }
65
IsFirewallOpen()66 bool NetFirewallPolicyManager::IsFirewallOpen()
67 {
68 std::unique_lock<std::shared_mutex> locker(setPolicyMutex_);
69 std::vector<int32_t> accountIds;
70 GetAllUserId(accountIds);
71 for (auto &accountId : accountIds) {
72 if (IsNetFirewallOpen(accountId)) {
73 return true;
74 }
75 }
76 return false;
77 }
78
GetAllUserId(std::vector<int32_t> & accountIds)79 void NetFirewallPolicyManager::GetAllUserId(std::vector<int32_t> &accountIds)
80 {
81 std::vector<AccountSA::OsAccountInfo> osAccountInfos;
82 int32_t ret = AccountSA::OsAccountManager::QueryAllCreatedOsAccounts(osAccountInfos);
83 if (ret != ERR_OK || osAccountInfos.empty()) {
84 NETMGR_EXT_LOG_W("query user failed errCode=%{public}d", ret);
85 }
86 for (const auto &info : osAccountInfos) {
87 accountIds.push_back(info.GetLocalId());
88 }
89 }
90
FirewallPreferencePathOfUser(int32_t userId)91 std::string NetFirewallPolicyManager::FirewallPreferencePathOfUser(int32_t userId)
92 {
93 return FIREWALL_PREFERENCE_PATH + std::to_string(userId) + ".xml";
94 }
95
InitNetfirewallPolicy()96 int32_t NetFirewallPolicyManager::InitNetfirewallPolicy()
97 {
98 std::unique_lock<std::shared_mutex> locker(setPolicyMutex_);
99 std::vector<int32_t> accountIds;
100 GetAllUserId(accountIds);
101 NETMGR_EXT_LOG_I("InitNetfirewallPolicy accountIds size =%{public}zu", accountIds.size());
102 sptr<NetFirewallPolicy> netfirewallPolicy = new (std::nothrow) NetFirewallPolicy();
103 if (netfirewallPolicy == nullptr) {
104 NETMGR_EXT_LOG_E("InitNetfirewallPolicy error");
105 return FIREWALL_ERR_INTERNAL;
106 }
107 NetFirewallRuleNativeHelper::GetInstance().ClearFirewallRules(NetFirewallRuleType::RULE_DEFAULT_ACTION);
108 for (auto &accountId : accountIds) {
109 NETMGR_EXT_LOG_I("InitNetfirewallPolicy accountId=%{public}d", accountId);
110 LoadPolicyFormPreference(accountId, netfirewallPolicy);
111 // set policy is open to bpf map
112 if (netfirewallPolicy->isOpen) {
113 NetFirewallRuleNativeHelper::GetInstance().SetFirewallDefaultAction(accountId,
114 netfirewallPolicy->inAction, netfirewallPolicy->outAction);
115 }
116 }
117 return FIREWALL_SUCCESS;
118 }
119
GetNetFirewallPolicy(const int32_t userId,sptr<NetFirewallPolicy> & policy)120 int32_t NetFirewallPolicyManager::GetNetFirewallPolicy(const int32_t userId, sptr<NetFirewallPolicy> &policy)
121 {
122 if (policy == nullptr) {
123 NETMGR_EXT_LOG_E("GetNetFirewallPolicy failed, policy is nullptr.");
124 return FIREWALL_ERR_INTERNAL;
125 }
126 LoadPolicyFormPreference(userId, policy);
127 return FIREWALL_SUCCESS;
128 }
129
GetNetFirewallStatus(const int32_t userId)130 bool NetFirewallPolicyManager::GetNetFirewallStatus(const int32_t userId)
131 {
132 std::unique_lock<std::shared_mutex> locker(setPolicyMutex_);
133 return IsNetFirewallOpen(userId);
134 }
135
IsNetFirewallOpen(const int32_t userId)136 bool NetFirewallPolicyManager::IsNetFirewallOpen(const int32_t userId)
137 {
138 NETMGR_EXT_LOG_D("IsNetFirewallOpen");
139 bool defVal = false;
140 auto helper = NetFirewallPreferenceHelper::CreateInstance(FirewallPreferencePathOfUser(userId));
141 if (helper == nullptr) {
142 NETMGR_EXT_LOG_E("IsNetFirewallOpen failed, reference is nullptr.");
143 return defVal;
144 }
145 return helper->ObtainBool(NET_FIREWALL_IS_OPEN, defVal);
146 }
147
ClearFirewallPolicy(const int32_t userId)148 int32_t NetFirewallPolicyManager::ClearFirewallPolicy(const int32_t userId)
149 {
150 NetFirewallPreferenceHelper::Clear(FirewallPreferencePathOfUser(userId));
151 InitNetfirewallPolicy();
152 return FIREWALL_SUCCESS;
153 }
154
LoadPolicyFormPreference(const int32_t userId,sptr<NetFirewallPolicy> & policy)155 void NetFirewallPolicyManager::LoadPolicyFormPreference(const int32_t userId, sptr<NetFirewallPolicy> &policy)
156 {
157 auto helper = NetFirewallPreferenceHelper::CreateInstance(FirewallPreferencePathOfUser(userId));
158 if (helper == nullptr) {
159 NETMGR_EXT_LOG_E("LoadPolicyFormPreference failed, reference is nullptr.");
160 return;
161 }
162 policy->isOpen = helper->ObtainBool(NET_FIREWALL_IS_OPEN, false);
163 policy->inAction = static_cast<FirewallRuleAction>(
164 helper->ObtainInt(NET_FIREWALL_IN_ACTION, static_cast<int>(FirewallRuleAction::RULE_ALLOW)));
165 policy->outAction = static_cast<FirewallRuleAction>(
166 helper->ObtainInt(NET_FIREWALL_OUT_ACTION, static_cast<int>(FirewallRuleAction::RULE_ALLOW)));
167 }
168 } // namespace NetManagerStandard
169 } // namespace OHOS
170