Setting tag MyPLC-4.3-35
[myplc.git] / plc.d / dns
1 #!/bin/bash
2 #
3 # priority: 850
4 #
5 # Start local DNS server to provide minimal DNS forward resolution for
6 # ourselves and our nodes.
7 #
8 # Mark Huang <mlhuang@cs.princeton.edu>
9 # Copyright (C) 2006 The Trustees of Princeton University
10 #
11 # $Id$
12 #
13
14 # Source function library and configuration
15 . /etc/plc.d/functions
16 . /etc/planetlab/plc_config
17
18 SIGNATURE="; generated by /etc/plc.d/dns --- DO NOT REMOVE THIS TEXT!"
19
20 # Be verbose
21 set -x
22
23 case "$1" in
24     start)
25         if [ $PLC_DNS_ENABLED -eq 1 ] ; then
26             MESSAGE="Starting DNS server"
27             dialog "$MESSAGE"
28
29             dns-config
30             check
31         fi
32
33         # Set up nameservers
34         if [ -n "$PLC_NET_DNS1" -o -n "$PLC_NET_DNS2" ] ; then
35             MESSAGE="Configuring resolv.conf"
36             dialog "$MESSAGE"
37
38             rfile=$(mktemp)
39             (
40                 echo $SIGNATURE
41                 [ -n "$PLC_NET_DNS1" ] && echo "nameserver $PLC_NET_DNS1"
42                 [ -n "$PLC_NET_DNS2" -a "$PLC_NET_DNS1" != "$PLC_NET_DNS2" ] && echo "nameserver $PLC_NET_DNS2"
43             ) > $rfile
44             
45             if [ $PLC_DNS_ENABLED -eq 1 ] ; then
46                 ourfile=$(grep "$SIGNATURE" /etc/resolv.conf| wc -l)
47                 if [ $ourfile -eq 0 ] ; then
48                     cp /etc/resolv.conf /etc/plc_resolv.conf
49                 fi
50             else
51                 cp /etc/resolv.conf /etc/resolv_plc.backup
52             fi
53             mv -f $rfile /etc/resolv.conf
54             chmod 444 /etc/resolv.conf
55         fi
56
57         if [ $PLC_DNS_ENABLED -eq 1 ] ; then
58             plc_daemon dnsmasq
59             check
60         fi
61
62         result "$MESSAGE"
63         ;;
64
65     stop)
66         if [ $PLC_DNS_ENABLED -eq 1 ] ; then
67             MESSAGE=$"Reverting /etc/resolv.conf"
68             dialog "$MESSAGE"
69             if [ -f "/etc/plc_resolv.conf" ] ; then
70                 ourfile=$(grep "$SIGNATURE" /etc/plc_resolv.conf| wc -l)
71                 if [ $ourfile -ne 0 ] ; then 
72                     dialog "WARNING: original /etc/resolv.conf was generated by etc/plc.d/dns"          
73                 fi
74                 mv -f /etc/plc_resolv.conf /etc/resolv.conf
75             fi
76             check
77             result ""
78             MESSAGE=$"Removing /etc/plc_hosts"
79             dialog "$MESSAGE"
80             [ -f "/etc/plc_hosts" ] && rm -f /etc/plc_hosts
81             result ""
82         fi
83         if [ $PLC_DNS_ENABLED -eq 0 -a -f "/etc/resolv_plc.backup" ] ; then
84             MESSAGE="Restoring /etc/resolv.conf"
85             dialog "$MESSAGE"
86             mv -f /etc/resolv_plc.backup /etc/resolv.conf
87             result ""
88         fi
89
90         if [ $PLC_DNS_ENABLED -eq 1 ] ; then
91             MESSAGE="Stopping DNS server"
92             dialog "$MESSAGE"
93             killproc plc_dnsmasq
94             check
95             result ""
96         fi
97
98         ;;
99 esac
100
101 exit $ERRORS