From 8119bf7f574ccedcd9689293dc0dad98ed245e5a Mon Sep 17 00:00:00 2001 From: Philippe PITTOLI Date: Mon, 10 Jun 2024 03:29:26 +0200 Subject: [PATCH] Migration: things are getting pretty serious. --- Makefile | 22 +++++++++++++++++++- bin/migration-final.awk | 4 ++++ src/client/lib/dnsmanager-client.cr | 12 +++++++++++ src/client/main.cr | 31 ++++++++++++++++++++++++++--- src/requests/zone.cr | 4 +--- src/storage.cr | 8 ++++---- src/storage/zone.cr | 18 ++++++++--------- 7 files changed, 79 insertions(+), 20 deletions(-) diff --git a/Makefile b/Makefile index 83871d2..b94b34e 100644 --- a/Makefile +++ b/Makefile @@ -107,7 +107,7 @@ print-response-messages-without-comments: MIGRATION_FILE_INIT ?= /tmp/dnsmanager-migration-init.txt MIGRATION_FILE_INTERMEDIARY ?= /tmp/dnsmanager-migration-intermediary.txt MIGRATION_FILE_TARGET ?= /tmp/dnsmanager-migration-target.txt -migrate: +$(MIGRATION_FILE_TARGET): # Format entries: one per line. ./bin/format.awk < $(MIGRATION_FILE_INIT) |\ ./bin/fix-last-element.awk |\ @@ -115,6 +115,8 @@ migrate: sed 's/ \+activated \+=> \+0, \+//' |\ sed 's/domain \+=> \+/"domain": /' |\ sed 's/ \+login \+=> \+/"login": /' |\ + # Put double-quotes around logins composed of digits. \ + sed 's/"login": \([0-9]\+\)/"login": "\1"/' |\ sed 's/", \+}/" }/' |\ # Fix @ in logins. \ sed 's/\\@/@/' |\ @@ -125,6 +127,24 @@ migrate: grep -v '\\' > $(MIGRATION_FILE_INTERMEDIARY) ./bin/migration-final.awk < $(MIGRATION_FILE_INTERMEDIARY) | sort -n > $(MIGRATION_FILE_TARGET) +MIGRATION_FILE_AUTHD ?= /tmp/authd-migration-user-db.txt +$(MIGRATION_FILE_AUTHD):; cat $(MIGRATION_FILE_TARGET) | awk '{ print $$2 }' > $(MIGRATION_FILE_AUTHD) +migration-file-authd: $(MIGRATION_FILE_AUTHD) + +run-migration-client:; ./bin/dnsmanager-client admin migration-script $(MIGRATION_FILE_TARGET) $(LOGIN) +migration-files: $(MIGRATION_FILE_TARGET) $(MIGRATION_FILE_AUTHD) +migration: migration-files run-migration-client + +doc: + crystal docs src/main.cr src/client.cr lib/authd/src/client.cr + +HTTPD_ACCESS_LOGS ?= /tmp/access-dodb-docs.log +HTTPD_ADDR ?= 127.0.0.1 +HTTPD_PORT ?= 9001 +DIR ?= docs +serve-doc: + darkhttpd $(DIR) --addr $(HTTPD_ADDR) --port $(HTTPD_PORT) --log $(HTTPD_ACCESS_LOGS) + wipe-db: rm -r $(DBDIR) diff --git a/bin/migration-final.awk b/bin/migration-final.awk index 27b1ac9..71b4032 100755 --- a/bin/migration-final.awk +++ b/bin/migration-final.awk @@ -1,5 +1,9 @@ #!/usr/bin/gawk -f +BEGIN { + OFS = "\t" +} + /^\[/ || /^\]/ { next } diff --git a/src/client/lib/dnsmanager-client.cr b/src/client/lib/dnsmanager-client.cr index 92485cc..fee0547 100644 --- a/src/client/lib/dnsmanager-client.cr +++ b/src/client/lib/dnsmanager-client.cr @@ -58,6 +58,18 @@ class DNSManager::Client < IPC ], read end + # Migration: add a domain for a user. + def migration(login : String, domain : String) + request = Request::Migration.new login, domain + send_now request + parse_message [ Response::DomainAdded, + Response::DomainAlreadyExists, + Response::InvalidDomainName, + Response::UnacceptableDomain, + Response::UnknownUser + ], read + end + # Remove a domain. def user_domain_del(domain : String) request = Request::DeleteDomain.new domain diff --git a/src/client/main.cr b/src/client/main.cr index b5c9496..c0ed12c 100644 --- a/src/client/main.cr +++ b/src/client/main.cr @@ -96,11 +96,36 @@ class Actions filename = Context.args.not_nil!.[0] File.each_line(filename) do |line| data = line.split "\t" + _ = data.shift login = data.shift + nb_domains = data.size begin - Baguette::Log.info "migrating login #{login}, domains: #{data.join(',')}" - # TODO - #pp! @dnsmanagerd.migration + str = data.join(", ").to_s + i = 0 + data.each do |domain| + STDOUT.write "migrating '#{i}/#{nb_domains}' domains for '#{login}': #{ "%.50s" % domain }\r".to_slice + STDOUT.write ((" " * 100) + "\r").to_slice + i += 1 + response = @dnsmanagerd.migration login, domain + case response + when DNSManager::Response::DomainAdded + # Do nothing. + else + case response + when DNSManager::Response::DomainAlreadyExists + Baguette::Log.error "error: domain name '#{domain}' already exists" + when DNSManager::Response::InvalidDomainName + Baguette::Log.error "error: invalid domain name #{domain}" + when DNSManager::Response::UnacceptableDomain + Baguette::Log.error "error: unacceptable domain name '#{domain}'" + when DNSManager::Response::UnknownUser + Baguette::Log.error "error: unknown user #{login}" + break + else + Baguette::Log.error "error: unknown error" + end + end + end rescue e puts "error for generate_zonefile: #{e.message}" end diff --git a/src/requests/zone.cr b/src/requests/zone.cr index 004b7db..4128400 100644 --- a/src/requests/zone.cr +++ b/src/requests/zone.cr @@ -13,9 +13,7 @@ class DNSManager::Request user = dnsmanagerd.get_logged_user event return Response::ErrorUserNotLogged.new unless user - accepted_domains = dnsmanagerd.configuration.accepted_domains.not_nil! - template_directory = dnsmanagerd.configuration.template_directory - dnsmanagerd.storage.new_domain accepted_domains, template_directory, user.uid, @domain + dnsmanagerd.storage.new_domain user.uid, @domain end end DNSManager.requests << NewDomain diff --git a/src/storage.cr b/src/storage.cr index 89b8754..ae5237e 100644 --- a/src/storage.cr +++ b/src/storage.cr @@ -111,10 +111,10 @@ class DNSManager::Storage Response::GeneratedZone.new domain, (String.new io.buffer, io.pos) end - def new_domain(accepted_domains : Array(String), - template_directory : String, - user_id : UserDataID, - domain : String) : IPC::JSON + def new_domain(user_id : UserDataID, domain : String) : IPC::JSON + + accepted_domains = dnsmanagerd.configuration.accepted_domains.not_nil! + template_directory = dnsmanagerd.configuration.template_directory # Prevent future very confusing errors. domain = domain.downcase diff --git a/src/storage/zone.cr b/src/storage/zone.cr index 4a6ef10..b61bd82 100644 --- a/src/storage/zone.cr +++ b/src/storage/zone.cr @@ -90,7 +90,7 @@ class DNSManager::Storage::Zone def to_s(io : IO) io << "(#{ "%4d" % @rrid }) " - io << "#{ "%30s" % @name} #{ "%6d" % @ttl} #{ "%10s" % @rrtype } #{ "%30s" % @target}\n" + io << "#{ "%.30s" % @name} #{ "%6d" % @ttl} #{ "%.10s" % @rrtype } #{ "%.30s" % @target}\n" end def to_bind9(io : IO) @@ -226,7 +226,7 @@ class DNSManager::Storage::Zone def to_s(io : IO) io << "(#{ "%4d" % @rrid }) " - io << "#{ "%30s" % @name} #{ "%6d" % @ttl} #{ "%10s" % @rrtype } #{quoted_string @target}\n" + io << "#{ "%.30s" % @name} #{ "%6d" % @ttl} #{ "%.10s" % @rrtype } #{quoted_string @target}\n" end def to_bind9(io : IO) @@ -465,7 +465,7 @@ class DNSManager::Storage::Zone def to_s(io : IO) io << "(#{ "%4d" % @rrid }) " - io << "#{ "%30s" % @name} #{ "%6d" % @ttl} SPF " + io << "#{ "%.30s" % @name} #{ "%6d" % @ttl} SPF " io << '"' @mechanisms.each do |m| io << m @@ -556,7 +556,7 @@ class DNSManager::Storage::Zone def to_s(io : IO) io << "(#{ "%4d" % @rrid }) " - io << "#{ "%30s" % @name} #{ "%6d" % @ttl} DKIM #{split_line dkim.to_s}\n" + io << "#{ "%.30s" % @name} #{ "%6d" % @ttl} DKIM #{split_line dkim.to_s}\n" end def to_bind9(io : IO) @@ -714,7 +714,7 @@ class DNSManager::Storage::Zone def to_s(io : IO) io << "(#{ "%4d" % @rrid }) " - io << "#{ "%30s" % @name} #{ "%6d" % @ttl} DMARC #{split_line dmarc.to_s}\n" + io << "#{ "%.30s" % @name} #{ "%6d" % @ttl} DMARC #{split_line dmarc.to_s}\n" end def to_bind9(io : IO) @@ -755,8 +755,8 @@ class DNSManager::Storage::Zone def to_s(io : IO) io << "(#{ "%4d" % @rrid }) " - io << "#{ "%30s" % @name} #{ "%6d" % @ttl} CAA " - io << "#{ "%3s" % @caa.flag} #{ "%15s" % @caa.tag} #{@caa.value}\n" + io << "#{ "%.30s" % @name} #{ "%6d" % @ttl} CAA " + io << "#{ "%.3s" % @caa.flag} #{ "%.15s" % @caa.tag} #{@caa.value}\n" end def to_bind9(io : IO) @@ -789,7 +789,7 @@ class DNSManager::Storage::Zone def to_s(io : IO) io << "(#{ "%4d" % @rrid }) " - io << "#{ "%30s" % @name} #{ "%6d" % @ttl} MX #{ "%3d" % @priority} #{ "%30s" % @target}\n" + io << "#{ "%.30s" % @name} #{ "%6d" % @ttl} MX #{ "%3d" % @priority} #{ "%.30s" % @target}\n" end def to_bind9(io : IO) @@ -850,7 +850,7 @@ class DNSManager::Storage::Zone io << "#{ "%3d" % @priority} " io << "#{ "%3d" % @weight} " io << "#{ "%5d" % @port} " - io << "#{ "%30s" % @target}\n" + io << "#{ "%.30s" % @target}\n" end def to_bind9(io : IO)