-
Notifications
You must be signed in to change notification settings - Fork 457
/
Copy pathfirewall.rb
2522 lines (2028 loc) · 74.2 KB
/
firewall.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# frozen_string_literal: true
# See: #10295 for more details.
#
# This is a workaround for bug: #4248 whereby ruby files outside of the normal
# provider/type path do not load until pluginsync has occured on the puppet server
#
# In this case I'm trying the relative path first, then falling back to normal
# mechanisms. This should be fixed in future versions of puppet but it looks
# like we'll need to maintain this for some time perhaps.
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', '..'))
require 'puppet/util/firewall'
Puppet::Type.newtype(:firewall) do
include Puppet::Util::Firewall
@doc = <<-PUPPETCODE
@summary
This type provides the capability to manage firewall rules within puppet.
**Autorequires:**
If Puppet is managing the iptables or ip6tables chains specified in the
`chain` or `jump` parameters, the firewall resource will autorequire
those firewallchain resources.
If Puppet is managing the iptables, iptables-persistent, or iptables-services packages,
and the provider is iptables or ip6tables, the firewall resource will
autorequire those packages to ensure that any required binaries are
installed.
#### Providers
Note: Not all features are available with all providers.
* ip6tables: Ip6tables type provider
* Required binaries: ip6tables-save, ip6tables.
* Supported features: address_type, connection_limiting, conntrack, dnat, hop_limiting, icmp_match,
interface_match, iprange, ipsec_dir, ipsec_policy, ipset, iptables, isfirstfrag,
ishasmorefrags, islastfrag, length, log_level, log_prefix, log_uid,
log_tcp_sequence, log_tcp_options, log_ip_options, mask, mss,
owner, pkttype, queue_bypass, queue_num, rate_limiting, recent_limiting, reject_type,
snat, socket, state_match, string_matching, tcp_flags, hashlimit, bpf.
* iptables: Iptables type provider
* Required binaries: iptables-save, iptables.
* Default for kernel == linux.
* Supported features: address_type, clusterip, connection_limiting, conntrack, dnat, icmp_match,
interface_match, iprange, ipsec_dir, ipsec_policy, ipset, iptables, isfragment, length,
log_level, log_prefix, log_uid, log_tcp_sequence, log_tcp_options, log_ip_options,
mark, mask, mss, netmap, nflog_group, nflog_prefix,
nflog_range, nflog_threshold, owner, pkttype, queue_bypass, queue_num, rate_limiting,
recent_limiting, reject_type, snat, socket, state_match, string_matching, tcp_flags, bpf.
#### Features
* address_type: The ability to match on source or destination address type.
* clusterip: Configure a simple cluster of nodes that share a certain IP and MAC address without an explicit load balancer in front of them.
* condition: Match if a specific condition variable is (un)set (requires xtables-addons)
* connection_limiting: Connection limiting features.
* conntrack: Connection tracking features.
* dnat: Destination NATing.
* hop_limiting: Hop limiting features.
* icmp_match: The ability to match ICMP types.
* interface_match: Interface matching.
* iprange: The ability to match on source or destination IP range.
* ipsec_dir: The ability to match IPsec policy direction.
* ipsec_policy: The ability to match IPsec policy.
* iptables: The provider provides iptables features.
* isfirstfrag: The ability to match the first fragment of a fragmented ipv6 packet.
* isfragment: The ability to match fragments.
* ishasmorefrags: The ability to match a non-last fragment of a fragmented ipv6 packet.
* islastfrag: The ability to match the last fragment of an ipv6 packet.
* length: The ability to match the length of the layer-3 payload.
* log_level: The ability to control the log level.
* log_prefix: The ability to add prefixes to log messages.
* log_uid: The ability to log the userid of the process which generated the packet.
* log_tcp_sequence: The ability to log TCP sequence numbers.
* log_tcp_options: The ability to log TCP packet header.
* log_ip_options: The ability to log IP/IPv6 packet header.
* mark: The ability to match or set the netfilter mark value associated with the packet.
* mask: The ability to match recent rules based on the ipv4 mask.
* nflog_group: The ability to set the group number for NFLOG.
* nflog_prefix: The ability to set a prefix for nflog messages.
* nflog_range: The ability to set nflog_range.
* nflog_threshold: The ability to set nflog_threshold.
* owner: The ability to match owners.
* pkttype: The ability to match a packet type.
* rate_limiting: Rate limiting features.
* recent_limiting: The netfilter recent module.
* reject_type: The ability to control reject messages.
* set_mss: Set the TCP MSS of a packet.
* snat: Source NATing.
* socket: The ability to match open sockets.
* state_match: The ability to match stateful firewall states.
* string_matching: The ability to match a given string by using some pattern matching strategy.
* tcp_flags: The ability to match on particular TCP flag settings.
* netmap: The ability to map entire subnets via source or destination nat rules.
* hashlimit: The ability to use the hashlimit-module.
* bpf: The ability to use Berkeley Paket Filter rules.
* ipvs: The ability to match IP Virtual Server packets.
* ct_target: The ability to set connection tracking parameters for a packet or its associated connection.
* random_fully: The ability to use --random-fully flag.
PUPPETCODE
feature :connection_limiting, 'Connection limiting features.'
feature :condition, 'Match if a specific condition variable is (un)set.'
feature :conntrack, 'Connection tracking features.'
feature :hop_limiting, 'Hop limiting features.'
feature :rate_limiting, 'Rate limiting features.'
feature :recent_limiting, 'The netfilter recent module'
feature :snat, 'Source NATing'
feature :dnat, 'Destination NATing'
feature :netmap, 'NET MAPping'
feature :interface_match, 'Interface matching'
feature :icmp_match, 'Matching ICMP types'
feature :owner, 'Matching owners'
feature :state_match, 'Matching stateful firewall states'
feature :reject_type, 'The ability to control reject messages'
feature :log_level, 'The ability to control the log level'
feature :log_prefix, 'The ability to add prefixes to log messages'
feature :log_uid, 'Add UIDs to log messages'
feature :log_tcp_sequence, 'Add TCP sequence numbers to log messages'
feature :log_tcp_options, 'Add TCP packet header to log messages'
feature :log_ip_options, 'Add IP/IPv6 packet header to log messages'
feature :mark, 'Match or Set the netfilter mark value associated with the packet'
feature :mss, 'Match a given TCP MSS value or range.'
feature :tcp_flags, 'The ability to match on particular TCP flag settings'
feature :pkttype, 'Match a packet type'
feature :rpfilter, 'Perform reverse-path filtering'
feature :socket, 'Match open sockets'
feature :isfragment, 'Match fragments'
feature :address_type, 'The ability match on source or destination address type'
feature :iprange, 'The ability match on source or destination IP range '
feature :ishasmorefrags, 'Match a non-last fragment of a fragmented ipv6 packet - might be first'
feature :islastfrag, 'Match the last fragment of an ipv6 packet'
feature :isfirstfrag, 'Match the first fragment of a fragmented ipv6 packet'
feature :ipsec_policy, 'Match IPsec policy'
feature :ipsec_dir, 'Match IPsec policy direction'
feature :mask, 'Ability to match recent rules based on the ipv4 mask'
feature :nflog_group, 'netlink group to subscribe to for logging'
feature :nflog_prefix, ''
feature :nflog_range, ''
feature :nflog_threshold, ''
feature :ipset, 'Match against specified ipset list'
feature :clusterip, 'Configure a simple cluster of nodes that share a certain IP and MAC address without an explicit load balancer in front of them.'
feature :length, 'Match the length of layer-3 payload'
feature :string_matching, 'String matching features'
feature :queue_num, 'Which NFQUEUE to send packets to'
feature :queue_bypass, 'If nothing is listening on queue_num, allow packets to bypass the queue'
feature :hashlimit, 'Hashlimit features'
feature :bpf, 'Berkeley Paket Filter feature'
feature :ipvs, 'Packet belongs to an IP Virtual Server connection'
feature :ct_target, 'The ability to set connection tracking parameters for a packet or its associated connection'
feature :random_fully, 'The ability to use --random-fully flag'
# provider specific features
feature :iptables, 'The provider provides iptables features.'
ensurable do
desc <<-PUPPETCODE
Manage the state of this rule.
PUPPETCODE
newvalue(:present) do
provider.insert
end
newvalue(:absent) do
provider.delete
end
defaultto :present
end
newparam(:name) do
desc <<-PUPPETCODE
The canonical name of the rule. This name is also used for ordering
so make sure you prefix the rule with a number:
000 this runs first
999 this runs last
Depending on the provider, the name of the rule can be stored using
the comment feature of the underlying firewall subsystem.
PUPPETCODE
isnamevar
# Keep rule names simple - they must start with a number
newvalues(%r{^\d+[[:graph:][:space:]]+$})
end
newproperty(:action) do
desc <<-PUPPETCODE
This is the action to perform on a match. Can be one of:
* accept - the packet is accepted
* reject - the packet is rejected with a suitable ICMP response
* drop - the packet is dropped
If you specify no value it will simply match the rule but perform no
action unless you provide a provider specific parameter (such as *jump*).
PUPPETCODE
newvalues(:accept, :reject, :drop)
end
# Generic matching properties
newproperty(:source) do
desc <<-PUPPETCODE
The source address. For example:
source => '192.168.2.0/24'
You can also negate a mask by putting ! in front. For example:
source => '! 192.168.2.0/24'
The source can also be an IPv6 address if your provider supports it.
PUPPETCODE
munge do |value|
case @resource[:provider]
when :iptables
protocol = :IPv4
when :ip6tables
protocol = :IPv6
else
raise('cannot work out protocol family')
end
begin
@resource.host_to_mask(value, protocol)
rescue StandardError => e
raise("host_to_ip failed for #{value}, exception #{e}")
end
end
end
# Source IP range
newproperty(:src_range, required_features: :iprange) do
desc <<-PUPPETCODE
The source IP range. For example:
src_range => '192.168.1.1-192.168.1.10'
The source IP range must be in 'IP1-IP2' format.
PUPPETCODE
validate do |value|
matches = %r{^([^\-\/]+)-([^\-\/]+)$}.match(value)
raise(ArgumentError, "The source IP range must be in 'IP1-IP2' format.") unless matches
start_addr = matches[1]
end_addr = matches[2]
[start_addr, end_addr].each do |addr|
begin
@resource.host_to_ip(addr)
rescue StandardError
raise("Invalid IP address \"#{addr}\" in range \"#{value}\"")
end
end
end
end
newproperty(:destination) do
desc <<-PUPPETCODE
The destination address to match. For example:
destination => '192.168.1.0/24'
You can also negate a mask by putting ! in front. For example:
destination => '! 192.168.2.0/24'
The destination can also be an IPv6 address if your provider supports it.
PUPPETCODE
munge do |value|
case @resource[:provider]
when :iptables
protocol = :IPv4
when :ip6tables
protocol = :IPv6
else
raise('cannot work out protocol family')
end
begin
@resource.host_to_mask(value, protocol)
rescue StandardError => e
raise("host_to_ip failed for #{value}, exception #{e}")
end
end
end
# Destination IP range
newproperty(:dst_range, required_features: :iprange) do
desc <<-PUPPETCODE
The destination IP range. For example:
dst_range => '192.168.1.1-192.168.1.10'
The destination IP range must be in 'IP1-IP2' format.
PUPPETCODE
validate do |value|
matches = %r{^([^\-\/]+)-([^\-\/]+)$}.match(value)
raise(ArgumentError, "The destination IP range must be in 'IP1-IP2' format.") unless matches
start_addr = matches[1]
end_addr = matches[2]
[start_addr, end_addr].each do |addr|
begin
@resource.host_to_ip(addr)
rescue StandardError
raise("Invalid IP address \"#{addr}\" in range \"#{value}\"")
end
end
end
end
newproperty(:sport, array_matching: :all) do
desc <<-PUPPETCODE
The source port to match for this filter (if the protocol supports
ports). Will accept a single element or an array.
For some firewall providers you can pass a range of ports in the format:
<start_number>-<ending_number>
For example:
1-1024
This would cover ports 1 to 1024.
PUPPETCODE
munge do |value|
@resource.string_to_port(value, :proto)
end
def to_s?(value)
should_to_s(value)
end
def should_to_s(value)
value = [value] unless value.is_a?(Array)
value.join(',')
end
end
newproperty(:dport, array_matching: :all) do
desc <<-PUPPETCODE
The destination port to match for this filter (if the protocol supports
ports). Will accept a single element or an array.
For some firewall providers you can pass a range of ports in the format:
<start_number>-<ending_number>
For example:
1-1024
This would cover ports 1 to 1024.
PUPPETCODE
munge do |value|
@resource.string_to_port(value, :proto)
end
def to_s?(value)
should_to_s(value)
end
def should_to_s(value)
value = [value] unless value.is_a?(Array)
value.join(',')
end
end
newproperty(:port, array_matching: :all) do
desc <<-PUPPETCODE
*note* This property has been DEPRECATED
The destination or source port to match for this filter (if the protocol
supports ports). Will accept a single element or an array.
For some firewall providers you can pass a range of ports in the format:
<start_number>-<ending_number>
For example:
1-1024
This would cover ports 1 to 1024.
PUPPETCODE
validate do |_value|
Puppet.warning('Passing port to firewall is deprecated and will be removed. Use dport and/or sport instead.')
end
munge do |value|
@resource.string_to_port(value, :proto)
end
def to_s?(value)
should_to_s(value)
end
def should_to_s(value)
value = [value] unless value.is_a?(Array)
value.join(',')
end
end
newproperty(:dst_type, required_features: :address_type, array_matching: :all) do
desc <<-PUPPETCODE
The destination address type. For example:
dst_type => ['LOCAL']
Can be one of:
* UNSPEC - an unspecified address
* UNICAST - a unicast address
* LOCAL - a local address
* BROADCAST - a broadcast address
* ANYCAST - an anycast packet
* MULTICAST - a multicast address
* BLACKHOLE - a blackhole address
* UNREACHABLE - an unreachable address
* PROHIBIT - a prohibited address
* THROW - undocumented
* NAT - undocumented
* XRESOLVE - undocumented
In addition, it accepts '--limit-iface-in' and '--limit-iface-out' flags, specified as:
dst_type => ['LOCAL --limit-iface-in']
It can also be negated using '!':
dst_type => ['! LOCAL']
Will accept a single element or an array.
PUPPETCODE
newvalues(*[:UNSPEC, :UNICAST, :LOCAL, :BROADCAST, :ANYCAST, :MULTICAST,
:BLACKHOLE, :UNREACHABLE, :PROHIBIT, :THROW, :NAT, :XRESOLVE].map { |address_type|
[
address_type,
"! #{address_type}".to_sym,
"#{address_type} --limit-iface-in".to_sym,
"#{address_type} --limit-iface-out".to_sym,
"! #{address_type} --limit-iface-in".to_sym,
"! #{address_type} --limit-iface-out".to_sym,
]
}.flatten)
end
newproperty(:src_type, required_features: :address_type, array_matching: :all) do
desc <<-PUPPETCODE
The source address type. For example:
src_type => ['LOCAL']
Can be one of:
* UNSPEC - an unspecified address
* UNICAST - a unicast address
* LOCAL - a local address
* BROADCAST - a broadcast address
* ANYCAST - an anycast packet
* MULTICAST - a multicast address
* BLACKHOLE - a blackhole address
* UNREACHABLE - an unreachable address
* PROHIBIT - a prohibited address
* THROW - undocumented
* NAT - undocumented
* XRESOLVE - undocumented
In addition, it accepts '--limit-iface-in' and '--limit-iface-out' flags, specified as:
src_type => ['LOCAL --limit-iface-in']
It can also be negated using '!':
src_type => ['! LOCAL']
Will accept a single element or an array.
PUPPETCODE
newvalues(*[:UNSPEC, :UNICAST, :LOCAL, :BROADCAST, :ANYCAST, :MULTICAST,
:BLACKHOLE, :UNREACHABLE, :PROHIBIT, :THROW, :NAT, :XRESOLVE].map { |address_type|
[
address_type,
"! #{address_type}".to_sym,
"#{address_type} --limit-iface-in".to_sym,
"#{address_type} --limit-iface-out".to_sym,
"! #{address_type} --limit-iface-in".to_sym,
"! #{address_type} --limit-iface-out".to_sym,
]
}.flatten)
end
newproperty(:proto) do
desc <<-PUPPETCODE
The specific protocol to match for this rule.
PUPPETCODE
newvalues(*[:ip, :tcp, :udp, :icmp, :"ipv6-icmp", :esp, :ah, :vrrp, :igmp, :ipencap, :ipv4, :ipv6, :ospf, :gre, :cbt, :sctp, :pim, :all].map { |proto|
[proto, "! #{proto}".to_sym]
}.flatten)
defaultto 'tcp'
end
# tcp-specific
newproperty(:mss) do
desc <<-PUPPETCODE
Match a given TCP MSS value or range.
PUPPETCODE
end
# tcp-specific
newproperty(:tcp_flags, required_features: :tcp_flags) do
desc <<-PUPPETCODE
Match when the TCP flags are as specified.
Is a string with a list of comma-separated flag names for the mask,
then a space, then a comma-separated list of flags that should be set.
The flags are: SYN ACK FIN RST URG PSH ALL NONE
Note that you specify them in the order that iptables --list-rules
would list them to avoid having puppet think you changed the flags.
Example: FIN,SYN,RST,ACK SYN matches packets with the SYN bit set and the
ACK,RST and FIN bits cleared. Such packets are used to request
TCP connection initiation.
PUPPETCODE
end
# Iptables specific
newproperty(:chain, required_features: :iptables) do
desc <<-PUPPETCODE
Name of the chain to use. Can be one of the built-ins:
* INPUT
* FORWARD
* OUTPUT
* PREROUTING
* POSTROUTING
Or you can provide a user-based chain.
PUPPETCODE
defaultto 'INPUT'
newvalue(%r{^[a-zA-Z0-9\-_]+$})
end
newproperty(:table, required_features: :iptables) do
desc <<-PUPPETCODE
Table to use. Can be one of:
* nat
* mangle
* filter
* raw
* rawpost
PUPPETCODE
newvalues(:nat, :mangle, :filter, :raw, :rawpost)
defaultto 'filter'
end
newproperty(:jump, required_features: :iptables) do
desc <<-PUPPETCODE
The value for the iptables --jump parameter. Normal values are:
* QUEUE
* RETURN
* DNAT
* SNAT
* LOG
* NFLOG
* MASQUERADE
* REDIRECT
* MARK
* CT
But any valid chain name is allowed.
For the values ACCEPT, DROP, and REJECT, you must use the generic
'action' parameter. This is to enfore the use of generic parameters where
possible for maximum cross-platform modelling.
If you set both 'accept' and 'jump' parameters, you will get an error as
only one of the options should be set.
PUPPETCODE
validate do |value|
unless %r{^[a-zA-Z0-9\-_]+$}.match?(value)
raise ArgumentError, <<-PUPPETCODE
Jump destination must consist of alphanumeric characters, an
underscore or a hyphen.
PUPPETCODE
end
if ['accept', 'reject', 'drop'].include?(value.downcase)
raise ArgumentError, <<-PUPPETCODE
Jump destination should not be one of ACCEPT, REJECT or DROP. Use
the action property instead.
PUPPETCODE
end
end
end
newproperty(:goto, required_features: :iptables) do
desc <<-PUPPETCODE
The value for the iptables --goto parameter. Normal values are:
* QUEUE
* RETURN
* DNAT
* SNAT
* LOG
* MASQUERADE
* REDIRECT
* MARK
But any valid chain name is allowed.
PUPPETCODE
validate do |value|
unless %r{^[a-zA-Z0-9\-_]+$}.match?(value)
raise ArgumentError, <<-PUPPETCODE
Goto destination must consist of alphanumeric characters, an
underscore or a hyphen.
PUPPETCODE
end
if ['accept', 'reject', 'drop'].include?(value.downcase)
raise ArgumentError, <<-PUPPETCODE
Goto destination should not be one of ACCEPT, REJECT or DROP. Use
the action property instead.
PUPPETCODE
end
end
end
# Interface specific matching properties
newproperty(:iniface, required_features: :interface_match) do
desc <<-PUPPETCODE
Input interface to filter on. Supports interface alias like eth0:0.
To negate the match try this:
iniface => '! lo',
PUPPETCODE
newvalues(%r{^!?\s?[a-zA-Z0-9\-\._\+\:@]+$})
end
newproperty(:outiface, required_features: :interface_match) do
desc <<-PUPPETCODE
Output interface to filter on. Supports interface alias like eth0:0.
To negate the match try this:
outiface => '! lo',
PUPPETCODE
newvalues(%r{^!?\s?[a-zA-Z0-9\-\._\+\:@]+$})
end
# NAT specific properties
newproperty(:tosource, required_features: :snat) do
desc <<-PUPPETCODE
When using jump => "SNAT" you can specify the new source address using
this parameter.
PUPPETCODE
end
newproperty(:todest, required_features: :dnat) do
desc <<-PUPPETCODE
When using jump => "DNAT" you can specify the new destination address
using this paramter.
PUPPETCODE
end
newproperty(:toports, required_features: :dnat) do
desc <<-PUPPETCODE
For DNAT this is the port that will replace the destination port.
PUPPETCODE
end
newproperty(:to, required_features: :netmap) do
desc <<-PUPPETCODE
For NETMAP this will replace the destination IP
PUPPETCODE
end
newproperty(:random_fully, required_features: :random_fully) do
desc <<-PUPPETCODE
When using a jump value of "MASQUERADE", "DNAT", "REDIRECT", or "SNAT"
this boolean will enable fully randomized port mapping.
**NOTE** Requires Kernel >= 3.13 and iptables >= 1.6.2
PUPPETCODE
newvalues(:true, :false)
end
newproperty(:random, required_features: :dnat) do
desc <<-PUPPETCODE
When using a jump value of "MASQUERADE", "DNAT", "REDIRECT", or "SNAT"
this boolean will enable randomized port mapping.
PUPPETCODE
newvalues(:true, :false)
end
# Reject ICMP type
newproperty(:reject, required_features: :reject_type) do
desc <<-PUPPETCODE
When combined with action => "REJECT" you can specify a different icmp
response to be sent back to the packet sender.
PUPPETCODE
end
# Logging properties
newproperty(:log_level, required_features: :log_level) do
desc <<-PUPPETCODE
When combined with jump => "LOG" specifies the system log level to log
to.
PUPPETCODE
munge do |value|
if value.is_a?(String)
value = @resource.log_level_name_to_number(value)
else
value
end
if value.nil? && value != ''
raise('Unable to determine log level')
end
value
end
end
newproperty(:log_prefix, required_features: :log_prefix) do
desc <<-PUPPETCODE
When combined with jump => "LOG" specifies the log prefix to use when
logging.
PUPPETCODE
munge do |value|
if value == ''
raise('log_prefix should not be an empty string')
end
value
end
end
newproperty(:log_uid, required_features: :log_uid) do
desc <<-PUPPETCODE
When combined with jump => "LOG" specifies the uid of the process making
the connection.
PUPPETCODE
newvalues(:true, :false)
end
newproperty(:log_tcp_sequence, required_features: :log_tcp_sequence) do
desc <<-PUPPETCODE
When combined with jump => "LOG" enables logging of the TCP sequence
numbers.
PUPPETCODE
newvalues(:true, :false)
end
newproperty(:log_tcp_options, required_features: :log_tcp_options) do
desc <<-PUPPETCODE
When combined with jump => "LOG" logging of the TCP packet
header.
PUPPETCODE
newvalues(:true, :false)
end
newproperty(:log_ip_options, required_features: :log_ip_options) do
desc <<-PUPPETCODE
When combined with jump => "LOG" logging of the TCP IP/IPv6
packet header.
PUPPETCODE
newvalues(:true, :false)
end
newproperty(:nflog_group, required_features: :nflog_group) do
desc <<-PUPPETCODE
Used with the jump target NFLOG.
The netlink group (0 - 2^16-1) to which packets are (only applicable
for nfnetlink_log). Defaults to 0.
PUPPETCODE
validate do |value|
if value.to_i > (2**16) - 1 || value.to_i < 0
raise ArgumentError, 'nflog_group must be between 0 and 2^16-1'
end
end
munge do |value|
if value.is_a?(String) && value =~ %r{^[-0-9]+$}
Integer(value)
else
value
end
end
end
newproperty(:nflog_prefix, required_features: :nflog_prefix) do
desc <<-PUPPETCODE
Used with the jump target NFLOG.
A prefix string to include in the log message, up to 64 characters long,
useful for distinguishing messages in the logs.
PUPPETCODE
validate do |value|
if value.length > 64
raise ArgumentError, 'nflog_prefix must be less than 64 characters.'
end
end
end
newproperty(:nflog_range, required_features: :nflog_range) do
desc <<-PUPPETCODE
Used with the jump target NFLOG.
The number of bytes to be copied to userspace (only applicable for nfnetlink_log).
nfnetlink_log instances may specify their own range, this option overrides it.
PUPPETCODE
end
newproperty(:nflog_threshold, required_features: :nflog_threshold) do
desc <<-PUPPETCODE
Used with the jump target NFLOG.
Number of packets to queue inside the kernel before sending them to userspace
(only applicable for nfnetlink_log). Higher values result in less overhead
per packet, but increase delay until the packets reach userspace. Defaults to 1.
PUPPETCODE
munge do |value|
if value.is_a?(String) && value =~ %r{^[-0-9]+$}
Integer(value)
else
value
end
end
end
# ICMP matching property
newproperty(:icmp, required_features: :icmp_match) do
desc <<-PUPPETCODE
When matching ICMP packets, this is the type of ICMP packet to match.
A value of "any" is not supported. To achieve this behaviour the
parameter should simply be omitted or undefined.
An array of values is also not supported. To match against multiple ICMP
types, please use separate rules for each ICMP type.
PUPPETCODE
validate do |value|
if value == 'any'
raise ArgumentError,
"Value 'any' is not valid. This behaviour should be achieved " \
'by omitting or undefining the ICMP parameter.'
end
if value.is_a?(Array)
raise ArgumentError,
'Argument must not be an array of values. To match multiple ' \
'ICMP types, please use separate rules for each ICMP type.'
end
end
munge do |value|
if value.is_a?(String)
# ICMP codes differ between IPv4 and IPv6.
case @resource[:provider]
when :iptables
protocol = 'inet'
when :ip6tables
protocol = 'inet6'
else
raise('cannot work out protocol family')
end
value = @resource.icmp_name_to_number(value, protocol)
else
value
end
if value.nil? && value != ''
raise('cannot work out icmp type')
end
value
end
end
newproperty(:state, array_matching: :all, required_features: :state_match) do
desc <<-PUPPETCODE
Matches a packet based on its state in the firewall stateful inspection
table. Values can be:
* INVALID
* ESTABLISHED
* NEW
* RELATED
* UNTRACKED
PUPPETCODE
newvalues(:INVALID, :ESTABLISHED, :NEW, :RELATED, :UNTRACKED)
# States should always be sorted. This normalizes the resource states to
# keep it consistent with the sorted result from iptables-save.
def should=(values)
@should = super(values).sort_by { |sym| sym.to_s }
end
def to_s?(value)
should_to_s(value)
end
def should_to_s(value)
value = [value] unless value.is_a?(Array)
value.join(',')
end
end
newproperty(:ctstate, array_matching: :all, required_features: :conntrack) do
desc <<-PUPPETCODE
Matches a packet based on its state in the firewall stateful inspection
table, using the conntrack module. Values can be:
* INVALID
* ESTABLISHED
* NEW
* RELATED
* UNTRACKED
* SNAT
* DNAT
PUPPETCODE
newvalues(:INVALID, :ESTABLISHED, :NEW, :RELATED, :UNTRACKED, :SNAT, :DNAT)
# States should always be sorted. This normalizes the resource states to
# keep it consistent with the sorted result from iptables-save.
def should=(values)