Users can now ask for the zonefile.

master
Philippe Pittoli 2024-02-27 07:42:10 +01:00
parent ba872ac4a9
commit faacccda15
6 changed files with 79 additions and 0 deletions

View File

@ -65,6 +65,13 @@ class DNSManager::Client < IPC
parse_message [ Response::Zone ], read
end
# Get the generated zonefile.
def user_zonefile(domain : String)
request = Request::AskGeneratedZoneFile.new domain
send_now request
parse_message [ Response::GeneratedZone ], read
end
#
# Resource Record operations
#

View File

@ -45,6 +45,7 @@ class Actions
# Zone operations.
@the_call["user-zone-add"] = ->user_zone_add
@the_call["user-zone-get"] = ->user_zone_get
@the_call["user-zonefile"] = ->user_zonefile
# Zone RR operations.
@the_call["user-rr-add-a"] = ->user_rr_add_a
@ -153,6 +154,27 @@ class Actions
end
end
def user_zonefile
domains = Context.args.not_nil!
domains.each do |domain|
begin
pp! domain
response = @dnsmanagerd.user_zonefile domain
case response
when DNSManager::Response::GeneratedZone
pp! response.domain
puts response.zonefile
else
puts "Didn't get the zone"
pp! response
end
rescue e
puts "error for user_zonefile: #{e.message}"
end
end
end
def user_domain_list
response = @dnsmanagerd.user_domain_list
case response

View File

@ -144,6 +144,13 @@ def parsing_cli(authd_config : Baguette::Configuration::Auth)
end
end
parser.on("zonefile", "Get a the generated zonefile.") do
Baguette::Log.info "get a zone file."
Context.command = "user-zonefile"
parser.banner = "COMMAND: user zonefile domain [domain...]"
unrecognized_args_to_context_args.call parser, nil, 1
end
# Zone.
parser.on("zone", "Zone operations.") do
parser.on("add", "Add new zone.") do

View File

@ -118,4 +118,18 @@ class DNSManager::Request
end
end
DNSManager.requests << DeleteRR
IPC::JSON.message AskGeneratedZoneFile, 17 do
property domain : String
def initialize(@domain)
end
def handle(dnsmanagerd : DNSManager::Service, event : IPC::Event) : IPC::JSON
user = dnsmanagerd.get_logged_user event
return Response::ErrorUserNotLogged.new unless user
dnsmanagerd.storage.get_generated_zonefile user.uid, @domain
end
end
DNSManager.requests << AskGeneratedZoneFile
end

View File

@ -102,5 +102,13 @@ class DNSManager::Response
end
end
DNSManager.responses << RRReadOnly
IPC::JSON.message GeneratedZone, 23 do
property domain : String
property zonefile : String
def initialize(@domain, @zonefile)
end
end
DNSManager.responses << GeneratedZone
end

View File

@ -85,6 +85,27 @@ class DNSManager::Storage
Response::Success.new
end
def get_generated_zonefile(user_id : Int32, domain : String) : IPC::JSON
zone = zones_by_domain.get? domain
return Response::DomainNotFound.new unless zone
# User must own the zone.
user_data = user_data_by_uid.get? user_id.to_s
unless user_data
Baguette::Log.warning "unknown user #{user_id} asked a zonefile for domain #{domain}"
return Response::UnknownUser.new
end
unless user_data.domains.includes?(zone.domain) || user_data.admin
Baguette::Log.warning "user #{user_id} doesn't own domain #{zone.domain}"
return Response::NoOwnership.new
end
io = IO::Memory.new
zone.to_bind9 io
Response::GeneratedZone.new domain, (String.new io.buffer, io.pos)
end
def new_domain(accepted_domains : Array(String),
template_directory : String,
user_id : Int32,