summaryrefslogtreecommitdiff
path: root/scripts/system/vyatta_update_resolv.pl
diff options
context:
space:
mode:
authorDaniil Baturin <daniil@baturin.org>2015-02-15 05:20:44 +0100
committerDaniil Baturin <daniil@baturin.org>2015-02-15 05:20:44 +0100
commit13d64226876ea325b8f32d410452ded6ad65e529 (patch)
tree62eb2b368deec5f8b45cdb0207e92f6a4abbf711 /scripts/system/vyatta_update_resolv.pl
parente8f156f129057427a59d162a11c414525f6495ac (diff)
parentddf9a933451f639edc15d669c0c32b15cc0a2717 (diff)
downloadvyatta-cfg-system-13d64226876ea325b8f32d410452ded6ad65e529.tar.gz
vyatta-cfg-system-13d64226876ea325b8f32d410452ded6ad65e529.zip
Merge branch 'lithium' of github.com:vyos/vyatta-cfg-system into lithium
Diffstat (limited to 'scripts/system/vyatta_update_resolv.pl')
-rwxr-xr-xscripts/system/vyatta_update_resolv.pl86
1 files changed, 63 insertions, 23 deletions
diff --git a/scripts/system/vyatta_update_resolv.pl b/scripts/system/vyatta_update_resolv.pl
index 921052f0..0dfa4bec 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,39 @@ 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 = '';
+my $search_includes_system = 0;
+
+foreach my $domain (@search_domains) {
+ if (length($search_domain_list) > 0) {
+ $search_domain_list .= ' ';
}
- $doms .= $domain;
+ $search_domain_list .= $domain;
+ $search_includes_system = 1;
}
-# 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 +93,34 @@ if (!defined($domain_name)) {
my @dhcp_domains = split(/\s+/, $search_string, 2);
my $dhcp_domain = $dhcp_domains[1];
chomp $dhcp_domain;
- $doms .= ' ' . $dhcp_domain;
+ if (length($search_domain_list) > 0) {
+ $search_domain_list .= ' ';
+ }
+ $search_domain_list .= $dhcp_domain;
+ $search_includes_dhcp = 1;
}
}
}
}
my $search = '';
-if (length($doms) > 0) {
- $search = "search\t\t$doms\t\t#line generated by $0\n";
+if (length($search_domain_list) > 0) {
+ my $source;
+ if ($search_includes_system == 1) {
+ if ($search_includes_dhcp == 1) {
+ $source = "(system + dhcp)";
+ } else {
+ $source = "(system)";
+ }
+ } else {
+ $source = "(dhcp)";
+ }
+ $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 = "domain\t\t$domain_name\t\t#line generated by $0\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 +162,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 +175,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 +194,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;
}
@@ -221,7 +261,7 @@ foreach my $line (@resolv) {
if (length($domain) > 0) {
print $r $domain;
}
- } else {
+ } elsif ($line !~ /^#line generated by\s/) {
print $r $line;
}
}