ipsec.init 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. #!/bin/sh /etc/rc.common
  2. # IPsec startup and shutdown script
  3. # Copyright (C) 1998, 1999, 2001 Henry Spencer.
  4. # Copyright (C) 2002 Michael Richardson <[email protected]>
  5. # Copyright (C) 2006 OpenWrt.org
  6. #
  7. # This program is free software; you can redistribute it and/or modify it
  8. # under the terms of the GNU General Public License as published by the
  9. # Free Software Foundation; either version 2 of the License, or (at your
  10. # option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  11. #
  12. # This program is distributed in the hope that it will be useful, but
  13. # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  14. # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  15. # for more details.
  16. #
  17. # RCSID $Id: setup.in,v 1.122.6.1 2005/07/25 19:17:03 ken Exp $
  18. #
  19. # ipsec init.d script for starting and stopping
  20. # the IPsec security subsystem (KLIPS and Pluto).
  21. #
  22. # This script becomes /etc/rc.d/init.d/ipsec (or possibly /etc/init.d/ipsec)
  23. # and is also accessible as "ipsec setup" (the preferred route for human
  24. # invocation).
  25. #
  26. # The startup and shutdown times are a difficult compromise (in particular,
  27. # it is almost impossible to reconcile them with the insanely early/late
  28. # times of NFS filesystem startup/shutdown). Startup is after startup of
  29. # syslog and pcmcia support; shutdown is just before shutdown of syslog.
  30. #
  31. # chkconfig: 2345 47 76
  32. # description: IPsec provides encrypted and authenticated communications; \
  33. # KLIPS is the kernel half of it, Pluto is the user-level management daemon.
  34. START=60
  35. script_init() {
  36. me='ipsec setup' # for messages
  37. # where the private directory and the config files are
  38. IPSEC_EXECDIR="${IPSEC_EXECDIR-/usr/libexec/ipsec}"
  39. IPSEC_LIBDIR="${IPSEC_LIBDIR-/usr/lib/ipsec}"
  40. IPSEC_SBINDIR="${IPSEC_SBINDIR-/usr/sbin}"
  41. IPSEC_CONFS="${IPSEC_CONFS-/etc}"
  42. if test " $IPSEC_DIR" = " " # if we were not called by the ipsec command
  43. then
  44. # we must establish a suitable PATH ourselves
  45. PATH="${IPSEC_SBINDIR}":/sbin:/usr/sbin:/usr/local/bin:/bin:/usr/bin
  46. export PATH
  47. IPSEC_DIR="$IPSEC_LIBDIR"
  48. export IPSEC_DIR IPSEC_CONFS IPSEC_LIBDIR IPSEC_EXECDIR
  49. fi
  50. # Check that the ipsec command is available.
  51. found=
  52. for dir in `echo $PATH | tr ':' ' '`
  53. do
  54. if test -f $dir/ipsec -a -x $dir/ipsec
  55. then
  56. found=yes
  57. break # NOTE BREAK OUT
  58. fi
  59. done
  60. if ! test "$found"
  61. then
  62. echo "cannot find ipsec command -- \`$1' aborted" |
  63. logger -s -p daemon.error -t ipsec_setup
  64. exit 1
  65. fi
  66. # Pick up IPsec configuration (until we have done this, successfully, we
  67. # do not know where errors should go, hence the explicit "daemon.error"s.)
  68. # Note the "--export", which exports the variables created.
  69. eval `ipsec _confread $config --optional --varprefix IPSEC --export --type config setup`
  70. if test " $IPSEC_confreadstatus" != " "
  71. then
  72. case $1 in
  73. stop|--stop|_autostop)
  74. echo "$IPSEC_confreadstatus -- \`$1' may not work" |
  75. logger -s -p daemon.error -t ipsec_setup;;
  76. *) echo "$IPSEC_confreadstatus -- \`$1' aborted" |
  77. logger -s -p daemon.error -t ipsec_setup;
  78. exit 1;;
  79. esac
  80. fi
  81. IPSEC_confreadsection=${IPSEC_confreadsection:-setup}
  82. export IPSEC_confreadsection
  83. IPSECsyslog=${IPSECsyslog-daemon.error}
  84. export IPSECsyslog
  85. # misc setup
  86. umask 022
  87. mkdir -p /var/run/pluto
  88. }
  89. script_command() {
  90. if [ "${USER}" != "root" ]
  91. then
  92. echo "permission denied (must be superuser)" |
  93. logger -s -p $IPSECsyslog -t ipsec_setup 2>&1
  94. exit 1
  95. fi
  96. # make sure all required directories exist
  97. if [ ! -d /var/run/pluto ]
  98. then
  99. mkdir -p /var/run/pluto
  100. fi
  101. if [ ! -d /var/lock/subsys ]
  102. then
  103. mkdir -p /var/lock/subsys
  104. fi
  105. tmp=/var/run/pluto/ipsec_setup.st
  106. outtmp=/var/run/pluto/ipsec_setup.out
  107. (
  108. ipsec _realsetup $1
  109. echo "$?" >$tmp
  110. ) > ${outtmp} 2>&1
  111. st=$?
  112. if test -f $tmp
  113. then
  114. st=`cat $tmp`
  115. rm -f $tmp
  116. fi
  117. if [ -f ${outtmp} ]; then
  118. cat ${outtmp} | logger -s -p $IPSECsyslog -t ipsec_setup 2>&1
  119. rm -f ${outtmp}
  120. fi
  121. }
  122. start() {
  123. script_init start "$@"
  124. script_command start "$@"
  125. }
  126. stop() {
  127. script_init stop "$@"
  128. script_command stop "$@"
  129. }
  130. restart() {
  131. script_init stop "$@"
  132. script_command stop "$@"
  133. script_command start "$@"
  134. }
  135. status() {
  136. script_init status "$@"
  137. ipsec _realsetup status
  138. }
  139. EXTRA_COMMANDS=status
  140. EXTRA_HELP=" status Show the status of the service"