3
0
mirror of https://github.com/ergochat/ergo.git synced 2025-01-22 10:14:07 +01:00

Merge pull request #1289 from slingamn/ucred

output unix socket credentials where applicable
This commit is contained in:
Shivaram Lingamneni 2020-09-24 10:09:48 -07:00 committed by GitHub
commit b876c296eb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

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) {
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()) return fmt.Sprintf("%s <-> %s", conn.LocalAddr().String(), conn.RemoteAddr().String())
}
} }