PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/parser/large_examples/acl.rb

https://github.com/mfurr/Diamondback-Ruby
Ruby | 144 lines | 119 code | 18 blank | 7 comment | 15 complexity | 18e465e8504533514ec56a493e8426ba MD5 | raw file
Possible License(s): BSD-3-Clause
  1. # acl-2.0 - simple Access Control List
  2. #
  3. # Copyright (c) 2000,2002,2003 Masatoshi SEKI
  4. #
  5. # acl.rb is copyrighted free software by Masatoshi SEKI.
  6. # You can redistribute it and/or modify it under the same terms as Ruby.
  7. require 'ipaddr'
  8. class ACL
  9. VERSION=["2.0.0"]
  10. class ACLEntry
  11. def initialize(str)
  12. if str == '*' or str == 'all'
  13. @pat = [:all]
  14. else
  15. begin
  16. @pat = [:ip, IPAddr.new(str)]
  17. rescue ArgumentError
  18. @pat = [:name, dot_pat(str)]
  19. end
  20. end
  21. end
  22. private
  23. def dot_pat_str(str)
  24. list = str.split('.').collect { |s|
  25. (s == '*') ? '.+' : s
  26. }
  27. list.join("\\.")
  28. end
  29. private
  30. def dot_pat(str)
  31. exp = "^" + dot_pat_str(str) + "$"
  32. Regexp.new(exp)
  33. end
  34. public
  35. def match(addr)
  36. case @pat[0]
  37. when :all
  38. true
  39. when :ip
  40. begin
  41. ipaddr = IPAddr.new(addr[3])
  42. ipaddr = ipaddr.ipv4_mapped if @pat[1].ipv6? && ipaddr.ipv4?
  43. rescue ArgumentError
  44. return false
  45. end
  46. (@pat[1].include?(ipaddr)) ? true : false
  47. when :name
  48. (@pat[1] =~ addr[2]) ? true : false
  49. else
  50. false
  51. end
  52. end
  53. end
  54. class ACLList
  55. def initialize
  56. @list = []
  57. end
  58. public
  59. def match(addr)
  60. @list.each do |e|
  61. return true if e.match(addr)
  62. end
  63. false
  64. end
  65. public
  66. def add(str)
  67. @list.push(ACLEntry.new(str))
  68. end
  69. end
  70. DENY_ALLOW = 0
  71. ALLOW_DENY = 1
  72. def initialize(list=nil, order = DENY_ALLOW)
  73. @order = order
  74. @deny = ACLList.new
  75. @allow = ACLList.new
  76. install_list(list) if list
  77. end
  78. public
  79. def allow_socket?(soc)
  80. allow_addr?(soc.peeraddr)
  81. end
  82. public
  83. def allow_addr?(addr)
  84. case @order
  85. when DENY_ALLOW
  86. return true if @allow.match(addr)
  87. return false if @deny.match(addr)
  88. return true
  89. when ALLOW_DENY
  90. return false if @deny.match(addr)
  91. return true if @allow.match(addr)
  92. return false
  93. else
  94. false
  95. end
  96. end
  97. public
  98. def install_list(list)
  99. i = 0
  100. while i < list.size
  101. permission, domain = list.slice(i,2)
  102. case permission.downcase
  103. when 'allow'
  104. @allow.add(domain)
  105. when 'deny'
  106. @deny.add(domain)
  107. else
  108. raise "Invalid ACL entry #{list.to_s}"
  109. end
  110. i += 2
  111. end
  112. end
  113. end
  114. if __FILE__ == $0
  115. # example
  116. list = %w(deny all
  117. allow 192.168.1.1
  118. allow ::ffff:192.168.1.2
  119. allow 192.168.1.3
  120. )
  121. addr = ["AF_INET", 10, "lc630", "192.168.1.3"]
  122. acl = ACL.new
  123. p acl.allow_addr?(addr)
  124. acl = ACL.new(list, ACL::DENY_ALLOW)
  125. p acl.allow_addr?(addr)
  126. end