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