# Copyright(C) 2005 INL # Written by Jean Gillaux <jean@inl.fr> # # 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, version 3 of the License. # # 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/>. # classes for iptables NAT rules from os import linesep import re def gen_rule(*args): text = " ".join(args) # Remove trailing spaces text = re.sub(" +%s$" % linesep, linesep, text) return text def gen_rule_list(arg_list): return gen_rule(*arg_list) _TARGET_NAMES = { 'accept': 'ACCEPT', 'reject': 'REJECT', } def make_target(s): return _TARGET_NAMES.get(s, 'DROP') def _make_syn(target,h): """returns chain that fits well to the target of the iptables command example: for accept target: we want to match syn packets, the others are matched by the RELATED/ESTABLISHED rules """ res = '' if target == 'ACCEPT': res += '-m state --state NEW' if h.get('proto') == 'tcp': res += ' --syn' return res