You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

207 lines
4.4 KiB

  1. #!/bin/sh /etc/rc.common
  2. START=90
  3. STOP=10
  4. #USE_PROCD=1
  5. . $IPKG_INSTROOT/lib/functions.sh
  6. EXTRA_COMMANDS=status
  7. EXTRA_HELP=" status Show the status of the service"
  8. # Check that networking is up.
  9. [ "${NETWORKING}" = "no" ] && exit 6
  10. if [ $(id -u) -ne 0 ]; then
  11. echo "permission denied (must be superuser)" | \
  12. logger -s -p daemon.error -t ipsec_setup 2>&1
  13. exit 4
  14. fi
  15. # where the private directory and the config files are
  16. IPSEC_EXECDIR="${IPSEC_EXECDIR-/usr/libexec/ipsec}"
  17. IPSEC_SBINDIR="${IPSEC_SBINDIR-/usr/sbin}"
  18. IPSEC_CONF="${IPSEC_CONF-/etc/ipsec.conf}"
  19. unset PLUTO_OPTIONS
  20. rundir=/var/run/pluto
  21. plutopid=${rundir}/pluto.pid
  22. plutoctl=${rundir}/pluto.ctl
  23. lockdir=/var/lock
  24. lockfile=${lockdir}/ipsec
  25. ipsecversion=/proc/net/ipsec_version
  26. kamepfkey=/proc/net/pfkey
  27. # /etc/resolv.conf related paths
  28. LIBRESWAN_RESOLV_CONF=${rundir}/libreswan-resolv-conf-backup
  29. ORIG_RESOLV_CONF=/etc/resolv.conf
  30. # misc setup
  31. umask 022
  32. # standardize PATH, and export it for everything else's benefit
  33. PATH="${IPSEC_SBINDIR}":/sbin:/usr/sbin:/usr/local/bin:/bin:/usr/bin
  34. export PATH
  35. mkdir -p ${rundir}
  36. chmod 700 ${rundir}
  37. verify_config() {
  38. [ -f ${IPSEC_CONF} ] || exit 6
  39. config_error=$(ipsec addconn --config ${IPSEC_CONF} --checkconfig 2>&1)
  40. RETVAL=$?
  41. if [ ${RETVAL} -gt 0 ]; then
  42. echo "Configuration error - the following error occurred:"
  43. echo ${config_error}
  44. echo "IKE daemon status was not modified"
  45. exit ${RETVAL}
  46. fi
  47. }
  48. start() {
  49. echo -n "Starting pluto IKE daemon for IPsec: "
  50. ipsec _stackmanager start
  51. # pluto searches the current directory, so this is required for making it selinux compliant
  52. cd /
  53. # Create nss db or convert from old format to new sql format
  54. ipsec --checknss
  55. # Enable nflog if configured
  56. ipsec --checknflog > /dev/null
  57. # This script will enter an endless loop to ensure pluto restarts on crash
  58. ipsec _plutorun --config ${IPSEC_CONF} --nofork ${PLUTO_OPTIONS} & [ -d ${lockdir} ] || mkdir -p ${lockdir}
  59. touch ${lockfile}
  60. # Because _plutorun starts pluto at background we need to make sure pluto is started
  61. # before we know if start was successful or not
  62. for waitsec in 1 2 3 4 5; do
  63. if status >/dev/null; then
  64. RETVAL=0
  65. break
  66. else
  67. echo -n "."
  68. sleep 1
  69. RETVAL=1
  70. fi
  71. done
  72. if [ ${RETVAL} -ge 1 ]; then
  73. rm -f ${lockfile}
  74. fi
  75. echo
  76. return ${RETVAL}
  77. }
  78. stop() {
  79. if [ -e ${plutoctl} ]; then
  80. echo "Shutting down pluto IKE daemon"
  81. ipsec whack --shutdown 2>/dev/null
  82. # don't use seq, might not exist on embedded
  83. for waitsec in 1 2 3 4 5 6 7 8 9 10; do
  84. if [ -s ${plutopid} ]; then
  85. echo -n "."
  86. sleep 1
  87. else
  88. break
  89. fi
  90. done
  91. echo
  92. rm -f ${plutoctl} # we won't be using this anymore
  93. fi
  94. if [ -s ${plutopid} ]; then
  95. # pluto did not die peacefully
  96. pid=$(cat ${plutopid})
  97. if [ -d /proc/${pid} ]; then
  98. kill -TERM ${pid}
  99. RETVAL=$?
  100. sleep 5;
  101. if [ -d /proc/${pid} ]; then
  102. kill -KILL ${pid}
  103. RETVAL=$?
  104. fi
  105. if [ ${RETVAL} -ne 0 ]; then
  106. echo "Kill failed - removing orphaned ${plutopid}"
  107. fi
  108. else
  109. echo "Removing orphaned ${plutopid}"
  110. fi
  111. rm -f ${plutopid}
  112. fi
  113. ipsec _stackmanager stop
  114. ipsec --stopnflog > /dev/null
  115. # cleaning up backup resolv.conf
  116. if [ -e ${LIBRESWAN_RESOLV_CONF} ]; then
  117. if grep 'Libreswan' ${ORIG_RESOLV_CONF} > /dev/null 2>&1; then
  118. cp ${LIBRESWAN_RESOLV_CONF} ${ORIG_RESOLV_CONF}
  119. fi
  120. rm -f ${LIBRESWAN_RESOLV_CONF}
  121. fi
  122. rm -f ${lockfile}
  123. return ${RETVAL}
  124. }
  125. restart() {
  126. verify_config
  127. stop
  128. start
  129. return $?
  130. }
  131. status() {
  132. local RC
  133. if [ -f ${plutopid} ]; then
  134. if [ -r ${plutopid} ]; then
  135. pid=$(cat ${plutopid})
  136. if [ -n "$pid" -a -d /proc/${pid} ]; then
  137. RC=0 # running
  138. else
  139. RC=1 # not running but pid exists
  140. fi
  141. else
  142. RC=4 # insufficient privileges
  143. fi
  144. fi
  145. if [ -z "${RC}" ]; then
  146. if [ -f ${lockfile} ]; then
  147. RC=2
  148. else
  149. RC=3
  150. fi
  151. fi
  152. case "${RC}" in
  153. 0)
  154. echo "ipsec: pluto (pid ${pid}) is running..."
  155. return 0
  156. ;;
  157. 1)
  158. echo "ipsec: pluto dead but pid file exits"
  159. return 1
  160. ;;
  161. 2)
  162. echo "ipsec: pluto dead but subsys locked"
  163. return 2
  164. ;;
  165. 4)
  166. echo "ipsec: pluto status unknown due to insufficient privileges."
  167. return 4
  168. ;;
  169. esac
  170. echo "ipsec: pluto is stopped"
  171. return 3
  172. }
  173. condrestart() {
  174. verify_config
  175. RETVAL=$?
  176. if [ -f ${lockfile} ]; then
  177. restart
  178. RETVAL=$?
  179. fi
  180. return ${RETVAL}
  181. }
  182. version() {
  183. ipsec version
  184. return $?
  185. }