123456789_123456789_123456789_123456789_123456789_

Class: ACL::ACLEntry

Relationships & Source Files
Inherits: Object
Defined in: lib/drb/acl.rb

Overview

An entry in an ::ACL

Class Method Summary

Instance Method Summary

Constructor Details

.new(str) ⇒ ACLEntry

Creates a new entry using str.

str may be “*” or “all” to match any address, an IP address string to match a specific address, an IP address mask per IPAddr, or one containing “*” to match part of an IPv4 address.

[ GitHub ]

  
# File 'lib/drb/acl.rb', line 52

def initialize(str)
  if str == '*' or str == 'all'
    @pat = [:all]
  elsif str.include?('*')
    @pat = [:name, dot_pat(str)]
  else
    begin
      @pat = [:ip, IPAddr.new(str)]
    rescue ArgumentError
      @pat = [:name, dot_pat(str)]
    end
  end
end

Instance Method Details

#dot_pat(str) (private)

Creates a Regexp to match an address.

[ GitHub ]

  
# File 'lib/drb/acl.rb', line 83

def dot_pat(str)
  exp = "^" + dot_pat_str(str) + "$"
  Regexp.new(exp)
end

#dot_pat_str(str) (private)

Creates a regular expression to match IPv4 addresses

[ GitHub ]

  
# File 'lib/drb/acl.rb', line 71

def dot_pat_str(str)
  list = str.split('.').collect { |s|
    (s == '*') ? '.+' : s
  }
  list.join("\\.")
end

#match(addr)

Matches addr against this entry.

[ GitHub ]

  
# File 'lib/drb/acl.rb', line 93

def match(addr)
  case @pat[0]
  when :all
    true
  when :ip
    begin
      ipaddr = IPAddr.new(addr[3])
      ipaddr = ipaddr.ipv4_mapped if @pat[1].ipv6? && ipaddr.ipv4?
    rescue ArgumentError
      return false
    end
    (@pat[1].include?(ipaddr)) ? true : false
  when :name
    (@pat[1] =~ addr[2]) ? true : false
  else
    false
  end
end