summaryrefslogtreecommitdiff
path: root/scripts/vyatta-system-nameservers
blob: 74f2c8b687a5ae92709208d680c03971ba799a80 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/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 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
}

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