Russ Cox | 470549d | 2012-01-25 15:31:12 -0500 | [diff] [blame] | 1 | // Copyright 2011 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | package ssh |
| 6 | |
| 7 | import ( |
Russ Cox | 470549d | 2012-01-25 15:31:12 -0500 | [diff] [blame] | 8 | "errors" |
| 9 | "fmt" |
Russ Cox | 470549d | 2012-01-25 15:31:12 -0500 | [diff] [blame] | 10 | "net" |
| 11 | "sync" |
| 12 | ) |
| 13 | |
Adam Langley | fa50e74 | 2014-04-09 13:57:52 -0700 | [diff] [blame] | 14 | // Client implements a traditional SSH client that supports shells, |
| 15 | // subprocesses, port forwarding and tunneled dialing. |
| 16 | type Client struct { |
| 17 | Conn |
Han-Wen Nienhuys | afdc305 | 2013-06-21 12:46:35 -0400 | [diff] [blame] | 18 | |
Adam Langley | fa50e74 | 2014-04-09 13:57:52 -0700 | [diff] [blame] | 19 | forwards forwardList // forwarded tcpip connections from the remote side |
| 20 | mu sync.Mutex |
| 21 | channelHandlers map[string]chan NewChannel |
Dave Cheney | b4b4222 | 2012-05-01 15:43:58 +1000 | [diff] [blame] | 22 | } |
| 23 | |
Adam Langley | fa50e74 | 2014-04-09 13:57:52 -0700 | [diff] [blame] | 24 | // HandleChannelOpen returns a channel on which NewChannel requests |
| 25 | // for the given type are sent. If the type already is being handled, |
| 26 | // nil is returned. The channel is closed when the connection is closed. |
| 27 | func (c *Client) HandleChannelOpen(channelType string) <-chan NewChannel { |
| 28 | c.mu.Lock() |
| 29 | defer c.mu.Unlock() |
| 30 | if c.channelHandlers == nil { |
| 31 | // The SSH channel has been closed. |
| 32 | c := make(chan NewChannel) |
| 33 | close(c) |
| 34 | return c |
| 35 | } |
| 36 | |
| 37 | ch := c.channelHandlers[channelType] |
| 38 | if ch != nil { |
| 39 | return nil |
| 40 | } |
| 41 | |
| 42 | ch = make(chan NewChannel, 16) |
| 43 | c.channelHandlers[channelType] = ch |
| 44 | return ch |
Russ Cox | 470549d | 2012-01-25 15:31:12 -0500 | [diff] [blame] | 45 | } |
| 46 | |
Adam Langley | fa50e74 | 2014-04-09 13:57:52 -0700 | [diff] [blame] | 47 | // NewClient creates a Client on top of the given connection. |
| 48 | func NewClient(c Conn, chans <-chan NewChannel, reqs <-chan *Request) *Client { |
| 49 | conn := &Client{ |
| 50 | Conn: c, |
| 51 | channelHandlers: make(map[string]chan NewChannel, 1), |
| 52 | } |
| 53 | |
| 54 | go conn.handleGlobalRequests(reqs) |
| 55 | go conn.handleChannelOpens(chans) |
| 56 | go func() { |
| 57 | conn.Wait() |
| 58 | conn.forwards.closeAll() |
| 59 | }() |
| 60 | go conn.forwards.handleChannels(conn.HandleChannelOpen("forwarded-tcpip")) |
| 61 | return conn |
Han-Wen Nienhuys | afdc305 | 2013-06-21 12:46:35 -0400 | [diff] [blame] | 62 | } |
| 63 | |
Adam Langley | fa50e74 | 2014-04-09 13:57:52 -0700 | [diff] [blame] | 64 | // NewClientConn establishes an authenticated SSH connection using c |
| 65 | // as the underlying transport. The Request and NewChannel channels |
| 66 | // must be serviced or the connection will hang. |
| 67 | func NewClientConn(c net.Conn, addr string, config *ClientConfig) (Conn, <-chan NewChannel, <-chan *Request, error) { |
| 68 | fullConf := *config |
| 69 | fullConf.SetDefaults() |
| 70 | conn := &connection{ |
| 71 | sshConn: sshConn{conn: c}, |
Russ Cox | 470549d | 2012-01-25 15:31:12 -0500 | [diff] [blame] | 72 | } |
Han-Wen Nienhuys | afdc305 | 2013-06-21 12:46:35 -0400 | [diff] [blame] | 73 | |
Adam Langley | fa50e74 | 2014-04-09 13:57:52 -0700 | [diff] [blame] | 74 | if err := conn.clientHandshake(addr, &fullConf); err != nil { |
| 75 | c.Close() |
| 76 | return nil, nil, nil, fmt.Errorf("ssh: handshake failed: %v", err) |
Russ Cox | 470549d | 2012-01-25 15:31:12 -0500 | [diff] [blame] | 77 | } |
Adam Langley | fa50e74 | 2014-04-09 13:57:52 -0700 | [diff] [blame] | 78 | conn.mux = newMux(conn.transport) |
| 79 | return conn, conn.mux.incomingChannels, conn.mux.incomingRequests, nil |
Russ Cox | 470549d | 2012-01-25 15:31:12 -0500 | [diff] [blame] | 80 | } |
| 81 | |
Adam Langley | fa50e74 | 2014-04-09 13:57:52 -0700 | [diff] [blame] | 82 | // clientHandshake performs the client side key exchange. See RFC 4253 Section |
| 83 | // 7. |
| 84 | func (c *connection) clientHandshake(dialAddress string, config *ClientConfig) error { |
Adam Langley | fa50e74 | 2014-04-09 13:57:52 -0700 | [diff] [blame] | 85 | if config.ClientVersion != "" { |
| 86 | c.clientVersion = []byte(config.ClientVersion) |
Kristopher Watts | 280be00 | 2014-12-18 21:06:17 -0700 | [diff] [blame^] | 87 | } else { |
| 88 | c.clientVersion = []byte(packageVersion) |
JP Sugarbroad | c2c80b6 | 2013-08-27 13:40:08 -0400 | [diff] [blame] | 89 | } |
Adam Langley | fa50e74 | 2014-04-09 13:57:52 -0700 | [diff] [blame] | 90 | var err error |
| 91 | c.serverVersion, err = exchangeVersions(c.sshConn.conn, c.clientVersion) |
Russ Cox | 470549d | 2012-01-25 15:31:12 -0500 | [diff] [blame] | 92 | if err != nil { |
| 93 | return err |
| 94 | } |
| 95 | |
Adam Langley | fa50e74 | 2014-04-09 13:57:52 -0700 | [diff] [blame] | 96 | c.transport = newClientTransport( |
| 97 | newTransport(c.sshConn.conn, config.Rand, true /* is client */), |
| 98 | c.clientVersion, c.serverVersion, config, dialAddress, c.sshConn.RemoteAddr()) |
| 99 | if err := c.transport.requestKeyChange(); err != nil { |
Russ Cox | 470549d | 2012-01-25 15:31:12 -0500 | [diff] [blame] | 100 | return err |
| 101 | } |
| 102 | |
Adam Langley | fa50e74 | 2014-04-09 13:57:52 -0700 | [diff] [blame] | 103 | if packet, err := c.transport.readPacket(); err != nil { |
Russ Cox | 470549d | 2012-01-25 15:31:12 -0500 | [diff] [blame] | 104 | return err |
Adam Langley | fa50e74 | 2014-04-09 13:57:52 -0700 | [diff] [blame] | 105 | } else if packet[0] != msgNewKeys { |
| 106 | return unexpectedMessageError(msgNewKeys, packet[0]) |
Russ Cox | 470549d | 2012-01-25 15:31:12 -0500 | [diff] [blame] | 107 | } |
Adam Langley | fa50e74 | 2014-04-09 13:57:52 -0700 | [diff] [blame] | 108 | return c.clientAuthenticate(config) |
Russ Cox | 470549d | 2012-01-25 15:31:12 -0500 | [diff] [blame] | 109 | } |
| 110 | |
Adam Langley | fa50e74 | 2014-04-09 13:57:52 -0700 | [diff] [blame] | 111 | // verifyHostKeySignature verifies the host key obtained in the key |
| 112 | // exchange. |
| 113 | func verifyHostKeySignature(hostKey PublicKey, result *kexResult) error { |
| 114 | sig, rest, ok := parseSignatureBody(result.Signature) |
Han-Wen Nienhuys | d7d50b0 | 2013-08-28 10:50:25 -0400 | [diff] [blame] | 115 | if len(rest) > 0 || !ok { |
| 116 | return errors.New("ssh: signature parse error") |
| 117 | } |
Han-Wen Nienhuys | d7d50b0 | 2013-08-28 10:50:25 -0400 | [diff] [blame] | 118 | |
Adam Langley | fa50e74 | 2014-04-09 13:57:52 -0700 | [diff] [blame] | 119 | return hostKey.Verify(result.H, sig) |
Han-Wen Nienhuys | d7d50b0 | 2013-08-28 10:50:25 -0400 | [diff] [blame] | 120 | } |
| 121 | |
Adam Langley | fa50e74 | 2014-04-09 13:57:52 -0700 | [diff] [blame] | 122 | // NewSession opens a new Session for this client. (A session is a remote |
| 123 | // execution of a program.) |
| 124 | func (c *Client) NewSession() (*Session, error) { |
| 125 | ch, in, err := c.OpenChannel("session", nil) |
| 126 | if err != nil { |
Dave Cheney | b4b4222 | 2012-05-01 15:43:58 +1000 | [diff] [blame] | 127 | return nil, err |
| 128 | } |
Adam Langley | fa50e74 | 2014-04-09 13:57:52 -0700 | [diff] [blame] | 129 | return newSession(ch, in) |
Dave Cheney | b4b4222 | 2012-05-01 15:43:58 +1000 | [diff] [blame] | 130 | } |
| 131 | |
Adam Langley | fa50e74 | 2014-04-09 13:57:52 -0700 | [diff] [blame] | 132 | func (c *Client) handleGlobalRequests(incoming <-chan *Request) { |
| 133 | for r := range incoming { |
| 134 | // This handles keepalive messages and matches |
| 135 | // the behaviour of OpenSSH. |
| 136 | r.Reply(false, nil) |
Dave Cheney | b4b4222 | 2012-05-01 15:43:58 +1000 | [diff] [blame] | 137 | } |
Adam Langley | fa50e74 | 2014-04-09 13:57:52 -0700 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | // handleChannelOpens channel open messages from the remote side. |
| 141 | func (c *Client) handleChannelOpens(in <-chan NewChannel) { |
| 142 | for ch := range in { |
| 143 | c.mu.Lock() |
| 144 | handler := c.channelHandlers[ch.ChannelType()] |
| 145 | c.mu.Unlock() |
| 146 | |
| 147 | if handler != nil { |
| 148 | handler <- ch |
| 149 | } else { |
| 150 | ch.Reject(UnknownChannelType, fmt.Sprintf("unknown channel type: %v", ch.ChannelType())) |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | c.mu.Lock() |
| 155 | for _, ch := range c.channelHandlers { |
| 156 | close(ch) |
| 157 | } |
| 158 | c.channelHandlers = nil |
| 159 | c.mu.Unlock() |
Dave Cheney | b4b4222 | 2012-05-01 15:43:58 +1000 | [diff] [blame] | 160 | } |
| 161 | |
Adam Langley | fa50e74 | 2014-04-09 13:57:52 -0700 | [diff] [blame] | 162 | // Dial starts a client connection to the given SSH server. It is a |
| 163 | // convenience function that connects to the given network address, |
| 164 | // initiates the SSH handshake, and then sets up a Client. For access |
| 165 | // to incoming channels and requests, use net.Dial with NewClientConn |
| 166 | // instead. |
| 167 | func Dial(network, addr string, config *ClientConfig) (*Client, error) { |
Russ Cox | 470549d | 2012-01-25 15:31:12 -0500 | [diff] [blame] | 168 | conn, err := net.Dial(network, addr) |
| 169 | if err != nil { |
| 170 | return nil, err |
| 171 | } |
Adam Langley | fa50e74 | 2014-04-09 13:57:52 -0700 | [diff] [blame] | 172 | c, chans, reqs, err := NewClientConn(conn, addr, config) |
| 173 | if err != nil { |
| 174 | return nil, err |
| 175 | } |
| 176 | return NewClient(c, chans, reqs), nil |
Russ Cox | 470549d | 2012-01-25 15:31:12 -0500 | [diff] [blame] | 177 | } |
| 178 | |
Adam Langley | fa50e74 | 2014-04-09 13:57:52 -0700 | [diff] [blame] | 179 | // A ClientConfig structure is used to configure a Client. It must not be |
| 180 | // modified after having been passed to an SSH function. |
Russ Cox | 470549d | 2012-01-25 15:31:12 -0500 | [diff] [blame] | 181 | type ClientConfig struct { |
Adam Langley | fa50e74 | 2014-04-09 13:57:52 -0700 | [diff] [blame] | 182 | // Config contains configuration that is shared between clients and |
| 183 | // servers. |
| 184 | Config |
Russ Cox | 470549d | 2012-01-25 15:31:12 -0500 | [diff] [blame] | 185 | |
Adam Langley | fa50e74 | 2014-04-09 13:57:52 -0700 | [diff] [blame] | 186 | // User contains the username to authenticate as. |
Russ Cox | 470549d | 2012-01-25 15:31:12 -0500 | [diff] [blame] | 187 | User string |
| 188 | |
Adam Langley | fa50e74 | 2014-04-09 13:57:52 -0700 | [diff] [blame] | 189 | // Auth contains possible authentication methods to use with the |
| 190 | // server. Only the first instance of a particular RFC 4252 method will |
| 191 | // be used during authentication. |
| 192 | Auth []AuthMethod |
Russ Cox | 470549d | 2012-01-25 15:31:12 -0500 | [diff] [blame] | 193 | |
Adam Langley | fa50e74 | 2014-04-09 13:57:52 -0700 | [diff] [blame] | 194 | // HostKeyCallback, if not nil, is called during the cryptographic |
| 195 | // handshake to validate the server's host key. A nil HostKeyCallback |
Han-Wen Nienhuys | afdc305 | 2013-06-21 12:46:35 -0400 | [diff] [blame] | 196 | // implies that all host keys are accepted. |
Adam Langley | fa50e74 | 2014-04-09 13:57:52 -0700 | [diff] [blame] | 197 | HostKeyCallback func(hostname string, remote net.Addr, key PublicKey) error |
Han-Wen Nienhuys | afdc305 | 2013-06-21 12:46:35 -0400 | [diff] [blame] | 198 | |
Adam Langley | fa50e74 | 2014-04-09 13:57:52 -0700 | [diff] [blame] | 199 | // ClientVersion contains the version identification string that will |
| 200 | // be used for the connection. If empty, a reasonable default is used. |
JP Sugarbroad | c2c80b6 | 2013-08-27 13:40:08 -0400 | [diff] [blame] | 201 | ClientVersion string |
Russ Cox | 470549d | 2012-01-25 15:31:12 -0500 | [diff] [blame] | 202 | } |