_ipsec 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. #! /bin/sh
  2. # prefix command to run stuff from our programs directory
  3. # Copyright (C) 1998-2002 Henry Spencer.
  4. # Copyright (C) 2006-2014 Andreas Steffen
  5. # Copyright (C) 2006 Martin Willi
  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. # define a minimum PATH environment in case it is not set
  17. PATH=${PATH:-"/sbin:/bin:/usr/sbin:/usr/bin"}
  18. export PATH
  19. # set daemon name
  20. [ -z "$DAEMON_NAME" ] && DAEMON_NAME="charon"
  21. # name and version of the ipsec implementation
  22. OS_NAME=`uname -s`
  23. IPSEC_NAME="strongSwan"
  24. IPSEC_VERSION="U5.8.1/K`uname -r`"
  25. # where the private directory and the config files are
  26. IPSEC_DIR="/usr/local/libexec/ipsec"
  27. IPSEC_BINDIR="/usr/local/bin"
  28. IPSEC_SBINDIR="/usr/local/sbin"
  29. IPSEC_CONFDIR="/usr/local/etc"
  30. IPSEC_PIDDIR="/var/run"
  31. IPSEC_SCRIPT="ipsec"
  32. IPSEC_STARTER_PID="${IPSEC_PIDDIR}/starter.${DAEMON_NAME}.pid"
  33. IPSEC_CHARON_PID="${IPSEC_PIDDIR}/${DAEMON_NAME}.pid"
  34. IPSEC_STROKE="${IPSEC_DIR}/stroke"
  35. IPSEC_STARTER="${IPSEC_DIR}/starter"
  36. export IPSEC_DIR IPSEC_BINDIR IPSEC_SBINDIR IPSEC_CONFDIR IPSEC_PIDDIR IPSEC_SCRIPT IPSEC_VERSION IPSEC_NAME IPSEC_STARTER_PID IPSEC_CHARON_PID
  37. IPSEC_DISTRO="University of Applied Sciences Rapperswil, Switzerland"
  38. command_dir="$IPSEC_DIR"
  39. case "$1" in
  40. '')
  41. echo "$IPSEC_SCRIPT command [arguments]"
  42. echo
  43. echo "Use --help for a list of commands, or refer to the $IPSEC_SCRIPT(8) man page."
  44. echo "See <http://www.strongswan.org> for more general information."
  45. exit 0
  46. ;;
  47. --help)
  48. echo "$IPSEC_SCRIPT command [arguments]"
  49. echo
  50. echo "Commands:"
  51. echo " start|restart [arguments]"
  52. echo " update|reload|stop"
  53. echo " up|down|route|unroute <connectionname>"
  54. echo " down-srcip <start> [<end>]"
  55. echo " status|statusall [<connectionname>]"
  56. echo " listalgs|listpubkeys|listcerts [--utc]"
  57. echo " listcacerts|listaacerts|listocspcerts [--utc]"
  58. echo " listacerts|listgroups|listcainfos [--utc]"
  59. echo " listcrls|listocsp|listplugins|listall [--utc]"
  60. echo " listcounters|resetcounters [name]"
  61. echo " leases [<poolname> [<address>]]"
  62. echo " rereadsecrets|rereadcacerts|rereadaacerts"
  63. echo " rereadocspcerts|rereadacerts|rereadcrls|rereadall"
  64. echo " purgecerts|purgecrls|purgeike|purgeocsp"
  65. echo " scepclient|pki"
  66. echo " stroke"
  67. echo " version"
  68. echo
  69. echo "Refer to the $IPSEC_SCRIPT(8) man page for details."
  70. echo "Some commands have their own man pages, e.g. pki(1) or scepclient(8)."
  71. exit 0
  72. ;;
  73. --versioncode)
  74. echo "$IPSEC_VERSION"
  75. exit 0
  76. ;;
  77. --directory)
  78. echo "$IPSEC_DIR"
  79. exit 0
  80. ;;
  81. --confdir)
  82. echo "$IPSEC_CONFDIR"
  83. exit 0
  84. ;;
  85. --piddir)
  86. echo "$IPSEC_PIDDIR"
  87. exit 0
  88. ;;
  89. down)
  90. shift
  91. if [ "$#" -ne 1 ]
  92. then
  93. echo "Usage: $IPSEC_SCRIPT down <connection name>"
  94. exit 2
  95. fi
  96. rc=7
  97. if [ -e $IPSEC_CHARON_PID ]
  98. then
  99. $IPSEC_STROKE down "$1"
  100. rc="$?"
  101. fi
  102. exit "$rc"
  103. ;;
  104. down-srcip)
  105. shift
  106. if [ "$#" -lt 1 ]
  107. then
  108. echo "Usage: $IPSEC_SCRIPT down-srcip <start> [<end>]"
  109. exit 2
  110. fi
  111. rc=7
  112. if [ -e $IPSEC_CHARON_PID ]
  113. then
  114. $IPSEC_STROKE down-srcip $*
  115. rc="$?"
  116. fi
  117. exit "$rc"
  118. ;;
  119. leases)
  120. op="$1"
  121. rc=7
  122. shift
  123. if [ -e $IPSEC_CHARON_PID ]
  124. then
  125. case "$#" in
  126. 0) $IPSEC_STROKE "$op" ;;
  127. 1) $IPSEC_STROKE "$op" "$1" ;;
  128. *) $IPSEC_STROKE "$op" "$1" "$2" ;;
  129. esac
  130. rc="$?"
  131. fi
  132. exit "$rc"
  133. ;;
  134. listalgs|listpubkeys|listplugins|\
  135. listcerts|listcacerts|listaacerts|\
  136. listacerts|listgroups|listocspcerts|\
  137. listcainfos|listcrls|listocsp|listall|\
  138. rereadsecrets|rereadcacerts|rereadaacerts|\
  139. rereadacerts|rereadocspcerts|rereadcrls|\
  140. rereadall|purgeocsp|listcounters|resetcounters)
  141. op="$1"
  142. rc=7
  143. shift
  144. if [ -e $IPSEC_CHARON_PID ]
  145. then
  146. $IPSEC_STROKE "$op" "$@"
  147. rc="$?"
  148. fi
  149. exit "$rc"
  150. ;;
  151. purgeike|purgecrls|purgecerts)
  152. rc=7
  153. if [ -e $IPSEC_CHARON_PID ]
  154. then
  155. $IPSEC_STROKE "$1"
  156. rc="$?"
  157. fi
  158. exit "$rc"
  159. ;;
  160. reload)
  161. rc=7
  162. if [ -e $IPSEC_STARTER_PID ]
  163. then
  164. echo "Reloading strongSwan IPsec configuration..." >&2
  165. kill -USR1 `cat $IPSEC_STARTER_PID` 2>/dev/null && rc=0
  166. else
  167. echo "Reloading strongSwan IPsec failed: starter is not running" >&2
  168. fi
  169. exit "$rc"
  170. ;;
  171. restart)
  172. $IPSEC_SBINDIR/$IPSEC_SCRIPT stop
  173. sleep 2
  174. shift
  175. exec $IPSEC_SBINDIR/$IPSEC_SCRIPT start "$@"
  176. ;;
  177. route|unroute)
  178. op="$1"
  179. rc=7
  180. shift
  181. if [ "$#" -ne 1 ]
  182. then
  183. echo "Usage: $IPSEC_SCRIPT $op <connection name>"
  184. exit 2
  185. fi
  186. if [ -e $IPSEC_CHARON_PID ]
  187. then
  188. $IPSEC_STROKE "$op" "$1"
  189. rc="$?"
  190. fi
  191. exit "$rc"
  192. ;;
  193. secrets)
  194. rc=7
  195. if [ -e $IPSEC_CHARON_PID ]
  196. then
  197. $IPSEC_STROKE rereadsecrets
  198. rc="$?"
  199. fi
  200. exit "$rc"
  201. ;;
  202. start)
  203. shift
  204. if [ -d /var/lock/subsys ]; then
  205. touch /var/lock/subsys/ipsec
  206. fi
  207. exec $IPSEC_STARTER --daemon $DAEMON_NAME "$@"
  208. ;;
  209. status|statusall)
  210. op="$1"
  211. # Return value is slightly different for the status command:
  212. # 0 - service up and running
  213. # 1 - service dead, but /var/run/ pid file exists
  214. # 2 - service dead, but /var/lock/ lock file exists
  215. # 3 - service not running (unused)
  216. # 4 - service status unknown :-(
  217. # 5--199 reserved (5--99 LSB, 100--149 distro, 150--199 appl.)
  218. shift
  219. if [ $# -eq 0 ]
  220. then
  221. if [ -e $IPSEC_CHARON_PID ]
  222. then
  223. $IPSEC_STROKE "$op"
  224. fi
  225. else
  226. if [ -e $IPSEC_CHARON_PID ]
  227. then
  228. $IPSEC_STROKE "$op" "$1"
  229. fi
  230. fi
  231. if [ -e $IPSEC_STARTER_PID ]
  232. then
  233. kill -0 `cat $IPSEC_STARTER_PID` 2>/dev/null
  234. exit $?
  235. fi
  236. exit 3
  237. ;;
  238. stop)
  239. # stopping a not-running service is considered as success
  240. if [ -e $IPSEC_STARTER_PID ]
  241. then
  242. echo "Stopping strongSwan IPsec..." >&2
  243. spid=`cat $IPSEC_STARTER_PID`
  244. if [ -n "$spid" ]
  245. then
  246. kill $spid 2>/dev/null
  247. loop=110
  248. while [ $loop -gt 0 ] ; do
  249. kill -0 $spid 2>/dev/null || break
  250. sleep 0.1 2>/dev/null
  251. if [ $? -ne 0 ]
  252. then
  253. sleep 1
  254. loop=$(($loop - 9))
  255. fi
  256. loop=$(($loop - 1))
  257. done
  258. if [ $loop -le 0 ]
  259. then
  260. kill -KILL $spid 2>/dev/null
  261. rm -f $IPSEC_STARTER_PID
  262. fi
  263. fi
  264. else
  265. echo "Stopping strongSwan IPsec failed: starter is not running" >&2
  266. fi
  267. if [ -d /var/lock/subsys ]; then
  268. rm -f /var/lock/subsys/ipsec
  269. fi
  270. exit 0
  271. ;;
  272. up)
  273. shift
  274. if [ "$#" -ne 1 ]
  275. then
  276. echo "Usage: $IPSEC_SCRIPT up <connection name>"
  277. exit 2
  278. fi
  279. rc=7
  280. if [ -e $IPSEC_CHARON_PID ]
  281. then
  282. $IPSEC_STROKE up "$1"
  283. rc="$?"
  284. fi
  285. exit "$rc"
  286. ;;
  287. update)
  288. if [ -e $IPSEC_STARTER_PID ]
  289. then
  290. echo "Updating strongSwan IPsec configuration..." >&2
  291. kill -HUP `cat $IPSEC_STARTER_PID`
  292. exit 0
  293. else
  294. echo "Updating strongSwan IPsec failed: starter is not running" >&2
  295. exit 7
  296. fi
  297. ;;
  298. aikgen|pki)
  299. # programs in BINDIR may be called directly, these two are listed for legacy reasons
  300. command_dir="$IPSEC_BINDIR"
  301. # fall through
  302. ;;
  303. attest|conftest|dumm|irdumm|pool|pt-tls-client|scepclient|stroke|\
  304. duplicheck|error-notify|imv_policy_manager|load-tester|lookip|whitelist|\
  305. _updown|_imv_policy)
  306. # fall through
  307. ;;
  308. copyright|--copyright)
  309. set _copyright
  310. # fall through
  311. ;;
  312. version|--version)
  313. printf "$OS_NAME $IPSEC_NAME $IPSEC_VERSION\n"
  314. printf "$IPSEC_DISTRO\n"
  315. printf "See '$IPSEC_SCRIPT --copyright' for copyright information.\n"
  316. exit 0
  317. ;;
  318. --*)
  319. echo "$0: unknown option \`$1' (perhaps command name was omitted?)" >&2
  320. exit 2
  321. ;;
  322. *)
  323. echo "$0: unknown command \`$1' (\`$IPSEC_SCRIPT --help' for list)" >&2
  324. exit 2
  325. ;;
  326. esac
  327. cmd="$1"
  328. shift
  329. path="$command_dir/$cmd"
  330. if [ ! -x "$path" ]
  331. then
  332. echo "$0: unknown command \`$cmd' (\`$IPSEC_SCRIPT --help' for list)" >&2
  333. exit 2
  334. fi
  335. exec $path "$@"