sshd-r1.initd 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #!/sbin/openrc-run
  2. # Copyright 1999-2019 Gentoo Authors
  3. # Distributed under the terms of the GNU General Public License v2
  4. extra_commands="checkconfig"
  5. extra_started_commands="reload"
  6. : ${SSHD_CONFDIR:=${RC_PREFIX%/}/etc/ssh}
  7. : ${SSHD_CONFIG:=${SSHD_CONFDIR}/sshd_config}
  8. : ${SSHD_PIDFILE:=${RC_PREFIX%/}/run/${SVCNAME}.pid}
  9. : ${SSHD_BINARY:=${RC_PREFIX%/}/usr/sbin/sshd}
  10. : ${SSHD_KEYGEN_BINARY:=${RC_PREFIX%/}/usr/bin/ssh-keygen}
  11. command="${SSHD_BINARY}"
  12. pidfile="${SSHD_PIDFILE}"
  13. command_args="${SSHD_OPTS} -o PidFile=${pidfile} -f ${SSHD_CONFIG}"
  14. # Wait one second (length chosen arbitrarily) to see if sshd actually
  15. # creates a PID file, or if it crashes for some reason like not being
  16. # able to bind to the address in ListenAddress (bug 617596).
  17. : ${SSHD_SSD_OPTS:=--wait 1000}
  18. start_stop_daemon_args="${SSHD_SSD_OPTS}"
  19. depend() {
  20. # Entropy can be used by ssh-keygen, among other things, but
  21. # is not strictly required (bug 470020).
  22. use logger dns entropy
  23. if [ "${rc_need+set}" = "set" ] ; then
  24. : # Do nothing, the user has explicitly set rc_need
  25. else
  26. local x warn_addr
  27. for x in $(awk '/^ListenAddress/{ print $2 }' "$SSHD_CONFIG" 2>/dev/null) ; do
  28. case "${x}" in
  29. 0.0.0.0|0.0.0.0:*) ;;
  30. ::|\[::\]*) ;;
  31. *) warn_addr="${warn_addr} ${x}" ;;
  32. esac
  33. done
  34. if [ -n "${warn_addr}" ] ; then
  35. need net
  36. ewarn "You are binding an interface in ListenAddress statement in your sshd_config!"
  37. ewarn "You must add rc_need=\"net.FOO\" to your ${RC_PREFIX%/}/etc/conf.d/sshd"
  38. ewarn "where FOO is the interface(s) providing the following address(es):"
  39. ewarn "${warn_addr}"
  40. fi
  41. fi
  42. }
  43. checkconfig() {
  44. checkpath --mode 0755 --directory "${RC_PREFIX%/}/var/empty"
  45. if [ ! -e "${SSHD_CONFIG}" ] ; then
  46. eerror "You need an ${SSHD_CONFIG} file to run sshd"
  47. eerror "There is a sample file in /usr/share/doc/openssh"
  48. return 1
  49. fi
  50. ${SSHD_KEYGEN_BINARY} -A || return 2
  51. "${command}" -t ${command_args} || return 3
  52. }
  53. start_pre() {
  54. # Make sure that the user's config isn't busted before we try
  55. # to start the daemon (this will produce better error messages
  56. # than if we just try to start it blindly).
  57. #
  58. # We always need to call checkconfig because this function will
  59. # also generate any missing host key and you can start a
  60. # non-running service with "restart" argument.
  61. checkconfig || return $?
  62. }
  63. stop_pre() {
  64. # If this is a restart, check to make sure the user's config
  65. # isn't busted before we stop the running daemon.
  66. if [ "${RC_CMD}" = "restart" ] ; then
  67. checkconfig || return $?
  68. fi
  69. }
  70. reload() {
  71. checkconfig || return $?
  72. ebegin "Reloading ${SVCNAME}"
  73. start-stop-daemon --signal HUP --pidfile "${pidfile}"
  74. eend $?
  75. }