diff options
author | Alex Harpin <development@landsofshadow.co.uk> | 2015-02-14 10:36:55 +0000 |
---|---|---|
committer | Daniil Baturin <daniil@baturin.org> | 2015-04-07 02:11:35 +0200 |
commit | 82d58c7b45fc113a056e42854cba26b8ecff2d45 (patch) | |
tree | 6dbacb08d561043cbe95a7c19140276a38cd6bc5 | |
parent | 2c9e68f302f3116451be72f9abf0cf1aa318899f (diff) | |
download | vyatta-cfg-system-82d58c7b45fc113a056e42854cba26b8ecff2d45.tar.gz vyatta-cfg-system-82d58c7b45fc113a056e42854cba26b8ecff2d45.zip |
vyatta-cfg-system: redesign the layout and updating of /etc/resolv.conf
Following the documentation, /etc/resolv.conf should only have comments
inserted that start with # at the beginning of a line, comments in
other locations are not supported. The exisiting method of tracking
changes in this file (done by both vyatta-system-nameservers and
vyatta_update_resolv.pl) relies on this unsupported feature.
This commit restructures the updates so they use comments on the
preceding line to track changes, along with updating the old style
comments to the new ones when the relevant scripts are called.
This also fixes a previous issue (which was reverted) where IPv6
nameservers worked incorrectly when the /etc/resolv.conf line ended
with a comment.
Bug #486 http://bugzilla.vyos.net/show_bug.cgi?id=486
-rwxr-xr-x | scripts/system/vyatta_update_resolv.pl | 74 | ||||
-rwxr-xr-x | scripts/vyatta-system-nameservers | 2 |
2 files changed, 53 insertions, 23 deletions
diff --git a/scripts/system/vyatta_update_resolv.pl b/scripts/system/vyatta_update_resolv.pl index 93e6041d..4c54cfce 100755 --- a/scripts/system/vyatta_update_resolv.pl +++ b/scripts/system/vyatta_update_resolv.pl @@ -37,7 +37,7 @@ GetOptions("dhclient-script=i" => \$dhclient_script, my $vc = new Vyatta::Config(); $vc->setLevel('system'); -my @domains; +my @search_domains; my $domain_name = undef; my $disable_dhcp_nameservers = undef; @@ -48,34 +48,36 @@ if ($config_mode == 1) { } if ($dhclient_script == 1) { - @domains = $vc->returnOrigValues('domain-search domain'); + @search_domains = $vc->returnOrigValues('domain-search domain'); $domain_name = $vc->returnOrigValue('domain-name'); } else { - @domains = $vc->returnValues('domain-search domain'); + @search_domains = $vc->returnValues('domain-search domain'); $domain_name = $vc->returnValue('domain-name'); } -if ($dhclient_script == 0 && @domains > 0 && $domain_name && length($domain_name) > 0) { - my @loc; +if ($dhclient_script == 0 && @search_domains > 0 && $domain_name && length($domain_name) > 0) { + my @location; if ($vc->returnOrigValues('domain-search domain') > 0) { - @loc = ["system","domain-name"]; + @location = ["system","domain-name"]; } else { - @loc = ["system","domain-search","domain"]; + @location = ["system","domain-search","domain"]; } - Vyatta::Config::outputError(@loc,"System configuration error. Both \'domain-name\' and \'domain-search\' are specified, but only one of these mutually exclusive parameters is allowed."); + Vyatta::Config::outputError(@location,"System configuration error. Both \'domain-name\' and \'domain-search\' are specified, but only one of these mutually exclusive parameters is allowed."); exit(1); } -my $doms = ''; -foreach my $domain (@domains) { - if (length($doms) > 0) { - $doms .= ' '; +my $search_domain_list = ''; +foreach my $domain (@search_domains) { + if (length($search_domain_list) > 0) { + $search_domain_list .= ' '; } - $doms .= $domain; + $search_domain_list .= $domain; } -# add domain names received from dhcp client to domain search in /etc/resolv.conf if domain-name not set in CLI +# add domain searches received from the dhcp client to those already in /etc/resolv.conf, as long as domain-name is not set in the CLI + +my $search_includes_dhcp = 0; if (!defined($domain_name)) { my @dhcp_interfaces_resolv_files = `ls /etc/ | grep resolv.conf.dhclient-new`; @@ -88,20 +90,27 @@ if (!defined($domain_name)) { my @dhcp_domains = split(/\s+/, $search_string, 2); my $dhcp_domain = $dhcp_domains[1]; chomp $dhcp_domain; - $doms .= ' ' . $dhcp_domain; + $search_domain_list .= ' ' . $dhcp_domain; + $search_includes_dhcp = 1; } } } } my $search = ''; -if (length($doms) > 0) { - $search = "#line generated by $0\nsearch\t\t$doms\n"; +if (length($search_domain_list) > 0) { + my $source; + if ($search_includes_dhcp == 1) { + $source = "(system + dhcp)"; + } else { + $source = "(system)"; + } + $search = "#line generated by vyatta_update_resolv.pl $source\nsearch\t\t$search_domain_list\n"; } my $domain = ''; if ($domain_name && length($domain_name) > 0) { - $domain = "#line generated by $0\ndomain\t\t$domain_name\n"; + $domain = "#line generated by vyatta_update_resolv.pl (system)\ndomain\t\t$domain_name\n"; } # update /etc/resolv.conf with name-servers received from dhcp client, done when this script is called @@ -143,7 +152,7 @@ if (($dhclient_script == 1) || ($config_mode == 1)) { if (($ns_in_resolvconf == 0) && !($disable_dhcp_nameservers)) { open (my $rf, '>>', '/etc/resolv.conf') or die "$! error trying to overwrite"; - print $rf "nameserver\t$ns\t\t#nameserver written by $0\n"; + print $rf "#nameserver written by vyatta_update_resolv.pl (dhcp)\nnameserver\t$ns\n"; close $rf; $restart_ntp = 1; } @@ -156,7 +165,16 @@ if (($dhclient_script == 1) || ($config_mode == 1)) { # from dhcp-server, or to remove previous dhcp supplied name-servers if disable-dhcp-nameservers has # been enabled. - my @nameservers_dhcp_in_resolvconf = `grep 'nameserver written' /etc/resolv.conf`; + my $found_old = 0; + + my @nameservers_dhcp_in_resolvconf = `sed -n '/nameserver written/{n;P;}' /etc/resolv.conf`; + for my $old_style_nameservers (@nameservers_dhcp_in_resolvconf) { + if ($old_style_nameservers =~ /#line generated by/){ + @nameservers_dhcp_in_resolvconf = `sed -n '/nameserver written/{P;}' /etc/resolv.conf`; + $found_old = 1; + last; + } + } my @dhcp_nameservers_in_resolvconf; my $count_nameservers_in_resolvconf = 0; for my $count_dhcp_nameserver (@nameservers_dhcp_in_resolvconf) { @@ -166,20 +184,32 @@ if (($dhclient_script == 1) || ($config_mode == 1)) { } if (($#current_dhcp_nameservers < 0) || ($disable_dhcp_nameservers)) { for my $dhcpnameserver (@dhcp_nameservers_in_resolvconf) { - my $cmd = "sed -i '/$dhcpnameserver\t/d' /etc/resolv.conf"; + my $cmd; + chomp $dhcpnameserver; + if ($found_old == 1) { + $cmd = "sed -i '/nameserver\t$dhcpnameserver/d' /etc/resolv.conf"; + } else { + $cmd = "sed -i -n '/nameserver\t$dhcpnameserver/".'{n;x;d;};x;1d;p;${x;p;}'."' /etc/resolv.conf"; + } system($cmd); $restart_ntp = 1; } } else { for my $dhcpnameserver (@dhcp_nameservers_in_resolvconf) { my $found = 0; + my $cmd; + chomp $dhcpnameserver; for my $currentnameserver (@current_dhcp_nameservers) { if ($dhcpnameserver eq $currentnameserver){ $found = 1; } } if ($found == 0) { - my $cmd = "sed -i '/$dhcpnameserver\t/d' /etc/resolv.conf"; + if ($found_old == 1) { + $cmd = "sed -i '/nameserver\t$dhcpnameserver/d' /etc/resolv.conf"; + } else { + $cmd = "sed -i -n '/nameserver\t$dhcpnameserver/".'{n;x;d;};x;1d;p;${x;p;}'."' /etc/resolv.conf"; + } system($cmd); $restart_ntp = 1; } diff --git a/scripts/vyatta-system-nameservers b/scripts/vyatta-system-nameservers index 74f2c8b6..7b66cf77 100755 --- a/scripts/vyatta-system-nameservers +++ b/scripts/vyatta-system-nameservers @@ -53,7 +53,7 @@ update_system_nameservers () { # find last instance of cli inserted nameserver # insert currently received nameserver immediately after that # this is done to keep system set nameservers priority over dhcp received nameservers - cli_ns_array=($(awk '{if (!$3) print $2}' /etc/resolv.conf)) + cli_ns_array=($(awk '{if (/(dhcp)/) exit 0; if (!$3) print $2}' /etc/resolv.conf)) cli_ns_array_len=${#cli_ns_array[*]} line_num=0 if [ $cli_ns_array_len -gt 0 ]; then |