init.pp 8.53 KB
Newer Older
mh's avatar
mh committed
1
#
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
# modules/shorewall/manifests/init.pp - manage firewalling with shorewall 3.x
# Copyright (C) 2007 David Schmitt <david@schmitt.edv-bus.at>
# See LICENSE for the full license granted to you.
# 
# Based on the work of ADNET Ghislain <gadnet@aqueos.com> from AQUEOS
# at https://reductivelabs.com/trac/puppet/wiki/AqueosShorewall
#
# Changes:
#  * FHS Layout: put configuration in /var/lib/puppet/modules/shorewall and
#    adjust CONFIG_PATH
#  * remove shorewall- prefix from defines in the shorewall namespace
#  * refactor the whole define structure
#  * manage all shorewall files
#  * add 000-header and 999-footer files for all managed_files
#  * added rule_section define and a few more parameters for rules
#  * add managing for masq, proxyarp, blacklist, nat, rfc1918
mh's avatar
mh committed
18 19 20
# adapted by immerda project group - admin+puppet(at)immerda.ch
# adapted by Puzzle ITC - haerry+puppet(at)puzzle.ch
#
21 22 23

modules_dir { "shorewall": }

mh's avatar
mh committed
24
class shorewall { 
25

mh's avatar
mh committed
26 27 28
    case $operatingsystem {
        gentoo: { include shorewall::gentoo }
        default: { include shorewall::base }
mh's avatar
mh committed
29
    }
30

mh's avatar
mh committed
31
    file {
32 33 34
        	"/var/lib/puppet/modules/shorewall":
        		ensure => directory,
        		force => true,
mh's avatar
mh committed
35
        		mode => 0755, owner => root, group => 0;
mh's avatar
mh committed
36 37 38
    }

    # private
39
	define managed_file () {
mh's avatar
mh committed
40
		$dir = "/var/lib/puppet/modules/shorewall/${name}.d"
41
		concatenated_file { "/var/lib/puppet/modules/shorewall/$name":
mh's avatar
mh committed
42
            dir => $dir,
43 44 45 46
			mode => 0600,
		}
		file {
			"${dir}/000-header":
47
				source => "puppet://$server/shorewall/boilerplate/${name}.header",
mh's avatar
mh committed
48
				mode => 0600, owner => root, group => 0,
49 50
				notify => Exec["concat_${dir}"];
			"${dir}/999-footer":
51
				source => "puppet://$server/shorewall/boilerplate/${name}.footer",
mh's avatar
mh committed
52
				mode => 0600, owner => root, group => 0,
53 54 55 56 57 58 59 60 61 62
				notify => Exec["concat_${dir}"];
		}
	}

	# private
	define entry ($line) {
		$target = "/var/lib/puppet/modules/shorewall/${name}"
		$dir = dirname($target)
		file { $target:
			content => "${line}\n",
mh's avatar
mh committed
63
			mode => 0600, owner => root, group => 0,
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
			notify => Exec["concat_${dir}"],
		}
	}

	# See http://www.shorewall.net/3.0/Documentation.htm#Zones
	managed_file{ zones: }
	define zone($type, $options = '-', $in = '-', $out = '-', $parent = '-', $order = 100) {
		$real_name = $parent ? { '-' => $name, default => "${name}:${parent}" }
		entry { "zones.d/${order}-${name}":
			line => "${real_name} ${type} ${options} ${in} ${out}"
		}
	}

	# See http://www.shorewall.net/3.0/Documentation.htm#Interfaces
	managed_file{ interfaces: }
mh's avatar
mh committed
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
	define interface(
		$zone,
		$broadcast = 'detect',
		$options = 'tcpflags,blacklist,routefilter,nosmurfs,logmartians',
		$rfc1918 = false,
		$dhcp = false,
        $order = 100
		)
	{
		if $rfc1918 {
			if $dhcp {
				$options_real = "${options},dhcp"
			} else {
				$options_real = $options
			}
		} else {
			if $dhcp {
				$options_real = "${options},norfc1918,dhcp"
			} else {
				$options_real = "${options},norfc1918"
			}
		}

am's avatar
am committed
102
		entry { "interfaces.d/${order}-${name}":
mh's avatar
mh committed
103
			line => "${zone} ${name} ${broadcast} ${options_real}",
104 105 106 107 108
		}
	}

	# See http://www.shorewall.net/3.0/Documentation.htm#Hosts
	managed_file { hosts: }
am's avatar
am committed
109 110
	define host($zone, $options = 'tcpflags,blacklist,norfc1918',$order='100') {
		entry { "hosts.d/${order}-${name}":
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
			line => "${zone} ${name} ${options}"
		}
	}

	# See http://www.shorewall.net/3.0/Documentation.htm#Policy
	managed_file { policy: }
	define policy($sourcezone, $destinationzone, $policy, $shloglevel = '-', $limitburst = '-', $order) {
		entry { "policy.d/${order}-${name}":
			line => "# ${name}\n${sourcezone} ${destinationzone} ${policy} ${shloglevel} ${limitburst}",
		}
	}

	# See http://www.shorewall.net/3.0/Documentation.htm#Rules
	managed_file { rules: }
	define rule_section($order) {
		entry { "rules.d/${order}-${name}":
			line => "SECTION ${name}",
		}
	}
	# mark is new in 3.4.4
	define rule($action, $source, $destination, $proto = '-',
		$destinationport = '-', $sourceport = '-', $originaldest = '-',
		$ratelimit = '-', $user = '-', $mark = '', $order)
	{
		entry { "rules.d/${order}-${name}":
am's avatar
am committed
136
			line => "# ${name}\n${action} ${source} ${destination} ${proto} ${destinationport} ${sourceport} ${originaldest} ${ratelimit} ${user} ${mark}",
137 138 139 140 141 142
		}
	}

