3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-11-25 13:29:27 +01:00

Output unix socket credentials where applicable

thanks @ajaspers!
This commit is contained in:
Shivaram Lingamneni 2020-09-24 10:59:30 -04:00
parent e990bc9baa
commit f12384c5a6

View File

@ -9,6 +9,7 @@ import (
"net" "net"
"regexp" "regexp"
"strings" "strings"
"syscall"
) )
var ( var (
@ -195,10 +196,24 @@ func HandleXForwardedFor(remoteAddr string, xForwardedFor string, whitelist []ne
return return
} }
func DescribeConn(conn net.Conn) string { // Output a description of a connection that can identify it to other systems
// XXX for unix domain sockets, this is not informative enough for an operator // administration tools.
// to determine who holds the other side of the connection. there seems to be func DescribeConn(c net.Conn) (description string) {
// no way to get either the correct file descriptor of the connection, or the description = "<error>"
// udiag_ino from `man 7 sock_diag`. maybe there's something else we can do? switch conn := c.(type) {
return fmt.Sprintf("%s <-> %s", conn.LocalAddr().String(), conn.RemoteAddr().String()) case *net.UnixConn:
f, err := conn.File()
if err != nil {
return
}
defer f.Close()
ucred, err := syscall.GetsockoptUcred(int(f.Fd()), syscall.SOL_SOCKET, syscall.SO_PEERCRED)
if err != nil {
return
}
return fmt.Sprintf("%s <-> %s [pid=%d, uid=%d]", conn.LocalAddr().String(), conn.RemoteAddr().String(), ucred.Pid, ucred.Uid)
default:
// *net.TCPConn or *tls.Conn
return fmt.Sprintf("%s <-> %s", conn.LocalAddr().String(), conn.RemoteAddr().String())
}
} }