openconnect.init.in 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #!/sbin/openrc-run
  2. # Copyright 1999-2012 Gentoo Foundation
  3. # Distributed under the terms of the GNU General Public License v2
  4. VPN="${RC_SVCNAME#*.}"
  5. VPNLOG="/var/log/openconnect/${VPN}"
  6. VPNLOGFILE="${VPNLOG}/openconnect.log"
  7. VPNERRFILE="${VPNLOG}/openconnect.err"
  8. VPNPID="/run/openconnect/${VPN}.pid"
  9. VPNDIR="/etc/openconnect/${VPN}"
  10. PREUPSCRIPT="${VPNDIR}/preup.sh"
  11. PREDOWNSCRIPT="${VPNDIR}/predown.sh"
  12. POSTUPSCRIPT="${VPNDIR}/postup.sh"
  13. POSTDOWNSCRIPT="${VPNDIR}/postdown.sh"
  14. SERVER="server_${VPN}"
  15. PASSWORD="password_${VPN}"
  16. VPNOPTS="vpnopts_${VPN}"
  17. depend() {
  18. before netmount
  19. }
  20. checkconfig() {
  21. if [ $VPN = "openconnect" ]; then
  22. eerror "You cannot call openconnect directly. You must create a symbolic link to it with the vpn name:"
  23. echo
  24. eerror "ln -s /etc/init.d/openconnect /etc/init.d/openconnect.vpn0"
  25. echo
  26. eerror "And then call it instead:"
  27. echo
  28. eerror "/etc/init.d/openconnect.vpn0 start"
  29. return 1
  30. fi
  31. }
  32. checktuntap() {
  33. if [ $(uname -s) = "Linux" ] ; then
  34. if [ ! -e /dev/net/tun ]; then
  35. if ! modprobe tun ; then
  36. eerror "TUN/TAP support is not available in this kernel"
  37. return 1
  38. fi
  39. fi
  40. if [ -h /dev/net/tun ] && [ -c /dev/misc/net/tun ]; then
  41. ebegin "Detected broken /dev/net/tun symlink, fixing..."
  42. rm -f /dev/net/tun
  43. ln -s /dev/misc/net/tun /dev/net/tun
  44. eend $?
  45. fi
  46. fi
  47. }
  48. start() {
  49. ebegin "Starting OpenConnect: ${VPN}"
  50. checkconfig || return 1
  51. checktuntap || return 1
  52. if [ "${!SERVER}" == "vpn.server.tld" ]; then
  53. eend 1 "${VPN} not configured"
  54. return 1
  55. fi
  56. if [ ! -e "${VPNLOG}" ]; then
  57. mkdir -p "${VPNLOG}"
  58. fi
  59. local piddir="${VPNPID%/*}"
  60. if [ ! -d "$piddir" ] ; then
  61. mkdir -p "$piddir"
  62. if [ $? -ne 0 ]; then
  63. eerror "Directory $piddir for pidfile does not exist and cannot be created"
  64. return 1
  65. fi
  66. fi
  67. if [ -x "${PREUPSCRIPT}" ] ; then
  68. "${PREUPSCRIPT}"
  69. fi
  70. start-stop-daemon --start --make-pidfile --pidfile "${VPNPID}" --stderr "${VPNERRFILE}" --stdout "${VPNLOGFILE}" --background \
  71. --exec /usr/sbin/openconnect \
  72. -- --pid-file="${VPNPID}" ${!VPNOPTS} ${!SERVER} <<< ${!PASSWORD}
  73. local retval=$?
  74. if [ ! ${retval} -eq 0 ]; then
  75. eend ${retval}
  76. return ${retval}
  77. fi
  78. if [ -x "${POSTUPSCRIPT}" ] ; then
  79. "${POSTUPSCRIPT}"
  80. fi
  81. eend $?
  82. }
  83. stop() {
  84. ebegin "Stopping OpenConnect: ${VPN}"
  85. checkconfig || return 1
  86. if [ -x "${PREDOWNSCRIPT}" ] ; then
  87. "${PREDOWNSCRIPT}"
  88. fi
  89. start-stop-daemon --pidfile "${VPNPID}" --stop /usr/sbin/openconnect
  90. local retval=$?
  91. if [ ! ${retval} -eq 0 ]; then
  92. eend ${retval}
  93. return ${retval}
  94. fi
  95. if [ -x "${POSTDOWNSCRIPT}" ] ; then
  96. "${POSTDOWNSCRIPT}"
  97. fi
  98. eend $?
  99. }