mirror of
https://github.com/rwinkhart/peercred-mini.git
synced 2026-08-27 20:36:30 -04:00
@@ -2,4 +2,4 @@ module inet.af/peercred
|
||||
|
||||
go 1.14
|
||||
|
||||
require golang.org/x/sys v0.0.0-20210216224549-f992740a1bac
|
||||
require golang.org/x/sys v0.0.0-20210301091718-77cc2087c03b
|
||||
|
||||
@@ -2,3 +2,5 @@ golang.org/x/sys v0.0.0-20210216163648-f7da38b97c65 h1:pTMjDVnP5eVRRlWO76rEWJ8Jo
|
||||
golang.org/x/sys v0.0.0-20210216163648-f7da38b97c65/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210216224549-f992740a1bac h1:9glrpwtNjBYgRpb67AZJKHfzj1stG/8BL5H7In2oTC4=
|
||||
golang.org/x/sys v0.0.0-20210216224549-f992740a1bac/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210301091718-77cc2087c03b h1:kHlr0tATeLRMEiZJu5CknOw/E8V6h69sXXQFGoPtjcc=
|
||||
golang.org/x/sys v0.0.0-20210301091718-77cc2087c03b/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
|
||||
+2
-2
@@ -13,10 +13,10 @@ import (
|
||||
)
|
||||
|
||||
func init() {
|
||||
osGet = getLinux
|
||||
osGet = getDarwin
|
||||
}
|
||||
|
||||
func getLinux(c net.Conn) (*Creds, error) {
|
||||
func getDarwin(c net.Conn) (*Creds, error) {
|
||||
switch c := c.(type) {
|
||||
case *net.UnixConn:
|
||||
return getUnix(c)
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
// Copyright (c) 2021 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"
|
||||
"net"
|
||||
"strconv"
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
func init() {
|
||||
osGet = getFreeBSD
|
||||
}
|
||||
|
||||
func getFreeBSD(c net.Conn) (*Creds, error) {
|
||||
switch c := c.(type) {
|
||||
case *net.UnixConn:
|
||||
return getUnix(c)
|
||||
case *net.TCPConn:
|
||||
// TODO: use sysctl net.inet.tcp.pcblist for localhost connections like Windows?
|
||||
}
|
||||
return nil, ErrUnsupportedConnType
|
||||
}
|
||||
|
||||
func getUnix(c *net.UnixConn) (*Creds, error) {
|
||||
raw, err := c.SyscallConn()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("SyscallConn: %w", err)
|
||||
}
|
||||
|
||||
var cred *unix.Xucred
|
||||
cerr := raw.Control(func(fd uintptr) {
|
||||
cred, err = unix.GetsockoptXucred(int(fd),
|
||||
unix.SOL_LOCAL,
|
||||
unix.LOCAL_PEERCRED)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("unix.GetsockoptXucred: %w", err)
|
||||
return
|
||||
}
|
||||
})
|
||||
if cerr != nil {
|
||||
return nil, fmt.Errorf("raw.Control: %w", err)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &Creds{
|
||||
pid: 0, // FreeBSD 13 adds a cr_pid field, can be used here.
|
||||
uid: strconv.FormatUint(uint64(cred.Uid), 10),
|
||||
}, nil
|
||||
}
|
||||
+13
-7
@@ -3,7 +3,7 @@
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
// +build go1.15
|
||||
// +build linux darwin
|
||||
// +build linux darwin freebsd
|
||||
|
||||
package peercred // import "inet.af/peercred"
|
||||
|
||||
@@ -12,6 +12,7 @@ import (
|
||||
"net"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@@ -59,11 +60,16 @@ func TestUnixSock(t *testing.T) {
|
||||
t.Errorf("UID = %q; want %q", got, want)
|
||||
}
|
||||
pid, ok := creds.PID()
|
||||
if !ok {
|
||||
t.Errorf("no PID")
|
||||
if runtime.GOOS == "freebsd" {
|
||||
if ok {
|
||||
t.Error("PID ok; want !ok. Thank you for fixing FreeBSD, please update the test.")
|
||||
}
|
||||
} else {
|
||||
if !ok {
|
||||
t.Errorf("no PID")
|
||||
}
|
||||
if got, want := pid, os.Getpid(); got != want {
|
||||
t.Errorf("PID = %v; want %v", got, want)
|
||||
}
|
||||
}
|
||||
if got, want := pid, os.Getpid(); got != want {
|
||||
t.Errorf("PID = %v; want %v", got, want)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user