diff options
-rw-r--r-- | Makefile.am | 1 | ||||
-rw-r--r-- | scripts/vyatta-system-nameservers | 120 | ||||
-rw-r--r-- | templates/system/name-server/node.def | 35 |
3 files changed, 123 insertions, 33 deletions
diff --git a/Makefile.am b/Makefile.am index 9cd18256..0ab8017e 100644 --- a/Makefile.am +++ b/Makefile.am @@ -32,6 +32,7 @@ sbin_SCRIPTS += scripts/add_bootfile_eth_hwid sbin_SCRIPTS += scripts/mod_bootfile_eth_hwid sbin_SCRIPTS += scripts/dns-forwarding/vyatta-dns-forwarding.pl sbin_SCRIPTS += scripts/dynamic-dns/vyatta-dynamic-dns.pl +sbin_SCRIPTS += scripts/vyatta-system-nameservers noinst_DATA = test_bootfile diff --git a/scripts/vyatta-system-nameservers b/scripts/vyatta-system-nameservers new file mode 100644 index 00000000..8756bbb6 --- /dev/null +++ b/scripts/vyatta-system-nameservers @@ -0,0 +1,120 @@ +#!/bin/bash +# +# Module: vyatta-system-nameservers +# +# **** License **** +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License version 2 as +# published by the Free Software Foundation. +# +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. +# +# This code was originally developed by Vyatta, Inc. +# Portions created by Vyatta are Copyright (C) 2008 Vyatta, Inc. +# All Rights Reserved. +# +# Author: Mohit Mehta +# Date: September 2008 +# Description: CLI back-end script for setting/deleting system nameservers +# +# **** End License **** +# + +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 [ -d /opt/vyatta/config/active/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 +} + + +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 +} + + +# +# main +# + +case "$1" in + update) + if [ $# -ne 2 ]; then + print_usage + exit 1 + fi + update_system_nameservers $2 + exit 0 + ;; + + delete) + if [ $# -ne 2 ]; then + print_usage + exit 1 + fi + delete_system_nameserver $2 + exit 0 + ;; + + + *) + print_usage + exit 1 + ;; + +esac diff --git a/templates/system/name-server/node.def b/templates/system/name-server/node.def index d2ce0c30..f762a8a1 100644 --- a/templates/system/name-server/node.def +++ b/templates/system/name-server/node.def @@ -1,36 +1,5 @@ multi: type: ipv4 help: Set Domain Name Server (DNS) -update: touch /etc/resolv.conf -if grep -q "$VAR(@)\($\|[[:space:]]\)" /etc/resolv.conf; then - exit 0 -else - 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 $VAR(@)" >> /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 && -if [ -d /opt/vyatta/config/active/service/dns/forwarding ]; then - /opt/vyatta/sbin/vyatta-dns-forwarding.pl --update-dnsforwarding >&/dev/null -fi && -if [ -f /etc/ntp.conf ] && grep -q 'server' /etc/ntp.conf; then - /usr/sbin/invoke-rc.d ntp restart >&/dev/null -fi -delete:expression: "sudo sh -c \"touch /etc/resolv.conf && \ -sed -i '/$VAR(@)$/d' /etc/resolv.conf && \ -if [ -f /etc/ntp.conf ] && grep -q 'server' /etc/ntp.conf; then \ - /usr/sbin/invoke-rc.d ntp restart >&/dev/null; \ -fi && \ -if [ -d /opt/vyatta/config/active/service/dns/forwarding ]; then \ - /opt/vyatta/sbin/vyatta-dns-forwarding.pl --update-dnsforwarding >&/dev/null; \ -fi\" " +update: sudo /opt/vyatta/sbin/vyatta-system-nameservers update $VAR(@) +delete: sudo /opt/vyatta/sbin/vyatta-system-nameservers delete $VAR(@) |