2014-12-29 04:06:09 +00:00
|
|
|
package sshrpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net"
|
|
|
|
"net/rpc"
|
|
|
|
|
|
|
|
"golang.org/x/crypto/ssh"
|
|
|
|
)
|
|
|
|
|
2015-04-05 10:41:07 +00:00
|
|
|
// DefaultRPCChannel is the Channel Name that will be used to carry the RPC traffic, can be changed in Serverr and Client
|
|
|
|
const DefaultRPCChannel = "RPCChannel"
|
|
|
|
|
|
|
|
// RPCSubsystem is the subsystem that will be used to trigger RPC endpoint creation
|
|
|
|
const RPCSubsystem = "RPCSubsystem"
|
|
|
|
|
2014-12-29 04:17:16 +00:00
|
|
|
// Server represents an SSH Server that spins up RPC servers when requested.
|
2014-12-29 04:06:09 +00:00
|
|
|
type Server struct {
|
|
|
|
*rpc.Server
|
2015-04-05 10:41:07 +00:00
|
|
|
Config *ssh.ServerConfig
|
|
|
|
ChannelName string
|
2014-12-29 04:06:09 +00:00
|
|
|
}
|
|
|
|
|
2014-12-29 04:17:16 +00:00
|
|
|
// NewServer returns a new Server to handle incoming SSH and RPC requests.
|
2014-12-29 04:06:09 +00:00
|
|
|
func NewServer() *Server {
|
|
|
|
c := &ssh.ServerConfig{
|
|
|
|
PasswordCallback: func(c ssh.ConnMetadata, pass []byte) (*ssh.Permissions, error) {
|
2014-12-29 04:17:16 +00:00
|
|
|
if c.User() == "sshrpc" && string(pass) == "sshrpc" {
|
2014-12-29 04:06:09 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("password rejected for %q", c.User())
|
|
|
|
},
|
|
|
|
}
|
2015-04-05 10:41:07 +00:00
|
|
|
return &Server{rpc.NewServer(), c, DefaultRPCChannel}
|
2014-12-29 04:06:09 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-12-29 04:17:16 +00:00
|
|
|
// StartServer starts the server listening for requests
|
2014-12-29 04:06:09 +00:00
|
|
|
func (s *Server) StartServer(address string) {
|
|
|
|
|
|
|
|
// Once a ServerConfig has been configured, connections can be accepted.
|
|
|
|
listener, err := net.Listen("tcp", address)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("failed to listen on ", address)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Accept all connections
|
|
|
|
log.Print("listening on ", address)
|
|
|
|
for {
|
|
|
|
tcpConn, err := listener.Accept()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("failed to accept incoming connection (%s)", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Before use, a handshake must be performed on the incoming net.Conn.
|
|
|
|
sshConn, chans, reqs, err := ssh.NewServerConn(tcpConn, s.Config)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("failed to handshake (%s)", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("new ssh connection from %s (%s)", sshConn.RemoteAddr(), sshConn.ClientVersion())
|
|
|
|
// Print incoming out-of-band Requests
|
|
|
|
go s.handleRequests(reqs)
|
|
|
|
// Accept all channels
|
|
|
|
go s.handleChannels(chans)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-05 10:41:07 +00:00
|
|
|
// handleRequests handles global out-of-band SSH Requests
|
2014-12-29 04:06:09 +00:00
|
|
|
func (s *Server) handleRequests(reqs <-chan *ssh.Request) {
|
|
|
|
for req := range reqs {
|
|
|
|
log.Printf("recieved out-of-band request: %+v", req)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-05 10:41:07 +00:00
|
|
|
// handleChannels handels SSH Channel requests and their local out-of-band SSH Requests
|
2014-12-29 04:06:09 +00:00
|
|
|
func (s *Server) handleChannels(chans <-chan ssh.NewChannel) {
|
|
|
|
// Service the incoming Channel channel.
|
|
|
|
for newChannel := range chans {
|
2015-04-05 10:41:07 +00:00
|
|
|
|
|
|
|
log.Printf("Received channel: %v", newChannel.ChannelType())
|
|
|
|
// Check the type of channel
|
|
|
|
if t := newChannel.ChannelType(); t != s.ChannelName {
|
2014-12-29 04:06:09 +00:00
|
|
|
newChannel.Reject(ssh.UnknownChannelType, fmt.Sprintf("unknown channel type: %s", t))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
channel, requests, err := newChannel.Accept()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("could not accept channel (%s)", err)
|
|
|
|
continue
|
|
|
|
}
|
2015-04-05 10:41:07 +00:00
|
|
|
log.Printf("Accepted channel")
|
2014-12-29 04:06:09 +00:00
|
|
|
|
2015-04-05 10:41:07 +00:00
|
|
|
// Channels can have out-of-band requests
|
2014-12-29 04:06:09 +00:00
|
|
|
go func(in <-chan *ssh.Request) {
|
|
|
|
for req := range in {
|
|
|
|
ok := false
|
|
|
|
switch req.Type {
|
|
|
|
|
|
|
|
case "subsystem":
|
|
|
|
ok = true
|
|
|
|
log.Printf("subsystem '%s'", req.Payload)
|
|
|
|
switch string(req.Payload[4:]) {
|
2015-04-05 10:41:07 +00:00
|
|
|
//RPCSubsystem Request made indicates client desires RPC Server access
|
|
|
|
case RPCSubsystem:
|
2014-12-29 04:06:09 +00:00
|
|
|
go s.ServeConn(channel)
|
|
|
|
log.Printf("Started SSH RPC")
|
|
|
|
default:
|
|
|
|
log.Printf("Unknown subsystem: %s", req.Payload)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
log.Printf("declining %s request...", req.Type)
|
|
|
|
}
|
|
|
|
req.Reply(ok, nil)
|
|
|
|
}
|
|
|
|
}(requests)
|
|
|
|
}
|
|
|
|
}
|