	# See http://www.shorewall.net/3.0/Documentation.htm#Masq
	managed_file{ masq: }
	# mark is new in 3.4.4
143 144 145 146
	# source (= subnet) = Set of hosts that you wish to masquerade.
	# address = If  you  specify  an  address here, SNAT will be used and this will be the source address.
	define masq($interface, $source, $address = '-', $proto = '-', $port = '-', $ipsec = '-', $mark = '', $order='100' ) {
		entry { "masq.d/${order}-${name}":
am's avatar
am committed
147
			line => "# ${name}\n${interface} ${source} ${address} ${proto} ${port} ${ipsec} ${mark}"
148 149 150 151 152
		}
	}

	# See http://www.shorewall.net/3.0/Documentation.htm#ProxyArp
	managed_file { proxyarp: }
am's avatar
am committed
153 154
	define proxyarp($interface, $external, $haveroute = yes, $persistent = no, $order='100') {
		entry { "proxyarp.d/${order}-${name}":
am's avatar
am committed
155
			line => "# ${name}\n${name} ${interface} ${external} ${haveroute} ${persistent}"
156 157 158 159 160
		}
	}

	# See http://www.shorewall.net/3.0/Documentation.htm#NAT
	managed_file { nat: }
am's avatar
am committed
161 162
	define nat($interface, $internal, $all = 'no', $local = 'yes',$order='100') {
		entry { "nat.d/${order}-${name}":
163 164 165 166 167 168
			line => "${name} ${interface} ${internal} ${all} ${local}"
		}
	}

	# See http://www.shorewall.net/3.0/Documentation.htm#Blacklist
	managed_file { blacklist: }
am's avatar
am committed
169 170
	define blacklist($proto = '-', $port = '-', $order='100') {
		entry { "blacklist.d/${order}-${name}":
171 172 173 174 175 176
			line => "${name} ${proto} ${port}",
		}
	}

	# See http://www.shorewall.net/3.0/Documentation.htm#rfc1918
	managed_file { rfc1918: }
am's avatar
am committed
177 178
	define rfc1918($action = 'logdrop', $order='100') {
		entry { "rfc1918.d/${order}-${name}":
179 180 181 182 183 184
			line => "${name} ${action}"
		}
	}
	
	# See http://www.shorewall.net/3.0/Documentation.htm#Routestopped
	managed_file { routestopped: }
mh's avatar
mh committed
185 186 187 188 189
	define routestopped($interface = '', $host = '-', $options = '', $order='100') {
        $real_interface = $interface ? {
            '' => $name,
            default => $interface,
        }
190
		entry { "routestopped.d/${order}-${name}":
mh's avatar
mh committed
191
			line => "${real_interface} ${host} ${options}",
192 193 194
		}
	}

mh's avatar
mh committed
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
    # See http://www.shorewall.net/3.0/Documentation.htm#Variables 
    managed_file { params: }
    define params($value, $order='100'){
        entry { "params.d/${order}-${name}":
            line => "${name}=${value}",
        }
    }

}

class shorewall::base {

	# service { shorewall: ensure  => running, enable  => true, }
	package { 'shorewall':
        ensure => present,
    }

    # This file has to be managed in place, so shorewall can find it
	file { "/etc/shorewall/shorewall.conf":
		# use OS specific defaults, but use Default if no other is found
		source => [
am's avatar
am committed
216 217 218 219 220 221 222 223 224
            "puppet://$server/files/shorewall/${fqdn}/shorewall.conf.$operatingsystem",
            "puppet://$server/files/shorewall/${fqdn}/shorewall.conf",
            "puppet://$server/files/shorewall/shorewall.conf.$operatingsystem.$lsbdistcodename",
            "puppet://$server/files/shorewall/shorewall.conf.$operatingsystem",
            "puppet://$server/files/shorewall/shorewall.conf",
            "puppet://$server/shorewall/shorewall.conf.$operatingsystem.$lsbdistcodename",
            "puppet://$server/shorewall/shorewall.conf.$operatingsystem",
            "puppet://$server/shorewall/shorewall.conf.Default"
        ],
mh's avatar
mh committed
225 226 227 228 229 230 231 232 233
		mode => 0644, owner => root, group => 0,
        require => Package[shorewall],
        notify => Service[shorewall],
	}

	service{shorewall: 
        ensure  => running, 
        enable  => true, 
        hasstatus => true,
mh's avatar
mh committed
234
        hasrestart => true,
mh's avatar
mh committed
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
        subscribe => [ 
            Exec["concat_/var/lib/puppet/modules/shorewall/zones"], 
            Exec["concat_/var/lib/puppet/modules/shorewall/interfaces"], 
            Exec["concat_/var/lib/puppet/modules/shorewall/hosts"], 
            Exec["concat_/var/lib/puppet/modules/shorewall/policy"], 
            Exec["concat_/var/lib/puppet/modules/shorewall/rules"], 
            Exec["concat_/var/lib/puppet/modules/shorewall/masq"], 
            Exec["concat_/var/lib/puppet/modules/shorewall/proxyarp"], 
            Exec["concat_/var/lib/puppet/modules/shorewall/nat"], 
            Exec["concat_/var/lib/puppet/modules/shorewall/blacklist"], 
            Exec["concat_/var/lib/puppet/modules/shorewall/rfc1918"], 
            Exec["concat_/var/lib/puppet/modules/shorewall/routestopped"], 
            Exec["concat_/var/lib/puppet/modules/shorewall/params"] 
        ],
        require => Package[shorewall],
    }
251 252
}

mh's avatar
mh committed
253 254 255 256 257
class shorewall::gentoo inherits shorewall::base {
    Package[shorewall]{
        category => 'net-firewall',
    }
}