summaryrefslogtreecommitdiff
path: root/scripts/system
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/system')
-rwxr-xr-xscripts/system/vyatta_update_resolv.pl74
1 files changed, 52 insertions, 22 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;
}