Skip to content

Dev #4

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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
43 changes: 33 additions & 10 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type Server struct {
KeyboardInteractiveHandler KeyboardInteractiveHandler // keyboard-interactive authentication handler
PasswordHandler PasswordHandler // password authentication handler
PublicKeyHandler PublicKeyHandler // public key authentication handler
NextAuthMethodsHandler NextAuthMethodsHandler // next auth methods handler for 2 step auth
PtyCallback PtyCallback // callback for allowing PTY sessions, allows all if nil
ConnCallback ConnCallback // optional callback for wrapping net.Conn before handling
LocalPortForwardingCallback LocalPortForwardingCallback // callback for allowing local port forwarding, denies all if nil
Expand Down Expand Up @@ -90,37 +91,59 @@ func (srv *Server) config(ctx Context) *gossh.ServerConfig {
for _, signer := range srv.HostSigners {
config.AddHostKey(signer)
}
if srv.PasswordHandler == nil && srv.PublicKeyHandler == nil {
config.NoClientAuth = true
}
//if srv.PasswordHandler == nil && srv.PublicKeyHandler == nil {
// config.NoClientAuth = true
//}
if srv.Version != "" {
config.ServerVersion = "SSH-2.0-" + srv.Version
}
if srv.PasswordHandler != nil {
config.PasswordCallback = func(conn gossh.ConnMetadata, password []byte) (*gossh.Permissions, error) {
applyConnMetadata(ctx, conn)
if ok := srv.PasswordHandler(ctx, string(password)); !ok {
res := srv.PasswordHandler(ctx, string(password))
switch res {
case AuthSuccessful:
return ctx.Permissions().Permissions, nil
case AuthPartiallySuccessful:
return ctx.Permissions().Permissions, gossh.ErrPartialSuccess
default:
return ctx.Permissions().Permissions, fmt.Errorf("permission denied")
}
return ctx.Permissions().Permissions, nil
}
}
if srv.PublicKeyHandler != nil {
config.PublicKeyCallback = func(conn gossh.ConnMetadata, key gossh.PublicKey) (*gossh.Permissions, error) {
applyConnMetadata(ctx, conn)
if ok := srv.PublicKeyHandler(ctx, key); !ok {
res := srv.PublicKeyHandler(ctx, key)
switch res {
case AuthSuccessful:
ctx.SetValue(ContextKeyPublicKey, key)
return ctx.Permissions().Permissions, nil
case AuthPartiallySuccessful:
ctx.SetValue(ContextKeyPublicKey, key)
return ctx.Permissions().Permissions, gossh.ErrPartialSuccess
default:
return ctx.Permissions().Permissions, fmt.Errorf("permission denied")
}
ctx.SetValue(ContextKeyPublicKey, key)
return ctx.Permissions().Permissions, nil
}
}
if srv.KeyboardInteractiveHandler != nil {
config.KeyboardInteractiveCallback = func(conn gossh.ConnMetadata, challenger gossh.KeyboardInteractiveChallenge) (*gossh.Permissions, error) {
if ok := srv.KeyboardInteractiveHandler(ctx, challenger); !ok {
res := srv.KeyboardInteractiveHandler(ctx, challenger)
switch res {
case AuthSuccessful:
return ctx.Permissions().Permissions, nil
case AuthPartiallySuccessful:
return ctx.Permissions().Permissions, gossh.ErrPartialSuccess
default:
return ctx.Permissions().Permissions, fmt.Errorf("permission denied")
}
return ctx.Permissions().Permissions, nil
}
}
if srv.NextAuthMethodsHandler != nil {
config.NextAuthMethodsCallback = func(conn gossh.ConnMetadata) []string {
applyConnMetadata(ctx, conn)
return srv.NextAuthMethodsHandler(ctx)
}
}
return config
Expand Down
1 change: 1 addition & 0 deletions session.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@ func (sess *session) handleRequests(reqs <-chan *gossh.Request) {
handler, ok := sess.subsystemHandlers[subname]
if !ok {
req.Reply(false, nil)
continue
}
sess.handled = true
req.Reply(true, nil)
Expand Down
19 changes: 16 additions & 3 deletions ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ const (
SIGUSR2 Signal = "USR2"
)

// AuthResult, use some code except bool, to mark
// auth state
type AuthResult int

const (
AuthFailed AuthResult = iota
AuthSuccessful
AuthPartiallySuccessful
)

// DefaultHandler is the default Handler used by Serve.
var DefaultHandler Handler

Expand All @@ -39,13 +49,16 @@ type Handler func(Session)
type SubsystemHandler func(Session)

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

// PasswordHandler is a callback for performing password authentication.
type PasswordHandler func(ctx Context, password string) bool
type PasswordHandler func(ctx Context, password string) AuthResult

// KeyboardInteractiveHandler is a callback for performing keyboard-interactive authentication.
type KeyboardInteractiveHandler func(ctx Context, challenger gossh.KeyboardInteractiveChallenge) bool
type KeyboardInteractiveHandler func(ctx Context, challenger gossh.KeyboardInteractiveChallenge) AuthResult

// NextAuthMethodsHandler is a callback for performing 2 step auth
type NextAuthMethodsHandler func(ctx Context) []string

// PtyCallback is a hook for allowing PTY sessions.
type PtyCallback func(ctx Context, pty Pty) bool
Expand Down