Skip to content

Support subsystem #103

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions _examples/ssh-sftp/sftp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package main

import (
"fmt"
"github.com/gliderlabs/ssh"
"github.com/pkg/sftp"
"io"
"io/ioutil"
"log"
)

func SftpHandler(sess ssh.Session) {
debugStream := ioutil.Discard
serverOptions := []sftp.ServerOption{
sftp.WithDebug(debugStream),
}
server, err := sftp.NewServer(
sess,
serverOptions...,
)
if err != nil {
log.Printf("sftp server init error: %s\n", err)
return
}
if err := server.Serve(); err == io.EOF {
server.Close()
fmt.Println("sftp client exited session.")
} else if err != nil {
fmt.Println("sftp server completed with error:", err)
}

}

func main() {
srv := ssh.Server{
Addr: ":2223",
SubsystemHandlers: map[string]ssh.SubsystemHandler{},
}
srv.SetSubsystemHandler("sftp", SftpHandler)
log.Fatal(srv.ListenAndServe())
}
6 changes: 6 additions & 0 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ type Server struct {
channelHandlers map[string]channelHandler
requestHandlers map[string]RequestHandler

SubsystemHandlers map[string]SubsystemHandler

listenerWg sync.WaitGroup
mu sync.Mutex
listeners map[net.Listener]struct{}
Expand Down Expand Up @@ -381,3 +383,7 @@ func (srv *Server) trackConn(c *gossh.ServerConn, add bool) {
srv.connWg.Done()
}
}

func (srv *Server) SetSubsystemHandler(name string, handler SubsystemHandler) {
srv.SubsystemHandlers[name] = handler
}
45 changes: 32 additions & 13 deletions session.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package ssh

import (
"bytes"
"context"
"errors"
"fmt"
Expand Down Expand Up @@ -90,6 +89,8 @@ func sessionHandler(srv *Server, conn *gossh.ServerConn, newChan gossh.NewChanne
ptyCb: srv.PtyCallback,
sessReqCb: srv.SessionRequestCallback,
ctx: ctx,

subsystemHandlers: srv.SubsystemHandlers,
}
sess.handleRequests(reqs)
}
Expand All @@ -110,21 +111,24 @@ type session struct {
ctx Context
sigCh chan<- Signal
sigBuf []Signal

subsystemHandlers map[string]SubsystemHandler
}

func (sess *session) Write(p []byte) (n int, err error) {
if sess.pty != nil {
m := len(p)
// normalize \n to \r\n when pty is accepted.
// this is a hardcoded shortcut since we don't support terminal modes.
p = bytes.Replace(p, []byte{'\n'}, []byte{'\r', '\n'}, -1)
p = bytes.Replace(p, []byte{'\r', '\r', '\n'}, []byte{'\r', '\n'}, -1)
n, err = sess.Channel.Write(p)
if n > m {
n = m
}
return
}
// If change the \n to \r\n, then zmodem(rzsz) will be error
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If do that replace, zmodem sz will be error

//if sess.pty != nil {
// m := len(p)
// // normalize \n to \r\n when pty is accepted.
// // this is a hardcoded shortcut since we don't support terminal modes.
// p = bytes.Replace(p, []byte{'\n'}, []byte{'\r', '\n'}, -1)
// p = bytes.Replace(p, []byte{'\r', '\r', '\n'}, []byte{'\r', '\n'}, -1)
// n, err = sess.Channel.Write(p)
// if n > m {
// n = m
// }
// return
//}
return sess.Channel.Write(p)
}

Expand Down Expand Up @@ -292,6 +296,21 @@ func (sess *session) handleRequests(reqs <-chan *gossh.Request) {
// TODO: option/callback to allow agent forwarding
SetAgentRequested(sess.ctx)
req.Reply(true, nil)
case "subsystem":
subname := string(req.Payload[4:])
handler, ok := sess.subsystemHandlers[subname]
if !ok {
req.Reply(false, nil)
continue
}
sess.handled = true
req.Reply(true, nil)

go func() {
handler(sess)
sess.Exit(0)
}()

default:
// TODO: debug log
req.Reply(false, nil)
Expand Down
3 changes: 3 additions & 0 deletions ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ type Option func(*Server) error
// Handler is a callback for handling established SSH sessions.
type Handler func(Session)

// SubsystemHandler is a callback for handling session subsystem request
type SubsystemHandler func(Session)

// PublicKeyHandler is a callback for performing public key authentication.
type PublicKeyHandler func(ctx Context, key PublicKey) bool

Expand Down