2023-07-17 03:23:26 +02:00
|
|
|
package sockets
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"os/user"
|
|
|
|
|
|
|
|
gops "github.com/mitchellh/go-ps"
|
|
|
|
"inet.af/peercred"
|
|
|
|
)
|
|
|
|
|
|
|
|
type CallingContext struct {
|
|
|
|
UserName string
|
|
|
|
ProcessName string
|
|
|
|
ParentProcessName string
|
|
|
|
GrandParentProcessName string
|
2023-09-12 18:56:35 +02:00
|
|
|
ProcessPid int
|
|
|
|
ParentProcessPid int
|
|
|
|
GrandParentProcessPid int
|
2023-07-17 03:23:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func GetCallingContext(connection net.Conn) CallingContext {
|
|
|
|
creds, err := peercred.Get(connection)
|
2023-12-23 13:24:46 +01:00
|
|
|
errorContext := CallingContext{
|
2023-12-23 13:44:16 +01:00
|
|
|
UserName: "unknown",
|
|
|
|
ProcessName: "unknown",
|
|
|
|
ParentProcessName: "unknown",
|
|
|
|
GrandParentProcessName: "unknown",
|
|
|
|
ProcessPid: 0,
|
|
|
|
ParentProcessPid: 0,
|
|
|
|
GrandParentProcessPid: 0,
|
2023-12-23 13:24:46 +01:00
|
|
|
}
|
2023-07-17 03:23:26 +02:00
|
|
|
if err != nil {
|
2023-12-23 13:24:46 +01:00
|
|
|
return errorContext
|
2023-07-17 03:23:26 +02:00
|
|
|
}
|
|
|
|
pid, _ := creds.PID()
|
|
|
|
process, err := gops.FindProcess(pid)
|
2023-12-23 13:24:46 +01:00
|
|
|
if err != nil {
|
|
|
|
return errorContext
|
|
|
|
}
|
|
|
|
if process == nil {
|
|
|
|
return errorContext
|
|
|
|
}
|
2023-09-19 22:27:03 +02:00
|
|
|
|
|
|
|
// git is epheremal and spawns ssh-keygen and ssh so we need to anchor to git
|
|
|
|
if process.Executable() == "ssh-keygen" || process.Executable() == "ssh" {
|
|
|
|
p, e := gops.FindProcess(process.PPid())
|
|
|
|
if p.Executable() == "git" && e == nil {
|
|
|
|
process, err = p, e
|
|
|
|
pid = process.Pid()
|
|
|
|
}
|
2023-09-19 22:14:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
uid, _ := creds.UserID()
|
2023-07-17 03:23:26 +02:00
|
|
|
ppid := process.PPid()
|
|
|
|
if err != nil {
|
2023-12-23 13:24:46 +01:00
|
|
|
return errorContext
|
2023-07-17 03:23:26 +02:00
|
|
|
}
|
2023-09-12 02:54:46 +02:00
|
|
|
|
2023-07-17 03:23:26 +02:00
|
|
|
parentProcess, err := gops.FindProcess(ppid)
|
|
|
|
if err != nil {
|
2023-12-23 13:24:46 +01:00
|
|
|
return errorContext
|
2023-07-17 03:23:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
parentParentProcess, err := gops.FindProcess(parentProcess.PPid())
|
|
|
|
if err != nil {
|
2023-12-23 13:24:46 +01:00
|
|
|
return errorContext
|
2023-07-17 03:23:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
username, err := user.LookupId(uid)
|
|
|
|
if err != nil {
|
2023-12-23 13:24:46 +01:00
|
|
|
return errorContext
|
2023-07-17 03:23:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return CallingContext{
|
|
|
|
UserName: username.Username,
|
|
|
|
ProcessName: process.Executable(),
|
|
|
|
ParentProcessName: parentProcess.Executable(),
|
|
|
|
GrandParentProcessName: parentParentProcess.Executable(),
|
2023-09-12 18:56:35 +02:00
|
|
|
ProcessPid: pid,
|
|
|
|
ParentProcessPid: ppid,
|
|
|
|
GrandParentProcessPid: parentParentProcess.PPid(),
|
2023-07-17 03:23:26 +02:00
|
|
|
}
|
|
|
|
}
|