From ee2ce3f5c5d7749d1ae05e158fad00501365f806 Mon Sep 17 00:00:00 2001 From: MickaelK Date: Sun, 14 Apr 2024 22:12:45 +1000 Subject: [PATCH] chore (plg_backend_nfs): splitdown plugin onto smaller chunks --- server/plugin/plg_backend_nfs/auth_helper.go | 78 ++++++++++++++++++++ server/plugin/plg_backend_nfs/auth_unix.go | 34 +++++++++ server/plugin/plg_backend_nfs/index.go | 73 ++---------------- 3 files changed, 117 insertions(+), 68 deletions(-) create mode 100644 server/plugin/plg_backend_nfs/auth_helper.go create mode 100644 server/plugin/plg_backend_nfs/auth_unix.go diff --git a/server/plugin/plg_backend_nfs/auth_helper.go b/server/plugin/plg_backend_nfs/auth_helper.go new file mode 100644 index 00000000..7edb36af --- /dev/null +++ b/server/plugin/plg_backend_nfs/auth_helper.go @@ -0,0 +1,78 @@ +package plg_backend_nfs + +import ( + "bufio" + "os" + "strconv" + "strings" + + . "github.com/mickael-kerjean/filestash/server/common" +) + +var cacheForEtc AppCache + +const ( + DEFAULT_UID = 1000 + DEFAULT_GID = 1000 +) + +func init() { + cacheForEtc = NewAppCache(120, 60) +} + +func getUid(hint string) uint32 { + if hint == "" { + return DEFAULT_UID + } else if _uid, err := strconv.Atoi(hint); err == nil { + return uint32(_uid) + } else if uid, _, err := extractFromEtcPasswd(hint); err == nil { + return uid + } + return DEFAULT_UID +} + +func getGid(hint string) uint32 { + if hint == "" { + return DEFAULT_UID + } else if _gid, err := strconv.Atoi(hint); err == nil { + return uint32(_gid) + } else if _, gid, err := extractFromEtcPasswd(hint); err == nil { + return gid + } + return DEFAULT_GID +} + +func extractFromEtcPasswd(username string) (uint32, uint32, error) { + if v := cacheForEtc.Get(map[string]string{"username": username}); v != nil { + inCache := v.([]int) + return uint32(inCache[0]), uint32(inCache[1]), nil + } + f, err := os.OpenFile("/etc/passwd", os.O_RDONLY, os.ModePerm) + if err != nil { + return DEFAULT_UID, DEFAULT_GID, err + } + defer f.Close() + lines := bufio.NewReader(f) + for { + line, _, err := lines.ReadLine() + if err != nil { + break + } + s := strings.Split(string(line), ":") + if len(s) != 7 { + continue + } else if username == s[0] { + u, err := strconv.Atoi(s[2]) + if err != nil { + continue + } + g, err := strconv.Atoi(s[3]) + if err != nil { + continue + } + cacheForEtc.Set(map[string]string{"username": username}, []int{u, g}) + return uint32(u), uint32(g), nil + } + } + return DEFAULT_UID, DEFAULT_GID, ErrNotFound +} diff --git a/server/plugin/plg_backend_nfs/auth_unix.go b/server/plugin/plg_backend_nfs/auth_unix.go new file mode 100644 index 00000000..91e2cba9 --- /dev/null +++ b/server/plugin/plg_backend_nfs/auth_unix.go @@ -0,0 +1,34 @@ +package plg_backend_nfs + +import ( + "bytes" + "math/rand" + "time" + + "github.com/vmware/go-nfs-client/nfs/rpc" + "github.com/vmware/go-nfs-client/nfs/xdr" +) + +type AuthUnix struct { + Stamp uint32 + Machinename string + Uid uint32 + Gid uint32 + GidLen uint32 + Gids []uint32 +} + +func NewAuth(machineName string, uid, gid uint32) rpc.Auth { + w := new(bytes.Buffer) + xdr.Write(w, AuthUnix{ + Stamp: rand.New(rand.NewSource(time.Now().UnixNano())).Uint32(), + Machinename: machineName, + Uid: uid, + Gid: gid, + GidLen: 1, + }) + return rpc.Auth{ + 1, + w.Bytes(), + } +} diff --git a/server/plugin/plg_backend_nfs/index.go b/server/plugin/plg_backend_nfs/index.go index 53eb55ba..e26b8c6f 100644 --- a/server/plugin/plg_backend_nfs/index.go +++ b/server/plugin/plg_backend_nfs/index.go @@ -1,12 +1,10 @@ package plg_backend_nfs import ( - "bufio" "context" "io" "os" "path/filepath" - "strconv" "strings" . "github.com/mickael-kerjean/filestash/server/common" @@ -17,11 +15,6 @@ import ( "github.com/vmware/go-nfs-client/nfs/xdr" ) -const ( - DEFAULT_UID = 1000 - DEFAULT_GID = 1000 -) - type NfsShare struct { mount *nfs.Mount v *nfs.Target @@ -34,7 +27,6 @@ type NfsShare struct { func init() { Backend.Register("nfs", NfsShare{}) util.DefaultLogger.SetDebug(false) - cacheForEtc = NewAppCache(120, 60) } func (this NfsShare) Init(params map[string]string, app *App) (IBackend, error) { @@ -48,12 +40,15 @@ func (this NfsShare) Init(params map[string]string, app *App) (IBackend, error) uid := getUid(params["uid"]) gid := getGid(params["gid"]) - auth := rpc.NewAuthUnix(params["machine_name"], uid, gid).Auth() mount, err := nfs.DialMount(params["hostname"]) if err != nil { return nil, err } - v, err := mount.Mount(params["target"], auth) + auth := NewAuth(params["machine_name"], uid, gid) + v, err := mount.Mount( + params["target"], + auth, + ) if err != nil { return nil, err } @@ -271,61 +266,3 @@ func (this NfsShare) Close() { func (this NfsShare) nfsPath(path string) string { return strings.TrimSuffix(path, "/") } - -func getUid(hint string) uint32 { - if hint == "" { - return DEFAULT_UID - } else if _uid, err := strconv.Atoi(hint); err == nil { - return uint32(_uid) - } else if uid, _, err := extractFromEtcPasswd(hint); err == nil { - return uid - } - return DEFAULT_UID -} -func getGid(hint string) uint32 { - if hint == "" { - return DEFAULT_UID - } else if _gid, err := strconv.Atoi(hint); err == nil { - return uint32(_gid) - } else if _, gid, err := extractFromEtcPasswd(hint); err == nil { - return gid - } - return DEFAULT_GID -} - -var cacheForEtc AppCache - -func extractFromEtcPasswd(username string) (uint32, uint32, error) { - if v := cacheForEtc.Get(map[string]string{"username": username}); v != nil { - inCache := v.([]int) - return uint32(inCache[0]), uint32(inCache[1]), nil - } - f, err := os.OpenFile("/etc/passwd", os.O_RDONLY, os.ModePerm) - if err != nil { - return DEFAULT_UID, DEFAULT_GID, err - } - defer f.Close() - lines := bufio.NewReader(f) - for { - line, _, err := lines.ReadLine() - if err != nil { - break - } - s := strings.Split(string(line), ":") - if len(s) != 7 { - continue - } else if username == s[0] { - u, err := strconv.Atoi(s[2]) - if err != nil { - continue - } - g, err := strconv.Atoi(s[3]) - if err != nil { - continue - } - cacheForEtc.Set(map[string]string{"username": username}, []int{u, g}) - return uint32(u), uint32(g), nil - } - } - return DEFAULT_UID, DEFAULT_GID, ErrNotFound -}