Skip to content

Commit

Permalink
ssh: add ServerConfig.NoClientAuthCallback
Browse files Browse the repository at this point in the history
It was possible to accept auth type "none" before, but not dynamically
at runtime as a function of the ConnMetadata like the other auth types'
callback hooks.

See golang/go#51994
and https://go-review.googlesource.com/c/crypto/+/395314

Change-Id: I83ea80901d4977d8f78523e3d1e16e0a7df5b172
(cherry picked from commit 4a431fab27b09acb1458fbb8709e12b2760e58a2)
  • Loading branch information
bradfitz committed Mar 30, 2022
1 parent d690bbf commit 6211952
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
14 changes: 13 additions & 1 deletion ssh/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,16 @@ type ServerConfig struct {

// NoClientAuth is true if clients are allowed to connect without
// authenticating.
// To determine NoClientAuth at runtime, set NoClientAuth to true
// and the optional NoClientAuthCallback to a non-nil value.
NoClientAuth bool

// NoClientAuthCallback, if non-nil, is called when a user
// attempts to authenticate with auth method "none".
// NoClientAuth must also be set to true for this be used, or
// this func is unused.
NoClientAuthCallback func(ConnMetadata) (*Permissions, error)

// MaxAuthTries specifies the maximum number of authentication attempts
// permitted per connection. If set to a negative number, the number of
// attempts are unlimited. If set to zero, the number of attempts are limited
Expand Down Expand Up @@ -455,7 +463,11 @@ userAuthLoop:
switch userAuthReq.Method {
case "none":
if config.NoClientAuth {
authErr = nil
if config.NoClientAuthCallback != nil {
perms, authErr = config.NoClientAuthCallback(s)
} else {
authErr = nil
}
}

// allow initial attempt of 'none' without penalty
Expand Down
51 changes: 51 additions & 0 deletions ssh/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -780,3 +780,54 @@ func TestHostKeyAlgorithms(t *testing.T) {
t.Fatal("succeeded connecting with unknown hostkey algorithm")
}
}

func TestServerClientAuthCallback(t *testing.T) {
c1, c2, err := netPipe()
if err != nil {
t.Fatalf("netPipe: %v", err)
}
defer c1.Close()
defer c2.Close()

userCh := make(chan string, 1)

serverConf := &ServerConfig{
NoClientAuth: true,
NoClientAuthCallback: func(conn ConnMetadata) (*Permissions, error) {
userCh <- conn.User()
return nil, nil
},
}
const someUsername = "some-username"

serverConf.AddHostKey(testSigners["ecdsa"])
clientConf := &ClientConfig{
HostKeyCallback: InsecureIgnoreHostKey(),
User: someUsername,
}

go func() {
_, chans, reqs, err := NewServerConn(c1, serverConf)
if err != nil {
t.Errorf("server handshake: %v", err)
userCh <- "error"
return
}
go DiscardRequests(reqs)
for ch := range chans {
ch.Reject(Prohibited, "")
}
}()

conn, _, _, err := NewClientConn(c2, "", clientConf)
if err != nil {
t.Fatalf("client handshake: %v", err)
return
}
conn.Close()

got := <-userCh
if got != someUsername {
t.Errorf("username = %q; want %q", got, someUsername)
}
}

0 comments on commit 6211952

Please sign in to comment.