mirror of
https://github.com/rwinkhart/peercred-mini.git
synced 2026-09-05 00:27:16 -04:00
Add FreeBSD support
For FreeBSD 12.2 and before I think we could use SCM_CREDS to get the pid of the peer. For FreeBSD 13.x and later, the Xucred is extended with a pid. For now we'll leave it unimplemented and expect to add FreeBSD 13+ support whern it is released. Signed-off-by: Denton Gentry <dgentry@tailscale.com>
This commit is contained in:
committed by
Denton Gentry
parent
88448a8ba5
commit
6ba7dd52e8
@@ -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 /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 {
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user