ipaccess 0.0.4 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (88) hide show
  1. checksums.yaml +15 -0
  2. checksums.yaml.gz.sig +1 -0
  3. data.tar.gz.sig +0 -0
  4. data/.gemtest +0 -0
  5. data/.rspec +1 -0
  6. data/.yardopts +12 -0
  7. data/ChangeLog +1495 -0
  8. data/{docs/LGPL-LICENSE → LGPL-LICENSE} +0 -0
  9. data/Manifest.txt +76 -0
  10. data/README.md +96 -0
  11. data/Rakefile +65 -42
  12. data/docs/COPYING +41 -45
  13. data/docs/FAQ +12 -0
  14. data/docs/HISTORY +17 -0
  15. data/docs/LEGAL +1 -1
  16. data/docs/LGPL +166 -0
  17. data/docs/TODO +150 -7
  18. data/docs/images/ipaccess.png +0 -0
  19. data/docs/images/ipaccess_ac_for_args.png +0 -0
  20. data/docs/images/ipaccess_ac_for_socket.png +0 -0
  21. data/docs/images/ipaccess_logo.png +0 -0
  22. data/docs/images/ipaccess_relations.png +0 -0
  23. data/docs/images/ipaccess_setup_origin.png +0 -0
  24. data/docs/images/ipaccess_setup_origin_tab.png +0 -0
  25. data/docs/images/ipaccess_view.png +0 -0
  26. data/docs/rdoc.css +22 -0
  27. data/examples/ftp.rb +62 -0
  28. data/examples/http.rb +81 -0
  29. data/examples/imap.rb +37 -0
  30. data/examples/pop.rb +31 -0
  31. data/examples/smtp.rb +26 -0
  32. data/examples/tcp_server.rb +32 -0
  33. data/examples/tcp_socket.rb +7 -3
  34. data/examples/telnet.rb +32 -0
  35. data/examples/text_message.rb +45 -0
  36. data/lib/ipaccess.rb +557 -7
  37. data/lib/ipaccess/arm_sockets.rb +7 -4
  38. data/lib/ipaccess/ghost_doc/ghost_doc.rb +23 -0
  39. data/lib/ipaccess/ghost_doc/ghost_doc_acl.rb +54 -0
  40. data/lib/ipaccess/ghost_doc/ghost_doc_net_ftp.rb +213 -0
  41. data/lib/ipaccess/ghost_doc/ghost_doc_net_http.rb +272 -0
  42. data/lib/ipaccess/ghost_doc/ghost_doc_net_smtp.rb +186 -0
  43. data/lib/ipaccess/ghost_doc/ghost_doc_net_telnet.rb +227 -0
  44. data/lib/ipaccess/ghost_doc/ghost_doc_p_blacklist.rb +36 -0
  45. data/lib/ipaccess/ghost_doc/ghost_doc_p_blacklist_e.rb +7 -0
  46. data/lib/ipaccess/ghost_doc/ghost_doc_p_unblacklist.rb +36 -0
  47. data/lib/ipaccess/ghost_doc/ghost_doc_p_unblacklist_e.rb +7 -0
  48. data/lib/ipaccess/ghost_doc/ghost_doc_p_unwhitelist.rb +36 -0
  49. data/lib/ipaccess/ghost_doc/ghost_doc_p_unwhitelist_e.rb +7 -0
  50. data/lib/ipaccess/ghost_doc/ghost_doc_p_whitelist.rb +36 -0
  51. data/lib/ipaccess/ghost_doc/ghost_doc_p_whitelist_e.rb +7 -0
  52. data/lib/ipaccess/ghost_doc/ghost_doc_patched_usage.rb +64 -0
  53. data/lib/ipaccess/ghost_doc/ghost_doc_sockets.rb +571 -0
  54. data/lib/ipaccess/ip_access_check.rb +508 -0
  55. data/lib/ipaccess/ip_access_errors.rb +186 -40
  56. data/lib/ipaccess/ip_access_list.rb +955 -1122
  57. data/lib/ipaccess/ip_access_set.rb +212 -0
  58. data/lib/ipaccess/net/ftp.rb +39 -0
  59. data/lib/ipaccess/net/http.rb +39 -0
  60. data/lib/ipaccess/net/https.rb +30 -0
  61. data/lib/ipaccess/net/imap.rb +39 -0
  62. data/lib/ipaccess/net/pop.rb +46 -0
  63. data/lib/ipaccess/net/smtp.rb +39 -0
  64. data/lib/ipaccess/net/telnet.rb +38 -0
  65. data/lib/ipaccess/patches/generic.rb +807 -0
  66. data/lib/ipaccess/patches/net_ftp.rb +165 -0
  67. data/lib/ipaccess/patches/net_http.rb +175 -0
  68. data/lib/ipaccess/patches/net_https.rb +29 -0
  69. data/lib/ipaccess/patches/net_imap.rb +117 -0
  70. data/lib/ipaccess/patches/net_pop.rb +171 -0
  71. data/lib/ipaccess/patches/net_smtp.rb +130 -0
  72. data/lib/ipaccess/patches/net_telnet.rb +103 -0
  73. data/lib/ipaccess/{netaddr_patch.rb → patches/netaddr.rb} +20 -11
  74. data/lib/ipaccess/patches/sockets.rb +586 -0
  75. data/lib/ipaccess/socket.rb +52 -1
  76. data/lib/ipaccess/sockets.rb +4 -30
  77. data/spec/ip_access_list_spec.rb +33 -21
  78. data/spec/spec.opts +2 -2
  79. metadata +289 -63
  80. metadata.gz.sig +0 -0
  81. data/docs/DOWNLOAD +0 -17
  82. data/docs/README +0 -95
  83. data/docs/WELCOME +0 -8
  84. data/lib/ipaccess/ghost_doc.rb +0 -206
  85. data/lib/ipaccess/ghost_doc_acl.rb +0 -31
  86. data/lib/ipaccess/ip_access.rb +0 -456
  87. data/lib/ipaccess/ip_access_patches.rb +0 -431
  88. data/spec/core_spec.rb +0 -5
