# -*- coding: utf-8 -*- # # Copyright (C) 2010-2016 Red Hat, Inc. # # Authors: # Thomas Woerner <twoerner@redhat.com> # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. # __all__ = [ "ebtables" ] import os.path from firewall.core.prog import runProg from firewall.core.logger import log from firewall.functions import tempFile, readfile, splitArgs from firewall.config import COMMANDS from firewall.core import ipXtables # some common stuff lives there from firewall.errors import FirewallError, INVALID_IPV import string BUILT_IN_CHAINS = { "broute": [ "BROUTING" ], "nat": [ "PREROUTING", "POSTROUTING", "OUTPUT" ], "filter": [ "INPUT", "OUTPUT", "FORWARD" ], } DEFAULT_RULES = { } LOG_RULES = { } OUR_CHAINS = {} # chains created by firewalld for table in BUILT_IN_CHAINS.keys(): DEFAULT_RULES[table] = [ ] OUR_CHAINS[table] = set() for chain in BUILT_IN_CHAINS[table]: DEFAULT_RULES[table].append("-N %s_direct" % chain) DEFAULT_RULES[table].append("-I %s 1 -j %s_direct" % (chain, chain)) DEFAULT_RULES[table].append("-I %s_direct 1 -j RETURN" % chain) OUR_CHAINS[table].add("%s_direct" % chain) class ebtables(object): ipv = "eb" name = "ebtables" zones_supported = False # ebtables only supported with direct interface def __init__(self): self._command = COMMANDS[self.ipv] self._restore_command = COMMANDS["%s-restore" % self.ipv] self.restore_noflush_option = self._detect_restore_noflush_option() self.concurrent_option = self._detect_concurrent_option() self.fill_exists() self.available_tables = [] def fill_exists(self): self.command_exists = os.path.exists(self._command) self.restore_command_exists = os.path.exists(self._restore_command) def _detect_concurrent_option(self): # Do not change any rules, just try to use the --concurrent option # with -L concurrent_option = "" ret = runProg(self._command, ["--concurrent", "-L"]) if ret[0] == 0: concurrent_option = "--concurrent" # concurrent for ebtables lock return concurrent_option def _detect_restore_noflush_option(self): # Do not change any rules, just try to use the restore command # with --noflush rules = [ ] try: self.set_rules(rules, "off") except ValueError: return False return True def __run(self, args): # convert to string list _args = [ ] if self.concurrent_option and self.concurrent_option not in args: _args.append(self.concurrent_option) _args += ["%s" % item for item in args] log.debug2("%s: %s %s", self.__class__, self._command, " ".join(_args)) (status, ret) = runProg(self._command, _args) if status != 0: raise ValueError("'%s %s' failed: %s" % (self._command, " ".join(args), ret)) return ret def _rule_validate(self, rule): for str in ["%%REJECT%%", "%%ICMP%%", "%%LOGTYPE%%"]: if str in rule: raise FirewallError(INVALID_IPV, "'%s' invalid for ebtables" % str) def is_chain_builtin(self, ipv, table, chain): return table in BUILT_IN_CHAINS and \ chain in BUILT_IN_CHAINS[table] def build_chain_rules(self, add, table, chain): rules = [] if add: rules.append([ "-t", table, "-N", chain ]) rules.append([ "-t", table, "-I", chain, "1", "-j", "RETURN" ]) else: rules.append([ "-t", table, "-X", chain ]) return rules def build_rule(self, add, table, chain, index, args): rule = [ "-t", table ] if add: rule += [ "-I", chain, str(index) ] else: rule += [ "-D", chain ] rule += args return rule def reverse_rule(self, args): return ipXtables.common_reverse_rule(args) def check_passthrough(self, args): ipXtables.common_check_passthrough(args) def reverse_passthrough(self, args): return ipXtables.common_reverse_passthrough(args) def set_rules(self, rules, log_denied): temp_file = tempFile() table = "filter" table_rules = { } for _rule in rules: rule = _rule[:] self._rule_validate(rule) # get table form rule for opt in [ "-t", "--table" ]: try: i = rule.index(opt) except ValueError: pass else: if len(rule) >= i+1: rule.pop(i) table = rule.pop(i) # we can not use joinArgs here, because it would use "'" instead # of '"' for the start and end of the string, this breaks # iptables-restore for i in range(len(rule)): for c in string.whitespace: if c in rule[i] and not (rule[i].startswith('"') and rule[i].endswith('"')): rule[i] = '"%s"' % rule[i] table_rules.setdefault(table, []).append(rule) for table in table_rules: temp_file.write("*%s\n" % table) for rule in table_rules[table]: temp_file.write(" ".join(rule) + "\n") temp_file.close() stat = os.stat(temp_file.name) log.debug2("%s: %s %s", self.__class__, self._restore_command, "%s: %d" % (temp_file.name, stat.st_size)) args = [ ] args.append("--noflush") (status, ret) = runProg(self._restore_command, args, stdin=temp_file.name) if log.getDebugLogLevel() > 2: lines = readfile(temp_file.name) if lines is not None: i = 1 for line in lines: log.debug3("%8d: %s" % (i, line), nofmt=1, nl=0) if not line.endswith("\n"): log.debug3("", nofmt=1) i += 1 os.unlink(temp_file.name) if status != 0: raise ValueError("'%s %s' failed: %s" % (self._restore_command, " ".join(args), ret)) return ret def set_rule(self, rule, log_denied): self._rule_validate(rule) return self.__run(rule) def get_available_tables(self, table=None): ret = [] tables = [ table ] if table else BUILT_IN_CHAINS.keys() for table in tables: if table in self.available_tables: ret.append(table) else: try: self.__run(["-t", table, "-L"]) self.available_tables.append(table) ret.append(table) except ValueError: log.debug1("ebtables table '%s' does not exist." % table) return ret def get_zone_table_chains(self, table): return {} def build_flush_rules(self): rules = [] for table in BUILT_IN_CHAINS.keys(): if table not in self.get_available_tables(): continue # Flush firewall rules: -F # Delete firewall chains: -X # Set counter to zero: -Z for flag in [ "-F", "-X", "-Z" ]: rules.append(["-t", table, flag]) return rules def build_set_policy_rules(self, policy): rules = [] for table in BUILT_IN_CHAINS.keys(): if table not in self.get_available_tables(): continue for chain in BUILT_IN_CHAINS[table]: rules.append(["-t", table, "-P", chain, policy]) return rules def build_default_tables(self): # nothing to do, they always exist return [] def build_default_rules(self, log_denied="off"): default_rules = [] for table in DEFAULT_RULES: if table not in self.get_available_tables(): continue _default_rules = DEFAULT_RULES[table][:] if log_denied != "off" and table in LOG_RULES: _default_rules.extend(LOG_RULES[table]) prefix = [ "-t", table ] for rule in _default_rules: if type(rule) == list: default_rules.append(prefix + rule) else: default_rules.append(prefix + splitArgs(rule)) return default_rules def is_ipv_supported(self, ipv): return ipv == self.ipv
Name | Type | Size | Permission | Actions |
---|---|---|---|---|
io | Folder | 0755 |
|
|
.__init__.pyo.40009 | File | 145 B | 0644 |
|
.base.pyo.40009 | File | 1.29 KB | 0644 |
|
.ebtables.pyo.40009 | File | 9.04 KB | 0644 |
|
.fw.pyo.40009 | File | 30.67 KB | 0644 |
|
.fw_config.pyo.40009 | File | 30.69 KB | 0644 |
|
.fw_direct.pyo.40009 | File | 14.77 KB | 0644 |
|
.fw_helper.pyo.40009 | File | 2.57 KB | 0644 |
|
.fw_icmptype.pyo.40009 | File | 3 KB | 0644 |
|
.fw_ifcfg.pyo.40009 | File | 1.84 KB | 0644 |
|
.fw_ipset.pyo.40009 | File | 9.02 KB | 0644 |
|
.fw_nm.pyo.40009 | File | 5.93 KB | 0644 |
|
.fw_policies.pyo.40009 | File | 2.94 KB | 0644 |
|
.fw_service.pyo.40009 | File | 2.14 KB | 0644 |
|
.fw_test.pyo.40009 | File | 17.45 KB | 0644 |
|
.fw_transaction.pyo.40009 | File | 10.96 KB | 0644 |
|
.fw_zone.pyo.40009 | File | 57.31 KB | 0644 |
|
.helper.pyo.40009 | File | 222 B | 0644 |
|
.icmp.pyo.40009 | File | 2.89 KB | 0644 |
|
.ipXtables.pyo.40009 | File | 34.8 KB | 0644 |
|
.ipset.pyo.40009 | File | 9.15 KB | 0644 |
|
.logger.pyo.40009 | File | 27.43 KB | 0644 |
|
.modules.pyo.40009 | File | 3.56 KB | 0644 |
|
.nftables.pyo.40009 | File | 38.56 KB | 0644 |
|
.prog.pyo.40009 | File | 988 B | 0644 |
|
.rich.pyo.40009 | File | 23.73 KB | 0644 |
|
.watcher.pyo.40009 | File | 3.55 KB | 0644 |
|
__init__.py | File | 0 B | 0644 |
|
__init__.pyc | File | 145 B | 0644 |
|
__init__.pyo | File | 145 B | 0644 |
|
base.py | File | 1.94 KB | 0644 |
|
base.pyc | File | 1.29 KB | 0644 |
|
base.pyo | File | 1.29 KB | 0644 |
|
ebtables.py | File | 9.13 KB | 0644 |
|
ebtables.pyc | File | 9.04 KB | 0644 |
|
ebtables.pyo | File | 9.04 KB | 0644 |
|
fw.py | File | 43.71 KB | 0644 |
|
fw.pyc | File | 30.67 KB | 0644 |
|
fw.pyo | File | 30.67 KB | 0644 |
|
fw_config.py | File | 35.99 KB | 0644 |
|
fw_config.pyc | File | 30.69 KB | 0644 |
|
fw_config.pyo | File | 30.69 KB | 0644 |
|
fw_direct.py | File | 20.12 KB | 0644 |
|
fw_direct.pyc | File | 14.77 KB | 0644 |
|
fw_direct.pyo | File | 14.77 KB | 0644 |
|
fw_helper.py | File | 1.79 KB | 0644 |
|
fw_helper.pyc | File | 2.57 KB | 0644 |
|
fw_helper.pyo | File | 2.57 KB | 0644 |
|
fw_icmptype.py | File | 2.77 KB | 0644 |
|
fw_icmptype.pyc | File | 3 KB | 0644 |
|
fw_icmptype.pyo | File | 3 KB | 0644 |
|
fw_ifcfg.py | File | 2.5 KB | 0644 |
|
fw_ifcfg.pyc | File | 1.84 KB | 0644 |
|
fw_ifcfg.pyo | File | 1.84 KB | 0644 |
|
fw_ipset.py | File | 8.96 KB | 0644 |
|
fw_ipset.pyc | File | 9.02 KB | 0644 |
|
fw_ipset.pyo | File | 9.02 KB | 0644 |
|
fw_nm.py | File | 6.49 KB | 0644 |
|
fw_nm.pyc | File | 5.93 KB | 0644 |
|
fw_nm.pyo | File | 5.93 KB | 0644 |
|
fw_policies.py | File | 2.74 KB | 0644 |
|
fw_policies.pyc | File | 2.94 KB | 0644 |
|
fw_policies.pyo | File | 2.94 KB | 0644 |
|
fw_service.py | File | 1.6 KB | 0644 |
|
fw_service.pyc | File | 2.14 KB | 0644 |
|
fw_service.pyo | File | 2.14 KB | 0644 |
|
fw_test.py | File | 22.06 KB | 0644 |
|
fw_test.pyc | File | 17.45 KB | 0644 |
|
fw_test.pyo | File | 17.45 KB | 0644 |
|
fw_transaction.py | File | 10.54 KB | 0644 |
|
fw_transaction.pyc | File | 10.96 KB | 0644 |
|
fw_transaction.pyo | File | 10.96 KB | 0644 |
|
fw_zone.py | File | 75.6 KB | 0644 |
|
fw_zone.pyc | File | 57.31 KB | 0644 |
|
fw_zone.pyo | File | 57.31 KB | 0644 |
|
helper.py | File | 804 B | 0644 |
|
helper.pyc | File | 222 B | 0644 |
|
helper.pyo | File | 222 B | 0644 |
|
icmp.py | File | 3.03 KB | 0644 |
|
icmp.pyc | File | 2.89 KB | 0644 |
|
icmp.pyo | File | 2.89 KB | 0644 |
|
ipXtables.py | File | 47.68 KB | 0644 |
|
ipXtables.pyc | File | 34.8 KB | 0644 |
|
ipXtables.pyo | File | 34.8 KB | 0644 |
|
ipset.py | File | 9.1 KB | 0644 |
|
ipset.pyc | File | 9.15 KB | 0644 |
|
ipset.pyo | File | 9.15 KB | 0644 |
|
logger.py | File | 30.31 KB | 0644 |
|
logger.pyc | File | 27.43 KB | 0644 |
|
logger.pyo | File | 27.43 KB | 0644 |
|
modules.py | File | 3.63 KB | 0644 |
|
modules.pyc | File | 3.56 KB | 0644 |
|
modules.pyo | File | 3.56 KB | 0644 |
|
nftables.py | File | 60.55 KB | 0644 |
|
nftables.pyc | File | 38.56 KB | 0644 |
|
nftables.pyo | File | 38.56 KB | 0644 |
|
prog.py | File | 1.47 KB | 0644 |
|
prog.pyc | File | 988 B | 0644 |
|
prog.pyo | File | 988 B | 0644 |
|
rich.py | File | 29.34 KB | 0644 |
|
rich.pyc | File | 23.73 KB | 0644 |
|
rich.pyo | File | 23.73 KB | 0644 |
|
watcher.py | File | 3.15 KB | 0644 |
|
watcher.pyc | File | 3.55 KB | 0644 |
|
watcher.pyo | File | 3.55 KB | 0644 |
|