You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

doctor.go 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package cmd
  5. import (
  6. "bufio"
  7. "errors"
  8. "fmt"
  9. "os"
  10. "os/exec"
  11. "path/filepath"
  12. "regexp"
  13. "strings"
  14. "code.gitea.io/gitea/modules/setting"
  15. "github.com/urfave/cli"
  16. )
  17. // CmdDoctor represents the available doctor sub-command.
  18. var CmdDoctor = cli.Command{
  19. Name: "doctor",
  20. Usage: "Diagnose the problems",
  21. Description: "A command to diagnose the problems of current gitea instance according the given configuration.",
  22. Action: runDoctor,
  23. }
  24. type check struct {
  25. title string
  26. f func(ctx *cli.Context) ([]string, error)
  27. }
  28. // checklist represents list for all checks
  29. var checklist = []check{
  30. {
  31. title: "Check if OpenSSH authorized_keys file id correct",
  32. f: runDoctorLocationMoved,
  33. },
  34. // more checks please append here
  35. }
  36. func runDoctor(ctx *cli.Context) error {
  37. err := initDB()
  38. fmt.Println("Using app.ini at", setting.CustomConf)
  39. if err != nil {
  40. fmt.Println(err)
  41. fmt.Println("Check if you are using the right config file. You can use a --config directive to specify one.")
  42. return nil
  43. }
  44. for i, check := range checklist {
  45. fmt.Println("[", i+1, "]", check.title)
  46. if messages, err := check.f(ctx); err != nil {
  47. fmt.Println("Error:", err)
  48. } else if len(messages) > 0 {
  49. for _, message := range messages {
  50. fmt.Println("-", message)
  51. }
  52. } else {
  53. fmt.Println("OK.")
  54. }
  55. fmt.Println()
  56. }
  57. return nil
  58. }
  59. func exePath() (string, error) {
  60. file, err := exec.LookPath(os.Args[0])
  61. if err != nil {
  62. return "", err
  63. }
  64. return filepath.Abs(file)
  65. }
  66. func runDoctorLocationMoved(ctx *cli.Context) ([]string, error) {
  67. if setting.SSH.StartBuiltinServer || !setting.SSH.CreateAuthorizedKeysFile {
  68. return nil, nil
  69. }
  70. fPath := filepath.Join(setting.SSH.RootPath, "authorized_keys")
  71. f, err := os.Open(fPath)
  72. if err != nil {
  73. return nil, err
  74. }
  75. defer f.Close()
  76. var firstline string
  77. scanner := bufio.NewScanner(f)
  78. for scanner.Scan() {
  79. firstline = strings.TrimSpace(scanner.Text())
  80. if len(firstline) == 0 || firstline[0] == '#' {
  81. continue
  82. }
  83. break
  84. }
  85. // command="/Volumes/data/Projects/gitea/gitea/gitea --config
  86. if len(firstline) > 0 {
  87. exp := regexp.MustCompile(`^[ \t]*(?:command=")([^ ]+) --config='([^']+)' serv key-([^"]+)",(?:[^ ]+) ssh-rsa ([^ ]+) ([^ ]+)[ \t]*$`)
  88. // command="/home/user/gitea --config='/home/user/etc/app.ini' serv key-999",option-1,option-2,option-n ssh-rsa public-key-value key-name
  89. res := exp.FindStringSubmatch(firstline)
  90. if res == nil {
  91. return nil, errors.New("Unknow authorized_keys format")
  92. }
  93. giteaPath := res[1] // => /home/user/gitea
  94. iniPath := res[2] // => /home/user/etc/app.ini
  95. p, err := exePath()
  96. if err != nil {
  97. return nil, err
  98. }
  99. p, err = filepath.Abs(p)
  100. if err != nil {
  101. return nil, err
  102. }
  103. if len(giteaPath) > 0 && giteaPath != p {
  104. return []string{fmt.Sprintf("Gitea exe path wants %s but %s on %s", p, giteaPath, fPath)}, nil
  105. }
  106. if len(iniPath) > 0 && iniPath != setting.CustomConf {
  107. return []string{fmt.Sprintf("Gitea config path wants %s but %s on %s", setting.CustomConf, iniPath, fPath)}, nil
  108. }
  109. }
  110. return nil, nil
  111. }