Class: IPTables::Chain

Inherits:
Object
  • Object
show all
Defined in:
lib/iptables/tables.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, chain_info_hash, my_table) ⇒ Chain

Returns a new instance of Chain.



425
426
427
428
429
430
431
432
433
434
435
436
# File 'lib/iptables/tables.rb', line 425

def initialize(name, chain_info_hash, my_table)
  @name = name
  @chain_info_hash = chain_info_hash
  @my_table = my_table

  $log.debug("init Chain #{@name}")
  @node_addition_points = []

  @policy = @chain_info_hash['policy']
  @rules = self.find_and_add_type('rules')
  @additions = self.find_and_add_type('additions')
end

Instance Attribute Details

#additionsObject (readonly)

example chain names in filter table: INPUT, FORWARD, OUTPUT



423
424
425
# File 'lib/iptables/tables.rb', line 423

def additions
  @additions
end

#my_tableObject (readonly)

example chain names in filter table: INPUT, FORWARD, OUTPUT



423
424
425
# File 'lib/iptables/tables.rb', line 423

def my_table
  @my_table
end

#nameObject (readonly)

example chain names in filter table: INPUT, FORWARD, OUTPUT



423
424
425
# File 'lib/iptables/tables.rb', line 423

def name
  @name
end

#node_addition_pointsObject (readonly)

example chain names in filter table: INPUT, FORWARD, OUTPUT



423
424
425
# File 'lib/iptables/tables.rb', line 423

def node_addition_points
  @node_addition_points
end

#policyObject (readonly)

example chain names in filter table: INPUT, FORWARD, OUTPUT



423
424
425
# File 'lib/iptables/tables.rb', line 423

def policy
  @policy
end

#rulesObject (readonly)

example chain names in filter table: INPUT, FORWARD, OUTPUT



423
424
425
# File 'lib/iptables/tables.rb', line 423

def rules
  @rules
end

Instance Method Details

#all_as_array(comments = true) ⇒ Object



461
462
463
464
465
466
# File 'lib/iptables/tables.rb', line 461

def all_as_array(comments = true)
  return [
    ":#{@name} #{self.output_policy}",
    self.as_array
  ].flatten
end

#apply_additions(other_firewall) ⇒ Object



490
491
492
493
494
495
# File 'lib/iptables/tables.rb', line 490

def apply_additions(other_firewall)
  @node_addition_points.each{ |rule_object|
    $log.debug("applying additions for #{rule_object.path}")
    rule_object.apply_additions(other_firewall)
  }
end

#as_array(comments = true) ⇒ Object



453
454
455
456
457
458
459
# File 'lib/iptables/tables.rb', line 453

def as_array(comments = true)
  $log.debug("Chain #{@name} array")
  return [] if @rules == nil
  rules = @rules.collect{ |rule| rule.as_array(comments)}.flatten
  $log.debug(rules)
  return rules
end

#complete?Boolean

Returns:

  • (Boolean)


505
506
507
508
509
510
511
# File 'lib/iptables/tables.rb', line 505

def complete?
  if @rules.nil?
    return true if @additions.nil?
    return false
  end
  return true if @rules.any?
end

#find_and_add_type(data_type) ⇒ Object



438
439
440
441
442
443
444
445
446
447
# File 'lib/iptables/tables.rb', line 438

def find_and_add_type(data_type)
  rules = []
  return unless @chain_info_hash.has_key? data_type
  @chain_info_hash[data_type].each_with_index{ |rule, index|
    rule_object = IPTables::Rule.new(rule, self)
    rule_object.set_position(index)
    rules.push(rule_object)
  }
  return rules
end

#get_node_additionsObject



485
486
487
488
# File 'lib/iptables/tables.rb', line 485

def get_node_additions()
  return if @additions.empty?
  return @additions
end

#merge(chain_object) ⇒ Object



468
469
470
471
472
473
474
# File 'lib/iptables/tables.rb', line 468

def merge(chain_object)
  # if found, replace policy
  @policy = chain_object.policy unless chain_object.policy.nil?

  # if found, replace rules
  @rules = chain_object.rules unless chain_object.rules.nil?
end

#output_policyObject



449
450
451
# File 'lib/iptables/tables.rb', line 449

def output_policy()
  return (@policy == nil) ? '-' : @policy
end

#parse_rule(args) ⇒ Object



497
498
499
500
501
502
503
# File 'lib/iptables/tables.rb', line 497

def parse_rule(args)
  @rules = [] if @rules.nil?
  # parsed rules come with trailing whitespace; remove
  rule_object = IPTables::Rule.new(args.strip, self)
  rule_object.set_position(@rules.length)
  @rules.push(rule_object)
end

#pathObject



476
477
478
# File 'lib/iptables/tables.rb', line 476

def path()
  @my_table.path + ".#{@name}"
end

#register_node_addition_point(rule_object, addition_name) ⇒ Object



480
481
482
483
# File 'lib/iptables/tables.rb', line 480

def register_node_addition_point(rule_object, addition_name)
  @node_addition_points.push(rule_object) unless @node_addition_points.include? rule_object
  @my_table.register_node_addition_point(addition_name)
end