Subversion Repositories configs

Rev

Rev 8 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
3 - 1
#!/bin/bash
2
#
3
# named           This shell script takes care of starting and stopping
4
#                 named (BIND DNS server).
5
#
6
# chkconfig: - 13 87
7
# description: named (BIND) is a Domain Name Server (DNS) \
8
# that is used to resolve host names to IP addresses.
9
# probe: true
10
 
11
### BEGIN INIT INFO
12
# Provides: $named
13
# Required-Start: $local_fs $network $syslog
14
# Required-Stop: $local_fs $network $syslog
15
# Default-Start:
16
# Default-Stop: 0 1 2 3 4 5 6
17
# Short-Description: start|stop|status|restart|try-restart|reload|force-reload DNS server
18
# Description: control ISC BIND implementation of DNS server
19
### END INIT INFO
20
 
21
# Source function library.
22
. /etc/rc.d/init.d/functions
23
 
24
[ -r /etc/sysconfig/named ] && . /etc/sysconfig/named
25
 
26
RETVAL=0
27
export KRB5_KTNAME=${KEYTAB_FILE:-/etc/named.keytab}
28
 
29
named='named'
30
if [ -x /usr/sbin/named-sdb ]; then
31
	named='named-sdb'
32
fi
33
 
34
# Don't kill named during clean-up
35
NAMED_SHUTDOWN_TIMEOUT=${NAMED_SHUTDOWN_TIMEOUT:-25}
36
 
37
if [ -n "$ROOTDIR" ]; then
38
   ROOTDIR=`echo $ROOTDIR | sed 's#//*#/#g;s#/$##'`;
39
   rdl=`/usr/bin/readlink $ROOTDIR`;
40
   if [ -n "$rdl" ]; then
41
      ROOTDIR="$rdl";
42
   fi;
43
fi
44
 
45
PIDFILE="/var/run/named/named.pid"
46
 
47
ROOTDIR_MOUNT='/etc/named /etc/pki/dnssec-keys /var/named /etc/named.conf
48
/etc/named.dnssec.keys /etc/named.rfc1912.zones /etc/rndc.conf /etc/rndc.key
95 - 49
/usr/lib64/bind /usr/lib/bind /etc/named.iscdlv.key /etc/named.root.key
50
/etc/services /etc/protocols'
3 - 51
 
95 - 52
for LIB in /lib /lib64
53
do
54
  if [ -d "$ROOTDIR$LIB" ]; then
55
    for NSSLIB in "$LIB"/libnss_files.so.[1-9]
56
    do
57
      ROOTDIR_MOUNT+=" $NSSLIB"
58
    done
59
  fi
60
done
61
 
3 - 62
mount_chroot_conf()
63
{
64
  if [ -n "$ROOTDIR" ]; then
65
    for all in $ROOTDIR_MOUNT; do
66
      # Skip nonexistant files
67
      [ -e "$all" ] || continue
68
 
69
      # If mount source is a file
70
      if ! [ -d "$all" ]; then
71
        # mount it only if it is not present in chroot or it is empty
95 - 72
        if ! [ -e "$ROOTDIR$all" ] || [ "`stat -c'%s' "$ROOTDIR$all"`" -eq 0 ]; then
3 - 73
          touch "$ROOTDIR$all"
74
          mount --bind "$all" "$ROOTDIR$all"
75
        fi
76
      else
77
        # Mount source is a directory. Mount it only if directory in chroot is
78
        # empty.
95 - 79
	if [ -e "$all" ] && [ "`ls -1A "$ROOTDIR$all" | wc -l`" -eq 0 ]; then
3 - 80
          mount --bind "$all" "$ROOTDIR$all"
81
	fi
82
      fi
83
    done
84
  fi
85
}
86
 
87
umount_chroot_conf()
88
{
89
  if [ -n "$ROOTDIR" ]; then
90
    for all in $ROOTDIR_MOUNT; do
91
      # Check if file is mount target. Do not use /proc/mounts because detecting
92
      # of modified mounted files can fail.
93
      if mount | grep -q '.* on '"$ROOTDIR$all"' .*'; then
94
        umount "$ROOTDIR$all"
95
        # Remove temporary created files
96
        [ -f "$all" ] && rm -f "$ROOTDIR$all"
97
      fi
98
    done
99
  fi
100
}
101
 
