summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorADAM David Alan Martin <adam.martin@10gen.com>2018-02-09 16:35:52 -0500
committerADAM David Alan Martin <adam.martin@10gen.com>2018-02-09 16:35:52 -0500
commitbf33c24ff0cf4018dfa94e3a349421ace28d1fac (patch)
tree0cb96460c56912b61093289cd713703d485ad13e /src
parentfdc23cf6a586a607299ec8a880574e95071e8f39 (diff)
downloadmongo-bf33c24ff0cf4018dfa94e3a349421ace28d1fac.tar.gz
SERVER-33140 DNS must disregard irrelevant records
DNS Queries can return superfluous records of non-requested type in Windows DNSApi calls. Windows implementations will now ignore records of those types.
Diffstat (limited to 'src')
-rw-r--r--src/mongo/base/error_codes.err1
-rw-r--r--src/mongo/util/dns_query.cpp66
-rw-r--r--src/mongo/util/dns_query_windows-impl.h21
3 files changed, 60 insertions, 28 deletions
diff --git a/src/mongo/base/error_codes.err b/src/mongo/base/error_codes.err
index ed5f7f63b16..f43da8c8a6c 100644
--- a/src/mongo/base/error_codes.err
+++ b/src/mongo/base/error_codes.err
@@ -237,6 +237,7 @@ error_code("ForTestingErrorExtraInfo", 236, extra="ErrorExtraInfoExample")
error_code("CursorKilled", 237)
error_code("NotImplemented", 238)
error_code("SnapshotTooOld", 239)
+error_code("DNSRecordTypeMismatch", 240)
# Error codes 4000-8999 are reserved.
diff --git a/src/mongo/util/dns_query.cpp b/src/mongo/util/dns_query.cpp
index 7da5909b0cd..5d10b2fb82e 100644
--- a/src/mongo/util/dns_query.cpp
+++ b/src/mongo/util/dns_query.cpp
@@ -43,6 +43,8 @@
#include <boost/noncopyable.hpp>
+#include "mongo/bson/util/builder.h"
+
// It is safe to include the implementation "headers" in an anonymous namespace, as the code is
// meant to live in a single TU -- this one. Include one of these headers last.
#define MONGO_UTIL_DNS_QUERY_PLATFORM_INCLUDE_WHITELIST
@@ -68,15 +70,25 @@ std::vector<std::string> dns::lookupARecords(const std::string& service) {
DNSQueryState dnsQuery;
auto response = dnsQuery.lookup(service, DNSQueryClass::kInternet, DNSQueryType::kAddress);
- if (response.size() == 0) {
- uasserted(ErrorCodes::DNSProtocolError,
- "Looking up " + service + " A record yielded no results.");
+ std::vector<std::string> rv;
+
+ for (const auto& entry : response) {
+ try {
+ rv.push_back(entry.addressEntry());
+ } catch (const ExceptionFor<ErrorCodes::DNSRecordTypeMismatch>&) {
+ }
}
- std::vector<std::string> rv;
- std::transform(begin(response), end(response), back_inserter(rv), [](const auto& entry) {
- return entry.addressEntry();
- });
+ if (rv.empty()) {
+ StringBuilder oss;
+ oss << "Looking up " << service << " A record yielded ";
+ if (response.size() == 0) {
+ oss << "no results.";
+ } else {
+ oss << "no A records but " << response.size() << " other records";
+ }
+ uasserted(ErrorCodes::DNSProtocolError, oss.str());
+ }
return rv;
}
@@ -88,9 +100,24 @@ std::vector<dns::SRVHostEntry> dns::lookupSRVRecords(const std::string& service)
std::vector<SRVHostEntry> rv;
- std::transform(begin(response), end(response), back_inserter(rv), [](const auto& entry) {
- return entry.srvHostEntry();
- });
+ for (const auto& entry : response) {
+ try {
+ rv.push_back(entry.srvHostEntry());
+ } catch (const ExceptionFor<ErrorCodes::DNSRecordTypeMismatch>&) {
+ }
+ }
+
+ if (rv.empty()) {
+ StringBuilder oss;
+ oss << "Looking up " << service << " SRV record yielded ";
+ if (response.size() == 0) {
+ oss << "no results.";
+ } else {
+ oss << "no SRV records but " << response.size() << " other records";
+ }
+ uasserted(ErrorCodes::DNSProtocolError, oss.str());
+ }
+
return rv;
}
@@ -102,20 +129,21 @@ std::vector<std::string> dns::lookupTXTRecords(const std::string& service) {
std::vector<std::string> rv;
for (auto& entry : response) {
- auto txtEntry = entry.txtEntry();
- rv.insert(end(rv),
- std::make_move_iterator(begin(txtEntry)),
- std::make_move_iterator(end(txtEntry)));
+ try {
+ auto txtEntry = entry.txtEntry();
+ rv.insert(end(rv),
+ std::make_move_iterator(begin(txtEntry)),
+ std::make_move_iterator(end(txtEntry)));
+ } catch (const ExceptionFor<ErrorCodes::DNSRecordTypeMismatch>&) {
+ }
}
+
return rv;
}
std::vector<std::string> dns::getTXTRecords(const std::string& service) try {
return lookupTXTRecords(service);
-} catch (const DBException& ex) {
- if (ex.code() == ErrorCodes::DNSHostNotFound) {
- return {};
- }
- throw;
+} catch (const ExceptionFor<ErrorCodes::DNSHostNotFound>&) {
+ return {};
}
} // namespace mongo
diff --git a/src/mongo/util/dns_query_windows-impl.h b/src/mongo/util/dns_query_windows-impl.h
index f131bd76275..c7eb5e8d29a 100644
--- a/src/mongo/util/dns_query_windows-impl.h
+++ b/src/mongo/util/dns_query_windows-impl.h
@@ -78,10 +78,11 @@ public:
*/
std::vector<std::string> txtEntry() const {
if (this->_record->wType != DNS_TYPE_TEXT) {
- std::ostringstream oss;
+ StringBuilder oss;
oss << "Incorrect record format for \"" << this->_service
- << "\": expected TXT record, found something else";
- uasserted(ErrorCodes::DNSProtocolError, oss.str());
+ << "\": expected TXT record, found a record of type " << this->_record->wType
+ << " instead";
+ uasserted(ErrorCodes::DNSRecordTypeMismatch, oss.str());
}
std::vector<std::string> rv;
@@ -97,10 +98,11 @@ public:
*/
std::string addressEntry() const {
if (this->_record->wType != DNS_TYPE_A) {
- std::ostringstream oss;
+ StringBuilder oss;
oss << "Incorrect record format for \"" << this->_service
- << "\": expected A record, found something else";
- uasserted(ErrorCodes::DNSProtocolError, oss.str());
+ << "\": expected A record, found a record of type " << this->_record->wType
+ << " instead";
+ uasserted(ErrorCodes::DNSRecordTypeMismatch, oss.str());
}
std::string rv;
@@ -121,10 +123,11 @@ public:
*/
SRVHostEntry srvHostEntry() const {
if (this->_record->wType != DNS_TYPE_SRV) {
- std::ostringstream oss;
+ StringBuilder oss;
oss << "Incorrect record format for \"" << this->_service
- << "\": expected SRV record, found something else";
- uasserted(ErrorCodes::DNSProtocolError, oss.str());
+ << "\": expected SRV record, found a record of type " << this->_record->wType
+ << " instead";
+ uasserted(ErrorCodes::DNSRecordTypeMismatch, oss.str());
}
const auto& data = this->_record->Data.SRV;