浏览代码

ssh: add ServerConfig.ServerVersion option

The SSH server does not allow for setting a version string in the same
manner as the client.  This update adds a ServerVersion member to the
ServerConfig structure which when set, causes the server to use that
member instead of the default version string.  This allows building
an golang based SSH server which can present any version string
the user wishes.

It also adds an else statement to the client assignment of the
ClientVersion to avoid an allocation when using a user defined
ClientVersion.

Change-Id: I43d97cfd5a174f2c68f53c5b4e267539ef21937b
Reviewed-on: https://go-review.googlesource.com/1860
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Kristopher Watts 11 年之前
父节点
当前提交
280be005b3
共有 2 个文件被更改,包括 12 次插入3 次删除
  1. 2 2
      ssh/client.go
  2. 10 1
      ssh/server.go

+ 2 - 2
ssh/client.go

@@ -82,11 +82,11 @@ func NewClientConn(c net.Conn, addr string, config *ClientConfig) (Conn, <-chan
 // clientHandshake performs the client side key exchange. See RFC 4253 Section
 // 7.
 func (c *connection) clientHandshake(dialAddress string, config *ClientConfig) error {
-	c.clientVersion = []byte(packageVersion)
 	if config.ClientVersion != "" {
 		c.clientVersion = []byte(config.ClientVersion)
+	} else {
+		c.clientVersion = []byte(packageVersion)
 	}
-
 	var err error
 	c.serverVersion, err = exchangeVersions(c.sshConn.conn, c.clientVersion)
 	if err != nil {

+ 10 - 1
ssh/server.go

@@ -65,6 +65,11 @@ type ServerConfig struct {
 	// AuthLogCallback, if non-nil, is called to log all authentication
 	// attempts.
 	AuthLogCallback func(conn ConnMetadata, method string, err error)
+
+	// ServerVersion is the version identification string to
+	// announce in the public handshake.
+	// If empty, a reasonable default is used.
+	ServerVersion string
 }
 
 // AddHostKey adds a private key as a host key. If an existing host
@@ -163,8 +168,12 @@ func (s *connection) serverHandshake(config *ServerConfig) (*Permissions, error)
 		return nil, errors.New("ssh: server has no host keys")
 	}
 
+	if config.ServerVersion != "" {
+		s.serverVersion = []byte(config.ServerVersion)
+	} else {
+		s.serverVersion = []byte(packageVersion)
+	}
 	var err error
-	s.serverVersion = []byte(packageVersion)
 	s.clientVersion, err = exchangeVersions(s.sshConn.conn, s.serverVersion)
 	if err != nil {
 		return nil, err