18e745fdaSopenharmony_ci/* 28e745fdaSopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd. 38e745fdaSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 48e745fdaSopenharmony_ci * you may not use this file except in compliance with the License. 58e745fdaSopenharmony_ci * You may obtain a copy of the License at 68e745fdaSopenharmony_ci * 78e745fdaSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 88e745fdaSopenharmony_ci * 98e745fdaSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 108e745fdaSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 118e745fdaSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 128e745fdaSopenharmony_ci * See the License for the specific language governing permissions and 138e745fdaSopenharmony_ci * limitations under the License. 148e745fdaSopenharmony_ci */ 158e745fdaSopenharmony_ci 168e745fdaSopenharmony_ci#ifndef NET_FIREWALL_SERVICE_H 178e745fdaSopenharmony_ci#define NET_FIREWALL_SERVICE_H 188e745fdaSopenharmony_ci 198e745fdaSopenharmony_ci#include <string> 208e745fdaSopenharmony_ci 218e745fdaSopenharmony_ci#include "os_account_manager.h" 228e745fdaSopenharmony_ci#include "netfirewall_common.h" 238e745fdaSopenharmony_ci#include "netfirewall_stub.h" 248e745fdaSopenharmony_ci#include "singleton.h" 258e745fdaSopenharmony_ci#include "system_ability.h" 268e745fdaSopenharmony_ci#include "common_event_manager.h" 278e745fdaSopenharmony_ci#include "common_event_subscriber.h" 288e745fdaSopenharmony_ci#include "common_event_support.h" 298e745fdaSopenharmony_ci 308e745fdaSopenharmony_ci#include "netfirewall_policy_manager.h" 318e745fdaSopenharmony_ci#include "netfirewall_rule_manager.h" 328e745fdaSopenharmony_ci#include "netfirewall_rule_native_helper.h" 338e745fdaSopenharmony_ci#include "ffrt.h" 348e745fdaSopenharmony_ci 358e745fdaSopenharmony_cinamespace OHOS { 368e745fdaSopenharmony_cinamespace NetManagerStandard { 378e745fdaSopenharmony_ciusing namespace OHOS::EventFwk; 388e745fdaSopenharmony_ciclass NetFirewallService : public SystemAbility, 398e745fdaSopenharmony_ci public NetFirewallStub, 408e745fdaSopenharmony_ci public std::enable_shared_from_this<NetFirewallService> { 418e745fdaSopenharmony_ci DECLARE_DELAYED_SINGLETON(NetFirewallService); 428e745fdaSopenharmony_ci DECLARE_SYSTEM_ABILITY(NetFirewallService) 438e745fdaSopenharmony_ci enum class ServiceRunningState { 448e745fdaSopenharmony_ci STATE_NOT_START, 458e745fdaSopenharmony_ci STATE_RUNNING 468e745fdaSopenharmony_ci }; 478e745fdaSopenharmony_ci 488e745fdaSopenharmony_cipublic: 498e745fdaSopenharmony_ci void SubscribeCommonEvent(); 508e745fdaSopenharmony_ci 518e745fdaSopenharmony_ci // Broadcast Listener 528e745fdaSopenharmony_ci class ReceiveMessage : public OHOS::EventFwk::CommonEventSubscriber { 538e745fdaSopenharmony_ci public: 548e745fdaSopenharmony_ci explicit ReceiveMessage(const EventFwk::CommonEventSubscribeInfo &subscriberInfo, 558e745fdaSopenharmony_ci std::shared_ptr<NetFirewallService> netfirewallService) 568e745fdaSopenharmony_ci : EventFwk::CommonEventSubscriber(subscriberInfo), netfirewallService_(netfirewallService) {}; 578e745fdaSopenharmony_ci 588e745fdaSopenharmony_ci virtual void OnReceiveEvent(const EventFwk::CommonEventData &eventData) override; 598e745fdaSopenharmony_ci 608e745fdaSopenharmony_ci private: 618e745fdaSopenharmony_ci std::shared_ptr<NetFirewallService> netfirewallService_ = nullptr; 628e745fdaSopenharmony_ci }; 638e745fdaSopenharmony_ci 648e745fdaSopenharmony_ci /* * 658e745fdaSopenharmony_ci * Turn on or off the firewall 668e745fdaSopenharmony_ci * 678e745fdaSopenharmony_ci * @param userId User id 688e745fdaSopenharmony_ci * @param status The firewall status to be set 698e745fdaSopenharmony_ci * @return Returns 0 success. Otherwise fail 708e745fdaSopenharmony_ci */ 718e745fdaSopenharmony_ci int32_t SetNetFirewallPolicy(const int32_t userId, const sptr<NetFirewallPolicy> &status) override; 728e745fdaSopenharmony_ci 738e745fdaSopenharmony_ci /** 748e745fdaSopenharmony_ci * Query firewall status 758e745fdaSopenharmony_ci * 768e745fdaSopenharmony_ci * @param userId User id 778e745fdaSopenharmony_ci * @param status Return to firewall status 788e745fdaSopenharmony_ci * @return Returns 0 success. Otherwise fail 798e745fdaSopenharmony_ci */ 808e745fdaSopenharmony_ci int32_t GetNetFirewallPolicy(const int32_t userId, sptr<NetFirewallPolicy> &status) override; 818e745fdaSopenharmony_ci 828e745fdaSopenharmony_ci /** 838e745fdaSopenharmony_ci * Add firewall rules 848e745fdaSopenharmony_ci * 858e745fdaSopenharmony_ci * @param rule Firewall rules 868e745fdaSopenharmony_ci * @param ruleId Rule id genarated by database 878e745fdaSopenharmony_ci * @return Returns 0 success. Otherwise fail 888e745fdaSopenharmony_ci */ 898e745fdaSopenharmony_ci int32_t AddNetFirewallRule(const sptr<NetFirewallRule> &rule, int32_t &ruleId) override; 908e745fdaSopenharmony_ci 918e745fdaSopenharmony_ci /** 928e745fdaSopenharmony_ci * Modify firewall rules 938e745fdaSopenharmony_ci * 948e745fdaSopenharmony_ci * @param rule Firewall rules 958e745fdaSopenharmony_ci * @return Returns 0 success. Otherwise fail 968e745fdaSopenharmony_ci */ 978e745fdaSopenharmony_ci int32_t UpdateNetFirewallRule(const sptr<NetFirewallRule> &rule) override; 988e745fdaSopenharmony_ci 998e745fdaSopenharmony_ci /** 1008e745fdaSopenharmony_ci * Delete firewall rules 1018e745fdaSopenharmony_ci * 1028e745fdaSopenharmony_ci * @param userId User ID 1038e745fdaSopenharmony_ci * @param ruleId Rule ID 1048e745fdaSopenharmony_ci * @return Returns 0 success. Otherwise fail 1058e745fdaSopenharmony_ci */ 1068e745fdaSopenharmony_ci int32_t DeleteNetFirewallRule(const int32_t userId, const int32_t ruleId) override; 1078e745fdaSopenharmony_ci 1088e745fdaSopenharmony_ci /** 1098e745fdaSopenharmony_ci * Get all firewall rules 1108e745fdaSopenharmony_ci * 1118e745fdaSopenharmony_ci * @param userId User ID 1128e745fdaSopenharmony_ci * @param requestParam Paging in parameter information 1138e745fdaSopenharmony_ci * @param info Paging data information 1148e745fdaSopenharmony_ci * @return Returns 0 success. Otherwise fail 1158e745fdaSopenharmony_ci */ 1168e745fdaSopenharmony_ci int32_t GetNetFirewallRules(const int32_t userId, const sptr<RequestParam> &requestParam, 1178e745fdaSopenharmony_ci sptr<FirewallRulePage> &info) override; 1188e745fdaSopenharmony_ci /** 1198e745fdaSopenharmony_ci * Get information about the specified rule ID 1208e745fdaSopenharmony_ci * 1218e745fdaSopenharmony_ci * @param ruleId Rule ID 1228e745fdaSopenharmony_ci * @param rule Return to firewall rules 1238e745fdaSopenharmony_ci * @return Returns 0 success. Otherwise fail 1248e745fdaSopenharmony_ci */ 1258e745fdaSopenharmony_ci int32_t GetNetFirewallRule(const int32_t userId, const int32_t ruleId, sptr<NetFirewallRule> &rule) override; 1268e745fdaSopenharmony_ci 1278e745fdaSopenharmony_ci /** 1288e745fdaSopenharmony_ci * Get all interception records 1298e745fdaSopenharmony_ci * 1308e745fdaSopenharmony_ci * @param userId User ID 1318e745fdaSopenharmony_ci * @param requestParam Paging in parameter information 1328e745fdaSopenharmony_ci * @param info Paging data information 1338e745fdaSopenharmony_ci * @return Returns 0 success. Otherwise fail 1348e745fdaSopenharmony_ci */ 1358e745fdaSopenharmony_ci int32_t GetInterceptRecords(const int32_t userId, const sptr<RequestParam> &requestParam, 1368e745fdaSopenharmony_ci sptr<InterceptRecordPage> &info) override; 1378e745fdaSopenharmony_ci 1388e745fdaSopenharmony_ci /** 1398e745fdaSopenharmony_ci * dump function 1408e745fdaSopenharmony_ci * 1418e745fdaSopenharmony_ci * @param fd File handle 1428e745fdaSopenharmony_ci * @param args Input data 1438e745fdaSopenharmony_ci * @return Returns 0 success. Otherwise fail 1448e745fdaSopenharmony_ci */ 1458e745fdaSopenharmony_ci int32_t Dump(int32_t fd, const std::vector<std::u16string> &args) override; 1468e745fdaSopenharmony_ci 1478e745fdaSopenharmony_ciprotected: 1488e745fdaSopenharmony_ci void OnStart() override; 1498e745fdaSopenharmony_ci 1508e745fdaSopenharmony_ci void OnStop() override; 1518e745fdaSopenharmony_ci 1528e745fdaSopenharmony_ci void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override; 1538e745fdaSopenharmony_ci 1548e745fdaSopenharmony_ci void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override; 1558e745fdaSopenharmony_ci 1568e745fdaSopenharmony_ciprivate: 1578e745fdaSopenharmony_ci void GetDumpMessage(std::string &message); 1588e745fdaSopenharmony_ci 1598e745fdaSopenharmony_ci int32_t OnInit(); 1608e745fdaSopenharmony_ci 1618e745fdaSopenharmony_ci int32_t GetCurrentAccountId(); 1628e745fdaSopenharmony_ci 1638e745fdaSopenharmony_ci void SetCurrentUserId(int32_t userId); 1648e745fdaSopenharmony_ci 1658e745fdaSopenharmony_ci void InitQueryUserId(int32_t times); 1668e745fdaSopenharmony_ci 1678e745fdaSopenharmony_ci bool InitUsersOnBoot(); 1688e745fdaSopenharmony_ci 1698e745fdaSopenharmony_ci void InitServiceHandler(); 1708e745fdaSopenharmony_ci 1718e745fdaSopenharmony_ci void InitQueryNetFirewallRules(); 1728e745fdaSopenharmony_ci 1738e745fdaSopenharmony_ci std::string GetServiceState(); 1748e745fdaSopenharmony_ci 1758e745fdaSopenharmony_ci std::string GetLastRulePushTime(); 1768e745fdaSopenharmony_ci 1778e745fdaSopenharmony_ci std::string GetLastRulePushResult(); 1788e745fdaSopenharmony_ci 1798e745fdaSopenharmony_ci int32_t GetAllUserFirewallState(std::map<int32_t, bool> &firewallStateMap); 1808e745fdaSopenharmony_ci 1818e745fdaSopenharmony_ci int32_t AddDefaultNetFirewallRule(int32_t userId); 1828e745fdaSopenharmony_ci 1838e745fdaSopenharmony_ci int32_t CheckUserExist(const int32_t userId); 1848e745fdaSopenharmony_ci 1858e745fdaSopenharmony_ci void RegisterSubscribeCommonEvent(); 1868e745fdaSopenharmony_ci 1878e745fdaSopenharmony_ciprivate: 1888e745fdaSopenharmony_ci static std::shared_ptr<ffrt::queue> ffrtServiceHandler_; 1898e745fdaSopenharmony_ci std::atomic<uint64_t> currentSetRuleSecond_ = 0; 1908e745fdaSopenharmony_ci std::atomic<int64_t> lastRulePushResult_ = -1; 1918e745fdaSopenharmony_ci std::atomic<uint64_t> serviceSpendTime_ = 0; 1928e745fdaSopenharmony_ci std::atomic<int32_t> currentUserId_ = 0; 1938e745fdaSopenharmony_ci std::atomic<ServiceRunningState> state_; 1948e745fdaSopenharmony_ci bool isServicePublished_ = false; 1958e745fdaSopenharmony_ci bool hasSaRemoved_ = false; 1968e745fdaSopenharmony_ci std::shared_ptr<ReceiveMessage> subscriber_ = nullptr; 1978e745fdaSopenharmony_ci}; 1988e745fdaSopenharmony_ci} // namespace NetManagerStandard 1998e745fdaSopenharmony_ci} // namespace OHOS 2008e745fdaSopenharmony_ci#endif /* NET_FIREWALL_SERVICE_H */ 201