8 - 102
check_pidfile() {
103
  PID="`pidofproc -p "$ROOTDIR$PIDFILE" "$named"`"
104
  if [ -n "$PID" ] && [ "`ps -p "$PID" --no-headers -o comm`" != "$named" ]; then
105
    rm -f $ROOTDIR$PIDFILE &> /dev/null
106
  fi
107
}
108
 
3 - 109
pidofnamed() {
8 - 110
	pidofproc -p "$ROOTDIR$PIDFILE" "$named";
3 - 111
}
112
 
113
# Check if all what named needs running
114
start()
115
{
116
  [ "$EUID" != "0" ] && exit 4
117
 
118
  # Source networking configuration.
119
  [ -r /etc/sysconfig/network ] && . /etc/sysconfig/network
120
 
121
  # Check that networking is up
122
  [ "${NETWORKING}" = "no" ] && exit 1
123
 
124
 
125
  [ -x /usr/sbin/"$named" ] || exit 5
126
 
8 - 127
  if [ ! -s /etc/rndc.key -a ! -s /etc/rndc.conf ]; then
128
    # Generate rndc.key if doesn't exist AND there is no rndc.conf
3 - 129
    echo -n $"Generating /etc/rndc.key:"
8 - 130
    if /usr/sbin/rndc-confgen -a -r /dev/urandom > /dev/null 2>&1; then
3 - 131
      chmod 640 /etc/rndc.key
132
      chown root.named /etc/rndc.key
133
      [ -x /sbin/restorecon ] && /sbin/restorecon /etc/rndc.key
134
      success $"/etc/rndc.key generation"
135
      echo
136
    else
137
      failure $"/etc/rndc.key generation"
138
      echo
139
    fi
140
  fi
141
 
142
  # Handle -c option
143
  previous_option='unspecified';
144
  for a in $OPTIONS; do
145
    if [ $previous_option = '-c' ]; then
146
      named_conf=$a;
147
    fi;
148
    previous_option=$a;
149
  done;
150
 
151
  named_conf=${named_conf:-/etc/named.conf};
152
 
8 - 153
  # check if named is running before mounting files/dirs
154
  echo -n $"Starting named: "
155
  check_pidfile
156
  if [ -n "`pidofnamed`" ]; then
157
    echo -n $"named: already running"
158
    success
159
    echo
160
    exit 0;
161
  fi;
162
 
3 - 163
  mount_chroot_conf
164
 
165
  if [ ! -r $ROOTDIR$named_conf ]; then
166
    echo 'Cannot find configuration file. You could create it by system-config-bind'
167
    exit 6;
168
  fi;
169
 
170
  [ -x /sbin/portrelease ] && /sbin/portrelease named &>/dev/null || :
171
 
172
  if ! [ "$DISABLE_ZONE_CHECKING" = yes ]; then
173
    ckcf_options='-z'; # enable named-checkzone for each zone (9.3.1+) !
174
  fi;
175
 
176
  if [ -n "${ROOTDIR}" -a "x${ROOTDIR}" != "x/" ]; then
177
    OPTIONS="${OPTIONS} -t ${ROOTDIR}"
178
    ckcf_options="$ckcf_options -t ${ROOTDIR}";
179
    [ -s /etc/localtime ] && cp -fp /etc/localtime ${ROOTDIR}/etc/localtime;
180
  fi
181
 
182
  RETVAL=0
183
  # check if configuration is correct
184
  if [ -x /usr/sbin/named-checkconf ] && [ -x /usr/sbin/named-checkzone ] && /usr/sbin/named-checkconf $ckcf_options ${named_conf} >/dev/null 2>&1; then
185
 
8 - 186
    daemon --pidfile "$ROOTDIR$PIDFILE" /usr/sbin/"$named" -u named ${OPTIONS};
3 - 187
    RETVAL=$?
188
    if [ $RETVAL -eq 0 ]; then
189
      rm -f /var/run/{named,named-sdb}.pid;
8 - 190
      ln -s "$ROOTDIR$PIDFILE" /var/run/"$named".pid;
3 - 191
    fi;
192
 
193
  else
194
    named_err="`/usr/sbin/named-checkconf $ckcf_options $named_conf 2>&1`";
195
    echo
196
    echo "Error in named configuration:";
197
    echo "$named_err";
198
    failure
199
    echo
200
    [ -x /usr/bin/logger ] && echo "$named_err" | /usr/bin/logger -pdaemon.error -tnamed;
201
    umount_chroot_conf
202
    exit 2;
203
  fi;
204
  echo
205
  if [ $RETVAL -eq 0 ]; then
206
    touch /var/lock/subsys/named;
207
  else
208
    umount_chroot_conf
209
    exit 7;
210
  fi
211
  return 0;
212
}
213
 
