Puppet Function: iptables_generate_rule

Defined in:
lib/puppet/parser/functions/iptables_generate_rule.rb
Function type:
Ruby 3.x API

Overview

iptables_generate_rule()Any

Provided an array of options, generates iptables rule(s).

Returns:

  • (Any)


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
# File 'lib/puppet/parser/functions/iptables_generate_rule.rb', line 2

newfunction(:iptables_generate_rule, :type => :rvalue, :doc => <<-EOS
Provided an array of options, generates iptables rule(s).
EOS
) do |args|
  Puppet::Parser::Functions.function('iptables_parse_options')
  Puppet::Parser::Functions.function('format_action')
  Puppet::Parser::Functions.function('split_ip_by_version')
  Puppet::Parser::Functions.function('format_chain')
  Puppet::Parser::Functions.function('format_interface')
  Puppet::Parser::Functions.function('format_log')
  Puppet::Parser::Functions.function('format_port')
  Puppet::Parser::Functions.function('format_protocol')
  Puppet::Parser::Functions.function('format_reject')
  Puppet::Parser::Functions.function('format_state')

  opt = args[0]

  version = '4'
  version = String(args[1])[-1].chr \
    if String(args[1]) =~ /(?i-mx:(ip)?(v)?(4|6))/

  raise Puppet::Error, "invalid version detected - #{version}" \
    unless version =~ /(4|6)/

  flg = { }
  flg = opt['mod_flags'] if opt['mod_flags'].is_a?(Hash)
  flg.default=false

  # addresses are arrays that should always have at least one object, even if
  # its an empty-string
  dst = function_split_ip_by_version( [ opt['destination'] ] )[version]
  dst.push('') if dst.size == 0
  src = function_split_ip_by_version( [ opt['source'] ] )[version]
  src.push('') if src.size == 0

  # our ports also require a little logic
  dpt_h = function_format_port( [ opt['destination_port'], 'dport' ] )
  dpt = dpt_h['port']
  spt_h = function_format_port( [ opt['source_port'], 'sport' ] )
  spt = spt_h['port']
  flg['multiport'] = true if spt_h['multiport'] or dpt_h['multiport']

  # the rest are pretty easy
  act = function_format_action( [ opt['action'] ] )
  chn = function_format_chain( [ opt['chain'] ] )
  in_int = function_format_interface( [ opt['incoming_interface'], 'in' ] )
  out_int = function_format_interface( [ opt['outgoing_interface'], 'out' ] )
  proto = function_format_protocol( [ opt['protocol'], version, 
                                      opt['strict_protocol_checking'] ] )
  ste = function_format_state( [ opt['state'] ] )
  rej = function_format_reject( [ opt['reject_with'], version ] )

  # logging options are all formatted in one function, so we'll pass in a
  # hash of values.  we'll also only format if the act_LOG flag is set,
  # otherwise these options are useless
  log_opts = {
    'log_ip_opt' => opt['log_ip_opt'],
    'log_level' => opt['log_level'],
    'log_prefix' => opt['log_prefix'],
    'log_tcp_opt' => opt['log_tcp_opt'],
    'log_tcp_sequence' => opt['log_tcp_sequence'],
  }
  log = function_format_log( [ log_opts ] ) if flg['act_LOG']

  # throw some errors when appropriate
  raise Puppet::ParseError,
    "only the FORWARD chain may specify both an in and out interface" \
    + " FWD=#{flg['chn_FORWARD']}, out=#{out_int}, in=#{in_int}" \
    if out_int != '' and in_int != '' and ! flg['chn_FORWARD']

  raise Puppet::Error,
    "something broke. we should have a valid CHAIN by this point" \
    if chn == ''

  raise Puppet::ParseError,
    "protocol required if a source or destination port is provided" \
    if ( spt != '' or dpt != '' ) and proto == ''

  #
  ## begin processing
  #

  # we will store our rules and comments in this array and return it when
  # we are all done
  rules = [ ]

  # lets handle the comments first
  comment_line_width = 80
  comment = opt['comment']
  if comment != nil 
    prepend = "# "
    comment_width = comment_line_width - prepend.length
    comments = []
    if comment.kind_of?(Array)
      comment.each do |c|
        comments += c.scan(/.{1,#{comment_width}}/) if c.kind_of?(String)
      end
    else
      comments = comment.scan(/.{1,#{comment_width}}/)
    end
    comments.map! { |c| c = prepend + c }
    rules += comments
  end

  # allow users to pass rule rule code through, without being
  # tampered with
  raw = opt['raw']

  src.each do |s|
    # we'll store our pieces here, and join() them later

    @src = "-s #{s}" if s != ''
    @src = nil if s == nil or s == ''
    dst.each do |d|
      @dst = "-d #{d}" if d != ''
      @dst = nil if d == nil or d == ''

      rule = [ ]
      rule.push(chn)
      rule.push(in_int)
      rule.push(out_int)
      rule.push(@src)
      rule.push(@dst)
      rule.push(proto)
      rule.push('-m multiport') if flg['multiport']
      rule.push(spt)
      rule.push(dpt)
      rule.push(ste)
      rule.push(raw)
      rule.push(act)
      rule.push(log) if flg['act_LOG']
      rule.push(rej) if flg['act_REJECT']
      rule.compact!
      rule.delete('')
      rules.push(rule.join(' '))
    end
  end
  return rules
end