-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathpeercred_freebsd.go
55 lines (48 loc) · 1.14 KB
/
peercred_freebsd.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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", cerr)
}
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
}