forked from authentricity/authentricity
56 lines
893 B
Go
56 lines
893 B
Go
package peercred
|
|
|
|
import (
|
|
"fmt"
|
|
"syscall"
|
|
|
|
"github.com/varlink/go/varlink"
|
|
"golang.org/x/sys/unix"
|
|
)
|
|
|
|
type getSyscallConn interface {
|
|
SyscallConn() (syscall.RawConn, error)
|
|
}
|
|
|
|
func Get(conn varlink.ReadWriterContext) *unix.Ucred {
|
|
gnc, ok := conn.(varlink.GetNetConn)
|
|
if !ok {
|
|
fmt.Println("Not GetNetConn")
|
|
return nil
|
|
}
|
|
|
|
nc := gnc.NetConn()
|
|
if nc == nil {
|
|
fmt.Println("No NetConn")
|
|
return nil
|
|
}
|
|
|
|
gsc, ok := nc.(getSyscallConn)
|
|
if !ok {
|
|
fmt.Println("Not GetSyscallConn")
|
|
return nil
|
|
}
|
|
|
|
raw, err := gsc.SyscallConn()
|
|
if err != nil {
|
|
fmt.Println("Getting syscall conn", err)
|
|
return nil
|
|
}
|
|
|
|
var cred *unix.Ucred
|
|
err2 := raw.Control(func(fd uintptr) {
|
|
cred, err = unix.GetsockoptUcred(int(fd),
|
|
unix.SOL_SOCKET,
|
|
unix.SO_PEERCRED)
|
|
})
|
|
|
|
if err != nil || err2 != nil {
|
|
fmt.Println(err, err2)
|
|
return nil
|
|
}
|
|
|
|
fmt.Println("Peercred ", cred)
|
|
|
|
return cred
|
|
}
|