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