#!/usr/bin/env ruby # a script to convert _currently_ active iptables rules as dumped by # iptables-save into /etc/net "human readable" format as documented # in http://wiki.sisyphus.ru/admin/etcnet/firewall#h468-6 [ru] # Copyleft 2006 by Michael Shigorin require "iptables-save.rb" DEBUG=true #IPT_SAVE = "/sbin/iptables-save" IPT_SAVE = "/bin/cat iptables" SYNTAX = "/etc/net/ifaces/default/fw/iptables/syntax" FWOUTDIR = "ifaces/default/fw/iptables" NEWTABLE = /^\*([a-z]+)$/ NEWCHAIN = /^:([A-Za-z0-9_-]+) (\w+) \[(\d+):(\d+)\]$/ NEWRULE = /^-A (\w+) (.*)$/ HEADER = /^(# Generated by .*)$/ COMMIT = /^COMMIT$/ tables = Iptables.new chains = Table.new(nil) # slurp existing rules # FIXME: exception handling IO.popen(IPT_SAVE) do |rules| rules.each do |line| case line when NEWTABLE then chains = Table.new($1) # see appropriate regexp when NEWCHAIN then chains.add(Chain.new($1,$2)) when NEWRULE then chains[$1].add($2.strip) when COMMIT then tables.add(chains.commit) end end end # OK, now fluch them out # FIXME: IO errors handling! tables.each do |table| tabledir = File.join(FWOUTDIR, table.name) # one dir per table `/bin/mkdir -p #{tabledir}` table.each do |chain| File.open(File.join(tabledir, chain.name), "w") do |f| chain.each do |rule| f.puts(rule) end end end end # FIXME: figure out what's up with other policies File.open(File.join(FWOUTDIR, "..", "options"), "w") do |f| tables["filter"].each do |chain| case chain.name when "INPUT", "OUTPUT", "FORWARD" f.puts("IPTABLES_#{chain.name}_POLICY=#{chain.policy}") end end end