Fixed bug allowing PASS to be given before USER:
* Added a unit test for this case. * Added a TODO item about the protocol errors. * Added SetUserFirst protocol error and new mapping to error code 531 (protocol change!). * Adapated Handler#pass_cmd to raise the SetUserFirst error when a username has not been set. git-svn-id: svn+ssh://svn.luon.net/svn/ildus/trunk@7 65a33f86-aa00-0410-91be-cd1bf5efb309
This commit is contained in:
parent
046f4b7b1e
commit
ac69b92281
5
TODO
5
TODO
|
@ -1,7 +1,8 @@
|
||||||
= Ildus ToDo list
|
= Ildus ToDo list
|
||||||
|
|
||||||
_Empty for now._
|
* Put Protocol Errors in a separate module, document it
|
||||||
|
better together with the protocol.
|
||||||
|
|
||||||
= Known Issues
|
= Known Issues
|
||||||
|
|
||||||
_Unknown as of yet._
|
_Unknown as of yet_.
|
||||||
|
|
|
@ -8,8 +8,7 @@
|
||||||
# any later version.
|
# any later version.
|
||||||
|
|
||||||
require 'yaml'
|
require 'yaml'
|
||||||
|
require 'gserver'
|
||||||
require 'ildus/server/handler'
|
|
||||||
|
|
||||||
module Ildus
|
module Ildus
|
||||||
|
|
||||||
|
@ -77,3 +76,5 @@ module Ildus
|
||||||
end # class Server
|
end # class Server
|
||||||
|
|
||||||
end # module Ildus
|
end # module Ildus
|
||||||
|
|
||||||
|
require 'ildus/server/handler'
|
||||||
|
|
|
@ -7,14 +7,12 @@
|
||||||
# Software Foundation; either version 2 of the License, or (at your option)
|
# Software Foundation; either version 2 of the License, or (at your option)
|
||||||
# any later version.
|
# any later version.
|
||||||
|
|
||||||
require 'gserver'
|
|
||||||
|
|
||||||
require 'ildus/server/account'
|
require 'ildus/server/account'
|
||||||
require 'ildus/server/backend'
|
require 'ildus/server/backend'
|
||||||
|
|
||||||
module Ildus
|
module Ildus
|
||||||
|
|
||||||
class Server
|
class Server < GServer
|
||||||
|
|
||||||
class Handler
|
class Handler
|
||||||
|
|
||||||
|
@ -32,6 +30,7 @@ module Ildus
|
||||||
class AlreadyAuthError < ProtocolException; end # 504
|
class AlreadyAuthError < ProtocolException; end # 504
|
||||||
class BackendError < ProtocolException; end # 506
|
class BackendError < ProtocolException; end # 506
|
||||||
class NotAuthError < ProtocolException; end # 530
|
class NotAuthError < ProtocolException; end # 530
|
||||||
|
class SetUserFirstError < ProtocolException; end # 531
|
||||||
|
|
||||||
MaxCmdErrs = 3
|
MaxCmdErrs = 3
|
||||||
|
|
||||||
|
@ -59,15 +58,16 @@ module Ildus
|
||||||
504 => "You are already authenticated!",
|
504 => "You are already authenticated!",
|
||||||
505 => "Server error: %s!",
|
505 => "Server error: %s!",
|
||||||
506 => "Server error, update failed: %s",
|
506 => "Server error, update failed: %s",
|
||||||
530 => "Not authenticated!"
|
530 => "Not authenticated!",
|
||||||
|
531 => "Login with USER first."
|
||||||
}
|
}
|
||||||
|
|
||||||
def initialize(server, io)
|
def initialize(server, io)
|
||||||
@server = server
|
@server = server
|
||||||
@io = io
|
@io = io
|
||||||
@account = Account.new
|
@account = Account.new
|
||||||
type = server.config["backend"]["type"]
|
type = server.config["backend"]["type"]
|
||||||
klass = Backend[server.config["backend"]["type"]]
|
klass = Backend[server.config["backend"]["type"]]
|
||||||
raise "backend type `#{type}' not found" if klass.nil?
|
raise "backend type `#{type}' not found" if klass.nil?
|
||||||
@backend = klass.new
|
@backend = klass.new
|
||||||
end
|
end
|
||||||
|
@ -99,6 +99,8 @@ module Ildus
|
||||||
prot_msg 506, msg
|
prot_msg 506, msg
|
||||||
rescue NotAuthError # 530
|
rescue NotAuthError # 530
|
||||||
prot_msg 530
|
prot_msg 530
|
||||||
|
rescue GiveUserFirstError # 531
|
||||||
|
prot_msg 531
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
rescue TooManyUnknownError # 503
|
rescue TooManyUnknownError # 503
|
||||||
|
@ -143,6 +145,8 @@ module Ildus
|
||||||
end
|
end
|
||||||
|
|
||||||
def pass_cmd(password)
|
def pass_cmd(password)
|
||||||
|
raise SetUserFirstError unless @account.user
|
||||||
|
|
||||||
@account.pass = password
|
@account.pass = password
|
||||||
if @account.authenticated?
|
if @account.authenticated?
|
||||||
prot_msg 230, @account.user
|
prot_msg 230, @account.user
|
||||||
|
|
|
@ -1,13 +1,14 @@
|
||||||
require 'test/unit'
|
require 'test/unit'
|
||||||
require 'ildus/server/handler'
|
require 'ildus/server'
|
||||||
|
|
||||||
class TC_HandlerTest < Test::Unit::TestCase
|
class TC_HandlerTest < Test::Unit::TestCase
|
||||||
include Ildus
|
include Ildus
|
||||||
|
|
||||||
def setup
|
def setup
|
||||||
# Create a handler object.
|
# Create a handler object.
|
||||||
@io = StringIO.new
|
@io = StringIO.new
|
||||||
@hdl = Server::Handler.new(nil, @io)
|
serv = Server.new("conf/ildusd.conf")
|
||||||
|
@hdl = Server::Handler.new(serv, @io)
|
||||||
assert(@hdl)
|
assert(@hdl)
|
||||||
|
|
||||||
class << @hdl
|
class << @hdl
|
||||||
|
@ -20,6 +21,11 @@ class TC_HandlerTest < Test::Unit::TestCase
|
||||||
# "list" shouldn't be possible until authenticated.
|
# "list" shouldn't be possible until authenticated.
|
||||||
assert_raises(Server::Handler::NotAuthError) { @hdl.handle_command("list") }
|
assert_raises(Server::Handler::NotAuthError) { @hdl.handle_command("list") }
|
||||||
|
|
||||||
|
# Password is not allowed before setting a user.
|
||||||
|
assert_raises(Server::Handler::SetUserFirstError) do
|
||||||
|
@hdl.handle_command("pass", ["secret"])
|
||||||
|
end
|
||||||
|
|
||||||
# Set the user.
|
# Set the user.
|
||||||
@hdl.handle_command("user", ["test"])
|
@hdl.handle_command("user", ["test"])
|
||||||
assert_equal("test", @hdl.account.user)
|
assert_equal("test", @hdl.account.user)
|
||||||
|
@ -31,12 +37,18 @@ class TC_HandlerTest < Test::Unit::TestCase
|
||||||
@hdl.handle_command("user", ["test2"])
|
@hdl.handle_command("user", ["test2"])
|
||||||
assert_equal("test2", @hdl.account.user)
|
assert_equal("test2", @hdl.account.user)
|
||||||
|
|
||||||
# Nothing should be raised when setting the password,
|
# Give an incorrect password.
|
||||||
# we should be authenticated afterwards.
|
assert_raises(Server::Handler::NotAuthError) do
|
||||||
assert_nothing_raised do
|
|
||||||
@hdl.handle_command("pass", ["secret"])
|
@hdl.handle_command("pass", ["secret"])
|
||||||
end
|
end
|
||||||
assert_equal("secret", @hdl.account.pass)
|
assert_equal("secret", @hdl.account.pass)
|
||||||
|
|
||||||
|
# Nothing should be raised when setting the password,
|
||||||
|
# we should be authenticated afterwards.
|
||||||
|
assert_nothing_raised do
|
||||||
|
@hdl.handle_command("pass", ["foo"])
|
||||||
|
end
|
||||||
|
assert_equal("foo", @hdl.account.pass)
|
||||||
assert(@hdl.account.authenticated?)
|
assert(@hdl.account.authenticated?)
|
||||||
|
|
||||||
# "list" should be possible now.
|
# "list" should be possible now.
|
||||||
|
|
Reference in New Issue