mirror of
https://github.com/rwinkhart/peercred-mini.git
synced 2026-08-28 04:46:43 -04:00
Strip unneeded functionality
This commit is contained in:
+9
-18
@@ -7,9 +7,7 @@
|
||||
package peercred
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net"
|
||||
"runtime"
|
||||
)
|
||||
|
||||
// Creds are the peer credentials.
|
||||
@@ -23,25 +21,18 @@ func (c *Creds) PID() (pid int, ok bool) {
|
||||
}
|
||||
|
||||
// UserID returns the userid (or Windows SID) that owns the other side
|
||||
// of the connection, if known. (ok is false if not known)
|
||||
// of the connection.
|
||||
// The returned string is suitable to passing to os/user.LookupId.
|
||||
func (c *Creds) UserID() (uid string, ok bool) {
|
||||
return c.uid, c.uid != ""
|
||||
func (c *Creds) UserID() (uid string) {
|
||||
return c.uid
|
||||
}
|
||||
|
||||
var osGet func(net.Conn) (*Creds, error)
|
||||
|
||||
var (
|
||||
ErrNotImplemented = errors.New("not implemented on " + runtime.GOOS)
|
||||
ErrUnsupportedConnType = errors.New("unsupported connection type")
|
||||
)
|
||||
func osGet(c net.Conn) (*Creds, error) {
|
||||
return getUnix(c.(*net.UnixConn))
|
||||
}
|
||||
|
||||
// Get returns the peer credentials for c.
|
||||
//
|
||||
// For unsupported system, the error is ErrNotImplemented.
|
||||
func Get(c net.Conn) (*Creds, error) {
|
||||
if osGet == nil {
|
||||
return nil, ErrNotImplemented
|
||||
}
|
||||
return osGet(c)
|
||||
func Get(c net.Conn) *Creds {
|
||||
creds, _ := osGet(c)
|
||||
return creds
|
||||
}
|
||||
|
||||
@@ -12,20 +12,6 @@ import (
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
func init() {
|
||||
osGet = getDarwin
|
||||
}
|
||||
|
||||
func getDarwin(c net.Conn) (*Creds, error) {
|
||||
switch c := c.(type) {
|
||||
case *net.UnixConn:
|
||||
return getUnix(c)
|
||||
case *net.TCPConn:
|
||||
// TODO: use /proc tcp info for localhost connections like Windows?
|
||||
}
|
||||
return nil, ErrUnsupportedConnType
|
||||
}
|
||||
|
||||
func getUnix(c *net.UnixConn) (*Creds, error) {
|
||||
raw, err := c.SyscallConn()
|
||||
if err != nil {
|
||||
|
||||
@@ -12,20 +12,6 @@ import (
|
||||
"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 {
|
||||
|
||||
@@ -12,20 +12,6 @@ import (
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
func init() {
|
||||
osGet = getLinux
|
||||
}
|
||||
|
||||
func getLinux(c net.Conn) (*Creds, error) {
|
||||
switch c := c.(type) {
|
||||
case *net.UnixConn:
|
||||
return getUnix(c)
|
||||
case *net.TCPConn:
|
||||
// TODO: use /proc tcp info for localhost connections like Windows?
|
||||
}
|
||||
return nil, ErrUnsupportedConnType
|
||||
}
|
||||
|
||||
func getUnix(c *net.UnixConn) (*Creds, error) {
|
||||
raw, err := c.SyscallConn()
|
||||
if err != nil {
|
||||
|
||||
@@ -12,20 +12,6 @@ import (
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
func init() {
|
||||
osGet = getSolaris
|
||||
}
|
||||
|
||||
func getSolaris(c net.Conn) (*Creds, error) {
|
||||
switch c := c.(type) {
|
||||
case *net.UnixConn:
|
||||
return getUnix(c)
|
||||
case *net.TCPConn:
|
||||
// TODO: Need ideas
|
||||
}
|
||||
return nil, ErrUnsupportedConnType
|
||||
}
|
||||
|
||||
func getUnix(c *net.UnixConn) (*Creds, error) {
|
||||
raw, err := c.SyscallConn()
|
||||
if err != nil {
|
||||
|
||||
@@ -46,12 +46,12 @@ func TestUnixSock(t *testing.T) {
|
||||
}
|
||||
defer c.Close()
|
||||
|
||||
creds, err := Get(c)
|
||||
creds := Get(c)
|
||||
if err != nil {
|
||||
t.Fatalf("Get: %v", err)
|
||||
}
|
||||
|
||||
uid, ok := creds.UserID()
|
||||
uid := creds.UserID()
|
||||
if !ok {
|
||||
t.Errorf("no UID")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user