Extract handle from pipe connection

This commit is contained in:
2025-04-06 22:44:56 -04:00
parent 081b05659a
commit ffe191394a
3 changed files with 12 additions and 5 deletions
+4 -1
View File
@@ -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
+2
View File
@@ -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=
+6 -4
View File
@@ -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