]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
Stubresolver: Use only `resolver` setting if given
authorPieter Lexis <pieter.lexis@powerdns.com>
Mon, 6 Mar 2017 14:06:26 +0000 (15:06 +0100)
committerPieter Lexis <pieter.lexis@powerdns.com>
Mon, 13 Nov 2017 12:36:28 +0000 (13:36 +0100)
Use resolv.conf otherwise. Also, do not use 127.0.0.1:53 as fallback,
as this could be ourselves.

Closes #4655

(cherry picked from commit 2b78726c6c9edd48c0905e44af9f88b5299dad75)

docs/markdown/authoritative/howtos.md
pdns/dnsproxy.cc
pdns/stubresolver.cc
pdns/stubresolver.hh

index 9dd164594dcaaf94e845105d43724d2209d8519a..a2cdb537dbebfb04fee81d1ed8791b4d26ae3dd1 100644 (file)
@@ -161,6 +161,8 @@ setting to an existing resolver:
 recursor=[::1]:5300
 ```
 
+**note**: If `resolver` is unset, ALIAS expension is disabled!
+
 and add the ALIAS record to your zone apex. e.g.:
 
 ```
index d29a76ab900978a44332f42234f5748403a5a06b..ea1d714e5abf21c5040b5da8c668c32c500c97cb 100644 (file)
@@ -42,7 +42,10 @@ DNSProxy::DNSProxy(const string &remote)
   d_resanswers=S.getPointer("recursing-answers");
   d_resquestions=S.getPointer("recursing-questions");
   d_udpanswers=S.getPointer("udp-answers");
-  ComboAddress remaddr(remote, 53);
+
+  vector<string> addresses;
+  stringtok(addresses, remote, " ,\t");
+  ComboAddress remaddr(addresses[0], 53);
   
   if((d_sock=socket(remaddr.sin4.sin_family, SOCK_DGRAM,0))<0)
     throw PDNSException(string("socket: ")+strerror(errno));
index e1a8935f9ebf4e01e9dbbdf3ce6b7af86acb6ad8..ddc05ead6e2255714450ba3c7d28d29bf75feb88 100644 (file)
 #include "statbag.hh"
 #include "stubresolver.hh"
 
-// s_stubresolvers contains the ComboAddresses that are used by
+// s_resolversForStub contains the ComboAddresses that are used by
 // stubDoResolve
-static vector<ComboAddress> s_stubresolvers;
+static vector<ComboAddress> s_resolversForStub;
 
