From 66dbffb4ee3dadbccde0016d3a55debbef157902 Mon Sep 17 00:00:00 2001 From: Miod Vallat Date: Mon, 11 Aug 2025 09:07:32 +0200 Subject: [PATCH] Pick better state names, rework variable scope a bit. Signed-off-by: Miod Vallat --- pdns/pdnsutil.cc | 108 +++++++++++++++++++++++------------------------ 1 file changed, 53 insertions(+), 55 deletions(-) diff --git a/pdns/pdnsutil.cc b/pdns/pdnsutil.cc index 46ad3a3fcf..a017512582 100644 --- a/pdns/pdnsutil.cc +++ b/pdns/pdnsutil.cc @@ -1844,7 +1844,7 @@ static int editZone(const ZoneName &zone, const PDNSColors& col) } if (isatty(STDIN_FILENO) == 0) { - cerr << "edit-zone requires a terminal" << endl; + cerr << "zone edit requires a terminal" << endl; return EXIT_FAILURE; } @@ -1865,13 +1865,10 @@ static int editZone(const ZoneName &zone, const PDNSColors& col) } } - /* ensure that the temporary file will only - be accessible by the current user, not even - by other users in the same group, and certainly - not by other users. - */ + // ensure that the temporary file will only be accessible by the current user, + // not even by other users in the same group, and certainly not by other + // users. umask(S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH); - vector pre, post; char tmpnam[]="/tmp/pdnsutil-XXXXXX"; int tmpfd=mkstemp(tmpnam); if(tmpfd < 0) @@ -1886,47 +1883,52 @@ static int editZone(const ZoneName &zone, const PDNSColors& col) string d_name; } dm(tmpnam); - vector checkrr; int gotoline=0; string editor="editor"; if(auto e=getenv("EDITOR")) // <3 editor=e; - map, vector > grouped; + vector pre, post; + map, vector> grouped; map, string> changed; - enum { EDITAGAIN, EDITMORE, REASK, REASK2, REASK3, VALIDATE, APPLY } state = EDITAGAIN; + + enum { CREATEZONEFILE, EDITFILE, INVALIDZONE, ASKAPPLY, ASKSOA, VALIDATE, APPLY } state = CREATEZONEFILE; while (true) { switch (state) { - case EDITAGAIN: + case CREATEZONEFILE: di.backend->list(zone, di.id); - pre.clear(); post.clear(); + pre.clear(); + post.clear(); { - if(tmpfd < 0 && (tmpfd=open(tmpnam, O_CREAT | O_WRONLY | O_TRUNC, 0600)) < 0) + if(tmpfd < 0 && (tmpfd=open(tmpnam, O_CREAT | O_WRONLY | O_TRUNC, 0600)) < 0) { unixDie("Error reopening temporary file "+string(tmpnam)); - string header("; Warning - every name in this file is ABSOLUTE!\n$ORIGIN .\n"); - if(write(tmpfd, header.c_str(), header.length()) < 0) + } + const string header("; Warning - every name in this file is ABSOLUTE!\n$ORIGIN .\n"); + if(write(tmpfd, header.c_str(), header.length()) < 0) { unixDie("Writing zone to temporary file"); + } DNSResourceRecord rr; while(di.backend->get(rr)) { - if(rr.qtype.getCode() == 0) { + if(rr.qtype.getCode() == QType::ENT) { continue; } DNSRecord dr(rr); - pre.push_back(std::move(dr)); + pre.emplace_back(std::move(dr)); } sort(pre.begin(), pre.end(), DNSRecord::prettyCompare); for(const auto& dr : pre) { ostringstream os; os<getZoneRepresentation(true)< checkrr; + for(const DNSRecord& rr : post) { + DNSResourceRecord drr = DNSResourceRecord::fromWire(rr); + drr.domain_id = di.id; + checkrr.push_back(std::move(drr)); + } + if(checkZone(dk, B, zone, &checkrr) != 0) { + state = INVALIDZONE; + break; + } } state = VALIDATE; break; - case REASK: + case INVALIDZONE: cerr << col.red() << col.bold() << "There was a problem with your zone" << col.rst() << "\nOptions are: (e)dit your changes, (r)etry with original zone, (a)pply change anyhow, (q)uit: " << std::flush; resp = ::tolower(read1char()); if (resp != '\n') { @@ -1987,10 +1990,10 @@ static int editZone(const ZoneName &zone, const PDNSColors& col) } if(resp=='e') { post.clear(); - state = EDITMORE; + state = EDITFILE; } else if(resp=='r') { post.clear(); - state = EDITAGAIN; + state = CREATEZONEFILE; } else if(resp=='q') { return EXIT_FAILURE; } else if(resp=='a') { @@ -2020,12 +2023,12 @@ static int editZone(const ZoneName &zone, const PDNSColors& col) for(const auto& c : changed) { cout<startTransaction(zone, UnknownDomainID); -- 2.47.3