From 28d64ae922e1104154c8de9d31d6235dbc93a10e Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Tue, 2 May 2017 22:57:34 +0200 Subject: safePath --- file/names.go | 25 ++++++++++++++++++++++++- main.go | 2 -- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/file/names.go b/file/names.go index dd24617..7c714a8 100644 --- a/file/names.go +++ b/file/names.go @@ -2,6 +2,9 @@ package file import ( "errors" + "os" + "os/user" + "path" "path/filepath" "strings" ) @@ -34,7 +37,7 @@ func PubFile(comment string) (string, bool) { if strings.HasPrefix(comment, verifyWith) { file := comment[len(verifyWith):] if strings.HasSuffix(file, ".pub") { - return file, true + return FindFile(file), true } } return "", false @@ -43,3 +46,23 @@ func PubFile(comment string) (string, bool) { func VerifyWith(encFile string) string { return verifyWith + PubName(encFile) } + +var safePath = []string{ + "/etc/signify", + "~/.signify", + ".", +} + +func FindFile(fname string) string { + usr, _ := user.Current() + for _, v := range safePath { + p := path.Join(v, fname) + if p[0] == '~' { + p = path.Join(usr.HomeDir, p[1:]) + } + if _, err := os.Stat(p); err == nil { + return p + } + } + return fname +} diff --git a/main.go b/main.go index d74c055..48942bd 100644 --- a/main.go +++ b/main.go @@ -8,8 +8,6 @@ import ( var ErrEZ = errors.New("can't combine -e and -z options") -const safePath = "/etc/signify" - func usage() { fmt.Print("Usage:") fmt.Println("\tsignify -C [-q] -p pubkey -x sigfile [file ...]") -- cgit v1.2.3