Merge pull request #110 from gliderlabs/belak/raw-cmd

Add Session.RawCommand()
This commit is contained in:
Kaleb Elwert 2019-06-19 20:22:20 -07:00 committed by GitHub
commit 5b6cc7030f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -44,6 +44,9 @@ type Session interface {
// which considers quoting not just whitespace. // which considers quoting not just whitespace.
Command() []string Command() []string
// RawCommand returns the exact command that was provided by the user.
RawCommand() string
// PublicKey returns the PublicKey used to authenticate. If a public key was not // PublicKey returns the PublicKey used to authenticate. If a public key was not
// used it will return nil. // used it will return nil.
PublicKey() PublicKey PublicKey() PublicKey
@ -106,7 +109,7 @@ type session struct {
env []string env []string
ptyCb PtyCallback ptyCb PtyCallback
sessReqCb SessionRequestCallback sessReqCb SessionRequestCallback
cmd []string rawCmd string
ctx Context ctx Context
sigCh chan<- Signal sigCh chan<- Signal
sigBuf []Signal sigBuf []Signal
@ -179,8 +182,13 @@ func (sess *session) Environ() []string {
return append([]string(nil), sess.env...) return append([]string(nil), sess.env...)
} }
func (sess *session) RawCommand() string {
return sess.rawCmd
}
func (sess *session) Command() []string { func (sess *session) Command() []string {
return append([]string(nil), sess.cmd...) cmd, _ := shlex.Split(sess.rawCmd, true)
return append([]string(nil), cmd...)
} }
func (sess *session) Pty() (Pty, <-chan Window, bool) { func (sess *session) Pty() (Pty, <-chan Window, bool) {
@ -214,12 +222,12 @@ func (sess *session) handleRequests(reqs <-chan *gossh.Request) {
var payload = struct{ Value string }{} var payload = struct{ Value string }{}
gossh.Unmarshal(req.Payload, &payload) gossh.Unmarshal(req.Payload, &payload)
sess.cmd, _ = shlex.Split(payload.Value, true) sess.rawCmd = payload.Value
// If there's a session policy callback, we need to confirm before // If there's a session policy callback, we need to confirm before
// accepting the session. // accepting the session.
if sess.sessReqCb != nil && !sess.sessReqCb(sess, req.Type) { if sess.sessReqCb != nil && !sess.sessReqCb(sess, req.Type) {
sess.cmd = nil sess.rawCmd = ""
req.Reply(false, nil) req.Reply(false, nil)
continue continue
} }