ipsec 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. #!/bin/sh
  2. # IPsec startup and shutdown script
  3. #
  4. ### BEGIN INIT INFO
  5. # Provides: ipsec
  6. # Required-Start: $network $remote_fs $syslog $named
  7. # Required-Stop: $syslog $remote_fs
  8. # Default-Start:
  9. # Default-Stop: 0 1 6
  10. # Short-Description: Start Libreswan IPsec at boot time
  11. # Description: Enable automatic key management for IPsec (KLIPS and NETKEY)
  12. ### END INIT INFO
  13. #
  14. ### see https://bugzilla.redhat.com/show_bug.cgi?id=636572
  15. ### Debian and Fedora interpret the LSB differently for Default-Start:
  16. # Copyright (C) 1998, 1999, 2001 Henry Spencer.
  17. # Copyright (C) 2002 Michael Richardson <[email protected]>
  18. # Copyright (C) 2006 Michael Richardson <[email protected]>
  19. # Copyright (C) 2008 Michael Richardson <[email protected]>
  20. # Copyright (C) 2008-2015 Tuomo Soini <[email protected]>
  21. # Copyright (C) 2012 Paul Wouters <[email protected]>
  22. #
  23. # This program is free software; you can redistribute it and/or modify it
  24. # under the terms of the GNU General Public License as published by the
  25. # Free Software Foundation; either version 2 of the License, or (at your
  26. # option) any later version. See <https://www.gnu.org/licenses/gpl2.txt>.
  27. #
  28. # This program is distributed in the hope that it will be useful, but
  29. # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  30. # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  31. # for more details.
  32. #
  33. # ipsec sysv style init.d script for starting and stopping
  34. # the IPsec security subsystem (KLIPS and Pluto).
  35. #
  36. # This script becomes /etc/init.d/ipsec
  37. # and is also accessible as "ipsec setup"
  38. #
  39. # The startup and shutdown times are a difficult compromise (in particular,
  40. # it is almost impossible to reconcile them with the insanely early/late
  41. # times of NFS filesystem startup/shutdown). Startup is after startup of
  42. # syslog and pcmcia support; shutdown is just before shutdown of syslog.
  43. #
  44. # chkconfig: - 47 76
  45. # description: IPsec provides encrypted and authenticated communications; \
  46. # NETKEY/KLIPS is the kernel half of it, Pluto is the user-level management daemon.
  47. test ${IPSEC_INIT_SCRIPT_DEBUG} && set -v -x
  48. # Source function library.
  49. if [ -f /etc/init.d/functions ]; then
  50. . /etc/init.d/functions
  51. elif [ -f /lib/lsb/init-functions ]; then
  52. . /lib/lsb/init-functions
  53. fi
  54. # Check that networking is up.
  55. [ "${NETWORKING}" = "no" ] && exit 6
  56. if [ $(id -u) -ne 0 ]; then
  57. echo "permission denied (must be superuser)" | \
  58. logger -s -p daemon.error -t ipsec_setup 2>&1
  59. exit 4
  60. fi
  61. if [ $(ip addr list | grep -c cipsec) -ne 0 ]; then
  62. echo "Cisco IPsec client is already loaded, aborting! (cipsec# device found)"
  63. exit 1
  64. fi
  65. # where the private directory and the config files are
  66. IPSEC_CONF="${IPSEC_CONF:-/etc/ipsec.conf}"
  67. IPSEC_EXECDIR="${IPSEC_EXECDIR:-/usr/libexec/ipsec}"
  68. IPSEC_SBINDIR="${IPSEC_SBINDIR:-/usr/sbin}"
  69. unset PLUTO_OPTIONS
  70. rundir=/run/pluto
  71. plutopid=${rundir}/pluto.pid
  72. plutoctl=${rundir}/pluto.ctl
  73. lockdir=/var/lock/subsys
  74. lockfile=${lockdir}/ipsec
  75. # /etc/resolv.conf related paths
  76. LIBRESWAN_RESOLV_CONF=${rundir}/libreswan-resolv-conf-backup
  77. ORIG_RESOLV_CONF=/etc/resolv.conf
  78. # there is some confusion over the name - just do both
  79. [ -f /etc/sysconfig/ipsec ] && . /etc/sysconfig/ipsec
  80. [ -f /etc/sysconfig/pluto ] && . /etc/sysconfig/pluto
  81. # misc setup
  82. umask 022
  83. # standardize PATH, and export it for everything else's benefit
  84. PATH="${IPSEC_SBINDIR}:/sbin:/usr/sbin:/usr/local/bin:/bin:/usr/bin"
  85. export PATH
  86. mkdir -p ${rundir}
  87. chmod 700 ${rundir}
  88. verify_config() {
  89. [ -f ${IPSEC_CONF} ] || exit 6
  90. config_error=$(ipsec addconn --config ${IPSEC_CONF} --checkconfig 2>&1)
  91. RETVAL=$?
  92. if [ ${RETVAL} -gt 0 ]; then
  93. echo "Configuration error - the following error occurred:"
  94. echo ${config_error}
  95. echo "IKE daemon status was not modified"
  96. exit ${RETVAL}
  97. fi
  98. }
  99. start() {
  100. echo -n "Starting pluto IKE daemon for IPsec: "
  101. ipsec _stackmanager start
  102. # pluto searches the current directory, so this is required for making it selinux compliant
  103. cd /
  104. # Create nss db or convert from old format to new sql format
  105. ipsec --checknss
  106. # Enable nflog if configured
  107. ipsec --checknflog > /dev/null
  108. # This script will enter an endless loop to ensure pluto restarts on crash
  109. ipsec _plutorun --config ${IPSEC_CONF} --nofork ${PLUTO_OPTIONS} &
  110. [ -d ${lockdir} ] || mkdir -p ${lockdir}
  111. touch ${lockfile}
  112. # Because _plutorun starts pluto at background we need to make sure pluto is started
  113. # before we know if start was successful or not
  114. for waitsec in 1 2 3 4 5; do
  115. if status >/dev/null; then
  116. RETVAL=0
  117. break
  118. else
  119. echo -n "."
  120. sleep 1
  121. RETVAL=1
  122. fi
  123. done
  124. if [ ${RETVAL} -ge 1 ]; then
  125. rm -f ${lockfile}
  126. fi
  127. echo
  128. if [ -f /usr/libexec/ipsec/portexcludes ] ; then
  129. /usr/libexec/ipsec/portexcludes
  130. fi
  131. return ${RETVAL}
  132. }
  133. stop() {
  134. if [ -e ${plutoctl} ]; then
  135. echo "Shutting down pluto IKE daemon"
  136. ipsec whack --shutdown 2>/dev/null
  137. # don't use seq, might not exist on embedded
  138. for waitsec in 1 2 3 4 5 6 7 8 9 10; do
  139. if [ -s ${plutopid} ]; then
  140. echo -n "."
  141. sleep 1
  142. else
  143. break
  144. fi
  145. done
  146. echo
  147. rm -f ${plutoctl} # we won't be using this anymore
  148. fi
  149. if [ -s ${plutopid} ]; then
  150. # pluto did not die peacefully
  151. pid=$(cat ${plutopid})
  152. if [ -d /proc/${pid} ]; then
  153. kill -TERM ${pid}
  154. RETVAL=$?
  155. sleep 5;
  156. if [ -d /proc/${pid} ]; then
  157. kill -KILL ${pid}
  158. RETVAL=$?
  159. fi
  160. if [ ${RETVAL} -ne 0 ]; then
  161. echo "Kill failed - removing orphaned ${plutopid}"
  162. fi
  163. else
  164. echo "Removing orphaned ${plutopid}"
  165. fi
  166. rm -f ${plutopid}
  167. fi
  168. ipsec _stackmanager stop
  169. ipsec --stopnflog > /dev/null
  170. # cleaning up backup resolv.conf
  171. if [ -e ${LIBRESWAN_RESOLV_CONF} ]; then
  172. if grep 'Libreswan' ${ORIG_RESOLV_CONF} > /dev/null 2>&1; then
  173. cp ${LIBRESWAN_RESOLV_CONF} ${ORIG_RESOLV_CONF}
  174. fi
  175. rm -f ${LIBRESWAN_RESOLV_CONF}
  176. fi
  177. rm -f ${lockfile}
  178. return ${RETVAL}
  179. }
  180. restart() {
  181. verify_config
  182. stop
  183. start
  184. return $?
  185. }
  186. status() {
  187. local RC
  188. if [ -f ${plutopid} ]; then
  189. if [ -r ${plutopid} ]; then
  190. pid=$(cat ${plutopid})
  191. if [ -n "$pid" -a -d /proc/${pid} ]; then
  192. RC=0 # running
  193. else
  194. RC=1 # not running but pid exists
  195. fi
  196. else
  197. RC=4 # insufficient privileges
  198. fi
  199. fi
  200. if [ -z "${RC}" ]; then
  201. if [ -f ${lockfile} ]; then
  202. RC=2
  203. else
  204. RC=3
  205. fi
  206. fi
  207. case "${RC}" in
  208. 0)
  209. echo "ipsec: pluto (pid ${pid}) is running..."
  210. return 0
  211. ;;
  212. 1)
  213. echo "ipsec: pluto dead but pid file exits"
  214. return 1
  215. ;;
  216. 2)
  217. echo "ipsec: pluto dead but subsys locked"
  218. return 2
  219. ;;
  220. 4)
  221. echo "ipsec: pluto status unknown due to insufficient privileges."
  222. return 4
  223. ;;
  224. esac
  225. echo "ipsec: pluto is stopped"
  226. return 3
  227. }
  228. condrestart() {
  229. verify_config
  230. RETVAL=$?
  231. if [ -f ${lockfile} ]; then
  232. restart
  233. RETVAL=$?
  234. fi
  235. return ${RETVAL}
  236. }
  237. version() {
  238. ipsec version
  239. return $?
  240. }
  241. # do it
  242. case "$1" in
  243. start)
  244. start
  245. RETVAL=$?
  246. ;;
  247. stop)
  248. stop
  249. RETVAL=$?
  250. ;;
  251. restart)
  252. restart
  253. RETVAL=$?
  254. ;;
  255. reload|force-reload)
  256. restart
  257. RETVAL=$?
  258. ;;
  259. condrestart|try-restart)
  260. condrestart
  261. RETVAL=$?
  262. ;;
  263. status)
  264. status
  265. RETVAL=$?
  266. ${IPSEC_EXECDIR}/whack --status 2>/dev/null | grep Total | sed 's/^000\ Total\ //'
  267. ;;
  268. version)
  269. version
  270. RETVAL=$?
  271. ;;
  272. *)
  273. echo "Usage: $0 {start|stop|restart|reload|force-reload|condrestart|try-restart|status|version}"
  274. RETVAL=2
  275. esac
  276. exit ${RETVAL}