From: Christian Hofstaedtler Date: Wed, 9 Dec 2015 10:31:55 +0000 (+0100) Subject: Replace dnsnameFromJson() with apiNameToDNSName(stringFromJson()) X-Git-Tag: dnsdist-1.0.0-alpha1~85^2~1 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=c576d0c561b15e70b7c6b26ec9ad9c611c977332;p=pdns Replace dnsnameFromJson() with apiNameToDNSName(stringFromJson()) To reuse the name checking code for strings that didn't come from json. Also makes it clearer that the checking logic is an API only thing, and other json consumers may use their own policy on dots, etc. --- diff --git a/pdns/json.cc b/pdns/json.cc index f898bb88d..2124f8d32 100644 --- a/pdns/json.cc +++ b/pdns/json.cc @@ -90,27 +90,6 @@ string stringFromJson(const Value& container, const char* key, const string& def } } -DNSName dnsnameFromJson(const rapidjson::Value& container, const char* key) -{ - if (!container.IsObject()) { - throw JsonException("Container was not an object."); - } - const Value& val = container[key]; - if (val.IsString()) { - string name = val.GetString(); - if (!isCanonical(name)) { - throw JsonException("DNS Name '" + name + "' is not canonical"); - } - try { - return DNSName(name); - } catch (...) { - throw JsonException("Unable to parse DNS Name '" + name + "'"); - } - } else { - throw JsonException("Key '" + string(key) + "' not present or not a String"); - } -} - bool boolFromJson(const rapidjson::Value& container, const char* key) { if (!container.IsObject()) { diff --git a/pdns/json.hh b/pdns/json.hh index da6ca5c89..774631d8e 100644 --- a/pdns/json.hh +++ b/pdns/json.hh @@ -25,7 +25,6 @@ #include #include #include "rapidjson/document.h" -#include "dnsname.hh" std::string returnJsonObject(const std::map& items); std::string returnJsonError(const std::string& error); @@ -35,7 +34,6 @@ int intFromJson(const rapidjson::Value& container, const char* key); int intFromJson(const rapidjson::Value& container, const char* key, const int default_value); std::string stringFromJson(const rapidjson::Value& container, const char* key); std::string stringFromJson(const rapidjson::Value& container, const char* key, const std::string& default_value); -DNSName dnsnameFromJson(const rapidjson::Value& container, const char* key); bool boolFromJson(const rapidjson::Value& container, const char* key); bool boolFromJson(const rapidjson::Value& container, const char* key, const bool default_value); diff --git a/pdns/ws-api.cc b/pdns/ws-api.cc index 54a62a1b3..a24b7774e 100644 --- a/pdns/ws-api.cc +++ b/pdns/ws-api.cc @@ -221,6 +221,17 @@ void apiServerStatistics(HttpRequest* req, HttpResponse* resp) { resp->setBody(doc); } +DNSName apiNameToDNSName(const string& name) { + if (!isCanonical(name)) { + throw ApiException("DNS Name '" + name + "' is not canonical"); + } + try { + return DNSName(name); + } catch (...) { + throw ApiException("Unable to parse DNS Name '" + name + "'"); + } +} + DNSName apiZoneIdToName(const string& id) { string zonename; ostringstream ss; diff --git a/pdns/ws-api.hh b/pdns/ws-api.hh index 6ae4c78fb..fd0071976 100644 --- a/pdns/ws-api.hh +++ b/pdns/ws-api.hh @@ -38,6 +38,7 @@ void apiServerStatistics(HttpRequest* req, HttpResponse* resp); DNSName apiZoneIdToName(const string& id); string apiZoneNameToId(const DNSName& name); void apiCheckNameAllowedCharacters(const string& label); +DNSName apiNameToDNSName(const string& name); // To be provided by product code. void productServerStatisticsFetch(std::map& out); diff --git a/pdns/ws-auth.cc b/pdns/ws-auth.cc index 547988cf8..30402fc1b 100644 --- a/pdns/ws-auth.cc +++ b/pdns/ws-auth.cc @@ -413,7 +413,7 @@ static void gatherRecords(const Value& container, vector& new if (records.IsArray()) { for (SizeType idx = 0; idx < records.Size(); ++idx) { const Value& record = records[idx]; - rr.qname = dnsnameFromJson(record, "name"); + rr.qname = apiNameToDNSName(stringFromJson(record, "name")); rr.qtype = stringFromJson(record, "type"); string content = stringFromJson(record, "content"); rr.auth = 1; @@ -621,7 +621,7 @@ static void apiServerZones(HttpRequest* req, HttpResponse* resp) { DomainInfo di; Document document; req->json(document); - DNSName zonename = dnsnameFromJson(document, "name"); + DNSName zonename = apiNameToDNSName(stringFromJson(document, "name")); apiCheckNameAllowedCharacters(zonename.toString()); string zonestring = stringFromJson(document, "zone", ""); @@ -952,7 +952,7 @@ static void patchZone(HttpRequest* req, HttpResponse* resp) { const Value& rrset = rrsets[rrsetIdx]; string changetype; QType qtype; - DNSName qname = dnsnameFromJson(rrset, "name"); + DNSName qname = apiNameToDNSName(stringFromJson(rrset, "name")); apiCheckNameAllowedCharacters(qname.toString()); qtype = stringFromJson(rrset, "type"); changetype = toUpper(stringFromJson(rrset, "changetype")); diff --git a/pdns/ws-recursor.cc b/pdns/ws-recursor.cc index 978e9da1b..2755bdea5 100644 --- a/pdns/ws-recursor.cc +++ b/pdns/ws-recursor.cc @@ -187,7 +187,7 @@ static void doCreateZone(const Value& document) throw ApiException("Config Option \"api-config-dir\" must be set"); } - DNSName zonename = dnsnameFromJson(document, "name"); + DNSName zonename = apiNameToDNSName(stringFromJson(document, "name")); apiCheckNameAllowedCharacters(zonename.toString()); string singleIPTarget = stringFromJson(document, "single_target_ip", ""); @@ -279,7 +279,7 @@ static void apiServerZones(HttpRequest* req, HttpResponse* resp) Document document; req->json(document); - DNSName zonename = dnsnameFromJson(document, "name"); + DNSName zonename = apiNameToDNSName(stringFromJson(document, "name")); auto iter = t_sstorage->domainmap->find(zonename); if (iter != t_sstorage->domainmap->end()) @@ -340,7 +340,7 @@ static void apiServerZoneDetail(HttpRequest* req, HttpResponse* resp) doDeleteZone(zonename); doCreateZone(document); reloadAuthAndForwards(); - fillZone(dnsnameFromJson(document, "name"), resp); + fillZone(apiNameToDNSName(stringFromJson(document, "name")), resp); } else if(req->method == "DELETE" && !::arg().mustDo("api-readonly")) { if (!doDeleteZone(zonename)) {