From ac593cca3f18334b788c0f6dd6e6cbd2e6ba2f90 Mon Sep 17 00:00:00 2001 From: Boshi Lian Date: Mon, 27 Jun 2022 07:27:16 +0000 Subject: [PATCH] fix sub dir version --- sshpiperd/upstream/workingdir/config.go | 2 +- sshpiperd/upstream/workingdir/v1.go | 13 +++++++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/sshpiperd/upstream/workingdir/config.go b/sshpiperd/upstream/workingdir/config.go index eddee987..4ff12a0a 100644 --- a/sshpiperd/upstream/workingdir/config.go +++ b/sshpiperd/upstream/workingdir/config.go @@ -7,6 +7,6 @@ var ( NoCheckPerm bool `long:"upstream-workingdir-nocheckperm" description:"Disable 0400 checking when using files in the working directory" env:"SSHPIPERD_UPSTREAM_WORKINGDIR_NOCHECKPERM" ini-name:"upstream-workingdir-nocheckperm"` FallbackUsername string `long:"upstream-workingdir-fallbackusername" description:"Fallback to a user when user does not exists in directory" env:"SSHPIPERD_UPSTREAM_WORKINGDIR_FALLBACKUSERNAME" ini-name:"upstream-workingdir-fallbackusername"` StrictHostKey bool `long:"upstream-workingdir-stricthostkey" description:"Upstream host public key must be in known_hosts file, otherwise drop the connection" env:"SSHPIPERD_UPSTREAM_WORKINGDIR_STRICTHOSTKEY" ini-name:"upstream-workingdir-stricthostkey"` - MatchPublicKeyInSubDir bool `long:"upstream-workingdir-matchpublickeyinsubdir" description:"Remap user in user's sub dir with publickey" env:"SSHPIPERD_UPSTREAM_WORKINGDIR_MATCHPUBLICKeYINSUBDIR" ini-name:"upstream-workingdir-matchpublickeyinsubdir"` + MatchPublicKeyInSubDir bool `long:"upstream-workingdir-matchpublickeyinsubdir" description:"Remap user in user's sub dir with publickey" env:"SSHPIPERD_UPSTREAM_WORKINGDIR_MATCHPUBLICKEYINSUBDIR" ini-name:"upstream-workingdir-matchpublickeyinsubdir"` }{} ) diff --git a/sshpiperd/upstream/workingdir/v1.go b/sshpiperd/upstream/workingdir/v1.go index c3db53d0..d2430167 100644 --- a/sshpiperd/upstream/workingdir/v1.go +++ b/sshpiperd/upstream/workingdir/v1.go @@ -17,6 +17,15 @@ func (p *plugin) InstallUpstream(piper *ssh.PiperConfig) error { piper.PublicKeyCallback = p.matchPublicKeyInSubDir + if config.MatchPublicKeyInSubDir { + old := piper.NextAuthMethods + piper.NextAuthMethods = func(conn ssh.ConnMetadata, ctx ssh.ChallengeContext) ([]string, error) { + methods, err := old(conn, ctx) + methods = append(methods, "publickey") + return methods, err + } + } + return nil } @@ -102,7 +111,7 @@ func (p *plugin) matchPublicKeyInSubDir(conn ssh.ConnMetadata, key ssh.PublicKey // search in working dir filepath.Walk(config.WorkingDir, func(path string, info os.FileInfo, err error) error { - logger.Debug("search public key in path: %v", path) + logger.Debugf("search public key in path: %v", path) if err != nil { logger.Debug("error walking path: ", err) return nil @@ -114,7 +123,7 @@ func (p *plugin) matchPublicKeyInSubDir(conn ssh.ConnMetadata, key ssh.PublicKey u, err := p.matchPublicKeyDir(conn, key, conn.User(), path) if err != nil { - logger.Info("cannot map private key in %v: %v, search next", path, err) + logger.Infof("cannot map private key in %v: %v, search next", path, err) } if u != nil {