* Adding explicit MPL license for sub-package. This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Adding explicit MPL license for sub-package This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Updating the license from MPL to Business Source License Going forward, this project will be licensed under the Business Source License v1.1. Please see our blog post for more details at https://hashi.co/bsl-blog, FAQ at www.hashicorp.com/licensing-faq, and details of the license at www.hashicorp.com/bsl. * Update copyright file headers to BUS-1.1 * Rerun make gen This will pick up the last of the license changes * Revert "Rerun make gen" This reverts commit dbe0968bffd92745d8663c6940debae098395a6b. * Remove copywrite from generation script The current version of copywrite is not sophisticated enough to only apply the copywrite changes to certain subdirectories, so to avoid accidentally relicensing our api and sdk modules, we will stop running the copywrite script until such time that it can do the right thing. * License protobuf API as MPL The protobuf API files are used to generate the SDK, since we want the SDK to be MPL, the protobuf files must also be MPL. This extends to all protobuf files whose sources end up in the SDK directory, including: - internal/proto/controller/api - internal/proto/controller/custom_options - internal/proto/plugin It also includes the generated service directory in internal/gen/controller/api/services, since they are generated from the protobuf sources used for the SDK. * Change website license file name This makes the naming scheme consistent with the rest of the repository. * Add note on licensing to README * Set copywrite license to BUSL * Revert "Add note on licensing to README" This reverts commit 4d865a91a7a4f11605f0dc4609355d7e3c3cfffe. --------- Co-authored-by: hashicorp-copywrite[bot] <110428419+hashicorp-copywrite[bot]@users.noreply.github.com> Co-authored-by: Johan Brandhorst-Satzkorn <johan.brandhorst@gmail.com>
306 lines
8.2 KiB
Go
306 lines
8.2 KiB
Go
// Copyright (c) HashiCorp, Inc.
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
package ssh
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/hashicorp/boundary/internal/bsr"
|
|
pssh "github.com/hashicorp/boundary/internal/bsr/gen/ssh/v1"
|
|
"github.com/hashicorp/boundary/internal/bsr/internal/is"
|
|
"google.golang.org/protobuf/proto"
|
|
)
|
|
|
|
func init() {
|
|
for _, ct := range []bsr.ChunkType{
|
|
DataChunkType,
|
|
BreakReqChunkType,
|
|
CancelTCPIPForwardReqChunkType,
|
|
DirectTCPIPReqChunkType,
|
|
EnvReqChunkType,
|
|
ExecReqChunkType,
|
|
ExitSignalReqChunkType,
|
|
ExitStatusReqChunkType,
|
|
ForwardedTCPIPReqChunkType,
|
|
PtyReqChunkType,
|
|
SessionReqChunkType,
|
|
ShellReqChunkType,
|
|
SignalReqChunkType,
|
|
SubsystemReqChunkType,
|
|
TCPIPForwardReqChunkType,
|
|
WindowChangeReqChunkType,
|
|
X11ForwardingReqChunkType,
|
|
X11ReqChunkType,
|
|
XonXoffReqChunkType,
|
|
UnknownReqChunkType,
|
|
} {
|
|
if err := bsr.RegisterChunkType(Protocol, ct, DecodeChunk); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
}
|
|
|
|
const (
|
|
// Protocol is used to identify chunks that are recorded from SSH.
|
|
Protocol bsr.Protocol = "BSSH"
|
|
|
|
// MaxPacketSize is used by the DataWriter to determine if SSH data should
|
|
// be broken into multiple chunks.
|
|
MaxPacketSize = 256 * 1024
|
|
)
|
|
|
|
// Chunk types
|
|
const (
|
|
DataChunkType bsr.ChunkType = "DATA"
|
|
BreakReqChunkType bsr.ChunkType = "BREK"
|
|
CancelTCPIPForwardReqChunkType bsr.ChunkType = "CTPF"
|
|
DirectTCPIPReqChunkType bsr.ChunkType = "DTCP"
|
|
EnvReqChunkType bsr.ChunkType = "ENVR"
|
|
ExecReqChunkType bsr.ChunkType = "EXEC"
|
|
ExitSignalReqChunkType bsr.ChunkType = "EXSG"
|
|
ExitStatusReqChunkType bsr.ChunkType = "EXST"
|
|
ForwardedTCPIPReqChunkType bsr.ChunkType = "FTCP"
|
|
PtyReqChunkType bsr.ChunkType = "PTYR"
|
|
SessionReqChunkType bsr.ChunkType = "SESS"
|
|
ShellReqChunkType bsr.ChunkType = "SHLL"
|
|
SignalReqChunkType bsr.ChunkType = "SGNL"
|
|
SubsystemReqChunkType bsr.ChunkType = "SUBS"
|
|
TCPIPForwardReqChunkType bsr.ChunkType = "TCPF"
|
|
UnknownReqChunkType bsr.ChunkType = "UNKR"
|
|
WindowChangeReqChunkType bsr.ChunkType = "WCHG"
|
|
X11ForwardingReqChunkType bsr.ChunkType = "X11F"
|
|
X11ReqChunkType bsr.ChunkType = "X11R"
|
|
XonXoffReqChunkType bsr.ChunkType = "XOXO"
|
|
)
|
|
|
|
// DataChunk contains the raw byte data from an SSH session
|
|
type DataChunk struct {
|
|
*bsr.BaseChunk
|
|
Data []byte
|
|
}
|
|
|
|
// NewDataChunk constructs a DataChunk
|
|
func NewDataChunk(ctx context.Context, d bsr.Direction, t *bsr.Timestamp, data []byte) (*DataChunk, error) {
|
|
const op = "ssh.NewDataChunk"
|
|
|
|
baseChunk, err := bsr.NewBaseChunk(ctx, Protocol, d, t, DataChunkType)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("%s: unable to create base chunk: %w", op, err)
|
|
}
|
|
|
|
return &DataChunk{
|
|
BaseChunk: baseChunk,
|
|
Data: data,
|
|
}, nil
|
|
}
|
|
|
|
// MarshalData returns the data for a DataChunk
|
|
func (c *DataChunk) MarshalData(_ context.Context) ([]byte, error) {
|
|
return c.Data, nil
|
|
}
|
|
|
|
// DecodeChunk will decode any known SSH Chunk type. If the chunk type is
|
|
// not an ssh chunk type, and error is returned.
|
|
func DecodeChunk(_ context.Context, bc *bsr.BaseChunk, data []byte) (bsr.Chunk, error) {
|
|
const op = "ssh.DecodeChunk"
|
|
|
|
if is.Nil(bc) {
|
|
return nil, fmt.Errorf("%s: nil base chunk: %w", op, bsr.ErrInvalidParameter)
|
|
}
|
|
|
|
if bc.Protocol != Protocol {
|
|
return nil, fmt.Errorf("%s: invalid protocol %s", op, bc.Protocol)
|
|
}
|
|
|
|
switch bc.Type {
|
|
case DataChunkType:
|
|
return &DataChunk{
|
|
BaseChunk: bc,
|
|
Data: data,
|
|
}, nil
|
|
}
|
|
|
|
if len(data) <= 0 {
|
|
return nil, fmt.Errorf("%s: not enough data", op)
|
|
}
|
|
|
|
switch bc.Type {
|
|
case BreakReqChunkType:
|
|
mm := &pssh.BreakRequest{}
|
|
if err := proto.Unmarshal(data, mm); err != nil {
|
|
return nil, fmt.Errorf("%s: %w", op, err)
|
|
}
|
|
return &BreakRequest{
|
|
BaseChunk: bc,
|
|
BreakRequest: mm,
|
|
}, nil
|
|
case CancelTCPIPForwardReqChunkType:
|
|
mm := &pssh.CancelTCPIPForwardRequest{}
|
|
if err := proto.Unmarshal(data, mm); err != nil {
|
|
return nil, fmt.Errorf("%s: %w", op, err)
|
|
}
|
|
return &CancelTCPIPForwardRequest{
|
|
BaseChunk: bc,
|
|
CancelTCPIPForwardRequest: mm,
|
|
}, nil
|
|
case DirectTCPIPReqChunkType:
|
|
mm := &pssh.DirectTCPIPRequest{}
|
|
if err := proto.Unmarshal(data, mm); err != nil {
|
|
return nil, fmt.Errorf("%s: %w", op, err)
|
|
}
|
|
return &DirectTCPIPRequest{
|
|
BaseChunk: bc,
|
|
DirectTCPIPRequest: mm,
|
|
}, nil
|
|
case EnvReqChunkType:
|
|
mm := &pssh.EnvRequest{}
|
|
if err := proto.Unmarshal(data, mm); err != nil {
|
|
return nil, fmt.Errorf("%s: %w", op, err)
|
|
}
|
|
return &EnvRequest{
|
|
BaseChunk: bc,
|
|
EnvRequest: mm,
|
|
}, nil
|
|
case ExecReqChunkType:
|
|
mm := &pssh.ExecRequest{}
|
|
if err := proto.Unmarshal(data, mm); err != nil {
|
|
return nil, fmt.Errorf("%s: %w", op, err)
|
|
}
|
|
return &ExecRequest{
|
|
BaseChunk: bc,
|
|
ExecRequest: mm,
|
|
}, nil
|
|
case ExitSignalReqChunkType:
|
|
mm := &pssh.ExitSignalRequest{}
|
|
if err := proto.Unmarshal(data, mm); err != nil {
|
|
return nil, fmt.Errorf("%s: %w", op, err)
|
|
}
|
|
return &ExitSignalRequest{
|
|
BaseChunk: bc,
|
|
ExitSignalRequest: mm,
|
|
}, nil
|
|
case ExitStatusReqChunkType:
|
|
mm := &pssh.ExitStatusRequest{}
|
|
if err := proto.Unmarshal(data, mm); err != nil {
|
|
return nil, fmt.Errorf("%s: %w", op, err)
|
|
}
|
|
return &ExitStatusRequest{
|
|
BaseChunk: bc,
|
|
ExitStatusRequest: mm,
|
|
}, nil
|
|
case ForwardedTCPIPReqChunkType:
|
|
mm := &pssh.ForwardedTCPIPRequest{}
|
|
if err := proto.Unmarshal(data, mm); err != nil {
|
|
return nil, fmt.Errorf("%s: %w", op, err)
|
|
}
|
|
return &ForwardedTCPIPRequest{
|
|
BaseChunk: bc,
|
|
ForwardedTCPIPRequest: mm,
|
|
}, nil
|
|
case PtyReqChunkType:
|
|
mm := &pssh.PtyRequest{}
|
|
if err := proto.Unmarshal(data, mm); err != nil {
|
|
return nil, fmt.Errorf("%s: %w", op, err)
|
|
}
|
|
return &PtyRequest{
|
|
BaseChunk: bc,
|
|
PtyRequest: mm,
|
|
}, nil
|
|
case SessionReqChunkType:
|
|
mm := &pssh.SessionRequest{}
|
|
if err := proto.Unmarshal(data, mm); err != nil {
|
|
return nil, fmt.Errorf("%s: %w", op, err)
|
|
}
|
|
return &SessionRequest{
|
|
BaseChunk: bc,
|
|
SessionRequest: mm,
|
|
}, nil
|
|
case ShellReqChunkType:
|
|
mm := &pssh.ShellRequest{}
|
|
if err := proto.Unmarshal(data, mm); err != nil {
|
|
return nil, fmt.Errorf("%s: %w", op, err)
|
|
}
|
|
return &ShellRequest{
|
|
BaseChunk: bc,
|
|
ShellRequest: mm,
|
|
}, nil
|
|
case SignalReqChunkType:
|
|
mm := &pssh.SignalRequest{}
|
|
if err := proto.Unmarshal(data, mm); err != nil {
|
|
return nil, fmt.Errorf("%s: %w", op, err)
|
|
}
|
|
return &SignalRequest{
|
|
BaseChunk: bc,
|
|
SignalRequest: mm,
|
|
}, nil
|
|
case SubsystemReqChunkType:
|
|
mm := &pssh.SubsystemRequest{}
|
|
if err := proto.Unmarshal(data, mm); err != nil {
|
|
return nil, fmt.Errorf("%s: %w", op, err)
|
|
}
|
|
return &SubsystemRequest{
|
|
BaseChunk: bc,
|
|
SubsystemRequest: mm,
|
|
}, nil
|
|
case TCPIPForwardReqChunkType:
|
|
mm := &pssh.TCPIPForwardRequest{}
|
|
if err := proto.Unmarshal(data, mm); err != nil {
|
|
return nil, fmt.Errorf("%s: %w", op, err)
|
|
}
|
|
return &TCPIPForwardRequest{
|
|
BaseChunk: bc,
|
|
TCPIPForwardRequest: mm,
|
|
}, nil
|
|
case WindowChangeReqChunkType:
|
|
mm := &pssh.WindowChangeRequest{}
|
|
if err := proto.Unmarshal(data, mm); err != nil {
|
|
return nil, fmt.Errorf("%s: %w", op, err)
|
|
}
|
|
return &WindowChangeRequest{
|
|
BaseChunk: bc,
|
|
WindowChangeRequest: mm,
|
|
}, nil
|
|
case X11ForwardingReqChunkType:
|
|
mm := &pssh.X11ForwardingRequest{}
|
|
if err := proto.Unmarshal(data, mm); err != nil {
|
|
return nil, fmt.Errorf("%s: %w", op, err)
|
|
}
|
|
return &X11ForwardingRequest{
|
|
BaseChunk: bc,
|
|
X11ForwardingRequest: mm,
|
|
}, nil
|
|
case X11ReqChunkType:
|
|
mm := &pssh.X11Request{}
|
|
if err := proto.Unmarshal(data, mm); err != nil {
|
|
return nil, fmt.Errorf("%s: %w", op, err)
|
|
}
|
|
return &X11Request{
|
|
BaseChunk: bc,
|
|
X11Request: mm,
|
|
}, nil
|
|
case XonXoffReqChunkType:
|
|
mm := &pssh.XonXoffRequest{}
|
|
if err := proto.Unmarshal(data, mm); err != nil {
|
|
return nil, fmt.Errorf("%s: %w", op, err)
|
|
}
|
|
return &XonXoffRequest{
|
|
BaseChunk: bc,
|
|
XonXoffRequest: mm,
|
|
}, nil
|
|
case UnknownReqChunkType:
|
|
mm := &pssh.UnknownRequest{}
|
|
if err := proto.Unmarshal(data, mm); err != nil {
|
|
return nil, fmt.Errorf("%s: %w", op, err)
|
|
}
|
|
return &UnknownRequest{
|
|
BaseChunk: bc,
|
|
UnknownRequest: mm,
|
|
}, nil
|
|
|
|
default:
|
|
return nil, fmt.Errorf("%s: unsupported chunk type %s", op, bc.Type)
|
|
}
|
|
}
|