From ffe191394a3a6735c9a93d8c065d49ffa6e7fb7b Mon Sep 17 00:00:00 2001 From: Randall Winkhart Date: Sun, 6 Apr 2025 22:44:56 -0400 Subject: [PATCH] Extract handle from pipe connection --- go.mod | 5 ++++- go.sum | 2 ++ peercred_windows.go | 10 ++++++---- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/go.mod b/go.mod index 0cde453..a0f32cb 100644 --- a/go.mod +++ b/go.mod @@ -2,6 +2,9 @@ module github.com/rwinkhart/peercred-mini go 1.24.2 -require golang.org/x/sys v0.32.0 +require ( + github.com/Microsoft/go-winio v0.6.2 + golang.org/x/sys v0.32.0 +) replace golang.org/x/sys => github.com/rwinkhart/sys-freebsd-13-xucred v0.0.0-20250405010723-99a5f0732c0e diff --git a/go.sum b/go.sum index 51e1927..dc324b3 100644 --- a/go.sum +++ b/go.sum @@ -1,2 +1,4 @@ +github.com/Microsoft/go-winio v0.6.2 h1:F2VQgta7ecxGYO8k3ZZz3RS8fVIXVxONVUPlNERoyfY= +github.com/Microsoft/go-winio v0.6.2/go.mod h1:yd8OoFMLzJbo9gZq8j5qaps8bJ9aShtEA8Ipt1oGCvU= github.com/rwinkhart/sys-freebsd-13-xucred v0.0.0-20250405010723-99a5f0732c0e h1:YRpZcbGU/LVLwK87IURH4Sfye6Fv0ri1IRZ27loWpHs= github.com/rwinkhart/sys-freebsd-13-xucred v0.0.0-20250405010723-99a5f0732c0e/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k= diff --git a/peercred_windows.go b/peercred_windows.go index fa19ab3..efbbaca 100644 --- a/peercred_windows.go +++ b/peercred_windows.go @@ -6,20 +6,22 @@ package peercred import ( "fmt" + "net" + "github.com/Microsoft/go-winio" "golang.org/x/sys/windows" ) // Get returns the peer credentials for c. -func Get(h windows.Handle) *Creds { - creds, _ := get(&h) +func Get(c net.Conn) *Creds { + creds, _ := get(winio.UtilGetPipeHandle(c)) return creds } -func get(h *windows.Handle) (*Creds, error) { +func get(h windows.Handle) (*Creds, error) { // get PID var clientPID uint32 - windows.GetNamedPipeClientProcessId(*h, &clientPID) + windows.GetNamedPipeClientProcessId(h, &clientPID) // get SID (UID) var access uint32 = windows.PROCESS_QUERY_INFORMATION