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