openvpn.initd 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #!/usr/bin/openrc-run
  2. # Copyright 1999-2007 Gentoo Foundation
  3. # Distributed under the terms of the GNU General Public License v2
  4. VPNDIR=${VPNDIR:-/etc/openvpn}
  5. VPN=${SVCNAME#*.}
  6. if [ -n "${VPN}" ] && [ ${SVCNAME} != "openvpn" ]; then
  7. VPNPID="/run/openvpn.${VPN}.pid"
  8. else
  9. VPNPID="/run/openvpn.pid"
  10. fi
  11. VPNCONF="${VPNDIR}/${VPN}.conf"
  12. depend() {
  13. need localmount net
  14. use dns
  15. after bootmisc
  16. }
  17. checkconfig() {
  18. # Linux has good dynamic tun/tap creation
  19. if [ $(uname -s) = "Linux" ] ; then
  20. if [ ! -e /dev/net/tun ]; then
  21. if ! modprobe tun ; then
  22. eerror "TUN/TAP support is not available" \
  23. "in this kernel"
  24. return 1
  25. fi
  26. fi
  27. if [ -h /dev/net/tun ] && [ -c /dev/misc/net/tun ]; then
  28. ebegin "Detected broken /dev/net/tun symlink, fixing..."
  29. rm -f /dev/net/tun
  30. ln -s /dev/misc/net/tun /dev/net/tun
  31. eend $?
  32. fi
  33. return 0
  34. fi
  35. # Other OS's don't, so we rely on a pre-configured interface
  36. # per vpn instance
  37. local ifname=$(sed -n -e 's/[[:space:]]*dev[[:space:]][[:space:]]*\([^[:space:]]*\).*/\1/p' "${VPNCONF}")
  38. if [ -z ${ifname} ] ; then
  39. eerror "You need to specify the interface that this openvpn" \
  40. "instance should use" \
  41. "by using the dev option in ${VPNCONF}"
  42. return 1
  43. fi
  44. if ! ifconfig "${ifname}" >/dev/null 2>/dev/null ; then
  45. # Try and create it
  46. echo > /dev/"${ifname}" >/dev/null
  47. fi
  48. if ! ifconfig "${ifname}" >/dev/null 2>/dev/null ; then
  49. eerror "${VPNCONF} requires interface ${ifname}" \
  50. "but that does not exist"
  51. return 1
  52. fi
  53. }
  54. start() {
  55. # If we are re-called by the openvpn gentoo-up.sh script
  56. # then we don't actually want to start openvpn
  57. [ "${IN_BACKGROUND}" = "true" ] && return 0
  58. ebegin "Starting ${SVCNAME}"
  59. checkconfig || return 1
  60. local args="" reenter=${RE_ENTER:-no}
  61. # If the config file does not specify the cd option, we do
  62. # But if we specify it, we override the config option which we do not want
  63. if ! grep -q "^[ ]*cd[ ].*" "${VPNCONF}" ; then
  64. args="${args} --cd ${VPNDIR}"
  65. fi
  66. # We mark the service as inactive and then start it.
  67. # When we get an authenticated packet from the peer then we run our script
  68. # which configures our DNS if any and marks us as up.
  69. if [ "${DETECT_CLIENT:-yes}" = "yes" ] && \
  70. grep -q "^[ ]*remote[ ].*" "${VPNCONF}" ; then
  71. reenter="yes"
  72. args="${args} --up-delay --up-restart"
  73. args="${args} --script-security 2"
  74. args="${args} --up /etc/openvpn/up.sh"
  75. args="${args} --down-pre --down /etc/openvpn/down.sh"
  76. # Warn about setting scripts as we override them
  77. if grep -Eq "^[ ]*(up|down)[ ].*" "${VPNCONF}" ; then
  78. ewarn "WARNING: You have defined your own up/down scripts"
  79. ewarn "As you're running as a client, we now force Gentoo specific"
  80. ewarn "scripts to be run for up and down events."
  81. ewarn "These scripts will call /etc/openvpn/${SVCNAME}-{up,down}.sh"
  82. ewarn "where you can put your own code."
  83. fi
  84. # Warn about the inability to change ip/route/dns information when
  85. # dropping privs
  86. if grep -q "^[ ]*user[ ].*" "${VPNCONF}" ; then
  87. ewarn "WARNING: You are dropping root privileges!"
  88. ewarn "As such openvpn may not be able to change ip, routing"
  89. ewarn "or DNS configuration."
  90. fi
  91. else
  92. # So we're a server. Run as openvpn unless otherwise specified
  93. grep -q "^[ ]*user[ ].*" "${VPNCONF}" || args="${args} --user openvpn"
  94. grep -q "^[ ]*group[ ].*" "${VPNCONF}" || args="${args} --group openvpn"
  95. fi
  96. # Ensure that our scripts get the PEER_DNS variable
  97. [ -n "${PEER_DNS}" ] && args="${args} --setenv PEER_DNS ${PEER_DNS}"
  98. [ "${reenter}" = "yes" ] && mark_service_inactive "${SVCNAME}"
  99. start-stop-daemon --start --exec /usr/bin/openvpn --pidfile "${VPNPID}" \
  100. -- --config "${VPNCONF}" --writepid "${VPNPID}" --daemon \
  101. --setenv SVCNAME "${SVCNAME}" ${args}
  102. eend $? "Check your logs to see why startup failed"
  103. }
  104. stop() {
  105. # If we are re-called by the openvpn gentoo-down.sh script
  106. # then we don't actually want to stop openvpn
  107. if [ "${IN_BACKGROUND}" = "true" ] ; then
  108. mark_service_inactive "${SVCNAME}"
  109. return 0
  110. fi
  111. ebegin "Stopping ${SVCNAME}"
  112. start-stop-daemon --stop --quiet \
  113. --exec /usr/bin/openvpn --pidfile "${VPNPID}"
  114. eend $?
  115. }
  116. # vim: set ts=4 :