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
16 #include <cstdint>
17 #include <securec.h>
18
19 #include "napi_utils.h"
20 #include "net_firewall_exec.h"
21 #include "net_firewall_rule_parse.h"
22 #include "net_manager_constants.h"
23 #include "netmanager_ext_log.h"
24 #include "netfirewall_client.h"
25 #include "singleton.h"
26
27 namespace OHOS {
28 namespace NetManagerStandard {
29 namespace NetFirewallExec {
GetNetFirewallInstance(ContextT * context)30 template <typename ContextT> static inline NetFirewallClient *GetNetFirewallInstance(ContextT *context)
31 {
32 if (context == nullptr) {
33 return nullptr;
34 }
35 auto manager = context->GetManager();
36 return (manager == nullptr) ? nullptr : reinterpret_cast<NetFirewallClient *>(manager->GetData());
37 }
38
ExecSetNetFirewallPolicy(SetNetFirewallPolicyContext * context)39 bool ExecSetNetFirewallPolicy(SetNetFirewallPolicyContext *context)
40 {
41 if (context == nullptr || context->status_ == nullptr) {
42 return false;
43 }
44 if (!context->IsParseOK()) {
45 return false;
46 }
47 int32_t result =
48 DelayedSingleton<NetFirewallClient>::GetInstance()->SetNetFirewallPolicy(context->userId_, context->status_);
49 if (result != FIREWALL_SUCCESS) {
50 NETMANAGER_EXT_LOGE("ExecSetIfaceConfig error, errorCode: %{public}d", result);
51 context->SetErrorCode(result);
52 return false;
53 }
54 return true;
55 }
56
SetNetFirewallPolicyCallback(SetNetFirewallPolicyContext * context)57 napi_value SetNetFirewallPolicyCallback(SetNetFirewallPolicyContext *context)
58 {
59 if (context == nullptr) {
60 return nullptr;
61 }
62 return NapiUtils::GetUndefined(context->GetEnv());
63 }
64
ExecGetNetFirewallPolicy(GetNetFirewallPolicyContext * context)65 bool ExecGetNetFirewallPolicy(GetNetFirewallPolicyContext *context)
66 {
67 if (context == nullptr) {
68 return false;
69 }
70 if (!context->IsParseOK()) {
71 return false;
72 }
73 int32_t result =
74 DelayedSingleton<NetFirewallClient>::GetInstance()->GetNetFirewallPolicy(context->userId_, context->status_);
75 if (result != FIREWALL_SUCCESS || context->status_ == nullptr) {
76 NETMANAGER_EXT_LOGE("ExecGetIfaceConfig error, errorCode: %{public}d", result);
77 context->SetErrorCode(result);
78 return false;
79 }
80 return true;
81 }
82
GetNetFirewallPolicyCallback(GetNetFirewallPolicyContext * context)83 napi_value GetNetFirewallPolicyCallback(GetNetFirewallPolicyContext *context)
84 {
85 if (context == nullptr || context->status_ == nullptr) {
86 return nullptr;
87 }
88 napi_value firewallStatus = NapiUtils::CreateObject(context->GetEnv());
89
90 NapiUtils::SetBooleanProperty(context->GetEnv(), firewallStatus, NET_FIREWALL_IS_OPEN, context->status_->isOpen);
91 NapiUtils::SetInt32Property(context->GetEnv(), firewallStatus, NET_FIREWALL_IN_ACTION,
92 static_cast<int32_t>(context->status_->inAction));
93 NapiUtils::SetInt32Property(context->GetEnv(), firewallStatus, NET_FIREWALL_OUT_ACTION,
94 static_cast<int32_t>(context->status_->outAction));
95 return firewallStatus;
96 }
97
ExecAddNetFirewallRule(AddNetFirewallRuleContext * context)98 bool ExecAddNetFirewallRule(AddNetFirewallRuleContext *context)
99 {
100 if (context == nullptr || context->rule_ == nullptr) {
101 return false;
102 }
103 if (!context->IsParseOK()) {
104 return false;
105 }
106 int32_t result =
107 DelayedSingleton<NetFirewallClient>::GetInstance()->AddNetFirewallRule(context->rule_, context->reslut_);
108 if (result != FIREWALL_SUCCESS) {
109 NETMANAGER_EXT_LOGE("ExecAddNetFirewallRule error, errorCode: %{public}d", result);
110 context->SetErrorCode(result);
111 return false;
112 }
113 return true;
114 }
115
AddNetFirewallRuleCallback(AddNetFirewallRuleContext * context)116 napi_value AddNetFirewallRuleCallback(AddNetFirewallRuleContext *context)
117 {
118 if (context == nullptr) {
119 return nullptr;
120 }
121 // return basic data type
122 return NapiUtils::CreateInt32(context->GetEnv(), context->reslut_);
123 }
124
ExecUpdateNetFirewallRule(UpdateNetFirewallRuleContext * context)125 bool ExecUpdateNetFirewallRule(UpdateNetFirewallRuleContext *context)
126 {
127 if (context == nullptr || context->rule_ == nullptr) {
128 return false;
129 }
130 if (!context->IsParseOK()) {
131 return false;
132 }
133 int32_t result = DelayedSingleton<NetFirewallClient>::GetInstance()->UpdateNetFirewallRule(context->rule_);
134 if (result != FIREWALL_SUCCESS) {
135 NETMANAGER_EXT_LOGE("ExecUpdateNetFirewallRule error, errorCode: %{public}d", result);
136 context->SetErrorCode(result);
137 return false;
138 }
139 return true;
140 }
141
UpdateNetFirewallRuleCallback(UpdateNetFirewallRuleContext * context)142 napi_value UpdateNetFirewallRuleCallback(UpdateNetFirewallRuleContext *context)
143 {
144 if (context == nullptr) {
145 return nullptr;
146 }
147 // return basic data type
148 return NapiUtils::GetUndefined(context->GetEnv());
149 }
150
ExecDeleteNetFirewallRule(DeleteNetFirewallRuleContext * context)151 bool ExecDeleteNetFirewallRule(DeleteNetFirewallRuleContext *context)
152 {
153 if (context == nullptr) {
154 return false;
155 }
156 if (!context->IsParseOK()) {
157 return false;
158 }
159 int32_t result =
160 DelayedSingleton<NetFirewallClient>::GetInstance()->DeleteNetFirewallRule(context->userId_, context->ruleId_);
161 if (result != FIREWALL_SUCCESS) {
162 NETMANAGER_EXT_LOGE("ExecDeleteNetFirewallRule error, errorCode: %{public}d", result);
163 context->SetErrorCode(result);
164 return false;
165 }
166 return true;
167 }
168
DeleteNetFirewallRuleCallback(DeleteNetFirewallRuleContext * context)169 napi_value DeleteNetFirewallRuleCallback(DeleteNetFirewallRuleContext *context)
170 {
171 if (context == nullptr) {
172 return nullptr;
173 }
174 // return basic data type
175 return NapiUtils::GetUndefined(context->GetEnv());
176 }
177
ExecGetNetFirewallRules(GetNetFirewallRulesContext * context)178 bool ExecGetNetFirewallRules(GetNetFirewallRulesContext *context)
179 {
180 if (context == nullptr || context->requestParam_ == nullptr) {
181 return false;
182 }
183 if (!context->IsParseOK()) {
184 return false;
185 }
186 int32_t result = DelayedSingleton<NetFirewallClient>::GetInstance()->GetNetFirewallRules(context->userId_,
187 context->requestParam_, context->pageInfo_);
188 if (result != FIREWALL_SUCCESS || context->pageInfo_ == nullptr) {
189 NETMANAGER_EXT_LOGE("ExecGetNetFirewallRules error, errorCode: %{public}d", result);
190 context->SetErrorCode(result);
191 return false;
192 }
193 return true;
194 }
195
GetNetFirewallRulesCallback(GetNetFirewallRulesContext * context)196 napi_value GetNetFirewallRulesCallback(GetNetFirewallRulesContext *context)
197 {
198 if (context == nullptr || context->pageInfo_ == nullptr) {
199 return nullptr;
200 }
201 napi_value pageInfo = NapiUtils::CreateObject(context->GetEnv());
202 if (pageInfo == nullptr) {
203 return nullptr;
204 }
205 NapiUtils::SetInt32Property(context->GetEnv(), pageInfo, NET_FIREWALL_PAGE, context->pageInfo_->page);
206 NapiUtils::SetInt32Property(context->GetEnv(), pageInfo, NET_FIREWALL_PAGE_SIZE, context->pageInfo_->pageSize);
207 NapiUtils::SetInt32Property(context->GetEnv(), pageInfo, NET_FIREWALL_TOTAL_PAGE, context->pageInfo_->totalPage);
208 napi_value list = NapiUtils::CreateArray(context->GetEnv(), context->pageInfo_->data.size());
209 uint32_t index = 0;
210 for (const auto &iface : context->pageInfo_->data) {
211 napi_value rule = NapiUtils::CreateObject(context->GetEnv());
212 NetFirewallRuleParse::SetRuleParams(context->GetEnv(), rule, iface);
213 NapiUtils::SetArrayElement(context->GetEnv(), list, index++, rule);
214 }
215 NapiUtils::SetNamedProperty(context->GetEnv(), pageInfo, NET_FIREWALL_PAGE_DATA, list);
216 return pageInfo;
217 }
218
ExecGetNetFirewallRule(GetNetFirewallRuleContext * context)219 bool ExecGetNetFirewallRule(GetNetFirewallRuleContext *context)
220 {
221 if (context == nullptr) {
222 return false;
223 }
224 if (!context->IsParseOK()) {
225 return false;
226 }
227 int32_t result = DelayedSingleton<NetFirewallClient>::GetInstance()->GetNetFirewallRule(context->userId_,
228 context->ruleId_, context->rule_);
229 if (result != FIREWALL_SUCCESS || context->rule_ == nullptr) {
230 NETMANAGER_EXT_LOGE("ExecGetNetFirewallRule error, errorCode: %{public}d", result);
231 context->SetErrorCode(result);
232 return false;
233 }
234 return true;
235 }
236
GetNetFirewallRuleCallback(GetNetFirewallRuleContext * context)237 napi_value GetNetFirewallRuleCallback(GetNetFirewallRuleContext *context)
238 {
239 if (context == nullptr || context->rule_ == nullptr) {
240 return nullptr;
241 }
242 napi_value rule = NapiUtils::CreateObject(context->GetEnv());
243 NetFirewallRuleParse::SetRuleParams(context->GetEnv(), rule, *(context->rule_));
244 return rule;
245 }
246
ExecGetInterceptRecords(GetInterceptRecordsContext * context)247 bool ExecGetInterceptRecords(GetInterceptRecordsContext *context)
248 {
249 if (context == nullptr || context->requestParam_ == nullptr) {
250 return false;
251 }
252 if (!context->IsParseOK()) {
253 return false;
254 }
255 int32_t result = DelayedSingleton<NetFirewallClient>::GetInstance()->GetInterceptRecords(context->userId_,
256 context->requestParam_, context->pageInfo_);
257 if (result != FIREWALL_SUCCESS || context->pageInfo_ == nullptr) {
258 NETMANAGER_EXT_LOGE("ExecGetNetFirewallRules error, errorCode: %{public}d", result);
259 context->SetErrorCode(result);
260 return false;
261 }
262 return true;
263 }
264
GetInterceptRecordCallbacks(GetInterceptRecordsContext * context)265 napi_value GetInterceptRecordCallbacks(GetInterceptRecordsContext *context)
266 {
267 if (context == nullptr) {
268 return nullptr;
269 }
270 napi_value pageInfo = NapiUtils::CreateObject(context->GetEnv());
271 if (context->pageInfo_ == nullptr) {
272 return nullptr;
273 }
274 NapiUtils::SetInt32Property(context->GetEnv(), pageInfo, NET_FIREWALL_PAGE, context->pageInfo_->page);
275 NapiUtils::SetInt32Property(context->GetEnv(), pageInfo, NET_FIREWALL_PAGE_SIZE, context->pageInfo_->pageSize);
276 NapiUtils::SetInt32Property(context->GetEnv(), pageInfo, NET_FIREWALL_TOTAL_PAGE, context->pageInfo_->totalPage);
277 napi_value list = NapiUtils::CreateArray(context->GetEnv(), context->pageInfo_->data.size());
278
279 uint32_t index = 0;
280 for (const auto &iface : context->pageInfo_->data) {
281 napi_value rule = NapiUtils::CreateObject(context->GetEnv());
282 NapiUtils::SetInt32Property(context->GetEnv(), rule, NET_FIREWALL_RECORD_TIME, iface.time);
283 NapiUtils::SetStringPropertyUtf8(context->GetEnv(), rule, NET_FIREWALL_RECORD_LOCAL_IP, iface.localIp);
284 NapiUtils::SetStringPropertyUtf8(context->GetEnv(), rule, NET_FIREWALL_RECORD_REMOTE_IP, iface.remoteIp);
285 NapiUtils::SetInt32Property(context->GetEnv(), rule, NET_FIREWALL_RECORD_LOCAL_PORT, iface.localPort);
286 NapiUtils::SetInt32Property(context->GetEnv(), rule, NET_FIREWALL_RECORD_REMOTE_PORT, iface.remotePort);
287 NapiUtils::SetInt32Property(context->GetEnv(), rule, NET_FIREWALL_RECORD_PROTOCOL, iface.protocol);
288 NapiUtils::SetInt32Property(context->GetEnv(), rule, NET_FIREWALL_RECORD_UID, iface.appUid);
289 NapiUtils::SetStringPropertyUtf8(context->GetEnv(), rule, NET_FIREWALL_DOMAIN, iface.domain);
290
291 NapiUtils::SetArrayElement(context->GetEnv(), list, index++, rule);
292 }
293 NapiUtils::SetNamedProperty(context->GetEnv(), pageInfo, NET_FIREWALL_PAGE_DATA, list);
294 return pageInfo;
295 }
296 } // namespace NetFirewallExec
297 } // namespace NetManagerStandard
298 } // namespace OHOS