Blame SOURCES/openscap-1.3.6-PR-1806-alternative-hostname.patch

909fca
From d97687c12ba6cbca1d732534ff7394bd14547d92 Mon Sep 17 00:00:00 2001
909fca
From: =?UTF-8?q?Jan=20=C4=8Cern=C3=BD?= <jcerny@redhat.com>
909fca
Date: Fri, 10 Sep 2021 14:53:42 +0200
909fca
Subject: [PATCH] Add an alternative source of hostname
909fca
909fca
If /etc/hostname can't be read, we will try to open /proc/sys/kernel/hostname instead.
909fca
909fca
Resolves: rhbz#1977668
909fca
---
909fca
 src/XCCDF/result.c                            |  5 ++++
909fca
 tests/API/XCCDF/unittests/CMakeLists.txt      |  1 +
909fca
 .../XCCDF/unittests/test_results_hostname.sh  | 26 +++++++++++++++++++
909fca
 3 files changed, 32 insertions(+)
909fca
 create mode 100755 tests/API/XCCDF/unittests/test_results_hostname.sh
909fca
909fca
diff --git a/src/XCCDF/result.c b/src/XCCDF/result.c
909fca
index 91fcc6041d..c0ad4b926f 100644
909fca
--- a/src/XCCDF/result.c
909fca
+++ b/src/XCCDF/result.c
909fca
@@ -271,6 +271,11 @@ static char *_get_etc_hostname(const char *oscap_probe_root)
909fca
 
909fca
 	fp = oscap_fopen_with_prefix(oscap_probe_root, "/etc/hostname");
909fca
 
909fca
+	if (fp == NULL) {
909fca
+		dD("Trying to use /proc/sys/kernel/hostname instead of /etc/hostname");
909fca
+		fp = oscap_fopen_with_prefix(oscap_probe_root, "/proc/sys/kernel/hostname");
909fca
+	}
909fca
+
909fca
 	if (fp == NULL)
909fca
 		goto fail;
909fca
 
909fca
diff --git a/tests/API/XCCDF/unittests/CMakeLists.txt b/tests/API/XCCDF/unittests/CMakeLists.txt
909fca
index 52645834c4..6549538440 100644
909fca
--- a/tests/API/XCCDF/unittests/CMakeLists.txt
909fca
+++ b/tests/API/XCCDF/unittests/CMakeLists.txt
909fca
@@ -101,3 +101,4 @@ add_oscap_test("test_fix_arf.sh")
909fca
 add_oscap_test("test_fix_resultid_by_suffix.sh")
909fca
 add_oscap_test("test_generate_fix_ansible_vars.sh")
909fca
 add_oscap_test("test_xccdf_requires_conflicts.sh")
909fca
+add_oscap_test("test_results_hostname.sh")
909fca
diff --git a/tests/API/XCCDF/unittests/test_results_hostname.sh b/tests/API/XCCDF/unittests/test_results_hostname.sh
909fca
new file mode 100755
909fca
index 0000000000..c4408affbb
909fca
--- /dev/null
909fca
+++ b/tests/API/XCCDF/unittests/test_results_hostname.sh
909fca
@@ -0,0 +1,26 @@
909fca
+#!/usr/bin/env bash
909fca
+. $builddir/tests/test_common.sh
909fca
+
909fca
+set -e
909fca
+set -o pipefail
909fca
+
909fca
+result=$(mktemp)
909fca
+tmpdir=$(mktemp -d)
909fca
+
909fca
+export OSCAP_PROBE_ROOT="$tmpdir"
909fca
+
909fca
+mkdir -p "$tmpdir/etc"
909fca
+echo "hostname_defined_in_etc_hostname" > "$tmpdir/etc/hostname"
909fca
+$OSCAP xccdf eval --results "$result" "$srcdir/test_single_rule.ds.xml" || ret=$?
909fca
+assert_exists 1 '/Benchmark/TestResult/target[text()="hostname_defined_in_etc_hostname"]'
909fca
+assert_exists 0 '/Benchmark/TestResult/target[text()="hostname_defined_in_proc_sys_kernel"]'
909fca
+
909fca
+rm -rf "$tmpdir/etc/hostname"
909fca
+mkdir -p "$tmpdir/proc/sys/kernel/"
909fca
+echo "hostname_defined_in_proc_sys_kernel" > "$tmpdir/proc/sys/kernel/hostname"
909fca
+$OSCAP xccdf eval --results "$result" "$srcdir/test_single_rule.ds.xml" || ret=$?
909fca
+assert_exists 0 '/Benchmark/TestResult/target[text()="hostname_defined_in_etc_hostname"]'
909fca
+assert_exists 1 '/Benchmark/TestResult/target[text()="hostname_defined_in_proc_sys_kernel"]'
909fca
+
909fca
+rm -f "$result"
909fca
+rm -rf "$tmpdir"