-/** Parse /etc/resolv.conf and add the nameservers to the vector
- * s_stubresolvers.
+/*
+ * Returns false if no resolvers are configured, while emitting a warning about this
+ */
+bool resolversDefined()
+{
+  if (s_resolversForStub.empty()) {
+    L<<Logger::Warning<<"No upstream resolvers configured, stub resolving (including secpoll and ALIAS) impossible."<<endl;
+    return false;
+  }
+  return true;
+}
+
+/*
+ * Fill the s_resolversForStub vector with addresses for the upstream resolvers.
+ * First, parse the `resolver` configuration option for IP addresses to use.
+ * If that doesn't work, parse /etc/resolv.conf and add those nameservers to
+ * s_resolversForStub.
  */
 void stubParseResolveConf()
 {
-  ifstream ifs("/etc/resolv.conf");
-  if(!ifs)
-    return;
-
-  string line;
-  while(std::getline(ifs, line)) {
-    boost::trim_right_if(line, is_any_of(" \r\n\x1a"));
-    boost::trim_left(line); // leading spaces, let's be nice
-
-    string::size_type tpos = line.find_first_of(";#");
-    if(tpos != string::npos)
-      line.resize(tpos);
-
-    if(boost::starts_with(line, "nameserver ") || boost::starts_with(line, "nameserver\t")) {
-      vector<string> parts;
-      stringtok(parts, line, " \t,"); // be REALLY nice
-      for(vector<string>::const_iterator iter = parts.begin()+1; iter != parts.end(); ++iter) {
-        try {
-          s_stubresolvers.push_back(ComboAddress(*iter, 53));
-        }
-        catch(...)
-        {
+  if(::arg().mustDo("recursor")) {
+    vector<string> parts;
+    stringtok(parts, ::arg()["recursor"], " ,\t");
+    for (const auto& addr : parts)
+      s_resolversForStub.push_back(ComboAddress(addr, 53));
+  }
+
+  if (s_resolversForStub.empty()) {
+    ifstream ifs("/etc/resolv.conf");
+    if(!ifs)
+      return;
+
+    string line;
+    while(std::getline(ifs, line)) {
+      boost::trim_right_if(line, is_any_of(" \r\n\x1a"));
+      boost::trim_left(line); // leading spaces, let's be nice
+
+      string::size_type tpos = line.find_first_of(";#");
+      if(tpos != string::npos)
+        line.resize(tpos);
+
+      if(boost::starts_with(line, "nameserver ") || boost::starts_with(line, "nameserver\t")) {
+        vector<string> parts;
+        stringtok(parts, line, " \t,"); // be REALLY nice
+        for(vector<string>::const_iterator iter = parts.begin()+1; iter != parts.end(); ++iter) {
+          try {
+            s_resolversForStub.push_back(ComboAddress(*iter, 53));
+          }
+          catch(...)
+          {
+          }
         }
       }
     }
   }
-
-  if(::arg().mustDo("recursor"))
-    s_stubresolvers.push_back(ComboAddress(::arg()["recursor"], 53));
-
-  // Last resort, add 127.0.0.1
-  if(s_stubresolvers.empty()) {
-    s_stubresolvers.push_back(ComboAddress("127.0.0.1", 53));
-  }
 }
 
 // s_stubresolvers contains the ComboAddresses that are used to resolve the
-int stubDoResolve(const string& qname, uint16_t qtype, vector<DNSResourceRecord>& ret)
-{
+int stubDoResolve(const string& qname, uint16_t qtype, vector<DNSResourceRecord>& ret) {
+  // Emit a warning if there are no stubs.
+  resolversDefined();
+
   vector<uint8_t> packet;
 
   DNSPacketWriter pw(packet, DNSName(qname), qtype);
   pw.getHeader()->id=dns_random(0xffff);
   pw.getHeader()->rd=1;
-  if (s_stubresolvers.empty()) {
-    L<<Logger::Warning<<"No recursors set, stub resolving (including secpoll and ALIAS) impossible."<<endl;
-    return RCode::ServFail;
-  }
 
   string msg ="Doing stub resolving, using resolvers: ";
-  for (const auto& server : s_stubresolvers) {
+  for (const auto& server : s_resolversForStub) {
     msg += server.toString() + ", ";
   }
   L<<Logger::Debug<<msg.substr(0, msg.length() - 2)<<endl;
 
-  for(ComboAddress& dest :  s_stubresolvers) {
+  for(ComboAddress& dest :  s_resolversForStub) {
     Socket sock(dest.sin4.sin_family, SOCK_DGRAM);
     sock.setNonBlocking();
     sock.sendTo(string(packet.begin(), packet.end()), dest);
@@ -117,4 +131,4 @@ int stubDoResolve(const string& qname, uint16_t qtype, vector<DNSResourceRecord>
     return mdp.d_header.rcode;
   }
   return RCode::ServFail;
-}
\ No newline at end of file
+}
index cb1620ca29c617aef10a48078c2b14c525ebad62..46a7075af2840f61e0860e72a13c6955dcc86461 100644 (file)
@@ -25,3 +25,4 @@
 
 void stubParseResolveConf();
 int stubDoResolve(const string& qname, uint16_t qtype, vector<DNSResourceRecord>& ret);
+bool resolversDefined();