Skip to content

Commit 91bba0d

Browse files
committed
* messages SSLRequest and PasswordMessage added
* fixed bug (read until ReadyForQuery even when a ErrorNotification is sent) * authentification implemented
1 parent 762a57a commit 91bba0d

File tree

3 files changed

+83
-3
lines changed

3 files changed

+83
-3
lines changed

TODO

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
* Message types
2+
3+
Bind, BindComplete, CancelRequest, Close, CloseComplete, CommandComplete,
4+
CopyData, CopyDone, CopyFail, CopyInResponse, CopyOutResponse, Describe,
5+
Execute, Flush, FunctionCall, FunctionCallResponse, NoData,
6+
NotificationResponse, ParameterDescription, PortalSuspended
7+
8+
* SSLRequest instead of StartupMessage

lib/postgres-pr/connection.rb

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class Connection
1414

1515
# sync
1616

17-
def initialize(database, user, auth=nil, uri = nil)
17+
def initialize(database, user, password=nil, uri = nil)
1818
uri ||= "unix:/tmp/.s.PGSQL.5432"
1919

2020
raise unless @mutex.nil?
@@ -29,10 +29,31 @@ def initialize(database, user, auth=nil, uri = nil)
2929

3030
loop do
3131
msg = Message.read(@conn)
32+
3233
case msg
34+
when AuthentificationClearTextPassword
35+
raise ArgumentError, "no password specified" if password.nil?
36+
@conn << PasswordMessage.new(password).dump
37+
38+
when AuthentificationCryptPassword
39+
raise ArgumentError, "no password specified" if password.nil?
40+
@conn << PasswordMessage.new(password.crypt(msg.salt)).dump
41+
42+
when AuthentificationMD5Password
43+
raise ArgumentError, "no password specified" if password.nil?
44+
require 'digest/md5'
45+
46+
m = Digest::MD5.hexdigest(password + user)
47+
m = Digest::MD5.hexdigest(m + msg.salt)
48+
m = 'md5' + m
49+
@conn << PasswordMessage.new(m).dump
50+
51+
when AuthentificationKerberosV4, AuthentificationKerberosV5, AuthentificationSCMCredential
52+
raise "unsupported authentification"
53+
3354
when AuthentificationOk
3455
when ErrorResponse
35-
raise
56+
raise "authentification failed"
3657
when NoticeResponse
3758
# TODO
3859
when ParameterStatus
@@ -62,6 +83,7 @@ def query(sql)
6283
@conn << Query.dump(sql)
6384

6485
result = Result.new
86+
errors = []
6587

6688
loop do
6789
msg = Message.read(@conn)
@@ -78,13 +100,16 @@ def query(sql)
78100
when EmptyQueryResponse
79101
when ErrorResponse
80102
# TODO
81-
raise msg.inspect
103+
errors << msg
82104
when NoticeResponse
83105
p msg
84106
else
85107
# TODO
86108
end
87109
end
110+
111+
raise errors.map{|e| e.inspect}.join(" ") unless errors.empty?
112+
88113
result
89114
}
90115
end

lib/postgres-pr/message.rb

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,23 @@ class AuthentificationSCMCredential < Authentification
183183
register_auth_type 6
184184
end
185185

186+
class PasswordMessage < Message
187+
register_message_type ?p
188+
fields :password
189+
190+
def dump
191+
super(@password.size + 1) do |buffer|
192+
buffer.write_cstring(@password)
193+
end
194+
end
195+
196+
def parse(buffer)
197+
super do
198+
@password = buffer.read_cstring
199+
end
200+
end
201+
end
202+
186203
class ParameterStatus < Message
187204
register_message_type ?S
188205
fields :key, :value
@@ -473,3 +490,33 @@ def parse(buffer)
473490
raise ParseError unless buffer.at_end?
474491
end
475492
end
493+
494+
class SSLRequest < Message
495+
fields :ssl_request_code
496+
497+
def dump
498+
sz = 4 + 4
499+
buffer = Buffer.new(sz)
500+
buffer.write_int32_network(sz)
501+
buffer.write_int32_network(@ssl_request_code)
502+
raise DumpError unless buffer.at_end?
503+
return buffer.content
504+
end
505+
506+
def parse(buffer)
507+
buffer.position = 4
508+
@ssl_request_code = buffer.read_int32_network
509+
raise ParseError unless buffer.at_end?
510+
end
511+
end
512+
513+
=begin
514+
# TODO: duplicate message-type, split into client/server messages
515+
class Sync < Message
516+
register_message_type ?S
517+
end
518+
=end
519+
520+
class Terminate < Message
521+
register_message_type ?X
522+
end

0 commit comments

Comments
 (0)