sshd_config.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package fsdriver
  2. import (
  3. "fmt"
  4. "strings"
  5. )
  6. func genSshdConfig(lines []string, loginAccount, loginPassword string, sshPort int) []string {
  7. var permitRootLogin, passwordAuthentication, port string
  8. if loginAccount == "root" {
  9. permitRootLogin = "PermitRootLogin yes"
  10. }
  11. if len(loginPassword) > 0 {
  12. passwordAuthentication = "PasswordAuthentication yes"
  13. }
  14. if sshPort > 0 && sshPort != 22 {
  15. port = fmt.Sprintf("Port %d", sshPort)
  16. }
  17. for i := range lines {
  18. line := strings.TrimSpace(lines[i])
  19. if strings.HasPrefix(line, "PermitRootLogin") {
  20. if len(permitRootLogin) > 0 {
  21. line = permitRootLogin
  22. permitRootLogin = ""
  23. }
  24. } else if strings.HasPrefix(line, "PasswordAuthentication") {
  25. if len(passwordAuthentication) > 0 {
  26. line = passwordAuthentication
  27. passwordAuthentication = ""
  28. }
  29. } else if strings.HasPrefix(line, "Port") {
  30. if len(port) > 0 {
  31. line = port
  32. port = ""
  33. }
  34. }
  35. lines[i] = line
  36. }
  37. if len(permitRootLogin) > 0 {
  38. lines = append(lines, permitRootLogin)
  39. }
  40. if len(passwordAuthentication) > 0 {
  41. lines = append(lines, passwordAuthentication)
  42. }
  43. if len(port) > 0 {
  44. lines = append(lines, port)
  45. }
  46. return lines
  47. }