eventmachine 1.2.0.dev.2-x64-mingw32

Sign up to get free protection for your applications and to get access to all the features.
Files changed (181) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +105 -0
  3. data/GNU +281 -0
  4. data/LICENSE +60 -0
  5. data/README.md +108 -0
  6. data/docs/DocumentationGuidesIndex.md +27 -0
  7. data/docs/GettingStarted.md +521 -0
  8. data/docs/old/ChangeLog +211 -0
  9. data/docs/old/DEFERRABLES +246 -0
  10. data/docs/old/EPOLL +141 -0
  11. data/docs/old/INSTALL +13 -0
  12. data/docs/old/KEYBOARD +42 -0
  13. data/docs/old/LEGAL +25 -0
  14. data/docs/old/LIGHTWEIGHT_CONCURRENCY +130 -0
  15. data/docs/old/PURE_RUBY +75 -0
  16. data/docs/old/RELEASE_NOTES +94 -0
  17. data/docs/old/SMTP +4 -0
  18. data/docs/old/SPAWNED_PROCESSES +148 -0
  19. data/docs/old/TODO +8 -0
  20. data/examples/guides/getting_started/01_eventmachine_echo_server.rb +18 -0
  21. data/examples/guides/getting_started/02_eventmachine_echo_server_that_recognizes_exit_command.rb +22 -0
  22. data/examples/guides/getting_started/03_simple_chat_server.rb +149 -0
  23. data/examples/guides/getting_started/04_simple_chat_server_step_one.rb +27 -0
  24. data/examples/guides/getting_started/05_simple_chat_server_step_two.rb +43 -0
  25. data/examples/guides/getting_started/06_simple_chat_server_step_three.rb +98 -0
  26. data/examples/guides/getting_started/07_simple_chat_server_step_four.rb +121 -0
  27. data/examples/guides/getting_started/08_simple_chat_server_step_five.rb +141 -0
  28. data/examples/old/ex_channel.rb +43 -0
  29. data/examples/old/ex_queue.rb +2 -0
  30. data/examples/old/ex_tick_loop_array.rb +15 -0
  31. data/examples/old/ex_tick_loop_counter.rb +32 -0
  32. data/examples/old/helper.rb +2 -0
  33. data/ext/binder.cpp +124 -0
  34. data/ext/binder.h +46 -0
  35. data/ext/cmain.cpp +988 -0
  36. data/ext/ed.cpp +2111 -0
  37. data/ext/ed.h +442 -0
  38. data/ext/em.cpp +2379 -0
  39. data/ext/em.h +308 -0
  40. data/ext/eventmachine.h +143 -0
  41. data/ext/extconf.rb +270 -0
  42. data/ext/fastfilereader/extconf.rb +110 -0
  43. data/ext/fastfilereader/mapper.cpp +216 -0
  44. data/ext/fastfilereader/mapper.h +59 -0
  45. data/ext/fastfilereader/rubymain.cpp +127 -0
  46. data/ext/kb.cpp +79 -0
  47. data/ext/page.cpp +107 -0
  48. data/ext/page.h +51 -0
  49. data/ext/pipe.cpp +354 -0
  50. data/ext/project.h +176 -0
  51. data/ext/rubymain.cpp +1504 -0
  52. data/ext/ssl.cpp +615 -0
  53. data/ext/ssl.h +103 -0
  54. data/java/.classpath +8 -0
  55. data/java/.project +17 -0
  56. data/java/src/com/rubyeventmachine/EmReactor.java +591 -0
  57. data/java/src/com/rubyeventmachine/EmReactorException.java +40 -0
  58. data/java/src/com/rubyeventmachine/EventableChannel.java +72 -0
  59. data/java/src/com/rubyeventmachine/EventableDatagramChannel.java +201 -0
  60. data/java/src/com/rubyeventmachine/EventableSocketChannel.java +415 -0
  61. data/lib/2.0/fastfilereaderext.so +0 -0
  62. data/lib/2.0/rubyeventmachine.so +0 -0
  63. data/lib/2.1/fastfilereaderext.so +0 -0
  64. data/lib/2.1/rubyeventmachine.so +0 -0
  65. data/lib/2.2/fastfilereaderext.so +0 -0
  66. data/lib/2.2/rubyeventmachine.so +0 -0
  67. data/lib/2.3/fastfilereaderext.so +0 -0
  68. data/lib/2.3/rubyeventmachine.so +0 -0
  69. data/lib/em/buftok.rb +59 -0
  70. data/lib/em/callback.rb +58 -0
  71. data/lib/em/channel.rb +69 -0
  72. data/lib/em/completion.rb +304 -0
  73. data/lib/em/connection.rb +770 -0
  74. data/lib/em/deferrable.rb +210 -0
  75. data/lib/em/deferrable/pool.rb +2 -0
  76. data/lib/em/file_watch.rb +73 -0
  77. data/lib/em/future.rb +61 -0
  78. data/lib/em/iterator.rb +252 -0
  79. data/lib/em/messages.rb +66 -0
  80. data/lib/em/pool.rb +151 -0
  81. data/lib/em/process_watch.rb +45 -0
  82. data/lib/em/processes.rb +123 -0
  83. data/lib/em/protocols.rb +37 -0
  84. data/lib/em/protocols/header_and_content.rb +138 -0
  85. data/lib/em/protocols/httpclient.rb +299 -0
  86. data/lib/em/protocols/httpclient2.rb +600 -0
  87. data/lib/em/protocols/line_and_text.rb +125 -0
  88. data/lib/em/protocols/line_protocol.rb +29 -0
  89. data/lib/em/protocols/linetext2.rb +166 -0
  90. data/lib/em/protocols/memcache.rb +331 -0
  91. data/lib/em/protocols/object_protocol.rb +46 -0
  92. data/lib/em/protocols/postgres3.rb +246 -0
  93. data/lib/em/protocols/saslauth.rb +175 -0
  94. data/lib/em/protocols/smtpclient.rb +394 -0
  95. data/lib/em/protocols/smtpserver.rb +666 -0
  96. data/lib/em/protocols/socks4.rb +66 -0
  97. data/lib/em/protocols/stomp.rb +205 -0
  98. data/lib/em/protocols/tcptest.rb +54 -0
  99. data/lib/em/pure_ruby.rb +1022 -0
  100. data/lib/em/queue.rb +80 -0
  101. data/lib/em/resolver.rb +232 -0
  102. data/lib/em/spawnable.rb +84 -0
  103. data/lib/em/streamer.rb +118 -0
  104. data/lib/em/threaded_resource.rb +90 -0
  105. data/lib/em/tick_loop.rb +85 -0
  106. data/lib/em/timers.rb +61 -0
  107. data/lib/em/version.rb +3 -0
  108. data/lib/eventmachine.rb +1584 -0
  109. data/lib/fastfilereaderext.rb +2 -0
  110. data/lib/jeventmachine.rb +301 -0
  111. data/lib/rubyeventmachine.rb +2 -0
  112. data/rakelib/package.rake +120 -0
  113. data/rakelib/test.rake +8 -0
  114. data/tests/client.crt +31 -0
  115. data/tests/client.key +51 -0
  116. data/tests/dhparam.pem +13 -0
  117. data/tests/em_test_helper.rb +151 -0
  118. data/tests/test_attach.rb +151 -0
  119. data/tests/test_basic.rb +283 -0
  120. data/tests/test_channel.rb +75 -0
  121. data/tests/test_completion.rb +178 -0
  122. data/tests/test_connection_count.rb +54 -0
  123. data/tests/test_connection_write.rb +35 -0
  124. data/tests/test_defer.rb +35 -0
  125. data/tests/test_deferrable.rb +35 -0
  126. data/tests/test_epoll.rb +142 -0
  127. data/tests/test_error_handler.rb +38 -0
  128. data/tests/test_exc.rb +28 -0
  129. data/tests/test_file_watch.rb +66 -0
  130. data/tests/test_fork.rb +75 -0
  131. data/tests/test_futures.rb +170 -0
  132. data/tests/test_get_sock_opt.rb +37 -0
  133. data/tests/test_handler_check.rb +35 -0
  134. data/tests/test_hc.rb +155 -0
  135. data/tests/test_httpclient.rb +233 -0
  136. data/tests/test_httpclient2.rb +128 -0
  137. data/tests/test_idle_connection.rb +25 -0
  138. data/tests/test_inactivity_timeout.rb +54 -0
  139. data/tests/test_ipv4.rb +125 -0
  140. data/tests/test_ipv6.rb +131 -0
  141. data/tests/test_iterator.rb +115 -0
  142. data/tests/test_kb.rb +28 -0
  143. data/tests/test_line_protocol.rb +33 -0
  144. data/tests/test_ltp.rb +138 -0
  145. data/tests/test_ltp2.rb +308 -0
  146. data/tests/test_many_fds.rb +22 -0
  147. data/tests/test_next_tick.rb +104 -0
  148. data/tests/test_object_protocol.rb +36 -0
  149. data/tests/test_pause.rb +107 -0
  150. data/tests/test_pending_connect_timeout.rb +52 -0
  151. data/tests/test_pool.rb +196 -0
  152. data/tests/test_process_watch.rb +50 -0
  153. data/tests/test_processes.rb +128 -0
  154. data/tests/test_proxy_connection.rb +180 -0
  155. data/tests/test_pure.rb +88 -0
  156. data/tests/test_queue.rb +64 -0
  157. data/tests/test_resolver.rb +104 -0
  158. data/tests/test_running.rb +14 -0
  159. data/tests/test_sasl.rb +47 -0
  160. data/tests/test_send_file.rb +217 -0
  161. data/tests/test_servers.rb +33 -0
  162. data/tests/test_set_sock_opt.rb +39 -0
  163. data/tests/test_shutdown_hooks.rb +23 -0
  164. data/tests/test_smtpclient.rb +75 -0
  165. data/tests/test_smtpserver.rb +57 -0
  166. data/tests/test_spawn.rb +293 -0
  167. data/tests/test_ssl_args.rb +78 -0
  168. data/tests/test_ssl_dhparam.rb +83 -0
  169. data/tests/test_ssl_ecdh_curve.rb +79 -0
  170. data/tests/test_ssl_extensions.rb +49 -0
  171. data/tests/test_ssl_methods.rb +65 -0
  172. data/tests/test_ssl_protocols.rb +246 -0
  173. data/tests/test_ssl_verify.rb +126 -0
  174. data/tests/test_stomp.rb +37 -0
  175. data/tests/test_system.rb +46 -0
  176. data/tests/test_threaded_resource.rb +61 -0
  177. data/tests/test_tick_loop.rb +59 -0
  178. data/tests/test_timers.rb +123 -0
  179. data/tests/test_ud.rb +8 -0
  180. data/tests/test_unbind_reason.rb +52 -0
  181. metadata +381 -0
