Cleaned up hkexshd.go to use new logger as well

Signed-off-by: Russ Magee <rmagee@gmail.com>
This commit is contained in:
Russ Magee 2018-10-26 18:20:32 -07:00
parent 2f83d488d6
commit 25d85f6759

View file

@ -18,7 +18,6 @@ import (
"io" "io"
"io/ioutil" "io/ioutil"
"log" "log"
"log/syslog"
"os" "os"
"os/exec" "os/exec"
"os/user" "os/user"
@ -27,13 +26,14 @@ import (
"syscall" "syscall"
"blitter.com/go/goutmp" "blitter.com/go/goutmp"
"blitter.com/go/hkexsh/logger"
hkexsh "blitter.com/go/hkexsh" hkexsh "blitter.com/go/hkexsh"
"blitter.com/go/hkexsh/hkexnet" "blitter.com/go/hkexsh/hkexnet"
"github.com/kr/pty" "github.com/kr/pty"
) )
var ( var (
Log *syslog.Writer // reg. syslog output (no -d) Log *logger.Writer // reg. syslog output (no -d)
) )
/* -------------------------------------------------------------- */ /* -------------------------------------------------------------- */
@ -387,8 +387,8 @@ func main() {
} }
} }
Log, _ = syslog.New(syslog.LOG_DAEMON|syslog.LOG_DEBUG, "hkexshd") Log, _ = logger.New(logger.LOG_DAEMON|logger.LOG_DEBUG, "hkexshd")
hkexnet.Init(dbg, "hkexshd", syslog.LOG_DAEMON|syslog.LOG_DEBUG) hkexnet.Init(dbg, "hkexshd", logger.LOG_DAEMON|logger.LOG_DEBUG)
if dbg { if dbg {
log.SetOutput(Log) log.SetOutput(Log)
} else { } else {
@ -506,7 +506,7 @@ func main() {
if valid { if valid {
hc.Write([]byte{1}) hc.Write([]byte{1})
} else { } else {
Log.Notice(fmt.Sprintln("Invalid user", string(rec.Who()))) logger.LogNotice(fmt.Sprintln("Invalid user", string(rec.Who())))
hc.Write([]byte{0}) // ? required? hc.Write([]byte{0}) // ? required?
return return
} }
@ -517,7 +517,7 @@ func main() {
// Generate automated login token // Generate automated login token
addr := hc.RemoteAddr() addr := hc.RemoteAddr()
hname := goutmp.GetHost(addr.String()) hname := goutmp.GetHost(addr.String())
Log.Notice(fmt.Sprintf("[Generating autologin token for [%s@%s]]\n", rec.Who(), hname)) logger.LogNotice(fmt.Sprintf("[Generating autologin token for [%s@%s]]\n", rec.Who(), hname))
token := GenAuthToken(string(rec.Who()), string(rec.ConnHost())) token := GenAuthToken(string(rec.Who()), string(rec.ConnHost()))
tokenCmd := fmt.Sprintf("echo \"%s\" | tee -a ~/.hkexsh_id", token) tokenCmd := fmt.Sprintf("echo \"%s\" | tee -a ~/.hkexsh_id", token)
runErr, cmdStatus := runShellAs(string(rec.Who()), string(rec.TermType()), tokenCmd, false, hc, chaffEnabled) runErr, cmdStatus := runShellAs(string(rec.Who()), string(rec.TermType()), tokenCmd, false, hc, chaffEnabled)
@ -525,7 +525,7 @@ func main() {
// Clear current op so user can enter next, or EOF // Clear current op so user can enter next, or EOF
rec.SetOp([]byte{0}) rec.SetOp([]byte{0})
if runErr != nil { if runErr != nil {
Log.Err(fmt.Sprintf("[Error generating autologin token for %s@%s]\n", rec.Who(), hname)) logger.LogErr(fmt.Sprintf("[Error generating autologin token for %s@%s]\n", rec.Who(), hname))
} else { } else {
log.Printf("[Autologin token generation completed for %s@%s, status %d]\n", rec.Who(), hname, cmdStatus) log.Printf("[Autologin token generation completed for %s@%s, status %d]\n", rec.Who(), hname, cmdStatus)
hc.SetStatus(hkexnet.CSOType(cmdStatus)) hc.SetStatus(hkexnet.CSOType(cmdStatus))
@ -534,22 +534,22 @@ func main() {
// Non-interactive command // Non-interactive command
addr := hc.RemoteAddr() addr := hc.RemoteAddr()
hname := goutmp.GetHost(addr.String()) hname := goutmp.GetHost(addr.String())
Log.Notice(fmt.Sprintf("[Running command for [%s@%s]]\n", rec.Who(), hname)) logger.LogNotice(fmt.Sprintf("[Running command for [%s@%s]]\n", rec.Who(), hname))
runErr, cmdStatus := runShellAs(string(rec.Who()), string(rec.TermType()), string(rec.Cmd()), false, hc, chaffEnabled) runErr, cmdStatus := runShellAs(string(rec.Who()), string(rec.TermType()), string(rec.Cmd()), false, hc, chaffEnabled)
// Returned hopefully via an EOF or exit/logout; // Returned hopefully via an EOF or exit/logout;
// Clear current op so user can enter next, or EOF // Clear current op so user can enter next, or EOF
rec.SetOp([]byte{0}) rec.SetOp([]byte{0})
if runErr != nil { if runErr != nil {
Log.Err(fmt.Sprintf("[Error spawning cmd for %s@%s]\n", rec.Who(), hname)) logger.LogErr(fmt.Sprintf("[Error spawning cmd for %s@%s]\n", rec.Who(), hname))
} else { } else {
Log.Notice(fmt.Sprintf("[Command completed for %s@%s, status %d]\n", rec.Who(), hname, cmdStatus)) logger.LogNotice(fmt.Sprintf("[Command completed for %s@%s, status %d]\n", rec.Who(), hname, cmdStatus))
hc.SetStatus(hkexnet.CSOType(cmdStatus)) hc.SetStatus(hkexnet.CSOType(cmdStatus))
} }
} else if rec.Op()[0] == 's' { } else if rec.Op()[0] == 's' {
// Interactive session // Interactive session
addr := hc.RemoteAddr() addr := hc.RemoteAddr()
hname := goutmp.GetHost(addr.String()) hname := goutmp.GetHost(addr.String())
Log.Notice(fmt.Sprintf("[Running shell for [%s@%s]]\n", rec.Who(), hname)) logger.LogNotice(fmt.Sprintf("[Running shell for [%s@%s]]\n", rec.Who(), hname))
utmpx := goutmp.Put_utmp(string(rec.Who()), hname) utmpx := goutmp.Put_utmp(string(rec.Who()), hname)
defer func() { goutmp.Unput_utmp(utmpx) }() defer func() { goutmp.Unput_utmp(utmpx) }()
@ -561,7 +561,7 @@ func main() {
if runErr != nil { if runErr != nil {
Log.Err(fmt.Sprintf("[Error spawning shell for %s@%s]\n", rec.Who(), hname)) Log.Err(fmt.Sprintf("[Error spawning shell for %s@%s]\n", rec.Who(), hname))
} else { } else {
Log.Notice(fmt.Sprintf("[Shell completed for %s@%s, status %d]\n", rec.Who(), hname, cmdStatus)) logger.LogNotice(fmt.Sprintf("[Shell completed for %s@%s, status %d]\n", rec.Who(), hname, cmdStatus))
hc.SetStatus(hkexnet.CSOType(cmdStatus)) hc.SetStatus(hkexnet.CSOType(cmdStatus))
} }
} else if rec.Op()[0] == 'D' { } else if rec.Op()[0] == 'D' {
@ -569,15 +569,15 @@ func main() {
log.Printf("[Client->Server copy]\n") log.Printf("[Client->Server copy]\n")
addr := hc.RemoteAddr() addr := hc.RemoteAddr()
hname := goutmp.GetHost(addr.String()) hname := goutmp.GetHost(addr.String())
Log.Notice(fmt.Sprintf("[Running copy for [%s@%s]]\n", rec.Who(), hname)) logger.LogNotice(fmt.Sprintf("[Running copy for [%s@%s]]\n", rec.Who(), hname))
runErr, cmdStatus := runClientToServerCopyAs(string(rec.Who()), string(rec.TermType()), hc, string(rec.Cmd()), chaffEnabled) runErr, cmdStatus := runClientToServerCopyAs(string(rec.Who()), string(rec.TermType()), hc, string(rec.Cmd()), chaffEnabled)
// Returned hopefully via an EOF or exit/logout; // Returned hopefully via an EOF or exit/logout;
// Clear current op so user can enter next, or EOF // Clear current op so user can enter next, or EOF
rec.SetOp([]byte{0}) rec.SetOp([]byte{0})
if runErr != nil { if runErr != nil {
Log.Err(fmt.Sprintf("[Error running cp for %s@%s]\n", rec.Who(), hname)) logger.LogErr(fmt.Sprintf("[Error running cp for %s@%s]\n", rec.Who(), hname))
} else { } else {
Log.Notice(fmt.Sprintf("[Command completed for %s@%s, status %d]\n", rec.Who(), hname, cmdStatus)) logger.LogNotice(fmt.Sprintf("[Command completed for %s@%s, status %d]\n", rec.Who(), hname, cmdStatus))
} }
hc.SetStatus(hkexnet.CSOType(cmdStatus)) hc.SetStatus(hkexnet.CSOType(cmdStatus))
@ -591,26 +591,26 @@ func main() {
log.Printf("[Server->Client copy]\n") log.Printf("[Server->Client copy]\n")
addr := hc.RemoteAddr() addr := hc.RemoteAddr()
hname := goutmp.GetHost(addr.String()) hname := goutmp.GetHost(addr.String())
Log.Notice(fmt.Sprintf("[Running copy for [%s@%s]]\n", rec.Who(), hname)) logger.LogNotice(fmt.Sprintf("[Running copy for [%s@%s]]\n", rec.Who(), hname))
runErr, cmdStatus := runServerToClientCopyAs(string(rec.Who()), string(rec.TermType()), hc, string(rec.Cmd()), chaffEnabled) runErr, cmdStatus := runServerToClientCopyAs(string(rec.Who()), string(rec.TermType()), hc, string(rec.Cmd()), chaffEnabled)
// Returned hopefully via an EOF or exit/logout; // Returned hopefully via an EOF or exit/logout;
// Clear current op so user can enter next, or EOF // Clear current op so user can enter next, or EOF
rec.SetOp([]byte{0}) rec.SetOp([]byte{0})
if runErr != nil { if runErr != nil {
Log.Err(fmt.Sprintf("[Error spawning cp for %s@%s]\n", rec.Who(), hname)) logger.LogErr(fmt.Sprintf("[Error spawning cp for %s@%s]\n", rec.Who(), hname))
} else { } else {
Log.Notice(fmt.Sprintf("[Command completed for %s@%s, status %d]\n", rec.Who(), hname, cmdStatus)) logger.LogNotice(fmt.Sprintf("[Command completed for %s@%s, status %d]\n", rec.Who(), hname, cmdStatus))
} }
hc.SetStatus(hkexnet.CSOType(cmdStatus)) hc.SetStatus(hkexnet.CSOType(cmdStatus))
//fmt.Println("Waiting for EOF from other end.") //fmt.Println("Waiting for EOF from other end.")
//_, _ = hc.Read(nil /*ackByte*/) //_, _ = hc.Read(nil /*ackByte*/)
//fmt.Println("Got remote end ack.") //fmt.Println("Got remote end ack.")
} else { } else {
Log.Err(fmt.Sprintln("[Bad hkexsh.Session]")) logger.LogErr(fmt.Sprintln("[Bad hkexsh.Session]"))
} }
return return
}(&conn) }(&conn)
} // Accept() success } // Accept() success
} //endfor } //endfor
Log.Notice(fmt.Sprintln("[Exiting]")) logger.LogNotice(fmt.Sprintln("[Exiting]"))
} }