]> granicus.if.org Git - pdns/commitdiff
auth+rec secpoll: Use early return
authorPieter Lexis <pieter.lexis@powerdns.com>
Tue, 18 Jun 2019 15:39:48 +0000 (17:39 +0200)
committerPieter Lexis <pieter.lexis@powerdns.com>
Tue, 18 Jun 2019 15:55:27 +0000 (17:55 +0200)
instead of nested if's

pdns/secpoll-auth.cc
pdns/secpoll-recursor.cc

index 4166108e04e52da534a126f4b9fc36b3a9c53bbd..57b4ba61c71221ed78ed41b3d6b67cc24f926e2f 100644 (file)
@@ -45,29 +45,31 @@ void doSecPoll(bool first)
   boost::replace_all(query, "+", "_");
   boost::replace_all(query, "~", "_");
 
-  vector<DNSZoneRecord> ret;
+  int security_status = 0;
 
+  vector<DNSZoneRecord> ret;
   int res=stubDoResolve(DNSName(query), QType::TXT, ret);
 
-  int security_status=0;
-
-  if(!res && !ret.empty()) {
-    string content=getRR<TXTRecordContent>(ret.begin()->dr)->d_text;
-
-    pair<string, string> split = splitField(unquotify(content), ' ');
-
-    security_status = std::stoi(split.first);
-    g_security_message = split.second;
-
-  }
-  else {
+  if (res != 0) { // not NOERROR
     string pkgv(PACKAGEVERSION);
-    if (std::count(pkgv.begin(), pkgv.end(), '.') > 2)
+    if (std::count(pkgv.begin(), pkgv.end(), '.') > 2) {
       g_log<<Logger::Warning<<"Not validating response for security status update, this is a non-release version."<<endl;
-    else
-      g_log<<Logger::Warning<<"Could not retrieve security status update for '" + pkgv + "' on '"+query+"', RCODE = "<< RCode::to_s(res)<<endl;
+      return;
+    }
+    g_log<<Logger::Warning<<"Could not retrieve security status update for '" + PACKAGEVERSION + "' on '"+ query + "', RCODE = "<< RCode::to_s(res)<<endl;
+    return;
+  }
+
+  if (ret.empty()) { // empty NOERROR... wat?
+    g_log<<Logger::Warning<<"Could not retrieve security status update for '" + PACKAGEVERSION + "' on '"+ query + "', had empty answer, RCODE = "<< RCode::to_s(res)<<endl;
+    return;
   }
 
+  string content=getRR<TXTRecordContent>(ret.begin()->dr)->d_text;
+  pair<string, string> split = splitField(unquotify(content), ' ');
+  security_status = std::stoi(split.first);
+  g_security_message = split.second;
+
   if(security_status == 1 && first) {
     g_log<<Logger::Warning << "Polled security status of version "<<PACKAGEVERSION<<" at startup, no known issues reported: " <<g_security_message<<endl;
   }
@@ -78,6 +80,5 @@ void doSecPoll(bool first)
     g_log<<Logger::Error<<"PowerDNS Security Update Mandatory: "<<g_security_message<<endl;
   }
 
-  S.set("security-status",security_status);
-
+  S.set("security-status", security_status);
 }
index 5e1ecd268388fa5f60ef1385672813d10a09b9e4..79717a75c65254d714594257b1f73b875b4d20c8 100644 (file)
@@ -61,32 +61,40 @@ void doSecPoll(time_t* last_secpoll)
     return;
   }
 
-  if(!res && !ret.empty()) {
-    string content;
-    for(const auto&r : ret) {
-      if(r.d_type == QType::TXT)
-        content = r.d_content->getZoneRepresentation();
-    }
+  if (res != 0) { // Not NOERROR
+    if(g_security_status == 1) // it was ok, now it is unknown
+      g_security_status = 0;
 
-    if(!content.empty() && content[0]=='"' && content[content.size()-1]=='"') {
-      content=content.substr(1, content.length()-2);
+    if (std::count(pkgv.begin(), pkgv.end(), '.') > 2) {
+      g_log<<Logger::Warning<<"Ignoring response for security status update, this is a non-release version."<<endl;
+      return;
     }
-
-    pair<string, string> split = splitField(content, ' ');
-    
-    g_security_status = std::stoi(split.first);
-    g_security_message = split.second;
+    g_log<<Logger::Warning<<"Could not retrieve security status update for '" +pkgv+ "' on '"<<query<<"', RCODE = "<< RCode::to_s(res)<<endl;
+    return;
   }
-  else {
-    if (std::count(pkgv.begin(), pkgv.end(), '.') > 2)
-      g_log<<Logger::Warning<<"Ignoring response for security status update, this is a non-release version."<<endl;
-    else
-      g_log<<Logger::Warning<<"Could not retrieve security status update for '" +pkgv+ "' on '"<<query<<"', RCODE = "<< RCode::to_s(res)<<endl;
 
+  if (ret.empty()) { // Empty NOERROR
     if(g_security_status == 1) // it was ok, now it is unknown
       g_security_status = 0;
+    g_log<<Logger::Warning<<"Could not retrieve security status update for '" +pkgv+ "' on '"<<query<<"', had empty answer, RCODE = "<< RCode::to_s(res)<<endl;
+    return;
+  }
+
+  string content;
+  for(const auto&r : ret) {
+    if(r.d_type == QType::TXT)
+      content = r.d_content->getZoneRepresentation();
   }
 
+  if(!content.empty() && content[0]=='"' && content[content.size()-1]=='"') {
+    content=content.substr(1, content.length()-2);
+  }
+
+  pair<string, string> split = splitField(content, ' ');
+
+  g_security_status = std::stoi(split.first);
+  g_security_message = split.second;
+
   if(g_security_status == 2) {
     g_log<<Logger::Error<<"PowerDNS Security Update Recommended: "<<g_security_message<<endl;
   }