diff --git a/cmd/toolsrv/p9.go b/cmd/toolsrv/p9.go index cb099b7..811368e 100644 --- a/cmd/toolsrv/p9.go +++ b/cmd/toolsrv/p9.go @@ -31,6 +31,7 @@ type fid struct { // authFid represents an auth file handle for Tauth. type authFid struct { + mu sync.Mutex uname string aname string authenticated bool @@ -214,8 +215,10 @@ func handleAttach(cs *connState, fc *plan9.Fcall, tree *virtfs.Tree) *plan9.Fcal if !hasAuth { return errFcall(fc, "invalid afid") } - - if !af.authenticated { + af.mu.Lock() + authenticated := af.authenticated + af.mu.Unlock() + if !authenticated { return errFcall(fc, "not authenticated") } @@ -238,19 +241,25 @@ func handleAuthWrite(cs *connState, fc *plan9.Fcall, af *authFid) *plan9.Fcall { return errFcall(fc, err.Error()) } + af.mu.Lock() af.authenticated = true af.token = token + af.mu.Unlock() return &plan9.Fcall{Type: plan9.Rwrite, Tag: fc.Tag, Count: uint32(len(fc.Data))} } // handleAuthRead handles reading the token from an auth fid. func handleAuthRead(cs *connState, fc *plan9.Fcall, af *authFid) *plan9.Fcall { - if !af.authenticated { + af.mu.Lock() + authenticated := af.authenticated + token := af.token + af.mu.Unlock() + if !authenticated { return errFcall(fc, "write secret first") } - data := []byte(af.token + "\n") + data := []byte(token + "\n") offset := int(fc.Offset) if offset >= len(data) { @@ -573,14 +582,18 @@ func handleWrite(cs *connState, fc *plan9.Fcall, tree *virtfs.Tree) *plan9.Fcall if !isFid { return errFcall(fc, "unknown fid") } - if !f.opened { + f.mu.Lock() + opened := f.opened + entry := f.entry + f.mu.Unlock() + if !opened { return errFcall(fc, "not open") } - if f.entry == nil { + if entry == nil { return errFcall(fc, "no entry") } - err := f.entry.Write(fc.Data) + err := entry.Write(fc.Data) if err != nil { return errFcall(fc, err.Error()) }