icingaweb2-module-director icon indicating copy to clipboard operation
icingaweb2-module-director copied to clipboard

Get host by name import modifier broken when lookup fails

Open rhld-linux opened this issue 1 year ago • 0 comments

Expected Behavior

the "Get host by name (DNS lookup)" modifer in an import source shall obey the "On failure" setting and in our case keep the hostname given even if ( or espacially when ) the named can't be looked up

Current Behavior

import preview shows an error; import fails

inet_pton(): Unrecognized address OUR-HOST-NAME-HERE (PropertyModifierGetHostByName.php:37)
#0 [internal function]: Icinga\Application\ApplicationBootstrap->Icinga\Application\{closure}(2, 'inet_pton(): Un...', '/usr/share/icin...', 37, Array)
#1 /usr/share/icingaweb2/modules/director/library/Director/PropertyModifier/PropertyModifierGetHostByName.php(37): inet_pton('OUR-HOST-NAME-HERE')
#2 /usr/share/icingaweb2/modules/director/library/Director/Objects/ImportSource.php(338): Icinga\Module\Director\PropertyModifier\PropertyModifierGetHostByName->transform('OUR-HOST-NAME-HERE')
#3 /usr/share/icingaweb2/modules/director/library/Director/Objects/ImportSource.php(265): Icinga\Module\Director\Objects\ImportSource->applyPropertyModifierToRow(Object(Icinga\Module\Director\PropertyModifier\PropertyModifierGetHostByName), 'object_name', Object(stdClass))
#4 /usr/share/icingaweb2/modules/director/library/Director/Web/Table/ImportsourceHookTable.php(102): Icinga\Module\Director\Objects\ImportSource->applyModifiers(Array)
#5 /usr/share/icingaweb2/modules/incubator/vendor/gipfl/icingaweb2/src/Table/SimpleQueryBasedTable.php(29): Icinga\Module\Director\Web\Table\ImportsourceHookTable->prepareQuery()
#6 /usr/share/icingaweb2/modules/incubator/vendor/gipfl/icingaweb2/src/Table/SimpleQueryBasedTable.php(15): gipfl\IcingaWeb2\Table\SimpleQueryBasedTable->getQuery()
#7 /usr/share/icingaweb2/modules/incubator/vendor/gipfl/icingaweb2/src/Table/QueryBasedTable.php(59): gipfl\IcingaWeb2\Table\SimpleQueryBasedTable->getPaginationAdapter()
#8 /usr/share/icingaweb2/modules/incubator/vendor/gipfl/icingaweb2/src/Table/QueryBasedTable.php(258): gipfl\IcingaWeb2\Table\QueryBasedTable->getPaginator(Object(gipfl\IcingaWeb2\Url))
#9 /usr/share/icingaweb2/modules/director/application/controllers/ImportsourceController.php(185): gipfl\IcingaWeb2\Table\QueryBasedTable->renderTo(Object(Icinga\Module\Director\Controllers\ImportsourceController))
#10 /usr/share/icinga-php/vendor/vendor/shardj/zf1-future/library/Zend/Controller/Action.php(516): Icinga\Module\Director\Controllers\ImportsourceController->previewAction()
#11 /usr/share/php/Icinga/Web/Controller/Dispatcher.php(76): Zend_Controller_Action->dispatch('previewAction')
#12 /usr/share/icinga-php/vendor/vendor/shardj/zf1-future/library/Zend/Controller/Front.php(954): Icinga\Web\Controller\Dispatcher->dispatch(Object(Icinga\Web\Request), Object(Icinga\Web\Response))
#13 /usr/share/php/Icinga/Application/Web.php(294): Zend_Controller_Front->dispatch(Object(Icinga\Web\Request), Object(Icinga\Web\Response))
#14 /usr/share/php/Icinga/Application/webrouter.php(105): Icinga\Application\Web->dispatch()
#15 /usr/share/icingaweb2/public/index.php(4): require_once('/usr/share/php/...')
#16 {main}

Possible Solution

as the inet_pton fails because gethostbyname returns the name it was called to look up when no record exists which then fails our workaround is to edit library/Director/PropertyModifier/PropertyModifierGetHostByName.php like this

        $host = gethostbyname($value);
+	if ($host !== $value) {
+		$inAddr = inet_pton($host);
-	$inAddr = inet_pton($host);
+	} else {
+		$inAddr = "$host";
+	}
        if (! $inAddr || strlen($inAddr) !== 4) {

so the inet_pton function is only called when the gethostbyname did a resolution

Steps to Reproduce (for bugs)

  • create an import source in director which import some kind of hosts
  • add a modifier to have one fields content be resolved to an IP address
  • make sure not all field values actually have a DNS A record set up

Your Environment

  • Director version (System - About): 1.11.1
  • Icinga Web 2 version and modules (System - About): 2.12.1
  • Icinga 2 version (icinga2 --version): 2.14.2-1
  • Operating System and version:
  • Webserver, PHP versions: apache-2.4.37-62.0.1, php-7.2.24-1

rhld-linux avatar Mar 22 '24 12:03 rhld-linux