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 "executer_factory.h"
17
18 #include <algorithm>
19
20 #include "domain_executer.h"
21 #include "firewall_executer.h"
22 #include "rule_utils.h"
23
24 namespace OHOS {
25 namespace EDM {
26 namespace IPTABLES {
27
28 const std::string FORWARD_CHAIN = "FORWARD";
29 const std::string OUTPUT_CHAIN = "OUTPUT";
30 const std::string INPUT_CHAIN = "INPUT";
31
32 std::shared_ptr<ExecuterFactory> ExecuterFactory::instance_;
33 std::mutex ExecuterFactory::mutexLock_;
34
GetInstance()35 std::shared_ptr<ExecuterFactory> ExecuterFactory::GetInstance()
36 {
37 if (instance_ == nullptr) {
38 std::lock_guard<std::mutex> lock(mutexLock_);
39 if (instance_ == nullptr) {
40 std::shared_ptr<ExecuterFactory> temp = std::make_shared<ExecuterFactory>();
41 instance_ = temp;
42
43 InitDefaultExecuter();
44
45 InitFirewallExecuter();
46
47 InitDomainExecuter();
48 }
49 }
50 return instance_;
51 }
52
InitDefaultExecuter()53 void ExecuterFactory::InitDefaultExecuter()
54 {
55 instance_->chainNames_.emplace_back(EDM_DEFAULT_DENY_OUTPUT_CHAIN_NAME);
56 instance_->executerVector_.emplace_back(std::make_shared<FirewallExecuter>(OUTPUT_CHAIN,
57 EDM_DEFAULT_DENY_OUTPUT_CHAIN_NAME));
58 instance_->chainNames_.emplace_back(EDM_DEFAULT_DENY_FORWARD_CHAIN_NAME);
59 instance_->executerVector_.emplace_back(std::make_shared<FirewallExecuter>(FORWARD_CHAIN,
60 EDM_DEFAULT_DENY_FORWARD_CHAIN_NAME));
61 instance_->chainNames_.emplace_back(EDM_DEFAULT_DNS_DENY_OUTPUT_CHAIN_NAME);
62 instance_->executerVector_.emplace_back(std::make_shared<DomainExecuter>(OUTPUT_CHAIN,
63 EDM_DEFAULT_DNS_DENY_OUTPUT_CHAIN_NAME));
64 instance_->chainNames_.emplace_back(EDM_DEFAULT_DNS_DENY_FORWARD_CHAIN_NAME);
65 instance_->executerVector_.emplace_back(std::make_shared<DomainExecuter>(FORWARD_CHAIN,
66 EDM_DEFAULT_DNS_DENY_FORWARD_CHAIN_NAME));
67 }
68
InitFirewallExecuter()69 void ExecuterFactory::InitFirewallExecuter()
70 {
71 instance_->chainNames_.emplace_back(EDM_REJECT_OUTPUT_CHAIN_NAME);
72 instance_->executerVector_.emplace_back(std::make_shared<FirewallExecuter>(OUTPUT_CHAIN,
73 EDM_REJECT_OUTPUT_CHAIN_NAME));
74 instance_->chainNames_.emplace_back(EDM_REJECT_INPUT_CHAIN_NAME);
75 instance_->executerVector_.emplace_back(std::make_shared<FirewallExecuter>(INPUT_CHAIN,
76 EDM_REJECT_INPUT_CHAIN_NAME));
77 instance_->chainNames_.emplace_back(EDM_REJECT_FORWARD_CHAIN_NAME);
78 instance_->executerVector_.emplace_back(std::make_shared<FirewallExecuter>(FORWARD_CHAIN,
79 EDM_REJECT_FORWARD_CHAIN_NAME));
80 instance_->chainNames_.emplace_back(EDM_DENY_OUTPUT_CHAIN_NAME);
81 instance_->executerVector_.emplace_back(std::make_shared<FirewallExecuter>(OUTPUT_CHAIN,
82 EDM_DENY_OUTPUT_CHAIN_NAME));
83 instance_->chainNames_.emplace_back(EDM_DENY_INPUT_CHAIN_NAME);
84 instance_->executerVector_.emplace_back(std::make_shared<FirewallExecuter>(INPUT_CHAIN,
85 EDM_DENY_INPUT_CHAIN_NAME));
86 instance_->chainNames_.emplace_back(EDM_DENY_FORWARD_CHAIN_NAME);
87 instance_->executerVector_.emplace_back(std::make_shared<FirewallExecuter>(FORWARD_CHAIN,
88 EDM_DENY_FORWARD_CHAIN_NAME));
89 instance_->chainNames_.emplace_back(EDM_ALLOW_OUTPUT_CHAIN_NAME);
90 instance_->executerVector_.emplace_back(std::make_shared<FirewallExecuter>(OUTPUT_CHAIN,
91 EDM_ALLOW_OUTPUT_CHAIN_NAME));
92 instance_->chainNames_.emplace_back(EDM_ALLOW_INPUT_CHAIN_NAME);
93 instance_->executerVector_.emplace_back(std::make_shared<FirewallExecuter>(INPUT_CHAIN,
94 EDM_ALLOW_INPUT_CHAIN_NAME));
95 instance_->chainNames_.emplace_back(EDM_ALLOW_FORWARD_CHAIN_NAME);
96 instance_->executerVector_.emplace_back(std::make_shared<FirewallExecuter>(FORWARD_CHAIN,
97 EDM_ALLOW_FORWARD_CHAIN_NAME));
98 }
99
InitDomainExecuter()100 void ExecuterFactory::InitDomainExecuter()
101 {
102 instance_->chainNames_.emplace_back(EDM_DNS_REJECT_OUTPUT_CHAIN_NAME);
103 instance_->executerVector_.emplace_back(std::make_shared<DomainExecuter>(OUTPUT_CHAIN,
104 EDM_DNS_REJECT_OUTPUT_CHAIN_NAME));
105 instance_->chainNames_.emplace_back(EDM_DNS_REJECT_FORWARD_CHAIN_NAME);
106 instance_->executerVector_.emplace_back(std::make_shared<DomainExecuter>(FORWARD_CHAIN,
107 EDM_DNS_REJECT_FORWARD_CHAIN_NAME));
108 instance_->chainNames_.emplace_back(EDM_DNS_DENY_OUTPUT_CHAIN_NAME);
109 instance_->executerVector_.emplace_back(std::make_shared<DomainExecuter>(OUTPUT_CHAIN,
110 EDM_DNS_DENY_OUTPUT_CHAIN_NAME));
111 instance_->chainNames_.emplace_back(EDM_DNS_DENY_FORWARD_CHAIN_NAME);
112 instance_->executerVector_.emplace_back(std::make_shared<DomainExecuter>(FORWARD_CHAIN,
113 EDM_DNS_DENY_FORWARD_CHAIN_NAME));
114 instance_->chainNames_.emplace_back(EDM_DNS_ALLOW_OUTPUT_CHAIN_NAME);
115 instance_->executerVector_.emplace_back(std::make_shared<DomainExecuter>(OUTPUT_CHAIN,
116 EDM_DNS_ALLOW_OUTPUT_CHAIN_NAME));
117 instance_->chainNames_.emplace_back(EDM_DNS_ALLOW_FORWARD_CHAIN_NAME);
118 instance_->executerVector_.emplace_back(std::make_shared<DomainExecuter>(FORWARD_CHAIN,
119 EDM_DNS_ALLOW_FORWARD_CHAIN_NAME));
120 }
121
GetExecuter(const std::string & chainName) const122 std::shared_ptr<IExecuter> ExecuterFactory::GetExecuter(const std::string& chainName) const
123 {
124 auto it = std::find(chainNames_.begin(), chainNames_.end(), chainName);
125 if (it != chainNames_.end()) {
126 int index = it - chainNames_.begin();
127 return executerVector_[index];
128 }
129 return nullptr;
130 }
131
GetAllExecuter() const132 std::vector<std::shared_ptr<IExecuter>> ExecuterFactory::GetAllExecuter() const
133 {
134 return executerVector_;
135 }
136 } // namespace IPTABLES
137 } // namespace EDM
138 } // namespace OHOS