Open the socket before opening the log

This commit is contained in:
Timmy Welch 2023-01-16 19:45:32 -08:00
parent d9b0b2e21a
commit 9573438f75
No known key found for this signature in database

View File

@ -28,7 +28,7 @@ import (
var ( var (
sigExit = []os.Signal{os.Kill, os.Interrupt} sigExit = []os.Signal{os.Kill, os.Interrupt}
sigIgnore []os.Signal sigIgnore []os.Signal
logger = logrus.New() logger = logrus.New()
log *logrus.Entry log *logrus.Entry
appname = "sshrimp" appname = "sshrimp"
@ -104,7 +104,6 @@ func setupLoging(config cfg) error {
log = logger.WithFields(logrus.Fields{ log = logger.WithFields(logrus.Fields{
"pid": os.Getpid(), "pid": os.Getpid(),
}) })
log.Logger.Info("testing")
sshrimpagent.Log = log sshrimpagent.Log = log
signer.Log = log signer.Log = log
@ -132,58 +131,73 @@ func main() {
flag.Parse() flag.Parse()
if err := setupLoging(cli); err != nil {
logger.Warnf("Error setting up logging: %v", err)
}
c := config.NewSSHrimpWithDefaults() c := config.NewSSHrimpWithDefaults()
err := c.Read(cli.Config) err := c.Read(cli.Config)
if err != nil { if err != nil {
panic(err) panic(err)
} }
err = launchAgent(c) listener := openSocket(c)
if listener == nil {
logger.Errorln("Failed to open socket")
return
}
if err := setupLoging(cli); err != nil {
logger.Warnf("Error setting up logging: %v", err)
}
err = launchAgent(c, listener)
if err != nil { if err != nil {
panic(err) panic(err)
} }
} }
func launchAgent(c *config.SSHrimp) error { func openSocket(c *config.SSHrimp) net.Listener {
var ( var (
err error
listener net.Listener listener net.Listener
privateKey crypto.Signer err error
sshSigner ssh.Signer
logMessage string logMessage string
socketPath = ExpandPath(c.Agent.Socket)
) )
log.Traceln("Creating socket") if _, err = os.Stat(socketPath); err == nil {
if _, err = os.Stat(ExpandPath(c.Agent.Socket)); err == nil { fmt.Println("Creating socket")
log.Tracef("File already exists at %s", c.Agent.Socket) fmt.Printf("File already exists at %s\n", c.Agent.Socket)
conn, sockErr := net.Dial("unix", ExpandPath(c.Agent.Socket)) conn, sockErr := net.Dial("unix", socketPath)
if conn == nil { if conn == nil {
logMessage = "conn is nil" logMessage = "conn is nil"
} }
if sockErr == nil { // socket is accepting connections if sockErr == nil { // socket is accepting connections
logMessage += "err reports successful connection" logMessage += "err reports successful connection"
conn.Close() conn.Close()
log.Errorf("Socket connected successfully %s", logMessage) fmt.Printf("socket %s already exists\n", c.Agent.Socket)
return fmt.Errorf("socket %s already exists", c.Agent.Socket) return nil
} }
log.Tracef("Socket is not connected %s", logMessage) fmt.Printf("Socket is not connected %s\n", logMessage)
if os.Remove(c.Agent.Socket) == nil { // socket is not accepting connections, assuming safe to remove err = os.Remove(socketPath)
log.Traceln("Deleting socket: success") if err == nil { // socket is not accepting connections, assuming safe to remove
fmt.Println("Deleting socket: success")
} else { } else {
log.Errorf("Deleting socket: fail") fmt.Println("Deleting socket: fail", err)
return nil
} }
} }
// This affects all files created for the process. Since this is a sensitive // This affects all files created for the process. Since this is a sensitive
// socket, only allow the current user to write to the socket. // socket, only allow the current user to write to the socket.
syscall.Umask(0077) syscall.Umask(0077)
listener, err = net.Listen("unix", ExpandPath(c.Agent.Socket)) listener, err = net.Listen("unix", socketPath)
if err != nil { if err != nil {
return err fmt.Println("Error opening socket:", err)
return nil
} }
return listener
}
func launchAgent(c *config.SSHrimp, listener net.Listener) error {
var (
err error
privateKey crypto.Signer
sshSigner ssh.Signer
)
defer listener.Close() defer listener.Close()
fmt.Printf("listening on %s\n", c.Agent.Socket) fmt.Printf("listening on %s\n", c.Agent.Socket)