@@ -0,0 +1,171 @@
1
+ # encoding: utf-8
2
+ #
3
+ # Author:: Paweł Wilk (mailto:pw@gnu.org)
4
+ # Copyright:: Copyright (c) 2009-2014 by Paweł Wilk
5
+ # License:: This program is licensed under the terms of {GNU Lesser General Public License}[link:docs/LGPL.html] or {Ruby License}[link:docs/COPYING.html].
6
+ #
7
+ # Modules contained in this file are meant for
8
+ # patching Ruby's Net::POP3 class in order to add
9
+ # IP access control to it. It is also used
10
+ # to create variant of Net::POP3 class
11
+ # with IP access control.
12
+ #
13
+ #--
14
+ #
15
+ # Copyright (C) 2009 by Paweł Wilk. All Rights Reserved.
16
+ #
17
+ # This program is free software; you can redistribute it and/or modify
18
+ # it under the terms of either: 1) the GNU Lesser General Public License
19
+ # as published by the Free Software Foundation; either version 3 of the
20
+ # License, or (at your option) any later version; or 2) Ruby's License.
21
+ #
22
+ # See the file COPYING for complete licensing information.
23
+ #
24
+ #++
25
+ #
26
+
27
+ require 'socket'
28
+ require 'net/pop'
29
+ require 'ipaccess/ip_access_errors'
30
+ require 'ipaccess/patches/generic'
31
+ require 'ipaccess/patches/sockets'
32
+
33
+ # :stopdoc:
34
+
35
+ module IPAccess::Patches::Net
36
+
37
+ ###################################################################
38
+ # Net::POP3 class with IP access control.
39
+ # It uses output access lists.
40
+
41
+ module POP3
42
+
43
+ include IPAccess::Patches::ACL
44
+
45
+ def self.included(base)
46
+
47
+ marker = (base.name =~ /IPAccess/) ? base.superclass : base
48
+ return if marker.instance_variable_defined?(:@uses_ipaccess)
49
+ base.instance_variable_set(:@uses_ipaccess, true)
50
+
51
+ base.class_eval do
52
+
53
+ # CLASS METHODS
54
+ unless (base.name.nil? && base.class.name == "Class")
55
+ (class << self; self; end).class_eval do
56
+
57
+ # overwrite POP3.start()
58
+ define_method :__ipacall__start do |block, address, *args|
59
+ late_on_deny = nil
60
+ args.delete_if { |x| late_on_deny = x if (x.is_a?(Symbol) && x == :opened_on_deny) }
61
+ args.pop if args.last.nil?
62
+ late_acl = IPAccess.valid_acl?(args.last) ? args.pop : :global
63
+ port, account, password, isapop = *args
64
+ isapop = false if isapop.nil?
65
+ obj = new(address, port, isapop, late_acl, late_on_deny)
66
+ obj.start(account, password, &block)
67
+ end
68
+
69
+ # block passing wrapper for Ruby 1.8
70
+ def start(address, *args, &block)
71
+ __ipacall__start(block, address, *args)
72
+ end
73
+
74
+ # overwrite POP3.delete_all()
75
+ define_method :__ipacall__delete_all do |block, address, *args|
76
+ start(address, *args) { |pop|
77
+ pop.delete_all(&block)
78
+ }
79
+ end
80
+
81
+ # block passing wrapper for Ruby 1.8
82
+ def delete_all(address, *args, &block)
83
+ __ipacall__delete_all(block, address, *args)
84
+ end
85
+
86
+ # overwrite POP3.auth_only()
87
+ define_method :auth_only do |address, *args|
88
+ port, account, password, isapop, late_acl = *args
89
+ new(address, port, isapop, late_acl).auth_only account, password
90
+ end
91
+
92
+ # overwrite POP3.foreach()
93
+ define_method :__ipacall__foreach do |block, address, *args|
94
+ start(address, *args) { |pop|
95
+ pop.each_mail(&block)
96
+ }
97
+ end
98
+
99
+ # block passing wrapper for Ruby 1.8
100
+ def foreach(address, *args, &block)
101
+ __ipacall__foreach(block, address, *args)
102
+ end
103
+
104
+ end
105
+
106
+ end # class methods
107
+
108
+ orig_initialize = self.instance_method :initialize
109
+ orig_do_start = self.instance_method :do_start
110
+ orig_on_connect = self.instance_method :on_connect
111
+
112
+ # initialize on steroids.
113
+ define_method :initialize do |addr, *args|
114
+ @opened_on_deny = false
115
+ args.delete_if { |x| @opened_on_deny = true if (x.is_a?(Symbol) && x == :opened_on_deny) }
116
+ args.pop if args.last.nil?
117
+ self.acl = IPAccess.valid_acl?(args.last) ? args.pop : :global
118
+ obj = orig_initialize.bind(self).call(addr, *args)
119
+ self.acl_recheck
120
+ return obj
121
+ end
122
+
123
+ # start on steroids.
124
+ define_method :do_start do |account, password|
125
+ prev_addr = @address
126
+ ipaddr = ::TCPSocket.getaddress(@address)
127
+ real_acl.output.check_ipstring(ipaddr, self)
128
+ @address = ipaddr
129
+ ret = orig_do_start.bind(self).call(account, password)
130
+ @address = prev_addr
131
+ self.acl_recheck
132
+ return ret
133
+ end
134
+
135
+ # on_connect on steroids.
136
+ define_method :on_connect do
137
+ acl_recheck
138
+ orig_on_connect.bind(self).call
139
+ end
140
+
141
+ # This method returns default access list indicator
142
+ # used by protected object; in this case it's +:output+.
143
+ define_method :default_list do
144
+ :output
145
+ end
146
+
147
+ # this hook will be called each time @acl is reassigned
148
+ define_method :acl_recheck do
149
+ try_arm_and_check_socket @socket
150
+ nil
151
+ end
152
+
153
+ # this hook terminates connection
154
+ define_method :terminate do
155
+ self.finish if started?
156
+ nil
157
+ end
158
+
159
+ end # base.class_eval
160
+
161
+ end # self.included
162
+
163
+ end # module POP3
164
+
165
+ # patching APOP works the same way as patching POP3
166
+ APOP = :POP3
167
+
168
+ end # module IPAccess::Patches
169
+
170
+ # :startdoc:
171
+
@@ -0,0 +1,130 @@
1
+ # encoding: utf-8
2
+ #
3
+ # Author:: Paweł Wilk (mailto:pw@gnu.org)
4
+ # Copyright:: Copyright (c) 2009-2014 by Paweł Wilk
5
+ # License:: This program is licensed under the terms of {GNU Lesser General Public License}[link:docs/LGPL.html] or {Ruby License}[link:docs/COPYING.html].
6
+ #
7
+ # Modules contained in this file are meant for
8
+ # patching Ruby's Net::SMTP class in order to add
9
+ # IP access control to it. It is also used
10
+ # to create variant of Net::SMTP class
11
+ # with IP access control.
12
+ #
13
+ #--
14
+ #
15
+ # Copyright (C) 2009 by Paweł Wilk. All Rights Reserved.
16
+ #
17
+ # This program is free software; you can redistribute it and/or modify
18
+ # it under the terms of either: 1) the GNU Lesser General Public License
19
+ # as published by the Free Software Foundation; either version 3 of the
20
+ # License, or (at your option) any later version; or 2) Ruby's License.
21
+ #
22
+ # See the file COPYING for complete licensing information.
23
+ #
24
+ #++
25
+ #
26
+
27
+ require 'socket'
28
+ require 'net/smtp'
29
+ require 'ipaccess/ip_access_errors'
30
+ require 'ipaccess/patches/generic'
31
+ require 'ipaccess/patches/sockets'
32
+
33
+ # :stopdoc:
34
+
35
+ module IPAccess::Patches::Net
36
+
37
+ ###################################################################
38
+ # Net::SMTP class with IP access control.
39
+ # It uses output access lists.
40
+
41
+ module SMTP
42
+
43
+ include IPAccess::Patches::ACL
44
+
45
+ def self.included(base)
46
+
47
+ marker = (base.name =~ /IPAccess/) ? base.superclass : base
48
+ return if marker.instance_variable_defined?(:@uses_ipaccess)
49
+ base.instance_variable_set(:@uses_ipaccess, true)
50
+
51
+ base.class_eval do
52
+
53
+ # CLASS METHODS
54
+ unless (base.name.nil? && base.class.name == "Class")
55
+ (class << self; self; end).class_eval do
56
+
57
+ # overwrite SMTP.start()
58
+ define_method :__ipacall__start do |block, address, *args|
59
+ late_on_deny = nil
60
+ args.delete_if { |x| late_on_deny = x if (x.is_a?(Symbol) && x == :opened_on_deny) }
61
+ args.pop if args.last.nil?
62
+ late_acl = IPAccess.valid_acl?(args.last) ? args.smtp : :global
63
+ port, helo, user, secret, authtype = *args
64
+ obj = new(address, port, late_acl, late_on_deny)
65
+ obj.start(helo, user, secret, authtype, &block)
66
+ end
67
+
68
+ # block passing wrapper for Ruby 1.8
69
+ def start(address, *args, &block)
70
+ __ipacall__start(block, address, *args)
71
+ end
72
+
73
+ end
74
+
75
+ end # class methods
76
+
77
+ orig_initialize = self.instance_method :initialize
78
+ orig_do_start = self.instance_method :do_start
79
+
80
+ # initialize on steroids.
81
+ define_method :initialize do |addr, *args|
82
+ @opened_on_deny = false
83
+ args.delete_if { |x| @opened_on_deny = true if (x.is_a?(Symbol) && x == :opened_on_deny) }
84
+ args.pop if args.last.nil?
85
+ self.acl = IPAccess.valid_acl?(args.last) ? args.pop : :global
86
+ obj = orig_initialize.bind(self).call(addr, *args)
87
+ self.acl_recheck
88
+ return obj
89
+ end
90
+
91
+ # start on steroids.
92
+ define_method :do_start do |helo_domain, user, secret, authtype|
93
+ prev_addr = @address
94
+ ipaddr = ::TCPSocket.getaddress(@address)
95
+ real_acl.output.check_ipstring(ipaddr, self)
96
+ @address = ipaddr
97
+ ret = orig_do_start.bind(self).call(helo_domain, user, secret, authtype)
98
+ @address = prev_addr
99
+ self.acl_recheck
100
+ return ret
101
+ end
102
+ private :do_start
103
+
104
+ # This method returns default access list indicator
105
+ # used by protected object; in this case it's +:output+.
106
+ define_method :default_list do
107
+ :output
108
+ end
109
+
110
+ # this hook will be called each time @acl is reassigned
111
+ define_method :acl_recheck do
112
+ try_arm_and_check_socket @socket
113
+ nil
114
+ end
115
+
116
+ # this hook terminates connection
117
+ define_method :terminate do
118
+ self.finish if started?
119
+ end
120
+
121
+ end # base.class_eval
122
+
123
+ end # self.included
124
+
125
+ end # module SMTP
126
+
127
+ end # module IPAccess::Patches
128
+
129
+ # :startdoc:
130
+
@@ -0,0 +1,103 @@
1
+ # encoding: utf-8
2
+ #
3
+ # Author:: Paweł Wilk (mailto:pw@gnu.org)
4
+ # Copyright:: Copyright (c) 2009-2014 by Paweł Wilk
5
+ # License:: This program is licensed under the terms of {GNU Lesser General Public License}[link:docs/LGPL.html] or {Ruby License}[link:docs/COPYING.html].
6
+ #
7
+ # Modules contained in this file are meant for
8
+ # patching Ruby's Net::Telnet class in order to add
9
+ # IP access control to it. It is also used
10
+ # to create variant of Net::Telnet class
11
+ # with IP access control.
12
+ #
13
+ #--
14
+ #
15
+ # Copyright (C) 2009 by Paweł Wilk. All Rights Reserved.
16
+ #
17
+ # This program is free software; you can redistribute it and/or modify
18
+ # it under the terms of either: 1) the GNU Lesser General Public License
19
+ # as published by the Free Software Foundation; either version 3 of the
20
+ # License, or (at your option) any later version; or 2) Ruby's License.
21
+ #
22
+ # See the file COPYING for complete licensing information.
23
+ #
24
+ #++
25
+ #
26
+
27
+ require 'socket'
28
+ require 'net/telnet'
29
+ require 'ipaccess/ip_access_errors'
30
+ require 'ipaccess/patches/generic'
31
+ require 'ipaccess/patches/sockets'
32
+
33
+ # :stopdoc:
34
+
35
+ module IPAccess::Patches::Net
36
+
37
+ ###################################################################
38
+ # Net::Telnet class with IP access control.
39
+ # It uses output access lists.
40
+
41
+ module Telnet
42
+
43
+ include IPAccess::Patches::ACL
44
+
45
+ def self.included(base)
46
+
47
+ marker = (base.name =~ /IPAccess/) ? base.superclass : base
48
+ return if marker.instance_variable_defined?(:@uses_ipaccess)
49
+ base.instance_variable_set(:@uses_ipaccess, true)
50
+
51
+ base.class_eval do
52
+
53
+ orig_initialize = self.instance_method :initialize
54
+
55
+ # initialize on steroids.
56
+ define_method :__ipacall__initialize do |block, *args|
57
+ @opened_on_deny = false
58
+ args.delete_if { |x| @opened_on_deny = true if (x.is_a?(Symbol) && x == :opened_on_deny) }
59
+ args.pop if args.last.nil?
60
+ options = args.first
61
+ options["ACL"] = args.pop if (IPAccess.valid_acl?(args.last) && options.is_a?(Hash))
62
+ options["Host"] = "localhost" unless options.has_key?("Host")
63
+ self.acl = IPAccess.valid_acl?(options["ACL"]) ? options["ACL"] : :global
64
+ options["Host"] = ::TCPSocket.getaddress(options["Host"])
65
+ self.acl.output.check_ipstring(options["Host"], :none)
66
+ args[0] = options
67
+ ret = orig_initialize.bind(self).call(*args, &block)
68
+ self.acl_recheck
69
+ return ret
70
+ end
71
+
72
+ # This method returns default access list indicator
73
+ # used by protected object; in this case it's +:output+.
74
+ define_method :default_list do
75
+ :output
76
+ end
77
+
78
+ # this hook terminates connection
79
+ define_method :terminate do
80
+ self.close unless self.closed?
81
+ end
82
+
83
+ # this hook will be called each time @acl is reassigned
84
+ define_method :acl_recheck do
85
+ try_arm_and_check_socket @sock
86
+ nil
87
+ end
88
+
89
+ # block passing wrapper for Ruby 1.8
90
+ def initialize(*args, &block)
91
+ __ipacall__initialize(block, *args)
92
+ end
93
+
94
+ end # base.class_eval
95
+
96
+ end # self.included
97
+
98
+ end # module Telnet
99
+
100
+ end # module IPAccess::Patches
101
+
102
+ # :startdoc:
103
+
@@ -1,8 +1,8 @@
1
1
  # encoding: utf-8
