diff options
-rwxr-xr-x | scripts/system/vyatta_update_resolv.pl | 38 | ||||
-rwxr-xr-x | scripts/vyatta-system-nameservers | 107 | ||||
-rw-r--r-- | templates/system/allow-dhcp-nameservers/node.def | 3 |
3 files changed, 78 insertions, 70 deletions
diff --git a/scripts/system/vyatta_update_resolv.pl b/scripts/system/vyatta_update_resolv.pl index ee14034b..f193fd96 100755 --- a/scripts/system/vyatta_update_resolv.pl +++ b/scripts/system/vyatta_update_resolv.pl @@ -25,17 +25,27 @@ use strict; use lib "/opt/vyatta/share/perl5/"; - use Getopt::Long; +use Vyatta::Config; + my $dhclient_script = 0; -GetOptions("dhclient-script=i" => \$dhclient_script); +my $config_mode = 0; +GetOptions("dhclient-script=i" => \$dhclient_script, + "config-mode=i" => \$config_mode, +); -use Vyatta::Config; my $vc = new Vyatta::Config(); - $vc->setLevel('system'); + my @domains; my $domain_name = undef; +my $allow_dhcp_nameservers = ''; + +if ($config_mode == 1) { + $allow_dhcp_nameservers = $vc->returnValue('allow-dhcp-nameservers'); +} else { + $allow_dhcp_nameservers = $vc->returnOrigValue('allow-dhcp-nameservers'); +} if ($dhclient_script == 1) { @domains = $vc->returnOrigValues('domain-search domain'); @@ -66,6 +76,7 @@ foreach my $domain (@domains) { } # 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) { @@ -93,14 +104,16 @@ if ($domain_name && length($domain_name) > 0) { $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 -# and allow-dhcp-nameservers is set to true (default) -if (($dhclient_script == 1) && ($vc->returnOrigValue('allow-dhcp-nameservers') eq "true")) { +# update /etc/resolv.conf with name-servers received from dhcp client, done when this script is called +# with either the dhclient-script (on DHCP changes) or config-mode (allow-dhcp-nameservers) options. + +if (($dhclient_script == 1) || ($config_mode == 1)) { my @current_dhcp_nameservers; my $restart_ntp = 0; - # code below to add new name-servers received from dhcp client - + # code below to add new name-servers received from dhcp client, but only if allow-dhcp-nameservers + # is set to true (default) + my @dhcp_interfaces_resolv_files = `ls /etc/ | grep resolv.conf.dhclient-new`; if ($#dhcp_interfaces_resolv_files >= 0) { my $ns_count = 0; @@ -127,7 +140,7 @@ if (($dhclient_script == 1) && ($vc->returnOrigValue('allow-dhcp-nameservers') e } } } - if ($ns_in_resolvconf == 0) { + if (($ns_in_resolvconf == 0) && ($allow_dhcp_nameservers eq "true")) { open (my $rf, '>>', '/etc/resolv.conf') or die "$! error trying to overwrite"; print $rf "nameserver\t$ns\t\t#nameserver written by $0\n"; @@ -139,7 +152,8 @@ if (($dhclient_script == 1) && ($vc->returnOrigValue('allow-dhcp-nameservers') e } } - # code below to remove old name-servers from /etc/resolv.conf that were not received in this response from dhcp-server + # code below to remove old name-servers from /etc/resolv.conf that were not received in this response + # from dhcp-server, or to remove previous dhcp supplied name-servers if allow-dhcp-nameservers is false my @nameservers_dhcp_in_resolvconf = `grep 'nameserver written' /etc/resolv.conf`; my @dhcp_nameservers_in_resolvconf; @@ -149,7 +163,7 @@ if (($dhclient_script == 1) && ($vc->returnOrigValue('allow-dhcp-nameservers') e $dhcp_nameservers_in_resolvconf[$count_nameservers_in_resolvconf] = $dhcp_nameserver[1]; $count_nameservers_in_resolvconf++; } - if ($#current_dhcp_nameservers < 0) { + if (($#current_dhcp_nameservers < 0) || ($allow_dhcp_nameservers eq "false")) { for my $dhcpnameserver (@dhcp_nameservers_in_resolvconf) { my $cmd = "sed -i '/$dhcpnameserver\t/d' /etc/resolv.conf"; system($cmd); diff --git a/scripts/vyatta-system-nameservers b/scripts/vyatta-system-nameservers index 1df0f02c..74f2c8b6 100755 --- a/scripts/vyatta-system-nameservers +++ b/scripts/vyatta-system-nameservers @@ -23,71 +23,64 @@ # **** End License **** # -print_usage() -{ +print_usage() { echo "Usage:" echo -e "\t$0 update <ip of name-server>" echo -e "\t$0 delete <ip of name-server>" } -restart_dnsmasq () -{ - # restart dnsmasq if dns-forwarding is configured - if cli-shell-api existsActive service dns forwarding; then - /opt/vyatta/sbin/vyatta-dns-forwarding.pl --update-dnsforwarding >&/dev/null - fi +restart_dnsmasq () { + # restart dnsmasq if dns-forwarding is configured + if cli-shell-api existsActive service dns forwarding; then + /opt/vyatta/sbin/vyatta-dns-forwarding.pl --update-dnsforwarding >&/dev/null + fi } -restart_ntp () -{ - # restart ntp if ntp is configured - if [ -f /etc/ntp.conf ] && grep -q "^server" /etc/ntp.conf; then - /usr/sbin/invoke-rc.d ntp restart >&/dev/null - fi +restart_ntp () { + # restart ntp if ntp is configured + if [ -f /etc/ntp.conf ] && grep -q "^server" /etc/ntp.conf; then + /usr/sbin/invoke-rc.d ntp restart >&/dev/null + fi } - -update_system_nameservers () -{ - nameserver=$1 - touch /etc/resolv.conf - # if name-server already in /etc/resolv.conf then exit - if grep -q "$nameserver\($\|[[:space:]]\)" /etc/resolv.conf; then - exit 0 - else - # 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_len=${#cli_ns_array[*]} - line_num=0 - if [ $cli_ns_array_len -gt 0 ]; then - grepped_ns_line=`grep "${cli_ns_array[$cli_ns_array_len-1]}$" -n /etc/resolv.conf` - echo ${grepped_ns_line%%:*} > /etc/resolv_tmp.conf - line_num=`cat /etc/resolv_tmp.conf` - fi - head -$line_num /etc/resolv.conf > /etc/resolv_tmp.conf - echo "nameserver $nameserver" >> /etc/resolv_tmp.conf - total_lines=`cat /etc/resolv.conf | wc -l` - rest_lines=`expr $total_lines - $line_num` - tail -$rest_lines /etc/resolv.conf >> /etc/resolv_tmp.conf - mv -f /etc/resolv_tmp.conf /etc/resolv.conf - fi - restart_dnsmasq - restart_ntp +update_system_nameservers () { + nameserver=$1 + touch /etc/resolv.conf + # if name-server already in /etc/resolv.conf then exit + if grep -q "$nameserver\($\|[[:space:]]\)" /etc/resolv.conf; then + exit 0 + else + # 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_len=${#cli_ns_array[*]} + line_num=0 + if [ $cli_ns_array_len -gt 0 ]; then + grepped_ns_line=`grep "${cli_ns_array[$cli_ns_array_len-1]}$" -n /etc/resolv.conf` + echo ${grepped_ns_line%%:*} > /etc/resolv_tmp.conf + line_num=`cat /etc/resolv_tmp.conf` + fi + head -$line_num /etc/resolv.conf > /etc/resolv_tmp.conf + echo "nameserver $nameserver" >> /etc/resolv_tmp.conf + total_lines=`cat /etc/resolv.conf | wc -l` + rest_lines=`expr $total_lines - $line_num` + tail -$rest_lines /etc/resolv.conf >> /etc/resolv_tmp.conf + mv -f /etc/resolv_tmp.conf /etc/resolv.conf + fi + restart_dnsmasq + restart_ntp } -delete_system_nameserver () -{ - nameserver=$1 - touch /etc/resolv.conf - # remove specified nameserver - sed -i "/$nameserver$/d" /etc/resolv.conf - restart_dnsmasq - restart_ntp +delete_system_nameserver () { + nameserver=$1 + touch /etc/resolv.conf + # remove specified nameserver + sed -i "/$nameserver$/d" /etc/resolv.conf + restart_dnsmasq + restart_ntp } - # # main # @@ -95,8 +88,8 @@ delete_system_nameserver () case "$1" in update) if [ $# -ne 2 ]; then - print_usage - exit 1 + print_usage + exit 1 fi update_system_nameservers $2 exit 0 @@ -104,17 +97,15 @@ case "$1" in delete) if [ $# -ne 2 ]; then - print_usage - exit 1 + print_usage + exit 1 fi delete_system_nameserver $2 exit 0 ;; - *) print_usage exit 1 ;; - esac diff --git a/templates/system/allow-dhcp-nameservers/node.def b/templates/system/allow-dhcp-nameservers/node.def index c010baf1..442e003f 100644 --- a/templates/system/allow-dhcp-nameservers/node.def +++ b/templates/system/allow-dhcp-nameservers/node.def @@ -2,3 +2,6 @@ priority: 300 type: bool help: Allow DHCP to update DNS settings default: true + +update: sudo /opt/vyatta/sbin/vyatta_update_resolv.pl --config-mode 1 +delete: sudo /opt/vyatta/sbin/vyatta_update_resolv.pl --config-mode 1
\ No newline at end of file |