Skip to content

Commit

Permalink
use regex match authorized_keys on doctor
Browse files Browse the repository at this point in the history
  • Loading branch information
lunny committed Jan 11, 2020
1 parent eb462b1 commit a4c1235
Showing 1 changed file with 28 additions and 23 deletions.
51 changes: 28 additions & 23 deletions cmd/doctor.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"

"code.gitea.io/gitea/modules/setting"
Expand All @@ -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 {
Expand Down Expand Up @@ -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
}
}

Expand Down

0 comments on commit a4c1235

Please sign in to comment.