Advertisement
zigojacko

init.d named

Mar 11th, 2014
412
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.49 KB | None | 0 0
  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
  49. /usr/lib64/bind /usr/lib/bind /etc/named.iscdlv.key /etc/named.root.key'
  50.  
  51. mount_chroot_conf()
  52. {
  53. if [ -n "$ROOTDIR" ]; then
  54. for all in $ROOTDIR_MOUNT; do
  55. # Skip nonexistant files
  56. [ -e "$all" ] || continue
  57.  
  58. # If mount source is a file
  59. if ! [ -d "$all" ]; then
  60. # mount it only if it is not present in chroot or it is empty
  61. if ! [ -e "$ROOTDIR$all" ] || [ `stat -c'%s' "$ROOTDIR$all"` -eq 0 ]; then
  62. touch "$ROOTDIR$all"
  63. mount --bind "$all" "$ROOTDIR$all"
  64. fi
  65. else
  66. # Mount source is a directory. Mount it only if directory in chroot is
  67. # empty.
  68. if [ -e "$all" ] && [ `ls -1A $ROOTDIR$all | wc -l` -eq 0 ]; then
  69. mount --bind "$all" "$ROOTDIR$all"
  70. fi
  71. fi
  72. done
  73. fi
  74. }
  75.  
  76. umount_chroot_conf()
  77. {
  78. if [ -n "$ROOTDIR" ]; then
  79. for all in $ROOTDIR_MOUNT; do
  80. # Check if file is mount target. Do not use /proc/mounts because detecting
  81. # of modified mounted files can fail.
  82. if mount | grep -q '.* on '"$ROOTDIR$all"' .*'; then
  83. umount "$ROOTDIR$all"
  84. # Remove temporary created files
  85. [ -f "$all" ] && rm -f "$ROOTDIR$all"
  86. fi
  87. done
  88. fi
  89. }
  90.  
  91. pidofnamed() {
  92. pidofproc -p "$ROOTDIR/$PIDFILE" "$named";
  93. }
  94.  
  95. # Check if all what named needs running
  96. start()
  97. {
  98. [ "$EUID" != "0" ] && exit 4
  99.  
  100. # Source networking configuration.
  101. [ -r /etc/sysconfig/network ] && . /etc/sysconfig/network
  102.  
  103. # Check that networking is up
  104. [ "${NETWORKING}" = "no" ] && exit 1
  105.  
  106.  
  107. [ -x /usr/sbin/"$named" ] || exit 5
  108.  
  109. if [ ! -s /etc/rndc.key ]; then
  110. # Generate rndc.key if doesn't exist
  111. echo -n $"Generating /etc/rndc.key:"
  112. if /usr/sbin/rndc-confgen -a > /dev/null 2>&1; then
  113. chmod 640 /etc/rndc.key
  114. chown root.named /etc/rndc.key
  115. [ -x /sbin/restorecon ] && /sbin/restorecon /etc/rndc.key
  116. success $"/etc/rndc.key generation"
  117. echo
  118. else
  119. failure $"/etc/rndc.key generation"
  120. echo
  121. fi
  122. fi
  123.  
  124. # Handle -c option
  125. previous_option='unspecified';
  126. for a in $OPTIONS; do
  127. if [ $previous_option = '-c' ]; then
  128. named_conf=$a;
  129. fi;
  130. previous_option=$a;
  131. done;
  132.  
  133. named_conf=${named_conf:-/etc/named.conf};
  134.  
  135. mount_chroot_conf
  136.  
  137. if [ ! -r $ROOTDIR$named_conf ]; then
  138. echo 'Cannot find configuration file. You could create it by system-config-bind'
  139. exit 6;
  140. fi;
  141.  
  142. [ -x /sbin/portrelease ] && /sbin/portrelease named &>/dev/null || :
  143.  
  144. # all pre-start is done, lets start named
  145. echo -n $"Starting named: "
  146. if [ -n "`pidofnamed`" ]; then
  147. echo -n $"named: already running"
  148. success
  149. echo
  150. exit 0;
  151. fi;
  152.  
  153. if ! [ "$DISABLE_ZONE_CHECKING" = yes ]; then
  154. ckcf_options='-z'; # enable named-checkzone for each zone (9.3.1+) !
  155. fi;
  156.  
  157. if [ -n "${ROOTDIR}" -a "x${ROOTDIR}" != "x/" ]; then
  158. OPTIONS="${OPTIONS} -t ${ROOTDIR}"
  159. ckcf_options="$ckcf_options -t ${ROOTDIR}";
  160. [ -s /etc/localtime ] && cp -fp /etc/localtime ${ROOTDIR}/etc/localtime;
  161. fi
  162.  
  163. RETVAL=0
  164. # check if configuration is correct
  165. if [ -x /usr/sbin/named-checkconf ] && [ -x /usr/sbin/named-checkzone ] && /usr/sbin/named-checkconf $ckcf_options ${named_conf} >/dev/null 2>&1; then
  166.  
  167. daemon --pidfile "$ROOTDIR/$PIDFILE" /usr/sbin/"$named" -u named ${OPTIONS};
  168. RETVAL=$?
  169. if [ $RETVAL -eq 0 ]; then
  170. rm -f /var/run/{named,named-sdb}.pid;
  171. ln -s $ROOTDIR/"$PIDFILE" /var/run/"$named".pid;
  172. fi;
  173.  
  174. else
  175. named_err="`/usr/sbin/named-checkconf $ckcf_options $named_conf 2>&1`";
  176. echo
  177. echo "Error in named configuration:";
  178. echo "$named_err";
  179. failure
  180. echo
  181. [ -x /usr/bin/logger ] && echo "$named_err" | /usr/bin/logger -pdaemon.error -tnamed;
  182. umount_chroot_conf
  183. exit 2;
  184. fi;
  185. echo
  186. if [ $RETVAL -eq 0 ]; then
  187. touch /var/lock/subsys/named;
  188. else
  189. umount_chroot_conf
  190. exit 7;
  191. fi
  192. return 0;
  193. }
  194.  
  195. stop() {
  196. [ "$EUID" != "0" ] && exit 4
  197.  
  198. # Stop daemons.
  199. echo -n $"Stopping named: "
  200. [ -x /usr/sbin/rndc ] && /usr/sbin/rndc stop >/dev/null 2>&1;
  201. RETVAL=$?
  202. # was rndc successful?
  203. [ "$RETVAL" -eq 0 ] || \
  204. killproc -p "$ROOTDIR/$PIDFILE" "$named" -TERM >/dev/null 2>&1
  205.  
  206. timeout=0
  207. RETVAL=0
  208. while pidofnamed &>/dev/null; do
  209. if [ $timeout -ge $NAMED_SHUTDOWN_TIMEOUT ]; then
  210. RETVAL=1
  211. break
  212. else
  213. sleep 2 && echo -n "."
  214. timeout=$((timeout+2))
  215. fi;
  216. done
  217.  
  218. umount_chroot_conf
  219.  
  220. # remove pid files
  221. if [ $RETVAL -eq 0 ]; then
  222. rm -f /var/lock/subsys/named
  223. rm -f /var/run/{named,named-sdb}.pid
  224. fi;
  225.  
  226. if [ $RETVAL -eq 0 ]; then
  227. success
  228. else
  229. failure
  230. RETVAL=1
  231. fi;
  232. echo
  233. return $RETVAL
  234. }
  235.  
  236.  
  237. rhstatus() {
  238. [ -x /usr/sbin/rndc ] && /usr/sbin/rndc status;
  239. status -p "$ROOTDIR/$PIDFILE" -l named /usr/sbin/"$named";
  240. return $?
  241. }
  242. restart() {
  243. stop
  244. start
  245. }
  246. reload() {
  247. [ "$EUID" != "0" ] && exit
  248.  
  249. echo -n $"Reloading "$named": "
  250. p=`pidofnamed`
  251. RETVAL=$?
  252. if [ "$RETVAL" -eq 0 ]; then
  253. /usr/sbin/rndc reload >/dev/null 2>&1 || /bin/kill -HUP $p;
  254. RETVAL=$?
  255. fi
  256. [ "$RETVAL" -eq 0 ] && success $"$named reload" || failure $"$named reload"
  257. echo
  258. return $RETVAL
  259. }
  260.  
  261. checkconfig() {
  262. ckcf_options='-z';
  263. if [ -n "${ROOTDIR}" -a "x${ROOTDIR}" != "x/" ]; then
  264. ckcf_options="$ckcf_options -t ${ROOTDIR}";
  265. mount_chroot_conf
  266. fi;
  267. if [ -x /usr/sbin/named-checkconf ] && [ -x /usr/sbin/named-checkzone ] && /usr/sbin/named-checkconf $ckcf_options ${named_conf} ; then
  268. umount_chroot_conf
  269. return 0;
  270. else
  271. umount_chroot_conf
  272. return 1;
  273. fi
  274. }
  275.  
  276. # See how we were called.
  277. case "$1" in
  278. start)
  279. start
  280. ;;
  281. stop)
  282. stop
  283. ;;
  284. status)
  285. rhstatus;
  286. RETVAL=$?
  287. ;;
  288. restart)
  289. restart
  290. ;;
  291. condrestart|try-restart)
  292. if [ -e /var/lock/subsys/named ]; then restart; fi
  293. ;;
  294. reload)
  295. reload
  296. ;;
  297. force-reload)
  298. if ! reload; then restart; fi
  299. ;;
  300. checkconfig|configtest|check|test)
  301. checkconfig
  302. ;;
  303. *)
  304. echo $"Usage: $0 {start|stop|status|restart|try-restart|reload|force-reload}"
  305. [ "x$1" = "x" ] && exit 0
  306. exit 2
  307. esac
  308.  
  309. exit $RETVAL
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement