sshd.initd 2.8 KB

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