214
stop() {
215
  [ "$EUID" != "0" ] && exit 4
216
 
217
  # Stop daemons.
218
  echo -n $"Stopping named: "
8 - 219
  check_pidfile
3 - 220
  [ -x /usr/sbin/rndc ] && /usr/sbin/rndc stop >/dev/null 2>&1;
221
  RETVAL=$?
222
  # was rndc successful?
223
  [ "$RETVAL" -eq 0 ] || \
8 - 224
    killproc -p "$ROOTDIR$PIDFILE" "$named" -TERM >/dev/null 2>&1
3 - 225
 
226
  timeout=0
227
  RETVAL=0
228
  while pidofnamed &>/dev/null; do
229
    if [ $timeout -ge $NAMED_SHUTDOWN_TIMEOUT ]; then
230
      RETVAL=1
231
      break
232
    else
233
      sleep 2 && echo -n "."
234
      timeout=$((timeout+2))
235
    fi;
236
  done
237
 
238
  umount_chroot_conf
239
 
240
  # remove pid files
241
  if [ $RETVAL -eq 0 ]; then
242
    rm -f /var/lock/subsys/named
243
    rm -f /var/run/{named,named-sdb}.pid
244
  fi;
245
 
246
  if [ $RETVAL -eq 0 ]; then
247
    success
248
  else
249
    failure
250
    RETVAL=1
251
  fi;
252
  echo
253
  return $RETVAL
254
}
255
 
256
 
257
rhstatus() {
258
  [ -x /usr/sbin/rndc ] && /usr/sbin/rndc status;
8 - 259
  check_pidfile
260
  status -p "$ROOTDIR$PIDFILE" -l named /usr/sbin/"$named";
3 - 261
  return $?
262
}
8 - 263
 
3 - 264
restart() {
265
	stop
266
	start
8 - 267
}
268
 
3 - 269
reload() {
270
        [ "$EUID" != "0" ] && exit
271
 
272
        echo -n $"Reloading "$named": "
8 - 273
	check_pidfile
3 - 274
	p=`pidofnamed`
275
	RETVAL=$?
276
	if [ "$RETVAL" -eq 0 ]; then
277
	    /usr/sbin/rndc reload >/dev/null 2>&1 || /bin/kill -HUP $p;
278
	    RETVAL=$?
279
        fi
280
	[ "$RETVAL" -eq 0 ] && success $"$named reload" || failure $"$named reload"
281
        echo
282
	return $RETVAL
283
}
284
 
285
checkconfig() {
286
	ckcf_options='-z';
287
	if [ -n "${ROOTDIR}" -a "x${ROOTDIR}" != "x/" ]; then
288
		ckcf_options="$ckcf_options -t ${ROOTDIR}";
8 - 289
		if ! [ -e "$ROOTDIR/$PIDFILE" ]; then
290
			mount_chroot_conf
291
		fi
3 - 292
	fi;
293
	if [ -x /usr/sbin/named-checkconf ] && [ -x /usr/sbin/named-checkzone ] && /usr/sbin/named-checkconf $ckcf_options ${named_conf} ; then
8 - 294
		RETVAL=0
3 - 295
	else
8 - 296
		RETVAL=1
297
	fi
298
	if ! [ -e "$ROOTDIR/$PIDFILE" ]; then
3 - 299
		umount_chroot_conf
300
	fi
8 - 301
	return $RETVAL
3 - 302
}
303
 
304
# See how we were called.
305
case "$1" in
306
	start)
307
		start
308
		;;
309
	stop)
310
		stop
311
		;;
312
	status)
313
		rhstatus;
314
		RETVAL=$?
315
		;;
316
	restart)
317
		restart
318
		;;
319
	condrestart|try-restart)
320
		if [ -e /var/lock/subsys/named ]; then restart; fi
321
                ;;
322
	reload)
323
		reload
324
		;;
325
	force-reload)
326
		if ! reload; then restart; fi
327
		;;
328
	checkconfig|configtest|check|test)
329
		checkconfig
8 - 330
		RETVAL=$?
3 - 331
		;;
332
	*)
333
        	echo $"Usage: $0 {start|stop|status|restart|try-restart|reload|force-reload}"
334
		[ "x$1" = "x" ] && exit 0
335
		exit 2
336
esac
337
 
338
exit $RETVAL
339