2013-11-05 08:25:17 +08:00
|
|
|
package sftp
|
|
|
|
|
2013-11-05 08:36:47 +08:00
|
|
|
import (
|
2016-06-13 12:45:13 +08:00
|
|
|
"bytes"
|
2014-09-26 04:40:48 +08:00
|
|
|
"encoding"
|
2016-06-13 12:45:13 +08:00
|
|
|
"encoding/binary"
|
2013-11-05 08:36:47 +08:00
|
|
|
"fmt"
|
2013-11-05 11:36:38 +08:00
|
|
|
"io"
|
2015-08-01 06:46:13 +08:00
|
|
|
"os"
|
2013-11-05 09:03:58 +08:00
|
|
|
"reflect"
|
2016-05-19 13:16:48 +08:00
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2013-11-05 08:36:47 +08:00
|
|
|
)
|
|
|
|
|
2015-09-07 10:36:47 +08:00
|
|
|
var (
|
2019-09-29 14:39:33 +08:00
|
|
|
errLongPacket = errors.New("packet too long")
|
2016-06-13 12:45:13 +08:00
|
|
|
errShortPacket = errors.New("packet too short")
|
|
|
|
errUnknownExtendedPacket = errors.New("unknown extended packet")
|
2015-09-07 14:55:15 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2019-09-29 14:39:33 +08:00
|
|
|
maxMsgLength = 256 * 1024
|
2015-09-07 10:36:47 +08:00
|
|
|
debugDumpTxPacket = false
|
|
|
|
debugDumpRxPacket = false
|
|
|
|
debugDumpTxPacketBytes = false
|
|
|
|
debugDumpRxPacketBytes = false
|
|
|
|
)
|
2015-07-25 16:19:29 +08:00
|
|
|
|
2013-11-05 08:25:17 +08:00
|
|
|
func marshalUint32(b []byte, v uint32) []byte {
|
|
|
|
return append(b, byte(v>>24), byte(v>>16), byte(v>>8), byte(v))
|
|
|
|
}
|
|
|
|
|
|
|
|
func marshalUint64(b []byte, v uint64) []byte {
|
2013-12-11 07:14:59 +08:00
|
|
|
return marshalUint32(marshalUint32(b, uint32(v>>32)), uint32(v))
|
2013-11-05 08:25:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func marshalString(b []byte, v string) []byte {
|
|
|
|
return append(marshalUint32(b, uint32(len(v))), v...)
|
|
|
|
}
|
2013-11-05 08:36:47 +08:00
|
|
|
|
|
|
|
func marshal(b []byte, v interface{}) []byte {
|
2015-08-01 06:46:13 +08:00
|
|
|
if v == nil {
|
|
|
|
return b
|
|
|
|
}
|
2013-11-05 08:36:47 +08:00
|
|
|
switch v := v.(type) {
|
2013-11-05 11:36:38 +08:00
|
|
|
case uint8:
|
|
|
|
return append(b, v)
|
2013-11-05 08:36:47 +08:00
|
|
|
case uint32:
|
|
|
|
return marshalUint32(b, v)
|
|
|
|
case uint64:
|
|
|
|
return marshalUint64(b, v)
|
|
|
|
case string:
|
|
|
|
return marshalString(b, v)
|
2015-08-01 06:46:13 +08:00
|
|
|
case os.FileInfo:
|
|
|
|
return marshalFileInfo(b, v)
|
2013-11-05 08:36:47 +08:00
|
|
|
default:
|
2013-11-05 09:03:58 +08:00
|
|
|
switch d := reflect.ValueOf(v); d.Kind() {
|
|
|
|
case reflect.Struct:
|
|
|
|
for i, n := 0, d.NumField(); i < n; i++ {
|
|
|
|
b = append(marshal(b, d.Field(i).Interface()))
|
|
|
|
}
|
|
|
|
return b
|
2013-11-05 11:36:38 +08:00
|
|
|
case reflect.Slice:
|
|
|
|
for i, n := 0, d.Len(); i < n; i++ {
|
|
|
|
b = append(marshal(b, d.Index(i).Interface()))
|
|
|
|
}
|
|
|
|
return b
|
2013-11-05 09:03:58 +08:00
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("marshal(%#v): cannot handle type %T", v, v))
|
|
|
|
}
|
2013-11-05 08:36:47 +08:00
|
|
|
}
|
|
|
|
}
|
2013-11-05 09:23:48 +08:00
|
|
|
|
|
|
|
func unmarshalUint32(b []byte) (uint32, []byte) {
|
|
|
|
v := uint32(b[3]) | uint32(b[2])<<8 | uint32(b[1])<<16 | uint32(b[0])<<24
|
|
|
|
return v, b[4:]
|
|
|
|
}
|
|
|
|
|
2015-07-25 16:19:29 +08:00
|
|
|
func unmarshalUint32Safe(b []byte) (uint32, []byte, error) {
|
2016-01-05 05:15:21 +08:00
|
|
|
var v uint32
|
2015-07-25 16:19:29 +08:00
|
|
|
if len(b) < 4 {
|
2016-01-05 05:15:21 +08:00
|
|
|
return 0, nil, errShortPacket
|
2015-07-25 16:19:29 +08:00
|
|
|
}
|
2015-09-08 13:50:46 +08:00
|
|
|
v, b = unmarshalUint32(b)
|
|
|
|
return v, b, nil
|
2015-07-25 16:19:29 +08:00
|
|
|
}
|
|
|
|
|
2013-11-05 09:23:48 +08:00
|
|
|
func unmarshalUint64(b []byte) (uint64, []byte) {
|
|
|
|
h, b := unmarshalUint32(b)
|
|
|
|
l, b := unmarshalUint32(b)
|
|
|
|
return uint64(h)<<32 | uint64(l), b
|
|
|
|
}
|
2013-11-05 09:31:20 +08:00
|
|
|
|
2015-07-25 16:19:29 +08:00
|
|
|
func unmarshalUint64Safe(b []byte) (uint64, []byte, error) {
|
2016-01-05 05:15:21 +08:00
|
|
|
var v uint64
|
2015-07-25 16:19:29 +08:00
|
|
|
if len(b) < 8 {
|
2016-01-05 05:15:21 +08:00
|
|
|
return 0, nil, errShortPacket
|
2015-07-25 16:19:29 +08:00
|
|
|
}
|
2015-09-08 13:50:46 +08:00
|
|
|
v, b = unmarshalUint64(b)
|
|
|
|
return v, b, nil
|
2015-07-25 16:19:29 +08:00
|
|
|
}
|
|
|
|
|
2013-11-05 09:31:20 +08:00
|
|
|
func unmarshalString(b []byte) (string, []byte) {
|
|
|
|
n, b := unmarshalUint32(b)
|
|
|
|
return string(b[:n]), b[n:]
|
|
|
|
}
|
2013-11-05 11:36:38 +08:00
|
|
|
|
2015-07-25 16:19:29 +08:00
|
|
|
func unmarshalStringSafe(b []byte) (string, []byte, error) {
|
|
|
|
n, b, err := unmarshalUint32Safe(b)
|
|
|
|
if err != nil {
|
|
|
|
return "", nil, err
|
|
|
|
}
|
|
|
|
if int64(n) > int64(len(b)) {
|
2016-01-05 05:15:21 +08:00
|
|
|
return "", nil, errShortPacket
|
2015-07-25 16:19:29 +08:00
|
|
|
}
|
|
|
|
return string(b[:n]), b[n:], nil
|
|
|
|
}
|
|
|
|
|
2013-11-05 11:36:38 +08:00
|
|
|
// sendPacket marshals p according to RFC 4234.
|
2014-09-26 04:40:48 +08:00
|
|
|
func sendPacket(w io.Writer, m encoding.BinaryMarshaler) error {
|
|
|
|
bb, err := m.MarshalBinary()
|
|
|
|
if err != nil {
|
2016-06-15 18:04:25 +08:00
|
|
|
return errors.Errorf("binary marshaller failed: %v", err)
|
2014-09-26 04:40:48 +08:00
|
|
|
}
|
2015-09-07 10:36:47 +08:00
|
|
|
if debugDumpTxPacketBytes {
|
2015-09-08 13:50:46 +08:00
|
|
|
debug("send packet: %s %d bytes %x", fxp(bb[0]), len(bb), bb[1:])
|
2015-09-07 10:36:47 +08:00
|
|
|
} else if debugDumpTxPacket {
|
2015-09-08 13:50:46 +08:00
|
|
|
debug("send packet: %s %d bytes", fxp(bb[0]), len(bb))
|
2015-09-07 10:36:47 +08:00
|
|
|
}
|
2018-05-12 12:44:57 +08:00
|
|
|
// Slide packet down 4 bytes to make room for length header.
|
|
|
|
packet := append(bb, make([]byte, 4)...) // optimistically assume bb has capacity
|
|
|
|
copy(packet[4:], bb)
|
|
|
|
binary.BigEndian.PutUint32(packet[:4], uint32(len(bb)))
|
|
|
|
|
|
|
|
_, err = w.Write(packet)
|
2016-06-15 18:04:25 +08:00
|
|
|
if err != nil {
|
2018-05-12 12:44:57 +08:00
|
|
|
return errors.Errorf("failed to send packet: %v", err)
|
2016-06-15 18:04:25 +08:00
|
|
|
}
|
|
|
|
return nil
|
2013-11-05 11:36:38 +08:00
|
|
|
}
|
|
|
|
|
2020-03-15 02:42:19 +08:00
|
|
|
func recvPacket(r io.Reader, alloc *allocator, orderID uint32) (uint8, []byte, error) {
|
|
|
|
var b []byte
|
|
|
|
if alloc != nil {
|
|
|
|
b = alloc.GetPage(orderID)
|
|
|
|
} else {
|
|
|
|
b = make([]byte, 4)
|
|
|
|
}
|
|
|
|
if _, err := io.ReadFull(r, b[:4]); err != nil {
|
2013-11-05 11:36:38 +08:00
|
|
|
return 0, nil, err
|
|
|
|
}
|
2020-03-18 16:36:07 +08:00
|
|
|
length, _ := unmarshalUint32(b)
|
|
|
|
if length > maxMsgLength {
|
|
|
|
debug("recv packet %d bytes too long", length)
|
2019-09-29 14:39:33 +08:00
|
|
|
return 0, nil, errLongPacket
|
|
|
|
}
|
2020-10-30 16:36:20 +08:00
|
|
|
if length == 0 {
|
|
|
|
debug("recv packet of 0 bytes too short")
|
|
|
|
return 0, nil, errShortPacket
|
|
|
|
}
|
2020-03-15 02:42:19 +08:00
|
|
|
if alloc == nil {
|
2020-03-18 16:36:07 +08:00
|
|
|
b = make([]byte, length)
|
2020-03-15 02:42:19 +08:00
|
|
|
}
|
2020-03-18 16:36:07 +08:00
|
|
|
if _, err := io.ReadFull(r, b[:length]); err != nil {
|
|
|
|
debug("recv packet %d bytes: err %v", length, err)
|
2013-11-05 11:36:38 +08:00
|
|
|
return 0, nil, err
|
|
|
|
}
|
2015-09-07 10:36:47 +08:00
|
|
|
if debugDumpRxPacketBytes {
|
2020-03-18 16:36:07 +08:00
|
|
|
debug("recv packet: %s %d bytes %x", fxp(b[0]), length, b[1:length])
|
2015-09-07 10:36:47 +08:00
|
|
|
} else if debugDumpRxPacket {
|
2020-03-18 16:36:07 +08:00
|
|
|
debug("recv packet: %s %d bytes", fxp(b[0]), length)
|
2015-09-07 10:36:47 +08:00
|
|
|
}
|
2020-03-18 16:36:07 +08:00
|
|
|
return b[0], b[1:length], nil
|
2013-11-05 11:36:38 +08:00
|
|
|
}
|
2014-09-26 04:40:48 +08:00
|
|
|
|
2016-01-08 04:11:16 +08:00
|
|
|
type extensionPair struct {
|
2015-07-25 16:19:29 +08:00
|
|
|
Name string
|
|
|
|
Data string
|
|
|
|
}
|
|
|
|
|
2016-01-08 04:11:16 +08:00
|
|
|
func unmarshalExtensionPair(b []byte) (extensionPair, []byte, error) {
|
|
|
|
var ep extensionPair
|
2016-01-05 05:15:21 +08:00
|
|
|
var err error
|
2015-07-25 16:19:29 +08:00
|
|
|
ep.Name, b, err = unmarshalStringSafe(b)
|
|
|
|
if err != nil {
|
|
|
|
return ep, b, err
|
|
|
|
}
|
|
|
|
ep.Data, b, err = unmarshalStringSafe(b)
|
|
|
|
return ep, b, err
|
|
|
|
}
|
|
|
|
|
2014-09-26 04:40:48 +08:00
|
|
|
// Here starts the definition of packets along with their MarshalBinary
|
|
|
|
// implementations.
|
|
|
|
// Manually writing the marshalling logic wins us a lot of time and
|
|
|
|
// allocation.
|
|
|
|
|
|
|
|
type sshFxInitPacket struct {
|
2015-07-25 16:19:29 +08:00
|
|
|
Version uint32
|
2016-01-08 04:11:16 +08:00
|
|
|
Extensions []extensionPair
|
2015-07-25 16:19:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p sshFxInitPacket) MarshalBinary() ([]byte, error) {
|
|
|
|
l := 1 + 4 // byte + uint32
|
|
|
|
for _, e := range p.Extensions {
|
|
|
|
l += 4 + len(e.Name) + 4 + len(e.Data)
|
|
|
|
}
|
|
|
|
|
|
|
|
b := make([]byte, 0, l)
|
2019-08-30 23:04:37 +08:00
|
|
|
b = append(b, sshFxpInit)
|
2015-07-25 16:19:29 +08:00
|
|
|
b = marshalUint32(b, p.Version)
|
|
|
|
for _, e := range p.Extensions {
|
|
|
|
b = marshalString(b, e.Name)
|
|
|
|
b = marshalString(b, e.Data)
|
|
|
|
}
|
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
2016-01-08 04:11:16 +08:00
|
|
|
func (p *sshFxInitPacket) UnmarshalBinary(b []byte) error {
|
|
|
|
var err error
|
2015-07-25 16:19:29 +08:00
|
|
|
if p.Version, b, err = unmarshalUint32Safe(b); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for len(b) > 0 {
|
2016-01-08 04:11:16 +08:00
|
|
|
var ep extensionPair
|
2015-07-25 16:19:29 +08:00
|
|
|
ep, b, err = unmarshalExtensionPair(b)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
p.Extensions = append(p.Extensions, ep)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type sshFxVersionPacket struct {
|
2014-09-26 04:40:48 +08:00
|
|
|
Version uint32
|
2019-05-25 03:23:18 +08:00
|
|
|
Extensions []sshExtensionPair
|
|
|
|
}
|
|
|
|
|
|
|
|
type sshExtensionPair struct {
|
|
|
|
Name, Data string
|
2014-09-26 04:40:48 +08:00
|
|
|
}
|
|
|
|
|
2015-07-25 16:19:29 +08:00
|
|
|
func (p sshFxVersionPacket) MarshalBinary() ([]byte, error) {
|
2014-09-26 04:40:48 +08:00
|
|
|
l := 1 + 4 // byte + uint32
|
|
|
|
for _, e := range p.Extensions {
|
|
|
|
l += 4 + len(e.Name) + 4 + len(e.Data)
|
|
|
|
}
|
|
|
|
|
|
|
|
b := make([]byte, 0, l)
|
2019-08-30 23:04:37 +08:00
|
|
|
b = append(b, sshFxpVersion)
|
2014-09-26 04:40:48 +08:00
|
|
|
b = marshalUint32(b, p.Version)
|
|
|
|
for _, e := range p.Extensions {
|
|
|
|
b = marshalString(b, e.Name)
|
|
|
|
b = marshalString(b, e.Data)
|
|
|
|
}
|
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
2016-01-05 05:15:21 +08:00
|
|
|
func marshalIDString(packetType byte, id uint32, str string) ([]byte, error) {
|
2014-09-26 04:40:48 +08:00
|
|
|
l := 1 + 4 + // type(byte) + uint32
|
|
|
|
4 + len(str)
|
|
|
|
|
2014-09-28 10:39:40 +08:00
|
|
|
b := make([]byte, 0, l)
|
2014-09-26 04:40:48 +08:00
|
|
|
b = append(b, packetType)
|
|
|
|
b = marshalUint32(b, id)
|
|
|
|
b = marshalString(b, str)
|
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
2016-01-05 05:15:21 +08:00
|
|
|
func unmarshalIDString(b []byte, id *uint32, str *string) error {
|
|
|
|
var err error
|
2015-07-25 16:19:29 +08:00
|
|
|
*id, b, err = unmarshalUint32Safe(b)
|
|
|
|
if err != nil {
|
2015-09-07 14:55:15 +08:00
|
|
|
return err
|
2015-07-25 16:19:29 +08:00
|
|
|
}
|
2018-02-16 03:00:22 +08:00
|
|
|
*str, _, err = unmarshalStringSafe(b)
|
2016-06-14 16:05:33 +08:00
|
|
|
return err
|
2015-07-25 16:19:29 +08:00
|
|
|
}
|
|
|
|
|
2014-09-26 04:40:48 +08:00
|
|
|
type sshFxpReaddirPacket struct {
|
2016-01-05 05:15:21 +08:00
|
|
|
ID uint32
|
2014-09-26 04:40:48 +08:00
|
|
|
Handle string
|
|
|
|
}
|
|
|
|
|
2016-01-05 05:15:21 +08:00
|
|
|
func (p sshFxpReaddirPacket) id() uint32 { return p.ID }
|
2015-08-06 03:57:28 +08:00
|
|
|
|
2014-09-26 04:40:48 +08:00
|
|
|
func (p sshFxpReaddirPacket) MarshalBinary() ([]byte, error) {
|
2019-08-30 23:04:37 +08:00
|
|
|
return marshalIDString(sshFxpReaddir, p.ID, p.Handle)
|
2014-09-26 04:40:48 +08:00
|
|
|
}
|
|
|
|
|
2015-08-01 06:46:13 +08:00
|
|
|
func (p *sshFxpReaddirPacket) UnmarshalBinary(b []byte) error {
|
2016-01-05 05:15:21 +08:00
|
|
|
return unmarshalIDString(b, &p.ID, &p.Handle)
|
2015-08-01 06:46:13 +08:00
|
|
|
}
|
|
|
|
|
2014-09-26 04:40:48 +08:00
|
|
|
type sshFxpOpendirPacket struct {
|
2016-01-05 05:15:21 +08:00
|
|
|
ID uint32
|
2014-09-26 04:40:48 +08:00
|
|
|
Path string
|
|
|
|
}
|
|
|
|
|
2016-01-05 05:15:21 +08:00
|
|
|
func (p sshFxpOpendirPacket) id() uint32 { return p.ID }
|
2015-08-06 03:57:28 +08:00
|
|
|
|
2014-09-26 04:40:48 +08:00
|
|
|
func (p sshFxpOpendirPacket) MarshalBinary() ([]byte, error) {
|
2019-08-30 23:04:37 +08:00
|
|
|
return marshalIDString(sshFxpOpendir, p.ID, p.Path)
|
2014-09-26 04:40:48 +08:00
|
|
|
}
|
|
|
|
|
2015-08-01 06:46:13 +08:00
|
|
|
func (p *sshFxpOpendirPacket) UnmarshalBinary(b []byte) error {
|
2016-01-05 05:15:21 +08:00
|
|
|
return unmarshalIDString(b, &p.ID, &p.Path)
|
2015-08-01 06:46:13 +08:00
|
|
|
}
|
|
|
|
|
2014-09-26 04:40:48 +08:00
|
|
|
type sshFxpLstatPacket struct {
|
2016-01-05 05:15:21 +08:00
|
|
|
ID uint32
|
2014-09-26 04:40:48 +08:00
|
|
|
Path string
|
|
|
|
}
|
|
|
|
|
2016-01-05 05:15:21 +08:00
|
|
|
func (p sshFxpLstatPacket) id() uint32 { return p.ID }
|
2015-05-23 20:51:29 +08:00
|
|
|
|
2014-09-26 04:40:48 +08:00
|
|
|
func (p sshFxpLstatPacket) MarshalBinary() ([]byte, error) {
|
2019-08-30 23:04:37 +08:00
|
|
|
return marshalIDString(sshFxpLstat, p.ID, p.Path)
|
2014-09-26 04:40:48 +08:00
|
|
|
}
|
|
|
|
|
2015-07-25 16:19:29 +08:00
|
|
|
func (p *sshFxpLstatPacket) UnmarshalBinary(b []byte) error {
|
2016-01-05 05:15:21 +08:00
|
|
|
return unmarshalIDString(b, &p.ID, &p.Path)
|
2015-07-25 16:19:29 +08:00
|
|
|
}
|
|
|
|
|
2015-08-06 03:57:28 +08:00
|
|
|
type sshFxpStatPacket struct {
|
2016-01-05 05:15:21 +08:00
|
|
|
ID uint32
|
2015-08-06 03:57:28 +08:00
|
|
|
Path string
|
|
|
|
}
|
|
|
|
|
2016-01-05 05:15:21 +08:00
|
|
|
func (p sshFxpStatPacket) id() uint32 { return p.ID }
|
2015-08-06 03:57:28 +08:00
|
|
|
|
|
|
|
func (p sshFxpStatPacket) MarshalBinary() ([]byte, error) {
|
2019-08-30 23:04:37 +08:00
|
|
|
return marshalIDString(sshFxpStat, p.ID, p.Path)
|
2015-08-06 03:57:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *sshFxpStatPacket) UnmarshalBinary(b []byte) error {
|
2016-01-05 05:15:21 +08:00
|
|
|
return unmarshalIDString(b, &p.ID, &p.Path)
|
2015-08-06 03:57:28 +08:00
|
|
|
}
|
|
|
|
|
2014-09-26 04:40:48 +08:00
|
|
|
type sshFxpFstatPacket struct {
|
2016-01-05 05:15:21 +08:00
|
|
|
ID uint32
|
2014-09-26 04:40:48 +08:00
|
|
|
Handle string
|
|
|
|
}
|
|
|
|
|
2016-01-05 05:15:21 +08:00
|
|
|
func (p sshFxpFstatPacket) id() uint32 { return p.ID }
|
2015-05-23 20:51:29 +08:00
|
|
|
|
2014-09-26 04:40:48 +08:00
|
|
|
func (p sshFxpFstatPacket) MarshalBinary() ([]byte, error) {
|
2019-08-30 23:04:37 +08:00
|
|
|
return marshalIDString(sshFxpFstat, p.ID, p.Handle)
|
2014-09-26 04:40:48 +08:00
|
|
|
}
|
|
|
|
|
2015-07-31 00:21:59 +08:00
|
|
|
func (p *sshFxpFstatPacket) UnmarshalBinary(b []byte) error {
|
2016-01-05 05:15:21 +08:00
|
|
|
return unmarshalIDString(b, &p.ID, &p.Handle)
|
2015-07-31 00:21:59 +08:00
|
|
|
}
|
|
|
|
|
2014-09-26 04:40:48 +08:00
|
|
|
type sshFxpClosePacket struct {
|
2016-01-05 05:15:21 +08:00
|
|
|
ID uint32
|
2014-09-26 04:40:48 +08:00
|
|
|
Handle string
|
|
|
|
}
|
|
|
|
|
2016-01-05 05:15:21 +08:00
|
|
|
func (p sshFxpClosePacket) id() uint32 { return p.ID }
|
2015-08-06 03:57:28 +08:00
|
|
|
|
2014-09-26 04:40:48 +08:00
|
|
|
func (p sshFxpClosePacket) MarshalBinary() ([]byte, error) {
|
2019-08-30 23:04:37 +08:00
|
|
|
return marshalIDString(sshFxpClose, p.ID, p.Handle)
|
2014-09-26 04:40:48 +08:00
|
|
|
}
|
|
|
|
|
2015-07-26 16:32:19 +08:00
|
|
|
func (p *sshFxpClosePacket) UnmarshalBinary(b []byte) error {
|
2016-01-05 05:15:21 +08:00
|
|
|
return unmarshalIDString(b, &p.ID, &p.Handle)
|
2015-07-26 16:32:19 +08:00
|
|
|
}
|
|
|
|
|
2014-09-26 04:40:48 +08:00
|
|
|
type sshFxpRemovePacket struct {
|
2016-01-05 05:15:21 +08:00
|
|
|
ID uint32
|
2014-09-26 04:40:48 +08:00
|
|
|
Filename string
|
|
|
|
}
|
|
|
|
|
2016-01-05 05:15:21 +08:00
|
|
|
func (p sshFxpRemovePacket) id() uint32 { return p.ID }
|
2015-05-23 20:51:29 +08:00
|
|
|
|
2014-09-26 04:40:48 +08:00
|
|
|
func (p sshFxpRemovePacket) MarshalBinary() ([]byte, error) {
|
2019-08-30 23:04:37 +08:00
|
|
|
return marshalIDString(sshFxpRemove, p.ID, p.Filename)
|
2014-09-26 04:40:48 +08:00
|
|
|
}
|
|
|
|
|
2015-08-01 06:46:13 +08:00
|
|
|
func (p *sshFxpRemovePacket) UnmarshalBinary(b []byte) error {
|
2016-01-05 05:15:21 +08:00
|
|
|
return unmarshalIDString(b, &p.ID, &p.Filename)
|
2015-08-01 06:46:13 +08:00
|
|
|
}
|
|
|
|
|
2014-09-26 04:40:48 +08:00
|
|
|
type sshFxpRmdirPacket struct {
|
2016-01-05 05:15:21 +08:00
|
|
|
ID uint32
|
2014-09-26 04:40:48 +08:00
|
|
|
Path string
|
|
|
|
}
|
|
|
|
|
2016-01-05 05:15:21 +08:00
|
|
|
func (p sshFxpRmdirPacket) id() uint32 { return p.ID }
|
2015-05-23 20:51:29 +08:00
|
|
|
|
2014-09-26 04:40:48 +08:00
|
|
|
func (p sshFxpRmdirPacket) MarshalBinary() ([]byte, error) {
|
2019-08-30 23:04:37 +08:00
|
|
|
return marshalIDString(sshFxpRmdir, p.ID, p.Path)
|
2014-09-26 04:40:48 +08:00
|
|
|
}
|
|
|
|
|
2015-08-01 06:46:13 +08:00
|
|
|
func (p *sshFxpRmdirPacket) UnmarshalBinary(b []byte) error {
|
2016-01-05 05:15:21 +08:00
|
|
|
return unmarshalIDString(b, &p.ID, &p.Path)
|
2015-08-01 06:46:13 +08:00
|
|
|
}
|
|
|
|
|
2015-09-07 16:05:16 +08:00
|
|
|
type sshFxpSymlinkPacket struct {
|
2016-01-05 05:15:21 +08:00
|
|
|
ID uint32
|
2015-09-07 16:05:16 +08:00
|
|
|
Targetpath string
|
|
|
|
Linkpath string
|
|
|
|
}
|
|
|
|
|
2016-01-05 05:15:21 +08:00
|
|
|
func (p sshFxpSymlinkPacket) id() uint32 { return p.ID }
|
2015-09-07 16:05:16 +08:00
|
|
|
|
|
|
|
func (p sshFxpSymlinkPacket) MarshalBinary() ([]byte, error) {
|
|
|
|
l := 1 + 4 + // type(byte) + uint32
|
|
|
|
4 + len(p.Targetpath) +
|
|
|
|
4 + len(p.Linkpath)
|
|
|
|
|
|
|
|
b := make([]byte, 0, l)
|
2019-08-30 23:04:37 +08:00
|
|
|
b = append(b, sshFxpSymlink)
|
2016-01-05 05:15:21 +08:00
|
|
|
b = marshalUint32(b, p.ID)
|
2015-09-07 16:05:16 +08:00
|
|
|
b = marshalString(b, p.Targetpath)
|
|
|
|
b = marshalString(b, p.Linkpath)
|
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *sshFxpSymlinkPacket) UnmarshalBinary(b []byte) error {
|
2016-01-05 05:15:21 +08:00
|
|
|
var err error
|
|
|
|
if p.ID, b, err = unmarshalUint32Safe(b); err != nil {
|
2015-09-07 16:05:16 +08:00
|
|
|
return err
|
|
|
|
} else if p.Targetpath, b, err = unmarshalStringSafe(b); err != nil {
|
|
|
|
return err
|
2018-02-16 03:00:22 +08:00
|
|
|
} else if p.Linkpath, _, err = unmarshalStringSafe(b); err != nil {
|
2015-09-07 16:05:16 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-05-25 03:23:18 +08:00
|
|
|
type sshFxpHardlinkPacket struct {
|
|
|
|
ID uint32
|
|
|
|
Oldpath string
|
|
|
|
Newpath string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p sshFxpHardlinkPacket) id() uint32 { return p.ID }
|
|
|
|
|
|
|
|
func (p sshFxpHardlinkPacket) MarshalBinary() ([]byte, error) {
|
|
|
|
const ext = "hardlink@openssh.com"
|
|
|
|
l := 1 + 4 + // type(byte) + uint32
|
|
|
|
4 + len(ext) +
|
|
|
|
4 + len(p.Oldpath) +
|
|
|
|
4 + len(p.Newpath)
|
|
|
|
|
|
|
|
b := make([]byte, 0, l)
|
2019-08-30 23:04:37 +08:00
|
|
|
b = append(b, sshFxpExtended)
|
2019-05-25 03:23:18 +08:00
|
|
|
b = marshalUint32(b, p.ID)
|
|
|
|
b = marshalString(b, ext)
|
|
|
|
b = marshalString(b, p.Oldpath)
|
|
|
|
b = marshalString(b, p.Newpath)
|
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
2014-09-28 09:57:44 +08:00
|
|
|
type sshFxpReadlinkPacket struct {
|
2016-01-05 05:15:21 +08:00
|
|
|
ID uint32
|
2014-09-28 09:57:44 +08:00
|
|
|
Path string
|
|
|
|
}
|
|
|
|
|
2016-01-05 05:15:21 +08:00
|
|
|
func (p sshFxpReadlinkPacket) id() uint32 { return p.ID }
|
2015-05-23 20:51:29 +08:00
|
|
|
|
2014-09-28 09:57:44 +08:00
|
|
|
func (p sshFxpReadlinkPacket) MarshalBinary() ([]byte, error) {
|
2019-08-30 23:04:37 +08:00
|
|
|
return marshalIDString(sshFxpReadlink, p.ID, p.Path)
|
2014-09-28 09:57:44 +08:00
|
|
|
}
|
|
|
|
|
2015-08-01 06:46:13 +08:00
|
|
|
func (p *sshFxpReadlinkPacket) UnmarshalBinary(b []byte) error {
|
2016-01-05 05:15:21 +08:00
|
|
|
return unmarshalIDString(b, &p.ID, &p.Path)
|
2015-08-01 06:46:13 +08:00
|
|
|
}
|
|
|
|
|
2015-08-06 03:57:28 +08:00
|
|
|
type sshFxpRealpathPacket struct {
|
2016-01-05 05:15:21 +08:00
|
|
|
ID uint32
|
2015-08-06 03:57:28 +08:00
|
|
|
Path string
|
|
|
|
}
|
|
|
|
|
2016-01-05 05:15:21 +08:00
|
|
|
func (p sshFxpRealpathPacket) id() uint32 { return p.ID }
|
2015-08-06 03:57:28 +08:00
|
|
|
|
|
|
|
func (p sshFxpRealpathPacket) MarshalBinary() ([]byte, error) {
|
2019-08-30 23:04:37 +08:00
|
|
|
return marshalIDString(sshFxpRealpath, p.ID, p.Path)
|
2015-08-06 03:57:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *sshFxpRealpathPacket) UnmarshalBinary(b []byte) error {
|
2016-01-05 05:15:21 +08:00
|
|
|
return unmarshalIDString(b, &p.ID, &p.Path)
|
2015-08-06 03:57:28 +08:00
|
|
|
}
|
|
|
|
|
2015-08-01 06:46:13 +08:00
|
|
|
type sshFxpNameAttr struct {
|
2015-08-06 03:57:28 +08:00
|
|
|
Name string
|
|
|
|
LongName string
|
|
|
|
Attrs []interface{}
|
2015-08-01 06:46:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p sshFxpNameAttr) MarshalBinary() ([]byte, error) {
|
|
|
|
b := []byte{}
|
|
|
|
b = marshalString(b, p.Name)
|
2015-08-06 03:57:28 +08:00
|
|
|
b = marshalString(b, p.LongName)
|
2015-08-01 14:09:51 +08:00
|
|
|
for _, attr := range p.Attrs {
|
|
|
|
b = marshal(b, attr)
|
|
|
|
}
|
2015-08-01 06:46:13 +08:00
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type sshFxpNamePacket struct {
|
2016-01-05 05:15:21 +08:00
|
|
|
ID uint32
|
2015-08-01 06:46:13 +08:00
|
|
|
NameAttrs []sshFxpNameAttr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p sshFxpNamePacket) MarshalBinary() ([]byte, error) {
|
|
|
|
b := []byte{}
|
2019-08-30 23:04:37 +08:00
|
|
|
b = append(b, sshFxpName)
|
2016-01-05 05:15:21 +08:00
|
|
|
b = marshalUint32(b, p.ID)
|
2015-08-01 06:46:13 +08:00
|
|
|
b = marshalUint32(b, uint32(len(p.NameAttrs)))
|
|
|
|
for _, na := range p.NameAttrs {
|
2016-01-05 05:15:21 +08:00
|
|
|
ab, err := na.MarshalBinary()
|
|
|
|
if err != nil {
|
2015-08-01 06:46:13 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
2016-01-05 05:15:21 +08:00
|
|
|
|
|
|
|
b = append(b, ab...)
|
2015-08-01 06:46:13 +08:00
|
|
|
}
|
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
2014-09-26 04:40:48 +08:00
|
|
|
type sshFxpOpenPacket struct {
|
2016-01-05 05:15:21 +08:00
|
|
|
ID uint32
|
2014-09-26 04:40:48 +08:00
|
|
|
Path string
|
|
|
|
Pflags uint32
|
|
|
|
Flags uint32 // ignored
|
|
|
|
}
|
|
|
|
|
2016-01-05 05:15:21 +08:00
|
|
|
func (p sshFxpOpenPacket) id() uint32 { return p.ID }
|
2015-05-23 20:51:29 +08:00
|
|
|
|
2014-09-26 04:40:48 +08:00
|
|
|
func (p sshFxpOpenPacket) MarshalBinary() ([]byte, error) {
|
|
|
|
l := 1 + 4 +
|
|
|
|
4 + len(p.Path) +
|
2014-09-28 11:00:42 +08:00
|
|
|
4 + 4
|
2014-09-26 04:40:48 +08:00
|
|
|
|
|
|
|
b := make([]byte, 0, l)
|
2019-08-30 23:04:37 +08:00
|
|
|
b = append(b, sshFxpOpen)
|
2016-01-05 05:15:21 +08:00
|
|
|
b = marshalUint32(b, p.ID)
|
2014-09-26 04:40:48 +08:00
|
|
|
b = marshalString(b, p.Path)
|
|
|
|
b = marshalUint32(b, p.Pflags)
|
|
|
|
b = marshalUint32(b, p.Flags)
|
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
2016-01-08 04:56:04 +08:00
|
|
|
func (p *sshFxpOpenPacket) UnmarshalBinary(b []byte) error {
|
|
|
|
var err error
|
2016-01-05 05:15:21 +08:00
|
|
|
if p.ID, b, err = unmarshalUint32Safe(b); err != nil {
|
2016-01-08 04:56:04 +08:00
|
|
|
return err
|
2015-07-26 16:32:19 +08:00
|
|
|
} else if p.Path, b, err = unmarshalStringSafe(b); err != nil {
|
2016-01-08 04:56:04 +08:00
|
|
|
return err
|
2015-07-26 16:32:19 +08:00
|
|
|
} else if p.Pflags, b, err = unmarshalUint32Safe(b); err != nil {
|
2016-01-08 04:56:04 +08:00
|
|
|
return err
|
2018-02-16 03:00:22 +08:00
|
|
|
} else if p.Flags, _, err = unmarshalUint32Safe(b); err != nil {
|
2016-01-08 04:56:04 +08:00
|
|
|
return err
|
2015-07-26 16:32:19 +08:00
|
|
|
}
|
2016-01-08 04:56:04 +08:00
|
|
|
return nil
|
2015-07-26 16:32:19 +08:00
|
|
|
}
|
|
|
|
|
2014-09-26 04:40:48 +08:00
|
|
|
type sshFxpReadPacket struct {
|
2016-01-05 05:15:21 +08:00
|
|
|
ID uint32
|
2014-09-26 04:40:48 +08:00
|
|
|
Len uint32
|
2020-09-08 16:18:15 +08:00
|
|
|
Offset uint64
|
|
|
|
Handle string
|
2014-09-26 04:40:48 +08:00
|
|
|
}
|
|
|
|
|
2016-01-05 05:15:21 +08:00
|
|
|
func (p sshFxpReadPacket) id() uint32 { return p.ID }
|
2015-05-23 20:51:29 +08:00
|
|
|
|
2014-09-26 04:40:48 +08:00
|
|
|
func (p sshFxpReadPacket) MarshalBinary() ([]byte, error) {
|
|
|
|
l := 1 + 4 + // type(byte) + uint32
|
|
|
|
4 + len(p.Handle) +
|
|
|
|
8 + 4 // uint64 + uint32
|
|
|
|
|
2014-09-28 10:39:40 +08:00
|
|
|
b := make([]byte, 0, l)
|
2019-08-30 23:04:37 +08:00
|
|
|
b = append(b, sshFxpRead)
|
2016-01-05 05:15:21 +08:00
|
|
|
b = marshalUint32(b, p.ID)
|
2014-09-26 04:40:48 +08:00
|
|
|
b = marshalString(b, p.Handle)
|
|
|
|
b = marshalUint64(b, p.Offset)
|
|
|
|
b = marshalUint32(b, p.Len)
|
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
2016-01-08 04:56:04 +08:00
|
|
|
func (p *sshFxpReadPacket) UnmarshalBinary(b []byte) error {
|
|
|
|
var err error
|
2016-01-05 05:15:21 +08:00
|
|
|
if p.ID, b, err = unmarshalUint32Safe(b); err != nil {
|
2016-01-08 04:56:04 +08:00
|
|
|
return err
|
2015-07-30 08:24:24 +08:00
|
|
|
} else if p.Handle, b, err = unmarshalStringSafe(b); err != nil {
|
2016-01-08 04:56:04 +08:00
|
|
|
return err
|
2015-07-30 08:24:24 +08:00
|
|
|
} else if p.Offset, b, err = unmarshalUint64Safe(b); err != nil {
|
2016-01-08 04:56:04 +08:00
|
|
|
return err
|
2018-02-16 03:00:22 +08:00
|
|
|
} else if p.Len, _, err = unmarshalUint32Safe(b); err != nil {
|
2016-01-08 04:56:04 +08:00
|
|
|
return err
|
2015-07-30 08:24:24 +08:00
|
|
|
}
|
2016-01-08 04:56:04 +08:00
|
|
|
return nil
|
2015-07-30 08:24:24 +08:00
|
|
|
}
|
|
|
|
|
2020-03-15 02:42:19 +08:00
|
|
|
func (p *sshFxpReadPacket) getDataSlice(alloc *allocator, orderID uint32) []byte {
|
2020-03-10 18:46:46 +08:00
|
|
|
dataLen := clamp(p.Len, maxTxPacket)
|
2020-03-15 02:42:19 +08:00
|
|
|
if alloc != nil {
|
2020-03-18 16:36:07 +08:00
|
|
|
// GetPage returns a slice with capacity = maxMsgLength this is enough to avoid new allocations in
|
|
|
|
// sshFxpDataPacket.MarshalBinary and sendPacket
|
2020-03-15 02:42:19 +08:00
|
|
|
return alloc.GetPage(orderID)[:dataLen]
|
|
|
|
}
|
2020-03-18 16:36:07 +08:00
|
|
|
// we allocate a slice with a bigger capacity so we avoid a new allocation in sshFxpDataPacket.MarshalBinary
|
|
|
|
// and in sendPacket, we need 9 bytes in MarshalBinary and 4 bytes in sendPacket.
|
2020-03-10 22:35:56 +08:00
|
|
|
return make([]byte, dataLen, dataLen+9+4)
|
2020-03-10 18:46:46 +08:00
|
|
|
}
|
|
|
|
|
2014-09-26 04:40:48 +08:00
|
|
|
type sshFxpRenamePacket struct {
|
2016-01-05 05:15:21 +08:00
|
|
|
ID uint32
|
2014-09-26 04:40:48 +08:00
|
|
|
Oldpath string
|
|
|
|
Newpath string
|
|
|
|
}
|
|
|
|
|
2016-01-05 05:15:21 +08:00
|
|
|
func (p sshFxpRenamePacket) id() uint32 { return p.ID }
|
2015-05-23 20:51:29 +08:00
|
|
|
|
2014-09-26 04:40:48 +08:00
|
|
|
func (p sshFxpRenamePacket) MarshalBinary() ([]byte, error) {
|
2014-09-28 10:41:49 +08:00
|
|
|
l := 1 + 4 + // type(byte) + uint32
|
2014-09-26 04:40:48 +08:00
|
|
|
4 + len(p.Oldpath) +
|
|
|
|
4 + len(p.Newpath)
|
|
|
|
|
2014-09-28 10:39:40 +08:00
|
|
|
b := make([]byte, 0, l)
|
2019-08-30 23:04:37 +08:00
|
|
|
b = append(b, sshFxpRename)
|
2016-01-05 05:15:21 +08:00
|
|
|
b = marshalUint32(b, p.ID)
|
2014-09-26 04:40:48 +08:00
|
|
|
b = marshalString(b, p.Oldpath)
|
|
|
|
b = marshalString(b, p.Newpath)
|
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
2016-01-08 04:56:04 +08:00
|
|
|
func (p *sshFxpRenamePacket) UnmarshalBinary(b []byte) error {
|
|
|
|
var err error
|
2016-01-05 05:15:21 +08:00
|
|
|
if p.ID, b, err = unmarshalUint32Safe(b); err != nil {
|
2016-01-08 04:56:04 +08:00
|
|
|
return err
|
2015-08-01 06:46:13 +08:00
|
|
|
} else if p.Oldpath, b, err = unmarshalStringSafe(b); err != nil {
|
2016-01-08 04:56:04 +08:00
|
|
|
return err
|
2018-02-16 03:00:22 +08:00
|
|
|
} else if p.Newpath, _, err = unmarshalStringSafe(b); err != nil {
|
2016-01-08 04:56:04 +08:00
|
|
|
return err
|
2015-08-01 06:46:13 +08:00
|
|
|
}
|
2016-01-08 04:56:04 +08:00
|
|
|
return nil
|
2015-08-01 06:46:13 +08:00
|
|
|
}
|
|
|
|
|
2017-09-05 19:46:03 +08:00
|
|
|
type sshFxpPosixRenamePacket struct {
|
|
|
|
ID uint32
|
|
|
|
Oldpath string
|
|
|
|
Newpath string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p sshFxpPosixRenamePacket) id() uint32 { return p.ID }
|
|
|
|
|
|
|
|
func (p sshFxpPosixRenamePacket) MarshalBinary() ([]byte, error) {
|
|
|
|
const ext = "posix-rename@openssh.com"
|
|
|
|
l := 1 + 4 + // type(byte) + uint32
|
|
|
|
4 + len(ext) +
|
|
|
|
4 + len(p.Oldpath) +
|
|
|
|
4 + len(p.Newpath)
|
|
|
|
|
|
|
|
b := make([]byte, 0, l)
|
2019-08-30 23:04:37 +08:00
|
|
|
b = append(b, sshFxpExtended)
|
2017-09-05 19:46:03 +08:00
|
|
|
b = marshalUint32(b, p.ID)
|
|
|
|
b = marshalString(b, ext)
|
|
|
|
b = marshalString(b, p.Oldpath)
|
|
|
|
b = marshalString(b, p.Newpath)
|
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
2014-09-26 04:40:48 +08:00
|
|
|
type sshFxpWritePacket struct {
|
2016-01-05 05:15:21 +08:00
|
|
|
ID uint32
|
2014-09-26 04:40:48 +08:00
|
|
|
Length uint32
|
2020-09-08 16:18:15 +08:00
|
|
|
Offset uint64
|
|
|
|
Handle string
|
2014-09-26 04:40:48 +08:00
|
|
|
Data []byte
|
|
|
|
}
|
|
|
|
|
2016-01-05 05:15:21 +08:00
|
|
|
func (p sshFxpWritePacket) id() uint32 { return p.ID }
|
2015-05-23 20:51:29 +08:00
|
|
|
|
2016-01-05 05:15:21 +08:00
|
|
|
func (p sshFxpWritePacket) MarshalBinary() ([]byte, error) {
|
2014-09-26 04:40:48 +08:00
|
|
|
l := 1 + 4 + // type(byte) + uint32
|
2016-01-05 05:15:21 +08:00
|
|
|
4 + len(p.Handle) +
|
2014-09-26 04:40:48 +08:00
|
|
|
8 + 4 + // uint64 + uint32
|
2016-01-05 05:15:21 +08:00
|
|
|
len(p.Data)
|
2014-09-26 04:40:48 +08:00
|
|
|
|
|
|
|
b := make([]byte, 0, l)
|
2019-08-30 23:04:37 +08:00
|
|
|
b = append(b, sshFxpWrite)
|
2016-01-05 05:15:21 +08:00
|
|
|
b = marshalUint32(b, p.ID)
|
|
|
|
b = marshalString(b, p.Handle)
|
|
|
|
b = marshalUint64(b, p.Offset)
|
|
|
|
b = marshalUint32(b, p.Length)
|
|
|
|
b = append(b, p.Data...)
|
2014-09-26 04:40:48 +08:00
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
2016-01-08 04:56:04 +08:00
|
|
|
func (p *sshFxpWritePacket) UnmarshalBinary(b []byte) error {
|
|
|
|
var err error
|
2016-01-05 05:15:21 +08:00
|
|
|
if p.ID, b, err = unmarshalUint32Safe(b); err != nil {
|
2016-01-08 04:56:04 +08:00
|
|
|
return err
|
2015-07-30 08:37:58 +08:00
|
|
|
} else if p.Handle, b, err = unmarshalStringSafe(b); err != nil {
|
2016-01-08 04:56:04 +08:00
|
|
|
return err
|
2015-07-30 08:37:58 +08:00
|
|
|
} else if p.Offset, b, err = unmarshalUint64Safe(b); err != nil {
|
2016-01-08 04:56:04 +08:00
|
|
|
return err
|
2015-07-30 08:37:58 +08:00
|
|
|
} else if p.Length, b, err = unmarshalUint32Safe(b); err != nil {
|
2016-01-08 04:56:04 +08:00
|
|
|
return err
|
2015-07-30 08:37:58 +08:00
|
|
|
} else if uint32(len(b)) < p.Length {
|
2016-01-08 04:56:04 +08:00
|
|
|
return errShortPacket
|
2015-07-30 08:37:58 +08:00
|
|
|
}
|
2016-01-05 05:15:21 +08:00
|
|
|
|
2020-02-26 19:04:13 +08:00
|
|
|
p.Data = b[:p.Length]
|
2016-01-08 04:56:04 +08:00
|
|
|
return nil
|
2015-07-30 08:37:58 +08:00
|
|
|
}
|
|
|
|
|
2014-09-26 04:40:48 +08:00
|
|
|
type sshFxpMkdirPacket struct {
|
2016-01-05 05:15:21 +08:00
|
|
|
ID uint32
|
2014-09-26 04:40:48 +08:00
|
|
|
Flags uint32 // ignored
|
2020-09-08 16:18:15 +08:00
|
|
|
Path string
|
2014-09-26 04:40:48 +08:00
|
|
|
}
|
|
|
|
|
2016-01-05 05:15:21 +08:00
|
|
|
func (p sshFxpMkdirPacket) id() uint32 { return p.ID }
|
2015-05-23 20:51:29 +08:00
|
|
|
|
2014-09-26 04:40:48 +08:00
|
|
|
func (p sshFxpMkdirPacket) MarshalBinary() ([]byte, error) {
|
|
|
|
l := 1 + 4 + // type(byte) + uint32
|
|
|
|
4 + len(p.Path) +
|
2014-10-10 02:40:02 +08:00
|
|
|
4 // uint32
|
2014-09-26 04:40:48 +08:00
|
|
|
|
|
|
|
b := make([]byte, 0, l)
|
2019-08-30 23:04:37 +08:00
|
|
|
b = append(b, sshFxpMkdir)
|
2016-01-05 05:15:21 +08:00
|
|
|
b = marshalUint32(b, p.ID)
|
2014-09-26 04:40:48 +08:00
|
|
|
b = marshalString(b, p.Path)
|
|
|
|
b = marshalUint32(b, p.Flags)
|
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
2016-01-08 04:56:04 +08:00
|
|
|
func (p *sshFxpMkdirPacket) UnmarshalBinary(b []byte) error {
|
|
|
|
var err error
|
2016-01-05 05:15:21 +08:00
|
|
|
if p.ID, b, err = unmarshalUint32Safe(b); err != nil {
|
2015-07-26 10:07:33 +08:00
|
|
|
return err
|
|
|
|
} else if p.Path, b, err = unmarshalStringSafe(b); err != nil {
|
|
|
|
return err
|
2018-02-16 03:00:22 +08:00
|
|
|
} else if p.Flags, _, err = unmarshalUint32Safe(b); err != nil {
|
2015-07-26 10:07:33 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-09-26 04:40:48 +08:00
|
|
|
type sshFxpSetstatPacket struct {
|
2016-01-05 05:15:21 +08:00
|
|
|
ID uint32
|
2014-09-26 04:40:48 +08:00
|
|
|
Flags uint32
|
2020-09-08 16:18:15 +08:00
|
|
|
Path string
|
2014-09-26 04:40:48 +08:00
|
|
|
Attrs interface{}
|
|
|
|
}
|
|
|
|
|
2015-09-07 17:13:07 +08:00
|
|
|
type sshFxpFsetstatPacket struct {
|
2016-01-05 05:15:21 +08:00
|
|
|
ID uint32
|
2015-09-07 17:13:07 +08:00
|
|
|
Flags uint32
|
2020-09-08 16:18:15 +08:00
|
|
|
Handle string
|
2015-09-07 17:13:07 +08:00
|
|
|
Attrs interface{}
|
|
|
|
}
|
|
|
|
|
2016-01-05 05:15:21 +08:00
|
|
|
func (p sshFxpSetstatPacket) id() uint32 { return p.ID }
|
|
|
|
func (p sshFxpFsetstatPacket) id() uint32 { return p.ID }
|
2015-05-23 20:51:29 +08:00
|
|
|
|
2014-09-26 04:40:48 +08:00
|
|
|
func (p sshFxpSetstatPacket) MarshalBinary() ([]byte, error) {
|
|
|
|
l := 1 + 4 + // type(byte) + uint32
|
|
|
|
4 + len(p.Path) +
|
|
|
|
4 // uint32 + uint64
|
|
|
|
|
|
|
|
b := make([]byte, 0, l)
|
2019-08-30 23:04:37 +08:00
|
|
|
b = append(b, sshFxpSetstat)
|
2016-01-05 05:15:21 +08:00
|
|
|
b = marshalUint32(b, p.ID)
|
2014-09-26 04:40:48 +08:00
|
|
|
b = marshalString(b, p.Path)
|
|
|
|
b = marshalUint32(b, p.Flags)
|
|
|
|
b = marshal(b, p.Attrs)
|
|
|
|
return b, nil
|
|
|
|
}
|
2015-07-26 16:32:19 +08:00
|
|
|
|
2015-09-07 17:13:07 +08:00
|
|
|
func (p sshFxpFsetstatPacket) MarshalBinary() ([]byte, error) {
|
|
|
|
l := 1 + 4 + // type(byte) + uint32
|
|
|
|
4 + len(p.Handle) +
|
|
|
|
4 // uint32 + uint64
|
|
|
|
|
|
|
|
b := make([]byte, 0, l)
|
2019-08-30 23:04:37 +08:00
|
|
|
b = append(b, sshFxpFsetstat)
|
2016-01-05 05:15:21 +08:00
|
|
|
b = marshalUint32(b, p.ID)
|
2015-09-07 17:13:07 +08:00
|
|
|
b = marshalString(b, p.Handle)
|
|
|
|
b = marshalUint32(b, p.Flags)
|
|
|
|
b = marshal(b, p.Attrs)
|
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *sshFxpSetstatPacket) UnmarshalBinary(b []byte) error {
|
2016-01-05 05:15:21 +08:00
|
|
|
var err error
|
|
|
|
if p.ID, b, err = unmarshalUint32Safe(b); err != nil {
|
2015-09-07 17:13:07 +08:00
|
|
|
return err
|
|
|
|
} else if p.Path, b, err = unmarshalStringSafe(b); err != nil {
|
|
|
|
return err
|
|
|
|
} else if p.Flags, b, err = unmarshalUint32Safe(b); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
p.Attrs = b
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *sshFxpFsetstatPacket) UnmarshalBinary(b []byte) error {
|
2016-01-05 05:15:21 +08:00
|
|
|
var err error
|
|
|
|
if p.ID, b, err = unmarshalUint32Safe(b); err != nil {
|
2015-09-07 17:13:07 +08:00
|
|
|
return err
|
|
|
|
} else if p.Handle, b, err = unmarshalStringSafe(b); err != nil {
|
|
|
|
return err
|
|
|
|
} else if p.Flags, b, err = unmarshalUint32Safe(b); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
p.Attrs = b
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-07-26 16:32:19 +08:00
|
|
|
type sshFxpHandlePacket struct {
|
2016-01-05 05:15:21 +08:00
|
|
|
ID uint32
|
2015-07-26 16:32:19 +08:00
|
|
|
Handle string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p sshFxpHandlePacket) MarshalBinary() ([]byte, error) {
|
2019-08-30 23:04:37 +08:00
|
|
|
b := []byte{sshFxpHandle}
|
2016-01-05 05:15:21 +08:00
|
|
|
b = marshalUint32(b, p.ID)
|
2015-07-26 16:32:19 +08:00
|
|
|
b = marshalString(b, p.Handle)
|
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type sshFxpStatusPacket struct {
|
2016-01-05 05:15:21 +08:00
|
|
|
ID uint32
|
2015-07-26 16:32:19 +08:00
|
|
|
StatusError
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p sshFxpStatusPacket) MarshalBinary() ([]byte, error) {
|
2019-08-30 23:04:37 +08:00
|
|
|
b := []byte{sshFxpStatus}
|
2016-01-05 05:15:21 +08:00
|
|
|
b = marshalUint32(b, p.ID)
|
2015-07-26 16:32:19 +08:00
|
|
|
b = marshalStatus(b, p.StatusError)
|
|
|
|
return b, nil
|
|
|
|
}
|
2015-07-30 08:24:24 +08:00
|
|
|
|
|
|
|
type sshFxpDataPacket struct {
|
2016-01-05 05:15:21 +08:00
|
|
|
ID uint32
|
2015-07-30 08:24:24 +08:00
|
|
|
Length uint32
|
|
|
|
Data []byte
|
|
|
|
}
|
|
|
|
|
2020-03-11 05:56:41 +08:00
|
|
|
// MarshalBinary encodes the receiver into a binary form and returns the result.
|
|
|
|
// To avoid a new allocation the Data slice must have a capacity >= Length + 9
|
2015-07-30 08:24:24 +08:00
|
|
|
func (p sshFxpDataPacket) MarshalBinary() ([]byte, error) {
|
2020-03-10 02:22:48 +08:00
|
|
|
b := append(p.Data, make([]byte, 9)...)
|
|
|
|
copy(b[9:], p.Data[:p.Length])
|
|
|
|
b[0] = sshFxpData
|
|
|
|
binary.BigEndian.PutUint32(b[1:5], p.ID)
|
|
|
|
binary.BigEndian.PutUint32(b[5:9], p.Length)
|
2015-07-30 08:24:24 +08:00
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
2016-01-05 05:15:21 +08:00
|
|
|
func (p *sshFxpDataPacket) UnmarshalBinary(b []byte) error {
|
|
|
|
var err error
|
|
|
|
if p.ID, b, err = unmarshalUint32Safe(b); err != nil {
|
2015-07-30 08:24:24 +08:00
|
|
|
return err
|
|
|
|
} else if p.Length, b, err = unmarshalUint32Safe(b); err != nil {
|
|
|
|
return err
|
|
|
|
} else if uint32(len(b)) < p.Length {
|
2019-08-26 09:21:33 +08:00
|
|
|
return errShortPacket
|
2015-07-30 08:24:24 +08:00
|
|
|
}
|
2016-01-05 05:15:21 +08:00
|
|
|
|
2020-02-16 06:50:08 +08:00
|
|
|
p.Data = b[:p.Length]
|
2016-01-05 05:15:21 +08:00
|
|
|
return nil
|
2015-07-30 08:24:24 +08:00
|
|
|
}
|
2015-08-05 12:11:01 +08:00
|
|
|
|
2015-05-16 02:37:52 +08:00
|
|
|
type sshFxpStatvfsPacket struct {
|
2016-01-05 05:15:21 +08:00
|
|
|
ID uint32
|
2015-05-16 02:37:52 +08:00
|
|
|
Path string
|
|
|
|
}
|
|
|
|
|
2016-01-05 05:15:21 +08:00
|
|
|
func (p sshFxpStatvfsPacket) id() uint32 { return p.ID }
|
2015-05-23 20:51:29 +08:00
|
|
|
|
2015-05-16 02:37:52 +08:00
|
|
|
func (p sshFxpStatvfsPacket) MarshalBinary() ([]byte, error) {
|
|
|
|
l := 1 + 4 + // type(byte) + uint32
|
2015-05-23 15:10:22 +08:00
|
|
|
len(p.Path) +
|
2015-05-16 02:37:52 +08:00
|
|
|
len("statvfs@openssh.com")
|
|
|
|
|
|
|
|
b := make([]byte, 0, l)
|
2019-08-30 23:04:37 +08:00
|
|
|
b = append(b, sshFxpExtended)
|
2016-01-05 05:15:21 +08:00
|
|
|
b = marshalUint32(b, p.ID)
|
2015-05-16 02:37:52 +08:00
|
|
|
b = marshalString(b, "statvfs@openssh.com")
|
|
|
|
b = marshalString(b, p.Path)
|
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
2016-01-08 04:11:16 +08:00
|
|
|
// A StatVFS contains statistics about a filesystem.
|
2015-05-16 02:37:52 +08:00
|
|
|
type StatVFS struct {
|
2016-01-05 05:15:21 +08:00
|
|
|
ID uint32
|
2015-05-23 15:10:22 +08:00
|
|
|
Bsize uint64 /* file system block size */
|
|
|
|
Frsize uint64 /* fundamental fs block size */
|
|
|
|
Blocks uint64 /* number of blocks (unit f_frsize) */
|
|
|
|
Bfree uint64 /* free blocks in file system */
|
|
|
|
Bavail uint64 /* free blocks for non-root */
|
|
|
|
Files uint64 /* total file inodes */
|
|
|
|
Ffree uint64 /* free file inodes */
|
|
|
|
Favail uint64 /* free file inodes for to non-root */
|
|
|
|
Fsid uint64 /* file system id */
|
|
|
|
Flag uint64 /* bit mask of f_flag values */
|
|
|
|
Namemax uint64 /* maximum filename length */
|
2015-05-16 02:37:52 +08:00
|
|
|
}
|
|
|
|
|
2016-01-08 04:11:16 +08:00
|
|
|
// TotalSpace calculates the amount of total space in a filesystem.
|
2015-05-16 02:37:52 +08:00
|
|
|
func (p *StatVFS) TotalSpace() uint64 {
|
|
|
|
return p.Frsize * p.Blocks
|
|
|
|
}
|
|
|
|
|
2016-01-08 04:11:16 +08:00
|
|
|
// FreeSpace calculates the amount of free space in a filesystem.
|
2015-05-16 02:37:52 +08:00
|
|
|
func (p *StatVFS) FreeSpace() uint64 {
|
|
|
|
return p.Frsize * p.Bfree
|
2015-05-23 15:10:22 +08:00
|
|
|
}
|
2016-06-13 12:45:13 +08:00
|
|
|
|
2019-08-30 23:04:37 +08:00
|
|
|
// MarshalBinary converts to ssh_FXP_EXTENDED_REPLY packet binary format
|
2016-06-13 12:45:13 +08:00
|
|
|
func (p *StatVFS) MarshalBinary() ([]byte, error) {
|
2016-06-14 16:05:33 +08:00
|
|
|
var buf bytes.Buffer
|
2019-08-30 23:04:37 +08:00
|
|
|
buf.Write([]byte{sshFxpExtendedReply})
|
2016-06-14 16:05:33 +08:00
|
|
|
err := binary.Write(&buf, binary.BigEndian, p)
|
|
|
|
return buf.Bytes(), err
|
2016-06-13 12:45:13 +08:00
|
|
|
}
|
|
|
|
|
2020-10-11 20:03:31 +08:00
|
|
|
type sshFxpFsyncPacket struct {
|
|
|
|
ID uint32
|
|
|
|
Handle string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p sshFxpFsyncPacket) id() uint32 { return p.ID }
|
|
|
|
|
|
|
|
func (p sshFxpFsyncPacket) MarshalBinary() ([]byte, error) {
|
|
|
|
l := 1 + 4 + // type (byte) + ID (uint32)
|
|
|
|
4 + len("fsync@openssh.com") +
|
|
|
|
4 + len(p.Handle)
|
|
|
|
|
|
|
|
b := make([]byte, 0, l)
|
|
|
|
b = append(b, sshFxpExtended)
|
|
|
|
b = marshalUint32(b, p.ID)
|
|
|
|
b = marshalString(b, "fsync@openssh.com")
|
|
|
|
b = marshalString(b, p.Handle)
|
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
2016-06-13 12:45:13 +08:00
|
|
|
type sshFxpExtendedPacket struct {
|
|
|
|
ID uint32
|
|
|
|
ExtendedRequest string
|
|
|
|
SpecificPacket interface {
|
|
|
|
serverRespondablePacket
|
|
|
|
readonly() bool
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-19 22:32:22 +08:00
|
|
|
func (p sshFxpExtendedPacket) id() uint32 { return p.ID }
|
|
|
|
func (p sshFxpExtendedPacket) readonly() bool {
|
|
|
|
if p.SpecificPacket == nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return p.SpecificPacket.readonly()
|
|
|
|
}
|
2016-06-13 12:45:13 +08:00
|
|
|
|
2018-07-26 05:54:02 +08:00
|
|
|
func (p sshFxpExtendedPacket) respond(svr *Server) responsePacket {
|
2018-03-19 22:32:22 +08:00
|
|
|
if p.SpecificPacket == nil {
|
2018-07-26 05:54:02 +08:00
|
|
|
return statusFromError(p, nil)
|
2018-03-19 22:32:22 +08:00
|
|
|
}
|
2016-06-13 12:45:13 +08:00
|
|
|
return p.SpecificPacket.respond(svr)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *sshFxpExtendedPacket) UnmarshalBinary(b []byte) error {
|
|
|
|
var err error
|
|
|
|
bOrig := b
|
|
|
|
if p.ID, b, err = unmarshalUint32Safe(b); err != nil {
|
|
|
|
return err
|
2018-02-16 03:00:22 +08:00
|
|
|
} else if p.ExtendedRequest, _, err = unmarshalStringSafe(b); err != nil {
|
2016-06-13 12:45:13 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// specific unmarshalling
|
|
|
|
switch p.ExtendedRequest {
|
|
|
|
case "statvfs@openssh.com":
|
|
|
|
p.SpecificPacket = &sshFxpExtendedPacketStatVFS{}
|
2017-09-05 19:46:03 +08:00
|
|
|
case "posix-rename@openssh.com":
|
|
|
|
p.SpecificPacket = &sshFxpExtendedPacketPosixRename{}
|
2019-05-25 03:23:18 +08:00
|
|
|
case "hardlink@openssh.com":
|
|
|
|
p.SpecificPacket = &sshFxpExtendedPacketHardlink{}
|
2016-06-13 12:45:13 +08:00
|
|
|
default:
|
2018-03-19 22:32:22 +08:00
|
|
|
return errors.Wrapf(errUnknownExtendedPacket, "packet type %v", p.SpecificPacket)
|
2016-06-13 12:45:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return p.SpecificPacket.UnmarshalBinary(bOrig)
|
|
|
|
}
|
|
|
|
|
|
|
|
type sshFxpExtendedPacketStatVFS struct {
|
|
|
|
ID uint32
|
|
|
|
ExtendedRequest string
|
|
|
|
Path string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p sshFxpExtendedPacketStatVFS) id() uint32 { return p.ID }
|
|
|
|
func (p sshFxpExtendedPacketStatVFS) readonly() bool { return true }
|
|
|
|
func (p *sshFxpExtendedPacketStatVFS) UnmarshalBinary(b []byte) error {
|
|
|
|
var err error
|
|
|
|
if p.ID, b, err = unmarshalUint32Safe(b); err != nil {
|
|
|
|
return err
|
|
|
|
} else if p.ExtendedRequest, b, err = unmarshalStringSafe(b); err != nil {
|
|
|
|
return err
|
2018-02-16 03:00:22 +08:00
|
|
|
} else if p.Path, _, err = unmarshalStringSafe(b); err != nil {
|
2016-06-13 12:45:13 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2017-09-05 19:46:03 +08:00
|
|
|
|
|
|
|
type sshFxpExtendedPacketPosixRename struct {
|
|
|
|
ID uint32
|
|
|
|
ExtendedRequest string
|
|
|
|
Oldpath string
|
|
|
|
Newpath string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p sshFxpExtendedPacketPosixRename) id() uint32 { return p.ID }
|
|
|
|
func (p sshFxpExtendedPacketPosixRename) readonly() bool { return false }
|
|
|
|
func (p *sshFxpExtendedPacketPosixRename) UnmarshalBinary(b []byte) error {
|
|
|
|
var err error
|
|
|
|
if p.ID, b, err = unmarshalUint32Safe(b); err != nil {
|
|
|
|
return err
|
|
|
|
} else if p.ExtendedRequest, b, err = unmarshalStringSafe(b); err != nil {
|
|
|
|
return err
|
|
|
|
} else if p.Oldpath, b, err = unmarshalStringSafe(b); err != nil {
|
|
|
|
return err
|
2018-02-16 03:00:22 +08:00
|
|
|
} else if p.Newpath, _, err = unmarshalStringSafe(b); err != nil {
|
2017-09-05 19:46:03 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-07-26 05:54:02 +08:00
|
|
|
func (p sshFxpExtendedPacketPosixRename) respond(s *Server) responsePacket {
|
2017-09-05 19:46:03 +08:00
|
|
|
err := os.Rename(p.Oldpath, p.Newpath)
|
2018-07-26 05:54:02 +08:00
|
|
|
return statusFromError(p, err)
|
2017-09-05 19:46:03 +08:00
|
|
|
}
|
2019-05-25 03:23:18 +08:00
|
|
|
|
|
|
|
type sshFxpExtendedPacketHardlink struct {
|
|
|
|
ID uint32
|
|
|
|
ExtendedRequest string
|
|
|
|
Oldpath string
|
|
|
|
Newpath string
|
|
|
|
}
|
|
|
|
|
|
|
|
// https://github.com/openssh/openssh-portable/blob/master/PROTOCOL
|
|
|
|
func (p sshFxpExtendedPacketHardlink) id() uint32 { return p.ID }
|
|
|
|
func (p sshFxpExtendedPacketHardlink) readonly() bool { return true }
|
|
|
|
func (p *sshFxpExtendedPacketHardlink) UnmarshalBinary(b []byte) error {
|
|
|
|
var err error
|
|
|
|
if p.ID, b, err = unmarshalUint32Safe(b); err != nil {
|
|
|
|
return err
|
|
|
|
} else if p.ExtendedRequest, b, err = unmarshalStringSafe(b); err != nil {
|
|
|
|
return err
|
|
|
|
} else if p.Oldpath, b, err = unmarshalStringSafe(b); err != nil {
|
|
|
|
return err
|
|
|
|
} else if p.Newpath, _, err = unmarshalStringSafe(b); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p sshFxpExtendedPacketHardlink) respond(s *Server) responsePacket {
|
|
|
|
err := os.Link(p.Oldpath, p.Newpath)
|
|
|
|
return statusFromError(p, err)
|
|
|
|
}
|