2
2
  #
3
3
  # Author:: Paweł Wilk (mailto:pw@gnu.org)
4
- # Copyright:: Copyright (c) 2009 Paweł Wilk
5
- # License:: This program is licensed under the terms of {GNU Lesser General Public License}[link:docs/LGPL-LICENSE.html] or {Ruby License}[link:docs/COPYING.html].
4
+ # Copyright:: Copyright (c) 2009-2014 by Paweł Wilk
5
+ # License:: This program is licensed under the terms of {GNU Lesser General Public License}[link:docs/LGPL.html] or {Ruby License}[link:docs/COPYING.html].
6
6
  #
7
7
  # This file extends NetAddr by adding methods
8
8
  # that bring some comfort into IPv6 handling.
@@ -22,22 +22,25 @@
22
22
 
23
23
  require 'netaddr'
24
24
 
25
- # This module contains few new methods extending
26
- # original NetAddr module.
25
+ # :stopdoc:
26
+
27
+ # This module contains a few new methods extending
28
+ # original NetAddr[http://netaddr.rubyforge.org/classes/NetAddr.html] module.
27
29
 
28
30
  module NetAddr
29
31
 
30
- # :stopdoc:
31
-
32
+ # This class contains methods extending
33
+ # original CIDR[http://netaddr.rubyforge.org/classes/NetAddr/CIDR.html] class.
34
+
32
35
  class CIDR
