From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gate001.proxmox.com (gate001.proxmox.com [IPv6:2a0f:8001:1:32::40]) by lore.proxmox.com (Postfix) with ESMTPS id D0DC91FF0BC for ; Mon, 16 Nov 2026 14:31:35 +0100 (CET) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id 84E842171E; Mon, 16 Nov 2026 14:31:31 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1794835880; x=1795440680; darn=lists.proxmox.com; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=67088/jDvDAF/kBMWM0hel3VML8q5cMVGx2DF8w6TPU=; b=QOliebt972UNcXbRwTEU0YMDMcus5FQrFmzghbFgL7N6q0aDoXzTORL7Lengy+PL+t ho6Ob6kwKUA3DWITCBJBOCuJYpz01XIKQ2jy0lYezrhbfqJEf932csfSEZZXOiV8BH26 V3V2t9OUHQJ+sowfgdiCefSpq6+gVn4dDX8zNEogP3z/qjFUvcAUaDsZPyP4NhtpHjJW j8P8jTy6EXI1FgpBVaoCIHRZ3taasv85LjUQeAoC2RzjyTMlZtT1xgR7a/jsquNd6eU0 m6MyW9wsG2EgcIL1ObzUYnO0BT4M9x+AA0/t2HUU1Yx9LpTSmB3zyJANiEmmR0Ru0CiX L8lw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1794835880; x=1795440680; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=67088/jDvDAF/kBMWM0hel3VML8q5cMVGx2DF8w6TPU=; b=wyJbzm/6mkNNqfUkt9vU/u2o50s60zKtBkTSUR7CBsU1FZTtYlAb2G5xqcv8KEQz9N dbyrvuRSttCje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6DTGRzwxX932inNZKb/WgBstvcZAGAAGksaPV+WU/PT3jOt X-Received: by 2002:a05:690c:c188:b0:8b4:e9e6:462e with SMTP id 00721157ae682-8b4e9e64f5bmr1539467b3.5.1794835879361; Mon, 16 Nov 2026 05:31:19 -0800 (PST) From: Lucas Barros To: pve-devel@lists.proxmox.com Subject: [PATCH common v3 1/2] inotify: resolv.conf: add reader that keeps the whole search list Date: Mon, 16 Nov 2026 10:31:07 -0300 Message-ID: <20261116133000.6357-1-lucasbarros.kam@gmail.com> X-Mailer: git-send-email 2.50.1 In-Reply-To: <20261116133000.6357-0-lucasbarros.kam@gmail.com> References: <20261116133000.6357-0-lucasbarros.kam@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL 0.050 Adjusted score from AWL reputation of From: address DKIM_SIGNED 0.1 Message has a DKIM or DK signature, not necessarily valid DKIM_VALID -0.1 Message has at least one valid DKIM or DK signature DKIM_VALID_AU -0.1 Message has a valid DKIM or DK signature from author's domain DKIM_VALID_EF -0.1 Message has a valid DKIM or DK signature from envelope-from domain DMARC_PASS -0.1 DMARC pass policy FREEMAIL_FROM 0.001 Sender email is commonly abused enduser mail provider RCVD_IN_DNSWL_NONE -0.0001 Sender listed at https://www.dnswl.org/, no trust SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Message-ID-Hash: 5VUM2HE6WZT3E3BJICIL6S2KYQ6JCOGG X-Message-ID-Hash: 5VUM2HE6WZT3E3BJICIL6S2KYQ6JCOGG X-MailFrom: lucasbarros.kam@gmail.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: t.lamprecht@proxmox.com, Lucas Barros X-Mailman-Version: 3.3.10 Precedence: list List-Id: Proxmox VE development discussion List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: The 'resolvconf' parser only keeps the first domain of the 'search' directive. Some users rely on that, for example to build the FQDN of the node certificate, both in PVE and PMG, so it stays as it is. Add read_resolv_conf_with_search_list(), which returns all domains of the 'search' directive, separated by a single space, for the node DNS API. As resolv.conf(5) documents that the obsolete 'domain' directive only takes one domain, only its first word is used in both readers. Add a test for both readers. Signed-off-by: Lucas Barros --- changes since v2: * keep read_file('resolvconf') as it was and add a new reader for the node DNS API, as PMG uses the old one too (node certificate, get_host_dns_info(), pmg-smtp-filter) * only use the first word of the obsolete 'domain' directive, as documented in resolv.conf(5), with a regression test for it This patch was prepared with the help of an LLM based coding assistant; I reviewed the change and ran the tests from the cover letter myself. src/PVE/INotify.pm | 29 ++++++++++-- test/Makefile | 1 + test/resolv_conf_test.pl | 97 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 124 insertions(+), 3 deletions(-) create mode 100755 test/resolv_conf_test.pl diff --git a/src/PVE/INotify.pm b/src/PVE/INotify.pm index 53b4e21..ea9b069 100644 --- a/src/PVE/INotify.pm +++ b/src/PVE/INotify.pm @@ -601,15 +601,17 @@ sub write_etc_hosts { register_file('etchosts', "/etc/hosts", \&read_etc_hosts, \&write_etc_hosts); -sub read_etc_resolv_conf { - my ($filename, $fh) = @_; +my sub parse_resolv_conf { + my ($fh, $all_search_domains) = @_; my $res = {}; my $nscount = 0; while (my $line = <$fh>) { chomp $line; - if ($line =~ m/^(search|domain)\s+(\S+)\s*/) { + if ($all_search_domains && $line =~ m/^search\s+(\S.*)$/) { + $res->{search} = join(' ', split(/\s+/, $1)); + } elsif ($line =~ m/^(search|domain)\s+(\S+)\s*/) { $res->{search} = $2; } elsif ($line =~ m/^\s*nameserver\s+($PVE::Tools::IPRE)\s*/) { $nscount++; @@ -622,6 +624,27 @@ sub read_etc_resolv_conf { return $res; } +# only returns the first search domain, as callers use it to build an FQDN +sub read_etc_resolv_conf { + my ($filename, $fh) = @_; + + return parse_resolv_conf($fh, 0); +} + +# like read_file('resolvconf'), but returns all domains of a 'search' directive, separated by a +# single space. the obsolete 'domain' directive only takes one domain, see resolv.conf(5). +sub read_resolv_conf_with_search_list { + my ($filename) = @_; + + $filename //= '/etc/resolv.conf'; + + open(my $fh, '<', $filename) or return undef; + my $res = parse_resolv_conf($fh, 1); + close($fh); + + return $res; +} + sub update_etc_resolv_conf { my ($filename, $fh, $resolv, @args) = @_; diff --git a/test/Makefile b/test/Makefile index 9caba10..416bc0c 100644 --- a/test/Makefile +++ b/test/Makefile @@ -19,6 +19,7 @@ TESTS = lock_file.test \ ticket_test.test \ check_pem_test.test \ split_pem_test.test \ + resolv_conf_test.test \ all: diff --git a/test/resolv_conf_test.pl b/test/resolv_conf_test.pl new file mode 100755 index 0000000..00b7f68 --- /dev/null +++ b/test/resolv_conf_test.pl @@ -0,0 +1,97 @@ +#!/usr/bin/perl + +use v5.36; + +use lib '../src'; + +use File::Temp; +use Test::More; + +use PVE::INotify; + +my $tests = [ + { + name => 'single search domain', + input => "search example.com\nnameserver 192.0.2.1\n", + first => { search => 'example.com', dns1 => '192.0.2.1' }, + list => { search => 'example.com', dns1 => '192.0.2.1' }, + }, + { + name => 'multiple search domains', + input => "search a.example b.example c.example\nnameserver 192.0.2.1\n", + first => { search => 'a.example', dns1 => '192.0.2.1' }, + list => { search => 'a.example b.example c.example', dns1 => '192.0.2.1' }, + }, + { + name => 'multiple search domains with extra whitespace', + input => "search\ta.example b.example \t\nnameserver 192.0.2.1\n", + first => { search => 'a.example', dns1 => '192.0.2.1' }, + list => { search => 'a.example b.example', dns1 => '192.0.2.1' }, + }, + { + name => 'domain directive', + input => "domain example.com\nnameserver 192.0.2.1\n", + first => { search => 'example.com', dns1 => '192.0.2.1' }, + list => { search => 'example.com', dns1 => '192.0.2.1' }, + }, + { + name => 'domain directive with trailing words', + input => "domain a.example b.example\nnameserver 192.0.2.1\n", + first => { search => 'a.example', dns1 => '192.0.2.1' }, + list => { search => 'a.example', dns1 => '192.0.2.1' }, + }, + { + name => 'last of search and domain wins', + input => "search a.example b.example\ndomain c.example d.example\n", + first => { search => 'c.example' }, + list => { search => 'c.example' }, + }, + { + name => 'only the first three name servers', + input => "nameserver 192.0.2.1\nnameserver 192.0.2.2\n" + . "nameserver 2001:db8::1\nnameserver 192.0.2.4\n", + first => { dns1 => '192.0.2.1', dns2 => '192.0.2.2', dns3 => '2001:db8::1' }, + list => { dns1 => '192.0.2.1', dns2 => '192.0.2.2', dns3 => '2001:db8::1' }, + }, +]; + +for my $test ($tests->@*) { + open(my $fh, '<', \$test->{input}) or die "failed to open in-memory file - $!\n"; + my $res = PVE::INotify::read_etc_resolv_conf('/etc/resolv.conf', $fh); + is_deeply($res, $test->{first}, "first search domain: $test->{name}"); + + my $tmp = File::Temp->new(); + print $tmp $test->{input}; + close($tmp); + $res = PVE::INotify::read_resolv_conf_with_search_list($tmp->filename); + is_deeply($res, $test->{list}, "search list: $test->{name}"); +} + +is( + PVE::INotify::read_resolv_conf_with_search_list('/nonexistent/resolv.conf'), + undef, + 'search list: missing file', +); + +{ + my $input = "search old.example\nnameserver 192.0.2.1\noptions rotate\n"; + open(my $fh, '<', \$input) or die "failed to open in-memory file - $!\n"; + my $data = PVE::INotify::update_etc_resolv_conf( + '/etc/resolv.conf', + $fh, + { search => 'a.example b.example', dns1 => '192.0.2.2' }, + ); + is( + $data, + "search a.example b.example\nnameserver 192.0.2.2\noptions rotate\n", + 'update: write multiple search domains', + ); + + my $tmp = File::Temp->new(); + print $tmp $data; + close($tmp); + my $res = PVE::INotify::read_resolv_conf_with_search_list($tmp->filename); + is($res->{search}, 'a.example b.example', 'update: read back multiple search domains'); +} + +done_testing(); -- 2.50.1 (Apple Git-155)