@@ -0,0 +1,46 @@
1
+ module EventMachine
2
+ module Protocols
3
+ # ObjectProtocol allows for easy communication using marshaled ruby objects
4
+ #
5
+ # module RubyServer
6
+ # include EM::P::ObjectProtocol
7
+ #
8
+ # def receive_object obj
9
+ # send_object({'you said' => obj})
10
+ # end
11
+ # end
12
+ #
13
+ module ObjectProtocol
14
+ # By default returns Marshal, override to return JSON or YAML, or any
15
+ # other serializer/deserializer responding to #dump and #load.
16
+ def serializer
17
+ Marshal
18
+ end
19
+
20
+ # @private
21
+ def receive_data data
22
+ (@buf ||= '') << data
23
+
24
+ while @buf.size >= 4
25
+ if @buf.size >= 4+(size=@buf.unpack('N').first)
26
+ @buf.slice!(0,4)
27
+ receive_object serializer.load(@buf.slice!(0,size))
28
+ else
29
+ break
30
+ end
31
+ end
32
+ end
33
+
34
+ # Invoked with ruby objects received over the network
35
+ def receive_object obj
36
+ # stub
37
+ end
38
+
39
+ # Sends a ruby object over the network
40
+ def send_object obj
41
+ data = serializer.dump(obj)
42
+ send_data [data.respond_to?(:bytesize) ? data.bytesize : data.size, data].pack('Na*')
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,246 @@
1
+ #--
2
+ #
3
+ # Author:: Francis Cianfrocca (gmail: blackhedd)
4
+ # Homepage:: http://rubyeventmachine.com
5
+ # Date:: 15 November 2006
6
+ #
7
+ # See EventMachine and EventMachine::Connection for documentation and
8
+ # usage examples.
9
+ #
10
+ #----------------------------------------------------------------------------
11
+ #
12
+ # Copyright (C) 2006-08 by Francis Cianfrocca. All Rights Reserved.
13
+ # Gmail: blackhedd
14
+ #
15
+ # This program is free software; you can redistribute it and/or modify
16
+ # it under the terms of either: 1) the GNU General Public License
17
+ # as published by the Free Software Foundation; either version 2 of the
18
+ # License, or (at your option) any later version; or 2) Ruby's License.
19
+ #
20
+ # See the file COPYING for complete licensing information.
21
+ #
22
+ #---------------------------------------------------------------------------
23
+ #
24
+ #
25
+ #
26
+
27
+ require 'postgres-pr/message'
28
+ require 'postgres-pr/connection'
29
+ require 'stringio'
30
+
31
+ # @private
32
+ class StringIO
33
+ # Reads exactly +n+ bytes.
34
+ #
35
+ # If the data read is nil an EOFError is raised.
36
+ #
37
+ # If the data read is too short an IOError is raised
38
+ def readbytes(n)
39
+ str = read(n)
40
+ if str == nil
41
+ raise EOFError, "End of file reached"
42
+ end
43
+ if str.size < n
44
+ raise IOError, "data truncated"
45
+ end
46
+ str
47
+ end
48
+ alias read_exactly_n_bytes readbytes
49
+ end
50
+
51
+
52
+ module EventMachine
53
+ module Protocols
54
+ # PROVISIONAL IMPLEMENTATION of an evented Postgres client.
55
+ # This implements version 3 of the Postgres wire protocol, which will work
56
+ # with any Postgres version from roughly 7.4 onward.
57
+ #
58
+ # Objective: we want to access Postgres databases without requiring threads.
59
+ # Until now this has been a problem because the Postgres client implementations
60
+ # have all made use of blocking I/O calls, which is incompatible with a
61
+ # thread-free evented model.
62
+ #
63
+ # But rather than re-implement the Postgres Wire3 protocol, we're taking advantage
64
+ # of the existing postgres-pr library, which was originally written by Michael
65
+ # Neumann but (at this writing) appears to be no longer maintained. Still, it's
66
+ # in basically a production-ready state, and the wire protocol isn't that complicated
67
+ # anyway.
68
+ #
69
+ # We're tucking in a bunch of require statements that may not be present in garden-variety
70
+ # EM installations. Until we find a good way to only require these if a program
71
+ # requires postgres, this file will need to be required explicitly.
72
+ #
73
+ # We need to monkeypatch StringIO because it lacks the #readbytes method needed
74
+ # by postgres-pr.
75
+ # The StringIO monkeypatch is lifted from the standard library readbytes.rb,
76
+ # which adds method #readbytes directly to class IO. But StringIO is not a subclass of IO.
77
+ # It is modified to raise an IOError instead of TruncatedDataException since the exception is unused.
78
+ #
79
+ # We cloned the handling of postgres messages from lib/postgres-pr/connection.rb
80
+ # in the postgres-pr library, and modified it for event-handling.
81
+ #
82
+ # TODO: The password handling in dispatch_conn_message is totally incomplete.
83
+ #
84
+ #
85
+ # We return Deferrables from the user-level operations surfaced by this interface.
86
+ # Experimentally, we're using the pattern of always returning a boolean value as the
87
+ # first argument of a deferrable callback to indicate success or failure. This is
88
+ # instead of the traditional pattern of calling Deferrable#succeed or #fail, and
89
+ # requiring the user to define both a callback and an errback function.
90
+ #
91
+ # === Usage
92
+ # EM.run {
93
+ # db = EM.connect_unix_domain( "/tmp/.s.PGSQL.5432", EM::P::Postgres3 )
94
+ # db.connect( dbname, username, psw ).callback do |status|
95
+ # if status
96
+ # db.query( "select * from some_table" ).callback do |status, result, errors|
97
+ # if status
98
+ # result.rows.each do |row|
99
+ # p row
100
+ # end
101
+ # end
102
+ # end
103
+ # end
104
+ # end
105
+ # }
106
+ class Postgres3 < EventMachine::Connection
107
+ include PostgresPR
108
+
109
+ def initialize
110
+ @data = ""
111
+ @params = {}
112
+ end
113
+
114
+ def connect db, user, psw=nil
115
+ d = EM::DefaultDeferrable.new
116
+ d.timeout 15
117
+
118
+ if @pending_query || @pending_conn
119
+ d.succeed false, "Operation already in progress"
120
+ else
121
+ @pending_conn = d
122
+ prms = {"user"=>user, "database"=>db}
123
+ @user = user
124
+ if psw
125
+ @password = psw
126
+ #prms["password"] = psw
127
+ end
128
+ send_data PostgresPR::StartupMessage.new( 3 << 16, prms ).dump
129
+ end
130
+
131
+ d
132
+ end
133
+
134
+ def query sql
135
+ d = EM::DefaultDeferrable.new
136
+ d.timeout 15
137
+
138
+ if @pending_query || @pending_conn
139
+ d.succeed false, "Operation already in progress"
140
+ else
141
+ @r = PostgresPR::Connection::Result.new
142
+ @e = []
143
+ @pending_query = d
144
+ send_data PostgresPR::Query.dump(sql)
145
+ end
146
+
147
+ d
148
+ end
149
+
150
+
151
+ def receive_data data
152
+ @data << data
153
+ while @data.length >= 5
154
+ pktlen = @data[1...5].unpack("N").first
155
+ if @data.length >= (1 + pktlen)
156
+ pkt = @data.slice!(0...(1+pktlen))
157
+ m = StringIO.open( pkt, "r" ) {|io| PostgresPR::Message.read( io ) }
158
+ if @pending_conn
159
+ dispatch_conn_message m
160
+ elsif @pending_query
161
+ dispatch_query_message m
162
+ else
163
+ raise "Unexpected message from database"
164
+ end
165
+ else
166
+ break # very important, break out of the while
167
+ end
168
+ end
169
+ end
170
+
171
+
172
+ def unbind
173
+ if o = (@pending_query || @pending_conn)
174
+ o.succeed false, "lost connection"
175
+ end
176
+ end
177
+
178
+ # Cloned and modified from the postgres-pr.
179
+ def dispatch_conn_message msg
180
+ case msg
181
+ when AuthentificationClearTextPassword
182
+ raise ArgumentError, "no password specified" if @password.nil?
183
+ send_data PasswordMessage.new(@password).dump
184
+
185
+ when AuthentificationCryptPassword
186
+ raise ArgumentError, "no password specified" if @password.nil?
187
+ send_data PasswordMessage.new(@password.crypt(msg.salt)).dump
188
+
189
+ when AuthentificationMD5Password
190
+ raise ArgumentError, "no password specified" if @password.nil?
191
+ require 'digest/md5'
192
+
193
+ m = Digest::MD5.hexdigest(@password + @user)
194
+ m = Digest::MD5.hexdigest(m + msg.salt)
195
+ m = 'md5' + m
196
+ send_data PasswordMessage.new(m).dump
197
+
198
+ when AuthentificationKerberosV4, AuthentificationKerberosV5, AuthentificationSCMCredential
199
+ raise "unsupported authentification"
200
+
201
+ when AuthentificationOk
202
+ when ErrorResponse
203
+ raise msg.field_values.join("\t")
204
+ when NoticeResponse
205
+ @notice_processor.call(msg) if @notice_processor
206
+ when ParameterStatus
207
+ @params[msg.key] = msg.value
208
+ when BackendKeyData
209
+ # TODO
210
+ #p msg
211
+ when ReadyForQuery
212
+ # TODO: use transaction status
213
+ pc,@pending_conn = @pending_conn,nil
214
+ pc.succeed true
215
+ else
216
+ raise "unhandled message type"
217
+ end
218
+ end
219
+
220
+ # Cloned and modified from the postgres-pr.
221
+ def dispatch_query_message msg
222
+ case msg
223
+ when DataRow
224
+ @r.rows << msg.columns
225
+ when CommandComplete
226
+ @r.cmd_tag = msg.cmd_tag
227
+ when ReadyForQuery
228
+ pq,@pending_query = @pending_query,nil
229
+ pq.succeed true, @r, @e
230
+ when RowDescription
231
+ @r.fields = msg.fields
232
+ when CopyInResponse
233
+ when CopyOutResponse
234
+ when EmptyQueryResponse
235
+ when ErrorResponse
236
+ # TODO
237
+ @e << msg
238
+ when NoticeResponse
239
+ @notice_processor.call(msg) if @notice_processor
240
+ else
241
+ # TODO
242
+ end
243
+ end
244
+ end
245
+ end
246
+ end
@@ -0,0 +1,175 @@
1
+ #--
2
+ #
3
+ # Author:: Francis Cianfrocca (gmail: blackhedd)
4
+ # Homepage:: http://rubyeventmachine.com
5
+ # Date:: 15 November 2006
6
+ #
7
+ # See EventMachine and EventMachine::Connection for documentation and
8
+ # usage examples.
9
+ #
10
+ #----------------------------------------------------------------------------
11
+ #
12
+ # Copyright (C) 2006-07 by Francis Cianfrocca. All Rights Reserved.
13
+ # Gmail: blackhedd
14
+ #
15
+ # This program is free software; you can redistribute it and/or modify
16
+ # it under the terms of either: 1) the GNU General Public License
17
+ # as published by the Free Software Foundation; either version 2 of the
18
+ # License, or (at your option) any later version; or 2) Ruby's License.
19
+ #
20
+ # See the file COPYING for complete licensing information.
21
+ #
22
+ #---------------------------------------------------------------------------
23
+ #
24
+ #
25
+ #
26
+
27
+ module EventMachine
28
+ module Protocols
29
+
30
+ # Implements SASL authd.
31
+ # This is a very, very simple protocol that mimics the one used
32
+ # by saslauthd and pwcheck, two outboard daemons included in the
33
+ # standard SASL library distro.
34
+ # The only thing this is really suitable for is SASL PLAIN
35
+ # (user+password) authentication, but the SASL libs that are
36
+ # linked into standard servers (like imapd and sendmail) implement
37
+ # the other ones.
38
+ #
39
+ # SASL-auth is intended for reasonably fast operation inside a
40
+ # single machine, so it has no transport-security (although there
41
+ # have been multi-machine extensions incorporating transport-layer
42
+ # encryption).
43
+ #
44
+ # The standard saslauthd module generally runs privileged and does
45
+ # its work by referring to the system-account files.
46
+ #
47
+ # This feature was added to EventMachine to enable the development
48
+ # of custom authentication/authorization engines for standard servers.
49
+ #
50
+ # To use SASLauth, include it in a class that subclasses EM::Connection,
51
+ # and reimplement the validate method.
52
+ #
53
+ # The typical way to incorporate this module into an authentication
54
+ # daemon would be to set it as the handler for a UNIX-domain socket.
55
+ # The code might look like this:
56
+ #
57
+ # EM.start_unix_domain_server( "/var/run/saslauthd/mux", MyHandler )
58
+ # File.chmod( 0777, "/var/run/saslauthd/mux")
59
+ #
60
+ # The chmod is probably needed to ensure that unprivileged clients can
61
+ # access the UNIX-domain socket.
62
+ #
63
+ # It's also a very good idea to drop superuser privileges (if any), after
64
+ # the UNIX-domain socket has been opened.
65
+ #--
66
+ # Implementation details: assume the client can send us pipelined requests,
67
+ # and that the client will close the connection.
68
+ #
69
+ # The client sends us four values, each encoded as a two-byte length field in
70
+ # network order followed by the specified number of octets.
71
+ # The fields specify the username, password, service name (such as imap),
72
+ # and the "realm" name. We send back the barest minimum reply, a single
73
+ # field also encoded as a two-octet length in network order, followed by
74
+ # either "NO" or "OK" - simplicity itself.
75
+ #
76
+ # We enforce a maximum field size just as a sanity check.
77
+ # We do NOT automatically time out the connection.
78
+ #
79
+ # The code we use to parse out the values is ugly and probably slow.
80
+ # Improvements welcome.
81
+ #
82
+ module SASLauth
83
+
84
+ MaxFieldSize = 128*1024
85
+ def post_init
86
+ super
87
+ @sasl_data = ""
88
+ @sasl_values = []
89
+ end
90
+
91
+ def receive_data data
92
+ @sasl_data << data
93
+ while @sasl_data.length >= 2
94
+ len = (@sasl_data[0,2].unpack("n")).first
95
+ raise "SASL Max Field Length exceeded" if len > MaxFieldSize
96
+ if @sasl_data.length >= (len + 2)
97
+ @sasl_values << @sasl_data[2,len]
98
+ @sasl_data.slice!(0...(2+len))
99
+ if @sasl_values.length == 4
100
+ send_data( validate(*@sasl_values) ? "\0\002OK" : "\0\002NO" )
101
+ @sasl_values.clear
102
+ end
103
+ else
104
+ break
105
+ end
106
+ end
107
+ end
108
+
109
+ def validate username, psw, sysname, realm
110
+ p username
111
+ p psw
112
+ p sysname
113
+ p realm
114
+ true
115
+ end
116
+ end
117
+
118
+ # Implements the SASL authd client protocol.
119
+ # This is a very, very simple protocol that mimics the one used
120
+ # by saslauthd and pwcheck, two outboard daemons included in the
121
+ # standard SASL library distro.
122
+ # The only thing this is really suitable for is SASL PLAIN
123
+ # (user+password) authentication, but the SASL libs that are
124
+ # linked into standard servers (like imapd and sendmail) implement
125
+ # the other ones.
126
+ #
127
+ # You can use this module directly as a handler for EM Connections,
128
+ # or include it in a module or handler class of your own.
129
+ #
130
+ # First connect to a SASL server (it's probably a TCP server, or more
131
+ # likely a Unix-domain socket). Then call the #validate? method,
132
+ # passing at least a username and a password. #validate? returns
133
+ # a Deferrable which will either succeed or fail, depending
134
+ # on the status of the authentication operation.
135
+ #
136
+ module SASLauthclient
137
+ MaxFieldSize = 128*1024
138
+
139
+ def validate? username, psw, sysname=nil, realm=nil
140
+
141
+ str = [username, psw, sysname, realm].map {|m|
142
+ [(m || "").length, (m || "")]
143
+ }.flatten.pack( "nA*" * 4 )
144
+ send_data str
145
+
146
+ d = EM::DefaultDeferrable.new
147
+ @queries.unshift d
148
+ d
149
+ end
150
+
151
+ def post_init
152
+ @sasl_data = ""
153
+ @queries = []
154
+ end
155
+
156
+ def receive_data data
157
+ @sasl_data << data
158
+
159
+ while @sasl_data.length > 2
160
+ len = (@sasl_data[0,2].unpack("n")).first
161
+ raise "SASL Max Field Length exceeded" if len > MaxFieldSize
162
+ if @sasl_data.length >= (len + 2)
163
+ val = @sasl_data[2,len]
164
+ @sasl_data.slice!(0...(2+len))
165
+ q = @queries.pop
166
+ (val == "NO") ? q.fail : q.succeed
167
+ else
168
+ break
169
+ end
170
+ end
171
+ end
172
+ end
173
+
174
+ end
175
+ end