From a4c12358c23112cdc2792ca07ddeeb3ce00aab9f Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Sat, 23 Nov 2019 16:57:59 +0800 Subject: [PATCH] use regex match authorized_keys on doctor --- cmd/doctor.go | 51 ++++++++++++++++++++++++++++----------------------- 1 file changed, 28 insertions(+), 23 deletions(-) diff --git a/cmd/doctor.go b/cmd/doctor.go index 0850bd184ac4..90a6bcd2fe96 100644 --- a/cmd/doctor.go +++ b/cmd/doctor.go @@ -10,6 +10,7 @@ import ( "os" "os/exec" "path/filepath" + "regexp" "strings" "code.gitea.io/gitea/modules/setting" @@ -32,14 +33,18 @@ type check struct { var checklist = []check{ { - title: "Check if openssh authorized_keys file correct", + title: "Check if openssh authorized_keys file id correct", f: runDoctorLocationMoved, }, } func runDoctor(ctx *cli.Context) error { - if err := initDB(); err != nil { - return err + err := initDB() + fmt.Println("Using app.ini at ", setting.CustomConf) + if err != nil { + fmt.Println(err) + fmt.Println("Check if you are using the right config file. You can use a --config directive to specify one.") + return nil } for i, check := range checklist { @@ -90,28 +95,28 @@ func runDoctorLocationMoved(ctx *cli.Context) ([]string, error) { // command="/Volumes/data/Projects/gitea/gitea/gitea --config if len(firstline) > 0 { - var start, end int - for i, c := range firstline { - if c == ' ' { - end = i - break - } else if c == '"' { - start = i + 1 - } + exp := regexp.MustCompile(`^[ \t]*(?:command=")([^ ]+) --config='([^']+)' serv key-([^"]+)",(?:[^ ]+) ssh-rsa ([^ ]+) ([^ ]+)[ \t]*$`) + + // 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 + res := exp.FindAllStringSubmatch(firstline, -1) + + giteaPath := res[1] // => /home/user/gitea + iniPath := res[2] // => /home/user/etc/app.ini + + p, err := exePath() + if err != nil { + return nil, err + } + p, err = filepath.Abs(p) + if err != nil { + return nil, err } - if start > 0 && end > 0 { - p, err := exePath() - if err != nil { - return nil, err - } - p, err = filepath.Abs(p) - if err != nil { - return nil, err - } - if firstline[start:end] != p { - return []string{fmt.Sprintf("Wants %s but %s on %s", p, firstline[start:end], fPath)}, nil - } + if len(giteaPath) > 0 && giteaPath[0] != p { + return []string{fmt.Sprintf("Gitea exe path wants %s but %s on %s", p, giteaPath[0], fPath)}, nil + } + if len(iniPath) > 0 && iniPath[0] != setting.CustomConf { + return []string{fmt.Sprintf("Gitea config path wants %s but %s on %s", setting.CustomConf, iniPath[0], fPath)}, nil } }