* Only delegate the last authentication part to the backend,
added Backend::Basic#authenticate by splitting pass=(). * Make Hanlder#updt_cmd parse the address via IPAddr. * Moved some raising of 5xx errors from Backend to Handler. * Rename *hostname to *host. * Implemented Backend#LDAPv3#update_host. * Style fixes. git-svn-id: svn+ssh://svn.luon.net/svn/ildus/trunk@10 65a33f86-aa00-0410-91be-cd1bf5efb309
This commit is contained in:
parent
40bc5d22a3
commit
baca1dd160
|
@ -17,25 +17,21 @@ module Ildus
|
||||||
|
|
||||||
BackendClasses = Hash.new
|
BackendClasses = Hash.new
|
||||||
|
|
||||||
class << self
|
def self.load
|
||||||
|
path = File.dirname(__FILE__)
|
||||||
|
Dir["#{path}/backends/**/*.rb"].each { |file| require file }
|
||||||
|
end
|
||||||
|
|
||||||
def load
|
def self.get(type)
|
||||||
path = File.dirname(__FILE__)
|
BackendClasses[type.to_sym]
|
||||||
Dir["#{path}/backends/**/*.rb"].each { |file| require file }
|
end
|
||||||
|
alias_method :[], :get
|
||||||
|
|
||||||
|
def self.register(type, backend_class)
|
||||||
|
if BackendClasses.include? type
|
||||||
|
raise "type #{type} already registered"
|
||||||
end
|
end
|
||||||
|
BackendClasses[type] = backend_class
|
||||||
def get(type)
|
|
||||||
BackendClasses[type.to_sym]
|
|
||||||
end
|
|
||||||
alias_method :[], :get
|
|
||||||
|
|
||||||
def register(type, backend_class)
|
|
||||||
if BackendClasses.include? type
|
|
||||||
raise "type #{type} already registered"
|
|
||||||
end
|
|
||||||
BackendClasses[type] = backend_class
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
class Basic
|
class Basic
|
||||||
|
@ -59,22 +55,25 @@ module Ildus
|
||||||
# Account methods
|
# Account methods
|
||||||
|
|
||||||
def user=(username)
|
def user=(username)
|
||||||
raise Handler::AlreadyAuthError if @auth
|
|
||||||
@user = username
|
@user = username
|
||||||
end
|
end
|
||||||
|
|
||||||
def pass=(password)
|
def pass=(password)
|
||||||
raise Handler::AlreadyAuthError if @auth
|
|
||||||
@pass = password
|
@pass = password
|
||||||
## STUB
|
authenticate
|
||||||
@auth = (password == "foo")
|
|
||||||
##
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def authenticated?
|
def authenticated?
|
||||||
@auth
|
@auth
|
||||||
end
|
end
|
||||||
|
|
||||||
|
#################
|
||||||
|
# Backend methods
|
||||||
|
|
||||||
|
def authenticate
|
||||||
|
raise Handler::NotImplementedError
|
||||||
|
end
|
||||||
|
|
||||||
def register_account
|
def register_account
|
||||||
raise Handler::NotImplementedError
|
raise Handler::NotImplementedError
|
||||||
end
|
end
|
||||||
|
@ -83,22 +82,19 @@ module Ildus
|
||||||
raise Handler::NotImplementedError
|
raise Handler::NotImplementedError
|
||||||
end
|
end
|
||||||
|
|
||||||
#################
|
def hosts
|
||||||
# Backend methods
|
|
||||||
|
|
||||||
def hostnames
|
|
||||||
raise Handler::NotImplementedError
|
raise Handler::NotImplementedError
|
||||||
end
|
end
|
||||||
|
|
||||||
def add_hostname(host)
|
def add_host(host)
|
||||||
raise Handler::NotImplementedError
|
raise Handler::NotImplementedError
|
||||||
end
|
end
|
||||||
|
|
||||||
def remove_hostname(host)
|
def remove_host(host)
|
||||||
raise Handler::NotImplementedError
|
raise Handler::NotImplementedError
|
||||||
end
|
end
|
||||||
|
|
||||||
def update_hostname(host, addr)
|
def update_host(host, addr)
|
||||||
raise Handler::NotImplementedError
|
raise Handler::NotImplementedError
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -20,31 +20,53 @@ module Ildus::Server::Backend
|
||||||
@ldap.simple_bind(config['user'], config['pass'])
|
@ldap.simple_bind(config['user'], config['pass'])
|
||||||
end
|
end
|
||||||
|
|
||||||
def hostnames
|
def authenticate
|
||||||
|
## STUB
|
||||||
|
@auth = (@pass == "foo")
|
||||||
|
##
|
||||||
|
end
|
||||||
|
|
||||||
|
def hosts
|
||||||
entries = Hash.new { |h, k| h[k] = [[], []] }
|
entries = Hash.new { |h, k| h[k] = [[], []] }
|
||||||
@ldap.search(config['base'], LDAP::LDAP_SCOPE_SUBTREE,
|
|
||||||
"ildusOwner=#{user}") do |entry|
|
all_entries.each do |entry|
|
||||||
assoc_dom, a_rr, aaaa_rr, cname_rr =
|
assoc_dom, a_rr, aaaa_rr, cname_rr =
|
||||||
["associatedDomain", "aRecord",
|
["associatedDomain", "aRecord",
|
||||||
"aAAArecord", "cNAMErecord"].map do |attr|
|
"aAAArecord", "cNAMErecord"].map { |attr| entry[attr] }
|
||||||
entry.vals(attr)
|
|
||||||
end
|
|
||||||
|
|
||||||
host = assoc_dom.first.gsub(/\.#{config['domain']}$/, '')
|
host = assoc_dom.first.gsub(/\.#{config['domain']}$/, '')
|
||||||
if a_rr
|
|
||||||
entries[host].first.push(*a_rr)
|
entries[host].first.push(*a_rr) if a_rr
|
||||||
end
|
entries[host].first.push(*aaaa_rr) if aaaa_rr
|
||||||
if aaaa_rr
|
|
||||||
entries[host].first.push(*aaaa_rr)
|
|
||||||
end
|
|
||||||
if cname_rr
|
if cname_rr
|
||||||
cname = cname_rr.first.gsub(/\.#{config['domain']}$/, '')
|
cname = cname_rr.first.gsub(/\.#{config['domain']}$/, '')
|
||||||
entries[cname].last << host
|
entries[cname].last << host
|
||||||
end
|
end
|
||||||
end
|
end # search
|
||||||
|
|
||||||
return entries
|
return entries
|
||||||
end # hostnames
|
end # def hosts
|
||||||
|
|
||||||
|
def update_host(host, addr)
|
||||||
|
entry = all_entries.find do |entry|
|
||||||
|
entry['associatedDomain'][0] == host + "." + config['domain']
|
||||||
|
end
|
||||||
|
raise Handler::HostNotFoundError if entry.nil?
|
||||||
|
|
||||||
|
if addr.ipv4?
|
||||||
|
@ldap.modify(entry['dn'][0], {"aRecord" => [addr.to_s]})
|
||||||
|
elsif addr.ipv6?
|
||||||
|
@ldap.modify(entry['dn'][0], {"aAAARecord" => [addr.to_s]})
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
#########
|
||||||
|
private
|
||||||
|
#########
|
||||||
|
|
||||||
|
def all_entries
|
||||||
|
@ldap.search2(config['base'], LDAP::LDAP_SCOPE_SUBTREE,
|
||||||
|
"(&(objectClass=ildusRecord)(ildusOwner=#{user}))")
|
||||||
|
end
|
||||||
|
|
||||||
end # class LDAPv3
|
end # class LDAPv3
|
||||||
|
|
||||||
|
|
|
@ -7,6 +7,8 @@
|
||||||
# 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 'ipaddr'
|
||||||
|
|
||||||
require 'ildus/server/backend'
|
require 'ildus/server/backend'
|
||||||
|
|
||||||
module Ildus
|
module Ildus
|
||||||
|
@ -63,11 +65,12 @@ module Ildus
|
||||||
|
|
||||||
def initialize(server, io)
|
def initialize(server, io)
|
||||||
@server = server
|
@server = server
|
||||||
|
@config = server.config
|
||||||
@io = io
|
@io = io
|
||||||
type = server.config["backend"]["type"]
|
type = @config["backend"]["type"]
|
||||||
klass = Backend[server.config["backend"]["type"]]
|
klass = Backend[@config["backend"]["type"]]
|
||||||
raise "backend type `#{type}' not found" if klass.nil?
|
raise "backend type `#{type}' not found" if klass.nil?
|
||||||
@backend = klass.new(server.config["backend"])
|
@backend = klass.new(@config["backend"])
|
||||||
rescue => msg
|
rescue => msg
|
||||||
prot_msg 505, msg
|
prot_msg 505, msg
|
||||||
raise
|
raise
|
||||||
|
@ -141,14 +144,16 @@ module Ildus
|
||||||
# Commands methods
|
# Commands methods
|
||||||
|
|
||||||
def user_cmd(username)
|
def user_cmd(username)
|
||||||
|
raise AlreadyAuthError if @backend.authenticated?
|
||||||
@backend.user = username
|
@backend.user = username
|
||||||
prot_msg 331
|
prot_msg 331
|
||||||
end
|
end
|
||||||
|
|
||||||
def pass_cmd(password)
|
def pass_cmd(password)
|
||||||
raise SetUserFirstError unless @backend.user
|
raise SetUserFirstError unless @backend.user
|
||||||
|
raise AlreadyAuthError if @backend.authenticated?
|
||||||
@backend.pass = password
|
@backend.pass = password
|
||||||
|
|
||||||
if @backend.authenticated?
|
if @backend.authenticated?
|
||||||
prot_msg 230, @backend.user
|
prot_msg 230, @backend.user
|
||||||
else
|
else
|
||||||
|
@ -156,42 +161,41 @@ module Ildus
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def updt_cmd(hostname, addr)
|
def updt_cmd(host, addr)
|
||||||
raise NotAuthError unless @backend.authenticated?
|
raise NotAuthError unless @backend.authenticated?
|
||||||
@backend.update_hostname(@backend.user, hostname, addr)
|
addr = IPAddr.new(addr)
|
||||||
|
@backend.update_host(host, addr)
|
||||||
rescue HostNotFoundError
|
rescue HostNotFoundError
|
||||||
prot_msg 425, hostname
|
prot_msg 425, host
|
||||||
rescue RecordNotFoundError
|
rescue RecordNotFoundError
|
||||||
prot_msg 426, type, hostname
|
prot_msg 426, type, host
|
||||||
else
|
else
|
||||||
prot_msg 240, hostname, addr
|
prot_msg 240, host, addr
|
||||||
end
|
end
|
||||||
|
|
||||||
def adda_cmd(hostname, new_alias)
|
def adda_cmd(host, new_alias)
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
end
|
end
|
||||||
|
|
||||||
def dela_cmd(hostname, old_alias)
|
def dela_cmd(host, old_alias)
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
end
|
end
|
||||||
|
|
||||||
def list_cmd
|
def list_cmd
|
||||||
raise NotAuthError unless @backend.authenticated?
|
raise NotAuthError unless @backend.authenticated?
|
||||||
user = @backend.user
|
user = @backend.user
|
||||||
list = @backend.hostnames
|
list = @backend.hosts.inject(Hash.new) do |memo, (host, info)|
|
||||||
h = list.inject(Hash.new) do |memo, (host, info)|
|
memo[host] = {"addresses" => info.first, "aliases" => info.last}
|
||||||
memo[host] = {"addresses" => info.first, "aliases" => info.last}
|
memo
|
||||||
memo
|
end
|
||||||
end
|
|
||||||
|
|
||||||
prot_msg_with_body 215,
|
prot_msg_with_body 215,
|
||||||
"Listing of hosts (and aliases) for user #{user}\n" +
|
"Listing of hosts (and aliases) for user #{user}\n" +
|
||||||
h.to_yaml + "\n"
|
list.to_yaml + "\n"
|
||||||
end
|
end
|
||||||
|
|
||||||
def help_cmd
|
def help_cmd
|
||||||
prot_msg_with_body 214,
|
prot_msg_with_body 214, <<-EOT, "ildus-admin@localhost"
|
||||||
<<-EOT, "ildus-admin@localhost"
|
|
||||||
The following commands are recognized:
|
The following commands are recognized:
|
||||||
HELP\t\t\tshow this help
|
HELP\t\t\tshow this help
|
||||||
QUIT\t\t\tclose connection
|
QUIT\t\t\tclose connection
|
||||||
|
|
Reference in New Issue