33
36
 
34
- # Returns +true+ if the IP address is an IPv4-mapped IPv6 address.
37
+ # Returns +true+ if an IP address is IPv4-mapped IPv6 address.
35
38
 
36
39
  def ipv4_mapped?
37
40
  return @version == 6 && (@ip >> 32) == 0xffff
38
41
  end
39
42
 
40
- # Returns +true+ if the IP address is an IPv4-compatible IPv6 address.
43
+ # Returns +true+ if an IP address is IPv4-compatible IPv6 address.
41
44
 
42
45
  def ipv4_compat?
43
46
  return false if @version != 6
@@ -46,7 +49,7 @@ module NetAddr
46
49
  return (a != 0 && a != 1)
47
50
  end
48
51
 
49
- # Returns +true+ if the IP address is an IPv4-compatible or
52
+ # Returns +true+ if an IP address is IPv4-compatible or
50
53
  # IPv4-mapped IPv6 address.
51
54
 
52
55
  def ipv4_compliant?
@@ -74,6 +77,9 @@ module NetAddr
74
77
 
75
78
  end # class CIDR
76
79
 
80
+ # This class contains methods extending
81
+ # original CIDRv4[http://netaddr.rubyforge.org/classes/NetAddr/CIDRv4.html] class.
82
+
77
83
  class CIDRv4
78
84
 
79
85
  # Returns a new NetAddr::CIDRv6 object built by converting
@@ -101,6 +107,9 @@ module NetAddr
101
107
 
102
108
  end # class CIDRv4
103
109
 
110
+ # This class contains methods extending
111
+ # original CIDRv6[http://netaddr.rubyforge.org/classes/NetAddr/CIDRv6.html] class.
112
+
104
113
  class CIDRv6
105
114
 
106
115
  def ipv4
@@ -121,7 +130,7 @@ module NetAddr
121
130
 
122
131
  end # class CIDRv4
123
132
 
124
- # :startdoc:
125
-
126
133
  end # module NetAddr
127
134
 
135
+ # :startdoc:
136
+