From c4e926d40a6dbc2bb0b24d01ab748bedac58fb35 Mon Sep 17 00:00:00 2001 From: Alex Harpin Date: Sun, 7 Sep 2014 19:42:34 +0100 Subject: vyatta-cfg-system: formatting changes for style consistency Update scripts/system/vyatta_update_resolv.pl to have consistent identation levels and style throughout. --- scripts/system/vyatta_update_resolv.pl | 230 ++++++++++++++++----------------- 1 file changed, 113 insertions(+), 117 deletions(-) (limited to 'scripts/system') diff --git a/scripts/system/vyatta_update_resolv.pl b/scripts/system/vyatta_update_resolv.pl index b311bff5..d200e0cc 100755 --- a/scripts/system/vyatta_update_resolv.pl +++ b/scripts/system/vyatta_update_resolv.pl @@ -26,7 +26,6 @@ use strict; use lib "/opt/vyatta/share/perl5/"; - use Getopt::Long; my $dhclient_script = 0; GetOptions("dhclient-script=i" => \$dhclient_script); @@ -39,20 +38,20 @@ my @domains; my $domain_name = undef; if ($dhclient_script == 1) { - @domains = $vc->returnOrigValues('domain-search domain'); - $domain_name = $vc->returnOrigValue('domain-name'); + @domains = $vc->returnOrigValues('domain-search domain'); + $domain_name = $vc->returnOrigValue('domain-name'); } else { - @domains = $vc->returnValues('domain-search domain'); - $domain_name = $vc->returnValue('domain-name'); + @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 ($vc->returnOrigValues('domain-search domain') > 0) { - @loc = ["system","domain-name"]; + @loc = ["system","domain-name"]; } else { - @loc = ["system","domain-search","domain"]; + @loc = ["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."); exit(1); @@ -60,126 +59,124 @@ if ($dhclient_script == 0 && @domains > 0 && $domain_name && length($domain_name my $doms = ''; foreach my $domain (@domains) { - if (length($doms) > 0) { - $doms .= ' '; - } - $doms .= $domain; + if (length($doms) > 0) { + $doms .= ' '; + } + $doms .= $domain; } # add domain names received from dhcp client to domain search in /etc/resolv.conf if domain-name not set in CLI if (!defined($domain_name)) { - my @dhcp_interfaces_resolv_files = `ls /etc/ | grep resolv.conf.dhclient-new`; - if ($#dhcp_interfaces_resolv_files >= 0) { - for my $each_file (@dhcp_interfaces_resolv_files) { - chomp $each_file; - my $find_search = `grep "^search" /etc/$each_file 2> /dev/null | wc -l`; - if ($find_search == 1) { - my $search_string = `grep "^search" /etc/$each_file`; - my @dhcp_domains = split(/\s+/, $search_string, 2); - my $dhcp_domain = $dhcp_domains[1]; - chomp $dhcp_domain; - $doms .= ' ' . $dhcp_domain; - } + my @dhcp_interfaces_resolv_files = `ls /etc/ | grep resolv.conf.dhclient-new`; + if ($#dhcp_interfaces_resolv_files >= 0) { + for my $each_file (@dhcp_interfaces_resolv_files) { + chomp $each_file; + my $find_search = `grep "^search" /etc/$each_file 2> /dev/null | wc -l`; + if ($find_search == 1) { + my $search_string = `grep "^search" /etc/$each_file`; + my @dhcp_domains = split(/\s+/, $search_string, 2); + my $dhcp_domain = $dhcp_domains[1]; + chomp $dhcp_domain; + $doms .= ' ' . $dhcp_domain; + } + } } - } } my $search = ''; if (length($doms) > 0) { - $search = "search\t\t$doms\t\t#line generated by $0\n"; + $search = "search\t\t$doms\t\t#line generated by $0\n"; } my $domain = ''; if ($domain_name && length($domain_name) > 0) { - $domain = "domain\t\t$domain_name\t\t#line generated by $0\n"; + $domain = "domain\t\t$domain_name\t\t#line generated by $0\n"; } # update /etc/resolv.conf for name-servers received from dhcp client, only done when dhclient-script calls this script if ($dhclient_script == 1) { - my @current_dhcp_nameservers; - my $restart_ntp = 0; - - # code below to add new name-servers received from dhcp client - - my @dhcp_interfaces_resolv_files = `ls /etc/ | grep resolv.conf.dhclient-new`; - if ($#dhcp_interfaces_resolv_files >= 0) { - my $ns_count = 0; - for my $each_file (@dhcp_interfaces_resolv_files) { - chomp $each_file; - my $find_nameserver = `grep nameserver /etc/$each_file 2> /dev/null | wc -l`; - if ($find_nameserver > 0) { - my @nameservers = `grep nameserver /etc/$each_file`; - for my $each_nameserver (@nameservers) { - my @nameserver = split(/ /, $each_nameserver, 2); - my $ns = $nameserver[1]; - chomp $ns; - $current_dhcp_nameservers[$ns_count] = $ns; - $ns_count++; - my @search_ns_in_resolvconf = `grep $ns /etc/resolv.conf`; - my $ns_in_resolvconf = 0; - if (@search_ns_in_resolvconf > 0) { - foreach my $ns_resolvconf (@search_ns_in_resolvconf) { - my @resolv_ns = split(/\s+/, $ns_resolvconf); - my $final_ns = $resolv_ns[1]; - chomp $final_ns; - if ($final_ns eq $ns) { - $ns_in_resolvconf = 1; - } - } - } - if ($ns_in_resolvconf == 0) { - 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\t$ns\n"; - close $rf; - $restart_ntp = 1; - } + my @current_dhcp_nameservers; + my $restart_ntp = 0; + + # code below to add new name-servers received from dhcp client + + my @dhcp_interfaces_resolv_files = `ls /etc/ | grep resolv.conf.dhclient-new`; + if ($#dhcp_interfaces_resolv_files >= 0) { + my $ns_count = 0; + for my $each_file (@dhcp_interfaces_resolv_files) { + chomp $each_file; + my $find_nameserver = `grep nameserver /etc/$each_file 2> /dev/null | wc -l`; + if ($find_nameserver > 0) { + my @nameservers = `grep nameserver /etc/$each_file`; + for my $each_nameserver (@nameservers) { + my @nameserver = split(/ /, $each_nameserver, 2); + my $ns = $nameserver[1]; + chomp $ns; + $current_dhcp_nameservers[$ns_count] = $ns; + $ns_count++; + my @search_ns_in_resolvconf = `grep $ns /etc/resolv.conf`; + my $ns_in_resolvconf = 0; + if (@search_ns_in_resolvconf > 0) { + foreach my $ns_resolvconf (@search_ns_in_resolvconf) { + my @resolv_ns = split(/\s+/, $ns_resolvconf); + my $final_ns = $resolv_ns[1]; + chomp $final_ns; + if ($final_ns eq $ns) { + $ns_in_resolvconf = 1; + } + } + } + if ($ns_in_resolvconf == 0) { + 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\t$ns\n"; + close $rf; + $restart_ntp = 1; + } + } } - } + } } - } - - # code below to remove old name-servers from /etc/resolv.conf that were not received in this response from dhcp-server - - my @nameservers_dhcp_in_resolvconf = `grep 'nameserver written' /etc/resolv.conf`; - my @dhcp_nameservers_in_resolvconf; - my $count_nameservers_in_resolvconf = 0; - for my $count_dhcp_nameserver (@nameservers_dhcp_in_resolvconf) { - my @dhcp_nameserver = split(/\t/, $count_dhcp_nameserver, 3); - $dhcp_nameservers_in_resolvconf[$count_nameservers_in_resolvconf] = $dhcp_nameserver[1]; - $count_nameservers_in_resolvconf++; - } - if ($#current_dhcp_nameservers < 0) { - for my $dhcpnameserver (@dhcp_nameservers_in_resolvconf) { - my $cmd = "sed -i '/$dhcpnameserver\t/d' /etc/resolv.conf"; - system($cmd); - $restart_ntp = 1; + + # code below to remove old name-servers from /etc/resolv.conf that were not received in this response from dhcp-server + + my @nameservers_dhcp_in_resolvconf = `grep 'nameserver written' /etc/resolv.conf`; + my @dhcp_nameservers_in_resolvconf; + my $count_nameservers_in_resolvconf = 0; + for my $count_dhcp_nameserver (@nameservers_dhcp_in_resolvconf) { + my @dhcp_nameserver = split(/\t/, $count_dhcp_nameserver, 3); + $dhcp_nameservers_in_resolvconf[$count_nameservers_in_resolvconf] = $dhcp_nameserver[1]; + $count_nameservers_in_resolvconf++; } - } else { - for my $dhcpnameserver (@dhcp_nameservers_in_resolvconf) { + if ($#current_dhcp_nameservers < 0) { + for my $dhcpnameserver (@dhcp_nameservers_in_resolvconf) { + my $cmd = "sed -i '/$dhcpnameserver\t/d' /etc/resolv.conf"; + system($cmd); + $restart_ntp = 1; + } + } else { + for my $dhcpnameserver (@dhcp_nameservers_in_resolvconf) { my $found = 0; for my $currentnameserver (@current_dhcp_nameservers) { - if ($dhcpnameserver eq $currentnameserver){ - $found = 1; - } + if ($dhcpnameserver eq $currentnameserver){ + $found = 1; + } } if ($found == 0) { - my $cmd = "sed -i '/$dhcpnameserver\t/d' /etc/resolv.conf"; - system($cmd); - $restart_ntp = 1; + my $cmd = "sed -i '/$dhcpnameserver\t/d' /etc/resolv.conf"; + system($cmd); + $restart_ntp = 1; } - } - } - if ($restart_ntp == 1) { - # this corresponds to what is done in name-server/node.def as a fix for bug 1300 - my $cmd_ntp_restart = "if [ -f /etc/ntp.conf ] && grep -q '^server' /etc/ntp.conf; then /usr/sbin/invoke-rc.d ntp restart >&/dev/null; fi &"; - system($cmd_ntp_restart); - } + } + if ($restart_ntp == 1) { + # this corresponds to what is done in name-server/node.def as a fix for bug 1300 + my $cmd_ntp_restart = "if [ -f /etc/ntp.conf ] && grep -q '^server' /etc/ntp.conf; then /usr/sbin/invoke-rc.d ntp restart >&/dev/null; fi &"; + system($cmd_ntp_restart); + } } - # The following will re-write '/etc/resolv.conf' line by line, # replacing the 'search' specifier with the latest values, # or replacing the 'domain' specifier with the latest value. @@ -187,12 +184,11 @@ if ($dhclient_script == 1) { my @resolv; if (-e '/etc/resolv.conf') { open (my $f, '<', '/etc/resolv.conf') - or die("$0: Error! Unable to open '/etc/resolv.conf' for input: $!\n"); + or die("$0: Error! Unable to open '/etc/resolv.conf' for input: $!\n"); @resolv = <$f>; close ($f); } - my $foundSearch = 0; my $foundDomain = 0; @@ -200,26 +196,26 @@ open (my $r, '>', '/etc/resolv.conf') or die("$0: Error! Unable to open '/etc/resolv.conf' for output: $!\n"); foreach my $line (@resolv) { - if ($line =~ /^search\s/) { - $foundSearch = 1; - if (length($search) > 0) { - print $r $search; - } - } elsif ($line =~ /^domain\s/) { - $foundDomain = 1; - if (length($domain) > 0) { - print $r $domain; - } - } else { - print $r $line; - } + if ($line =~ /^search\s/) { + $foundSearch = 1; + if (length($search) > 0) { + print $r $search; + } + } elsif ($line =~ /^domain\s/) { + $foundDomain = 1; + if (length($domain) > 0) { + print $r $domain; + } + } else { + print $r $line; + } } + if ($foundSearch == 0 && length($search) > 0) { - print $r $search; + print $r $search; } if ($foundDomain == 0 && length($domain) > 0) { - print $r $domain; + print $r $domain; } close ($r); - -- cgit v1.2.3 From 1a2d4456587604730cb29bf635253941a50669ab Mon Sep 17 00:00:00 2001 From: Alex Harpin Date: Sun, 7 Sep 2014 19:59:29 +0100 Subject: vyatta-cfg-system: prevent dhcp updating dns when statically configured When DNS settings are statically configured, prevent DHCP interfaces from appending to resolv.conf with additional name servers. If the system has been configured with a static name server, just use that instead of accepting ISP provided name servers. These can be accepted again by simply deleting the statically configured name servers. Bug #182 http://bugzilla.vyos.net/show_bug.cgi?id=182 --- scripts/system/vyatta_update_resolv.pl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'scripts/system') diff --git a/scripts/system/vyatta_update_resolv.pl b/scripts/system/vyatta_update_resolv.pl index d200e0cc..d20eb848 100755 --- a/scripts/system/vyatta_update_resolv.pl +++ b/scripts/system/vyatta_update_resolv.pl @@ -94,7 +94,8 @@ if ($domain_name && length($domain_name) > 0) { } # update /etc/resolv.conf for name-servers received from dhcp client, only done when dhclient-script calls this script -if ($dhclient_script == 1) { +# and there aren't statically configured DNS settings, via 'set system name-server', in place. +if (($dhclient_script == 1) && !($vc->existsOrig('name-server'))) { my @current_dhcp_nameservers; my $restart_ntp = 0; -- cgit v1.2.3