# frozen_string_literal: false
# Copyright (c) 2000,2002,2003 Masatoshi SEKI
# acl.rb is copyrighted free software by Masatoshi SEKI.
# You can redistribute it and/or modify it under the same terms as Ruby.
# Simple Access Control Lists.
# Access control lists are composed of "allow" and "deny" halves to control
# access. Use "all" or "*" to match any address. To match a specific address
# use any address or address mask that IPAddr can understand.
# allow ::ffff:192.168.1.2
# # From Socket#peeraddr, see also ACL#allow_socket?
# addr = ["AF_INET", 10, "lc630", "192.168.1.3"]
# p acl.allow_addr?(addr) # => true
# acl = ACL.new(list, ACL::DENY_ALLOW)
# p acl.allow_addr?(addr) # => true
# The current version of ACL
# 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.
# IPAddr::InvalidPrefixError may be raised when an IP network
# address with an invalid netmask/prefix is given.
if str == '*' or str == 'all'
@pat = [:name, dot_pat(str)]
@pat = [:ip, IPAddr.new(str)]
rescue IPAddr::InvalidPrefixError
# In this case, `str` shouldn't be a host name pattern
# because it contains a slash.
@pat = [:name, dot_pat(str)]
# Creates a regular expression to match IPv4 addresses
list = str.split('.').collect { |s|
# Creates a Regexp to match an address.
/\A#{dot_pat_str(str)}\z/
# Matches +addr+ against this entry.
ipaddr = IPAddr.new(addr[3])
ipaddr = ipaddr.ipv4_mapped if @pat[1].ipv6? && ipaddr.ipv4?
(@pat[1].include?(ipaddr)) ? true : false
(@pat[1] =~ addr[2]) ? true : false
# A list of ACLEntry objects. Used to implement the allow and deny halves
# Creates an empty ACLList
# Matches +addr+ against each ACLEntry in this list.
return true if e.match(addr)
# Adds +str+ as an ACLEntry in this list
@list.push(ACLEntry.new(str))
# Creates a new ACL from +list+ with an evaluation +order+ of DENY_ALLOW or
# An ACL +list+ is an Array of "allow" or "deny" and an address or address
# mask or "all" or "*" to match any address:
def initialize(list=nil, order = DENY_ALLOW)
install_list(list) if list
# Allow connections from Socket +soc+?
allow_addr?(soc.peeraddr)
# Allow connections from addrinfo +addr+? It must be formatted like
# ["AF_INET", 10, "lc630", "192.0.2.1"]
return true if @allow.match(addr)
return false if @deny.match(addr)
return false if @deny.match(addr)
return true if @allow.match(addr)
# Adds +list+ of ACL entries to this ACL.
permission, domain = list.slice(i,2)
raise "Invalid ACL entry #{list}"