From 5e0572a43f236c4a0f0aa80f88a5964f216a60d9 Mon Sep 17 00:00:00 2001 From: Boshi Lian Date: Tue, 5 Jul 2022 20:31:14 +0000 Subject: [PATCH] minor fix while doing docs --- Dockerfile | 14 ++++++-------- cmd/sshpiperd/daemon.go | 24 ++++++++++++++++++++++-- cmd/sshpiperd/internal/plugin/grpc.go | 10 +++++----- cmd/sshpiperd/main.go | 10 +--------- libplugin/pluginbase.go | 6 +++--- plugin/azdevicecode/main.go | 2 +- plugin/simplemath/main.go | 7 +++++-- 7 files changed, 43 insertions(+), 30 deletions(-) diff --git a/Dockerfile b/Dockerfile index 4f209595..e6baf43a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,13 +2,11 @@ FROM golang:1.18-stretch as builder ARG VER=devel -RUN mkdir -p /out +RUN mkdir -p /out/plugins ADD . /src/ -WORKDIR /src/cmd/sshpiperd -RUN CGO_ENABLED=0 go build -ldflags "-X main.mainver=$VER" -o /out/sshpiperd - -WORKDIR /src/plugin/workingdir -RUN CGO_ENABLED=0 go build -o /out/workingdir +WORKDIR /src +RUN CGO_ENABLED=0 go build -o /out -ldflags "-X main.mainver=$VER" ./cmd/... +RUN CGO_ENABLED=0 go build -o /out/plugins -ldflags "-X main.mainver=$VER" ./plugin/... FROM busybox LABEL maintainer="Boshi Lian" @@ -16,9 +14,9 @@ LABEL maintainer="Boshi Lian" RUN mkdir /etc/ssh/ ADD entrypoint.sh / -COPY --from=builder /out/* / +COPY --from=builder /out/ / COPY --from=ep76/openssh-static:latest /usr/bin/ssh-keygen /ssh-keygen EXPOSE 2222 ENTRYPOINT ["/entrypoint.sh"] -CMD ["/sshpiperd", "/workingdir"] +CMD ["/sshpiperd", "/plugins/workingdir"] diff --git a/cmd/sshpiperd/daemon.go b/cmd/sshpiperd/daemon.go index 15be02f6..8f603553 100644 --- a/cmd/sshpiperd/daemon.go +++ b/cmd/sshpiperd/daemon.go @@ -4,6 +4,8 @@ import ( "fmt" "io/ioutil" "net" + "os" + "path" "path/filepath" "time" @@ -18,7 +20,7 @@ type daemon struct { lis net.Listener loginGraceTime time.Duration - uphook, downhook func(msg []byte) ([]byte, error) + recorddir string } func newDaemon(ctx *cli.Context) (*daemon, error) { @@ -128,7 +130,25 @@ func (d *daemon) run() error { log.Infof("ssh connection pipe created %v -> %v", p.DownstreamConnMeta().RemoteAddr(), p.UpstreamConnMeta().RemoteAddr().String()) - err = p.WaitWithHook(d.uphook, d.downhook) + var uphook func([]byte) ([]byte, error) + if d.recorddir != "" { + recorddir := path.Join(d.recorddir, p.DownstreamConnMeta().User()) + err = os.MkdirAll(recorddir, 0700) + if err != nil { + log.Errorf("cannot create screen recording dir %v: %v", recorddir, err) + return + } + + recorder, err := newFilePtyLogger(recorddir) + if err != nil { + log.Errorf("cannot create screen recording logger: %v", err) + return + } + + uphook = recorder.loggingTty + } + + err = p.WaitWithHook(uphook, nil) log.Infof("connection from %v closed reason: %v", c.RemoteAddr(), err) }(conn) diff --git a/cmd/sshpiperd/internal/plugin/grpc.go b/cmd/sshpiperd/internal/plugin/grpc.go index ce772eb0..7c6ff16e 100644 --- a/cmd/sshpiperd/internal/plugin/grpc.go +++ b/cmd/sshpiperd/internal/plugin/grpc.go @@ -78,7 +78,7 @@ func (g *GrpcPlugin) InstallPiperConfig(config *ssh.PiperConfig) error { log.Debugf("downstream %v is sending none auth", conn.RemoteAddr().String()) u, err := g.NoneAuthCallback(conn, challengeCtx) if err != nil { - log.Debugf("cannot create upstream for %v with none auth: %v", conn.RemoteAddr().String(), err) + log.Errorf("cannot create upstream for %v with none auth: %v", conn.RemoteAddr().String(), err) } return u, err } @@ -87,7 +87,7 @@ func (g *GrpcPlugin) InstallPiperConfig(config *ssh.PiperConfig) error { log.Debugf("downstream %v is sending password auth", conn.RemoteAddr().String()) u, err := g.PasswordCallback(conn, password, challengeCtx) if err != nil { - log.Debugf("cannot create upstream for %v with password auth: %v", conn.RemoteAddr().String(), err) + log.Errorf("cannot create upstream for %v with password auth: %v", conn.RemoteAddr().String(), err) } return u, err } @@ -96,7 +96,7 @@ func (g *GrpcPlugin) InstallPiperConfig(config *ssh.PiperConfig) error { log.Debugf("downstream %v is sending public key auth", conn.RemoteAddr().String()) u, err := g.PublicKeyCallback(conn, key, challengeCtx) if err != nil { - log.Debugf("cannot create upstream for %v with public key auth: %v", conn.RemoteAddr().String(), err) + log.Errorf("cannot create upstream for %v with public key auth: %v", conn.RemoteAddr().String(), err) } return u, err } @@ -105,7 +105,7 @@ func (g *GrpcPlugin) InstallPiperConfig(config *ssh.PiperConfig) error { log.Debugf("downstream %v is sending keyboard interactive auth", conn.RemoteAddr().String()) u, err := g.KeyboardInteractiveCallback(conn, challenge, challengeCtx) if err != nil { - log.Debugf("cannot create upstream for %v with keyboard interactive auth: %v", conn.RemoteAddr().String(), err) + log.Errorf("cannot create upstream for %v with keyboard interactive auth: %v", conn.RemoteAddr().String(), err) } return u, err } @@ -418,7 +418,7 @@ func (g *GrpcPlugin) KeyboardInteractiveCallback(conn ssh.ConnMetadata, client s echo = append(echo, q.GetEcho()) } - ans, err := client(conn.User(), r.GetInstruction(), questions, echo) + ans, err := client(r.GetName(), r.GetInstruction(), questions, echo) if err != nil { return nil, err } diff --git a/cmd/sshpiperd/main.go b/cmd/sshpiperd/main.go index 91db4058..1db53898 100644 --- a/cmd/sshpiperd/main.go +++ b/cmd/sshpiperd/main.go @@ -164,15 +164,7 @@ func main() { return err } - recorddir := ctx.String("typescript-log-dir") - if recorddir != "" { - recorder, err := newFilePtyLogger(recorddir) - if err != nil { - return err - } - - d.uphook = recorder.loggingTty - } + d.recorddir = ctx.String("typescript-log-dir") return d.run() }, diff --git a/libplugin/pluginbase.go b/libplugin/pluginbase.go index 202076be..3454cf53 100644 --- a/libplugin/pluginbase.go +++ b/libplugin/pluginbase.go @@ -34,7 +34,7 @@ func (c *ConnMeta) UniqueID() string { return c.UniqId } -type KeyboardInteractiveChallenge func(instruction string, question string, echo bool) (answer string, err error) +type KeyboardInteractiveChallenge func(user, instruction string, question string, echo bool) (answer string, err error) type SshPiperPluginConfig struct { NewConnectionCallback func(conn ConnMetadata) error @@ -277,7 +277,7 @@ func (s *server) KeyboardInteractiveAuth(stream SshPiperPlugin_KeyboardInteracti return status.Errorf(codes.InvalidArgument, "missing meta") } - upstream, err := s.config.KeyboardInteractiveCallback(meta.Meta, func(instruction string, question string, echo bool) (answer string, err error) { + upstream, err := s.config.KeyboardInteractiveCallback(meta.Meta, func(user, instruction string, question string, echo bool) (answer string, err error) { var questions []*KeyboardInteractivePromptRequest_Question if question != "" { questions = append(questions, &KeyboardInteractivePromptRequest_Question{ @@ -289,7 +289,7 @@ func (s *server) KeyboardInteractiveAuth(stream SshPiperPlugin_KeyboardInteracti if err := stream.Send(&KeyboardInteractiveAuthMessage{ Message: &KeyboardInteractiveAuthMessage_PromptRequest{ PromptRequest: &KeyboardInteractivePromptRequest{ - Name: "", // temporary unused + Name: user, Instruction: instruction, Questions: questions, }, diff --git a/plugin/azdevicecode/main.go b/plugin/azdevicecode/main.go index 1b9fd487..cf70ab8c 100644 --- a/plugin/azdevicecode/main.go +++ b/plugin/azdevicecode/main.go @@ -50,7 +50,7 @@ func main() { TenantID: c.String("tenant-id"), ClientID: c.String("client-id"), UserPrompt: func(ctx context.Context, message azidentity.DeviceCodeMessage) error { - _, err := client(message.Message, "", false) + _, err := client("", message.Message, "", false) return err }, }) diff --git a/plugin/simplemath/main.go b/plugin/simplemath/main.go index 07571e2e..5b698b01 100644 --- a/plugin/simplemath/main.go +++ b/plugin/simplemath/main.go @@ -18,14 +18,14 @@ func main() { CreateConfig: func(_ *cli.Context) (*libplugin.SshPiperPluginConfig, error) { return &libplugin.SshPiperPluginConfig{ KeyboardInteractiveCallback: func(conn libplugin.ConnMetadata, client libplugin.KeyboardInteractiveChallenge) (*libplugin.Upstream, error) { - client("lets do math", "", false) + client("", "lets do math", "", false) for { a := rand.Intn(10) b := rand.Intn(10) - ans, err := client("", fmt.Sprintf("what is %v + %v = ", a, b), true) + ans, err := client("", "", fmt.Sprintf("what is %v + %v = ", a, b), true) if err != nil { return nil, err } @@ -33,6 +33,9 @@ func main() { log.Printf("got ans = %v", ans) if ans == fmt.Sprintf("%v", a+b) { + + log.Printf("got ans = %v", ans) + return &libplugin.Upstream{ Auth: libplugin.CreateNextPluginAuth(map[string]string{ "a": strconv.Itoa(a),