Synchronize toolsrv authentication state

This commit is contained in:
Ollie Agent 2026-08-18 07:49:59 +02:00
parent a8a6bb5204
commit 45ad8c624e
1 changed files with 20 additions and 7 deletions

View File

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