From a8f61ee95daea74a70b7281cd518703225cba556 Mon Sep 17 00:00:00 2001 From: Thomas Markwalder Date: Fri, 30 Jul 2021 11:09:40 -0400 Subject: [#1529] Avoid duplicating qualifying suffix src/lib/dhcpsrv/d2_client_mgr.* D2ClientMgr::qualifyName() - modified to only add the qualifying suffix if the input name does not already end with it. src/lib/dhcpsrv/tests/d2_client_unittest.cc TEST_F(D2ClientMgrParamsTest, qualifyNameWithoutDuplicatingSuffix) - new test --- src/lib/dhcpsrv/d2_client_mgr.cc | 43 +++++++++++++++++++++++++++++++++++----- 1 file changed, 38 insertions(+), 5 deletions(-) (limited to 'src/lib/dhcpsrv/d2_client_mgr.cc') diff --git a/src/lib/dhcpsrv/d2_client_mgr.cc b/src/lib/dhcpsrv/d2_client_mgr.cc index 6671471309..3ba12d172c 100644 --- a/src/lib/dhcpsrv/d2_client_mgr.cc +++ b/src/lib/dhcpsrv/d2_client_mgr.cc @@ -1,4 +1,4 @@ -// Copyright (C) 2014-2020 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2014-2021 Internet Systems Consortium, Inc. ("ISC") // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this @@ -186,14 +186,47 @@ D2ClientMgr::qualifyName(const std::string& partial_name, gen_name << partial_name; std::string suffix = ddns_params.getQualifyingSuffix(); + bool suffix_present = true; if (!suffix.empty()) { std::string str = gen_name.str(); - size_t len = str.length(); - if ((len > 0) && (str[len - 1] != '.')) { - gen_name << "."; + auto suffix_rit = suffix.rbegin(); + if (*suffix_rit == '.') { + ++suffix_rit; + } + + auto gen_rit = str.rbegin(); + if (*gen_rit == '.') { + ++gen_rit; } - gen_name << suffix; + while (suffix_rit != suffix.rend()) { + if ((gen_rit == str.rend()) || + (*suffix_rit != *gen_rit)) { + // They don't match. + suffix_present = false; + break; + } + + ++suffix_rit; + ++gen_rit; + } + + // Catch the case where name has suffix embedded. + // input: foo.barexample.com suffix: example.com + if ((suffix_present) && (suffix_rit == suffix.rend())) { + if (*gen_rit != '.') { + suffix_present = false; + } + } + + if (!suffix_present) { + size_t len = str.length(); + if ((len > 0) && (str[len - 1] != '.')) { + gen_name << "."; + } + + gen_name << suffix; + } } std::string str = gen_name.str(); -- cgit v1.2.3