Initial Windows support

This commit is contained in:
2025-04-05 14:43:55 -04:00
parent b563a7af34
commit dcf2844318
9 changed files with 72 additions and 21 deletions
+2 -1
View File
@@ -1 +1,2 @@
# peercred
# peercred-mini
A simple module for getting the PID and UID of processes accessing UNIX domain sockets and Windows named pipes.
+1 -11
View File
@@ -1,4 +1,4 @@
// Copyright (c) 2021-2024 AUTHORS All rights reserved.
// Copyright (c) 2021-2025 AUTHORS All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
@@ -6,10 +6,6 @@
// other side of the connection, using various OS-specific facilities.
package peercred
import (
"net"
)
// Creds are the peer credentials.
type Creds struct {
pid int
@@ -26,9 +22,3 @@ func (c *Creds) PID() (pid int) {
func (c *Creds) UID() (uid string) {
return c.uid
}
// Get returns the peer credentials for c.
func Get(c net.Conn) *Creds {
creds, _ := getUnix(c.(*net.UnixConn))
return creds
}
+2 -2
View File
@@ -1,4 +1,4 @@
// Copyright (c) 2021 AUTHORS All rights reserved.
// Copyright (c) 2021-2025 AUTHORS All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
@@ -12,7 +12,7 @@ import (
"github.com/rwinkhart/sys-freebsd-13-xucred/unix"
)
func getUnix(c *net.UnixConn) (*Creds, error) {
func get(c *net.UnixConn) (*Creds, error) {
raw, err := c.SyscallConn()
if err != nil {
return nil, fmt.Errorf("SyscallConn: %w", err)
+2 -2
View File
@@ -1,4 +1,4 @@
// Copyright (c) 2021 AUTHORS All rights reserved.
// Copyright (c) 2021-2025 AUTHORS All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
@@ -12,7 +12,7 @@ import (
"github.com/rwinkhart/sys-freebsd-13-xucred/unix"
)
func getUnix(c *net.UnixConn) (*Creds, error) {
func get(c *net.UnixConn) (*Creds, error) {
raw, err := c.SyscallConn()
if err != nil {
return nil, fmt.Errorf("SyscallConn: %w", err)
+2 -2
View File
@@ -1,4 +1,4 @@
// Copyright (c) 2021 AUTHORS All rights reserved.
// Copyright (c) 2021-2025 AUTHORS All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
@@ -12,7 +12,7 @@ import (
"github.com/rwinkhart/sys-freebsd-13-xucred/unix"
)
func getUnix(c *net.UnixConn) (*Creds, error) {
func get(c *net.UnixConn) (*Creds, error) {
raw, err := c.SyscallConn()
if err != nil {
return nil, fmt.Errorf("SyscallConn: %w", err)
+2 -2
View File
@@ -1,4 +1,4 @@
// Copyright (c) 2021 AUTHORS All rights reserved.
// Copyright (c) 2021-2025 AUTHORS All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
@@ -12,7 +12,7 @@ import (
"github.com/rwinkhart/sys-freebsd-13-xucred/unix"
)
func getUnix(c *net.UnixConn) (*Creds, error) {
func get(c *net.UnixConn) (*Creds, error) {
raw, err := c.SyscallConn()
if err != nil {
return nil, fmt.Errorf("SyscallConn: %w", err)
+17
View File
@@ -0,0 +1,17 @@
//go:build !windows
// Copyright (c) 2021-2025 AUTHORS All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package peercred maps from a net.Conn to information about the
// other side of the connection, using various OS-specific facilities.
package peercred
import "net"
// Get returns the peer credentials for c.
func Get(c net.Conn) *Creds {
creds, _ := get(c.(*net.UnixConn))
return creds
}
+1 -1
View File
@@ -1,4 +1,4 @@
// Copyright (c) 2021 AUTHORS All rights reserved.
// Copyright (c) 2021-2025 AUTHORS All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
+43
View File
@@ -0,0 +1,43 @@
// Copyright (c) 2021-2025 AUTHORS All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package peercred
import (
"fmt"
"github.com/rwinkhart/sys-freebsd-13-xucred/windows"
)
// Get returns the peer credentials for c.
func Get(h *windows.Handle) *Creds {
creds, _ := get(h)
return creds
}
func get(h *windows.Handle) (*Creds, error) {
// get PID
var clientPID uint32
windows.GetNamedPipeClientProcessId(*h, &clientPID)
// get SID (UID)
var access uint32 = windows.PROCESS_QUERY_INFORMATION
hProcess, herr := windows.OpenProcess(access, false, clientPID)
defer windows.CloseHandle(hProcess)
var hToken windows.Token
windows.OpenProcessToken(hProcess, windows.TOKEN_QUERY, &hToken)
defer hToken.Close()
tokenUser, terr := hToken.GetTokenUser()
if terr != nil {
return nil, fmt.Errorf("windows.OpenProcessToken: %w", terr)
}
if herr != nil {
return nil, fmt.Errorf("windows.OpenProcess: %w", herr)
}
return &Creds{
pid: int(clientPID),
uid: tokenUser.User.Sid.String(),
}, nil
}