summaryrefslogtreecommitdiffstats
path: root/named.init
blob: f567c07401ec41271f4e828eba7757dff18dcde5 (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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#!/bin/bash
#
# named           This shell script takes care of starting and stopping
#                 named (BIND DNS server).
#
# chkconfig: - 13 87
# description: named (BIND) is a Domain Name Server (DNS) \
# that is used to resolve host names to IP addresses.
# probe: true

### BEGIN INIT INFO
# Provides: $named
# Required-Start: $local_fs $network $syslog
# Required-Stop: $local_fs $network $syslog
# Default-Start:
# Default-Stop: 0 1 2 3 4 5 6
# Short-Description: start|stop|status|restart|try-restart|reload|force-reload DNS server
# Description: control ISC BIND implementation of DNS server
### END INIT INFO

# Source function library.
. /etc/rc.d/init.d/functions

[ -r /etc/sysconfig/named ] && . /etc/sysconfig/named

RETVAL=0
export KRB5_KTNAME=${KEYTAB_FILE:-/etc/named.keytab}

named='named'
if [ -x /usr/sbin/named-sdb ]; then
	named='named-sdb'
fi

# Don't kill named during clean-up
NAMED_SHUTDOWN_TIMEOUT=${NAMED_SHUTDOWN_TIMEOUT:-25}

if [ -n "$ROOTDIR" ]; then
   ROOTDIR=`echo $ROOTDIR | sed 's#//*#/#g;s#/$##'`;
   rdl=`/usr/bin/readlink $ROOTDIR`;
   if [ -n "$rdl" ]; then
      ROOTDIR="$rdl";
   fi;
fi

ROOTDIR_MOUNT='/etc/named /etc/pki/dnssec-keys /var/named /etc/named.conf
/etc/named.dnssec.keys /etc/named.rfc1912.zones /etc/rndc.conf /etc/rndc.key
/usr/lib64/bind /usr/lib/bind /etc/named.iscdlv.key /etc/named.root.key'

mount_chroot_conf()
{
  if [ -n "$ROOTDIR" ]; then
    for all in $ROOTDIR_MOUNT; do
      # Skip nonexistant files
      [ -e "$all" ] || continue

      # If mount source is a file
      if ! [ -d "$all" ]; then
        # mount it only if it is not present in chroot or it is empty
        if ! [ -e "$ROOTDIR$all" ] || [ `stat -c'%s' "$ROOTDIR$all"` -eq 0 ]; then
          touch "$ROOTDIR$all"
          mount --bind "$all" "$ROOTDIR$all"
        fi
      else
        # Mount source is a directory. Mount it only if directory in chroot is
        # empty.
	if [ -e "$all" ] && [ `ls -1A $ROOTDIR$all | wc -l` -eq 0 ]; then
          mount --bind "$all" "$ROOTDIR$all"
	fi
      fi
    done
  fi
}

umount_chroot_conf()
{
  for all in $ROOTDIR_MOUNT; do
    # Check if file is mount target. Do not use /proc/mounts because detecting
    # of modified mounted files can fail.
    if mount | grep -q '.* on '"$ROOTDIR$all"' .*'; then
      umount "$ROOTDIR$all"
      # Remove temporary created files
      [ -f "$all" ] && rm -f "$ROOTDIR$all"
    fi
  done
}

# Check if all what named needs running
start()
{
  [ "$EUID" != "0" ] && exit 4

  # Source networking configuration.
  [ -r /etc/sysconfig/network ] && . /etc/sysconfig/network

  # Check that networking is up
  [ "${NETWORKING}" = "no" ] && exit 1


  [ -x /usr/sbin/"$named" ] || exit 5

  # Handle -c option
  previous_option='unspecified';
  for a in $OPTIONS; do
    if [ $previous_option = '-c' ]; then
      named_conf=$a;
    fi;
    previous_option=$a;
  done;

  named_conf=${named_conf:-/etc/named.conf};

  mount_chroot_conf

  if [ ! -r $ROOTDIR$named_conf ]; then
    echo 'Cannot find configuration file. You could create it by system-config-bind'
    exit 6;
  fi;

  # all pre-start is done, lets start named
  echo -n $"Starting named: "
  if [ -n "`/sbin/pidof -o %PPID "$named"`" ]; then
    echo -n $"named: already running"
    success
    echo
    exit 0;
  fi;

  ckcf_options='-z'; # enable named-checkzone for each zone (9.3.1+) !
  if [ -n "${ROOTDIR}" -a "x${ROOTDIR}" != "x/" ]; then
    OPTIONS="${OPTIONS} -t ${ROOTDIR}"
    ckcf_options="$ckcf_options -t ${ROOTDIR}";
    [ -s /etc/localtime ] && cp -fp /etc/localtime ${ROOTDIR}/etc/localtime;
  fi

  RETVAL=0
  # check if configuration is correct
  if [ -x /usr/sbin/named-checkconf ] && [ -x /usr/sbin/named-checkzone ] && /usr/sbin/named-checkconf $ckcf_options ${named_conf} >/dev/null 2>&1; then

    daemon /usr/sbin/"$named" -u named ${OPTIONS};
    RETVAL=$?
    if [ $RETVAL -eq 0 ]; then
      rm -f /var/run/{named,named-sdb}.pid;
      ln -s $ROOTDIR/var/run/named/"$named".pid /var/run/"$named".pid;
    fi;

  else
    named_err="`/usr/sbin/named-checkconf $ckcf_options $named_conf 2>&1`";	   
    echo
    echo "Error in named configuration:";
    echo "$named_err";
    failure
    echo
    [ -x /usr/bin/logger ] && echo "$named_err" | /usr/bin/logger -pdaemon.error -tnamed;
    umount_chroot_conf
    exit 2;
  fi;
  echo
  if [ $RETVAL -eq 0 ]; then
    touch /var/lock/subsys/named;
  else
    umount_chroot_conf
    exit 7;
  fi
  return 0;
}

stop() {
  [ "$EUID" != "0" ] && exit 4

  # Stop daemons.
  echo -n $"Stopping named: "
  [ -x /usr/sbin/rndc ] && /usr/sbin/rndc stop >/dev/null 2>&1;
  RETVAL=$?
  # was rndc successful?
  [ "$RETVAL" -eq 0 ] || killproc "$named" -TERM >/dev/null 2>&1

  timeout=0
  RETVAL=0
  while /sbin/pidof -o %PPID "$named" >/dev/null; do
    if [ $timeout -ge $NAMED_SHUTDOWN_TIMEOUT ]; then
      RETVAL=1
      break
    else
      sleep 2 && echo -n "."
      timeout=$((timeout+2))
    fi;
  done

  umount_chroot_conf

  # remove pid files
  if [ $RETVAL -eq 0 ]; then
    rm -f /var/lock/subsys/named
    rm -f /var/run/{named,named-sdb}.pid
  fi;

  if [ $RETVAL -eq 0 ]; then
    success
  else
    failure
    RETVAL=1
  fi;
  echo
  return $RETVAL
}


rhstatus() {
  [ -x /usr/sbin/rndc ] && /usr/sbin/rndc status;
  status /usr/sbin/"$named";
  return $?
}	
restart() {
	stop
	start
}	
reload() {
        [ "$EUID" != "0" ] && exit 

        echo -n $"Reloading "$named": "
	p=`/sbin/pidof -o %PPID "$named"`
	RETVAL=$?
	if [ "$RETVAL" -eq 0 ]; then 
	    /usr/sbin/rndc reload >/dev/null 2>&1 || /bin/kill -HUP $p;
	    RETVAL=$?
        fi
	[ "$RETVAL" -eq 0 ] && success $"$named reload" || failure $"$named reload"
        echo
	return $RETVAL
}

# See how we were called.
case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	status)
		rhstatus;
		RETVAL=$?
		;;
	restart)
		restart
		;;
	condrestart|try-restart)
		if [ -e /var/lock/subsys/named ]; then restart; fi
                ;;
	reload)
		reload
		;;
	force-reload)
		if ! reload; then restart; fi
		;;
	*)
        	echo $"Usage: $0 {start|stop|status|restart|try-restart|reload|force-reload}"
		[ "x$1" = "x" ] && exit 0
		exit 2
esac

exit $RETVAL